diff options
Diffstat (limited to 'src/AddressSpace.hpp')
-rw-r--r-- | src/AddressSpace.hpp | 204 |
1 files changed, 113 insertions, 91 deletions
diff --git a/src/AddressSpace.hpp b/src/AddressSpace.hpp index 567cbda..402cfe0 100644 --- a/src/AddressSpace.hpp +++ b/src/AddressSpace.hpp @@ -32,67 +32,31 @@ namespace libunwind { #include "libunwind.h" #include "config.h" #include "dwarf2.h" -#include "Registers.hpp" - -#if _LIBUNWIND_ARM_EHABI -#ifdef __linux__ - -typedef long unsigned int *_Unwind_Ptr; -extern "C" _Unwind_Ptr __gnu_Unwind_Find_exidx(_Unwind_Ptr addr, int *len); - -// Emulate the BSD dl_unwind_find_exidx API when on a GNU libdl system. -#define dl_unwind_find_exidx __gnu_Unwind_Find_exidx - -#elif !defined(_LIBUNWIND_IS_BAREMETAL) -#include <link.h> -#else // !defined(_LIBUNWIND_IS_BAREMETAL) -// When statically linked on bare-metal, the symbols for the EH table are looked -// up without going through the dynamic loader. -struct EHTEntry { - uint32_t functionOffset; - uint32_t unwindOpcodes; -}; -extern EHTEntry __exidx_start; -extern EHTEntry __exidx_end; -#endif // !defined(_LIBUNWIND_IS_BAREMETAL) -#endif // _LIBUNWIND_ARM_EHABI - -#if defined(__CloudABI__) || defined(__FreeBSD__) || defined(__linux__) -#if _LIBUNWIND_SUPPORT_DWARF_UNWIND && _LIBUNWIND_SUPPORT_DWARF_INDEX -#include <link.h> -// Macro for machine-independent access to the ELF program headers. This -// macro is not available on some systems (e.g., FreeBSD). On these -// systems the data structures are just called Elf_XXX. Define ElfW() -// locally. -#if !defined(ElfW) -#define ElfW(type) Elf_##type -#endif #include "EHHeaderParser.hpp" -#endif -#endif +#include "Registers.hpp" namespace libunwind { /// Used by findUnwindSections() to return info about needed sections. struct UnwindInfoSections { -#if _LIBUNWIND_SUPPORT_DWARF_UNWIND || _LIBUNWIND_SUPPORT_DWARF_INDEX || \ - _LIBUNWIND_SUPPORT_COMPACT_UNWIND +#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) || defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) || \ + defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) // No dso_base for ARM EHABI. uintptr_t dso_base; #endif -#if _LIBUNWIND_SUPPORT_DWARF_UNWIND +#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) uintptr_t dwarf_section; uintptr_t dwarf_section_length; #endif -#if _LIBUNWIND_SUPPORT_DWARF_INDEX +#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) uintptr_t dwarf_index_section; uintptr_t dwarf_index_section_length; #endif -#if _LIBUNWIND_SUPPORT_COMPACT_UNWIND +#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) uintptr_t compact_unwind_section; uintptr_t compact_unwind_section_length; #endif -#if _LIBUNWIND_ARM_EHABI +#if defined(_LIBUNWIND_ARM_EHABI) uintptr_t arm_section; uintptr_t arm_section_length; #endif @@ -302,6 +266,7 @@ LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding, } #ifdef __APPLE__ + struct dyld_unwind_sections { const struct mach_header* mh; @@ -316,31 +281,57 @@ LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding, // In 10.7.0 or later, libSystem.dylib implements this function. extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *); #else - // In 10.6.x and earlier, we need to implement this functionality. + // In 10.6.x and earlier, we need to implement this functionality. Note + // that this requires a newer version of libmacho (from cctools) than is + // present in libSystem on 10.6.x (for getsectiondata). static inline bool _dyld_find_unwind_sections(void* addr, dyld_unwind_sections* info) { // Find mach-o image containing address. Dl_info dlinfo; if (!dladdr(addr, &dlinfo)) return false; - const mach_header *mh = (const mach_header *)dlinfo.dli_saddr; - - // Find dwarf unwind section in that image. - unsigned long size; - const uint8_t *p = getsectiondata(mh, "__TEXT", "__eh_frame", &size); - if (!p) - return false; - - // Fill in return struct. - info->mh = mh; - info->dwarf_section = p; - info->dwarf_section_length = size; - info->compact_unwind_section = 0; - info->compact_unwind_section_length = 0; - +#if __LP64__ + const struct mach_header_64 *mh = (const struct mach_header_64 *)dlinfo.dli_fbase; +#else + const struct mach_header *mh = (const struct mach_header *)dlinfo.dli_fbase; +#endif + + // Initialize the return struct + info->mh = (const struct mach_header *)mh; + info->dwarf_section = getsectiondata(mh, "__TEXT", "__eh_frame", &info->dwarf_section_length); + info->compact_unwind_section = getsectiondata(mh, "__TEXT", "__unwind_info", &info->compact_unwind_section_length); + + if (!info->dwarf_section) { + info->dwarf_section_length = 0; + } + + if (!info->compact_unwind_section) { + info->compact_unwind_section_length = 0; + } + return true; } #endif + +#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL) + +// When statically linked on bare-metal, the symbols for the EH table are looked +// up without going through the dynamic loader. +extern char __exidx_start; +extern char __exidx_end; + +#elif defined(_LIBUNWIND_ARM_EHABI) || defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) + +// ELF-based systems may use dl_iterate_phdr() to access sections +// containing unwinding information. The ElfW() macro for pointer-size +// independent ELF header traversal is not provided by <link.h> on some +// systems (e.g., FreeBSD). On these systems the data structures are +// just called Elf_XXX. Define ElfW() locally. +#include <link.h> +#if !defined(ElfW) +#define ElfW(type) Elf_##type +#endif + #endif inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr, @@ -349,7 +340,7 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr, dyld_unwind_sections dyldInfo; if (_dyld_find_unwind_sections((void *)targetAddr, &dyldInfo)) { info.dso_base = (uintptr_t)dyldInfo.mh; - #if _LIBUNWIND_SUPPORT_DWARF_UNWIND + #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) info.dwarf_section = (uintptr_t)dyldInfo.dwarf_section; info.dwarf_section_length = dyldInfo.dwarf_section_length; #endif @@ -357,23 +348,15 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr, info.compact_unwind_section_length = dyldInfo.compact_unwind_section_length; return true; } -#elif _LIBUNWIND_ARM_EHABI - #ifdef _LIBUNWIND_IS_BAREMETAL +#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL) // Bare metal is statically linked, so no need to ask the dynamic loader info.arm_section = (uintptr_t)(&__exidx_start); info.arm_section_length = (uintptr_t)(&__exidx_end - &__exidx_start); - #else - int length = 0; - info.arm_section = (uintptr_t) dl_unwind_find_exidx( - (_Unwind_Ptr) targetAddr, &length); - info.arm_section_length = (uintptr_t)length; - #endif - _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %X length %x\n", + _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %X length %x", info.arm_section, info.arm_section_length); if (info.arm_section && info.arm_section_length) return true; -#elif _LIBUNWIND_SUPPORT_DWARF_UNWIND -#if _LIBUNWIND_SUPPORT_DWARF_INDEX +#elif defined(_LIBUNWIND_ARM_EHABI) || defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) struct dl_iterate_cb_data { LocalAddressSpace *addressSpace; UnwindInfoSections *sects; @@ -384,7 +367,6 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr, int found = dl_iterate_phdr( [](struct dl_phdr_info *pinfo, size_t, void *data) -> int { auto cbdata = static_cast<dl_iterate_cb_data *>(data); - size_t object_length; bool found_obj = false; bool found_hdr = false; @@ -401,11 +383,32 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr, #if !defined(Elf_Phdr) typedef ElfW(Phdr) Elf_Phdr; #endif +#if !defined(Elf_Addr) && defined(__ANDROID__) + typedef ElfW(Addr) Elf_Addr; +#endif + + #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) + #if !defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) + #error "_LIBUNWIND_SUPPORT_DWARF_UNWIND requires _LIBUNWIND_SUPPORT_DWARF_INDEX on this platform." + #endif + size_t object_length; +#if defined(__ANDROID__) + Elf_Addr image_base = + pinfo->dlpi_phnum + ? reinterpret_cast<Elf_Addr>(pinfo->dlpi_phdr) - + reinterpret_cast<const Elf_Phdr *>(pinfo->dlpi_phdr) + ->p_offset + : 0; +#endif for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) { const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i]; if (phdr->p_type == PT_LOAD) { uintptr_t begin = pinfo->dlpi_addr + phdr->p_vaddr; +#if defined(__ANDROID__) + if (pinfo->dlpi_addr == 0 && phdr->p_vaddr < image_base) + begin = begin + image_base; +#endif uintptr_t end = begin + phdr->p_memsz; if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) { cbdata->sects->dso_base = begin; @@ -415,6 +418,10 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr, } else if (phdr->p_type == PT_GNU_EH_FRAME) { EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo; uintptr_t eh_frame_hdr_start = pinfo->dlpi_addr + phdr->p_vaddr; +#if defined(__ANDROID__) + if (pinfo->dlpi_addr == 0 && phdr->p_vaddr < image_base) + eh_frame_hdr_start = eh_frame_hdr_start + image_base; +#endif cbdata->sects->dwarf_index_section = eh_frame_hdr_start; cbdata->sects->dwarf_index_section_length = phdr->p_memsz; EHHeaderParser<LocalAddressSpace>::decodeEHHdr( @@ -431,12 +438,26 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr, } else { return false; } + #else // defined(_LIBUNWIND_ARM_EHABI) + for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) { + const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i]; + if (phdr->p_type == PT_LOAD) { + uintptr_t begin = pinfo->dlpi_addr + phdr->p_vaddr; + uintptr_t end = begin + phdr->p_memsz; + if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) + found_obj = true; + } else if (phdr->p_type == PT_ARM_EXIDX) { + uintptr_t exidx_start = pinfo->dlpi_addr + phdr->p_vaddr; + cbdata->sects->arm_section = exidx_start; + cbdata->sects->arm_section_length = phdr->p_memsz; + found_hdr = true; + } + } + return found_obj && found_hdr; + #endif }, &cb_data); return static_cast<bool>(found); -#else -#error "_LIBUNWIND_SUPPORT_DWARF_UNWIND requires _LIBUNWIND_SUPPORT_DWARF_INDEX on this platform." -#endif #endif return false; @@ -474,14 +495,14 @@ inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf, #ifdef UNW_REMOTE -/// OtherAddressSpace is used as a template parameter to UnwindCursor when +/// RemoteAddressSpace is used as a template parameter to UnwindCursor when /// unwinding a thread in the another process. The other process can be a /// different endianness and a different pointer size which is handled by /// the P template parameter. template <typename P> -class OtherAddressSpace { +class RemoteAddressSpace { public: - OtherAddressSpace(task_t task) : fTask(task) {} + RemoteAddressSpace(task_t task) : fTask(task) {} typedef typename P::uint_t pint_t; @@ -504,29 +525,29 @@ private: task_t fTask; }; -template <typename P> uint8_t OtherAddressSpace<P>::get8(pint_t addr) { +template <typename P> uint8_t RemoteAddressSpace<P>::get8(pint_t addr) { return *((uint8_t *)localCopy(addr)); } -template <typename P> uint16_t OtherAddressSpace<P>::get16(pint_t addr) { +template <typename P> uint16_t RemoteAddressSpace<P>::get16(pint_t addr) { return P::E::get16(*(uint16_t *)localCopy(addr)); } -template <typename P> uint32_t OtherAddressSpace<P>::get32(pint_t addr) { +template <typename P> uint32_t RemoteAddressSpace<P>::get32(pint_t addr) { return P::E::get32(*(uint32_t *)localCopy(addr)); } -template <typename P> uint64_t OtherAddressSpace<P>::get64(pint_t addr) { +template <typename P> uint64_t RemoteAddressSpace<P>::get64(pint_t addr) { return P::E::get64(*(uint64_t *)localCopy(addr)); } template <typename P> -typename P::uint_t OtherAddressSpace<P>::getP(pint_t addr) { +typename P::uint_t RemoteAddressSpace<P>::getP(pint_t addr) { return P::getP(*(uint64_t *)localCopy(addr)); } template <typename P> -uint64_t OtherAddressSpace<P>::getULEB128(pint_t &addr, pint_t end) { +uint64_t RemoteAddressSpace<P>::getULEB128(pint_t &addr, pint_t end) { uintptr_t size = (end - addr); LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr); LocalAddressSpace::pint_t sladdr = laddr; @@ -536,7 +557,7 @@ uint64_t OtherAddressSpace<P>::getULEB128(pint_t &addr, pint_t end) { } template <typename P> -int64_t OtherAddressSpace<P>::getSLEB128(pint_t &addr, pint_t end) { +int64_t RemoteAddressSpace<P>::getSLEB128(pint_t &addr, pint_t end) { uintptr_t size = (end - addr); LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr); LocalAddressSpace::pint_t sladdr = laddr; @@ -545,13 +566,14 @@ int64_t OtherAddressSpace<P>::getSLEB128(pint_t &addr, pint_t end) { return result; } -template <typename P> void *OtherAddressSpace<P>::localCopy(pint_t addr) { +template <typename P> void *RemoteAddressSpace<P>::localCopy(pint_t addr) { // FIX ME } template <typename P> -bool OtherAddressSpace<P>::findFunctionName(pint_t addr, char *buf, - size_t bufLen, unw_word_t *offset) { +bool RemoteAddressSpace<P>::findFunctionName(pint_t addr, char *buf, + size_t bufLen, + unw_word_t *offset) { // FIX ME } @@ -567,7 +589,7 @@ struct unw_addr_space { /// a 32-bit intel process. struct unw_addr_space_i386 : public unw_addr_space { unw_addr_space_i386(task_t task) : oas(task) {} - OtherAddressSpace<Pointer32<LittleEndian> > oas; + RemoteAddressSpace<Pointer32<LittleEndian>> oas; }; /// unw_addr_space_x86_64 is the concrete instance that a unw_addr_space_t @@ -575,7 +597,7 @@ struct unw_addr_space_i386 : public unw_addr_space { /// a 64-bit intel process. struct unw_addr_space_x86_64 : public unw_addr_space { unw_addr_space_x86_64(task_t task) : oas(task) {} - OtherAddressSpace<Pointer64<LittleEndian> > oas; + RemoteAddressSpace<Pointer64<LittleEndian>> oas; }; /// unw_addr_space_ppc is the concrete instance that a unw_addr_space_t points @@ -583,7 +605,7 @@ struct unw_addr_space_x86_64 : public unw_addr_space { /// a 32-bit PowerPC process. struct unw_addr_space_ppc : public unw_addr_space { unw_addr_space_ppc(task_t task) : oas(task) {} - OtherAddressSpace<Pointer32<BigEndian> > oas; + RemoteAddressSpace<Pointer32<BigEndian>> oas; }; #endif // UNW_REMOTE |