1 //===--------------------- Unwind_AppleExtras.cpp -------------------------===//
2 //
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
6 //
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "config.h"
11 #include "AddressSpace.hpp"
12 #include "DwarfParser.hpp"
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;
59 #elif defined(__arm64__)
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 
78 #if defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)
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 
117 #endif // defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)
118 
119 
120 
121 
122 #if defined(_LIBUNWIND_BUILD_SJLJ_APIS)
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 
142 #endif // defined(_LIBUNWIND_BUILD_SJLJ_APIS)
143 
144 
145 namespace libunwind {
146 
147 _LIBUNWIND_HIDDEN
checkKeyMgrRegisteredFDEs(uintptr_t pc,void * & fde)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 
184