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