Path: blob/main/system/lib/libunwind/src/UnwindLevel1-gcc-ext.c
6175 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//7// Implements gcc extensions to the C++ ABI Exception Handling Level 1.8//9//===----------------------------------------------------------------------===//1011#include <inttypes.h>12#include <stdbool.h>13#include <stdint.h>14#include <stdio.h>15#include <stdlib.h>16#include <string.h>1718#include "config.h"19#include "libunwind_ext.h"20#include "libunwind.h"21#include "Unwind-EHABI.h"22#include "unwind.h"2324#if defined(_AIX)25#include <sys/debug.h>26#endif2728#if defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)2930#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)31#define PRIVATE_1 private_[0]32#elif defined(_LIBUNWIND_ARM_EHABI)33#define PRIVATE_1 unwinder_cache.reserved134#else35#define PRIVATE_1 private_136#endif3738/// Called by __cxa_rethrow().39_LIBUNWIND_EXPORT _Unwind_Reason_Code40_Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object) {41_LIBUNWIND_TRACE_API(42"_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%" PRIdPTR,43(void *)exception_object, (intptr_t)exception_object->PRIVATE_1);4445// If this is non-forced and a stopping place was found, then this is a46// re-throw.47// Call _Unwind_RaiseException() as if this was a new exception48if (exception_object->PRIVATE_1 == 0) {49return _Unwind_RaiseException(exception_object);50// Will return if there is no catch clause, so that __cxa_rethrow can call51// std::terminate().52}5354// Call through to _Unwind_Resume() which distinguishes between forced and55// regular exceptions.56_Unwind_Resume(exception_object);57_LIBUNWIND_ABORT("_Unwind_Resume_or_Rethrow() called _Unwind_RaiseException()"58" which unexpectedly returned");59}6061/// Called by personality handler during phase 2 to get base address for data62/// relative encodings.63_LIBUNWIND_EXPORT uintptr_t64_Unwind_GetDataRelBase(struct _Unwind_Context *context) {65_LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)", (void *)context);66#if defined(_AIX)67return unw_get_data_rel_base((unw_cursor_t *)context);68#else69(void)context;70_LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented");71#endif72}7374/// Called by personality handler during phase 2 to get base address for text75/// relative encodings.76_LIBUNWIND_EXPORT uintptr_t77_Unwind_GetTextRelBase(struct _Unwind_Context *context) {78(void)context;79_LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)", (void *)context);80_LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented");81}828384/// Scans unwind information to find the function that contains the85/// specified code address "pc".86_LIBUNWIND_EXPORT void *_Unwind_FindEnclosingFunction(void *pc) {87_LIBUNWIND_TRACE_API("_Unwind_FindEnclosingFunction(pc=%p)", pc);88#if defined(_AIX)89if (pc == NULL)90return NULL;9192// Get the start address of the enclosing function from the function's93// traceback table.94uint32_t *p = (uint32_t *)pc;9596// Keep looking forward until a word of 0 is found. The traceback97// table starts at the following word.98while (*p)99++p;100struct tbtable *TBTable = (struct tbtable *)(p + 1);101102// Get the address of the traceback table extension.103p = (uint32_t *)&TBTable->tb_ext;104105// Skip field parminfo if it exists.106if (TBTable->tb.fixedparms || TBTable->tb.floatparms)107++p;108109if (TBTable->tb.has_tboff)110// *p contains the offset from the function start to traceback table.111return (void *)((uintptr_t)TBTable - *p - sizeof(uint32_t));112return NULL;113#else114// This is slow, but works.115// We create an unwind cursor then alter the IP to be pc116unw_cursor_t cursor;117unw_context_t uc;118unw_proc_info_t info;119__unw_getcontext(&uc);120__unw_init_local(&cursor, &uc);121__unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t)pc);122if (__unw_get_proc_info(&cursor, &info) == UNW_ESUCCESS)123return (void *)(intptr_t) info.start_ip;124else125return NULL;126#endif127}128129/// Walk every frame and call trace function at each one. If trace function130/// returns anything other than _URC_NO_REASON, then walk is terminated.131_LIBUNWIND_EXPORT _Unwind_Reason_Code132_Unwind_Backtrace(_Unwind_Trace_Fn callback, void *ref) {133unw_cursor_t cursor;134unw_context_t uc;135__unw_getcontext(&uc);136__unw_init_local(&cursor, &uc);137138_LIBUNWIND_TRACE_API("_Unwind_Backtrace(callback=%p)",139(void *)(uintptr_t)callback);140141#if defined(_LIBUNWIND_ARM_EHABI)142// Create a mock exception object for force unwinding.143_Unwind_Exception ex;144memset(&ex, '\0', sizeof(ex));145memcpy(&ex.exception_class, "CLNGUNW", sizeof(ex.exception_class));146#endif147148// walk each frame149while (true) {150_Unwind_Reason_Code result;151152#if !defined(_LIBUNWIND_ARM_EHABI)153// ask libunwind to get next frame (skip over first frame which is154// _Unwind_Backtrace())155if (__unw_step(&cursor) <= 0) {156_LIBUNWIND_TRACE_UNWINDING(" _backtrace: ended because cursor reached "157"bottom of stack, returning %d",158_URC_END_OF_STACK);159return _URC_END_OF_STACK;160}161#else162// Get the information for this frame.163unw_proc_info_t frameInfo;164if (__unw_get_proc_info(&cursor, &frameInfo) != UNW_ESUCCESS) {165return _URC_END_OF_STACK;166}167168// Update the pr_cache in the mock exception object.169uint32_t *unwindInfo = (uint32_t *)frameInfo.unwind_info;170ex.pr_cache.fnstart = frameInfo.start_ip;171ex.pr_cache.ehtp = (_Unwind_EHT_Header *) unwindInfo;172ex.pr_cache.additional= frameInfo.flags;173174struct _Unwind_Context *context = (struct _Unwind_Context *)&cursor;175// Get and call the personality function to unwind the frame.176_Unwind_Personality_Fn handler = (_Unwind_Personality_Fn)frameInfo.handler;177if (handler == NULL) {178return _URC_END_OF_STACK;179}180if (handler(_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND, &ex, context) !=181_URC_CONTINUE_UNWIND) {182return _URC_END_OF_STACK;183}184#endif // defined(_LIBUNWIND_ARM_EHABI)185186// debugging187if (_LIBUNWIND_TRACING_UNWINDING) {188char functionName[512];189unw_proc_info_t frame;190unw_word_t offset;191__unw_get_proc_name(&cursor, functionName, 512, &offset);192__unw_get_proc_info(&cursor, &frame);193_LIBUNWIND_TRACE_UNWINDING(194" _backtrace: start_ip=0x%" PRIxPTR ", func=%s, lsda=0x%" PRIxPTR ", context=%p",195frame.start_ip, functionName, frame.lsda,196(void *)&cursor);197}198199// call trace function with this frame200result = (*callback)((struct _Unwind_Context *)(&cursor), ref);201if (result != _URC_NO_REASON) {202_LIBUNWIND_TRACE_UNWINDING(203" _backtrace: ended because callback returned %d", result);204return result;205}206}207}208209210/// Find DWARF unwind info for an address 'pc' in some function.211_LIBUNWIND_EXPORT const void *_Unwind_Find_FDE(const void *pc,212struct dwarf_eh_bases *bases) {213// This is slow, but works.214// We create an unwind cursor then alter the IP to be pc215unw_cursor_t cursor;216unw_context_t uc;217unw_proc_info_t info;218__unw_getcontext(&uc);219__unw_init_local(&cursor, &uc);220__unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t)pc);221__unw_get_proc_info(&cursor, &info);222bases->tbase = (uintptr_t)info.extra;223bases->dbase = 0; // dbase not used on Mac OS X224bases->func = (uintptr_t)info.start_ip;225_LIBUNWIND_TRACE_API("_Unwind_Find_FDE(pc=%p) => %p", pc,226(void *)(intptr_t) info.unwind_info);227return (void *)(intptr_t) info.unwind_info;228}229230/// Returns the CFA (call frame area, or stack pointer at start of function)231/// for the current context.232_LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) {233unw_cursor_t *cursor = (unw_cursor_t *)context;234unw_word_t result;235__unw_get_reg(cursor, UNW_REG_SP, &result);236_LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p) => 0x%" PRIxPTR,237(void *)context, result);238return (uintptr_t)result;239}240241242/// Called by personality handler during phase 2 to get instruction pointer.243/// ipBefore is a boolean that says if IP is already adjusted to be the call244/// site address. Normally IP is the return address.245_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,246int *ipBefore) {247_LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p)", (void *)context);248int isSignalFrame = __unw_is_signal_frame((unw_cursor_t *)context);249// Negative means some kind of error (probably UNW_ENOINFO), but we have no250// good way to report that, and this maintains backward compatibility with the251// implementation that hard-coded zero in every case, even signal frames.252if (isSignalFrame <= 0)253*ipBefore = 0;254else255*ipBefore = 1;256return _Unwind_GetIP(context);257}258259#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)260261/// Called by programs with dynamic code generators that want262/// to register a dynamically generated FDE.263/// This function has existed on Mac OS X since 10.4, but264/// was broken until 10.6.265_LIBUNWIND_EXPORT void __register_frame(const void *fde) {266_LIBUNWIND_TRACE_API("__register_frame(%p)", fde);267__unw_add_dynamic_fde((unw_word_t)(uintptr_t)fde);268}269270271/// Called by programs with dynamic code generators that want272/// to unregister a dynamically generated FDE.273/// This function has existed on Mac OS X since 10.4, but274/// was broken until 10.6.275_LIBUNWIND_EXPORT void __deregister_frame(const void *fde) {276_LIBUNWIND_TRACE_API("__deregister_frame(%p)", fde);277__unw_remove_dynamic_fde((unw_word_t)(uintptr_t)fde);278}279280281// The following register/deregister functions are gcc extensions.282// They have existed on Mac OS X, but have never worked because Mac OS X283// before 10.6 used keymgr to track known FDEs, but these functions284// never got updated to use keymgr.285// For now, we implement these as do-nothing functions to keep any existing286// applications working. We also add the not in 10.6 symbol so that nwe287// application won't be able to use them.288289#if defined(_LIBUNWIND_SUPPORT_FRAME_APIS)290_LIBUNWIND_EXPORT void __register_frame_info_bases(const void *fde, void *ob,291void *tb, void *db) {292(void)fde;293(void)ob;294(void)tb;295(void)db;296_LIBUNWIND_TRACE_API("__register_frame_info_bases(%p,%p, %p, %p)",297fde, ob, tb, db);298// do nothing, this function never worked in Mac OS X299}300301_LIBUNWIND_EXPORT void __register_frame_info(const void *fde, void *ob) {302(void)fde;303(void)ob;304_LIBUNWIND_TRACE_API("__register_frame_info(%p, %p)", fde, ob);305// do nothing, this function never worked in Mac OS X306}307308_LIBUNWIND_EXPORT void __register_frame_info_table_bases(const void *fde,309void *ob, void *tb,310void *db) {311(void)fde;312(void)ob;313(void)tb;314(void)db;315_LIBUNWIND_TRACE_API("__register_frame_info_table_bases"316"(%p,%p, %p, %p)", fde, ob, tb, db);317// do nothing, this function never worked in Mac OS X318}319320_LIBUNWIND_EXPORT void __register_frame_info_table(const void *fde, void *ob) {321(void)fde;322(void)ob;323_LIBUNWIND_TRACE_API("__register_frame_info_table(%p, %p)", fde, ob);324// do nothing, this function never worked in Mac OS X325}326327_LIBUNWIND_EXPORT void __register_frame_table(const void *fde) {328(void)fde;329_LIBUNWIND_TRACE_API("__register_frame_table(%p)", fde);330// do nothing, this function never worked in Mac OS X331}332333_LIBUNWIND_EXPORT void *__deregister_frame_info(const void *fde) {334(void)fde;335_LIBUNWIND_TRACE_API("__deregister_frame_info(%p)", fde);336// do nothing, this function never worked in Mac OS X337return NULL;338}339340_LIBUNWIND_EXPORT void *__deregister_frame_info_bases(const void *fde) {341(void)fde;342_LIBUNWIND_TRACE_API("__deregister_frame_info_bases(%p)", fde);343// do nothing, this function never worked in Mac OS X344return NULL;345}346#endif // defined(_LIBUNWIND_SUPPORT_FRAME_APIS)347348#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)349350#endif // defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)351352353