1 //===--------------------- UnwindLevel1-gcc-ext.c -------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 // 9 // Implements gcc extensions to the C++ ABI Exception Handling Level 1. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include <inttypes.h> 14 #include <stdbool.h> 15 #include <stdint.h> 16 #include <stdio.h> 17 #include <stdlib.h> 18 19 #include "config.h" 20 #include "libunwind_ext.h" 21 #include "libunwind.h" 22 #include "Unwind-EHABI.h" 23 #include "unwind.h" 24 25 #if _LIBUNWIND_BUILD_ZERO_COST_APIS 26 27 /// Called by __cxa_rethrow(). 28 _LIBUNWIND_EXPORT _Unwind_Reason_Code 29 _Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object) { 30 #if LIBCXXABI_ARM_EHABI 31 _LIBUNWIND_TRACE_API("_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%ld\n", 32 (void *)exception_object, 33 (long)exception_object->unwinder_cache.reserved1); 34 #else 35 _LIBUNWIND_TRACE_API("_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%ld\n", 36 (void *)exception_object, 37 (long)exception_object->private_1); 38 #endif 39 40 #if LIBCXXABI_ARM_EHABI 41 // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0, 42 // which is in the same position as private_1 below. 43 return _Unwind_RaiseException(exception_object); 44 #else 45 // If this is non-forced and a stopping place was found, then this is a 46 // re-throw. 47 // Call _Unwind_RaiseException() as if this was a new exception 48 if (exception_object->private_1 == 0) { 49 return _Unwind_RaiseException(exception_object); 50 // Will return if there is no catch clause, so that __cxa_rethrow can call 51 // std::terminate(). 52 } 53 54 // Call through to _Unwind_Resume() which distiguishes between forced and 55 // regular exceptions. 56 _Unwind_Resume(exception_object); 57 _LIBUNWIND_ABORT("_Unwind_Resume_or_Rethrow() called _Unwind_RaiseException()" 58 " which unexpectedly returned"); 59 #endif 60 } 61 62 63 /// Called by personality handler during phase 2 to get base address for data 64 /// relative encodings. 65 _LIBUNWIND_EXPORT uintptr_t 66 _Unwind_GetDataRelBase(struct _Unwind_Context *context) { 67 (void)context; 68 _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)\n", (void *)context); 69 _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented"); 70 } 71 72 73 /// Called by personality handler during phase 2 to get base address for text 74 /// relative encodings. 75 _LIBUNWIND_EXPORT uintptr_t 76 _Unwind_GetTextRelBase(struct _Unwind_Context *context) { 77 (void)context; 78 _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)\n", (void *)context); 79 _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented"); 80 } 81 82 83 /// Scans unwind information to find the function that contains the 84 /// specified code address "pc". 85 _LIBUNWIND_EXPORT void *_Unwind_FindEnclosingFunction(void *pc) { 86 _LIBUNWIND_TRACE_API("_Unwind_FindEnclosingFunction(pc=%p)\n", pc); 87 // This is slow, but works. 88 // We create an unwind cursor then alter the IP to be pc 89 unw_cursor_t cursor; 90 unw_context_t uc; 91 unw_proc_info_t info; 92 unw_getcontext(&uc); 93 unw_init_local(&cursor, &uc); 94 unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(long) pc); 95 if (unw_get_proc_info(&cursor, &info) == UNW_ESUCCESS) 96 return (void *)(long) info.start_ip; 97 else 98 return NULL; 99 } 100 101 /// Walk every frame and call trace function at each one. If trace function 102 /// returns anything other than _URC_NO_REASON, then walk is terminated. 103 _LIBUNWIND_EXPORT _Unwind_Reason_Code 104 _Unwind_Backtrace(_Unwind_Trace_Fn callback, void *ref) { 105 unw_cursor_t cursor; 106 unw_context_t uc; 107 unw_getcontext(&uc); 108 unw_init_local(&cursor, &uc); 109 110 _LIBUNWIND_TRACE_API("_Unwind_Backtrace(callback=%p)\n", 111 (void *)(uintptr_t)callback); 112 113 // walk each frame 114 while (true) { 115 _Unwind_Reason_Code result; 116 117 // ask libuwind to get next frame (skip over first frame which is 118 // _Unwind_Backtrace()) 119 if (unw_step(&cursor) <= 0) { 120 _LIBUNWIND_TRACE_UNWINDING(" _backtrace: ended because cursor reached " 121 "bottom of stack, returning %d\n", 122 _URC_END_OF_STACK); 123 return _URC_END_OF_STACK; 124 } 125 126 #if LIBCXXABI_ARM_EHABI 127 // Get the information for this frame. 128 unw_proc_info_t frameInfo; 129 if (unw_get_proc_info(&cursor, &frameInfo) != UNW_ESUCCESS) { 130 return _URC_END_OF_STACK; 131 } 132 133 struct _Unwind_Context *context = (struct _Unwind_Context *)&cursor; 134 const uint32_t* unwindInfo = (uint32_t *) frameInfo.unwind_info; 135 if ((*unwindInfo & 0x80000000) == 0) { 136 // 6.2: Generic Model 137 // EHT entry is a prel31 pointing to the PR, followed by data understood 138 // only by the personality routine. Since EHABI doesn't guarantee the 139 // location or availability of the unwind opcodes in the generic model, 140 // we have to call personality functions with (_US_VIRTUAL_UNWIND_FRAME | 141 // _US_FORCE_UNWIND) state. 142 143 // Create a mock exception object for force unwinding. 144 _Unwind_Exception ex; 145 ex.exception_class = 0x434C4E47554E5700; // CLNGUNW\0 146 ex.pr_cache.fnstart = frameInfo.start_ip; 147 ex.pr_cache.ehtp = (_Unwind_EHT_Header *) unwindInfo; 148 ex.pr_cache.additional= frameInfo.flags; 149 150 // Get and call the personality function to unwind the frame. 151 __personality_routine pr = (__personality_routine) readPrel31(unwindInfo); 152 if (pr(_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND, &ex, context) != 153 _URC_CONTINUE_UNWIND) { 154 return _URC_END_OF_STACK; 155 } 156 } else { 157 size_t off, len; 158 unwindInfo = decode_eht_entry(unwindInfo, &off, &len); 159 if (unwindInfo == NULL) { 160 return _URC_FAILURE; 161 } 162 163 result = _Unwind_VRS_Interpret(context, unwindInfo, off, len); 164 if (result != _URC_CONTINUE_UNWIND) { 165 return _URC_END_OF_STACK; 166 } 167 } 168 #endif // LIBCXXABI_ARM_EHABI 169 170 // debugging 171 if (_LIBUNWIND_TRACING_UNWINDING) { 172 char functionName[512]; 173 unw_proc_info_t frame; 174 unw_word_t offset; 175 unw_get_proc_name(&cursor, functionName, 512, &offset); 176 unw_get_proc_info(&cursor, &frame); 177 _LIBUNWIND_TRACE_UNWINDING( 178 " _backtrace: start_ip=0x%llX, func=%s, lsda=0x%llX, context=%p\n", 179 (long long)frame.start_ip, functionName, (long long)frame.lsda, 180 (void *)&cursor); 181 } 182 183 // call trace function with this frame 184 result = (*callback)((struct _Unwind_Context *)(&cursor), ref); 185 if (result != _URC_NO_REASON) { 186 _LIBUNWIND_TRACE_UNWINDING( 187 " _backtrace: ended because callback returned %d\n", result); 188 return result; 189 } 190 } 191 } 192 193 194 /// Find dwarf unwind info for an address 'pc' in some function. 195 _LIBUNWIND_EXPORT const void *_Unwind_Find_FDE(const void *pc, 196 struct dwarf_eh_bases *bases) { 197 // This is slow, but works. 198 // We create an unwind cursor then alter the IP to be pc 199 unw_cursor_t cursor; 200 unw_context_t uc; 201 unw_proc_info_t info; 202 unw_getcontext(&uc); 203 unw_init_local(&cursor, &uc); 204 unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(long) pc); 205 unw_get_proc_info(&cursor, &info); 206 bases->tbase = (uintptr_t)info.extra; 207 bases->dbase = 0; // dbase not used on Mac OS X 208 bases->func = (uintptr_t)info.start_ip; 209 _LIBUNWIND_TRACE_API("_Unwind_Find_FDE(pc=%p) => %p\n", pc, 210 (void *)(long) info.unwind_info); 211 return (void *)(long) info.unwind_info; 212 } 213 214 /// Returns the CFA (call frame area, or stack pointer at start of function) 215 /// for the current context. 216 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) { 217 unw_cursor_t *cursor = (unw_cursor_t *)context; 218 unw_word_t result; 219 unw_get_reg(cursor, UNW_REG_SP, &result); 220 _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p) => 0x%" PRIx64 "\n", 221 (void *)context, (uint64_t)result); 222 return (uintptr_t)result; 223 } 224 225 226 /// Called by personality handler during phase 2 to get instruction pointer. 227 /// ipBefore is a boolean that says if IP is already adjusted to be the call 228 /// site address. Normally IP is the return address. 229 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context, 230 int *ipBefore) { 231 _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p)\n", (void *)context); 232 *ipBefore = 0; 233 return _Unwind_GetIP(context); 234 } 235 236 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND 237 238 /// Called by programs with dynamic code generators that want 239 /// to register a dynamically generated FDE. 240 /// This function has existed on Mac OS X since 10.4, but 241 /// was broken until 10.6. 242 _LIBUNWIND_EXPORT void __register_frame(const void *fde) { 243 _LIBUNWIND_TRACE_API("__register_frame(%p)\n", fde); 244 _unw_add_dynamic_fde((unw_word_t)(uintptr_t) fde); 245 } 246 247 248 /// Called by programs with dynamic code generators that want 249 /// to unregister a dynamically generated FDE. 250 /// This function has existed on Mac OS X since 10.4, but 251 /// was broken until 10.6. 252 _LIBUNWIND_EXPORT void __deregister_frame(const void *fde) { 253 _LIBUNWIND_TRACE_API("__deregister_frame(%p)\n", fde); 254 _unw_remove_dynamic_fde((unw_word_t)(uintptr_t) fde); 255 } 256 257 258 // The following register/deregister functions are gcc extensions. 259 // They have existed on Mac OS X, but have never worked because Mac OS X 260 // before 10.6 used keymgr to track known FDEs, but these functions 261 // never got updated to use keymgr. 262 // For now, we implement these as do-nothing functions to keep any existing 263 // applications working. We also add the not in 10.6 symbol so that nwe 264 // application won't be able to use them. 265 266 #if _LIBUNWIND_SUPPORT_FRAME_APIS 267 _LIBUNWIND_EXPORT void __register_frame_info_bases(const void *fde, void *ob, 268 void *tb, void *db) { 269 (void)fde; 270 (void)ob; 271 (void)tb; 272 (void)db; 273 _LIBUNWIND_TRACE_API("__register_frame_info_bases(%p,%p, %p, %p)\n", 274 fde, ob, tb, db); 275 // do nothing, this function never worked in Mac OS X 276 } 277 278 _LIBUNWIND_EXPORT void __register_frame_info(const void *fde, void *ob) { 279 (void)fde; 280 (void)ob; 281 _LIBUNWIND_TRACE_API("__register_frame_info(%p, %p)\n", fde, ob); 282 // do nothing, this function never worked in Mac OS X 283 } 284 285 _LIBUNWIND_EXPORT void __register_frame_info_table_bases(const void *fde, 286 void *ob, void *tb, 287 void *db) { 288 (void)fde; 289 (void)ob; 290 (void)tb; 291 (void)db; 292 _LIBUNWIND_TRACE_API("__register_frame_info_table_bases" 293 "(%p,%p, %p, %p)\n", fde, ob, tb, db); 294 // do nothing, this function never worked in Mac OS X 295 } 296 297 _LIBUNWIND_EXPORT void __register_frame_info_table(const void *fde, void *ob) { 298 (void)fde; 299 (void)ob; 300 _LIBUNWIND_TRACE_API("__register_frame_info_table(%p, %p)\n", fde, ob); 301 // do nothing, this function never worked in Mac OS X 302 } 303 304 _LIBUNWIND_EXPORT void __register_frame_table(const void *fde) { 305 (void)fde; 306 _LIBUNWIND_TRACE_API("__register_frame_table(%p)\n", fde); 307 // do nothing, this function never worked in Mac OS X 308 } 309 310 _LIBUNWIND_EXPORT void *__deregister_frame_info(const void *fde) { 311 (void)fde; 312 _LIBUNWIND_TRACE_API("__deregister_frame_info(%p)\n", fde); 313 // do nothing, this function never worked in Mac OS X 314 return NULL; 315 } 316 317 _LIBUNWIND_EXPORT void *__deregister_frame_info_bases(const void *fde) { 318 (void)fde; 319 _LIBUNWIND_TRACE_API("__deregister_frame_info_bases(%p)\n", fde); 320 // do nothing, this function never worked in Mac OS X 321 return NULL; 322 } 323 #endif // _LIBUNWIND_SUPPORT_FRAME_APIS 324 325 #endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND 326 327 #endif // _LIBUNWIND_BUILD_ZERO_COST_APIS 328