1 //===----------------------------------------------------------------------===//
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-exception
8 // Abstracts accessing local vs remote address spaces.
10 //===----------------------------------------------------------------------===//
12 #ifndef __ADDRESSSPACE_HPP__
13 #define __ADDRESSSPACE_HPP__
20 #include "libunwind.h"
23 #include "EHHeaderParser.hpp"
24 #include "Registers.hpp"
26 #ifndef _LIBUNWIND_USE_DLADDR
27 #if !(defined(_LIBUNWIND_IS_BAREMETAL) || defined(_WIN32) || defined(_AIX))
28 #define _LIBUNWIND_USE_DLADDR 1
30 #define _LIBUNWIND_USE_DLADDR 0
34 #if _LIBUNWIND_USE_DLADDR
36 #if defined(__ELF__) && defined(_LIBUNWIND_LINK_DL_LIB)
37 #pragma comment(lib, "dl")
41 #if defined(_LIBUNWIND_ARM_EHABI)
42 struct EHABIIndexEntry {
43 uint32_t functionOffset;
50 char *getFuncNameFromTBTable(uintptr_t pc, uint16_t &NameLen,
57 struct dyld_unwind_sections
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;
66 // In 10.7.0 or later, libSystem.dylib implements this function.
67 extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *);
69 #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL)
71 // When statically linked on bare-metal, the symbols for the EH table are looked
72 // up without going through the dynamic loader.
74 // The following linker script may be used to produce the necessary sections and symbols.
75 // Unless the --eh-frame-hdr linker option is provided, the section is not generated
76 // and does not take space in the output file.
80 // __eh_frame_start = .;
82 // __eh_frame_end = .;
87 // KEEP(*(.eh_frame_hdr))
90 // __eh_frame_hdr_start = SIZEOF(.eh_frame_hdr) > 0 ? ADDR(.eh_frame_hdr) : 0;
91 // __eh_frame_hdr_end = SIZEOF(.eh_frame_hdr) > 0 ? . : 0;
93 extern char __eh_frame_start;
94 extern char __eh_frame_end;
96 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
97 extern char __eh_frame_hdr_start;
98 extern char __eh_frame_hdr_end;
101 #elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)
103 // When statically linked on bare-metal, the symbols for the EH table are looked
104 // up without going through the dynamic loader.
105 extern char __exidx_start;
106 extern char __exidx_end;
108 #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_WIN32)
113 #elif defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) || \
114 defined(_LIBUNWIND_USE_DL_UNWIND_FIND_EXIDX)
120 namespace libunwind {
122 /// Used by findUnwindSections() to return info about needed sections.
123 struct UnwindInfoSections {
124 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) || \
125 defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) || \
126 defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)
127 // No dso_base for SEH.
130 #if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)
131 size_t text_segment_length;
133 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
134 uintptr_t dwarf_section;
135 size_t dwarf_section_length;
137 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
138 uintptr_t dwarf_index_section;
139 size_t dwarf_index_section_length;
141 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
142 uintptr_t compact_unwind_section;
143 size_t compact_unwind_section_length;
145 #if defined(_LIBUNWIND_ARM_EHABI)
146 uintptr_t arm_section;
147 size_t arm_section_length;
152 /// LocalAddressSpace is used as a template parameter to UnwindCursor when
153 /// unwinding a thread in the same process. The wrappers compile away,
154 /// making local unwinds fast.
155 class _LIBUNWIND_HIDDEN LocalAddressSpace {
157 typedef uintptr_t pint_t;
158 typedef intptr_t sint_t;
159 uint8_t get8(pint_t addr) {
161 memcpy(&val, (void *)addr, sizeof(val));
164 uint16_t get16(pint_t addr) {
166 memcpy(&val, (void *)addr, sizeof(val));
169 uint32_t get32(pint_t addr) {
171 memcpy(&val, (void *)addr, sizeof(val));
174 uint64_t get64(pint_t addr) {
176 memcpy(&val, (void *)addr, sizeof(val));
179 double getDouble(pint_t addr) {
181 memcpy(&val, (void *)addr, sizeof(val));
184 v128 getVector(pint_t addr) {
186 memcpy(&val, (void *)addr, sizeof(val));
189 uintptr_t getP(pint_t addr);
190 uint64_t getRegister(pint_t addr);
191 static uint64_t getULEB128(pint_t &addr, pint_t end);
192 static int64_t getSLEB128(pint_t &addr, pint_t end);
194 pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
195 pint_t datarelBase = 0);
196 bool findFunctionName(pint_t addr, char *buf, size_t bufLen,
198 bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
199 bool findOtherFDE(pint_t targetAddr, pint_t &fde);
201 static LocalAddressSpace sThisAddressSpace;
204 inline uintptr_t LocalAddressSpace::getP(pint_t addr) {
205 #if __SIZEOF_POINTER__ == 8
212 inline uint64_t LocalAddressSpace::getRegister(pint_t addr) {
213 #if __SIZEOF_POINTER__ == 8 || defined(__mips64)
220 /// Read a ULEB128 into a 64-bit word.
221 inline uint64_t LocalAddressSpace::getULEB128(pint_t &addr, pint_t end) {
222 const uint8_t *p = (uint8_t *)addr;
223 const uint8_t *pend = (uint8_t *)end;
230 _LIBUNWIND_ABORT("truncated uleb128 expression");
234 if (bit >= 64 || b << bit >> bit != b) {
235 _LIBUNWIND_ABORT("malformed uleb128 expression");
240 } while (*p++ >= 0x80);
245 /// Read a SLEB128 into a 64-bit word.
246 inline int64_t LocalAddressSpace::getSLEB128(pint_t &addr, pint_t end) {
247 const uint8_t *p = (uint8_t *)addr;
248 const uint8_t *pend = (uint8_t *)end;
254 _LIBUNWIND_ABORT("truncated sleb128 expression");
256 result |= (uint64_t)(byte & 0x7f) << bit;
258 } while (byte & 0x80);
259 // sign extend negative numbers
260 if ((byte & 0x40) != 0 && bit < 64)
261 result |= (-1ULL) << bit;
266 inline LocalAddressSpace::pint_t
267 LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
268 pint_t datarelBase) {
269 pint_t startAddr = addr;
270 const uint8_t *p = (uint8_t *)addr;
274 switch (encoding & 0x0F) {
280 case DW_EH_PE_uleb128:
281 result = (pint_t)getULEB128(addr, end);
283 case DW_EH_PE_udata2:
284 result = get16(addr);
288 case DW_EH_PE_udata4:
289 result = get32(addr);
293 case DW_EH_PE_udata8:
294 result = (pint_t)get64(addr);
298 case DW_EH_PE_sleb128:
299 result = (pint_t)getSLEB128(addr, end);
301 case DW_EH_PE_sdata2:
302 // Sign extend from signed 16-bit value.
303 result = (pint_t)(int16_t)get16(addr);
307 case DW_EH_PE_sdata4:
308 // Sign extend from signed 32-bit value.
309 result = (pint_t)(int32_t)get32(addr);
313 case DW_EH_PE_sdata8:
314 result = (pint_t)get64(addr);
319 _LIBUNWIND_ABORT("unknown pointer encoding");
322 // then add relative offset
323 switch (encoding & 0x70) {
324 case DW_EH_PE_absptr:
330 case DW_EH_PE_textrel:
331 _LIBUNWIND_ABORT("DW_EH_PE_textrel pointer encoding not supported");
333 case DW_EH_PE_datarel:
334 // DW_EH_PE_datarel is only valid in a few places, so the parameter has a
335 // default value of 0, and we abort in the event that someone calls this
336 // function with a datarelBase of 0 and DW_EH_PE_datarel encoding.
337 if (datarelBase == 0)
338 _LIBUNWIND_ABORT("DW_EH_PE_datarel is invalid with a datarelBase of 0");
339 result += datarelBase;
341 case DW_EH_PE_funcrel:
342 _LIBUNWIND_ABORT("DW_EH_PE_funcrel pointer encoding not supported");
344 case DW_EH_PE_aligned:
345 _LIBUNWIND_ABORT("DW_EH_PE_aligned pointer encoding not supported");
348 _LIBUNWIND_ABORT("unknown pointer encoding");
352 if (encoding & DW_EH_PE_indirect)
353 result = getP(result);
358 #if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)
360 // The ElfW() macro for pointer-size independent ELF header traversal is not
361 // provided by <link.h> on some systems (e.g., FreeBSD). On these systems the
362 // data structures are just called Elf_XXX. Define ElfW() locally.
364 #define ElfW(type) Elf_##type
366 #if !defined(Elf_Half)
367 typedef ElfW(Half) Elf_Half;
369 #if !defined(Elf_Phdr)
370 typedef ElfW(Phdr) Elf_Phdr;
372 #if !defined(Elf_Addr)
373 typedef ElfW(Addr) Elf_Addr;
376 static Elf_Addr calculateImageBase(struct dl_phdr_info *pinfo) {
377 Elf_Addr image_base = pinfo->dlpi_addr;
378 #if defined(__ANDROID__) && __ANDROID_API__ < 18
379 if (image_base == 0) {
380 // Normally, an image base of 0 indicates a non-PIE executable. On
381 // versions of Android prior to API 18, the dynamic linker reported a
382 // dlpi_addr of 0 for PIE executables. Compute the true image base
383 // using the PT_PHDR segment.
384 // See https://github.com/android/ndk/issues/505.
385 for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) {
386 const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i];
387 if (phdr->p_type == PT_PHDR) {
388 image_base = reinterpret_cast<Elf_Addr>(pinfo->dlpi_phdr) -
398 struct _LIBUNWIND_HIDDEN dl_iterate_cb_data {
399 LocalAddressSpace *addressSpace;
400 UnwindInfoSections *sects;
401 uintptr_t targetAddr;
404 #if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
405 #include "FrameHeaderCache.hpp"
407 // Typically there is one cache per process, but when libunwind is built as a
408 // hermetic static library, then each shared object may have its own cache.
409 static FrameHeaderCache TheFrameHeaderCache;
412 static bool checkAddrInSegment(const Elf_Phdr *phdr, size_t image_base,
413 dl_iterate_cb_data *cbdata) {
414 if (phdr->p_type == PT_LOAD) {
415 uintptr_t begin = image_base + phdr->p_vaddr;
416 uintptr_t end = begin + phdr->p_memsz;
417 if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) {
418 cbdata->sects->dso_base = begin;
419 cbdata->sects->text_segment_length = phdr->p_memsz;
426 static bool checkForUnwindInfoSegment(const Elf_Phdr *phdr, size_t image_base,
427 dl_iterate_cb_data *cbdata) {
428 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
429 if (phdr->p_type == PT_GNU_EH_FRAME) {
430 EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo;
431 uintptr_t eh_frame_hdr_start = image_base + phdr->p_vaddr;
432 cbdata->sects->dwarf_index_section = eh_frame_hdr_start;
433 cbdata->sects->dwarf_index_section_length = phdr->p_memsz;
434 if (EHHeaderParser<LocalAddressSpace>::decodeEHHdr(
435 *cbdata->addressSpace, eh_frame_hdr_start, phdr->p_memsz,
437 // .eh_frame_hdr records the start of .eh_frame, but not its size.
438 // Rely on a zero terminator to find the end of the section.
439 cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr;
440 cbdata->sects->dwarf_section_length = SIZE_MAX;
445 #elif defined(_LIBUNWIND_ARM_EHABI)
446 if (phdr->p_type == PT_ARM_EXIDX) {
447 uintptr_t exidx_start = image_base + phdr->p_vaddr;
448 cbdata->sects->arm_section = exidx_start;
449 cbdata->sects->arm_section_length = phdr->p_memsz;
454 #error Need one of _LIBUNWIND_SUPPORT_DWARF_INDEX or _LIBUNWIND_ARM_EHABI
458 static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo,
459 size_t pinfo_size, void *data) {
460 auto cbdata = static_cast<dl_iterate_cb_data *>(data);
461 if (pinfo->dlpi_phnum == 0 || cbdata->targetAddr < pinfo->dlpi_addr)
463 #if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
464 if (TheFrameHeaderCache.find(pinfo, pinfo_size, data))
467 // Avoid warning about unused variable.
471 Elf_Addr image_base = calculateImageBase(pinfo);
473 // Most shared objects seen in this callback function likely don't contain the
474 // target address, so optimize for that. Scan for a matching PT_LOAD segment
475 // first and bail when it isn't found.
476 bool found_text = false;
477 for (Elf_Half i = 0; i < pinfo->dlpi_phnum; ++i) {
478 if (checkAddrInSegment(&pinfo->dlpi_phdr[i], image_base, cbdata)) {
486 // PT_GNU_EH_FRAME and PT_ARM_EXIDX are usually near the end. Iterate
488 bool found_unwind = false;
489 for (Elf_Half i = pinfo->dlpi_phnum; i > 0; i--) {
490 const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i - 1];
491 if (checkForUnwindInfoSegment(phdr, image_base, cbdata)) {
499 #if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
500 TheFrameHeaderCache.add(cbdata->sects);
505 #endif // defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)
508 inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
509 UnwindInfoSections &info) {
511 dyld_unwind_sections dyldInfo;
512 if (_dyld_find_unwind_sections((void *)targetAddr, &dyldInfo)) {
513 info.dso_base = (uintptr_t)dyldInfo.mh;
514 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
515 info.dwarf_section = (uintptr_t)dyldInfo.dwarf_section;
516 info.dwarf_section_length = (size_t)dyldInfo.dwarf_section_length;
518 info.compact_unwind_section = (uintptr_t)dyldInfo.compact_unwind_section;
519 info.compact_unwind_section_length = (size_t)dyldInfo.compact_unwind_section_length;
522 #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL)
524 // Bare metal is statically linked, so no need to ask the dynamic loader
525 info.dwarf_section_length = (size_t)(&__eh_frame_end - &__eh_frame_start);
526 info.dwarf_section = (uintptr_t)(&__eh_frame_start);
527 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p",
528 (void *)info.dwarf_section, (void *)info.dwarf_section_length);
529 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
530 info.dwarf_index_section = (uintptr_t)(&__eh_frame_hdr_start);
531 info.dwarf_index_section_length = (size_t)(&__eh_frame_hdr_end - &__eh_frame_hdr_start);
532 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: index section %p length %p",
533 (void *)info.dwarf_index_section, (void *)info.dwarf_index_section_length);
535 if (info.dwarf_section_length)
537 #elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)
538 // Bare metal is statically linked, so no need to ask the dynamic loader
539 info.arm_section = (uintptr_t)(&__exidx_start);
540 info.arm_section_length = (size_t)(&__exidx_end - &__exidx_start);
541 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p",
542 (void *)info.arm_section, (void *)info.arm_section_length);
543 if (info.arm_section && info.arm_section_length)
545 #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_WIN32)
547 HANDLE process = GetCurrentProcess();
550 if (!EnumProcessModules(process, mods, sizeof(mods), &needed)) {
551 DWORD err = GetLastError();
552 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: EnumProcessModules failed, "
553 "returned error %d", (int)err);
557 for (unsigned i = 0; i < (needed / sizeof(HMODULE)); i++) {
558 PIMAGE_DOS_HEADER pidh = (PIMAGE_DOS_HEADER)mods[i];
559 PIMAGE_NT_HEADERS pinh = (PIMAGE_NT_HEADERS)((BYTE *)pidh + pidh->e_lfanew);
560 PIMAGE_FILE_HEADER pifh = (PIMAGE_FILE_HEADER)&pinh->FileHeader;
561 PIMAGE_SECTION_HEADER pish = IMAGE_FIRST_SECTION(pinh);
562 bool found_obj = false;
563 bool found_hdr = false;
565 info.dso_base = (uintptr_t)mods[i];
566 for (unsigned j = 0; j < pifh->NumberOfSections; j++, pish++) {
567 uintptr_t begin = pish->VirtualAddress + (uintptr_t)mods[i];
568 uintptr_t end = begin + pish->Misc.VirtualSize;
569 if (!strncmp((const char *)pish->Name, ".text",
570 IMAGE_SIZEOF_SHORT_NAME)) {
571 if (targetAddr >= begin && targetAddr < end)
573 } else if (!strncmp((const char *)pish->Name, ".eh_frame",
574 IMAGE_SIZEOF_SHORT_NAME)) {
575 info.dwarf_section = begin;
576 info.dwarf_section_length = pish->Misc.VirtualSize;
579 if (found_obj && found_hdr)
584 #elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
585 // Don't even bother, since Windows has functions that do all this stuff
590 #elif defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
591 // The traceback table is used for unwinding.
595 #elif defined(_LIBUNWIND_USE_DL_UNWIND_FIND_EXIDX)
598 (uintptr_t)dl_unwind_find_exidx((_Unwind_Ptr)targetAddr, &length);
599 info.arm_section_length = (size_t)length * sizeof(EHABIIndexEntry);
600 if (info.arm_section && info.arm_section_length)
602 #elif defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)
603 dl_iterate_cb_data cb_data = {this, &info, targetAddr};
604 int found = dl_iterate_phdr(findUnwindSectionsByPhdr, &cb_data);
605 return static_cast<bool>(found);
611 inline bool LocalAddressSpace::findOtherFDE(pint_t targetAddr, pint_t &fde) {
612 // TO DO: if OS has way to dynamically register FDEs, check that.
618 inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf,
620 unw_word_t *offset) {
621 #if _LIBUNWIND_USE_DLADDR
623 if (dladdr((void *)addr, &dyldInfo)) {
624 if (dyldInfo.dli_sname != NULL) {
625 snprintf(buf, bufLen, "%s", dyldInfo.dli_sname);
626 *offset = (addr - (pint_t) dyldInfo.dli_saddr);
632 char *funcName = getFuncNameFromTBTable(addr, nameLen, offset);
633 if (funcName != NULL) {
634 snprintf(buf, bufLen, "%.*s", nameLen, funcName);
646 } // namespace libunwind
648 #endif // __ADDRESSSPACE_HPP__