diff --git a/libunwind/src/AddressSpace.hpp b/libunwind/src/AddressSpace.hpp --- a/libunwind/src/AddressSpace.hpp +++ b/libunwind/src/AddressSpace.hpp @@ -19,9 +19,11 @@ #include "libunwind.h" #include "config.h" +#include "DlIteratePhdrLookup.hpp" #include "dwarf2.h" #include "EHHeaderParser.hpp" #include "Registers.hpp" +#include "UnwindInfoSections.hpp" #ifndef _LIBUNWIND_USE_DLADDR #if !defined(_LIBUNWIND_IS_BAREMETAL) && !defined(_WIN32) @@ -105,8 +107,7 @@ #include #include -#elif defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) || \ - defined(_LIBUNWIND_USE_DL_UNWIND_FIND_EXIDX) +#elif defined(_LIBUNWIND_USE_DL_UNWIND_FIND_EXIDX) #include @@ -114,36 +115,6 @@ namespace libunwind { -/// Used by findUnwindSections() to return info about needed sections. -struct UnwindInfoSections { -#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) || \ - defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) - // No dso_base for SEH or ARM EHABI. - uintptr_t dso_base; -#endif -#if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) && \ - defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) - uintptr_t dso_length; -#endif -#if defined(_LIBUNWIND_SUPPORT_DWARF_SECTION) - uintptr_t dwarf_section; - uintptr_t dwarf_section_length; -#endif -#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) - uintptr_t dwarf_index_section; - uintptr_t dwarf_index_section_length; -#endif -#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) - uintptr_t compact_unwind_section; - uintptr_t compact_unwind_section_length; -#endif -#if defined(_LIBUNWIND_ARM_EHABI) - uintptr_t arm_section; - uintptr_t arm_section_length; -#endif -}; - - /// 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. @@ -350,164 +321,6 @@ return result; } -#if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) - -// The ElfW() macro for pointer-size independent ELF header traversal is not -// provided by 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(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_SUPPORT_DWARF_INDEX) - #if defined(_LIBUNWIND_SUPPORT_DWARF_SECTION) - // dl_iterate_phdr finds PT_GNU_EH_FRAME (.eh_frame_hdr section), which - // provides the start of the .eh_frame section, but not its length. The - // .eh_frame length isn't strictly necessary if we assume the section ends - // with a terminator, but sometimes it doesn't, and if we have the index, - // then scanning .eh_frame shouldn't be necessary anyway. - #error _LIBUNWIND_SUPPORT_DWARF_SECTION is unsupported with dl_iterate_phdr. - #endif - -#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->dso_length = phdr->p_memsz; - return true; - } - } - return false; -} - -static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, - size_t pinfo_size, void *data) { - auto cbdata = static_cast(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); - bool found_obj = false; - bool found_hdr = false; - - // Third phdr is usually the executable phdr. - if (pinfo->dlpi_phnum > 2) - found_obj = checkAddrInSegment(&pinfo->dlpi_phdr[2], image_base, cbdata); - - // PT_GNU_EH_FRAME is usually near the end. Iterate backward. We already know - // that there is one or more phdrs. - for (Elf_Half i = pinfo->dlpi_phnum; i > 0; i--) { - const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i - 1]; - if (!found_hdr && phdr->p_type == PT_GNU_EH_FRAME) { - found_hdr = true; - 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; - } else if (!found_obj) { - found_obj = checkAddrInSegment(phdr, image_base, cbdata); - } - if (found_obj && found_hdr) { -#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE) - TheFrameHeaderCache.add(cbdata->sects); -#endif - return 1; - } - } - return 0; -} - -#elif defined(_LIBUNWIND_ARM_EHABI) - -static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, size_t, - void *data) { - auto *cbdata = static_cast(data); - bool found_obj = false; - bool found_hdr = false; - - assert(cbdata); - assert(cbdata->sects); - - if (cbdata->targetAddr < pinfo->dlpi_addr) - return 0; - - Elf_Addr image_base = calculateImageBase(pinfo); - - 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 = image_base + 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 = image_base + 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 -#endif // defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) - - inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr, UnwindInfoSections &info) { #if defined(__APPLE__) && defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) @@ -601,9 +414,7 @@ if (info.arm_section && info.arm_section_length) return true; #elif defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) - dl_iterate_cb_data cb_data = {this, &info, targetAddr}; - int found = dl_iterate_phdr(findUnwindSectionsByPhdr, &cb_data); - return static_cast(found); + return findUnwindSectionsByDlIteratePhdr(targetAddr, info); #endif return false; diff --git a/libunwind/src/CMakeLists.txt b/libunwind/src/CMakeLists.txt --- a/libunwind/src/CMakeLists.txt +++ b/libunwind/src/CMakeLists.txt @@ -35,16 +35,17 @@ assembly.h CompactUnwinder.hpp config.h + DlIteratePhdrLookup.hpp dwarf2.h DwarfInstructions.hpp DwarfParser.hpp EHHeaderParser.hpp - FrameHeaderCache.hpp libunwind_ext.h Registers.hpp RWMutex.hpp Unwind-EHABI.h UnwindCursor.hpp + UnwindInfoSections.hpp ../include/libunwind.h ../include/unwind.h ) diff --git a/libunwind/src/DlIteratePhdrLookup.hpp b/libunwind/src/DlIteratePhdrLookup.hpp new file mode 100644 --- /dev/null +++ b/libunwind/src/DlIteratePhdrLookup.hpp @@ -0,0 +1,320 @@ +//===-DlIteratePhdrLookup.hpp ---------------------------------------------===// +// +// 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 +// +// Finds unwind information using dl_iterate_phdr. Caches the elf program +// headers necessary to unwind the stack more efficiently in the presence of +// many dsos. +// +//===----------------------------------------------------------------------===// + +#ifndef __DL_ITERATE_PHDR_LOOKUP_HPP__ +#define __DL_ITERATE_PHDR_LOOKUP_HPP__ + +#include "config.h" + +#if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) + +#include +#include +#include + +#include "UnwindInfoSections.hpp" + +namespace libunwind { + +#ifdef _LIBUNWIND_DEBUG_FRAMEHEADER_CACHE +#define _LIBUNWIND_FRAMEHEADERCACHE_TRACE0(x) _LIBUNWIND_LOG0(x) +#define _LIBUNWIND_FRAMEHEADERCACHE_TRACE(msg, ...) \ + _LIBUNWIND_LOG(msg, __VA_ARGS__) +#else +#define _LIBUNWIND_FRAMEHEADERCACHE_TRACE0(x) +#define _LIBUNWIND_FRAMEHEADERCACHE_TRACE(msg, ...) +#endif + +// The ElfW() macro for pointer-size independent ELF header traversal is not +// provided by 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(pinfo->dlpi_phdr) - + phdr->p_vaddr; + break; + } + } + } +#endif + return image_base; +} + +struct _LIBUNWIND_HIDDEN dl_iterate_cb_data { + UnwindInfoSections *sects; + uintptr_t targetAddr; +}; + +#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) + #if defined(_LIBUNWIND_SUPPORT_DWARF_SECTION) + // dl_iterate_phdr finds PT_GNU_EH_FRAME (.eh_frame_hdr section), which + // provides the start of the .eh_frame section, but not its length. The + // .eh_frame length isn't strictly necessary if we assume the section ends + // with a terminator, but sometimes it doesn't, and if we have the index, + // then scanning .eh_frame shouldn't be necessary anyway. + #error _LIBUNWIND_SUPPORT_DWARF_SECTION is unsupported with dl_iterate_phdr. + #endif + +// This cache should only be be used from within a dl_iterate_phdr callback. +// dl_iterate_phdr does the necessary synchronization to prevent problems +// with concurrent access via the libc load lock. Adding synchronization +// for other uses is possible, but not currently done. + +#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE) +class _LIBUNWIND_HIDDEN FrameHeaderCache { + struct CacheEntry { + uintptr_t LowPC() { return Info.dso_base; }; + uintptr_t HighPC() { return Info.dso_base + Info.dso_length; }; + UnwindInfoSections Info; + CacheEntry *Next; + }; + + static const size_t kCacheEntryCount = 8; + + // Can't depend on the C++ standard library in libunwind, so use an array to + // allocate the entries, and two linked lists for ordering unused and recently + // used entries. FIXME: Would the the extra memory for a doubly-linked list + // be better than the runtime cost of traversing a very short singly-linked + // list on a cache miss? The entries themselves are all small and consecutive, + // so unlikely to cause page faults when following the pointers. The memory + // spent on additional pointers could also be spent on more entries. + + CacheEntry Entries[kCacheEntryCount]; + CacheEntry *MostRecentlyUsed; + CacheEntry *Unused; + + void resetCache() { + _LIBUNWIND_FRAMEHEADERCACHE_TRACE0("FrameHeaderCache reset"); + MostRecentlyUsed = nullptr; + Unused = &Entries[0]; + for (size_t i = 0; i < kCacheEntryCount - 1; i++) { + Entries[i].Next = &Entries[i + 1]; + } + Entries[kCacheEntryCount - 1].Next = nullptr; + } + + bool cacheNeedsReset(dl_phdr_info *PInfo) { + // C libraries increment dl_phdr_info.adds and dl_phdr_info.subs when + // loading and unloading shared libraries. If these values change between + // iterations of dl_iterate_phdr, then invalidate the cache. + + // These are static to avoid needing an initializer, and unsigned long long + // because that is their type within the extended dl_phdr_info. Initialize + // these to something extremely unlikely to be found upon the first call to + // dl_iterate_phdr. + static unsigned long long LastAdds = ULLONG_MAX; + static unsigned long long LastSubs = ULLONG_MAX; + if (PInfo->dlpi_adds != LastAdds || PInfo->dlpi_subs != LastSubs) { + // Resetting the entire cache is a big hammer, but this path is rare-- + // usually just on the very first call, when the cache is empty anyway--so + // added complexity doesn't buy much. + LastAdds = PInfo->dlpi_adds; + LastSubs = PInfo->dlpi_subs; + resetCache(); + return true; + } + return false; + } + +public: + bool find(dl_phdr_info *PInfo, size_t, void *data) { + if (cacheNeedsReset(PInfo) || MostRecentlyUsed == nullptr) + return false; + + auto *CBData = static_cast(data); + CacheEntry *Current = MostRecentlyUsed; + CacheEntry *Previous = nullptr; + while (Current != nullptr) { + _LIBUNWIND_FRAMEHEADERCACHE_TRACE( + "FrameHeaderCache check %lx in [%lx - %lx)", CBData->targetAddr, + Current->LowPC(), Current->HighPC()); + if (Current->LowPC() <= CBData->targetAddr && + CBData->targetAddr < Current->HighPC()) { + _LIBUNWIND_FRAMEHEADERCACHE_TRACE( + "FrameHeaderCache hit %lx in [%lx - %lx)", CBData->targetAddr, + Current->LowPC(), Current->HighPC()); + if (Previous) { + // If there is no Previous, then Current is already the + // MostRecentlyUsed, and no need to move it up. + Previous->Next = Current->Next; + Current->Next = MostRecentlyUsed; + MostRecentlyUsed = Current; + } + *CBData->sects = Current->Info; + return true; + } + Previous = Current; + Current = Current->Next; + } + _LIBUNWIND_FRAMEHEADERCACHE_TRACE("FrameHeaderCache miss for address %lx", + CBData->targetAddr); + return false; + } + + void add(const UnwindInfoSections *UIS) { + CacheEntry *Current = nullptr; + + if (Unused != nullptr) { + Current = Unused; + Unused = Unused->Next; + } else { + Current = MostRecentlyUsed; + CacheEntry *Previous = nullptr; + while (Current->Next != nullptr) { + Previous = Current; + Current = Current->Next; + } + Previous->Next = nullptr; + _LIBUNWIND_FRAMEHEADERCACHE_TRACE("FrameHeaderCache evict [%lx - %lx)", + Current->LowPC(), Current->HighPC()); + } + + Current->Info = *UIS; + Current->Next = MostRecentlyUsed; + MostRecentlyUsed = Current; + _LIBUNWIND_FRAMEHEADERCACHE_TRACE("FrameHeaderCache add [%lx - %lx)", + MostRecentlyUsed->LowPC(), + MostRecentlyUsed->HighPC()); + } +}; + +// 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 // defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE) + +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->dso_length = phdr->p_memsz; + return true; + } + } + return false; +} + +static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, + size_t pinfo_size, void *data) { + auto cbdata = static_cast(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); + bool found_obj = false; + bool found_hdr = false; + + // Third phdr is usually the executable phdr. + if (pinfo->dlpi_phnum > 2) + found_obj = checkAddrInSegment(&pinfo->dlpi_phdr[2], image_base, cbdata); + + // PT_GNU_EH_FRAME is usually near the end. Iterate backward. We already know + // that there is one or more phdrs. + for (Elf_Half i = pinfo->dlpi_phnum; i > 0; i--) { + const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i - 1]; + if (!found_hdr && phdr->p_type == PT_GNU_EH_FRAME) { + found_hdr = true; + 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; + } else if (!found_obj) { + found_obj = checkAddrInSegment(phdr, image_base, cbdata); + } + if (found_obj && found_hdr) { +#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE) + TheFrameHeaderCache.add(cbdata->sects); +#endif + return 1; + } + } + return 0; +} + +#elif defined(_LIBUNWIND_ARM_EHABI) + +static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, size_t, + void *data) { + auto *cbdata = static_cast(data); + bool found_obj = false; + bool found_hdr = false; + + assert(cbdata); + assert(cbdata->sects); + + if (cbdata->targetAddr < pinfo->dlpi_addr) + return 0; + + Elf_Addr image_base = calculateImageBase(pinfo); + + 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 = image_base + 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 = image_base + 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 + +static bool findUnwindSectionsByDlIteratePhdr(uintptr_t targetAddr, + UnwindInfoSections &info) { + dl_iterate_cb_data cb_data = {&info, targetAddr}; + int found = dl_iterate_phdr(findUnwindSectionsByPhdr, &cb_data); + return static_cast(found); +} + +} // namespace libunwind + +#endif // defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) +#endif // __DL_ITERATE_PHDR_LOOKUP_HPP__ diff --git a/libunwind/src/FrameHeaderCache.hpp b/libunwind/src/FrameHeaderCache.hpp deleted file mode 100644 --- a/libunwind/src/FrameHeaderCache.hpp +++ /dev/null @@ -1,149 +0,0 @@ -//===-FrameHeaderCache.hpp ------------------------------------------------===// -// -// 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 -// -// Cache the elf program headers necessary to unwind the stack more efficiently -// in the presence of many dsos. -// -//===----------------------------------------------------------------------===// - -#ifndef __FRAMEHEADER_CACHE_HPP__ -#define __FRAMEHEADER_CACHE_HPP__ - -#include "config.h" -#include - -#ifdef _LIBUNWIND_DEBUG_FRAMEHEADER_CACHE -#define _LIBUNWIND_FRAMEHEADERCACHE_TRACE0(x) _LIBUNWIND_LOG0(x) -#define _LIBUNWIND_FRAMEHEADERCACHE_TRACE(msg, ...) \ - _LIBUNWIND_LOG(msg, __VA_ARGS__) -#else -#define _LIBUNWIND_FRAMEHEADERCACHE_TRACE0(x) -#define _LIBUNWIND_FRAMEHEADERCACHE_TRACE(msg, ...) -#endif - -// This cache should only be be used from within a dl_iterate_phdr callback. -// dl_iterate_phdr does the necessary synchronization to prevent problems -// with concurrent access via the libc load lock. Adding synchronization -// for other uses is possible, but not currently done. - -class _LIBUNWIND_HIDDEN FrameHeaderCache { - struct CacheEntry { - uintptr_t LowPC() { return Info.dso_base; }; - uintptr_t HighPC() { return Info.dso_base + Info.dso_length; }; - UnwindInfoSections Info; - CacheEntry *Next; - }; - - static const size_t kCacheEntryCount = 8; - - // Can't depend on the C++ standard library in libunwind, so use an array to - // allocate the entries, and two linked lists for ordering unused and recently - // used entries. FIXME: Would the the extra memory for a doubly-linked list - // be better than the runtime cost of traversing a very short singly-linked - // list on a cache miss? The entries themselves are all small and consecutive, - // so unlikely to cause page faults when following the pointers. The memory - // spent on additional pointers could also be spent on more entries. - - CacheEntry Entries[kCacheEntryCount]; - CacheEntry *MostRecentlyUsed; - CacheEntry *Unused; - - void resetCache() { - _LIBUNWIND_FRAMEHEADERCACHE_TRACE0("FrameHeaderCache reset"); - MostRecentlyUsed = nullptr; - Unused = &Entries[0]; - for (size_t i = 0; i < kCacheEntryCount - 1; i++) { - Entries[i].Next = &Entries[i + 1]; - } - Entries[kCacheEntryCount - 1].Next = nullptr; - } - - bool cacheNeedsReset(dl_phdr_info *PInfo) { - // C libraries increment dl_phdr_info.adds and dl_phdr_info.subs when - // loading and unloading shared libraries. If these values change between - // iterations of dl_iterate_phdr, then invalidate the cache. - - // These are static to avoid needing an initializer, and unsigned long long - // because that is their type within the extended dl_phdr_info. Initialize - // these to something extremely unlikely to be found upon the first call to - // dl_iterate_phdr. - static unsigned long long LastAdds = ULLONG_MAX; - static unsigned long long LastSubs = ULLONG_MAX; - if (PInfo->dlpi_adds != LastAdds || PInfo->dlpi_subs != LastSubs) { - // Resetting the entire cache is a big hammer, but this path is rare-- - // usually just on the very first call, when the cache is empty anyway--so - // added complexity doesn't buy much. - LastAdds = PInfo->dlpi_adds; - LastSubs = PInfo->dlpi_subs; - resetCache(); - return true; - } - return false; - } - -public: - bool find(dl_phdr_info *PInfo, size_t, void *data) { - if (cacheNeedsReset(PInfo) || MostRecentlyUsed == nullptr) - return false; - - auto *CBData = static_cast(data); - CacheEntry *Current = MostRecentlyUsed; - CacheEntry *Previous = nullptr; - while (Current != nullptr) { - _LIBUNWIND_FRAMEHEADERCACHE_TRACE( - "FrameHeaderCache check %lx in [%lx - %lx)", CBData->targetAddr, - Current->LowPC(), Current->HighPC()); - if (Current->LowPC() <= CBData->targetAddr && - CBData->targetAddr < Current->HighPC()) { - _LIBUNWIND_FRAMEHEADERCACHE_TRACE( - "FrameHeaderCache hit %lx in [%lx - %lx)", CBData->targetAddr, - Current->LowPC(), Current->HighPC()); - if (Previous) { - // If there is no Previous, then Current is already the - // MostRecentlyUsed, and no need to move it up. - Previous->Next = Current->Next; - Current->Next = MostRecentlyUsed; - MostRecentlyUsed = Current; - } - *CBData->sects = Current->Info; - return true; - } - Previous = Current; - Current = Current->Next; - } - _LIBUNWIND_FRAMEHEADERCACHE_TRACE("FrameHeaderCache miss for address %lx", - CBData->targetAddr); - return false; - } - - void add(const UnwindInfoSections *UIS) { - CacheEntry *Current = nullptr; - - if (Unused != nullptr) { - Current = Unused; - Unused = Unused->Next; - } else { - Current = MostRecentlyUsed; - CacheEntry *Previous = nullptr; - while (Current->Next != nullptr) { - Previous = Current; - Current = Current->Next; - } - Previous->Next = nullptr; - _LIBUNWIND_FRAMEHEADERCACHE_TRACE("FrameHeaderCache evict [%lx - %lx)", - Current->LowPC(), Current->HighPC()); - } - - Current->Info = *UIS; - Current->Next = MostRecentlyUsed; - MostRecentlyUsed = Current; - _LIBUNWIND_FRAMEHEADERCACHE_TRACE("FrameHeaderCache add [%lx - %lx)", - MostRecentlyUsed->LowPC(), - MostRecentlyUsed->HighPC()); - } -}; - -#endif // __FRAMEHEADER_CACHE_HPP__ diff --git a/libunwind/src/UnwindInfoSections.hpp b/libunwind/src/UnwindInfoSections.hpp new file mode 100644 --- /dev/null +++ b/libunwind/src/UnwindInfoSections.hpp @@ -0,0 +1,51 @@ +//===-UnwindInfoSections.hpp ----------------------------------------------===// +// +// 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 +// +// Define the UnwindInfoSections struct used by findUnwindSections. +// +//===----------------------------------------------------------------------===// + +#ifndef __UNWIND_INFO_SECTIONS_HPP__ +#define __UNWIND_INFO_SECTIONS_HPP__ + +#include + +#include "config.h" + +namespace libunwind { + +/// Used by findUnwindSections() to return info about needed sections. +struct UnwindInfoSections { +#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) || \ + defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) + // No dso_base for SEH or ARM EHABI. + uintptr_t dso_base; +#endif +#if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) && \ + defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) + uintptr_t dso_length; +#endif +#if defined(_LIBUNWIND_SUPPORT_DWARF_SECTION) + uintptr_t dwarf_section; + uintptr_t dwarf_section_length; +#endif +#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) + uintptr_t dwarf_index_section; + uintptr_t dwarf_index_section_length; +#endif +#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) + uintptr_t compact_unwind_section; + uintptr_t compact_unwind_section_length; +#endif +#if defined(_LIBUNWIND_ARM_EHABI) + uintptr_t arm_section; + uintptr_t arm_section_length; +#endif +}; + +} // namespace libunwind + +#endif // __UNWIND_INFO_SECTIONS_HPP__