Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1 | //===--------------------- Unwind_AppleExtras.cpp -------------------------===// |
| 2 | // |
Chandler Carruth | 61860a5 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 6 | // |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "config.h" |
Ed Schouten | edb7680 | 2017-03-09 08:04:07 +0000 | [diff] [blame] | 11 | #include "AddressSpace.hpp" |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 12 | #include "DwarfParser.hpp" |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 13 | |
| 14 | |
| 15 | // private keymgr stuff |
| 16 | #define KEYMGR_GCC3_DW2_OBJ_LIST 302 |
| 17 | extern "C" { |
| 18 | extern void _keymgr_set_and_unlock_processwide_ptr(int key, void *ptr); |
| 19 | extern void *_keymgr_get_and_lock_processwide_ptr(int key); |
| 20 | } |
| 21 | |
| 22 | // undocumented libgcc "struct object" |
| 23 | struct libgcc_object { |
| 24 | void *start; |
| 25 | void *unused1; |
| 26 | void *unused2; |
| 27 | void *fde; |
| 28 | unsigned long encoding; |
| 29 | void *fde_end; |
| 30 | libgcc_object *next; |
| 31 | }; |
| 32 | |
| 33 | // undocumented libgcc "struct km_object_info" referenced by |
| 34 | // KEYMGR_GCC3_DW2_OBJ_LIST |
| 35 | struct libgcc_object_info { |
| 36 | libgcc_object *seen_objects; |
| 37 | libgcc_object *unseen_objects; |
| 38 | unsigned spare[2]; |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | // static linker symbols to prevent wrong two level namespace for _Unwind symbols |
| 43 | #if defined(__arm__) |
| 44 | #define NOT_HERE_BEFORE_5_0(sym) \ |
| 45 | extern const char sym##_tmp30 __asm("$ld$hide$os3.0$_" #sym ); \ |
| 46 | __attribute__((visibility("default"))) const char sym##_tmp30 = 0; \ |
| 47 | extern const char sym##_tmp31 __asm("$ld$hide$os3.1$_" #sym ); \ |
| 48 | __attribute__((visibility("default"))) const char sym##_tmp31 = 0; \ |
| 49 | extern const char sym##_tmp32 __asm("$ld$hide$os3.2$_" #sym );\ |
| 50 | __attribute__((visibility("default"))) const char sym##_tmp32 = 0; \ |
| 51 | extern const char sym##_tmp40 __asm("$ld$hide$os4.0$_" #sym ); \ |
| 52 | __attribute__((visibility("default"))) const char sym##_tmp40 = 0; \ |
| 53 | extern const char sym##_tmp41 __asm("$ld$hide$os4.1$_" #sym ); \ |
| 54 | __attribute__((visibility("default"))) const char sym##_tmp41 = 0; \ |
| 55 | extern const char sym##_tmp42 __asm("$ld$hide$os4.2$_" #sym ); \ |
| 56 | __attribute__((visibility("default"))) const char sym##_tmp42 = 0; \ |
| 57 | extern const char sym##_tmp43 __asm("$ld$hide$os4.3$_" #sym ); \ |
| 58 | __attribute__((visibility("default"))) const char sym##_tmp43 = 0; |
Fangrui Song | 1b5cb74 | 2020-04-09 14:09:43 -0700 | [diff] [blame^] | 59 | #elif defined(__aarch64__) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 60 | #define NOT_HERE_BEFORE_10_6(sym) |
| 61 | #define NEVER_HERE(sym) |
| 62 | #else |
| 63 | #define NOT_HERE_BEFORE_10_6(sym) \ |
| 64 | extern const char sym##_tmp4 __asm("$ld$hide$os10.4$_" #sym ); \ |
| 65 | __attribute__((visibility("default"))) const char sym##_tmp4 = 0; \ |
| 66 | extern const char sym##_tmp5 __asm("$ld$hide$os10.5$_" #sym ); \ |
| 67 | __attribute__((visibility("default"))) const char sym##_tmp5 = 0; |
| 68 | #define NEVER_HERE(sym) \ |
| 69 | extern const char sym##_tmp4 __asm("$ld$hide$os10.4$_" #sym ); \ |
| 70 | __attribute__((visibility("default"))) const char sym##_tmp4 = 0; \ |
| 71 | extern const char sym##_tmp5 __asm("$ld$hide$os10.5$_" #sym ); \ |
| 72 | __attribute__((visibility("default"))) const char sym##_tmp5 = 0; \ |
| 73 | extern const char sym##_tmp6 __asm("$ld$hide$os10.6$_" #sym ); \ |
| 74 | __attribute__((visibility("default"))) const char sym##_tmp6 = 0; |
| 75 | #endif |
| 76 | |
| 77 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 78 | #if defined(_LIBUNWIND_BUILD_ZERO_COST_APIS) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 79 | |
| 80 | // |
| 81 | // symbols in libSystem.dylib in 10.6 and later, but are in libgcc_s.dylib in |
| 82 | // earlier versions |
| 83 | // |
| 84 | NOT_HERE_BEFORE_10_6(_Unwind_DeleteException) |
| 85 | NOT_HERE_BEFORE_10_6(_Unwind_Find_FDE) |
| 86 | NOT_HERE_BEFORE_10_6(_Unwind_ForcedUnwind) |
| 87 | NOT_HERE_BEFORE_10_6(_Unwind_GetGR) |
| 88 | NOT_HERE_BEFORE_10_6(_Unwind_GetIP) |
| 89 | NOT_HERE_BEFORE_10_6(_Unwind_GetLanguageSpecificData) |
| 90 | NOT_HERE_BEFORE_10_6(_Unwind_GetRegionStart) |
| 91 | NOT_HERE_BEFORE_10_6(_Unwind_RaiseException) |
| 92 | NOT_HERE_BEFORE_10_6(_Unwind_Resume) |
| 93 | NOT_HERE_BEFORE_10_6(_Unwind_SetGR) |
| 94 | NOT_HERE_BEFORE_10_6(_Unwind_SetIP) |
| 95 | NOT_HERE_BEFORE_10_6(_Unwind_Backtrace) |
| 96 | NOT_HERE_BEFORE_10_6(_Unwind_FindEnclosingFunction) |
| 97 | NOT_HERE_BEFORE_10_6(_Unwind_GetCFA) |
| 98 | NOT_HERE_BEFORE_10_6(_Unwind_GetDataRelBase) |
| 99 | NOT_HERE_BEFORE_10_6(_Unwind_GetTextRelBase) |
| 100 | NOT_HERE_BEFORE_10_6(_Unwind_Resume_or_Rethrow) |
| 101 | NOT_HERE_BEFORE_10_6(_Unwind_GetIPInfo) |
| 102 | NOT_HERE_BEFORE_10_6(__register_frame) |
| 103 | NOT_HERE_BEFORE_10_6(__deregister_frame) |
| 104 | |
| 105 | // |
| 106 | // symbols in libSystem.dylib for compatibility, but we don't want any new code |
| 107 | // using them |
| 108 | // |
| 109 | NEVER_HERE(__register_frame_info_bases) |
| 110 | NEVER_HERE(__register_frame_info) |
| 111 | NEVER_HERE(__register_frame_info_table_bases) |
| 112 | NEVER_HERE(__register_frame_info_table) |
| 113 | NEVER_HERE(__register_frame_table) |
| 114 | NEVER_HERE(__deregister_frame_info) |
| 115 | NEVER_HERE(__deregister_frame_info_bases) |
| 116 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 117 | #endif // defined(_LIBUNWIND_BUILD_ZERO_COST_APIS) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | |
| 121 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 122 | #if defined(_LIBUNWIND_BUILD_SJLJ_APIS) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 123 | // |
| 124 | // symbols in libSystem.dylib in iOS 5.0 and later, but are in libgcc_s.dylib in |
| 125 | // earlier versions |
| 126 | // |
| 127 | NOT_HERE_BEFORE_5_0(_Unwind_GetLanguageSpecificData) |
| 128 | NOT_HERE_BEFORE_5_0(_Unwind_GetRegionStart) |
| 129 | NOT_HERE_BEFORE_5_0(_Unwind_GetIP) |
| 130 | NOT_HERE_BEFORE_5_0(_Unwind_SetGR) |
| 131 | NOT_HERE_BEFORE_5_0(_Unwind_SetIP) |
| 132 | NOT_HERE_BEFORE_5_0(_Unwind_DeleteException) |
| 133 | NOT_HERE_BEFORE_5_0(_Unwind_SjLj_Register) |
| 134 | NOT_HERE_BEFORE_5_0(_Unwind_GetGR) |
| 135 | NOT_HERE_BEFORE_5_0(_Unwind_GetIPInfo) |
| 136 | NOT_HERE_BEFORE_5_0(_Unwind_GetCFA) |
| 137 | NOT_HERE_BEFORE_5_0(_Unwind_SjLj_Resume) |
| 138 | NOT_HERE_BEFORE_5_0(_Unwind_SjLj_RaiseException) |
| 139 | NOT_HERE_BEFORE_5_0(_Unwind_SjLj_Resume_or_Rethrow) |
| 140 | NOT_HERE_BEFORE_5_0(_Unwind_SjLj_Unregister) |
| 141 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 142 | #endif // defined(_LIBUNWIND_BUILD_SJLJ_APIS) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 143 | |
| 144 | |
| 145 | namespace libunwind { |
| 146 | |
| 147 | _LIBUNWIND_HIDDEN |
| 148 | bool checkKeyMgrRegisteredFDEs(uintptr_t pc, void *&fde) { |
| 149 | #if __MAC_OS_X_VERSION_MIN_REQUIRED |
| 150 | // lastly check for old style keymgr registration of dynamically generated |
| 151 | // FDEs acquire exclusive access to libgcc_object_info |
| 152 | libgcc_object_info *head = (libgcc_object_info *) |
| 153 | _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST); |
| 154 | if (head != NULL) { |
| 155 | // look at each FDE in keymgr |
| 156 | for (libgcc_object *ob = head->unseen_objects; ob != NULL; ob = ob->next) { |
| 157 | CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; |
| 158 | CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; |
| 159 | const char *msg = CFI_Parser<LocalAddressSpace>::decodeFDE( |
| 160 | LocalAddressSpace::sThisAddressSpace, |
| 161 | (uintptr_t)ob->fde, &fdeInfo, &cieInfo); |
| 162 | if (msg == NULL) { |
| 163 | // Check if this FDE is for a function that includes the pc |
| 164 | if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) { |
| 165 | fde = (void*)fdeInfo.pcStart; |
| 166 | _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST, |
| 167 | head); |
| 168 | return true; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | // release libgcc_object_info |
| 174 | _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST, head); |
| 175 | #else |
| 176 | (void)pc; |
| 177 | (void)fde; |
| 178 | #endif |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | } |
| 183 | |