710 lines · plain
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//8// Abstracts accessing local vs remote address spaces.9//10//===----------------------------------------------------------------------===//11 12#ifndef __ADDRESSSPACE_HPP__13#define __ADDRESSSPACE_HPP__14 15#include <stdint.h>16#include <stdio.h>17#include <stdlib.h>18#include <string.h>19 20#include "libunwind.h"21#include "config.h"22#include "dwarf2.h"23#include "EHHeaderParser.hpp"24#include "Registers.hpp"25 26#ifndef _LIBUNWIND_USE_DLADDR27 #if !(defined(_LIBUNWIND_IS_BAREMETAL) || defined(_WIN32) || defined(_AIX))28 #define _LIBUNWIND_USE_DLADDR 129 #else30 #define _LIBUNWIND_USE_DLADDR 031 #endif32#endif33 34#if _LIBUNWIND_USE_DLADDR35#include <dlfcn.h>36#if defined(__ELF__) && defined(_LIBUNWIND_LINK_DL_LIB)37#pragma comment(lib, "dl")38#endif39#endif40 41#if defined(_LIBUNWIND_ARM_EHABI)42struct EHABIIndexEntry {43 uint32_t functionOffset;44 uint32_t data;45};46#endif47 48#if defined(_AIX)49namespace libunwind {50char *getFuncNameFromTBTable(uintptr_t pc, uint16_t &NameLen,51 unw_word_t *offset);52}53#endif54 55#ifdef __APPLE__56 57 struct dyld_unwind_sections58 {59 const struct mach_header* mh;60 const void* dwarf_section;61 uintptr_t dwarf_section_length;62 const void* compact_unwind_section;63 uintptr_t compact_unwind_section_length;64 };65 66 // In 10.7.0 or later, libSystem.dylib implements this function.67 extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *);68 69namespace libunwind {70 bool findDynamicUnwindSections(void *, unw_dynamic_unwind_sections *);71}72 73#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL)74 75// When statically linked on bare-metal, the symbols for the EH table are looked76// up without going through the dynamic loader.77 78// The following linker script may be used to produce the necessary sections and symbols.79// Unless the --eh-frame-hdr linker option is provided, the section is not generated80// and does not take space in the output file.81//82// .eh_frame :83// {84// __eh_frame_start = .;85// KEEP(*(.eh_frame))86// __eh_frame_end = .;87// }88//89// .eh_frame_hdr :90// {91// KEEP(*(.eh_frame_hdr))92// }93//94// __eh_frame_hdr_start = SIZEOF(.eh_frame_hdr) > 0 ? ADDR(.eh_frame_hdr) : 0;95// __eh_frame_hdr_end = SIZEOF(.eh_frame_hdr) > 0 ? . : 0;96 97extern char __eh_frame_start;98extern char __eh_frame_end;99 100#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)101extern char __eh_frame_hdr_start;102extern char __eh_frame_hdr_end;103#endif104 105#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)106 107// When statically linked on bare-metal, the symbols for the EH table are looked108// up without going through the dynamic loader.109extern char __exidx_start;110extern char __exidx_end;111 112#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_WIN32)113 114#include <windows.h>115#include <psapi.h>116 117#elif defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) || \118 defined(_LIBUNWIND_USE_DL_UNWIND_FIND_EXIDX)119 120#include <link.h>121 122#endif123 124namespace libunwind {125 126/// Used by findUnwindSections() to return info about needed sections.127struct UnwindInfoSections {128#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) || \129 defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) || \130 defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)131 // No dso_base for SEH.132 uintptr_t __ptrauth_unwind_uis_dso_base133 dso_base = 0;134#endif135#if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)136 size_t text_segment_length;137#endif138#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)139 uintptr_t __ptrauth_unwind_uis_dwarf_section140 dwarf_section = 0;141 size_t __ptrauth_unwind_uis_dwarf_section_length142 dwarf_section_length = 0;143#endif144#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)145 uintptr_t dwarf_index_section;146 size_t dwarf_index_section_length;147#endif148#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)149 uintptr_t __ptrauth_unwind_uis_compact_unwind_section150 compact_unwind_section = 0;151 size_t __ptrauth_unwind_uis_compact_unwind_section_length152 compact_unwind_section_length = 0;153#endif154#if defined(_LIBUNWIND_ARM_EHABI)155 uintptr_t arm_section;156 size_t arm_section_length;157#endif158};159 160 161/// LocalAddressSpace is used as a template parameter to UnwindCursor when162/// unwinding a thread in the same process. The wrappers compile away,163/// making local unwinds fast.164class _LIBUNWIND_HIDDEN LocalAddressSpace {165public:166 typedef uintptr_t pint_t;167 typedef intptr_t sint_t;168 uint8_t get8(pint_t addr) {169 uint8_t val;170 memcpy(&val, (void *)addr, sizeof(val));171 return val;172 }173 uint16_t get16(pint_t addr) {174 uint16_t val;175 memcpy(&val, (void *)addr, sizeof(val));176 return val;177 }178 uint32_t get32(pint_t addr) {179 uint32_t val;180 memcpy(&val, (void *)addr, sizeof(val));181 return val;182 }183 uint64_t get64(pint_t addr) {184 uint64_t val;185 memcpy(&val, (void *)addr, sizeof(val));186 return val;187 }188 double getDouble(pint_t addr) {189 double val;190 memcpy(&val, (void *)addr, sizeof(val));191 return val;192 }193 v128 getVector(pint_t addr) {194 v128 val;195 memcpy(&val, (void *)addr, sizeof(val));196 return val;197 }198 uintptr_t getP(pint_t addr);199 uint64_t getRegister(pint_t addr);200 static uint64_t getULEB128(pint_t &addr, pint_t end);201 static int64_t getSLEB128(pint_t &addr, pint_t end);202 203 pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,204 pint_t datarelBase = 0, pint_t *resultAddr = nullptr);205 bool findFunctionName(pint_t addr, char *buf, size_t bufLen,206 unw_word_t *offset);207 bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);208 bool findOtherFDE(pint_t targetAddr, pint_t &fde);209 210 static LocalAddressSpace sThisAddressSpace;211};212 213inline uintptr_t LocalAddressSpace::getP(pint_t addr) {214#if __SIZEOF_POINTER__ == 8215 return get64(addr);216#else217 return get32(addr);218#endif219}220 221inline uint64_t LocalAddressSpace::getRegister(pint_t addr) {222#if __SIZEOF_POINTER__ == 8 || defined(__mips64)223 return get64(addr);224#else225 return get32(addr);226#endif227}228 229/// Read a ULEB128 into a 64-bit word.230inline uint64_t LocalAddressSpace::getULEB128(pint_t &addr, pint_t end) {231 const uint8_t *p = (uint8_t *)addr;232 const uint8_t *pend = (uint8_t *)end;233 uint64_t result = 0;234 int bit = 0;235 do {236 uint64_t b;237 238 if (p == pend)239 _LIBUNWIND_ABORT("truncated uleb128 expression");240 241 b = *p & 0x7f;242 243 if (bit >= 64 || b << bit >> bit != b) {244 _LIBUNWIND_ABORT("malformed uleb128 expression");245 } else {246 result |= b << bit;247 bit += 7;248 }249 } while (*p++ >= 0x80);250 addr = (pint_t) p;251 return result;252}253 254/// Read a SLEB128 into a 64-bit word.255inline int64_t LocalAddressSpace::getSLEB128(pint_t &addr, pint_t end) {256 const uint8_t *p = (uint8_t *)addr;257 const uint8_t *pend = (uint8_t *)end;258 uint64_t result = 0;259 int bit = 0;260 uint8_t byte;261 do {262 if (p == pend)263 _LIBUNWIND_ABORT("truncated sleb128 expression");264 byte = *p++;265 result |= (uint64_t)(byte & 0x7f) << bit;266 bit += 7;267 } while (byte & 0x80);268 // sign extend negative numbers269 if ((byte & 0x40) != 0 && bit < 64)270 result |= (-1ULL) << bit;271 addr = (pint_t) p;272 return (int64_t)result;273}274 275inline LocalAddressSpace::pint_t276LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,277 pint_t datarelBase, pint_t *resultAddr) {278 pint_t startAddr = addr;279 const uint8_t *p = (uint8_t *)addr;280 pint_t result;281 282 // first get value283 switch (encoding & 0x0F) {284 case DW_EH_PE_ptr:285 result = getP(addr);286 p += sizeof(pint_t);287 addr = (pint_t) p;288 break;289 case DW_EH_PE_uleb128:290 result = (pint_t)getULEB128(addr, end);291 break;292 case DW_EH_PE_udata2:293 result = get16(addr);294 p += 2;295 addr = (pint_t) p;296 break;297 case DW_EH_PE_udata4:298 result = get32(addr);299 p += 4;300 addr = (pint_t) p;301 break;302 case DW_EH_PE_udata8:303 result = (pint_t)get64(addr);304 p += 8;305 addr = (pint_t) p;306 break;307 case DW_EH_PE_sleb128:308 result = (pint_t)getSLEB128(addr, end);309 break;310 case DW_EH_PE_sdata2:311 // Sign extend from signed 16-bit value.312 result = (pint_t)(int16_t)get16(addr);313 p += 2;314 addr = (pint_t) p;315 break;316 case DW_EH_PE_sdata4:317 // Sign extend from signed 32-bit value.318 result = (pint_t)(int32_t)get32(addr);319 p += 4;320 addr = (pint_t) p;321 break;322 case DW_EH_PE_sdata8:323 result = (pint_t)get64(addr);324 p += 8;325 addr = (pint_t) p;326 break;327 default:328 _LIBUNWIND_ABORT("unknown pointer encoding");329 }330 331 // then add relative offset332 switch (encoding & 0x70) {333 case DW_EH_PE_absptr:334 // do nothing335 break;336 case DW_EH_PE_pcrel:337 result += startAddr;338 break;339 case DW_EH_PE_textrel:340 _LIBUNWIND_ABORT("DW_EH_PE_textrel pointer encoding not supported");341 break;342 case DW_EH_PE_datarel:343 // DW_EH_PE_datarel is only valid in a few places, so the parameter has a344 // default value of 0, and we abort in the event that someone calls this345 // function with a datarelBase of 0 and DW_EH_PE_datarel encoding.346 if (datarelBase == 0)347 _LIBUNWIND_ABORT("DW_EH_PE_datarel is invalid with a datarelBase of 0");348 result += datarelBase;349 break;350 case DW_EH_PE_funcrel:351 _LIBUNWIND_ABORT("DW_EH_PE_funcrel pointer encoding not supported");352 break;353 case DW_EH_PE_aligned:354 _LIBUNWIND_ABORT("DW_EH_PE_aligned pointer encoding not supported");355 break;356 default:357 _LIBUNWIND_ABORT("unknown pointer encoding");358 break;359 }360 361 if (encoding & DW_EH_PE_indirect) {362 if (resultAddr)363 *resultAddr = result;364 result = getP(result);365 } else {366 if (resultAddr)367 *resultAddr = startAddr;368 }369 370 return result;371}372 373#if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)374 375// The ElfW() macro for pointer-size independent ELF header traversal is not376// provided by <link.h> on some systems (e.g., FreeBSD). On these systems the377// data structures are just called Elf_XXX. Define ElfW() locally.378#if !defined(ElfW)379 #define ElfW(type) Elf_##type380#endif381#if !defined(Elf_Half)382 typedef ElfW(Half) Elf_Half;383#endif384#if !defined(Elf_Phdr)385 typedef ElfW(Phdr) Elf_Phdr;386#endif387#if !defined(Elf_Addr)388 typedef ElfW(Addr) Elf_Addr;389#endif390 391struct _LIBUNWIND_HIDDEN dl_iterate_cb_data {392 LocalAddressSpace *addressSpace;393 UnwindInfoSections *sects;394 uintptr_t targetAddr;395};396 397#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)398#include "FrameHeaderCache.hpp"399 400// Typically there is one cache per process, but when libunwind is built as a401// hermetic static library, then each shared object may have its own cache.402static FrameHeaderCache TheFrameHeaderCache;403#endif404 405static bool checkAddrInSegment(const Elf_Phdr *phdr, size_t image_base,406 dl_iterate_cb_data *cbdata) {407 if (phdr->p_type == PT_LOAD) {408 uintptr_t begin = image_base + phdr->p_vaddr;409 uintptr_t end = begin + phdr->p_memsz;410 if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) {411 cbdata->sects->dso_base = begin;412 cbdata->sects->text_segment_length = phdr->p_memsz;413 return true;414 }415 }416 return false;417}418 419static bool checkForUnwindInfoSegment(const Elf_Phdr *phdr, size_t image_base,420 dl_iterate_cb_data *cbdata) {421#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)422 if (phdr->p_type == PT_GNU_EH_FRAME) {423 EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo;424 uintptr_t eh_frame_hdr_start = image_base + phdr->p_vaddr;425 cbdata->sects->dwarf_index_section = eh_frame_hdr_start;426 cbdata->sects->dwarf_index_section_length = phdr->p_memsz;427 if (EHHeaderParser<LocalAddressSpace>::decodeEHHdr(428 *cbdata->addressSpace, eh_frame_hdr_start,429 eh_frame_hdr_start + phdr->p_memsz, hdrInfo)) {430 // .eh_frame_hdr records the start of .eh_frame, but not its size.431 // Rely on a zero terminator to find the end of the section.432 cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr;433 cbdata->sects->dwarf_section_length = SIZE_MAX;434 return true;435 }436 }437 return false;438#elif defined(_LIBUNWIND_ARM_EHABI)439 if (phdr->p_type == PT_ARM_EXIDX) {440 uintptr_t exidx_start = image_base + phdr->p_vaddr;441 cbdata->sects->arm_section = exidx_start;442 cbdata->sects->arm_section_length = phdr->p_memsz;443 return true;444 }445 return false;446#else447#error Need one of _LIBUNWIND_SUPPORT_DWARF_INDEX or _LIBUNWIND_ARM_EHABI448#endif449}450 451static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo,452 size_t pinfo_size, void *data) {453 auto cbdata = static_cast<dl_iterate_cb_data *>(data);454 if (pinfo->dlpi_phnum == 0 || cbdata->targetAddr < pinfo->dlpi_addr)455 return 0;456#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)457 if (TheFrameHeaderCache.find(pinfo, pinfo_size, data))458 return 1;459#else460 // Avoid warning about unused variable.461 (void)pinfo_size;462#endif463 464 Elf_Addr image_base = pinfo->dlpi_addr;465 466 // Most shared objects seen in this callback function likely don't contain the467 // target address, so optimize for that. Scan for a matching PT_LOAD segment468 // first and bail when it isn't found.469 bool found_text = false;470 for (Elf_Half i = 0; i < pinfo->dlpi_phnum; ++i) {471 if (checkAddrInSegment(&pinfo->dlpi_phdr[i], image_base, cbdata)) {472 found_text = true;473 break;474 }475 }476 if (!found_text)477 return 0;478 479 // PT_GNU_EH_FRAME and PT_ARM_EXIDX are usually near the end. Iterate480 // backward.481 bool found_unwind = false;482 for (Elf_Half i = pinfo->dlpi_phnum; i > 0; i--) {483 const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i - 1];484 if (checkForUnwindInfoSegment(phdr, image_base, cbdata)) {485 found_unwind = true;486 break;487 }488 }489 if (!found_unwind)490 return 0;491 492#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)493 TheFrameHeaderCache.add(cbdata->sects);494#endif495 return 1;496}497 498#endif // defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)499 500 501inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,502 UnwindInfoSections &info) {503#ifdef __APPLE__504 dyld_unwind_sections dyldInfo;505 if (_dyld_find_unwind_sections((void *)targetAddr, &dyldInfo)) {506 info.dso_base = (uintptr_t)dyldInfo.mh;507 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)508 info.dwarf_section = (uintptr_t)dyldInfo.dwarf_section;509 info.dwarf_section_length = (size_t)dyldInfo.dwarf_section_length;510 #endif511 info.compact_unwind_section = (uintptr_t)dyldInfo.compact_unwind_section;512 info.compact_unwind_section_length = (size_t)dyldInfo.compact_unwind_section_length;513 return true;514 }515 516 unw_dynamic_unwind_sections dynamicUnwindSectionInfo;517 if (findDynamicUnwindSections((void *)targetAddr,518 &dynamicUnwindSectionInfo)) {519 info.dso_base = dynamicUnwindSectionInfo.dso_base;520#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)521 info.dwarf_section = (uintptr_t)dynamicUnwindSectionInfo.dwarf_section;522 info.dwarf_section_length = dynamicUnwindSectionInfo.dwarf_section_length;523#endif524 info.compact_unwind_section =525 (uintptr_t)dynamicUnwindSectionInfo.compact_unwind_section;526 info.compact_unwind_section_length =527 dynamicUnwindSectionInfo.compact_unwind_section_length;528 return true;529 }530 531#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL)532 info.dso_base = 0;533 // Bare metal is statically linked, so no need to ask the dynamic loader534 info.dwarf_section_length = (size_t)(&__eh_frame_end - &__eh_frame_start);535 info.dwarf_section = (uintptr_t)(&__eh_frame_start);536 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p",537 (void *)info.dwarf_section, (void *)info.dwarf_section_length);538#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)539 info.dwarf_index_section = (uintptr_t)(&__eh_frame_hdr_start);540 info.dwarf_index_section_length = (size_t)(&__eh_frame_hdr_end - &__eh_frame_hdr_start);541 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: index section %p length %p",542 (void *)info.dwarf_index_section, (void *)info.dwarf_index_section_length);543#endif544 if (info.dwarf_section_length)545 return true;546#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)547 // Bare metal is statically linked, so no need to ask the dynamic loader548 info.arm_section = (uintptr_t)(&__exidx_start);549 info.arm_section_length = (size_t)(&__exidx_end - &__exidx_start);550 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p",551 (void *)info.arm_section, (void *)info.arm_section_length);552 if (info.arm_section && info.arm_section_length)553 return true;554#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_WIN32)555 HMODULE mods[1024];556 HANDLE process = GetCurrentProcess();557 DWORD needed;558 559 if (!EnumProcessModules(process, mods, sizeof(mods), &needed)) {560 DWORD err = GetLastError();561 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: EnumProcessModules failed, "562 "returned error %d", (int)err);563 (void)err;564 return false;565 }566 567 for (unsigned i = 0; i < (needed / sizeof(HMODULE)); i++) {568 PIMAGE_DOS_HEADER pidh = (PIMAGE_DOS_HEADER)mods[i];569 PIMAGE_NT_HEADERS pinh = (PIMAGE_NT_HEADERS)((BYTE *)pidh + pidh->e_lfanew);570 PIMAGE_FILE_HEADER pifh = (PIMAGE_FILE_HEADER)&pinh->FileHeader;571 PIMAGE_SECTION_HEADER pish = IMAGE_FIRST_SECTION(pinh);572 bool found_obj = false;573 bool found_hdr = false;574 575 info.dso_base = (uintptr_t)mods[i];576 for (unsigned j = 0; j < pifh->NumberOfSections; j++, pish++) {577 uintptr_t begin = pish->VirtualAddress + (uintptr_t)mods[i];578 uintptr_t end = begin + pish->Misc.VirtualSize;579 if (!strncmp((const char *)pish->Name, ".text",580 IMAGE_SIZEOF_SHORT_NAME)) {581 if (targetAddr >= begin && targetAddr < end)582 found_obj = true;583 } else if (!strncmp((const char *)pish->Name, ".eh_frame",584 IMAGE_SIZEOF_SHORT_NAME)) {585 info.dwarf_section = begin;586 info.dwarf_section_length = pish->Misc.VirtualSize;587 found_hdr = true;588 }589 if (found_obj && found_hdr)590 return true;591 }592 }593 return false;594#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)595 // Don't even bother, since Windows has functions that do all this stuff596 // for us.597 (void)targetAddr;598 (void)info;599 return true;600#elif defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)601 // The traceback table is used for unwinding.602 (void)targetAddr;603 (void)info;604 return true;605#elif defined(_LIBUNWIND_USE_DL_UNWIND_FIND_EXIDX)606 int length = 0;607 info.arm_section =608 (uintptr_t)dl_unwind_find_exidx((_Unwind_Ptr)targetAddr, &length);609 info.arm_section_length = (size_t)length * sizeof(EHABIIndexEntry);610 if (info.arm_section && info.arm_section_length)611 return true;612#elif defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)613 // Use DLFO_STRUCT_HAS_EH_DBASE to determine the existence of614 // `_dl_find_object`. Use _LIBUNWIND_SUPPORT_DWARF_INDEX, because libunwind615 // support for _dl_find_object on other unwind formats is not implemented,616 // yet.617#if defined(DLFO_STRUCT_HAS_EH_DBASE) & defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)618 // We expect `_dl_find_object` to return PT_GNU_EH_FRAME.619#if DLFO_EH_SEGMENT_TYPE != PT_GNU_EH_FRAME620#error _dl_find_object retrieves an unexpected section type621#endif622 // We look-up `dl_find_object` dynamically at runtime to ensure backwards623 // compatibility with earlier version of glibc not yet providing it. On older624 // systems, we gracefully fallback to `dl_iterate_phdr`. Cache the pointer625 // so we only look it up once. Do manual lock to avoid _cxa_guard_acquire.626 static decltype(_dl_find_object) *dlFindObject;627 static bool dlFindObjectChecked = false;628 if (!dlFindObjectChecked) {629 dlFindObject = reinterpret_cast<decltype(_dl_find_object) *>(630 dlsym(RTLD_DEFAULT, "_dl_find_object"));631 dlFindObjectChecked = true;632 }633 // Try to find the unwind info using `dl_find_object`634 dl_find_object findResult;635 if (dlFindObject && dlFindObject((void *)targetAddr, &findResult) == 0) {636 if (findResult.dlfo_eh_frame == nullptr) {637 // Found an entry for `targetAddr`, but there is no unwind info.638 return false;639 }640 info.dso_base = reinterpret_cast<uintptr_t>(findResult.dlfo_map_start);641 info.text_segment_length = static_cast<size_t>(642 (char *)findResult.dlfo_map_end - (char *)findResult.dlfo_map_start);643 644 // Record the start of PT_GNU_EH_FRAME.645 info.dwarf_index_section =646 reinterpret_cast<uintptr_t>(findResult.dlfo_eh_frame);647 // `_dl_find_object` does not give us the size of PT_GNU_EH_FRAME.648 // Setting length to `SIZE_MAX` effectively disables all range checks.649 info.dwarf_index_section_length = SIZE_MAX;650 EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo;651 if (!EHHeaderParser<LocalAddressSpace>::decodeEHHdr(652 *this, info.dwarf_index_section,653 info.dwarf_index_section + info.dwarf_index_section_length,654 hdrInfo)) {655 return false;656 }657 // Record the start of the FDE and use SIZE_MAX to indicate that we do658 // not know the end address.659 info.dwarf_section = hdrInfo.eh_frame_ptr;660 info.dwarf_section_length = SIZE_MAX;661 return true;662 }663#endif664 dl_iterate_cb_data cb_data = {this, &info, targetAddr};665 int found = dl_iterate_phdr(findUnwindSectionsByPhdr, &cb_data);666 return static_cast<bool>(found);667#endif668 669 return false;670}671 672inline bool LocalAddressSpace::findOtherFDE(pint_t targetAddr, pint_t &fde) {673 // TO DO: if OS has way to dynamically register FDEs, check that.674 (void)targetAddr;675 (void)fde;676 return false;677}678 679inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf,680 size_t bufLen,681 unw_word_t *offset) {682#if _LIBUNWIND_USE_DLADDR683 Dl_info dyldInfo;684 if (dladdr((void *)addr, &dyldInfo)) {685 if (dyldInfo.dli_sname != NULL) {686 snprintf(buf, bufLen, "%s", dyldInfo.dli_sname);687 *offset = (addr - (pint_t) dyldInfo.dli_saddr);688 return true;689 }690 }691#elif defined(_AIX)692 uint16_t nameLen;693 char *funcName = getFuncNameFromTBTable(addr, nameLen, offset);694 if (funcName != NULL) {695 snprintf(buf, bufLen, "%.*s", nameLen, funcName);696 return true;697 }698#else699 (void)addr;700 (void)buf;701 (void)bufLen;702 (void)offset;703#endif704 return false;705}706 707} // namespace libunwind708 709#endif // __ADDRESSSPACE_HPP__710