xref: /freebsd/contrib/llvm-project/libunwind/src/UnwindCursor.hpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //
80b57cec5SDimitry Andric // C++ interface to lower levels of libunwind
90b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #ifndef __UNWINDCURSOR_HPP__
120b57cec5SDimitry Andric #define __UNWINDCURSOR_HPP__
130b57cec5SDimitry Andric 
14349cc55cSDimitry Andric #include "cet_unwind.h"
150b57cec5SDimitry Andric #include <stdint.h>
160b57cec5SDimitry Andric #include <stdio.h>
170b57cec5SDimitry Andric #include <stdlib.h>
180b57cec5SDimitry Andric #include <unwind.h>
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric #ifdef _WIN32
210b57cec5SDimitry Andric   #include <windows.h>
220b57cec5SDimitry Andric   #include <ntverp.h>
230b57cec5SDimitry Andric #endif
240b57cec5SDimitry Andric #ifdef __APPLE__
250b57cec5SDimitry Andric   #include <mach-o/dyld.h>
260b57cec5SDimitry Andric #endif
2781ad6265SDimitry Andric #ifdef _AIX
2881ad6265SDimitry Andric #include <dlfcn.h>
2981ad6265SDimitry Andric #include <sys/debug.h>
3081ad6265SDimitry Andric #include <sys/pseg.h>
3181ad6265SDimitry Andric #endif
3281ad6265SDimitry Andric 
3381ad6265SDimitry Andric #if defined(_LIBUNWIND_TARGET_LINUX) &&                                        \
3406c3fb27SDimitry Andric     (defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_RISCV) || \
3506c3fb27SDimitry Andric      defined(_LIBUNWIND_TARGET_S390X))
3681ad6265SDimitry Andric #include <sys/syscall.h>
3781ad6265SDimitry Andric #include <sys/uio.h>
3881ad6265SDimitry Andric #include <unistd.h>
3981ad6265SDimitry Andric #define _LIBUNWIND_CHECK_LINUX_SIGRETURN 1
4081ad6265SDimitry Andric #endif
410b57cec5SDimitry Andric 
42bdd1243dSDimitry Andric #include "AddressSpace.hpp"
43bdd1243dSDimitry Andric #include "CompactUnwinder.hpp"
44bdd1243dSDimitry Andric #include "config.h"
45bdd1243dSDimitry Andric #include "DwarfInstructions.hpp"
46bdd1243dSDimitry Andric #include "EHHeaderParser.hpp"
47bdd1243dSDimitry Andric #include "libunwind.h"
48bdd1243dSDimitry Andric #include "libunwind_ext.h"
49bdd1243dSDimitry Andric #include "Registers.hpp"
50bdd1243dSDimitry Andric #include "RWMutex.hpp"
51bdd1243dSDimitry Andric #include "Unwind-EHABI.h"
52bdd1243dSDimitry Andric 
530b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
540b57cec5SDimitry Andric // Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and
550b57cec5SDimitry Andric // earlier) SDKs.
560b57cec5SDimitry Andric // MinGW-w64 has always provided this struct.
570b57cec5SDimitry Andric   #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \
580b57cec5SDimitry Andric       !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
590b57cec5SDimitry Andric struct _DISPATCHER_CONTEXT {
600b57cec5SDimitry Andric   ULONG64 ControlPc;
610b57cec5SDimitry Andric   ULONG64 ImageBase;
620b57cec5SDimitry Andric   PRUNTIME_FUNCTION FunctionEntry;
630b57cec5SDimitry Andric   ULONG64 EstablisherFrame;
640b57cec5SDimitry Andric   ULONG64 TargetIp;
650b57cec5SDimitry Andric   PCONTEXT ContextRecord;
660b57cec5SDimitry Andric   PEXCEPTION_ROUTINE LanguageHandler;
670b57cec5SDimitry Andric   PVOID HandlerData;
680b57cec5SDimitry Andric   PUNWIND_HISTORY_TABLE HistoryTable;
690b57cec5SDimitry Andric   ULONG ScopeIndex;
700b57cec5SDimitry Andric   ULONG Fill0;
710b57cec5SDimitry Andric };
720b57cec5SDimitry Andric   #endif
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric struct UNWIND_INFO {
750b57cec5SDimitry Andric   uint8_t Version : 3;
760b57cec5SDimitry Andric   uint8_t Flags : 5;
770b57cec5SDimitry Andric   uint8_t SizeOfProlog;
780b57cec5SDimitry Andric   uint8_t CountOfCodes;
790b57cec5SDimitry Andric   uint8_t FrameRegister : 4;
800b57cec5SDimitry Andric   uint8_t FrameOffset : 4;
810b57cec5SDimitry Andric   uint16_t UnwindCodes[2];
820b57cec5SDimitry Andric };
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
850b57cec5SDimitry Andric     int, _Unwind_Action, uint64_t, _Unwind_Exception *,
860b57cec5SDimitry Andric     struct _Unwind_Context *);
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric #endif
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric namespace libunwind {
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
930b57cec5SDimitry Andric /// Cache of recently found FDEs.
940b57cec5SDimitry Andric template <typename A>
950b57cec5SDimitry Andric class _LIBUNWIND_HIDDEN DwarfFDECache {
960b57cec5SDimitry Andric   typedef typename A::pint_t pint_t;
970b57cec5SDimitry Andric public:
98e8d8bef9SDimitry Andric   static constexpr pint_t kSearchAll = static_cast<pint_t>(-1);
990b57cec5SDimitry Andric   static pint_t findFDE(pint_t mh, pint_t pc);
1000b57cec5SDimitry Andric   static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
1010b57cec5SDimitry Andric   static void removeAllIn(pint_t mh);
1020b57cec5SDimitry Andric   static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
1030b57cec5SDimitry Andric                                                unw_word_t ip_end,
1040b57cec5SDimitry Andric                                                unw_word_t fde, unw_word_t mh));
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric private:
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   struct entry {
1090b57cec5SDimitry Andric     pint_t mh;
1100b57cec5SDimitry Andric     pint_t ip_start;
1110b57cec5SDimitry Andric     pint_t ip_end;
1120b57cec5SDimitry Andric     pint_t fde;
1130b57cec5SDimitry Andric   };
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   // These fields are all static to avoid needing an initializer.
1160b57cec5SDimitry Andric   // There is only one instance of this class per process.
1170b57cec5SDimitry Andric   static RWMutex _lock;
1180b57cec5SDimitry Andric #ifdef __APPLE__
1190b57cec5SDimitry Andric   static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
1200b57cec5SDimitry Andric   static bool _registeredForDyldUnloads;
1210b57cec5SDimitry Andric #endif
1220b57cec5SDimitry Andric   static entry *_buffer;
1230b57cec5SDimitry Andric   static entry *_bufferUsed;
1240b57cec5SDimitry Andric   static entry *_bufferEnd;
1250b57cec5SDimitry Andric   static entry _initialBuffer[64];
1260b57cec5SDimitry Andric };
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric template <typename A>
1290b57cec5SDimitry Andric typename DwarfFDECache<A>::entry *
1300b57cec5SDimitry Andric DwarfFDECache<A>::_buffer = _initialBuffer;
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric template <typename A>
1330b57cec5SDimitry Andric typename DwarfFDECache<A>::entry *
1340b57cec5SDimitry Andric DwarfFDECache<A>::_bufferUsed = _initialBuffer;
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric template <typename A>
1370b57cec5SDimitry Andric typename DwarfFDECache<A>::entry *
1380b57cec5SDimitry Andric DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric template <typename A>
1410b57cec5SDimitry Andric typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric template <typename A>
1440b57cec5SDimitry Andric RWMutex DwarfFDECache<A>::_lock;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric #ifdef __APPLE__
1470b57cec5SDimitry Andric template <typename A>
1480b57cec5SDimitry Andric bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
1490b57cec5SDimitry Andric #endif
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric template <typename A>
1520b57cec5SDimitry Andric typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
1530b57cec5SDimitry Andric   pint_t result = 0;
1540b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
1550b57cec5SDimitry Andric   for (entry *p = _buffer; p < _bufferUsed; ++p) {
156e8d8bef9SDimitry Andric     if ((mh == p->mh) || (mh == kSearchAll)) {
1570b57cec5SDimitry Andric       if ((p->ip_start <= pc) && (pc < p->ip_end)) {
1580b57cec5SDimitry Andric         result = p->fde;
1590b57cec5SDimitry Andric         break;
1600b57cec5SDimitry Andric       }
1610b57cec5SDimitry Andric     }
1620b57cec5SDimitry Andric   }
1630b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
1640b57cec5SDimitry Andric   return result;
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric template <typename A>
1680b57cec5SDimitry Andric void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
1690b57cec5SDimitry Andric                            pint_t fde) {
1700b57cec5SDimitry Andric #if !defined(_LIBUNWIND_NO_HEAP)
1710b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
1720b57cec5SDimitry Andric   if (_bufferUsed >= _bufferEnd) {
1730b57cec5SDimitry Andric     size_t oldSize = (size_t)(_bufferEnd - _buffer);
1740b57cec5SDimitry Andric     size_t newSize = oldSize * 4;
1750b57cec5SDimitry Andric     // Can't use operator new (we are below it).
1760b57cec5SDimitry Andric     entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
1770b57cec5SDimitry Andric     memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
1780b57cec5SDimitry Andric     if (_buffer != _initialBuffer)
1790b57cec5SDimitry Andric       free(_buffer);
1800b57cec5SDimitry Andric     _buffer = newBuffer;
1810b57cec5SDimitry Andric     _bufferUsed = &newBuffer[oldSize];
1820b57cec5SDimitry Andric     _bufferEnd = &newBuffer[newSize];
1830b57cec5SDimitry Andric   }
1840b57cec5SDimitry Andric   _bufferUsed->mh = mh;
1850b57cec5SDimitry Andric   _bufferUsed->ip_start = ip_start;
1860b57cec5SDimitry Andric   _bufferUsed->ip_end = ip_end;
1870b57cec5SDimitry Andric   _bufferUsed->fde = fde;
1880b57cec5SDimitry Andric   ++_bufferUsed;
1890b57cec5SDimitry Andric #ifdef __APPLE__
1900b57cec5SDimitry Andric   if (!_registeredForDyldUnloads) {
1910b57cec5SDimitry Andric     _dyld_register_func_for_remove_image(&dyldUnloadHook);
1920b57cec5SDimitry Andric     _registeredForDyldUnloads = true;
1930b57cec5SDimitry Andric   }
1940b57cec5SDimitry Andric #endif
1950b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
1960b57cec5SDimitry Andric #endif
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric template <typename A>
2000b57cec5SDimitry Andric void DwarfFDECache<A>::removeAllIn(pint_t mh) {
2010b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
2020b57cec5SDimitry Andric   entry *d = _buffer;
2030b57cec5SDimitry Andric   for (const entry *s = _buffer; s < _bufferUsed; ++s) {
2040b57cec5SDimitry Andric     if (s->mh != mh) {
2050b57cec5SDimitry Andric       if (d != s)
2060b57cec5SDimitry Andric         *d = *s;
2070b57cec5SDimitry Andric       ++d;
2080b57cec5SDimitry Andric     }
2090b57cec5SDimitry Andric   }
2100b57cec5SDimitry Andric   _bufferUsed = d;
2110b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric #ifdef __APPLE__
2150b57cec5SDimitry Andric template <typename A>
2160b57cec5SDimitry Andric void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
2170b57cec5SDimitry Andric   removeAllIn((pint_t) mh);
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric #endif
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric template <typename A>
2220b57cec5SDimitry Andric void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
2230b57cec5SDimitry Andric     unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
2240b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
2250b57cec5SDimitry Andric   for (entry *p = _buffer; p < _bufferUsed; ++p) {
2260b57cec5SDimitry Andric     (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
2270b57cec5SDimitry Andric   }
2280b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric #define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
2360b57cec5SDimitry Andric template <typename A> class UnwindSectionHeader {
2370b57cec5SDimitry Andric public:
2380b57cec5SDimitry Andric   UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
2390b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric   uint32_t version() const {
2420b57cec5SDimitry Andric     return _addressSpace.get32(_addr +
2430b57cec5SDimitry Andric                                offsetof(unwind_info_section_header, version));
2440b57cec5SDimitry Andric   }
2450b57cec5SDimitry Andric   uint32_t commonEncodingsArraySectionOffset() const {
2460b57cec5SDimitry Andric     return _addressSpace.get32(_addr +
2470b57cec5SDimitry Andric                                offsetof(unwind_info_section_header,
2480b57cec5SDimitry Andric                                         commonEncodingsArraySectionOffset));
2490b57cec5SDimitry Andric   }
2500b57cec5SDimitry Andric   uint32_t commonEncodingsArrayCount() const {
2510b57cec5SDimitry Andric     return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
2520b57cec5SDimitry Andric                                                 commonEncodingsArrayCount));
2530b57cec5SDimitry Andric   }
2540b57cec5SDimitry Andric   uint32_t personalityArraySectionOffset() const {
2550b57cec5SDimitry Andric     return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
2560b57cec5SDimitry Andric                                                 personalityArraySectionOffset));
2570b57cec5SDimitry Andric   }
2580b57cec5SDimitry Andric   uint32_t personalityArrayCount() const {
2590b57cec5SDimitry Andric     return _addressSpace.get32(
2600b57cec5SDimitry Andric         _addr + offsetof(unwind_info_section_header, personalityArrayCount));
2610b57cec5SDimitry Andric   }
2620b57cec5SDimitry Andric   uint32_t indexSectionOffset() const {
2630b57cec5SDimitry Andric     return _addressSpace.get32(
2640b57cec5SDimitry Andric         _addr + offsetof(unwind_info_section_header, indexSectionOffset));
2650b57cec5SDimitry Andric   }
2660b57cec5SDimitry Andric   uint32_t indexCount() const {
2670b57cec5SDimitry Andric     return _addressSpace.get32(
2680b57cec5SDimitry Andric         _addr + offsetof(unwind_info_section_header, indexCount));
2690b57cec5SDimitry Andric   }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric private:
2720b57cec5SDimitry Andric   A                     &_addressSpace;
2730b57cec5SDimitry Andric   typename A::pint_t     _addr;
2740b57cec5SDimitry Andric };
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric template <typename A> class UnwindSectionIndexArray {
2770b57cec5SDimitry Andric public:
2780b57cec5SDimitry Andric   UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
2790b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
2820b57cec5SDimitry Andric     return _addressSpace.get32(
2830b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
2840b57cec5SDimitry Andric                               functionOffset));
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric   uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
2870b57cec5SDimitry Andric     return _addressSpace.get32(
2880b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
2890b57cec5SDimitry Andric                               secondLevelPagesSectionOffset));
2900b57cec5SDimitry Andric   }
2910b57cec5SDimitry Andric   uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
2920b57cec5SDimitry Andric     return _addressSpace.get32(
2930b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
2940b57cec5SDimitry Andric                               lsdaIndexArraySectionOffset));
2950b57cec5SDimitry Andric   }
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric private:
2980b57cec5SDimitry Andric   A                   &_addressSpace;
2990b57cec5SDimitry Andric   typename A::pint_t   _addr;
3000b57cec5SDimitry Andric };
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric template <typename A> class UnwindSectionRegularPageHeader {
3030b57cec5SDimitry Andric public:
3040b57cec5SDimitry Andric   UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
3050b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric   uint32_t kind() const {
3080b57cec5SDimitry Andric     return _addressSpace.get32(
3090b57cec5SDimitry Andric         _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
3100b57cec5SDimitry Andric   }
3110b57cec5SDimitry Andric   uint16_t entryPageOffset() const {
3120b57cec5SDimitry Andric     return _addressSpace.get16(
3130b57cec5SDimitry Andric         _addr + offsetof(unwind_info_regular_second_level_page_header,
3140b57cec5SDimitry Andric                          entryPageOffset));
3150b57cec5SDimitry Andric   }
3160b57cec5SDimitry Andric   uint16_t entryCount() const {
3170b57cec5SDimitry Andric     return _addressSpace.get16(
3180b57cec5SDimitry Andric         _addr +
3190b57cec5SDimitry Andric         offsetof(unwind_info_regular_second_level_page_header, entryCount));
3200b57cec5SDimitry Andric   }
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric private:
3230b57cec5SDimitry Andric   A &_addressSpace;
3240b57cec5SDimitry Andric   typename A::pint_t _addr;
3250b57cec5SDimitry Andric };
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric template <typename A> class UnwindSectionRegularArray {
3280b57cec5SDimitry Andric public:
3290b57cec5SDimitry Andric   UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
3300b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
3330b57cec5SDimitry Andric     return _addressSpace.get32(
3340b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
3350b57cec5SDimitry Andric                               functionOffset));
3360b57cec5SDimitry Andric   }
3370b57cec5SDimitry Andric   uint32_t encoding(uint32_t index) const {
3380b57cec5SDimitry Andric     return _addressSpace.get32(
3390b57cec5SDimitry Andric         _addr +
3400b57cec5SDimitry Andric         arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
3410b57cec5SDimitry Andric   }
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric private:
3440b57cec5SDimitry Andric   A &_addressSpace;
3450b57cec5SDimitry Andric   typename A::pint_t _addr;
3460b57cec5SDimitry Andric };
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric template <typename A> class UnwindSectionCompressedPageHeader {
3490b57cec5SDimitry Andric public:
3500b57cec5SDimitry Andric   UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
3510b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric   uint32_t kind() const {
3540b57cec5SDimitry Andric     return _addressSpace.get32(
3550b57cec5SDimitry Andric         _addr +
3560b57cec5SDimitry Andric         offsetof(unwind_info_compressed_second_level_page_header, kind));
3570b57cec5SDimitry Andric   }
3580b57cec5SDimitry Andric   uint16_t entryPageOffset() const {
3590b57cec5SDimitry Andric     return _addressSpace.get16(
3600b57cec5SDimitry Andric         _addr + offsetof(unwind_info_compressed_second_level_page_header,
3610b57cec5SDimitry Andric                          entryPageOffset));
3620b57cec5SDimitry Andric   }
3630b57cec5SDimitry Andric   uint16_t entryCount() const {
3640b57cec5SDimitry Andric     return _addressSpace.get16(
3650b57cec5SDimitry Andric         _addr +
3660b57cec5SDimitry Andric         offsetof(unwind_info_compressed_second_level_page_header, entryCount));
3670b57cec5SDimitry Andric   }
3680b57cec5SDimitry Andric   uint16_t encodingsPageOffset() const {
3690b57cec5SDimitry Andric     return _addressSpace.get16(
3700b57cec5SDimitry Andric         _addr + offsetof(unwind_info_compressed_second_level_page_header,
3710b57cec5SDimitry Andric                          encodingsPageOffset));
3720b57cec5SDimitry Andric   }
3730b57cec5SDimitry Andric   uint16_t encodingsCount() const {
3740b57cec5SDimitry Andric     return _addressSpace.get16(
3750b57cec5SDimitry Andric         _addr + offsetof(unwind_info_compressed_second_level_page_header,
3760b57cec5SDimitry Andric                          encodingsCount));
3770b57cec5SDimitry Andric   }
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric private:
3800b57cec5SDimitry Andric   A &_addressSpace;
3810b57cec5SDimitry Andric   typename A::pint_t _addr;
3820b57cec5SDimitry Andric };
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric template <typename A> class UnwindSectionCompressedArray {
3850b57cec5SDimitry Andric public:
3860b57cec5SDimitry Andric   UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
3870b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
3900b57cec5SDimitry Andric     return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
3910b57cec5SDimitry Andric         _addressSpace.get32(_addr + index * sizeof(uint32_t)));
3920b57cec5SDimitry Andric   }
3930b57cec5SDimitry Andric   uint16_t encodingIndex(uint32_t index) const {
3940b57cec5SDimitry Andric     return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
3950b57cec5SDimitry Andric         _addressSpace.get32(_addr + index * sizeof(uint32_t)));
3960b57cec5SDimitry Andric   }
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric private:
3990b57cec5SDimitry Andric   A &_addressSpace;
4000b57cec5SDimitry Andric   typename A::pint_t _addr;
4010b57cec5SDimitry Andric };
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric template <typename A> class UnwindSectionLsdaArray {
4040b57cec5SDimitry Andric public:
4050b57cec5SDimitry Andric   UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
4060b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
4090b57cec5SDimitry Andric     return _addressSpace.get32(
4100b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
4110b57cec5SDimitry Andric                               index, functionOffset));
4120b57cec5SDimitry Andric   }
4130b57cec5SDimitry Andric   uint32_t lsdaOffset(uint32_t index) const {
4140b57cec5SDimitry Andric     return _addressSpace.get32(
4150b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
4160b57cec5SDimitry Andric                               index, lsdaOffset));
4170b57cec5SDimitry Andric   }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric private:
4200b57cec5SDimitry Andric   A                   &_addressSpace;
4210b57cec5SDimitry Andric   typename A::pint_t   _addr;
4220b57cec5SDimitry Andric };
4230b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
4260b57cec5SDimitry Andric public:
4270b57cec5SDimitry Andric   // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
4280b57cec5SDimitry Andric   // This avoids an unnecessary dependency to libc++abi.
4290b57cec5SDimitry Andric   void operator delete(void *, size_t) {}
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   virtual ~AbstractUnwindCursor() {}
4320b57cec5SDimitry Andric   virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
4330b57cec5SDimitry Andric   virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
4340b57cec5SDimitry Andric   virtual void setReg(int, unw_word_t) {
4350b57cec5SDimitry Andric     _LIBUNWIND_ABORT("setReg not implemented");
4360b57cec5SDimitry Andric   }
4370b57cec5SDimitry Andric   virtual bool validFloatReg(int) {
4380b57cec5SDimitry Andric     _LIBUNWIND_ABORT("validFloatReg not implemented");
4390b57cec5SDimitry Andric   }
4400b57cec5SDimitry Andric   virtual unw_fpreg_t getFloatReg(int) {
4410b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getFloatReg not implemented");
4420b57cec5SDimitry Andric   }
4430b57cec5SDimitry Andric   virtual void setFloatReg(int, unw_fpreg_t) {
4440b57cec5SDimitry Andric     _LIBUNWIND_ABORT("setFloatReg not implemented");
4450b57cec5SDimitry Andric   }
446bdd1243dSDimitry Andric   virtual int step(bool = false) { _LIBUNWIND_ABORT("step not implemented"); }
4470b57cec5SDimitry Andric   virtual void getInfo(unw_proc_info_t *) {
4480b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getInfo not implemented");
4490b57cec5SDimitry Andric   }
4500b57cec5SDimitry Andric   virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
4510b57cec5SDimitry Andric   virtual bool isSignalFrame() {
4520b57cec5SDimitry Andric     _LIBUNWIND_ABORT("isSignalFrame not implemented");
4530b57cec5SDimitry Andric   }
4540b57cec5SDimitry Andric   virtual bool getFunctionName(char *, size_t, unw_word_t *) {
4550b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getFunctionName not implemented");
4560b57cec5SDimitry Andric   }
4570b57cec5SDimitry Andric   virtual void setInfoBasedOnIPRegister(bool = false) {
4580b57cec5SDimitry Andric     _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
4590b57cec5SDimitry Andric   }
4600b57cec5SDimitry Andric   virtual const char *getRegisterName(int) {
4610b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getRegisterName not implemented");
4620b57cec5SDimitry Andric   }
4630b57cec5SDimitry Andric #ifdef __arm__
4640b57cec5SDimitry Andric   virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
4650b57cec5SDimitry Andric #endif
466349cc55cSDimitry Andric 
46781ad6265SDimitry Andric #ifdef _AIX
46881ad6265SDimitry Andric   virtual uintptr_t getDataRelBase() {
46981ad6265SDimitry Andric     _LIBUNWIND_ABORT("getDataRelBase not implemented");
47081ad6265SDimitry Andric   }
47181ad6265SDimitry Andric #endif
47281ad6265SDimitry Andric 
473349cc55cSDimitry Andric #if defined(_LIBUNWIND_USE_CET)
474349cc55cSDimitry Andric   virtual void *get_registers() {
475349cc55cSDimitry Andric     _LIBUNWIND_ABORT("get_registers not implemented");
476349cc55cSDimitry Andric   }
477349cc55cSDimitry Andric #endif
4780b57cec5SDimitry Andric };
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric /// \c UnwindCursor contains all state (including all register values) during
4830b57cec5SDimitry Andric /// an unwind.  This is normally stack-allocated inside a unw_cursor_t.
4840b57cec5SDimitry Andric template <typename A, typename R>
4850b57cec5SDimitry Andric class UnwindCursor : public AbstractUnwindCursor {
4860b57cec5SDimitry Andric   typedef typename A::pint_t pint_t;
4870b57cec5SDimitry Andric public:
4880b57cec5SDimitry Andric                       UnwindCursor(unw_context_t *context, A &as);
4890b57cec5SDimitry Andric                       UnwindCursor(CONTEXT *context, A &as);
4900b57cec5SDimitry Andric                       UnwindCursor(A &as, void *threadArg);
4910b57cec5SDimitry Andric   virtual             ~UnwindCursor() {}
4920b57cec5SDimitry Andric   virtual bool        validReg(int);
4930b57cec5SDimitry Andric   virtual unw_word_t  getReg(int);
4940b57cec5SDimitry Andric   virtual void        setReg(int, unw_word_t);
4950b57cec5SDimitry Andric   virtual bool        validFloatReg(int);
4960b57cec5SDimitry Andric   virtual unw_fpreg_t getFloatReg(int);
4970b57cec5SDimitry Andric   virtual void        setFloatReg(int, unw_fpreg_t);
498bdd1243dSDimitry Andric   virtual int         step(bool = false);
4990b57cec5SDimitry Andric   virtual void        getInfo(unw_proc_info_t *);
5000b57cec5SDimitry Andric   virtual void        jumpto();
5010b57cec5SDimitry Andric   virtual bool        isSignalFrame();
5020b57cec5SDimitry Andric   virtual bool        getFunctionName(char *buf, size_t len, unw_word_t *off);
5030b57cec5SDimitry Andric   virtual void        setInfoBasedOnIPRegister(bool isReturnAddress = false);
5040b57cec5SDimitry Andric   virtual const char *getRegisterName(int num);
5050b57cec5SDimitry Andric #ifdef __arm__
5060b57cec5SDimitry Andric   virtual void        saveVFPAsX();
5070b57cec5SDimitry Andric #endif
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric   DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
51006c3fb27SDimitry Andric   void setDispatcherContext(DISPATCHER_CONTEXT *disp) {
51106c3fb27SDimitry Andric     _dispContext = *disp;
51206c3fb27SDimitry Andric     _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
51306c3fb27SDimitry Andric     if (_dispContext.LanguageHandler) {
51406c3fb27SDimitry Andric       _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
51506c3fb27SDimitry Andric     } else
51606c3fb27SDimitry Andric       _info.handler = 0;
51706c3fb27SDimitry Andric   }
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   // libunwind does not and should not depend on C++ library which means that we
520bdd1243dSDimitry Andric   // need our own definition of inline placement new.
5210b57cec5SDimitry Andric   static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
5220b57cec5SDimitry Andric 
5230b57cec5SDimitry Andric private:
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric   pint_t getLastPC() const { return _dispContext.ControlPc; }
5260b57cec5SDimitry Andric   void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
5270b57cec5SDimitry Andric   RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
52881ad6265SDimitry Andric #ifdef __arm__
52981ad6265SDimitry Andric     // Remove the thumb bit; FunctionEntry ranges don't include the thumb bit.
53081ad6265SDimitry Andric     pc &= ~1U;
53181ad6265SDimitry Andric #endif
53281ad6265SDimitry Andric     // If pc points exactly at the end of the range, we might resolve the
53381ad6265SDimitry Andric     // next function instead. Decrement pc by 1 to fit inside the current
53481ad6265SDimitry Andric     // function.
53581ad6265SDimitry Andric     pc -= 1;
5360b57cec5SDimitry Andric     _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
5370b57cec5SDimitry Andric                                                         &_dispContext.ImageBase,
5380b57cec5SDimitry Andric                                                         _dispContext.HistoryTable);
5390b57cec5SDimitry Andric     *base = _dispContext.ImageBase;
5400b57cec5SDimitry Andric     return _dispContext.FunctionEntry;
5410b57cec5SDimitry Andric   }
5420b57cec5SDimitry Andric   bool getInfoFromSEH(pint_t pc);
5430b57cec5SDimitry Andric   int stepWithSEHData() {
5440b57cec5SDimitry Andric     _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
5450b57cec5SDimitry Andric                                                     _dispContext.ImageBase,
5460b57cec5SDimitry Andric                                                     _dispContext.ControlPc,
5470b57cec5SDimitry Andric                                                     _dispContext.FunctionEntry,
5480b57cec5SDimitry Andric                                                     _dispContext.ContextRecord,
5490b57cec5SDimitry Andric                                                     &_dispContext.HandlerData,
5500b57cec5SDimitry Andric                                                     &_dispContext.EstablisherFrame,
5510b57cec5SDimitry Andric                                                     NULL);
5520b57cec5SDimitry Andric     // Update some fields of the unwind info now, since we have them.
5530b57cec5SDimitry Andric     _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
5540b57cec5SDimitry Andric     if (_dispContext.LanguageHandler) {
5550b57cec5SDimitry Andric       _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
5560b57cec5SDimitry Andric     } else
5570b57cec5SDimitry Andric       _info.handler = 0;
5580b57cec5SDimitry Andric     return UNW_STEP_SUCCESS;
5590b57cec5SDimitry Andric   }
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric   A                   &_addressSpace;
5620b57cec5SDimitry Andric   unw_proc_info_t      _info;
5630b57cec5SDimitry Andric   DISPATCHER_CONTEXT   _dispContext;
5640b57cec5SDimitry Andric   CONTEXT              _msContext;
5650b57cec5SDimitry Andric   UNWIND_HISTORY_TABLE _histTable;
5660b57cec5SDimitry Andric   bool                 _unwindInfoMissing;
5670b57cec5SDimitry Andric };
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric 
5700b57cec5SDimitry Andric template <typename A, typename R>
5710b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
5720b57cec5SDimitry Andric     : _addressSpace(as), _unwindInfoMissing(false) {
5730b57cec5SDimitry Andric   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
5740b57cec5SDimitry Andric                 "UnwindCursor<> does not fit in unw_cursor_t");
575e8d8bef9SDimitry Andric   static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
576e8d8bef9SDimitry Andric                 "UnwindCursor<> requires more alignment than unw_cursor_t");
5770b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
5780b57cec5SDimitry Andric   memset(&_histTable, 0, sizeof(_histTable));
57906c3fb27SDimitry Andric   memset(&_dispContext, 0, sizeof(_dispContext));
5800b57cec5SDimitry Andric   _dispContext.ContextRecord = &_msContext;
5810b57cec5SDimitry Andric   _dispContext.HistoryTable = &_histTable;
5820b57cec5SDimitry Andric   // Initialize MS context from ours.
5830b57cec5SDimitry Andric   R r(context);
58406c3fb27SDimitry Andric   RtlCaptureContext(&_msContext);
5850b57cec5SDimitry Andric   _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
5860b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
5870b57cec5SDimitry Andric   _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
5880b57cec5SDimitry Andric   _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
5890b57cec5SDimitry Andric   _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
5900b57cec5SDimitry Andric   _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
5910b57cec5SDimitry Andric   _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
5920b57cec5SDimitry Andric   _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
5930b57cec5SDimitry Andric   _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
5940b57cec5SDimitry Andric   _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
5950b57cec5SDimitry Andric   _msContext.R8 = r.getRegister(UNW_X86_64_R8);
5960b57cec5SDimitry Andric   _msContext.R9 = r.getRegister(UNW_X86_64_R9);
5970b57cec5SDimitry Andric   _msContext.R10 = r.getRegister(UNW_X86_64_R10);
5980b57cec5SDimitry Andric   _msContext.R11 = r.getRegister(UNW_X86_64_R11);
5990b57cec5SDimitry Andric   _msContext.R12 = r.getRegister(UNW_X86_64_R12);
6000b57cec5SDimitry Andric   _msContext.R13 = r.getRegister(UNW_X86_64_R13);
6010b57cec5SDimitry Andric   _msContext.R14 = r.getRegister(UNW_X86_64_R14);
6020b57cec5SDimitry Andric   _msContext.R15 = r.getRegister(UNW_X86_64_R15);
6030b57cec5SDimitry Andric   _msContext.Rip = r.getRegister(UNW_REG_IP);
6040b57cec5SDimitry Andric   union {
6050b57cec5SDimitry Andric     v128 v;
6060b57cec5SDimitry Andric     M128A m;
6070b57cec5SDimitry Andric   } t;
6080b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM0);
6090b57cec5SDimitry Andric   _msContext.Xmm0 = t.m;
6100b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM1);
6110b57cec5SDimitry Andric   _msContext.Xmm1 = t.m;
6120b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM2);
6130b57cec5SDimitry Andric   _msContext.Xmm2 = t.m;
6140b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM3);
6150b57cec5SDimitry Andric   _msContext.Xmm3 = t.m;
6160b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM4);
6170b57cec5SDimitry Andric   _msContext.Xmm4 = t.m;
6180b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM5);
6190b57cec5SDimitry Andric   _msContext.Xmm5 = t.m;
6200b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM6);
6210b57cec5SDimitry Andric   _msContext.Xmm6 = t.m;
6220b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM7);
6230b57cec5SDimitry Andric   _msContext.Xmm7 = t.m;
6240b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM8);
6250b57cec5SDimitry Andric   _msContext.Xmm8 = t.m;
6260b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM9);
6270b57cec5SDimitry Andric   _msContext.Xmm9 = t.m;
6280b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM10);
6290b57cec5SDimitry Andric   _msContext.Xmm10 = t.m;
6300b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM11);
6310b57cec5SDimitry Andric   _msContext.Xmm11 = t.m;
6320b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM12);
6330b57cec5SDimitry Andric   _msContext.Xmm12 = t.m;
6340b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM13);
6350b57cec5SDimitry Andric   _msContext.Xmm13 = t.m;
6360b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM14);
6370b57cec5SDimitry Andric   _msContext.Xmm14 = t.m;
6380b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM15);
6390b57cec5SDimitry Andric   _msContext.Xmm15 = t.m;
6400b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
6410b57cec5SDimitry Andric   _msContext.R0 = r.getRegister(UNW_ARM_R0);
6420b57cec5SDimitry Andric   _msContext.R1 = r.getRegister(UNW_ARM_R1);
6430b57cec5SDimitry Andric   _msContext.R2 = r.getRegister(UNW_ARM_R2);
6440b57cec5SDimitry Andric   _msContext.R3 = r.getRegister(UNW_ARM_R3);
6450b57cec5SDimitry Andric   _msContext.R4 = r.getRegister(UNW_ARM_R4);
6460b57cec5SDimitry Andric   _msContext.R5 = r.getRegister(UNW_ARM_R5);
6470b57cec5SDimitry Andric   _msContext.R6 = r.getRegister(UNW_ARM_R6);
6480b57cec5SDimitry Andric   _msContext.R7 = r.getRegister(UNW_ARM_R7);
6490b57cec5SDimitry Andric   _msContext.R8 = r.getRegister(UNW_ARM_R8);
6500b57cec5SDimitry Andric   _msContext.R9 = r.getRegister(UNW_ARM_R9);
6510b57cec5SDimitry Andric   _msContext.R10 = r.getRegister(UNW_ARM_R10);
6520b57cec5SDimitry Andric   _msContext.R11 = r.getRegister(UNW_ARM_R11);
6530b57cec5SDimitry Andric   _msContext.R12 = r.getRegister(UNW_ARM_R12);
6540b57cec5SDimitry Andric   _msContext.Sp = r.getRegister(UNW_ARM_SP);
6550b57cec5SDimitry Andric   _msContext.Lr = r.getRegister(UNW_ARM_LR);
6560b57cec5SDimitry Andric   _msContext.Pc = r.getRegister(UNW_ARM_IP);
6570b57cec5SDimitry Andric   for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
6580b57cec5SDimitry Andric     union {
6590b57cec5SDimitry Andric       uint64_t w;
6600b57cec5SDimitry Andric       double d;
6610b57cec5SDimitry Andric     } d;
6620b57cec5SDimitry Andric     d.d = r.getFloatRegister(i);
6630b57cec5SDimitry Andric     _msContext.D[i - UNW_ARM_D0] = d.w;
6640b57cec5SDimitry Andric   }
6650b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
666349cc55cSDimitry Andric   for (int i = UNW_AARCH64_X0; i <= UNW_ARM64_X30; ++i)
667349cc55cSDimitry Andric     _msContext.X[i - UNW_AARCH64_X0] = r.getRegister(i);
6680b57cec5SDimitry Andric   _msContext.Sp = r.getRegister(UNW_REG_SP);
6690b57cec5SDimitry Andric   _msContext.Pc = r.getRegister(UNW_REG_IP);
670349cc55cSDimitry Andric   for (int i = UNW_AARCH64_V0; i <= UNW_ARM64_D31; ++i)
671349cc55cSDimitry Andric     _msContext.V[i - UNW_AARCH64_V0].D[0] = r.getFloatRegister(i);
6720b57cec5SDimitry Andric #endif
6730b57cec5SDimitry Andric }
6740b57cec5SDimitry Andric 
6750b57cec5SDimitry Andric template <typename A, typename R>
6760b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
6770b57cec5SDimitry Andric     : _addressSpace(as), _unwindInfoMissing(false) {
6780b57cec5SDimitry Andric   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
6790b57cec5SDimitry Andric                 "UnwindCursor<> does not fit in unw_cursor_t");
6800b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
6810b57cec5SDimitry Andric   memset(&_histTable, 0, sizeof(_histTable));
68206c3fb27SDimitry Andric   memset(&_dispContext, 0, sizeof(_dispContext));
6830b57cec5SDimitry Andric   _dispContext.ContextRecord = &_msContext;
6840b57cec5SDimitry Andric   _dispContext.HistoryTable = &_histTable;
6850b57cec5SDimitry Andric   _msContext = *context;
6860b57cec5SDimitry Andric }
6870b57cec5SDimitry Andric 
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric template <typename A, typename R>
6900b57cec5SDimitry Andric bool UnwindCursor<A, R>::validReg(int regNum) {
6910b57cec5SDimitry Andric   if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
6920b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
69306c3fb27SDimitry Andric   if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_RIP) return true;
6940b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
6950eae32dcSDimitry Andric   if ((regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) ||
6960eae32dcSDimitry Andric       regNum == UNW_ARM_RA_AUTH_CODE)
6970eae32dcSDimitry Andric     return true;
6980b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
699349cc55cSDimitry Andric   if (regNum >= UNW_AARCH64_X0 && regNum <= UNW_ARM64_X30) return true;
7000b57cec5SDimitry Andric #endif
7010b57cec5SDimitry Andric   return false;
7020b57cec5SDimitry Andric }
7030b57cec5SDimitry Andric 
7040b57cec5SDimitry Andric template <typename A, typename R>
7050b57cec5SDimitry Andric unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
7060b57cec5SDimitry Andric   switch (regNum) {
7070b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
70806c3fb27SDimitry Andric   case UNW_X86_64_RIP:
7090b57cec5SDimitry Andric   case UNW_REG_IP: return _msContext.Rip;
7100b57cec5SDimitry Andric   case UNW_X86_64_RAX: return _msContext.Rax;
7110b57cec5SDimitry Andric   case UNW_X86_64_RDX: return _msContext.Rdx;
7120b57cec5SDimitry Andric   case UNW_X86_64_RCX: return _msContext.Rcx;
7130b57cec5SDimitry Andric   case UNW_X86_64_RBX: return _msContext.Rbx;
7140b57cec5SDimitry Andric   case UNW_REG_SP:
7150b57cec5SDimitry Andric   case UNW_X86_64_RSP: return _msContext.Rsp;
7160b57cec5SDimitry Andric   case UNW_X86_64_RBP: return _msContext.Rbp;
7170b57cec5SDimitry Andric   case UNW_X86_64_RSI: return _msContext.Rsi;
7180b57cec5SDimitry Andric   case UNW_X86_64_RDI: return _msContext.Rdi;
7190b57cec5SDimitry Andric   case UNW_X86_64_R8: return _msContext.R8;
7200b57cec5SDimitry Andric   case UNW_X86_64_R9: return _msContext.R9;
7210b57cec5SDimitry Andric   case UNW_X86_64_R10: return _msContext.R10;
7220b57cec5SDimitry Andric   case UNW_X86_64_R11: return _msContext.R11;
7230b57cec5SDimitry Andric   case UNW_X86_64_R12: return _msContext.R12;
7240b57cec5SDimitry Andric   case UNW_X86_64_R13: return _msContext.R13;
7250b57cec5SDimitry Andric   case UNW_X86_64_R14: return _msContext.R14;
7260b57cec5SDimitry Andric   case UNW_X86_64_R15: return _msContext.R15;
7270b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
7280b57cec5SDimitry Andric   case UNW_ARM_R0: return _msContext.R0;
7290b57cec5SDimitry Andric   case UNW_ARM_R1: return _msContext.R1;
7300b57cec5SDimitry Andric   case UNW_ARM_R2: return _msContext.R2;
7310b57cec5SDimitry Andric   case UNW_ARM_R3: return _msContext.R3;
7320b57cec5SDimitry Andric   case UNW_ARM_R4: return _msContext.R4;
7330b57cec5SDimitry Andric   case UNW_ARM_R5: return _msContext.R5;
7340b57cec5SDimitry Andric   case UNW_ARM_R6: return _msContext.R6;
7350b57cec5SDimitry Andric   case UNW_ARM_R7: return _msContext.R7;
7360b57cec5SDimitry Andric   case UNW_ARM_R8: return _msContext.R8;
7370b57cec5SDimitry Andric   case UNW_ARM_R9: return _msContext.R9;
7380b57cec5SDimitry Andric   case UNW_ARM_R10: return _msContext.R10;
7390b57cec5SDimitry Andric   case UNW_ARM_R11: return _msContext.R11;
7400b57cec5SDimitry Andric   case UNW_ARM_R12: return _msContext.R12;
7410b57cec5SDimitry Andric   case UNW_REG_SP:
7420b57cec5SDimitry Andric   case UNW_ARM_SP: return _msContext.Sp;
7430b57cec5SDimitry Andric   case UNW_ARM_LR: return _msContext.Lr;
7440b57cec5SDimitry Andric   case UNW_REG_IP:
7450b57cec5SDimitry Andric   case UNW_ARM_IP: return _msContext.Pc;
7460b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
7470b57cec5SDimitry Andric   case UNW_REG_SP: return _msContext.Sp;
7480b57cec5SDimitry Andric   case UNW_REG_IP: return _msContext.Pc;
749349cc55cSDimitry Andric   default: return _msContext.X[regNum - UNW_AARCH64_X0];
7500b57cec5SDimitry Andric #endif
7510b57cec5SDimitry Andric   }
7520b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported register");
7530b57cec5SDimitry Andric }
7540b57cec5SDimitry Andric 
7550b57cec5SDimitry Andric template <typename A, typename R>
7560b57cec5SDimitry Andric void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
7570b57cec5SDimitry Andric   switch (regNum) {
7580b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
75906c3fb27SDimitry Andric   case UNW_X86_64_RIP:
7600b57cec5SDimitry Andric   case UNW_REG_IP: _msContext.Rip = value; break;
7610b57cec5SDimitry Andric   case UNW_X86_64_RAX: _msContext.Rax = value; break;
7620b57cec5SDimitry Andric   case UNW_X86_64_RDX: _msContext.Rdx = value; break;
7630b57cec5SDimitry Andric   case UNW_X86_64_RCX: _msContext.Rcx = value; break;
7640b57cec5SDimitry Andric   case UNW_X86_64_RBX: _msContext.Rbx = value; break;
7650b57cec5SDimitry Andric   case UNW_REG_SP:
7660b57cec5SDimitry Andric   case UNW_X86_64_RSP: _msContext.Rsp = value; break;
7670b57cec5SDimitry Andric   case UNW_X86_64_RBP: _msContext.Rbp = value; break;
7680b57cec5SDimitry Andric   case UNW_X86_64_RSI: _msContext.Rsi = value; break;
7690b57cec5SDimitry Andric   case UNW_X86_64_RDI: _msContext.Rdi = value; break;
7700b57cec5SDimitry Andric   case UNW_X86_64_R8: _msContext.R8 = value; break;
7710b57cec5SDimitry Andric   case UNW_X86_64_R9: _msContext.R9 = value; break;
7720b57cec5SDimitry Andric   case UNW_X86_64_R10: _msContext.R10 = value; break;
7730b57cec5SDimitry Andric   case UNW_X86_64_R11: _msContext.R11 = value; break;
7740b57cec5SDimitry Andric   case UNW_X86_64_R12: _msContext.R12 = value; break;
7750b57cec5SDimitry Andric   case UNW_X86_64_R13: _msContext.R13 = value; break;
7760b57cec5SDimitry Andric   case UNW_X86_64_R14: _msContext.R14 = value; break;
7770b57cec5SDimitry Andric   case UNW_X86_64_R15: _msContext.R15 = value; break;
7780b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
7790b57cec5SDimitry Andric   case UNW_ARM_R0: _msContext.R0 = value; break;
7800b57cec5SDimitry Andric   case UNW_ARM_R1: _msContext.R1 = value; break;
7810b57cec5SDimitry Andric   case UNW_ARM_R2: _msContext.R2 = value; break;
7820b57cec5SDimitry Andric   case UNW_ARM_R3: _msContext.R3 = value; break;
7830b57cec5SDimitry Andric   case UNW_ARM_R4: _msContext.R4 = value; break;
7840b57cec5SDimitry Andric   case UNW_ARM_R5: _msContext.R5 = value; break;
7850b57cec5SDimitry Andric   case UNW_ARM_R6: _msContext.R6 = value; break;
7860b57cec5SDimitry Andric   case UNW_ARM_R7: _msContext.R7 = value; break;
7870b57cec5SDimitry Andric   case UNW_ARM_R8: _msContext.R8 = value; break;
7880b57cec5SDimitry Andric   case UNW_ARM_R9: _msContext.R9 = value; break;
7890b57cec5SDimitry Andric   case UNW_ARM_R10: _msContext.R10 = value; break;
7900b57cec5SDimitry Andric   case UNW_ARM_R11: _msContext.R11 = value; break;
7910b57cec5SDimitry Andric   case UNW_ARM_R12: _msContext.R12 = value; break;
7920b57cec5SDimitry Andric   case UNW_REG_SP:
7930b57cec5SDimitry Andric   case UNW_ARM_SP: _msContext.Sp = value; break;
7940b57cec5SDimitry Andric   case UNW_ARM_LR: _msContext.Lr = value; break;
7950b57cec5SDimitry Andric   case UNW_REG_IP:
7960b57cec5SDimitry Andric   case UNW_ARM_IP: _msContext.Pc = value; break;
7970b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
7980b57cec5SDimitry Andric   case UNW_REG_SP: _msContext.Sp = value; break;
7990b57cec5SDimitry Andric   case UNW_REG_IP: _msContext.Pc = value; break;
800349cc55cSDimitry Andric   case UNW_AARCH64_X0:
801349cc55cSDimitry Andric   case UNW_AARCH64_X1:
802349cc55cSDimitry Andric   case UNW_AARCH64_X2:
803349cc55cSDimitry Andric   case UNW_AARCH64_X3:
804349cc55cSDimitry Andric   case UNW_AARCH64_X4:
805349cc55cSDimitry Andric   case UNW_AARCH64_X5:
806349cc55cSDimitry Andric   case UNW_AARCH64_X6:
807349cc55cSDimitry Andric   case UNW_AARCH64_X7:
808349cc55cSDimitry Andric   case UNW_AARCH64_X8:
809349cc55cSDimitry Andric   case UNW_AARCH64_X9:
810349cc55cSDimitry Andric   case UNW_AARCH64_X10:
811349cc55cSDimitry Andric   case UNW_AARCH64_X11:
812349cc55cSDimitry Andric   case UNW_AARCH64_X12:
813349cc55cSDimitry Andric   case UNW_AARCH64_X13:
814349cc55cSDimitry Andric   case UNW_AARCH64_X14:
815349cc55cSDimitry Andric   case UNW_AARCH64_X15:
816349cc55cSDimitry Andric   case UNW_AARCH64_X16:
817349cc55cSDimitry Andric   case UNW_AARCH64_X17:
818349cc55cSDimitry Andric   case UNW_AARCH64_X18:
819349cc55cSDimitry Andric   case UNW_AARCH64_X19:
820349cc55cSDimitry Andric   case UNW_AARCH64_X20:
821349cc55cSDimitry Andric   case UNW_AARCH64_X21:
822349cc55cSDimitry Andric   case UNW_AARCH64_X22:
823349cc55cSDimitry Andric   case UNW_AARCH64_X23:
824349cc55cSDimitry Andric   case UNW_AARCH64_X24:
825349cc55cSDimitry Andric   case UNW_AARCH64_X25:
826349cc55cSDimitry Andric   case UNW_AARCH64_X26:
827349cc55cSDimitry Andric   case UNW_AARCH64_X27:
828349cc55cSDimitry Andric   case UNW_AARCH64_X28:
829349cc55cSDimitry Andric   case UNW_AARCH64_FP:
830349cc55cSDimitry Andric   case UNW_AARCH64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
8310b57cec5SDimitry Andric #endif
8320b57cec5SDimitry Andric   default:
8330b57cec5SDimitry Andric     _LIBUNWIND_ABORT("unsupported register");
8340b57cec5SDimitry Andric   }
8350b57cec5SDimitry Andric }
8360b57cec5SDimitry Andric 
8370b57cec5SDimitry Andric template <typename A, typename R>
8380b57cec5SDimitry Andric bool UnwindCursor<A, R>::validFloatReg(int regNum) {
8390b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
8400b57cec5SDimitry Andric   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
8410b57cec5SDimitry Andric   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
8420b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
843349cc55cSDimitry Andric   if (regNum >= UNW_AARCH64_V0 && regNum <= UNW_ARM64_D31) return true;
8440b57cec5SDimitry Andric #else
8450b57cec5SDimitry Andric   (void)regNum;
8460b57cec5SDimitry Andric #endif
8470b57cec5SDimitry Andric   return false;
8480b57cec5SDimitry Andric }
8490b57cec5SDimitry Andric 
8500b57cec5SDimitry Andric template <typename A, typename R>
8510b57cec5SDimitry Andric unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
8520b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
8530b57cec5SDimitry Andric   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
8540b57cec5SDimitry Andric     union {
8550b57cec5SDimitry Andric       uint32_t w;
8560b57cec5SDimitry Andric       float f;
8570b57cec5SDimitry Andric     } d;
8580b57cec5SDimitry Andric     d.w = _msContext.S[regNum - UNW_ARM_S0];
8590b57cec5SDimitry Andric     return d.f;
8600b57cec5SDimitry Andric   }
8610b57cec5SDimitry Andric   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
8620b57cec5SDimitry Andric     union {
8630b57cec5SDimitry Andric       uint64_t w;
8640b57cec5SDimitry Andric       double d;
8650b57cec5SDimitry Andric     } d;
8660b57cec5SDimitry Andric     d.w = _msContext.D[regNum - UNW_ARM_D0];
8670b57cec5SDimitry Andric     return d.d;
8680b57cec5SDimitry Andric   }
8690b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported float register");
8700b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
871349cc55cSDimitry Andric   return _msContext.V[regNum - UNW_AARCH64_V0].D[0];
8720b57cec5SDimitry Andric #else
8730b57cec5SDimitry Andric   (void)regNum;
8740b57cec5SDimitry Andric   _LIBUNWIND_ABORT("float registers unimplemented");
8750b57cec5SDimitry Andric #endif
8760b57cec5SDimitry Andric }
8770b57cec5SDimitry Andric 
8780b57cec5SDimitry Andric template <typename A, typename R>
8790b57cec5SDimitry Andric void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
8800b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
8810b57cec5SDimitry Andric   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
8820b57cec5SDimitry Andric     union {
8830b57cec5SDimitry Andric       uint32_t w;
8840b57cec5SDimitry Andric       float f;
8850b57cec5SDimitry Andric     } d;
88681ad6265SDimitry Andric     d.f = (float)value;
8870b57cec5SDimitry Andric     _msContext.S[regNum - UNW_ARM_S0] = d.w;
8880b57cec5SDimitry Andric   }
8890b57cec5SDimitry Andric   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
8900b57cec5SDimitry Andric     union {
8910b57cec5SDimitry Andric       uint64_t w;
8920b57cec5SDimitry Andric       double d;
8930b57cec5SDimitry Andric     } d;
8940b57cec5SDimitry Andric     d.d = value;
8950b57cec5SDimitry Andric     _msContext.D[regNum - UNW_ARM_D0] = d.w;
8960b57cec5SDimitry Andric   }
8970b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported float register");
8980b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
899349cc55cSDimitry Andric   _msContext.V[regNum - UNW_AARCH64_V0].D[0] = value;
9000b57cec5SDimitry Andric #else
9010b57cec5SDimitry Andric   (void)regNum;
9020b57cec5SDimitry Andric   (void)value;
9030b57cec5SDimitry Andric   _LIBUNWIND_ABORT("float registers unimplemented");
9040b57cec5SDimitry Andric #endif
9050b57cec5SDimitry Andric }
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
9080b57cec5SDimitry Andric   RtlRestoreContext(&_msContext, nullptr);
9090b57cec5SDimitry Andric }
9100b57cec5SDimitry Andric 
9110b57cec5SDimitry Andric #ifdef __arm__
9120b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
9130b57cec5SDimitry Andric #endif
9140b57cec5SDimitry Andric 
9150b57cec5SDimitry Andric template <typename A, typename R>
9160b57cec5SDimitry Andric const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
9170b57cec5SDimitry Andric   return R::getRegisterName(regNum);
9180b57cec5SDimitry Andric }
9190b57cec5SDimitry Andric 
9200b57cec5SDimitry Andric template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
9210b57cec5SDimitry Andric   return false;
9220b57cec5SDimitry Andric }
9230b57cec5SDimitry Andric 
9240b57cec5SDimitry Andric #else  // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric /// UnwindCursor contains all state (including all register values) during
9270b57cec5SDimitry Andric /// an unwind.  This is normally stack allocated inside a unw_cursor_t.
9280b57cec5SDimitry Andric template <typename A, typename R>
9290b57cec5SDimitry Andric class UnwindCursor : public AbstractUnwindCursor{
9300b57cec5SDimitry Andric   typedef typename A::pint_t pint_t;
9310b57cec5SDimitry Andric public:
9320b57cec5SDimitry Andric                       UnwindCursor(unw_context_t *context, A &as);
9330b57cec5SDimitry Andric                       UnwindCursor(A &as, void *threadArg);
9340b57cec5SDimitry Andric   virtual             ~UnwindCursor() {}
9350b57cec5SDimitry Andric   virtual bool        validReg(int);
9360b57cec5SDimitry Andric   virtual unw_word_t  getReg(int);
9370b57cec5SDimitry Andric   virtual void        setReg(int, unw_word_t);
9380b57cec5SDimitry Andric   virtual bool        validFloatReg(int);
9390b57cec5SDimitry Andric   virtual unw_fpreg_t getFloatReg(int);
9400b57cec5SDimitry Andric   virtual void        setFloatReg(int, unw_fpreg_t);
941bdd1243dSDimitry Andric   virtual int         step(bool stage2 = false);
9420b57cec5SDimitry Andric   virtual void        getInfo(unw_proc_info_t *);
9430b57cec5SDimitry Andric   virtual void        jumpto();
9440b57cec5SDimitry Andric   virtual bool        isSignalFrame();
9450b57cec5SDimitry Andric   virtual bool        getFunctionName(char *buf, size_t len, unw_word_t *off);
9460b57cec5SDimitry Andric   virtual void        setInfoBasedOnIPRegister(bool isReturnAddress = false);
9470b57cec5SDimitry Andric   virtual const char *getRegisterName(int num);
9480b57cec5SDimitry Andric #ifdef __arm__
9490b57cec5SDimitry Andric   virtual void        saveVFPAsX();
9500b57cec5SDimitry Andric #endif
9510b57cec5SDimitry Andric 
95281ad6265SDimitry Andric #ifdef _AIX
95381ad6265SDimitry Andric   virtual uintptr_t getDataRelBase();
95481ad6265SDimitry Andric #endif
95581ad6265SDimitry Andric 
956349cc55cSDimitry Andric #if defined(_LIBUNWIND_USE_CET)
957349cc55cSDimitry Andric   virtual void *get_registers() { return &_registers; }
958349cc55cSDimitry Andric #endif
95981ad6265SDimitry Andric 
9600b57cec5SDimitry Andric   // libunwind does not and should not depend on C++ library which means that we
961bdd1243dSDimitry Andric   // need our own definition of inline placement new.
9620b57cec5SDimitry Andric   static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
9630b57cec5SDimitry Andric 
9640b57cec5SDimitry Andric private:
9650b57cec5SDimitry Andric 
9660b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
9670b57cec5SDimitry Andric   bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
9680b57cec5SDimitry Andric 
9690b57cec5SDimitry Andric   int stepWithEHABI() {
9700b57cec5SDimitry Andric     size_t len = 0;
9710b57cec5SDimitry Andric     size_t off = 0;
9720b57cec5SDimitry Andric     // FIXME: Calling decode_eht_entry() here is violating the libunwind
9730b57cec5SDimitry Andric     // abstraction layer.
9740b57cec5SDimitry Andric     const uint32_t *ehtp =
9750b57cec5SDimitry Andric         decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
9760b57cec5SDimitry Andric                          &off, &len);
9770b57cec5SDimitry Andric     if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
9780b57cec5SDimitry Andric             _URC_CONTINUE_UNWIND)
9790b57cec5SDimitry Andric       return UNW_STEP_END;
9800b57cec5SDimitry Andric     return UNW_STEP_SUCCESS;
9810b57cec5SDimitry Andric   }
9820b57cec5SDimitry Andric #endif
9830b57cec5SDimitry Andric 
98481ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
985e8d8bef9SDimitry Andric   bool setInfoForSigReturn() {
986e8d8bef9SDimitry Andric     R dummy;
987e8d8bef9SDimitry Andric     return setInfoForSigReturn(dummy);
988e8d8bef9SDimitry Andric   }
989e8d8bef9SDimitry Andric   int stepThroughSigReturn() {
990e8d8bef9SDimitry Andric     R dummy;
991e8d8bef9SDimitry Andric     return stepThroughSigReturn(dummy);
992e8d8bef9SDimitry Andric   }
99381ad6265SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
994e8d8bef9SDimitry Andric   bool setInfoForSigReturn(Registers_arm64 &);
995e8d8bef9SDimitry Andric   int stepThroughSigReturn(Registers_arm64 &);
99681ad6265SDimitry Andric #endif
99706c3fb27SDimitry Andric #if defined(_LIBUNWIND_TARGET_RISCV)
99806c3fb27SDimitry Andric   bool setInfoForSigReturn(Registers_riscv &);
99906c3fb27SDimitry Andric   int stepThroughSigReturn(Registers_riscv &);
100006c3fb27SDimitry Andric #endif
100181ad6265SDimitry Andric #if defined(_LIBUNWIND_TARGET_S390X)
100281ad6265SDimitry Andric   bool setInfoForSigReturn(Registers_s390x &);
100381ad6265SDimitry Andric   int stepThroughSigReturn(Registers_s390x &);
100481ad6265SDimitry Andric #endif
1005e8d8bef9SDimitry Andric   template <typename Registers> bool setInfoForSigReturn(Registers &) {
1006e8d8bef9SDimitry Andric     return false;
1007e8d8bef9SDimitry Andric   }
1008e8d8bef9SDimitry Andric   template <typename Registers> int stepThroughSigReturn(Registers &) {
1009e8d8bef9SDimitry Andric     return UNW_STEP_END;
1010e8d8bef9SDimitry Andric   }
1011e8d8bef9SDimitry Andric #endif
1012e8d8bef9SDimitry Andric 
10130b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
1014e8d8bef9SDimitry Andric   bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1015e8d8bef9SDimitry Andric                          const typename CFI_Parser<A>::CIE_Info &cieInfo,
1016e8d8bef9SDimitry Andric                          pint_t pc, uintptr_t dso_base);
10170b57cec5SDimitry Andric   bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
10180b57cec5SDimitry Andric                                             uint32_t fdeSectionOffsetHint=0);
1019bdd1243dSDimitry Andric   int stepWithDwarfFDE(bool stage2) {
1020bdd1243dSDimitry Andric     return DwarfInstructions<A, R>::stepWithDwarf(
1021bdd1243dSDimitry Andric         _addressSpace, (pint_t)this->getReg(UNW_REG_IP),
1022bdd1243dSDimitry Andric         (pint_t)_info.unwind_info, _registers, _isSignalFrame, stage2);
10230b57cec5SDimitry Andric   }
10240b57cec5SDimitry Andric #endif
10250b57cec5SDimitry Andric 
10260b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
10270b57cec5SDimitry Andric   bool getInfoFromCompactEncodingSection(pint_t pc,
10280b57cec5SDimitry Andric                                             const UnwindInfoSections &sects);
1029bdd1243dSDimitry Andric   int stepWithCompactEncoding(bool stage2 = false) {
10300b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
10310b57cec5SDimitry Andric     if ( compactSaysUseDwarf() )
1032bdd1243dSDimitry Andric       return stepWithDwarfFDE(stage2);
10330b57cec5SDimitry Andric #endif
10340b57cec5SDimitry Andric     R dummy;
10350b57cec5SDimitry Andric     return stepWithCompactEncoding(dummy);
10360b57cec5SDimitry Andric   }
10370b57cec5SDimitry Andric 
10380b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
10390b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_x86_64 &) {
10400b57cec5SDimitry Andric     return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
10410b57cec5SDimitry Andric         _info.format, _info.start_ip, _addressSpace, _registers);
10420b57cec5SDimitry Andric   }
10430b57cec5SDimitry Andric #endif
10440b57cec5SDimitry Andric 
10450b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386)
10460b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_x86 &) {
10470b57cec5SDimitry Andric     return CompactUnwinder_x86<A>::stepWithCompactEncoding(
10480b57cec5SDimitry Andric         _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
10490b57cec5SDimitry Andric   }
10500b57cec5SDimitry Andric #endif
10510b57cec5SDimitry Andric 
10520b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC)
10530b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_ppc &) {
10540b57cec5SDimitry Andric     return UNW_EINVAL;
10550b57cec5SDimitry Andric   }
10560b57cec5SDimitry Andric #endif
10570b57cec5SDimitry Andric 
10580b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64)
10590b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_ppc64 &) {
10600b57cec5SDimitry Andric     return UNW_EINVAL;
10610b57cec5SDimitry Andric   }
10620b57cec5SDimitry Andric #endif
10630b57cec5SDimitry Andric 
10640b57cec5SDimitry Andric 
10650b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
10660b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_arm64 &) {
10670b57cec5SDimitry Andric     return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
10680b57cec5SDimitry Andric         _info.format, _info.start_ip, _addressSpace, _registers);
10690b57cec5SDimitry Andric   }
10700b57cec5SDimitry Andric #endif
10710b57cec5SDimitry Andric 
10720b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_O32)
10730b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_mips_o32 &) {
10740b57cec5SDimitry Andric     return UNW_EINVAL;
10750b57cec5SDimitry Andric   }
10760b57cec5SDimitry Andric #endif
10770b57cec5SDimitry Andric 
10780b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
10790b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_mips_newabi &) {
10800b57cec5SDimitry Andric     return UNW_EINVAL;
10810b57cec5SDimitry Andric   }
10820b57cec5SDimitry Andric #endif
10830b57cec5SDimitry Andric 
1084bdd1243dSDimitry Andric #if defined(_LIBUNWIND_TARGET_LOONGARCH)
1085bdd1243dSDimitry Andric   int stepWithCompactEncoding(Registers_loongarch &) { return UNW_EINVAL; }
1086bdd1243dSDimitry Andric #endif
1087bdd1243dSDimitry Andric 
10880b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC)
10890b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
10900b57cec5SDimitry Andric #endif
10910b57cec5SDimitry Andric 
1092d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64)
1093d56accc7SDimitry Andric   int stepWithCompactEncoding(Registers_sparc64 &) { return UNW_EINVAL; }
1094d56accc7SDimitry Andric #endif
1095d56accc7SDimitry Andric 
1096480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV)
1097480093f4SDimitry Andric   int stepWithCompactEncoding(Registers_riscv &) {
1098480093f4SDimitry Andric     return UNW_EINVAL;
1099480093f4SDimitry Andric   }
1100480093f4SDimitry Andric #endif
1101480093f4SDimitry Andric 
11020b57cec5SDimitry Andric   bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
11030b57cec5SDimitry Andric     R dummy;
11040b57cec5SDimitry Andric     return compactSaysUseDwarf(dummy, offset);
11050b57cec5SDimitry Andric   }
11060b57cec5SDimitry Andric 
11070b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
11080b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
11090b57cec5SDimitry Andric     if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
11100b57cec5SDimitry Andric       if (offset)
11110b57cec5SDimitry Andric         *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
11120b57cec5SDimitry Andric       return true;
11130b57cec5SDimitry Andric     }
11140b57cec5SDimitry Andric     return false;
11150b57cec5SDimitry Andric   }
11160b57cec5SDimitry Andric #endif
11170b57cec5SDimitry Andric 
11180b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386)
11190b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
11200b57cec5SDimitry Andric     if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
11210b57cec5SDimitry Andric       if (offset)
11220b57cec5SDimitry Andric         *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
11230b57cec5SDimitry Andric       return true;
11240b57cec5SDimitry Andric     }
11250b57cec5SDimitry Andric     return false;
11260b57cec5SDimitry Andric   }
11270b57cec5SDimitry Andric #endif
11280b57cec5SDimitry Andric 
11290b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC)
11300b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
11310b57cec5SDimitry Andric     return true;
11320b57cec5SDimitry Andric   }
11330b57cec5SDimitry Andric #endif
11340b57cec5SDimitry Andric 
11350b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64)
11360b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
11370b57cec5SDimitry Andric     return true;
11380b57cec5SDimitry Andric   }
11390b57cec5SDimitry Andric #endif
11400b57cec5SDimitry Andric 
11410b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
11420b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
11430b57cec5SDimitry Andric     if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
11440b57cec5SDimitry Andric       if (offset)
11450b57cec5SDimitry Andric         *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
11460b57cec5SDimitry Andric       return true;
11470b57cec5SDimitry Andric     }
11480b57cec5SDimitry Andric     return false;
11490b57cec5SDimitry Andric   }
11500b57cec5SDimitry Andric #endif
11510b57cec5SDimitry Andric 
11520b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_O32)
11530b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
11540b57cec5SDimitry Andric     return true;
11550b57cec5SDimitry Andric   }
11560b57cec5SDimitry Andric #endif
11570b57cec5SDimitry Andric 
11580b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
11590b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
11600b57cec5SDimitry Andric     return true;
11610b57cec5SDimitry Andric   }
11620b57cec5SDimitry Andric #endif
11630b57cec5SDimitry Andric 
1164bdd1243dSDimitry Andric #if defined(_LIBUNWIND_TARGET_LOONGARCH)
1165bdd1243dSDimitry Andric   bool compactSaysUseDwarf(Registers_loongarch &, uint32_t *) const {
1166bdd1243dSDimitry Andric     return true;
1167bdd1243dSDimitry Andric   }
1168bdd1243dSDimitry Andric #endif
1169bdd1243dSDimitry Andric 
11700b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC)
11710b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
11720b57cec5SDimitry Andric #endif
11730b57cec5SDimitry Andric 
1174d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64)
1175d56accc7SDimitry Andric   bool compactSaysUseDwarf(Registers_sparc64 &, uint32_t *) const {
1176d56accc7SDimitry Andric     return true;
1177d56accc7SDimitry Andric   }
1178d56accc7SDimitry Andric #endif
1179d56accc7SDimitry Andric 
1180480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV)
1181480093f4SDimitry Andric   bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const {
1182480093f4SDimitry Andric     return true;
1183480093f4SDimitry Andric   }
1184480093f4SDimitry Andric #endif
1185480093f4SDimitry Andric 
11860b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
11870b57cec5SDimitry Andric 
11880b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
11890b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding() const {
11900b57cec5SDimitry Andric     R dummy;
11910b57cec5SDimitry Andric     return dwarfEncoding(dummy);
11920b57cec5SDimitry Andric   }
11930b57cec5SDimitry Andric 
11940b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
11950b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
11960b57cec5SDimitry Andric     return UNWIND_X86_64_MODE_DWARF;
11970b57cec5SDimitry Andric   }
11980b57cec5SDimitry Andric #endif
11990b57cec5SDimitry Andric 
12000b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386)
12010b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
12020b57cec5SDimitry Andric     return UNWIND_X86_MODE_DWARF;
12030b57cec5SDimitry Andric   }
12040b57cec5SDimitry Andric #endif
12050b57cec5SDimitry Andric 
12060b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC)
12070b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
12080b57cec5SDimitry Andric     return 0;
12090b57cec5SDimitry Andric   }
12100b57cec5SDimitry Andric #endif
12110b57cec5SDimitry Andric 
12120b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64)
12130b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
12140b57cec5SDimitry Andric     return 0;
12150b57cec5SDimitry Andric   }
12160b57cec5SDimitry Andric #endif
12170b57cec5SDimitry Andric 
12180b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
12190b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
12200b57cec5SDimitry Andric     return UNWIND_ARM64_MODE_DWARF;
12210b57cec5SDimitry Andric   }
12220b57cec5SDimitry Andric #endif
12230b57cec5SDimitry Andric 
12240b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
12250b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
12260b57cec5SDimitry Andric     return 0;
12270b57cec5SDimitry Andric   }
12280b57cec5SDimitry Andric #endif
12290b57cec5SDimitry Andric 
12300b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_OR1K)
12310b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
12320b57cec5SDimitry Andric     return 0;
12330b57cec5SDimitry Andric   }
12340b57cec5SDimitry Andric #endif
12350b57cec5SDimitry Andric 
12365ffd83dbSDimitry Andric #if defined (_LIBUNWIND_TARGET_HEXAGON)
12375ffd83dbSDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const {
12385ffd83dbSDimitry Andric     return 0;
12395ffd83dbSDimitry Andric   }
12405ffd83dbSDimitry Andric #endif
12415ffd83dbSDimitry Andric 
12420b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_MIPS_O32)
12430b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
12440b57cec5SDimitry Andric     return 0;
12450b57cec5SDimitry Andric   }
12460b57cec5SDimitry Andric #endif
12470b57cec5SDimitry Andric 
12480b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
12490b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
12500b57cec5SDimitry Andric     return 0;
12510b57cec5SDimitry Andric   }
12520b57cec5SDimitry Andric #endif
12530b57cec5SDimitry Andric 
1254bdd1243dSDimitry Andric #if defined(_LIBUNWIND_TARGET_LOONGARCH)
1255bdd1243dSDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_loongarch &) const {
1256bdd1243dSDimitry Andric     return 0;
1257bdd1243dSDimitry Andric   }
1258bdd1243dSDimitry Andric #endif
1259bdd1243dSDimitry Andric 
12600b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC)
12610b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
12620b57cec5SDimitry Andric #endif
12630b57cec5SDimitry Andric 
1264d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64)
1265d56accc7SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_sparc64 &) const {
1266d56accc7SDimitry Andric     return 0;
1267d56accc7SDimitry Andric   }
1268d56accc7SDimitry Andric #endif
1269d56accc7SDimitry Andric 
1270480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV)
1271480093f4SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
1272480093f4SDimitry Andric     return 0;
1273480093f4SDimitry Andric   }
1274480093f4SDimitry Andric #endif
1275480093f4SDimitry Andric 
127681ad6265SDimitry Andric #if defined (_LIBUNWIND_TARGET_S390X)
127781ad6265SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_s390x &) const {
127881ad6265SDimitry Andric     return 0;
127981ad6265SDimitry Andric   }
128081ad6265SDimitry Andric #endif
128181ad6265SDimitry Andric 
12820b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
12830b57cec5SDimitry Andric 
12840b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
12850b57cec5SDimitry Andric   // For runtime environments using SEH unwind data without Windows runtime
12860b57cec5SDimitry Andric   // support.
12870b57cec5SDimitry Andric   pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
12880b57cec5SDimitry Andric   void setLastPC(pint_t pc) { /* FIXME: Implement */ }
12890b57cec5SDimitry Andric   RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
12900b57cec5SDimitry Andric     /* FIXME: Implement */
12910b57cec5SDimitry Andric     *base = 0;
12920b57cec5SDimitry Andric     return nullptr;
12930b57cec5SDimitry Andric   }
12940b57cec5SDimitry Andric   bool getInfoFromSEH(pint_t pc);
12950b57cec5SDimitry Andric   int stepWithSEHData() { /* FIXME: Implement */ return 0; }
12960b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
12970b57cec5SDimitry Andric 
129881ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
129981ad6265SDimitry Andric   bool getInfoFromTBTable(pint_t pc, R &registers);
130081ad6265SDimitry Andric   int stepWithTBTable(pint_t pc, tbtable *TBTable, R &registers,
130181ad6265SDimitry Andric                       bool &isSignalFrame);
130281ad6265SDimitry Andric   int stepWithTBTableData() {
130381ad6265SDimitry Andric     return stepWithTBTable(reinterpret_cast<pint_t>(this->getReg(UNW_REG_IP)),
130481ad6265SDimitry Andric                            reinterpret_cast<tbtable *>(_info.unwind_info),
130581ad6265SDimitry Andric                            _registers, _isSignalFrame);
130681ad6265SDimitry Andric   }
130781ad6265SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric   A               &_addressSpace;
13100b57cec5SDimitry Andric   R                _registers;
13110b57cec5SDimitry Andric   unw_proc_info_t  _info;
13120b57cec5SDimitry Andric   bool             _unwindInfoMissing;
13130b57cec5SDimitry Andric   bool             _isSignalFrame;
131481ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
1315e8d8bef9SDimitry Andric   bool             _isSigReturn = false;
1316e8d8bef9SDimitry Andric #endif
13170b57cec5SDimitry Andric };
13180b57cec5SDimitry Andric 
13190b57cec5SDimitry Andric 
13200b57cec5SDimitry Andric template <typename A, typename R>
13210b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
13220b57cec5SDimitry Andric     : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
13230b57cec5SDimitry Andric       _isSignalFrame(false) {
13240b57cec5SDimitry Andric   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
13250b57cec5SDimitry Andric                 "UnwindCursor<> does not fit in unw_cursor_t");
1326e8d8bef9SDimitry Andric   static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1327e8d8bef9SDimitry Andric                 "UnwindCursor<> requires more alignment than unw_cursor_t");
13280b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
13290b57cec5SDimitry Andric }
13300b57cec5SDimitry Andric 
13310b57cec5SDimitry Andric template <typename A, typename R>
13320b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(A &as, void *)
13330b57cec5SDimitry Andric     : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
13340b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
13350b57cec5SDimitry Andric   // FIXME
13360b57cec5SDimitry Andric   // fill in _registers from thread arg
13370b57cec5SDimitry Andric }
13380b57cec5SDimitry Andric 
13390b57cec5SDimitry Andric 
13400b57cec5SDimitry Andric template <typename A, typename R>
13410b57cec5SDimitry Andric bool UnwindCursor<A, R>::validReg(int regNum) {
13420b57cec5SDimitry Andric   return _registers.validRegister(regNum);
13430b57cec5SDimitry Andric }
13440b57cec5SDimitry Andric 
13450b57cec5SDimitry Andric template <typename A, typename R>
13460b57cec5SDimitry Andric unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
13470b57cec5SDimitry Andric   return _registers.getRegister(regNum);
13480b57cec5SDimitry Andric }
13490b57cec5SDimitry Andric 
13500b57cec5SDimitry Andric template <typename A, typename R>
13510b57cec5SDimitry Andric void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
13520b57cec5SDimitry Andric   _registers.setRegister(regNum, (typename A::pint_t)value);
13530b57cec5SDimitry Andric }
13540b57cec5SDimitry Andric 
13550b57cec5SDimitry Andric template <typename A, typename R>
13560b57cec5SDimitry Andric bool UnwindCursor<A, R>::validFloatReg(int regNum) {
13570b57cec5SDimitry Andric   return _registers.validFloatRegister(regNum);
13580b57cec5SDimitry Andric }
13590b57cec5SDimitry Andric 
13600b57cec5SDimitry Andric template <typename A, typename R>
13610b57cec5SDimitry Andric unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
13620b57cec5SDimitry Andric   return _registers.getFloatRegister(regNum);
13630b57cec5SDimitry Andric }
13640b57cec5SDimitry Andric 
13650b57cec5SDimitry Andric template <typename A, typename R>
13660b57cec5SDimitry Andric void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
13670b57cec5SDimitry Andric   _registers.setFloatRegister(regNum, value);
13680b57cec5SDimitry Andric }
13690b57cec5SDimitry Andric 
13700b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
13710b57cec5SDimitry Andric   _registers.jumpto();
13720b57cec5SDimitry Andric }
13730b57cec5SDimitry Andric 
13740b57cec5SDimitry Andric #ifdef __arm__
13750b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
13760b57cec5SDimitry Andric   _registers.saveVFPAsX();
13770b57cec5SDimitry Andric }
13780b57cec5SDimitry Andric #endif
13790b57cec5SDimitry Andric 
138081ad6265SDimitry Andric #ifdef _AIX
138181ad6265SDimitry Andric template <typename A, typename R>
138281ad6265SDimitry Andric uintptr_t UnwindCursor<A, R>::getDataRelBase() {
138381ad6265SDimitry Andric   return reinterpret_cast<uintptr_t>(_info.extra);
138481ad6265SDimitry Andric }
138581ad6265SDimitry Andric #endif
138681ad6265SDimitry Andric 
13870b57cec5SDimitry Andric template <typename A, typename R>
13880b57cec5SDimitry Andric const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
13890b57cec5SDimitry Andric   return _registers.getRegisterName(regNum);
13900b57cec5SDimitry Andric }
13910b57cec5SDimitry Andric 
13920b57cec5SDimitry Andric template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
13930b57cec5SDimitry Andric   return _isSignalFrame;
13940b57cec5SDimitry Andric }
13950b57cec5SDimitry Andric 
13960b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
13970b57cec5SDimitry Andric 
13980b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
13990b57cec5SDimitry Andric template<typename A>
14000b57cec5SDimitry Andric struct EHABISectionIterator {
14010b57cec5SDimitry Andric   typedef EHABISectionIterator _Self;
14020b57cec5SDimitry Andric 
14030b57cec5SDimitry Andric   typedef typename A::pint_t value_type;
14040b57cec5SDimitry Andric   typedef typename A::pint_t* pointer;
14050b57cec5SDimitry Andric   typedef typename A::pint_t& reference;
14060b57cec5SDimitry Andric   typedef size_t size_type;
14070b57cec5SDimitry Andric   typedef size_t difference_type;
14080b57cec5SDimitry Andric 
14090b57cec5SDimitry Andric   static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
14100b57cec5SDimitry Andric     return _Self(addressSpace, sects, 0);
14110b57cec5SDimitry Andric   }
14120b57cec5SDimitry Andric   static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
14130b57cec5SDimitry Andric     return _Self(addressSpace, sects,
14140b57cec5SDimitry Andric                  sects.arm_section_length / sizeof(EHABIIndexEntry));
14150b57cec5SDimitry Andric   }
14160b57cec5SDimitry Andric 
14170b57cec5SDimitry Andric   EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
14180b57cec5SDimitry Andric       : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
14190b57cec5SDimitry Andric 
14200b57cec5SDimitry Andric   _Self& operator++() { ++_i; return *this; }
14210b57cec5SDimitry Andric   _Self& operator+=(size_t a) { _i += a; return *this; }
14220b57cec5SDimitry Andric   _Self& operator--() { assert(_i > 0); --_i; return *this; }
14230b57cec5SDimitry Andric   _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
14240b57cec5SDimitry Andric 
14250b57cec5SDimitry Andric   _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
14260b57cec5SDimitry Andric   _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
14270b57cec5SDimitry Andric 
14285ffd83dbSDimitry Andric   size_t operator-(const _Self& other) const { return _i - other._i; }
14290b57cec5SDimitry Andric 
14300b57cec5SDimitry Andric   bool operator==(const _Self& other) const {
14310b57cec5SDimitry Andric     assert(_addressSpace == other._addressSpace);
14320b57cec5SDimitry Andric     assert(_sects == other._sects);
14330b57cec5SDimitry Andric     return _i == other._i;
14340b57cec5SDimitry Andric   }
14350b57cec5SDimitry Andric 
14365ffd83dbSDimitry Andric   bool operator!=(const _Self& other) const {
14375ffd83dbSDimitry Andric     assert(_addressSpace == other._addressSpace);
14385ffd83dbSDimitry Andric     assert(_sects == other._sects);
14395ffd83dbSDimitry Andric     return _i != other._i;
14405ffd83dbSDimitry Andric   }
14415ffd83dbSDimitry Andric 
14420b57cec5SDimitry Andric   typename A::pint_t operator*() const { return functionAddress(); }
14430b57cec5SDimitry Andric 
14440b57cec5SDimitry Andric   typename A::pint_t functionAddress() const {
14450b57cec5SDimitry Andric     typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
14460b57cec5SDimitry Andric         EHABIIndexEntry, _i, functionOffset);
14470b57cec5SDimitry Andric     return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
14480b57cec5SDimitry Andric   }
14490b57cec5SDimitry Andric 
14500b57cec5SDimitry Andric   typename A::pint_t dataAddress() {
14510b57cec5SDimitry Andric     typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
14520b57cec5SDimitry Andric         EHABIIndexEntry, _i, data);
14530b57cec5SDimitry Andric     return indexAddr;
14540b57cec5SDimitry Andric   }
14550b57cec5SDimitry Andric 
14560b57cec5SDimitry Andric  private:
14570b57cec5SDimitry Andric   size_t _i;
14580b57cec5SDimitry Andric   A* _addressSpace;
14590b57cec5SDimitry Andric   const UnwindInfoSections* _sects;
14600b57cec5SDimitry Andric };
14610b57cec5SDimitry Andric 
14620b57cec5SDimitry Andric namespace {
14630b57cec5SDimitry Andric 
14640b57cec5SDimitry Andric template <typename A>
14650b57cec5SDimitry Andric EHABISectionIterator<A> EHABISectionUpperBound(
14660b57cec5SDimitry Andric     EHABISectionIterator<A> first,
14670b57cec5SDimitry Andric     EHABISectionIterator<A> last,
14680b57cec5SDimitry Andric     typename A::pint_t value) {
14690b57cec5SDimitry Andric   size_t len = last - first;
14700b57cec5SDimitry Andric   while (len > 0) {
14710b57cec5SDimitry Andric     size_t l2 = len / 2;
14720b57cec5SDimitry Andric     EHABISectionIterator<A> m = first + l2;
14730b57cec5SDimitry Andric     if (value < *m) {
14740b57cec5SDimitry Andric         len = l2;
14750b57cec5SDimitry Andric     } else {
14760b57cec5SDimitry Andric         first = ++m;
14770b57cec5SDimitry Andric         len -= l2 + 1;
14780b57cec5SDimitry Andric     }
14790b57cec5SDimitry Andric   }
14800b57cec5SDimitry Andric   return first;
14810b57cec5SDimitry Andric }
14820b57cec5SDimitry Andric 
14830b57cec5SDimitry Andric }
14840b57cec5SDimitry Andric 
14850b57cec5SDimitry Andric template <typename A, typename R>
14860b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromEHABISection(
14870b57cec5SDimitry Andric     pint_t pc,
14880b57cec5SDimitry Andric     const UnwindInfoSections &sects) {
14890b57cec5SDimitry Andric   EHABISectionIterator<A> begin =
14900b57cec5SDimitry Andric       EHABISectionIterator<A>::begin(_addressSpace, sects);
14910b57cec5SDimitry Andric   EHABISectionIterator<A> end =
14920b57cec5SDimitry Andric       EHABISectionIterator<A>::end(_addressSpace, sects);
14930b57cec5SDimitry Andric   if (begin == end)
14940b57cec5SDimitry Andric     return false;
14950b57cec5SDimitry Andric 
14960b57cec5SDimitry Andric   EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
14970b57cec5SDimitry Andric   if (itNextPC == begin)
14980b57cec5SDimitry Andric     return false;
14990b57cec5SDimitry Andric   EHABISectionIterator<A> itThisPC = itNextPC - 1;
15000b57cec5SDimitry Andric 
15010b57cec5SDimitry Andric   pint_t thisPC = itThisPC.functionAddress();
15020b57cec5SDimitry Andric   // If an exception is thrown from a function, corresponding to the last entry
15030b57cec5SDimitry Andric   // in the table, we don't really know the function extent and have to choose a
15040b57cec5SDimitry Andric   // value for nextPC. Choosing max() will allow the range check during trace to
15050b57cec5SDimitry Andric   // succeed.
15060b57cec5SDimitry Andric   pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
15070b57cec5SDimitry Andric   pint_t indexDataAddr = itThisPC.dataAddress();
15080b57cec5SDimitry Andric 
15090b57cec5SDimitry Andric   if (indexDataAddr == 0)
15100b57cec5SDimitry Andric     return false;
15110b57cec5SDimitry Andric 
15120b57cec5SDimitry Andric   uint32_t indexData = _addressSpace.get32(indexDataAddr);
15130b57cec5SDimitry Andric   if (indexData == UNW_EXIDX_CANTUNWIND)
15140b57cec5SDimitry Andric     return false;
15150b57cec5SDimitry Andric 
15160b57cec5SDimitry Andric   // If the high bit is set, the exception handling table entry is inline inside
15170b57cec5SDimitry Andric   // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
1518473b61d3SDimitry Andric   // the table points at an offset in the exception handling table (section 5
1519473b61d3SDimitry Andric   // EHABI).
15200b57cec5SDimitry Andric   pint_t exceptionTableAddr;
15210b57cec5SDimitry Andric   uint32_t exceptionTableData;
15220b57cec5SDimitry Andric   bool isSingleWordEHT;
15230b57cec5SDimitry Andric   if (indexData & 0x80000000) {
15240b57cec5SDimitry Andric     exceptionTableAddr = indexDataAddr;
15250b57cec5SDimitry Andric     // TODO(ajwong): Should this data be 0?
15260b57cec5SDimitry Andric     exceptionTableData = indexData;
15270b57cec5SDimitry Andric     isSingleWordEHT = true;
15280b57cec5SDimitry Andric   } else {
15290b57cec5SDimitry Andric     exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
15300b57cec5SDimitry Andric     exceptionTableData = _addressSpace.get32(exceptionTableAddr);
15310b57cec5SDimitry Andric     isSingleWordEHT = false;
15320b57cec5SDimitry Andric   }
15330b57cec5SDimitry Andric 
15340b57cec5SDimitry Andric   // Now we know the 3 things:
15350b57cec5SDimitry Andric   //   exceptionTableAddr -- exception handler table entry.
15360b57cec5SDimitry Andric   //   exceptionTableData -- the data inside the first word of the eht entry.
15370b57cec5SDimitry Andric   //   isSingleWordEHT -- whether the entry is in the index.
15380b57cec5SDimitry Andric   unw_word_t personalityRoutine = 0xbadf00d;
15390b57cec5SDimitry Andric   bool scope32 = false;
15400b57cec5SDimitry Andric   uintptr_t lsda;
15410b57cec5SDimitry Andric 
15420b57cec5SDimitry Andric   // If the high bit in the exception handling table entry is set, the entry is
15430b57cec5SDimitry Andric   // in compact form (section 6.3 EHABI).
15440b57cec5SDimitry Andric   if (exceptionTableData & 0x80000000) {
15450b57cec5SDimitry Andric     // Grab the index of the personality routine from the compact form.
15460b57cec5SDimitry Andric     uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
15470b57cec5SDimitry Andric     uint32_t extraWords = 0;
15480b57cec5SDimitry Andric     switch (choice) {
15490b57cec5SDimitry Andric       case 0:
15500b57cec5SDimitry Andric         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
15510b57cec5SDimitry Andric         extraWords = 0;
15520b57cec5SDimitry Andric         scope32 = false;
15530b57cec5SDimitry Andric         lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
15540b57cec5SDimitry Andric         break;
15550b57cec5SDimitry Andric       case 1:
15560b57cec5SDimitry Andric         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
15570b57cec5SDimitry Andric         extraWords = (exceptionTableData & 0x00ff0000) >> 16;
15580b57cec5SDimitry Andric         scope32 = false;
15590b57cec5SDimitry Andric         lsda = exceptionTableAddr + (extraWords + 1) * 4;
15600b57cec5SDimitry Andric         break;
15610b57cec5SDimitry Andric       case 2:
15620b57cec5SDimitry Andric         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
15630b57cec5SDimitry Andric         extraWords = (exceptionTableData & 0x00ff0000) >> 16;
15640b57cec5SDimitry Andric         scope32 = true;
15650b57cec5SDimitry Andric         lsda = exceptionTableAddr + (extraWords + 1) * 4;
15660b57cec5SDimitry Andric         break;
15670b57cec5SDimitry Andric       default:
15680b57cec5SDimitry Andric         _LIBUNWIND_ABORT("unknown personality routine");
15690b57cec5SDimitry Andric         return false;
15700b57cec5SDimitry Andric     }
15710b57cec5SDimitry Andric 
15720b57cec5SDimitry Andric     if (isSingleWordEHT) {
15730b57cec5SDimitry Andric       if (extraWords != 0) {
15740b57cec5SDimitry Andric         _LIBUNWIND_ABORT("index inlined table detected but pr function "
15750b57cec5SDimitry Andric                          "requires extra words");
15760b57cec5SDimitry Andric         return false;
15770b57cec5SDimitry Andric       }
15780b57cec5SDimitry Andric     }
15790b57cec5SDimitry Andric   } else {
15800b57cec5SDimitry Andric     pint_t personalityAddr =
15810b57cec5SDimitry Andric         exceptionTableAddr + signExtendPrel31(exceptionTableData);
15820b57cec5SDimitry Andric     personalityRoutine = personalityAddr;
15830b57cec5SDimitry Andric 
15840b57cec5SDimitry Andric     // ARM EHABI # 6.2, # 9.2
15850b57cec5SDimitry Andric     //
15860b57cec5SDimitry Andric     //  +---- ehtp
15870b57cec5SDimitry Andric     //  v
15880b57cec5SDimitry Andric     // +--------------------------------------+
15890b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15900b57cec5SDimitry Andric     // | |0| prel31 to personalityRoutine   | |
15910b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15920b57cec5SDimitry Andric     // | |      N |      unwind opcodes     | |  <-- UnwindData
15930b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15940b57cec5SDimitry Andric     // | | Word 2        unwind opcodes     | |
15950b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15960b57cec5SDimitry Andric     // | ...                                  |
15970b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15980b57cec5SDimitry Andric     // | | Word N        unwind opcodes     | |
15990b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
16000b57cec5SDimitry Andric     // | | LSDA                             | |  <-- lsda
16010b57cec5SDimitry Andric     // | | ...                              | |
16020b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
16030b57cec5SDimitry Andric     // +--------------------------------------+
16040b57cec5SDimitry Andric 
16050b57cec5SDimitry Andric     uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
16060b57cec5SDimitry Andric     uint32_t FirstDataWord = *UnwindData;
16070b57cec5SDimitry Andric     size_t N = ((FirstDataWord >> 24) & 0xff);
16080b57cec5SDimitry Andric     size_t NDataWords = N + 1;
16090b57cec5SDimitry Andric     lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
16100b57cec5SDimitry Andric   }
16110b57cec5SDimitry Andric 
16120b57cec5SDimitry Andric   _info.start_ip = thisPC;
16130b57cec5SDimitry Andric   _info.end_ip = nextPC;
16140b57cec5SDimitry Andric   _info.handler = personalityRoutine;
16150b57cec5SDimitry Andric   _info.unwind_info = exceptionTableAddr;
16160b57cec5SDimitry Andric   _info.lsda = lsda;
16170b57cec5SDimitry Andric   // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
1618473b61d3SDimitry Andric   _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0);  // Use enum?
16190b57cec5SDimitry Andric 
16200b57cec5SDimitry Andric   return true;
16210b57cec5SDimitry Andric }
16220b57cec5SDimitry Andric #endif
16230b57cec5SDimitry Andric 
16240b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
16250b57cec5SDimitry Andric template <typename A, typename R>
1626e8d8bef9SDimitry Andric bool UnwindCursor<A, R>::getInfoFromFdeCie(
1627e8d8bef9SDimitry Andric     const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1628e8d8bef9SDimitry Andric     const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc,
1629e8d8bef9SDimitry Andric     uintptr_t dso_base) {
1630e8d8bef9SDimitry Andric   typename CFI_Parser<A>::PrologInfo prolog;
1631e8d8bef9SDimitry Andric   if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1632e8d8bef9SDimitry Andric                                           R::getArch(), &prolog)) {
1633e8d8bef9SDimitry Andric     // Save off parsed FDE info
1634e8d8bef9SDimitry Andric     _info.start_ip          = fdeInfo.pcStart;
1635e8d8bef9SDimitry Andric     _info.end_ip            = fdeInfo.pcEnd;
1636e8d8bef9SDimitry Andric     _info.lsda              = fdeInfo.lsda;
1637e8d8bef9SDimitry Andric     _info.handler           = cieInfo.personality;
1638e8d8bef9SDimitry Andric     // Some frameless functions need SP altered when resuming in function, so
1639e8d8bef9SDimitry Andric     // propagate spExtraArgSize.
1640e8d8bef9SDimitry Andric     _info.gp                = prolog.spExtraArgSize;
1641e8d8bef9SDimitry Andric     _info.flags             = 0;
1642e8d8bef9SDimitry Andric     _info.format            = dwarfEncoding();
1643e8d8bef9SDimitry Andric     _info.unwind_info       = fdeInfo.fdeStart;
1644e8d8bef9SDimitry Andric     _info.unwind_info_size  = static_cast<uint32_t>(fdeInfo.fdeLength);
1645e8d8bef9SDimitry Andric     _info.extra             = static_cast<unw_word_t>(dso_base);
1646e8d8bef9SDimitry Andric     return true;
1647e8d8bef9SDimitry Andric   }
1648e8d8bef9SDimitry Andric   return false;
1649e8d8bef9SDimitry Andric }
1650e8d8bef9SDimitry Andric 
1651e8d8bef9SDimitry Andric template <typename A, typename R>
16520b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
16530b57cec5SDimitry Andric                                                 const UnwindInfoSections &sects,
16540b57cec5SDimitry Andric                                                 uint32_t fdeSectionOffsetHint) {
16550b57cec5SDimitry Andric   typename CFI_Parser<A>::FDE_Info fdeInfo;
16560b57cec5SDimitry Andric   typename CFI_Parser<A>::CIE_Info cieInfo;
16570b57cec5SDimitry Andric   bool foundFDE = false;
16580b57cec5SDimitry Andric   bool foundInCache = false;
16590b57cec5SDimitry Andric   // If compact encoding table gave offset into dwarf section, go directly there
16600b57cec5SDimitry Andric   if (fdeSectionOffsetHint != 0) {
16610b57cec5SDimitry Andric     foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1662e8d8bef9SDimitry Andric                                     sects.dwarf_section_length,
16630b57cec5SDimitry Andric                                     sects.dwarf_section + fdeSectionOffsetHint,
16640b57cec5SDimitry Andric                                     &fdeInfo, &cieInfo);
16650b57cec5SDimitry Andric   }
16660b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
16670b57cec5SDimitry Andric   if (!foundFDE && (sects.dwarf_index_section != 0)) {
16680b57cec5SDimitry Andric     foundFDE = EHHeaderParser<A>::findFDE(
16690b57cec5SDimitry Andric         _addressSpace, pc, sects.dwarf_index_section,
16700b57cec5SDimitry Andric         (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
16710b57cec5SDimitry Andric   }
16720b57cec5SDimitry Andric #endif
16730b57cec5SDimitry Andric   if (!foundFDE) {
16740b57cec5SDimitry Andric     // otherwise, search cache of previously found FDEs.
16750b57cec5SDimitry Andric     pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
16760b57cec5SDimitry Andric     if (cachedFDE != 0) {
16770b57cec5SDimitry Andric       foundFDE =
16780b57cec5SDimitry Andric           CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1679e8d8bef9SDimitry Andric                                  sects.dwarf_section_length,
16800b57cec5SDimitry Andric                                  cachedFDE, &fdeInfo, &cieInfo);
16810b57cec5SDimitry Andric       foundInCache = foundFDE;
16820b57cec5SDimitry Andric     }
16830b57cec5SDimitry Andric   }
16840b57cec5SDimitry Andric   if (!foundFDE) {
16850b57cec5SDimitry Andric     // Still not found, do full scan of __eh_frame section.
16860b57cec5SDimitry Andric     foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1687e8d8bef9SDimitry Andric                                       sects.dwarf_section_length, 0,
16880b57cec5SDimitry Andric                                       &fdeInfo, &cieInfo);
16890b57cec5SDimitry Andric   }
16900b57cec5SDimitry Andric   if (foundFDE) {
1691e8d8bef9SDimitry Andric     if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) {
16920b57cec5SDimitry Andric       // Add to cache (to make next lookup faster) if we had no hint
16930b57cec5SDimitry Andric       // and there was no index.
16940b57cec5SDimitry Andric       if (!foundInCache && (fdeSectionOffsetHint == 0)) {
16950b57cec5SDimitry Andric   #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
16960b57cec5SDimitry Andric         if (sects.dwarf_index_section == 0)
16970b57cec5SDimitry Andric   #endif
16980b57cec5SDimitry Andric         DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
16990b57cec5SDimitry Andric                               fdeInfo.fdeStart);
17000b57cec5SDimitry Andric       }
17010b57cec5SDimitry Andric       return true;
17020b57cec5SDimitry Andric     }
17030b57cec5SDimitry Andric   }
17040b57cec5SDimitry Andric   //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
17050b57cec5SDimitry Andric   return false;
17060b57cec5SDimitry Andric }
17070b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
17080b57cec5SDimitry Andric 
17090b57cec5SDimitry Andric 
17100b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
17110b57cec5SDimitry Andric template <typename A, typename R>
17120b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
17130b57cec5SDimitry Andric                                               const UnwindInfoSections &sects) {
17140b57cec5SDimitry Andric   const bool log = false;
17150b57cec5SDimitry Andric   if (log)
17160b57cec5SDimitry Andric     fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
17170b57cec5SDimitry Andric             (uint64_t)pc, (uint64_t)sects.dso_base);
17180b57cec5SDimitry Andric 
17190b57cec5SDimitry Andric   const UnwindSectionHeader<A> sectionHeader(_addressSpace,
17200b57cec5SDimitry Andric                                                 sects.compact_unwind_section);
17210b57cec5SDimitry Andric   if (sectionHeader.version() != UNWIND_SECTION_VERSION)
17220b57cec5SDimitry Andric     return false;
17230b57cec5SDimitry Andric 
17240b57cec5SDimitry Andric   // do a binary search of top level index to find page with unwind info
17250b57cec5SDimitry Andric   pint_t targetFunctionOffset = pc - sects.dso_base;
17260b57cec5SDimitry Andric   const UnwindSectionIndexArray<A> topIndex(_addressSpace,
17270b57cec5SDimitry Andric                                            sects.compact_unwind_section
17280b57cec5SDimitry Andric                                          + sectionHeader.indexSectionOffset());
17290b57cec5SDimitry Andric   uint32_t low = 0;
17300b57cec5SDimitry Andric   uint32_t high = sectionHeader.indexCount();
17310b57cec5SDimitry Andric   uint32_t last = high - 1;
17320b57cec5SDimitry Andric   while (low < high) {
17330b57cec5SDimitry Andric     uint32_t mid = (low + high) / 2;
17340b57cec5SDimitry Andric     //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
17350b57cec5SDimitry Andric     //mid, low, high, topIndex.functionOffset(mid));
17360b57cec5SDimitry Andric     if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
17370b57cec5SDimitry Andric       if ((mid == last) ||
17380b57cec5SDimitry Andric           (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
17390b57cec5SDimitry Andric         low = mid;
17400b57cec5SDimitry Andric         break;
17410b57cec5SDimitry Andric       } else {
17420b57cec5SDimitry Andric         low = mid + 1;
17430b57cec5SDimitry Andric       }
17440b57cec5SDimitry Andric     } else {
17450b57cec5SDimitry Andric       high = mid;
17460b57cec5SDimitry Andric     }
17470b57cec5SDimitry Andric   }
17480b57cec5SDimitry Andric   const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
17490b57cec5SDimitry Andric   const uint32_t firstLevelNextPageFunctionOffset =
17500b57cec5SDimitry Andric       topIndex.functionOffset(low + 1);
17510b57cec5SDimitry Andric   const pint_t secondLevelAddr =
17520b57cec5SDimitry Andric       sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
17530b57cec5SDimitry Andric   const pint_t lsdaArrayStartAddr =
17540b57cec5SDimitry Andric       sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
17550b57cec5SDimitry Andric   const pint_t lsdaArrayEndAddr =
17560b57cec5SDimitry Andric       sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
17570b57cec5SDimitry Andric   if (log)
17580b57cec5SDimitry Andric     fprintf(stderr, "\tfirst level search for result index=%d "
17590b57cec5SDimitry Andric                     "to secondLevelAddr=0x%llX\n",
17600b57cec5SDimitry Andric                     low, (uint64_t) secondLevelAddr);
17610b57cec5SDimitry Andric   // do a binary search of second level page index
17620b57cec5SDimitry Andric   uint32_t encoding = 0;
17630b57cec5SDimitry Andric   pint_t funcStart = 0;
17640b57cec5SDimitry Andric   pint_t funcEnd = 0;
17650b57cec5SDimitry Andric   pint_t lsda = 0;
17660b57cec5SDimitry Andric   pint_t personality = 0;
17670b57cec5SDimitry Andric   uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
17680b57cec5SDimitry Andric   if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
17690b57cec5SDimitry Andric     // regular page
17700b57cec5SDimitry Andric     UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
17710b57cec5SDimitry Andric                                                  secondLevelAddr);
17720b57cec5SDimitry Andric     UnwindSectionRegularArray<A> pageIndex(
17730b57cec5SDimitry Andric         _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
17740b57cec5SDimitry Andric     // binary search looks for entry with e where index[e].offset <= pc <
17750b57cec5SDimitry Andric     // index[e+1].offset
17760b57cec5SDimitry Andric     if (log)
17770b57cec5SDimitry Andric       fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
17780b57cec5SDimitry Andric                       "regular page starting at secondLevelAddr=0x%llX\n",
17790b57cec5SDimitry Andric               (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
17800b57cec5SDimitry Andric     low = 0;
17810b57cec5SDimitry Andric     high = pageHeader.entryCount();
17820b57cec5SDimitry Andric     while (low < high) {
17830b57cec5SDimitry Andric       uint32_t mid = (low + high) / 2;
17840b57cec5SDimitry Andric       if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
17850b57cec5SDimitry Andric         if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
17860b57cec5SDimitry Andric           // at end of table
17870b57cec5SDimitry Andric           low = mid;
17880b57cec5SDimitry Andric           funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
17890b57cec5SDimitry Andric           break;
17900b57cec5SDimitry Andric         } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
17910b57cec5SDimitry Andric           // next is too big, so we found it
17920b57cec5SDimitry Andric           low = mid;
17930b57cec5SDimitry Andric           funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
17940b57cec5SDimitry Andric           break;
17950b57cec5SDimitry Andric         } else {
17960b57cec5SDimitry Andric           low = mid + 1;
17970b57cec5SDimitry Andric         }
17980b57cec5SDimitry Andric       } else {
17990b57cec5SDimitry Andric         high = mid;
18000b57cec5SDimitry Andric       }
18010b57cec5SDimitry Andric     }
18020b57cec5SDimitry Andric     encoding = pageIndex.encoding(low);
18030b57cec5SDimitry Andric     funcStart = pageIndex.functionOffset(low) + sects.dso_base;
18040b57cec5SDimitry Andric     if (pc < funcStart) {
18050b57cec5SDimitry Andric       if (log)
18060b57cec5SDimitry Andric         fprintf(
18070b57cec5SDimitry Andric             stderr,
18080b57cec5SDimitry Andric             "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
18090b57cec5SDimitry Andric             (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
18100b57cec5SDimitry Andric       return false;
18110b57cec5SDimitry Andric     }
18120b57cec5SDimitry Andric     if (pc > funcEnd) {
18130b57cec5SDimitry Andric       if (log)
18140b57cec5SDimitry Andric         fprintf(
18150b57cec5SDimitry Andric             stderr,
18160b57cec5SDimitry Andric             "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
18170b57cec5SDimitry Andric             (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
18180b57cec5SDimitry Andric       return false;
18190b57cec5SDimitry Andric     }
18200b57cec5SDimitry Andric   } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
18210b57cec5SDimitry Andric     // compressed page
18220b57cec5SDimitry Andric     UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
18230b57cec5SDimitry Andric                                                     secondLevelAddr);
18240b57cec5SDimitry Andric     UnwindSectionCompressedArray<A> pageIndex(
18250b57cec5SDimitry Andric         _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
18260b57cec5SDimitry Andric     const uint32_t targetFunctionPageOffset =
18270b57cec5SDimitry Andric         (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
18280b57cec5SDimitry Andric     // binary search looks for entry with e where index[e].offset <= pc <
18290b57cec5SDimitry Andric     // index[e+1].offset
18300b57cec5SDimitry Andric     if (log)
18310b57cec5SDimitry Andric       fprintf(stderr, "\tbinary search of compressed page starting at "
18320b57cec5SDimitry Andric                       "secondLevelAddr=0x%llX\n",
18330b57cec5SDimitry Andric               (uint64_t) secondLevelAddr);
18340b57cec5SDimitry Andric     low = 0;
18350b57cec5SDimitry Andric     last = pageHeader.entryCount() - 1;
18360b57cec5SDimitry Andric     high = pageHeader.entryCount();
18370b57cec5SDimitry Andric     while (low < high) {
18380b57cec5SDimitry Andric       uint32_t mid = (low + high) / 2;
18390b57cec5SDimitry Andric       if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
18400b57cec5SDimitry Andric         if ((mid == last) ||
18410b57cec5SDimitry Andric             (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
18420b57cec5SDimitry Andric           low = mid;
18430b57cec5SDimitry Andric           break;
18440b57cec5SDimitry Andric         } else {
18450b57cec5SDimitry Andric           low = mid + 1;
18460b57cec5SDimitry Andric         }
18470b57cec5SDimitry Andric       } else {
18480b57cec5SDimitry Andric         high = mid;
18490b57cec5SDimitry Andric       }
18500b57cec5SDimitry Andric     }
18510b57cec5SDimitry Andric     funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
18520b57cec5SDimitry Andric                                                               + sects.dso_base;
18530b57cec5SDimitry Andric     if (low < last)
18540b57cec5SDimitry Andric       funcEnd =
18550b57cec5SDimitry Andric           pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
18560b57cec5SDimitry Andric                                                               + sects.dso_base;
18570b57cec5SDimitry Andric     else
18580b57cec5SDimitry Andric       funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
18590b57cec5SDimitry Andric     if (pc < funcStart) {
1860fe6060f1SDimitry Andric       _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1861fe6060f1SDimitry Andric                            "not in second level compressed unwind table. "
1862fe6060f1SDimitry Andric                            "funcStart=0x%llX",
18630b57cec5SDimitry Andric                             (uint64_t) pc, (uint64_t) funcStart);
18640b57cec5SDimitry Andric       return false;
18650b57cec5SDimitry Andric     }
18660b57cec5SDimitry Andric     if (pc > funcEnd) {
1867fe6060f1SDimitry Andric       _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1868fe6060f1SDimitry Andric                            "not in second level compressed unwind table. "
1869fe6060f1SDimitry Andric                            "funcEnd=0x%llX",
18700b57cec5SDimitry Andric                            (uint64_t) pc, (uint64_t) funcEnd);
18710b57cec5SDimitry Andric       return false;
18720b57cec5SDimitry Andric     }
18730b57cec5SDimitry Andric     uint16_t encodingIndex = pageIndex.encodingIndex(low);
18740b57cec5SDimitry Andric     if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
18750b57cec5SDimitry Andric       // encoding is in common table in section header
18760b57cec5SDimitry Andric       encoding = _addressSpace.get32(
18770b57cec5SDimitry Andric           sects.compact_unwind_section +
18780b57cec5SDimitry Andric           sectionHeader.commonEncodingsArraySectionOffset() +
18790b57cec5SDimitry Andric           encodingIndex * sizeof(uint32_t));
18800b57cec5SDimitry Andric     } else {
18810b57cec5SDimitry Andric       // encoding is in page specific table
18820b57cec5SDimitry Andric       uint16_t pageEncodingIndex =
18830b57cec5SDimitry Andric           encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
18840b57cec5SDimitry Andric       encoding = _addressSpace.get32(secondLevelAddr +
18850b57cec5SDimitry Andric                                      pageHeader.encodingsPageOffset() +
18860b57cec5SDimitry Andric                                      pageEncodingIndex * sizeof(uint32_t));
18870b57cec5SDimitry Andric     }
18880b57cec5SDimitry Andric   } else {
1889fe6060f1SDimitry Andric     _LIBUNWIND_DEBUG_LOG(
1890fe6060f1SDimitry Andric         "malformed __unwind_info at 0x%0llX bad second level page",
18910b57cec5SDimitry Andric         (uint64_t)sects.compact_unwind_section);
18920b57cec5SDimitry Andric     return false;
18930b57cec5SDimitry Andric   }
18940b57cec5SDimitry Andric 
18950b57cec5SDimitry Andric   // look up LSDA, if encoding says function has one
18960b57cec5SDimitry Andric   if (encoding & UNWIND_HAS_LSDA) {
18970b57cec5SDimitry Andric     UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
18980b57cec5SDimitry Andric     uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
18990b57cec5SDimitry Andric     low = 0;
19000b57cec5SDimitry Andric     high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
19010b57cec5SDimitry Andric                     sizeof(unwind_info_section_header_lsda_index_entry);
19020b57cec5SDimitry Andric     // binary search looks for entry with exact match for functionOffset
19030b57cec5SDimitry Andric     if (log)
19040b57cec5SDimitry Andric       fprintf(stderr,
19050b57cec5SDimitry Andric               "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
19060b57cec5SDimitry Andric               funcStartOffset);
19070b57cec5SDimitry Andric     while (low < high) {
19080b57cec5SDimitry Andric       uint32_t mid = (low + high) / 2;
19090b57cec5SDimitry Andric       if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
19100b57cec5SDimitry Andric         lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
19110b57cec5SDimitry Andric         break;
19120b57cec5SDimitry Andric       } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
19130b57cec5SDimitry Andric         low = mid + 1;
19140b57cec5SDimitry Andric       } else {
19150b57cec5SDimitry Andric         high = mid;
19160b57cec5SDimitry Andric       }
19170b57cec5SDimitry Andric     }
19180b57cec5SDimitry Andric     if (lsda == 0) {
19190b57cec5SDimitry Andric       _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
19200b57cec5SDimitry Andric                     "pc=0x%0llX, but lsda table has no entry",
19210b57cec5SDimitry Andric                     encoding, (uint64_t) pc);
19220b57cec5SDimitry Andric       return false;
19230b57cec5SDimitry Andric     }
19240b57cec5SDimitry Andric   }
19250b57cec5SDimitry Andric 
1926e8d8bef9SDimitry Andric   // extract personality routine, if encoding says function has one
19270b57cec5SDimitry Andric   uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
19280b57cec5SDimitry Andric                               (__builtin_ctz(UNWIND_PERSONALITY_MASK));
19290b57cec5SDimitry Andric   if (personalityIndex != 0) {
19300b57cec5SDimitry Andric     --personalityIndex; // change 1-based to zero-based index
1931e8d8bef9SDimitry Andric     if (personalityIndex >= sectionHeader.personalityArrayCount()) {
19320b57cec5SDimitry Andric       _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d,  "
19330b57cec5SDimitry Andric                             "but personality table has only %d entries",
19340b57cec5SDimitry Andric                             encoding, personalityIndex,
19350b57cec5SDimitry Andric                             sectionHeader.personalityArrayCount());
19360b57cec5SDimitry Andric       return false;
19370b57cec5SDimitry Andric     }
19380b57cec5SDimitry Andric     int32_t personalityDelta = (int32_t)_addressSpace.get32(
19390b57cec5SDimitry Andric         sects.compact_unwind_section +
19400b57cec5SDimitry Andric         sectionHeader.personalityArraySectionOffset() +
19410b57cec5SDimitry Andric         personalityIndex * sizeof(uint32_t));
19420b57cec5SDimitry Andric     pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
19430b57cec5SDimitry Andric     personality = _addressSpace.getP(personalityPointer);
19440b57cec5SDimitry Andric     if (log)
19450b57cec5SDimitry Andric       fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
19460b57cec5SDimitry Andric                       "personalityDelta=0x%08X, personality=0x%08llX\n",
19470b57cec5SDimitry Andric               (uint64_t) pc, personalityDelta, (uint64_t) personality);
19480b57cec5SDimitry Andric   }
19490b57cec5SDimitry Andric 
19500b57cec5SDimitry Andric   if (log)
19510b57cec5SDimitry Andric     fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
19520b57cec5SDimitry Andric                     "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
19530b57cec5SDimitry Andric             (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
19540b57cec5SDimitry Andric   _info.start_ip = funcStart;
19550b57cec5SDimitry Andric   _info.end_ip = funcEnd;
19560b57cec5SDimitry Andric   _info.lsda = lsda;
19570b57cec5SDimitry Andric   _info.handler = personality;
19580b57cec5SDimitry Andric   _info.gp = 0;
19590b57cec5SDimitry Andric   _info.flags = 0;
19600b57cec5SDimitry Andric   _info.format = encoding;
19610b57cec5SDimitry Andric   _info.unwind_info = 0;
19620b57cec5SDimitry Andric   _info.unwind_info_size = 0;
19630b57cec5SDimitry Andric   _info.extra = sects.dso_base;
19640b57cec5SDimitry Andric   return true;
19650b57cec5SDimitry Andric }
19660b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
19670b57cec5SDimitry Andric 
19680b57cec5SDimitry Andric 
19690b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
19700b57cec5SDimitry Andric template <typename A, typename R>
19710b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
19720b57cec5SDimitry Andric   pint_t base;
19730b57cec5SDimitry Andric   RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
19740b57cec5SDimitry Andric   if (!unwindEntry) {
19750b57cec5SDimitry Andric     _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
19760b57cec5SDimitry Andric     return false;
19770b57cec5SDimitry Andric   }
19780b57cec5SDimitry Andric   _info.gp = 0;
19790b57cec5SDimitry Andric   _info.flags = 0;
19800b57cec5SDimitry Andric   _info.format = 0;
19810b57cec5SDimitry Andric   _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
19820b57cec5SDimitry Andric   _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
19830b57cec5SDimitry Andric   _info.extra = base;
19840b57cec5SDimitry Andric   _info.start_ip = base + unwindEntry->BeginAddress;
19850b57cec5SDimitry Andric #ifdef _LIBUNWIND_TARGET_X86_64
19860b57cec5SDimitry Andric   _info.end_ip = base + unwindEntry->EndAddress;
19870b57cec5SDimitry Andric   // Only fill in the handler and LSDA if they're stale.
19880b57cec5SDimitry Andric   if (pc != getLastPC()) {
19890b57cec5SDimitry Andric     UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
19900b57cec5SDimitry Andric     if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
19910b57cec5SDimitry Andric       // The personality is given in the UNWIND_INFO itself. The LSDA immediately
19920b57cec5SDimitry Andric       // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
19930b57cec5SDimitry Andric       // these structures.)
19940b57cec5SDimitry Andric       // N.B. UNWIND_INFO structs are DWORD-aligned.
19950b57cec5SDimitry Andric       uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
19960b57cec5SDimitry Andric       const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
19970b57cec5SDimitry Andric       _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
199806c3fb27SDimitry Andric       _dispContext.HandlerData = reinterpret_cast<void *>(_info.lsda);
199906c3fb27SDimitry Andric       _dispContext.LanguageHandler =
200006c3fb27SDimitry Andric           reinterpret_cast<EXCEPTION_ROUTINE *>(base + *handler);
20010b57cec5SDimitry Andric       if (*handler) {
20020b57cec5SDimitry Andric         _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
20030b57cec5SDimitry Andric       } else
20040b57cec5SDimitry Andric         _info.handler = 0;
20050b57cec5SDimitry Andric     } else {
20060b57cec5SDimitry Andric       _info.lsda = 0;
20070b57cec5SDimitry Andric       _info.handler = 0;
20080b57cec5SDimitry Andric     }
20090b57cec5SDimitry Andric   }
20100b57cec5SDimitry Andric #endif
20110b57cec5SDimitry Andric   setLastPC(pc);
20120b57cec5SDimitry Andric   return true;
20130b57cec5SDimitry Andric }
20140b57cec5SDimitry Andric #endif
20150b57cec5SDimitry Andric 
201681ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
201781ad6265SDimitry Andric // Masks for traceback table field xtbtable.
201881ad6265SDimitry Andric enum xTBTableMask : uint8_t {
201981ad6265SDimitry Andric   reservedBit = 0x02, // The traceback table was incorrectly generated if set
202081ad6265SDimitry Andric                       // (see comments in function getInfoFromTBTable().
202181ad6265SDimitry Andric   ehInfoBit = 0x08    // Exception handling info is present if set
202281ad6265SDimitry Andric };
202381ad6265SDimitry Andric 
202481ad6265SDimitry Andric enum frameType : unw_word_t {
202581ad6265SDimitry Andric   frameWithXLEHStateTable = 0,
202681ad6265SDimitry Andric   frameWithEHInfo = 1
202781ad6265SDimitry Andric };
202881ad6265SDimitry Andric 
202981ad6265SDimitry Andric extern "C" {
203081ad6265SDimitry Andric typedef _Unwind_Reason_Code __xlcxx_personality_v0_t(int, _Unwind_Action,
203181ad6265SDimitry Andric                                                      uint64_t,
203281ad6265SDimitry Andric                                                      _Unwind_Exception *,
203381ad6265SDimitry Andric                                                      struct _Unwind_Context *);
203481ad6265SDimitry Andric __attribute__((__weak__)) __xlcxx_personality_v0_t __xlcxx_personality_v0;
203581ad6265SDimitry Andric }
203681ad6265SDimitry Andric 
203781ad6265SDimitry Andric static __xlcxx_personality_v0_t *xlcPersonalityV0;
203881ad6265SDimitry Andric static RWMutex xlcPersonalityV0InitLock;
203981ad6265SDimitry Andric 
204081ad6265SDimitry Andric template <typename A, typename R>
204181ad6265SDimitry Andric bool UnwindCursor<A, R>::getInfoFromTBTable(pint_t pc, R &registers) {
204281ad6265SDimitry Andric   uint32_t *p = reinterpret_cast<uint32_t *>(pc);
204381ad6265SDimitry Andric 
204481ad6265SDimitry Andric   // Keep looking forward until a word of 0 is found. The traceback
204581ad6265SDimitry Andric   // table starts at the following word.
204681ad6265SDimitry Andric   while (*p)
204781ad6265SDimitry Andric     ++p;
204881ad6265SDimitry Andric   tbtable *TBTable = reinterpret_cast<tbtable *>(p + 1);
204981ad6265SDimitry Andric 
205081ad6265SDimitry Andric   if (_LIBUNWIND_TRACING_UNWINDING) {
205181ad6265SDimitry Andric     char functionBuf[512];
205281ad6265SDimitry Andric     const char *functionName = functionBuf;
205381ad6265SDimitry Andric     unw_word_t offset;
205481ad6265SDimitry Andric     if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) {
205581ad6265SDimitry Andric       functionName = ".anonymous.";
205681ad6265SDimitry Andric     }
205781ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("%s: Look up traceback table of func=%s at %p",
205881ad6265SDimitry Andric                                __func__, functionName,
205981ad6265SDimitry Andric                                reinterpret_cast<void *>(TBTable));
206081ad6265SDimitry Andric   }
206181ad6265SDimitry Andric 
206281ad6265SDimitry Andric   // If the traceback table does not contain necessary info, bypass this frame.
206381ad6265SDimitry Andric   if (!TBTable->tb.has_tboff)
206481ad6265SDimitry Andric     return false;
206581ad6265SDimitry Andric 
206681ad6265SDimitry Andric   // Structure tbtable_ext contains important data we are looking for.
206781ad6265SDimitry Andric   p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext);
206881ad6265SDimitry Andric 
206981ad6265SDimitry Andric   // Skip field parminfo if it exists.
207081ad6265SDimitry Andric   if (TBTable->tb.fixedparms || TBTable->tb.floatparms)
207181ad6265SDimitry Andric     ++p;
207281ad6265SDimitry Andric 
207381ad6265SDimitry Andric   // p now points to tb_offset, the offset from start of function to TB table.
207481ad6265SDimitry Andric   unw_word_t start_ip =
207581ad6265SDimitry Andric       reinterpret_cast<unw_word_t>(TBTable) - *p - sizeof(uint32_t);
207681ad6265SDimitry Andric   unw_word_t end_ip = reinterpret_cast<unw_word_t>(TBTable);
207781ad6265SDimitry Andric   ++p;
207881ad6265SDimitry Andric 
207981ad6265SDimitry Andric   _LIBUNWIND_TRACE_UNWINDING("start_ip=%p, end_ip=%p\n",
208081ad6265SDimitry Andric                              reinterpret_cast<void *>(start_ip),
208181ad6265SDimitry Andric                              reinterpret_cast<void *>(end_ip));
208281ad6265SDimitry Andric 
208381ad6265SDimitry Andric   // Skip field hand_mask if it exists.
208481ad6265SDimitry Andric   if (TBTable->tb.int_hndl)
208581ad6265SDimitry Andric     ++p;
208681ad6265SDimitry Andric 
208781ad6265SDimitry Andric   unw_word_t lsda = 0;
208881ad6265SDimitry Andric   unw_word_t handler = 0;
208981ad6265SDimitry Andric   unw_word_t flags = frameType::frameWithXLEHStateTable;
209081ad6265SDimitry Andric 
209181ad6265SDimitry Andric   if (TBTable->tb.lang == TB_CPLUSPLUS && TBTable->tb.has_ctl) {
209281ad6265SDimitry Andric     // State table info is available. The ctl_info field indicates the
209381ad6265SDimitry Andric     // number of CTL anchors. There should be only one entry for the C++
209481ad6265SDimitry Andric     // state table.
209581ad6265SDimitry Andric     assert(*p == 1 && "libunwind: there must be only one ctl_info entry");
209681ad6265SDimitry Andric     ++p;
209781ad6265SDimitry Andric     // p points to the offset of the state table into the stack.
209881ad6265SDimitry Andric     pint_t stateTableOffset = *p++;
209981ad6265SDimitry Andric 
210081ad6265SDimitry Andric     int framePointerReg;
210181ad6265SDimitry Andric 
210281ad6265SDimitry Andric     // Skip fields name_len and name if exist.
210381ad6265SDimitry Andric     if (TBTable->tb.name_present) {
210481ad6265SDimitry Andric       const uint16_t name_len = *(reinterpret_cast<uint16_t *>(p));
210581ad6265SDimitry Andric       p = reinterpret_cast<uint32_t *>(reinterpret_cast<char *>(p) + name_len +
210681ad6265SDimitry Andric                                        sizeof(uint16_t));
210781ad6265SDimitry Andric     }
210881ad6265SDimitry Andric 
210981ad6265SDimitry Andric     if (TBTable->tb.uses_alloca)
211081ad6265SDimitry Andric       framePointerReg = *(reinterpret_cast<char *>(p));
211181ad6265SDimitry Andric     else
211281ad6265SDimitry Andric       framePointerReg = 1; // default frame pointer == SP
211381ad6265SDimitry Andric 
211481ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
211581ad6265SDimitry Andric         "framePointerReg=%d, framePointer=%p, "
211681ad6265SDimitry Andric         "stateTableOffset=%#lx\n",
211781ad6265SDimitry Andric         framePointerReg,
211881ad6265SDimitry Andric         reinterpret_cast<void *>(_registers.getRegister(framePointerReg)),
211981ad6265SDimitry Andric         stateTableOffset);
212081ad6265SDimitry Andric     lsda = _registers.getRegister(framePointerReg) + stateTableOffset;
212181ad6265SDimitry Andric 
212281ad6265SDimitry Andric     // Since the traceback table generated by the legacy XLC++ does not
212381ad6265SDimitry Andric     // provide the location of the personality for the state table,
212481ad6265SDimitry Andric     // function __xlcxx_personality_v0(), which is the personality for the state
212581ad6265SDimitry Andric     // table and is exported from libc++abi, is directly assigned as the
212681ad6265SDimitry Andric     // handler here. When a legacy XLC++ frame is encountered, the symbol
212781ad6265SDimitry Andric     // is resolved dynamically using dlopen() to avoid hard dependency from
212881ad6265SDimitry Andric     // libunwind on libc++abi.
212981ad6265SDimitry Andric 
213081ad6265SDimitry Andric     // Resolve the function pointer to the state table personality if it has
213181ad6265SDimitry Andric     // not already.
213281ad6265SDimitry Andric     if (xlcPersonalityV0 == NULL) {
213381ad6265SDimitry Andric       xlcPersonalityV0InitLock.lock();
213481ad6265SDimitry Andric       if (xlcPersonalityV0 == NULL) {
213581ad6265SDimitry Andric         // If libc++abi is statically linked in, symbol __xlcxx_personality_v0
213681ad6265SDimitry Andric         // has been resolved at the link time.
213781ad6265SDimitry Andric         xlcPersonalityV0 = &__xlcxx_personality_v0;
213881ad6265SDimitry Andric         if (xlcPersonalityV0 == NULL) {
213981ad6265SDimitry Andric           // libc++abi is dynamically linked. Resolve __xlcxx_personality_v0
214081ad6265SDimitry Andric           // using dlopen().
214181ad6265SDimitry Andric           const char libcxxabi[] = "libc++abi.a(libc++abi.so.1)";
214281ad6265SDimitry Andric           void *libHandle;
2143bdd1243dSDimitry Andric           // The AIX dlopen() sets errno to 0 when it is successful, which
2144bdd1243dSDimitry Andric           // clobbers the value of errno from the user code. This is an AIX
2145bdd1243dSDimitry Andric           // bug because according to POSIX it should not set errno to 0. To
2146bdd1243dSDimitry Andric           // workaround before AIX fixes the bug, errno is saved and restored.
2147bdd1243dSDimitry Andric           int saveErrno = errno;
214881ad6265SDimitry Andric           libHandle = dlopen(libcxxabi, RTLD_MEMBER | RTLD_NOW);
214981ad6265SDimitry Andric           if (libHandle == NULL) {
215081ad6265SDimitry Andric             _LIBUNWIND_TRACE_UNWINDING("dlopen() failed with errno=%d\n",
215181ad6265SDimitry Andric                                        errno);
215281ad6265SDimitry Andric             assert(0 && "dlopen() failed");
215381ad6265SDimitry Andric           }
215481ad6265SDimitry Andric           xlcPersonalityV0 = reinterpret_cast<__xlcxx_personality_v0_t *>(
215581ad6265SDimitry Andric               dlsym(libHandle, "__xlcxx_personality_v0"));
215681ad6265SDimitry Andric           if (xlcPersonalityV0 == NULL) {
215781ad6265SDimitry Andric             _LIBUNWIND_TRACE_UNWINDING("dlsym() failed with errno=%d\n", errno);
215881ad6265SDimitry Andric             assert(0 && "dlsym() failed");
215981ad6265SDimitry Andric           }
216081ad6265SDimitry Andric           dlclose(libHandle);
2161bdd1243dSDimitry Andric           errno = saveErrno;
216281ad6265SDimitry Andric         }
216381ad6265SDimitry Andric       }
216481ad6265SDimitry Andric       xlcPersonalityV0InitLock.unlock();
216581ad6265SDimitry Andric     }
216681ad6265SDimitry Andric     handler = reinterpret_cast<unw_word_t>(xlcPersonalityV0);
216781ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("State table: LSDA=%p, Personality=%p\n",
216881ad6265SDimitry Andric                                reinterpret_cast<void *>(lsda),
216981ad6265SDimitry Andric                                reinterpret_cast<void *>(handler));
217081ad6265SDimitry Andric   } else if (TBTable->tb.longtbtable) {
217181ad6265SDimitry Andric     // This frame has the traceback table extension. Possible cases are
217281ad6265SDimitry Andric     // 1) a C++ frame that has the 'eh_info' structure; 2) a C++ frame that
217381ad6265SDimitry Andric     // is not EH aware; or, 3) a frame of other languages. We need to figure out
217481ad6265SDimitry Andric     // if the traceback table extension contains the 'eh_info' structure.
217581ad6265SDimitry Andric     //
217681ad6265SDimitry Andric     // We also need to deal with the complexity arising from some XL compiler
217781ad6265SDimitry Andric     // versions use the wrong ordering of 'longtbtable' and 'has_vec' bits
217881ad6265SDimitry Andric     // where the 'longtbtable' bit is meant to be the 'has_vec' bit and vice
217981ad6265SDimitry Andric     // versa. For frames of code generated by those compilers, the 'longtbtable'
218081ad6265SDimitry Andric     // bit may be set but there isn't really a traceback table extension.
218181ad6265SDimitry Andric     //
218281ad6265SDimitry Andric     // In </usr/include/sys/debug.h>, there is the following definition of
218381ad6265SDimitry Andric     // 'struct tbtable_ext'. It is not really a structure but a dummy to
218481ad6265SDimitry Andric     // collect the description of optional parts of the traceback table.
218581ad6265SDimitry Andric     //
218681ad6265SDimitry Andric     // struct tbtable_ext {
218781ad6265SDimitry Andric     //   ...
218881ad6265SDimitry Andric     //   char alloca_reg;        /* Register for alloca automatic storage */
218981ad6265SDimitry Andric     //   struct vec_ext vec_ext; /* Vector extension (if has_vec is set) */
219081ad6265SDimitry Andric     //   unsigned char xtbtable; /* More tbtable fields, if longtbtable is set*/
219181ad6265SDimitry Andric     // };
219281ad6265SDimitry Andric     //
219381ad6265SDimitry Andric     // Depending on how the 'has_vec'/'longtbtable' bit is interpreted, the data
219481ad6265SDimitry Andric     // following 'alloca_reg' can be treated either as 'struct vec_ext' or
219581ad6265SDimitry Andric     // 'unsigned char xtbtable'. 'xtbtable' bits are defined in
219681ad6265SDimitry Andric     // </usr/include/sys/debug.h> as flags. The 7th bit '0x02' is currently
219781ad6265SDimitry Andric     // unused and should not be set. 'struct vec_ext' is defined in
219881ad6265SDimitry Andric     // </usr/include/sys/debug.h> as follows:
219981ad6265SDimitry Andric     //
220081ad6265SDimitry Andric     // struct vec_ext {
220181ad6265SDimitry Andric     //   unsigned vr_saved:6;      /* Number of non-volatile vector regs saved
220281ad6265SDimitry Andric     //   */
220381ad6265SDimitry Andric     //                             /* first register saved is assumed to be */
220481ad6265SDimitry Andric     //                             /* 32 - vr_saved                         */
220581ad6265SDimitry Andric     //   unsigned saves_vrsave:1;  /* Set if vrsave is saved on the stack */
220681ad6265SDimitry Andric     //   unsigned has_varargs:1;
220781ad6265SDimitry Andric     //   ...
220881ad6265SDimitry Andric     // };
220981ad6265SDimitry Andric     //
221081ad6265SDimitry Andric     // Here, the 7th bit is used as 'saves_vrsave'. To determine whether it
221181ad6265SDimitry Andric     // is 'struct vec_ext' or 'xtbtable' that follows 'alloca_reg',
221281ad6265SDimitry Andric     // we checks if the 7th bit is set or not because 'xtbtable' should
221381ad6265SDimitry Andric     // never have the 7th bit set. The 7th bit of 'xtbtable' will be reserved
221481ad6265SDimitry Andric     // in the future to make sure the mitigation works. This mitigation
221581ad6265SDimitry Andric     // is not 100% bullet proof because 'struct vec_ext' may not always have
221681ad6265SDimitry Andric     // 'saves_vrsave' bit set.
221781ad6265SDimitry Andric     //
221881ad6265SDimitry Andric     // 'reservedBit' is defined in enum 'xTBTableMask' above as the mask for
221981ad6265SDimitry Andric     // checking the 7th bit.
222081ad6265SDimitry Andric 
222181ad6265SDimitry Andric     // p points to field name len.
222281ad6265SDimitry Andric     uint8_t *charPtr = reinterpret_cast<uint8_t *>(p);
222381ad6265SDimitry Andric 
222481ad6265SDimitry Andric     // Skip fields name_len and name if they exist.
222581ad6265SDimitry Andric     if (TBTable->tb.name_present) {
222681ad6265SDimitry Andric       const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr));
222781ad6265SDimitry Andric       charPtr = charPtr + name_len + sizeof(uint16_t);
222881ad6265SDimitry Andric     }
222981ad6265SDimitry Andric 
223081ad6265SDimitry Andric     // Skip field alloc_reg if it exists.
223181ad6265SDimitry Andric     if (TBTable->tb.uses_alloca)
223281ad6265SDimitry Andric       ++charPtr;
223381ad6265SDimitry Andric 
223481ad6265SDimitry Andric     // Check traceback table bit has_vec. Skip struct vec_ext if it exists.
223581ad6265SDimitry Andric     if (TBTable->tb.has_vec)
223681ad6265SDimitry Andric       // Note struct vec_ext does exist at this point because whether the
223781ad6265SDimitry Andric       // ordering of longtbtable and has_vec bits is correct or not, both
223881ad6265SDimitry Andric       // are set.
223981ad6265SDimitry Andric       charPtr += sizeof(struct vec_ext);
224081ad6265SDimitry Andric 
224181ad6265SDimitry Andric     // charPtr points to field 'xtbtable'. Check if the EH info is available.
224281ad6265SDimitry Andric     // Also check if the reserved bit of the extended traceback table field
224381ad6265SDimitry Andric     // 'xtbtable' is set. If it is, the traceback table was incorrectly
224481ad6265SDimitry Andric     // generated by an XL compiler that uses the wrong ordering of 'longtbtable'
224581ad6265SDimitry Andric     // and 'has_vec' bits and this is in fact 'struct vec_ext'. So skip the
224681ad6265SDimitry Andric     // frame.
224781ad6265SDimitry Andric     if ((*charPtr & xTBTableMask::ehInfoBit) &&
224881ad6265SDimitry Andric         !(*charPtr & xTBTableMask::reservedBit)) {
224981ad6265SDimitry Andric       // Mark this frame has the new EH info.
225081ad6265SDimitry Andric       flags = frameType::frameWithEHInfo;
225181ad6265SDimitry Andric 
225281ad6265SDimitry Andric       // eh_info is available.
225381ad6265SDimitry Andric       charPtr++;
225481ad6265SDimitry Andric       // The pointer is 4-byte aligned.
225581ad6265SDimitry Andric       if (reinterpret_cast<uintptr_t>(charPtr) % 4)
225681ad6265SDimitry Andric         charPtr += 4 - reinterpret_cast<uintptr_t>(charPtr) % 4;
225781ad6265SDimitry Andric       uintptr_t *ehInfo =
225881ad6265SDimitry Andric           reinterpret_cast<uintptr_t *>(*(reinterpret_cast<uintptr_t *>(
225981ad6265SDimitry Andric               registers.getRegister(2) +
226081ad6265SDimitry Andric               *(reinterpret_cast<uintptr_t *>(charPtr)))));
226181ad6265SDimitry Andric 
226281ad6265SDimitry Andric       // ehInfo points to structure en_info. The first member is version.
226381ad6265SDimitry Andric       // Only version 0 is currently supported.
226481ad6265SDimitry Andric       assert(*(reinterpret_cast<uint32_t *>(ehInfo)) == 0 &&
226581ad6265SDimitry Andric              "libunwind: ehInfo version other than 0 is not supported");
226681ad6265SDimitry Andric 
226781ad6265SDimitry Andric       // Increment ehInfo to point to member lsda.
226881ad6265SDimitry Andric       ++ehInfo;
226981ad6265SDimitry Andric       lsda = *ehInfo++;
227081ad6265SDimitry Andric 
227181ad6265SDimitry Andric       // enInfo now points to member personality.
227281ad6265SDimitry Andric       handler = *ehInfo;
227381ad6265SDimitry Andric 
227481ad6265SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING("Range table: LSDA=%#lx, Personality=%#lx\n",
227581ad6265SDimitry Andric                                  lsda, handler);
227681ad6265SDimitry Andric     }
227781ad6265SDimitry Andric   }
227881ad6265SDimitry Andric 
227981ad6265SDimitry Andric   _info.start_ip = start_ip;
228081ad6265SDimitry Andric   _info.end_ip = end_ip;
228181ad6265SDimitry Andric   _info.lsda = lsda;
228281ad6265SDimitry Andric   _info.handler = handler;
228381ad6265SDimitry Andric   _info.gp = 0;
228481ad6265SDimitry Andric   _info.flags = flags;
228581ad6265SDimitry Andric   _info.format = 0;
228681ad6265SDimitry Andric   _info.unwind_info = reinterpret_cast<unw_word_t>(TBTable);
228781ad6265SDimitry Andric   _info.unwind_info_size = 0;
228881ad6265SDimitry Andric   _info.extra = registers.getRegister(2);
228981ad6265SDimitry Andric 
229081ad6265SDimitry Andric   return true;
229181ad6265SDimitry Andric }
229281ad6265SDimitry Andric 
229381ad6265SDimitry Andric // Step back up the stack following the frame back link.
229481ad6265SDimitry Andric template <typename A, typename R>
229581ad6265SDimitry Andric int UnwindCursor<A, R>::stepWithTBTable(pint_t pc, tbtable *TBTable,
229681ad6265SDimitry Andric                                         R &registers, bool &isSignalFrame) {
229781ad6265SDimitry Andric   if (_LIBUNWIND_TRACING_UNWINDING) {
229881ad6265SDimitry Andric     char functionBuf[512];
229981ad6265SDimitry Andric     const char *functionName = functionBuf;
230081ad6265SDimitry Andric     unw_word_t offset;
230181ad6265SDimitry Andric     if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) {
230281ad6265SDimitry Andric       functionName = ".anonymous.";
230381ad6265SDimitry Andric     }
2304*5f757f3fSDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
2305*5f757f3fSDimitry Andric         "%s: Look up traceback table of func=%s at %p, pc=%p, "
2306*5f757f3fSDimitry Andric         "SP=%p, saves_lr=%d, stores_bc=%d",
2307*5f757f3fSDimitry Andric         __func__, functionName, reinterpret_cast<void *>(TBTable),
2308*5f757f3fSDimitry Andric         reinterpret_cast<void *>(pc),
2309*5f757f3fSDimitry Andric         reinterpret_cast<void *>(registers.getSP()), TBTable->tb.saves_lr,
2310*5f757f3fSDimitry Andric         TBTable->tb.stores_bc);
231181ad6265SDimitry Andric   }
231281ad6265SDimitry Andric 
231381ad6265SDimitry Andric #if defined(__powerpc64__)
2314*5f757f3fSDimitry Andric   // Instruction to reload TOC register "ld r2,40(r1)"
231581ad6265SDimitry Andric   const uint32_t loadTOCRegInst = 0xe8410028;
231681ad6265SDimitry Andric   const int32_t unwPPCF0Index = UNW_PPC64_F0;
231781ad6265SDimitry Andric   const int32_t unwPPCV0Index = UNW_PPC64_V0;
231881ad6265SDimitry Andric #else
2319*5f757f3fSDimitry Andric   // Instruction to reload TOC register "lwz r2,20(r1)"
232081ad6265SDimitry Andric   const uint32_t loadTOCRegInst = 0x80410014;
232181ad6265SDimitry Andric   const int32_t unwPPCF0Index = UNW_PPC_F0;
232281ad6265SDimitry Andric   const int32_t unwPPCV0Index = UNW_PPC_V0;
232381ad6265SDimitry Andric #endif
232481ad6265SDimitry Andric 
2325*5f757f3fSDimitry Andric   // lastStack points to the stack frame of the next routine up.
2326*5f757f3fSDimitry Andric   pint_t curStack = static_cast<pint_t>(registers.getSP());
2327*5f757f3fSDimitry Andric   pint_t lastStack = *reinterpret_cast<pint_t *>(curStack);
2328*5f757f3fSDimitry Andric 
2329*5f757f3fSDimitry Andric   if (lastStack == 0)
2330*5f757f3fSDimitry Andric     return UNW_STEP_END;
2331*5f757f3fSDimitry Andric 
233281ad6265SDimitry Andric   R newRegisters = registers;
233381ad6265SDimitry Andric 
2334*5f757f3fSDimitry Andric   // If backchain is not stored, use the current stack frame.
2335*5f757f3fSDimitry Andric   if (!TBTable->tb.stores_bc)
2336*5f757f3fSDimitry Andric     lastStack = curStack;
233781ad6265SDimitry Andric 
233881ad6265SDimitry Andric   // Return address is the address after call site instruction.
233981ad6265SDimitry Andric   pint_t returnAddress;
234081ad6265SDimitry Andric 
234181ad6265SDimitry Andric   if (isSignalFrame) {
234281ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("Possible signal handler frame: lastStack=%p",
234381ad6265SDimitry Andric                                reinterpret_cast<void *>(lastStack));
234481ad6265SDimitry Andric 
234581ad6265SDimitry Andric     sigcontext *sigContext = reinterpret_cast<sigcontext *>(
234681ad6265SDimitry Andric         reinterpret_cast<char *>(lastStack) + STKMINALIGN);
234781ad6265SDimitry Andric     returnAddress = sigContext->sc_jmpbuf.jmp_context.iar;
2348*5f757f3fSDimitry Andric 
2349*5f757f3fSDimitry Andric     bool useSTKMIN = false;
235081ad6265SDimitry Andric     if (returnAddress < 0x10000000) {
2351*5f757f3fSDimitry Andric       // Try again using STKMIN.
2352*5f757f3fSDimitry Andric       sigContext = reinterpret_cast<sigcontext *>(
2353*5f757f3fSDimitry Andric           reinterpret_cast<char *>(lastStack) + STKMIN);
2354*5f757f3fSDimitry Andric       returnAddress = sigContext->sc_jmpbuf.jmp_context.iar;
2355*5f757f3fSDimitry Andric       if (returnAddress < 0x10000000) {
2356*5f757f3fSDimitry Andric         _LIBUNWIND_TRACE_UNWINDING("Bad returnAddress=%p from sigcontext=%p",
2357*5f757f3fSDimitry Andric                                    reinterpret_cast<void *>(returnAddress),
2358*5f757f3fSDimitry Andric                                    reinterpret_cast<void *>(sigContext));
235981ad6265SDimitry Andric         return UNW_EBADFRAME;
2360*5f757f3fSDimitry Andric       }
2361*5f757f3fSDimitry Andric       useSTKMIN = true;
2362*5f757f3fSDimitry Andric     }
2363*5f757f3fSDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("Returning from a signal handler %s: "
236481ad6265SDimitry Andric                                "sigContext=%p, returnAddress=%p. "
2365*5f757f3fSDimitry Andric                                "Seems to be a valid address",
2366*5f757f3fSDimitry Andric                                useSTKMIN ? "STKMIN" : "STKMINALIGN",
236781ad6265SDimitry Andric                                reinterpret_cast<void *>(sigContext),
236881ad6265SDimitry Andric                                reinterpret_cast<void *>(returnAddress));
2369*5f757f3fSDimitry Andric 
237081ad6265SDimitry Andric     // Restore the condition register from sigcontext.
237181ad6265SDimitry Andric     newRegisters.setCR(sigContext->sc_jmpbuf.jmp_context.cr);
237281ad6265SDimitry Andric 
2373*5f757f3fSDimitry Andric     // Save the LR in sigcontext for stepping up when the function that
2374*5f757f3fSDimitry Andric     // raised the signal is a leaf function. This LR has the return address
2375*5f757f3fSDimitry Andric     // to the caller of the leaf function.
2376*5f757f3fSDimitry Andric     newRegisters.setLR(sigContext->sc_jmpbuf.jmp_context.lr);
2377*5f757f3fSDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
2378*5f757f3fSDimitry Andric         "Save LR=%p from sigcontext",
2379*5f757f3fSDimitry Andric         reinterpret_cast<void *>(sigContext->sc_jmpbuf.jmp_context.lr));
2380*5f757f3fSDimitry Andric 
238181ad6265SDimitry Andric     // Restore GPRs from sigcontext.
238281ad6265SDimitry Andric     for (int i = 0; i < 32; ++i)
238381ad6265SDimitry Andric       newRegisters.setRegister(i, sigContext->sc_jmpbuf.jmp_context.gpr[i]);
238481ad6265SDimitry Andric 
238581ad6265SDimitry Andric     // Restore FPRs from sigcontext.
238681ad6265SDimitry Andric     for (int i = 0; i < 32; ++i)
238781ad6265SDimitry Andric       newRegisters.setFloatRegister(i + unwPPCF0Index,
238881ad6265SDimitry Andric                                     sigContext->sc_jmpbuf.jmp_context.fpr[i]);
238981ad6265SDimitry Andric 
239081ad6265SDimitry Andric     // Restore vector registers if there is an associated extended context
239181ad6265SDimitry Andric     // structure.
239281ad6265SDimitry Andric     if (sigContext->sc_jmpbuf.jmp_context.msr & __EXTCTX) {
239381ad6265SDimitry Andric       ucontext_t *uContext = reinterpret_cast<ucontext_t *>(sigContext);
239481ad6265SDimitry Andric       if (uContext->__extctx->__extctx_magic == __EXTCTX_MAGIC) {
239581ad6265SDimitry Andric         for (int i = 0; i < 32; ++i)
239681ad6265SDimitry Andric           newRegisters.setVectorRegister(
239781ad6265SDimitry Andric               i + unwPPCV0Index, *(reinterpret_cast<v128 *>(
239881ad6265SDimitry Andric                                      &(uContext->__extctx->__vmx.__vr[i]))));
239981ad6265SDimitry Andric       }
240081ad6265SDimitry Andric     }
240181ad6265SDimitry Andric   } else {
240281ad6265SDimitry Andric     // Step up a normal frame.
240381ad6265SDimitry Andric 
2404*5f757f3fSDimitry Andric     if (!TBTable->tb.saves_lr && registers.getLR()) {
2405*5f757f3fSDimitry Andric       // This case should only occur if we were called from a signal handler
2406*5f757f3fSDimitry Andric       // and the signal occurred in a function that doesn't save the LR.
2407*5f757f3fSDimitry Andric       returnAddress = static_cast<pint_t>(registers.getLR());
2408*5f757f3fSDimitry Andric       _LIBUNWIND_TRACE_UNWINDING("Use saved LR=%p",
2409*5f757f3fSDimitry Andric                                  reinterpret_cast<void *>(returnAddress));
2410*5f757f3fSDimitry Andric     } else {
2411*5f757f3fSDimitry Andric       // Otherwise, use the LR value in the stack link area.
2412*5f757f3fSDimitry Andric       returnAddress = reinterpret_cast<pint_t *>(lastStack)[2];
2413*5f757f3fSDimitry Andric     }
2414*5f757f3fSDimitry Andric 
2415*5f757f3fSDimitry Andric     // Reset LR in the current context.
2416*5f757f3fSDimitry Andric     newRegisters.setLR(NULL);
2417*5f757f3fSDimitry Andric 
2418*5f757f3fSDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
2419*5f757f3fSDimitry Andric         "Extract info from lastStack=%p, returnAddress=%p",
242081ad6265SDimitry Andric         reinterpret_cast<void *>(lastStack),
242181ad6265SDimitry Andric         reinterpret_cast<void *>(returnAddress));
2422*5f757f3fSDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("fpr_regs=%d, gpr_regs=%d, saves_cr=%d",
242381ad6265SDimitry Andric                                TBTable->tb.fpr_saved, TBTable->tb.gpr_saved,
242481ad6265SDimitry Andric                                TBTable->tb.saves_cr);
242581ad6265SDimitry Andric 
242681ad6265SDimitry Andric     // Restore FP registers.
242781ad6265SDimitry Andric     char *ptrToRegs = reinterpret_cast<char *>(lastStack);
242881ad6265SDimitry Andric     double *FPRegs = reinterpret_cast<double *>(
242981ad6265SDimitry Andric         ptrToRegs - (TBTable->tb.fpr_saved * sizeof(double)));
243081ad6265SDimitry Andric     for (int i = 0; i < TBTable->tb.fpr_saved; ++i)
243181ad6265SDimitry Andric       newRegisters.setFloatRegister(
243281ad6265SDimitry Andric           32 - TBTable->tb.fpr_saved + i + unwPPCF0Index, FPRegs[i]);
243381ad6265SDimitry Andric 
243481ad6265SDimitry Andric     // Restore GP registers.
243581ad6265SDimitry Andric     ptrToRegs = reinterpret_cast<char *>(FPRegs);
243681ad6265SDimitry Andric     uintptr_t *GPRegs = reinterpret_cast<uintptr_t *>(
243781ad6265SDimitry Andric         ptrToRegs - (TBTable->tb.gpr_saved * sizeof(uintptr_t)));
243881ad6265SDimitry Andric     for (int i = 0; i < TBTable->tb.gpr_saved; ++i)
243981ad6265SDimitry Andric       newRegisters.setRegister(32 - TBTable->tb.gpr_saved + i, GPRegs[i]);
244081ad6265SDimitry Andric 
244181ad6265SDimitry Andric     // Restore Vector registers.
244281ad6265SDimitry Andric     ptrToRegs = reinterpret_cast<char *>(GPRegs);
244381ad6265SDimitry Andric 
244481ad6265SDimitry Andric     // Restore vector registers only if this is a Clang frame. Also
244581ad6265SDimitry Andric     // check if traceback table bit has_vec is set. If it is, structure
244681ad6265SDimitry Andric     // vec_ext is available.
244781ad6265SDimitry Andric     if (_info.flags == frameType::frameWithEHInfo && TBTable->tb.has_vec) {
244881ad6265SDimitry Andric 
244981ad6265SDimitry Andric       // Get to the vec_ext structure to check if vector registers are saved.
245081ad6265SDimitry Andric       uint32_t *p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext);
245181ad6265SDimitry Andric 
245281ad6265SDimitry Andric       // Skip field parminfo if exists.
245381ad6265SDimitry Andric       if (TBTable->tb.fixedparms || TBTable->tb.floatparms)
245481ad6265SDimitry Andric         ++p;
245581ad6265SDimitry Andric 
245681ad6265SDimitry Andric       // Skip field tb_offset if exists.
245781ad6265SDimitry Andric       if (TBTable->tb.has_tboff)
245881ad6265SDimitry Andric         ++p;
245981ad6265SDimitry Andric 
246081ad6265SDimitry Andric       // Skip field hand_mask if exists.
246181ad6265SDimitry Andric       if (TBTable->tb.int_hndl)
246281ad6265SDimitry Andric         ++p;
246381ad6265SDimitry Andric 
246481ad6265SDimitry Andric       // Skip fields ctl_info and ctl_info_disp if exist.
246581ad6265SDimitry Andric       if (TBTable->tb.has_ctl) {
246681ad6265SDimitry Andric         // Skip field ctl_info.
246781ad6265SDimitry Andric         ++p;
246881ad6265SDimitry Andric         // Skip field ctl_info_disp.
246981ad6265SDimitry Andric         ++p;
247081ad6265SDimitry Andric       }
247181ad6265SDimitry Andric 
247281ad6265SDimitry Andric       // Skip fields name_len and name if exist.
247381ad6265SDimitry Andric       // p is supposed to point to field name_len now.
247481ad6265SDimitry Andric       uint8_t *charPtr = reinterpret_cast<uint8_t *>(p);
247581ad6265SDimitry Andric       if (TBTable->tb.name_present) {
247681ad6265SDimitry Andric         const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr));
247781ad6265SDimitry Andric         charPtr = charPtr + name_len + sizeof(uint16_t);
247881ad6265SDimitry Andric       }
247981ad6265SDimitry Andric 
248081ad6265SDimitry Andric       // Skip field alloc_reg if it exists.
248181ad6265SDimitry Andric       if (TBTable->tb.uses_alloca)
248281ad6265SDimitry Andric         ++charPtr;
248381ad6265SDimitry Andric 
248481ad6265SDimitry Andric       struct vec_ext *vec_ext = reinterpret_cast<struct vec_ext *>(charPtr);
248581ad6265SDimitry Andric 
2486*5f757f3fSDimitry Andric       _LIBUNWIND_TRACE_UNWINDING("vr_saved=%d", vec_ext->vr_saved);
248781ad6265SDimitry Andric 
248881ad6265SDimitry Andric       // Restore vector register(s) if saved on the stack.
248981ad6265SDimitry Andric       if (vec_ext->vr_saved) {
249081ad6265SDimitry Andric         // Saved vector registers are 16-byte aligned.
249181ad6265SDimitry Andric         if (reinterpret_cast<uintptr_t>(ptrToRegs) % 16)
249281ad6265SDimitry Andric           ptrToRegs -= reinterpret_cast<uintptr_t>(ptrToRegs) % 16;
249381ad6265SDimitry Andric         v128 *VecRegs = reinterpret_cast<v128 *>(ptrToRegs - vec_ext->vr_saved *
249481ad6265SDimitry Andric                                                                  sizeof(v128));
249581ad6265SDimitry Andric         for (int i = 0; i < vec_ext->vr_saved; ++i) {
249681ad6265SDimitry Andric           newRegisters.setVectorRegister(
249781ad6265SDimitry Andric               32 - vec_ext->vr_saved + i + unwPPCV0Index, VecRegs[i]);
249881ad6265SDimitry Andric         }
249981ad6265SDimitry Andric       }
250081ad6265SDimitry Andric     }
250181ad6265SDimitry Andric     if (TBTable->tb.saves_cr) {
250281ad6265SDimitry Andric       // Get the saved condition register. The condition register is only
250381ad6265SDimitry Andric       // a single word.
250481ad6265SDimitry Andric       newRegisters.setCR(
250581ad6265SDimitry Andric           *(reinterpret_cast<uint32_t *>(lastStack + sizeof(uintptr_t))));
250681ad6265SDimitry Andric     }
250781ad6265SDimitry Andric 
250881ad6265SDimitry Andric     // Restore the SP.
250981ad6265SDimitry Andric     newRegisters.setSP(lastStack);
251081ad6265SDimitry Andric 
251181ad6265SDimitry Andric     // The first instruction after return.
251281ad6265SDimitry Andric     uint32_t firstInstruction = *(reinterpret_cast<uint32_t *>(returnAddress));
251381ad6265SDimitry Andric 
251481ad6265SDimitry Andric     // Do we need to set the TOC register?
251581ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
2516*5f757f3fSDimitry Andric         "Current gpr2=%p",
251781ad6265SDimitry Andric         reinterpret_cast<void *>(newRegisters.getRegister(2)));
251881ad6265SDimitry Andric     if (firstInstruction == loadTOCRegInst) {
251981ad6265SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
2520*5f757f3fSDimitry Andric           "Set gpr2=%p from frame",
252181ad6265SDimitry Andric           reinterpret_cast<void *>(reinterpret_cast<pint_t *>(lastStack)[5]));
252281ad6265SDimitry Andric       newRegisters.setRegister(2, reinterpret_cast<pint_t *>(lastStack)[5]);
252381ad6265SDimitry Andric     }
252481ad6265SDimitry Andric   }
252581ad6265SDimitry Andric   _LIBUNWIND_TRACE_UNWINDING("lastStack=%p, returnAddress=%p, pc=%p\n",
252681ad6265SDimitry Andric                              reinterpret_cast<void *>(lastStack),
252781ad6265SDimitry Andric                              reinterpret_cast<void *>(returnAddress),
252881ad6265SDimitry Andric                              reinterpret_cast<void *>(pc));
252981ad6265SDimitry Andric 
253081ad6265SDimitry Andric   // The return address is the address after call site instruction, so
2531bdd1243dSDimitry Andric   // setting IP to that simulates a return.
253281ad6265SDimitry Andric   newRegisters.setIP(reinterpret_cast<uintptr_t>(returnAddress));
253381ad6265SDimitry Andric 
253481ad6265SDimitry Andric   // Simulate the step by replacing the register set with the new ones.
253581ad6265SDimitry Andric   registers = newRegisters;
253681ad6265SDimitry Andric 
253781ad6265SDimitry Andric   // Check if the next frame is a signal frame.
253881ad6265SDimitry Andric   pint_t nextStack = *(reinterpret_cast<pint_t *>(registers.getSP()));
253981ad6265SDimitry Andric 
254081ad6265SDimitry Andric   // Return address is the address after call site instruction.
254181ad6265SDimitry Andric   pint_t nextReturnAddress = reinterpret_cast<pint_t *>(nextStack)[2];
254281ad6265SDimitry Andric 
254381ad6265SDimitry Andric   if (nextReturnAddress > 0x01 && nextReturnAddress < 0x10000) {
254481ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("The next is a signal handler frame: "
254581ad6265SDimitry Andric                                "nextStack=%p, next return address=%p\n",
254681ad6265SDimitry Andric                                reinterpret_cast<void *>(nextStack),
254781ad6265SDimitry Andric                                reinterpret_cast<void *>(nextReturnAddress));
254881ad6265SDimitry Andric     isSignalFrame = true;
254981ad6265SDimitry Andric   } else {
255081ad6265SDimitry Andric     isSignalFrame = false;
255181ad6265SDimitry Andric   }
255281ad6265SDimitry Andric   return UNW_STEP_SUCCESS;
255381ad6265SDimitry Andric }
255481ad6265SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
25550b57cec5SDimitry Andric 
25560b57cec5SDimitry Andric template <typename A, typename R>
25570b57cec5SDimitry Andric void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
255881ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
2559e8d8bef9SDimitry Andric   _isSigReturn = false;
2560e8d8bef9SDimitry Andric #endif
2561e8d8bef9SDimitry Andric 
2562e8d8bef9SDimitry Andric   pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
25630b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
25640b57cec5SDimitry Andric   // Remove the thumb bit so the IP represents the actual instruction address.
25650b57cec5SDimitry Andric   // This matches the behaviour of _Unwind_GetIP on arm.
25660b57cec5SDimitry Andric   pc &= (pint_t)~0x1;
25670b57cec5SDimitry Andric #endif
25680b57cec5SDimitry Andric 
25695ffd83dbSDimitry Andric   // Exit early if at the top of the stack.
25705ffd83dbSDimitry Andric   if (pc == 0) {
25715ffd83dbSDimitry Andric     _unwindInfoMissing = true;
25725ffd83dbSDimitry Andric     return;
25735ffd83dbSDimitry Andric   }
25745ffd83dbSDimitry Andric 
25750b57cec5SDimitry Andric   // If the last line of a function is a "throw" the compiler sometimes
25760b57cec5SDimitry Andric   // emits no instructions after the call to __cxa_throw.  This means
25770b57cec5SDimitry Andric   // the return address is actually the start of the next function.
25780b57cec5SDimitry Andric   // To disambiguate this, back up the pc when we know it is a return
25790b57cec5SDimitry Andric   // address.
25800b57cec5SDimitry Andric   if (isReturnAddress)
258181ad6265SDimitry Andric #if defined(_AIX)
258281ad6265SDimitry Andric     // PC needs to be a 4-byte aligned address to be able to look for a
258381ad6265SDimitry Andric     // word of 0 that indicates the start of the traceback table at the end
258481ad6265SDimitry Andric     // of a function on AIX.
258581ad6265SDimitry Andric     pc -= 4;
258681ad6265SDimitry Andric #else
25870b57cec5SDimitry Andric     --pc;
258881ad6265SDimitry Andric #endif
25890b57cec5SDimitry Andric 
25900b57cec5SDimitry Andric   // Ask address space object to find unwind sections for this pc.
25910b57cec5SDimitry Andric   UnwindInfoSections sects;
25920b57cec5SDimitry Andric   if (_addressSpace.findUnwindSections(pc, sects)) {
25930b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
25940b57cec5SDimitry Andric     // If there is a compact unwind encoding table, look there first.
25950b57cec5SDimitry Andric     if (sects.compact_unwind_section != 0) {
25960b57cec5SDimitry Andric       if (this->getInfoFromCompactEncodingSection(pc, sects)) {
25970b57cec5SDimitry Andric   #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
25980b57cec5SDimitry Andric         // Found info in table, done unless encoding says to use dwarf.
25990b57cec5SDimitry Andric         uint32_t dwarfOffset;
26000b57cec5SDimitry Andric         if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
26010b57cec5SDimitry Andric           if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
26020b57cec5SDimitry Andric             // found info in dwarf, done
26030b57cec5SDimitry Andric             return;
26040b57cec5SDimitry Andric           }
26050b57cec5SDimitry Andric         }
26060b57cec5SDimitry Andric   #endif
26070b57cec5SDimitry Andric         // If unwind table has entry, but entry says there is no unwind info,
26080b57cec5SDimitry Andric         // record that we have no unwind info.
26090b57cec5SDimitry Andric         if (_info.format == 0)
26100b57cec5SDimitry Andric           _unwindInfoMissing = true;
26110b57cec5SDimitry Andric         return;
26120b57cec5SDimitry Andric       }
26130b57cec5SDimitry Andric     }
26140b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
26150b57cec5SDimitry Andric 
26160b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
26170b57cec5SDimitry Andric     // If there is SEH unwind info, look there next.
26180b57cec5SDimitry Andric     if (this->getInfoFromSEH(pc))
26190b57cec5SDimitry Andric       return;
26200b57cec5SDimitry Andric #endif
26210b57cec5SDimitry Andric 
262281ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
262381ad6265SDimitry Andric     // If there is unwind info in the traceback table, look there next.
262481ad6265SDimitry Andric     if (this->getInfoFromTBTable(pc, _registers))
262581ad6265SDimitry Andric       return;
262681ad6265SDimitry Andric #endif
262781ad6265SDimitry Andric 
26280b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
26290b57cec5SDimitry Andric     // If there is dwarf unwind info, look there next.
26300b57cec5SDimitry Andric     if (sects.dwarf_section != 0) {
26310b57cec5SDimitry Andric       if (this->getInfoFromDwarfSection(pc, sects)) {
26320b57cec5SDimitry Andric         // found info in dwarf, done
26330b57cec5SDimitry Andric         return;
26340b57cec5SDimitry Andric       }
26350b57cec5SDimitry Andric     }
26360b57cec5SDimitry Andric #endif
26370b57cec5SDimitry Andric 
26380b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
26390b57cec5SDimitry Andric     // If there is ARM EHABI unwind info, look there next.
26400b57cec5SDimitry Andric     if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
26410b57cec5SDimitry Andric       return;
26420b57cec5SDimitry Andric #endif
26430b57cec5SDimitry Andric   }
26440b57cec5SDimitry Andric 
26450b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
26460b57cec5SDimitry Andric   // There is no static unwind info for this pc. Look to see if an FDE was
26470b57cec5SDimitry Andric   // dynamically registered for it.
2648e8d8bef9SDimitry Andric   pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll,
2649e8d8bef9SDimitry Andric                                                pc);
26500b57cec5SDimitry Andric   if (cachedFDE != 0) {
2651e8d8bef9SDimitry Andric     typename CFI_Parser<A>::FDE_Info fdeInfo;
2652e8d8bef9SDimitry Andric     typename CFI_Parser<A>::CIE_Info cieInfo;
2653e8d8bef9SDimitry Andric     if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo))
2654e8d8bef9SDimitry Andric       if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
26550b57cec5SDimitry Andric         return;
26560b57cec5SDimitry Andric   }
26570b57cec5SDimitry Andric 
26580b57cec5SDimitry Andric   // Lastly, ask AddressSpace object about platform specific ways to locate
26590b57cec5SDimitry Andric   // other FDEs.
26600b57cec5SDimitry Andric   pint_t fde;
26610b57cec5SDimitry Andric   if (_addressSpace.findOtherFDE(pc, fde)) {
2662e8d8bef9SDimitry Andric     typename CFI_Parser<A>::FDE_Info fdeInfo;
2663e8d8bef9SDimitry Andric     typename CFI_Parser<A>::CIE_Info cieInfo;
26640b57cec5SDimitry Andric     if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
26650b57cec5SDimitry Andric       // Double check this FDE is for a function that includes the pc.
2666e8d8bef9SDimitry Andric       if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd))
2667e8d8bef9SDimitry Andric         if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
26680b57cec5SDimitry Andric           return;
26690b57cec5SDimitry Andric     }
26700b57cec5SDimitry Andric   }
26710b57cec5SDimitry Andric #endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
26720b57cec5SDimitry Andric 
267381ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
2674e8d8bef9SDimitry Andric   if (setInfoForSigReturn())
2675e8d8bef9SDimitry Andric     return;
2676e8d8bef9SDimitry Andric #endif
2677e8d8bef9SDimitry Andric 
26780b57cec5SDimitry Andric   // no unwind info, flag that we can't reliably unwind
26790b57cec5SDimitry Andric   _unwindInfoMissing = true;
26800b57cec5SDimitry Andric }
26810b57cec5SDimitry Andric 
268281ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&                               \
268381ad6265SDimitry Andric     defined(_LIBUNWIND_TARGET_AARCH64)
2684e8d8bef9SDimitry Andric template <typename A, typename R>
2685e8d8bef9SDimitry Andric bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_arm64 &) {
2686e8d8bef9SDimitry Andric   // Look for the sigreturn trampoline. The trampoline's body is two
2687e8d8bef9SDimitry Andric   // specific instructions (see below). Typically the trampoline comes from the
2688e8d8bef9SDimitry Andric   // vDSO[1] (i.e. the __kernel_rt_sigreturn function). A libc might provide its
2689e8d8bef9SDimitry Andric   // own restorer function, though, or user-mode QEMU might write a trampoline
2690e8d8bef9SDimitry Andric   // onto the stack.
2691e8d8bef9SDimitry Andric   //
2692e8d8bef9SDimitry Andric   // This special code path is a fallback that is only used if the trampoline
2693e8d8bef9SDimitry Andric   // lacks proper (e.g. DWARF) unwind info. On AArch64, a new DWARF register
2694e8d8bef9SDimitry Andric   // constant for the PC needs to be defined before DWARF can handle a signal
2695e8d8bef9SDimitry Andric   // trampoline. This code may segfault if the target PC is unreadable, e.g.:
2696e8d8bef9SDimitry Andric   //  - The PC points at a function compiled without unwind info, and which is
2697e8d8bef9SDimitry Andric   //    part of an execute-only mapping (e.g. using -Wl,--execute-only).
2698e8d8bef9SDimitry Andric   //  - The PC is invalid and happens to point to unreadable or unmapped memory.
2699e8d8bef9SDimitry Andric   //
2700e8d8bef9SDimitry Andric   // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/vdso/sigreturn.S
2701e8d8bef9SDimitry Andric   const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
270281ad6265SDimitry Andric   // The PC might contain an invalid address if the unwind info is bad, so
270381ad6265SDimitry Andric   // directly accessing it could cause a segfault. Use process_vm_readv to read
270481ad6265SDimitry Andric   // the memory safely instead. process_vm_readv was added in Linux 3.2, and
270581ad6265SDimitry Andric   // AArch64 supported was added in Linux 3.7, so the syscall is guaranteed to
270681ad6265SDimitry Andric   // be present. Unfortunately, there are Linux AArch64 environments where the
270781ad6265SDimitry Andric   // libc wrapper for the syscall might not be present (e.g. Android 5), so call
270881ad6265SDimitry Andric   // the syscall directly instead.
270981ad6265SDimitry Andric   uint32_t instructions[2];
271081ad6265SDimitry Andric   struct iovec local_iov = {&instructions, sizeof instructions};
271181ad6265SDimitry Andric   struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof instructions};
271281ad6265SDimitry Andric   long bytesRead =
271381ad6265SDimitry Andric       syscall(SYS_process_vm_readv, getpid(), &local_iov, 1, &remote_iov, 1, 0);
2714e8d8bef9SDimitry Andric   // Look for instructions: mov x8, #0x8b; svc #0x0
271581ad6265SDimitry Andric   if (bytesRead != sizeof instructions || instructions[0] != 0xd2801168 ||
271681ad6265SDimitry Andric       instructions[1] != 0xd4000001)
271781ad6265SDimitry Andric     return false;
271881ad6265SDimitry Andric 
2719e8d8bef9SDimitry Andric   _info = {};
272081ad6265SDimitry Andric   _info.start_ip = pc;
272181ad6265SDimitry Andric   _info.end_ip = pc + 4;
2722e8d8bef9SDimitry Andric   _isSigReturn = true;
2723e8d8bef9SDimitry Andric   return true;
2724e8d8bef9SDimitry Andric }
2725e8d8bef9SDimitry Andric 
2726e8d8bef9SDimitry Andric template <typename A, typename R>
2727e8d8bef9SDimitry Andric int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) {
2728e8d8bef9SDimitry Andric   // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
2729e8d8bef9SDimitry Andric   //  - 128-byte siginfo struct
2730e8d8bef9SDimitry Andric   //  - ucontext struct:
2731e8d8bef9SDimitry Andric   //     - 8-byte long (uc_flags)
2732e8d8bef9SDimitry Andric   //     - 8-byte pointer (uc_link)
2733e8d8bef9SDimitry Andric   //     - 24-byte stack_t
2734e8d8bef9SDimitry Andric   //     - 128-byte signal set
2735e8d8bef9SDimitry Andric   //     - 8 bytes of padding because sigcontext has 16-byte alignment
2736e8d8bef9SDimitry Andric   //     - sigcontext/mcontext_t
2737e8d8bef9SDimitry Andric   // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c
2738e8d8bef9SDimitry Andric   const pint_t kOffsetSpToSigcontext = (128 + 8 + 8 + 24 + 128 + 8); // 304
2739e8d8bef9SDimitry Andric 
2740e8d8bef9SDimitry Andric   // Offsets from sigcontext to each register.
2741e8d8bef9SDimitry Andric   const pint_t kOffsetGprs = 8; // offset to "__u64 regs[31]" field
2742e8d8bef9SDimitry Andric   const pint_t kOffsetSp = 256; // offset to "__u64 sp" field
2743e8d8bef9SDimitry Andric   const pint_t kOffsetPc = 264; // offset to "__u64 pc" field
2744e8d8bef9SDimitry Andric 
2745e8d8bef9SDimitry Andric   pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
2746e8d8bef9SDimitry Andric 
2747e8d8bef9SDimitry Andric   for (int i = 0; i <= 30; ++i) {
2748e8d8bef9SDimitry Andric     uint64_t value = _addressSpace.get64(sigctx + kOffsetGprs +
2749e8d8bef9SDimitry Andric                                          static_cast<pint_t>(i * 8));
2750349cc55cSDimitry Andric     _registers.setRegister(UNW_AARCH64_X0 + i, value);
2751e8d8bef9SDimitry Andric   }
2752e8d8bef9SDimitry Andric   _registers.setSP(_addressSpace.get64(sigctx + kOffsetSp));
2753e8d8bef9SDimitry Andric   _registers.setIP(_addressSpace.get64(sigctx + kOffsetPc));
2754e8d8bef9SDimitry Andric   _isSignalFrame = true;
2755e8d8bef9SDimitry Andric   return UNW_STEP_SUCCESS;
2756e8d8bef9SDimitry Andric }
275781ad6265SDimitry Andric #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
275881ad6265SDimitry Andric        // defined(_LIBUNWIND_TARGET_AARCH64)
275981ad6265SDimitry Andric 
276081ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&                               \
276106c3fb27SDimitry Andric     defined(_LIBUNWIND_TARGET_RISCV)
276206c3fb27SDimitry Andric template <typename A, typename R>
276306c3fb27SDimitry Andric bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_riscv &) {
276406c3fb27SDimitry Andric   const pint_t pc = static_cast<pint_t>(getReg(UNW_REG_IP));
276506c3fb27SDimitry Andric   uint32_t instructions[2];
276606c3fb27SDimitry Andric   struct iovec local_iov = {&instructions, sizeof instructions};
276706c3fb27SDimitry Andric   struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof instructions};
276806c3fb27SDimitry Andric   long bytesRead =
276906c3fb27SDimitry Andric       syscall(SYS_process_vm_readv, getpid(), &local_iov, 1, &remote_iov, 1, 0);
277006c3fb27SDimitry Andric   // Look for the two instructions used in the sigreturn trampoline
277106c3fb27SDimitry Andric   // __vdso_rt_sigreturn:
277206c3fb27SDimitry Andric   //
277306c3fb27SDimitry Andric   // 0x08b00893 li a7,0x8b
277406c3fb27SDimitry Andric   // 0x00000073 ecall
277506c3fb27SDimitry Andric   if (bytesRead != sizeof instructions || instructions[0] != 0x08b00893 ||
277606c3fb27SDimitry Andric       instructions[1] != 0x00000073)
277706c3fb27SDimitry Andric     return false;
277806c3fb27SDimitry Andric 
277906c3fb27SDimitry Andric   _info = {};
278006c3fb27SDimitry Andric   _info.start_ip = pc;
278106c3fb27SDimitry Andric   _info.end_ip = pc + 4;
278206c3fb27SDimitry Andric   _isSigReturn = true;
278306c3fb27SDimitry Andric   return true;
278406c3fb27SDimitry Andric }
278506c3fb27SDimitry Andric 
278606c3fb27SDimitry Andric template <typename A, typename R>
278706c3fb27SDimitry Andric int UnwindCursor<A, R>::stepThroughSigReturn(Registers_riscv &) {
278806c3fb27SDimitry Andric   // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
278906c3fb27SDimitry Andric   //  - 128-byte siginfo struct
279006c3fb27SDimitry Andric   //  - ucontext_t struct:
279106c3fb27SDimitry Andric   //     - 8-byte long (__uc_flags)
279206c3fb27SDimitry Andric   //     - 8-byte pointer (*uc_link)
279306c3fb27SDimitry Andric   //     - 24-byte uc_stack
279406c3fb27SDimitry Andric   //     - 8-byte uc_sigmask
279506c3fb27SDimitry Andric   //     - 120-byte of padding to allow sigset_t to be expanded in the future
279606c3fb27SDimitry Andric   //     - 8 bytes of padding because sigcontext has 16-byte alignment
279706c3fb27SDimitry Andric   //     - struct sigcontext uc_mcontext
279806c3fb27SDimitry Andric   // [1]
279906c3fb27SDimitry Andric   // https://github.com/torvalds/linux/blob/master/arch/riscv/kernel/signal.c
280006c3fb27SDimitry Andric   const pint_t kOffsetSpToSigcontext = 128 + 8 + 8 + 24 + 8 + 128;
280106c3fb27SDimitry Andric 
280206c3fb27SDimitry Andric   const pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
280306c3fb27SDimitry Andric   _registers.setIP(_addressSpace.get64(sigctx));
280406c3fb27SDimitry Andric   for (int i = UNW_RISCV_X1; i <= UNW_RISCV_X31; ++i) {
280506c3fb27SDimitry Andric     uint64_t value = _addressSpace.get64(sigctx + static_cast<pint_t>(i * 8));
280606c3fb27SDimitry Andric     _registers.setRegister(i, value);
280706c3fb27SDimitry Andric   }
280806c3fb27SDimitry Andric   _isSignalFrame = true;
280906c3fb27SDimitry Andric   return UNW_STEP_SUCCESS;
281006c3fb27SDimitry Andric }
281106c3fb27SDimitry Andric #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
281206c3fb27SDimitry Andric        // defined(_LIBUNWIND_TARGET_RISCV)
281306c3fb27SDimitry Andric 
281406c3fb27SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&                               \
281581ad6265SDimitry Andric     defined(_LIBUNWIND_TARGET_S390X)
281681ad6265SDimitry Andric template <typename A, typename R>
281781ad6265SDimitry Andric bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_s390x &) {
281881ad6265SDimitry Andric   // Look for the sigreturn trampoline. The trampoline's body is a
281981ad6265SDimitry Andric   // specific instruction (see below). Typically the trampoline comes from the
282081ad6265SDimitry Andric   // vDSO (i.e. the __kernel_[rt_]sigreturn function). A libc might provide its
282181ad6265SDimitry Andric   // own restorer function, though, or user-mode QEMU might write a trampoline
282281ad6265SDimitry Andric   // onto the stack.
282381ad6265SDimitry Andric   const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
2824fcaf7f86SDimitry Andric   // The PC might contain an invalid address if the unwind info is bad, so
2825fcaf7f86SDimitry Andric   // directly accessing it could cause a segfault. Use process_vm_readv to
2826fcaf7f86SDimitry Andric   // read the memory safely instead.
2827fcaf7f86SDimitry Andric   uint16_t inst;
2828fcaf7f86SDimitry Andric   struct iovec local_iov = {&inst, sizeof inst};
2829fcaf7f86SDimitry Andric   struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof inst};
2830fcaf7f86SDimitry Andric   long bytesRead = process_vm_readv(getpid(), &local_iov, 1, &remote_iov, 1, 0);
2831fcaf7f86SDimitry Andric   if (bytesRead == sizeof inst && (inst == 0x0a77 || inst == 0x0aad)) {
283281ad6265SDimitry Andric     _info = {};
283381ad6265SDimitry Andric     _info.start_ip = pc;
283481ad6265SDimitry Andric     _info.end_ip = pc + 2;
283581ad6265SDimitry Andric     _isSigReturn = true;
283681ad6265SDimitry Andric     return true;
283781ad6265SDimitry Andric   }
283881ad6265SDimitry Andric   return false;
283981ad6265SDimitry Andric }
284081ad6265SDimitry Andric 
284181ad6265SDimitry Andric template <typename A, typename R>
284281ad6265SDimitry Andric int UnwindCursor<A, R>::stepThroughSigReturn(Registers_s390x &) {
284381ad6265SDimitry Andric   // Determine current SP.
284481ad6265SDimitry Andric   const pint_t sp = static_cast<pint_t>(this->getReg(UNW_REG_SP));
284581ad6265SDimitry Andric   // According to the s390x ABI, the CFA is at (incoming) SP + 160.
284681ad6265SDimitry Andric   const pint_t cfa = sp + 160;
284781ad6265SDimitry Andric 
284881ad6265SDimitry Andric   // Determine current PC and instruction there (this must be either
284981ad6265SDimitry Andric   // a "svc __NR_sigreturn" or "svc __NR_rt_sigreturn").
285081ad6265SDimitry Andric   const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
285181ad6265SDimitry Andric   const uint16_t inst = _addressSpace.get16(pc);
285281ad6265SDimitry Andric 
285381ad6265SDimitry Andric   // Find the addresses of the signo and sigcontext in the frame.
285481ad6265SDimitry Andric   pint_t pSigctx = 0;
285581ad6265SDimitry Andric   pint_t pSigno = 0;
285681ad6265SDimitry Andric 
285781ad6265SDimitry Andric   // "svc __NR_sigreturn" uses a non-RT signal trampoline frame.
285881ad6265SDimitry Andric   if (inst == 0x0a77) {
285981ad6265SDimitry Andric     // Layout of a non-RT signal trampoline frame, starting at the CFA:
286081ad6265SDimitry Andric     //  - 8-byte signal mask
286181ad6265SDimitry Andric     //  - 8-byte pointer to sigcontext, followed by signo
286281ad6265SDimitry Andric     //  - 4-byte signo
286381ad6265SDimitry Andric     pSigctx = _addressSpace.get64(cfa + 8);
286481ad6265SDimitry Andric     pSigno = pSigctx + 344;
286581ad6265SDimitry Andric   }
286681ad6265SDimitry Andric 
286781ad6265SDimitry Andric   // "svc __NR_rt_sigreturn" uses a RT signal trampoline frame.
286881ad6265SDimitry Andric   if (inst == 0x0aad) {
286981ad6265SDimitry Andric     // Layout of a RT signal trampoline frame, starting at the CFA:
287081ad6265SDimitry Andric     //  - 8-byte retcode (+ alignment)
287181ad6265SDimitry Andric     //  - 128-byte siginfo struct (starts with signo)
287281ad6265SDimitry Andric     //  - ucontext struct:
287381ad6265SDimitry Andric     //     - 8-byte long (uc_flags)
287481ad6265SDimitry Andric     //     - 8-byte pointer (uc_link)
287581ad6265SDimitry Andric     //     - 24-byte stack_t
287681ad6265SDimitry Andric     //     - 8 bytes of padding because sigcontext has 16-byte alignment
287781ad6265SDimitry Andric     //     - sigcontext/mcontext_t
287881ad6265SDimitry Andric     pSigctx = cfa + 8 + 128 + 8 + 8 + 24 + 8;
287981ad6265SDimitry Andric     pSigno = cfa + 8;
288081ad6265SDimitry Andric   }
288181ad6265SDimitry Andric 
288281ad6265SDimitry Andric   assert(pSigctx != 0);
288381ad6265SDimitry Andric   assert(pSigno != 0);
288481ad6265SDimitry Andric 
288581ad6265SDimitry Andric   // Offsets from sigcontext to each register.
288681ad6265SDimitry Andric   const pint_t kOffsetPc = 8;
288781ad6265SDimitry Andric   const pint_t kOffsetGprs = 16;
288881ad6265SDimitry Andric   const pint_t kOffsetFprs = 216;
288981ad6265SDimitry Andric 
289081ad6265SDimitry Andric   // Restore all registers.
289181ad6265SDimitry Andric   for (int i = 0; i < 16; ++i) {
289281ad6265SDimitry Andric     uint64_t value = _addressSpace.get64(pSigctx + kOffsetGprs +
289381ad6265SDimitry Andric                                          static_cast<pint_t>(i * 8));
289481ad6265SDimitry Andric     _registers.setRegister(UNW_S390X_R0 + i, value);
289581ad6265SDimitry Andric   }
289681ad6265SDimitry Andric   for (int i = 0; i < 16; ++i) {
289781ad6265SDimitry Andric     static const int fpr[16] = {
289881ad6265SDimitry Andric       UNW_S390X_F0, UNW_S390X_F1, UNW_S390X_F2, UNW_S390X_F3,
289981ad6265SDimitry Andric       UNW_S390X_F4, UNW_S390X_F5, UNW_S390X_F6, UNW_S390X_F7,
290081ad6265SDimitry Andric       UNW_S390X_F8, UNW_S390X_F9, UNW_S390X_F10, UNW_S390X_F11,
290181ad6265SDimitry Andric       UNW_S390X_F12, UNW_S390X_F13, UNW_S390X_F14, UNW_S390X_F15
290281ad6265SDimitry Andric     };
290381ad6265SDimitry Andric     double value = _addressSpace.getDouble(pSigctx + kOffsetFprs +
290481ad6265SDimitry Andric                                            static_cast<pint_t>(i * 8));
290581ad6265SDimitry Andric     _registers.setFloatRegister(fpr[i], value);
290681ad6265SDimitry Andric   }
290781ad6265SDimitry Andric   _registers.setIP(_addressSpace.get64(pSigctx + kOffsetPc));
290881ad6265SDimitry Andric 
290981ad6265SDimitry Andric   // SIGILL, SIGFPE and SIGTRAP are delivered with psw_addr
291081ad6265SDimitry Andric   // after the faulting instruction rather than before it.
291181ad6265SDimitry Andric   // Do not set _isSignalFrame in that case.
291281ad6265SDimitry Andric   uint32_t signo = _addressSpace.get32(pSigno);
291381ad6265SDimitry Andric   _isSignalFrame = (signo != 4 && signo != 5 && signo != 8);
291481ad6265SDimitry Andric 
291581ad6265SDimitry Andric   return UNW_STEP_SUCCESS;
291681ad6265SDimitry Andric }
291781ad6265SDimitry Andric #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
291881ad6265SDimitry Andric        // defined(_LIBUNWIND_TARGET_S390X)
2919e8d8bef9SDimitry Andric 
2920bdd1243dSDimitry Andric template <typename A, typename R> int UnwindCursor<A, R>::step(bool stage2) {
2921bdd1243dSDimitry Andric   (void)stage2;
29220b57cec5SDimitry Andric   // Bottom of stack is defined is when unwind info cannot be found.
29230b57cec5SDimitry Andric   if (_unwindInfoMissing)
29240b57cec5SDimitry Andric     return UNW_STEP_END;
29250b57cec5SDimitry Andric 
29260b57cec5SDimitry Andric   // Use unwinding info to modify register set as if function returned.
29270b57cec5SDimitry Andric   int result;
292881ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
2929e8d8bef9SDimitry Andric   if (_isSigReturn) {
2930e8d8bef9SDimitry Andric     result = this->stepThroughSigReturn();
2931e8d8bef9SDimitry Andric   } else
2932e8d8bef9SDimitry Andric #endif
2933e8d8bef9SDimitry Andric   {
29340b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
2935bdd1243dSDimitry Andric     result = this->stepWithCompactEncoding(stage2);
29360b57cec5SDimitry Andric #elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
29370b57cec5SDimitry Andric     result = this->stepWithSEHData();
293881ad6265SDimitry Andric #elif defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
293981ad6265SDimitry Andric     result = this->stepWithTBTableData();
29400b57cec5SDimitry Andric #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
2941bdd1243dSDimitry Andric     result = this->stepWithDwarfFDE(stage2);
29420b57cec5SDimitry Andric #elif defined(_LIBUNWIND_ARM_EHABI)
29430b57cec5SDimitry Andric     result = this->stepWithEHABI();
29440b57cec5SDimitry Andric #else
29450b57cec5SDimitry Andric   #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
29460b57cec5SDimitry Andric               _LIBUNWIND_SUPPORT_SEH_UNWIND or \
29470b57cec5SDimitry Andric               _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
29480b57cec5SDimitry Andric               _LIBUNWIND_ARM_EHABI
29490b57cec5SDimitry Andric #endif
2950e8d8bef9SDimitry Andric   }
29510b57cec5SDimitry Andric 
29520b57cec5SDimitry Andric   // update info based on new PC
29530b57cec5SDimitry Andric   if (result == UNW_STEP_SUCCESS) {
29540b57cec5SDimitry Andric     this->setInfoBasedOnIPRegister(true);
29550b57cec5SDimitry Andric     if (_unwindInfoMissing)
29560b57cec5SDimitry Andric       return UNW_STEP_END;
29570b57cec5SDimitry Andric   }
29580b57cec5SDimitry Andric 
29590b57cec5SDimitry Andric   return result;
29600b57cec5SDimitry Andric }
29610b57cec5SDimitry Andric 
29620b57cec5SDimitry Andric template <typename A, typename R>
29630b57cec5SDimitry Andric void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
2964c2c6a179SDimitry Andric   if (_unwindInfoMissing)
2965c2c6a179SDimitry Andric     memset(info, 0, sizeof(*info));
2966c2c6a179SDimitry Andric   else
29670b57cec5SDimitry Andric     *info = _info;
29680b57cec5SDimitry Andric }
29690b57cec5SDimitry Andric 
29700b57cec5SDimitry Andric template <typename A, typename R>
29710b57cec5SDimitry Andric bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
29720b57cec5SDimitry Andric                                                            unw_word_t *offset) {
29730b57cec5SDimitry Andric   return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
29740b57cec5SDimitry Andric                                          buf, bufLen, offset);
29750b57cec5SDimitry Andric }
29760b57cec5SDimitry Andric 
2977349cc55cSDimitry Andric #if defined(_LIBUNWIND_USE_CET)
2978349cc55cSDimitry Andric extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) {
2979349cc55cSDimitry Andric   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
2980349cc55cSDimitry Andric   return co->get_registers();
2981349cc55cSDimitry Andric }
2982349cc55cSDimitry Andric #endif
29830b57cec5SDimitry Andric } // namespace libunwind
29840b57cec5SDimitry Andric 
29850b57cec5SDimitry Andric #endif // __UNWINDCURSOR_HPP__
2986