Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1 | //===------------------------- AddressSpace.hpp ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | // |
| 9 | // Abstracts accessing local vs remote address spaces. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef __ADDRESSSPACE_HPP__ |
| 14 | #define __ADDRESSSPACE_HPP__ |
| 15 | |
| 16 | #include <stdint.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | #ifndef _LIBUNWIND_IS_BAREMETAL |
| 22 | #include <dlfcn.h> |
| 23 | #endif |
| 24 | |
| 25 | #ifdef __APPLE__ |
| 26 | #include <mach-o/getsect.h> |
| 27 | namespace libunwind { |
| 28 | bool checkKeyMgrRegisteredFDEs(uintptr_t targetAddr, void *&fde); |
| 29 | } |
| 30 | #endif |
| 31 | |
| 32 | #include "libunwind.h" |
| 33 | #include "config.h" |
| 34 | #include "dwarf2.h" |
| 35 | #include "Registers.hpp" |
| 36 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 37 | namespace libunwind { |
| 38 | |
| 39 | /// Used by findUnwindSections() to return info about needed sections. |
| 40 | struct UnwindInfoSections { |
| 41 | #if _LIBUNWIND_SUPPORT_DWARF_UNWIND || _LIBUNWIND_SUPPORT_DWARF_INDEX || \ |
| 42 | _LIBUNWIND_SUPPORT_COMPACT_UNWIND |
| 43 | // No dso_base for ARM EHABI. |
| 44 | uintptr_t dso_base; |
| 45 | #endif |
| 46 | #if _LIBUNWIND_SUPPORT_DWARF_UNWIND |
| 47 | uintptr_t dwarf_section; |
| 48 | uintptr_t dwarf_section_length; |
| 49 | #endif |
| 50 | #if _LIBUNWIND_SUPPORT_DWARF_INDEX |
| 51 | uintptr_t dwarf_index_section; |
| 52 | uintptr_t dwarf_index_section_length; |
| 53 | #endif |
| 54 | #if _LIBUNWIND_SUPPORT_COMPACT_UNWIND |
| 55 | uintptr_t compact_unwind_section; |
| 56 | uintptr_t compact_unwind_section_length; |
| 57 | #endif |
Logan Chien | 06b0c7a | 2015-07-19 15:23:10 +0000 | [diff] [blame] | 58 | #if _LIBUNWIND_ARM_EHABI |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 59 | uintptr_t arm_section; |
| 60 | uintptr_t arm_section_length; |
| 61 | #endif |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | /// LocalAddressSpace is used as a template parameter to UnwindCursor when |
| 66 | /// unwinding a thread in the same process. The wrappers compile away, |
| 67 | /// making local unwinds fast. |
| 68 | class __attribute__((visibility("hidden"))) LocalAddressSpace { |
| 69 | public: |
| 70 | #ifdef __LP64__ |
| 71 | typedef uint64_t pint_t; |
| 72 | typedef int64_t sint_t; |
| 73 | #else |
| 74 | typedef uint32_t pint_t; |
| 75 | typedef int32_t sint_t; |
| 76 | #endif |
| 77 | uint8_t get8(pint_t addr) { |
| 78 | uint8_t val; |
| 79 | memcpy(&val, (void *)addr, sizeof(val)); |
| 80 | return val; |
| 81 | } |
| 82 | uint16_t get16(pint_t addr) { |
| 83 | uint16_t val; |
| 84 | memcpy(&val, (void *)addr, sizeof(val)); |
| 85 | return val; |
| 86 | } |
| 87 | uint32_t get32(pint_t addr) { |
| 88 | uint32_t val; |
| 89 | memcpy(&val, (void *)addr, sizeof(val)); |
| 90 | return val; |
| 91 | } |
| 92 | uint64_t get64(pint_t addr) { |
| 93 | uint64_t val; |
| 94 | memcpy(&val, (void *)addr, sizeof(val)); |
| 95 | return val; |
| 96 | } |
| 97 | double getDouble(pint_t addr) { |
| 98 | double val; |
| 99 | memcpy(&val, (void *)addr, sizeof(val)); |
| 100 | return val; |
| 101 | } |
| 102 | v128 getVector(pint_t addr) { |
| 103 | v128 val; |
| 104 | memcpy(&val, (void *)addr, sizeof(val)); |
| 105 | return val; |
| 106 | } |
| 107 | uintptr_t getP(pint_t addr); |
| 108 | static uint64_t getULEB128(pint_t &addr, pint_t end); |
| 109 | static int64_t getSLEB128(pint_t &addr, pint_t end); |
| 110 | |
| 111 | pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding, |
| 112 | pint_t datarelBase = 0); |
| 113 | bool findFunctionName(pint_t addr, char *buf, size_t bufLen, |
| 114 | unw_word_t *offset); |
| 115 | bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info); |
| 116 | bool findOtherFDE(pint_t targetAddr, pint_t &fde); |
| 117 | |
| 118 | static LocalAddressSpace sThisAddressSpace; |
| 119 | }; |
| 120 | |
| 121 | inline uintptr_t LocalAddressSpace::getP(pint_t addr) { |
| 122 | #ifdef __LP64__ |
| 123 | return get64(addr); |
| 124 | #else |
| 125 | return get32(addr); |
| 126 | #endif |
| 127 | } |
| 128 | |
| 129 | /// Read a ULEB128 into a 64-bit word. |
| 130 | inline uint64_t LocalAddressSpace::getULEB128(pint_t &addr, pint_t end) { |
| 131 | const uint8_t *p = (uint8_t *)addr; |
| 132 | const uint8_t *pend = (uint8_t *)end; |
| 133 | uint64_t result = 0; |
| 134 | int bit = 0; |
| 135 | do { |
| 136 | uint64_t b; |
| 137 | |
| 138 | if (p == pend) |
| 139 | _LIBUNWIND_ABORT("truncated uleb128 expression"); |
| 140 | |
| 141 | b = *p & 0x7f; |
| 142 | |
| 143 | if (bit >= 64 || b << bit >> bit != b) { |
| 144 | _LIBUNWIND_ABORT("malformed uleb128 expression"); |
| 145 | } else { |
| 146 | result |= b << bit; |
| 147 | bit += 7; |
| 148 | } |
| 149 | } while (*p++ >= 0x80); |
| 150 | addr = (pint_t) p; |
| 151 | return result; |
| 152 | } |
| 153 | |
| 154 | /// Read a SLEB128 into a 64-bit word. |
| 155 | inline int64_t LocalAddressSpace::getSLEB128(pint_t &addr, pint_t end) { |
| 156 | const uint8_t *p = (uint8_t *)addr; |
| 157 | const uint8_t *pend = (uint8_t *)end; |
| 158 | int64_t result = 0; |
| 159 | int bit = 0; |
| 160 | uint8_t byte; |
| 161 | do { |
| 162 | if (p == pend) |
| 163 | _LIBUNWIND_ABORT("truncated sleb128 expression"); |
| 164 | byte = *p++; |
| 165 | result |= ((byte & 0x7f) << bit); |
| 166 | bit += 7; |
| 167 | } while (byte & 0x80); |
| 168 | // sign extend negative numbers |
| 169 | if ((byte & 0x40) != 0) |
| 170 | result |= (-1LL) << bit; |
| 171 | addr = (pint_t) p; |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | inline LocalAddressSpace::pint_t |
| 176 | LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding, |
| 177 | pint_t datarelBase) { |
| 178 | pint_t startAddr = addr; |
| 179 | const uint8_t *p = (uint8_t *)addr; |
| 180 | pint_t result; |
| 181 | |
| 182 | // first get value |
| 183 | switch (encoding & 0x0F) { |
| 184 | case DW_EH_PE_ptr: |
| 185 | result = getP(addr); |
| 186 | p += sizeof(pint_t); |
| 187 | addr = (pint_t) p; |
| 188 | break; |
| 189 | case DW_EH_PE_uleb128: |
| 190 | result = (pint_t)getULEB128(addr, end); |
| 191 | break; |
| 192 | case DW_EH_PE_udata2: |
| 193 | result = get16(addr); |
| 194 | p += 2; |
| 195 | addr = (pint_t) p; |
| 196 | break; |
| 197 | case DW_EH_PE_udata4: |
| 198 | result = get32(addr); |
| 199 | p += 4; |
| 200 | addr = (pint_t) p; |
| 201 | break; |
| 202 | case DW_EH_PE_udata8: |
| 203 | result = (pint_t)get64(addr); |
| 204 | p += 8; |
| 205 | addr = (pint_t) p; |
| 206 | break; |
| 207 | case DW_EH_PE_sleb128: |
| 208 | result = (pint_t)getSLEB128(addr, end); |
| 209 | break; |
| 210 | case DW_EH_PE_sdata2: |
| 211 | // Sign extend from signed 16-bit value. |
| 212 | result = (pint_t)(int16_t)get16(addr); |
| 213 | p += 2; |
| 214 | addr = (pint_t) p; |
| 215 | break; |
| 216 | case DW_EH_PE_sdata4: |
| 217 | // Sign extend from signed 32-bit value. |
| 218 | result = (pint_t)(int32_t)get32(addr); |
| 219 | p += 4; |
| 220 | addr = (pint_t) p; |
| 221 | break; |
| 222 | case DW_EH_PE_sdata8: |
| 223 | result = (pint_t)get64(addr); |
| 224 | p += 8; |
| 225 | addr = (pint_t) p; |
| 226 | break; |
| 227 | default: |
| 228 | _LIBUNWIND_ABORT("unknown pointer encoding"); |
| 229 | } |
| 230 | |
| 231 | // then add relative offset |
| 232 | switch (encoding & 0x70) { |
| 233 | case DW_EH_PE_absptr: |
| 234 | // do nothing |
| 235 | break; |
| 236 | case DW_EH_PE_pcrel: |
| 237 | result += startAddr; |
| 238 | break; |
| 239 | case DW_EH_PE_textrel: |
| 240 | _LIBUNWIND_ABORT("DW_EH_PE_textrel pointer encoding not supported"); |
| 241 | break; |
| 242 | case DW_EH_PE_datarel: |
| 243 | // DW_EH_PE_datarel is only valid in a few places, so the parameter has a |
| 244 | // default value of 0, and we abort in the event that someone calls this |
| 245 | // function with a datarelBase of 0 and DW_EH_PE_datarel encoding. |
| 246 | if (datarelBase == 0) |
| 247 | _LIBUNWIND_ABORT("DW_EH_PE_datarel is invalid with a datarelBase of 0"); |
| 248 | result += datarelBase; |
| 249 | break; |
| 250 | case DW_EH_PE_funcrel: |
| 251 | _LIBUNWIND_ABORT("DW_EH_PE_funcrel pointer encoding not supported"); |
| 252 | break; |
| 253 | case DW_EH_PE_aligned: |
| 254 | _LIBUNWIND_ABORT("DW_EH_PE_aligned pointer encoding not supported"); |
| 255 | break; |
| 256 | default: |
| 257 | _LIBUNWIND_ABORT("unknown pointer encoding"); |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | if (encoding & DW_EH_PE_indirect) |
| 262 | result = getP(result); |
| 263 | |
| 264 | return result; |
| 265 | } |
| 266 | |
| 267 | #ifdef __APPLE__ |
Ed Schouten | 600e546 | 2017-03-07 18:15:52 +0000 | [diff] [blame^] | 268 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 269 | struct dyld_unwind_sections |
| 270 | { |
| 271 | const struct mach_header* mh; |
| 272 | const void* dwarf_section; |
| 273 | uintptr_t dwarf_section_length; |
| 274 | const void* compact_unwind_section; |
| 275 | uintptr_t compact_unwind_section_length; |
| 276 | }; |
| 277 | #if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) \ |
| 278 | && (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)) \ |
| 279 | || defined(__IPHONE_OS_VERSION_MIN_REQUIRED) |
| 280 | // In 10.7.0 or later, libSystem.dylib implements this function. |
| 281 | extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *); |
| 282 | #else |
Nick Kledzik | 9c56de1 | 2016-10-31 21:04:17 +0000 | [diff] [blame] | 283 | // In 10.6.x and earlier, we need to implement this functionality. Note |
| 284 | // that this requires a newer version of libmacho (from cctools) than is |
| 285 | // present in libSystem on 10.6.x (for getsectiondata). |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 286 | static inline bool _dyld_find_unwind_sections(void* addr, |
| 287 | dyld_unwind_sections* info) { |
| 288 | // Find mach-o image containing address. |
| 289 | Dl_info dlinfo; |
| 290 | if (!dladdr(addr, &dlinfo)) |
| 291 | return false; |
Nick Kledzik | 9c56de1 | 2016-10-31 21:04:17 +0000 | [diff] [blame] | 292 | #if __LP64__ |
| 293 | const struct mach_header_64 *mh = (const struct mach_header_64 *)dlinfo.dli_fbase; |
| 294 | #else |
| 295 | const struct mach_header *mh = (const struct mach_header *)dlinfo.dli_fbase; |
| 296 | #endif |
| 297 | |
| 298 | // Initialize the return struct |
| 299 | info->mh = (const struct mach_header *)mh; |
| 300 | info->dwarf_section = getsectiondata(mh, "__TEXT", "__eh_frame", &info->dwarf_section_length); |
| 301 | info->compact_unwind_section = getsectiondata(mh, "__TEXT", "__unwind_info", &info->compact_unwind_section_length); |
| 302 | |
| 303 | if (!info->dwarf_section) { |
| 304 | info->dwarf_section_length = 0; |
| 305 | } |
| 306 | |
| 307 | if (!info->compact_unwind_section) { |
| 308 | info->compact_unwind_section_length = 0; |
| 309 | } |
| 310 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 311 | return true; |
| 312 | } |
| 313 | #endif |
Ed Schouten | 600e546 | 2017-03-07 18:15:52 +0000 | [diff] [blame^] | 314 | |
| 315 | #elif _LIBUNWIND_ARM_EHABI && defined(_LIBUNWIND_IS_BAREMETAL) |
| 316 | |
| 317 | // When statically linked on bare-metal, the symbols for the EH table are looked |
| 318 | // up without going through the dynamic loader. |
| 319 | extern char __exidx_start; |
| 320 | extern char __exidx_end; |
| 321 | |
| 322 | #elif _LIBUNWIND_ARM_EHABI || _LIBUNWIND_SUPPORT_DWARF_UNWIND |
| 323 | |
| 324 | // ELF-based systems may use dl_iterate_phdr() to access sections |
| 325 | // containing unwinding information. The ElfW() macro for pointer-size |
| 326 | // independent ELF header traversal is not provided by <link.h> on some |
| 327 | // systems (e.g., FreeBSD). On these systems the data structures are |
| 328 | // just called Elf_XXX. Define ElfW() locally. |
| 329 | #include <link.h> |
| 330 | #if !defined(ElfW) |
| 331 | #define ElfW(type) Elf_##type |
| 332 | #endif |
| 333 | |
| 334 | #if _LIBUNWIND_SUPPORT_DWARF_UNWIND |
| 335 | #include "EHHeaderParser.hpp" |
| 336 | #endif |
| 337 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 338 | #endif |
| 339 | |
| 340 | inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr, |
| 341 | UnwindInfoSections &info) { |
| 342 | #ifdef __APPLE__ |
| 343 | dyld_unwind_sections dyldInfo; |
| 344 | if (_dyld_find_unwind_sections((void *)targetAddr, &dyldInfo)) { |
| 345 | info.dso_base = (uintptr_t)dyldInfo.mh; |
| 346 | #if _LIBUNWIND_SUPPORT_DWARF_UNWIND |
| 347 | info.dwarf_section = (uintptr_t)dyldInfo.dwarf_section; |
| 348 | info.dwarf_section_length = dyldInfo.dwarf_section_length; |
| 349 | #endif |
| 350 | info.compact_unwind_section = (uintptr_t)dyldInfo.compact_unwind_section; |
| 351 | info.compact_unwind_section_length = dyldInfo.compact_unwind_section_length; |
| 352 | return true; |
| 353 | } |
Ed Schouten | 6a4939b | 2017-03-05 19:11:24 +0000 | [diff] [blame] | 354 | #elif _LIBUNWIND_ARM_EHABI && defined(_LIBUNWIND_IS_BAREMETAL) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 355 | // Bare metal is statically linked, so no need to ask the dynamic loader |
| 356 | info.arm_section = (uintptr_t)(&__exidx_start); |
| 357 | info.arm_section_length = (uintptr_t)(&__exidx_end - &__exidx_start); |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 358 | _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %X length %x", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 359 | info.arm_section, info.arm_section_length); |
| 360 | if (info.arm_section && info.arm_section_length) |
| 361 | return true; |
Ed Schouten | 6a4939b | 2017-03-05 19:11:24 +0000 | [diff] [blame] | 362 | #elif _LIBUNWIND_ARM_EHABI || _LIBUNWIND_SUPPORT_DWARF_UNWIND |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 363 | struct dl_iterate_cb_data { |
| 364 | LocalAddressSpace *addressSpace; |
| 365 | UnwindInfoSections *sects; |
| 366 | uintptr_t targetAddr; |
| 367 | }; |
| 368 | |
| 369 | dl_iterate_cb_data cb_data = {this, &info, targetAddr}; |
| 370 | int found = dl_iterate_phdr( |
| 371 | [](struct dl_phdr_info *pinfo, size_t, void *data) -> int { |
| 372 | auto cbdata = static_cast<dl_iterate_cb_data *>(data); |
Ed Schouten | 08032ee | 2017-02-23 09:13:22 +0000 | [diff] [blame] | 373 | bool found_obj = false; |
| 374 | bool found_hdr = false; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 375 | |
| 376 | assert(cbdata); |
| 377 | assert(cbdata->sects); |
| 378 | |
| 379 | if (cbdata->targetAddr < pinfo->dlpi_addr) { |
| 380 | return false; |
| 381 | } |
| 382 | |
Viktor Kutuzov | a28f5b3 | 2015-05-06 10:32:28 +0000 | [diff] [blame] | 383 | #if !defined(Elf_Half) |
| 384 | typedef ElfW(Half) Elf_Half; |
| 385 | #endif |
| 386 | #if !defined(Elf_Phdr) |
| 387 | typedef ElfW(Phdr) Elf_Phdr; |
| 388 | #endif |
| 389 | |
Ed Schouten | 6a4939b | 2017-03-05 19:11:24 +0000 | [diff] [blame] | 390 | #if _LIBUNWIND_SUPPORT_DWARF_UNWIND |
| 391 | #if !_LIBUNWIND_SUPPORT_DWARF_INDEX |
| 392 | #error "_LIBUNWIND_SUPPORT_DWARF_UNWIND requires _LIBUNWIND_SUPPORT_DWARF_INDEX on this platform." |
| 393 | #endif |
| 394 | size_t object_length; |
Viktor Kutuzov | a28f5b3 | 2015-05-06 10:32:28 +0000 | [diff] [blame] | 395 | for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) { |
| 396 | const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i]; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 397 | if (phdr->p_type == PT_LOAD) { |
| 398 | uintptr_t begin = pinfo->dlpi_addr + phdr->p_vaddr; |
| 399 | uintptr_t end = begin + phdr->p_memsz; |
| 400 | if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) { |
| 401 | cbdata->sects->dso_base = begin; |
| 402 | object_length = phdr->p_memsz; |
| 403 | found_obj = true; |
| 404 | } |
| 405 | } else if (phdr->p_type == PT_GNU_EH_FRAME) { |
| 406 | EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo; |
| 407 | uintptr_t eh_frame_hdr_start = pinfo->dlpi_addr + phdr->p_vaddr; |
| 408 | cbdata->sects->dwarf_index_section = eh_frame_hdr_start; |
| 409 | cbdata->sects->dwarf_index_section_length = phdr->p_memsz; |
| 410 | EHHeaderParser<LocalAddressSpace>::decodeEHHdr( |
| 411 | *cbdata->addressSpace, eh_frame_hdr_start, phdr->p_memsz, |
| 412 | hdrInfo); |
| 413 | cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr; |
| 414 | found_hdr = true; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (found_obj && found_hdr) { |
| 419 | cbdata->sects->dwarf_section_length = object_length; |
| 420 | return true; |
| 421 | } else { |
| 422 | return false; |
| 423 | } |
Ed Schouten | 6a4939b | 2017-03-05 19:11:24 +0000 | [diff] [blame] | 424 | #else // _LIBUNWIND_ARM_EHABI |
| 425 | for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) { |
| 426 | const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i]; |
| 427 | if (phdr->p_type == PT_LOAD) { |
| 428 | uintptr_t begin = pinfo->dlpi_addr + phdr->p_vaddr; |
| 429 | uintptr_t end = begin + phdr->p_memsz; |
| 430 | if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) |
| 431 | found_obj = true; |
| 432 | } else if (phdr->p_type == PT_ARM_EXIDX) { |
| 433 | uintptr_t exidx_start = pinfo->dlpi_addr + phdr->p_vaddr; |
| 434 | cbdata->sects->arm_section = exidx_start; |
Ed Schouten | 5d3f35b | 2017-03-07 15:21:57 +0000 | [diff] [blame] | 435 | cbdata->sects->arm_section_length = phdr->p_memsz; |
Ed Schouten | 6a4939b | 2017-03-05 19:11:24 +0000 | [diff] [blame] | 436 | found_hdr = true; |
| 437 | } |
| 438 | } |
| 439 | return found_obj && found_hdr; |
| 440 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 441 | }, |
| 442 | &cb_data); |
| 443 | return static_cast<bool>(found); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 444 | #endif |
| 445 | |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | inline bool LocalAddressSpace::findOtherFDE(pint_t targetAddr, pint_t &fde) { |
| 451 | #ifdef __APPLE__ |
| 452 | return checkKeyMgrRegisteredFDEs(targetAddr, *((void**)&fde)); |
| 453 | #else |
| 454 | // TO DO: if OS has way to dynamically register FDEs, check that. |
| 455 | (void)targetAddr; |
| 456 | (void)fde; |
| 457 | return false; |
| 458 | #endif |
| 459 | } |
| 460 | |
| 461 | inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf, |
| 462 | size_t bufLen, |
| 463 | unw_word_t *offset) { |
| 464 | #ifndef _LIBUNWIND_IS_BAREMETAL |
| 465 | Dl_info dyldInfo; |
| 466 | if (dladdr((void *)addr, &dyldInfo)) { |
| 467 | if (dyldInfo.dli_sname != NULL) { |
| 468 | snprintf(buf, bufLen, "%s", dyldInfo.dli_sname); |
| 469 | *offset = (addr - (pint_t) dyldInfo.dli_saddr); |
| 470 | return true; |
| 471 | } |
| 472 | } |
| 473 | #endif |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | |
| 479 | #ifdef UNW_REMOTE |
| 480 | |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 481 | /// RemoteAddressSpace is used as a template parameter to UnwindCursor when |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 482 | /// unwinding a thread in the another process. The other process can be a |
| 483 | /// different endianness and a different pointer size which is handled by |
| 484 | /// the P template parameter. |
| 485 | template <typename P> |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 486 | class RemoteAddressSpace { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 487 | public: |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 488 | RemoteAddressSpace(task_t task) : fTask(task) {} |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 489 | |
| 490 | typedef typename P::uint_t pint_t; |
| 491 | |
| 492 | uint8_t get8(pint_t addr); |
| 493 | uint16_t get16(pint_t addr); |
| 494 | uint32_t get32(pint_t addr); |
| 495 | uint64_t get64(pint_t addr); |
| 496 | pint_t getP(pint_t addr); |
| 497 | uint64_t getULEB128(pint_t &addr, pint_t end); |
| 498 | int64_t getSLEB128(pint_t &addr, pint_t end); |
| 499 | pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding, |
| 500 | pint_t datarelBase = 0); |
| 501 | bool findFunctionName(pint_t addr, char *buf, size_t bufLen, |
| 502 | unw_word_t *offset); |
| 503 | bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info); |
| 504 | bool findOtherFDE(pint_t targetAddr, pint_t &fde); |
| 505 | private: |
| 506 | void *localCopy(pint_t addr); |
| 507 | |
| 508 | task_t fTask; |
| 509 | }; |
| 510 | |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 511 | template <typename P> uint8_t RemoteAddressSpace<P>::get8(pint_t addr) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 512 | return *((uint8_t *)localCopy(addr)); |
| 513 | } |
| 514 | |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 515 | template <typename P> uint16_t RemoteAddressSpace<P>::get16(pint_t addr) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 516 | return P::E::get16(*(uint16_t *)localCopy(addr)); |
| 517 | } |
| 518 | |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 519 | template <typename P> uint32_t RemoteAddressSpace<P>::get32(pint_t addr) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 520 | return P::E::get32(*(uint32_t *)localCopy(addr)); |
| 521 | } |
| 522 | |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 523 | template <typename P> uint64_t RemoteAddressSpace<P>::get64(pint_t addr) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 524 | return P::E::get64(*(uint64_t *)localCopy(addr)); |
| 525 | } |
| 526 | |
| 527 | template <typename P> |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 528 | typename P::uint_t RemoteAddressSpace<P>::getP(pint_t addr) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 529 | return P::getP(*(uint64_t *)localCopy(addr)); |
| 530 | } |
| 531 | |
| 532 | template <typename P> |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 533 | uint64_t RemoteAddressSpace<P>::getULEB128(pint_t &addr, pint_t end) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 534 | uintptr_t size = (end - addr); |
| 535 | LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr); |
| 536 | LocalAddressSpace::pint_t sladdr = laddr; |
| 537 | uint64_t result = LocalAddressSpace::getULEB128(laddr, laddr + size); |
| 538 | addr += (laddr - sladdr); |
| 539 | return result; |
| 540 | } |
| 541 | |
| 542 | template <typename P> |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 543 | int64_t RemoteAddressSpace<P>::getSLEB128(pint_t &addr, pint_t end) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 544 | uintptr_t size = (end - addr); |
| 545 | LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr); |
| 546 | LocalAddressSpace::pint_t sladdr = laddr; |
| 547 | uint64_t result = LocalAddressSpace::getSLEB128(laddr, laddr + size); |
| 548 | addr += (laddr - sladdr); |
| 549 | return result; |
| 550 | } |
| 551 | |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 552 | template <typename P> void *RemoteAddressSpace<P>::localCopy(pint_t addr) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 553 | // FIX ME |
| 554 | } |
| 555 | |
| 556 | template <typename P> |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 557 | bool RemoteAddressSpace<P>::findFunctionName(pint_t addr, char *buf, |
| 558 | size_t bufLen, |
| 559 | unw_word_t *offset) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 560 | // FIX ME |
| 561 | } |
| 562 | |
| 563 | /// unw_addr_space is the base class that abstract unw_addr_space_t type in |
| 564 | /// libunwind.h points to. |
| 565 | struct unw_addr_space { |
| 566 | cpu_type_t cpuType; |
| 567 | task_t taskPort; |
| 568 | }; |
| 569 | |
| 570 | /// unw_addr_space_i386 is the concrete instance that a unw_addr_space_t points |
| 571 | /// to when examining |
| 572 | /// a 32-bit intel process. |
| 573 | struct unw_addr_space_i386 : public unw_addr_space { |
| 574 | unw_addr_space_i386(task_t task) : oas(task) {} |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 575 | RemoteAddressSpace<Pointer32<LittleEndian>> oas; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | /// unw_addr_space_x86_64 is the concrete instance that a unw_addr_space_t |
| 579 | /// points to when examining |
| 580 | /// a 64-bit intel process. |
| 581 | struct unw_addr_space_x86_64 : public unw_addr_space { |
| 582 | unw_addr_space_x86_64(task_t task) : oas(task) {} |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 583 | RemoteAddressSpace<Pointer64<LittleEndian>> oas; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 584 | }; |
| 585 | |
| 586 | /// unw_addr_space_ppc is the concrete instance that a unw_addr_space_t points |
| 587 | /// to when examining |
| 588 | /// a 32-bit PowerPC process. |
| 589 | struct unw_addr_space_ppc : public unw_addr_space { |
| 590 | unw_addr_space_ppc(task_t task) : oas(task) {} |
Saleem Abdulrasool | 66efa8e | 2017-01-21 16:22:46 +0000 | [diff] [blame] | 591 | RemoteAddressSpace<Pointer32<BigEndian>> oas; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
| 594 | #endif // UNW_REMOTE |
| 595 | |
| 596 | } // namespace libunwind |
| 597 | |
| 598 | #endif // __ADDRESSSPACE_HPP__ |