blob: 717aa336fd13c724b8e54f2d8328d41730a752b4 [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001//===------------------------- AddressSpace.hpp ---------------------------===//
2//
Chandler Carruth61860a52019-01-19 10:56:40 +00003// 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
Saleem Abdulrasool17552662015-04-24 19:39:17 +00006//
7//
8// Abstracts accessing local vs remote address spaces.
9//
10//===----------------------------------------------------------------------===//
11
12#ifndef __ADDRESSSPACE_HPP__
13#define __ADDRESSSPACE_HPP__
14
15#include <stdint.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
Jordan Rupprecht8ac87db2018-06-29 20:41:50 +000020#ifndef _LIBUNWIND_USE_DLADDR
21 #if !defined(_LIBUNWIND_IS_BAREMETAL) && !defined(_WIN32)
22 #define _LIBUNWIND_USE_DLADDR 1
23 #else
24 #define _LIBUNWIND_USE_DLADDR 0
25 #endif
26#endif
27
28#if _LIBUNWIND_USE_DLADDR
Saleem Abdulrasool17552662015-04-24 19:39:17 +000029#include <dlfcn.h>
Michał Górnyf939ab72019-11-30 15:13:56 +010030#if defined(__ELF__) && defined(_LIBUNWIND_LINK_DL_LIB)
Petr Hosek7b727032019-05-30 01:34:41 +000031#pragma comment(lib, "dl")
32#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +000033#endif
34
Ryan Prichard2a4f1362019-10-18 19:59:22 +000035#if defined(_LIBUNWIND_ARM_EHABI)
36struct EHABIIndexEntry {
37 uint32_t functionOffset;
38 uint32_t data;
39};
40#endif
41
Saleem Abdulrasool17552662015-04-24 19:39:17 +000042#include "libunwind.h"
43#include "config.h"
44#include "dwarf2.h"
Ed Schoutenda7d6252017-03-07 18:21:51 +000045#include "EHHeaderParser.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000046#include "Registers.hpp"
47
John Baldwin67737ec2017-09-21 21:28:48 +000048#ifdef __APPLE__
49
50 struct dyld_unwind_sections
51 {
52 const struct mach_header* mh;
53 const void* dwarf_section;
54 uintptr_t dwarf_section_length;
55 const void* compact_unwind_section;
56 uintptr_t compact_unwind_section_length;
57 };
John Baldwin67737ec2017-09-21 21:28:48 +000058
Steven Wu8d1344f2020-08-17 14:08:50 -070059 // In 10.7.0 or later, libSystem.dylib implements this function.
60 extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *);
John Baldwin67737ec2017-09-21 21:28:48 +000061
whitequarkffd92e52017-12-25 17:05:07 +000062#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL)
63
64// When statically linked on bare-metal, the symbols for the EH table are looked
65// up without going through the dynamic loader.
66
67// The following linker script may be used to produce the necessary sections and symbols.
68// Unless the --eh-frame-hdr linker option is provided, the section is not generated
69// and does not take space in the output file.
70//
71// .eh_frame :
72// {
73// __eh_frame_start = .;
74// KEEP(*(.eh_frame))
75// __eh_frame_end = .;
76// }
77//
78// .eh_frame_hdr :
79// {
80// KEEP(*(.eh_frame_hdr))
81// }
82//
83// __eh_frame_hdr_start = SIZEOF(.eh_frame_hdr) > 0 ? ADDR(.eh_frame_hdr) : 0;
84// __eh_frame_hdr_end = SIZEOF(.eh_frame_hdr) > 0 ? . : 0;
85
86extern char __eh_frame_start;
87extern char __eh_frame_end;
88
89#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
90extern char __eh_frame_hdr_start;
91extern char __eh_frame_hdr_end;
92#endif
93
John Baldwin67737ec2017-09-21 21:28:48 +000094#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)
95
96// When statically linked on bare-metal, the symbols for the EH table are looked
97// up without going through the dynamic loader.
98extern char __exidx_start;
99extern char __exidx_end;
100
101#elif defined(_LIBUNWIND_ARM_EHABI) || defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
102
103// ELF-based systems may use dl_iterate_phdr() to access sections
104// containing unwinding information. The ElfW() macro for pointer-size
105// independent ELF header traversal is not provided by <link.h> on some
106// systems (e.g., FreeBSD). On these systems the data structures are
107// just called Elf_XXX. Define ElfW() locally.
Martin Storsjo4c1f84d2017-10-11 20:06:18 +0000108#ifndef _WIN32
John Baldwin67737ec2017-09-21 21:28:48 +0000109#include <link.h>
Martin Storsjo4c1f84d2017-10-11 20:06:18 +0000110#else
111#include <windows.h>
112#include <psapi.h>
113#endif
John Baldwin67737ec2017-09-21 21:28:48 +0000114#if !defined(ElfW)
115#define ElfW(type) Elf_##type
116#endif
117
118#endif
119
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000120namespace libunwind {
121
122/// Used by findUnwindSections() to return info about needed sections.
123struct UnwindInfoSections {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000124#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) || defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) || \
125 defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Charles Davisfa2e6202018-08-30 21:29:00 +0000126 // No dso_base for SEH or ARM EHABI.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000127 uintptr_t dso_base;
128#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +0000129#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000130 uintptr_t dwarf_section;
131 uintptr_t dwarf_section_length;
132#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +0000133#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000134 uintptr_t dwarf_index_section;
135 uintptr_t dwarf_index_section_length;
136#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +0000137#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000138 uintptr_t compact_unwind_section;
139 uintptr_t compact_unwind_section_length;
140#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +0000141#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000142 uintptr_t arm_section;
143 uintptr_t arm_section_length;
144#endif
145};
146
147
148/// LocalAddressSpace is used as a template parameter to UnwindCursor when
149/// unwinding a thread in the same process. The wrappers compile away,
150/// making local unwinds fast.
Charles Davis04f0c2e2018-08-31 13:41:05 +0000151class _LIBUNWIND_HIDDEN LocalAddressSpace {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000152public:
Martin Storsjocac41382017-10-27 08:11:36 +0000153 typedef uintptr_t pint_t;
154 typedef intptr_t sint_t;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000155 uint8_t get8(pint_t addr) {
156 uint8_t val;
157 memcpy(&val, (void *)addr, sizeof(val));
158 return val;
159 }
160 uint16_t get16(pint_t addr) {
161 uint16_t val;
162 memcpy(&val, (void *)addr, sizeof(val));
163 return val;
164 }
165 uint32_t get32(pint_t addr) {
166 uint32_t val;
167 memcpy(&val, (void *)addr, sizeof(val));
168 return val;
169 }
170 uint64_t get64(pint_t addr) {
171 uint64_t val;
172 memcpy(&val, (void *)addr, sizeof(val));
173 return val;
174 }
175 double getDouble(pint_t addr) {
176 double val;
177 memcpy(&val, (void *)addr, sizeof(val));
178 return val;
179 }
180 v128 getVector(pint_t addr) {
181 v128 val;
182 memcpy(&val, (void *)addr, sizeof(val));
183 return val;
184 }
185 uintptr_t getP(pint_t addr);
John Baldwin1a6e6be2018-02-27 21:24:02 +0000186 uint64_t getRegister(pint_t addr);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000187 static uint64_t getULEB128(pint_t &addr, pint_t end);
188 static int64_t getSLEB128(pint_t &addr, pint_t end);
189
190 pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
191 pint_t datarelBase = 0);
192 bool findFunctionName(pint_t addr, char *buf, size_t bufLen,
193 unw_word_t *offset);
194 bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
195 bool findOtherFDE(pint_t targetAddr, pint_t &fde);
196
197 static LocalAddressSpace sThisAddressSpace;
198};
199
200inline uintptr_t LocalAddressSpace::getP(pint_t addr) {
Martin Storsjocac41382017-10-27 08:11:36 +0000201#if __SIZEOF_POINTER__ == 8
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000202 return get64(addr);
203#else
204 return get32(addr);
205#endif
206}
207
John Baldwin1a6e6be2018-02-27 21:24:02 +0000208inline uint64_t LocalAddressSpace::getRegister(pint_t addr) {
209#if __SIZEOF_POINTER__ == 8 || defined(__mips64)
210 return get64(addr);
211#else
212 return get32(addr);
213#endif
214}
215
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000216/// Read a ULEB128 into a 64-bit word.
217inline uint64_t LocalAddressSpace::getULEB128(pint_t &addr, pint_t end) {
218 const uint8_t *p = (uint8_t *)addr;
219 const uint8_t *pend = (uint8_t *)end;
220 uint64_t result = 0;
221 int bit = 0;
222 do {
223 uint64_t b;
224
225 if (p == pend)
226 _LIBUNWIND_ABORT("truncated uleb128 expression");
227
228 b = *p & 0x7f;
229
230 if (bit >= 64 || b << bit >> bit != b) {
231 _LIBUNWIND_ABORT("malformed uleb128 expression");
232 } else {
233 result |= b << bit;
234 bit += 7;
235 }
236 } while (*p++ >= 0x80);
237 addr = (pint_t) p;
238 return result;
239}
240
241/// Read a SLEB128 into a 64-bit word.
242inline int64_t LocalAddressSpace::getSLEB128(pint_t &addr, pint_t end) {
243 const uint8_t *p = (uint8_t *)addr;
244 const uint8_t *pend = (uint8_t *)end;
245 int64_t result = 0;
246 int bit = 0;
247 uint8_t byte;
248 do {
249 if (p == pend)
250 _LIBUNWIND_ABORT("truncated sleb128 expression");
251 byte = *p++;
Ryan Prichardc2174592020-07-13 22:06:47 -0700252 result |= (uint64_t)(byte & 0x7f) << bit;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000253 bit += 7;
254 } while (byte & 0x80);
255 // sign extend negative numbers
Ryan Prichardc2174592020-07-13 22:06:47 -0700256 if ((byte & 0x40) != 0 && bit < 64)
Marshall Clowa6d7c382017-06-21 16:02:53 +0000257 result |= (-1ULL) << bit;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000258 addr = (pint_t) p;
259 return result;
260}
261
262inline LocalAddressSpace::pint_t
263LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
264 pint_t datarelBase) {
265 pint_t startAddr = addr;
266 const uint8_t *p = (uint8_t *)addr;
267 pint_t result;
268
269 // first get value
270 switch (encoding & 0x0F) {
271 case DW_EH_PE_ptr:
272 result = getP(addr);
273 p += sizeof(pint_t);
274 addr = (pint_t) p;
275 break;
276 case DW_EH_PE_uleb128:
277 result = (pint_t)getULEB128(addr, end);
278 break;
279 case DW_EH_PE_udata2:
280 result = get16(addr);
281 p += 2;
282 addr = (pint_t) p;
283 break;
284 case DW_EH_PE_udata4:
285 result = get32(addr);
286 p += 4;
287 addr = (pint_t) p;
288 break;
289 case DW_EH_PE_udata8:
290 result = (pint_t)get64(addr);
291 p += 8;
292 addr = (pint_t) p;
293 break;
294 case DW_EH_PE_sleb128:
295 result = (pint_t)getSLEB128(addr, end);
296 break;
297 case DW_EH_PE_sdata2:
298 // Sign extend from signed 16-bit value.
299 result = (pint_t)(int16_t)get16(addr);
300 p += 2;
301 addr = (pint_t) p;
302 break;
303 case DW_EH_PE_sdata4:
304 // Sign extend from signed 32-bit value.
305 result = (pint_t)(int32_t)get32(addr);
306 p += 4;
307 addr = (pint_t) p;
308 break;
309 case DW_EH_PE_sdata8:
310 result = (pint_t)get64(addr);
311 p += 8;
312 addr = (pint_t) p;
313 break;
314 default:
315 _LIBUNWIND_ABORT("unknown pointer encoding");
316 }
317
318 // then add relative offset
319 switch (encoding & 0x70) {
320 case DW_EH_PE_absptr:
321 // do nothing
322 break;
323 case DW_EH_PE_pcrel:
324 result += startAddr;
325 break;
326 case DW_EH_PE_textrel:
327 _LIBUNWIND_ABORT("DW_EH_PE_textrel pointer encoding not supported");
328 break;
329 case DW_EH_PE_datarel:
330 // DW_EH_PE_datarel is only valid in a few places, so the parameter has a
331 // default value of 0, and we abort in the event that someone calls this
332 // function with a datarelBase of 0 and DW_EH_PE_datarel encoding.
333 if (datarelBase == 0)
334 _LIBUNWIND_ABORT("DW_EH_PE_datarel is invalid with a datarelBase of 0");
335 result += datarelBase;
336 break;
337 case DW_EH_PE_funcrel:
338 _LIBUNWIND_ABORT("DW_EH_PE_funcrel pointer encoding not supported");
339 break;
340 case DW_EH_PE_aligned:
341 _LIBUNWIND_ABORT("DW_EH_PE_aligned pointer encoding not supported");
342 break;
343 default:
344 _LIBUNWIND_ABORT("unknown pointer encoding");
345 break;
346 }
347
348 if (encoding & DW_EH_PE_indirect)
349 result = getP(result);
350
351 return result;
352}
353
Sterling Augustine6d474102020-03-04 12:41:54 -0800354#ifdef __APPLE__
355#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL)
356#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)
357#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_WIN32)
358#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
359#elif defined(_LIBUNWIND_ARM_EHABI) && defined(__BIONIC__)
360// Code inside findUnwindSections handles all these cases.
361//
362// Although the above ifdef chain is ugly, there doesn't seem to be a cleaner
363// way to handle it. The generalized boolean expression is:
364//
365// A OR (B AND C) OR (D AND C) OR (B AND E) OR (F AND E) OR (D AND G)
366//
367// Running it through various boolean expression simplifiers gives expressions
368// that don't help at all.
369#elif defined(_LIBUNWIND_ARM_EHABI) || defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
370
Sterling Augustine6d474102020-03-04 12:41:54 -0800371#if !defined(Elf_Half)
372 typedef ElfW(Half) Elf_Half;
373#endif
374#if !defined(Elf_Phdr)
375 typedef ElfW(Phdr) Elf_Phdr;
376#endif
377#if !defined(Elf_Addr)
378 typedef ElfW(Addr) Elf_Addr;
379#endif
380
Sterling Augustine8eb93ce2020-03-05 11:50:51 -0800381static Elf_Addr calculateImageBase(struct dl_phdr_info *pinfo) {
Sterling Augustine6d474102020-03-04 12:41:54 -0800382 Elf_Addr image_base = pinfo->dlpi_addr;
Sterling Augustine6d474102020-03-04 12:41:54 -0800383#if defined(__ANDROID__) && __ANDROID_API__ < 18
384 if (image_base == 0) {
385 // Normally, an image base of 0 indicates a non-PIE executable. On
386 // versions of Android prior to API 18, the dynamic linker reported a
387 // dlpi_addr of 0 for PIE executables. Compute the true image base
388 // using the PT_PHDR segment.
389 // See https://github.com/android/ndk/issues/505.
390 for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) {
391 const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i];
392 if (phdr->p_type == PT_PHDR) {
393 image_base = reinterpret_cast<Elf_Addr>(pinfo->dlpi_phdr) -
394 phdr->p_vaddr;
395 break;
396 }
397 }
398 }
399#endif
Sterling Augustine8eb93ce2020-03-05 11:50:51 -0800400 return image_base;
401}
Sterling Augustine6d474102020-03-04 12:41:54 -0800402
Sterling Augustine8eb93ce2020-03-05 11:50:51 -0800403struct _LIBUNWIND_HIDDEN dl_iterate_cb_data {
404 LocalAddressSpace *addressSpace;
405 UnwindInfoSections *sects;
406 uintptr_t targetAddr;
407};
408
409#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Sterling Augustine6d474102020-03-04 12:41:54 -0800410 #if !defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Sterling Augustine8eb93ce2020-03-05 11:50:51 -0800411 #error "_LIBUNWIND_SUPPORT_DWARF_UNWIND requires _LIBUNWIND_SUPPORT_DWARF_INDEX on this platform."
Sterling Augustine6d474102020-03-04 12:41:54 -0800412 #endif
Sterling Augustine8eb93ce2020-03-05 11:50:51 -0800413
Sterling Augustineb778c912020-08-18 12:05:07 -0700414#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
Sterling Augustine2e9018a2020-03-10 12:03:24 -0700415#include "FrameHeaderCache.hpp"
416
417// There should be just one of these per process.
418static FrameHeaderCache ProcessFrameHeaderCache;
Sterling Augustineb778c912020-08-18 12:05:07 -0700419#endif
Sterling Augustine2e9018a2020-03-10 12:03:24 -0700420
Sterling Augustine5101ebe2020-03-06 15:59:16 -0800421static bool checkAddrInSegment(const Elf_Phdr *phdr, size_t image_base,
422 dl_iterate_cb_data *cbdata) {
423 if (phdr->p_type == PT_LOAD) {
424 uintptr_t begin = image_base + phdr->p_vaddr;
425 uintptr_t end = begin + phdr->p_memsz;
426 if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) {
427 cbdata->sects->dso_base = begin;
428 cbdata->sects->dwarf_section_length = phdr->p_memsz;
429 return true;
430 }
431 }
432 return false;
433}
434
Ryan Prichard6de71032020-08-22 17:12:52 -0700435static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo,
436 size_t pinfo_size, void *data) {
Sterling Augustine5101ebe2020-03-06 15:59:16 -0800437 auto cbdata = static_cast<dl_iterate_cb_data *>(data);
438 if (pinfo->dlpi_phnum == 0 || cbdata->targetAddr < pinfo->dlpi_addr)
Sterling Augustine8eb93ce2020-03-05 11:50:51 -0800439 return 0;
Sterling Augustineb778c912020-08-18 12:05:07 -0700440#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
Sterling Augustine2e9018a2020-03-10 12:03:24 -0700441 if (ProcessFrameHeaderCache.find(pinfo, pinfo_size, data))
442 return 1;
Mikael Holmen7c371492020-08-25 12:26:48 +0200443#else
444 // Avoid warning about unused variable.
445 (void)pinfo_size;
Sterling Augustineb778c912020-08-18 12:05:07 -0700446#endif
Sterling Augustine8eb93ce2020-03-05 11:50:51 -0800447
448 Elf_Addr image_base = calculateImageBase(pinfo);
Sterling Augustine5101ebe2020-03-06 15:59:16 -0800449 bool found_obj = false;
450 bool found_hdr = false;
Sterling Augustine6d474102020-03-04 12:41:54 -0800451
Sterling Augustine5101ebe2020-03-06 15:59:16 -0800452 // Third phdr is usually the executable phdr.
453 if (pinfo->dlpi_phnum > 2)
454 found_obj = checkAddrInSegment(&pinfo->dlpi_phdr[2], image_base, cbdata);
455
456 // PT_GNU_EH_FRAME is usually near the end. Iterate backward. We already know
457 // that there is one or more phdrs.
458 for (Elf_Half i = pinfo->dlpi_phnum; i > 0; i--) {
459 const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i - 1];
460 if (!found_hdr && phdr->p_type == PT_GNU_EH_FRAME) {
Sterling Augustine6d474102020-03-04 12:41:54 -0800461 EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo;
462 uintptr_t eh_frame_hdr_start = image_base + phdr->p_vaddr;
463 cbdata->sects->dwarf_index_section = eh_frame_hdr_start;
464 cbdata->sects->dwarf_index_section_length = phdr->p_memsz;
465 found_hdr = EHHeaderParser<LocalAddressSpace>::decodeEHHdr(
466 *cbdata->addressSpace, eh_frame_hdr_start, phdr->p_memsz,
467 hdrInfo);
468 if (found_hdr)
469 cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr;
Sterling Augustine5101ebe2020-03-06 15:59:16 -0800470 } else if (!found_obj) {
471 found_obj = checkAddrInSegment(phdr, image_base, cbdata);
Sterling Augustine6d474102020-03-04 12:41:54 -0800472 }
Sterling Augustine2e9018a2020-03-10 12:03:24 -0700473 if (found_obj && found_hdr) {
Sterling Augustineb778c912020-08-18 12:05:07 -0700474#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
Sterling Augustine2e9018a2020-03-10 12:03:24 -0700475 ProcessFrameHeaderCache.add(cbdata->sects);
Sterling Augustineb778c912020-08-18 12:05:07 -0700476#endif
Sterling Augustine5101ebe2020-03-06 15:59:16 -0800477 return 1;
Sterling Augustine2e9018a2020-03-10 12:03:24 -0700478 }
Sterling Augustine6d474102020-03-04 12:41:54 -0800479 }
Sterling Augustine5101ebe2020-03-06 15:59:16 -0800480 cbdata->sects->dwarf_section_length = 0;
481 return 0;
Sterling Augustine8eb93ce2020-03-05 11:50:51 -0800482}
483
484#else // defined(LIBUNWIND_SUPPORT_DWARF_UNWIND)
485// Given all the #ifdef's above, the code here is for
486// defined(LIBUNWIND_ARM_EHABI)
487
Ryan Prichard6de71032020-08-22 17:12:52 -0700488static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, size_t,
489 void *data) {
Sterling Augustine8eb93ce2020-03-05 11:50:51 -0800490 auto *cbdata = static_cast<dl_iterate_cb_data *>(data);
491 bool found_obj = false;
492 bool found_hdr = false;
493
494 assert(cbdata);
495 assert(cbdata->sects);
496
497 if (cbdata->targetAddr < pinfo->dlpi_addr)
498 return 0;
499
500 Elf_Addr image_base = calculateImageBase(pinfo);
501
Sterling Augustine6d474102020-03-04 12:41:54 -0800502 for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) {
503 const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i];
504 if (phdr->p_type == PT_LOAD) {
505 uintptr_t begin = image_base + phdr->p_vaddr;
506 uintptr_t end = begin + phdr->p_memsz;
507 if (cbdata->targetAddr >= begin && cbdata->targetAddr < end)
508 found_obj = true;
509 } else if (phdr->p_type == PT_ARM_EXIDX) {
510 uintptr_t exidx_start = image_base + phdr->p_vaddr;
511 cbdata->sects->arm_section = exidx_start;
512 cbdata->sects->arm_section_length = phdr->p_memsz;
513 found_hdr = true;
514 }
515 }
516 return found_obj && found_hdr;
Sterling Augustine6d474102020-03-04 12:41:54 -0800517}
Sterling Augustine8eb93ce2020-03-05 11:50:51 -0800518#endif // defined(LIBUNWIND_SUPPORT_DWARF_UNWIND)
Sterling Augustine6d474102020-03-04 12:41:54 -0800519#endif // defined(_LIBUNWIND_ARM_EHABI) || defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
520
521
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000522inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
523 UnwindInfoSections &info) {
524#ifdef __APPLE__
525 dyld_unwind_sections dyldInfo;
526 if (_dyld_find_unwind_sections((void *)targetAddr, &dyldInfo)) {
527 info.dso_base = (uintptr_t)dyldInfo.mh;
Ranjeet Singh421231a2017-03-31 15:28:06 +0000528 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000529 info.dwarf_section = (uintptr_t)dyldInfo.dwarf_section;
530 info.dwarf_section_length = dyldInfo.dwarf_section_length;
531 #endif
532 info.compact_unwind_section = (uintptr_t)dyldInfo.compact_unwind_section;
533 info.compact_unwind_section_length = dyldInfo.compact_unwind_section_length;
534 return true;
535 }
whitequarkffd92e52017-12-25 17:05:07 +0000536#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL)
537 // Bare metal is statically linked, so no need to ask the dynamic loader
538 info.dwarf_section_length = (uintptr_t)(&__eh_frame_end - &__eh_frame_start);
539 info.dwarf_section = (uintptr_t)(&__eh_frame_start);
Chandler Carruth3d957272017-12-27 05:46:53 +0000540 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p",
541 (void *)info.dwarf_section, (void *)info.dwarf_section_length);
whitequarkffd92e52017-12-25 17:05:07 +0000542#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
543 info.dwarf_index_section = (uintptr_t)(&__eh_frame_hdr_start);
544 info.dwarf_index_section_length = (uintptr_t)(&__eh_frame_hdr_end - &__eh_frame_hdr_start);
Chandler Carruth3d957272017-12-27 05:46:53 +0000545 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: index section %p length %p",
546 (void *)info.dwarf_index_section, (void *)info.dwarf_index_section_length);
whitequarkffd92e52017-12-25 17:05:07 +0000547#endif
548 if (info.dwarf_section_length)
549 return true;
Ranjeet Singh421231a2017-03-31 15:28:06 +0000550#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000551 // Bare metal is statically linked, so no need to ask the dynamic loader
552 info.arm_section = (uintptr_t)(&__exidx_start);
553 info.arm_section_length = (uintptr_t)(&__exidx_end - &__exidx_start);
Chandler Carruth3d957272017-12-27 05:46:53 +0000554 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p",
555 (void *)info.arm_section, (void *)info.arm_section_length);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000556 if (info.arm_section && info.arm_section_length)
557 return true;
Martin Storsjo4c1f84d2017-10-11 20:06:18 +0000558#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_WIN32)
559 HMODULE mods[1024];
560 HANDLE process = GetCurrentProcess();
561 DWORD needed;
562
Martin Storsjöa914ec12019-10-28 10:11:05 +0200563 if (!EnumProcessModules(process, mods, sizeof(mods), &needed)) {
564 DWORD err = GetLastError();
565 _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: EnumProcessModules failed, "
566 "returned error %d", (int)err);
Martin Storsjo4c1f84d2017-10-11 20:06:18 +0000567 return false;
Martin Storsjöa914ec12019-10-28 10:11:05 +0200568 }
Martin Storsjo4c1f84d2017-10-11 20:06:18 +0000569
570 for (unsigned i = 0; i < (needed / sizeof(HMODULE)); i++) {
571 PIMAGE_DOS_HEADER pidh = (PIMAGE_DOS_HEADER)mods[i];
572 PIMAGE_NT_HEADERS pinh = (PIMAGE_NT_HEADERS)((BYTE *)pidh + pidh->e_lfanew);
573 PIMAGE_FILE_HEADER pifh = (PIMAGE_FILE_HEADER)&pinh->FileHeader;
574 PIMAGE_SECTION_HEADER pish = IMAGE_FIRST_SECTION(pinh);
575 bool found_obj = false;
576 bool found_hdr = false;
577
578 info.dso_base = (uintptr_t)mods[i];
579 for (unsigned j = 0; j < pifh->NumberOfSections; j++, pish++) {
580 uintptr_t begin = pish->VirtualAddress + (uintptr_t)mods[i];
581 uintptr_t end = begin + pish->Misc.VirtualSize;
582 if (!strncmp((const char *)pish->Name, ".text",
583 IMAGE_SIZEOF_SHORT_NAME)) {
584 if (targetAddr >= begin && targetAddr < end)
585 found_obj = true;
586 } else if (!strncmp((const char *)pish->Name, ".eh_frame",
587 IMAGE_SIZEOF_SHORT_NAME)) {
Martin Storsjo4c1f84d2017-10-11 20:06:18 +0000588 info.dwarf_section = begin;
589 info.dwarf_section_length = pish->Misc.VirtualSize;
590 found_hdr = true;
591 }
592 if (found_obj && found_hdr)
593 return true;
594 }
595 }
596 return false;
Charles Davisfa2e6202018-08-30 21:29:00 +0000597#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
598 // Don't even bother, since Windows has functions that do all this stuff
599 // for us.
Martin Storsjo059a1632019-01-22 22:12:23 +0000600 (void)targetAddr;
601 (void)info;
Charles Davisfa2e6202018-08-30 21:29:00 +0000602 return true;
Ryan Prichard2a4f1362019-10-18 19:59:22 +0000603#elif defined(_LIBUNWIND_ARM_EHABI) && defined(__BIONIC__)
604 // For ARM EHABI, Bionic didn't implement dl_iterate_phdr until API 21. After
605 // API 21, dl_iterate_phdr exists, but dl_unwind_find_exidx is much faster.
Dan Albert1bf6ebe2017-11-01 21:26:06 +0000606 int length = 0;
607 info.arm_section =
608 (uintptr_t)dl_unwind_find_exidx((_Unwind_Ptr)targetAddr, &length);
Ryan Prichard2a4f1362019-10-18 19:59:22 +0000609 info.arm_section_length = (uintptr_t)length * sizeof(EHABIIndexEntry);
Dan Albert1bf6ebe2017-11-01 21:26:06 +0000610 if (info.arm_section && info.arm_section_length)
611 return true;
Ranjeet Singh421231a2017-03-31 15:28:06 +0000612#elif defined(_LIBUNWIND_ARM_EHABI) || defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000613 dl_iterate_cb_data cb_data = {this, &info, targetAddr};
Sterling Augustine6d474102020-03-04 12:41:54 -0800614 int found = dl_iterate_phdr(findUnwindSectionsByPhdr, &cb_data);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000615 return static_cast<bool>(found);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000616#endif
617
618 return false;
619}
620
621
622inline bool LocalAddressSpace::findOtherFDE(pint_t targetAddr, pint_t &fde) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000623 // TO DO: if OS has way to dynamically register FDEs, check that.
624 (void)targetAddr;
625 (void)fde;
626 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000627}
628
629inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf,
630 size_t bufLen,
631 unw_word_t *offset) {
Jordan Rupprecht8ac87db2018-06-29 20:41:50 +0000632#if _LIBUNWIND_USE_DLADDR
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000633 Dl_info dyldInfo;
634 if (dladdr((void *)addr, &dyldInfo)) {
635 if (dyldInfo.dli_sname != NULL) {
636 snprintf(buf, bufLen, "%s", dyldInfo.dli_sname);
637 *offset = (addr - (pint_t) dyldInfo.dli_saddr);
638 return true;
639 }
640 }
Martin Storsjo059a1632019-01-22 22:12:23 +0000641#else
642 (void)addr;
643 (void)buf;
644 (void)bufLen;
645 (void)offset;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000646#endif
647 return false;
648}
649
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000650} // namespace libunwind
651
652#endif // __ADDRESSSPACE_HPP__