📄 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. *//**************************************************************************** * The functions here are derived from gcc/config/arm/unwind-arm.c from the * 4.3.x release. The main changes here involve the use of ptrace to retrieve * memory/processor states from a remote process. ****************************************************************************/#include <cutils/logd.h>#include <sys/ptrace.h>#include <unwind.h>#include "utility.h"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, bool is_reference, void **matched_object);/* 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 vfpv3_regs{ /* Always populated via VSTM, so no need for the "pad" field from vfp_regs (which is used to store the format word for FSTMX). */ _uw64 d[16];};struct fpa_reg{ _uw w[3];};struct fpa_regs{ struct fpa_reg f[8];};struct wmmxd_regs{ _uw64 wd[16];};struct wmmxc_regs{ _uw wc[4];};/* 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 vfpv3_regs vfp_regs_16_to_31; struct fpa_regs fpa; struct wmmxd_regs wmmxd; struct wmmxc_regs wmmxc;} phase1_vrs;/* 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;/* Derived version to use ptrace */typedef _Unwind_Reason_Code (*personality_routine_with_ptrace) (_Unwind_State, _Unwind_Control_Block *, _Unwind_Context *, pid_t);/* Derived version to use ptrace *//* ABI defined personality routines. */static _Unwind_Reason_Code unwind_cpp_pr0_with_ptrace (_Unwind_State, _Unwind_Control_Block *, _Unwind_Context *, pid_t);static _Unwind_Reason_Code unwind_cpp_pr1_with_ptrace (_Unwind_State, _Unwind_Control_Block *, _Unwind_Context *, pid_t);static _Unwind_Reason_Code unwind_cpp_pr2_with_ptrace (_Unwind_State, _Unwind_Control_Block *, _Unwind_Context *, pid_t);/* Execute the unwinding instructions described by UWS. */extern _Unwind_Reason_Codeunwind_execute_with_ptrace(_Unwind_Context * context, __gnu_unwind_state * uws, pid_t pid);/* Derived version to use ptrace. Only handles core registers. Disregards * FP and others. *//* ABI defined function to pop registers off the stack. */_Unwind_VRS_Result unwind_VRS_Pop_with_ptrace (_Unwind_Context *context, _Unwind_VRS_RegClass regclass, _uw discriminator, _Unwind_VRS_DataRepresentation representation, pid_t pid){ 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] = get_remote_word(pid, ptr); 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; 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, pid_t pid){ _uw offset = get_remote_word(pid, (void*)p); //offset = *p; /* Sign extend to 32 bits. */ if (offset & (1 << 30)) offset |= 1u << 31; else 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, pid_t pid){ _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, pid); if (n != nrec - 1) next_fn = selfrel_offset31 (&table[n + 1].fnoffset, pid) - 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. */static const __EIT_entry*get_eitp(_uw return_address, pid_t pid, mapinfo *map, mapinfo **containing_map){ const __EIT_entry *eitp = NULL; int nrec; mapinfo *mi; /* 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. */ if (return_address >= 2) return_address -= 2; for (mi = map; mi != NULL; mi = mi->next) { if (return_address >= mi->start && return_address <= mi->end) break; } if (mi) { if (containing_map) *containing_map = mi; eitp = (__EIT_entry *) mi->exidx_start; nrec = (mi->exidx_end - mi->exidx_start)/sizeof(__EIT_entry); eitp = search_EIT_table (eitp, nrec, return_address, pid); } return eitp;}/* Find the exception index table eintry for the given address. Fill in the relevant fields of the UCB.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -