155682Smarkm//===----------------------------------------------------------------------===// 255682Smarkm// 355682Smarkm// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 455682Smarkm// See https://llvm.org/LICENSE.txt for license information. 555682Smarkm// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 655682Smarkm// 755682Smarkm// 855682Smarkm// Implements gcc extensions to the C++ ABI Exception Handling Level 1. 955682Smarkm// 1055682Smarkm//===----------------------------------------------------------------------===// 1155682Smarkm 1255682Smarkm#include <inttypes.h> 1355682Smarkm#include <stdbool.h> 1455682Smarkm#include <stdint.h> 1555682Smarkm#include <stdio.h> 1655682Smarkm#include <stdlib.h> 1755682Smarkm#include <string.h> 1855682Smarkm 1955682Smarkm#include "config.h" 2055682Smarkm#include "libunwind_ext.h" 2155682Smarkm#include "libunwind.h" 2255682Smarkm#include "Unwind-EHABI.h" 2355682Smarkm#include "unwind.h" 2455682Smarkm 2555682Smarkm#if defined(_AIX) 2655682Smarkm#include <sys/debug.h> 2755682Smarkm#endif 2855682Smarkm 2955682Smarkm#if defined(_LIBUNWIND_BUILD_ZERO_COST_APIS) 3055682Smarkm 3155682Smarkm#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 3255682Smarkm#define PRIVATE_1 private_[0] 3355682Smarkm#elif defined(_LIBUNWIND_ARM_EHABI) 3455682Smarkm#define PRIVATE_1 unwinder_cache.reserved1 3555682Smarkm#else 3655682Smarkm#define PRIVATE_1 private_1 3755682Smarkm#endif 3855682Smarkm 3955682Smarkm/// Called by __cxa_rethrow(). 4055682Smarkm_LIBUNWIND_EXPORT _Unwind_Reason_Code 4155682Smarkm_Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object) { 4255682Smarkm _LIBUNWIND_TRACE_API( 4355682Smarkm "_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%" PRIdPTR, 4455682Smarkm (void *)exception_object, (intptr_t)exception_object->PRIVATE_1); 4555682Smarkm 4655682Smarkm // If this is non-forced and a stopping place was found, then this is a 4755682Smarkm // re-throw. 4855682Smarkm // Call _Unwind_RaiseException() as if this was a new exception 4955682Smarkm if (exception_object->PRIVATE_1 == 0) { 5055682Smarkm return _Unwind_RaiseException(exception_object); 5155682Smarkm // Will return if there is no catch clause, so that __cxa_rethrow can call 5255682Smarkm // std::terminate(). 5355682Smarkm } 5455682Smarkm 5555682Smarkm // Call through to _Unwind_Resume() which distinguishes between forced and 5655682Smarkm // regular exceptions. 5755682Smarkm _Unwind_Resume(exception_object); 5855682Smarkm _LIBUNWIND_ABORT("_Unwind_Resume_or_Rethrow() called _Unwind_RaiseException()" 5955682Smarkm " which unexpectedly returned"); 6055682Smarkm} 6155682Smarkm 6255682Smarkm/// Called by personality handler during phase 2 to get base address for data 6355682Smarkm/// relative encodings. 6455682Smarkm_LIBUNWIND_EXPORT uintptr_t 6555682Smarkm_Unwind_GetDataRelBase(struct _Unwind_Context *context) { 6655682Smarkm _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)", (void *)context); 6755682Smarkm#if defined(_AIX) 6855682Smarkm return unw_get_data_rel_base((unw_cursor_t *)context); 6955682Smarkm#else 7055682Smarkm (void)context; 7155682Smarkm _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented"); 7255682Smarkm#endif 7355682Smarkm} 7455682Smarkm 7555682Smarkm/// Called by personality handler during phase 2 to get base address for text 7655682Smarkm/// relative encodings. 7755682Smarkm_LIBUNWIND_EXPORT uintptr_t 7855682Smarkm_Unwind_GetTextRelBase(struct _Unwind_Context *context) { 7955682Smarkm (void)context; 8055682Smarkm _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)", (void *)context); 8155682Smarkm _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented"); 8255682Smarkm} 8355682Smarkm 8455682Smarkm 8555682Smarkm/// Scans unwind information to find the function that contains the 8655682Smarkm/// specified code address "pc". 8755682Smarkm_LIBUNWIND_EXPORT void *_Unwind_FindEnclosingFunction(void *pc) { 8855682Smarkm _LIBUNWIND_TRACE_API("_Unwind_FindEnclosingFunction(pc=%p)", pc); 8955682Smarkm#if defined(_AIX) 9055682Smarkm if (pc == NULL) 9155682Smarkm return NULL; 9255682Smarkm 9355682Smarkm // Get the start address of the enclosing function from the function's 9455682Smarkm // traceback table. 9555682Smarkm uint32_t *p = (uint32_t *)pc; 9655682Smarkm 9755682Smarkm // Keep looking forward until a word of 0 is found. The traceback 9855682Smarkm // table starts at the following word. 9955682Smarkm while (*p) 10055682Smarkm ++p; 10155682Smarkm struct tbtable *TBTable = (struct tbtable *)(p + 1); 10255682Smarkm 10355682Smarkm // Get the address of the traceback table extension. 10455682Smarkm p = (uint32_t *)&TBTable->tb_ext; 10555682Smarkm 10655682Smarkm // Skip field parminfo if it exists. 10755682Smarkm if (TBTable->tb.fixedparms || TBTable->tb.floatparms) 10855682Smarkm ++p; 10955682Smarkm 11055682Smarkm if (TBTable->tb.has_tboff) 11155682Smarkm // *p contains the offset from the function start to traceback table. 11255682Smarkm return (void *)((uintptr_t)TBTable - *p - sizeof(uint32_t)); 11355682Smarkm return NULL; 11455682Smarkm#else 11555682Smarkm // This is slow, but works. 11655682Smarkm // We create an unwind cursor then alter the IP to be pc 11755682Smarkm unw_cursor_t cursor; 11855682Smarkm unw_context_t uc; 11955682Smarkm unw_proc_info_t info; 12055682Smarkm __unw_getcontext(&uc); 12155682Smarkm __unw_init_local(&cursor, &uc); 12255682Smarkm __unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t)pc); 12355682Smarkm if (__unw_get_proc_info(&cursor, &info) == UNW_ESUCCESS) 12455682Smarkm return (void *)(intptr_t) info.start_ip; 12555682Smarkm else 12655682Smarkm return NULL; 12755682Smarkm#endif 12855682Smarkm} 12955682Smarkm 13055682Smarkm/// Walk every frame and call trace function at each one. If trace function 13155682Smarkm/// returns anything other than _URC_NO_REASON, then walk is terminated. 13255682Smarkm_LIBUNWIND_EXPORT _Unwind_Reason_Code 13355682Smarkm_Unwind_Backtrace(_Unwind_Trace_Fn callback, void *ref) { 13455682Smarkm unw_cursor_t cursor; 13555682Smarkm unw_context_t uc; 13655682Smarkm __unw_getcontext(&uc); 13755682Smarkm __unw_init_local(&cursor, &uc); 13855682Smarkm 13955682Smarkm _LIBUNWIND_TRACE_API("_Unwind_Backtrace(callback=%p)", 14055682Smarkm (void *)(uintptr_t)callback); 14155682Smarkm 14255682Smarkm#if defined(_LIBUNWIND_ARM_EHABI) 14355682Smarkm // Create a mock exception object for force unwinding. 14455682Smarkm _Unwind_Exception ex; 14555682Smarkm memset(&ex, '\0', sizeof(ex)); 14655682Smarkm memcpy(&ex.exception_class, "CLNGUNW", sizeof(ex.exception_class)); 14755682Smarkm#endif 14855682Smarkm 14955682Smarkm // walk each frame 15055682Smarkm while (true) { 15155682Smarkm _Unwind_Reason_Code result; 15255682Smarkm 15355682Smarkm#if !defined(_LIBUNWIND_ARM_EHABI) 15455682Smarkm // ask libunwind to get next frame (skip over first frame which is 15555682Smarkm // _Unwind_Backtrace()) 15655682Smarkm if (__unw_step(&cursor) <= 0) { 15755682Smarkm _LIBUNWIND_TRACE_UNWINDING(" _backtrace: ended because cursor reached " 15855682Smarkm "bottom of stack, returning %d", 15955682Smarkm _URC_END_OF_STACK); 16055682Smarkm return _URC_END_OF_STACK; 16155682Smarkm } 16255682Smarkm#else 16355682Smarkm // Get the information for this frame. 16455682Smarkm unw_proc_info_t frameInfo; 16555682Smarkm if (__unw_get_proc_info(&cursor, &frameInfo) != UNW_ESUCCESS) { 16655682Smarkm return _URC_END_OF_STACK; 16755682Smarkm } 16855682Smarkm 16955682Smarkm // Update the pr_cache in the mock exception object. 17055682Smarkm uint32_t *unwindInfo = (uint32_t *)frameInfo.unwind_info; 17155682Smarkm ex.pr_cache.fnstart = frameInfo.start_ip; 17255682Smarkm ex.pr_cache.ehtp = (_Unwind_EHT_Header *) unwindInfo; 17355682Smarkm ex.pr_cache.additional= frameInfo.flags; 17455682Smarkm 17555682Smarkm struct _Unwind_Context *context = (struct _Unwind_Context *)&cursor; 17655682Smarkm // Get and call the personality function to unwind the frame. 17755682Smarkm _Unwind_Personality_Fn handler = (_Unwind_Personality_Fn)frameInfo.handler; 17855682Smarkm if (handler == NULL) { 17955682Smarkm return _URC_END_OF_STACK; 18055682Smarkm } 18155682Smarkm if (handler(_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND, &ex, context) != 18255682Smarkm _URC_CONTINUE_UNWIND) { 18355682Smarkm return _URC_END_OF_STACK; 18455682Smarkm } 18555682Smarkm#endif // defined(_LIBUNWIND_ARM_EHABI) 18655682Smarkm 18755682Smarkm // debugging 18855682Smarkm if (_LIBUNWIND_TRACING_UNWINDING) { 18955682Smarkm char functionName[512]; 19055682Smarkm unw_proc_info_t frame; 19155682Smarkm unw_word_t offset; 19255682Smarkm __unw_get_proc_name(&cursor, functionName, 512, &offset); 19355682Smarkm __unw_get_proc_info(&cursor, &frame); 19455682Smarkm _LIBUNWIND_TRACE_UNWINDING( 19555682Smarkm " _backtrace: start_ip=0x%" PRIxPTR ", func=%s, lsda=0x%" PRIxPTR ", context=%p", 19655682Smarkm frame.start_ip, functionName, frame.lsda, 19755682Smarkm (void *)&cursor); 19855682Smarkm } 19955682Smarkm 20055682Smarkm // call trace function with this frame 20155682Smarkm result = (*callback)((struct _Unwind_Context *)(&cursor), ref); 20255682Smarkm if (result != _URC_NO_REASON) { 20355682Smarkm _LIBUNWIND_TRACE_UNWINDING( 20455682Smarkm " _backtrace: ended because callback returned %d", result); 20555682Smarkm return result; 20655682Smarkm } 20755682Smarkm } 20855682Smarkm} 20955682Smarkm#ifdef __arm__ 21055682Smarkm/* Preserve legacy libgcc (pre r318024) ARM ABI mistake */ 21155682Smarkm__sym_compat(_Unwind_Backtrace, _Unwind_Backtrace, GCC_3.3); 21255682Smarkm#endif 21355682Smarkm 21455682Smarkm 21555682Smarkm/// Find DWARF unwind info for an address 'pc' in some function. 21655682Smarkm_LIBUNWIND_EXPORT const void *_Unwind_Find_FDE(const void *pc, 21755682Smarkm struct dwarf_eh_bases *bases) { 21855682Smarkm // This is slow, but works. 21955682Smarkm // We create an unwind cursor then alter the IP to be pc 22055682Smarkm unw_cursor_t cursor; 22155682Smarkm unw_context_t uc; 22255682Smarkm unw_proc_info_t info; 22355682Smarkm __unw_getcontext(&uc); 22455682Smarkm __unw_init_local(&cursor, &uc); 22555682Smarkm __unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t)pc); 22655682Smarkm __unw_get_proc_info(&cursor, &info); 22755682Smarkm bases->tbase = (uintptr_t)info.extra; 22855682Smarkm bases->dbase = 0; // dbase not used on Mac OS X 22955682Smarkm bases->func = (uintptr_t)info.start_ip; 23055682Smarkm _LIBUNWIND_TRACE_API("_Unwind_Find_FDE(pc=%p) => %p", pc, 23155682Smarkm (void *)(intptr_t) info.unwind_info); 23255682Smarkm return (void *)(intptr_t) info.unwind_info; 23355682Smarkm} 23455682Smarkm 23555682Smarkm/// Returns the CFA (call frame area, or stack pointer at start of function) 23655682Smarkm/// for the current context. 23755682Smarkm_LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) { 23855682Smarkm unw_cursor_t *cursor = (unw_cursor_t *)context; 23955682Smarkm unw_word_t result; 24055682Smarkm __unw_get_reg(cursor, UNW_REG_SP, &result); 24155682Smarkm _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p) => 0x%" PRIxPTR, 24255682Smarkm (void *)context, result); 24355682Smarkm return (uintptr_t)result; 24455682Smarkm} 24555682Smarkm 24655682Smarkm 24755682Smarkm/// Called by personality handler during phase 2 to get instruction pointer. 24855682Smarkm/// ipBefore is a boolean that says if IP is already adjusted to be the call 24955682Smarkm/// site address. Normally IP is the return address. 25055682Smarkm_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context, 25155682Smarkm int *ipBefore) { 25255682Smarkm _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p)", (void *)context); 25355682Smarkm int isSignalFrame = __unw_is_signal_frame((unw_cursor_t *)context); 25455682Smarkm // Negative means some kind of error (probably UNW_ENOINFO), but we have no 25555682Smarkm // good way to report that, and this maintains backward compatibility with the 25655682Smarkm // implementation that hard-coded zero in every case, even signal frames. 25755682Smarkm if (isSignalFrame <= 0) 25855682Smarkm *ipBefore = 0; 25955682Smarkm else 26055682Smarkm *ipBefore = 1; 26155682Smarkm return _Unwind_GetIP(context); 26255682Smarkm} 26355682Smarkm 26455682Smarkm#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 26555682Smarkm 26655682Smarkm#if defined(__FreeBSD__) 26755682Smarkm 26855682Smarkm// Based on LLVM's lib/ExecutionEngine/RuntimeDyld/RTDyldMemoryManager.cpp 26955682Smarkm// and XXX should be fixed to be alignment-safe. 27055682Smarkmstatic void processFDE(const char *addr, bool isDeregister) { 27155682Smarkm uint64_t length; 27255682Smarkm while ((length = *((const uint32_t *)addr)) != 0) { 27355682Smarkm const char *p = addr + 4; 27455682Smarkm if (length == 0xffffffff) { 27555682Smarkm length = *((const uint64_t *)p); 27655682Smarkm p += 8; 27755682Smarkm } 27855682Smarkm uint32_t offset = *((const uint32_t *)p); 27955682Smarkm if (offset != 0) { 28055682Smarkm if (isDeregister) 28155682Smarkm __unw_remove_dynamic_fde((unw_word_t)(uintptr_t)addr); 28255682Smarkm else 28355682Smarkm __unw_add_dynamic_fde((unw_word_t)(uintptr_t)addr); 284 } 285 addr = p + length; 286 } 287} 288 289/// Called by programs with dynamic code generators that want to register 290/// dynamically generated FDEs, with a libgcc-compatible API. 291 292_LIBUNWIND_EXPORT void __register_frame(const void *addr) { 293 _LIBUNWIND_TRACE_API("__register_frame(%p)", addr); 294 processFDE(addr, false); 295} 296 297/// Called by programs with dynamic code generators that want to unregister 298/// dynamically generated FDEs, with a libgcc-compatible API. 299_LIBUNWIND_EXPORT void __deregister_frame(const void *addr) { 300 _LIBUNWIND_TRACE_API("__deregister_frame(%p)", addr); 301 processFDE(addr, true); 302} 303 304#else // defined(__FreeBSD__) 305 306/// Called by programs with dynamic code generators that want 307/// to register a dynamically generated FDE. 308/// This function has existed on Mac OS X since 10.4, but 309/// was broken until 10.6. 310_LIBUNWIND_EXPORT void __register_frame(const void *fde) { 311 _LIBUNWIND_TRACE_API("__register_frame(%p)", fde); 312 __unw_add_dynamic_fde((unw_word_t)(uintptr_t)fde); 313} 314 315 316/// Called by programs with dynamic code generators that want 317/// to unregister a dynamically generated FDE. 318/// This function has existed on Mac OS X since 10.4, but 319/// was broken until 10.6. 320_LIBUNWIND_EXPORT void __deregister_frame(const void *fde) { 321 _LIBUNWIND_TRACE_API("__deregister_frame(%p)", fde); 322 __unw_remove_dynamic_fde((unw_word_t)(uintptr_t)fde); 323} 324 325#endif // defined(__FreeBSD__) 326 327// The following register/deregister functions are gcc extensions. 328// They have existed on Mac OS X, but have never worked because Mac OS X 329// before 10.6 used keymgr to track known FDEs, but these functions 330// never got updated to use keymgr. 331// For now, we implement these as do-nothing functions to keep any existing 332// applications working. We also add the not in 10.6 symbol so that nwe 333// application won't be able to use them. 334 335#if defined(_LIBUNWIND_SUPPORT_FRAME_APIS) 336_LIBUNWIND_EXPORT void __register_frame_info_bases(const void *fde, void *ob, 337 void *tb, void *db) { 338 (void)fde; 339 (void)ob; 340 (void)tb; 341 (void)db; 342 _LIBUNWIND_TRACE_API("__register_frame_info_bases(%p,%p, %p, %p)", 343 fde, ob, tb, db); 344 // do nothing, this function never worked in Mac OS X 345} 346 347_LIBUNWIND_EXPORT void __register_frame_info(const void *fde, void *ob) { 348 (void)fde; 349 (void)ob; 350 _LIBUNWIND_TRACE_API("__register_frame_info(%p, %p)", fde, ob); 351 // do nothing, this function never worked in Mac OS X 352} 353 354_LIBUNWIND_EXPORT void __register_frame_info_table_bases(const void *fde, 355 void *ob, void *tb, 356 void *db) { 357 (void)fde; 358 (void)ob; 359 (void)tb; 360 (void)db; 361 _LIBUNWIND_TRACE_API("__register_frame_info_table_bases" 362 "(%p,%p, %p, %p)", fde, ob, tb, db); 363 // do nothing, this function never worked in Mac OS X 364} 365 366_LIBUNWIND_EXPORT void __register_frame_info_table(const void *fde, void *ob) { 367 (void)fde; 368 (void)ob; 369 _LIBUNWIND_TRACE_API("__register_frame_info_table(%p, %p)", fde, ob); 370 // do nothing, this function never worked in Mac OS X 371} 372 373_LIBUNWIND_EXPORT void __register_frame_table(const void *fde) { 374 (void)fde; 375 _LIBUNWIND_TRACE_API("__register_frame_table(%p)", fde); 376 // do nothing, this function never worked in Mac OS X 377} 378 379_LIBUNWIND_EXPORT void *__deregister_frame_info(const void *fde) { 380 (void)fde; 381 _LIBUNWIND_TRACE_API("__deregister_frame_info(%p)", fde); 382 // do nothing, this function never worked in Mac OS X 383 return NULL; 384} 385 386_LIBUNWIND_EXPORT void *__deregister_frame_info_bases(const void *fde) { 387 (void)fde; 388 _LIBUNWIND_TRACE_API("__deregister_frame_info_bases(%p)", fde); 389 // do nothing, this function never worked in Mac OS X 390 return NULL; 391} 392#endif // defined(_LIBUNWIND_SUPPORT_FRAME_APIS) 393 394#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 395 396#endif // defined(_LIBUNWIND_BUILD_ZERO_COST_APIS) 397