1 //===------------------------- UnwindCursor.hpp ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
9 // C++ interface to lower levels of libunwind
10 //===----------------------------------------------------------------------===//
12 #ifndef __UNWINDCURSOR_HPP__
13 #define __UNWINDCURSOR_HPP__
23 #include <mach-o/dyld.h>
28 #include "AddressSpace.hpp"
29 #include "CompactUnwinder.hpp"
31 #include "DwarfInstructions.hpp"
32 #include "EHHeaderParser.hpp"
33 #include "libunwind.h"
34 #include "Registers.hpp"
35 #include "Unwind-EHABI.h"
39 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
40 /// Cache of recently found FDEs.
42 class _LIBUNWIND_HIDDEN DwarfFDECache {
43 typedef typename A::pint_t pint_t;
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,
50 unw_word_t fde, unw_word_t mh));
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;
65 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
66 static bool _registeredForDyldUnloads;
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];
76 typename DwarfFDECache<A>::entry *
77 DwarfFDECache<A>::_buffer = _initialBuffer;
80 typename DwarfFDECache<A>::entry *
81 DwarfFDECache<A>::_bufferUsed = _initialBuffer;
84 typename DwarfFDECache<A>::entry *
85 DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
88 typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
91 pthread_rwlock_t DwarfFDECache<A>::_lock = PTHREAD_RWLOCK_INITIALIZER;
95 bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
99 typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
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)) {
110 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
114 template <typename A>
115 void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
117 #if !defined(_LIBUNWIND_NO_HEAP)
118 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock));
119 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)
128 _bufferUsed = &newBuffer[oldSize];
129 _bufferEnd = &newBuffer[newSize];
131 _bufferUsed->mh = mh;
132 _bufferUsed->ip_start = ip_start;
133 _bufferUsed->ip_end = ip_end;
134 _bufferUsed->fde = fde;
137 if (!_registeredForDyldUnloads) {
138 _dyld_register_func_for_remove_image(&dyldUnloadHook);
139 _registeredForDyldUnloads = true;
142 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
146 template <typename A>
147 void DwarfFDECache<A>::removeAllIn(pint_t mh) {
148 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock));
150 for (const entry *s = _buffer; s < _bufferUsed; ++s) {
158 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
162 template <typename A>
163 void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
164 removeAllIn((pint_t) mh);
168 template <typename A>
169 void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
170 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
171 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock));
172 for (entry *p = _buffer; p < _bufferUsed; ++p) {
173 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
175 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
177 #endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
180 #define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
182 #if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
183 template <typename A> class UnwindSectionHeader {
185 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
186 : _addressSpace(addressSpace), _addr(addr) {}
188 uint32_t version() const {
189 return _addressSpace.get32(_addr +
190 offsetof(unwind_info_section_header, version));
192 uint32_t commonEncodingsArraySectionOffset() const {
193 return _addressSpace.get32(_addr +
194 offsetof(unwind_info_section_header,
195 commonEncodingsArraySectionOffset));
197 uint32_t commonEncodingsArrayCount() const {
198 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
199 commonEncodingsArrayCount));
201 uint32_t personalityArraySectionOffset() const {
202 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
203 personalityArraySectionOffset));
205 uint32_t personalityArrayCount() const {
206 return _addressSpace.get32(
207 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
209 uint32_t indexSectionOffset() const {
210 return _addressSpace.get32(
211 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
213 uint32_t indexCount() const {
214 return _addressSpace.get32(
215 _addr + offsetof(unwind_info_section_header, indexCount));
220 typename A::pint_t _addr;
223 template <typename A> class UnwindSectionIndexArray {
225 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
226 : _addressSpace(addressSpace), _addr(addr) {}
228 uint32_t functionOffset(uint32_t index) const {
229 return _addressSpace.get32(
230 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
233 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
234 return _addressSpace.get32(
235 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
236 secondLevelPagesSectionOffset));
238 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
239 return _addressSpace.get32(
240 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
241 lsdaIndexArraySectionOffset));
246 typename A::pint_t _addr;
249 template <typename A> class UnwindSectionRegularPageHeader {
251 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
252 : _addressSpace(addressSpace), _addr(addr) {}
254 uint32_t kind() const {
255 return _addressSpace.get32(
256 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
258 uint16_t entryPageOffset() const {
259 return _addressSpace.get16(
260 _addr + offsetof(unwind_info_regular_second_level_page_header,
263 uint16_t entryCount() const {
264 return _addressSpace.get16(
266 offsetof(unwind_info_regular_second_level_page_header, entryCount));
271 typename A::pint_t _addr;
274 template <typename A> class UnwindSectionRegularArray {
276 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
277 : _addressSpace(addressSpace), _addr(addr) {}
279 uint32_t functionOffset(uint32_t index) const {
280 return _addressSpace.get32(
281 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
284 uint32_t encoding(uint32_t index) const {
285 return _addressSpace.get32(
287 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
292 typename A::pint_t _addr;
295 template <typename A> class UnwindSectionCompressedPageHeader {
297 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
298 : _addressSpace(addressSpace), _addr(addr) {}
300 uint32_t kind() const {
301 return _addressSpace.get32(
303 offsetof(unwind_info_compressed_second_level_page_header, kind));
305 uint16_t entryPageOffset() const {
306 return _addressSpace.get16(
307 _addr + offsetof(unwind_info_compressed_second_level_page_header,
310 uint16_t entryCount() const {
311 return _addressSpace.get16(
313 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
315 uint16_t encodingsPageOffset() const {
316 return _addressSpace.get16(
317 _addr + offsetof(unwind_info_compressed_second_level_page_header,
318 encodingsPageOffset));
320 uint16_t encodingsCount() const {
321 return _addressSpace.get16(
322 _addr + offsetof(unwind_info_compressed_second_level_page_header,
328 typename A::pint_t _addr;
331 template <typename A> class UnwindSectionCompressedArray {
333 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
334 : _addressSpace(addressSpace), _addr(addr) {}
336 uint32_t functionOffset(uint32_t index) const {
337 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
338 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
340 uint16_t encodingIndex(uint32_t index) const {
341 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
342 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
347 typename A::pint_t _addr;
350 template <typename A> class UnwindSectionLsdaArray {
352 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
353 : _addressSpace(addressSpace), _addr(addr) {}
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));
360 uint32_t lsdaOffset(uint32_t index) const {
361 return _addressSpace.get32(
362 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
368 typename A::pint_t _addr;
370 #endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
372 class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
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) {}
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");
384 virtual bool validFloatReg(int) {
385 _LIBUNWIND_ABORT("validFloatReg not implemented");
387 virtual unw_fpreg_t getFloatReg(int) {
388 _LIBUNWIND_ABORT("getFloatReg not implemented");
390 virtual void setFloatReg(int, unw_fpreg_t) {
391 _LIBUNWIND_ABORT("setFloatReg not implemented");
393 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
394 virtual void getInfo(unw_proc_info_t *) {
395 _LIBUNWIND_ABORT("getInfo not implemented");
397 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
398 virtual bool isSignalFrame() {
399 _LIBUNWIND_ABORT("isSignalFrame not implemented");
401 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
402 _LIBUNWIND_ABORT("getFunctionName not implemented");
404 virtual void setInfoBasedOnIPRegister(bool = false) {
405 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
407 virtual const char *getRegisterName(int) {
408 _LIBUNWIND_ABORT("getRegisterName not implemented");
411 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
415 /// UnwindCursor contains all state (including all register values) during
416 /// an unwind. This is normally stack allocated inside a unw_cursor_t.
417 template <typename A, typename R>
418 class UnwindCursor : public AbstractUnwindCursor{
419 typedef typename A::pint_t pint_t;
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);
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);
438 virtual void saveVFPAsX();
443 #if _LIBUNWIND_ARM_EHABI
444 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections §s);
446 int stepWithEHABI() {
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),
454 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
455 _URC_CONTINUE_UNWIND)
457 return UNW_STEP_SUCCESS;
461 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
462 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections §s,
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,
472 #if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
473 bool getInfoFromCompactEncodingSection(pint_t pc,
474 const UnwindInfoSections §s);
475 int stepWithCompactEncoding() {
476 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
477 if ( compactSaysUseDwarf() )
478 return stepWithDwarfFDE();
481 return stepWithCompactEncoding(dummy);
484 #if defined(_LIBUNWIND_TARGET_X86_64)
485 int stepWithCompactEncoding(Registers_x86_64 &) {
486 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
487 _info.format, _info.start_ip, _addressSpace, _registers);
491 #if defined(_LIBUNWIND_TARGET_I386)
492 int stepWithCompactEncoding(Registers_x86 &) {
493 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
494 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
498 #if defined(_LIBUNWIND_TARGET_PPC)
499 int stepWithCompactEncoding(Registers_ppc &) {
504 #if defined(_LIBUNWIND_TARGET_AARCH64)
505 int stepWithCompactEncoding(Registers_arm64 &) {
506 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
507 _info.format, _info.start_ip, _addressSpace, _registers);
511 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
513 return compactSaysUseDwarf(dummy, offset);
516 #if defined(_LIBUNWIND_TARGET_X86_64)
517 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
518 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
520 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
527 #if defined(_LIBUNWIND_TARGET_I386)
528 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
529 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
531 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
538 #if defined(_LIBUNWIND_TARGET_PPC)
539 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
544 #if defined(_LIBUNWIND_TARGET_AARCH64)
545 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
546 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
548 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
554 #endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
556 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
557 compact_unwind_encoding_t dwarfEncoding() const {
559 return dwarfEncoding(dummy);
562 #if defined(_LIBUNWIND_TARGET_X86_64)
563 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
564 return UNWIND_X86_64_MODE_DWARF;
568 #if defined(_LIBUNWIND_TARGET_I386)
569 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
570 return UNWIND_X86_MODE_DWARF;
574 #if defined(_LIBUNWIND_TARGET_PPC)
575 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
580 #if defined(_LIBUNWIND_TARGET_AARCH64)
581 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
582 return UNWIND_ARM64_MODE_DWARF;
586 #if defined (_LIBUNWIND_TARGET_OR1K)
587 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
592 #if defined (_LIBUNWIND_TARGET_RISCV)
593 compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
597 #endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
602 unw_proc_info_t _info;
603 bool _unwindInfoMissing;
608 template <typename A, typename R>
609 UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
610 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
611 _isSignalFrame(false) {
612 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
613 "UnwindCursor<> does not fit in unw_cursor_t");
614 memset(&_info, 0, sizeof(_info));
617 template <typename A, typename R>
618 UnwindCursor<A, R>::UnwindCursor(A &as, void *)
619 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
620 memset(&_info, 0, sizeof(_info));
622 // fill in _registers from thread arg
626 template <typename A, typename R>
627 bool UnwindCursor<A, R>::validReg(int regNum) {
628 return _registers.validRegister(regNum);
631 template <typename A, typename R>
632 unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
633 return _registers.getRegister(regNum);
636 template <typename A, typename R>
637 void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
638 _registers.setRegister(regNum, (typename A::pint_t)value);
641 template <typename A, typename R>
642 bool UnwindCursor<A, R>::validFloatReg(int regNum) {
643 return _registers.validFloatRegister(regNum);
646 template <typename A, typename R>
647 unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
648 return _registers.getFloatRegister(regNum);
651 template <typename A, typename R>
652 void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
653 _registers.setFloatRegister(regNum, value);
656 template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
661 template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
662 _registers.saveVFPAsX();
666 template <typename A, typename R>
667 const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
668 return _registers.getRegisterName(regNum);
671 template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
672 return _isSignalFrame;
675 #if _LIBUNWIND_ARM_EHABI
676 struct EHABIIndexEntry {
677 uint32_t functionOffset;
682 struct EHABISectionIterator {
683 typedef EHABISectionIterator _Self;
685 typedef std::random_access_iterator_tag iterator_category;
686 typedef typename A::pint_t value_type;
687 typedef typename A::pint_t* pointer;
688 typedef typename A::pint_t& reference;
689 typedef size_t size_type;
690 typedef size_t difference_type;
692 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
693 return _Self(addressSpace, sects, 0);
695 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
696 return _Self(addressSpace, sects, sects.arm_section_length);
699 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
700 : _i(i), _addressSpace(&addressSpace), _sects(§s) {}
702 _Self& operator++() { ++_i; return *this; }
703 _Self& operator+=(size_t a) { _i += a; return *this; }
704 _Self& operator--() { assert(_i > 0); --_i; return *this; }
705 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
707 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
708 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
710 size_t operator-(const _Self& other) { return _i - other._i; }
712 bool operator==(const _Self& other) const {
713 assert(_addressSpace == other._addressSpace);
714 assert(_sects == other._sects);
715 return _i == other._i;
718 typename A::pint_t operator*() const { return functionAddress(); }
720 typename A::pint_t functionAddress() const {
721 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
722 EHABIIndexEntry, _i, functionOffset);
723 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
726 typename A::pint_t dataAddress() {
727 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
728 EHABIIndexEntry, _i, data);
735 const UnwindInfoSections* _sects;
738 template <typename A, typename R>
739 bool UnwindCursor<A, R>::getInfoFromEHABISection(
741 const UnwindInfoSections §s) {
742 EHABISectionIterator<A> begin =
743 EHABISectionIterator<A>::begin(_addressSpace, sects);
744 EHABISectionIterator<A> end =
745 EHABISectionIterator<A>::end(_addressSpace, sects);
747 EHABISectionIterator<A> itNextPC = std::upper_bound(begin, end, pc);
748 if (itNextPC == begin || itNextPC == end)
750 EHABISectionIterator<A> itThisPC = itNextPC - 1;
752 pint_t thisPC = itThisPC.functionAddress();
753 pint_t nextPC = itNextPC.functionAddress();
754 pint_t indexDataAddr = itThisPC.dataAddress();
756 if (indexDataAddr == 0)
759 uint32_t indexData = _addressSpace.get32(indexDataAddr);
760 if (indexData == UNW_EXIDX_CANTUNWIND)
763 // If the high bit is set, the exception handling table entry is inline inside
764 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
765 // the table points at an offset in the exception handling table (section 5 EHABI).
766 pint_t exceptionTableAddr;
767 uint32_t exceptionTableData;
768 bool isSingleWordEHT;
769 if (indexData & 0x80000000) {
770 exceptionTableAddr = indexDataAddr;
771 // TODO(ajwong): Should this data be 0?
772 exceptionTableData = indexData;
773 isSingleWordEHT = true;
775 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
776 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
777 isSingleWordEHT = false;
780 // Now we know the 3 things:
781 // exceptionTableAddr -- exception handler table entry.
782 // exceptionTableData -- the data inside the first word of the eht entry.
783 // isSingleWordEHT -- whether the entry is in the index.
784 unw_word_t personalityRoutine = 0xbadf00d;
785 bool scope32 = false;
788 // If the high bit in the exception handling table entry is set, the entry is
789 // in compact form (section 6.3 EHABI).
790 if (exceptionTableData & 0x80000000) {
791 // Grab the index of the personality routine from the compact form.
792 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
793 uint32_t extraWords = 0;
796 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
799 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
802 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
803 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
805 lsda = exceptionTableAddr + (extraWords + 1) * 4;
808 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
809 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
811 lsda = exceptionTableAddr + (extraWords + 1) * 4;
814 _LIBUNWIND_ABORT("unknown personality routine");
818 if (isSingleWordEHT) {
819 if (extraWords != 0) {
820 _LIBUNWIND_ABORT("index inlined table detected but pr function "
821 "requires extra words");
826 pint_t personalityAddr =
827 exceptionTableAddr + signExtendPrel31(exceptionTableData);
828 personalityRoutine = personalityAddr;
830 // ARM EHABI # 6.2, # 9.2
834 // +--------------------------------------+
835 // | +--------+--------+--------+-------+ |
836 // | |0| prel31 to personalityRoutine | |
837 // | +--------+--------+--------+-------+ |
838 // | | N | unwind opcodes | | <-- UnwindData
839 // | +--------+--------+--------+-------+ |
840 // | | Word 2 unwind opcodes | |
841 // | +--------+--------+--------+-------+ |
843 // | +--------+--------+--------+-------+ |
844 // | | Word N unwind opcodes | |
845 // | +--------+--------+--------+-------+ |
846 // | | LSDA | | <-- lsda
848 // | +--------+--------+--------+-------+ |
849 // +--------------------------------------+
851 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
852 uint32_t FirstDataWord = *UnwindData;
853 size_t N = ((FirstDataWord >> 24) & 0xff);
854 size_t NDataWords = N + 1;
855 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
858 _info.start_ip = thisPC;
859 _info.end_ip = nextPC;
860 _info.handler = personalityRoutine;
861 _info.unwind_info = exceptionTableAddr;
863 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
864 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum?
870 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
871 template <typename A, typename R>
872 bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
873 const UnwindInfoSections §s,
874 uint32_t fdeSectionOffsetHint) {
875 typename CFI_Parser<A>::FDE_Info fdeInfo;
876 typename CFI_Parser<A>::CIE_Info cieInfo;
877 bool foundFDE = false;
878 bool foundInCache = false;
879 // If compact encoding table gave offset into dwarf section, go directly there
880 if (fdeSectionOffsetHint != 0) {
881 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
882 (uint32_t)sects.dwarf_section_length,
883 sects.dwarf_section + fdeSectionOffsetHint,
886 #if _LIBUNWIND_SUPPORT_DWARF_INDEX
887 if (!foundFDE && (sects.dwarf_index_section != 0)) {
888 foundFDE = EHHeaderParser<A>::findFDE(
889 _addressSpace, pc, sects.dwarf_index_section,
890 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
894 // otherwise, search cache of previously found FDEs.
895 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
896 if (cachedFDE != 0) {
898 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
899 (uint32_t)sects.dwarf_section_length,
900 cachedFDE, &fdeInfo, &cieInfo);
901 foundInCache = foundFDE;
905 // Still not found, do full scan of __eh_frame section.
906 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
907 (uint32_t)sects.dwarf_section_length, 0,
911 typename CFI_Parser<A>::PrologInfo prolog;
912 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
914 // Save off parsed FDE info
915 _info.start_ip = fdeInfo.pcStart;
916 _info.end_ip = fdeInfo.pcEnd;
917 _info.lsda = fdeInfo.lsda;
918 _info.handler = cieInfo.personality;
919 _info.gp = prolog.spExtraArgSize;
921 _info.format = dwarfEncoding();
922 _info.unwind_info = fdeInfo.fdeStart;
923 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
924 _info.extra = (unw_word_t) sects.dso_base;
926 // Add to cache (to make next lookup faster) if we had no hint
927 // and there was no index.
928 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
929 #if _LIBUNWIND_SUPPORT_DWARF_INDEX
930 if (sects.dwarf_index_section == 0)
932 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
938 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
941 #endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
944 #if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
945 template <typename A, typename R>
946 bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
947 const UnwindInfoSections §s) {
948 const bool log = false;
950 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
951 (uint64_t)pc, (uint64_t)sects.dso_base);
953 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
954 sects.compact_unwind_section);
955 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
958 // do a binary search of top level index to find page with unwind info
959 pint_t targetFunctionOffset = pc - sects.dso_base;
960 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
961 sects.compact_unwind_section
962 + sectionHeader.indexSectionOffset());
964 uint32_t high = sectionHeader.indexCount();
965 uint32_t last = high - 1;
967 uint32_t mid = (low + high) / 2;
968 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
969 //mid, low, high, topIndex.functionOffset(mid));
970 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
972 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
982 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
983 const uint32_t firstLevelNextPageFunctionOffset =
984 topIndex.functionOffset(low + 1);
985 const pint_t secondLevelAddr =
986 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
987 const pint_t lsdaArrayStartAddr =
988 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
989 const pint_t lsdaArrayEndAddr =
990 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
992 fprintf(stderr, "\tfirst level search for result index=%d "
993 "to secondLevelAddr=0x%llX\n",
994 low, (uint64_t) secondLevelAddr);
995 // do a binary search of second level page index
996 uint32_t encoding = 0;
997 pint_t funcStart = 0;
1000 pint_t personality = 0;
1001 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1002 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1004 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1006 UnwindSectionRegularArray<A> pageIndex(
1007 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1008 // binary search looks for entry with e where index[e].offset <= pc <
1009 // index[e+1].offset
1011 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1012 "regular page starting at secondLevelAddr=0x%llX\n",
1013 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1015 high = pageHeader.entryCount();
1016 while (low < high) {
1017 uint32_t mid = (low + high) / 2;
1018 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1019 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1022 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1024 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1025 // next is too big, so we found it
1027 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1036 encoding = pageIndex.encoding(low);
1037 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1038 if (pc < funcStart) {
1042 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1043 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1050 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1051 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1054 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1056 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1058 UnwindSectionCompressedArray<A> pageIndex(
1059 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1060 const uint32_t targetFunctionPageOffset =
1061 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1062 // binary search looks for entry with e where index[e].offset <= pc <
1063 // index[e+1].offset
1065 fprintf(stderr, "\tbinary search of compressed page starting at "
1066 "secondLevelAddr=0x%llX\n",
1067 (uint64_t) secondLevelAddr);
1069 last = pageHeader.entryCount() - 1;
1070 high = pageHeader.entryCount();
1071 while (low < high) {
1072 uint32_t mid = (low + high) / 2;
1073 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1074 if ((mid == last) ||
1075 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1085 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1089 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1092 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1093 if (pc < funcStart) {
1094 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
1095 "level compressed unwind table. funcStart=0x%llX",
1096 (uint64_t) pc, (uint64_t) funcStart);
1100 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
1101 "level compressed unwind table. funcEnd=0x%llX",
1102 (uint64_t) pc, (uint64_t) funcEnd);
1105 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1106 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1107 // encoding is in common table in section header
1108 encoding = _addressSpace.get32(
1109 sects.compact_unwind_section +
1110 sectionHeader.commonEncodingsArraySectionOffset() +
1111 encodingIndex * sizeof(uint32_t));
1113 // encoding is in page specific table
1114 uint16_t pageEncodingIndex =
1115 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1116 encoding = _addressSpace.get32(secondLevelAddr +
1117 pageHeader.encodingsPageOffset() +
1118 pageEncodingIndex * sizeof(uint32_t));
1121 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
1123 (uint64_t) sects.compact_unwind_section);
1127 // look up LSDA, if encoding says function has one
1128 if (encoding & UNWIND_HAS_LSDA) {
1129 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1130 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1132 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1133 sizeof(unwind_info_section_header_lsda_index_entry);
1134 // binary search looks for entry with exact match for functionOffset
1137 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1139 while (low < high) {
1140 uint32_t mid = (low + high) / 2;
1141 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1142 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1144 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1151 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
1152 "pc=0x%0llX, but lsda table has no entry",
1153 encoding, (uint64_t) pc);
1158 // extact personality routine, if encoding says function has one
1159 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1160 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1161 if (personalityIndex != 0) {
1162 --personalityIndex; // change 1-based to zero-based index
1163 if (personalityIndex > sectionHeader.personalityArrayCount()) {
1164 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
1165 "but personality table has only %d entires",
1166 encoding, personalityIndex,
1167 sectionHeader.personalityArrayCount());
1170 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1171 sects.compact_unwind_section +
1172 sectionHeader.personalityArraySectionOffset() +
1173 personalityIndex * sizeof(uint32_t));
1174 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1175 personality = _addressSpace.getP(personalityPointer);
1177 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1178 "personalityDelta=0x%08X, personality=0x%08llX\n",
1179 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1183 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1184 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1185 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1186 _info.start_ip = funcStart;
1187 _info.end_ip = funcEnd;
1189 _info.handler = personality;
1192 _info.format = encoding;
1193 _info.unwind_info = 0;
1194 _info.unwind_info_size = 0;
1195 _info.extra = sects.dso_base;
1198 #endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1201 template <typename A, typename R>
1202 void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1203 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
1204 #if _LIBUNWIND_ARM_EHABI
1205 // Remove the thumb bit so the IP represents the actual instruction address.
1206 // This matches the behaviour of _Unwind_GetIP on arm.
1210 // If the last line of a function is a "throw" the compiler sometimes
1211 // emits no instructions after the call to __cxa_throw. This means
1212 // the return address is actually the start of the next function.
1213 // To disambiguate this, back up the pc when we know it is a return
1215 if (isReturnAddress)
1218 // Ask address space object to find unwind sections for this pc.
1219 UnwindInfoSections sects;
1220 if (_addressSpace.findUnwindSections(pc, sects)) {
1221 #if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1222 // If there is a compact unwind encoding table, look there first.
1223 if (sects.compact_unwind_section != 0) {
1224 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
1225 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1226 // Found info in table, done unless encoding says to use dwarf.
1227 uint32_t dwarfOffset;
1228 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1229 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1230 // found info in dwarf, done
1235 // If unwind table has entry, but entry says there is no unwind info,
1236 // record that we have no unwind info.
1237 if (_info.format == 0)
1238 _unwindInfoMissing = true;
1242 #endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1244 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1245 // If there is dwarf unwind info, look there next.
1246 if (sects.dwarf_section != 0) {
1247 if (this->getInfoFromDwarfSection(pc, sects)) {
1248 // found info in dwarf, done
1254 #if _LIBUNWIND_ARM_EHABI
1255 // If there is ARM EHABI unwind info, look there next.
1256 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1261 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1262 // There is no static unwind info for this pc. Look to see if an FDE was
1263 // dynamically registered for it.
1264 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1265 if (cachedFDE != 0) {
1266 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1267 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1268 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1269 cachedFDE, &fdeInfo, &cieInfo);
1271 typename CFI_Parser<A>::PrologInfo prolog;
1272 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1274 // save off parsed FDE info
1275 _info.start_ip = fdeInfo.pcStart;
1276 _info.end_ip = fdeInfo.pcEnd;
1277 _info.lsda = fdeInfo.lsda;
1278 _info.handler = cieInfo.personality;
1279 _info.gp = prolog.spExtraArgSize;
1280 // Some frameless functions need SP
1281 // altered when resuming in function.
1283 _info.format = dwarfEncoding();
1284 _info.unwind_info = fdeInfo.fdeStart;
1285 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1292 // Lastly, ask AddressSpace object about platform specific ways to locate
1295 if (_addressSpace.findOtherFDE(pc, fde)) {
1296 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1297 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1298 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1299 // Double check this FDE is for a function that includes the pc.
1300 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1301 typename CFI_Parser<A>::PrologInfo prolog;
1302 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo,
1303 cieInfo, pc, &prolog)) {
1304 // save off parsed FDE info
1305 _info.start_ip = fdeInfo.pcStart;
1306 _info.end_ip = fdeInfo.pcEnd;
1307 _info.lsda = fdeInfo.lsda;
1308 _info.handler = cieInfo.personality;
1309 _info.gp = prolog.spExtraArgSize;
1311 _info.format = dwarfEncoding();
1312 _info.unwind_info = fdeInfo.fdeStart;
1313 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1320 #endif // #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1322 // no unwind info, flag that we can't reliably unwind
1323 _unwindInfoMissing = true;
1326 template <typename A, typename R>
1327 int UnwindCursor<A, R>::step() {
1328 // Bottom of stack is defined is when unwind info cannot be found.
1329 if (_unwindInfoMissing)
1330 return UNW_STEP_END;
1332 // Use unwinding info to modify register set as if function returned.
1334 #if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1335 result = this->stepWithCompactEncoding();
1336 #elif _LIBUNWIND_SUPPORT_DWARF_UNWIND
1337 result = this->stepWithDwarfFDE();
1338 #elif _LIBUNWIND_ARM_EHABI
1339 result = this->stepWithEHABI();
1341 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
1342 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
1343 _LIBUNWIND_ARM_EHABI
1346 // update info based on new PC
1347 if (result == UNW_STEP_SUCCESS) {
1348 this->setInfoBasedOnIPRegister(true);
1349 if (_unwindInfoMissing)
1350 return UNW_STEP_END;
1352 setReg(UNW_REG_SP, getReg(UNW_REG_SP) + _info.gp);
1358 template <typename A, typename R>
1359 void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
1363 template <typename A, typename R>
1364 bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
1365 unw_word_t *offset) {
1366 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
1367 buf, bufLen, offset);
1370 } // namespace libunwind
1372 #endif // __UNWINDCURSOR_HPP__