blob: f7cb767d90cce856fb44701e4bf598a0988f4c53 [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001//===------------------------- 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
Martin Storsjo4c1f84d2017-10-11 20:06:18 +000021#if !defined(_LIBUNWIND_IS_BAREMETAL) && !defined(_WIN32)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000022#include <dlfcn.h>
23#endif
24
25#ifdef __APPLE__
26#include <mach-o/getsect.h>
27namespace 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"
Ed Schoutenda7d6252017-03-07 18:21:51 +000035#include "EHHeaderParser.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000036#include "Registers.hpp"
37
John Baldwin67737ec2017-09-21 21:28:48 +000038#ifdef __APPLE__
39
40 struct dyld_unwind_sections
41 {
42 const struct mach_header* mh;
43 const void* dwarf_section;
44 uintptr_t dwarf_section_length;
45 const void* compact_unwind_section;
46 uintptr_t compact_unwind_section_length;
47 };
48 #if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) \
49 && (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)) \
50 || defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
51 // In 10.7.0 or later, libSystem.dylib implements this function.
52 extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *);
53 #else
54 // In 10.6.x and earlier, we need to implement this functionality. Note
55 // that this requires a newer version of libmacho (from cctools) than is
56 // present in libSystem on 10.6.x (for getsectiondata).
57 static inline bool _dyld_find_unwind_sections(void* addr,
58 dyld_unwind_sections* info) {
59 // Find mach-o image containing address.
60 Dl_info dlinfo;
61 if (!dladdr(addr, &dlinfo))
62 return false;
63#if __LP64__
64 const struct mach_header_64 *mh = (const struct mach_header_64 *)dlinfo.dli_fbase;
65#else
66 const struct mach_header *mh = (const struct mach_header *)dlinfo.dli_fbase;
67#endif
68
69 // Initialize the return struct
70 info->mh = (const struct mach_header *)mh;
71 info->dwarf_section = getsectiondata(mh, "__TEXT", "__eh_frame", &info->dwarf_section_length);
72 info->compact_unwind_section = getsectiondata(mh, "__TEXT", "__unwind_info", &info->compact_unwind_section_length);
73
74 if (!info->dwarf_section) {
75 info->dwarf_section_length = 0;
76 }
77
78 if (!info->compact_unwind_section) {
79 info->compact_unwind_section_length = 0;
80 }
81
82 return true;
83 }
84 #endif
85
86#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)
87
88// When statically linked on bare-metal, the symbols for the EH table are looked
89// up without going through the dynamic loader.
90extern char __exidx_start;
91extern char __exidx_end;
92
93#elif defined(_LIBUNWIND_ARM_EHABI) || defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
94
95// ELF-based systems may use dl_iterate_phdr() to access sections
96// containing unwinding information. The ElfW() macro for pointer-size
97// independent ELF header traversal is not provided by <link.h> on some
98// systems (e.g., FreeBSD). On these systems the data structures are
99// just called Elf_XXX. Define ElfW() locally.
Martin Storsjo4c1f84d2017-10-11 20:06:18 +0000100#ifndef _WIN32
John Baldwin67737ec2017-09-21 21:28:48 +0000101#include <link.h>
Martin Storsjo4c1f84d2017-10-11 20:06:18 +0000102#else
103#include <windows.h>
104#include <psapi.h>
105#endif
John Baldwin67737ec2017-09-21 21:28:48 +0000106#if !defined(ElfW)
107#define ElfW(type) Elf_##type
108#endif
109
110#endif
111
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000112namespace libunwind {
113
114/// Used by findUnwindSections() to return info about needed sections.
115struct UnwindInfoSections {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000116#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) || defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) || \
117 defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000118 // No dso_base for ARM EHABI.
119 uintptr_t dso_base;
120#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +0000121#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000122 uintptr_t dwarf_section;
123 uintptr_t dwarf_section_length;
124#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +0000125#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000126 uintptr_t dwarf_index_section;
127 uintptr_t dwarf_index_section_length;
128#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +0000129#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000130 uintptr_t compact_unwind_section;
131 uintptr_t compact_unwind_section_length;
132#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +0000133#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000134 uintptr_t arm_section;
135 uintptr_t arm_section_length;
136#endif
137};
138
139
140/// LocalAddressSpace is used as a template parameter to UnwindCursor when
141/// unwinding a thread in the same process. The wrappers compile away,
142/// making local unwinds fast.
143class __attribute__((visibility("hidden"))) LocalAddressSpace {
144public:
Martin Storsjocac41382017-10-27 08:11:36 +0000145 typedef uintptr_t pint_t;
146 typedef intptr_t sint_t;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000147 uint8_t get8(pint_t addr) {
148 uint8_t val;
149 memcpy(&val, (void *)addr, sizeof(val));
150 return val;
151 }
152 uint16_t get16(pint_t addr) {
153 uint16_t val;
154 memcpy(&val, (void *)addr, sizeof(val));
155 return val;
156 }
157 uint32_t get32(pint_t addr) {
158 uint32_t val;
159 memcpy(&val, (void *)addr, sizeof(val));
160 return val;
161 }
162 uint64_t get64(pint_t addr) {
163 uint64_t val;
164 memcpy(&val, (void *)addr, sizeof(val));
165 return val;
166 }
167 double getDouble(pint_t addr) {
168 double val;
169 memcpy(&val, (void *)addr, sizeof(val));
170 return val;
171 }
172 v128 getVector(pint_t addr) {
173 v128 val;
174 memcpy(&val, (void *)addr, sizeof(val));
175 return val;
176 }
177 uintptr_t getP(pint_t addr);
178 static uint64_t getULEB128(pint_t &addr, pint_t end);
179 static int64_t getSLEB128(pint_t &addr, pint_t end);
180
181 pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
182 pint_t datarelBase = 0);
183 bool findFunctionName(pint_t addr, char *buf, size_t bufLen,
184 unw_word_t *offset);
185 bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
186 bool findOtherFDE(pint_t targetAddr, pint_t &fde);
187
188 static LocalAddressSpace sThisAddressSpace;
189};
190
191inline uintptr_t LocalAddressSpace::getP(pint_t addr) {
Martin Storsjocac41382017-10-27 08:11:36 +0000192#if __SIZEOF_POINTER__ == 8
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000193 return get64(addr);
194#else
195 return get32(addr);
196#endif
197}
198
199/// Read a ULEB128 into a 64-bit word.
200inline uint64_t LocalAddressSpace::getULEB128(pint_t &addr, pint_t end) {
201 const uint8_t *p = (uint8_t *)addr;
202 const uint8_t *pend = (uint8_t *)end;
203 uint64_t result = 0;
204 int bit = 0;
205 do {
206 uint64_t b;
207
208 if (p == pend)
209 _LIBUNWIND_ABORT("truncated uleb128 expression");
210
211 b = *p & 0x7f;
212
213 if (bit >= 64 || b << bit >> bit != b) {
214 _LIBUNWIND_ABORT("malformed uleb128 expression");
215 } else {
216 result |= b << bit;
217 bit += 7;
218 }
219 } while (*p++ >= 0x80);
220 addr = (pint_t) p;
221 return result;
222}
223
224/// Read a SLEB128 into a 64-bit word.
225inline int64_t LocalAddressSpace::getSLEB128(pint_t &addr, pint_t end) {
226 const uint8_t *p = (uint8_t *)addr;
227 const uint8_t *pend = (uint8_t *)end;
228 int64_t result = 0;
229 int bit = 0;
230 uint8_t byte;
231 do {
232 if (p == pend)
233 _LIBUNWIND_ABORT("truncated sleb128 expression");
234 byte = *p++;
235 result |= ((byte & 0x7f) << bit);
236 bit += 7;
237 } while (byte & 0x80);
238 // sign extend negative numbers
239 if ((byte & 0x40) != 0)
Marshall Clowa6d7c382017-06-21 16:02:53 +0000240 result |= (-1ULL) << bit;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000241 addr = (pint_t) p;
242 return result;
243}
244
245inline LocalAddressSpace::pint_t
246LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
247 pint_t datarelBase) {
248 pint_t startAddr = addr;
249 const uint8_t *p = (uint8_t *)addr;
250 pint_t result;
251
252 // first get value
253 switch (encoding & 0x0F) {
254 case DW_EH_PE_ptr:
255 result = getP(addr);
256 p += sizeof(pint_t);
257 addr = (pint_t) p;
258 break;
259 case DW_EH_PE_uleb128:
260 result = (pint_t)getULEB128(addr, end);
261 break;
262 case DW_EH_PE_udata2:
263 result = get16(addr);
264 p += 2;
265 addr = (pint_t) p;
266 break;
267 case DW_EH_PE_udata4:
268 result = get32(addr);
269 p += 4;
270 addr = (pint_t) p;
271 break;
272 case DW_EH_PE_udata8:
273 result = (pint_t)get64(addr);
274 p += 8;
275 addr = (pint_t) p;
276 break;
277 case DW_EH_PE_sleb128:
278 result = (pint_t)getSLEB128(addr, end);
279 break;
280 case DW_EH_PE_sdata2:
281 // Sign extend from signed 16-bit value.
282 result = (pint_t)(int16_t)get16(addr);
283 p += 2;
284 addr = (pint_t) p;
285 break;
286 case DW_EH_PE_sdata4:
287 // Sign extend from signed 32-bit value.
288 result = (pint_t)(int32_t)get32(addr);
289 p += 4;
290 addr = (pint_t) p;
291 break;
292 case DW_EH_PE_sdata8:
293 result = (pint_t)get64(addr);
294 p += 8;
295 addr = (pint_t) p;
296 break;
297 default:
298 _LIBUNWIND_ABORT("unknown pointer encoding");
299 }
300
301 // then add relative offset
302 switch (encoding & 0x70) {
303 case DW_EH_PE_absptr:
304 // do nothing
305 break;
306 case DW_EH_PE_pcrel:
307 result += startAddr;
308 break;
309 case DW_EH_PE_textrel:
310 _LIBUNWIND_ABORT("DW_EH_PE_textrel pointer encoding not supported");
311 break;
312 case DW_EH_PE_datarel:
313 // DW_EH_PE_datarel is only valid in a few places, so the parameter has a
314 // default value of 0, and we abort in the event that someone calls this
315 // function with a datarelBase of 0 and DW_EH_PE_datarel encoding.
316 if (datarelBase == 0)
317 _LIBUNWIND_ABORT("DW_EH_PE_datarel is invalid with a datarelBase of 0");
318 result += datarelBase;
319 break;
320 case DW_EH_PE_funcrel:
321 _LIBUNWIND_ABORT("DW_EH_PE_funcrel pointer encoding not supported");
322 break;
323 case DW_EH_PE_aligned:
324 _LIBUNWIND_ABORT("DW_EH_PE_aligned pointer encoding not supported");
325 break;
326 default:
327 _LIBUNWIND_ABORT("unknown pointer encoding");
328 break;
329 }
330
331 if (encoding & DW_EH_PE_indirect)
332 result = getP(result);
333
334 return result;
335}
336
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000337inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
338 UnwindInfoSections &info) {
339#ifdef __APPLE__
340 dyld_unwind_sections dyldInfo;
341 if (_dyld_find_unwind_sections((void *)targetAddr, &dyldInfo)) {
342 info.dso_base = (uintptr_t)dyldInfo.mh;
Ranjeet Singh421231a2017-03-31 15:28:06 +0000343 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000344 info.dwarf_section = (uintptr_t)dyldInfo.dwarf_section;
345 info.dwarf_section_length = dyldInfo.dwarf_section_length;
346 #endif
347 info.compact_unwind_section = (uintptr_t)dyldInfo.compact_unwind_section;
348 info.compact_unwind_section_length = dyldInfo.compact_unwind_section_length;
349 return true;
350 }
Ranjeet Singh421231a2017-03-31 15:28:06 +0000351#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000352 // Bare metal is statically linked, so no need to ask the dynamic loader
353 info.arm_section = (uintptr_t)(&__exidx_start);
354 info.arm_section_length = (uintptr_t)(&__exidx_end - &__exidx_start);
Ed Maste41bc5a72016-08-30 15:38:10 +0000355 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %X length %x",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000356 info.arm_section, info.arm_section_length);
357 if (info.arm_section && info.arm_section_length)
358 return true;
Martin Storsjo4c1f84d2017-10-11 20:06:18 +0000359#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_WIN32)
360 HMODULE mods[1024];
361 HANDLE process = GetCurrentProcess();
362 DWORD needed;
363
364 if (!EnumProcessModules(process, mods, sizeof(mods), &needed))
365 return false;
366
367 for (unsigned i = 0; i < (needed / sizeof(HMODULE)); i++) {
368 PIMAGE_DOS_HEADER pidh = (PIMAGE_DOS_HEADER)mods[i];
369 PIMAGE_NT_HEADERS pinh = (PIMAGE_NT_HEADERS)((BYTE *)pidh + pidh->e_lfanew);
370 PIMAGE_FILE_HEADER pifh = (PIMAGE_FILE_HEADER)&pinh->FileHeader;
371 PIMAGE_SECTION_HEADER pish = IMAGE_FIRST_SECTION(pinh);
372 bool found_obj = false;
373 bool found_hdr = false;
374
375 info.dso_base = (uintptr_t)mods[i];
376 for (unsigned j = 0; j < pifh->NumberOfSections; j++, pish++) {
377 uintptr_t begin = pish->VirtualAddress + (uintptr_t)mods[i];
378 uintptr_t end = begin + pish->Misc.VirtualSize;
379 if (!strncmp((const char *)pish->Name, ".text",
380 IMAGE_SIZEOF_SHORT_NAME)) {
381 if (targetAddr >= begin && targetAddr < end)
382 found_obj = true;
383 } else if (!strncmp((const char *)pish->Name, ".eh_frame",
384 IMAGE_SIZEOF_SHORT_NAME)) {
385 // FIXME: This section name actually is truncated, ideally we
386 // should locate and check the full long name instead.
387 info.dwarf_section = begin;
388 info.dwarf_section_length = pish->Misc.VirtualSize;
389 found_hdr = true;
390 }
391 if (found_obj && found_hdr)
392 return true;
393 }
394 }
395 return false;
Ranjeet Singh421231a2017-03-31 15:28:06 +0000396#elif defined(_LIBUNWIND_ARM_EHABI) || defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000397 struct dl_iterate_cb_data {
398 LocalAddressSpace *addressSpace;
399 UnwindInfoSections *sects;
400 uintptr_t targetAddr;
401 };
402
403 dl_iterate_cb_data cb_data = {this, &info, targetAddr};
404 int found = dl_iterate_phdr(
405 [](struct dl_phdr_info *pinfo, size_t, void *data) -> int {
406 auto cbdata = static_cast<dl_iterate_cb_data *>(data);
Ed Schouten08032ee2017-02-23 09:13:22 +0000407 bool found_obj = false;
408 bool found_hdr = false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000409
410 assert(cbdata);
411 assert(cbdata->sects);
412
413 if (cbdata->targetAddr < pinfo->dlpi_addr) {
414 return false;
415 }
416
Viktor Kutuzova28f5b32015-05-06 10:32:28 +0000417#if !defined(Elf_Half)
418 typedef ElfW(Half) Elf_Half;
419#endif
420#if !defined(Elf_Phdr)
421 typedef ElfW(Phdr) Elf_Phdr;
422#endif
Ivan Krasind34d77f2017-04-06 17:35:35 +0000423#if !defined(Elf_Addr) && defined(__ANDROID__)
Saleem Abdulrasool241fe722017-04-05 18:33:23 +0000424 typedef ElfW(Addr) Elf_Addr;
425#endif
Viktor Kutuzova28f5b32015-05-06 10:32:28 +0000426
Ranjeet Singh421231a2017-03-31 15:28:06 +0000427 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
428 #if !defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Ed Schouten6a4939b2017-03-05 19:11:24 +0000429 #error "_LIBUNWIND_SUPPORT_DWARF_UNWIND requires _LIBUNWIND_SUPPORT_DWARF_INDEX on this platform."
430 #endif
431 size_t object_length;
Saleem Abdulrasool241fe722017-04-05 18:33:23 +0000432#if defined(__ANDROID__)
433 Elf_Addr image_base =
Saleem Abdulrasoole4fe5f92017-04-05 21:29:38 +0000434 pinfo->dlpi_phnum
435 ? reinterpret_cast<Elf_Addr>(pinfo->dlpi_phdr) -
436 reinterpret_cast<const Elf_Phdr *>(pinfo->dlpi_phdr)
437 ->p_offset
438 : 0;
Saleem Abdulrasool241fe722017-04-05 18:33:23 +0000439#endif
440
Viktor Kutuzova28f5b32015-05-06 10:32:28 +0000441 for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) {
442 const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i];
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000443 if (phdr->p_type == PT_LOAD) {
444 uintptr_t begin = pinfo->dlpi_addr + phdr->p_vaddr;
Saleem Abdulrasool241fe722017-04-05 18:33:23 +0000445#if defined(__ANDROID__)
446 if (pinfo->dlpi_addr == 0 && phdr->p_vaddr < image_base)
447 begin = begin + image_base;
448#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000449 uintptr_t end = begin + phdr->p_memsz;
450 if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) {
451 cbdata->sects->dso_base = begin;
452 object_length = phdr->p_memsz;
453 found_obj = true;
454 }
455 } else if (phdr->p_type == PT_GNU_EH_FRAME) {
456 EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo;
457 uintptr_t eh_frame_hdr_start = pinfo->dlpi_addr + phdr->p_vaddr;
Saleem Abdulrasool241fe722017-04-05 18:33:23 +0000458#if defined(__ANDROID__)
459 if (pinfo->dlpi_addr == 0 && phdr->p_vaddr < image_base)
460 eh_frame_hdr_start = eh_frame_hdr_start + image_base;
461#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000462 cbdata->sects->dwarf_index_section = eh_frame_hdr_start;
463 cbdata->sects->dwarf_index_section_length = phdr->p_memsz;
464 EHHeaderParser<LocalAddressSpace>::decodeEHHdr(
465 *cbdata->addressSpace, eh_frame_hdr_start, phdr->p_memsz,
466 hdrInfo);
467 cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr;
468 found_hdr = true;
469 }
470 }
471
472 if (found_obj && found_hdr) {
473 cbdata->sects->dwarf_section_length = object_length;
474 return true;
475 } else {
476 return false;
477 }
Ranjeet Singh421231a2017-03-31 15:28:06 +0000478 #else // defined(_LIBUNWIND_ARM_EHABI)
Ed Schouten6a4939b2017-03-05 19:11:24 +0000479 for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) {
480 const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i];
481 if (phdr->p_type == PT_LOAD) {
482 uintptr_t begin = pinfo->dlpi_addr + phdr->p_vaddr;
483 uintptr_t end = begin + phdr->p_memsz;
484 if (cbdata->targetAddr >= begin && cbdata->targetAddr < end)
485 found_obj = true;
486 } else if (phdr->p_type == PT_ARM_EXIDX) {
487 uintptr_t exidx_start = pinfo->dlpi_addr + phdr->p_vaddr;
488 cbdata->sects->arm_section = exidx_start;
Ed Schouten5d3f35b2017-03-07 15:21:57 +0000489 cbdata->sects->arm_section_length = phdr->p_memsz;
Ed Schouten6a4939b2017-03-05 19:11:24 +0000490 found_hdr = true;
491 }
492 }
493 return found_obj && found_hdr;
494 #endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000495 },
496 &cb_data);
497 return static_cast<bool>(found);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000498#endif
499
500 return false;
501}
502
503
504inline bool LocalAddressSpace::findOtherFDE(pint_t targetAddr, pint_t &fde) {
505#ifdef __APPLE__
506 return checkKeyMgrRegisteredFDEs(targetAddr, *((void**)&fde));
507#else
508 // TO DO: if OS has way to dynamically register FDEs, check that.
509 (void)targetAddr;
510 (void)fde;
511 return false;
512#endif
513}
514
515inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf,
516 size_t bufLen,
517 unw_word_t *offset) {
Martin Storsjo4c1f84d2017-10-11 20:06:18 +0000518#if !defined(_LIBUNWIND_IS_BAREMETAL) && !defined(_WIN32)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000519 Dl_info dyldInfo;
520 if (dladdr((void *)addr, &dyldInfo)) {
521 if (dyldInfo.dli_sname != NULL) {
522 snprintf(buf, bufLen, "%s", dyldInfo.dli_sname);
523 *offset = (addr - (pint_t) dyldInfo.dli_saddr);
524 return true;
525 }
526 }
527#endif
528 return false;
529}
530
531
532
533#ifdef UNW_REMOTE
534
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000535/// RemoteAddressSpace is used as a template parameter to UnwindCursor when
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000536/// unwinding a thread in the another process. The other process can be a
537/// different endianness and a different pointer size which is handled by
538/// the P template parameter.
539template <typename P>
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000540class RemoteAddressSpace {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000541public:
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000542 RemoteAddressSpace(task_t task) : fTask(task) {}
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000543
544 typedef typename P::uint_t pint_t;
545
546 uint8_t get8(pint_t addr);
547 uint16_t get16(pint_t addr);
548 uint32_t get32(pint_t addr);
549 uint64_t get64(pint_t addr);
550 pint_t getP(pint_t addr);
551 uint64_t getULEB128(pint_t &addr, pint_t end);
552 int64_t getSLEB128(pint_t &addr, pint_t end);
553 pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
554 pint_t datarelBase = 0);
555 bool findFunctionName(pint_t addr, char *buf, size_t bufLen,
556 unw_word_t *offset);
557 bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
558 bool findOtherFDE(pint_t targetAddr, pint_t &fde);
559private:
560 void *localCopy(pint_t addr);
561
562 task_t fTask;
563};
564
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000565template <typename P> uint8_t RemoteAddressSpace<P>::get8(pint_t addr) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000566 return *((uint8_t *)localCopy(addr));
567}
568
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000569template <typename P> uint16_t RemoteAddressSpace<P>::get16(pint_t addr) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000570 return P::E::get16(*(uint16_t *)localCopy(addr));
571}
572
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000573template <typename P> uint32_t RemoteAddressSpace<P>::get32(pint_t addr) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000574 return P::E::get32(*(uint32_t *)localCopy(addr));
575}
576
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000577template <typename P> uint64_t RemoteAddressSpace<P>::get64(pint_t addr) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000578 return P::E::get64(*(uint64_t *)localCopy(addr));
579}
580
581template <typename P>
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000582typename P::uint_t RemoteAddressSpace<P>::getP(pint_t addr) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000583 return P::getP(*(uint64_t *)localCopy(addr));
584}
585
586template <typename P>
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000587uint64_t RemoteAddressSpace<P>::getULEB128(pint_t &addr, pint_t end) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000588 uintptr_t size = (end - addr);
589 LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr);
590 LocalAddressSpace::pint_t sladdr = laddr;
591 uint64_t result = LocalAddressSpace::getULEB128(laddr, laddr + size);
592 addr += (laddr - sladdr);
593 return result;
594}
595
596template <typename P>
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000597int64_t RemoteAddressSpace<P>::getSLEB128(pint_t &addr, pint_t end) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000598 uintptr_t size = (end - addr);
599 LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr);
600 LocalAddressSpace::pint_t sladdr = laddr;
601 uint64_t result = LocalAddressSpace::getSLEB128(laddr, laddr + size);
602 addr += (laddr - sladdr);
603 return result;
604}
605
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000606template <typename P> void *RemoteAddressSpace<P>::localCopy(pint_t addr) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000607 // FIX ME
608}
609
610template <typename P>
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000611bool RemoteAddressSpace<P>::findFunctionName(pint_t addr, char *buf,
612 size_t bufLen,
613 unw_word_t *offset) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000614 // FIX ME
615}
616
617/// unw_addr_space is the base class that abstract unw_addr_space_t type in
618/// libunwind.h points to.
619struct unw_addr_space {
620 cpu_type_t cpuType;
621 task_t taskPort;
622};
623
624/// unw_addr_space_i386 is the concrete instance that a unw_addr_space_t points
625/// to when examining
626/// a 32-bit intel process.
627struct unw_addr_space_i386 : public unw_addr_space {
628 unw_addr_space_i386(task_t task) : oas(task) {}
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000629 RemoteAddressSpace<Pointer32<LittleEndian>> oas;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000630};
631
632/// unw_addr_space_x86_64 is the concrete instance that a unw_addr_space_t
633/// points to when examining
634/// a 64-bit intel process.
635struct unw_addr_space_x86_64 : public unw_addr_space {
636 unw_addr_space_x86_64(task_t task) : oas(task) {}
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000637 RemoteAddressSpace<Pointer64<LittleEndian>> oas;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000638};
639
640/// unw_addr_space_ppc is the concrete instance that a unw_addr_space_t points
641/// to when examining
642/// a 32-bit PowerPC process.
643struct unw_addr_space_ppc : public unw_addr_space {
644 unw_addr_space_ppc(task_t task) : oas(task) {}
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000645 RemoteAddressSpace<Pointer32<BigEndian>> oas;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000646};
647
648#endif // UNW_REMOTE
649
650} // namespace libunwind
651
652#endif // __ADDRESSSPACE_HPP__