blob: 040d13e9256378037b02e562b4a511806f06cc94 [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//
9// C++ interface to lower levels of libuwind
10//===----------------------------------------------------------------------===//
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>
19#include <pthread.h>
20#include <unwind.h>
21
22#ifdef __APPLE__
23 #include <mach-o/dyld.h>
24#endif
25
26#include "config.h"
27
28#include "AddressSpace.hpp"
29#include "CompactUnwinder.hpp"
30#include "config.h"
31#include "DwarfInstructions.hpp"
32#include "EHHeaderParser.hpp"
33#include "libunwind.h"
34#include "Registers.hpp"
35#include "Unwind-EHABI.h"
36
37namespace libunwind {
38
39#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
40/// 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.
63 static pthread_rwlock_t _lock;
64#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>
91pthread_rwlock_t DwarfFDECache<A>::_lock = PTHREAD_RWLOCK_INITIALIZER;
92
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;
101 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_rdlock(&_lock));
102 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 }
110 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
111 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) {
117 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock));
118 if (_bufferUsed >= _bufferEnd) {
119 size_t oldSize = (size_t)(_bufferEnd - _buffer);
120 size_t newSize = oldSize * 4;
121 // Can't use operator new (we are below it).
122 entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
123 memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
124 if (_buffer != _initialBuffer)
125 free(_buffer);
126 _buffer = newBuffer;
127 _bufferUsed = &newBuffer[oldSize];
128 _bufferEnd = &newBuffer[newSize];
129 }
130 _bufferUsed->mh = mh;
131 _bufferUsed->ip_start = ip_start;
132 _bufferUsed->ip_end = ip_end;
133 _bufferUsed->fde = fde;
134 ++_bufferUsed;
135#ifdef __APPLE__
136 if (!_registeredForDyldUnloads) {
137 _dyld_register_func_for_remove_image(&dyldUnloadHook);
138 _registeredForDyldUnloads = true;
139 }
140#endif
141 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
142}
143
144template <typename A>
145void DwarfFDECache<A>::removeAllIn(pint_t mh) {
146 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock));
147 entry *d = _buffer;
148 for (const entry *s = _buffer; s < _bufferUsed; ++s) {
149 if (s->mh != mh) {
150 if (d != s)
151 *d = *s;
152 ++d;
153 }
154 }
155 _bufferUsed = d;
156 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
157}
158
159#ifdef __APPLE__
160template <typename A>
161void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
162 removeAllIn((pint_t) mh);
163}
164#endif
165
166template <typename A>
167void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
168 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
169 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock));
170 for (entry *p = _buffer; p < _bufferUsed; ++p) {
171 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
172 }
173 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
174}
175#endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
176
177
178#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
179
180#if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
181template <typename A> class UnwindSectionHeader {
182public:
183 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
184 : _addressSpace(addressSpace), _addr(addr) {}
185
186 uint32_t version() const {
187 return _addressSpace.get32(_addr +
188 offsetof(unwind_info_section_header, version));
189 }
190 uint32_t commonEncodingsArraySectionOffset() const {
191 return _addressSpace.get32(_addr +
192 offsetof(unwind_info_section_header,
193 commonEncodingsArraySectionOffset));
194 }
195 uint32_t commonEncodingsArrayCount() const {
196 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
197 commonEncodingsArrayCount));
198 }
199 uint32_t personalityArraySectionOffset() const {
200 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
201 personalityArraySectionOffset));
202 }
203 uint32_t personalityArrayCount() const {
204 return _addressSpace.get32(
205 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
206 }
207 uint32_t indexSectionOffset() const {
208 return _addressSpace.get32(
209 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
210 }
211 uint32_t indexCount() const {
212 return _addressSpace.get32(
213 _addr + offsetof(unwind_info_section_header, indexCount));
214 }
215
216private:
217 A &_addressSpace;
218 typename A::pint_t _addr;
219};
220
221template <typename A> class UnwindSectionIndexArray {
222public:
223 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
224 : _addressSpace(addressSpace), _addr(addr) {}
225
226 uint32_t functionOffset(uint32_t index) const {
227 return _addressSpace.get32(
228 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
229 functionOffset));
230 }
231 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
232 return _addressSpace.get32(
233 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
234 secondLevelPagesSectionOffset));
235 }
236 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
237 return _addressSpace.get32(
238 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
239 lsdaIndexArraySectionOffset));
240 }
241
242private:
243 A &_addressSpace;
244 typename A::pint_t _addr;
245};
246
247template <typename A> class UnwindSectionRegularPageHeader {
248public:
249 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
250 : _addressSpace(addressSpace), _addr(addr) {}
251
252 uint32_t kind() const {
253 return _addressSpace.get32(
254 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
255 }
256 uint16_t entryPageOffset() const {
257 return _addressSpace.get16(
258 _addr + offsetof(unwind_info_regular_second_level_page_header,
259 entryPageOffset));
260 }
261 uint16_t entryCount() const {
262 return _addressSpace.get16(
263 _addr +
264 offsetof(unwind_info_regular_second_level_page_header, entryCount));
265 }
266
267private:
268 A &_addressSpace;
269 typename A::pint_t _addr;
270};
271
272template <typename A> class UnwindSectionRegularArray {
273public:
274 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
275 : _addressSpace(addressSpace), _addr(addr) {}
276
277 uint32_t functionOffset(uint32_t index) const {
278 return _addressSpace.get32(
279 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
280 functionOffset));
281 }
282 uint32_t encoding(uint32_t index) const {
283 return _addressSpace.get32(
284 _addr +
285 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
286 }
287
288private:
289 A &_addressSpace;
290 typename A::pint_t _addr;
291};
292
293template <typename A> class UnwindSectionCompressedPageHeader {
294public:
295 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
296 : _addressSpace(addressSpace), _addr(addr) {}
297
298 uint32_t kind() const {
299 return _addressSpace.get32(
300 _addr +
301 offsetof(unwind_info_compressed_second_level_page_header, kind));
302 }
303 uint16_t entryPageOffset() const {
304 return _addressSpace.get16(
305 _addr + offsetof(unwind_info_compressed_second_level_page_header,
306 entryPageOffset));
307 }
308 uint16_t entryCount() const {
309 return _addressSpace.get16(
310 _addr +
311 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
312 }
313 uint16_t encodingsPageOffset() const {
314 return _addressSpace.get16(
315 _addr + offsetof(unwind_info_compressed_second_level_page_header,
316 encodingsPageOffset));
317 }
318 uint16_t encodingsCount() const {
319 return _addressSpace.get16(
320 _addr + offsetof(unwind_info_compressed_second_level_page_header,
321 encodingsCount));
322 }
323
324private:
325 A &_addressSpace;
326 typename A::pint_t _addr;
327};
328
329template <typename A> class UnwindSectionCompressedArray {
330public:
331 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
332 : _addressSpace(addressSpace), _addr(addr) {}
333
334 uint32_t functionOffset(uint32_t index) const {
335 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
336 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
337 }
338 uint16_t encodingIndex(uint32_t index) const {
339 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
340 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
341 }
342
343private:
344 A &_addressSpace;
345 typename A::pint_t _addr;
346};
347
348template <typename A> class UnwindSectionLsdaArray {
349public:
350 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
351 : _addressSpace(addressSpace), _addr(addr) {}
352
353 uint32_t functionOffset(uint32_t index) const {
354 return _addressSpace.get32(
355 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
356 index, functionOffset));
357 }
358 uint32_t lsdaOffset(uint32_t index) const {
359 return _addressSpace.get32(
360 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
361 index, lsdaOffset));
362 }
363
364private:
365 A &_addressSpace;
366 typename A::pint_t _addr;
367};
368#endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
369
370class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
371public:
372 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
373 // This avoids an unnecessary dependency to libc++abi.
374 void operator delete(void *, size_t) {}
375
376 virtual ~AbstractUnwindCursor() {}
377 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
378 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
379 virtual void setReg(int, unw_word_t) {
380 _LIBUNWIND_ABORT("setReg not implemented");
381 }
382 virtual bool validFloatReg(int) {
383 _LIBUNWIND_ABORT("validFloatReg not implemented");
384 }
385 virtual unw_fpreg_t getFloatReg(int) {
386 _LIBUNWIND_ABORT("getFloatReg not implemented");
387 }
388 virtual void setFloatReg(int, unw_fpreg_t) {
389 _LIBUNWIND_ABORT("setFloatReg not implemented");
390 }
391 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
392 virtual void getInfo(unw_proc_info_t *) {
393 _LIBUNWIND_ABORT("getInfo not implemented");
394 }
395 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
396 virtual bool isSignalFrame() {
397 _LIBUNWIND_ABORT("isSignalFrame not implemented");
398 }
399 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
400 _LIBUNWIND_ABORT("getFunctionName not implemented");
401 }
402 virtual void setInfoBasedOnIPRegister(bool = false) {
403 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
404 }
405 virtual const char *getRegisterName(int) {
406 _LIBUNWIND_ABORT("getRegisterName not implemented");
407 }
408#ifdef __arm__
409 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
410#endif
411};
412
413/// UnwindCursor contains all state (including all register values) during
414/// an unwind. This is normally stack allocated inside a unw_cursor_t.
415template <typename A, typename R>
416class UnwindCursor : public AbstractUnwindCursor{
417 typedef typename A::pint_t pint_t;
418public:
419 UnwindCursor(unw_context_t *context, A &as);
420 UnwindCursor(A &as, void *threadArg);
421 virtual ~UnwindCursor() {}
422 virtual bool validReg(int);
423 virtual unw_word_t getReg(int);
424 virtual void setReg(int, unw_word_t);
425 virtual bool validFloatReg(int);
426 virtual unw_fpreg_t getFloatReg(int);
427 virtual void setFloatReg(int, unw_fpreg_t);
428 virtual int step();
429 virtual void getInfo(unw_proc_info_t *);
430 virtual void jumpto();
431 virtual bool isSignalFrame();
432 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
433 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
434 virtual const char *getRegisterName(int num);
435#ifdef __arm__
436 virtual void saveVFPAsX();
437#endif
438
439private:
440
Logan Chien06b0c7a2015-07-19 15:23:10 +0000441#if _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000442 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000443
444 int stepWithEHABI() {
445 size_t len = 0;
446 size_t off = 0;
447 // FIXME: Calling decode_eht_entry() here is violating the libunwind
448 // abstraction layer.
449 const uint32_t *ehtp =
450 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
451 &off, &len);
452 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
453 _URC_CONTINUE_UNWIND)
454 return UNW_STEP_END;
455 return UNW_STEP_SUCCESS;
456 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000457#endif
458
459#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
460 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
461 uint32_t fdeSectionOffsetHint=0);
462 int stepWithDwarfFDE() {
463 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
464 (pint_t)this->getReg(UNW_REG_IP),
465 (pint_t)_info.unwind_info,
466 _registers);
467 }
468#endif
469
470#if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
471 bool getInfoFromCompactEncodingSection(pint_t pc,
472 const UnwindInfoSections &sects);
473 int stepWithCompactEncoding() {
474 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
475 if ( compactSaysUseDwarf() )
476 return stepWithDwarfFDE();
477 #endif
478 R dummy;
479 return stepWithCompactEncoding(dummy);
480 }
481
482 int stepWithCompactEncoding(Registers_x86_64 &) {
483 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
484 _info.format, _info.start_ip, _addressSpace, _registers);
485 }
486
487 int stepWithCompactEncoding(Registers_x86 &) {
488 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
489 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
490 }
491
492 int stepWithCompactEncoding(Registers_ppc &) {
493 return UNW_EINVAL;
494 }
495
496 int stepWithCompactEncoding(Registers_arm64 &) {
497 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
498 _info.format, _info.start_ip, _addressSpace, _registers);
499 }
500
501 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
502 R dummy;
503 return compactSaysUseDwarf(dummy, offset);
504 }
505
506 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
507 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
508 if (offset)
509 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
510 return true;
511 }
512 return false;
513 }
514
515 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
516 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
517 if (offset)
518 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
519 return true;
520 }
521 return false;
522 }
523
524 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
525 return true;
526 }
527
528 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
529 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
530 if (offset)
531 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
532 return true;
533 }
534 return false;
535 }
536#endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
537
538#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
539 compact_unwind_encoding_t dwarfEncoding() const {
540 R dummy;
541 return dwarfEncoding(dummy);
542 }
543
544 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
545 return UNWIND_X86_64_MODE_DWARF;
546 }
547
548 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
549 return UNWIND_X86_MODE_DWARF;
550 }
551
552 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
553 return 0;
554 }
555
556 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
557 return UNWIND_ARM64_MODE_DWARF;
558 }
Peter Zotov8d639992015-08-31 05:26:37 +0000559
560 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
561 return 0;
562 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000563#endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
564
565
566 A &_addressSpace;
567 R _registers;
568 unw_proc_info_t _info;
569 bool _unwindInfoMissing;
570 bool _isSignalFrame;
571};
572
573
574template <typename A, typename R>
575UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
576 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
577 _isSignalFrame(false) {
578 static_assert(sizeof(UnwindCursor<A, R>) < sizeof(unw_cursor_t),
579 "UnwindCursor<> does not fit in unw_cursor_t");
580 memset(&_info, 0, sizeof(_info));
581}
582
583template <typename A, typename R>
584UnwindCursor<A, R>::UnwindCursor(A &as, void *)
585 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
586 memset(&_info, 0, sizeof(_info));
587 // FIXME
588 // fill in _registers from thread arg
589}
590
591
592template <typename A, typename R>
593bool UnwindCursor<A, R>::validReg(int regNum) {
594 return _registers.validRegister(regNum);
595}
596
597template <typename A, typename R>
598unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
599 return _registers.getRegister(regNum);
600}
601
602template <typename A, typename R>
603void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
604 _registers.setRegister(regNum, (typename A::pint_t)value);
605}
606
607template <typename A, typename R>
608bool UnwindCursor<A, R>::validFloatReg(int regNum) {
609 return _registers.validFloatRegister(regNum);
610}
611
612template <typename A, typename R>
613unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
614 return _registers.getFloatRegister(regNum);
615}
616
617template <typename A, typename R>
618void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
619 _registers.setFloatRegister(regNum, value);
620}
621
622template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
623 _registers.jumpto();
624}
625
626#ifdef __arm__
627template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
628 _registers.saveVFPAsX();
629}
630#endif
631
632template <typename A, typename R>
633const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
634 return _registers.getRegisterName(regNum);
635}
636
637template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
638 return _isSignalFrame;
639}
640
Logan Chien06b0c7a2015-07-19 15:23:10 +0000641#if _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000642struct EHABIIndexEntry {
643 uint32_t functionOffset;
644 uint32_t data;
645};
646
647template<typename A>
648struct EHABISectionIterator {
649 typedef EHABISectionIterator _Self;
650
651 typedef std::random_access_iterator_tag iterator_category;
652 typedef typename A::pint_t value_type;
653 typedef typename A::pint_t* pointer;
654 typedef typename A::pint_t& reference;
655 typedef size_t size_type;
656 typedef size_t difference_type;
657
658 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
659 return _Self(addressSpace, sects, 0);
660 }
661 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
662 return _Self(addressSpace, sects, sects.arm_section_length);
663 }
664
665 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
666 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
667
668 _Self& operator++() { ++_i; return *this; }
669 _Self& operator+=(size_t a) { _i += a; return *this; }
670 _Self& operator--() { assert(_i > 0); --_i; return *this; }
671 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
672
673 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
674 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
675
676 size_t operator-(const _Self& other) { return _i - other._i; }
677
678 bool operator==(const _Self& other) const {
679 assert(_addressSpace == other._addressSpace);
680 assert(_sects == other._sects);
681 return _i == other._i;
682 }
683
684 typename A::pint_t operator*() const { return functionAddress(); }
685
686 typename A::pint_t functionAddress() const {
687 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
688 EHABIIndexEntry, _i, functionOffset);
689 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
690 }
691
692 typename A::pint_t dataAddress() {
693 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
694 EHABIIndexEntry, _i, data);
695 return indexAddr;
696 }
697
698 private:
699 size_t _i;
700 A* _addressSpace;
701 const UnwindInfoSections* _sects;
702};
703
704template <typename A, typename R>
705bool UnwindCursor<A, R>::getInfoFromEHABISection(
706 pint_t pc,
707 const UnwindInfoSections &sects) {
708 EHABISectionIterator<A> begin =
709 EHABISectionIterator<A>::begin(_addressSpace, sects);
710 EHABISectionIterator<A> end =
711 EHABISectionIterator<A>::end(_addressSpace, sects);
712
713 EHABISectionIterator<A> itNextPC = std::upper_bound(begin, end, pc);
714 if (itNextPC == begin || itNextPC == end)
715 return false;
716 EHABISectionIterator<A> itThisPC = itNextPC - 1;
717
718 pint_t thisPC = itThisPC.functionAddress();
719 pint_t nextPC = itNextPC.functionAddress();
720 pint_t indexDataAddr = itThisPC.dataAddress();
721
722 if (indexDataAddr == 0)
723 return false;
724
725 uint32_t indexData = _addressSpace.get32(indexDataAddr);
726 if (indexData == UNW_EXIDX_CANTUNWIND)
727 return false;
728
729 // If the high bit is set, the exception handling table entry is inline inside
730 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
731 // the table points at an offset in the exception handling table (section 5 EHABI).
732 pint_t exceptionTableAddr;
733 uint32_t exceptionTableData;
734 bool isSingleWordEHT;
735 if (indexData & 0x80000000) {
736 exceptionTableAddr = indexDataAddr;
737 // TODO(ajwong): Should this data be 0?
738 exceptionTableData = indexData;
739 isSingleWordEHT = true;
740 } else {
741 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
742 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
743 isSingleWordEHT = false;
744 }
745
746 // Now we know the 3 things:
747 // exceptionTableAddr -- exception handler table entry.
748 // exceptionTableData -- the data inside the first word of the eht entry.
749 // isSingleWordEHT -- whether the entry is in the index.
750 unw_word_t personalityRoutine = 0xbadf00d;
751 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +0000752 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000753
754 // If the high bit in the exception handling table entry is set, the entry is
755 // in compact form (section 6.3 EHABI).
756 if (exceptionTableData & 0x80000000) {
757 // Grab the index of the personality routine from the compact form.
758 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
759 uint32_t extraWords = 0;
760 switch (choice) {
761 case 0:
762 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
763 extraWords = 0;
764 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +0000765 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000766 break;
767 case 1:
768 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
769 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
770 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +0000771 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000772 break;
773 case 2:
774 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
775 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
776 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +0000777 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000778 break;
779 default:
780 _LIBUNWIND_ABORT("unknown personality routine");
781 return false;
782 }
783
784 if (isSingleWordEHT) {
785 if (extraWords != 0) {
786 _LIBUNWIND_ABORT("index inlined table detected but pr function "
787 "requires extra words");
788 return false;
789 }
790 }
791 } else {
792 pint_t personalityAddr =
793 exceptionTableAddr + signExtendPrel31(exceptionTableData);
794 personalityRoutine = personalityAddr;
795
796 // ARM EHABI # 6.2, # 9.2
797 //
798 // +---- ehtp
799 // v
800 // +--------------------------------------+
801 // | +--------+--------+--------+-------+ |
802 // | |0| prel31 to personalityRoutine | |
803 // | +--------+--------+--------+-------+ |
804 // | | N | unwind opcodes | | <-- UnwindData
805 // | +--------+--------+--------+-------+ |
806 // | | Word 2 unwind opcodes | |
807 // | +--------+--------+--------+-------+ |
808 // | ... |
809 // | +--------+--------+--------+-------+ |
810 // | | Word N unwind opcodes | |
811 // | +--------+--------+--------+-------+ |
812 // | | LSDA | | <-- lsda
813 // | | ... | |
814 // | +--------+--------+--------+-------+ |
815 // +--------------------------------------+
816
817 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
818 uint32_t FirstDataWord = *UnwindData;
819 size_t N = ((FirstDataWord >> 24) & 0xff);
820 size_t NDataWords = N + 1;
821 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
822 }
823
824 _info.start_ip = thisPC;
825 _info.end_ip = nextPC;
826 _info.handler = personalityRoutine;
827 _info.unwind_info = exceptionTableAddr;
828 _info.lsda = lsda;
829 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
830 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum?
831
832 return true;
833}
834#endif
835
836#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
837template <typename A, typename R>
838bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
839 const UnwindInfoSections &sects,
840 uint32_t fdeSectionOffsetHint) {
841 typename CFI_Parser<A>::FDE_Info fdeInfo;
842 typename CFI_Parser<A>::CIE_Info cieInfo;
843 bool foundFDE = false;
844 bool foundInCache = false;
845 // If compact encoding table gave offset into dwarf section, go directly there
846 if (fdeSectionOffsetHint != 0) {
847 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
848 (uint32_t)sects.dwarf_section_length,
849 sects.dwarf_section + fdeSectionOffsetHint,
850 &fdeInfo, &cieInfo);
851 }
852#if _LIBUNWIND_SUPPORT_DWARF_INDEX
853 if (!foundFDE && (sects.dwarf_index_section != 0)) {
854 foundFDE = EHHeaderParser<A>::findFDE(
855 _addressSpace, pc, sects.dwarf_index_section,
856 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
857 }
858#endif
859 if (!foundFDE) {
860 // otherwise, search cache of previously found FDEs.
861 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
862 if (cachedFDE != 0) {
863 foundFDE =
864 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
865 (uint32_t)sects.dwarf_section_length,
866 cachedFDE, &fdeInfo, &cieInfo);
867 foundInCache = foundFDE;
868 }
869 }
870 if (!foundFDE) {
871 // Still not found, do full scan of __eh_frame section.
872 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
873 (uint32_t)sects.dwarf_section_length, 0,
874 &fdeInfo, &cieInfo);
875 }
876 if (foundFDE) {
877 typename CFI_Parser<A>::PrologInfo prolog;
878 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
879 &prolog)) {
880 // Save off parsed FDE info
881 _info.start_ip = fdeInfo.pcStart;
882 _info.end_ip = fdeInfo.pcEnd;
883 _info.lsda = fdeInfo.lsda;
884 _info.handler = cieInfo.personality;
885 _info.gp = prolog.spExtraArgSize;
886 _info.flags = 0;
887 _info.format = dwarfEncoding();
888 _info.unwind_info = fdeInfo.fdeStart;
889 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
890 _info.extra = (unw_word_t) sects.dso_base;
891
892 // Add to cache (to make next lookup faster) if we had no hint
893 // and there was no index.
894 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
895 #if _LIBUNWIND_SUPPORT_DWARF_INDEX
896 if (sects.dwarf_index_section == 0)
897 #endif
898 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
899 fdeInfo.fdeStart);
900 }
901 return true;
902 }
903 }
904 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX\n", (uint64_t)pc);
905 return false;
906}
907#endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
908
909
910#if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
911template <typename A, typename R>
912bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
913 const UnwindInfoSections &sects) {
914 const bool log = false;
915 if (log)
916 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
917 (uint64_t)pc, (uint64_t)sects.dso_base);
918
919 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
920 sects.compact_unwind_section);
921 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
922 return false;
923
924 // do a binary search of top level index to find page with unwind info
925 pint_t targetFunctionOffset = pc - sects.dso_base;
926 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
927 sects.compact_unwind_section
928 + sectionHeader.indexSectionOffset());
929 uint32_t low = 0;
930 uint32_t high = sectionHeader.indexCount();
931 uint32_t last = high - 1;
932 while (low < high) {
933 uint32_t mid = (low + high) / 2;
934 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
935 //mid, low, high, topIndex.functionOffset(mid));
936 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
937 if ((mid == last) ||
938 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
939 low = mid;
940 break;
941 } else {
942 low = mid + 1;
943 }
944 } else {
945 high = mid;
946 }
947 }
948 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
949 const uint32_t firstLevelNextPageFunctionOffset =
950 topIndex.functionOffset(low + 1);
951 const pint_t secondLevelAddr =
952 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
953 const pint_t lsdaArrayStartAddr =
954 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
955 const pint_t lsdaArrayEndAddr =
956 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
957 if (log)
958 fprintf(stderr, "\tfirst level search for result index=%d "
959 "to secondLevelAddr=0x%llX\n",
960 low, (uint64_t) secondLevelAddr);
961 // do a binary search of second level page index
962 uint32_t encoding = 0;
963 pint_t funcStart = 0;
964 pint_t funcEnd = 0;
965 pint_t lsda = 0;
966 pint_t personality = 0;
967 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
968 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
969 // regular page
970 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
971 secondLevelAddr);
972 UnwindSectionRegularArray<A> pageIndex(
973 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
974 // binary search looks for entry with e where index[e].offset <= pc <
975 // index[e+1].offset
976 if (log)
977 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
978 "regular page starting at secondLevelAddr=0x%llX\n",
979 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
980 low = 0;
981 high = pageHeader.entryCount();
982 while (low < high) {
983 uint32_t mid = (low + high) / 2;
984 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
985 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
986 // at end of table
987 low = mid;
988 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
989 break;
990 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
991 // next is too big, so we found it
992 low = mid;
993 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
994 break;
995 } else {
996 low = mid + 1;
997 }
998 } else {
999 high = mid;
1000 }
1001 }
1002 encoding = pageIndex.encoding(low);
1003 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1004 if (pc < funcStart) {
1005 if (log)
1006 fprintf(
1007 stderr,
1008 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1009 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1010 return false;
1011 }
1012 if (pc > funcEnd) {
1013 if (log)
1014 fprintf(
1015 stderr,
1016 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1017 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1018 return false;
1019 }
1020 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1021 // compressed page
1022 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1023 secondLevelAddr);
1024 UnwindSectionCompressedArray<A> pageIndex(
1025 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1026 const uint32_t targetFunctionPageOffset =
1027 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1028 // binary search looks for entry with e where index[e].offset <= pc <
1029 // index[e+1].offset
1030 if (log)
1031 fprintf(stderr, "\tbinary search of compressed page starting at "
1032 "secondLevelAddr=0x%llX\n",
1033 (uint64_t) secondLevelAddr);
1034 low = 0;
1035 last = pageHeader.entryCount() - 1;
1036 high = pageHeader.entryCount();
1037 while (low < high) {
1038 uint32_t mid = (low + high) / 2;
1039 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1040 if ((mid == last) ||
1041 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1042 low = mid;
1043 break;
1044 } else {
1045 low = mid + 1;
1046 }
1047 } else {
1048 high = mid;
1049 }
1050 }
1051 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1052 + sects.dso_base;
1053 if (low < last)
1054 funcEnd =
1055 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1056 + sects.dso_base;
1057 else
1058 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1059 if (pc < funcStart) {
1060 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
1061 "level compressed unwind table. funcStart=0x%llX\n",
1062 (uint64_t) pc, (uint64_t) funcStart);
1063 return false;
1064 }
1065 if (pc > funcEnd) {
1066 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
1067 "level compressed unwind table. funcEnd=0x%llX\n",
1068 (uint64_t) pc, (uint64_t) funcEnd);
1069 return false;
1070 }
1071 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1072 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1073 // encoding is in common table in section header
1074 encoding = _addressSpace.get32(
1075 sects.compact_unwind_section +
1076 sectionHeader.commonEncodingsArraySectionOffset() +
1077 encodingIndex * sizeof(uint32_t));
1078 } else {
1079 // encoding is in page specific table
1080 uint16_t pageEncodingIndex =
1081 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1082 encoding = _addressSpace.get32(secondLevelAddr +
1083 pageHeader.encodingsPageOffset() +
1084 pageEncodingIndex * sizeof(uint32_t));
1085 }
1086 } else {
1087 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
1088 "level page\n",
1089 (uint64_t) sects.compact_unwind_section);
1090 return false;
1091 }
1092
1093 // look up LSDA, if encoding says function has one
1094 if (encoding & UNWIND_HAS_LSDA) {
1095 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1096 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1097 low = 0;
1098 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1099 sizeof(unwind_info_section_header_lsda_index_entry);
1100 // binary search looks for entry with exact match for functionOffset
1101 if (log)
1102 fprintf(stderr,
1103 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1104 funcStartOffset);
1105 while (low < high) {
1106 uint32_t mid = (low + high) / 2;
1107 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1108 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1109 break;
1110 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1111 low = mid + 1;
1112 } else {
1113 high = mid;
1114 }
1115 }
1116 if (lsda == 0) {
1117 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
1118 "pc=0x%0llX, but lsda table has no entry\n",
1119 encoding, (uint64_t) pc);
1120 return false;
1121 }
1122 }
1123
1124 // extact personality routine, if encoding says function has one
1125 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1126 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1127 if (personalityIndex != 0) {
1128 --personalityIndex; // change 1-based to zero-based index
1129 if (personalityIndex > sectionHeader.personalityArrayCount()) {
1130 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
1131 "but personality table has only %d entires\n",
1132 encoding, personalityIndex,
1133 sectionHeader.personalityArrayCount());
1134 return false;
1135 }
1136 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1137 sects.compact_unwind_section +
1138 sectionHeader.personalityArraySectionOffset() +
1139 personalityIndex * sizeof(uint32_t));
1140 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1141 personality = _addressSpace.getP(personalityPointer);
1142 if (log)
1143 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1144 "personalityDelta=0x%08X, personality=0x%08llX\n",
1145 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1146 }
1147
1148 if (log)
1149 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1150 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1151 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1152 _info.start_ip = funcStart;
1153 _info.end_ip = funcEnd;
1154 _info.lsda = lsda;
1155 _info.handler = personality;
1156 _info.gp = 0;
1157 _info.flags = 0;
1158 _info.format = encoding;
1159 _info.unwind_info = 0;
1160 _info.unwind_info_size = 0;
1161 _info.extra = sects.dso_base;
1162 return true;
1163}
1164#endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1165
1166
1167template <typename A, typename R>
1168void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1169 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Logan Chien06b0c7a2015-07-19 15:23:10 +00001170#if _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001171 // Remove the thumb bit so the IP represents the actual instruction address.
1172 // This matches the behaviour of _Unwind_GetIP on arm.
1173 pc &= (pint_t)~0x1;
1174#endif
1175
1176 // If the last line of a function is a "throw" the compiler sometimes
1177 // emits no instructions after the call to __cxa_throw. This means
1178 // the return address is actually the start of the next function.
1179 // To disambiguate this, back up the pc when we know it is a return
1180 // address.
1181 if (isReturnAddress)
1182 --pc;
1183
1184 // Ask address space object to find unwind sections for this pc.
1185 UnwindInfoSections sects;
1186 if (_addressSpace.findUnwindSections(pc, sects)) {
1187#if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1188 // If there is a compact unwind encoding table, look there first.
1189 if (sects.compact_unwind_section != 0) {
1190 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
1191 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1192 // Found info in table, done unless encoding says to use dwarf.
1193 uint32_t dwarfOffset;
1194 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1195 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1196 // found info in dwarf, done
1197 return;
1198 }
1199 }
1200 #endif
1201 // If unwind table has entry, but entry says there is no unwind info,
1202 // record that we have no unwind info.
1203 if (_info.format == 0)
1204 _unwindInfoMissing = true;
1205 return;
1206 }
1207 }
1208#endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1209
1210#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1211 // If there is dwarf unwind info, look there next.
1212 if (sects.dwarf_section != 0) {
1213 if (this->getInfoFromDwarfSection(pc, sects)) {
1214 // found info in dwarf, done
1215 return;
1216 }
1217 }
1218#endif
1219
Logan Chien06b0c7a2015-07-19 15:23:10 +00001220#if _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001221 // If there is ARM EHABI unwind info, look there next.
1222 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1223 return;
1224#endif
1225 }
1226
1227#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1228 // There is no static unwind info for this pc. Look to see if an FDE was
1229 // dynamically registered for it.
1230 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1231 if (cachedFDE != 0) {
1232 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1233 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1234 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1235 cachedFDE, &fdeInfo, &cieInfo);
1236 if (msg == NULL) {
1237 typename CFI_Parser<A>::PrologInfo prolog;
1238 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1239 pc, &prolog)) {
1240 // save off parsed FDE info
1241 _info.start_ip = fdeInfo.pcStart;
1242 _info.end_ip = fdeInfo.pcEnd;
1243 _info.lsda = fdeInfo.lsda;
1244 _info.handler = cieInfo.personality;
1245 _info.gp = prolog.spExtraArgSize;
1246 // Some frameless functions need SP
1247 // altered when resuming in function.
1248 _info.flags = 0;
1249 _info.format = dwarfEncoding();
1250 _info.unwind_info = fdeInfo.fdeStart;
1251 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1252 _info.extra = 0;
1253 return;
1254 }
1255 }
1256 }
1257
1258 // Lastly, ask AddressSpace object about platform specific ways to locate
1259 // other FDEs.
1260 pint_t fde;
1261 if (_addressSpace.findOtherFDE(pc, fde)) {
1262 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1263 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1264 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1265 // Double check this FDE is for a function that includes the pc.
1266 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1267 typename CFI_Parser<A>::PrologInfo prolog;
1268 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo,
1269 cieInfo, pc, &prolog)) {
1270 // save off parsed FDE info
1271 _info.start_ip = fdeInfo.pcStart;
1272 _info.end_ip = fdeInfo.pcEnd;
1273 _info.lsda = fdeInfo.lsda;
1274 _info.handler = cieInfo.personality;
1275 _info.gp = prolog.spExtraArgSize;
1276 _info.flags = 0;
1277 _info.format = dwarfEncoding();
1278 _info.unwind_info = fdeInfo.fdeStart;
1279 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1280 _info.extra = 0;
1281 return;
1282 }
1283 }
1284 }
1285 }
1286#endif // #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1287
1288 // no unwind info, flag that we can't reliably unwind
1289 _unwindInfoMissing = true;
1290}
1291
1292template <typename A, typename R>
1293int UnwindCursor<A, R>::step() {
1294 // Bottom of stack is defined is when unwind info cannot be found.
1295 if (_unwindInfoMissing)
1296 return UNW_STEP_END;
1297
1298 // Use unwinding info to modify register set as if function returned.
1299 int result;
1300#if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1301 result = this->stepWithCompactEncoding();
1302#elif _LIBUNWIND_SUPPORT_DWARF_UNWIND
1303 result = this->stepWithDwarfFDE();
Logan Chien06b0c7a2015-07-19 15:23:10 +00001304#elif _LIBUNWIND_ARM_EHABI
Logan Chiena54f0962015-05-29 15:33:38 +00001305 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001306#else
1307 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
1308 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00001309 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001310#endif
1311
1312 // update info based on new PC
1313 if (result == UNW_STEP_SUCCESS) {
1314 this->setInfoBasedOnIPRegister(true);
1315 if (_unwindInfoMissing)
1316 return UNW_STEP_END;
1317 if (_info.gp)
1318 setReg(UNW_REG_SP, getReg(UNW_REG_SP) + _info.gp);
1319 }
1320
1321 return result;
1322}
1323
1324template <typename A, typename R>
1325void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
1326 *info = _info;
1327}
1328
1329template <typename A, typename R>
1330bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
1331 unw_word_t *offset) {
1332 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
1333 buf, bufLen, offset);
1334}
1335
1336} // namespace libunwind
1337
1338#endif // __UNWINDCURSOR_HPP__