diff options
author | orivej <orivej@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
commit | 2d37894b1b037cf24231090eda8589bbb44fb6fc (patch) | |
tree | be835aa92c6248212e705f25388ebafcf84bc7a1 /contrib/libs/libunwind/src/AddressSpace.hpp | |
parent | 718c552901d703c502ccbefdfc3c9028d608b947 (diff) | |
download | ydb-2d37894b1b037cf24231090eda8589bbb44fb6fc.tar.gz |
Restoring authorship annotation for <orivej@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/libs/libunwind/src/AddressSpace.hpp')
-rw-r--r-- | contrib/libs/libunwind/src/AddressSpace.hpp | 668 |
1 files changed, 334 insertions, 334 deletions
diff --git a/contrib/libs/libunwind/src/AddressSpace.hpp b/contrib/libs/libunwind/src/AddressSpace.hpp index d15d5302a7..0c4dfeb4e6 100644 --- a/contrib/libs/libunwind/src/AddressSpace.hpp +++ b/contrib/libs/libunwind/src/AddressSpace.hpp @@ -1,8 +1,8 @@ //===----------------------------------------------------------------------===// // -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // // // Abstracts accessing local vs remote address spaces. @@ -17,125 +17,125 @@ #include <stdlib.h> #include <string.h> -#include "libunwind.h" -#include "config.h" -#include "dwarf2.h" -#include "EHHeaderParser.hpp" -#include "Registers.hpp" - -#ifndef _LIBUNWIND_USE_DLADDR - #if !defined(_LIBUNWIND_IS_BAREMETAL) && !defined(_WIN32) - #define _LIBUNWIND_USE_DLADDR 1 - #else - #define _LIBUNWIND_USE_DLADDR 0 - #endif -#endif - -#if _LIBUNWIND_USE_DLADDR +#include "libunwind.h" +#include "config.h" +#include "dwarf2.h" +#include "EHHeaderParser.hpp" +#include "Registers.hpp" + +#ifndef _LIBUNWIND_USE_DLADDR + #if !defined(_LIBUNWIND_IS_BAREMETAL) && !defined(_WIN32) + #define _LIBUNWIND_USE_DLADDR 1 + #else + #define _LIBUNWIND_USE_DLADDR 0 + #endif +#endif + +#if _LIBUNWIND_USE_DLADDR #include <dlfcn.h> -#if defined(__ELF__) && defined(_LIBUNWIND_LINK_DL_LIB) -#pragma comment(lib, "dl") +#if defined(__ELF__) && defined(_LIBUNWIND_LINK_DL_LIB) +#pragma comment(lib, "dl") +#endif +#endif + +#if defined(_LIBUNWIND_ARM_EHABI) +struct EHABIIndexEntry { + uint32_t functionOffset; + uint32_t data; +}; #endif -#endif - -#if defined(_LIBUNWIND_ARM_EHABI) -struct EHABIIndexEntry { - uint32_t functionOffset; - uint32_t data; -}; -#endif - + #ifdef __APPLE__ - struct dyld_unwind_sections - { - const struct mach_header* mh; - const void* dwarf_section; - uintptr_t dwarf_section_length; - const void* compact_unwind_section; - uintptr_t compact_unwind_section_length; - }; + struct dyld_unwind_sections + { + const struct mach_header* mh; + const void* dwarf_section; + uintptr_t dwarf_section_length; + const void* compact_unwind_section; + uintptr_t compact_unwind_section_length; + }; + + // In 10.7.0 or later, libSystem.dylib implements this function. + extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *); - // In 10.7.0 or later, libSystem.dylib implements this function. - extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *); +#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL) -#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && 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. -// The following linker script may be used to produce the necessary sections and symbols. -// Unless the --eh-frame-hdr linker option is provided, the section is not generated -// and does not take space in the output file. -// -// .eh_frame : -// { -// __eh_frame_start = .; -// KEEP(*(.eh_frame)) -// __eh_frame_end = .; -// } -// -// .eh_frame_hdr : -// { -// KEEP(*(.eh_frame_hdr)) -// } -// -// __eh_frame_hdr_start = SIZEOF(.eh_frame_hdr) > 0 ? ADDR(.eh_frame_hdr) : 0; -// __eh_frame_hdr_end = SIZEOF(.eh_frame_hdr) > 0 ? . : 0; - -extern char __eh_frame_start; -extern char __eh_frame_end; - -#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) -extern char __eh_frame_hdr_start; -extern char __eh_frame_hdr_end; -#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_SUPPORT_DWARF_UNWIND) && defined(_WIN32) - -#include <windows.h> -#include <psapi.h> - -#elif defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) || \ - defined(_LIBUNWIND_USE_DL_UNWIND_FIND_EXIDX) - -#include <link.h> - +// The following linker script may be used to produce the necessary sections and symbols. +// Unless the --eh-frame-hdr linker option is provided, the section is not generated +// and does not take space in the output file. +// +// .eh_frame : +// { +// __eh_frame_start = .; +// KEEP(*(.eh_frame)) +// __eh_frame_end = .; +// } +// +// .eh_frame_hdr : +// { +// KEEP(*(.eh_frame_hdr)) +// } +// +// __eh_frame_hdr_start = SIZEOF(.eh_frame_hdr) > 0 ? ADDR(.eh_frame_hdr) : 0; +// __eh_frame_hdr_end = SIZEOF(.eh_frame_hdr) > 0 ? . : 0; + +extern char __eh_frame_start; +extern char __eh_frame_end; + +#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) +extern char __eh_frame_hdr_start; +extern char __eh_frame_hdr_end; +#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_SUPPORT_DWARF_UNWIND) && defined(_WIN32) + +#include <windows.h> +#include <psapi.h> + +#elif defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) || \ + defined(_LIBUNWIND_USE_DL_UNWIND_FIND_EXIDX) + +#include <link.h> + #endif namespace libunwind { /// Used by findUnwindSections() to return info about needed sections. struct UnwindInfoSections { -#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) || \ - defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) || \ - defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) - // No dso_base for SEH. +#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) || \ + defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) || \ + defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) + // No dso_base for SEH. uintptr_t dso_base; #endif -#if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) +#if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) size_t text_segment_length; -#endif -#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) +#endif +#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) uintptr_t dwarf_section; size_t dwarf_section_length; #endif -#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) +#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) uintptr_t dwarf_index_section; size_t dwarf_index_section_length; #endif -#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) +#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) uintptr_t compact_unwind_section; size_t compact_unwind_section_length; #endif -#if defined(_LIBUNWIND_ARM_EHABI) +#if defined(_LIBUNWIND_ARM_EHABI) uintptr_t arm_section; size_t arm_section_length; #endif @@ -145,10 +145,10 @@ struct UnwindInfoSections { /// LocalAddressSpace is used as a template parameter to UnwindCursor when /// unwinding a thread in the same process. The wrappers compile away, /// making local unwinds fast. -class _LIBUNWIND_HIDDEN LocalAddressSpace { +class _LIBUNWIND_HIDDEN LocalAddressSpace { public: - typedef uintptr_t pint_t; - typedef intptr_t sint_t; + typedef uintptr_t pint_t; + typedef intptr_t sint_t; uint8_t get8(pint_t addr) { uint8_t val; memcpy(&val, (void *)addr, sizeof(val)); @@ -180,7 +180,7 @@ public: return val; } uintptr_t getP(pint_t addr); - uint64_t getRegister(pint_t addr); + uint64_t getRegister(pint_t addr); static uint64_t getULEB128(pint_t &addr, pint_t end); static int64_t getSLEB128(pint_t &addr, pint_t end); @@ -195,21 +195,21 @@ public: }; inline uintptr_t LocalAddressSpace::getP(pint_t addr) { -#if __SIZEOF_POINTER__ == 8 +#if __SIZEOF_POINTER__ == 8 + return get64(addr); +#else + return get32(addr); +#endif +} + +inline uint64_t LocalAddressSpace::getRegister(pint_t addr) { +#if __SIZEOF_POINTER__ == 8 || defined(__mips64) return get64(addr); #else return get32(addr); #endif } -inline uint64_t LocalAddressSpace::getRegister(pint_t addr) { -#if __SIZEOF_POINTER__ == 8 || defined(__mips64) - return get64(addr); -#else - return get32(addr); -#endif -} - /// Read a ULEB128 into a 64-bit word. inline uint64_t LocalAddressSpace::getULEB128(pint_t &addr, pint_t end) { const uint8_t *p = (uint8_t *)addr; @@ -246,11 +246,11 @@ inline int64_t LocalAddressSpace::getSLEB128(pint_t &addr, pint_t end) { if (p == pend) _LIBUNWIND_ABORT("truncated sleb128 expression"); byte = *p++; - result |= (uint64_t)(byte & 0x7f) << bit; + result |= (uint64_t)(byte & 0x7f) << bit; bit += 7; } while (byte & 0x80); // sign extend negative numbers - if ((byte & 0x40) != 0 && bit < 64) + if ((byte & 0x40) != 0 && bit < 64) result |= (-1ULL) << bit; addr = (pint_t) p; return result; @@ -348,163 +348,163 @@ LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding, return result; } -#if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) - -// 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. -#if !defined(ElfW) - #define ElfW(type) Elf_##type -#endif -#if !defined(Elf_Half) - typedef ElfW(Half) Elf_Half; -#endif -#if !defined(Elf_Phdr) - typedef ElfW(Phdr) Elf_Phdr; -#endif -#if !defined(Elf_Addr) - typedef ElfW(Addr) Elf_Addr; -#endif - -static Elf_Addr calculateImageBase(struct dl_phdr_info *pinfo) { - Elf_Addr image_base = pinfo->dlpi_addr; -#if defined(__ANDROID__) && __ANDROID_API__ < 18 - if (image_base == 0) { - // Normally, an image base of 0 indicates a non-PIE executable. On - // versions of Android prior to API 18, the dynamic linker reported a - // dlpi_addr of 0 for PIE executables. Compute the true image base - // using the PT_PHDR segment. - // See https://github.com/android/ndk/issues/505. - for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) { - const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i]; - if (phdr->p_type == PT_PHDR) { - image_base = reinterpret_cast<Elf_Addr>(pinfo->dlpi_phdr) - - phdr->p_vaddr; - break; - } - } - } -#endif - return image_base; -} - -struct _LIBUNWIND_HIDDEN dl_iterate_cb_data { - LocalAddressSpace *addressSpace; - UnwindInfoSections *sects; - uintptr_t targetAddr; -}; - -#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE) -#include "FrameHeaderCache.hpp" - -// Typically there is one cache per process, but when libunwind is built as a -// hermetic static library, then each shared object may have its own cache. -static FrameHeaderCache TheFrameHeaderCache; -#endif - -static bool checkAddrInSegment(const Elf_Phdr *phdr, size_t image_base, - dl_iterate_cb_data *cbdata) { - if (phdr->p_type == PT_LOAD) { - uintptr_t begin = image_base + phdr->p_vaddr; - uintptr_t end = begin + phdr->p_memsz; - if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) { - cbdata->sects->dso_base = begin; - cbdata->sects->text_segment_length = phdr->p_memsz; - return true; - } - } - return false; -} - -static bool checkForUnwindInfoSegment(const Elf_Phdr *phdr, size_t image_base, - dl_iterate_cb_data *cbdata) { -#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) - if (phdr->p_type == PT_GNU_EH_FRAME) { - EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo; - uintptr_t eh_frame_hdr_start = image_base + phdr->p_vaddr; - cbdata->sects->dwarf_index_section = eh_frame_hdr_start; - cbdata->sects->dwarf_index_section_length = phdr->p_memsz; - if (EHHeaderParser<LocalAddressSpace>::decodeEHHdr( - *cbdata->addressSpace, eh_frame_hdr_start, phdr->p_memsz, - hdrInfo)) { - // .eh_frame_hdr records the start of .eh_frame, but not its size. - // Rely on a zero terminator to find the end of the section. - cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr; +#if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) + +// 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. +#if !defined(ElfW) + #define ElfW(type) Elf_##type +#endif +#if !defined(Elf_Half) + typedef ElfW(Half) Elf_Half; +#endif +#if !defined(Elf_Phdr) + typedef ElfW(Phdr) Elf_Phdr; +#endif +#if !defined(Elf_Addr) + typedef ElfW(Addr) Elf_Addr; +#endif + +static Elf_Addr calculateImageBase(struct dl_phdr_info *pinfo) { + Elf_Addr image_base = pinfo->dlpi_addr; +#if defined(__ANDROID__) && __ANDROID_API__ < 18 + if (image_base == 0) { + // Normally, an image base of 0 indicates a non-PIE executable. On + // versions of Android prior to API 18, the dynamic linker reported a + // dlpi_addr of 0 for PIE executables. Compute the true image base + // using the PT_PHDR segment. + // See https://github.com/android/ndk/issues/505. + for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) { + const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i]; + if (phdr->p_type == PT_PHDR) { + image_base = reinterpret_cast<Elf_Addr>(pinfo->dlpi_phdr) - + phdr->p_vaddr; + break; + } + } + } +#endif + return image_base; +} + +struct _LIBUNWIND_HIDDEN dl_iterate_cb_data { + LocalAddressSpace *addressSpace; + UnwindInfoSections *sects; + uintptr_t targetAddr; +}; + +#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE) +#include "FrameHeaderCache.hpp" + +// Typically there is one cache per process, but when libunwind is built as a +// hermetic static library, then each shared object may have its own cache. +static FrameHeaderCache TheFrameHeaderCache; +#endif + +static bool checkAddrInSegment(const Elf_Phdr *phdr, size_t image_base, + dl_iterate_cb_data *cbdata) { + if (phdr->p_type == PT_LOAD) { + uintptr_t begin = image_base + phdr->p_vaddr; + uintptr_t end = begin + phdr->p_memsz; + if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) { + cbdata->sects->dso_base = begin; + cbdata->sects->text_segment_length = phdr->p_memsz; + return true; + } + } + return false; +} + +static bool checkForUnwindInfoSegment(const Elf_Phdr *phdr, size_t image_base, + dl_iterate_cb_data *cbdata) { +#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) + if (phdr->p_type == PT_GNU_EH_FRAME) { + EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo; + uintptr_t eh_frame_hdr_start = image_base + phdr->p_vaddr; + cbdata->sects->dwarf_index_section = eh_frame_hdr_start; + cbdata->sects->dwarf_index_section_length = phdr->p_memsz; + if (EHHeaderParser<LocalAddressSpace>::decodeEHHdr( + *cbdata->addressSpace, eh_frame_hdr_start, phdr->p_memsz, + hdrInfo)) { + // .eh_frame_hdr records the start of .eh_frame, but not its size. + // Rely on a zero terminator to find the end of the section. + cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr; cbdata->sects->dwarf_section_length = SIZE_MAX; - return true; - } - } - return false; -#elif defined(_LIBUNWIND_ARM_EHABI) - if (phdr->p_type == PT_ARM_EXIDX) { - uintptr_t exidx_start = image_base + phdr->p_vaddr; - cbdata->sects->arm_section = exidx_start; - cbdata->sects->arm_section_length = phdr->p_memsz; - return true; - } - return false; -#else -#error Need one of _LIBUNWIND_SUPPORT_DWARF_INDEX or _LIBUNWIND_ARM_EHABI -#endif -} - -static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, - size_t pinfo_size, void *data) { - auto cbdata = static_cast<dl_iterate_cb_data *>(data); - if (pinfo->dlpi_phnum == 0 || cbdata->targetAddr < pinfo->dlpi_addr) - return 0; -#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE) - if (TheFrameHeaderCache.find(pinfo, pinfo_size, data)) - return 1; -#else - // Avoid warning about unused variable. - (void)pinfo_size; -#endif - - Elf_Addr image_base = calculateImageBase(pinfo); - - // Most shared objects seen in this callback function likely don't contain the - // target address, so optimize for that. Scan for a matching PT_LOAD segment - // first and bail when it isn't found. - bool found_text = false; - for (Elf_Half i = 0; i < pinfo->dlpi_phnum; ++i) { - if (checkAddrInSegment(&pinfo->dlpi_phdr[i], image_base, cbdata)) { - found_text = true; - break; - } - } - if (!found_text) - return 0; - - // PT_GNU_EH_FRAME and PT_ARM_EXIDX are usually near the end. Iterate - // backward. - bool found_unwind = false; - for (Elf_Half i = pinfo->dlpi_phnum; i > 0; i--) { - const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i - 1]; - if (checkForUnwindInfoSegment(phdr, image_base, cbdata)) { - found_unwind = true; - break; - } - } - if (!found_unwind) - return 0; - -#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE) - TheFrameHeaderCache.add(cbdata->sects); -#endif - return 1; -} - -#endif // defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) - - + return true; + } + } + return false; +#elif defined(_LIBUNWIND_ARM_EHABI) + if (phdr->p_type == PT_ARM_EXIDX) { + uintptr_t exidx_start = image_base + phdr->p_vaddr; + cbdata->sects->arm_section = exidx_start; + cbdata->sects->arm_section_length = phdr->p_memsz; + return true; + } + return false; +#else +#error Need one of _LIBUNWIND_SUPPORT_DWARF_INDEX or _LIBUNWIND_ARM_EHABI +#endif +} + +static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, + size_t pinfo_size, void *data) { + auto cbdata = static_cast<dl_iterate_cb_data *>(data); + if (pinfo->dlpi_phnum == 0 || cbdata->targetAddr < pinfo->dlpi_addr) + return 0; +#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE) + if (TheFrameHeaderCache.find(pinfo, pinfo_size, data)) + return 1; +#else + // Avoid warning about unused variable. + (void)pinfo_size; +#endif + + Elf_Addr image_base = calculateImageBase(pinfo); + + // Most shared objects seen in this callback function likely don't contain the + // target address, so optimize for that. Scan for a matching PT_LOAD segment + // first and bail when it isn't found. + bool found_text = false; + for (Elf_Half i = 0; i < pinfo->dlpi_phnum; ++i) { + if (checkAddrInSegment(&pinfo->dlpi_phdr[i], image_base, cbdata)) { + found_text = true; + break; + } + } + if (!found_text) + return 0; + + // PT_GNU_EH_FRAME and PT_ARM_EXIDX are usually near the end. Iterate + // backward. + bool found_unwind = false; + for (Elf_Half i = pinfo->dlpi_phnum; i > 0; i--) { + const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i - 1]; + if (checkForUnwindInfoSegment(phdr, image_base, cbdata)) { + found_unwind = true; + break; + } + } + if (!found_unwind) + return 0; + +#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE) + TheFrameHeaderCache.add(cbdata->sects); +#endif + return 1; +} + +#endif // defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) + + inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr, UnwindInfoSections &info) { #ifdef __APPLE__ dyld_unwind_sections dyldInfo; if (_dyld_find_unwind_sections((void *)targetAddr, &dyldInfo)) { info.dso_base = (uintptr_t)dyldInfo.mh; - #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) + #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) info.dwarf_section = (uintptr_t)dyldInfo.dwarf_section; info.dwarf_section_length = (size_t)dyldInfo.dwarf_section_length; #endif @@ -512,84 +512,84 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr, info.compact_unwind_section_length = (size_t)dyldInfo.compact_unwind_section_length; return true; } -#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL) - info.dso_base = 0; +#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL) + info.dso_base = 0; // Bare metal is statically linked, so no need to ask the dynamic loader info.dwarf_section_length = (size_t)(&__eh_frame_end - &__eh_frame_start); - info.dwarf_section = (uintptr_t)(&__eh_frame_start); - _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p", - (void *)info.dwarf_section, (void *)info.dwarf_section_length); -#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) - info.dwarf_index_section = (uintptr_t)(&__eh_frame_hdr_start); + info.dwarf_section = (uintptr_t)(&__eh_frame_start); + _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p", + (void *)info.dwarf_section, (void *)info.dwarf_section_length); +#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) + info.dwarf_index_section = (uintptr_t)(&__eh_frame_hdr_start); info.dwarf_index_section_length = (size_t)(&__eh_frame_hdr_end - &__eh_frame_hdr_start); - _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: index section %p length %p", - (void *)info.dwarf_index_section, (void *)info.dwarf_index_section_length); -#endif - if (info.dwarf_section_length) - return true; -#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL) - // Bare metal is statically linked, so no need to ask the dynamic loader + _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: index section %p length %p", + (void *)info.dwarf_index_section, (void *)info.dwarf_index_section_length); +#endif + if (info.dwarf_section_length) + return true; +#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 = (size_t)(&__exidx_end - &__exidx_start); - _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p", - (void *)info.arm_section, (void *)info.arm_section_length); - if (info.arm_section && info.arm_section_length) - return true; -#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_WIN32) - HMODULE mods[1024]; - HANDLE process = GetCurrentProcess(); - DWORD needed; - - if (!EnumProcessModules(process, mods, sizeof(mods), &needed)) { - DWORD err = GetLastError(); - _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: EnumProcessModules failed, " - "returned error %d", (int)err); - return false; - } - - for (unsigned i = 0; i < (needed / sizeof(HMODULE)); i++) { - PIMAGE_DOS_HEADER pidh = (PIMAGE_DOS_HEADER)mods[i]; - PIMAGE_NT_HEADERS pinh = (PIMAGE_NT_HEADERS)((BYTE *)pidh + pidh->e_lfanew); - PIMAGE_FILE_HEADER pifh = (PIMAGE_FILE_HEADER)&pinh->FileHeader; - PIMAGE_SECTION_HEADER pish = IMAGE_FIRST_SECTION(pinh); - bool found_obj = false; - bool found_hdr = false; - - info.dso_base = (uintptr_t)mods[i]; - for (unsigned j = 0; j < pifh->NumberOfSections; j++, pish++) { - uintptr_t begin = pish->VirtualAddress + (uintptr_t)mods[i]; - uintptr_t end = begin + pish->Misc.VirtualSize; - if (!strncmp((const char *)pish->Name, ".text", - IMAGE_SIZEOF_SHORT_NAME)) { - if (targetAddr >= begin && targetAddr < end) - found_obj = true; - } else if (!strncmp((const char *)pish->Name, ".eh_frame", - IMAGE_SIZEOF_SHORT_NAME)) { - info.dwarf_section = begin; - info.dwarf_section_length = pish->Misc.VirtualSize; - found_hdr = true; - } - if (found_obj && found_hdr) - return true; - } - } - return false; -#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32) - // Don't even bother, since Windows has functions that do all this stuff - // for us. - (void)targetAddr; - (void)info; - return true; -#elif defined(_LIBUNWIND_USE_DL_UNWIND_FIND_EXIDX) + _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p", + (void *)info.arm_section, (void *)info.arm_section_length); + if (info.arm_section && info.arm_section_length) + return true; +#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_WIN32) + HMODULE mods[1024]; + HANDLE process = GetCurrentProcess(); + DWORD needed; + + if (!EnumProcessModules(process, mods, sizeof(mods), &needed)) { + DWORD err = GetLastError(); + _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: EnumProcessModules failed, " + "returned error %d", (int)err); + return false; + } + + for (unsigned i = 0; i < (needed / sizeof(HMODULE)); i++) { + PIMAGE_DOS_HEADER pidh = (PIMAGE_DOS_HEADER)mods[i]; + PIMAGE_NT_HEADERS pinh = (PIMAGE_NT_HEADERS)((BYTE *)pidh + pidh->e_lfanew); + PIMAGE_FILE_HEADER pifh = (PIMAGE_FILE_HEADER)&pinh->FileHeader; + PIMAGE_SECTION_HEADER pish = IMAGE_FIRST_SECTION(pinh); + bool found_obj = false; + bool found_hdr = false; + + info.dso_base = (uintptr_t)mods[i]; + for (unsigned j = 0; j < pifh->NumberOfSections; j++, pish++) { + uintptr_t begin = pish->VirtualAddress + (uintptr_t)mods[i]; + uintptr_t end = begin + pish->Misc.VirtualSize; + if (!strncmp((const char *)pish->Name, ".text", + IMAGE_SIZEOF_SHORT_NAME)) { + if (targetAddr >= begin && targetAddr < end) + found_obj = true; + } else if (!strncmp((const char *)pish->Name, ".eh_frame", + IMAGE_SIZEOF_SHORT_NAME)) { + info.dwarf_section = begin; + info.dwarf_section_length = pish->Misc.VirtualSize; + found_hdr = true; + } + if (found_obj && found_hdr) + return true; + } + } + return false; +#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32) + // Don't even bother, since Windows has functions that do all this stuff + // for us. + (void)targetAddr; + (void)info; + return true; +#elif defined(_LIBUNWIND_USE_DL_UNWIND_FIND_EXIDX) int length = 0; - info.arm_section = - (uintptr_t)dl_unwind_find_exidx((_Unwind_Ptr)targetAddr, &length); + info.arm_section = + (uintptr_t)dl_unwind_find_exidx((_Unwind_Ptr)targetAddr, &length); info.arm_section_length = (size_t)length * sizeof(EHABIIndexEntry); if (info.arm_section && info.arm_section_length) return true; -#elif defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) +#elif defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) dl_iterate_cb_data cb_data = {this, &info, targetAddr}; - int found = dl_iterate_phdr(findUnwindSectionsByPhdr, &cb_data); + int found = dl_iterate_phdr(findUnwindSectionsByPhdr, &cb_data); return static_cast<bool>(found); #endif @@ -607,7 +607,7 @@ inline bool LocalAddressSpace::findOtherFDE(pint_t targetAddr, pint_t &fde) { inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf, size_t bufLen, unw_word_t *offset) { -#if _LIBUNWIND_USE_DLADDR +#if _LIBUNWIND_USE_DLADDR Dl_info dyldInfo; if (dladdr((void *)addr, &dyldInfo)) { if (dyldInfo.dli_sname != NULL) { @@ -616,11 +616,11 @@ inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf, return true; } } -#else - (void)addr; - (void)buf; - (void)bufLen; - (void)offset; +#else + (void)addr; + (void)buf; + (void)bufLen; + (void)offset; #endif return false; } |