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 uint64_t getRegister(pint_t addr);
152 static uint64_t getULEB128(pint_t &addr, pint_t end);
153 static int64_t getSLEB128(pint_t &addr, pint_t end);
155 pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
156 pint_t datarelBase = 0);
157 bool findFunctionName(pint_t addr, char *buf, size_t bufLen,
159 bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
160 bool findOtherFDE(pint_t targetAddr, pint_t &fde);
162 static LocalAddressSpace sThisAddressSpace;
165 inline uintptr_t LocalAddressSpace::getP(pint_t addr) {
173 inline uint64_t LocalAddressSpace::getRegister(pint_t addr) {
174 #if defined(__LP64__) || defined(__mips64)
181 /// Read a ULEB128 into a 64-bit word.
182 inline uint64_t LocalAddressSpace::getULEB128(pint_t &addr, pint_t end) {
183 const uint8_t *p = (uint8_t *)addr;
184 const uint8_t *pend = (uint8_t *)end;
191 _LIBUNWIND_ABORT("truncated uleb128 expression");
195 if (bit >= 64 || b << bit >> bit != b) {
196 _LIBUNWIND_ABORT("malformed uleb128 expression");
201 } while (*p++ >= 0x80);
206 /// Read a SLEB128 into a 64-bit word.
207 inline int64_t LocalAddressSpace::getSLEB128(pint_t &addr, pint_t end) {
208 const uint8_t *p = (uint8_t *)addr;
209 const uint8_t *pend = (uint8_t *)end;
215 _LIBUNWIND_ABORT("truncated sleb128 expression");
217 result |= ((byte & 0x7f) << bit);
219 } while (byte & 0x80);
220 // sign extend negative numbers
221 if ((byte & 0x40) != 0)
222 result |= (-1LL) << bit;
227 inline LocalAddressSpace::pint_t
228 LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
229 pint_t datarelBase) {
230 pint_t startAddr = addr;
231 const uint8_t *p = (uint8_t *)addr;
235 switch (encoding & 0x0F) {
241 case DW_EH_PE_uleb128:
242 result = (pint_t)getULEB128(addr, end);
244 case DW_EH_PE_udata2:
245 result = get16(addr);
249 case DW_EH_PE_udata4:
250 result = get32(addr);
254 case DW_EH_PE_udata8:
255 result = (pint_t)get64(addr);
259 case DW_EH_PE_sleb128:
260 result = (pint_t)getSLEB128(addr, end);
262 case DW_EH_PE_sdata2:
263 // Sign extend from signed 16-bit value.
264 result = (pint_t)(int16_t)get16(addr);
268 case DW_EH_PE_sdata4:
269 // Sign extend from signed 32-bit value.
270 result = (pint_t)(int32_t)get32(addr);
274 case DW_EH_PE_sdata8:
275 result = (pint_t)get64(addr);
280 _LIBUNWIND_ABORT("unknown pointer encoding");
283 // then add relative offset
284 switch (encoding & 0x70) {
285 case DW_EH_PE_absptr:
291 case DW_EH_PE_textrel:
292 _LIBUNWIND_ABORT("DW_EH_PE_textrel pointer encoding not supported");
294 case DW_EH_PE_datarel:
295 // DW_EH_PE_datarel is only valid in a few places, so the parameter has a
296 // default value of 0, and we abort in the event that someone calls this
297 // function with a datarelBase of 0 and DW_EH_PE_datarel encoding.
298 if (datarelBase == 0)
299 _LIBUNWIND_ABORT("DW_EH_PE_datarel is invalid with a datarelBase of 0");
300 result += datarelBase;
302 case DW_EH_PE_funcrel:
303 _LIBUNWIND_ABORT("DW_EH_PE_funcrel pointer encoding not supported");
305 case DW_EH_PE_aligned:
306 _LIBUNWIND_ABORT("DW_EH_PE_aligned pointer encoding not supported");
309 _LIBUNWIND_ABORT("unknown pointer encoding");
313 if (encoding & DW_EH_PE_indirect)
314 result = getP(result);
320 struct dyld_unwind_sections
322 const struct mach_header* mh;
323 const void* dwarf_section;
324 uintptr_t dwarf_section_length;
325 const void* compact_unwind_section;
326 uintptr_t compact_unwind_section_length;
328 #if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) \
329 && (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)) \
330 || defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
331 // In 10.7.0 or later, libSystem.dylib implements this function.
332 extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *);
334 // In 10.6.x and earlier, we need to implement this functionality.
335 static inline bool _dyld_find_unwind_sections(void* addr,
336 dyld_unwind_sections* info) {
337 // Find mach-o image containing address.
339 if (!dladdr(addr, &dlinfo))
341 const mach_header *mh = (const mach_header *)dlinfo.dli_saddr;
343 // Find dwarf unwind section in that image.
345 const uint8_t *p = getsectiondata(mh, "__TEXT", "__eh_frame", &size);
349 // Fill in return struct.
351 info->dwarf_section = p;
352 info->dwarf_section_length = size;
353 info->compact_unwind_section = 0;
354 info->compact_unwind_section_length = 0;
361 inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
362 UnwindInfoSections &info) {
364 dyld_unwind_sections dyldInfo;
365 if (_dyld_find_unwind_sections((void *)targetAddr, &dyldInfo)) {
366 info.dso_base = (uintptr_t)dyldInfo.mh;
367 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
368 info.dwarf_section = (uintptr_t)dyldInfo.dwarf_section;
369 info.dwarf_section_length = dyldInfo.dwarf_section_length;
371 info.compact_unwind_section = (uintptr_t)dyldInfo.compact_unwind_section;
372 info.compact_unwind_section_length = dyldInfo.compact_unwind_section_length;
375 #elif _LIBUNWIND_ARM_EHABI
376 #ifdef _LIBUNWIND_IS_BAREMETAL
377 // Bare metal is statically linked, so no need to ask the dynamic loader
378 info.arm_section = (uintptr_t)(&__exidx_start);
379 info.arm_section_length = (uintptr_t)(&__exidx_end - &__exidx_start);
382 info.arm_section = (uintptr_t) dl_unwind_find_exidx(
383 (_Unwind_Ptr) targetAddr, &length);
384 info.arm_section_length = (uintptr_t)length;
386 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %X length %x",
387 info.arm_section, info.arm_section_length);
388 if (info.arm_section && info.arm_section_length)
390 #elif _LIBUNWIND_SUPPORT_DWARF_UNWIND
391 #if _LIBUNWIND_SUPPORT_DWARF_INDEX
392 struct dl_iterate_cb_data {
393 LocalAddressSpace *addressSpace;
394 UnwindInfoSections *sects;
395 uintptr_t targetAddr;
398 dl_iterate_cb_data cb_data = {this, &info, targetAddr};
399 int found = dl_iterate_phdr(
400 [](struct dl_phdr_info *pinfo, size_t, void *data) -> int {
401 auto cbdata = static_cast<dl_iterate_cb_data *>(data);
402 size_t object_length;
403 bool found_obj = false;
404 bool found_hdr = false;
407 assert(cbdata->sects);
409 if (cbdata->targetAddr < pinfo->dlpi_addr) {
413 #if !defined(Elf_Half)
414 typedef ElfW(Half) Elf_Half;
416 #if !defined(Elf_Phdr)
417 typedef ElfW(Phdr) Elf_Phdr;
420 for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) {
421 const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i];
422 if (phdr->p_type == PT_LOAD) {
423 uintptr_t begin = pinfo->dlpi_addr + phdr->p_vaddr;
424 uintptr_t end = begin + phdr->p_memsz;
425 if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) {
426 cbdata->sects->dso_base = begin;
427 object_length = phdr->p_memsz;
430 } else if (phdr->p_type == PT_GNU_EH_FRAME) {
431 EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo;
432 uintptr_t eh_frame_hdr_start = pinfo->dlpi_addr + phdr->p_vaddr;
433 cbdata->sects->dwarf_index_section = eh_frame_hdr_start;
434 cbdata->sects->dwarf_index_section_length = phdr->p_memsz;
435 EHHeaderParser<LocalAddressSpace>::decodeEHHdr(
436 *cbdata->addressSpace, eh_frame_hdr_start, phdr->p_memsz,
438 cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr;
443 if (found_obj && found_hdr) {
444 cbdata->sects->dwarf_section_length = object_length;
451 return static_cast<bool>(found);
453 #error "_LIBUNWIND_SUPPORT_DWARF_UNWIND requires _LIBUNWIND_SUPPORT_DWARF_INDEX on this platform."
461 inline bool LocalAddressSpace::findOtherFDE(pint_t targetAddr, pint_t &fde) {
463 return checkKeyMgrRegisteredFDEs(targetAddr, *((void**)&fde));
465 // TO DO: if OS has way to dynamically register FDEs, check that.
472 inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf,
474 unw_word_t *offset) {
475 #ifndef _LIBUNWIND_IS_BAREMETAL
477 if (dladdr((void *)addr, &dyldInfo)) {
478 if (dyldInfo.dli_sname != NULL) {
479 snprintf(buf, bufLen, "%s", dyldInfo.dli_sname);
480 *offset = (addr - (pint_t) dyldInfo.dli_saddr);
492 /// OtherAddressSpace is used as a template parameter to UnwindCursor when
493 /// unwinding a thread in the another process. The other process can be a
494 /// different endianness and a different pointer size which is handled by
495 /// the P template parameter.
496 template <typename P>
497 class OtherAddressSpace {
499 OtherAddressSpace(task_t task) : fTask(task) {}
501 typedef typename P::uint_t pint_t;
503 uint8_t get8(pint_t addr);
504 uint16_t get16(pint_t addr);
505 uint32_t get32(pint_t addr);
506 uint64_t get64(pint_t addr);
507 pint_t getP(pint_t addr);
508 uint64_t getRegister(pint_t addr);
509 uint64_t getULEB128(pint_t &addr, pint_t end);
510 int64_t getSLEB128(pint_t &addr, pint_t end);
511 pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
512 pint_t datarelBase = 0);
513 bool findFunctionName(pint_t addr, char *buf, size_t bufLen,
515 bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
516 bool findOtherFDE(pint_t targetAddr, pint_t &fde);
518 void *localCopy(pint_t addr);
523 template <typename P> uint8_t OtherAddressSpace<P>::get8(pint_t addr) {
524 return *((uint8_t *)localCopy(addr));
527 template <typename P> uint16_t OtherAddressSpace<P>::get16(pint_t addr) {
528 return P::E::get16(*(uint16_t *)localCopy(addr));
531 template <typename P> uint32_t OtherAddressSpace<P>::get32(pint_t addr) {
532 return P::E::get32(*(uint32_t *)localCopy(addr));
535 template <typename P> uint64_t OtherAddressSpace<P>::get64(pint_t addr) {
536 return P::E::get64(*(uint64_t *)localCopy(addr));
539 template <typename P>
540 typename P::uint_t OtherAddressSpace<P>::getP(pint_t addr) {
541 return P::getP(*(uint64_t *)localCopy(addr));
544 template <typename P>
545 typename P::uint_t OtherAddressSpace<P>::getRegister(pint_t addr) {
546 return P::getRegister(*(uint64_t *)localCopy(addr));
549 template <typename P>
550 uint64_t OtherAddressSpace<P>::getULEB128(pint_t &addr, pint_t end) {
551 uintptr_t size = (end - addr);
552 LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr);
553 LocalAddressSpace::pint_t sladdr = laddr;
554 uint64_t result = LocalAddressSpace::getULEB128(laddr, laddr + size);
555 addr += (laddr - sladdr);
559 template <typename P>
560 int64_t OtherAddressSpace<P>::getSLEB128(pint_t &addr, pint_t end) {
561 uintptr_t size = (end - addr);
562 LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr);
563 LocalAddressSpace::pint_t sladdr = laddr;
564 uint64_t result = LocalAddressSpace::getSLEB128(laddr, laddr + size);
565 addr += (laddr - sladdr);
569 template <typename P> void *OtherAddressSpace<P>::localCopy(pint_t addr) {
573 template <typename P>
574 bool OtherAddressSpace<P>::findFunctionName(pint_t addr, char *buf,
575 size_t bufLen, unw_word_t *offset) {
579 /// unw_addr_space is the base class that abstract unw_addr_space_t type in
580 /// libunwind.h points to.
581 struct unw_addr_space {
586 /// unw_addr_space_i386 is the concrete instance that a unw_addr_space_t points
587 /// to when examining
588 /// a 32-bit intel process.
589 struct unw_addr_space_i386 : public unw_addr_space {
590 unw_addr_space_i386(task_t task) : oas(task) {}
591 OtherAddressSpace<Pointer32<LittleEndian> > oas;
594 /// unw_addr_space_x86_64 is the concrete instance that a unw_addr_space_t
595 /// points to when examining
596 /// a 64-bit intel process.
597 struct unw_addr_space_x86_64 : public unw_addr_space {
598 unw_addr_space_x86_64(task_t task) : oas(task) {}
599 OtherAddressSpace<Pointer64<LittleEndian> > oas;
602 /// unw_addr_space_ppc is the concrete instance that a unw_addr_space_t points
603 /// to when examining
604 /// a 32-bit PowerPC process.
605 struct unw_addr_space_ppc : public unw_addr_space {
606 unw_addr_space_ppc(task_t task) : oas(task) {}
607 OtherAddressSpace<Pointer32<BigEndian> > oas;
612 } // namespace libunwind
614 #endif // __ADDRESSSPACE_HPP__