xref: /freebsd-src/contrib/llvm-project/libunwind/src/UnwindCursor.hpp (revision 62987288060ff68c817b7056815aa9fb8ba8ecd7)
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))
367a6dacacSDimitry Andric #include <errno.h>
377a6dacacSDimitry Andric #include <signal.h>
3881ad6265SDimitry Andric #include <sys/syscall.h>
3981ad6265SDimitry Andric #include <unistd.h>
4081ad6265SDimitry Andric #define _LIBUNWIND_CHECK_LINUX_SIGRETURN 1
4181ad6265SDimitry Andric #endif
420b57cec5SDimitry Andric 
43bdd1243dSDimitry Andric #include "AddressSpace.hpp"
44bdd1243dSDimitry Andric #include "CompactUnwinder.hpp"
45bdd1243dSDimitry Andric #include "config.h"
46bdd1243dSDimitry Andric #include "DwarfInstructions.hpp"
47bdd1243dSDimitry Andric #include "EHHeaderParser.hpp"
48bdd1243dSDimitry Andric #include "libunwind.h"
49bdd1243dSDimitry Andric #include "libunwind_ext.h"
50bdd1243dSDimitry Andric #include "Registers.hpp"
51bdd1243dSDimitry Andric #include "RWMutex.hpp"
52bdd1243dSDimitry Andric #include "Unwind-EHABI.h"
53bdd1243dSDimitry Andric 
540b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
550b57cec5SDimitry Andric // Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and
560b57cec5SDimitry Andric // earlier) SDKs.
570b57cec5SDimitry Andric // MinGW-w64 has always provided this struct.
580b57cec5SDimitry Andric   #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \
590b57cec5SDimitry Andric       !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
600b57cec5SDimitry Andric struct _DISPATCHER_CONTEXT {
610b57cec5SDimitry Andric   ULONG64 ControlPc;
620b57cec5SDimitry Andric   ULONG64 ImageBase;
630b57cec5SDimitry Andric   PRUNTIME_FUNCTION FunctionEntry;
640b57cec5SDimitry Andric   ULONG64 EstablisherFrame;
650b57cec5SDimitry Andric   ULONG64 TargetIp;
660b57cec5SDimitry Andric   PCONTEXT ContextRecord;
670b57cec5SDimitry Andric   PEXCEPTION_ROUTINE LanguageHandler;
680b57cec5SDimitry Andric   PVOID HandlerData;
690b57cec5SDimitry Andric   PUNWIND_HISTORY_TABLE HistoryTable;
700b57cec5SDimitry Andric   ULONG ScopeIndex;
710b57cec5SDimitry Andric   ULONG Fill0;
720b57cec5SDimitry Andric };
730b57cec5SDimitry Andric   #endif
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric struct UNWIND_INFO {
760b57cec5SDimitry Andric   uint8_t Version : 3;
770b57cec5SDimitry Andric   uint8_t Flags : 5;
780b57cec5SDimitry Andric   uint8_t SizeOfProlog;
790b57cec5SDimitry Andric   uint8_t CountOfCodes;
800b57cec5SDimitry Andric   uint8_t FrameRegister : 4;
810b57cec5SDimitry Andric   uint8_t FrameOffset : 4;
820b57cec5SDimitry Andric   uint16_t UnwindCodes[2];
830b57cec5SDimitry Andric };
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
860b57cec5SDimitry Andric     int, _Unwind_Action, uint64_t, _Unwind_Exception *,
870b57cec5SDimitry Andric     struct _Unwind_Context *);
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric #endif
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric namespace libunwind {
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
940b57cec5SDimitry Andric /// Cache of recently found FDEs.
950b57cec5SDimitry Andric template <typename A>
960b57cec5SDimitry Andric class _LIBUNWIND_HIDDEN DwarfFDECache {
970b57cec5SDimitry Andric   typedef typename A::pint_t pint_t;
980b57cec5SDimitry Andric public:
99e8d8bef9SDimitry Andric   static constexpr pint_t kSearchAll = static_cast<pint_t>(-1);
1000b57cec5SDimitry Andric   static pint_t findFDE(pint_t mh, pint_t pc);
1010b57cec5SDimitry Andric   static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
1020b57cec5SDimitry Andric   static void removeAllIn(pint_t mh);
1030b57cec5SDimitry Andric   static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
1040b57cec5SDimitry Andric                                                unw_word_t ip_end,
1050b57cec5SDimitry Andric                                                unw_word_t fde, unw_word_t mh));
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric private:
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   struct entry {
1100b57cec5SDimitry Andric     pint_t mh;
1110b57cec5SDimitry Andric     pint_t ip_start;
1120b57cec5SDimitry Andric     pint_t ip_end;
1130b57cec5SDimitry Andric     pint_t fde;
1140b57cec5SDimitry Andric   };
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   // These fields are all static to avoid needing an initializer.
1170b57cec5SDimitry Andric   // There is only one instance of this class per process.
1180b57cec5SDimitry Andric   static RWMutex _lock;
1190b57cec5SDimitry Andric #ifdef __APPLE__
1200b57cec5SDimitry Andric   static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
1210b57cec5SDimitry Andric   static bool _registeredForDyldUnloads;
1220b57cec5SDimitry Andric #endif
1230b57cec5SDimitry Andric   static entry *_buffer;
1240b57cec5SDimitry Andric   static entry *_bufferUsed;
1250b57cec5SDimitry Andric   static entry *_bufferEnd;
1260b57cec5SDimitry Andric   static entry _initialBuffer[64];
1270b57cec5SDimitry Andric };
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric template <typename A>
1300b57cec5SDimitry Andric typename DwarfFDECache<A>::entry *
1310b57cec5SDimitry Andric DwarfFDECache<A>::_buffer = _initialBuffer;
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric template <typename A>
1340b57cec5SDimitry Andric typename DwarfFDECache<A>::entry *
1350b57cec5SDimitry Andric DwarfFDECache<A>::_bufferUsed = _initialBuffer;
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric template <typename A>
1380b57cec5SDimitry Andric typename DwarfFDECache<A>::entry *
1390b57cec5SDimitry Andric DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric template <typename A>
1420b57cec5SDimitry Andric typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric template <typename A>
1450b57cec5SDimitry Andric RWMutex DwarfFDECache<A>::_lock;
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric #ifdef __APPLE__
1480b57cec5SDimitry Andric template <typename A>
1490b57cec5SDimitry Andric bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
1500b57cec5SDimitry Andric #endif
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric template <typename A>
1530b57cec5SDimitry Andric typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
1540b57cec5SDimitry Andric   pint_t result = 0;
1550b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
1560b57cec5SDimitry Andric   for (entry *p = _buffer; p < _bufferUsed; ++p) {
157e8d8bef9SDimitry Andric     if ((mh == p->mh) || (mh == kSearchAll)) {
1580b57cec5SDimitry Andric       if ((p->ip_start <= pc) && (pc < p->ip_end)) {
1590b57cec5SDimitry Andric         result = p->fde;
1600b57cec5SDimitry Andric         break;
1610b57cec5SDimitry Andric       }
1620b57cec5SDimitry Andric     }
1630b57cec5SDimitry Andric   }
1640b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
1650b57cec5SDimitry Andric   return result;
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric template <typename A>
1690b57cec5SDimitry Andric void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
1700b57cec5SDimitry Andric                            pint_t fde) {
1710b57cec5SDimitry Andric #if !defined(_LIBUNWIND_NO_HEAP)
1720b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
1730b57cec5SDimitry Andric   if (_bufferUsed >= _bufferEnd) {
1740b57cec5SDimitry Andric     size_t oldSize = (size_t)(_bufferEnd - _buffer);
1750b57cec5SDimitry Andric     size_t newSize = oldSize * 4;
1760b57cec5SDimitry Andric     // Can't use operator new (we are below it).
1770b57cec5SDimitry Andric     entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
1780b57cec5SDimitry Andric     memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
1790b57cec5SDimitry Andric     if (_buffer != _initialBuffer)
1800b57cec5SDimitry Andric       free(_buffer);
1810b57cec5SDimitry Andric     _buffer = newBuffer;
1820b57cec5SDimitry Andric     _bufferUsed = &newBuffer[oldSize];
1830b57cec5SDimitry Andric     _bufferEnd = &newBuffer[newSize];
1840b57cec5SDimitry Andric   }
1850b57cec5SDimitry Andric   _bufferUsed->mh = mh;
1860b57cec5SDimitry Andric   _bufferUsed->ip_start = ip_start;
1870b57cec5SDimitry Andric   _bufferUsed->ip_end = ip_end;
1880b57cec5SDimitry Andric   _bufferUsed->fde = fde;
1890b57cec5SDimitry Andric   ++_bufferUsed;
1900b57cec5SDimitry Andric #ifdef __APPLE__
1910b57cec5SDimitry Andric   if (!_registeredForDyldUnloads) {
1920b57cec5SDimitry Andric     _dyld_register_func_for_remove_image(&dyldUnloadHook);
1930b57cec5SDimitry Andric     _registeredForDyldUnloads = true;
1940b57cec5SDimitry Andric   }
1950b57cec5SDimitry Andric #endif
1960b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
1970b57cec5SDimitry Andric #endif
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric template <typename A>
2010b57cec5SDimitry Andric void DwarfFDECache<A>::removeAllIn(pint_t mh) {
2020b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
2030b57cec5SDimitry Andric   entry *d = _buffer;
2040b57cec5SDimitry Andric   for (const entry *s = _buffer; s < _bufferUsed; ++s) {
2050b57cec5SDimitry Andric     if (s->mh != mh) {
2060b57cec5SDimitry Andric       if (d != s)
2070b57cec5SDimitry Andric         *d = *s;
2080b57cec5SDimitry Andric       ++d;
2090b57cec5SDimitry Andric     }
2100b57cec5SDimitry Andric   }
2110b57cec5SDimitry Andric   _bufferUsed = d;
2120b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric #ifdef __APPLE__
2160b57cec5SDimitry Andric template <typename A>
2170b57cec5SDimitry Andric void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
2180b57cec5SDimitry Andric   removeAllIn((pint_t) mh);
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric #endif
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric template <typename A>
2230b57cec5SDimitry Andric void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
2240b57cec5SDimitry Andric     unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
2250b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
2260b57cec5SDimitry Andric   for (entry *p = _buffer; p < _bufferUsed; ++p) {
2270b57cec5SDimitry Andric     (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
2280b57cec5SDimitry Andric   }
2290b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric #define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
2370b57cec5SDimitry Andric template <typename A> class UnwindSectionHeader {
2380b57cec5SDimitry Andric public:
2390b57cec5SDimitry Andric   UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
2400b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   uint32_t version() const {
2430b57cec5SDimitry Andric     return _addressSpace.get32(_addr +
2440b57cec5SDimitry Andric                                offsetof(unwind_info_section_header, version));
2450b57cec5SDimitry Andric   }
2460b57cec5SDimitry Andric   uint32_t commonEncodingsArraySectionOffset() const {
2470b57cec5SDimitry Andric     return _addressSpace.get32(_addr +
2480b57cec5SDimitry Andric                                offsetof(unwind_info_section_header,
2490b57cec5SDimitry Andric                                         commonEncodingsArraySectionOffset));
2500b57cec5SDimitry Andric   }
2510b57cec5SDimitry Andric   uint32_t commonEncodingsArrayCount() const {
2520b57cec5SDimitry Andric     return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
2530b57cec5SDimitry Andric                                                 commonEncodingsArrayCount));
2540b57cec5SDimitry Andric   }
2550b57cec5SDimitry Andric   uint32_t personalityArraySectionOffset() const {
2560b57cec5SDimitry Andric     return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
2570b57cec5SDimitry Andric                                                 personalityArraySectionOffset));
2580b57cec5SDimitry Andric   }
2590b57cec5SDimitry Andric   uint32_t personalityArrayCount() const {
2600b57cec5SDimitry Andric     return _addressSpace.get32(
2610b57cec5SDimitry Andric         _addr + offsetof(unwind_info_section_header, personalityArrayCount));
2620b57cec5SDimitry Andric   }
2630b57cec5SDimitry Andric   uint32_t indexSectionOffset() const {
2640b57cec5SDimitry Andric     return _addressSpace.get32(
2650b57cec5SDimitry Andric         _addr + offsetof(unwind_info_section_header, indexSectionOffset));
2660b57cec5SDimitry Andric   }
2670b57cec5SDimitry Andric   uint32_t indexCount() const {
2680b57cec5SDimitry Andric     return _addressSpace.get32(
2690b57cec5SDimitry Andric         _addr + offsetof(unwind_info_section_header, indexCount));
2700b57cec5SDimitry Andric   }
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric private:
2730b57cec5SDimitry Andric   A                     &_addressSpace;
2740b57cec5SDimitry Andric   typename A::pint_t     _addr;
2750b57cec5SDimitry Andric };
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric template <typename A> class UnwindSectionIndexArray {
2780b57cec5SDimitry Andric public:
2790b57cec5SDimitry Andric   UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
2800b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
2830b57cec5SDimitry Andric     return _addressSpace.get32(
2840b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
2850b57cec5SDimitry Andric                               functionOffset));
2860b57cec5SDimitry Andric   }
2870b57cec5SDimitry Andric   uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
2880b57cec5SDimitry Andric     return _addressSpace.get32(
2890b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
2900b57cec5SDimitry Andric                               secondLevelPagesSectionOffset));
2910b57cec5SDimitry Andric   }
2920b57cec5SDimitry Andric   uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
2930b57cec5SDimitry Andric     return _addressSpace.get32(
2940b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
2950b57cec5SDimitry Andric                               lsdaIndexArraySectionOffset));
2960b57cec5SDimitry Andric   }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric private:
2990b57cec5SDimitry Andric   A                   &_addressSpace;
3000b57cec5SDimitry Andric   typename A::pint_t   _addr;
3010b57cec5SDimitry Andric };
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric template <typename A> class UnwindSectionRegularPageHeader {
3040b57cec5SDimitry Andric public:
3050b57cec5SDimitry Andric   UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
3060b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   uint32_t kind() const {
3090b57cec5SDimitry Andric     return _addressSpace.get32(
3100b57cec5SDimitry Andric         _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
3110b57cec5SDimitry Andric   }
3120b57cec5SDimitry Andric   uint16_t entryPageOffset() const {
3130b57cec5SDimitry Andric     return _addressSpace.get16(
3140b57cec5SDimitry Andric         _addr + offsetof(unwind_info_regular_second_level_page_header,
3150b57cec5SDimitry Andric                          entryPageOffset));
3160b57cec5SDimitry Andric   }
3170b57cec5SDimitry Andric   uint16_t entryCount() const {
3180b57cec5SDimitry Andric     return _addressSpace.get16(
3190b57cec5SDimitry Andric         _addr +
3200b57cec5SDimitry Andric         offsetof(unwind_info_regular_second_level_page_header, entryCount));
3210b57cec5SDimitry Andric   }
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric private:
3240b57cec5SDimitry Andric   A &_addressSpace;
3250b57cec5SDimitry Andric   typename A::pint_t _addr;
3260b57cec5SDimitry Andric };
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric template <typename A> class UnwindSectionRegularArray {
3290b57cec5SDimitry Andric public:
3300b57cec5SDimitry Andric   UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
3310b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
3340b57cec5SDimitry Andric     return _addressSpace.get32(
3350b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
3360b57cec5SDimitry Andric                               functionOffset));
3370b57cec5SDimitry Andric   }
3380b57cec5SDimitry Andric   uint32_t encoding(uint32_t index) const {
3390b57cec5SDimitry Andric     return _addressSpace.get32(
3400b57cec5SDimitry Andric         _addr +
3410b57cec5SDimitry Andric         arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
3420b57cec5SDimitry Andric   }
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric private:
3450b57cec5SDimitry Andric   A &_addressSpace;
3460b57cec5SDimitry Andric   typename A::pint_t _addr;
3470b57cec5SDimitry Andric };
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric template <typename A> class UnwindSectionCompressedPageHeader {
3500b57cec5SDimitry Andric public:
3510b57cec5SDimitry Andric   UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
3520b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric   uint32_t kind() const {
3550b57cec5SDimitry Andric     return _addressSpace.get32(
3560b57cec5SDimitry Andric         _addr +
3570b57cec5SDimitry Andric         offsetof(unwind_info_compressed_second_level_page_header, kind));
3580b57cec5SDimitry Andric   }
3590b57cec5SDimitry Andric   uint16_t entryPageOffset() const {
3600b57cec5SDimitry Andric     return _addressSpace.get16(
3610b57cec5SDimitry Andric         _addr + offsetof(unwind_info_compressed_second_level_page_header,
3620b57cec5SDimitry Andric                          entryPageOffset));
3630b57cec5SDimitry Andric   }
3640b57cec5SDimitry Andric   uint16_t entryCount() const {
3650b57cec5SDimitry Andric     return _addressSpace.get16(
3660b57cec5SDimitry Andric         _addr +
3670b57cec5SDimitry Andric         offsetof(unwind_info_compressed_second_level_page_header, entryCount));
3680b57cec5SDimitry Andric   }
3690b57cec5SDimitry Andric   uint16_t encodingsPageOffset() const {
3700b57cec5SDimitry Andric     return _addressSpace.get16(
3710b57cec5SDimitry Andric         _addr + offsetof(unwind_info_compressed_second_level_page_header,
3720b57cec5SDimitry Andric                          encodingsPageOffset));
3730b57cec5SDimitry Andric   }
3740b57cec5SDimitry Andric   uint16_t encodingsCount() const {
3750b57cec5SDimitry Andric     return _addressSpace.get16(
3760b57cec5SDimitry Andric         _addr + offsetof(unwind_info_compressed_second_level_page_header,
3770b57cec5SDimitry Andric                          encodingsCount));
3780b57cec5SDimitry Andric   }
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric private:
3810b57cec5SDimitry Andric   A &_addressSpace;
3820b57cec5SDimitry Andric   typename A::pint_t _addr;
3830b57cec5SDimitry Andric };
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric template <typename A> class UnwindSectionCompressedArray {
3860b57cec5SDimitry Andric public:
3870b57cec5SDimitry Andric   UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
3880b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
3910b57cec5SDimitry Andric     return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
3920b57cec5SDimitry Andric         _addressSpace.get32(_addr + index * sizeof(uint32_t)));
3930b57cec5SDimitry Andric   }
3940b57cec5SDimitry Andric   uint16_t encodingIndex(uint32_t index) const {
3950b57cec5SDimitry Andric     return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
3960b57cec5SDimitry Andric         _addressSpace.get32(_addr + index * sizeof(uint32_t)));
3970b57cec5SDimitry Andric   }
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric private:
4000b57cec5SDimitry Andric   A &_addressSpace;
4010b57cec5SDimitry Andric   typename A::pint_t _addr;
4020b57cec5SDimitry Andric };
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric template <typename A> class UnwindSectionLsdaArray {
4050b57cec5SDimitry Andric public:
4060b57cec5SDimitry Andric   UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
4070b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
4100b57cec5SDimitry Andric     return _addressSpace.get32(
4110b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
4120b57cec5SDimitry Andric                               index, functionOffset));
4130b57cec5SDimitry Andric   }
4140b57cec5SDimitry Andric   uint32_t lsdaOffset(uint32_t index) const {
4150b57cec5SDimitry Andric     return _addressSpace.get32(
4160b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
4170b57cec5SDimitry Andric                               index, lsdaOffset));
4180b57cec5SDimitry Andric   }
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric private:
4210b57cec5SDimitry Andric   A                   &_addressSpace;
4220b57cec5SDimitry Andric   typename A::pint_t   _addr;
4230b57cec5SDimitry Andric };
4240b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
4270b57cec5SDimitry Andric public:
4280b57cec5SDimitry Andric   // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
4290b57cec5SDimitry Andric   // This avoids an unnecessary dependency to libc++abi.
4300b57cec5SDimitry Andric   void operator delete(void *, size_t) {}
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric   virtual ~AbstractUnwindCursor() {}
4330b57cec5SDimitry Andric   virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
4340b57cec5SDimitry Andric   virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
4350b57cec5SDimitry Andric   virtual void setReg(int, unw_word_t) {
4360b57cec5SDimitry Andric     _LIBUNWIND_ABORT("setReg not implemented");
4370b57cec5SDimitry Andric   }
4380b57cec5SDimitry Andric   virtual bool validFloatReg(int) {
4390b57cec5SDimitry Andric     _LIBUNWIND_ABORT("validFloatReg not implemented");
4400b57cec5SDimitry Andric   }
4410b57cec5SDimitry Andric   virtual unw_fpreg_t getFloatReg(int) {
4420b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getFloatReg not implemented");
4430b57cec5SDimitry Andric   }
4440b57cec5SDimitry Andric   virtual void setFloatReg(int, unw_fpreg_t) {
4450b57cec5SDimitry Andric     _LIBUNWIND_ABORT("setFloatReg not implemented");
4460b57cec5SDimitry Andric   }
447bdd1243dSDimitry Andric   virtual int step(bool = false) { _LIBUNWIND_ABORT("step not implemented"); }
4480b57cec5SDimitry Andric   virtual void getInfo(unw_proc_info_t *) {
4490b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getInfo not implemented");
4500b57cec5SDimitry Andric   }
4510b57cec5SDimitry Andric   virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
4520b57cec5SDimitry Andric   virtual bool isSignalFrame() {
4530b57cec5SDimitry Andric     _LIBUNWIND_ABORT("isSignalFrame not implemented");
4540b57cec5SDimitry Andric   }
4550b57cec5SDimitry Andric   virtual bool getFunctionName(char *, size_t, unw_word_t *) {
4560b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getFunctionName not implemented");
4570b57cec5SDimitry Andric   }
4580b57cec5SDimitry Andric   virtual void setInfoBasedOnIPRegister(bool = false) {
4590b57cec5SDimitry Andric     _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
4600b57cec5SDimitry Andric   }
4610b57cec5SDimitry Andric   virtual const char *getRegisterName(int) {
4620b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getRegisterName not implemented");
4630b57cec5SDimitry Andric   }
4640b57cec5SDimitry Andric #ifdef __arm__
4650b57cec5SDimitry Andric   virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
4660b57cec5SDimitry Andric #endif
467349cc55cSDimitry Andric 
46881ad6265SDimitry Andric #ifdef _AIX
46981ad6265SDimitry Andric   virtual uintptr_t getDataRelBase() {
47081ad6265SDimitry Andric     _LIBUNWIND_ABORT("getDataRelBase not implemented");
47181ad6265SDimitry Andric   }
47281ad6265SDimitry Andric #endif
47381ad6265SDimitry Andric 
474*62987288SDimitry Andric #if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
475349cc55cSDimitry Andric   virtual void *get_registers() {
476349cc55cSDimitry Andric     _LIBUNWIND_ABORT("get_registers not implemented");
477349cc55cSDimitry Andric   }
478349cc55cSDimitry Andric #endif
4790b57cec5SDimitry Andric };
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric /// \c UnwindCursor contains all state (including all register values) during
4840b57cec5SDimitry Andric /// an unwind.  This is normally stack-allocated inside a unw_cursor_t.
4850b57cec5SDimitry Andric template <typename A, typename R>
4860b57cec5SDimitry Andric class UnwindCursor : public AbstractUnwindCursor {
4870b57cec5SDimitry Andric   typedef typename A::pint_t pint_t;
4880b57cec5SDimitry Andric public:
4890b57cec5SDimitry Andric                       UnwindCursor(unw_context_t *context, A &as);
4900b57cec5SDimitry Andric                       UnwindCursor(CONTEXT *context, A &as);
4910b57cec5SDimitry Andric                       UnwindCursor(A &as, void *threadArg);
4920b57cec5SDimitry Andric   virtual             ~UnwindCursor() {}
4930b57cec5SDimitry Andric   virtual bool        validReg(int);
4940b57cec5SDimitry Andric   virtual unw_word_t  getReg(int);
4950b57cec5SDimitry Andric   virtual void        setReg(int, unw_word_t);
4960b57cec5SDimitry Andric   virtual bool        validFloatReg(int);
4970b57cec5SDimitry Andric   virtual unw_fpreg_t getFloatReg(int);
4980b57cec5SDimitry Andric   virtual void        setFloatReg(int, unw_fpreg_t);
499bdd1243dSDimitry Andric   virtual int         step(bool = false);
5000b57cec5SDimitry Andric   virtual void        getInfo(unw_proc_info_t *);
5010b57cec5SDimitry Andric   virtual void        jumpto();
5020b57cec5SDimitry Andric   virtual bool        isSignalFrame();
5030b57cec5SDimitry Andric   virtual bool        getFunctionName(char *buf, size_t len, unw_word_t *off);
5040b57cec5SDimitry Andric   virtual void        setInfoBasedOnIPRegister(bool isReturnAddress = false);
5050b57cec5SDimitry Andric   virtual const char *getRegisterName(int num);
5060b57cec5SDimitry Andric #ifdef __arm__
5070b57cec5SDimitry Andric   virtual void        saveVFPAsX();
5080b57cec5SDimitry Andric #endif
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric   DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
51106c3fb27SDimitry Andric   void setDispatcherContext(DISPATCHER_CONTEXT *disp) {
51206c3fb27SDimitry Andric     _dispContext = *disp;
51306c3fb27SDimitry Andric     _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
51406c3fb27SDimitry Andric     if (_dispContext.LanguageHandler) {
51506c3fb27SDimitry Andric       _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
51606c3fb27SDimitry Andric     } else
51706c3fb27SDimitry Andric       _info.handler = 0;
51806c3fb27SDimitry Andric   }
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric   // libunwind does not and should not depend on C++ library which means that we
521bdd1243dSDimitry Andric   // need our own definition of inline placement new.
5220b57cec5SDimitry Andric   static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric private:
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric   pint_t getLastPC() const { return _dispContext.ControlPc; }
5270b57cec5SDimitry Andric   void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
5280b57cec5SDimitry Andric   RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
52981ad6265SDimitry Andric #ifdef __arm__
53081ad6265SDimitry Andric     // Remove the thumb bit; FunctionEntry ranges don't include the thumb bit.
53181ad6265SDimitry Andric     pc &= ~1U;
53281ad6265SDimitry Andric #endif
53381ad6265SDimitry Andric     // If pc points exactly at the end of the range, we might resolve the
53481ad6265SDimitry Andric     // next function instead. Decrement pc by 1 to fit inside the current
53581ad6265SDimitry Andric     // function.
53681ad6265SDimitry Andric     pc -= 1;
5370b57cec5SDimitry Andric     _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
5380b57cec5SDimitry Andric                                                         &_dispContext.ImageBase,
5390b57cec5SDimitry Andric                                                         _dispContext.HistoryTable);
5400b57cec5SDimitry Andric     *base = _dispContext.ImageBase;
5410b57cec5SDimitry Andric     return _dispContext.FunctionEntry;
5420b57cec5SDimitry Andric   }
5430b57cec5SDimitry Andric   bool getInfoFromSEH(pint_t pc);
5440b57cec5SDimitry Andric   int stepWithSEHData() {
5450b57cec5SDimitry Andric     _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
5460b57cec5SDimitry Andric                                                     _dispContext.ImageBase,
5470b57cec5SDimitry Andric                                                     _dispContext.ControlPc,
5480b57cec5SDimitry Andric                                                     _dispContext.FunctionEntry,
5490b57cec5SDimitry Andric                                                     _dispContext.ContextRecord,
5500b57cec5SDimitry Andric                                                     &_dispContext.HandlerData,
5510b57cec5SDimitry Andric                                                     &_dispContext.EstablisherFrame,
5520b57cec5SDimitry Andric                                                     NULL);
5530b57cec5SDimitry Andric     // Update some fields of the unwind info now, since we have them.
5540b57cec5SDimitry Andric     _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
5550b57cec5SDimitry Andric     if (_dispContext.LanguageHandler) {
5560b57cec5SDimitry Andric       _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
5570b57cec5SDimitry Andric     } else
5580b57cec5SDimitry Andric       _info.handler = 0;
5590b57cec5SDimitry Andric     return UNW_STEP_SUCCESS;
5600b57cec5SDimitry Andric   }
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric   A                   &_addressSpace;
5630b57cec5SDimitry Andric   unw_proc_info_t      _info;
5640b57cec5SDimitry Andric   DISPATCHER_CONTEXT   _dispContext;
5650b57cec5SDimitry Andric   CONTEXT              _msContext;
5660b57cec5SDimitry Andric   UNWIND_HISTORY_TABLE _histTable;
5670b57cec5SDimitry Andric   bool                 _unwindInfoMissing;
5680b57cec5SDimitry Andric };
5690b57cec5SDimitry Andric 
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric template <typename A, typename R>
5720b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
5730b57cec5SDimitry Andric     : _addressSpace(as), _unwindInfoMissing(false) {
5740b57cec5SDimitry Andric   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
5750b57cec5SDimitry Andric                 "UnwindCursor<> does not fit in unw_cursor_t");
576e8d8bef9SDimitry Andric   static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
577e8d8bef9SDimitry Andric                 "UnwindCursor<> requires more alignment than unw_cursor_t");
5780b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
5790b57cec5SDimitry Andric   memset(&_histTable, 0, sizeof(_histTable));
58006c3fb27SDimitry Andric   memset(&_dispContext, 0, sizeof(_dispContext));
5810b57cec5SDimitry Andric   _dispContext.ContextRecord = &_msContext;
5820b57cec5SDimitry Andric   _dispContext.HistoryTable = &_histTable;
5830b57cec5SDimitry Andric   // Initialize MS context from ours.
5840b57cec5SDimitry Andric   R r(context);
58506c3fb27SDimitry Andric   RtlCaptureContext(&_msContext);
5860b57cec5SDimitry Andric   _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
5870b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
5880b57cec5SDimitry Andric   _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
5890b57cec5SDimitry Andric   _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
5900b57cec5SDimitry Andric   _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
5910b57cec5SDimitry Andric   _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
5920b57cec5SDimitry Andric   _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
5930b57cec5SDimitry Andric   _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
5940b57cec5SDimitry Andric   _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
5950b57cec5SDimitry Andric   _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
5960b57cec5SDimitry Andric   _msContext.R8 = r.getRegister(UNW_X86_64_R8);
5970b57cec5SDimitry Andric   _msContext.R9 = r.getRegister(UNW_X86_64_R9);
5980b57cec5SDimitry Andric   _msContext.R10 = r.getRegister(UNW_X86_64_R10);
5990b57cec5SDimitry Andric   _msContext.R11 = r.getRegister(UNW_X86_64_R11);
6000b57cec5SDimitry Andric   _msContext.R12 = r.getRegister(UNW_X86_64_R12);
6010b57cec5SDimitry Andric   _msContext.R13 = r.getRegister(UNW_X86_64_R13);
6020b57cec5SDimitry Andric   _msContext.R14 = r.getRegister(UNW_X86_64_R14);
6030b57cec5SDimitry Andric   _msContext.R15 = r.getRegister(UNW_X86_64_R15);
6040b57cec5SDimitry Andric   _msContext.Rip = r.getRegister(UNW_REG_IP);
6050b57cec5SDimitry Andric   union {
6060b57cec5SDimitry Andric     v128 v;
6070b57cec5SDimitry Andric     M128A m;
6080b57cec5SDimitry Andric   } t;
6090b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM0);
6100b57cec5SDimitry Andric   _msContext.Xmm0 = t.m;
6110b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM1);
6120b57cec5SDimitry Andric   _msContext.Xmm1 = t.m;
6130b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM2);
6140b57cec5SDimitry Andric   _msContext.Xmm2 = t.m;
6150b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM3);
6160b57cec5SDimitry Andric   _msContext.Xmm3 = t.m;
6170b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM4);
6180b57cec5SDimitry Andric   _msContext.Xmm4 = t.m;
6190b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM5);
6200b57cec5SDimitry Andric   _msContext.Xmm5 = t.m;
6210b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM6);
6220b57cec5SDimitry Andric   _msContext.Xmm6 = t.m;
6230b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM7);
6240b57cec5SDimitry Andric   _msContext.Xmm7 = t.m;
6250b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM8);
6260b57cec5SDimitry Andric   _msContext.Xmm8 = t.m;
6270b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM9);
6280b57cec5SDimitry Andric   _msContext.Xmm9 = t.m;
6290b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM10);
6300b57cec5SDimitry Andric   _msContext.Xmm10 = t.m;
6310b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM11);
6320b57cec5SDimitry Andric   _msContext.Xmm11 = t.m;
6330b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM12);
6340b57cec5SDimitry Andric   _msContext.Xmm12 = t.m;
6350b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM13);
6360b57cec5SDimitry Andric   _msContext.Xmm13 = t.m;
6370b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM14);
6380b57cec5SDimitry Andric   _msContext.Xmm14 = t.m;
6390b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM15);
6400b57cec5SDimitry Andric   _msContext.Xmm15 = t.m;
6410b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
6420b57cec5SDimitry Andric   _msContext.R0 = r.getRegister(UNW_ARM_R0);
6430b57cec5SDimitry Andric   _msContext.R1 = r.getRegister(UNW_ARM_R1);
6440b57cec5SDimitry Andric   _msContext.R2 = r.getRegister(UNW_ARM_R2);
6450b57cec5SDimitry Andric   _msContext.R3 = r.getRegister(UNW_ARM_R3);
6460b57cec5SDimitry Andric   _msContext.R4 = r.getRegister(UNW_ARM_R4);
6470b57cec5SDimitry Andric   _msContext.R5 = r.getRegister(UNW_ARM_R5);
6480b57cec5SDimitry Andric   _msContext.R6 = r.getRegister(UNW_ARM_R6);
6490b57cec5SDimitry Andric   _msContext.R7 = r.getRegister(UNW_ARM_R7);
6500b57cec5SDimitry Andric   _msContext.R8 = r.getRegister(UNW_ARM_R8);
6510b57cec5SDimitry Andric   _msContext.R9 = r.getRegister(UNW_ARM_R9);
6520b57cec5SDimitry Andric   _msContext.R10 = r.getRegister(UNW_ARM_R10);
6530b57cec5SDimitry Andric   _msContext.R11 = r.getRegister(UNW_ARM_R11);
6540b57cec5SDimitry Andric   _msContext.R12 = r.getRegister(UNW_ARM_R12);
6550b57cec5SDimitry Andric   _msContext.Sp = r.getRegister(UNW_ARM_SP);
6560b57cec5SDimitry Andric   _msContext.Lr = r.getRegister(UNW_ARM_LR);
6570b57cec5SDimitry Andric   _msContext.Pc = r.getRegister(UNW_ARM_IP);
6580b57cec5SDimitry Andric   for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
6590b57cec5SDimitry Andric     union {
6600b57cec5SDimitry Andric       uint64_t w;
6610b57cec5SDimitry Andric       double d;
6620b57cec5SDimitry Andric     } d;
6630b57cec5SDimitry Andric     d.d = r.getFloatRegister(i);
6640b57cec5SDimitry Andric     _msContext.D[i - UNW_ARM_D0] = d.w;
6650b57cec5SDimitry Andric   }
6660b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
667349cc55cSDimitry Andric   for (int i = UNW_AARCH64_X0; i <= UNW_ARM64_X30; ++i)
668349cc55cSDimitry Andric     _msContext.X[i - UNW_AARCH64_X0] = r.getRegister(i);
6690b57cec5SDimitry Andric   _msContext.Sp = r.getRegister(UNW_REG_SP);
6700b57cec5SDimitry Andric   _msContext.Pc = r.getRegister(UNW_REG_IP);
671349cc55cSDimitry Andric   for (int i = UNW_AARCH64_V0; i <= UNW_ARM64_D31; ++i)
672349cc55cSDimitry Andric     _msContext.V[i - UNW_AARCH64_V0].D[0] = r.getFloatRegister(i);
6730b57cec5SDimitry Andric #endif
6740b57cec5SDimitry Andric }
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric template <typename A, typename R>
6770b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
6780b57cec5SDimitry Andric     : _addressSpace(as), _unwindInfoMissing(false) {
6790b57cec5SDimitry Andric   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
6800b57cec5SDimitry Andric                 "UnwindCursor<> does not fit in unw_cursor_t");
6810b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
6820b57cec5SDimitry Andric   memset(&_histTable, 0, sizeof(_histTable));
68306c3fb27SDimitry Andric   memset(&_dispContext, 0, sizeof(_dispContext));
6840b57cec5SDimitry Andric   _dispContext.ContextRecord = &_msContext;
6850b57cec5SDimitry Andric   _dispContext.HistoryTable = &_histTable;
6860b57cec5SDimitry Andric   _msContext = *context;
6870b57cec5SDimitry Andric }
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric template <typename A, typename R>
6910b57cec5SDimitry Andric bool UnwindCursor<A, R>::validReg(int regNum) {
6920b57cec5SDimitry Andric   if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
6930b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
69406c3fb27SDimitry Andric   if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_RIP) return true;
6950b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
6960eae32dcSDimitry Andric   if ((regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) ||
6970eae32dcSDimitry Andric       regNum == UNW_ARM_RA_AUTH_CODE)
6980eae32dcSDimitry Andric     return true;
6990b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
700349cc55cSDimitry Andric   if (regNum >= UNW_AARCH64_X0 && regNum <= UNW_ARM64_X30) return true;
7010b57cec5SDimitry Andric #endif
7020b57cec5SDimitry Andric   return false;
7030b57cec5SDimitry Andric }
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric template <typename A, typename R>
7060b57cec5SDimitry Andric unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
7070b57cec5SDimitry Andric   switch (regNum) {
7080b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
70906c3fb27SDimitry Andric   case UNW_X86_64_RIP:
7100b57cec5SDimitry Andric   case UNW_REG_IP: return _msContext.Rip;
7110b57cec5SDimitry Andric   case UNW_X86_64_RAX: return _msContext.Rax;
7120b57cec5SDimitry Andric   case UNW_X86_64_RDX: return _msContext.Rdx;
7130b57cec5SDimitry Andric   case UNW_X86_64_RCX: return _msContext.Rcx;
7140b57cec5SDimitry Andric   case UNW_X86_64_RBX: return _msContext.Rbx;
7150b57cec5SDimitry Andric   case UNW_REG_SP:
7160b57cec5SDimitry Andric   case UNW_X86_64_RSP: return _msContext.Rsp;
7170b57cec5SDimitry Andric   case UNW_X86_64_RBP: return _msContext.Rbp;
7180b57cec5SDimitry Andric   case UNW_X86_64_RSI: return _msContext.Rsi;
7190b57cec5SDimitry Andric   case UNW_X86_64_RDI: return _msContext.Rdi;
7200b57cec5SDimitry Andric   case UNW_X86_64_R8: return _msContext.R8;
7210b57cec5SDimitry Andric   case UNW_X86_64_R9: return _msContext.R9;
7220b57cec5SDimitry Andric   case UNW_X86_64_R10: return _msContext.R10;
7230b57cec5SDimitry Andric   case UNW_X86_64_R11: return _msContext.R11;
7240b57cec5SDimitry Andric   case UNW_X86_64_R12: return _msContext.R12;
7250b57cec5SDimitry Andric   case UNW_X86_64_R13: return _msContext.R13;
7260b57cec5SDimitry Andric   case UNW_X86_64_R14: return _msContext.R14;
7270b57cec5SDimitry Andric   case UNW_X86_64_R15: return _msContext.R15;
7280b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
7290b57cec5SDimitry Andric   case UNW_ARM_R0: return _msContext.R0;
7300b57cec5SDimitry Andric   case UNW_ARM_R1: return _msContext.R1;
7310b57cec5SDimitry Andric   case UNW_ARM_R2: return _msContext.R2;
7320b57cec5SDimitry Andric   case UNW_ARM_R3: return _msContext.R3;
7330b57cec5SDimitry Andric   case UNW_ARM_R4: return _msContext.R4;
7340b57cec5SDimitry Andric   case UNW_ARM_R5: return _msContext.R5;
7350b57cec5SDimitry Andric   case UNW_ARM_R6: return _msContext.R6;
7360b57cec5SDimitry Andric   case UNW_ARM_R7: return _msContext.R7;
7370b57cec5SDimitry Andric   case UNW_ARM_R8: return _msContext.R8;
7380b57cec5SDimitry Andric   case UNW_ARM_R9: return _msContext.R9;
7390b57cec5SDimitry Andric   case UNW_ARM_R10: return _msContext.R10;
7400b57cec5SDimitry Andric   case UNW_ARM_R11: return _msContext.R11;
7410b57cec5SDimitry Andric   case UNW_ARM_R12: return _msContext.R12;
7420b57cec5SDimitry Andric   case UNW_REG_SP:
7430b57cec5SDimitry Andric   case UNW_ARM_SP: return _msContext.Sp;
7440b57cec5SDimitry Andric   case UNW_ARM_LR: return _msContext.Lr;
7450b57cec5SDimitry Andric   case UNW_REG_IP:
7460b57cec5SDimitry Andric   case UNW_ARM_IP: return _msContext.Pc;
7470b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
7480b57cec5SDimitry Andric   case UNW_REG_SP: return _msContext.Sp;
7490b57cec5SDimitry Andric   case UNW_REG_IP: return _msContext.Pc;
750349cc55cSDimitry Andric   default: return _msContext.X[regNum - UNW_AARCH64_X0];
7510b57cec5SDimitry Andric #endif
7520b57cec5SDimitry Andric   }
7530b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported register");
7540b57cec5SDimitry Andric }
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric template <typename A, typename R>
7570b57cec5SDimitry Andric void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
7580b57cec5SDimitry Andric   switch (regNum) {
7590b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
76006c3fb27SDimitry Andric   case UNW_X86_64_RIP:
7610b57cec5SDimitry Andric   case UNW_REG_IP: _msContext.Rip = value; break;
7620b57cec5SDimitry Andric   case UNW_X86_64_RAX: _msContext.Rax = value; break;
7630b57cec5SDimitry Andric   case UNW_X86_64_RDX: _msContext.Rdx = value; break;
7640b57cec5SDimitry Andric   case UNW_X86_64_RCX: _msContext.Rcx = value; break;
7650b57cec5SDimitry Andric   case UNW_X86_64_RBX: _msContext.Rbx = value; break;
7660b57cec5SDimitry Andric   case UNW_REG_SP:
7670b57cec5SDimitry Andric   case UNW_X86_64_RSP: _msContext.Rsp = value; break;
7680b57cec5SDimitry Andric   case UNW_X86_64_RBP: _msContext.Rbp = value; break;
7690b57cec5SDimitry Andric   case UNW_X86_64_RSI: _msContext.Rsi = value; break;
7700b57cec5SDimitry Andric   case UNW_X86_64_RDI: _msContext.Rdi = value; break;
7710b57cec5SDimitry Andric   case UNW_X86_64_R8: _msContext.R8 = value; break;
7720b57cec5SDimitry Andric   case UNW_X86_64_R9: _msContext.R9 = value; break;
7730b57cec5SDimitry Andric   case UNW_X86_64_R10: _msContext.R10 = value; break;
7740b57cec5SDimitry Andric   case UNW_X86_64_R11: _msContext.R11 = value; break;
7750b57cec5SDimitry Andric   case UNW_X86_64_R12: _msContext.R12 = value; break;
7760b57cec5SDimitry Andric   case UNW_X86_64_R13: _msContext.R13 = value; break;
7770b57cec5SDimitry Andric   case UNW_X86_64_R14: _msContext.R14 = value; break;
7780b57cec5SDimitry Andric   case UNW_X86_64_R15: _msContext.R15 = value; break;
7790b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
7800b57cec5SDimitry Andric   case UNW_ARM_R0: _msContext.R0 = value; break;
7810b57cec5SDimitry Andric   case UNW_ARM_R1: _msContext.R1 = value; break;
7820b57cec5SDimitry Andric   case UNW_ARM_R2: _msContext.R2 = value; break;
7830b57cec5SDimitry Andric   case UNW_ARM_R3: _msContext.R3 = value; break;
7840b57cec5SDimitry Andric   case UNW_ARM_R4: _msContext.R4 = value; break;
7850b57cec5SDimitry Andric   case UNW_ARM_R5: _msContext.R5 = value; break;
7860b57cec5SDimitry Andric   case UNW_ARM_R6: _msContext.R6 = value; break;
7870b57cec5SDimitry Andric   case UNW_ARM_R7: _msContext.R7 = value; break;
7880b57cec5SDimitry Andric   case UNW_ARM_R8: _msContext.R8 = value; break;
7890b57cec5SDimitry Andric   case UNW_ARM_R9: _msContext.R9 = value; break;
7900b57cec5SDimitry Andric   case UNW_ARM_R10: _msContext.R10 = value; break;
7910b57cec5SDimitry Andric   case UNW_ARM_R11: _msContext.R11 = value; break;
7920b57cec5SDimitry Andric   case UNW_ARM_R12: _msContext.R12 = value; break;
7930b57cec5SDimitry Andric   case UNW_REG_SP:
7940b57cec5SDimitry Andric   case UNW_ARM_SP: _msContext.Sp = value; break;
7950b57cec5SDimitry Andric   case UNW_ARM_LR: _msContext.Lr = value; break;
7960b57cec5SDimitry Andric   case UNW_REG_IP:
7970b57cec5SDimitry Andric   case UNW_ARM_IP: _msContext.Pc = value; break;
7980b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
7990b57cec5SDimitry Andric   case UNW_REG_SP: _msContext.Sp = value; break;
8000b57cec5SDimitry Andric   case UNW_REG_IP: _msContext.Pc = value; break;
801349cc55cSDimitry Andric   case UNW_AARCH64_X0:
802349cc55cSDimitry Andric   case UNW_AARCH64_X1:
803349cc55cSDimitry Andric   case UNW_AARCH64_X2:
804349cc55cSDimitry Andric   case UNW_AARCH64_X3:
805349cc55cSDimitry Andric   case UNW_AARCH64_X4:
806349cc55cSDimitry Andric   case UNW_AARCH64_X5:
807349cc55cSDimitry Andric   case UNW_AARCH64_X6:
808349cc55cSDimitry Andric   case UNW_AARCH64_X7:
809349cc55cSDimitry Andric   case UNW_AARCH64_X8:
810349cc55cSDimitry Andric   case UNW_AARCH64_X9:
811349cc55cSDimitry Andric   case UNW_AARCH64_X10:
812349cc55cSDimitry Andric   case UNW_AARCH64_X11:
813349cc55cSDimitry Andric   case UNW_AARCH64_X12:
814349cc55cSDimitry Andric   case UNW_AARCH64_X13:
815349cc55cSDimitry Andric   case UNW_AARCH64_X14:
816349cc55cSDimitry Andric   case UNW_AARCH64_X15:
817349cc55cSDimitry Andric   case UNW_AARCH64_X16:
818349cc55cSDimitry Andric   case UNW_AARCH64_X17:
819349cc55cSDimitry Andric   case UNW_AARCH64_X18:
820349cc55cSDimitry Andric   case UNW_AARCH64_X19:
821349cc55cSDimitry Andric   case UNW_AARCH64_X20:
822349cc55cSDimitry Andric   case UNW_AARCH64_X21:
823349cc55cSDimitry Andric   case UNW_AARCH64_X22:
824349cc55cSDimitry Andric   case UNW_AARCH64_X23:
825349cc55cSDimitry Andric   case UNW_AARCH64_X24:
826349cc55cSDimitry Andric   case UNW_AARCH64_X25:
827349cc55cSDimitry Andric   case UNW_AARCH64_X26:
828349cc55cSDimitry Andric   case UNW_AARCH64_X27:
829349cc55cSDimitry Andric   case UNW_AARCH64_X28:
830349cc55cSDimitry Andric   case UNW_AARCH64_FP:
831349cc55cSDimitry Andric   case UNW_AARCH64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
8320b57cec5SDimitry Andric #endif
8330b57cec5SDimitry Andric   default:
8340b57cec5SDimitry Andric     _LIBUNWIND_ABORT("unsupported register");
8350b57cec5SDimitry Andric   }
8360b57cec5SDimitry Andric }
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric template <typename A, typename R>
8390b57cec5SDimitry Andric bool UnwindCursor<A, R>::validFloatReg(int regNum) {
8400b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
8410b57cec5SDimitry Andric   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
8420b57cec5SDimitry Andric   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
8430b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
844349cc55cSDimitry Andric   if (regNum >= UNW_AARCH64_V0 && regNum <= UNW_ARM64_D31) return true;
8450b57cec5SDimitry Andric #else
8460b57cec5SDimitry Andric   (void)regNum;
8470b57cec5SDimitry Andric #endif
8480b57cec5SDimitry Andric   return false;
8490b57cec5SDimitry Andric }
8500b57cec5SDimitry Andric 
8510b57cec5SDimitry Andric template <typename A, typename R>
8520b57cec5SDimitry Andric unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
8530b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
8540b57cec5SDimitry Andric   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
8550b57cec5SDimitry Andric     union {
8560b57cec5SDimitry Andric       uint32_t w;
8570b57cec5SDimitry Andric       float f;
8580b57cec5SDimitry Andric     } d;
8590b57cec5SDimitry Andric     d.w = _msContext.S[regNum - UNW_ARM_S0];
8600b57cec5SDimitry Andric     return d.f;
8610b57cec5SDimitry Andric   }
8620b57cec5SDimitry Andric   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
8630b57cec5SDimitry Andric     union {
8640b57cec5SDimitry Andric       uint64_t w;
8650b57cec5SDimitry Andric       double d;
8660b57cec5SDimitry Andric     } d;
8670b57cec5SDimitry Andric     d.w = _msContext.D[regNum - UNW_ARM_D0];
8680b57cec5SDimitry Andric     return d.d;
8690b57cec5SDimitry Andric   }
8700b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported float register");
8710b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
872349cc55cSDimitry Andric   return _msContext.V[regNum - UNW_AARCH64_V0].D[0];
8730b57cec5SDimitry Andric #else
8740b57cec5SDimitry Andric   (void)regNum;
8750b57cec5SDimitry Andric   _LIBUNWIND_ABORT("float registers unimplemented");
8760b57cec5SDimitry Andric #endif
8770b57cec5SDimitry Andric }
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric template <typename A, typename R>
8800b57cec5SDimitry Andric void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
8810b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
8820b57cec5SDimitry Andric   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
8830b57cec5SDimitry Andric     union {
8840b57cec5SDimitry Andric       uint32_t w;
8850b57cec5SDimitry Andric       float f;
8860b57cec5SDimitry Andric     } d;
88781ad6265SDimitry Andric     d.f = (float)value;
8880b57cec5SDimitry Andric     _msContext.S[regNum - UNW_ARM_S0] = d.w;
8890b57cec5SDimitry Andric   }
8900b57cec5SDimitry Andric   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
8910b57cec5SDimitry Andric     union {
8920b57cec5SDimitry Andric       uint64_t w;
8930b57cec5SDimitry Andric       double d;
8940b57cec5SDimitry Andric     } d;
8950b57cec5SDimitry Andric     d.d = value;
8960b57cec5SDimitry Andric     _msContext.D[regNum - UNW_ARM_D0] = d.w;
8970b57cec5SDimitry Andric   }
8980b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported float register");
8990b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
900349cc55cSDimitry Andric   _msContext.V[regNum - UNW_AARCH64_V0].D[0] = value;
9010b57cec5SDimitry Andric #else
9020b57cec5SDimitry Andric   (void)regNum;
9030b57cec5SDimitry Andric   (void)value;
9040b57cec5SDimitry Andric   _LIBUNWIND_ABORT("float registers unimplemented");
9050b57cec5SDimitry Andric #endif
9060b57cec5SDimitry Andric }
9070b57cec5SDimitry Andric 
9080b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
9090b57cec5SDimitry Andric   RtlRestoreContext(&_msContext, nullptr);
9100b57cec5SDimitry Andric }
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric #ifdef __arm__
9130b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
9140b57cec5SDimitry Andric #endif
9150b57cec5SDimitry Andric 
9160b57cec5SDimitry Andric template <typename A, typename R>
9170b57cec5SDimitry Andric const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
9180b57cec5SDimitry Andric   return R::getRegisterName(regNum);
9190b57cec5SDimitry Andric }
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
9220b57cec5SDimitry Andric   return false;
9230b57cec5SDimitry Andric }
9240b57cec5SDimitry Andric 
9250b57cec5SDimitry Andric #else  // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric /// UnwindCursor contains all state (including all register values) during
9280b57cec5SDimitry Andric /// an unwind.  This is normally stack allocated inside a unw_cursor_t.
9290b57cec5SDimitry Andric template <typename A, typename R>
9300b57cec5SDimitry Andric class UnwindCursor : public AbstractUnwindCursor{
9310b57cec5SDimitry Andric   typedef typename A::pint_t pint_t;
9320b57cec5SDimitry Andric public:
9330b57cec5SDimitry Andric                       UnwindCursor(unw_context_t *context, A &as);
9340b57cec5SDimitry Andric                       UnwindCursor(A &as, void *threadArg);
9350b57cec5SDimitry Andric   virtual             ~UnwindCursor() {}
9360b57cec5SDimitry Andric   virtual bool        validReg(int);
9370b57cec5SDimitry Andric   virtual unw_word_t  getReg(int);
9380b57cec5SDimitry Andric   virtual void        setReg(int, unw_word_t);
9390b57cec5SDimitry Andric   virtual bool        validFloatReg(int);
9400b57cec5SDimitry Andric   virtual unw_fpreg_t getFloatReg(int);
9410b57cec5SDimitry Andric   virtual void        setFloatReg(int, unw_fpreg_t);
942bdd1243dSDimitry Andric   virtual int         step(bool stage2 = false);
9430b57cec5SDimitry Andric   virtual void        getInfo(unw_proc_info_t *);
9440b57cec5SDimitry Andric   virtual void        jumpto();
9450b57cec5SDimitry Andric   virtual bool        isSignalFrame();
9460b57cec5SDimitry Andric   virtual bool        getFunctionName(char *buf, size_t len, unw_word_t *off);
9470b57cec5SDimitry Andric   virtual void        setInfoBasedOnIPRegister(bool isReturnAddress = false);
9480b57cec5SDimitry Andric   virtual const char *getRegisterName(int num);
9490b57cec5SDimitry Andric #ifdef __arm__
9500b57cec5SDimitry Andric   virtual void        saveVFPAsX();
9510b57cec5SDimitry Andric #endif
9520b57cec5SDimitry Andric 
95381ad6265SDimitry Andric #ifdef _AIX
95481ad6265SDimitry Andric   virtual uintptr_t getDataRelBase();
95581ad6265SDimitry Andric #endif
95681ad6265SDimitry Andric 
957*62987288SDimitry Andric #if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
958349cc55cSDimitry Andric   virtual void *get_registers() { return &_registers; }
959349cc55cSDimitry Andric #endif
96081ad6265SDimitry Andric 
9610b57cec5SDimitry Andric   // libunwind does not and should not depend on C++ library which means that we
962bdd1243dSDimitry Andric   // need our own definition of inline placement new.
9630b57cec5SDimitry Andric   static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
9640b57cec5SDimitry Andric 
9650b57cec5SDimitry Andric private:
9660b57cec5SDimitry Andric 
9670b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
9680b57cec5SDimitry Andric   bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
9690b57cec5SDimitry Andric 
9700b57cec5SDimitry Andric   int stepWithEHABI() {
9710b57cec5SDimitry Andric     size_t len = 0;
9720b57cec5SDimitry Andric     size_t off = 0;
9730b57cec5SDimitry Andric     // FIXME: Calling decode_eht_entry() here is violating the libunwind
9740b57cec5SDimitry Andric     // abstraction layer.
9750b57cec5SDimitry Andric     const uint32_t *ehtp =
9760b57cec5SDimitry Andric         decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
9770b57cec5SDimitry Andric                          &off, &len);
9780b57cec5SDimitry Andric     if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
9790b57cec5SDimitry Andric             _URC_CONTINUE_UNWIND)
9800b57cec5SDimitry Andric       return UNW_STEP_END;
9810b57cec5SDimitry Andric     return UNW_STEP_SUCCESS;
9820b57cec5SDimitry Andric   }
9830b57cec5SDimitry Andric #endif
9840b57cec5SDimitry Andric 
98581ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
986e8d8bef9SDimitry Andric   bool setInfoForSigReturn() {
987e8d8bef9SDimitry Andric     R dummy;
988e8d8bef9SDimitry Andric     return setInfoForSigReturn(dummy);
989e8d8bef9SDimitry Andric   }
990e8d8bef9SDimitry Andric   int stepThroughSigReturn() {
991e8d8bef9SDimitry Andric     R dummy;
992e8d8bef9SDimitry Andric     return stepThroughSigReturn(dummy);
993e8d8bef9SDimitry Andric   }
9941db9f3b2SDimitry Andric   bool isReadableAddr(const pint_t addr) const;
99581ad6265SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
996e8d8bef9SDimitry Andric   bool setInfoForSigReturn(Registers_arm64 &);
997e8d8bef9SDimitry Andric   int stepThroughSigReturn(Registers_arm64 &);
99881ad6265SDimitry Andric #endif
99906c3fb27SDimitry Andric #if defined(_LIBUNWIND_TARGET_RISCV)
100006c3fb27SDimitry Andric   bool setInfoForSigReturn(Registers_riscv &);
100106c3fb27SDimitry Andric   int stepThroughSigReturn(Registers_riscv &);
100206c3fb27SDimitry Andric #endif
100381ad6265SDimitry Andric #if defined(_LIBUNWIND_TARGET_S390X)
100481ad6265SDimitry Andric   bool setInfoForSigReturn(Registers_s390x &);
100581ad6265SDimitry Andric   int stepThroughSigReturn(Registers_s390x &);
100681ad6265SDimitry Andric #endif
1007e8d8bef9SDimitry Andric   template <typename Registers> bool setInfoForSigReturn(Registers &) {
1008e8d8bef9SDimitry Andric     return false;
1009e8d8bef9SDimitry Andric   }
1010e8d8bef9SDimitry Andric   template <typename Registers> int stepThroughSigReturn(Registers &) {
1011e8d8bef9SDimitry Andric     return UNW_STEP_END;
1012e8d8bef9SDimitry Andric   }
1013e8d8bef9SDimitry Andric #endif
1014e8d8bef9SDimitry Andric 
10150b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
1016e8d8bef9SDimitry Andric   bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1017e8d8bef9SDimitry Andric                          const typename CFI_Parser<A>::CIE_Info &cieInfo,
1018e8d8bef9SDimitry Andric                          pint_t pc, uintptr_t dso_base);
10190b57cec5SDimitry Andric   bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
10200b57cec5SDimitry Andric                                             uint32_t fdeSectionOffsetHint=0);
1021bdd1243dSDimitry Andric   int stepWithDwarfFDE(bool stage2) {
1022bdd1243dSDimitry Andric     return DwarfInstructions<A, R>::stepWithDwarf(
1023bdd1243dSDimitry Andric         _addressSpace, (pint_t)this->getReg(UNW_REG_IP),
1024bdd1243dSDimitry Andric         (pint_t)_info.unwind_info, _registers, _isSignalFrame, stage2);
10250b57cec5SDimitry Andric   }
10260b57cec5SDimitry Andric #endif
10270b57cec5SDimitry Andric 
10280b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
10290b57cec5SDimitry Andric   bool getInfoFromCompactEncodingSection(pint_t pc,
10300b57cec5SDimitry Andric                                             const UnwindInfoSections &sects);
1031bdd1243dSDimitry Andric   int stepWithCompactEncoding(bool stage2 = false) {
10320b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
10330b57cec5SDimitry Andric     if ( compactSaysUseDwarf() )
1034bdd1243dSDimitry Andric       return stepWithDwarfFDE(stage2);
10350b57cec5SDimitry Andric #endif
10360b57cec5SDimitry Andric     R dummy;
10370b57cec5SDimitry Andric     return stepWithCompactEncoding(dummy);
10380b57cec5SDimitry Andric   }
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
10410b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_x86_64 &) {
10420b57cec5SDimitry Andric     return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
10430b57cec5SDimitry Andric         _info.format, _info.start_ip, _addressSpace, _registers);
10440b57cec5SDimitry Andric   }
10450b57cec5SDimitry Andric #endif
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386)
10480b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_x86 &) {
10490b57cec5SDimitry Andric     return CompactUnwinder_x86<A>::stepWithCompactEncoding(
10500b57cec5SDimitry Andric         _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
10510b57cec5SDimitry Andric   }
10520b57cec5SDimitry Andric #endif
10530b57cec5SDimitry Andric 
10540b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC)
10550b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_ppc &) {
10560b57cec5SDimitry Andric     return UNW_EINVAL;
10570b57cec5SDimitry Andric   }
10580b57cec5SDimitry Andric #endif
10590b57cec5SDimitry Andric 
10600b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64)
10610b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_ppc64 &) {
10620b57cec5SDimitry Andric     return UNW_EINVAL;
10630b57cec5SDimitry Andric   }
10640b57cec5SDimitry Andric #endif
10650b57cec5SDimitry Andric 
10660b57cec5SDimitry Andric 
10670b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
10680b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_arm64 &) {
10690b57cec5SDimitry Andric     return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
10700b57cec5SDimitry Andric         _info.format, _info.start_ip, _addressSpace, _registers);
10710b57cec5SDimitry Andric   }
10720b57cec5SDimitry Andric #endif
10730b57cec5SDimitry Andric 
10740b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_O32)
10750b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_mips_o32 &) {
10760b57cec5SDimitry Andric     return UNW_EINVAL;
10770b57cec5SDimitry Andric   }
10780b57cec5SDimitry Andric #endif
10790b57cec5SDimitry Andric 
10800b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
10810b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_mips_newabi &) {
10820b57cec5SDimitry Andric     return UNW_EINVAL;
10830b57cec5SDimitry Andric   }
10840b57cec5SDimitry Andric #endif
10850b57cec5SDimitry Andric 
1086bdd1243dSDimitry Andric #if defined(_LIBUNWIND_TARGET_LOONGARCH)
1087bdd1243dSDimitry Andric   int stepWithCompactEncoding(Registers_loongarch &) { return UNW_EINVAL; }
1088bdd1243dSDimitry Andric #endif
1089bdd1243dSDimitry Andric 
10900b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC)
10910b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
10920b57cec5SDimitry Andric #endif
10930b57cec5SDimitry Andric 
1094d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64)
1095d56accc7SDimitry Andric   int stepWithCompactEncoding(Registers_sparc64 &) { return UNW_EINVAL; }
1096d56accc7SDimitry Andric #endif
1097d56accc7SDimitry Andric 
1098480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV)
1099480093f4SDimitry Andric   int stepWithCompactEncoding(Registers_riscv &) {
1100480093f4SDimitry Andric     return UNW_EINVAL;
1101480093f4SDimitry Andric   }
1102480093f4SDimitry Andric #endif
1103480093f4SDimitry Andric 
11040b57cec5SDimitry Andric   bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
11050b57cec5SDimitry Andric     R dummy;
11060b57cec5SDimitry Andric     return compactSaysUseDwarf(dummy, offset);
11070b57cec5SDimitry Andric   }
11080b57cec5SDimitry Andric 
11090b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
11100b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
11110b57cec5SDimitry Andric     if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
11120b57cec5SDimitry Andric       if (offset)
11130b57cec5SDimitry Andric         *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
11140b57cec5SDimitry Andric       return true;
11150b57cec5SDimitry Andric     }
11160b57cec5SDimitry Andric     return false;
11170b57cec5SDimitry Andric   }
11180b57cec5SDimitry Andric #endif
11190b57cec5SDimitry Andric 
11200b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386)
11210b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
11220b57cec5SDimitry Andric     if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
11230b57cec5SDimitry Andric       if (offset)
11240b57cec5SDimitry Andric         *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
11250b57cec5SDimitry Andric       return true;
11260b57cec5SDimitry Andric     }
11270b57cec5SDimitry Andric     return false;
11280b57cec5SDimitry Andric   }
11290b57cec5SDimitry Andric #endif
11300b57cec5SDimitry Andric 
11310b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC)
11320b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
11330b57cec5SDimitry Andric     return true;
11340b57cec5SDimitry Andric   }
11350b57cec5SDimitry Andric #endif
11360b57cec5SDimitry Andric 
11370b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64)
11380b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
11390b57cec5SDimitry Andric     return true;
11400b57cec5SDimitry Andric   }
11410b57cec5SDimitry Andric #endif
11420b57cec5SDimitry Andric 
11430b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
11440b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
11450b57cec5SDimitry Andric     if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
11460b57cec5SDimitry Andric       if (offset)
11470b57cec5SDimitry Andric         *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
11480b57cec5SDimitry Andric       return true;
11490b57cec5SDimitry Andric     }
11500b57cec5SDimitry Andric     return false;
11510b57cec5SDimitry Andric   }
11520b57cec5SDimitry Andric #endif
11530b57cec5SDimitry Andric 
11540b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_O32)
11550b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
11560b57cec5SDimitry Andric     return true;
11570b57cec5SDimitry Andric   }
11580b57cec5SDimitry Andric #endif
11590b57cec5SDimitry Andric 
11600b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
11610b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
11620b57cec5SDimitry Andric     return true;
11630b57cec5SDimitry Andric   }
11640b57cec5SDimitry Andric #endif
11650b57cec5SDimitry Andric 
1166bdd1243dSDimitry Andric #if defined(_LIBUNWIND_TARGET_LOONGARCH)
1167bdd1243dSDimitry Andric   bool compactSaysUseDwarf(Registers_loongarch &, uint32_t *) const {
1168bdd1243dSDimitry Andric     return true;
1169bdd1243dSDimitry Andric   }
1170bdd1243dSDimitry Andric #endif
1171bdd1243dSDimitry Andric 
11720b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC)
11730b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
11740b57cec5SDimitry Andric #endif
11750b57cec5SDimitry Andric 
1176d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64)
1177d56accc7SDimitry Andric   bool compactSaysUseDwarf(Registers_sparc64 &, uint32_t *) const {
1178d56accc7SDimitry Andric     return true;
1179d56accc7SDimitry Andric   }
1180d56accc7SDimitry Andric #endif
1181d56accc7SDimitry Andric 
1182480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV)
1183480093f4SDimitry Andric   bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const {
1184480093f4SDimitry Andric     return true;
1185480093f4SDimitry Andric   }
1186480093f4SDimitry Andric #endif
1187480093f4SDimitry Andric 
11880b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
11910b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding() const {
11920b57cec5SDimitry Andric     R dummy;
11930b57cec5SDimitry Andric     return dwarfEncoding(dummy);
11940b57cec5SDimitry Andric   }
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
11970b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
11980b57cec5SDimitry Andric     return UNWIND_X86_64_MODE_DWARF;
11990b57cec5SDimitry Andric   }
12000b57cec5SDimitry Andric #endif
12010b57cec5SDimitry Andric 
12020b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386)
12030b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
12040b57cec5SDimitry Andric     return UNWIND_X86_MODE_DWARF;
12050b57cec5SDimitry Andric   }
12060b57cec5SDimitry Andric #endif
12070b57cec5SDimitry Andric 
12080b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC)
12090b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
12100b57cec5SDimitry Andric     return 0;
12110b57cec5SDimitry Andric   }
12120b57cec5SDimitry Andric #endif
12130b57cec5SDimitry Andric 
12140b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64)
12150b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
12160b57cec5SDimitry Andric     return 0;
12170b57cec5SDimitry Andric   }
12180b57cec5SDimitry Andric #endif
12190b57cec5SDimitry Andric 
12200b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
12210b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
12220b57cec5SDimitry Andric     return UNWIND_ARM64_MODE_DWARF;
12230b57cec5SDimitry Andric   }
12240b57cec5SDimitry Andric #endif
12250b57cec5SDimitry Andric 
12260b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
12270b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
12280b57cec5SDimitry Andric     return 0;
12290b57cec5SDimitry Andric   }
12300b57cec5SDimitry Andric #endif
12310b57cec5SDimitry Andric 
12320b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_OR1K)
12330b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
12340b57cec5SDimitry Andric     return 0;
12350b57cec5SDimitry Andric   }
12360b57cec5SDimitry Andric #endif
12370b57cec5SDimitry Andric 
12385ffd83dbSDimitry Andric #if defined (_LIBUNWIND_TARGET_HEXAGON)
12395ffd83dbSDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const {
12405ffd83dbSDimitry Andric     return 0;
12415ffd83dbSDimitry Andric   }
12425ffd83dbSDimitry Andric #endif
12435ffd83dbSDimitry Andric 
12440b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_MIPS_O32)
12450b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
12460b57cec5SDimitry Andric     return 0;
12470b57cec5SDimitry Andric   }
12480b57cec5SDimitry Andric #endif
12490b57cec5SDimitry Andric 
12500b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
12510b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
12520b57cec5SDimitry Andric     return 0;
12530b57cec5SDimitry Andric   }
12540b57cec5SDimitry Andric #endif
12550b57cec5SDimitry Andric 
1256bdd1243dSDimitry Andric #if defined(_LIBUNWIND_TARGET_LOONGARCH)
1257bdd1243dSDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_loongarch &) const {
1258bdd1243dSDimitry Andric     return 0;
1259bdd1243dSDimitry Andric   }
1260bdd1243dSDimitry Andric #endif
1261bdd1243dSDimitry Andric 
12620b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC)
12630b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
12640b57cec5SDimitry Andric #endif
12650b57cec5SDimitry Andric 
1266d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64)
1267d56accc7SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_sparc64 &) const {
1268d56accc7SDimitry Andric     return 0;
1269d56accc7SDimitry Andric   }
1270d56accc7SDimitry Andric #endif
1271d56accc7SDimitry Andric 
1272480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV)
1273480093f4SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
1274480093f4SDimitry Andric     return 0;
1275480093f4SDimitry Andric   }
1276480093f4SDimitry Andric #endif
1277480093f4SDimitry Andric 
127881ad6265SDimitry Andric #if defined (_LIBUNWIND_TARGET_S390X)
127981ad6265SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_s390x &) const {
128081ad6265SDimitry Andric     return 0;
128181ad6265SDimitry Andric   }
128281ad6265SDimitry Andric #endif
128381ad6265SDimitry Andric 
12840b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
12850b57cec5SDimitry Andric 
12860b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
12870b57cec5SDimitry Andric   // For runtime environments using SEH unwind data without Windows runtime
12880b57cec5SDimitry Andric   // support.
12890b57cec5SDimitry Andric   pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
12900b57cec5SDimitry Andric   void setLastPC(pint_t pc) { /* FIXME: Implement */ }
12910b57cec5SDimitry Andric   RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
12920b57cec5SDimitry Andric     /* FIXME: Implement */
12930b57cec5SDimitry Andric     *base = 0;
12940b57cec5SDimitry Andric     return nullptr;
12950b57cec5SDimitry Andric   }
12960b57cec5SDimitry Andric   bool getInfoFromSEH(pint_t pc);
12970b57cec5SDimitry Andric   int stepWithSEHData() { /* FIXME: Implement */ return 0; }
12980b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
12990b57cec5SDimitry Andric 
130081ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
130181ad6265SDimitry Andric   bool getInfoFromTBTable(pint_t pc, R &registers);
130281ad6265SDimitry Andric   int stepWithTBTable(pint_t pc, tbtable *TBTable, R &registers,
130381ad6265SDimitry Andric                       bool &isSignalFrame);
130481ad6265SDimitry Andric   int stepWithTBTableData() {
130581ad6265SDimitry Andric     return stepWithTBTable(reinterpret_cast<pint_t>(this->getReg(UNW_REG_IP)),
130681ad6265SDimitry Andric                            reinterpret_cast<tbtable *>(_info.unwind_info),
130781ad6265SDimitry Andric                            _registers, _isSignalFrame);
130881ad6265SDimitry Andric   }
130981ad6265SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
13100b57cec5SDimitry Andric 
13110b57cec5SDimitry Andric   A               &_addressSpace;
13120b57cec5SDimitry Andric   R                _registers;
13130b57cec5SDimitry Andric   unw_proc_info_t  _info;
13140b57cec5SDimitry Andric   bool             _unwindInfoMissing;
13150b57cec5SDimitry Andric   bool             _isSignalFrame;
131681ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
1317e8d8bef9SDimitry Andric   bool             _isSigReturn = false;
1318e8d8bef9SDimitry Andric #endif
13190b57cec5SDimitry Andric };
13200b57cec5SDimitry Andric 
13210b57cec5SDimitry Andric 
13220b57cec5SDimitry Andric template <typename A, typename R>
13230b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
13240b57cec5SDimitry Andric     : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
13250b57cec5SDimitry Andric       _isSignalFrame(false) {
13260b57cec5SDimitry Andric   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
13270b57cec5SDimitry Andric                 "UnwindCursor<> does not fit in unw_cursor_t");
1328e8d8bef9SDimitry Andric   static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1329e8d8bef9SDimitry Andric                 "UnwindCursor<> requires more alignment than unw_cursor_t");
13300b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
13310b57cec5SDimitry Andric }
13320b57cec5SDimitry Andric 
13330b57cec5SDimitry Andric template <typename A, typename R>
13340b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(A &as, void *)
13350b57cec5SDimitry Andric     : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
13360b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
13370b57cec5SDimitry Andric   // FIXME
13380b57cec5SDimitry Andric   // fill in _registers from thread arg
13390b57cec5SDimitry Andric }
13400b57cec5SDimitry Andric 
13410b57cec5SDimitry Andric 
13420b57cec5SDimitry Andric template <typename A, typename R>
13430b57cec5SDimitry Andric bool UnwindCursor<A, R>::validReg(int regNum) {
13440b57cec5SDimitry Andric   return _registers.validRegister(regNum);
13450b57cec5SDimitry Andric }
13460b57cec5SDimitry Andric 
13470b57cec5SDimitry Andric template <typename A, typename R>
13480b57cec5SDimitry Andric unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
13490b57cec5SDimitry Andric   return _registers.getRegister(regNum);
13500b57cec5SDimitry Andric }
13510b57cec5SDimitry Andric 
13520b57cec5SDimitry Andric template <typename A, typename R>
13530b57cec5SDimitry Andric void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
13540b57cec5SDimitry Andric   _registers.setRegister(regNum, (typename A::pint_t)value);
13550b57cec5SDimitry Andric }
13560b57cec5SDimitry Andric 
13570b57cec5SDimitry Andric template <typename A, typename R>
13580b57cec5SDimitry Andric bool UnwindCursor<A, R>::validFloatReg(int regNum) {
13590b57cec5SDimitry Andric   return _registers.validFloatRegister(regNum);
13600b57cec5SDimitry Andric }
13610b57cec5SDimitry Andric 
13620b57cec5SDimitry Andric template <typename A, typename R>
13630b57cec5SDimitry Andric unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
13640b57cec5SDimitry Andric   return _registers.getFloatRegister(regNum);
13650b57cec5SDimitry Andric }
13660b57cec5SDimitry Andric 
13670b57cec5SDimitry Andric template <typename A, typename R>
13680b57cec5SDimitry Andric void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
13690b57cec5SDimitry Andric   _registers.setFloatRegister(regNum, value);
13700b57cec5SDimitry Andric }
13710b57cec5SDimitry Andric 
13720b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
13730b57cec5SDimitry Andric   _registers.jumpto();
13740b57cec5SDimitry Andric }
13750b57cec5SDimitry Andric 
13760b57cec5SDimitry Andric #ifdef __arm__
13770b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
13780b57cec5SDimitry Andric   _registers.saveVFPAsX();
13790b57cec5SDimitry Andric }
13800b57cec5SDimitry Andric #endif
13810b57cec5SDimitry Andric 
138281ad6265SDimitry Andric #ifdef _AIX
138381ad6265SDimitry Andric template <typename A, typename R>
138481ad6265SDimitry Andric uintptr_t UnwindCursor<A, R>::getDataRelBase() {
138581ad6265SDimitry Andric   return reinterpret_cast<uintptr_t>(_info.extra);
138681ad6265SDimitry Andric }
138781ad6265SDimitry Andric #endif
138881ad6265SDimitry Andric 
13890b57cec5SDimitry Andric template <typename A, typename R>
13900b57cec5SDimitry Andric const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
13910b57cec5SDimitry Andric   return _registers.getRegisterName(regNum);
13920b57cec5SDimitry Andric }
13930b57cec5SDimitry Andric 
13940b57cec5SDimitry Andric template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
13950b57cec5SDimitry Andric   return _isSignalFrame;
13960b57cec5SDimitry Andric }
13970b57cec5SDimitry Andric 
13980b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
13990b57cec5SDimitry Andric 
14000b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
14010b57cec5SDimitry Andric template<typename A>
14020b57cec5SDimitry Andric struct EHABISectionIterator {
14030b57cec5SDimitry Andric   typedef EHABISectionIterator _Self;
14040b57cec5SDimitry Andric 
14050b57cec5SDimitry Andric   typedef typename A::pint_t value_type;
14060b57cec5SDimitry Andric   typedef typename A::pint_t* pointer;
14070b57cec5SDimitry Andric   typedef typename A::pint_t& reference;
14080b57cec5SDimitry Andric   typedef size_t size_type;
14090b57cec5SDimitry Andric   typedef size_t difference_type;
14100b57cec5SDimitry Andric 
14110b57cec5SDimitry Andric   static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
14120b57cec5SDimitry Andric     return _Self(addressSpace, sects, 0);
14130b57cec5SDimitry Andric   }
14140b57cec5SDimitry Andric   static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
14150b57cec5SDimitry Andric     return _Self(addressSpace, sects,
14160b57cec5SDimitry Andric                  sects.arm_section_length / sizeof(EHABIIndexEntry));
14170b57cec5SDimitry Andric   }
14180b57cec5SDimitry Andric 
14190b57cec5SDimitry Andric   EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
14200b57cec5SDimitry Andric       : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
14210b57cec5SDimitry Andric 
14220b57cec5SDimitry Andric   _Self& operator++() { ++_i; return *this; }
14230b57cec5SDimitry Andric   _Self& operator+=(size_t a) { _i += a; return *this; }
14240b57cec5SDimitry Andric   _Self& operator--() { assert(_i > 0); --_i; return *this; }
14250b57cec5SDimitry Andric   _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
14260b57cec5SDimitry Andric 
14270b57cec5SDimitry Andric   _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
14280b57cec5SDimitry Andric   _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
14290b57cec5SDimitry Andric 
14305ffd83dbSDimitry Andric   size_t operator-(const _Self& other) const { return _i - other._i; }
14310b57cec5SDimitry Andric 
14320b57cec5SDimitry Andric   bool operator==(const _Self& other) const {
14330b57cec5SDimitry Andric     assert(_addressSpace == other._addressSpace);
14340b57cec5SDimitry Andric     assert(_sects == other._sects);
14350b57cec5SDimitry Andric     return _i == other._i;
14360b57cec5SDimitry Andric   }
14370b57cec5SDimitry Andric 
14385ffd83dbSDimitry Andric   bool operator!=(const _Self& other) const {
14395ffd83dbSDimitry Andric     assert(_addressSpace == other._addressSpace);
14405ffd83dbSDimitry Andric     assert(_sects == other._sects);
14415ffd83dbSDimitry Andric     return _i != other._i;
14425ffd83dbSDimitry Andric   }
14435ffd83dbSDimitry Andric 
14440b57cec5SDimitry Andric   typename A::pint_t operator*() const { return functionAddress(); }
14450b57cec5SDimitry Andric 
14460b57cec5SDimitry Andric   typename A::pint_t functionAddress() const {
14470b57cec5SDimitry Andric     typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
14480b57cec5SDimitry Andric         EHABIIndexEntry, _i, functionOffset);
14490b57cec5SDimitry Andric     return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
14500b57cec5SDimitry Andric   }
14510b57cec5SDimitry Andric 
14520b57cec5SDimitry Andric   typename A::pint_t dataAddress() {
14530b57cec5SDimitry Andric     typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
14540b57cec5SDimitry Andric         EHABIIndexEntry, _i, data);
14550b57cec5SDimitry Andric     return indexAddr;
14560b57cec5SDimitry Andric   }
14570b57cec5SDimitry Andric 
14580b57cec5SDimitry Andric  private:
14590b57cec5SDimitry Andric   size_t _i;
14600b57cec5SDimitry Andric   A* _addressSpace;
14610b57cec5SDimitry Andric   const UnwindInfoSections* _sects;
14620b57cec5SDimitry Andric };
14630b57cec5SDimitry Andric 
14640b57cec5SDimitry Andric namespace {
14650b57cec5SDimitry Andric 
14660b57cec5SDimitry Andric template <typename A>
14670b57cec5SDimitry Andric EHABISectionIterator<A> EHABISectionUpperBound(
14680b57cec5SDimitry Andric     EHABISectionIterator<A> first,
14690b57cec5SDimitry Andric     EHABISectionIterator<A> last,
14700b57cec5SDimitry Andric     typename A::pint_t value) {
14710b57cec5SDimitry Andric   size_t len = last - first;
14720b57cec5SDimitry Andric   while (len > 0) {
14730b57cec5SDimitry Andric     size_t l2 = len / 2;
14740b57cec5SDimitry Andric     EHABISectionIterator<A> m = first + l2;
14750b57cec5SDimitry Andric     if (value < *m) {
14760b57cec5SDimitry Andric         len = l2;
14770b57cec5SDimitry Andric     } else {
14780b57cec5SDimitry Andric         first = ++m;
14790b57cec5SDimitry Andric         len -= l2 + 1;
14800b57cec5SDimitry Andric     }
14810b57cec5SDimitry Andric   }
14820b57cec5SDimitry Andric   return first;
14830b57cec5SDimitry Andric }
14840b57cec5SDimitry Andric 
14850b57cec5SDimitry Andric }
14860b57cec5SDimitry Andric 
14870b57cec5SDimitry Andric template <typename A, typename R>
14880b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromEHABISection(
14890b57cec5SDimitry Andric     pint_t pc,
14900b57cec5SDimitry Andric     const UnwindInfoSections &sects) {
14910b57cec5SDimitry Andric   EHABISectionIterator<A> begin =
14920b57cec5SDimitry Andric       EHABISectionIterator<A>::begin(_addressSpace, sects);
14930b57cec5SDimitry Andric   EHABISectionIterator<A> end =
14940b57cec5SDimitry Andric       EHABISectionIterator<A>::end(_addressSpace, sects);
14950b57cec5SDimitry Andric   if (begin == end)
14960b57cec5SDimitry Andric     return false;
14970b57cec5SDimitry Andric 
14980b57cec5SDimitry Andric   EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
14990b57cec5SDimitry Andric   if (itNextPC == begin)
15000b57cec5SDimitry Andric     return false;
15010b57cec5SDimitry Andric   EHABISectionIterator<A> itThisPC = itNextPC - 1;
15020b57cec5SDimitry Andric 
15030b57cec5SDimitry Andric   pint_t thisPC = itThisPC.functionAddress();
15040b57cec5SDimitry Andric   // If an exception is thrown from a function, corresponding to the last entry
15050b57cec5SDimitry Andric   // in the table, we don't really know the function extent and have to choose a
15060b57cec5SDimitry Andric   // value for nextPC. Choosing max() will allow the range check during trace to
15070b57cec5SDimitry Andric   // succeed.
15080b57cec5SDimitry Andric   pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
15090b57cec5SDimitry Andric   pint_t indexDataAddr = itThisPC.dataAddress();
15100b57cec5SDimitry Andric 
15110b57cec5SDimitry Andric   if (indexDataAddr == 0)
15120b57cec5SDimitry Andric     return false;
15130b57cec5SDimitry Andric 
15140b57cec5SDimitry Andric   uint32_t indexData = _addressSpace.get32(indexDataAddr);
15150b57cec5SDimitry Andric   if (indexData == UNW_EXIDX_CANTUNWIND)
15160b57cec5SDimitry Andric     return false;
15170b57cec5SDimitry Andric 
15180b57cec5SDimitry Andric   // If the high bit is set, the exception handling table entry is inline inside
15190b57cec5SDimitry Andric   // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
1520473b61d3SDimitry Andric   // the table points at an offset in the exception handling table (section 5
1521473b61d3SDimitry Andric   // EHABI).
15220b57cec5SDimitry Andric   pint_t exceptionTableAddr;
15230b57cec5SDimitry Andric   uint32_t exceptionTableData;
15240b57cec5SDimitry Andric   bool isSingleWordEHT;
15250b57cec5SDimitry Andric   if (indexData & 0x80000000) {
15260b57cec5SDimitry Andric     exceptionTableAddr = indexDataAddr;
15270b57cec5SDimitry Andric     // TODO(ajwong): Should this data be 0?
15280b57cec5SDimitry Andric     exceptionTableData = indexData;
15290b57cec5SDimitry Andric     isSingleWordEHT = true;
15300b57cec5SDimitry Andric   } else {
15310b57cec5SDimitry Andric     exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
15320b57cec5SDimitry Andric     exceptionTableData = _addressSpace.get32(exceptionTableAddr);
15330b57cec5SDimitry Andric     isSingleWordEHT = false;
15340b57cec5SDimitry Andric   }
15350b57cec5SDimitry Andric 
15360b57cec5SDimitry Andric   // Now we know the 3 things:
15370b57cec5SDimitry Andric   //   exceptionTableAddr -- exception handler table entry.
15380b57cec5SDimitry Andric   //   exceptionTableData -- the data inside the first word of the eht entry.
15390b57cec5SDimitry Andric   //   isSingleWordEHT -- whether the entry is in the index.
15400b57cec5SDimitry Andric   unw_word_t personalityRoutine = 0xbadf00d;
15410b57cec5SDimitry Andric   bool scope32 = false;
15420b57cec5SDimitry Andric   uintptr_t lsda;
15430b57cec5SDimitry Andric 
15440b57cec5SDimitry Andric   // If the high bit in the exception handling table entry is set, the entry is
15450b57cec5SDimitry Andric   // in compact form (section 6.3 EHABI).
15460b57cec5SDimitry Andric   if (exceptionTableData & 0x80000000) {
15470b57cec5SDimitry Andric     // Grab the index of the personality routine from the compact form.
15480b57cec5SDimitry Andric     uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
15490b57cec5SDimitry Andric     uint32_t extraWords = 0;
15500b57cec5SDimitry Andric     switch (choice) {
15510b57cec5SDimitry Andric       case 0:
15520b57cec5SDimitry Andric         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
15530b57cec5SDimitry Andric         extraWords = 0;
15540b57cec5SDimitry Andric         scope32 = false;
15550b57cec5SDimitry Andric         lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
15560b57cec5SDimitry Andric         break;
15570b57cec5SDimitry Andric       case 1:
15580b57cec5SDimitry Andric         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
15590b57cec5SDimitry Andric         extraWords = (exceptionTableData & 0x00ff0000) >> 16;
15600b57cec5SDimitry Andric         scope32 = false;
15610b57cec5SDimitry Andric         lsda = exceptionTableAddr + (extraWords + 1) * 4;
15620b57cec5SDimitry Andric         break;
15630b57cec5SDimitry Andric       case 2:
15640b57cec5SDimitry Andric         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
15650b57cec5SDimitry Andric         extraWords = (exceptionTableData & 0x00ff0000) >> 16;
15660b57cec5SDimitry Andric         scope32 = true;
15670b57cec5SDimitry Andric         lsda = exceptionTableAddr + (extraWords + 1) * 4;
15680b57cec5SDimitry Andric         break;
15690b57cec5SDimitry Andric       default:
15700b57cec5SDimitry Andric         _LIBUNWIND_ABORT("unknown personality routine");
15710b57cec5SDimitry Andric         return false;
15720b57cec5SDimitry Andric     }
15730b57cec5SDimitry Andric 
15740b57cec5SDimitry Andric     if (isSingleWordEHT) {
15750b57cec5SDimitry Andric       if (extraWords != 0) {
15760b57cec5SDimitry Andric         _LIBUNWIND_ABORT("index inlined table detected but pr function "
15770b57cec5SDimitry Andric                          "requires extra words");
15780b57cec5SDimitry Andric         return false;
15790b57cec5SDimitry Andric       }
15800b57cec5SDimitry Andric     }
15810b57cec5SDimitry Andric   } else {
15820b57cec5SDimitry Andric     pint_t personalityAddr =
15830b57cec5SDimitry Andric         exceptionTableAddr + signExtendPrel31(exceptionTableData);
15840b57cec5SDimitry Andric     personalityRoutine = personalityAddr;
15850b57cec5SDimitry Andric 
15860b57cec5SDimitry Andric     // ARM EHABI # 6.2, # 9.2
15870b57cec5SDimitry Andric     //
15880b57cec5SDimitry Andric     //  +---- ehtp
15890b57cec5SDimitry Andric     //  v
15900b57cec5SDimitry Andric     // +--------------------------------------+
15910b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15920b57cec5SDimitry Andric     // | |0| prel31 to personalityRoutine   | |
15930b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15940b57cec5SDimitry Andric     // | |      N |      unwind opcodes     | |  <-- UnwindData
15950b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15960b57cec5SDimitry Andric     // | | Word 2        unwind opcodes     | |
15970b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15980b57cec5SDimitry Andric     // | ...                                  |
15990b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
16000b57cec5SDimitry Andric     // | | Word N        unwind opcodes     | |
16010b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
16020b57cec5SDimitry Andric     // | | LSDA                             | |  <-- lsda
16030b57cec5SDimitry Andric     // | | ...                              | |
16040b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
16050b57cec5SDimitry Andric     // +--------------------------------------+
16060b57cec5SDimitry Andric 
16070b57cec5SDimitry Andric     uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
16080b57cec5SDimitry Andric     uint32_t FirstDataWord = *UnwindData;
16090b57cec5SDimitry Andric     size_t N = ((FirstDataWord >> 24) & 0xff);
16100b57cec5SDimitry Andric     size_t NDataWords = N + 1;
16110b57cec5SDimitry Andric     lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
16120b57cec5SDimitry Andric   }
16130b57cec5SDimitry Andric 
16140b57cec5SDimitry Andric   _info.start_ip = thisPC;
16150b57cec5SDimitry Andric   _info.end_ip = nextPC;
16160b57cec5SDimitry Andric   _info.handler = personalityRoutine;
16170b57cec5SDimitry Andric   _info.unwind_info = exceptionTableAddr;
16180b57cec5SDimitry Andric   _info.lsda = lsda;
16190b57cec5SDimitry Andric   // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
1620473b61d3SDimitry Andric   _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0);  // Use enum?
16210b57cec5SDimitry Andric 
16220b57cec5SDimitry Andric   return true;
16230b57cec5SDimitry Andric }
16240b57cec5SDimitry Andric #endif
16250b57cec5SDimitry Andric 
16260b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
16270b57cec5SDimitry Andric template <typename A, typename R>
1628e8d8bef9SDimitry Andric bool UnwindCursor<A, R>::getInfoFromFdeCie(
1629e8d8bef9SDimitry Andric     const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1630e8d8bef9SDimitry Andric     const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc,
1631e8d8bef9SDimitry Andric     uintptr_t dso_base) {
1632e8d8bef9SDimitry Andric   typename CFI_Parser<A>::PrologInfo prolog;
1633e8d8bef9SDimitry Andric   if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1634e8d8bef9SDimitry Andric                                           R::getArch(), &prolog)) {
1635e8d8bef9SDimitry Andric     // Save off parsed FDE info
1636e8d8bef9SDimitry Andric     _info.start_ip          = fdeInfo.pcStart;
1637e8d8bef9SDimitry Andric     _info.end_ip            = fdeInfo.pcEnd;
1638e8d8bef9SDimitry Andric     _info.lsda              = fdeInfo.lsda;
1639e8d8bef9SDimitry Andric     _info.handler           = cieInfo.personality;
1640e8d8bef9SDimitry Andric     // Some frameless functions need SP altered when resuming in function, so
1641e8d8bef9SDimitry Andric     // propagate spExtraArgSize.
1642e8d8bef9SDimitry Andric     _info.gp                = prolog.spExtraArgSize;
1643e8d8bef9SDimitry Andric     _info.flags             = 0;
1644e8d8bef9SDimitry Andric     _info.format            = dwarfEncoding();
1645e8d8bef9SDimitry Andric     _info.unwind_info       = fdeInfo.fdeStart;
1646e8d8bef9SDimitry Andric     _info.unwind_info_size  = static_cast<uint32_t>(fdeInfo.fdeLength);
1647e8d8bef9SDimitry Andric     _info.extra             = static_cast<unw_word_t>(dso_base);
1648e8d8bef9SDimitry Andric     return true;
1649e8d8bef9SDimitry Andric   }
1650e8d8bef9SDimitry Andric   return false;
1651e8d8bef9SDimitry Andric }
1652e8d8bef9SDimitry Andric 
1653e8d8bef9SDimitry Andric template <typename A, typename R>
16540b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
16550b57cec5SDimitry Andric                                                 const UnwindInfoSections &sects,
16560b57cec5SDimitry Andric                                                 uint32_t fdeSectionOffsetHint) {
16570b57cec5SDimitry Andric   typename CFI_Parser<A>::FDE_Info fdeInfo;
16580b57cec5SDimitry Andric   typename CFI_Parser<A>::CIE_Info cieInfo;
16590b57cec5SDimitry Andric   bool foundFDE = false;
16600b57cec5SDimitry Andric   bool foundInCache = false;
16610b57cec5SDimitry Andric   // If compact encoding table gave offset into dwarf section, go directly there
16620b57cec5SDimitry Andric   if (fdeSectionOffsetHint != 0) {
16630b57cec5SDimitry Andric     foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1664e8d8bef9SDimitry Andric                                     sects.dwarf_section_length,
16650b57cec5SDimitry Andric                                     sects.dwarf_section + fdeSectionOffsetHint,
16660b57cec5SDimitry Andric                                     &fdeInfo, &cieInfo);
16670b57cec5SDimitry Andric   }
16680b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
16690b57cec5SDimitry Andric   if (!foundFDE && (sects.dwarf_index_section != 0)) {
16700b57cec5SDimitry Andric     foundFDE = EHHeaderParser<A>::findFDE(
16710b57cec5SDimitry Andric         _addressSpace, pc, sects.dwarf_index_section,
16720b57cec5SDimitry Andric         (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
16730b57cec5SDimitry Andric   }
16740b57cec5SDimitry Andric #endif
16750b57cec5SDimitry Andric   if (!foundFDE) {
16760b57cec5SDimitry Andric     // otherwise, search cache of previously found FDEs.
16770b57cec5SDimitry Andric     pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
16780b57cec5SDimitry Andric     if (cachedFDE != 0) {
16790b57cec5SDimitry Andric       foundFDE =
16800b57cec5SDimitry Andric           CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1681e8d8bef9SDimitry Andric                                  sects.dwarf_section_length,
16820b57cec5SDimitry Andric                                  cachedFDE, &fdeInfo, &cieInfo);
16830b57cec5SDimitry Andric       foundInCache = foundFDE;
16840b57cec5SDimitry Andric     }
16850b57cec5SDimitry Andric   }
16860b57cec5SDimitry Andric   if (!foundFDE) {
16870b57cec5SDimitry Andric     // Still not found, do full scan of __eh_frame section.
16880b57cec5SDimitry Andric     foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1689e8d8bef9SDimitry Andric                                       sects.dwarf_section_length, 0,
16900b57cec5SDimitry Andric                                       &fdeInfo, &cieInfo);
16910b57cec5SDimitry Andric   }
16920b57cec5SDimitry Andric   if (foundFDE) {
1693e8d8bef9SDimitry Andric     if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) {
16940b57cec5SDimitry Andric       // Add to cache (to make next lookup faster) if we had no hint
16950b57cec5SDimitry Andric       // and there was no index.
16960b57cec5SDimitry Andric       if (!foundInCache && (fdeSectionOffsetHint == 0)) {
16970b57cec5SDimitry Andric   #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
16980b57cec5SDimitry Andric         if (sects.dwarf_index_section == 0)
16990b57cec5SDimitry Andric   #endif
17000b57cec5SDimitry Andric         DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
17010b57cec5SDimitry Andric                               fdeInfo.fdeStart);
17020b57cec5SDimitry Andric       }
17030b57cec5SDimitry Andric       return true;
17040b57cec5SDimitry Andric     }
17050b57cec5SDimitry Andric   }
17060b57cec5SDimitry Andric   //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
17070b57cec5SDimitry Andric   return false;
17080b57cec5SDimitry Andric }
17090b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
17100b57cec5SDimitry Andric 
17110b57cec5SDimitry Andric 
17120b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
17130b57cec5SDimitry Andric template <typename A, typename R>
17140b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
17150b57cec5SDimitry Andric                                               const UnwindInfoSections &sects) {
17160b57cec5SDimitry Andric   const bool log = false;
17170b57cec5SDimitry Andric   if (log)
17180b57cec5SDimitry Andric     fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
17190b57cec5SDimitry Andric             (uint64_t)pc, (uint64_t)sects.dso_base);
17200b57cec5SDimitry Andric 
17210b57cec5SDimitry Andric   const UnwindSectionHeader<A> sectionHeader(_addressSpace,
17220b57cec5SDimitry Andric                                                 sects.compact_unwind_section);
17230b57cec5SDimitry Andric   if (sectionHeader.version() != UNWIND_SECTION_VERSION)
17240b57cec5SDimitry Andric     return false;
17250b57cec5SDimitry Andric 
17260b57cec5SDimitry Andric   // do a binary search of top level index to find page with unwind info
17270b57cec5SDimitry Andric   pint_t targetFunctionOffset = pc - sects.dso_base;
17280b57cec5SDimitry Andric   const UnwindSectionIndexArray<A> topIndex(_addressSpace,
17290b57cec5SDimitry Andric                                            sects.compact_unwind_section
17300b57cec5SDimitry Andric                                          + sectionHeader.indexSectionOffset());
17310b57cec5SDimitry Andric   uint32_t low = 0;
17320b57cec5SDimitry Andric   uint32_t high = sectionHeader.indexCount();
17330b57cec5SDimitry Andric   uint32_t last = high - 1;
17340b57cec5SDimitry Andric   while (low < high) {
17350b57cec5SDimitry Andric     uint32_t mid = (low + high) / 2;
17360b57cec5SDimitry Andric     //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
17370b57cec5SDimitry Andric     //mid, low, high, topIndex.functionOffset(mid));
17380b57cec5SDimitry Andric     if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
17390b57cec5SDimitry Andric       if ((mid == last) ||
17400b57cec5SDimitry Andric           (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
17410b57cec5SDimitry Andric         low = mid;
17420b57cec5SDimitry Andric         break;
17430b57cec5SDimitry Andric       } else {
17440b57cec5SDimitry Andric         low = mid + 1;
17450b57cec5SDimitry Andric       }
17460b57cec5SDimitry Andric     } else {
17470b57cec5SDimitry Andric       high = mid;
17480b57cec5SDimitry Andric     }
17490b57cec5SDimitry Andric   }
17500b57cec5SDimitry Andric   const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
17510b57cec5SDimitry Andric   const uint32_t firstLevelNextPageFunctionOffset =
17520b57cec5SDimitry Andric       topIndex.functionOffset(low + 1);
17530b57cec5SDimitry Andric   const pint_t secondLevelAddr =
17540b57cec5SDimitry Andric       sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
17550b57cec5SDimitry Andric   const pint_t lsdaArrayStartAddr =
17560b57cec5SDimitry Andric       sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
17570b57cec5SDimitry Andric   const pint_t lsdaArrayEndAddr =
17580b57cec5SDimitry Andric       sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
17590b57cec5SDimitry Andric   if (log)
17600b57cec5SDimitry Andric     fprintf(stderr, "\tfirst level search for result index=%d "
17610b57cec5SDimitry Andric                     "to secondLevelAddr=0x%llX\n",
17620b57cec5SDimitry Andric                     low, (uint64_t) secondLevelAddr);
17630b57cec5SDimitry Andric   // do a binary search of second level page index
17640b57cec5SDimitry Andric   uint32_t encoding = 0;
17650b57cec5SDimitry Andric   pint_t funcStart = 0;
17660b57cec5SDimitry Andric   pint_t funcEnd = 0;
17670b57cec5SDimitry Andric   pint_t lsda = 0;
17680b57cec5SDimitry Andric   pint_t personality = 0;
17690b57cec5SDimitry Andric   uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
17700b57cec5SDimitry Andric   if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
17710b57cec5SDimitry Andric     // regular page
17720b57cec5SDimitry Andric     UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
17730b57cec5SDimitry Andric                                                  secondLevelAddr);
17740b57cec5SDimitry Andric     UnwindSectionRegularArray<A> pageIndex(
17750b57cec5SDimitry Andric         _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
17760b57cec5SDimitry Andric     // binary search looks for entry with e where index[e].offset <= pc <
17770b57cec5SDimitry Andric     // index[e+1].offset
17780b57cec5SDimitry Andric     if (log)
17790b57cec5SDimitry Andric       fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
17800b57cec5SDimitry Andric                       "regular page starting at secondLevelAddr=0x%llX\n",
17810b57cec5SDimitry Andric               (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
17820b57cec5SDimitry Andric     low = 0;
17830b57cec5SDimitry Andric     high = pageHeader.entryCount();
17840b57cec5SDimitry Andric     while (low < high) {
17850b57cec5SDimitry Andric       uint32_t mid = (low + high) / 2;
17860b57cec5SDimitry Andric       if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
17870b57cec5SDimitry Andric         if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
17880b57cec5SDimitry Andric           // at end of table
17890b57cec5SDimitry Andric           low = mid;
17900b57cec5SDimitry Andric           funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
17910b57cec5SDimitry Andric           break;
17920b57cec5SDimitry Andric         } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
17930b57cec5SDimitry Andric           // next is too big, so we found it
17940b57cec5SDimitry Andric           low = mid;
17950b57cec5SDimitry Andric           funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
17960b57cec5SDimitry Andric           break;
17970b57cec5SDimitry Andric         } else {
17980b57cec5SDimitry Andric           low = mid + 1;
17990b57cec5SDimitry Andric         }
18000b57cec5SDimitry Andric       } else {
18010b57cec5SDimitry Andric         high = mid;
18020b57cec5SDimitry Andric       }
18030b57cec5SDimitry Andric     }
18040b57cec5SDimitry Andric     encoding = pageIndex.encoding(low);
18050b57cec5SDimitry Andric     funcStart = pageIndex.functionOffset(low) + sects.dso_base;
18060b57cec5SDimitry Andric     if (pc < funcStart) {
18070b57cec5SDimitry Andric       if (log)
18080b57cec5SDimitry Andric         fprintf(
18090b57cec5SDimitry Andric             stderr,
18100b57cec5SDimitry Andric             "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
18110b57cec5SDimitry Andric             (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
18120b57cec5SDimitry Andric       return false;
18130b57cec5SDimitry Andric     }
18140b57cec5SDimitry Andric     if (pc > funcEnd) {
18150b57cec5SDimitry Andric       if (log)
18160b57cec5SDimitry Andric         fprintf(
18170b57cec5SDimitry Andric             stderr,
18180b57cec5SDimitry Andric             "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
18190b57cec5SDimitry Andric             (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
18200b57cec5SDimitry Andric       return false;
18210b57cec5SDimitry Andric     }
18220b57cec5SDimitry Andric   } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
18230b57cec5SDimitry Andric     // compressed page
18240b57cec5SDimitry Andric     UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
18250b57cec5SDimitry Andric                                                     secondLevelAddr);
18260b57cec5SDimitry Andric     UnwindSectionCompressedArray<A> pageIndex(
18270b57cec5SDimitry Andric         _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
18280b57cec5SDimitry Andric     const uint32_t targetFunctionPageOffset =
18290b57cec5SDimitry Andric         (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
18300b57cec5SDimitry Andric     // binary search looks for entry with e where index[e].offset <= pc <
18310b57cec5SDimitry Andric     // index[e+1].offset
18320b57cec5SDimitry Andric     if (log)
18330b57cec5SDimitry Andric       fprintf(stderr, "\tbinary search of compressed page starting at "
18340b57cec5SDimitry Andric                       "secondLevelAddr=0x%llX\n",
18350b57cec5SDimitry Andric               (uint64_t) secondLevelAddr);
18360b57cec5SDimitry Andric     low = 0;
18370b57cec5SDimitry Andric     last = pageHeader.entryCount() - 1;
18380b57cec5SDimitry Andric     high = pageHeader.entryCount();
18390b57cec5SDimitry Andric     while (low < high) {
18400b57cec5SDimitry Andric       uint32_t mid = (low + high) / 2;
18410b57cec5SDimitry Andric       if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
18420b57cec5SDimitry Andric         if ((mid == last) ||
18430b57cec5SDimitry Andric             (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
18440b57cec5SDimitry Andric           low = mid;
18450b57cec5SDimitry Andric           break;
18460b57cec5SDimitry Andric         } else {
18470b57cec5SDimitry Andric           low = mid + 1;
18480b57cec5SDimitry Andric         }
18490b57cec5SDimitry Andric       } else {
18500b57cec5SDimitry Andric         high = mid;
18510b57cec5SDimitry Andric       }
18520b57cec5SDimitry Andric     }
18530b57cec5SDimitry Andric     funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
18540b57cec5SDimitry Andric                                                               + sects.dso_base;
18550b57cec5SDimitry Andric     if (low < last)
18560b57cec5SDimitry Andric       funcEnd =
18570b57cec5SDimitry Andric           pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
18580b57cec5SDimitry Andric                                                               + sects.dso_base;
18590b57cec5SDimitry Andric     else
18600b57cec5SDimitry Andric       funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
18610b57cec5SDimitry Andric     if (pc < funcStart) {
1862fe6060f1SDimitry Andric       _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1863fe6060f1SDimitry Andric                            "not in second level compressed unwind table. "
1864fe6060f1SDimitry Andric                            "funcStart=0x%llX",
18650b57cec5SDimitry Andric                             (uint64_t) pc, (uint64_t) funcStart);
18660b57cec5SDimitry Andric       return false;
18670b57cec5SDimitry Andric     }
18680b57cec5SDimitry Andric     if (pc > funcEnd) {
1869fe6060f1SDimitry Andric       _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1870fe6060f1SDimitry Andric                            "not in second level compressed unwind table. "
1871fe6060f1SDimitry Andric                            "funcEnd=0x%llX",
18720b57cec5SDimitry Andric                            (uint64_t) pc, (uint64_t) funcEnd);
18730b57cec5SDimitry Andric       return false;
18740b57cec5SDimitry Andric     }
18750b57cec5SDimitry Andric     uint16_t encodingIndex = pageIndex.encodingIndex(low);
18760b57cec5SDimitry Andric     if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
18770b57cec5SDimitry Andric       // encoding is in common table in section header
18780b57cec5SDimitry Andric       encoding = _addressSpace.get32(
18790b57cec5SDimitry Andric           sects.compact_unwind_section +
18800b57cec5SDimitry Andric           sectionHeader.commonEncodingsArraySectionOffset() +
18810b57cec5SDimitry Andric           encodingIndex * sizeof(uint32_t));
18820b57cec5SDimitry Andric     } else {
18830b57cec5SDimitry Andric       // encoding is in page specific table
18840b57cec5SDimitry Andric       uint16_t pageEncodingIndex =
18850b57cec5SDimitry Andric           encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
18860b57cec5SDimitry Andric       encoding = _addressSpace.get32(secondLevelAddr +
18870b57cec5SDimitry Andric                                      pageHeader.encodingsPageOffset() +
18880b57cec5SDimitry Andric                                      pageEncodingIndex * sizeof(uint32_t));
18890b57cec5SDimitry Andric     }
18900b57cec5SDimitry Andric   } else {
1891fe6060f1SDimitry Andric     _LIBUNWIND_DEBUG_LOG(
1892fe6060f1SDimitry Andric         "malformed __unwind_info at 0x%0llX bad second level page",
18930b57cec5SDimitry Andric         (uint64_t)sects.compact_unwind_section);
18940b57cec5SDimitry Andric     return false;
18950b57cec5SDimitry Andric   }
18960b57cec5SDimitry Andric 
18970b57cec5SDimitry Andric   // look up LSDA, if encoding says function has one
18980b57cec5SDimitry Andric   if (encoding & UNWIND_HAS_LSDA) {
18990b57cec5SDimitry Andric     UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
19000b57cec5SDimitry Andric     uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
19010b57cec5SDimitry Andric     low = 0;
19020b57cec5SDimitry Andric     high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
19030b57cec5SDimitry Andric                     sizeof(unwind_info_section_header_lsda_index_entry);
19040b57cec5SDimitry Andric     // binary search looks for entry with exact match for functionOffset
19050b57cec5SDimitry Andric     if (log)
19060b57cec5SDimitry Andric       fprintf(stderr,
19070b57cec5SDimitry Andric               "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
19080b57cec5SDimitry Andric               funcStartOffset);
19090b57cec5SDimitry Andric     while (low < high) {
19100b57cec5SDimitry Andric       uint32_t mid = (low + high) / 2;
19110b57cec5SDimitry Andric       if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
19120b57cec5SDimitry Andric         lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
19130b57cec5SDimitry Andric         break;
19140b57cec5SDimitry Andric       } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
19150b57cec5SDimitry Andric         low = mid + 1;
19160b57cec5SDimitry Andric       } else {
19170b57cec5SDimitry Andric         high = mid;
19180b57cec5SDimitry Andric       }
19190b57cec5SDimitry Andric     }
19200b57cec5SDimitry Andric     if (lsda == 0) {
19210b57cec5SDimitry Andric       _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
19220b57cec5SDimitry Andric                     "pc=0x%0llX, but lsda table has no entry",
19230b57cec5SDimitry Andric                     encoding, (uint64_t) pc);
19240b57cec5SDimitry Andric       return false;
19250b57cec5SDimitry Andric     }
19260b57cec5SDimitry Andric   }
19270b57cec5SDimitry Andric 
1928e8d8bef9SDimitry Andric   // extract personality routine, if encoding says function has one
19290b57cec5SDimitry Andric   uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
19300b57cec5SDimitry Andric                               (__builtin_ctz(UNWIND_PERSONALITY_MASK));
19310b57cec5SDimitry Andric   if (personalityIndex != 0) {
19320b57cec5SDimitry Andric     --personalityIndex; // change 1-based to zero-based index
1933e8d8bef9SDimitry Andric     if (personalityIndex >= sectionHeader.personalityArrayCount()) {
19340b57cec5SDimitry Andric       _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d,  "
19350b57cec5SDimitry Andric                             "but personality table has only %d entries",
19360b57cec5SDimitry Andric                             encoding, personalityIndex,
19370b57cec5SDimitry Andric                             sectionHeader.personalityArrayCount());
19380b57cec5SDimitry Andric       return false;
19390b57cec5SDimitry Andric     }
19400b57cec5SDimitry Andric     int32_t personalityDelta = (int32_t)_addressSpace.get32(
19410b57cec5SDimitry Andric         sects.compact_unwind_section +
19420b57cec5SDimitry Andric         sectionHeader.personalityArraySectionOffset() +
19430b57cec5SDimitry Andric         personalityIndex * sizeof(uint32_t));
19440b57cec5SDimitry Andric     pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
19450b57cec5SDimitry Andric     personality = _addressSpace.getP(personalityPointer);
19460b57cec5SDimitry Andric     if (log)
19470b57cec5SDimitry Andric       fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
19480b57cec5SDimitry Andric                       "personalityDelta=0x%08X, personality=0x%08llX\n",
19490b57cec5SDimitry Andric               (uint64_t) pc, personalityDelta, (uint64_t) personality);
19500b57cec5SDimitry Andric   }
19510b57cec5SDimitry Andric 
19520b57cec5SDimitry Andric   if (log)
19530b57cec5SDimitry Andric     fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
19540b57cec5SDimitry Andric                     "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
19550b57cec5SDimitry Andric             (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
19560b57cec5SDimitry Andric   _info.start_ip = funcStart;
19570b57cec5SDimitry Andric   _info.end_ip = funcEnd;
19580b57cec5SDimitry Andric   _info.lsda = lsda;
19590b57cec5SDimitry Andric   _info.handler = personality;
19600b57cec5SDimitry Andric   _info.gp = 0;
19610b57cec5SDimitry Andric   _info.flags = 0;
19620b57cec5SDimitry Andric   _info.format = encoding;
19630b57cec5SDimitry Andric   _info.unwind_info = 0;
19640b57cec5SDimitry Andric   _info.unwind_info_size = 0;
19650b57cec5SDimitry Andric   _info.extra = sects.dso_base;
19660b57cec5SDimitry Andric   return true;
19670b57cec5SDimitry Andric }
19680b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
19690b57cec5SDimitry Andric 
19700b57cec5SDimitry Andric 
19710b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
19720b57cec5SDimitry Andric template <typename A, typename R>
19730b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
19740b57cec5SDimitry Andric   pint_t base;
19750b57cec5SDimitry Andric   RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
19760b57cec5SDimitry Andric   if (!unwindEntry) {
19770b57cec5SDimitry Andric     _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
19780b57cec5SDimitry Andric     return false;
19790b57cec5SDimitry Andric   }
19800b57cec5SDimitry Andric   _info.gp = 0;
19810b57cec5SDimitry Andric   _info.flags = 0;
19820b57cec5SDimitry Andric   _info.format = 0;
19830b57cec5SDimitry Andric   _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
19840b57cec5SDimitry Andric   _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
19850b57cec5SDimitry Andric   _info.extra = base;
19860b57cec5SDimitry Andric   _info.start_ip = base + unwindEntry->BeginAddress;
19870b57cec5SDimitry Andric #ifdef _LIBUNWIND_TARGET_X86_64
19880b57cec5SDimitry Andric   _info.end_ip = base + unwindEntry->EndAddress;
19890b57cec5SDimitry Andric   // Only fill in the handler and LSDA if they're stale.
19900b57cec5SDimitry Andric   if (pc != getLastPC()) {
19910b57cec5SDimitry Andric     UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
19920b57cec5SDimitry Andric     if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
19930b57cec5SDimitry Andric       // The personality is given in the UNWIND_INFO itself. The LSDA immediately
19940b57cec5SDimitry Andric       // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
19950b57cec5SDimitry Andric       // these structures.)
19960b57cec5SDimitry Andric       // N.B. UNWIND_INFO structs are DWORD-aligned.
19970b57cec5SDimitry Andric       uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
19980b57cec5SDimitry Andric       const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
19990b57cec5SDimitry Andric       _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
200006c3fb27SDimitry Andric       _dispContext.HandlerData = reinterpret_cast<void *>(_info.lsda);
200106c3fb27SDimitry Andric       _dispContext.LanguageHandler =
200206c3fb27SDimitry Andric           reinterpret_cast<EXCEPTION_ROUTINE *>(base + *handler);
20030b57cec5SDimitry Andric       if (*handler) {
20040b57cec5SDimitry Andric         _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
20050b57cec5SDimitry Andric       } else
20060b57cec5SDimitry Andric         _info.handler = 0;
20070b57cec5SDimitry Andric     } else {
20080b57cec5SDimitry Andric       _info.lsda = 0;
20090b57cec5SDimitry Andric       _info.handler = 0;
20100b57cec5SDimitry Andric     }
20110b57cec5SDimitry Andric   }
20120b57cec5SDimitry Andric #endif
20130b57cec5SDimitry Andric   setLastPC(pc);
20140b57cec5SDimitry Andric   return true;
20150b57cec5SDimitry Andric }
20160b57cec5SDimitry Andric #endif
20170b57cec5SDimitry Andric 
201881ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
201981ad6265SDimitry Andric // Masks for traceback table field xtbtable.
202081ad6265SDimitry Andric enum xTBTableMask : uint8_t {
202181ad6265SDimitry Andric   reservedBit = 0x02, // The traceback table was incorrectly generated if set
202281ad6265SDimitry Andric                       // (see comments in function getInfoFromTBTable().
202381ad6265SDimitry Andric   ehInfoBit = 0x08    // Exception handling info is present if set
202481ad6265SDimitry Andric };
202581ad6265SDimitry Andric 
202681ad6265SDimitry Andric enum frameType : unw_word_t {
202781ad6265SDimitry Andric   frameWithXLEHStateTable = 0,
202881ad6265SDimitry Andric   frameWithEHInfo = 1
202981ad6265SDimitry Andric };
203081ad6265SDimitry Andric 
203181ad6265SDimitry Andric extern "C" {
203281ad6265SDimitry Andric typedef _Unwind_Reason_Code __xlcxx_personality_v0_t(int, _Unwind_Action,
203381ad6265SDimitry Andric                                                      uint64_t,
203481ad6265SDimitry Andric                                                      _Unwind_Exception *,
203581ad6265SDimitry Andric                                                      struct _Unwind_Context *);
203681ad6265SDimitry Andric __attribute__((__weak__)) __xlcxx_personality_v0_t __xlcxx_personality_v0;
203781ad6265SDimitry Andric }
203881ad6265SDimitry Andric 
203981ad6265SDimitry Andric static __xlcxx_personality_v0_t *xlcPersonalityV0;
204081ad6265SDimitry Andric static RWMutex xlcPersonalityV0InitLock;
204181ad6265SDimitry Andric 
204281ad6265SDimitry Andric template <typename A, typename R>
204381ad6265SDimitry Andric bool UnwindCursor<A, R>::getInfoFromTBTable(pint_t pc, R &registers) {
204481ad6265SDimitry Andric   uint32_t *p = reinterpret_cast<uint32_t *>(pc);
204581ad6265SDimitry Andric 
204681ad6265SDimitry Andric   // Keep looking forward until a word of 0 is found. The traceback
204781ad6265SDimitry Andric   // table starts at the following word.
204881ad6265SDimitry Andric   while (*p)
204981ad6265SDimitry Andric     ++p;
205081ad6265SDimitry Andric   tbtable *TBTable = reinterpret_cast<tbtable *>(p + 1);
205181ad6265SDimitry Andric 
205281ad6265SDimitry Andric   if (_LIBUNWIND_TRACING_UNWINDING) {
205381ad6265SDimitry Andric     char functionBuf[512];
205481ad6265SDimitry Andric     const char *functionName = functionBuf;
205581ad6265SDimitry Andric     unw_word_t offset;
205681ad6265SDimitry Andric     if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) {
205781ad6265SDimitry Andric       functionName = ".anonymous.";
205881ad6265SDimitry Andric     }
205981ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("%s: Look up traceback table of func=%s at %p",
206081ad6265SDimitry Andric                                __func__, functionName,
206181ad6265SDimitry Andric                                reinterpret_cast<void *>(TBTable));
206281ad6265SDimitry Andric   }
206381ad6265SDimitry Andric 
206481ad6265SDimitry Andric   // If the traceback table does not contain necessary info, bypass this frame.
206581ad6265SDimitry Andric   if (!TBTable->tb.has_tboff)
206681ad6265SDimitry Andric     return false;
206781ad6265SDimitry Andric 
206881ad6265SDimitry Andric   // Structure tbtable_ext contains important data we are looking for.
206981ad6265SDimitry Andric   p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext);
207081ad6265SDimitry Andric 
207181ad6265SDimitry Andric   // Skip field parminfo if it exists.
207281ad6265SDimitry Andric   if (TBTable->tb.fixedparms || TBTable->tb.floatparms)
207381ad6265SDimitry Andric     ++p;
207481ad6265SDimitry Andric 
207581ad6265SDimitry Andric   // p now points to tb_offset, the offset from start of function to TB table.
207681ad6265SDimitry Andric   unw_word_t start_ip =
207781ad6265SDimitry Andric       reinterpret_cast<unw_word_t>(TBTable) - *p - sizeof(uint32_t);
207881ad6265SDimitry Andric   unw_word_t end_ip = reinterpret_cast<unw_word_t>(TBTable);
207981ad6265SDimitry Andric   ++p;
208081ad6265SDimitry Andric 
208181ad6265SDimitry Andric   _LIBUNWIND_TRACE_UNWINDING("start_ip=%p, end_ip=%p\n",
208281ad6265SDimitry Andric                              reinterpret_cast<void *>(start_ip),
208381ad6265SDimitry Andric                              reinterpret_cast<void *>(end_ip));
208481ad6265SDimitry Andric 
208581ad6265SDimitry Andric   // Skip field hand_mask if it exists.
208681ad6265SDimitry Andric   if (TBTable->tb.int_hndl)
208781ad6265SDimitry Andric     ++p;
208881ad6265SDimitry Andric 
208981ad6265SDimitry Andric   unw_word_t lsda = 0;
209081ad6265SDimitry Andric   unw_word_t handler = 0;
209181ad6265SDimitry Andric   unw_word_t flags = frameType::frameWithXLEHStateTable;
209281ad6265SDimitry Andric 
209381ad6265SDimitry Andric   if (TBTable->tb.lang == TB_CPLUSPLUS && TBTable->tb.has_ctl) {
209481ad6265SDimitry Andric     // State table info is available. The ctl_info field indicates the
209581ad6265SDimitry Andric     // number of CTL anchors. There should be only one entry for the C++
209681ad6265SDimitry Andric     // state table.
209781ad6265SDimitry Andric     assert(*p == 1 && "libunwind: there must be only one ctl_info entry");
209881ad6265SDimitry Andric     ++p;
209981ad6265SDimitry Andric     // p points to the offset of the state table into the stack.
210081ad6265SDimitry Andric     pint_t stateTableOffset = *p++;
210181ad6265SDimitry Andric 
210281ad6265SDimitry Andric     int framePointerReg;
210381ad6265SDimitry Andric 
210481ad6265SDimitry Andric     // Skip fields name_len and name if exist.
210581ad6265SDimitry Andric     if (TBTable->tb.name_present) {
210681ad6265SDimitry Andric       const uint16_t name_len = *(reinterpret_cast<uint16_t *>(p));
210781ad6265SDimitry Andric       p = reinterpret_cast<uint32_t *>(reinterpret_cast<char *>(p) + name_len +
210881ad6265SDimitry Andric                                        sizeof(uint16_t));
210981ad6265SDimitry Andric     }
211081ad6265SDimitry Andric 
211181ad6265SDimitry Andric     if (TBTable->tb.uses_alloca)
211281ad6265SDimitry Andric       framePointerReg = *(reinterpret_cast<char *>(p));
211381ad6265SDimitry Andric     else
211481ad6265SDimitry Andric       framePointerReg = 1; // default frame pointer == SP
211581ad6265SDimitry Andric 
211681ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
211781ad6265SDimitry Andric         "framePointerReg=%d, framePointer=%p, "
211881ad6265SDimitry Andric         "stateTableOffset=%#lx\n",
211981ad6265SDimitry Andric         framePointerReg,
212081ad6265SDimitry Andric         reinterpret_cast<void *>(_registers.getRegister(framePointerReg)),
212181ad6265SDimitry Andric         stateTableOffset);
212281ad6265SDimitry Andric     lsda = _registers.getRegister(framePointerReg) + stateTableOffset;
212381ad6265SDimitry Andric 
212481ad6265SDimitry Andric     // Since the traceback table generated by the legacy XLC++ does not
212581ad6265SDimitry Andric     // provide the location of the personality for the state table,
212681ad6265SDimitry Andric     // function __xlcxx_personality_v0(), which is the personality for the state
212781ad6265SDimitry Andric     // table and is exported from libc++abi, is directly assigned as the
212881ad6265SDimitry Andric     // handler here. When a legacy XLC++ frame is encountered, the symbol
212981ad6265SDimitry Andric     // is resolved dynamically using dlopen() to avoid hard dependency from
213081ad6265SDimitry Andric     // libunwind on libc++abi.
213181ad6265SDimitry Andric 
213281ad6265SDimitry Andric     // Resolve the function pointer to the state table personality if it has
213381ad6265SDimitry Andric     // not already.
213481ad6265SDimitry Andric     if (xlcPersonalityV0 == NULL) {
213581ad6265SDimitry Andric       xlcPersonalityV0InitLock.lock();
213681ad6265SDimitry Andric       if (xlcPersonalityV0 == NULL) {
213781ad6265SDimitry Andric         // If libc++abi is statically linked in, symbol __xlcxx_personality_v0
213881ad6265SDimitry Andric         // has been resolved at the link time.
213981ad6265SDimitry Andric         xlcPersonalityV0 = &__xlcxx_personality_v0;
214081ad6265SDimitry Andric         if (xlcPersonalityV0 == NULL) {
214181ad6265SDimitry Andric           // libc++abi is dynamically linked. Resolve __xlcxx_personality_v0
214281ad6265SDimitry Andric           // using dlopen().
214381ad6265SDimitry Andric           const char libcxxabi[] = "libc++abi.a(libc++abi.so.1)";
214481ad6265SDimitry Andric           void *libHandle;
2145bdd1243dSDimitry Andric           // The AIX dlopen() sets errno to 0 when it is successful, which
2146bdd1243dSDimitry Andric           // clobbers the value of errno from the user code. This is an AIX
2147bdd1243dSDimitry Andric           // bug because according to POSIX it should not set errno to 0. To
2148bdd1243dSDimitry Andric           // workaround before AIX fixes the bug, errno is saved and restored.
2149bdd1243dSDimitry Andric           int saveErrno = errno;
215081ad6265SDimitry Andric           libHandle = dlopen(libcxxabi, RTLD_MEMBER | RTLD_NOW);
215181ad6265SDimitry Andric           if (libHandle == NULL) {
215281ad6265SDimitry Andric             _LIBUNWIND_TRACE_UNWINDING("dlopen() failed with errno=%d\n",
215381ad6265SDimitry Andric                                        errno);
215481ad6265SDimitry Andric             assert(0 && "dlopen() failed");
215581ad6265SDimitry Andric           }
215681ad6265SDimitry Andric           xlcPersonalityV0 = reinterpret_cast<__xlcxx_personality_v0_t *>(
215781ad6265SDimitry Andric               dlsym(libHandle, "__xlcxx_personality_v0"));
215881ad6265SDimitry Andric           if (xlcPersonalityV0 == NULL) {
215981ad6265SDimitry Andric             _LIBUNWIND_TRACE_UNWINDING("dlsym() failed with errno=%d\n", errno);
216081ad6265SDimitry Andric             assert(0 && "dlsym() failed");
216181ad6265SDimitry Andric           }
216281ad6265SDimitry Andric           dlclose(libHandle);
2163bdd1243dSDimitry Andric           errno = saveErrno;
216481ad6265SDimitry Andric         }
216581ad6265SDimitry Andric       }
216681ad6265SDimitry Andric       xlcPersonalityV0InitLock.unlock();
216781ad6265SDimitry Andric     }
216881ad6265SDimitry Andric     handler = reinterpret_cast<unw_word_t>(xlcPersonalityV0);
216981ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("State table: LSDA=%p, Personality=%p\n",
217081ad6265SDimitry Andric                                reinterpret_cast<void *>(lsda),
217181ad6265SDimitry Andric                                reinterpret_cast<void *>(handler));
217281ad6265SDimitry Andric   } else if (TBTable->tb.longtbtable) {
217381ad6265SDimitry Andric     // This frame has the traceback table extension. Possible cases are
217481ad6265SDimitry Andric     // 1) a C++ frame that has the 'eh_info' structure; 2) a C++ frame that
217581ad6265SDimitry Andric     // is not EH aware; or, 3) a frame of other languages. We need to figure out
217681ad6265SDimitry Andric     // if the traceback table extension contains the 'eh_info' structure.
217781ad6265SDimitry Andric     //
217881ad6265SDimitry Andric     // We also need to deal with the complexity arising from some XL compiler
217981ad6265SDimitry Andric     // versions use the wrong ordering of 'longtbtable' and 'has_vec' bits
218081ad6265SDimitry Andric     // where the 'longtbtable' bit is meant to be the 'has_vec' bit and vice
218181ad6265SDimitry Andric     // versa. For frames of code generated by those compilers, the 'longtbtable'
218281ad6265SDimitry Andric     // bit may be set but there isn't really a traceback table extension.
218381ad6265SDimitry Andric     //
218481ad6265SDimitry Andric     // In </usr/include/sys/debug.h>, there is the following definition of
218581ad6265SDimitry Andric     // 'struct tbtable_ext'. It is not really a structure but a dummy to
218681ad6265SDimitry Andric     // collect the description of optional parts of the traceback table.
218781ad6265SDimitry Andric     //
218881ad6265SDimitry Andric     // struct tbtable_ext {
218981ad6265SDimitry Andric     //   ...
219081ad6265SDimitry Andric     //   char alloca_reg;        /* Register for alloca automatic storage */
219181ad6265SDimitry Andric     //   struct vec_ext vec_ext; /* Vector extension (if has_vec is set) */
219281ad6265SDimitry Andric     //   unsigned char xtbtable; /* More tbtable fields, if longtbtable is set*/
219381ad6265SDimitry Andric     // };
219481ad6265SDimitry Andric     //
219581ad6265SDimitry Andric     // Depending on how the 'has_vec'/'longtbtable' bit is interpreted, the data
219681ad6265SDimitry Andric     // following 'alloca_reg' can be treated either as 'struct vec_ext' or
219781ad6265SDimitry Andric     // 'unsigned char xtbtable'. 'xtbtable' bits are defined in
219881ad6265SDimitry Andric     // </usr/include/sys/debug.h> as flags. The 7th bit '0x02' is currently
219981ad6265SDimitry Andric     // unused and should not be set. 'struct vec_ext' is defined in
220081ad6265SDimitry Andric     // </usr/include/sys/debug.h> as follows:
220181ad6265SDimitry Andric     //
220281ad6265SDimitry Andric     // struct vec_ext {
220381ad6265SDimitry Andric     //   unsigned vr_saved:6;      /* Number of non-volatile vector regs saved
220481ad6265SDimitry Andric     //   */
220581ad6265SDimitry Andric     //                             /* first register saved is assumed to be */
220681ad6265SDimitry Andric     //                             /* 32 - vr_saved                         */
220781ad6265SDimitry Andric     //   unsigned saves_vrsave:1;  /* Set if vrsave is saved on the stack */
220881ad6265SDimitry Andric     //   unsigned has_varargs:1;
220981ad6265SDimitry Andric     //   ...
221081ad6265SDimitry Andric     // };
221181ad6265SDimitry Andric     //
221281ad6265SDimitry Andric     // Here, the 7th bit is used as 'saves_vrsave'. To determine whether it
221381ad6265SDimitry Andric     // is 'struct vec_ext' or 'xtbtable' that follows 'alloca_reg',
221481ad6265SDimitry Andric     // we checks if the 7th bit is set or not because 'xtbtable' should
221581ad6265SDimitry Andric     // never have the 7th bit set. The 7th bit of 'xtbtable' will be reserved
221681ad6265SDimitry Andric     // in the future to make sure the mitigation works. This mitigation
221781ad6265SDimitry Andric     // is not 100% bullet proof because 'struct vec_ext' may not always have
221881ad6265SDimitry Andric     // 'saves_vrsave' bit set.
221981ad6265SDimitry Andric     //
222081ad6265SDimitry Andric     // 'reservedBit' is defined in enum 'xTBTableMask' above as the mask for
222181ad6265SDimitry Andric     // checking the 7th bit.
222281ad6265SDimitry Andric 
222381ad6265SDimitry Andric     // p points to field name len.
222481ad6265SDimitry Andric     uint8_t *charPtr = reinterpret_cast<uint8_t *>(p);
222581ad6265SDimitry Andric 
222681ad6265SDimitry Andric     // Skip fields name_len and name if they exist.
222781ad6265SDimitry Andric     if (TBTable->tb.name_present) {
222881ad6265SDimitry Andric       const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr));
222981ad6265SDimitry Andric       charPtr = charPtr + name_len + sizeof(uint16_t);
223081ad6265SDimitry Andric     }
223181ad6265SDimitry Andric 
223281ad6265SDimitry Andric     // Skip field alloc_reg if it exists.
223381ad6265SDimitry Andric     if (TBTable->tb.uses_alloca)
223481ad6265SDimitry Andric       ++charPtr;
223581ad6265SDimitry Andric 
223681ad6265SDimitry Andric     // Check traceback table bit has_vec. Skip struct vec_ext if it exists.
223781ad6265SDimitry Andric     if (TBTable->tb.has_vec)
223881ad6265SDimitry Andric       // Note struct vec_ext does exist at this point because whether the
223981ad6265SDimitry Andric       // ordering of longtbtable and has_vec bits is correct or not, both
224081ad6265SDimitry Andric       // are set.
224181ad6265SDimitry Andric       charPtr += sizeof(struct vec_ext);
224281ad6265SDimitry Andric 
224381ad6265SDimitry Andric     // charPtr points to field 'xtbtable'. Check if the EH info is available.
224481ad6265SDimitry Andric     // Also check if the reserved bit of the extended traceback table field
224581ad6265SDimitry Andric     // 'xtbtable' is set. If it is, the traceback table was incorrectly
224681ad6265SDimitry Andric     // generated by an XL compiler that uses the wrong ordering of 'longtbtable'
224781ad6265SDimitry Andric     // and 'has_vec' bits and this is in fact 'struct vec_ext'. So skip the
224881ad6265SDimitry Andric     // frame.
224981ad6265SDimitry Andric     if ((*charPtr & xTBTableMask::ehInfoBit) &&
225081ad6265SDimitry Andric         !(*charPtr & xTBTableMask::reservedBit)) {
225181ad6265SDimitry Andric       // Mark this frame has the new EH info.
225281ad6265SDimitry Andric       flags = frameType::frameWithEHInfo;
225381ad6265SDimitry Andric 
225481ad6265SDimitry Andric       // eh_info is available.
225581ad6265SDimitry Andric       charPtr++;
225681ad6265SDimitry Andric       // The pointer is 4-byte aligned.
225781ad6265SDimitry Andric       if (reinterpret_cast<uintptr_t>(charPtr) % 4)
225881ad6265SDimitry Andric         charPtr += 4 - reinterpret_cast<uintptr_t>(charPtr) % 4;
225981ad6265SDimitry Andric       uintptr_t *ehInfo =
226081ad6265SDimitry Andric           reinterpret_cast<uintptr_t *>(*(reinterpret_cast<uintptr_t *>(
226181ad6265SDimitry Andric               registers.getRegister(2) +
226281ad6265SDimitry Andric               *(reinterpret_cast<uintptr_t *>(charPtr)))));
226381ad6265SDimitry Andric 
226481ad6265SDimitry Andric       // ehInfo points to structure en_info. The first member is version.
226581ad6265SDimitry Andric       // Only version 0 is currently supported.
226681ad6265SDimitry Andric       assert(*(reinterpret_cast<uint32_t *>(ehInfo)) == 0 &&
226781ad6265SDimitry Andric              "libunwind: ehInfo version other than 0 is not supported");
226881ad6265SDimitry Andric 
226981ad6265SDimitry Andric       // Increment ehInfo to point to member lsda.
227081ad6265SDimitry Andric       ++ehInfo;
227181ad6265SDimitry Andric       lsda = *ehInfo++;
227281ad6265SDimitry Andric 
227381ad6265SDimitry Andric       // enInfo now points to member personality.
227481ad6265SDimitry Andric       handler = *ehInfo;
227581ad6265SDimitry Andric 
227681ad6265SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING("Range table: LSDA=%#lx, Personality=%#lx\n",
227781ad6265SDimitry Andric                                  lsda, handler);
227881ad6265SDimitry Andric     }
227981ad6265SDimitry Andric   }
228081ad6265SDimitry Andric 
228181ad6265SDimitry Andric   _info.start_ip = start_ip;
228281ad6265SDimitry Andric   _info.end_ip = end_ip;
228381ad6265SDimitry Andric   _info.lsda = lsda;
228481ad6265SDimitry Andric   _info.handler = handler;
228581ad6265SDimitry Andric   _info.gp = 0;
228681ad6265SDimitry Andric   _info.flags = flags;
228781ad6265SDimitry Andric   _info.format = 0;
228881ad6265SDimitry Andric   _info.unwind_info = reinterpret_cast<unw_word_t>(TBTable);
228981ad6265SDimitry Andric   _info.unwind_info_size = 0;
229081ad6265SDimitry Andric   _info.extra = registers.getRegister(2);
229181ad6265SDimitry Andric 
229281ad6265SDimitry Andric   return true;
229381ad6265SDimitry Andric }
229481ad6265SDimitry Andric 
229581ad6265SDimitry Andric // Step back up the stack following the frame back link.
229681ad6265SDimitry Andric template <typename A, typename R>
229781ad6265SDimitry Andric int UnwindCursor<A, R>::stepWithTBTable(pint_t pc, tbtable *TBTable,
229881ad6265SDimitry Andric                                         R &registers, bool &isSignalFrame) {
229981ad6265SDimitry Andric   if (_LIBUNWIND_TRACING_UNWINDING) {
230081ad6265SDimitry Andric     char functionBuf[512];
230181ad6265SDimitry Andric     const char *functionName = functionBuf;
230281ad6265SDimitry Andric     unw_word_t offset;
230381ad6265SDimitry Andric     if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) {
230481ad6265SDimitry Andric       functionName = ".anonymous.";
230581ad6265SDimitry Andric     }
23065f757f3fSDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
23075f757f3fSDimitry Andric         "%s: Look up traceback table of func=%s at %p, pc=%p, "
23085f757f3fSDimitry Andric         "SP=%p, saves_lr=%d, stores_bc=%d",
23095f757f3fSDimitry Andric         __func__, functionName, reinterpret_cast<void *>(TBTable),
23105f757f3fSDimitry Andric         reinterpret_cast<void *>(pc),
23115f757f3fSDimitry Andric         reinterpret_cast<void *>(registers.getSP()), TBTable->tb.saves_lr,
23125f757f3fSDimitry Andric         TBTable->tb.stores_bc);
231381ad6265SDimitry Andric   }
231481ad6265SDimitry Andric 
231581ad6265SDimitry Andric #if defined(__powerpc64__)
23165f757f3fSDimitry Andric   // Instruction to reload TOC register "ld r2,40(r1)"
231781ad6265SDimitry Andric   const uint32_t loadTOCRegInst = 0xe8410028;
231881ad6265SDimitry Andric   const int32_t unwPPCF0Index = UNW_PPC64_F0;
231981ad6265SDimitry Andric   const int32_t unwPPCV0Index = UNW_PPC64_V0;
232081ad6265SDimitry Andric #else
23215f757f3fSDimitry Andric   // Instruction to reload TOC register "lwz r2,20(r1)"
232281ad6265SDimitry Andric   const uint32_t loadTOCRegInst = 0x80410014;
232381ad6265SDimitry Andric   const int32_t unwPPCF0Index = UNW_PPC_F0;
232481ad6265SDimitry Andric   const int32_t unwPPCV0Index = UNW_PPC_V0;
232581ad6265SDimitry Andric #endif
232681ad6265SDimitry Andric 
23275f757f3fSDimitry Andric   // lastStack points to the stack frame of the next routine up.
23285f757f3fSDimitry Andric   pint_t curStack = static_cast<pint_t>(registers.getSP());
23295f757f3fSDimitry Andric   pint_t lastStack = *reinterpret_cast<pint_t *>(curStack);
23305f757f3fSDimitry Andric 
23315f757f3fSDimitry Andric   if (lastStack == 0)
23325f757f3fSDimitry Andric     return UNW_STEP_END;
23335f757f3fSDimitry Andric 
233481ad6265SDimitry Andric   R newRegisters = registers;
233581ad6265SDimitry Andric 
23365f757f3fSDimitry Andric   // If backchain is not stored, use the current stack frame.
23375f757f3fSDimitry Andric   if (!TBTable->tb.stores_bc)
23385f757f3fSDimitry Andric     lastStack = curStack;
233981ad6265SDimitry Andric 
234081ad6265SDimitry Andric   // Return address is the address after call site instruction.
234181ad6265SDimitry Andric   pint_t returnAddress;
234281ad6265SDimitry Andric 
234381ad6265SDimitry Andric   if (isSignalFrame) {
234481ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("Possible signal handler frame: lastStack=%p",
234581ad6265SDimitry Andric                                reinterpret_cast<void *>(lastStack));
234681ad6265SDimitry Andric 
234781ad6265SDimitry Andric     sigcontext *sigContext = reinterpret_cast<sigcontext *>(
234881ad6265SDimitry Andric         reinterpret_cast<char *>(lastStack) + STKMINALIGN);
234981ad6265SDimitry Andric     returnAddress = sigContext->sc_jmpbuf.jmp_context.iar;
23505f757f3fSDimitry Andric 
23515f757f3fSDimitry Andric     bool useSTKMIN = false;
235281ad6265SDimitry Andric     if (returnAddress < 0x10000000) {
23535f757f3fSDimitry Andric       // Try again using STKMIN.
23545f757f3fSDimitry Andric       sigContext = reinterpret_cast<sigcontext *>(
23555f757f3fSDimitry Andric           reinterpret_cast<char *>(lastStack) + STKMIN);
23565f757f3fSDimitry Andric       returnAddress = sigContext->sc_jmpbuf.jmp_context.iar;
23575f757f3fSDimitry Andric       if (returnAddress < 0x10000000) {
23585f757f3fSDimitry Andric         _LIBUNWIND_TRACE_UNWINDING("Bad returnAddress=%p from sigcontext=%p",
23595f757f3fSDimitry Andric                                    reinterpret_cast<void *>(returnAddress),
23605f757f3fSDimitry Andric                                    reinterpret_cast<void *>(sigContext));
236181ad6265SDimitry Andric         return UNW_EBADFRAME;
23625f757f3fSDimitry Andric       }
23635f757f3fSDimitry Andric       useSTKMIN = true;
23645f757f3fSDimitry Andric     }
23655f757f3fSDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("Returning from a signal handler %s: "
236681ad6265SDimitry Andric                                "sigContext=%p, returnAddress=%p. "
23675f757f3fSDimitry Andric                                "Seems to be a valid address",
23685f757f3fSDimitry Andric                                useSTKMIN ? "STKMIN" : "STKMINALIGN",
236981ad6265SDimitry Andric                                reinterpret_cast<void *>(sigContext),
237081ad6265SDimitry Andric                                reinterpret_cast<void *>(returnAddress));
23715f757f3fSDimitry Andric 
237281ad6265SDimitry Andric     // Restore the condition register from sigcontext.
237381ad6265SDimitry Andric     newRegisters.setCR(sigContext->sc_jmpbuf.jmp_context.cr);
237481ad6265SDimitry Andric 
23755f757f3fSDimitry Andric     // Save the LR in sigcontext for stepping up when the function that
23765f757f3fSDimitry Andric     // raised the signal is a leaf function. This LR has the return address
23775f757f3fSDimitry Andric     // to the caller of the leaf function.
23785f757f3fSDimitry Andric     newRegisters.setLR(sigContext->sc_jmpbuf.jmp_context.lr);
23795f757f3fSDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
23805f757f3fSDimitry Andric         "Save LR=%p from sigcontext",
23815f757f3fSDimitry Andric         reinterpret_cast<void *>(sigContext->sc_jmpbuf.jmp_context.lr));
23825f757f3fSDimitry Andric 
238381ad6265SDimitry Andric     // Restore GPRs from sigcontext.
238481ad6265SDimitry Andric     for (int i = 0; i < 32; ++i)
238581ad6265SDimitry Andric       newRegisters.setRegister(i, sigContext->sc_jmpbuf.jmp_context.gpr[i]);
238681ad6265SDimitry Andric 
238781ad6265SDimitry Andric     // Restore FPRs from sigcontext.
238881ad6265SDimitry Andric     for (int i = 0; i < 32; ++i)
238981ad6265SDimitry Andric       newRegisters.setFloatRegister(i + unwPPCF0Index,
239081ad6265SDimitry Andric                                     sigContext->sc_jmpbuf.jmp_context.fpr[i]);
239181ad6265SDimitry Andric 
239281ad6265SDimitry Andric     // Restore vector registers if there is an associated extended context
239381ad6265SDimitry Andric     // structure.
239481ad6265SDimitry Andric     if (sigContext->sc_jmpbuf.jmp_context.msr & __EXTCTX) {
239581ad6265SDimitry Andric       ucontext_t *uContext = reinterpret_cast<ucontext_t *>(sigContext);
239681ad6265SDimitry Andric       if (uContext->__extctx->__extctx_magic == __EXTCTX_MAGIC) {
239781ad6265SDimitry Andric         for (int i = 0; i < 32; ++i)
239881ad6265SDimitry Andric           newRegisters.setVectorRegister(
239981ad6265SDimitry Andric               i + unwPPCV0Index, *(reinterpret_cast<v128 *>(
240081ad6265SDimitry Andric                                      &(uContext->__extctx->__vmx.__vr[i]))));
240181ad6265SDimitry Andric       }
240281ad6265SDimitry Andric     }
240381ad6265SDimitry Andric   } else {
240481ad6265SDimitry Andric     // Step up a normal frame.
240581ad6265SDimitry Andric 
24065f757f3fSDimitry Andric     if (!TBTable->tb.saves_lr && registers.getLR()) {
24075f757f3fSDimitry Andric       // This case should only occur if we were called from a signal handler
24085f757f3fSDimitry Andric       // and the signal occurred in a function that doesn't save the LR.
24095f757f3fSDimitry Andric       returnAddress = static_cast<pint_t>(registers.getLR());
24105f757f3fSDimitry Andric       _LIBUNWIND_TRACE_UNWINDING("Use saved LR=%p",
24115f757f3fSDimitry Andric                                  reinterpret_cast<void *>(returnAddress));
24125f757f3fSDimitry Andric     } else {
24135f757f3fSDimitry Andric       // Otherwise, use the LR value in the stack link area.
24145f757f3fSDimitry Andric       returnAddress = reinterpret_cast<pint_t *>(lastStack)[2];
24155f757f3fSDimitry Andric     }
24165f757f3fSDimitry Andric 
24175f757f3fSDimitry Andric     // Reset LR in the current context.
24180fca6ea1SDimitry Andric     newRegisters.setLR(static_cast<uintptr_t>(NULL));
24195f757f3fSDimitry Andric 
24205f757f3fSDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
24215f757f3fSDimitry Andric         "Extract info from lastStack=%p, returnAddress=%p",
242281ad6265SDimitry Andric         reinterpret_cast<void *>(lastStack),
242381ad6265SDimitry Andric         reinterpret_cast<void *>(returnAddress));
24245f757f3fSDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("fpr_regs=%d, gpr_regs=%d, saves_cr=%d",
242581ad6265SDimitry Andric                                TBTable->tb.fpr_saved, TBTable->tb.gpr_saved,
242681ad6265SDimitry Andric                                TBTable->tb.saves_cr);
242781ad6265SDimitry Andric 
242881ad6265SDimitry Andric     // Restore FP registers.
242981ad6265SDimitry Andric     char *ptrToRegs = reinterpret_cast<char *>(lastStack);
243081ad6265SDimitry Andric     double *FPRegs = reinterpret_cast<double *>(
243181ad6265SDimitry Andric         ptrToRegs - (TBTable->tb.fpr_saved * sizeof(double)));
243281ad6265SDimitry Andric     for (int i = 0; i < TBTable->tb.fpr_saved; ++i)
243381ad6265SDimitry Andric       newRegisters.setFloatRegister(
243481ad6265SDimitry Andric           32 - TBTable->tb.fpr_saved + i + unwPPCF0Index, FPRegs[i]);
243581ad6265SDimitry Andric 
243681ad6265SDimitry Andric     // Restore GP registers.
243781ad6265SDimitry Andric     ptrToRegs = reinterpret_cast<char *>(FPRegs);
243881ad6265SDimitry Andric     uintptr_t *GPRegs = reinterpret_cast<uintptr_t *>(
243981ad6265SDimitry Andric         ptrToRegs - (TBTable->tb.gpr_saved * sizeof(uintptr_t)));
244081ad6265SDimitry Andric     for (int i = 0; i < TBTable->tb.gpr_saved; ++i)
244181ad6265SDimitry Andric       newRegisters.setRegister(32 - TBTable->tb.gpr_saved + i, GPRegs[i]);
244281ad6265SDimitry Andric 
244381ad6265SDimitry Andric     // Restore Vector registers.
244481ad6265SDimitry Andric     ptrToRegs = reinterpret_cast<char *>(GPRegs);
244581ad6265SDimitry Andric 
244681ad6265SDimitry Andric     // Restore vector registers only if this is a Clang frame. Also
244781ad6265SDimitry Andric     // check if traceback table bit has_vec is set. If it is, structure
244881ad6265SDimitry Andric     // vec_ext is available.
244981ad6265SDimitry Andric     if (_info.flags == frameType::frameWithEHInfo && TBTable->tb.has_vec) {
245081ad6265SDimitry Andric 
245181ad6265SDimitry Andric       // Get to the vec_ext structure to check if vector registers are saved.
245281ad6265SDimitry Andric       uint32_t *p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext);
245381ad6265SDimitry Andric 
245481ad6265SDimitry Andric       // Skip field parminfo if exists.
245581ad6265SDimitry Andric       if (TBTable->tb.fixedparms || TBTable->tb.floatparms)
245681ad6265SDimitry Andric         ++p;
245781ad6265SDimitry Andric 
245881ad6265SDimitry Andric       // Skip field tb_offset if exists.
245981ad6265SDimitry Andric       if (TBTable->tb.has_tboff)
246081ad6265SDimitry Andric         ++p;
246181ad6265SDimitry Andric 
246281ad6265SDimitry Andric       // Skip field hand_mask if exists.
246381ad6265SDimitry Andric       if (TBTable->tb.int_hndl)
246481ad6265SDimitry Andric         ++p;
246581ad6265SDimitry Andric 
246681ad6265SDimitry Andric       // Skip fields ctl_info and ctl_info_disp if exist.
246781ad6265SDimitry Andric       if (TBTable->tb.has_ctl) {
246881ad6265SDimitry Andric         // Skip field ctl_info.
246981ad6265SDimitry Andric         ++p;
247081ad6265SDimitry Andric         // Skip field ctl_info_disp.
247181ad6265SDimitry Andric         ++p;
247281ad6265SDimitry Andric       }
247381ad6265SDimitry Andric 
247481ad6265SDimitry Andric       // Skip fields name_len and name if exist.
247581ad6265SDimitry Andric       // p is supposed to point to field name_len now.
247681ad6265SDimitry Andric       uint8_t *charPtr = reinterpret_cast<uint8_t *>(p);
247781ad6265SDimitry Andric       if (TBTable->tb.name_present) {
247881ad6265SDimitry Andric         const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr));
247981ad6265SDimitry Andric         charPtr = charPtr + name_len + sizeof(uint16_t);
248081ad6265SDimitry Andric       }
248181ad6265SDimitry Andric 
248281ad6265SDimitry Andric       // Skip field alloc_reg if it exists.
248381ad6265SDimitry Andric       if (TBTable->tb.uses_alloca)
248481ad6265SDimitry Andric         ++charPtr;
248581ad6265SDimitry Andric 
248681ad6265SDimitry Andric       struct vec_ext *vec_ext = reinterpret_cast<struct vec_ext *>(charPtr);
248781ad6265SDimitry Andric 
24885f757f3fSDimitry Andric       _LIBUNWIND_TRACE_UNWINDING("vr_saved=%d", vec_ext->vr_saved);
248981ad6265SDimitry Andric 
249081ad6265SDimitry Andric       // Restore vector register(s) if saved on the stack.
249181ad6265SDimitry Andric       if (vec_ext->vr_saved) {
249281ad6265SDimitry Andric         // Saved vector registers are 16-byte aligned.
249381ad6265SDimitry Andric         if (reinterpret_cast<uintptr_t>(ptrToRegs) % 16)
249481ad6265SDimitry Andric           ptrToRegs -= reinterpret_cast<uintptr_t>(ptrToRegs) % 16;
249581ad6265SDimitry Andric         v128 *VecRegs = reinterpret_cast<v128 *>(ptrToRegs - vec_ext->vr_saved *
249681ad6265SDimitry Andric                                                                  sizeof(v128));
249781ad6265SDimitry Andric         for (int i = 0; i < vec_ext->vr_saved; ++i) {
249881ad6265SDimitry Andric           newRegisters.setVectorRegister(
249981ad6265SDimitry Andric               32 - vec_ext->vr_saved + i + unwPPCV0Index, VecRegs[i]);
250081ad6265SDimitry Andric         }
250181ad6265SDimitry Andric       }
250281ad6265SDimitry Andric     }
250381ad6265SDimitry Andric     if (TBTable->tb.saves_cr) {
250481ad6265SDimitry Andric       // Get the saved condition register. The condition register is only
250581ad6265SDimitry Andric       // a single word.
250681ad6265SDimitry Andric       newRegisters.setCR(
250781ad6265SDimitry Andric           *(reinterpret_cast<uint32_t *>(lastStack + sizeof(uintptr_t))));
250881ad6265SDimitry Andric     }
250981ad6265SDimitry Andric 
251081ad6265SDimitry Andric     // Restore the SP.
251181ad6265SDimitry Andric     newRegisters.setSP(lastStack);
251281ad6265SDimitry Andric 
251381ad6265SDimitry Andric     // The first instruction after return.
251481ad6265SDimitry Andric     uint32_t firstInstruction = *(reinterpret_cast<uint32_t *>(returnAddress));
251581ad6265SDimitry Andric 
251681ad6265SDimitry Andric     // Do we need to set the TOC register?
251781ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
25185f757f3fSDimitry Andric         "Current gpr2=%p",
251981ad6265SDimitry Andric         reinterpret_cast<void *>(newRegisters.getRegister(2)));
252081ad6265SDimitry Andric     if (firstInstruction == loadTOCRegInst) {
252181ad6265SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
25225f757f3fSDimitry Andric           "Set gpr2=%p from frame",
252381ad6265SDimitry Andric           reinterpret_cast<void *>(reinterpret_cast<pint_t *>(lastStack)[5]));
252481ad6265SDimitry Andric       newRegisters.setRegister(2, reinterpret_cast<pint_t *>(lastStack)[5]);
252581ad6265SDimitry Andric     }
252681ad6265SDimitry Andric   }
252781ad6265SDimitry Andric   _LIBUNWIND_TRACE_UNWINDING("lastStack=%p, returnAddress=%p, pc=%p\n",
252881ad6265SDimitry Andric                              reinterpret_cast<void *>(lastStack),
252981ad6265SDimitry Andric                              reinterpret_cast<void *>(returnAddress),
253081ad6265SDimitry Andric                              reinterpret_cast<void *>(pc));
253181ad6265SDimitry Andric 
253281ad6265SDimitry Andric   // The return address is the address after call site instruction, so
2533bdd1243dSDimitry Andric   // setting IP to that simulates a return.
253481ad6265SDimitry Andric   newRegisters.setIP(reinterpret_cast<uintptr_t>(returnAddress));
253581ad6265SDimitry Andric 
253681ad6265SDimitry Andric   // Simulate the step by replacing the register set with the new ones.
253781ad6265SDimitry Andric   registers = newRegisters;
253881ad6265SDimitry Andric 
253981ad6265SDimitry Andric   // Check if the next frame is a signal frame.
254081ad6265SDimitry Andric   pint_t nextStack = *(reinterpret_cast<pint_t *>(registers.getSP()));
254181ad6265SDimitry Andric 
254281ad6265SDimitry Andric   // Return address is the address after call site instruction.
254381ad6265SDimitry Andric   pint_t nextReturnAddress = reinterpret_cast<pint_t *>(nextStack)[2];
254481ad6265SDimitry Andric 
254581ad6265SDimitry Andric   if (nextReturnAddress > 0x01 && nextReturnAddress < 0x10000) {
254681ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("The next is a signal handler frame: "
254781ad6265SDimitry Andric                                "nextStack=%p, next return address=%p\n",
254881ad6265SDimitry Andric                                reinterpret_cast<void *>(nextStack),
254981ad6265SDimitry Andric                                reinterpret_cast<void *>(nextReturnAddress));
255081ad6265SDimitry Andric     isSignalFrame = true;
255181ad6265SDimitry Andric   } else {
255281ad6265SDimitry Andric     isSignalFrame = false;
255381ad6265SDimitry Andric   }
255481ad6265SDimitry Andric   return UNW_STEP_SUCCESS;
255581ad6265SDimitry Andric }
255681ad6265SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
25570b57cec5SDimitry Andric 
25580b57cec5SDimitry Andric template <typename A, typename R>
25590b57cec5SDimitry Andric void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
256081ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
2561e8d8bef9SDimitry Andric   _isSigReturn = false;
2562e8d8bef9SDimitry Andric #endif
2563e8d8bef9SDimitry Andric 
2564e8d8bef9SDimitry Andric   pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
25650b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
25660b57cec5SDimitry Andric   // Remove the thumb bit so the IP represents the actual instruction address.
25670b57cec5SDimitry Andric   // This matches the behaviour of _Unwind_GetIP on arm.
25680b57cec5SDimitry Andric   pc &= (pint_t)~0x1;
25690b57cec5SDimitry Andric #endif
25700b57cec5SDimitry Andric 
25715ffd83dbSDimitry Andric   // Exit early if at the top of the stack.
25725ffd83dbSDimitry Andric   if (pc == 0) {
25735ffd83dbSDimitry Andric     _unwindInfoMissing = true;
25745ffd83dbSDimitry Andric     return;
25755ffd83dbSDimitry Andric   }
25765ffd83dbSDimitry Andric 
25770b57cec5SDimitry Andric   // If the last line of a function is a "throw" the compiler sometimes
25780b57cec5SDimitry Andric   // emits no instructions after the call to __cxa_throw.  This means
25790b57cec5SDimitry Andric   // the return address is actually the start of the next function.
25800b57cec5SDimitry Andric   // To disambiguate this, back up the pc when we know it is a return
25810b57cec5SDimitry Andric   // address.
25820b57cec5SDimitry Andric   if (isReturnAddress)
258381ad6265SDimitry Andric #if defined(_AIX)
258481ad6265SDimitry Andric     // PC needs to be a 4-byte aligned address to be able to look for a
258581ad6265SDimitry Andric     // word of 0 that indicates the start of the traceback table at the end
258681ad6265SDimitry Andric     // of a function on AIX.
258781ad6265SDimitry Andric     pc -= 4;
258881ad6265SDimitry Andric #else
25890b57cec5SDimitry Andric     --pc;
259081ad6265SDimitry Andric #endif
25910b57cec5SDimitry Andric 
259252418fc2SDimitry Andric #if !(defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)) &&            \
259352418fc2SDimitry Andric     !defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
25940fca6ea1SDimitry Andric   // In case of this is frame of signal handler, the IP saved in the signal
25950fca6ea1SDimitry Andric   // handler points to first non-executed instruction, while FDE/CIE expects IP
25960fca6ea1SDimitry Andric   // to be after the first non-executed instruction.
25970fca6ea1SDimitry Andric   if (_isSignalFrame)
25980fca6ea1SDimitry Andric     ++pc;
25990fca6ea1SDimitry Andric #endif
26000fca6ea1SDimitry Andric 
26010b57cec5SDimitry Andric   // Ask address space object to find unwind sections for this pc.
26020b57cec5SDimitry Andric   UnwindInfoSections sects;
26030b57cec5SDimitry Andric   if (_addressSpace.findUnwindSections(pc, sects)) {
26040b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
26050b57cec5SDimitry Andric     // If there is a compact unwind encoding table, look there first.
26060b57cec5SDimitry Andric     if (sects.compact_unwind_section != 0) {
26070b57cec5SDimitry Andric       if (this->getInfoFromCompactEncodingSection(pc, sects)) {
26080b57cec5SDimitry Andric   #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
26090b57cec5SDimitry Andric         // Found info in table, done unless encoding says to use dwarf.
26100b57cec5SDimitry Andric         uint32_t dwarfOffset;
26110b57cec5SDimitry Andric         if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
26120b57cec5SDimitry Andric           if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
26130b57cec5SDimitry Andric             // found info in dwarf, done
26140b57cec5SDimitry Andric             return;
26150b57cec5SDimitry Andric           }
26160b57cec5SDimitry Andric         }
26170b57cec5SDimitry Andric   #endif
26180b57cec5SDimitry Andric         // If unwind table has entry, but entry says there is no unwind info,
26190b57cec5SDimitry Andric         // record that we have no unwind info.
26200b57cec5SDimitry Andric         if (_info.format == 0)
26210b57cec5SDimitry Andric           _unwindInfoMissing = true;
26220b57cec5SDimitry Andric         return;
26230b57cec5SDimitry Andric       }
26240b57cec5SDimitry Andric     }
26250b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
26260b57cec5SDimitry Andric 
26270b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
26280b57cec5SDimitry Andric     // If there is SEH unwind info, look there next.
26290b57cec5SDimitry Andric     if (this->getInfoFromSEH(pc))
26300b57cec5SDimitry Andric       return;
26310b57cec5SDimitry Andric #endif
26320b57cec5SDimitry Andric 
263381ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
263481ad6265SDimitry Andric     // If there is unwind info in the traceback table, look there next.
263581ad6265SDimitry Andric     if (this->getInfoFromTBTable(pc, _registers))
263681ad6265SDimitry Andric       return;
263781ad6265SDimitry Andric #endif
263881ad6265SDimitry Andric 
26390b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
26400b57cec5SDimitry Andric     // If there is dwarf unwind info, look there next.
26410b57cec5SDimitry Andric     if (sects.dwarf_section != 0) {
26420b57cec5SDimitry Andric       if (this->getInfoFromDwarfSection(pc, sects)) {
26430b57cec5SDimitry Andric         // found info in dwarf, done
26440b57cec5SDimitry Andric         return;
26450b57cec5SDimitry Andric       }
26460b57cec5SDimitry Andric     }
26470b57cec5SDimitry Andric #endif
26480b57cec5SDimitry Andric 
26490b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
26500b57cec5SDimitry Andric     // If there is ARM EHABI unwind info, look there next.
26510b57cec5SDimitry Andric     if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
26520b57cec5SDimitry Andric       return;
26530b57cec5SDimitry Andric #endif
26540b57cec5SDimitry Andric   }
26550b57cec5SDimitry Andric 
26560b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
26570b57cec5SDimitry Andric   // There is no static unwind info for this pc. Look to see if an FDE was
26580b57cec5SDimitry Andric   // dynamically registered for it.
2659e8d8bef9SDimitry Andric   pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll,
2660e8d8bef9SDimitry Andric                                                pc);
26610b57cec5SDimitry Andric   if (cachedFDE != 0) {
2662e8d8bef9SDimitry Andric     typename CFI_Parser<A>::FDE_Info fdeInfo;
2663e8d8bef9SDimitry Andric     typename CFI_Parser<A>::CIE_Info cieInfo;
2664e8d8bef9SDimitry Andric     if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo))
2665e8d8bef9SDimitry Andric       if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
26660b57cec5SDimitry Andric         return;
26670b57cec5SDimitry Andric   }
26680b57cec5SDimitry Andric 
26690b57cec5SDimitry Andric   // Lastly, ask AddressSpace object about platform specific ways to locate
26700b57cec5SDimitry Andric   // other FDEs.
26710b57cec5SDimitry Andric   pint_t fde;
26720b57cec5SDimitry Andric   if (_addressSpace.findOtherFDE(pc, fde)) {
2673e8d8bef9SDimitry Andric     typename CFI_Parser<A>::FDE_Info fdeInfo;
2674e8d8bef9SDimitry Andric     typename CFI_Parser<A>::CIE_Info cieInfo;
26750b57cec5SDimitry Andric     if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
26760b57cec5SDimitry Andric       // Double check this FDE is for a function that includes the pc.
2677e8d8bef9SDimitry Andric       if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd))
2678e8d8bef9SDimitry Andric         if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
26790b57cec5SDimitry Andric           return;
26800b57cec5SDimitry Andric     }
26810b57cec5SDimitry Andric   }
26820b57cec5SDimitry Andric #endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
26830b57cec5SDimitry Andric 
268481ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
2685e8d8bef9SDimitry Andric   if (setInfoForSigReturn())
2686e8d8bef9SDimitry Andric     return;
2687e8d8bef9SDimitry Andric #endif
2688e8d8bef9SDimitry Andric 
26890b57cec5SDimitry Andric   // no unwind info, flag that we can't reliably unwind
26900b57cec5SDimitry Andric   _unwindInfoMissing = true;
26910b57cec5SDimitry Andric }
26920b57cec5SDimitry Andric 
269381ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&                               \
269481ad6265SDimitry Andric     defined(_LIBUNWIND_TARGET_AARCH64)
2695e8d8bef9SDimitry Andric template <typename A, typename R>
2696e8d8bef9SDimitry Andric bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_arm64 &) {
2697e8d8bef9SDimitry Andric   // Look for the sigreturn trampoline. The trampoline's body is two
2698e8d8bef9SDimitry Andric   // specific instructions (see below). Typically the trampoline comes from the
2699e8d8bef9SDimitry Andric   // vDSO[1] (i.e. the __kernel_rt_sigreturn function). A libc might provide its
2700e8d8bef9SDimitry Andric   // own restorer function, though, or user-mode QEMU might write a trampoline
2701e8d8bef9SDimitry Andric   // onto the stack.
2702e8d8bef9SDimitry Andric   //
2703e8d8bef9SDimitry Andric   // This special code path is a fallback that is only used if the trampoline
2704e8d8bef9SDimitry Andric   // lacks proper (e.g. DWARF) unwind info. On AArch64, a new DWARF register
2705e8d8bef9SDimitry Andric   // constant for the PC needs to be defined before DWARF can handle a signal
2706e8d8bef9SDimitry Andric   // trampoline. This code may segfault if the target PC is unreadable, e.g.:
2707e8d8bef9SDimitry Andric   //  - The PC points at a function compiled without unwind info, and which is
2708e8d8bef9SDimitry Andric   //    part of an execute-only mapping (e.g. using -Wl,--execute-only).
2709e8d8bef9SDimitry Andric   //  - The PC is invalid and happens to point to unreadable or unmapped memory.
2710e8d8bef9SDimitry Andric   //
2711e8d8bef9SDimitry Andric   // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/vdso/sigreturn.S
2712e8d8bef9SDimitry Andric   const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
271381ad6265SDimitry Andric   // The PC might contain an invalid address if the unwind info is bad, so
27141db9f3b2SDimitry Andric   // directly accessing it could cause a SIGSEGV.
27151db9f3b2SDimitry Andric   if (!isReadableAddr(pc))
27161db9f3b2SDimitry Andric     return false;
27171db9f3b2SDimitry Andric   auto *instructions = reinterpret_cast<const uint32_t *>(pc);
2718e8d8bef9SDimitry Andric   // Look for instructions: mov x8, #0x8b; svc #0x0
27191db9f3b2SDimitry Andric   if (instructions[0] != 0xd2801168 || instructions[1] != 0xd4000001)
272081ad6265SDimitry Andric     return false;
272181ad6265SDimitry Andric 
2722e8d8bef9SDimitry Andric   _info = {};
272381ad6265SDimitry Andric   _info.start_ip = pc;
272481ad6265SDimitry Andric   _info.end_ip = pc + 4;
2725e8d8bef9SDimitry Andric   _isSigReturn = true;
2726e8d8bef9SDimitry Andric   return true;
2727e8d8bef9SDimitry Andric }
2728e8d8bef9SDimitry Andric 
2729e8d8bef9SDimitry Andric template <typename A, typename R>
2730e8d8bef9SDimitry Andric int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) {
2731e8d8bef9SDimitry Andric   // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
2732e8d8bef9SDimitry Andric   //  - 128-byte siginfo struct
2733e8d8bef9SDimitry Andric   //  - ucontext struct:
2734e8d8bef9SDimitry Andric   //     - 8-byte long (uc_flags)
2735e8d8bef9SDimitry Andric   //     - 8-byte pointer (uc_link)
2736e8d8bef9SDimitry Andric   //     - 24-byte stack_t
2737e8d8bef9SDimitry Andric   //     - 128-byte signal set
2738e8d8bef9SDimitry Andric   //     - 8 bytes of padding because sigcontext has 16-byte alignment
2739e8d8bef9SDimitry Andric   //     - sigcontext/mcontext_t
2740e8d8bef9SDimitry Andric   // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c
2741e8d8bef9SDimitry Andric   const pint_t kOffsetSpToSigcontext = (128 + 8 + 8 + 24 + 128 + 8); // 304
2742e8d8bef9SDimitry Andric 
2743e8d8bef9SDimitry Andric   // Offsets from sigcontext to each register.
2744e8d8bef9SDimitry Andric   const pint_t kOffsetGprs = 8; // offset to "__u64 regs[31]" field
2745e8d8bef9SDimitry Andric   const pint_t kOffsetSp = 256; // offset to "__u64 sp" field
2746e8d8bef9SDimitry Andric   const pint_t kOffsetPc = 264; // offset to "__u64 pc" field
2747e8d8bef9SDimitry Andric 
2748e8d8bef9SDimitry Andric   pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
2749e8d8bef9SDimitry Andric 
2750e8d8bef9SDimitry Andric   for (int i = 0; i <= 30; ++i) {
2751e8d8bef9SDimitry Andric     uint64_t value = _addressSpace.get64(sigctx + kOffsetGprs +
2752e8d8bef9SDimitry Andric                                          static_cast<pint_t>(i * 8));
2753349cc55cSDimitry Andric     _registers.setRegister(UNW_AARCH64_X0 + i, value);
2754e8d8bef9SDimitry Andric   }
2755e8d8bef9SDimitry Andric   _registers.setSP(_addressSpace.get64(sigctx + kOffsetSp));
2756e8d8bef9SDimitry Andric   _registers.setIP(_addressSpace.get64(sigctx + kOffsetPc));
2757e8d8bef9SDimitry Andric   _isSignalFrame = true;
2758e8d8bef9SDimitry Andric   return UNW_STEP_SUCCESS;
2759e8d8bef9SDimitry Andric }
276081ad6265SDimitry Andric #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
276181ad6265SDimitry Andric        // defined(_LIBUNWIND_TARGET_AARCH64)
276281ad6265SDimitry Andric 
276381ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&                               \
276406c3fb27SDimitry Andric     defined(_LIBUNWIND_TARGET_RISCV)
276506c3fb27SDimitry Andric template <typename A, typename R>
276606c3fb27SDimitry Andric bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_riscv &) {
276706c3fb27SDimitry Andric   const pint_t pc = static_cast<pint_t>(getReg(UNW_REG_IP));
27681db9f3b2SDimitry Andric   // The PC might contain an invalid address if the unwind info is bad, so
27691db9f3b2SDimitry Andric   // directly accessing it could cause a SIGSEGV.
27701db9f3b2SDimitry Andric   if (!isReadableAddr(pc))
27711db9f3b2SDimitry Andric     return false;
27721db9f3b2SDimitry Andric   const auto *instructions = reinterpret_cast<const uint32_t *>(pc);
277306c3fb27SDimitry Andric   // Look for the two instructions used in the sigreturn trampoline
277406c3fb27SDimitry Andric   // __vdso_rt_sigreturn:
277506c3fb27SDimitry Andric   //
277606c3fb27SDimitry Andric   // 0x08b00893 li a7,0x8b
277706c3fb27SDimitry Andric   // 0x00000073 ecall
27781db9f3b2SDimitry Andric   if (instructions[0] != 0x08b00893 || instructions[1] != 0x00000073)
277906c3fb27SDimitry Andric     return false;
278006c3fb27SDimitry Andric 
278106c3fb27SDimitry Andric   _info = {};
278206c3fb27SDimitry Andric   _info.start_ip = pc;
278306c3fb27SDimitry Andric   _info.end_ip = pc + 4;
278406c3fb27SDimitry Andric   _isSigReturn = true;
278506c3fb27SDimitry Andric   return true;
278606c3fb27SDimitry Andric }
278706c3fb27SDimitry Andric 
278806c3fb27SDimitry Andric template <typename A, typename R>
278906c3fb27SDimitry Andric int UnwindCursor<A, R>::stepThroughSigReturn(Registers_riscv &) {
279006c3fb27SDimitry Andric   // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
279106c3fb27SDimitry Andric   //  - 128-byte siginfo struct
279206c3fb27SDimitry Andric   //  - ucontext_t struct:
279306c3fb27SDimitry Andric   //     - 8-byte long (__uc_flags)
279406c3fb27SDimitry Andric   //     - 8-byte pointer (*uc_link)
279506c3fb27SDimitry Andric   //     - 24-byte uc_stack
279606c3fb27SDimitry Andric   //     - 8-byte uc_sigmask
279706c3fb27SDimitry Andric   //     - 120-byte of padding to allow sigset_t to be expanded in the future
279806c3fb27SDimitry Andric   //     - 8 bytes of padding because sigcontext has 16-byte alignment
279906c3fb27SDimitry Andric   //     - struct sigcontext uc_mcontext
280006c3fb27SDimitry Andric   // [1]
280106c3fb27SDimitry Andric   // https://github.com/torvalds/linux/blob/master/arch/riscv/kernel/signal.c
280206c3fb27SDimitry Andric   const pint_t kOffsetSpToSigcontext = 128 + 8 + 8 + 24 + 8 + 128;
280306c3fb27SDimitry Andric 
280406c3fb27SDimitry Andric   const pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
280506c3fb27SDimitry Andric   _registers.setIP(_addressSpace.get64(sigctx));
280606c3fb27SDimitry Andric   for (int i = UNW_RISCV_X1; i <= UNW_RISCV_X31; ++i) {
280706c3fb27SDimitry Andric     uint64_t value = _addressSpace.get64(sigctx + static_cast<pint_t>(i * 8));
280806c3fb27SDimitry Andric     _registers.setRegister(i, value);
280906c3fb27SDimitry Andric   }
281006c3fb27SDimitry Andric   _isSignalFrame = true;
281106c3fb27SDimitry Andric   return UNW_STEP_SUCCESS;
281206c3fb27SDimitry Andric }
281306c3fb27SDimitry Andric #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
281406c3fb27SDimitry Andric        // defined(_LIBUNWIND_TARGET_RISCV)
281506c3fb27SDimitry Andric 
281606c3fb27SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&                               \
281781ad6265SDimitry Andric     defined(_LIBUNWIND_TARGET_S390X)
281881ad6265SDimitry Andric template <typename A, typename R>
281981ad6265SDimitry Andric bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_s390x &) {
282081ad6265SDimitry Andric   // Look for the sigreturn trampoline. The trampoline's body is a
282181ad6265SDimitry Andric   // specific instruction (see below). Typically the trampoline comes from the
282281ad6265SDimitry Andric   // vDSO (i.e. the __kernel_[rt_]sigreturn function). A libc might provide its
282381ad6265SDimitry Andric   // own restorer function, though, or user-mode QEMU might write a trampoline
282481ad6265SDimitry Andric   // onto the stack.
282581ad6265SDimitry Andric   const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
2826fcaf7f86SDimitry Andric   // The PC might contain an invalid address if the unwind info is bad, so
28271db9f3b2SDimitry Andric   // directly accessing it could cause a SIGSEGV.
28281db9f3b2SDimitry Andric   if (!isReadableAddr(pc))
28291db9f3b2SDimitry Andric     return false;
28301db9f3b2SDimitry Andric   const auto inst = *reinterpret_cast<const uint16_t *>(pc);
28311db9f3b2SDimitry Andric   if (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 
29771db9f3b2SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
29781db9f3b2SDimitry Andric template <typename A, typename R>
29791db9f3b2SDimitry Andric bool UnwindCursor<A, R>::isReadableAddr(const pint_t addr) const {
29801db9f3b2SDimitry Andric   // We use SYS_rt_sigprocmask, inspired by Abseil's AddressIsReadable.
29811db9f3b2SDimitry Andric 
29821db9f3b2SDimitry Andric   const auto sigsetAddr = reinterpret_cast<sigset_t *>(addr);
29831db9f3b2SDimitry Andric   // We have to check that addr is nullptr because sigprocmask allows that
29841db9f3b2SDimitry Andric   // as an argument without failure.
29851db9f3b2SDimitry Andric   if (!sigsetAddr)
29861db9f3b2SDimitry Andric     return false;
29871db9f3b2SDimitry Andric   const auto saveErrno = errno;
29881db9f3b2SDimitry Andric   // We MUST use a raw syscall here, as wrappers may try to access
29891db9f3b2SDimitry Andric   // sigsetAddr which may cause a SIGSEGV. A raw syscall however is
29901db9f3b2SDimitry Andric   // safe. Additionally, we need to pass the kernel_sigset_size, which is
29911db9f3b2SDimitry Andric   // different from libc sizeof(sigset_t). For the majority of architectures,
29921db9f3b2SDimitry Andric   // it's 64 bits (_NSIG), and libc NSIG is _NSIG + 1.
29931db9f3b2SDimitry Andric   const auto kernelSigsetSize = NSIG / 8;
29941db9f3b2SDimitry Andric   [[maybe_unused]] const int Result = syscall(
29951db9f3b2SDimitry Andric       SYS_rt_sigprocmask, /*how=*/~0, sigsetAddr, nullptr, kernelSigsetSize);
29961db9f3b2SDimitry Andric   // Because our "how" is invalid, this syscall should always fail, and our
29971db9f3b2SDimitry Andric   // errno should always be EINVAL or an EFAULT. This relies on the Linux
29981db9f3b2SDimitry Andric   // kernel to check copy_from_user before checking if the "how" argument is
29991db9f3b2SDimitry Andric   // invalid.
30001db9f3b2SDimitry Andric   assert(Result == -1);
30011db9f3b2SDimitry Andric   assert(errno == EFAULT || errno == EINVAL);
30021db9f3b2SDimitry Andric   const auto readable = errno != EFAULT;
30031db9f3b2SDimitry Andric   errno = saveErrno;
30041db9f3b2SDimitry Andric   return readable;
30051db9f3b2SDimitry Andric }
30061db9f3b2SDimitry Andric #endif
30071db9f3b2SDimitry Andric 
3008*62987288SDimitry Andric #if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
3009349cc55cSDimitry Andric extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) {
3010349cc55cSDimitry Andric   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
3011349cc55cSDimitry Andric   return co->get_registers();
3012349cc55cSDimitry Andric }
3013349cc55cSDimitry Andric #endif
30140b57cec5SDimitry Andric } // namespace libunwind
30150b57cec5SDimitry Andric 
30160b57cec5SDimitry Andric #endif // __UNWINDCURSOR_HPP__
3017