📄 unwind-arm.c
字号:
/* ARM EABI compliant unwinding routines. Copyright (C) 2004, 2005 Free Software Foundation, Inc. Contributed by Paul Brook This file is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. In addition to the permissions in the GNU General Public License, the Free Software Foundation gives you unlimited permission to link the compiled version of this file into combinations with other programs, and to distribute those combinations without any restriction coming from the use of this file. (The General Public License restrictions do apply in other respects; for example, they cover modification of the file, and distribution when not linked into a combine executable.) This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */#include "unwind.h"/* We add a prototype for abort here to avoid creating a dependency on target headers. */extern void abort (void);/* Definitions for C++ runtime support routines. We make these weak declarations to avoid pulling in libsupc++ unnecessarily. */typedef unsigned char bool;typedef struct _ZSt9type_info type_info; /* This names C++ type_info type */void __attribute__((weak)) __cxa_call_unexpected(_Unwind_Control_Block *ucbp);bool __attribute__((weak)) __cxa_begin_cleanup(_Unwind_Control_Block *ucbp);bool __attribute__((weak)) __cxa_type_match(_Unwind_Control_Block *ucbp, const type_info *rttip, void **matched_object);_Unwind_Ptr __attribute__((weak))__gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);/* Misc constants. */#define R_IP 12#define R_SP 13#define R_LR 14#define R_PC 15#define EXIDX_CANTUNWIND 1#define uint32_highbit (((_uw) 1) << 31)#define UCB_FORCED_STOP_FN(ucbp) ((ucbp)->unwinder_cache.reserved1)#define UCB_PR_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved2)#define UCB_SAVED_CALLSITE_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved3)#define UCB_FORCED_STOP_ARG(ucbp) ((ucbp)->unwinder_cache.reserved4)struct core_regs{ _uw r[16];};/* We use normal integer types here to avoid the compiler generating coprocessor instructions. */struct vfp_regs{ _uw64 d[16]; _uw pad;};struct fpa_reg{ _uw w[3];};struct fpa_regs{ struct fpa_reg f[8];};/* Unwind descriptors. */typedef struct{ _uw16 length; _uw16 offset;} EHT16;typedef struct{ _uw length; _uw offset;} EHT32;/* The ABI specifies that the unwind routines may only use core registers, except when actually manipulating coprocessor state. This allows us to write one implementation that works on all platforms by demand-saving coprocessor registers. During unwinding we hold the coprocessor state in the actual hardware registers and allocate demand-save areas for use during phase1 unwinding. */typedef struct{ /* The first fields must be the same as a phase2_vrs. */ _uw demand_save_flags; struct core_regs core; _uw prev_sp; /* Only valid during forced unwinding. */ struct vfp_regs vfp; struct fpa_regs fpa;} phase1_vrs;#define DEMAND_SAVE_VFP 1/* This must match the structure created by the assembly wrappers. */typedef struct{ _uw demand_save_flags; struct core_regs core;} phase2_vrs;/* An exception index table entry. */typedef struct __EIT_entry{ _uw fnoffset; _uw content;} __EIT_entry;/* Assembly helper functions. *//* Restore core register state. Never returns. */void __attribute__((noreturn)) restore_core_regs (struct core_regs *);/* Coprocessor register state manipulation functions. */void __gnu_Unwind_Save_VFP (struct vfp_regs * p);void __gnu_Unwind_Restore_VFP (struct vfp_regs * p);/* Restore coprocessor state after phase1 unwinding. */static voidrestore_non_core_regs (phase1_vrs * vrs){ if ((vrs->demand_save_flags & DEMAND_SAVE_VFP) == 0) __gnu_Unwind_Restore_VFP (&vrs->vfp);}/* A better way to do this would probably be to compare the absolute address with a segment relative relocation of the same symbol. */extern int __text_start;extern int __data_start;/* The exception index table location. */extern __EIT_entry __exidx_start;extern __EIT_entry __exidx_end;/* ABI defined personality routines. */extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr0 (_Unwind_State, _Unwind_Control_Block *, _Unwind_Context *);// __attribute__((weak));extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr1 (_Unwind_State, _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr2 (_Unwind_State, _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));/* ABI defined routine to store a virtual register to memory. */_Unwind_VRS_Result _Unwind_VRS_Get (_Unwind_Context *context, _Unwind_VRS_RegClass regclass, _uw regno, _Unwind_VRS_DataRepresentation representation, void *valuep){ phase1_vrs *vrs = (phase1_vrs *) context; switch (regclass) { case _UVRSC_CORE: if (representation != _UVRSD_UINT32 || regno > 15) return _UVRSR_FAILED; *(_uw *) valuep = vrs->core.r[regno]; return _UVRSR_OK; case _UVRSC_VFP: case _UVRSC_FPA: case _UVRSC_WMMXD: case _UVRSC_WMMXC: return _UVRSR_NOT_IMPLEMENTED; default: return _UVRSR_FAILED; }}/* ABI defined function to load a virtual register from memory. */_Unwind_VRS_Result _Unwind_VRS_Set (_Unwind_Context *context, _Unwind_VRS_RegClass regclass, _uw regno, _Unwind_VRS_DataRepresentation representation, void *valuep){ phase1_vrs *vrs = (phase1_vrs *) context; switch (regclass) { case _UVRSC_CORE: if (representation != _UVRSD_UINT32 || regno > 15) return _UVRSR_FAILED; vrs->core.r[regno] = *(_uw *) valuep; return _UVRSR_OK; case _UVRSC_VFP: case _UVRSC_FPA: case _UVRSC_WMMXD: case _UVRSC_WMMXC: return _UVRSR_NOT_IMPLEMENTED; default: return _UVRSR_FAILED; }}/* ABI defined function to pop registers off the stack. */_Unwind_VRS_Result _Unwind_VRS_Pop (_Unwind_Context *context, _Unwind_VRS_RegClass regclass, _uw discriminator, _Unwind_VRS_DataRepresentation representation){ phase1_vrs *vrs = (phase1_vrs *) context; switch (regclass) { case _UVRSC_CORE: { _uw *ptr; _uw mask; int i; if (representation != _UVRSD_UINT32) return _UVRSR_FAILED; mask = discriminator & 0xffff; ptr = (_uw *) vrs->core.r[R_SP]; /* Pop the requested registers. */ for (i = 0; i < 16; i++) { if (mask & (1 << i)) vrs->core.r[i] = *(ptr++); } /* Writeback the stack pointer value if it wasn't restored. */ if ((mask & (1 << R_SP)) == 0) vrs->core.r[R_SP] = (_uw) ptr; } return _UVRSR_OK; case _UVRSC_VFP: { _uw start = discriminator >> 16; _uw count = discriminator & 0xffff; struct vfp_regs tmp; _uw *sp; _uw *dest; if ((representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE) || start + count > 16) return _UVRSR_FAILED; if (vrs->demand_save_flags & DEMAND_SAVE_VFP) { /* Demand-save resisters for stage1. */ vrs->demand_save_flags &= ~DEMAND_SAVE_VFP; __gnu_Unwind_Save_VFP (&vrs->vfp); } /* Restore the registers from the stack. Do this by saving the current VFP registers to a memory area, moving the in-memory values into that area, and restoring from the whole area. For _UVRSD_VFPX we assume FSTMX standard format 1. */ __gnu_Unwind_Save_VFP (&tmp); /* The stack address is only guaranteed to be word aligned, so we can't use doubleword copies. */ sp = (_uw *) vrs->core.r[R_SP]; dest = (_uw *) &tmp.d[start]; count *= 2; while (count--) *(dest++) = *(sp++); /* Skip the pad word */ if (representation == _UVRSD_VFPX) sp++; /* Set the new stack pointer. */ vrs->core.r[R_SP] = (_uw) sp; /* Reload the registers. */ __gnu_Unwind_Restore_VFP (&tmp); } return _UVRSR_OK; case _UVRSC_FPA: case _UVRSC_WMMXD: case _UVRSC_WMMXC: return _UVRSR_NOT_IMPLEMENTED; default: return _UVRSR_FAILED; }}/* Core unwinding functions. *//* Calculate the address encoded by a 31-bit self-relative offset at address P. */static inline _uwselfrel_offset31 (const _uw *p){ _uw offset; offset = *p; /* Sign extend to 32 bits. */ if (offset & (1 << 30)) offset |= 1u << 31; return offset + (_uw) p;}/* Perform a binary search for RETURN_ADDRESS in TABLE. The table contains NREC entries. */static const __EIT_entry *search_EIT_table (const __EIT_entry * table, int nrec, _uw return_address){ _uw next_fn; _uw this_fn; int n, left, right; if (nrec == 0) return (__EIT_entry *) 0; left = 0; right = nrec - 1; while (1) { n = (left + right) / 2; this_fn = selfrel_offset31 (&table[n].fnoffset); if (n != nrec - 1) next_fn = selfrel_offset31 (&table[n + 1].fnoffset) - 1; else next_fn = (_uw)0 - 1; if (return_address < this_fn) { if (n == left) return (__EIT_entry *) 0; right = n - 1; } else if (return_address <= next_fn) return &table[n]; else left = n + 1; }}/* Find the exception index table eintry for the given address. Fill in the relevant fields of the UCB. Returns _URC_FAILURE if an error occurred, _URC_OK on success. */static _Unwind_Reason_Codeget_eit_entry (_Unwind_Control_Block *ucbp, _uw return_address){ const __EIT_entry * eitp; int nrec; /* The return address is the address of the instruction following the call instruction (plus one in thumb mode). If this was the last instruction in the function the address will lie in the following function. Subtract 2 from the address so that it points within the call instruction itself. */ return_address -= 2; if (__gnu_Unwind_Find_exidx) { eitp = (const __EIT_entry *) __gnu_Unwind_Find_exidx (return_address, &nrec); if (!eitp) { UCB_PR_ADDR (ucbp) = 0; return _URC_FAILURE; } } else { eitp = &__exidx_start; nrec = &__exidx_end - &__exidx_start; } eitp = search_EIT_table (eitp, nrec, return_address); if (!eitp) { UCB_PR_ADDR (ucbp) = 0; return _URC_FAILURE; } ucbp->pr_cache.fnstart = selfrel_offset31 (&eitp->fnoffset); /* Can this frame be unwound at all? */ if (eitp->content == EXIDX_CANTUNWIND) { UCB_PR_ADDR (ucbp) = 0; return _URC_END_OF_STACK; } /* Obtain the address of the "real" __EHT_Header word. */ if (eitp->content & uint32_highbit) { /* It is immediate data. */ ucbp->pr_cache.ehtp = (_Unwind_EHT_Header *)&eitp->content; ucbp->pr_cache.additional = 1; } else { /* The low 31 bits of the content field are a self-relative offset to an _Unwind_EHT_Entry structure. */ ucbp->pr_cache.ehtp = (_Unwind_EHT_Header *) selfrel_offset31 (&eitp->content); ucbp->pr_cache.additional = 0; } /* Discover the personality routine address. */ if (*ucbp->pr_cache.ehtp & (1u << 31)) { /* One of the predefined standard routines. */ _uw idx = (*(_uw *) ucbp->pr_cache.ehtp >> 24) & 0xf; if (idx == 0) UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr0; else if (idx == 1) UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr1; else if (idx == 2) UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr2; else { /* Failed */ UCB_PR_ADDR (ucbp) = 0; return _URC_FAILURE; } } else { /* Execute region offset to PR */ UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp); } return _URC_OK;}/* Perform phase2 unwinding. VRS is the initial virtual register state. */static void __attribute__((noreturn))unwind_phase2 (_Unwind_Control_Block * ucbp, phase2_vrs * vrs){ _Unwind_Reason_Code pr_result; do { /* Find the entry for this routine. */ if (get_eit_entry (ucbp, vrs->core.r[R_PC]) != _URC_OK) abort (); UCB_SAVED_CALLSITE_ADDR (ucbp) = vrs->core.r[R_PC]; /* Call the pr to decide what to do. */ pr_result = ((personality_routine) UCB_PR_ADDR (ucbp)) (_US_UNWIND_FRAME_STARTING, ucbp, (_Unwind_Context *) vrs); } while (pr_result == _URC_CONTINUE_UNWIND); if (pr_result != _URC_INSTALL_CONTEXT) abort(); restore_core_regs (&vrs->core);}/* Perform phase2 forced unwinding. */static _Unwind_Reason_Codeunwind_phase2_forced (_Unwind_Control_Block *ucbp, phase2_vrs *entry_vrs, int resuming){ _Unwind_Stop_Fn stop_fn = (_Unwind_Stop_Fn) UCB_FORCED_STOP_FN (ucbp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -