blob: 6300b995a3aabe0f2b03d1738bc6c137e0703238 [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001//===------------------------- UnwindCursor.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//
Ed Maste6f723382016-08-30 13:08:21 +00009// C++ interface to lower levels of libunwind
Saleem Abdulrasool17552662015-04-24 19:39:17 +000010//===----------------------------------------------------------------------===//
11
12#ifndef __UNWINDCURSOR_HPP__
13#define __UNWINDCURSOR_HPP__
14
15#include <algorithm>
16#include <stdint.h>
17#include <stdio.h>
18#include <stdlib.h>
Saleem Abdulrasool17552662015-04-24 19:39:17 +000019#include <unwind.h>
20
21#ifdef __APPLE__
22 #include <mach-o/dyld.h>
23#endif
24
25#include "config.h"
26
27#include "AddressSpace.hpp"
28#include "CompactUnwinder.hpp"
29#include "config.h"
30#include "DwarfInstructions.hpp"
31#include "EHHeaderParser.hpp"
32#include "libunwind.h"
33#include "Registers.hpp"
Martin Storsjo590ffef2017-10-23 19:29:36 +000034#include "RWMutex.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000035#include "Unwind-EHABI.h"
36
37namespace libunwind {
38
Ranjeet Singh421231a2017-03-31 15:28:06 +000039#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000040/// Cache of recently found FDEs.
41template <typename A>
42class _LIBUNWIND_HIDDEN DwarfFDECache {
43 typedef typename A::pint_t pint_t;
44public:
45 static pint_t findFDE(pint_t mh, pint_t pc);
46 static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
47 static void removeAllIn(pint_t mh);
48 static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
49 unw_word_t ip_end,
50 unw_word_t fde, unw_word_t mh));
51
52private:
53
54 struct entry {
55 pint_t mh;
56 pint_t ip_start;
57 pint_t ip_end;
58 pint_t fde;
59 };
60
61 // These fields are all static to avoid needing an initializer.
62 // There is only one instance of this class per process.
Martin Storsjo590ffef2017-10-23 19:29:36 +000063 static RWMutex _lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +000064#ifdef __APPLE__
65 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
66 static bool _registeredForDyldUnloads;
67#endif
68 // Can't use std::vector<> here because this code is below libc++.
69 static entry *_buffer;
70 static entry *_bufferUsed;
71 static entry *_bufferEnd;
72 static entry _initialBuffer[64];
73};
74
75template <typename A>
76typename DwarfFDECache<A>::entry *
77DwarfFDECache<A>::_buffer = _initialBuffer;
78
79template <typename A>
80typename DwarfFDECache<A>::entry *
81DwarfFDECache<A>::_bufferUsed = _initialBuffer;
82
83template <typename A>
84typename DwarfFDECache<A>::entry *
85DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
86
87template <typename A>
88typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
89
90template <typename A>
Martin Storsjo590ffef2017-10-23 19:29:36 +000091RWMutex DwarfFDECache<A>::_lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +000092
93#ifdef __APPLE__
94template <typename A>
95bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
96#endif
97
98template <typename A>
99typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
100 pint_t result = 0;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000101 _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000102 for (entry *p = _buffer; p < _bufferUsed; ++p) {
103 if ((mh == p->mh) || (mh == 0)) {
104 if ((p->ip_start <= pc) && (pc < p->ip_end)) {
105 result = p->fde;
106 break;
107 }
108 }
109 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000110 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000111 return result;
112}
113
114template <typename A>
115void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
116 pint_t fde) {
Peter Zotov0717a2e2015-11-09 06:57:29 +0000117#if !defined(_LIBUNWIND_NO_HEAP)
Martin Storsjo590ffef2017-10-23 19:29:36 +0000118 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000119 if (_bufferUsed >= _bufferEnd) {
120 size_t oldSize = (size_t)(_bufferEnd - _buffer);
121 size_t newSize = oldSize * 4;
122 // Can't use operator new (we are below it).
123 entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
124 memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
125 if (_buffer != _initialBuffer)
126 free(_buffer);
127 _buffer = newBuffer;
128 _bufferUsed = &newBuffer[oldSize];
129 _bufferEnd = &newBuffer[newSize];
130 }
131 _bufferUsed->mh = mh;
132 _bufferUsed->ip_start = ip_start;
133 _bufferUsed->ip_end = ip_end;
134 _bufferUsed->fde = fde;
135 ++_bufferUsed;
136#ifdef __APPLE__
137 if (!_registeredForDyldUnloads) {
138 _dyld_register_func_for_remove_image(&dyldUnloadHook);
139 _registeredForDyldUnloads = true;
140 }
141#endif
Martin Storsjo590ffef2017-10-23 19:29:36 +0000142 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Peter Zotov0717a2e2015-11-09 06:57:29 +0000143#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000144}
145
146template <typename A>
147void DwarfFDECache<A>::removeAllIn(pint_t mh) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000148 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000149 entry *d = _buffer;
150 for (const entry *s = _buffer; s < _bufferUsed; ++s) {
151 if (s->mh != mh) {
152 if (d != s)
153 *d = *s;
154 ++d;
155 }
156 }
157 _bufferUsed = d;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000158 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000159}
160
161#ifdef __APPLE__
162template <typename A>
163void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
164 removeAllIn((pint_t) mh);
165}
166#endif
167
168template <typename A>
169void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
170 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000171 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000172 for (entry *p = _buffer; p < _bufferUsed; ++p) {
173 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
174 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000175 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000176}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000177#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000178
179
180#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
181
Ranjeet Singh421231a2017-03-31 15:28:06 +0000182#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000183template <typename A> class UnwindSectionHeader {
184public:
185 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
186 : _addressSpace(addressSpace), _addr(addr) {}
187
188 uint32_t version() const {
189 return _addressSpace.get32(_addr +
190 offsetof(unwind_info_section_header, version));
191 }
192 uint32_t commonEncodingsArraySectionOffset() const {
193 return _addressSpace.get32(_addr +
194 offsetof(unwind_info_section_header,
195 commonEncodingsArraySectionOffset));
196 }
197 uint32_t commonEncodingsArrayCount() const {
198 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
199 commonEncodingsArrayCount));
200 }
201 uint32_t personalityArraySectionOffset() const {
202 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
203 personalityArraySectionOffset));
204 }
205 uint32_t personalityArrayCount() const {
206 return _addressSpace.get32(
207 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
208 }
209 uint32_t indexSectionOffset() const {
210 return _addressSpace.get32(
211 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
212 }
213 uint32_t indexCount() const {
214 return _addressSpace.get32(
215 _addr + offsetof(unwind_info_section_header, indexCount));
216 }
217
218private:
219 A &_addressSpace;
220 typename A::pint_t _addr;
221};
222
223template <typename A> class UnwindSectionIndexArray {
224public:
225 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
226 : _addressSpace(addressSpace), _addr(addr) {}
227
228 uint32_t functionOffset(uint32_t index) const {
229 return _addressSpace.get32(
230 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
231 functionOffset));
232 }
233 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
234 return _addressSpace.get32(
235 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
236 secondLevelPagesSectionOffset));
237 }
238 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
239 return _addressSpace.get32(
240 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
241 lsdaIndexArraySectionOffset));
242 }
243
244private:
245 A &_addressSpace;
246 typename A::pint_t _addr;
247};
248
249template <typename A> class UnwindSectionRegularPageHeader {
250public:
251 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
252 : _addressSpace(addressSpace), _addr(addr) {}
253
254 uint32_t kind() const {
255 return _addressSpace.get32(
256 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
257 }
258 uint16_t entryPageOffset() const {
259 return _addressSpace.get16(
260 _addr + offsetof(unwind_info_regular_second_level_page_header,
261 entryPageOffset));
262 }
263 uint16_t entryCount() const {
264 return _addressSpace.get16(
265 _addr +
266 offsetof(unwind_info_regular_second_level_page_header, entryCount));
267 }
268
269private:
270 A &_addressSpace;
271 typename A::pint_t _addr;
272};
273
274template <typename A> class UnwindSectionRegularArray {
275public:
276 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
277 : _addressSpace(addressSpace), _addr(addr) {}
278
279 uint32_t functionOffset(uint32_t index) const {
280 return _addressSpace.get32(
281 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
282 functionOffset));
283 }
284 uint32_t encoding(uint32_t index) const {
285 return _addressSpace.get32(
286 _addr +
287 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
288 }
289
290private:
291 A &_addressSpace;
292 typename A::pint_t _addr;
293};
294
295template <typename A> class UnwindSectionCompressedPageHeader {
296public:
297 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
298 : _addressSpace(addressSpace), _addr(addr) {}
299
300 uint32_t kind() const {
301 return _addressSpace.get32(
302 _addr +
303 offsetof(unwind_info_compressed_second_level_page_header, kind));
304 }
305 uint16_t entryPageOffset() const {
306 return _addressSpace.get16(
307 _addr + offsetof(unwind_info_compressed_second_level_page_header,
308 entryPageOffset));
309 }
310 uint16_t entryCount() const {
311 return _addressSpace.get16(
312 _addr +
313 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
314 }
315 uint16_t encodingsPageOffset() const {
316 return _addressSpace.get16(
317 _addr + offsetof(unwind_info_compressed_second_level_page_header,
318 encodingsPageOffset));
319 }
320 uint16_t encodingsCount() const {
321 return _addressSpace.get16(
322 _addr + offsetof(unwind_info_compressed_second_level_page_header,
323 encodingsCount));
324 }
325
326private:
327 A &_addressSpace;
328 typename A::pint_t _addr;
329};
330
331template <typename A> class UnwindSectionCompressedArray {
332public:
333 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
334 : _addressSpace(addressSpace), _addr(addr) {}
335
336 uint32_t functionOffset(uint32_t index) const {
337 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
338 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
339 }
340 uint16_t encodingIndex(uint32_t index) const {
341 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
342 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
343 }
344
345private:
346 A &_addressSpace;
347 typename A::pint_t _addr;
348};
349
350template <typename A> class UnwindSectionLsdaArray {
351public:
352 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
353 : _addressSpace(addressSpace), _addr(addr) {}
354
355 uint32_t functionOffset(uint32_t index) const {
356 return _addressSpace.get32(
357 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
358 index, functionOffset));
359 }
360 uint32_t lsdaOffset(uint32_t index) const {
361 return _addressSpace.get32(
362 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
363 index, lsdaOffset));
364 }
365
366private:
367 A &_addressSpace;
368 typename A::pint_t _addr;
369};
Ranjeet Singh421231a2017-03-31 15:28:06 +0000370#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000371
372class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
373public:
374 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
375 // This avoids an unnecessary dependency to libc++abi.
376 void operator delete(void *, size_t) {}
377
378 virtual ~AbstractUnwindCursor() {}
379 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
380 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
381 virtual void setReg(int, unw_word_t) {
382 _LIBUNWIND_ABORT("setReg not implemented");
383 }
384 virtual bool validFloatReg(int) {
385 _LIBUNWIND_ABORT("validFloatReg not implemented");
386 }
387 virtual unw_fpreg_t getFloatReg(int) {
388 _LIBUNWIND_ABORT("getFloatReg not implemented");
389 }
390 virtual void setFloatReg(int, unw_fpreg_t) {
391 _LIBUNWIND_ABORT("setFloatReg not implemented");
392 }
393 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
394 virtual void getInfo(unw_proc_info_t *) {
395 _LIBUNWIND_ABORT("getInfo not implemented");
396 }
397 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
398 virtual bool isSignalFrame() {
399 _LIBUNWIND_ABORT("isSignalFrame not implemented");
400 }
401 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
402 _LIBUNWIND_ABORT("getFunctionName not implemented");
403 }
404 virtual void setInfoBasedOnIPRegister(bool = false) {
405 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
406 }
407 virtual const char *getRegisterName(int) {
408 _LIBUNWIND_ABORT("getRegisterName not implemented");
409 }
410#ifdef __arm__
411 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
412#endif
413};
414
415/// UnwindCursor contains all state (including all register values) during
416/// an unwind. This is normally stack allocated inside a unw_cursor_t.
417template <typename A, typename R>
418class UnwindCursor : public AbstractUnwindCursor{
419 typedef typename A::pint_t pint_t;
420public:
421 UnwindCursor(unw_context_t *context, A &as);
422 UnwindCursor(A &as, void *threadArg);
423 virtual ~UnwindCursor() {}
424 virtual bool validReg(int);
425 virtual unw_word_t getReg(int);
426 virtual void setReg(int, unw_word_t);
427 virtual bool validFloatReg(int);
428 virtual unw_fpreg_t getFloatReg(int);
429 virtual void setFloatReg(int, unw_fpreg_t);
430 virtual int step();
431 virtual void getInfo(unw_proc_info_t *);
432 virtual void jumpto();
433 virtual bool isSignalFrame();
434 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
435 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
436 virtual const char *getRegisterName(int num);
437#ifdef __arm__
438 virtual void saveVFPAsX();
439#endif
440
441private:
442
Ranjeet Singh421231a2017-03-31 15:28:06 +0000443#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000444 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000445
446 int stepWithEHABI() {
447 size_t len = 0;
448 size_t off = 0;
449 // FIXME: Calling decode_eht_entry() here is violating the libunwind
450 // abstraction layer.
451 const uint32_t *ehtp =
452 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
453 &off, &len);
454 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
455 _URC_CONTINUE_UNWIND)
456 return UNW_STEP_END;
457 return UNW_STEP_SUCCESS;
458 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000459#endif
460
Ranjeet Singh421231a2017-03-31 15:28:06 +0000461#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000462 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
463 uint32_t fdeSectionOffsetHint=0);
464 int stepWithDwarfFDE() {
465 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
466 (pint_t)this->getReg(UNW_REG_IP),
467 (pint_t)_info.unwind_info,
468 _registers);
469 }
470#endif
471
Ranjeet Singh421231a2017-03-31 15:28:06 +0000472#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000473 bool getInfoFromCompactEncodingSection(pint_t pc,
474 const UnwindInfoSections &sects);
475 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000476 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000477 if ( compactSaysUseDwarf() )
478 return stepWithDwarfFDE();
479 #endif
480 R dummy;
481 return stepWithCompactEncoding(dummy);
482 }
483
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000484#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000485 int stepWithCompactEncoding(Registers_x86_64 &) {
486 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
487 _info.format, _info.start_ip, _addressSpace, _registers);
488 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000489#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000490
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000491#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000492 int stepWithCompactEncoding(Registers_x86 &) {
493 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
494 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
495 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000496#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000497
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000498#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000499 int stepWithCompactEncoding(Registers_ppc &) {
500 return UNW_EINVAL;
501 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000502#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000503
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000504#if defined(_LIBUNWIND_TARGET_PPC64)
505 int stepWithCompactEncoding(Registers_ppc64 &) {
506 return UNW_EINVAL;
507 }
508#endif
509
510
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000511#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000512 int stepWithCompactEncoding(Registers_arm64 &) {
513 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
514 _info.format, _info.start_ip, _addressSpace, _registers);
515 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000516#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000517
John Baldwin56441d42017-12-12 21:43:36 +0000518#if defined(_LIBUNWIND_TARGET_MIPS_O32)
519 int stepWithCompactEncoding(Registers_mips_o32 &) {
520 return UNW_EINVAL;
521 }
522#endif
523
524#if defined(_LIBUNWIND_TARGET_MIPS_N64)
525 int stepWithCompactEncoding(Registers_mips_n64 &) {
526 return UNW_EINVAL;
527 }
528#endif
529
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000530 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
531 R dummy;
532 return compactSaysUseDwarf(dummy, offset);
533 }
534
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000535#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000536 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
537 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
538 if (offset)
539 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
540 return true;
541 }
542 return false;
543 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000544#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000545
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000546#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000547 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
548 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
549 if (offset)
550 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
551 return true;
552 }
553 return false;
554 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000555#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000556
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000557#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000558 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
559 return true;
560 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000561#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000562
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000563#if defined(_LIBUNWIND_TARGET_PPC64)
564 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
565 return true;
566 }
567#endif
568
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000569#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000570 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
571 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
572 if (offset)
573 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
574 return true;
575 }
576 return false;
577 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000578#endif
John Baldwin56441d42017-12-12 21:43:36 +0000579
580#if defined(_LIBUNWIND_TARGET_MIPS_O32)
581 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
582 return true;
583 }
584#endif
585
586#if defined(_LIBUNWIND_TARGET_MIPS_N64)
587 bool compactSaysUseDwarf(Registers_mips_n64 &, uint32_t *) const {
588 return true;
589 }
590#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +0000591#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000592
Ranjeet Singh421231a2017-03-31 15:28:06 +0000593#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000594 compact_unwind_encoding_t dwarfEncoding() const {
595 R dummy;
596 return dwarfEncoding(dummy);
597 }
598
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000599#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000600 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
601 return UNWIND_X86_64_MODE_DWARF;
602 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000603#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000604
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000605#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000606 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
607 return UNWIND_X86_MODE_DWARF;
608 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000609#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000610
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000611#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000612 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
613 return 0;
614 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000615#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000616
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000617#if defined(_LIBUNWIND_TARGET_PPC64)
618 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
619 return 0;
620 }
621#endif
622
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000623#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000624 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
625 return UNWIND_ARM64_MODE_DWARF;
626 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000627#endif
Peter Zotov8d639992015-08-31 05:26:37 +0000628
Martin Storsjoa72285f2017-11-02 08:16:16 +0000629#if defined(_LIBUNWIND_TARGET_ARM)
630 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
631 return 0;
632 }
633#endif
634
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000635#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +0000636 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
637 return 0;
638 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000639#endif
John Baldwin56441d42017-12-12 21:43:36 +0000640
641#if defined (_LIBUNWIND_TARGET_MIPS_O32)
642 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
643 return 0;
644 }
645#endif
646
647#if defined (_LIBUNWIND_TARGET_MIPS_N64)
648 compact_unwind_encoding_t dwarfEncoding(Registers_mips_n64 &) const {
649 return 0;
650 }
651#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +0000652#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000653
654
655 A &_addressSpace;
656 R _registers;
657 unw_proc_info_t _info;
658 bool _unwindInfoMissing;
659 bool _isSignalFrame;
660};
661
662
663template <typename A, typename R>
664UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
665 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
666 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +0000667 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000668 "UnwindCursor<> does not fit in unw_cursor_t");
669 memset(&_info, 0, sizeof(_info));
670}
671
672template <typename A, typename R>
673UnwindCursor<A, R>::UnwindCursor(A &as, void *)
674 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
675 memset(&_info, 0, sizeof(_info));
676 // FIXME
677 // fill in _registers from thread arg
678}
679
680
681template <typename A, typename R>
682bool UnwindCursor<A, R>::validReg(int regNum) {
683 return _registers.validRegister(regNum);
684}
685
686template <typename A, typename R>
687unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
688 return _registers.getRegister(regNum);
689}
690
691template <typename A, typename R>
692void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
693 _registers.setRegister(regNum, (typename A::pint_t)value);
694}
695
696template <typename A, typename R>
697bool UnwindCursor<A, R>::validFloatReg(int regNum) {
698 return _registers.validFloatRegister(regNum);
699}
700
701template <typename A, typename R>
702unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
703 return _registers.getFloatRegister(regNum);
704}
705
706template <typename A, typename R>
707void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
708 _registers.setFloatRegister(regNum, value);
709}
710
711template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
712 _registers.jumpto();
713}
714
715#ifdef __arm__
716template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
717 _registers.saveVFPAsX();
718}
719#endif
720
721template <typename A, typename R>
722const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
723 return _registers.getRegisterName(regNum);
724}
725
726template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
727 return _isSignalFrame;
728}
729
Ranjeet Singh421231a2017-03-31 15:28:06 +0000730#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000731struct EHABIIndexEntry {
732 uint32_t functionOffset;
733 uint32_t data;
734};
735
736template<typename A>
737struct EHABISectionIterator {
738 typedef EHABISectionIterator _Self;
739
740 typedef std::random_access_iterator_tag iterator_category;
741 typedef typename A::pint_t value_type;
742 typedef typename A::pint_t* pointer;
743 typedef typename A::pint_t& reference;
744 typedef size_t size_type;
745 typedef size_t difference_type;
746
747 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
748 return _Self(addressSpace, sects, 0);
749 }
750 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +0000751 return _Self(addressSpace, sects,
752 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000753 }
754
755 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
756 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
757
758 _Self& operator++() { ++_i; return *this; }
759 _Self& operator+=(size_t a) { _i += a; return *this; }
760 _Self& operator--() { assert(_i > 0); --_i; return *this; }
761 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
762
763 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
764 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
765
766 size_t operator-(const _Self& other) { return _i - other._i; }
767
768 bool operator==(const _Self& other) const {
769 assert(_addressSpace == other._addressSpace);
770 assert(_sects == other._sects);
771 return _i == other._i;
772 }
773
774 typename A::pint_t operator*() const { return functionAddress(); }
775
776 typename A::pint_t functionAddress() const {
777 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
778 EHABIIndexEntry, _i, functionOffset);
779 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
780 }
781
782 typename A::pint_t dataAddress() {
783 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
784 EHABIIndexEntry, _i, data);
785 return indexAddr;
786 }
787
788 private:
789 size_t _i;
790 A* _addressSpace;
791 const UnwindInfoSections* _sects;
792};
793
794template <typename A, typename R>
795bool UnwindCursor<A, R>::getInfoFromEHABISection(
796 pint_t pc,
797 const UnwindInfoSections &sects) {
798 EHABISectionIterator<A> begin =
799 EHABISectionIterator<A>::begin(_addressSpace, sects);
800 EHABISectionIterator<A> end =
801 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +0000802 if (begin == end)
803 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000804
805 EHABISectionIterator<A> itNextPC = std::upper_bound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +0000806 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000807 return false;
808 EHABISectionIterator<A> itThisPC = itNextPC - 1;
809
810 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +0000811 // If an exception is thrown from a function, corresponding to the last entry
812 // in the table, we don't really know the function extent and have to choose a
813 // value for nextPC. Choosing max() will allow the range check during trace to
814 // succeed.
815 pint_t nextPC = (itNextPC == end) ? std::numeric_limits<pint_t>::max()
816 : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000817 pint_t indexDataAddr = itThisPC.dataAddress();
818
819 if (indexDataAddr == 0)
820 return false;
821
822 uint32_t indexData = _addressSpace.get32(indexDataAddr);
823 if (indexData == UNW_EXIDX_CANTUNWIND)
824 return false;
825
826 // If the high bit is set, the exception handling table entry is inline inside
827 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
828 // the table points at an offset in the exception handling table (section 5 EHABI).
829 pint_t exceptionTableAddr;
830 uint32_t exceptionTableData;
831 bool isSingleWordEHT;
832 if (indexData & 0x80000000) {
833 exceptionTableAddr = indexDataAddr;
834 // TODO(ajwong): Should this data be 0?
835 exceptionTableData = indexData;
836 isSingleWordEHT = true;
837 } else {
838 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
839 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
840 isSingleWordEHT = false;
841 }
842
843 // Now we know the 3 things:
844 // exceptionTableAddr -- exception handler table entry.
845 // exceptionTableData -- the data inside the first word of the eht entry.
846 // isSingleWordEHT -- whether the entry is in the index.
847 unw_word_t personalityRoutine = 0xbadf00d;
848 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +0000849 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000850
851 // If the high bit in the exception handling table entry is set, the entry is
852 // in compact form (section 6.3 EHABI).
853 if (exceptionTableData & 0x80000000) {
854 // Grab the index of the personality routine from the compact form.
855 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
856 uint32_t extraWords = 0;
857 switch (choice) {
858 case 0:
859 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
860 extraWords = 0;
861 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +0000862 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000863 break;
864 case 1:
865 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
866 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
867 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +0000868 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000869 break;
870 case 2:
871 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
872 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
873 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +0000874 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000875 break;
876 default:
877 _LIBUNWIND_ABORT("unknown personality routine");
878 return false;
879 }
880
881 if (isSingleWordEHT) {
882 if (extraWords != 0) {
883 _LIBUNWIND_ABORT("index inlined table detected but pr function "
884 "requires extra words");
885 return false;
886 }
887 }
888 } else {
889 pint_t personalityAddr =
890 exceptionTableAddr + signExtendPrel31(exceptionTableData);
891 personalityRoutine = personalityAddr;
892
893 // ARM EHABI # 6.2, # 9.2
894 //
895 // +---- ehtp
896 // v
897 // +--------------------------------------+
898 // | +--------+--------+--------+-------+ |
899 // | |0| prel31 to personalityRoutine | |
900 // | +--------+--------+--------+-------+ |
901 // | | N | unwind opcodes | | <-- UnwindData
902 // | +--------+--------+--------+-------+ |
903 // | | Word 2 unwind opcodes | |
904 // | +--------+--------+--------+-------+ |
905 // | ... |
906 // | +--------+--------+--------+-------+ |
907 // | | Word N unwind opcodes | |
908 // | +--------+--------+--------+-------+ |
909 // | | LSDA | | <-- lsda
910 // | | ... | |
911 // | +--------+--------+--------+-------+ |
912 // +--------------------------------------+
913
914 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
915 uint32_t FirstDataWord = *UnwindData;
916 size_t N = ((FirstDataWord >> 24) & 0xff);
917 size_t NDataWords = N + 1;
918 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
919 }
920
921 _info.start_ip = thisPC;
922 _info.end_ip = nextPC;
923 _info.handler = personalityRoutine;
924 _info.unwind_info = exceptionTableAddr;
925 _info.lsda = lsda;
926 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
927 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum?
928
929 return true;
930}
931#endif
932
Ranjeet Singh421231a2017-03-31 15:28:06 +0000933#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000934template <typename A, typename R>
935bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
936 const UnwindInfoSections &sects,
937 uint32_t fdeSectionOffsetHint) {
938 typename CFI_Parser<A>::FDE_Info fdeInfo;
939 typename CFI_Parser<A>::CIE_Info cieInfo;
940 bool foundFDE = false;
941 bool foundInCache = false;
942 // If compact encoding table gave offset into dwarf section, go directly there
943 if (fdeSectionOffsetHint != 0) {
944 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
945 (uint32_t)sects.dwarf_section_length,
946 sects.dwarf_section + fdeSectionOffsetHint,
947 &fdeInfo, &cieInfo);
948 }
Ranjeet Singh421231a2017-03-31 15:28:06 +0000949#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000950 if (!foundFDE && (sects.dwarf_index_section != 0)) {
951 foundFDE = EHHeaderParser<A>::findFDE(
952 _addressSpace, pc, sects.dwarf_index_section,
953 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
954 }
955#endif
956 if (!foundFDE) {
957 // otherwise, search cache of previously found FDEs.
958 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
959 if (cachedFDE != 0) {
960 foundFDE =
961 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
962 (uint32_t)sects.dwarf_section_length,
963 cachedFDE, &fdeInfo, &cieInfo);
964 foundInCache = foundFDE;
965 }
966 }
967 if (!foundFDE) {
968 // Still not found, do full scan of __eh_frame section.
969 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
970 (uint32_t)sects.dwarf_section_length, 0,
971 &fdeInfo, &cieInfo);
972 }
973 if (foundFDE) {
974 typename CFI_Parser<A>::PrologInfo prolog;
975 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
976 &prolog)) {
977 // Save off parsed FDE info
978 _info.start_ip = fdeInfo.pcStart;
979 _info.end_ip = fdeInfo.pcEnd;
980 _info.lsda = fdeInfo.lsda;
981 _info.handler = cieInfo.personality;
982 _info.gp = prolog.spExtraArgSize;
983 _info.flags = 0;
984 _info.format = dwarfEncoding();
985 _info.unwind_info = fdeInfo.fdeStart;
986 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
987 _info.extra = (unw_word_t) sects.dso_base;
988
989 // Add to cache (to make next lookup faster) if we had no hint
990 // and there was no index.
991 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000992 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000993 if (sects.dwarf_index_section == 0)
994 #endif
995 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
996 fdeInfo.fdeStart);
997 }
998 return true;
999 }
1000 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001001 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001002 return false;
1003}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001004#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001005
1006
Ranjeet Singh421231a2017-03-31 15:28:06 +00001007#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001008template <typename A, typename R>
1009bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1010 const UnwindInfoSections &sects) {
1011 const bool log = false;
1012 if (log)
1013 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1014 (uint64_t)pc, (uint64_t)sects.dso_base);
1015
1016 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1017 sects.compact_unwind_section);
1018 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1019 return false;
1020
1021 // do a binary search of top level index to find page with unwind info
1022 pint_t targetFunctionOffset = pc - sects.dso_base;
1023 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1024 sects.compact_unwind_section
1025 + sectionHeader.indexSectionOffset());
1026 uint32_t low = 0;
1027 uint32_t high = sectionHeader.indexCount();
1028 uint32_t last = high - 1;
1029 while (low < high) {
1030 uint32_t mid = (low + high) / 2;
1031 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1032 //mid, low, high, topIndex.functionOffset(mid));
1033 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1034 if ((mid == last) ||
1035 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1036 low = mid;
1037 break;
1038 } else {
1039 low = mid + 1;
1040 }
1041 } else {
1042 high = mid;
1043 }
1044 }
1045 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1046 const uint32_t firstLevelNextPageFunctionOffset =
1047 topIndex.functionOffset(low + 1);
1048 const pint_t secondLevelAddr =
1049 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1050 const pint_t lsdaArrayStartAddr =
1051 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1052 const pint_t lsdaArrayEndAddr =
1053 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1054 if (log)
1055 fprintf(stderr, "\tfirst level search for result index=%d "
1056 "to secondLevelAddr=0x%llX\n",
1057 low, (uint64_t) secondLevelAddr);
1058 // do a binary search of second level page index
1059 uint32_t encoding = 0;
1060 pint_t funcStart = 0;
1061 pint_t funcEnd = 0;
1062 pint_t lsda = 0;
1063 pint_t personality = 0;
1064 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1065 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1066 // regular page
1067 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1068 secondLevelAddr);
1069 UnwindSectionRegularArray<A> pageIndex(
1070 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1071 // binary search looks for entry with e where index[e].offset <= pc <
1072 // index[e+1].offset
1073 if (log)
1074 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1075 "regular page starting at secondLevelAddr=0x%llX\n",
1076 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1077 low = 0;
1078 high = pageHeader.entryCount();
1079 while (low < high) {
1080 uint32_t mid = (low + high) / 2;
1081 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1082 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1083 // at end of table
1084 low = mid;
1085 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1086 break;
1087 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1088 // next is too big, so we found it
1089 low = mid;
1090 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1091 break;
1092 } else {
1093 low = mid + 1;
1094 }
1095 } else {
1096 high = mid;
1097 }
1098 }
1099 encoding = pageIndex.encoding(low);
1100 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1101 if (pc < funcStart) {
1102 if (log)
1103 fprintf(
1104 stderr,
1105 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1106 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1107 return false;
1108 }
1109 if (pc > funcEnd) {
1110 if (log)
1111 fprintf(
1112 stderr,
1113 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1114 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1115 return false;
1116 }
1117 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1118 // compressed page
1119 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1120 secondLevelAddr);
1121 UnwindSectionCompressedArray<A> pageIndex(
1122 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1123 const uint32_t targetFunctionPageOffset =
1124 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1125 // binary search looks for entry with e where index[e].offset <= pc <
1126 // index[e+1].offset
1127 if (log)
1128 fprintf(stderr, "\tbinary search of compressed page starting at "
1129 "secondLevelAddr=0x%llX\n",
1130 (uint64_t) secondLevelAddr);
1131 low = 0;
1132 last = pageHeader.entryCount() - 1;
1133 high = pageHeader.entryCount();
1134 while (low < high) {
1135 uint32_t mid = (low + high) / 2;
1136 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1137 if ((mid == last) ||
1138 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1139 low = mid;
1140 break;
1141 } else {
1142 low = mid + 1;
1143 }
1144 } else {
1145 high = mid;
1146 }
1147 }
1148 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1149 + sects.dso_base;
1150 if (low < last)
1151 funcEnd =
1152 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1153 + sects.dso_base;
1154 else
1155 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1156 if (pc < funcStart) {
1157 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001158 "level compressed unwind table. funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001159 (uint64_t) pc, (uint64_t) funcStart);
1160 return false;
1161 }
1162 if (pc > funcEnd) {
1163 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001164 "level compressed unwind table. funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001165 (uint64_t) pc, (uint64_t) funcEnd);
1166 return false;
1167 }
1168 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1169 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1170 // encoding is in common table in section header
1171 encoding = _addressSpace.get32(
1172 sects.compact_unwind_section +
1173 sectionHeader.commonEncodingsArraySectionOffset() +
1174 encodingIndex * sizeof(uint32_t));
1175 } else {
1176 // encoding is in page specific table
1177 uint16_t pageEncodingIndex =
1178 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1179 encoding = _addressSpace.get32(secondLevelAddr +
1180 pageHeader.encodingsPageOffset() +
1181 pageEncodingIndex * sizeof(uint32_t));
1182 }
1183 } else {
1184 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001185 "level page",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001186 (uint64_t) sects.compact_unwind_section);
1187 return false;
1188 }
1189
1190 // look up LSDA, if encoding says function has one
1191 if (encoding & UNWIND_HAS_LSDA) {
1192 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1193 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1194 low = 0;
1195 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1196 sizeof(unwind_info_section_header_lsda_index_entry);
1197 // binary search looks for entry with exact match for functionOffset
1198 if (log)
1199 fprintf(stderr,
1200 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1201 funcStartOffset);
1202 while (low < high) {
1203 uint32_t mid = (low + high) / 2;
1204 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1205 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1206 break;
1207 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1208 low = mid + 1;
1209 } else {
1210 high = mid;
1211 }
1212 }
1213 if (lsda == 0) {
1214 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001215 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001216 encoding, (uint64_t) pc);
1217 return false;
1218 }
1219 }
1220
1221 // extact personality routine, if encoding says function has one
1222 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1223 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1224 if (personalityIndex != 0) {
1225 --personalityIndex; // change 1-based to zero-based index
1226 if (personalityIndex > sectionHeader.personalityArrayCount()) {
1227 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +00001228 "but personality table has only %d entires",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001229 encoding, personalityIndex,
1230 sectionHeader.personalityArrayCount());
1231 return false;
1232 }
1233 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1234 sects.compact_unwind_section +
1235 sectionHeader.personalityArraySectionOffset() +
1236 personalityIndex * sizeof(uint32_t));
1237 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1238 personality = _addressSpace.getP(personalityPointer);
1239 if (log)
1240 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1241 "personalityDelta=0x%08X, personality=0x%08llX\n",
1242 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1243 }
1244
1245 if (log)
1246 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1247 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1248 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1249 _info.start_ip = funcStart;
1250 _info.end_ip = funcEnd;
1251 _info.lsda = lsda;
1252 _info.handler = personality;
1253 _info.gp = 0;
1254 _info.flags = 0;
1255 _info.format = encoding;
1256 _info.unwind_info = 0;
1257 _info.unwind_info_size = 0;
1258 _info.extra = sects.dso_base;
1259 return true;
1260}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001261#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001262
1263
1264template <typename A, typename R>
1265void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1266 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Ranjeet Singh421231a2017-03-31 15:28:06 +00001267#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001268 // Remove the thumb bit so the IP represents the actual instruction address.
1269 // This matches the behaviour of _Unwind_GetIP on arm.
1270 pc &= (pint_t)~0x1;
1271#endif
1272
1273 // If the last line of a function is a "throw" the compiler sometimes
1274 // emits no instructions after the call to __cxa_throw. This means
1275 // the return address is actually the start of the next function.
1276 // To disambiguate this, back up the pc when we know it is a return
1277 // address.
1278 if (isReturnAddress)
1279 --pc;
1280
1281 // Ask address space object to find unwind sections for this pc.
1282 UnwindInfoSections sects;
1283 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001284#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001285 // If there is a compact unwind encoding table, look there first.
1286 if (sects.compact_unwind_section != 0) {
1287 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001288 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001289 // Found info in table, done unless encoding says to use dwarf.
1290 uint32_t dwarfOffset;
1291 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1292 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1293 // found info in dwarf, done
1294 return;
1295 }
1296 }
1297 #endif
1298 // If unwind table has entry, but entry says there is no unwind info,
1299 // record that we have no unwind info.
1300 if (_info.format == 0)
1301 _unwindInfoMissing = true;
1302 return;
1303 }
1304 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001305#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001306
Ranjeet Singh421231a2017-03-31 15:28:06 +00001307#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001308 // If there is dwarf unwind info, look there next.
1309 if (sects.dwarf_section != 0) {
1310 if (this->getInfoFromDwarfSection(pc, sects)) {
1311 // found info in dwarf, done
1312 return;
1313 }
1314 }
1315#endif
1316
Ranjeet Singh421231a2017-03-31 15:28:06 +00001317#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001318 // If there is ARM EHABI unwind info, look there next.
1319 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1320 return;
1321#endif
1322 }
1323
Ranjeet Singh421231a2017-03-31 15:28:06 +00001324#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001325 // There is no static unwind info for this pc. Look to see if an FDE was
1326 // dynamically registered for it.
1327 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1328 if (cachedFDE != 0) {
1329 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1330 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1331 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1332 cachedFDE, &fdeInfo, &cieInfo);
1333 if (msg == NULL) {
1334 typename CFI_Parser<A>::PrologInfo prolog;
1335 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1336 pc, &prolog)) {
1337 // save off parsed FDE info
1338 _info.start_ip = fdeInfo.pcStart;
1339 _info.end_ip = fdeInfo.pcEnd;
1340 _info.lsda = fdeInfo.lsda;
1341 _info.handler = cieInfo.personality;
1342 _info.gp = prolog.spExtraArgSize;
1343 // Some frameless functions need SP
1344 // altered when resuming in function.
1345 _info.flags = 0;
1346 _info.format = dwarfEncoding();
1347 _info.unwind_info = fdeInfo.fdeStart;
1348 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1349 _info.extra = 0;
1350 return;
1351 }
1352 }
1353 }
1354
1355 // Lastly, ask AddressSpace object about platform specific ways to locate
1356 // other FDEs.
1357 pint_t fde;
1358 if (_addressSpace.findOtherFDE(pc, fde)) {
1359 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1360 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1361 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1362 // Double check this FDE is for a function that includes the pc.
1363 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1364 typename CFI_Parser<A>::PrologInfo prolog;
1365 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo,
1366 cieInfo, pc, &prolog)) {
1367 // save off parsed FDE info
1368 _info.start_ip = fdeInfo.pcStart;
1369 _info.end_ip = fdeInfo.pcEnd;
1370 _info.lsda = fdeInfo.lsda;
1371 _info.handler = cieInfo.personality;
1372 _info.gp = prolog.spExtraArgSize;
1373 _info.flags = 0;
1374 _info.format = dwarfEncoding();
1375 _info.unwind_info = fdeInfo.fdeStart;
1376 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1377 _info.extra = 0;
1378 return;
1379 }
1380 }
1381 }
1382 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001383#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001384
1385 // no unwind info, flag that we can't reliably unwind
1386 _unwindInfoMissing = true;
1387}
1388
1389template <typename A, typename R>
1390int UnwindCursor<A, R>::step() {
1391 // Bottom of stack is defined is when unwind info cannot be found.
1392 if (_unwindInfoMissing)
1393 return UNW_STEP_END;
1394
1395 // Use unwinding info to modify register set as if function returned.
1396 int result;
Ranjeet Singh421231a2017-03-31 15:28:06 +00001397#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001398 result = this->stepWithCompactEncoding();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001399#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001400 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001401#elif defined(_LIBUNWIND_ARM_EHABI)
Logan Chiena54f0962015-05-29 15:33:38 +00001402 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001403#else
1404 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
1405 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00001406 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001407#endif
1408
1409 // update info based on new PC
1410 if (result == UNW_STEP_SUCCESS) {
1411 this->setInfoBasedOnIPRegister(true);
1412 if (_unwindInfoMissing)
1413 return UNW_STEP_END;
1414 if (_info.gp)
1415 setReg(UNW_REG_SP, getReg(UNW_REG_SP) + _info.gp);
1416 }
1417
1418 return result;
1419}
1420
1421template <typename A, typename R>
1422void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
1423 *info = _info;
1424}
1425
1426template <typename A, typename R>
1427bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
1428 unw_word_t *offset) {
1429 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
1430 buf, bufLen, offset);
1431}
1432
1433} // namespace libunwind
1434
1435#endif // __UNWINDCURSOR_HPP__