xref: /freebsd/contrib/llvm-project/libunwind/src/UnwindCursor.hpp (revision fcaf7f8644a9988098ac6be2165bce3ea4786e91)
1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //
80b57cec5SDimitry Andric // C++ interface to lower levels of libunwind
90b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #ifndef __UNWINDCURSOR_HPP__
120b57cec5SDimitry Andric #define __UNWINDCURSOR_HPP__
130b57cec5SDimitry Andric 
14349cc55cSDimitry Andric #include "cet_unwind.h"
150b57cec5SDimitry Andric #include <stdint.h>
160b57cec5SDimitry Andric #include <stdio.h>
170b57cec5SDimitry Andric #include <stdlib.h>
180b57cec5SDimitry Andric #include <unwind.h>
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric #ifdef _WIN32
210b57cec5SDimitry Andric   #include <windows.h>
220b57cec5SDimitry Andric   #include <ntverp.h>
230b57cec5SDimitry Andric #endif
240b57cec5SDimitry Andric #ifdef __APPLE__
250b57cec5SDimitry Andric   #include <mach-o/dyld.h>
260b57cec5SDimitry Andric #endif
2781ad6265SDimitry Andric #ifdef _AIX
2881ad6265SDimitry Andric #include <dlfcn.h>
2981ad6265SDimitry Andric #include <sys/debug.h>
3081ad6265SDimitry Andric #include <sys/pseg.h>
3181ad6265SDimitry Andric #endif
3281ad6265SDimitry Andric 
3381ad6265SDimitry Andric #if defined(_LIBUNWIND_TARGET_LINUX) &&                                        \
3481ad6265SDimitry Andric     (defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_S390X))
3581ad6265SDimitry Andric #include <sys/syscall.h>
3681ad6265SDimitry Andric #include <sys/uio.h>
3781ad6265SDimitry Andric #include <unistd.h>
3881ad6265SDimitry Andric #define _LIBUNWIND_CHECK_LINUX_SIGRETURN 1
3981ad6265SDimitry Andric #endif
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
420b57cec5SDimitry Andric // Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and
430b57cec5SDimitry Andric // earlier) SDKs.
440b57cec5SDimitry Andric // MinGW-w64 has always provided this struct.
450b57cec5SDimitry Andric   #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \
460b57cec5SDimitry Andric       !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
470b57cec5SDimitry Andric struct _DISPATCHER_CONTEXT {
480b57cec5SDimitry Andric   ULONG64 ControlPc;
490b57cec5SDimitry Andric   ULONG64 ImageBase;
500b57cec5SDimitry Andric   PRUNTIME_FUNCTION FunctionEntry;
510b57cec5SDimitry Andric   ULONG64 EstablisherFrame;
520b57cec5SDimitry Andric   ULONG64 TargetIp;
530b57cec5SDimitry Andric   PCONTEXT ContextRecord;
540b57cec5SDimitry Andric   PEXCEPTION_ROUTINE LanguageHandler;
550b57cec5SDimitry Andric   PVOID HandlerData;
560b57cec5SDimitry Andric   PUNWIND_HISTORY_TABLE HistoryTable;
570b57cec5SDimitry Andric   ULONG ScopeIndex;
580b57cec5SDimitry Andric   ULONG Fill0;
590b57cec5SDimitry Andric };
600b57cec5SDimitry Andric   #endif
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric struct UNWIND_INFO {
630b57cec5SDimitry Andric   uint8_t Version : 3;
640b57cec5SDimitry Andric   uint8_t Flags : 5;
650b57cec5SDimitry Andric   uint8_t SizeOfProlog;
660b57cec5SDimitry Andric   uint8_t CountOfCodes;
670b57cec5SDimitry Andric   uint8_t FrameRegister : 4;
680b57cec5SDimitry Andric   uint8_t FrameOffset : 4;
690b57cec5SDimitry Andric   uint16_t UnwindCodes[2];
700b57cec5SDimitry Andric };
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
730b57cec5SDimitry Andric     int, _Unwind_Action, uint64_t, _Unwind_Exception *,
740b57cec5SDimitry Andric     struct _Unwind_Context *);
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric #endif
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric #include "config.h"
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric #include "AddressSpace.hpp"
810b57cec5SDimitry Andric #include "CompactUnwinder.hpp"
820b57cec5SDimitry Andric #include "config.h"
830b57cec5SDimitry Andric #include "DwarfInstructions.hpp"
840b57cec5SDimitry Andric #include "EHHeaderParser.hpp"
850b57cec5SDimitry Andric #include "libunwind.h"
860b57cec5SDimitry Andric #include "Registers.hpp"
870b57cec5SDimitry Andric #include "RWMutex.hpp"
880b57cec5SDimitry Andric #include "Unwind-EHABI.h"
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric namespace libunwind {
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
930b57cec5SDimitry Andric /// Cache of recently found FDEs.
940b57cec5SDimitry Andric template <typename A>
950b57cec5SDimitry Andric class _LIBUNWIND_HIDDEN DwarfFDECache {
960b57cec5SDimitry Andric   typedef typename A::pint_t pint_t;
970b57cec5SDimitry Andric public:
98e8d8bef9SDimitry Andric   static constexpr pint_t kSearchAll = static_cast<pint_t>(-1);
990b57cec5SDimitry Andric   static pint_t findFDE(pint_t mh, pint_t pc);
1000b57cec5SDimitry Andric   static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
1010b57cec5SDimitry Andric   static void removeAllIn(pint_t mh);
1020b57cec5SDimitry Andric   static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
1030b57cec5SDimitry Andric                                                unw_word_t ip_end,
1040b57cec5SDimitry Andric                                                unw_word_t fde, unw_word_t mh));
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric private:
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   struct entry {
1090b57cec5SDimitry Andric     pint_t mh;
1100b57cec5SDimitry Andric     pint_t ip_start;
1110b57cec5SDimitry Andric     pint_t ip_end;
1120b57cec5SDimitry Andric     pint_t fde;
1130b57cec5SDimitry Andric   };
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   // These fields are all static to avoid needing an initializer.
1160b57cec5SDimitry Andric   // There is only one instance of this class per process.
1170b57cec5SDimitry Andric   static RWMutex _lock;
1180b57cec5SDimitry Andric #ifdef __APPLE__
1190b57cec5SDimitry Andric   static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
1200b57cec5SDimitry Andric   static bool _registeredForDyldUnloads;
1210b57cec5SDimitry Andric #endif
1220b57cec5SDimitry Andric   static entry *_buffer;
1230b57cec5SDimitry Andric   static entry *_bufferUsed;
1240b57cec5SDimitry Andric   static entry *_bufferEnd;
1250b57cec5SDimitry Andric   static entry _initialBuffer[64];
1260b57cec5SDimitry Andric };
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric template <typename A>
1290b57cec5SDimitry Andric typename DwarfFDECache<A>::entry *
1300b57cec5SDimitry Andric DwarfFDECache<A>::_buffer = _initialBuffer;
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric template <typename A>
1330b57cec5SDimitry Andric typename DwarfFDECache<A>::entry *
1340b57cec5SDimitry Andric DwarfFDECache<A>::_bufferUsed = _initialBuffer;
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric template <typename A>
1370b57cec5SDimitry Andric typename DwarfFDECache<A>::entry *
1380b57cec5SDimitry Andric DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric template <typename A>
1410b57cec5SDimitry Andric typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric template <typename A>
1440b57cec5SDimitry Andric RWMutex DwarfFDECache<A>::_lock;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric #ifdef __APPLE__
1470b57cec5SDimitry Andric template <typename A>
1480b57cec5SDimitry Andric bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
1490b57cec5SDimitry Andric #endif
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric template <typename A>
1520b57cec5SDimitry Andric typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
1530b57cec5SDimitry Andric   pint_t result = 0;
1540b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
1550b57cec5SDimitry Andric   for (entry *p = _buffer; p < _bufferUsed; ++p) {
156e8d8bef9SDimitry Andric     if ((mh == p->mh) || (mh == kSearchAll)) {
1570b57cec5SDimitry Andric       if ((p->ip_start <= pc) && (pc < p->ip_end)) {
1580b57cec5SDimitry Andric         result = p->fde;
1590b57cec5SDimitry Andric         break;
1600b57cec5SDimitry Andric       }
1610b57cec5SDimitry Andric     }
1620b57cec5SDimitry Andric   }
1630b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
1640b57cec5SDimitry Andric   return result;
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric template <typename A>
1680b57cec5SDimitry Andric void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
1690b57cec5SDimitry Andric                            pint_t fde) {
1700b57cec5SDimitry Andric #if !defined(_LIBUNWIND_NO_HEAP)
1710b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
1720b57cec5SDimitry Andric   if (_bufferUsed >= _bufferEnd) {
1730b57cec5SDimitry Andric     size_t oldSize = (size_t)(_bufferEnd - _buffer);
1740b57cec5SDimitry Andric     size_t newSize = oldSize * 4;
1750b57cec5SDimitry Andric     // Can't use operator new (we are below it).
1760b57cec5SDimitry Andric     entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
1770b57cec5SDimitry Andric     memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
1780b57cec5SDimitry Andric     if (_buffer != _initialBuffer)
1790b57cec5SDimitry Andric       free(_buffer);
1800b57cec5SDimitry Andric     _buffer = newBuffer;
1810b57cec5SDimitry Andric     _bufferUsed = &newBuffer[oldSize];
1820b57cec5SDimitry Andric     _bufferEnd = &newBuffer[newSize];
1830b57cec5SDimitry Andric   }
1840b57cec5SDimitry Andric   _bufferUsed->mh = mh;
1850b57cec5SDimitry Andric   _bufferUsed->ip_start = ip_start;
1860b57cec5SDimitry Andric   _bufferUsed->ip_end = ip_end;
1870b57cec5SDimitry Andric   _bufferUsed->fde = fde;
1880b57cec5SDimitry Andric   ++_bufferUsed;
1890b57cec5SDimitry Andric #ifdef __APPLE__
1900b57cec5SDimitry Andric   if (!_registeredForDyldUnloads) {
1910b57cec5SDimitry Andric     _dyld_register_func_for_remove_image(&dyldUnloadHook);
1920b57cec5SDimitry Andric     _registeredForDyldUnloads = true;
1930b57cec5SDimitry Andric   }
1940b57cec5SDimitry Andric #endif
1950b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
1960b57cec5SDimitry Andric #endif
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric template <typename A>
2000b57cec5SDimitry Andric void DwarfFDECache<A>::removeAllIn(pint_t mh) {
2010b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
2020b57cec5SDimitry Andric   entry *d = _buffer;
2030b57cec5SDimitry Andric   for (const entry *s = _buffer; s < _bufferUsed; ++s) {
2040b57cec5SDimitry Andric     if (s->mh != mh) {
2050b57cec5SDimitry Andric       if (d != s)
2060b57cec5SDimitry Andric         *d = *s;
2070b57cec5SDimitry Andric       ++d;
2080b57cec5SDimitry Andric     }
2090b57cec5SDimitry Andric   }
2100b57cec5SDimitry Andric   _bufferUsed = d;
2110b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric #ifdef __APPLE__
2150b57cec5SDimitry Andric template <typename A>
2160b57cec5SDimitry Andric void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
2170b57cec5SDimitry Andric   removeAllIn((pint_t) mh);
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric #endif
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric template <typename A>
2220b57cec5SDimitry Andric void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
2230b57cec5SDimitry Andric     unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
2240b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
2250b57cec5SDimitry Andric   for (entry *p = _buffer; p < _bufferUsed; ++p) {
2260b57cec5SDimitry Andric     (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
2270b57cec5SDimitry Andric   }
2280b57cec5SDimitry Andric   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric #define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
2360b57cec5SDimitry Andric template <typename A> class UnwindSectionHeader {
2370b57cec5SDimitry Andric public:
2380b57cec5SDimitry Andric   UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
2390b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric   uint32_t version() const {
2420b57cec5SDimitry Andric     return _addressSpace.get32(_addr +
2430b57cec5SDimitry Andric                                offsetof(unwind_info_section_header, version));
2440b57cec5SDimitry Andric   }
2450b57cec5SDimitry Andric   uint32_t commonEncodingsArraySectionOffset() const {
2460b57cec5SDimitry Andric     return _addressSpace.get32(_addr +
2470b57cec5SDimitry Andric                                offsetof(unwind_info_section_header,
2480b57cec5SDimitry Andric                                         commonEncodingsArraySectionOffset));
2490b57cec5SDimitry Andric   }
2500b57cec5SDimitry Andric   uint32_t commonEncodingsArrayCount() const {
2510b57cec5SDimitry Andric     return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
2520b57cec5SDimitry Andric                                                 commonEncodingsArrayCount));
2530b57cec5SDimitry Andric   }
2540b57cec5SDimitry Andric   uint32_t personalityArraySectionOffset() const {
2550b57cec5SDimitry Andric     return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
2560b57cec5SDimitry Andric                                                 personalityArraySectionOffset));
2570b57cec5SDimitry Andric   }
2580b57cec5SDimitry Andric   uint32_t personalityArrayCount() const {
2590b57cec5SDimitry Andric     return _addressSpace.get32(
2600b57cec5SDimitry Andric         _addr + offsetof(unwind_info_section_header, personalityArrayCount));
2610b57cec5SDimitry Andric   }
2620b57cec5SDimitry Andric   uint32_t indexSectionOffset() const {
2630b57cec5SDimitry Andric     return _addressSpace.get32(
2640b57cec5SDimitry Andric         _addr + offsetof(unwind_info_section_header, indexSectionOffset));
2650b57cec5SDimitry Andric   }
2660b57cec5SDimitry Andric   uint32_t indexCount() const {
2670b57cec5SDimitry Andric     return _addressSpace.get32(
2680b57cec5SDimitry Andric         _addr + offsetof(unwind_info_section_header, indexCount));
2690b57cec5SDimitry Andric   }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric private:
2720b57cec5SDimitry Andric   A                     &_addressSpace;
2730b57cec5SDimitry Andric   typename A::pint_t     _addr;
2740b57cec5SDimitry Andric };
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric template <typename A> class UnwindSectionIndexArray {
2770b57cec5SDimitry Andric public:
2780b57cec5SDimitry Andric   UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
2790b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
2820b57cec5SDimitry Andric     return _addressSpace.get32(
2830b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
2840b57cec5SDimitry Andric                               functionOffset));
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric   uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
2870b57cec5SDimitry Andric     return _addressSpace.get32(
2880b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
2890b57cec5SDimitry Andric                               secondLevelPagesSectionOffset));
2900b57cec5SDimitry Andric   }
2910b57cec5SDimitry Andric   uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
2920b57cec5SDimitry Andric     return _addressSpace.get32(
2930b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
2940b57cec5SDimitry Andric                               lsdaIndexArraySectionOffset));
2950b57cec5SDimitry Andric   }
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric private:
2980b57cec5SDimitry Andric   A                   &_addressSpace;
2990b57cec5SDimitry Andric   typename A::pint_t   _addr;
3000b57cec5SDimitry Andric };
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric template <typename A> class UnwindSectionRegularPageHeader {
3030b57cec5SDimitry Andric public:
3040b57cec5SDimitry Andric   UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
3050b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric   uint32_t kind() const {
3080b57cec5SDimitry Andric     return _addressSpace.get32(
3090b57cec5SDimitry Andric         _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
3100b57cec5SDimitry Andric   }
3110b57cec5SDimitry Andric   uint16_t entryPageOffset() const {
3120b57cec5SDimitry Andric     return _addressSpace.get16(
3130b57cec5SDimitry Andric         _addr + offsetof(unwind_info_regular_second_level_page_header,
3140b57cec5SDimitry Andric                          entryPageOffset));
3150b57cec5SDimitry Andric   }
3160b57cec5SDimitry Andric   uint16_t entryCount() const {
3170b57cec5SDimitry Andric     return _addressSpace.get16(
3180b57cec5SDimitry Andric         _addr +
3190b57cec5SDimitry Andric         offsetof(unwind_info_regular_second_level_page_header, entryCount));
3200b57cec5SDimitry Andric   }
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric private:
3230b57cec5SDimitry Andric   A &_addressSpace;
3240b57cec5SDimitry Andric   typename A::pint_t _addr;
3250b57cec5SDimitry Andric };
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric template <typename A> class UnwindSectionRegularArray {
3280b57cec5SDimitry Andric public:
3290b57cec5SDimitry Andric   UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
3300b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
3330b57cec5SDimitry Andric     return _addressSpace.get32(
3340b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
3350b57cec5SDimitry Andric                               functionOffset));
3360b57cec5SDimitry Andric   }
3370b57cec5SDimitry Andric   uint32_t encoding(uint32_t index) const {
3380b57cec5SDimitry Andric     return _addressSpace.get32(
3390b57cec5SDimitry Andric         _addr +
3400b57cec5SDimitry Andric         arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
3410b57cec5SDimitry Andric   }
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric private:
3440b57cec5SDimitry Andric   A &_addressSpace;
3450b57cec5SDimitry Andric   typename A::pint_t _addr;
3460b57cec5SDimitry Andric };
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric template <typename A> class UnwindSectionCompressedPageHeader {
3490b57cec5SDimitry Andric public:
3500b57cec5SDimitry Andric   UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
3510b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric   uint32_t kind() const {
3540b57cec5SDimitry Andric     return _addressSpace.get32(
3550b57cec5SDimitry Andric         _addr +
3560b57cec5SDimitry Andric         offsetof(unwind_info_compressed_second_level_page_header, kind));
3570b57cec5SDimitry Andric   }
3580b57cec5SDimitry Andric   uint16_t entryPageOffset() const {
3590b57cec5SDimitry Andric     return _addressSpace.get16(
3600b57cec5SDimitry Andric         _addr + offsetof(unwind_info_compressed_second_level_page_header,
3610b57cec5SDimitry Andric                          entryPageOffset));
3620b57cec5SDimitry Andric   }
3630b57cec5SDimitry Andric   uint16_t entryCount() const {
3640b57cec5SDimitry Andric     return _addressSpace.get16(
3650b57cec5SDimitry Andric         _addr +
3660b57cec5SDimitry Andric         offsetof(unwind_info_compressed_second_level_page_header, entryCount));
3670b57cec5SDimitry Andric   }
3680b57cec5SDimitry Andric   uint16_t encodingsPageOffset() const {
3690b57cec5SDimitry Andric     return _addressSpace.get16(
3700b57cec5SDimitry Andric         _addr + offsetof(unwind_info_compressed_second_level_page_header,
3710b57cec5SDimitry Andric                          encodingsPageOffset));
3720b57cec5SDimitry Andric   }
3730b57cec5SDimitry Andric   uint16_t encodingsCount() const {
3740b57cec5SDimitry Andric     return _addressSpace.get16(
3750b57cec5SDimitry Andric         _addr + offsetof(unwind_info_compressed_second_level_page_header,
3760b57cec5SDimitry Andric                          encodingsCount));
3770b57cec5SDimitry Andric   }
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric private:
3800b57cec5SDimitry Andric   A &_addressSpace;
3810b57cec5SDimitry Andric   typename A::pint_t _addr;
3820b57cec5SDimitry Andric };
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric template <typename A> class UnwindSectionCompressedArray {
3850b57cec5SDimitry Andric public:
3860b57cec5SDimitry Andric   UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
3870b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
3900b57cec5SDimitry Andric     return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
3910b57cec5SDimitry Andric         _addressSpace.get32(_addr + index * sizeof(uint32_t)));
3920b57cec5SDimitry Andric   }
3930b57cec5SDimitry Andric   uint16_t encodingIndex(uint32_t index) const {
3940b57cec5SDimitry Andric     return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
3950b57cec5SDimitry Andric         _addressSpace.get32(_addr + index * sizeof(uint32_t)));
3960b57cec5SDimitry Andric   }
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric private:
3990b57cec5SDimitry Andric   A &_addressSpace;
4000b57cec5SDimitry Andric   typename A::pint_t _addr;
4010b57cec5SDimitry Andric };
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric template <typename A> class UnwindSectionLsdaArray {
4040b57cec5SDimitry Andric public:
4050b57cec5SDimitry Andric   UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
4060b57cec5SDimitry Andric       : _addressSpace(addressSpace), _addr(addr) {}
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   uint32_t functionOffset(uint32_t index) const {
4090b57cec5SDimitry Andric     return _addressSpace.get32(
4100b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
4110b57cec5SDimitry Andric                               index, functionOffset));
4120b57cec5SDimitry Andric   }
4130b57cec5SDimitry Andric   uint32_t lsdaOffset(uint32_t index) const {
4140b57cec5SDimitry Andric     return _addressSpace.get32(
4150b57cec5SDimitry Andric         _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
4160b57cec5SDimitry Andric                               index, lsdaOffset));
4170b57cec5SDimitry Andric   }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric private:
4200b57cec5SDimitry Andric   A                   &_addressSpace;
4210b57cec5SDimitry Andric   typename A::pint_t   _addr;
4220b57cec5SDimitry Andric };
4230b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
4260b57cec5SDimitry Andric public:
4270b57cec5SDimitry Andric   // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
4280b57cec5SDimitry Andric   // This avoids an unnecessary dependency to libc++abi.
4290b57cec5SDimitry Andric   void operator delete(void *, size_t) {}
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   virtual ~AbstractUnwindCursor() {}
4320b57cec5SDimitry Andric   virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
4330b57cec5SDimitry Andric   virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
4340b57cec5SDimitry Andric   virtual void setReg(int, unw_word_t) {
4350b57cec5SDimitry Andric     _LIBUNWIND_ABORT("setReg not implemented");
4360b57cec5SDimitry Andric   }
4370b57cec5SDimitry Andric   virtual bool validFloatReg(int) {
4380b57cec5SDimitry Andric     _LIBUNWIND_ABORT("validFloatReg not implemented");
4390b57cec5SDimitry Andric   }
4400b57cec5SDimitry Andric   virtual unw_fpreg_t getFloatReg(int) {
4410b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getFloatReg not implemented");
4420b57cec5SDimitry Andric   }
4430b57cec5SDimitry Andric   virtual void setFloatReg(int, unw_fpreg_t) {
4440b57cec5SDimitry Andric     _LIBUNWIND_ABORT("setFloatReg not implemented");
4450b57cec5SDimitry Andric   }
4460b57cec5SDimitry Andric   virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
4470b57cec5SDimitry Andric   virtual void getInfo(unw_proc_info_t *) {
4480b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getInfo not implemented");
4490b57cec5SDimitry Andric   }
4500b57cec5SDimitry Andric   virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
4510b57cec5SDimitry Andric   virtual bool isSignalFrame() {
4520b57cec5SDimitry Andric     _LIBUNWIND_ABORT("isSignalFrame not implemented");
4530b57cec5SDimitry Andric   }
4540b57cec5SDimitry Andric   virtual bool getFunctionName(char *, size_t, unw_word_t *) {
4550b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getFunctionName not implemented");
4560b57cec5SDimitry Andric   }
4570b57cec5SDimitry Andric   virtual void setInfoBasedOnIPRegister(bool = false) {
4580b57cec5SDimitry Andric     _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
4590b57cec5SDimitry Andric   }
4600b57cec5SDimitry Andric   virtual const char *getRegisterName(int) {
4610b57cec5SDimitry Andric     _LIBUNWIND_ABORT("getRegisterName not implemented");
4620b57cec5SDimitry Andric   }
4630b57cec5SDimitry Andric #ifdef __arm__
4640b57cec5SDimitry Andric   virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
4650b57cec5SDimitry Andric #endif
466349cc55cSDimitry Andric 
46781ad6265SDimitry Andric #ifdef _AIX
46881ad6265SDimitry Andric   virtual uintptr_t getDataRelBase() {
46981ad6265SDimitry Andric     _LIBUNWIND_ABORT("getDataRelBase not implemented");
47081ad6265SDimitry Andric   }
47181ad6265SDimitry Andric #endif
47281ad6265SDimitry Andric 
473349cc55cSDimitry Andric #if defined(_LIBUNWIND_USE_CET)
474349cc55cSDimitry Andric   virtual void *get_registers() {
475349cc55cSDimitry Andric     _LIBUNWIND_ABORT("get_registers not implemented");
476349cc55cSDimitry Andric   }
477349cc55cSDimitry Andric #endif
4780b57cec5SDimitry Andric };
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric /// \c UnwindCursor contains all state (including all register values) during
4830b57cec5SDimitry Andric /// an unwind.  This is normally stack-allocated inside a unw_cursor_t.
4840b57cec5SDimitry Andric template <typename A, typename R>
4850b57cec5SDimitry Andric class UnwindCursor : public AbstractUnwindCursor {
4860b57cec5SDimitry Andric   typedef typename A::pint_t pint_t;
4870b57cec5SDimitry Andric public:
4880b57cec5SDimitry Andric                       UnwindCursor(unw_context_t *context, A &as);
4890b57cec5SDimitry Andric                       UnwindCursor(CONTEXT *context, A &as);
4900b57cec5SDimitry Andric                       UnwindCursor(A &as, void *threadArg);
4910b57cec5SDimitry Andric   virtual             ~UnwindCursor() {}
4920b57cec5SDimitry Andric   virtual bool        validReg(int);
4930b57cec5SDimitry Andric   virtual unw_word_t  getReg(int);
4940b57cec5SDimitry Andric   virtual void        setReg(int, unw_word_t);
4950b57cec5SDimitry Andric   virtual bool        validFloatReg(int);
4960b57cec5SDimitry Andric   virtual unw_fpreg_t getFloatReg(int);
4970b57cec5SDimitry Andric   virtual void        setFloatReg(int, unw_fpreg_t);
4980b57cec5SDimitry Andric   virtual int         step();
4990b57cec5SDimitry Andric   virtual void        getInfo(unw_proc_info_t *);
5000b57cec5SDimitry Andric   virtual void        jumpto();
5010b57cec5SDimitry Andric   virtual bool        isSignalFrame();
5020b57cec5SDimitry Andric   virtual bool        getFunctionName(char *buf, size_t len, unw_word_t *off);
5030b57cec5SDimitry Andric   virtual void        setInfoBasedOnIPRegister(bool isReturnAddress = false);
5040b57cec5SDimitry Andric   virtual const char *getRegisterName(int num);
5050b57cec5SDimitry Andric #ifdef __arm__
5060b57cec5SDimitry Andric   virtual void        saveVFPAsX();
5070b57cec5SDimitry Andric #endif
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric   DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
5100b57cec5SDimitry Andric   void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; }
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric   // libunwind does not and should not depend on C++ library which means that we
5130b57cec5SDimitry Andric   // need our own defition of inline placement new.
5140b57cec5SDimitry Andric   static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric private:
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric   pint_t getLastPC() const { return _dispContext.ControlPc; }
5190b57cec5SDimitry Andric   void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
5200b57cec5SDimitry Andric   RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
52181ad6265SDimitry Andric #ifdef __arm__
52281ad6265SDimitry Andric     // Remove the thumb bit; FunctionEntry ranges don't include the thumb bit.
52381ad6265SDimitry Andric     pc &= ~1U;
52481ad6265SDimitry Andric #endif
52581ad6265SDimitry Andric     // If pc points exactly at the end of the range, we might resolve the
52681ad6265SDimitry Andric     // next function instead. Decrement pc by 1 to fit inside the current
52781ad6265SDimitry Andric     // function.
52881ad6265SDimitry Andric     pc -= 1;
5290b57cec5SDimitry Andric     _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
5300b57cec5SDimitry Andric                                                         &_dispContext.ImageBase,
5310b57cec5SDimitry Andric                                                         _dispContext.HistoryTable);
5320b57cec5SDimitry Andric     *base = _dispContext.ImageBase;
5330b57cec5SDimitry Andric     return _dispContext.FunctionEntry;
5340b57cec5SDimitry Andric   }
5350b57cec5SDimitry Andric   bool getInfoFromSEH(pint_t pc);
5360b57cec5SDimitry Andric   int stepWithSEHData() {
5370b57cec5SDimitry Andric     _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
5380b57cec5SDimitry Andric                                                     _dispContext.ImageBase,
5390b57cec5SDimitry Andric                                                     _dispContext.ControlPc,
5400b57cec5SDimitry Andric                                                     _dispContext.FunctionEntry,
5410b57cec5SDimitry Andric                                                     _dispContext.ContextRecord,
5420b57cec5SDimitry Andric                                                     &_dispContext.HandlerData,
5430b57cec5SDimitry Andric                                                     &_dispContext.EstablisherFrame,
5440b57cec5SDimitry Andric                                                     NULL);
5450b57cec5SDimitry Andric     // Update some fields of the unwind info now, since we have them.
5460b57cec5SDimitry Andric     _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
5470b57cec5SDimitry Andric     if (_dispContext.LanguageHandler) {
5480b57cec5SDimitry Andric       _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
5490b57cec5SDimitry Andric     } else
5500b57cec5SDimitry Andric       _info.handler = 0;
5510b57cec5SDimitry Andric     return UNW_STEP_SUCCESS;
5520b57cec5SDimitry Andric   }
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric   A                   &_addressSpace;
5550b57cec5SDimitry Andric   unw_proc_info_t      _info;
5560b57cec5SDimitry Andric   DISPATCHER_CONTEXT   _dispContext;
5570b57cec5SDimitry Andric   CONTEXT              _msContext;
5580b57cec5SDimitry Andric   UNWIND_HISTORY_TABLE _histTable;
5590b57cec5SDimitry Andric   bool                 _unwindInfoMissing;
5600b57cec5SDimitry Andric };
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric template <typename A, typename R>
5640b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
5650b57cec5SDimitry Andric     : _addressSpace(as), _unwindInfoMissing(false) {
5660b57cec5SDimitry Andric   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
5670b57cec5SDimitry Andric                 "UnwindCursor<> does not fit in unw_cursor_t");
568e8d8bef9SDimitry Andric   static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
569e8d8bef9SDimitry Andric                 "UnwindCursor<> requires more alignment than unw_cursor_t");
5700b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
5710b57cec5SDimitry Andric   memset(&_histTable, 0, sizeof(_histTable));
5720b57cec5SDimitry Andric   _dispContext.ContextRecord = &_msContext;
5730b57cec5SDimitry Andric   _dispContext.HistoryTable = &_histTable;
5740b57cec5SDimitry Andric   // Initialize MS context from ours.
5750b57cec5SDimitry Andric   R r(context);
5760b57cec5SDimitry Andric   _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
5770b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
5780b57cec5SDimitry Andric   _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
5790b57cec5SDimitry Andric   _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
5800b57cec5SDimitry Andric   _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
5810b57cec5SDimitry Andric   _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
5820b57cec5SDimitry Andric   _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
5830b57cec5SDimitry Andric   _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
5840b57cec5SDimitry Andric   _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
5850b57cec5SDimitry Andric   _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
5860b57cec5SDimitry Andric   _msContext.R8 = r.getRegister(UNW_X86_64_R8);
5870b57cec5SDimitry Andric   _msContext.R9 = r.getRegister(UNW_X86_64_R9);
5880b57cec5SDimitry Andric   _msContext.R10 = r.getRegister(UNW_X86_64_R10);
5890b57cec5SDimitry Andric   _msContext.R11 = r.getRegister(UNW_X86_64_R11);
5900b57cec5SDimitry Andric   _msContext.R12 = r.getRegister(UNW_X86_64_R12);
5910b57cec5SDimitry Andric   _msContext.R13 = r.getRegister(UNW_X86_64_R13);
5920b57cec5SDimitry Andric   _msContext.R14 = r.getRegister(UNW_X86_64_R14);
5930b57cec5SDimitry Andric   _msContext.R15 = r.getRegister(UNW_X86_64_R15);
5940b57cec5SDimitry Andric   _msContext.Rip = r.getRegister(UNW_REG_IP);
5950b57cec5SDimitry Andric   union {
5960b57cec5SDimitry Andric     v128 v;
5970b57cec5SDimitry Andric     M128A m;
5980b57cec5SDimitry Andric   } t;
5990b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM0);
6000b57cec5SDimitry Andric   _msContext.Xmm0 = t.m;
6010b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM1);
6020b57cec5SDimitry Andric   _msContext.Xmm1 = t.m;
6030b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM2);
6040b57cec5SDimitry Andric   _msContext.Xmm2 = t.m;
6050b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM3);
6060b57cec5SDimitry Andric   _msContext.Xmm3 = t.m;
6070b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM4);
6080b57cec5SDimitry Andric   _msContext.Xmm4 = t.m;
6090b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM5);
6100b57cec5SDimitry Andric   _msContext.Xmm5 = t.m;
6110b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM6);
6120b57cec5SDimitry Andric   _msContext.Xmm6 = t.m;
6130b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM7);
6140b57cec5SDimitry Andric   _msContext.Xmm7 = t.m;
6150b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM8);
6160b57cec5SDimitry Andric   _msContext.Xmm8 = t.m;
6170b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM9);
6180b57cec5SDimitry Andric   _msContext.Xmm9 = t.m;
6190b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM10);
6200b57cec5SDimitry Andric   _msContext.Xmm10 = t.m;
6210b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM11);
6220b57cec5SDimitry Andric   _msContext.Xmm11 = t.m;
6230b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM12);
6240b57cec5SDimitry Andric   _msContext.Xmm12 = t.m;
6250b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM13);
6260b57cec5SDimitry Andric   _msContext.Xmm13 = t.m;
6270b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM14);
6280b57cec5SDimitry Andric   _msContext.Xmm14 = t.m;
6290b57cec5SDimitry Andric   t.v = r.getVectorRegister(UNW_X86_64_XMM15);
6300b57cec5SDimitry Andric   _msContext.Xmm15 = t.m;
6310b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
6320b57cec5SDimitry Andric   _msContext.R0 = r.getRegister(UNW_ARM_R0);
6330b57cec5SDimitry Andric   _msContext.R1 = r.getRegister(UNW_ARM_R1);
6340b57cec5SDimitry Andric   _msContext.R2 = r.getRegister(UNW_ARM_R2);
6350b57cec5SDimitry Andric   _msContext.R3 = r.getRegister(UNW_ARM_R3);
6360b57cec5SDimitry Andric   _msContext.R4 = r.getRegister(UNW_ARM_R4);
6370b57cec5SDimitry Andric   _msContext.R5 = r.getRegister(UNW_ARM_R5);
6380b57cec5SDimitry Andric   _msContext.R6 = r.getRegister(UNW_ARM_R6);
6390b57cec5SDimitry Andric   _msContext.R7 = r.getRegister(UNW_ARM_R7);
6400b57cec5SDimitry Andric   _msContext.R8 = r.getRegister(UNW_ARM_R8);
6410b57cec5SDimitry Andric   _msContext.R9 = r.getRegister(UNW_ARM_R9);
6420b57cec5SDimitry Andric   _msContext.R10 = r.getRegister(UNW_ARM_R10);
6430b57cec5SDimitry Andric   _msContext.R11 = r.getRegister(UNW_ARM_R11);
6440b57cec5SDimitry Andric   _msContext.R12 = r.getRegister(UNW_ARM_R12);
6450b57cec5SDimitry Andric   _msContext.Sp = r.getRegister(UNW_ARM_SP);
6460b57cec5SDimitry Andric   _msContext.Lr = r.getRegister(UNW_ARM_LR);
6470b57cec5SDimitry Andric   _msContext.Pc = r.getRegister(UNW_ARM_IP);
6480b57cec5SDimitry Andric   for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
6490b57cec5SDimitry Andric     union {
6500b57cec5SDimitry Andric       uint64_t w;
6510b57cec5SDimitry Andric       double d;
6520b57cec5SDimitry Andric     } d;
6530b57cec5SDimitry Andric     d.d = r.getFloatRegister(i);
6540b57cec5SDimitry Andric     _msContext.D[i - UNW_ARM_D0] = d.w;
6550b57cec5SDimitry Andric   }
6560b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
657349cc55cSDimitry Andric   for (int i = UNW_AARCH64_X0; i <= UNW_ARM64_X30; ++i)
658349cc55cSDimitry Andric     _msContext.X[i - UNW_AARCH64_X0] = r.getRegister(i);
6590b57cec5SDimitry Andric   _msContext.Sp = r.getRegister(UNW_REG_SP);
6600b57cec5SDimitry Andric   _msContext.Pc = r.getRegister(UNW_REG_IP);
661349cc55cSDimitry Andric   for (int i = UNW_AARCH64_V0; i <= UNW_ARM64_D31; ++i)
662349cc55cSDimitry Andric     _msContext.V[i - UNW_AARCH64_V0].D[0] = r.getFloatRegister(i);
6630b57cec5SDimitry Andric #endif
6640b57cec5SDimitry Andric }
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric template <typename A, typename R>
6670b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
6680b57cec5SDimitry Andric     : _addressSpace(as), _unwindInfoMissing(false) {
6690b57cec5SDimitry Andric   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
6700b57cec5SDimitry Andric                 "UnwindCursor<> does not fit in unw_cursor_t");
6710b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
6720b57cec5SDimitry Andric   memset(&_histTable, 0, sizeof(_histTable));
6730b57cec5SDimitry Andric   _dispContext.ContextRecord = &_msContext;
6740b57cec5SDimitry Andric   _dispContext.HistoryTable = &_histTable;
6750b57cec5SDimitry Andric   _msContext = *context;
6760b57cec5SDimitry Andric }
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric template <typename A, typename R>
6800b57cec5SDimitry Andric bool UnwindCursor<A, R>::validReg(int regNum) {
6810b57cec5SDimitry Andric   if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
6820b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
6830b57cec5SDimitry Andric   if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
6840b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
6850eae32dcSDimitry Andric   if ((regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) ||
6860eae32dcSDimitry Andric       regNum == UNW_ARM_RA_AUTH_CODE)
6870eae32dcSDimitry Andric     return true;
6880b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
689349cc55cSDimitry Andric   if (regNum >= UNW_AARCH64_X0 && regNum <= UNW_ARM64_X30) return true;
6900b57cec5SDimitry Andric #endif
6910b57cec5SDimitry Andric   return false;
6920b57cec5SDimitry Andric }
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric template <typename A, typename R>
6950b57cec5SDimitry Andric unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
6960b57cec5SDimitry Andric   switch (regNum) {
6970b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
6980b57cec5SDimitry Andric   case UNW_REG_IP: return _msContext.Rip;
6990b57cec5SDimitry Andric   case UNW_X86_64_RAX: return _msContext.Rax;
7000b57cec5SDimitry Andric   case UNW_X86_64_RDX: return _msContext.Rdx;
7010b57cec5SDimitry Andric   case UNW_X86_64_RCX: return _msContext.Rcx;
7020b57cec5SDimitry Andric   case UNW_X86_64_RBX: return _msContext.Rbx;
7030b57cec5SDimitry Andric   case UNW_REG_SP:
7040b57cec5SDimitry Andric   case UNW_X86_64_RSP: return _msContext.Rsp;
7050b57cec5SDimitry Andric   case UNW_X86_64_RBP: return _msContext.Rbp;
7060b57cec5SDimitry Andric   case UNW_X86_64_RSI: return _msContext.Rsi;
7070b57cec5SDimitry Andric   case UNW_X86_64_RDI: return _msContext.Rdi;
7080b57cec5SDimitry Andric   case UNW_X86_64_R8: return _msContext.R8;
7090b57cec5SDimitry Andric   case UNW_X86_64_R9: return _msContext.R9;
7100b57cec5SDimitry Andric   case UNW_X86_64_R10: return _msContext.R10;
7110b57cec5SDimitry Andric   case UNW_X86_64_R11: return _msContext.R11;
7120b57cec5SDimitry Andric   case UNW_X86_64_R12: return _msContext.R12;
7130b57cec5SDimitry Andric   case UNW_X86_64_R13: return _msContext.R13;
7140b57cec5SDimitry Andric   case UNW_X86_64_R14: return _msContext.R14;
7150b57cec5SDimitry Andric   case UNW_X86_64_R15: return _msContext.R15;
7160b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
7170b57cec5SDimitry Andric   case UNW_ARM_R0: return _msContext.R0;
7180b57cec5SDimitry Andric   case UNW_ARM_R1: return _msContext.R1;
7190b57cec5SDimitry Andric   case UNW_ARM_R2: return _msContext.R2;
7200b57cec5SDimitry Andric   case UNW_ARM_R3: return _msContext.R3;
7210b57cec5SDimitry Andric   case UNW_ARM_R4: return _msContext.R4;
7220b57cec5SDimitry Andric   case UNW_ARM_R5: return _msContext.R5;
7230b57cec5SDimitry Andric   case UNW_ARM_R6: return _msContext.R6;
7240b57cec5SDimitry Andric   case UNW_ARM_R7: return _msContext.R7;
7250b57cec5SDimitry Andric   case UNW_ARM_R8: return _msContext.R8;
7260b57cec5SDimitry Andric   case UNW_ARM_R9: return _msContext.R9;
7270b57cec5SDimitry Andric   case UNW_ARM_R10: return _msContext.R10;
7280b57cec5SDimitry Andric   case UNW_ARM_R11: return _msContext.R11;
7290b57cec5SDimitry Andric   case UNW_ARM_R12: return _msContext.R12;
7300b57cec5SDimitry Andric   case UNW_REG_SP:
7310b57cec5SDimitry Andric   case UNW_ARM_SP: return _msContext.Sp;
7320b57cec5SDimitry Andric   case UNW_ARM_LR: return _msContext.Lr;
7330b57cec5SDimitry Andric   case UNW_REG_IP:
7340b57cec5SDimitry Andric   case UNW_ARM_IP: return _msContext.Pc;
7350b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
7360b57cec5SDimitry Andric   case UNW_REG_SP: return _msContext.Sp;
7370b57cec5SDimitry Andric   case UNW_REG_IP: return _msContext.Pc;
738349cc55cSDimitry Andric   default: return _msContext.X[regNum - UNW_AARCH64_X0];
7390b57cec5SDimitry Andric #endif
7400b57cec5SDimitry Andric   }
7410b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported register");
7420b57cec5SDimitry Andric }
7430b57cec5SDimitry Andric 
7440b57cec5SDimitry Andric template <typename A, typename R>
7450b57cec5SDimitry Andric void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
7460b57cec5SDimitry Andric   switch (regNum) {
7470b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
7480b57cec5SDimitry Andric   case UNW_REG_IP: _msContext.Rip = value; break;
7490b57cec5SDimitry Andric   case UNW_X86_64_RAX: _msContext.Rax = value; break;
7500b57cec5SDimitry Andric   case UNW_X86_64_RDX: _msContext.Rdx = value; break;
7510b57cec5SDimitry Andric   case UNW_X86_64_RCX: _msContext.Rcx = value; break;
7520b57cec5SDimitry Andric   case UNW_X86_64_RBX: _msContext.Rbx = value; break;
7530b57cec5SDimitry Andric   case UNW_REG_SP:
7540b57cec5SDimitry Andric   case UNW_X86_64_RSP: _msContext.Rsp = value; break;
7550b57cec5SDimitry Andric   case UNW_X86_64_RBP: _msContext.Rbp = value; break;
7560b57cec5SDimitry Andric   case UNW_X86_64_RSI: _msContext.Rsi = value; break;
7570b57cec5SDimitry Andric   case UNW_X86_64_RDI: _msContext.Rdi = value; break;
7580b57cec5SDimitry Andric   case UNW_X86_64_R8: _msContext.R8 = value; break;
7590b57cec5SDimitry Andric   case UNW_X86_64_R9: _msContext.R9 = value; break;
7600b57cec5SDimitry Andric   case UNW_X86_64_R10: _msContext.R10 = value; break;
7610b57cec5SDimitry Andric   case UNW_X86_64_R11: _msContext.R11 = value; break;
7620b57cec5SDimitry Andric   case UNW_X86_64_R12: _msContext.R12 = value; break;
7630b57cec5SDimitry Andric   case UNW_X86_64_R13: _msContext.R13 = value; break;
7640b57cec5SDimitry Andric   case UNW_X86_64_R14: _msContext.R14 = value; break;
7650b57cec5SDimitry Andric   case UNW_X86_64_R15: _msContext.R15 = value; break;
7660b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM)
7670b57cec5SDimitry Andric   case UNW_ARM_R0: _msContext.R0 = value; break;
7680b57cec5SDimitry Andric   case UNW_ARM_R1: _msContext.R1 = value; break;
7690b57cec5SDimitry Andric   case UNW_ARM_R2: _msContext.R2 = value; break;
7700b57cec5SDimitry Andric   case UNW_ARM_R3: _msContext.R3 = value; break;
7710b57cec5SDimitry Andric   case UNW_ARM_R4: _msContext.R4 = value; break;
7720b57cec5SDimitry Andric   case UNW_ARM_R5: _msContext.R5 = value; break;
7730b57cec5SDimitry Andric   case UNW_ARM_R6: _msContext.R6 = value; break;
7740b57cec5SDimitry Andric   case UNW_ARM_R7: _msContext.R7 = value; break;
7750b57cec5SDimitry Andric   case UNW_ARM_R8: _msContext.R8 = value; break;
7760b57cec5SDimitry Andric   case UNW_ARM_R9: _msContext.R9 = value; break;
7770b57cec5SDimitry Andric   case UNW_ARM_R10: _msContext.R10 = value; break;
7780b57cec5SDimitry Andric   case UNW_ARM_R11: _msContext.R11 = value; break;
7790b57cec5SDimitry Andric   case UNW_ARM_R12: _msContext.R12 = value; break;
7800b57cec5SDimitry Andric   case UNW_REG_SP:
7810b57cec5SDimitry Andric   case UNW_ARM_SP: _msContext.Sp = value; break;
7820b57cec5SDimitry Andric   case UNW_ARM_LR: _msContext.Lr = value; break;
7830b57cec5SDimitry Andric   case UNW_REG_IP:
7840b57cec5SDimitry Andric   case UNW_ARM_IP: _msContext.Pc = value; break;
7850b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
7860b57cec5SDimitry Andric   case UNW_REG_SP: _msContext.Sp = value; break;
7870b57cec5SDimitry Andric   case UNW_REG_IP: _msContext.Pc = value; break;
788349cc55cSDimitry Andric   case UNW_AARCH64_X0:
789349cc55cSDimitry Andric   case UNW_AARCH64_X1:
790349cc55cSDimitry Andric   case UNW_AARCH64_X2:
791349cc55cSDimitry Andric   case UNW_AARCH64_X3:
792349cc55cSDimitry Andric   case UNW_AARCH64_X4:
793349cc55cSDimitry Andric   case UNW_AARCH64_X5:
794349cc55cSDimitry Andric   case UNW_AARCH64_X6:
795349cc55cSDimitry Andric   case UNW_AARCH64_X7:
796349cc55cSDimitry Andric   case UNW_AARCH64_X8:
797349cc55cSDimitry Andric   case UNW_AARCH64_X9:
798349cc55cSDimitry Andric   case UNW_AARCH64_X10:
799349cc55cSDimitry Andric   case UNW_AARCH64_X11:
800349cc55cSDimitry Andric   case UNW_AARCH64_X12:
801349cc55cSDimitry Andric   case UNW_AARCH64_X13:
802349cc55cSDimitry Andric   case UNW_AARCH64_X14:
803349cc55cSDimitry Andric   case UNW_AARCH64_X15:
804349cc55cSDimitry Andric   case UNW_AARCH64_X16:
805349cc55cSDimitry Andric   case UNW_AARCH64_X17:
806349cc55cSDimitry Andric   case UNW_AARCH64_X18:
807349cc55cSDimitry Andric   case UNW_AARCH64_X19:
808349cc55cSDimitry Andric   case UNW_AARCH64_X20:
809349cc55cSDimitry Andric   case UNW_AARCH64_X21:
810349cc55cSDimitry Andric   case UNW_AARCH64_X22:
811349cc55cSDimitry Andric   case UNW_AARCH64_X23:
812349cc55cSDimitry Andric   case UNW_AARCH64_X24:
813349cc55cSDimitry Andric   case UNW_AARCH64_X25:
814349cc55cSDimitry Andric   case UNW_AARCH64_X26:
815349cc55cSDimitry Andric   case UNW_AARCH64_X27:
816349cc55cSDimitry Andric   case UNW_AARCH64_X28:
817349cc55cSDimitry Andric   case UNW_AARCH64_FP:
818349cc55cSDimitry Andric   case UNW_AARCH64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
8190b57cec5SDimitry Andric #endif
8200b57cec5SDimitry Andric   default:
8210b57cec5SDimitry Andric     _LIBUNWIND_ABORT("unsupported register");
8220b57cec5SDimitry Andric   }
8230b57cec5SDimitry Andric }
8240b57cec5SDimitry Andric 
8250b57cec5SDimitry Andric template <typename A, typename R>
8260b57cec5SDimitry Andric bool UnwindCursor<A, R>::validFloatReg(int regNum) {
8270b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
8280b57cec5SDimitry Andric   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
8290b57cec5SDimitry Andric   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
8300b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
831349cc55cSDimitry Andric   if (regNum >= UNW_AARCH64_V0 && regNum <= UNW_ARM64_D31) return true;
8320b57cec5SDimitry Andric #else
8330b57cec5SDimitry Andric   (void)regNum;
8340b57cec5SDimitry Andric #endif
8350b57cec5SDimitry Andric   return false;
8360b57cec5SDimitry Andric }
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric template <typename A, typename R>
8390b57cec5SDimitry Andric unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
8400b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
8410b57cec5SDimitry Andric   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
8420b57cec5SDimitry Andric     union {
8430b57cec5SDimitry Andric       uint32_t w;
8440b57cec5SDimitry Andric       float f;
8450b57cec5SDimitry Andric     } d;
8460b57cec5SDimitry Andric     d.w = _msContext.S[regNum - UNW_ARM_S0];
8470b57cec5SDimitry Andric     return d.f;
8480b57cec5SDimitry Andric   }
8490b57cec5SDimitry Andric   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
8500b57cec5SDimitry Andric     union {
8510b57cec5SDimitry Andric       uint64_t w;
8520b57cec5SDimitry Andric       double d;
8530b57cec5SDimitry Andric     } d;
8540b57cec5SDimitry Andric     d.w = _msContext.D[regNum - UNW_ARM_D0];
8550b57cec5SDimitry Andric     return d.d;
8560b57cec5SDimitry Andric   }
8570b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported float register");
8580b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
859349cc55cSDimitry Andric   return _msContext.V[regNum - UNW_AARCH64_V0].D[0];
8600b57cec5SDimitry Andric #else
8610b57cec5SDimitry Andric   (void)regNum;
8620b57cec5SDimitry Andric   _LIBUNWIND_ABORT("float registers unimplemented");
8630b57cec5SDimitry Andric #endif
8640b57cec5SDimitry Andric }
8650b57cec5SDimitry Andric 
8660b57cec5SDimitry Andric template <typename A, typename R>
8670b57cec5SDimitry Andric void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
8680b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
8690b57cec5SDimitry Andric   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
8700b57cec5SDimitry Andric     union {
8710b57cec5SDimitry Andric       uint32_t w;
8720b57cec5SDimitry Andric       float f;
8730b57cec5SDimitry Andric     } d;
87481ad6265SDimitry Andric     d.f = (float)value;
8750b57cec5SDimitry Andric     _msContext.S[regNum - UNW_ARM_S0] = d.w;
8760b57cec5SDimitry Andric   }
8770b57cec5SDimitry Andric   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
8780b57cec5SDimitry Andric     union {
8790b57cec5SDimitry Andric       uint64_t w;
8800b57cec5SDimitry Andric       double d;
8810b57cec5SDimitry Andric     } d;
8820b57cec5SDimitry Andric     d.d = value;
8830b57cec5SDimitry Andric     _msContext.D[regNum - UNW_ARM_D0] = d.w;
8840b57cec5SDimitry Andric   }
8850b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported float register");
8860b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64)
887349cc55cSDimitry Andric   _msContext.V[regNum - UNW_AARCH64_V0].D[0] = value;
8880b57cec5SDimitry Andric #else
8890b57cec5SDimitry Andric   (void)regNum;
8900b57cec5SDimitry Andric   (void)value;
8910b57cec5SDimitry Andric   _LIBUNWIND_ABORT("float registers unimplemented");
8920b57cec5SDimitry Andric #endif
8930b57cec5SDimitry Andric }
8940b57cec5SDimitry Andric 
8950b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
8960b57cec5SDimitry Andric   RtlRestoreContext(&_msContext, nullptr);
8970b57cec5SDimitry Andric }
8980b57cec5SDimitry Andric 
8990b57cec5SDimitry Andric #ifdef __arm__
9000b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
9010b57cec5SDimitry Andric #endif
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric template <typename A, typename R>
9040b57cec5SDimitry Andric const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
9050b57cec5SDimitry Andric   return R::getRegisterName(regNum);
9060b57cec5SDimitry Andric }
9070b57cec5SDimitry Andric 
9080b57cec5SDimitry Andric template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
9090b57cec5SDimitry Andric   return false;
9100b57cec5SDimitry Andric }
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric #else  // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
9130b57cec5SDimitry Andric 
9140b57cec5SDimitry Andric /// UnwindCursor contains all state (including all register values) during
9150b57cec5SDimitry Andric /// an unwind.  This is normally stack allocated inside a unw_cursor_t.
9160b57cec5SDimitry Andric template <typename A, typename R>
9170b57cec5SDimitry Andric class UnwindCursor : public AbstractUnwindCursor{
9180b57cec5SDimitry Andric   typedef typename A::pint_t pint_t;
9190b57cec5SDimitry Andric public:
9200b57cec5SDimitry Andric                       UnwindCursor(unw_context_t *context, A &as);
9210b57cec5SDimitry Andric                       UnwindCursor(A &as, void *threadArg);
9220b57cec5SDimitry Andric   virtual             ~UnwindCursor() {}
9230b57cec5SDimitry Andric   virtual bool        validReg(int);
9240b57cec5SDimitry Andric   virtual unw_word_t  getReg(int);
9250b57cec5SDimitry Andric   virtual void        setReg(int, unw_word_t);
9260b57cec5SDimitry Andric   virtual bool        validFloatReg(int);
9270b57cec5SDimitry Andric   virtual unw_fpreg_t getFloatReg(int);
9280b57cec5SDimitry Andric   virtual void        setFloatReg(int, unw_fpreg_t);
9290b57cec5SDimitry Andric   virtual int         step();
9300b57cec5SDimitry Andric   virtual void        getInfo(unw_proc_info_t *);
9310b57cec5SDimitry Andric   virtual void        jumpto();
9320b57cec5SDimitry Andric   virtual bool        isSignalFrame();
9330b57cec5SDimitry Andric   virtual bool        getFunctionName(char *buf, size_t len, unw_word_t *off);
9340b57cec5SDimitry Andric   virtual void        setInfoBasedOnIPRegister(bool isReturnAddress = false);
9350b57cec5SDimitry Andric   virtual const char *getRegisterName(int num);
9360b57cec5SDimitry Andric #ifdef __arm__
9370b57cec5SDimitry Andric   virtual void        saveVFPAsX();
9380b57cec5SDimitry Andric #endif
9390b57cec5SDimitry Andric 
94081ad6265SDimitry Andric #ifdef _AIX
94181ad6265SDimitry Andric   virtual uintptr_t getDataRelBase();
94281ad6265SDimitry Andric #endif
94381ad6265SDimitry Andric 
944349cc55cSDimitry Andric #if defined(_LIBUNWIND_USE_CET)
945349cc55cSDimitry Andric   virtual void *get_registers() { return &_registers; }
946349cc55cSDimitry Andric #endif
94781ad6265SDimitry Andric 
9480b57cec5SDimitry Andric   // libunwind does not and should not depend on C++ library which means that we
9490b57cec5SDimitry Andric   // need our own defition of inline placement new.
9500b57cec5SDimitry Andric   static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric private:
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
9550b57cec5SDimitry Andric   bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
9560b57cec5SDimitry Andric 
9570b57cec5SDimitry Andric   int stepWithEHABI() {
9580b57cec5SDimitry Andric     size_t len = 0;
9590b57cec5SDimitry Andric     size_t off = 0;
9600b57cec5SDimitry Andric     // FIXME: Calling decode_eht_entry() here is violating the libunwind
9610b57cec5SDimitry Andric     // abstraction layer.
9620b57cec5SDimitry Andric     const uint32_t *ehtp =
9630b57cec5SDimitry Andric         decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
9640b57cec5SDimitry Andric                          &off, &len);
9650b57cec5SDimitry Andric     if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
9660b57cec5SDimitry Andric             _URC_CONTINUE_UNWIND)
9670b57cec5SDimitry Andric       return UNW_STEP_END;
9680b57cec5SDimitry Andric     return UNW_STEP_SUCCESS;
9690b57cec5SDimitry Andric   }
9700b57cec5SDimitry Andric #endif
9710b57cec5SDimitry Andric 
97281ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
973e8d8bef9SDimitry Andric   bool setInfoForSigReturn() {
974e8d8bef9SDimitry Andric     R dummy;
975e8d8bef9SDimitry Andric     return setInfoForSigReturn(dummy);
976e8d8bef9SDimitry Andric   }
977e8d8bef9SDimitry Andric   int stepThroughSigReturn() {
978e8d8bef9SDimitry Andric     R dummy;
979e8d8bef9SDimitry Andric     return stepThroughSigReturn(dummy);
980e8d8bef9SDimitry Andric   }
98181ad6265SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
982e8d8bef9SDimitry Andric   bool setInfoForSigReturn(Registers_arm64 &);
983e8d8bef9SDimitry Andric   int stepThroughSigReturn(Registers_arm64 &);
98481ad6265SDimitry Andric #endif
98581ad6265SDimitry Andric #if defined(_LIBUNWIND_TARGET_S390X)
98681ad6265SDimitry Andric   bool setInfoForSigReturn(Registers_s390x &);
98781ad6265SDimitry Andric   int stepThroughSigReturn(Registers_s390x &);
98881ad6265SDimitry Andric #endif
989e8d8bef9SDimitry Andric   template <typename Registers> bool setInfoForSigReturn(Registers &) {
990e8d8bef9SDimitry Andric     return false;
991e8d8bef9SDimitry Andric   }
992e8d8bef9SDimitry Andric   template <typename Registers> int stepThroughSigReturn(Registers &) {
993e8d8bef9SDimitry Andric     return UNW_STEP_END;
994e8d8bef9SDimitry Andric   }
995e8d8bef9SDimitry Andric #endif
996e8d8bef9SDimitry Andric 
9970b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
998e8d8bef9SDimitry Andric   bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo,
999e8d8bef9SDimitry Andric                          const typename CFI_Parser<A>::CIE_Info &cieInfo,
1000e8d8bef9SDimitry Andric                          pint_t pc, uintptr_t dso_base);
10010b57cec5SDimitry Andric   bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
10020b57cec5SDimitry Andric                                             uint32_t fdeSectionOffsetHint=0);
10030b57cec5SDimitry Andric   int stepWithDwarfFDE() {
10040b57cec5SDimitry Andric     return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
10050b57cec5SDimitry Andric                                               (pint_t)this->getReg(UNW_REG_IP),
10060b57cec5SDimitry Andric                                               (pint_t)_info.unwind_info,
1007480093f4SDimitry Andric                                               _registers, _isSignalFrame);
10080b57cec5SDimitry Andric   }
10090b57cec5SDimitry Andric #endif
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
10120b57cec5SDimitry Andric   bool getInfoFromCompactEncodingSection(pint_t pc,
10130b57cec5SDimitry Andric                                             const UnwindInfoSections &sects);
10140b57cec5SDimitry Andric   int stepWithCompactEncoding() {
10150b57cec5SDimitry Andric   #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
10160b57cec5SDimitry Andric     if ( compactSaysUseDwarf() )
10170b57cec5SDimitry Andric       return stepWithDwarfFDE();
10180b57cec5SDimitry Andric   #endif
10190b57cec5SDimitry Andric     R dummy;
10200b57cec5SDimitry Andric     return stepWithCompactEncoding(dummy);
10210b57cec5SDimitry Andric   }
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
10240b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_x86_64 &) {
10250b57cec5SDimitry Andric     return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
10260b57cec5SDimitry Andric         _info.format, _info.start_ip, _addressSpace, _registers);
10270b57cec5SDimitry Andric   }
10280b57cec5SDimitry Andric #endif
10290b57cec5SDimitry Andric 
10300b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386)
10310b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_x86 &) {
10320b57cec5SDimitry Andric     return CompactUnwinder_x86<A>::stepWithCompactEncoding(
10330b57cec5SDimitry Andric         _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
10340b57cec5SDimitry Andric   }
10350b57cec5SDimitry Andric #endif
10360b57cec5SDimitry Andric 
10370b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC)
10380b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_ppc &) {
10390b57cec5SDimitry Andric     return UNW_EINVAL;
10400b57cec5SDimitry Andric   }
10410b57cec5SDimitry Andric #endif
10420b57cec5SDimitry Andric 
10430b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64)
10440b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_ppc64 &) {
10450b57cec5SDimitry Andric     return UNW_EINVAL;
10460b57cec5SDimitry Andric   }
10470b57cec5SDimitry Andric #endif
10480b57cec5SDimitry Andric 
10490b57cec5SDimitry Andric 
10500b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
10510b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_arm64 &) {
10520b57cec5SDimitry Andric     return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
10530b57cec5SDimitry Andric         _info.format, _info.start_ip, _addressSpace, _registers);
10540b57cec5SDimitry Andric   }
10550b57cec5SDimitry Andric #endif
10560b57cec5SDimitry Andric 
10570b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_O32)
10580b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_mips_o32 &) {
10590b57cec5SDimitry Andric     return UNW_EINVAL;
10600b57cec5SDimitry Andric   }
10610b57cec5SDimitry Andric #endif
10620b57cec5SDimitry Andric 
10630b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
10640b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_mips_newabi &) {
10650b57cec5SDimitry Andric     return UNW_EINVAL;
10660b57cec5SDimitry Andric   }
10670b57cec5SDimitry Andric #endif
10680b57cec5SDimitry Andric 
10690b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC)
10700b57cec5SDimitry Andric   int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
10710b57cec5SDimitry Andric #endif
10720b57cec5SDimitry Andric 
1073d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64)
1074d56accc7SDimitry Andric   int stepWithCompactEncoding(Registers_sparc64 &) { return UNW_EINVAL; }
1075d56accc7SDimitry Andric #endif
1076d56accc7SDimitry Andric 
1077480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV)
1078480093f4SDimitry Andric   int stepWithCompactEncoding(Registers_riscv &) {
1079480093f4SDimitry Andric     return UNW_EINVAL;
1080480093f4SDimitry Andric   }
1081480093f4SDimitry Andric #endif
1082480093f4SDimitry Andric 
10830b57cec5SDimitry Andric   bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
10840b57cec5SDimitry Andric     R dummy;
10850b57cec5SDimitry Andric     return compactSaysUseDwarf(dummy, offset);
10860b57cec5SDimitry Andric   }
10870b57cec5SDimitry Andric 
10880b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
10890b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
10900b57cec5SDimitry Andric     if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
10910b57cec5SDimitry Andric       if (offset)
10920b57cec5SDimitry Andric         *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
10930b57cec5SDimitry Andric       return true;
10940b57cec5SDimitry Andric     }
10950b57cec5SDimitry Andric     return false;
10960b57cec5SDimitry Andric   }
10970b57cec5SDimitry Andric #endif
10980b57cec5SDimitry Andric 
10990b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386)
11000b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
11010b57cec5SDimitry Andric     if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
11020b57cec5SDimitry Andric       if (offset)
11030b57cec5SDimitry Andric         *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
11040b57cec5SDimitry Andric       return true;
11050b57cec5SDimitry Andric     }
11060b57cec5SDimitry Andric     return false;
11070b57cec5SDimitry Andric   }
11080b57cec5SDimitry Andric #endif
11090b57cec5SDimitry Andric 
11100b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC)
11110b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
11120b57cec5SDimitry Andric     return true;
11130b57cec5SDimitry Andric   }
11140b57cec5SDimitry Andric #endif
11150b57cec5SDimitry Andric 
11160b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64)
11170b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
11180b57cec5SDimitry Andric     return true;
11190b57cec5SDimitry Andric   }
11200b57cec5SDimitry Andric #endif
11210b57cec5SDimitry Andric 
11220b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
11230b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
11240b57cec5SDimitry Andric     if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
11250b57cec5SDimitry Andric       if (offset)
11260b57cec5SDimitry Andric         *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
11270b57cec5SDimitry Andric       return true;
11280b57cec5SDimitry Andric     }
11290b57cec5SDimitry Andric     return false;
11300b57cec5SDimitry Andric   }
11310b57cec5SDimitry Andric #endif
11320b57cec5SDimitry Andric 
11330b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_O32)
11340b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
11350b57cec5SDimitry Andric     return true;
11360b57cec5SDimitry Andric   }
11370b57cec5SDimitry Andric #endif
11380b57cec5SDimitry Andric 
11390b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
11400b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
11410b57cec5SDimitry Andric     return true;
11420b57cec5SDimitry Andric   }
11430b57cec5SDimitry Andric #endif
11440b57cec5SDimitry Andric 
11450b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC)
11460b57cec5SDimitry Andric   bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
11470b57cec5SDimitry Andric #endif
11480b57cec5SDimitry Andric 
1149d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64)
1150d56accc7SDimitry Andric   bool compactSaysUseDwarf(Registers_sparc64 &, uint32_t *) const {
1151d56accc7SDimitry Andric     return true;
1152d56accc7SDimitry Andric   }
1153d56accc7SDimitry Andric #endif
1154d56accc7SDimitry Andric 
1155480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV)
1156480093f4SDimitry Andric   bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const {
1157480093f4SDimitry Andric     return true;
1158480093f4SDimitry Andric   }
1159480093f4SDimitry Andric #endif
1160480093f4SDimitry Andric 
11610b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
11620b57cec5SDimitry Andric 
11630b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
11640b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding() const {
11650b57cec5SDimitry Andric     R dummy;
11660b57cec5SDimitry Andric     return dwarfEncoding(dummy);
11670b57cec5SDimitry Andric   }
11680b57cec5SDimitry Andric 
11690b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64)
11700b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
11710b57cec5SDimitry Andric     return UNWIND_X86_64_MODE_DWARF;
11720b57cec5SDimitry Andric   }
11730b57cec5SDimitry Andric #endif
11740b57cec5SDimitry Andric 
11750b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386)
11760b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
11770b57cec5SDimitry Andric     return UNWIND_X86_MODE_DWARF;
11780b57cec5SDimitry Andric   }
11790b57cec5SDimitry Andric #endif
11800b57cec5SDimitry Andric 
11810b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC)
11820b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
11830b57cec5SDimitry Andric     return 0;
11840b57cec5SDimitry Andric   }
11850b57cec5SDimitry Andric #endif
11860b57cec5SDimitry Andric 
11870b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64)
11880b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
11890b57cec5SDimitry Andric     return 0;
11900b57cec5SDimitry Andric   }
11910b57cec5SDimitry Andric #endif
11920b57cec5SDimitry Andric 
11930b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64)
11940b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
11950b57cec5SDimitry Andric     return UNWIND_ARM64_MODE_DWARF;
11960b57cec5SDimitry Andric   }
11970b57cec5SDimitry Andric #endif
11980b57cec5SDimitry Andric 
11990b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM)
12000b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
12010b57cec5SDimitry Andric     return 0;
12020b57cec5SDimitry Andric   }
12030b57cec5SDimitry Andric #endif
12040b57cec5SDimitry Andric 
12050b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_OR1K)
12060b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
12070b57cec5SDimitry Andric     return 0;
12080b57cec5SDimitry Andric   }
12090b57cec5SDimitry Andric #endif
12100b57cec5SDimitry Andric 
12115ffd83dbSDimitry Andric #if defined (_LIBUNWIND_TARGET_HEXAGON)
12125ffd83dbSDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const {
12135ffd83dbSDimitry Andric     return 0;
12145ffd83dbSDimitry Andric   }
12155ffd83dbSDimitry Andric #endif
12165ffd83dbSDimitry Andric 
12170b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_MIPS_O32)
12180b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
12190b57cec5SDimitry Andric     return 0;
12200b57cec5SDimitry Andric   }
12210b57cec5SDimitry Andric #endif
12220b57cec5SDimitry Andric 
12230b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
12240b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
12250b57cec5SDimitry Andric     return 0;
12260b57cec5SDimitry Andric   }
12270b57cec5SDimitry Andric #endif
12280b57cec5SDimitry Andric 
12290b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC)
12300b57cec5SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
12310b57cec5SDimitry Andric #endif
12320b57cec5SDimitry Andric 
1233d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64)
1234d56accc7SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_sparc64 &) const {
1235d56accc7SDimitry Andric     return 0;
1236d56accc7SDimitry Andric   }
1237d56accc7SDimitry Andric #endif
1238d56accc7SDimitry Andric 
1239480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV)
1240480093f4SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
1241480093f4SDimitry Andric     return 0;
1242480093f4SDimitry Andric   }
1243480093f4SDimitry Andric #endif
1244480093f4SDimitry Andric 
124581ad6265SDimitry Andric #if defined (_LIBUNWIND_TARGET_S390X)
124681ad6265SDimitry Andric   compact_unwind_encoding_t dwarfEncoding(Registers_s390x &) const {
124781ad6265SDimitry Andric     return 0;
124881ad6265SDimitry Andric   }
124981ad6265SDimitry Andric #endif
125081ad6265SDimitry Andric 
12510b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
12520b57cec5SDimitry Andric 
12530b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
12540b57cec5SDimitry Andric   // For runtime environments using SEH unwind data without Windows runtime
12550b57cec5SDimitry Andric   // support.
12560b57cec5SDimitry Andric   pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
12570b57cec5SDimitry Andric   void setLastPC(pint_t pc) { /* FIXME: Implement */ }
12580b57cec5SDimitry Andric   RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
12590b57cec5SDimitry Andric     /* FIXME: Implement */
12600b57cec5SDimitry Andric     *base = 0;
12610b57cec5SDimitry Andric     return nullptr;
12620b57cec5SDimitry Andric   }
12630b57cec5SDimitry Andric   bool getInfoFromSEH(pint_t pc);
12640b57cec5SDimitry Andric   int stepWithSEHData() { /* FIXME: Implement */ return 0; }
12650b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
12660b57cec5SDimitry Andric 
126781ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
126881ad6265SDimitry Andric   bool getInfoFromTBTable(pint_t pc, R &registers);
126981ad6265SDimitry Andric   int stepWithTBTable(pint_t pc, tbtable *TBTable, R &registers,
127081ad6265SDimitry Andric                       bool &isSignalFrame);
127181ad6265SDimitry Andric   int stepWithTBTableData() {
127281ad6265SDimitry Andric     return stepWithTBTable(reinterpret_cast<pint_t>(this->getReg(UNW_REG_IP)),
127381ad6265SDimitry Andric                            reinterpret_cast<tbtable *>(_info.unwind_info),
127481ad6265SDimitry Andric                            _registers, _isSignalFrame);
127581ad6265SDimitry Andric   }
127681ad6265SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
12770b57cec5SDimitry Andric 
12780b57cec5SDimitry Andric   A               &_addressSpace;
12790b57cec5SDimitry Andric   R                _registers;
12800b57cec5SDimitry Andric   unw_proc_info_t  _info;
12810b57cec5SDimitry Andric   bool             _unwindInfoMissing;
12820b57cec5SDimitry Andric   bool             _isSignalFrame;
128381ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
1284e8d8bef9SDimitry Andric   bool             _isSigReturn = false;
1285e8d8bef9SDimitry Andric #endif
12860b57cec5SDimitry Andric };
12870b57cec5SDimitry Andric 
12880b57cec5SDimitry Andric 
12890b57cec5SDimitry Andric template <typename A, typename R>
12900b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
12910b57cec5SDimitry Andric     : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
12920b57cec5SDimitry Andric       _isSignalFrame(false) {
12930b57cec5SDimitry Andric   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
12940b57cec5SDimitry Andric                 "UnwindCursor<> does not fit in unw_cursor_t");
1295e8d8bef9SDimitry Andric   static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1296e8d8bef9SDimitry Andric                 "UnwindCursor<> requires more alignment than unw_cursor_t");
12970b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
12980b57cec5SDimitry Andric }
12990b57cec5SDimitry Andric 
13000b57cec5SDimitry Andric template <typename A, typename R>
13010b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(A &as, void *)
13020b57cec5SDimitry Andric     : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
13030b57cec5SDimitry Andric   memset(&_info, 0, sizeof(_info));
13040b57cec5SDimitry Andric   // FIXME
13050b57cec5SDimitry Andric   // fill in _registers from thread arg
13060b57cec5SDimitry Andric }
13070b57cec5SDimitry Andric 
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric template <typename A, typename R>
13100b57cec5SDimitry Andric bool UnwindCursor<A, R>::validReg(int regNum) {
13110b57cec5SDimitry Andric   return _registers.validRegister(regNum);
13120b57cec5SDimitry Andric }
13130b57cec5SDimitry Andric 
13140b57cec5SDimitry Andric template <typename A, typename R>
13150b57cec5SDimitry Andric unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
13160b57cec5SDimitry Andric   return _registers.getRegister(regNum);
13170b57cec5SDimitry Andric }
13180b57cec5SDimitry Andric 
13190b57cec5SDimitry Andric template <typename A, typename R>
13200b57cec5SDimitry Andric void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
13210b57cec5SDimitry Andric   _registers.setRegister(regNum, (typename A::pint_t)value);
13220b57cec5SDimitry Andric }
13230b57cec5SDimitry Andric 
13240b57cec5SDimitry Andric template <typename A, typename R>
13250b57cec5SDimitry Andric bool UnwindCursor<A, R>::validFloatReg(int regNum) {
13260b57cec5SDimitry Andric   return _registers.validFloatRegister(regNum);
13270b57cec5SDimitry Andric }
13280b57cec5SDimitry Andric 
13290b57cec5SDimitry Andric template <typename A, typename R>
13300b57cec5SDimitry Andric unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
13310b57cec5SDimitry Andric   return _registers.getFloatRegister(regNum);
13320b57cec5SDimitry Andric }
13330b57cec5SDimitry Andric 
13340b57cec5SDimitry Andric template <typename A, typename R>
13350b57cec5SDimitry Andric void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
13360b57cec5SDimitry Andric   _registers.setFloatRegister(regNum, value);
13370b57cec5SDimitry Andric }
13380b57cec5SDimitry Andric 
13390b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
13400b57cec5SDimitry Andric   _registers.jumpto();
13410b57cec5SDimitry Andric }
13420b57cec5SDimitry Andric 
13430b57cec5SDimitry Andric #ifdef __arm__
13440b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
13450b57cec5SDimitry Andric   _registers.saveVFPAsX();
13460b57cec5SDimitry Andric }
13470b57cec5SDimitry Andric #endif
13480b57cec5SDimitry Andric 
134981ad6265SDimitry Andric #ifdef _AIX
135081ad6265SDimitry Andric template <typename A, typename R>
135181ad6265SDimitry Andric uintptr_t UnwindCursor<A, R>::getDataRelBase() {
135281ad6265SDimitry Andric   return reinterpret_cast<uintptr_t>(_info.extra);
135381ad6265SDimitry Andric }
135481ad6265SDimitry Andric #endif
135581ad6265SDimitry Andric 
13560b57cec5SDimitry Andric template <typename A, typename R>
13570b57cec5SDimitry Andric const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
13580b57cec5SDimitry Andric   return _registers.getRegisterName(regNum);
13590b57cec5SDimitry Andric }
13600b57cec5SDimitry Andric 
13610b57cec5SDimitry Andric template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
13620b57cec5SDimitry Andric   return _isSignalFrame;
13630b57cec5SDimitry Andric }
13640b57cec5SDimitry Andric 
13650b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
13660b57cec5SDimitry Andric 
13670b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
13680b57cec5SDimitry Andric template<typename A>
13690b57cec5SDimitry Andric struct EHABISectionIterator {
13700b57cec5SDimitry Andric   typedef EHABISectionIterator _Self;
13710b57cec5SDimitry Andric 
13720b57cec5SDimitry Andric   typedef typename A::pint_t value_type;
13730b57cec5SDimitry Andric   typedef typename A::pint_t* pointer;
13740b57cec5SDimitry Andric   typedef typename A::pint_t& reference;
13750b57cec5SDimitry Andric   typedef size_t size_type;
13760b57cec5SDimitry Andric   typedef size_t difference_type;
13770b57cec5SDimitry Andric 
13780b57cec5SDimitry Andric   static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
13790b57cec5SDimitry Andric     return _Self(addressSpace, sects, 0);
13800b57cec5SDimitry Andric   }
13810b57cec5SDimitry Andric   static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
13820b57cec5SDimitry Andric     return _Self(addressSpace, sects,
13830b57cec5SDimitry Andric                  sects.arm_section_length / sizeof(EHABIIndexEntry));
13840b57cec5SDimitry Andric   }
13850b57cec5SDimitry Andric 
13860b57cec5SDimitry Andric   EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
13870b57cec5SDimitry Andric       : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
13880b57cec5SDimitry Andric 
13890b57cec5SDimitry Andric   _Self& operator++() { ++_i; return *this; }
13900b57cec5SDimitry Andric   _Self& operator+=(size_t a) { _i += a; return *this; }
13910b57cec5SDimitry Andric   _Self& operator--() { assert(_i > 0); --_i; return *this; }
13920b57cec5SDimitry Andric   _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
13930b57cec5SDimitry Andric 
13940b57cec5SDimitry Andric   _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
13950b57cec5SDimitry Andric   _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
13960b57cec5SDimitry Andric 
13975ffd83dbSDimitry Andric   size_t operator-(const _Self& other) const { return _i - other._i; }
13980b57cec5SDimitry Andric 
13990b57cec5SDimitry Andric   bool operator==(const _Self& other) const {
14000b57cec5SDimitry Andric     assert(_addressSpace == other._addressSpace);
14010b57cec5SDimitry Andric     assert(_sects == other._sects);
14020b57cec5SDimitry Andric     return _i == other._i;
14030b57cec5SDimitry Andric   }
14040b57cec5SDimitry Andric 
14055ffd83dbSDimitry Andric   bool operator!=(const _Self& other) const {
14065ffd83dbSDimitry Andric     assert(_addressSpace == other._addressSpace);
14075ffd83dbSDimitry Andric     assert(_sects == other._sects);
14085ffd83dbSDimitry Andric     return _i != other._i;
14095ffd83dbSDimitry Andric   }
14105ffd83dbSDimitry Andric 
14110b57cec5SDimitry Andric   typename A::pint_t operator*() const { return functionAddress(); }
14120b57cec5SDimitry Andric 
14130b57cec5SDimitry Andric   typename A::pint_t functionAddress() const {
14140b57cec5SDimitry Andric     typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
14150b57cec5SDimitry Andric         EHABIIndexEntry, _i, functionOffset);
14160b57cec5SDimitry Andric     return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
14170b57cec5SDimitry Andric   }
14180b57cec5SDimitry Andric 
14190b57cec5SDimitry Andric   typename A::pint_t dataAddress() {
14200b57cec5SDimitry Andric     typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
14210b57cec5SDimitry Andric         EHABIIndexEntry, _i, data);
14220b57cec5SDimitry Andric     return indexAddr;
14230b57cec5SDimitry Andric   }
14240b57cec5SDimitry Andric 
14250b57cec5SDimitry Andric  private:
14260b57cec5SDimitry Andric   size_t _i;
14270b57cec5SDimitry Andric   A* _addressSpace;
14280b57cec5SDimitry Andric   const UnwindInfoSections* _sects;
14290b57cec5SDimitry Andric };
14300b57cec5SDimitry Andric 
14310b57cec5SDimitry Andric namespace {
14320b57cec5SDimitry Andric 
14330b57cec5SDimitry Andric template <typename A>
14340b57cec5SDimitry Andric EHABISectionIterator<A> EHABISectionUpperBound(
14350b57cec5SDimitry Andric     EHABISectionIterator<A> first,
14360b57cec5SDimitry Andric     EHABISectionIterator<A> last,
14370b57cec5SDimitry Andric     typename A::pint_t value) {
14380b57cec5SDimitry Andric   size_t len = last - first;
14390b57cec5SDimitry Andric   while (len > 0) {
14400b57cec5SDimitry Andric     size_t l2 = len / 2;
14410b57cec5SDimitry Andric     EHABISectionIterator<A> m = first + l2;
14420b57cec5SDimitry Andric     if (value < *m) {
14430b57cec5SDimitry Andric         len = l2;
14440b57cec5SDimitry Andric     } else {
14450b57cec5SDimitry Andric         first = ++m;
14460b57cec5SDimitry Andric         len -= l2 + 1;
14470b57cec5SDimitry Andric     }
14480b57cec5SDimitry Andric   }
14490b57cec5SDimitry Andric   return first;
14500b57cec5SDimitry Andric }
14510b57cec5SDimitry Andric 
14520b57cec5SDimitry Andric }
14530b57cec5SDimitry Andric 
14540b57cec5SDimitry Andric template <typename A, typename R>
14550b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromEHABISection(
14560b57cec5SDimitry Andric     pint_t pc,
14570b57cec5SDimitry Andric     const UnwindInfoSections &sects) {
14580b57cec5SDimitry Andric   EHABISectionIterator<A> begin =
14590b57cec5SDimitry Andric       EHABISectionIterator<A>::begin(_addressSpace, sects);
14600b57cec5SDimitry Andric   EHABISectionIterator<A> end =
14610b57cec5SDimitry Andric       EHABISectionIterator<A>::end(_addressSpace, sects);
14620b57cec5SDimitry Andric   if (begin == end)
14630b57cec5SDimitry Andric     return false;
14640b57cec5SDimitry Andric 
14650b57cec5SDimitry Andric   EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
14660b57cec5SDimitry Andric   if (itNextPC == begin)
14670b57cec5SDimitry Andric     return false;
14680b57cec5SDimitry Andric   EHABISectionIterator<A> itThisPC = itNextPC - 1;
14690b57cec5SDimitry Andric 
14700b57cec5SDimitry Andric   pint_t thisPC = itThisPC.functionAddress();
14710b57cec5SDimitry Andric   // If an exception is thrown from a function, corresponding to the last entry
14720b57cec5SDimitry Andric   // in the table, we don't really know the function extent and have to choose a
14730b57cec5SDimitry Andric   // value for nextPC. Choosing max() will allow the range check during trace to
14740b57cec5SDimitry Andric   // succeed.
14750b57cec5SDimitry Andric   pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
14760b57cec5SDimitry Andric   pint_t indexDataAddr = itThisPC.dataAddress();
14770b57cec5SDimitry Andric 
14780b57cec5SDimitry Andric   if (indexDataAddr == 0)
14790b57cec5SDimitry Andric     return false;
14800b57cec5SDimitry Andric 
14810b57cec5SDimitry Andric   uint32_t indexData = _addressSpace.get32(indexDataAddr);
14820b57cec5SDimitry Andric   if (indexData == UNW_EXIDX_CANTUNWIND)
14830b57cec5SDimitry Andric     return false;
14840b57cec5SDimitry Andric 
14850b57cec5SDimitry Andric   // If the high bit is set, the exception handling table entry is inline inside
14860b57cec5SDimitry Andric   // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
1487473b61d3SDimitry Andric   // the table points at an offset in the exception handling table (section 5
1488473b61d3SDimitry Andric   // EHABI).
14890b57cec5SDimitry Andric   pint_t exceptionTableAddr;
14900b57cec5SDimitry Andric   uint32_t exceptionTableData;
14910b57cec5SDimitry Andric   bool isSingleWordEHT;
14920b57cec5SDimitry Andric   if (indexData & 0x80000000) {
14930b57cec5SDimitry Andric     exceptionTableAddr = indexDataAddr;
14940b57cec5SDimitry Andric     // TODO(ajwong): Should this data be 0?
14950b57cec5SDimitry Andric     exceptionTableData = indexData;
14960b57cec5SDimitry Andric     isSingleWordEHT = true;
14970b57cec5SDimitry Andric   } else {
14980b57cec5SDimitry Andric     exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
14990b57cec5SDimitry Andric     exceptionTableData = _addressSpace.get32(exceptionTableAddr);
15000b57cec5SDimitry Andric     isSingleWordEHT = false;
15010b57cec5SDimitry Andric   }
15020b57cec5SDimitry Andric 
15030b57cec5SDimitry Andric   // Now we know the 3 things:
15040b57cec5SDimitry Andric   //   exceptionTableAddr -- exception handler table entry.
15050b57cec5SDimitry Andric   //   exceptionTableData -- the data inside the first word of the eht entry.
15060b57cec5SDimitry Andric   //   isSingleWordEHT -- whether the entry is in the index.
15070b57cec5SDimitry Andric   unw_word_t personalityRoutine = 0xbadf00d;
15080b57cec5SDimitry Andric   bool scope32 = false;
15090b57cec5SDimitry Andric   uintptr_t lsda;
15100b57cec5SDimitry Andric 
15110b57cec5SDimitry Andric   // If the high bit in the exception handling table entry is set, the entry is
15120b57cec5SDimitry Andric   // in compact form (section 6.3 EHABI).
15130b57cec5SDimitry Andric   if (exceptionTableData & 0x80000000) {
15140b57cec5SDimitry Andric     // Grab the index of the personality routine from the compact form.
15150b57cec5SDimitry Andric     uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
15160b57cec5SDimitry Andric     uint32_t extraWords = 0;
15170b57cec5SDimitry Andric     switch (choice) {
15180b57cec5SDimitry Andric       case 0:
15190b57cec5SDimitry Andric         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
15200b57cec5SDimitry Andric         extraWords = 0;
15210b57cec5SDimitry Andric         scope32 = false;
15220b57cec5SDimitry Andric         lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
15230b57cec5SDimitry Andric         break;
15240b57cec5SDimitry Andric       case 1:
15250b57cec5SDimitry Andric         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
15260b57cec5SDimitry Andric         extraWords = (exceptionTableData & 0x00ff0000) >> 16;
15270b57cec5SDimitry Andric         scope32 = false;
15280b57cec5SDimitry Andric         lsda = exceptionTableAddr + (extraWords + 1) * 4;
15290b57cec5SDimitry Andric         break;
15300b57cec5SDimitry Andric       case 2:
15310b57cec5SDimitry Andric         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
15320b57cec5SDimitry Andric         extraWords = (exceptionTableData & 0x00ff0000) >> 16;
15330b57cec5SDimitry Andric         scope32 = true;
15340b57cec5SDimitry Andric         lsda = exceptionTableAddr + (extraWords + 1) * 4;
15350b57cec5SDimitry Andric         break;
15360b57cec5SDimitry Andric       default:
15370b57cec5SDimitry Andric         _LIBUNWIND_ABORT("unknown personality routine");
15380b57cec5SDimitry Andric         return false;
15390b57cec5SDimitry Andric     }
15400b57cec5SDimitry Andric 
15410b57cec5SDimitry Andric     if (isSingleWordEHT) {
15420b57cec5SDimitry Andric       if (extraWords != 0) {
15430b57cec5SDimitry Andric         _LIBUNWIND_ABORT("index inlined table detected but pr function "
15440b57cec5SDimitry Andric                          "requires extra words");
15450b57cec5SDimitry Andric         return false;
15460b57cec5SDimitry Andric       }
15470b57cec5SDimitry Andric     }
15480b57cec5SDimitry Andric   } else {
15490b57cec5SDimitry Andric     pint_t personalityAddr =
15500b57cec5SDimitry Andric         exceptionTableAddr + signExtendPrel31(exceptionTableData);
15510b57cec5SDimitry Andric     personalityRoutine = personalityAddr;
15520b57cec5SDimitry Andric 
15530b57cec5SDimitry Andric     // ARM EHABI # 6.2, # 9.2
15540b57cec5SDimitry Andric     //
15550b57cec5SDimitry Andric     //  +---- ehtp
15560b57cec5SDimitry Andric     //  v
15570b57cec5SDimitry Andric     // +--------------------------------------+
15580b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15590b57cec5SDimitry Andric     // | |0| prel31 to personalityRoutine   | |
15600b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15610b57cec5SDimitry Andric     // | |      N |      unwind opcodes     | |  <-- UnwindData
15620b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15630b57cec5SDimitry Andric     // | | Word 2        unwind opcodes     | |
15640b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15650b57cec5SDimitry Andric     // | ...                                  |
15660b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15670b57cec5SDimitry Andric     // | | Word N        unwind opcodes     | |
15680b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15690b57cec5SDimitry Andric     // | | LSDA                             | |  <-- lsda
15700b57cec5SDimitry Andric     // | | ...                              | |
15710b57cec5SDimitry Andric     // | +--------+--------+--------+-------+ |
15720b57cec5SDimitry Andric     // +--------------------------------------+
15730b57cec5SDimitry Andric 
15740b57cec5SDimitry Andric     uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
15750b57cec5SDimitry Andric     uint32_t FirstDataWord = *UnwindData;
15760b57cec5SDimitry Andric     size_t N = ((FirstDataWord >> 24) & 0xff);
15770b57cec5SDimitry Andric     size_t NDataWords = N + 1;
15780b57cec5SDimitry Andric     lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
15790b57cec5SDimitry Andric   }
15800b57cec5SDimitry Andric 
15810b57cec5SDimitry Andric   _info.start_ip = thisPC;
15820b57cec5SDimitry Andric   _info.end_ip = nextPC;
15830b57cec5SDimitry Andric   _info.handler = personalityRoutine;
15840b57cec5SDimitry Andric   _info.unwind_info = exceptionTableAddr;
15850b57cec5SDimitry Andric   _info.lsda = lsda;
15860b57cec5SDimitry Andric   // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
1587473b61d3SDimitry Andric   _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0);  // Use enum?
15880b57cec5SDimitry Andric 
15890b57cec5SDimitry Andric   return true;
15900b57cec5SDimitry Andric }
15910b57cec5SDimitry Andric #endif
15920b57cec5SDimitry Andric 
15930b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
15940b57cec5SDimitry Andric template <typename A, typename R>
1595e8d8bef9SDimitry Andric bool UnwindCursor<A, R>::getInfoFromFdeCie(
1596e8d8bef9SDimitry Andric     const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1597e8d8bef9SDimitry Andric     const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc,
1598e8d8bef9SDimitry Andric     uintptr_t dso_base) {
1599e8d8bef9SDimitry Andric   typename CFI_Parser<A>::PrologInfo prolog;
1600e8d8bef9SDimitry Andric   if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1601e8d8bef9SDimitry Andric                                           R::getArch(), &prolog)) {
1602e8d8bef9SDimitry Andric     // Save off parsed FDE info
1603e8d8bef9SDimitry Andric     _info.start_ip          = fdeInfo.pcStart;
1604e8d8bef9SDimitry Andric     _info.end_ip            = fdeInfo.pcEnd;
1605e8d8bef9SDimitry Andric     _info.lsda              = fdeInfo.lsda;
1606e8d8bef9SDimitry Andric     _info.handler           = cieInfo.personality;
1607e8d8bef9SDimitry Andric     // Some frameless functions need SP altered when resuming in function, so
1608e8d8bef9SDimitry Andric     // propagate spExtraArgSize.
1609e8d8bef9SDimitry Andric     _info.gp                = prolog.spExtraArgSize;
1610e8d8bef9SDimitry Andric     _info.flags             = 0;
1611e8d8bef9SDimitry Andric     _info.format            = dwarfEncoding();
1612e8d8bef9SDimitry Andric     _info.unwind_info       = fdeInfo.fdeStart;
1613e8d8bef9SDimitry Andric     _info.unwind_info_size  = static_cast<uint32_t>(fdeInfo.fdeLength);
1614e8d8bef9SDimitry Andric     _info.extra             = static_cast<unw_word_t>(dso_base);
1615e8d8bef9SDimitry Andric     return true;
1616e8d8bef9SDimitry Andric   }
1617e8d8bef9SDimitry Andric   return false;
1618e8d8bef9SDimitry Andric }
1619e8d8bef9SDimitry Andric 
1620e8d8bef9SDimitry Andric template <typename A, typename R>
16210b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
16220b57cec5SDimitry Andric                                                 const UnwindInfoSections &sects,
16230b57cec5SDimitry Andric                                                 uint32_t fdeSectionOffsetHint) {
16240b57cec5SDimitry Andric   typename CFI_Parser<A>::FDE_Info fdeInfo;
16250b57cec5SDimitry Andric   typename CFI_Parser<A>::CIE_Info cieInfo;
16260b57cec5SDimitry Andric   bool foundFDE = false;
16270b57cec5SDimitry Andric   bool foundInCache = false;
16280b57cec5SDimitry Andric   // If compact encoding table gave offset into dwarf section, go directly there
16290b57cec5SDimitry Andric   if (fdeSectionOffsetHint != 0) {
16300b57cec5SDimitry Andric     foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1631e8d8bef9SDimitry Andric                                     sects.dwarf_section_length,
16320b57cec5SDimitry Andric                                     sects.dwarf_section + fdeSectionOffsetHint,
16330b57cec5SDimitry Andric                                     &fdeInfo, &cieInfo);
16340b57cec5SDimitry Andric   }
16350b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
16360b57cec5SDimitry Andric   if (!foundFDE && (sects.dwarf_index_section != 0)) {
16370b57cec5SDimitry Andric     foundFDE = EHHeaderParser<A>::findFDE(
16380b57cec5SDimitry Andric         _addressSpace, pc, sects.dwarf_index_section,
16390b57cec5SDimitry Andric         (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
16400b57cec5SDimitry Andric   }
16410b57cec5SDimitry Andric #endif
16420b57cec5SDimitry Andric   if (!foundFDE) {
16430b57cec5SDimitry Andric     // otherwise, search cache of previously found FDEs.
16440b57cec5SDimitry Andric     pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
16450b57cec5SDimitry Andric     if (cachedFDE != 0) {
16460b57cec5SDimitry Andric       foundFDE =
16470b57cec5SDimitry Andric           CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1648e8d8bef9SDimitry Andric                                  sects.dwarf_section_length,
16490b57cec5SDimitry Andric                                  cachedFDE, &fdeInfo, &cieInfo);
16500b57cec5SDimitry Andric       foundInCache = foundFDE;
16510b57cec5SDimitry Andric     }
16520b57cec5SDimitry Andric   }
16530b57cec5SDimitry Andric   if (!foundFDE) {
16540b57cec5SDimitry Andric     // Still not found, do full scan of __eh_frame section.
16550b57cec5SDimitry Andric     foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1656e8d8bef9SDimitry Andric                                       sects.dwarf_section_length, 0,
16570b57cec5SDimitry Andric                                       &fdeInfo, &cieInfo);
16580b57cec5SDimitry Andric   }
16590b57cec5SDimitry Andric   if (foundFDE) {
1660e8d8bef9SDimitry Andric     if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) {
16610b57cec5SDimitry Andric       // Add to cache (to make next lookup faster) if we had no hint
16620b57cec5SDimitry Andric       // and there was no index.
16630b57cec5SDimitry Andric       if (!foundInCache && (fdeSectionOffsetHint == 0)) {
16640b57cec5SDimitry Andric   #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
16650b57cec5SDimitry Andric         if (sects.dwarf_index_section == 0)
16660b57cec5SDimitry Andric   #endif
16670b57cec5SDimitry Andric         DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
16680b57cec5SDimitry Andric                               fdeInfo.fdeStart);
16690b57cec5SDimitry Andric       }
16700b57cec5SDimitry Andric       return true;
16710b57cec5SDimitry Andric     }
16720b57cec5SDimitry Andric   }
16730b57cec5SDimitry Andric   //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
16740b57cec5SDimitry Andric   return false;
16750b57cec5SDimitry Andric }
16760b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
16770b57cec5SDimitry Andric 
16780b57cec5SDimitry Andric 
16790b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
16800b57cec5SDimitry Andric template <typename A, typename R>
16810b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
16820b57cec5SDimitry Andric                                               const UnwindInfoSections &sects) {
16830b57cec5SDimitry Andric   const bool log = false;
16840b57cec5SDimitry Andric   if (log)
16850b57cec5SDimitry Andric     fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
16860b57cec5SDimitry Andric             (uint64_t)pc, (uint64_t)sects.dso_base);
16870b57cec5SDimitry Andric 
16880b57cec5SDimitry Andric   const UnwindSectionHeader<A> sectionHeader(_addressSpace,
16890b57cec5SDimitry Andric                                                 sects.compact_unwind_section);
16900b57cec5SDimitry Andric   if (sectionHeader.version() != UNWIND_SECTION_VERSION)
16910b57cec5SDimitry Andric     return false;
16920b57cec5SDimitry Andric 
16930b57cec5SDimitry Andric   // do a binary search of top level index to find page with unwind info
16940b57cec5SDimitry Andric   pint_t targetFunctionOffset = pc - sects.dso_base;
16950b57cec5SDimitry Andric   const UnwindSectionIndexArray<A> topIndex(_addressSpace,
16960b57cec5SDimitry Andric                                            sects.compact_unwind_section
16970b57cec5SDimitry Andric                                          + sectionHeader.indexSectionOffset());
16980b57cec5SDimitry Andric   uint32_t low = 0;
16990b57cec5SDimitry Andric   uint32_t high = sectionHeader.indexCount();
17000b57cec5SDimitry Andric   uint32_t last = high - 1;
17010b57cec5SDimitry Andric   while (low < high) {
17020b57cec5SDimitry Andric     uint32_t mid = (low + high) / 2;
17030b57cec5SDimitry Andric     //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
17040b57cec5SDimitry Andric     //mid, low, high, topIndex.functionOffset(mid));
17050b57cec5SDimitry Andric     if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
17060b57cec5SDimitry Andric       if ((mid == last) ||
17070b57cec5SDimitry Andric           (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
17080b57cec5SDimitry Andric         low = mid;
17090b57cec5SDimitry Andric         break;
17100b57cec5SDimitry Andric       } else {
17110b57cec5SDimitry Andric         low = mid + 1;
17120b57cec5SDimitry Andric       }
17130b57cec5SDimitry Andric     } else {
17140b57cec5SDimitry Andric       high = mid;
17150b57cec5SDimitry Andric     }
17160b57cec5SDimitry Andric   }
17170b57cec5SDimitry Andric   const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
17180b57cec5SDimitry Andric   const uint32_t firstLevelNextPageFunctionOffset =
17190b57cec5SDimitry Andric       topIndex.functionOffset(low + 1);
17200b57cec5SDimitry Andric   const pint_t secondLevelAddr =
17210b57cec5SDimitry Andric       sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
17220b57cec5SDimitry Andric   const pint_t lsdaArrayStartAddr =
17230b57cec5SDimitry Andric       sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
17240b57cec5SDimitry Andric   const pint_t lsdaArrayEndAddr =
17250b57cec5SDimitry Andric       sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
17260b57cec5SDimitry Andric   if (log)
17270b57cec5SDimitry Andric     fprintf(stderr, "\tfirst level search for result index=%d "
17280b57cec5SDimitry Andric                     "to secondLevelAddr=0x%llX\n",
17290b57cec5SDimitry Andric                     low, (uint64_t) secondLevelAddr);
17300b57cec5SDimitry Andric   // do a binary search of second level page index
17310b57cec5SDimitry Andric   uint32_t encoding = 0;
17320b57cec5SDimitry Andric   pint_t funcStart = 0;
17330b57cec5SDimitry Andric   pint_t funcEnd = 0;
17340b57cec5SDimitry Andric   pint_t lsda = 0;
17350b57cec5SDimitry Andric   pint_t personality = 0;
17360b57cec5SDimitry Andric   uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
17370b57cec5SDimitry Andric   if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
17380b57cec5SDimitry Andric     // regular page
17390b57cec5SDimitry Andric     UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
17400b57cec5SDimitry Andric                                                  secondLevelAddr);
17410b57cec5SDimitry Andric     UnwindSectionRegularArray<A> pageIndex(
17420b57cec5SDimitry Andric         _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
17430b57cec5SDimitry Andric     // binary search looks for entry with e where index[e].offset <= pc <
17440b57cec5SDimitry Andric     // index[e+1].offset
17450b57cec5SDimitry Andric     if (log)
17460b57cec5SDimitry Andric       fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
17470b57cec5SDimitry Andric                       "regular page starting at secondLevelAddr=0x%llX\n",
17480b57cec5SDimitry Andric               (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
17490b57cec5SDimitry Andric     low = 0;
17500b57cec5SDimitry Andric     high = pageHeader.entryCount();
17510b57cec5SDimitry Andric     while (low < high) {
17520b57cec5SDimitry Andric       uint32_t mid = (low + high) / 2;
17530b57cec5SDimitry Andric       if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
17540b57cec5SDimitry Andric         if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
17550b57cec5SDimitry Andric           // at end of table
17560b57cec5SDimitry Andric           low = mid;
17570b57cec5SDimitry Andric           funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
17580b57cec5SDimitry Andric           break;
17590b57cec5SDimitry Andric         } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
17600b57cec5SDimitry Andric           // next is too big, so we found it
17610b57cec5SDimitry Andric           low = mid;
17620b57cec5SDimitry Andric           funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
17630b57cec5SDimitry Andric           break;
17640b57cec5SDimitry Andric         } else {
17650b57cec5SDimitry Andric           low = mid + 1;
17660b57cec5SDimitry Andric         }
17670b57cec5SDimitry Andric       } else {
17680b57cec5SDimitry Andric         high = mid;
17690b57cec5SDimitry Andric       }
17700b57cec5SDimitry Andric     }
17710b57cec5SDimitry Andric     encoding = pageIndex.encoding(low);
17720b57cec5SDimitry Andric     funcStart = pageIndex.functionOffset(low) + sects.dso_base;
17730b57cec5SDimitry Andric     if (pc < funcStart) {
17740b57cec5SDimitry Andric       if (log)
17750b57cec5SDimitry Andric         fprintf(
17760b57cec5SDimitry Andric             stderr,
17770b57cec5SDimitry Andric             "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
17780b57cec5SDimitry Andric             (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
17790b57cec5SDimitry Andric       return false;
17800b57cec5SDimitry Andric     }
17810b57cec5SDimitry Andric     if (pc > funcEnd) {
17820b57cec5SDimitry Andric       if (log)
17830b57cec5SDimitry Andric         fprintf(
17840b57cec5SDimitry Andric             stderr,
17850b57cec5SDimitry Andric             "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
17860b57cec5SDimitry Andric             (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
17870b57cec5SDimitry Andric       return false;
17880b57cec5SDimitry Andric     }
17890b57cec5SDimitry Andric   } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
17900b57cec5SDimitry Andric     // compressed page
17910b57cec5SDimitry Andric     UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
17920b57cec5SDimitry Andric                                                     secondLevelAddr);
17930b57cec5SDimitry Andric     UnwindSectionCompressedArray<A> pageIndex(
17940b57cec5SDimitry Andric         _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
17950b57cec5SDimitry Andric     const uint32_t targetFunctionPageOffset =
17960b57cec5SDimitry Andric         (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
17970b57cec5SDimitry Andric     // binary search looks for entry with e where index[e].offset <= pc <
17980b57cec5SDimitry Andric     // index[e+1].offset
17990b57cec5SDimitry Andric     if (log)
18000b57cec5SDimitry Andric       fprintf(stderr, "\tbinary search of compressed page starting at "
18010b57cec5SDimitry Andric                       "secondLevelAddr=0x%llX\n",
18020b57cec5SDimitry Andric               (uint64_t) secondLevelAddr);
18030b57cec5SDimitry Andric     low = 0;
18040b57cec5SDimitry Andric     last = pageHeader.entryCount() - 1;
18050b57cec5SDimitry Andric     high = pageHeader.entryCount();
18060b57cec5SDimitry Andric     while (low < high) {
18070b57cec5SDimitry Andric       uint32_t mid = (low + high) / 2;
18080b57cec5SDimitry Andric       if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
18090b57cec5SDimitry Andric         if ((mid == last) ||
18100b57cec5SDimitry Andric             (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
18110b57cec5SDimitry Andric           low = mid;
18120b57cec5SDimitry Andric           break;
18130b57cec5SDimitry Andric         } else {
18140b57cec5SDimitry Andric           low = mid + 1;
18150b57cec5SDimitry Andric         }
18160b57cec5SDimitry Andric       } else {
18170b57cec5SDimitry Andric         high = mid;
18180b57cec5SDimitry Andric       }
18190b57cec5SDimitry Andric     }
18200b57cec5SDimitry Andric     funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
18210b57cec5SDimitry Andric                                                               + sects.dso_base;
18220b57cec5SDimitry Andric     if (low < last)
18230b57cec5SDimitry Andric       funcEnd =
18240b57cec5SDimitry Andric           pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
18250b57cec5SDimitry Andric                                                               + sects.dso_base;
18260b57cec5SDimitry Andric     else
18270b57cec5SDimitry Andric       funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
18280b57cec5SDimitry Andric     if (pc < funcStart) {
1829fe6060f1SDimitry Andric       _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1830fe6060f1SDimitry Andric                            "not in second level compressed unwind table. "
1831fe6060f1SDimitry Andric                            "funcStart=0x%llX",
18320b57cec5SDimitry Andric                             (uint64_t) pc, (uint64_t) funcStart);
18330b57cec5SDimitry Andric       return false;
18340b57cec5SDimitry Andric     }
18350b57cec5SDimitry Andric     if (pc > funcEnd) {
1836fe6060f1SDimitry Andric       _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1837fe6060f1SDimitry Andric                            "not in second level compressed unwind table. "
1838fe6060f1SDimitry Andric                            "funcEnd=0x%llX",
18390b57cec5SDimitry Andric                            (uint64_t) pc, (uint64_t) funcEnd);
18400b57cec5SDimitry Andric       return false;
18410b57cec5SDimitry Andric     }
18420b57cec5SDimitry Andric     uint16_t encodingIndex = pageIndex.encodingIndex(low);
18430b57cec5SDimitry Andric     if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
18440b57cec5SDimitry Andric       // encoding is in common table in section header
18450b57cec5SDimitry Andric       encoding = _addressSpace.get32(
18460b57cec5SDimitry Andric           sects.compact_unwind_section +
18470b57cec5SDimitry Andric           sectionHeader.commonEncodingsArraySectionOffset() +
18480b57cec5SDimitry Andric           encodingIndex * sizeof(uint32_t));
18490b57cec5SDimitry Andric     } else {
18500b57cec5SDimitry Andric       // encoding is in page specific table
18510b57cec5SDimitry Andric       uint16_t pageEncodingIndex =
18520b57cec5SDimitry Andric           encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
18530b57cec5SDimitry Andric       encoding = _addressSpace.get32(secondLevelAddr +
18540b57cec5SDimitry Andric                                      pageHeader.encodingsPageOffset() +
18550b57cec5SDimitry Andric                                      pageEncodingIndex * sizeof(uint32_t));
18560b57cec5SDimitry Andric     }
18570b57cec5SDimitry Andric   } else {
1858fe6060f1SDimitry Andric     _LIBUNWIND_DEBUG_LOG(
1859fe6060f1SDimitry Andric         "malformed __unwind_info at 0x%0llX bad second level page",
18600b57cec5SDimitry Andric         (uint64_t)sects.compact_unwind_section);
18610b57cec5SDimitry Andric     return false;
18620b57cec5SDimitry Andric   }
18630b57cec5SDimitry Andric 
18640b57cec5SDimitry Andric   // look up LSDA, if encoding says function has one
18650b57cec5SDimitry Andric   if (encoding & UNWIND_HAS_LSDA) {
18660b57cec5SDimitry Andric     UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
18670b57cec5SDimitry Andric     uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
18680b57cec5SDimitry Andric     low = 0;
18690b57cec5SDimitry Andric     high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
18700b57cec5SDimitry Andric                     sizeof(unwind_info_section_header_lsda_index_entry);
18710b57cec5SDimitry Andric     // binary search looks for entry with exact match for functionOffset
18720b57cec5SDimitry Andric     if (log)
18730b57cec5SDimitry Andric       fprintf(stderr,
18740b57cec5SDimitry Andric               "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
18750b57cec5SDimitry Andric               funcStartOffset);
18760b57cec5SDimitry Andric     while (low < high) {
18770b57cec5SDimitry Andric       uint32_t mid = (low + high) / 2;
18780b57cec5SDimitry Andric       if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
18790b57cec5SDimitry Andric         lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
18800b57cec5SDimitry Andric         break;
18810b57cec5SDimitry Andric       } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
18820b57cec5SDimitry Andric         low = mid + 1;
18830b57cec5SDimitry Andric       } else {
18840b57cec5SDimitry Andric         high = mid;
18850b57cec5SDimitry Andric       }
18860b57cec5SDimitry Andric     }
18870b57cec5SDimitry Andric     if (lsda == 0) {
18880b57cec5SDimitry Andric       _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
18890b57cec5SDimitry Andric                     "pc=0x%0llX, but lsda table has no entry",
18900b57cec5SDimitry Andric                     encoding, (uint64_t) pc);
18910b57cec5SDimitry Andric       return false;
18920b57cec5SDimitry Andric     }
18930b57cec5SDimitry Andric   }
18940b57cec5SDimitry Andric 
1895e8d8bef9SDimitry Andric   // extract personality routine, if encoding says function has one
18960b57cec5SDimitry Andric   uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
18970b57cec5SDimitry Andric                               (__builtin_ctz(UNWIND_PERSONALITY_MASK));
18980b57cec5SDimitry Andric   if (personalityIndex != 0) {
18990b57cec5SDimitry Andric     --personalityIndex; // change 1-based to zero-based index
1900e8d8bef9SDimitry Andric     if (personalityIndex >= sectionHeader.personalityArrayCount()) {
19010b57cec5SDimitry Andric       _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d,  "
19020b57cec5SDimitry Andric                             "but personality table has only %d entries",
19030b57cec5SDimitry Andric                             encoding, personalityIndex,
19040b57cec5SDimitry Andric                             sectionHeader.personalityArrayCount());
19050b57cec5SDimitry Andric       return false;
19060b57cec5SDimitry Andric     }
19070b57cec5SDimitry Andric     int32_t personalityDelta = (int32_t)_addressSpace.get32(
19080b57cec5SDimitry Andric         sects.compact_unwind_section +
19090b57cec5SDimitry Andric         sectionHeader.personalityArraySectionOffset() +
19100b57cec5SDimitry Andric         personalityIndex * sizeof(uint32_t));
19110b57cec5SDimitry Andric     pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
19120b57cec5SDimitry Andric     personality = _addressSpace.getP(personalityPointer);
19130b57cec5SDimitry Andric     if (log)
19140b57cec5SDimitry Andric       fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
19150b57cec5SDimitry Andric                       "personalityDelta=0x%08X, personality=0x%08llX\n",
19160b57cec5SDimitry Andric               (uint64_t) pc, personalityDelta, (uint64_t) personality);
19170b57cec5SDimitry Andric   }
19180b57cec5SDimitry Andric 
19190b57cec5SDimitry Andric   if (log)
19200b57cec5SDimitry Andric     fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
19210b57cec5SDimitry Andric                     "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
19220b57cec5SDimitry Andric             (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
19230b57cec5SDimitry Andric   _info.start_ip = funcStart;
19240b57cec5SDimitry Andric   _info.end_ip = funcEnd;
19250b57cec5SDimitry Andric   _info.lsda = lsda;
19260b57cec5SDimitry Andric   _info.handler = personality;
19270b57cec5SDimitry Andric   _info.gp = 0;
19280b57cec5SDimitry Andric   _info.flags = 0;
19290b57cec5SDimitry Andric   _info.format = encoding;
19300b57cec5SDimitry Andric   _info.unwind_info = 0;
19310b57cec5SDimitry Andric   _info.unwind_info_size = 0;
19320b57cec5SDimitry Andric   _info.extra = sects.dso_base;
19330b57cec5SDimitry Andric   return true;
19340b57cec5SDimitry Andric }
19350b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
19360b57cec5SDimitry Andric 
19370b57cec5SDimitry Andric 
19380b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
19390b57cec5SDimitry Andric template <typename A, typename R>
19400b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
19410b57cec5SDimitry Andric   pint_t base;
19420b57cec5SDimitry Andric   RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
19430b57cec5SDimitry Andric   if (!unwindEntry) {
19440b57cec5SDimitry Andric     _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
19450b57cec5SDimitry Andric     return false;
19460b57cec5SDimitry Andric   }
19470b57cec5SDimitry Andric   _info.gp = 0;
19480b57cec5SDimitry Andric   _info.flags = 0;
19490b57cec5SDimitry Andric   _info.format = 0;
19500b57cec5SDimitry Andric   _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
19510b57cec5SDimitry Andric   _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
19520b57cec5SDimitry Andric   _info.extra = base;
19530b57cec5SDimitry Andric   _info.start_ip = base + unwindEntry->BeginAddress;
19540b57cec5SDimitry Andric #ifdef _LIBUNWIND_TARGET_X86_64
19550b57cec5SDimitry Andric   _info.end_ip = base + unwindEntry->EndAddress;
19560b57cec5SDimitry Andric   // Only fill in the handler and LSDA if they're stale.
19570b57cec5SDimitry Andric   if (pc != getLastPC()) {
19580b57cec5SDimitry Andric     UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
19590b57cec5SDimitry Andric     if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
19600b57cec5SDimitry Andric       // The personality is given in the UNWIND_INFO itself. The LSDA immediately
19610b57cec5SDimitry Andric       // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
19620b57cec5SDimitry Andric       // these structures.)
19630b57cec5SDimitry Andric       // N.B. UNWIND_INFO structs are DWORD-aligned.
19640b57cec5SDimitry Andric       uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
19650b57cec5SDimitry Andric       const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
19660b57cec5SDimitry Andric       _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
19670b57cec5SDimitry Andric       if (*handler) {
19680b57cec5SDimitry Andric         _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
19690b57cec5SDimitry Andric       } else
19700b57cec5SDimitry Andric         _info.handler = 0;
19710b57cec5SDimitry Andric     } else {
19720b57cec5SDimitry Andric       _info.lsda = 0;
19730b57cec5SDimitry Andric       _info.handler = 0;
19740b57cec5SDimitry Andric     }
19750b57cec5SDimitry Andric   }
19760b57cec5SDimitry Andric #endif
19770b57cec5SDimitry Andric   setLastPC(pc);
19780b57cec5SDimitry Andric   return true;
19790b57cec5SDimitry Andric }
19800b57cec5SDimitry Andric #endif
19810b57cec5SDimitry Andric 
198281ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
198381ad6265SDimitry Andric // Masks for traceback table field xtbtable.
198481ad6265SDimitry Andric enum xTBTableMask : uint8_t {
198581ad6265SDimitry Andric   reservedBit = 0x02, // The traceback table was incorrectly generated if set
198681ad6265SDimitry Andric                       // (see comments in function getInfoFromTBTable().
198781ad6265SDimitry Andric   ehInfoBit = 0x08    // Exception handling info is present if set
198881ad6265SDimitry Andric };
198981ad6265SDimitry Andric 
199081ad6265SDimitry Andric enum frameType : unw_word_t {
199181ad6265SDimitry Andric   frameWithXLEHStateTable = 0,
199281ad6265SDimitry Andric   frameWithEHInfo = 1
199381ad6265SDimitry Andric };
199481ad6265SDimitry Andric 
199581ad6265SDimitry Andric extern "C" {
199681ad6265SDimitry Andric typedef _Unwind_Reason_Code __xlcxx_personality_v0_t(int, _Unwind_Action,
199781ad6265SDimitry Andric                                                      uint64_t,
199881ad6265SDimitry Andric                                                      _Unwind_Exception *,
199981ad6265SDimitry Andric                                                      struct _Unwind_Context *);
200081ad6265SDimitry Andric __attribute__((__weak__)) __xlcxx_personality_v0_t __xlcxx_personality_v0;
200181ad6265SDimitry Andric }
200281ad6265SDimitry Andric 
200381ad6265SDimitry Andric static __xlcxx_personality_v0_t *xlcPersonalityV0;
200481ad6265SDimitry Andric static RWMutex xlcPersonalityV0InitLock;
200581ad6265SDimitry Andric 
200681ad6265SDimitry Andric template <typename A, typename R>
200781ad6265SDimitry Andric bool UnwindCursor<A, R>::getInfoFromTBTable(pint_t pc, R &registers) {
200881ad6265SDimitry Andric   uint32_t *p = reinterpret_cast<uint32_t *>(pc);
200981ad6265SDimitry Andric 
201081ad6265SDimitry Andric   // Keep looking forward until a word of 0 is found. The traceback
201181ad6265SDimitry Andric   // table starts at the following word.
201281ad6265SDimitry Andric   while (*p)
201381ad6265SDimitry Andric     ++p;
201481ad6265SDimitry Andric   tbtable *TBTable = reinterpret_cast<tbtable *>(p + 1);
201581ad6265SDimitry Andric 
201681ad6265SDimitry Andric   if (_LIBUNWIND_TRACING_UNWINDING) {
201781ad6265SDimitry Andric     char functionBuf[512];
201881ad6265SDimitry Andric     const char *functionName = functionBuf;
201981ad6265SDimitry Andric     unw_word_t offset;
202081ad6265SDimitry Andric     if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) {
202181ad6265SDimitry Andric       functionName = ".anonymous.";
202281ad6265SDimitry Andric     }
202381ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("%s: Look up traceback table of func=%s at %p",
202481ad6265SDimitry Andric                                __func__, functionName,
202581ad6265SDimitry Andric                                reinterpret_cast<void *>(TBTable));
202681ad6265SDimitry Andric   }
202781ad6265SDimitry Andric 
202881ad6265SDimitry Andric   // If the traceback table does not contain necessary info, bypass this frame.
202981ad6265SDimitry Andric   if (!TBTable->tb.has_tboff)
203081ad6265SDimitry Andric     return false;
203181ad6265SDimitry Andric 
203281ad6265SDimitry Andric   // Structure tbtable_ext contains important data we are looking for.
203381ad6265SDimitry Andric   p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext);
203481ad6265SDimitry Andric 
203581ad6265SDimitry Andric   // Skip field parminfo if it exists.
203681ad6265SDimitry Andric   if (TBTable->tb.fixedparms || TBTable->tb.floatparms)
203781ad6265SDimitry Andric     ++p;
203881ad6265SDimitry Andric 
203981ad6265SDimitry Andric   // p now points to tb_offset, the offset from start of function to TB table.
204081ad6265SDimitry Andric   unw_word_t start_ip =
204181ad6265SDimitry Andric       reinterpret_cast<unw_word_t>(TBTable) - *p - sizeof(uint32_t);
204281ad6265SDimitry Andric   unw_word_t end_ip = reinterpret_cast<unw_word_t>(TBTable);
204381ad6265SDimitry Andric   ++p;
204481ad6265SDimitry Andric 
204581ad6265SDimitry Andric   _LIBUNWIND_TRACE_UNWINDING("start_ip=%p, end_ip=%p\n",
204681ad6265SDimitry Andric                              reinterpret_cast<void *>(start_ip),
204781ad6265SDimitry Andric                              reinterpret_cast<void *>(end_ip));
204881ad6265SDimitry Andric 
204981ad6265SDimitry Andric   // Skip field hand_mask if it exists.
205081ad6265SDimitry Andric   if (TBTable->tb.int_hndl)
205181ad6265SDimitry Andric     ++p;
205281ad6265SDimitry Andric 
205381ad6265SDimitry Andric   unw_word_t lsda = 0;
205481ad6265SDimitry Andric   unw_word_t handler = 0;
205581ad6265SDimitry Andric   unw_word_t flags = frameType::frameWithXLEHStateTable;
205681ad6265SDimitry Andric 
205781ad6265SDimitry Andric   if (TBTable->tb.lang == TB_CPLUSPLUS && TBTable->tb.has_ctl) {
205881ad6265SDimitry Andric     // State table info is available. The ctl_info field indicates the
205981ad6265SDimitry Andric     // number of CTL anchors. There should be only one entry for the C++
206081ad6265SDimitry Andric     // state table.
206181ad6265SDimitry Andric     assert(*p == 1 && "libunwind: there must be only one ctl_info entry");
206281ad6265SDimitry Andric     ++p;
206381ad6265SDimitry Andric     // p points to the offset of the state table into the stack.
206481ad6265SDimitry Andric     pint_t stateTableOffset = *p++;
206581ad6265SDimitry Andric 
206681ad6265SDimitry Andric     int framePointerReg;
206781ad6265SDimitry Andric 
206881ad6265SDimitry Andric     // Skip fields name_len and name if exist.
206981ad6265SDimitry Andric     if (TBTable->tb.name_present) {
207081ad6265SDimitry Andric       const uint16_t name_len = *(reinterpret_cast<uint16_t *>(p));
207181ad6265SDimitry Andric       p = reinterpret_cast<uint32_t *>(reinterpret_cast<char *>(p) + name_len +
207281ad6265SDimitry Andric                                        sizeof(uint16_t));
207381ad6265SDimitry Andric     }
207481ad6265SDimitry Andric 
207581ad6265SDimitry Andric     if (TBTable->tb.uses_alloca)
207681ad6265SDimitry Andric       framePointerReg = *(reinterpret_cast<char *>(p));
207781ad6265SDimitry Andric     else
207881ad6265SDimitry Andric       framePointerReg = 1; // default frame pointer == SP
207981ad6265SDimitry Andric 
208081ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
208181ad6265SDimitry Andric         "framePointerReg=%d, framePointer=%p, "
208281ad6265SDimitry Andric         "stateTableOffset=%#lx\n",
208381ad6265SDimitry Andric         framePointerReg,
208481ad6265SDimitry Andric         reinterpret_cast<void *>(_registers.getRegister(framePointerReg)),
208581ad6265SDimitry Andric         stateTableOffset);
208681ad6265SDimitry Andric     lsda = _registers.getRegister(framePointerReg) + stateTableOffset;
208781ad6265SDimitry Andric 
208881ad6265SDimitry Andric     // Since the traceback table generated by the legacy XLC++ does not
208981ad6265SDimitry Andric     // provide the location of the personality for the state table,
209081ad6265SDimitry Andric     // function __xlcxx_personality_v0(), which is the personality for the state
209181ad6265SDimitry Andric     // table and is exported from libc++abi, is directly assigned as the
209281ad6265SDimitry Andric     // handler here. When a legacy XLC++ frame is encountered, the symbol
209381ad6265SDimitry Andric     // is resolved dynamically using dlopen() to avoid hard dependency from
209481ad6265SDimitry Andric     // libunwind on libc++abi.
209581ad6265SDimitry Andric 
209681ad6265SDimitry Andric     // Resolve the function pointer to the state table personality if it has
209781ad6265SDimitry Andric     // not already.
209881ad6265SDimitry Andric     if (xlcPersonalityV0 == NULL) {
209981ad6265SDimitry Andric       xlcPersonalityV0InitLock.lock();
210081ad6265SDimitry Andric       if (xlcPersonalityV0 == NULL) {
210181ad6265SDimitry Andric         // If libc++abi is statically linked in, symbol __xlcxx_personality_v0
210281ad6265SDimitry Andric         // has been resolved at the link time.
210381ad6265SDimitry Andric         xlcPersonalityV0 = &__xlcxx_personality_v0;
210481ad6265SDimitry Andric         if (xlcPersonalityV0 == NULL) {
210581ad6265SDimitry Andric           // libc++abi is dynamically linked. Resolve __xlcxx_personality_v0
210681ad6265SDimitry Andric           // using dlopen().
210781ad6265SDimitry Andric           const char libcxxabi[] = "libc++abi.a(libc++abi.so.1)";
210881ad6265SDimitry Andric           void *libHandle;
210981ad6265SDimitry Andric           libHandle = dlopen(libcxxabi, RTLD_MEMBER | RTLD_NOW);
211081ad6265SDimitry Andric           if (libHandle == NULL) {
211181ad6265SDimitry Andric             _LIBUNWIND_TRACE_UNWINDING("dlopen() failed with errno=%d\n",
211281ad6265SDimitry Andric                                        errno);
211381ad6265SDimitry Andric             assert(0 && "dlopen() failed");
211481ad6265SDimitry Andric           }
211581ad6265SDimitry Andric           xlcPersonalityV0 = reinterpret_cast<__xlcxx_personality_v0_t *>(
211681ad6265SDimitry Andric               dlsym(libHandle, "__xlcxx_personality_v0"));
211781ad6265SDimitry Andric           if (xlcPersonalityV0 == NULL) {
211881ad6265SDimitry Andric             _LIBUNWIND_TRACE_UNWINDING("dlsym() failed with errno=%d\n", errno);
211981ad6265SDimitry Andric             assert(0 && "dlsym() failed");
212081ad6265SDimitry Andric           }
212181ad6265SDimitry Andric           dlclose(libHandle);
212281ad6265SDimitry Andric         }
212381ad6265SDimitry Andric       }
212481ad6265SDimitry Andric       xlcPersonalityV0InitLock.unlock();
212581ad6265SDimitry Andric     }
212681ad6265SDimitry Andric     handler = reinterpret_cast<unw_word_t>(xlcPersonalityV0);
212781ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("State table: LSDA=%p, Personality=%p\n",
212881ad6265SDimitry Andric                                reinterpret_cast<void *>(lsda),
212981ad6265SDimitry Andric                                reinterpret_cast<void *>(handler));
213081ad6265SDimitry Andric   } else if (TBTable->tb.longtbtable) {
213181ad6265SDimitry Andric     // This frame has the traceback table extension. Possible cases are
213281ad6265SDimitry Andric     // 1) a C++ frame that has the 'eh_info' structure; 2) a C++ frame that
213381ad6265SDimitry Andric     // is not EH aware; or, 3) a frame of other languages. We need to figure out
213481ad6265SDimitry Andric     // if the traceback table extension contains the 'eh_info' structure.
213581ad6265SDimitry Andric     //
213681ad6265SDimitry Andric     // We also need to deal with the complexity arising from some XL compiler
213781ad6265SDimitry Andric     // versions use the wrong ordering of 'longtbtable' and 'has_vec' bits
213881ad6265SDimitry Andric     // where the 'longtbtable' bit is meant to be the 'has_vec' bit and vice
213981ad6265SDimitry Andric     // versa. For frames of code generated by those compilers, the 'longtbtable'
214081ad6265SDimitry Andric     // bit may be set but there isn't really a traceback table extension.
214181ad6265SDimitry Andric     //
214281ad6265SDimitry Andric     // In </usr/include/sys/debug.h>, there is the following definition of
214381ad6265SDimitry Andric     // 'struct tbtable_ext'. It is not really a structure but a dummy to
214481ad6265SDimitry Andric     // collect the description of optional parts of the traceback table.
214581ad6265SDimitry Andric     //
214681ad6265SDimitry Andric     // struct tbtable_ext {
214781ad6265SDimitry Andric     //   ...
214881ad6265SDimitry Andric     //   char alloca_reg;        /* Register for alloca automatic storage */
214981ad6265SDimitry Andric     //   struct vec_ext vec_ext; /* Vector extension (if has_vec is set) */
215081ad6265SDimitry Andric     //   unsigned char xtbtable; /* More tbtable fields, if longtbtable is set*/
215181ad6265SDimitry Andric     // };
215281ad6265SDimitry Andric     //
215381ad6265SDimitry Andric     // Depending on how the 'has_vec'/'longtbtable' bit is interpreted, the data
215481ad6265SDimitry Andric     // following 'alloca_reg' can be treated either as 'struct vec_ext' or
215581ad6265SDimitry Andric     // 'unsigned char xtbtable'. 'xtbtable' bits are defined in
215681ad6265SDimitry Andric     // </usr/include/sys/debug.h> as flags. The 7th bit '0x02' is currently
215781ad6265SDimitry Andric     // unused and should not be set. 'struct vec_ext' is defined in
215881ad6265SDimitry Andric     // </usr/include/sys/debug.h> as follows:
215981ad6265SDimitry Andric     //
216081ad6265SDimitry Andric     // struct vec_ext {
216181ad6265SDimitry Andric     //   unsigned vr_saved:6;      /* Number of non-volatile vector regs saved
216281ad6265SDimitry Andric     //   */
216381ad6265SDimitry Andric     //                             /* first register saved is assumed to be */
216481ad6265SDimitry Andric     //                             /* 32 - vr_saved                         */
216581ad6265SDimitry Andric     //   unsigned saves_vrsave:1;  /* Set if vrsave is saved on the stack */
216681ad6265SDimitry Andric     //   unsigned has_varargs:1;
216781ad6265SDimitry Andric     //   ...
216881ad6265SDimitry Andric     // };
216981ad6265SDimitry Andric     //
217081ad6265SDimitry Andric     // Here, the 7th bit is used as 'saves_vrsave'. To determine whether it
217181ad6265SDimitry Andric     // is 'struct vec_ext' or 'xtbtable' that follows 'alloca_reg',
217281ad6265SDimitry Andric     // we checks if the 7th bit is set or not because 'xtbtable' should
217381ad6265SDimitry Andric     // never have the 7th bit set. The 7th bit of 'xtbtable' will be reserved
217481ad6265SDimitry Andric     // in the future to make sure the mitigation works. This mitigation
217581ad6265SDimitry Andric     // is not 100% bullet proof because 'struct vec_ext' may not always have
217681ad6265SDimitry Andric     // 'saves_vrsave' bit set.
217781ad6265SDimitry Andric     //
217881ad6265SDimitry Andric     // 'reservedBit' is defined in enum 'xTBTableMask' above as the mask for
217981ad6265SDimitry Andric     // checking the 7th bit.
218081ad6265SDimitry Andric 
218181ad6265SDimitry Andric     // p points to field name len.
218281ad6265SDimitry Andric     uint8_t *charPtr = reinterpret_cast<uint8_t *>(p);
218381ad6265SDimitry Andric 
218481ad6265SDimitry Andric     // Skip fields name_len and name if they exist.
218581ad6265SDimitry Andric     if (TBTable->tb.name_present) {
218681ad6265SDimitry Andric       const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr));
218781ad6265SDimitry Andric       charPtr = charPtr + name_len + sizeof(uint16_t);
218881ad6265SDimitry Andric     }
218981ad6265SDimitry Andric 
219081ad6265SDimitry Andric     // Skip field alloc_reg if it exists.
219181ad6265SDimitry Andric     if (TBTable->tb.uses_alloca)
219281ad6265SDimitry Andric       ++charPtr;
219381ad6265SDimitry Andric 
219481ad6265SDimitry Andric     // Check traceback table bit has_vec. Skip struct vec_ext if it exists.
219581ad6265SDimitry Andric     if (TBTable->tb.has_vec)
219681ad6265SDimitry Andric       // Note struct vec_ext does exist at this point because whether the
219781ad6265SDimitry Andric       // ordering of longtbtable and has_vec bits is correct or not, both
219881ad6265SDimitry Andric       // are set.
219981ad6265SDimitry Andric       charPtr += sizeof(struct vec_ext);
220081ad6265SDimitry Andric 
220181ad6265SDimitry Andric     // charPtr points to field 'xtbtable'. Check if the EH info is available.
220281ad6265SDimitry Andric     // Also check if the reserved bit of the extended traceback table field
220381ad6265SDimitry Andric     // 'xtbtable' is set. If it is, the traceback table was incorrectly
220481ad6265SDimitry Andric     // generated by an XL compiler that uses the wrong ordering of 'longtbtable'
220581ad6265SDimitry Andric     // and 'has_vec' bits and this is in fact 'struct vec_ext'. So skip the
220681ad6265SDimitry Andric     // frame.
220781ad6265SDimitry Andric     if ((*charPtr & xTBTableMask::ehInfoBit) &&
220881ad6265SDimitry Andric         !(*charPtr & xTBTableMask::reservedBit)) {
220981ad6265SDimitry Andric       // Mark this frame has the new EH info.
221081ad6265SDimitry Andric       flags = frameType::frameWithEHInfo;
221181ad6265SDimitry Andric 
221281ad6265SDimitry Andric       // eh_info is available.
221381ad6265SDimitry Andric       charPtr++;
221481ad6265SDimitry Andric       // The pointer is 4-byte aligned.
221581ad6265SDimitry Andric       if (reinterpret_cast<uintptr_t>(charPtr) % 4)
221681ad6265SDimitry Andric         charPtr += 4 - reinterpret_cast<uintptr_t>(charPtr) % 4;
221781ad6265SDimitry Andric       uintptr_t *ehInfo =
221881ad6265SDimitry Andric           reinterpret_cast<uintptr_t *>(*(reinterpret_cast<uintptr_t *>(
221981ad6265SDimitry Andric               registers.getRegister(2) +
222081ad6265SDimitry Andric               *(reinterpret_cast<uintptr_t *>(charPtr)))));
222181ad6265SDimitry Andric 
222281ad6265SDimitry Andric       // ehInfo points to structure en_info. The first member is version.
222381ad6265SDimitry Andric       // Only version 0 is currently supported.
222481ad6265SDimitry Andric       assert(*(reinterpret_cast<uint32_t *>(ehInfo)) == 0 &&
222581ad6265SDimitry Andric              "libunwind: ehInfo version other than 0 is not supported");
222681ad6265SDimitry Andric 
222781ad6265SDimitry Andric       // Increment ehInfo to point to member lsda.
222881ad6265SDimitry Andric       ++ehInfo;
222981ad6265SDimitry Andric       lsda = *ehInfo++;
223081ad6265SDimitry Andric 
223181ad6265SDimitry Andric       // enInfo now points to member personality.
223281ad6265SDimitry Andric       handler = *ehInfo;
223381ad6265SDimitry Andric 
223481ad6265SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING("Range table: LSDA=%#lx, Personality=%#lx\n",
223581ad6265SDimitry Andric                                  lsda, handler);
223681ad6265SDimitry Andric     }
223781ad6265SDimitry Andric   }
223881ad6265SDimitry Andric 
223981ad6265SDimitry Andric   _info.start_ip = start_ip;
224081ad6265SDimitry Andric   _info.end_ip = end_ip;
224181ad6265SDimitry Andric   _info.lsda = lsda;
224281ad6265SDimitry Andric   _info.handler = handler;
224381ad6265SDimitry Andric   _info.gp = 0;
224481ad6265SDimitry Andric   _info.flags = flags;
224581ad6265SDimitry Andric   _info.format = 0;
224681ad6265SDimitry Andric   _info.unwind_info = reinterpret_cast<unw_word_t>(TBTable);
224781ad6265SDimitry Andric   _info.unwind_info_size = 0;
224881ad6265SDimitry Andric   _info.extra = registers.getRegister(2);
224981ad6265SDimitry Andric 
225081ad6265SDimitry Andric   return true;
225181ad6265SDimitry Andric }
225281ad6265SDimitry Andric 
225381ad6265SDimitry Andric // Step back up the stack following the frame back link.
225481ad6265SDimitry Andric template <typename A, typename R>
225581ad6265SDimitry Andric int UnwindCursor<A, R>::stepWithTBTable(pint_t pc, tbtable *TBTable,
225681ad6265SDimitry Andric                                         R &registers, bool &isSignalFrame) {
225781ad6265SDimitry Andric   if (_LIBUNWIND_TRACING_UNWINDING) {
225881ad6265SDimitry Andric     char functionBuf[512];
225981ad6265SDimitry Andric     const char *functionName = functionBuf;
226081ad6265SDimitry Andric     unw_word_t offset;
226181ad6265SDimitry Andric     if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) {
226281ad6265SDimitry Andric       functionName = ".anonymous.";
226381ad6265SDimitry Andric     }
226481ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("%s: Look up traceback table of func=%s at %p",
226581ad6265SDimitry Andric                                __func__, functionName,
226681ad6265SDimitry Andric                                reinterpret_cast<void *>(TBTable));
226781ad6265SDimitry Andric   }
226881ad6265SDimitry Andric 
226981ad6265SDimitry Andric #if defined(__powerpc64__)
227081ad6265SDimitry Andric   // Instruction to reload TOC register "l r2,40(r1)"
227181ad6265SDimitry Andric   const uint32_t loadTOCRegInst = 0xe8410028;
227281ad6265SDimitry Andric   const int32_t unwPPCF0Index = UNW_PPC64_F0;
227381ad6265SDimitry Andric   const int32_t unwPPCV0Index = UNW_PPC64_V0;
227481ad6265SDimitry Andric #else
227581ad6265SDimitry Andric   // Instruction to reload TOC register "l r2,20(r1)"
227681ad6265SDimitry Andric   const uint32_t loadTOCRegInst = 0x80410014;
227781ad6265SDimitry Andric   const int32_t unwPPCF0Index = UNW_PPC_F0;
227881ad6265SDimitry Andric   const int32_t unwPPCV0Index = UNW_PPC_V0;
227981ad6265SDimitry Andric #endif
228081ad6265SDimitry Andric 
228181ad6265SDimitry Andric   R newRegisters = registers;
228281ad6265SDimitry Andric 
228381ad6265SDimitry Andric   // lastStack points to the stack frame of the next routine up.
228481ad6265SDimitry Andric   pint_t lastStack = *(reinterpret_cast<pint_t *>(registers.getSP()));
228581ad6265SDimitry Andric 
228681ad6265SDimitry Andric   // Return address is the address after call site instruction.
228781ad6265SDimitry Andric   pint_t returnAddress;
228881ad6265SDimitry Andric 
228981ad6265SDimitry Andric   if (isSignalFrame) {
229081ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("Possible signal handler frame: lastStack=%p",
229181ad6265SDimitry Andric                                reinterpret_cast<void *>(lastStack));
229281ad6265SDimitry Andric 
229381ad6265SDimitry Andric     sigcontext *sigContext = reinterpret_cast<sigcontext *>(
229481ad6265SDimitry Andric         reinterpret_cast<char *>(lastStack) + STKMIN);
229581ad6265SDimitry Andric     returnAddress = sigContext->sc_jmpbuf.jmp_context.iar;
229681ad6265SDimitry Andric 
229781ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("From sigContext=%p, returnAddress=%p\n",
229881ad6265SDimitry Andric                                reinterpret_cast<void *>(sigContext),
229981ad6265SDimitry Andric                                reinterpret_cast<void *>(returnAddress));
230081ad6265SDimitry Andric 
230181ad6265SDimitry Andric     if (returnAddress < 0x10000000) {
230281ad6265SDimitry Andric       // Try again using STKMINALIGN
230381ad6265SDimitry Andric       sigContext = reinterpret_cast<sigcontext *>(
230481ad6265SDimitry Andric           reinterpret_cast<char *>(lastStack) + STKMINALIGN);
230581ad6265SDimitry Andric       returnAddress = sigContext->sc_jmpbuf.jmp_context.iar;
230681ad6265SDimitry Andric       if (returnAddress < 0x10000000) {
230781ad6265SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING("Bad returnAddress=%p\n",
230881ad6265SDimitry Andric                                    reinterpret_cast<void *>(returnAddress));
230981ad6265SDimitry Andric         return UNW_EBADFRAME;
231081ad6265SDimitry Andric       } else {
231181ad6265SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING("Tried again using STKMINALIGN: "
231281ad6265SDimitry Andric                                    "sigContext=%p, returnAddress=%p. "
231381ad6265SDimitry Andric                                    "Seems to be a valid address\n",
231481ad6265SDimitry Andric                                    reinterpret_cast<void *>(sigContext),
231581ad6265SDimitry Andric                                    reinterpret_cast<void *>(returnAddress));
231681ad6265SDimitry Andric       }
231781ad6265SDimitry Andric     }
231881ad6265SDimitry Andric     // Restore the condition register from sigcontext.
231981ad6265SDimitry Andric     newRegisters.setCR(sigContext->sc_jmpbuf.jmp_context.cr);
232081ad6265SDimitry Andric 
232181ad6265SDimitry Andric     // Restore GPRs from sigcontext.
232281ad6265SDimitry Andric     for (int i = 0; i < 32; ++i)
232381ad6265SDimitry Andric       newRegisters.setRegister(i, sigContext->sc_jmpbuf.jmp_context.gpr[i]);
232481ad6265SDimitry Andric 
232581ad6265SDimitry Andric     // Restore FPRs from sigcontext.
232681ad6265SDimitry Andric     for (int i = 0; i < 32; ++i)
232781ad6265SDimitry Andric       newRegisters.setFloatRegister(i + unwPPCF0Index,
232881ad6265SDimitry Andric                                     sigContext->sc_jmpbuf.jmp_context.fpr[i]);
232981ad6265SDimitry Andric 
233081ad6265SDimitry Andric     // Restore vector registers if there is an associated extended context
233181ad6265SDimitry Andric     // structure.
233281ad6265SDimitry Andric     if (sigContext->sc_jmpbuf.jmp_context.msr & __EXTCTX) {
233381ad6265SDimitry Andric       ucontext_t *uContext = reinterpret_cast<ucontext_t *>(sigContext);
233481ad6265SDimitry Andric       if (uContext->__extctx->__extctx_magic == __EXTCTX_MAGIC) {
233581ad6265SDimitry Andric         for (int i = 0; i < 32; ++i)
233681ad6265SDimitry Andric           newRegisters.setVectorRegister(
233781ad6265SDimitry Andric               i + unwPPCV0Index, *(reinterpret_cast<v128 *>(
233881ad6265SDimitry Andric                                      &(uContext->__extctx->__vmx.__vr[i]))));
233981ad6265SDimitry Andric       }
234081ad6265SDimitry Andric     }
234181ad6265SDimitry Andric   } else {
234281ad6265SDimitry Andric     // Step up a normal frame.
234381ad6265SDimitry Andric     returnAddress = reinterpret_cast<pint_t *>(lastStack)[2];
234481ad6265SDimitry Andric 
234581ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("Extract info from lastStack=%p, "
234681ad6265SDimitry Andric                                "returnAddress=%p\n",
234781ad6265SDimitry Andric                                reinterpret_cast<void *>(lastStack),
234881ad6265SDimitry Andric                                reinterpret_cast<void *>(returnAddress));
234981ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("fpr_regs=%d, gpr_regs=%d, saves_cr=%d\n",
235081ad6265SDimitry Andric                                TBTable->tb.fpr_saved, TBTable->tb.gpr_saved,
235181ad6265SDimitry Andric                                TBTable->tb.saves_cr);
235281ad6265SDimitry Andric 
235381ad6265SDimitry Andric     // Restore FP registers.
235481ad6265SDimitry Andric     char *ptrToRegs = reinterpret_cast<char *>(lastStack);
235581ad6265SDimitry Andric     double *FPRegs = reinterpret_cast<double *>(
235681ad6265SDimitry Andric         ptrToRegs - (TBTable->tb.fpr_saved * sizeof(double)));
235781ad6265SDimitry Andric     for (int i = 0; i < TBTable->tb.fpr_saved; ++i)
235881ad6265SDimitry Andric       newRegisters.setFloatRegister(
235981ad6265SDimitry Andric           32 - TBTable->tb.fpr_saved + i + unwPPCF0Index, FPRegs[i]);
236081ad6265SDimitry Andric 
236181ad6265SDimitry Andric     // Restore GP registers.
236281ad6265SDimitry Andric     ptrToRegs = reinterpret_cast<char *>(FPRegs);
236381ad6265SDimitry Andric     uintptr_t *GPRegs = reinterpret_cast<uintptr_t *>(
236481ad6265SDimitry Andric         ptrToRegs - (TBTable->tb.gpr_saved * sizeof(uintptr_t)));
236581ad6265SDimitry Andric     for (int i = 0; i < TBTable->tb.gpr_saved; ++i)
236681ad6265SDimitry Andric       newRegisters.setRegister(32 - TBTable->tb.gpr_saved + i, GPRegs[i]);
236781ad6265SDimitry Andric 
236881ad6265SDimitry Andric     // Restore Vector registers.
236981ad6265SDimitry Andric     ptrToRegs = reinterpret_cast<char *>(GPRegs);
237081ad6265SDimitry Andric 
237181ad6265SDimitry Andric     // Restore vector registers only if this is a Clang frame. Also
237281ad6265SDimitry Andric     // check if traceback table bit has_vec is set. If it is, structure
237381ad6265SDimitry Andric     // vec_ext is available.
237481ad6265SDimitry Andric     if (_info.flags == frameType::frameWithEHInfo && TBTable->tb.has_vec) {
237581ad6265SDimitry Andric 
237681ad6265SDimitry Andric       // Get to the vec_ext structure to check if vector registers are saved.
237781ad6265SDimitry Andric       uint32_t *p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext);
237881ad6265SDimitry Andric 
237981ad6265SDimitry Andric       // Skip field parminfo if exists.
238081ad6265SDimitry Andric       if (TBTable->tb.fixedparms || TBTable->tb.floatparms)
238181ad6265SDimitry Andric         ++p;
238281ad6265SDimitry Andric 
238381ad6265SDimitry Andric       // Skip field tb_offset if exists.
238481ad6265SDimitry Andric       if (TBTable->tb.has_tboff)
238581ad6265SDimitry Andric         ++p;
238681ad6265SDimitry Andric 
238781ad6265SDimitry Andric       // Skip field hand_mask if exists.
238881ad6265SDimitry Andric       if (TBTable->tb.int_hndl)
238981ad6265SDimitry Andric         ++p;
239081ad6265SDimitry Andric 
239181ad6265SDimitry Andric       // Skip fields ctl_info and ctl_info_disp if exist.
239281ad6265SDimitry Andric       if (TBTable->tb.has_ctl) {
239381ad6265SDimitry Andric         // Skip field ctl_info.
239481ad6265SDimitry Andric         ++p;
239581ad6265SDimitry Andric         // Skip field ctl_info_disp.
239681ad6265SDimitry Andric         ++p;
239781ad6265SDimitry Andric       }
239881ad6265SDimitry Andric 
239981ad6265SDimitry Andric       // Skip fields name_len and name if exist.
240081ad6265SDimitry Andric       // p is supposed to point to field name_len now.
240181ad6265SDimitry Andric       uint8_t *charPtr = reinterpret_cast<uint8_t *>(p);
240281ad6265SDimitry Andric       if (TBTable->tb.name_present) {
240381ad6265SDimitry Andric         const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr));
240481ad6265SDimitry Andric         charPtr = charPtr + name_len + sizeof(uint16_t);
240581ad6265SDimitry Andric       }
240681ad6265SDimitry Andric 
240781ad6265SDimitry Andric       // Skip field alloc_reg if it exists.
240881ad6265SDimitry Andric       if (TBTable->tb.uses_alloca)
240981ad6265SDimitry Andric         ++charPtr;
241081ad6265SDimitry Andric 
241181ad6265SDimitry Andric       struct vec_ext *vec_ext = reinterpret_cast<struct vec_ext *>(charPtr);
241281ad6265SDimitry Andric 
241381ad6265SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING("vr_saved=%d\n", vec_ext->vr_saved);
241481ad6265SDimitry Andric 
241581ad6265SDimitry Andric       // Restore vector register(s) if saved on the stack.
241681ad6265SDimitry Andric       if (vec_ext->vr_saved) {
241781ad6265SDimitry Andric         // Saved vector registers are 16-byte aligned.
241881ad6265SDimitry Andric         if (reinterpret_cast<uintptr_t>(ptrToRegs) % 16)
241981ad6265SDimitry Andric           ptrToRegs -= reinterpret_cast<uintptr_t>(ptrToRegs) % 16;
242081ad6265SDimitry Andric         v128 *VecRegs = reinterpret_cast<v128 *>(ptrToRegs - vec_ext->vr_saved *
242181ad6265SDimitry Andric                                                                  sizeof(v128));
242281ad6265SDimitry Andric         for (int i = 0; i < vec_ext->vr_saved; ++i) {
242381ad6265SDimitry Andric           newRegisters.setVectorRegister(
242481ad6265SDimitry Andric               32 - vec_ext->vr_saved + i + unwPPCV0Index, VecRegs[i]);
242581ad6265SDimitry Andric         }
242681ad6265SDimitry Andric       }
242781ad6265SDimitry Andric     }
242881ad6265SDimitry Andric     if (TBTable->tb.saves_cr) {
242981ad6265SDimitry Andric       // Get the saved condition register. The condition register is only
243081ad6265SDimitry Andric       // a single word.
243181ad6265SDimitry Andric       newRegisters.setCR(
243281ad6265SDimitry Andric           *(reinterpret_cast<uint32_t *>(lastStack + sizeof(uintptr_t))));
243381ad6265SDimitry Andric     }
243481ad6265SDimitry Andric 
243581ad6265SDimitry Andric     // Restore the SP.
243681ad6265SDimitry Andric     newRegisters.setSP(lastStack);
243781ad6265SDimitry Andric 
243881ad6265SDimitry Andric     // The first instruction after return.
243981ad6265SDimitry Andric     uint32_t firstInstruction = *(reinterpret_cast<uint32_t *>(returnAddress));
244081ad6265SDimitry Andric 
244181ad6265SDimitry Andric     // Do we need to set the TOC register?
244281ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
244381ad6265SDimitry Andric         "Current gpr2=%p\n",
244481ad6265SDimitry Andric         reinterpret_cast<void *>(newRegisters.getRegister(2)));
244581ad6265SDimitry Andric     if (firstInstruction == loadTOCRegInst) {
244681ad6265SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
244781ad6265SDimitry Andric           "Set gpr2=%p from frame\n",
244881ad6265SDimitry Andric           reinterpret_cast<void *>(reinterpret_cast<pint_t *>(lastStack)[5]));
244981ad6265SDimitry Andric       newRegisters.setRegister(2, reinterpret_cast<pint_t *>(lastStack)[5]);
245081ad6265SDimitry Andric     }
245181ad6265SDimitry Andric   }
245281ad6265SDimitry Andric   _LIBUNWIND_TRACE_UNWINDING("lastStack=%p, returnAddress=%p, pc=%p\n",
245381ad6265SDimitry Andric                              reinterpret_cast<void *>(lastStack),
245481ad6265SDimitry Andric                              reinterpret_cast<void *>(returnAddress),
245581ad6265SDimitry Andric                              reinterpret_cast<void *>(pc));
245681ad6265SDimitry Andric 
245781ad6265SDimitry Andric   // The return address is the address after call site instruction, so
245881ad6265SDimitry Andric   // setting IP to that simualates a return.
245981ad6265SDimitry Andric   newRegisters.setIP(reinterpret_cast<uintptr_t>(returnAddress));
246081ad6265SDimitry Andric 
246181ad6265SDimitry Andric   // Simulate the step by replacing the register set with the new ones.
246281ad6265SDimitry Andric   registers = newRegisters;
246381ad6265SDimitry Andric 
246481ad6265SDimitry Andric   // Check if the next frame is a signal frame.
246581ad6265SDimitry Andric   pint_t nextStack = *(reinterpret_cast<pint_t *>(registers.getSP()));
246681ad6265SDimitry Andric 
246781ad6265SDimitry Andric   // Return address is the address after call site instruction.
246881ad6265SDimitry Andric   pint_t nextReturnAddress = reinterpret_cast<pint_t *>(nextStack)[2];
246981ad6265SDimitry Andric 
247081ad6265SDimitry Andric   if (nextReturnAddress > 0x01 && nextReturnAddress < 0x10000) {
247181ad6265SDimitry Andric     _LIBUNWIND_TRACE_UNWINDING("The next is a signal handler frame: "
247281ad6265SDimitry Andric                                "nextStack=%p, next return address=%p\n",
247381ad6265SDimitry Andric                                reinterpret_cast<void *>(nextStack),
247481ad6265SDimitry Andric                                reinterpret_cast<void *>(nextReturnAddress));
247581ad6265SDimitry Andric     isSignalFrame = true;
247681ad6265SDimitry Andric   } else {
247781ad6265SDimitry Andric     isSignalFrame = false;
247881ad6265SDimitry Andric   }
247981ad6265SDimitry Andric 
248081ad6265SDimitry Andric   return UNW_STEP_SUCCESS;
248181ad6265SDimitry Andric }
248281ad6265SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
24830b57cec5SDimitry Andric 
24840b57cec5SDimitry Andric template <typename A, typename R>
24850b57cec5SDimitry Andric void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
248681ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
2487e8d8bef9SDimitry Andric   _isSigReturn = false;
2488e8d8bef9SDimitry Andric #endif
2489e8d8bef9SDimitry Andric 
2490e8d8bef9SDimitry Andric   pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
24910b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
24920b57cec5SDimitry Andric   // Remove the thumb bit so the IP represents the actual instruction address.
24930b57cec5SDimitry Andric   // This matches the behaviour of _Unwind_GetIP on arm.
24940b57cec5SDimitry Andric   pc &= (pint_t)~0x1;
24950b57cec5SDimitry Andric #endif
24960b57cec5SDimitry Andric 
24975ffd83dbSDimitry Andric   // Exit early if at the top of the stack.
24985ffd83dbSDimitry Andric   if (pc == 0) {
24995ffd83dbSDimitry Andric     _unwindInfoMissing = true;
25005ffd83dbSDimitry Andric     return;
25015ffd83dbSDimitry Andric   }
25025ffd83dbSDimitry Andric 
25030b57cec5SDimitry Andric   // If the last line of a function is a "throw" the compiler sometimes
25040b57cec5SDimitry Andric   // emits no instructions after the call to __cxa_throw.  This means
25050b57cec5SDimitry Andric   // the return address is actually the start of the next function.
25060b57cec5SDimitry Andric   // To disambiguate this, back up the pc when we know it is a return
25070b57cec5SDimitry Andric   // address.
25080b57cec5SDimitry Andric   if (isReturnAddress)
250981ad6265SDimitry Andric #if defined(_AIX)
251081ad6265SDimitry Andric     // PC needs to be a 4-byte aligned address to be able to look for a
251181ad6265SDimitry Andric     // word of 0 that indicates the start of the traceback table at the end
251281ad6265SDimitry Andric     // of a function on AIX.
251381ad6265SDimitry Andric     pc -= 4;
251481ad6265SDimitry Andric #else
25150b57cec5SDimitry Andric     --pc;
251681ad6265SDimitry Andric #endif
25170b57cec5SDimitry Andric 
25180b57cec5SDimitry Andric   // Ask address space object to find unwind sections for this pc.
25190b57cec5SDimitry Andric   UnwindInfoSections sects;
25200b57cec5SDimitry Andric   if (_addressSpace.findUnwindSections(pc, sects)) {
25210b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
25220b57cec5SDimitry Andric     // If there is a compact unwind encoding table, look there first.
25230b57cec5SDimitry Andric     if (sects.compact_unwind_section != 0) {
25240b57cec5SDimitry Andric       if (this->getInfoFromCompactEncodingSection(pc, sects)) {
25250b57cec5SDimitry Andric   #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
25260b57cec5SDimitry Andric         // Found info in table, done unless encoding says to use dwarf.
25270b57cec5SDimitry Andric         uint32_t dwarfOffset;
25280b57cec5SDimitry Andric         if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
25290b57cec5SDimitry Andric           if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
25300b57cec5SDimitry Andric             // found info in dwarf, done
25310b57cec5SDimitry Andric             return;
25320b57cec5SDimitry Andric           }
25330b57cec5SDimitry Andric         }
25340b57cec5SDimitry Andric   #endif
25350b57cec5SDimitry Andric         // If unwind table has entry, but entry says there is no unwind info,
25360b57cec5SDimitry Andric         // record that we have no unwind info.
25370b57cec5SDimitry Andric         if (_info.format == 0)
25380b57cec5SDimitry Andric           _unwindInfoMissing = true;
25390b57cec5SDimitry Andric         return;
25400b57cec5SDimitry Andric       }
25410b57cec5SDimitry Andric     }
25420b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
25430b57cec5SDimitry Andric 
25440b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
25450b57cec5SDimitry Andric     // If there is SEH unwind info, look there next.
25460b57cec5SDimitry Andric     if (this->getInfoFromSEH(pc))
25470b57cec5SDimitry Andric       return;
25480b57cec5SDimitry Andric #endif
25490b57cec5SDimitry Andric 
255081ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
255181ad6265SDimitry Andric     // If there is unwind info in the traceback table, look there next.
255281ad6265SDimitry Andric     if (this->getInfoFromTBTable(pc, _registers))
255381ad6265SDimitry Andric       return;
255481ad6265SDimitry Andric #endif
255581ad6265SDimitry Andric 
25560b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
25570b57cec5SDimitry Andric     // If there is dwarf unwind info, look there next.
25580b57cec5SDimitry Andric     if (sects.dwarf_section != 0) {
25590b57cec5SDimitry Andric       if (this->getInfoFromDwarfSection(pc, sects)) {
25600b57cec5SDimitry Andric         // found info in dwarf, done
25610b57cec5SDimitry Andric         return;
25620b57cec5SDimitry Andric       }
25630b57cec5SDimitry Andric     }
25640b57cec5SDimitry Andric #endif
25650b57cec5SDimitry Andric 
25660b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
25670b57cec5SDimitry Andric     // If there is ARM EHABI unwind info, look there next.
25680b57cec5SDimitry Andric     if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
25690b57cec5SDimitry Andric       return;
25700b57cec5SDimitry Andric #endif
25710b57cec5SDimitry Andric   }
25720b57cec5SDimitry Andric 
25730b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
25740b57cec5SDimitry Andric   // There is no static unwind info for this pc. Look to see if an FDE was
25750b57cec5SDimitry Andric   // dynamically registered for it.
2576e8d8bef9SDimitry Andric   pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll,
2577e8d8bef9SDimitry Andric                                                pc);
25780b57cec5SDimitry Andric   if (cachedFDE != 0) {
2579e8d8bef9SDimitry Andric     typename CFI_Parser<A>::FDE_Info fdeInfo;
2580e8d8bef9SDimitry Andric     typename CFI_Parser<A>::CIE_Info cieInfo;
2581e8d8bef9SDimitry Andric     if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo))
2582e8d8bef9SDimitry Andric       if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
25830b57cec5SDimitry Andric         return;
25840b57cec5SDimitry Andric   }
25850b57cec5SDimitry Andric 
25860b57cec5SDimitry Andric   // Lastly, ask AddressSpace object about platform specific ways to locate
25870b57cec5SDimitry Andric   // other FDEs.
25880b57cec5SDimitry Andric   pint_t fde;
25890b57cec5SDimitry Andric   if (_addressSpace.findOtherFDE(pc, fde)) {
2590e8d8bef9SDimitry Andric     typename CFI_Parser<A>::FDE_Info fdeInfo;
2591e8d8bef9SDimitry Andric     typename CFI_Parser<A>::CIE_Info cieInfo;
25920b57cec5SDimitry Andric     if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
25930b57cec5SDimitry Andric       // Double check this FDE is for a function that includes the pc.
2594e8d8bef9SDimitry Andric       if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd))
2595e8d8bef9SDimitry Andric         if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
25960b57cec5SDimitry Andric           return;
25970b57cec5SDimitry Andric     }
25980b57cec5SDimitry Andric   }
25990b57cec5SDimitry Andric #endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
26000b57cec5SDimitry Andric 
260181ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
2602e8d8bef9SDimitry Andric   if (setInfoForSigReturn())
2603e8d8bef9SDimitry Andric     return;
2604e8d8bef9SDimitry Andric #endif
2605e8d8bef9SDimitry Andric 
26060b57cec5SDimitry Andric   // no unwind info, flag that we can't reliably unwind
26070b57cec5SDimitry Andric   _unwindInfoMissing = true;
26080b57cec5SDimitry Andric }
26090b57cec5SDimitry Andric 
261081ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&                               \
261181ad6265SDimitry Andric     defined(_LIBUNWIND_TARGET_AARCH64)
2612e8d8bef9SDimitry Andric template <typename A, typename R>
2613e8d8bef9SDimitry Andric bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_arm64 &) {
2614e8d8bef9SDimitry Andric   // Look for the sigreturn trampoline. The trampoline's body is two
2615e8d8bef9SDimitry Andric   // specific instructions (see below). Typically the trampoline comes from the
2616e8d8bef9SDimitry Andric   // vDSO[1] (i.e. the __kernel_rt_sigreturn function). A libc might provide its
2617e8d8bef9SDimitry Andric   // own restorer function, though, or user-mode QEMU might write a trampoline
2618e8d8bef9SDimitry Andric   // onto the stack.
2619e8d8bef9SDimitry Andric   //
2620e8d8bef9SDimitry Andric   // This special code path is a fallback that is only used if the trampoline
2621e8d8bef9SDimitry Andric   // lacks proper (e.g. DWARF) unwind info. On AArch64, a new DWARF register
2622e8d8bef9SDimitry Andric   // constant for the PC needs to be defined before DWARF can handle a signal
2623e8d8bef9SDimitry Andric   // trampoline. This code may segfault if the target PC is unreadable, e.g.:
2624e8d8bef9SDimitry Andric   //  - The PC points at a function compiled without unwind info, and which is
2625e8d8bef9SDimitry Andric   //    part of an execute-only mapping (e.g. using -Wl,--execute-only).
2626e8d8bef9SDimitry Andric   //  - The PC is invalid and happens to point to unreadable or unmapped memory.
2627e8d8bef9SDimitry Andric   //
2628e8d8bef9SDimitry Andric   // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/vdso/sigreturn.S
2629e8d8bef9SDimitry Andric   const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
263081ad6265SDimitry Andric   // The PC might contain an invalid address if the unwind info is bad, so
263181ad6265SDimitry Andric   // directly accessing it could cause a segfault. Use process_vm_readv to read
263281ad6265SDimitry Andric   // the memory safely instead. process_vm_readv was added in Linux 3.2, and
263381ad6265SDimitry Andric   // AArch64 supported was added in Linux 3.7, so the syscall is guaranteed to
263481ad6265SDimitry Andric   // be present. Unfortunately, there are Linux AArch64 environments where the
263581ad6265SDimitry Andric   // libc wrapper for the syscall might not be present (e.g. Android 5), so call
263681ad6265SDimitry Andric   // the syscall directly instead.
263781ad6265SDimitry Andric   uint32_t instructions[2];
263881ad6265SDimitry Andric   struct iovec local_iov = {&instructions, sizeof instructions};
263981ad6265SDimitry Andric   struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof instructions};
264081ad6265SDimitry Andric   long bytesRead =
264181ad6265SDimitry Andric       syscall(SYS_process_vm_readv, getpid(), &local_iov, 1, &remote_iov, 1, 0);
2642e8d8bef9SDimitry Andric   // Look for instructions: mov x8, #0x8b; svc #0x0
264381ad6265SDimitry Andric   if (bytesRead != sizeof instructions || instructions[0] != 0xd2801168 ||
264481ad6265SDimitry Andric       instructions[1] != 0xd4000001)
264581ad6265SDimitry Andric     return false;
264681ad6265SDimitry Andric 
2647e8d8bef9SDimitry Andric   _info = {};
264881ad6265SDimitry Andric   _info.start_ip = pc;
264981ad6265SDimitry Andric   _info.end_ip = pc + 4;
2650e8d8bef9SDimitry Andric   _isSigReturn = true;
2651e8d8bef9SDimitry Andric   return true;
2652e8d8bef9SDimitry Andric }
2653e8d8bef9SDimitry Andric 
2654e8d8bef9SDimitry Andric template <typename A, typename R>
2655e8d8bef9SDimitry Andric int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) {
2656e8d8bef9SDimitry Andric   // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
2657e8d8bef9SDimitry Andric   //  - 128-byte siginfo struct
2658e8d8bef9SDimitry Andric   //  - ucontext struct:
2659e8d8bef9SDimitry Andric   //     - 8-byte long (uc_flags)
2660e8d8bef9SDimitry Andric   //     - 8-byte pointer (uc_link)
2661e8d8bef9SDimitry Andric   //     - 24-byte stack_t
2662e8d8bef9SDimitry Andric   //     - 128-byte signal set
2663e8d8bef9SDimitry Andric   //     - 8 bytes of padding because sigcontext has 16-byte alignment
2664e8d8bef9SDimitry Andric   //     - sigcontext/mcontext_t
2665e8d8bef9SDimitry Andric   // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c
2666e8d8bef9SDimitry Andric   const pint_t kOffsetSpToSigcontext = (128 + 8 + 8 + 24 + 128 + 8); // 304
2667e8d8bef9SDimitry Andric 
2668e8d8bef9SDimitry Andric   // Offsets from sigcontext to each register.
2669e8d8bef9SDimitry Andric   const pint_t kOffsetGprs = 8; // offset to "__u64 regs[31]" field
2670e8d8bef9SDimitry Andric   const pint_t kOffsetSp = 256; // offset to "__u64 sp" field
2671e8d8bef9SDimitry Andric   const pint_t kOffsetPc = 264; // offset to "__u64 pc" field
2672e8d8bef9SDimitry Andric 
2673e8d8bef9SDimitry Andric   pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
2674e8d8bef9SDimitry Andric 
2675e8d8bef9SDimitry Andric   for (int i = 0; i <= 30; ++i) {
2676e8d8bef9SDimitry Andric     uint64_t value = _addressSpace.get64(sigctx + kOffsetGprs +
2677e8d8bef9SDimitry Andric                                          static_cast<pint_t>(i * 8));
2678349cc55cSDimitry Andric     _registers.setRegister(UNW_AARCH64_X0 + i, value);
2679e8d8bef9SDimitry Andric   }
2680e8d8bef9SDimitry Andric   _registers.setSP(_addressSpace.get64(sigctx + kOffsetSp));
2681e8d8bef9SDimitry Andric   _registers.setIP(_addressSpace.get64(sigctx + kOffsetPc));
2682e8d8bef9SDimitry Andric   _isSignalFrame = true;
2683e8d8bef9SDimitry Andric   return UNW_STEP_SUCCESS;
2684e8d8bef9SDimitry Andric }
268581ad6265SDimitry Andric #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
268681ad6265SDimitry Andric        // defined(_LIBUNWIND_TARGET_AARCH64)
268781ad6265SDimitry Andric 
268881ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&                               \
268981ad6265SDimitry Andric     defined(_LIBUNWIND_TARGET_S390X)
269081ad6265SDimitry Andric template <typename A, typename R>
269181ad6265SDimitry Andric bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_s390x &) {
269281ad6265SDimitry Andric   // Look for the sigreturn trampoline. The trampoline's body is a
269381ad6265SDimitry Andric   // specific instruction (see below). Typically the trampoline comes from the
269481ad6265SDimitry Andric   // vDSO (i.e. the __kernel_[rt_]sigreturn function). A libc might provide its
269581ad6265SDimitry Andric   // own restorer function, though, or user-mode QEMU might write a trampoline
269681ad6265SDimitry Andric   // onto the stack.
269781ad6265SDimitry Andric   const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
2698*fcaf7f86SDimitry Andric   // The PC might contain an invalid address if the unwind info is bad, so
2699*fcaf7f86SDimitry Andric   // directly accessing it could cause a segfault. Use process_vm_readv to
2700*fcaf7f86SDimitry Andric   // read the memory safely instead.
2701*fcaf7f86SDimitry Andric   uint16_t inst;
2702*fcaf7f86SDimitry Andric   struct iovec local_iov = {&inst, sizeof inst};
2703*fcaf7f86SDimitry Andric   struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof inst};
2704*fcaf7f86SDimitry Andric   long bytesRead = process_vm_readv(getpid(), &local_iov, 1, &remote_iov, 1, 0);
2705*fcaf7f86SDimitry Andric   if (bytesRead == sizeof inst && (inst == 0x0a77 || inst == 0x0aad)) {
270681ad6265SDimitry Andric     _info = {};
270781ad6265SDimitry Andric     _info.start_ip = pc;
270881ad6265SDimitry Andric     _info.end_ip = pc + 2;
270981ad6265SDimitry Andric     _isSigReturn = true;
271081ad6265SDimitry Andric     return true;
271181ad6265SDimitry Andric   }
271281ad6265SDimitry Andric   return false;
271381ad6265SDimitry Andric }
271481ad6265SDimitry Andric 
271581ad6265SDimitry Andric template <typename A, typename R>
271681ad6265SDimitry Andric int UnwindCursor<A, R>::stepThroughSigReturn(Registers_s390x &) {
271781ad6265SDimitry Andric   // Determine current SP.
271881ad6265SDimitry Andric   const pint_t sp = static_cast<pint_t>(this->getReg(UNW_REG_SP));
271981ad6265SDimitry Andric   // According to the s390x ABI, the CFA is at (incoming) SP + 160.
272081ad6265SDimitry Andric   const pint_t cfa = sp + 160;
272181ad6265SDimitry Andric 
272281ad6265SDimitry Andric   // Determine current PC and instruction there (this must be either
272381ad6265SDimitry Andric   // a "svc __NR_sigreturn" or "svc __NR_rt_sigreturn").
272481ad6265SDimitry Andric   const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
272581ad6265SDimitry Andric   const uint16_t inst = _addressSpace.get16(pc);
272681ad6265SDimitry Andric 
272781ad6265SDimitry Andric   // Find the addresses of the signo and sigcontext in the frame.
272881ad6265SDimitry Andric   pint_t pSigctx = 0;
272981ad6265SDimitry Andric   pint_t pSigno = 0;
273081ad6265SDimitry Andric 
273181ad6265SDimitry Andric   // "svc __NR_sigreturn" uses a non-RT signal trampoline frame.
273281ad6265SDimitry Andric   if (inst == 0x0a77) {
273381ad6265SDimitry Andric     // Layout of a non-RT signal trampoline frame, starting at the CFA:
273481ad6265SDimitry Andric     //  - 8-byte signal mask
273581ad6265SDimitry Andric     //  - 8-byte pointer to sigcontext, followed by signo
273681ad6265SDimitry Andric     //  - 4-byte signo
273781ad6265SDimitry Andric     pSigctx = _addressSpace.get64(cfa + 8);
273881ad6265SDimitry Andric     pSigno = pSigctx + 344;
273981ad6265SDimitry Andric   }
274081ad6265SDimitry Andric 
274181ad6265SDimitry Andric   // "svc __NR_rt_sigreturn" uses a RT signal trampoline frame.
274281ad6265SDimitry Andric   if (inst == 0x0aad) {
274381ad6265SDimitry Andric     // Layout of a RT signal trampoline frame, starting at the CFA:
274481ad6265SDimitry Andric     //  - 8-byte retcode (+ alignment)
274581ad6265SDimitry Andric     //  - 128-byte siginfo struct (starts with signo)
274681ad6265SDimitry Andric     //  - ucontext struct:
274781ad6265SDimitry Andric     //     - 8-byte long (uc_flags)
274881ad6265SDimitry Andric     //     - 8-byte pointer (uc_link)
274981ad6265SDimitry Andric     //     - 24-byte stack_t
275081ad6265SDimitry Andric     //     - 8 bytes of padding because sigcontext has 16-byte alignment
275181ad6265SDimitry Andric     //     - sigcontext/mcontext_t
275281ad6265SDimitry Andric     pSigctx = cfa + 8 + 128 + 8 + 8 + 24 + 8;
275381ad6265SDimitry Andric     pSigno = cfa + 8;
275481ad6265SDimitry Andric   }
275581ad6265SDimitry Andric 
275681ad6265SDimitry Andric   assert(pSigctx != 0);
275781ad6265SDimitry Andric   assert(pSigno != 0);
275881ad6265SDimitry Andric 
275981ad6265SDimitry Andric   // Offsets from sigcontext to each register.
276081ad6265SDimitry Andric   const pint_t kOffsetPc = 8;
276181ad6265SDimitry Andric   const pint_t kOffsetGprs = 16;
276281ad6265SDimitry Andric   const pint_t kOffsetFprs = 216;
276381ad6265SDimitry Andric 
276481ad6265SDimitry Andric   // Restore all registers.
276581ad6265SDimitry Andric   for (int i = 0; i < 16; ++i) {
276681ad6265SDimitry Andric     uint64_t value = _addressSpace.get64(pSigctx + kOffsetGprs +
276781ad6265SDimitry Andric                                          static_cast<pint_t>(i * 8));
276881ad6265SDimitry Andric     _registers.setRegister(UNW_S390X_R0 + i, value);
276981ad6265SDimitry Andric   }
277081ad6265SDimitry Andric   for (int i = 0; i < 16; ++i) {
277181ad6265SDimitry Andric     static const int fpr[16] = {
277281ad6265SDimitry Andric       UNW_S390X_F0, UNW_S390X_F1, UNW_S390X_F2, UNW_S390X_F3,
277381ad6265SDimitry Andric       UNW_S390X_F4, UNW_S390X_F5, UNW_S390X_F6, UNW_S390X_F7,
277481ad6265SDimitry Andric       UNW_S390X_F8, UNW_S390X_F9, UNW_S390X_F10, UNW_S390X_F11,
277581ad6265SDimitry Andric       UNW_S390X_F12, UNW_S390X_F13, UNW_S390X_F14, UNW_S390X_F15
277681ad6265SDimitry Andric     };
277781ad6265SDimitry Andric     double value = _addressSpace.getDouble(pSigctx + kOffsetFprs +
277881ad6265SDimitry Andric                                            static_cast<pint_t>(i * 8));
277981ad6265SDimitry Andric     _registers.setFloatRegister(fpr[i], value);
278081ad6265SDimitry Andric   }
278181ad6265SDimitry Andric   _registers.setIP(_addressSpace.get64(pSigctx + kOffsetPc));
278281ad6265SDimitry Andric 
278381ad6265SDimitry Andric   // SIGILL, SIGFPE and SIGTRAP are delivered with psw_addr
278481ad6265SDimitry Andric   // after the faulting instruction rather than before it.
278581ad6265SDimitry Andric   // Do not set _isSignalFrame in that case.
278681ad6265SDimitry Andric   uint32_t signo = _addressSpace.get32(pSigno);
278781ad6265SDimitry Andric   _isSignalFrame = (signo != 4 && signo != 5 && signo != 8);
278881ad6265SDimitry Andric 
278981ad6265SDimitry Andric   return UNW_STEP_SUCCESS;
279081ad6265SDimitry Andric }
279181ad6265SDimitry Andric #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
279281ad6265SDimitry Andric        // defined(_LIBUNWIND_TARGET_S390X)
2793e8d8bef9SDimitry Andric 
27940b57cec5SDimitry Andric template <typename A, typename R>
27950b57cec5SDimitry Andric int UnwindCursor<A, R>::step() {
27960b57cec5SDimitry Andric   // Bottom of stack is defined is when unwind info cannot be found.
27970b57cec5SDimitry Andric   if (_unwindInfoMissing)
27980b57cec5SDimitry Andric     return UNW_STEP_END;
27990b57cec5SDimitry Andric 
28000b57cec5SDimitry Andric   // Use unwinding info to modify register set as if function returned.
28010b57cec5SDimitry Andric   int result;
280281ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
2803e8d8bef9SDimitry Andric   if (_isSigReturn) {
2804e8d8bef9SDimitry Andric     result = this->stepThroughSigReturn();
2805e8d8bef9SDimitry Andric   } else
2806e8d8bef9SDimitry Andric #endif
2807e8d8bef9SDimitry Andric   {
28080b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
28090b57cec5SDimitry Andric     result = this->stepWithCompactEncoding();
28100b57cec5SDimitry Andric #elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
28110b57cec5SDimitry Andric     result = this->stepWithSEHData();
281281ad6265SDimitry Andric #elif defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
281381ad6265SDimitry Andric     result = this->stepWithTBTableData();
28140b57cec5SDimitry Andric #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
28150b57cec5SDimitry Andric     result = this->stepWithDwarfFDE();
28160b57cec5SDimitry Andric #elif defined(_LIBUNWIND_ARM_EHABI)
28170b57cec5SDimitry Andric     result = this->stepWithEHABI();
28180b57cec5SDimitry Andric #else
28190b57cec5SDimitry Andric   #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
28200b57cec5SDimitry Andric               _LIBUNWIND_SUPPORT_SEH_UNWIND or \
28210b57cec5SDimitry Andric               _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
28220b57cec5SDimitry Andric               _LIBUNWIND_ARM_EHABI
28230b57cec5SDimitry Andric #endif
2824e8d8bef9SDimitry Andric   }
28250b57cec5SDimitry Andric 
28260b57cec5SDimitry Andric   // update info based on new PC
28270b57cec5SDimitry Andric   if (result == UNW_STEP_SUCCESS) {
28280b57cec5SDimitry Andric     this->setInfoBasedOnIPRegister(true);
28290b57cec5SDimitry Andric     if (_unwindInfoMissing)
28300b57cec5SDimitry Andric       return UNW_STEP_END;
28310b57cec5SDimitry Andric   }
28320b57cec5SDimitry Andric 
28330b57cec5SDimitry Andric   return result;
28340b57cec5SDimitry Andric }
28350b57cec5SDimitry Andric 
28360b57cec5SDimitry Andric template <typename A, typename R>
28370b57cec5SDimitry Andric void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
2838c2c6a179SDimitry Andric   if (_unwindInfoMissing)
2839c2c6a179SDimitry Andric     memset(info, 0, sizeof(*info));
2840c2c6a179SDimitry Andric   else
28410b57cec5SDimitry Andric     *info = _info;
28420b57cec5SDimitry Andric }
28430b57cec5SDimitry Andric 
28440b57cec5SDimitry Andric template <typename A, typename R>
28450b57cec5SDimitry Andric bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
28460b57cec5SDimitry Andric                                                            unw_word_t *offset) {
28470b57cec5SDimitry Andric   return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
28480b57cec5SDimitry Andric                                          buf, bufLen, offset);
28490b57cec5SDimitry Andric }
28500b57cec5SDimitry Andric 
2851349cc55cSDimitry Andric #if defined(_LIBUNWIND_USE_CET)
2852349cc55cSDimitry Andric extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) {
2853349cc55cSDimitry Andric   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
2854349cc55cSDimitry Andric   return co->get_registers();
2855349cc55cSDimitry Andric }
2856349cc55cSDimitry Andric #endif
28570b57cec5SDimitry Andric } // namespace libunwind
28580b57cec5SDimitry Andric 
28590b57cec5SDimitry Andric #endif // __UNWINDCURSOR_HPP__
2860