1 //===------------------------- AddressSpace.hpp ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
9 // Abstracts accessing local vs remote address spaces.
11 //===----------------------------------------------------------------------===//
13 #ifndef __ADDRESSSPACE_HPP__
14 #define __ADDRESSSPACE_HPP__
21 #ifndef _LIBUNWIND_IS_BAREMETAL
26 #include <mach-o/getsect.h>
28 bool checkKeyMgrRegisteredFDEs(uintptr_t targetAddr, void *&fde);
32 #include "libunwind.h"
35 #include "Registers.hpp"
37 #if _LIBUNWIND_ARM_EHABI
38 #if defined(__FreeBSD__) || defined(__NetBSD__)
40 #include <sys/link_elf.h>
41 typedef void *_Unwind_Ptr;
43 #elif defined(__linux__)
45 typedef long unsigned int *_Unwind_Ptr;
46 extern "C" _Unwind_Ptr __gnu_Unwind_Find_exidx(_Unwind_Ptr addr, int *len);
48 // Emulate the BSD dl_unwind_find_exidx API when on a GNU libdl system.
49 #define dl_unwind_find_exidx __gnu_Unwind_Find_exidx
51 #elif !defined(_LIBUNWIND_IS_BAREMETAL)
53 #else // !defined(_LIBUNWIND_IS_BAREMETAL)
54 // When statically linked on bare-metal, the symbols for the EH table are looked
55 // up without going through the dynamic loader.
57 uint32_t functionOffset;
58 uint32_t unwindOpcodes;
60 extern EHTEntry __exidx_start;
61 extern EHTEntry __exidx_end;
62 #endif // !defined(_LIBUNWIND_IS_BAREMETAL)
63 #endif // _LIBUNWIND_ARM_EHABI
65 #if defined(__CloudABI__) || defined(__FreeBSD__) || defined(__linux__) || \
67 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND && _LIBUNWIND_SUPPORT_DWARF_INDEX
69 // Macro for machine-independent access to the ELF program headers. This
70 // macro is not available on some systems (e.g., FreeBSD). On these
71 // systems the data structures are just called Elf_XXX. Define ElfW()
74 #define ElfW(type) Elf_##type
76 #include "EHHeaderParser.hpp"
82 /// Used by findUnwindSections() to return info about needed sections.
83 struct UnwindInfoSections {
84 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND || _LIBUNWIND_SUPPORT_DWARF_INDEX || \
85 _LIBUNWIND_SUPPORT_COMPACT_UNWIND
86 // No dso_base for ARM EHABI.
89 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
90 uintptr_t dwarf_section;
91 uintptr_t dwarf_section_length;
93 #if _LIBUNWIND_SUPPORT_DWARF_INDEX
94 uintptr_t dwarf_index_section;
95 uintptr_t dwarf_index_section_length;
97 #if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
98 uintptr_t compact_unwind_section;
99 uintptr_t compact_unwind_section_length;
101 #if _LIBUNWIND_ARM_EHABI
102 uintptr_t arm_section;
103 uintptr_t arm_section_length;
108 /// LocalAddressSpace is used as a template parameter to UnwindCursor when
109 /// unwinding a thread in the same process. The wrappers compile away,
110 /// making local unwinds fast.
111 class __attribute__((visibility("hidden"))) LocalAddressSpace {
114 typedef uint64_t pint_t;
115 typedef int64_t sint_t;
117 typedef uint32_t pint_t;
118 typedef int32_t sint_t;
120 uint8_t get8(pint_t addr) {
122 memcpy(&val, (void *)addr, sizeof(val));
125 uint16_t get16(pint_t addr) {
127 memcpy(&val, (void *)addr, sizeof(val));
130 uint32_t get32(pint_t addr) {
132 memcpy(&val, (void *)addr, sizeof(val));
135 uint64_t get64(pint_t addr) {
137 memcpy(&val, (void *)addr, sizeof(val));
140 double getDouble(pint_t addr) {
142 memcpy(&val, (void *)addr, sizeof(val));
145 v128 getVector(pint_t addr) {
147 memcpy(&val, (void *)addr, sizeof(val));
150 uintptr_t getP(pint_t addr);
151 static uint64_t getULEB128(pint_t &addr, pint_t end);
152 static int64_t getSLEB128(pint_t &addr, pint_t end);
154 pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
155 pint_t datarelBase = 0);
156 bool findFunctionName(pint_t addr, char *buf, size_t bufLen,
158 bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
159 bool findOtherFDE(pint_t targetAddr, pint_t &fde);
161 static LocalAddressSpace sThisAddressSpace;
164 inline uintptr_t LocalAddressSpace::getP(pint_t addr) {
172 /// Read a ULEB128 into a 64-bit word.
173 inline uint64_t LocalAddressSpace::getULEB128(pint_t &addr, pint_t end) {
174 const uint8_t *p = (uint8_t *)addr;
175 const uint8_t *pend = (uint8_t *)end;
182 _LIBUNWIND_ABORT("truncated uleb128 expression");
186 if (bit >= 64 || b << bit >> bit != b) {
187 _LIBUNWIND_ABORT("malformed uleb128 expression");
192 } while (*p++ >= 0x80);
197 /// Read a SLEB128 into a 64-bit word.
198 inline int64_t LocalAddressSpace::getSLEB128(pint_t &addr, pint_t end) {
199 const uint8_t *p = (uint8_t *)addr;
200 const uint8_t *pend = (uint8_t *)end;
206 _LIBUNWIND_ABORT("truncated sleb128 expression");
208 result |= ((byte & 0x7f) << bit);
210 } while (byte & 0x80);
211 // sign extend negative numbers
212 if ((byte & 0x40) != 0)
213 result |= (-1LL) << bit;
218 inline LocalAddressSpace::pint_t
219 LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
220 pint_t datarelBase) {
221 pint_t startAddr = addr;
222 const uint8_t *p = (uint8_t *)addr;
226 switch (encoding & 0x0F) {
232 case DW_EH_PE_uleb128:
233 result = (pint_t)getULEB128(addr, end);
235 case DW_EH_PE_udata2:
236 result = get16(addr);
240 case DW_EH_PE_udata4:
241 result = get32(addr);
245 case DW_EH_PE_udata8:
246 result = (pint_t)get64(addr);
250 case DW_EH_PE_sleb128:
251 result = (pint_t)getSLEB128(addr, end);
253 case DW_EH_PE_sdata2:
254 // Sign extend from signed 16-bit value.
255 result = (pint_t)(int16_t)get16(addr);
259 case DW_EH_PE_sdata4:
260 // Sign extend from signed 32-bit value.
261 result = (pint_t)(int32_t)get32(addr);
265 case DW_EH_PE_sdata8:
266 result = (pint_t)get64(addr);
271 _LIBUNWIND_ABORT("unknown pointer encoding");
274 // then add relative offset
275 switch (encoding & 0x70) {
276 case DW_EH_PE_absptr:
282 case DW_EH_PE_textrel:
283 _LIBUNWIND_ABORT("DW_EH_PE_textrel pointer encoding not supported");
285 case DW_EH_PE_datarel:
286 // DW_EH_PE_datarel is only valid in a few places, so the parameter has a
287 // default value of 0, and we abort in the event that someone calls this
288 // function with a datarelBase of 0 and DW_EH_PE_datarel encoding.
289 if (datarelBase == 0)
290 _LIBUNWIND_ABORT("DW_EH_PE_datarel is invalid with a datarelBase of 0");
291 result += datarelBase;
293 case DW_EH_PE_funcrel:
294 _LIBUNWIND_ABORT("DW_EH_PE_funcrel pointer encoding not supported");
296 case DW_EH_PE_aligned:
297 _LIBUNWIND_ABORT("DW_EH_PE_aligned pointer encoding not supported");
300 _LIBUNWIND_ABORT("unknown pointer encoding");
304 if (encoding & DW_EH_PE_indirect)
305 result = getP(result);
311 struct dyld_unwind_sections
313 const struct mach_header* mh;
314 const void* dwarf_section;
315 uintptr_t dwarf_section_length;
316 const void* compact_unwind_section;
317 uintptr_t compact_unwind_section_length;
319 #if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) \
320 && (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)) \
321 || defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
322 // In 10.7.0 or later, libSystem.dylib implements this function.
323 extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *);
325 // In 10.6.x and earlier, we need to implement this functionality.
326 static inline bool _dyld_find_unwind_sections(void* addr,
327 dyld_unwind_sections* info) {
328 // Find mach-o image containing address.
330 if (!dladdr(addr, &dlinfo))
332 const mach_header *mh = (const mach_header *)dlinfo.dli_saddr;
334 // Find dwarf unwind section in that image.
336 const uint8_t *p = getsectiondata(mh, "__TEXT", "__eh_frame", &size);
340 // Fill in return struct.
342 info->dwarf_section = p;
343 info->dwarf_section_length = size;
344 info->compact_unwind_section = 0;
345 info->compact_unwind_section_length = 0;
352 inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
353 UnwindInfoSections &info) {
355 dyld_unwind_sections dyldInfo;
356 if (_dyld_find_unwind_sections((void *)targetAddr, &dyldInfo)) {
357 info.dso_base = (uintptr_t)dyldInfo.mh;
358 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
359 info.dwarf_section = (uintptr_t)dyldInfo.dwarf_section;
360 info.dwarf_section_length = dyldInfo.dwarf_section_length;
362 info.compact_unwind_section = (uintptr_t)dyldInfo.compact_unwind_section;
363 info.compact_unwind_section_length = dyldInfo.compact_unwind_section_length;
366 #elif _LIBUNWIND_ARM_EHABI
367 #ifdef _LIBUNWIND_IS_BAREMETAL
368 // Bare metal is statically linked, so no need to ask the dynamic loader
369 info.arm_section = (uintptr_t)(&__exidx_start);
370 info.arm_section_length = (uintptr_t)(&__exidx_end - &__exidx_start);
373 info.arm_section = (uintptr_t) dl_unwind_find_exidx(
374 (_Unwind_Ptr) targetAddr, &length);
375 info.arm_section_length = (uintptr_t)length;
377 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %X length %x",
378 info.arm_section, info.arm_section_length);
379 if (info.arm_section && info.arm_section_length)
381 #elif _LIBUNWIND_SUPPORT_DWARF_UNWIND
382 #if _LIBUNWIND_SUPPORT_DWARF_INDEX
383 struct dl_iterate_cb_data {
384 LocalAddressSpace *addressSpace;
385 UnwindInfoSections *sects;
386 uintptr_t targetAddr;
389 dl_iterate_cb_data cb_data = {this, &info, targetAddr};
390 int found = dl_iterate_phdr(
391 [](struct dl_phdr_info *pinfo, size_t, void *data) -> int {
392 auto cbdata = static_cast<dl_iterate_cb_data *>(data);
393 size_t object_length;
394 bool found_obj = false;
395 bool found_hdr = false;
398 assert(cbdata->sects);
400 if (cbdata->targetAddr < pinfo->dlpi_addr) {
404 #if !defined(Elf_Half)
405 typedef ElfW(Half) Elf_Half;
407 #if !defined(Elf_Phdr)
408 typedef ElfW(Phdr) Elf_Phdr;
411 for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) {
412 const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i];
413 if (phdr->p_type == PT_LOAD) {
414 uintptr_t begin = pinfo->dlpi_addr + phdr->p_vaddr;
415 uintptr_t end = begin + phdr->p_memsz;
416 if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) {
417 cbdata->sects->dso_base = begin;
418 object_length = phdr->p_memsz;
421 } else if (phdr->p_type == PT_GNU_EH_FRAME) {
422 EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo;
423 uintptr_t eh_frame_hdr_start = pinfo->dlpi_addr + phdr->p_vaddr;
424 cbdata->sects->dwarf_index_section = eh_frame_hdr_start;
425 cbdata->sects->dwarf_index_section_length = phdr->p_memsz;
426 EHHeaderParser<LocalAddressSpace>::decodeEHHdr(
427 *cbdata->addressSpace, eh_frame_hdr_start, phdr->p_memsz,
429 cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr;
434 if (found_obj && found_hdr) {
435 cbdata->sects->dwarf_section_length = object_length;
442 return static_cast<bool>(found);
444 #error "_LIBUNWIND_SUPPORT_DWARF_UNWIND requires _LIBUNWIND_SUPPORT_DWARF_INDEX on this platform."
452 inline bool LocalAddressSpace::findOtherFDE(pint_t targetAddr, pint_t &fde) {
454 return checkKeyMgrRegisteredFDEs(targetAddr, *((void**)&fde));
456 // TO DO: if OS has way to dynamically register FDEs, check that.
463 inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf,
465 unw_word_t *offset) {
466 #ifndef _LIBUNWIND_IS_BAREMETAL
468 if (dladdr((void *)addr, &dyldInfo)) {
469 if (dyldInfo.dli_sname != NULL) {
470 snprintf(buf, bufLen, "%s", dyldInfo.dli_sname);
471 *offset = (addr - (pint_t) dyldInfo.dli_saddr);
483 /// OtherAddressSpace is used as a template parameter to UnwindCursor when
484 /// unwinding a thread in the another process. The other process can be a
485 /// different endianness and a different pointer size which is handled by
486 /// the P template parameter.
487 template <typename P>
488 class OtherAddressSpace {
490 OtherAddressSpace(task_t task) : fTask(task) {}
492 typedef typename P::uint_t pint_t;
494 uint8_t get8(pint_t addr);
495 uint16_t get16(pint_t addr);
496 uint32_t get32(pint_t addr);
497 uint64_t get64(pint_t addr);
498 pint_t getP(pint_t addr);
499 uint64_t getULEB128(pint_t &addr, pint_t end);
500 int64_t getSLEB128(pint_t &addr, pint_t end);
501 pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
502 pint_t datarelBase = 0);
503 bool findFunctionName(pint_t addr, char *buf, size_t bufLen,
505 bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
506 bool findOtherFDE(pint_t targetAddr, pint_t &fde);
508 void *localCopy(pint_t addr);
513 template <typename P> uint8_t OtherAddressSpace<P>::get8(pint_t addr) {
514 return *((uint8_t *)localCopy(addr));
517 template <typename P> uint16_t OtherAddressSpace<P>::get16(pint_t addr) {
518 return P::E::get16(*(uint16_t *)localCopy(addr));
521 template <typename P> uint32_t OtherAddressSpace<P>::get32(pint_t addr) {
522 return P::E::get32(*(uint32_t *)localCopy(addr));
525 template <typename P> uint64_t OtherAddressSpace<P>::get64(pint_t addr) {
526 return P::E::get64(*(uint64_t *)localCopy(addr));
529 template <typename P>
530 typename P::uint_t OtherAddressSpace<P>::getP(pint_t addr) {
531 return P::getP(*(uint64_t *)localCopy(addr));
534 template <typename P>
535 uint64_t OtherAddressSpace<P>::getULEB128(pint_t &addr, pint_t end) {
536 uintptr_t size = (end - addr);
537 LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr);
538 LocalAddressSpace::pint_t sladdr = laddr;
539 uint64_t result = LocalAddressSpace::getULEB128(laddr, laddr + size);
540 addr += (laddr - sladdr);
544 template <typename P>
545 int64_t OtherAddressSpace<P>::getSLEB128(pint_t &addr, pint_t end) {
546 uintptr_t size = (end - addr);
547 LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr);
548 LocalAddressSpace::pint_t sladdr = laddr;
549 uint64_t result = LocalAddressSpace::getSLEB128(laddr, laddr + size);
550 addr += (laddr - sladdr);
554 template <typename P> void *OtherAddressSpace<P>::localCopy(pint_t addr) {
558 template <typename P>
559 bool OtherAddressSpace<P>::findFunctionName(pint_t addr, char *buf,
560 size_t bufLen, unw_word_t *offset) {
564 /// unw_addr_space is the base class that abstract unw_addr_space_t type in
565 /// libunwind.h points to.
566 struct unw_addr_space {
571 /// unw_addr_space_i386 is the concrete instance that a unw_addr_space_t points
572 /// to when examining
573 /// a 32-bit intel process.
574 struct unw_addr_space_i386 : public unw_addr_space {
575 unw_addr_space_i386(task_t task) : oas(task) {}
576 OtherAddressSpace<Pointer32<LittleEndian> > oas;
579 /// unw_addr_space_x86_64 is the concrete instance that a unw_addr_space_t
580 /// points to when examining
581 /// a 64-bit intel process.
582 struct unw_addr_space_x86_64 : public unw_addr_space {
583 unw_addr_space_x86_64(task_t task) : oas(task) {}
584 OtherAddressSpace<Pointer64<LittleEndian> > oas;
587 /// unw_addr_space_ppc is the concrete instance that a unw_addr_space_t points
588 /// to when examining
589 /// a 32-bit PowerPC process.
590 struct unw_addr_space_ppc : public unw_addr_space {
591 unw_addr_space_ppc(task_t task) : oas(task) {}
592 OtherAddressSpace<Pointer32<BigEndian> > oas;
597 } // namespace libunwind
599 #endif // __ADDRESSSPACE_HPP__