Path: blob/main/contrib/llvm-project/libunwind/src/UnwindLevel1-gcc-ext.c
35154 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}208#ifdef __arm__209/* Preserve legacy libgcc (pre r318024) ARM ABI mistake */210__sym_compat(_Unwind_Backtrace, _Unwind_Backtrace, GCC_3.3);211#endif212213214/// Find DWARF unwind info for an address 'pc' in some function.215_LIBUNWIND_EXPORT const void *_Unwind_Find_FDE(const void *pc,216struct dwarf_eh_bases *bases) {217// This is slow, but works.218// We create an unwind cursor then alter the IP to be pc219unw_cursor_t cursor;220unw_context_t uc;221unw_proc_info_t info;222__unw_getcontext(&uc);223__unw_init_local(&cursor, &uc);224__unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t)pc);225__unw_get_proc_info(&cursor, &info);226bases->tbase = (uintptr_t)info.extra;227bases->dbase = 0; // dbase not used on Mac OS X228bases->func = (uintptr_t)info.start_ip;229_LIBUNWIND_TRACE_API("_Unwind_Find_FDE(pc=%p) => %p", pc,230(void *)(intptr_t) info.unwind_info);231return (void *)(intptr_t) info.unwind_info;232}233234/// Returns the CFA (call frame area, or stack pointer at start of function)235/// for the current context.236_LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) {237unw_cursor_t *cursor = (unw_cursor_t *)context;238unw_word_t result;239__unw_get_reg(cursor, UNW_REG_SP, &result);240_LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p) => 0x%" PRIxPTR,241(void *)context, result);242return (uintptr_t)result;243}244245246/// Called by personality handler during phase 2 to get instruction pointer.247/// ipBefore is a boolean that says if IP is already adjusted to be the call248/// site address. Normally IP is the return address.249_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,250int *ipBefore) {251_LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p)", (void *)context);252int isSignalFrame = __unw_is_signal_frame((unw_cursor_t *)context);253// Negative means some kind of error (probably UNW_ENOINFO), but we have no254// good way to report that, and this maintains backward compatibility with the255// implementation that hard-coded zero in every case, even signal frames.256if (isSignalFrame <= 0)257*ipBefore = 0;258else259*ipBefore = 1;260return _Unwind_GetIP(context);261}262263#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)264265#if defined(__FreeBSD__)266267// Based on LLVM's lib/ExecutionEngine/RuntimeDyld/RTDyldMemoryManager.cpp268// and XXX should be fixed to be alignment-safe.269static void processFDE(const char *addr, bool isDeregister) {270uint64_t length;271while ((length = *((const uint32_t *)addr)) != 0) {272const char *p = addr + 4;273if (length == 0xffffffff) {274length = *((const uint64_t *)p);275p += 8;276}277uint32_t offset = *((const uint32_t *)p);278if (offset != 0) {279if (isDeregister)280__unw_remove_dynamic_fde((unw_word_t)(uintptr_t)addr);281else282__unw_add_dynamic_fde((unw_word_t)(uintptr_t)addr);283}284addr = p + length;285}286}287288/// Called by programs with dynamic code generators that want to register289/// dynamically generated FDEs, with a libgcc-compatible API.290291_LIBUNWIND_EXPORT void __register_frame(const void *addr) {292_LIBUNWIND_TRACE_API("__register_frame(%p)", addr);293processFDE(addr, false);294}295296/// Called by programs with dynamic code generators that want to unregister297/// dynamically generated FDEs, with a libgcc-compatible API.298_LIBUNWIND_EXPORT void __deregister_frame(const void *addr) {299_LIBUNWIND_TRACE_API("__deregister_frame(%p)", addr);300processFDE(addr, true);301}302303#else // defined(__FreeBSD__)304305/// Called by programs with dynamic code generators that want306/// to register a dynamically generated FDE.307/// This function has existed on Mac OS X since 10.4, but308/// was broken until 10.6.309_LIBUNWIND_EXPORT void __register_frame(const void *fde) {310_LIBUNWIND_TRACE_API("__register_frame(%p)", fde);311__unw_add_dynamic_fde((unw_word_t)(uintptr_t)fde);312}313314315/// Called by programs with dynamic code generators that want316/// to unregister a dynamically generated FDE.317/// This function has existed on Mac OS X since 10.4, but318/// was broken until 10.6.319_LIBUNWIND_EXPORT void __deregister_frame(const void *fde) {320_LIBUNWIND_TRACE_API("__deregister_frame(%p)", fde);321__unw_remove_dynamic_fde((unw_word_t)(uintptr_t)fde);322}323324#endif // defined(__FreeBSD__)325326// The following register/deregister functions are gcc extensions.327// They have existed on Mac OS X, but have never worked because Mac OS X328// before 10.6 used keymgr to track known FDEs, but these functions329// never got updated to use keymgr.330// For now, we implement these as do-nothing functions to keep any existing331// applications working. We also add the not in 10.6 symbol so that nwe332// application won't be able to use them.333334#if defined(_LIBUNWIND_SUPPORT_FRAME_APIS)335_LIBUNWIND_EXPORT void __register_frame_info_bases(const void *fde, void *ob,336void *tb, void *db) {337(void)fde;338(void)ob;339(void)tb;340(void)db;341_LIBUNWIND_TRACE_API("__register_frame_info_bases(%p,%p, %p, %p)",342fde, ob, tb, db);343// do nothing, this function never worked in Mac OS X344}345346_LIBUNWIND_EXPORT void __register_frame_info(const void *fde, void *ob) {347(void)fde;348(void)ob;349_LIBUNWIND_TRACE_API("__register_frame_info(%p, %p)", fde, ob);350// do nothing, this function never worked in Mac OS X351}352353_LIBUNWIND_EXPORT void __register_frame_info_table_bases(const void *fde,354void *ob, void *tb,355void *db) {356(void)fde;357(void)ob;358(void)tb;359(void)db;360_LIBUNWIND_TRACE_API("__register_frame_info_table_bases"361"(%p,%p, %p, %p)", fde, ob, tb, db);362// do nothing, this function never worked in Mac OS X363}364365_LIBUNWIND_EXPORT void __register_frame_info_table(const void *fde, void *ob) {366(void)fde;367(void)ob;368_LIBUNWIND_TRACE_API("__register_frame_info_table(%p, %p)", fde, ob);369// do nothing, this function never worked in Mac OS X370}371372_LIBUNWIND_EXPORT void __register_frame_table(const void *fde) {373(void)fde;374_LIBUNWIND_TRACE_API("__register_frame_table(%p)", fde);375// do nothing, this function never worked in Mac OS X376}377378_LIBUNWIND_EXPORT void *__deregister_frame_info(const void *fde) {379(void)fde;380_LIBUNWIND_TRACE_API("__deregister_frame_info(%p)", fde);381// do nothing, this function never worked in Mac OS X382return NULL;383}384385_LIBUNWIND_EXPORT void *__deregister_frame_info_bases(const void *fde) {386(void)fde;387_LIBUNWIND_TRACE_API("__deregister_frame_info_bases(%p)", fde);388// do nothing, this function never worked in Mac OS X389return NULL;390}391#endif // defined(_LIBUNWIND_SUPPORT_FRAME_APIS)392393#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)394395#endif // defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)396397398