1288149Semaste//===--------------------- UnwindLevel1-gcc-ext.c -------------------------===// 2288149Semaste// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6288149Semaste// 7288149Semaste// 8288149Semaste// Implements gcc extensions to the C++ ABI Exception Handling Level 1. 9288149Semaste// 10288149Semaste//===----------------------------------------------------------------------===// 11288149Semaste 12288149Semaste#include <inttypes.h> 13288149Semaste#include <stdbool.h> 14288149Semaste#include <stdint.h> 15288149Semaste#include <stdio.h> 16288149Semaste#include <stdlib.h> 17288149Semaste#include <string.h> 18288149Semaste 19288149Semaste#include "config.h" 20288149Semaste#include "libunwind_ext.h" 21288149Semaste#include "libunwind.h" 22288149Semaste#include "Unwind-EHABI.h" 23288149Semaste#include "unwind.h" 24288149Semaste 25345018Sdim#if defined(_LIBUNWIND_BUILD_ZERO_COST_APIS) 26288149Semaste 27345018Sdim#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 28345018Sdim#define private_1 private_[0] 29345018Sdim#endif 30345018Sdim 31288149Semaste/// Called by __cxa_rethrow(). 32288149Semaste_LIBUNWIND_EXPORT _Unwind_Reason_Code 33288149Semaste_Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object) { 34345018Sdim#if defined(_LIBUNWIND_ARM_EHABI) 35308006Semaste _LIBUNWIND_TRACE_API("_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%ld", 36288149Semaste (void *)exception_object, 37288149Semaste (long)exception_object->unwinder_cache.reserved1); 38288149Semaste#else 39345018Sdim _LIBUNWIND_TRACE_API("_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%" PRIdPTR, 40288149Semaste (void *)exception_object, 41345018Sdim (intptr_t)exception_object->private_1); 42288149Semaste#endif 43288149Semaste 44345018Sdim#if defined(_LIBUNWIND_ARM_EHABI) 45288149Semaste // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0, 46288149Semaste // which is in the same position as private_1 below. 47288149Semaste return _Unwind_RaiseException(exception_object); 48288149Semaste#else 49288149Semaste // If this is non-forced and a stopping place was found, then this is a 50288149Semaste // re-throw. 51288149Semaste // Call _Unwind_RaiseException() as if this was a new exception 52288149Semaste if (exception_object->private_1 == 0) { 53288149Semaste return _Unwind_RaiseException(exception_object); 54288149Semaste // Will return if there is no catch clause, so that __cxa_rethrow can call 55288149Semaste // std::terminate(). 56288149Semaste } 57288149Semaste 58288149Semaste // Call through to _Unwind_Resume() which distiguishes between forced and 59288149Semaste // regular exceptions. 60288149Semaste _Unwind_Resume(exception_object); 61288149Semaste _LIBUNWIND_ABORT("_Unwind_Resume_or_Rethrow() called _Unwind_RaiseException()" 62288149Semaste " which unexpectedly returned"); 63288149Semaste#endif 64288149Semaste} 65288149Semaste 66288149Semaste 67288149Semaste/// Called by personality handler during phase 2 to get base address for data 68288149Semaste/// relative encodings. 69288149Semaste_LIBUNWIND_EXPORT uintptr_t 70288149Semaste_Unwind_GetDataRelBase(struct _Unwind_Context *context) { 71288149Semaste (void)context; 72308006Semaste _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)", (void *)context); 73288149Semaste _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented"); 74288149Semaste} 75288149Semaste 76288149Semaste 77288149Semaste/// Called by personality handler during phase 2 to get base address for text 78288149Semaste/// relative encodings. 79288149Semaste_LIBUNWIND_EXPORT uintptr_t 80288149Semaste_Unwind_GetTextRelBase(struct _Unwind_Context *context) { 81288149Semaste (void)context; 82308006Semaste _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)", (void *)context); 83288149Semaste _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented"); 84288149Semaste} 85288149Semaste 86288149Semaste 87288149Semaste/// Scans unwind information to find the function that contains the 88288149Semaste/// specified code address "pc". 89288149Semaste_LIBUNWIND_EXPORT void *_Unwind_FindEnclosingFunction(void *pc) { 90308006Semaste _LIBUNWIND_TRACE_API("_Unwind_FindEnclosingFunction(pc=%p)", pc); 91288149Semaste // This is slow, but works. 92288149Semaste // We create an unwind cursor then alter the IP to be pc 93288149Semaste unw_cursor_t cursor; 94288149Semaste unw_context_t uc; 95288149Semaste unw_proc_info_t info; 96353358Sdim __unw_getcontext(&uc); 97353358Sdim __unw_init_local(&cursor, &uc); 98353358Sdim __unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t)pc); 99353358Sdim if (__unw_get_proc_info(&cursor, &info) == UNW_ESUCCESS) 100345018Sdim return (void *)(intptr_t) info.start_ip; 101288149Semaste else 102288149Semaste return NULL; 103288149Semaste} 104288149Semaste 105288149Semaste/// Walk every frame and call trace function at each one. If trace function 106288149Semaste/// returns anything other than _URC_NO_REASON, then walk is terminated. 107288149Semaste_LIBUNWIND_EXPORT _Unwind_Reason_Code 108288149Semaste_Unwind_Backtrace(_Unwind_Trace_Fn callback, void *ref) { 109288149Semaste unw_cursor_t cursor; 110288149Semaste unw_context_t uc; 111353358Sdim __unw_getcontext(&uc); 112353358Sdim __unw_init_local(&cursor, &uc); 113288149Semaste 114308006Semaste _LIBUNWIND_TRACE_API("_Unwind_Backtrace(callback=%p)", 115288149Semaste (void *)(uintptr_t)callback); 116288149Semaste 117345018Sdim#if defined(_LIBUNWIND_ARM_EHABI) 118288149Semaste // Create a mock exception object for force unwinding. 119288149Semaste _Unwind_Exception ex; 120288149Semaste memset(&ex, '\0', sizeof(ex)); 121288149Semaste ex.exception_class = 0x434C4E47554E5700; // CLNGUNW\0 122288149Semaste#endif 123288149Semaste 124288149Semaste // walk each frame 125288149Semaste while (true) { 126288149Semaste _Unwind_Reason_Code result; 127288149Semaste 128345018Sdim#if !defined(_LIBUNWIND_ARM_EHABI) 129308006Semaste // ask libunwind to get next frame (skip over first frame which is 130288149Semaste // _Unwind_Backtrace()) 131353358Sdim if (__unw_step(&cursor) <= 0) { 132288149Semaste _LIBUNWIND_TRACE_UNWINDING(" _backtrace: ended because cursor reached " 133308006Semaste "bottom of stack, returning %d", 134288149Semaste _URC_END_OF_STACK); 135288149Semaste return _URC_END_OF_STACK; 136288149Semaste } 137288149Semaste#else 138288149Semaste // Get the information for this frame. 139288149Semaste unw_proc_info_t frameInfo; 140353358Sdim if (__unw_get_proc_info(&cursor, &frameInfo) != UNW_ESUCCESS) { 141288149Semaste return _URC_END_OF_STACK; 142288149Semaste } 143288149Semaste 144288149Semaste // Update the pr_cache in the mock exception object. 145288149Semaste const uint32_t* unwindInfo = (uint32_t *) frameInfo.unwind_info; 146288149Semaste ex.pr_cache.fnstart = frameInfo.start_ip; 147288149Semaste ex.pr_cache.ehtp = (_Unwind_EHT_Header *) unwindInfo; 148288149Semaste ex.pr_cache.additional= frameInfo.flags; 149288149Semaste 150288149Semaste struct _Unwind_Context *context = (struct _Unwind_Context *)&cursor; 151288149Semaste // Get and call the personality function to unwind the frame. 152288149Semaste __personality_routine handler = (__personality_routine) frameInfo.handler; 153288149Semaste if (handler == NULL) { 154288149Semaste return _URC_END_OF_STACK; 155288149Semaste } 156288149Semaste if (handler(_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND, &ex, context) != 157288149Semaste _URC_CONTINUE_UNWIND) { 158288149Semaste return _URC_END_OF_STACK; 159288149Semaste } 160345018Sdim#endif // defined(_LIBUNWIND_ARM_EHABI) 161288149Semaste 162288149Semaste // debugging 163288149Semaste if (_LIBUNWIND_TRACING_UNWINDING) { 164288149Semaste char functionName[512]; 165288149Semaste unw_proc_info_t frame; 166288149Semaste unw_word_t offset; 167353358Sdim __unw_get_proc_name(&cursor, functionName, 512, &offset); 168353358Sdim __unw_get_proc_info(&cursor, &frame); 169288149Semaste _LIBUNWIND_TRACE_UNWINDING( 170345018Sdim " _backtrace: start_ip=0x%" PRIxPTR ", func=%s, lsda=0x%" PRIxPTR ", context=%p", 171345018Sdim frame.start_ip, functionName, frame.lsda, 172288149Semaste (void *)&cursor); 173288149Semaste } 174288149Semaste 175288149Semaste // call trace function with this frame 176288149Semaste result = (*callback)((struct _Unwind_Context *)(&cursor), ref); 177288149Semaste if (result != _URC_NO_REASON) { 178288149Semaste _LIBUNWIND_TRACE_UNWINDING( 179308006Semaste " _backtrace: ended because callback returned %d", result); 180288149Semaste return result; 181288149Semaste } 182288149Semaste } 183288149Semaste} 184355645Scem#ifdef __arm__ 185355803Smmel/* Preserve legacy libgcc (pre r318024) ARM ABI mistake */ 186355803Smmel__sym_compat(_Unwind_Backtrace, _Unwind_Backtrace, GCC_3.3); 187355645Scem#endif 188288149Semaste 189288149Semaste 190345018Sdim/// Find DWARF unwind info for an address 'pc' in some function. 191288149Semaste_LIBUNWIND_EXPORT const void *_Unwind_Find_FDE(const void *pc, 192288149Semaste struct dwarf_eh_bases *bases) { 193288149Semaste // This is slow, but works. 194288149Semaste // We create an unwind cursor then alter the IP to be pc 195288149Semaste unw_cursor_t cursor; 196288149Semaste unw_context_t uc; 197288149Semaste unw_proc_info_t info; 198353358Sdim __unw_getcontext(&uc); 199353358Sdim __unw_init_local(&cursor, &uc); 200353358Sdim __unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t)pc); 201353358Sdim __unw_get_proc_info(&cursor, &info); 202288149Semaste bases->tbase = (uintptr_t)info.extra; 203288149Semaste bases->dbase = 0; // dbase not used on Mac OS X 204288149Semaste bases->func = (uintptr_t)info.start_ip; 205308006Semaste _LIBUNWIND_TRACE_API("_Unwind_Find_FDE(pc=%p) => %p", pc, 206345018Sdim (void *)(intptr_t) info.unwind_info); 207345018Sdim return (void *)(intptr_t) info.unwind_info; 208288149Semaste} 209288149Semaste 210288149Semaste/// Returns the CFA (call frame area, or stack pointer at start of function) 211288149Semaste/// for the current context. 212288149Semaste_LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) { 213288149Semaste unw_cursor_t *cursor = (unw_cursor_t *)context; 214288149Semaste unw_word_t result; 215353358Sdim __unw_get_reg(cursor, UNW_REG_SP, &result); 216345018Sdim _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p) => 0x%" PRIxPTR, 217345018Sdim (void *)context, result); 218288149Semaste return (uintptr_t)result; 219288149Semaste} 220288149Semaste 221288149Semaste 222288149Semaste/// Called by personality handler during phase 2 to get instruction pointer. 223288149Semaste/// ipBefore is a boolean that says if IP is already adjusted to be the call 224288149Semaste/// site address. Normally IP is the return address. 225288149Semaste_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context, 226288149Semaste int *ipBefore) { 227308006Semaste _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p)", (void *)context); 228360784Sdim int isSignalFrame = __unw_is_signal_frame((unw_cursor_t *)context); 229360784Sdim // Negative means some kind of error (probably UNW_ENOINFO), but we have no 230360784Sdim // good way to report that, and this maintains backward compatibility with the 231360784Sdim // implementation that hard-coded zero in every case, even signal frames. 232360784Sdim if (isSignalFrame <= 0) 233360784Sdim *ipBefore = 0; 234360784Sdim else 235360784Sdim *ipBefore = 1; 236288149Semaste return _Unwind_GetIP(context); 237288149Semaste} 238288149Semaste 239345018Sdim#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 240288149Semaste 241353358Sdim#if defined(__FreeBSD__) 242310365Semaste 243310365Semaste// Based on LLVM's lib/ExecutionEngine/RuntimeDyld/RTDyldMemoryManager.cpp 244310365Semaste// and XXX should be fixed to be alignment-safe. 245310365Semastestatic void processFDE(const char *addr, bool isDeregister) { 246310365Semaste uint64_t length; 247310365Semaste while ((length = *((const uint32_t *)addr)) != 0) { 248310365Semaste const char *p = addr + 4; 249310365Semaste if (length == 0xffffffff) { 250310365Semaste length = *((const uint64_t *)p); 251310365Semaste p += 8; 252310365Semaste } 253310365Semaste uint32_t offset = *((const uint32_t *)p); 254310365Semaste if (offset != 0) { 255310365Semaste if (isDeregister) 256353358Sdim __unw_remove_dynamic_fde((unw_word_t)(uintptr_t)addr); 257310365Semaste else 258353358Sdim __unw_add_dynamic_fde((unw_word_t)(uintptr_t)addr); 259310365Semaste } 260310365Semaste addr = p + length; 261310365Semaste } 262310365Semaste} 263310365Semaste 264310365Semaste/// Called by programs with dynamic code generators that want to register 265310365Semaste/// dynamically generated FDEs, with a libgcc-compatible API. 266310365Semaste 267310365Semaste_LIBUNWIND_EXPORT void __register_frame(const void *addr) { 268310365Semaste _LIBUNWIND_TRACE_API("__register_frame(%p)", addr); 269310365Semaste processFDE(addr, false); 270310365Semaste} 271310365Semaste 272310365Semaste/// Called by programs with dynamic code generators that want to unregister 273310365Semaste/// dynamically generated FDEs, with a libgcc-compatible API. 274310365Semaste_LIBUNWIND_EXPORT void __deregister_frame(const void *addr) { 275310365Semaste _LIBUNWIND_TRACE_API("__deregister_frame(%p)", addr); 276310365Semaste processFDE(addr, true); 277310365Semaste} 278310365Semaste 279353358Sdim#else // defined(__FreeBSD__) 280310365Semaste 281288149Semaste/// Called by programs with dynamic code generators that want 282288149Semaste/// to register a dynamically generated FDE. 283288149Semaste/// This function has existed on Mac OS X since 10.4, but 284288149Semaste/// was broken until 10.6. 285288149Semaste_LIBUNWIND_EXPORT void __register_frame(const void *fde) { 286308006Semaste _LIBUNWIND_TRACE_API("__register_frame(%p)", fde); 287353358Sdim __unw_add_dynamic_fde((unw_word_t)(uintptr_t)fde); 288288149Semaste} 289288149Semaste 290288149Semaste/// Called by programs with dynamic code generators that want 291288149Semaste/// to unregister a dynamically generated FDE. 292288149Semaste/// This function has existed on Mac OS X since 10.4, but 293288149Semaste/// was broken until 10.6. 294288149Semaste_LIBUNWIND_EXPORT void __deregister_frame(const void *fde) { 295308006Semaste _LIBUNWIND_TRACE_API("__deregister_frame(%p)", fde); 296353358Sdim __unw_remove_dynamic_fde((unw_word_t)(uintptr_t)fde); 297288149Semaste} 298288149Semaste 299353358Sdim#endif // defined(__FreeBSD__) 300288149Semaste 301288149Semaste// The following register/deregister functions are gcc extensions. 302288149Semaste// They have existed on Mac OS X, but have never worked because Mac OS X 303288149Semaste// before 10.6 used keymgr to track known FDEs, but these functions 304288149Semaste// never got updated to use keymgr. 305288149Semaste// For now, we implement these as do-nothing functions to keep any existing 306288149Semaste// applications working. We also add the not in 10.6 symbol so that nwe 307288149Semaste// application won't be able to use them. 308288149Semaste 309345018Sdim#if defined(_LIBUNWIND_SUPPORT_FRAME_APIS) 310288149Semaste_LIBUNWIND_EXPORT void __register_frame_info_bases(const void *fde, void *ob, 311288149Semaste void *tb, void *db) { 312288149Semaste (void)fde; 313288149Semaste (void)ob; 314288149Semaste (void)tb; 315288149Semaste (void)db; 316308006Semaste _LIBUNWIND_TRACE_API("__register_frame_info_bases(%p,%p, %p, %p)", 317288149Semaste fde, ob, tb, db); 318288149Semaste // do nothing, this function never worked in Mac OS X 319288149Semaste} 320288149Semaste 321288149Semaste_LIBUNWIND_EXPORT void __register_frame_info(const void *fde, void *ob) { 322288149Semaste (void)fde; 323288149Semaste (void)ob; 324308006Semaste _LIBUNWIND_TRACE_API("__register_frame_info(%p, %p)", fde, ob); 325288149Semaste // do nothing, this function never worked in Mac OS X 326288149Semaste} 327288149Semaste 328288149Semaste_LIBUNWIND_EXPORT void __register_frame_info_table_bases(const void *fde, 329288149Semaste void *ob, void *tb, 330288149Semaste void *db) { 331288149Semaste (void)fde; 332288149Semaste (void)ob; 333288149Semaste (void)tb; 334288149Semaste (void)db; 335288149Semaste _LIBUNWIND_TRACE_API("__register_frame_info_table_bases" 336308006Semaste "(%p,%p, %p, %p)", fde, ob, tb, db); 337288149Semaste // do nothing, this function never worked in Mac OS X 338288149Semaste} 339288149Semaste 340288149Semaste_LIBUNWIND_EXPORT void __register_frame_info_table(const void *fde, void *ob) { 341288149Semaste (void)fde; 342288149Semaste (void)ob; 343308006Semaste _LIBUNWIND_TRACE_API("__register_frame_info_table(%p, %p)", fde, ob); 344288149Semaste // do nothing, this function never worked in Mac OS X 345288149Semaste} 346288149Semaste 347288149Semaste_LIBUNWIND_EXPORT void __register_frame_table(const void *fde) { 348288149Semaste (void)fde; 349308006Semaste _LIBUNWIND_TRACE_API("__register_frame_table(%p)", fde); 350288149Semaste // do nothing, this function never worked in Mac OS X 351288149Semaste} 352288149Semaste 353288149Semaste_LIBUNWIND_EXPORT void *__deregister_frame_info(const void *fde) { 354288149Semaste (void)fde; 355308006Semaste _LIBUNWIND_TRACE_API("__deregister_frame_info(%p)", fde); 356288149Semaste // do nothing, this function never worked in Mac OS X 357288149Semaste return NULL; 358288149Semaste} 359288149Semaste 360288149Semaste_LIBUNWIND_EXPORT void *__deregister_frame_info_bases(const void *fde) { 361288149Semaste (void)fde; 362308006Semaste _LIBUNWIND_TRACE_API("__deregister_frame_info_bases(%p)", fde); 363288149Semaste // do nothing, this function never worked in Mac OS X 364288149Semaste return NULL; 365288149Semaste} 366345018Sdim#endif // defined(_LIBUNWIND_SUPPORT_FRAME_APIS) 367288149Semaste 368345018Sdim#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 369288149Semaste 370345018Sdim#endif // defined(_LIBUNWIND_BUILD_ZERO_COST_APIS) 371