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 41*bdd1243dSDimitry Andric #include "AddressSpace.hpp" 42*bdd1243dSDimitry Andric #include "CompactUnwinder.hpp" 43*bdd1243dSDimitry Andric #include "config.h" 44*bdd1243dSDimitry Andric #include "DwarfInstructions.hpp" 45*bdd1243dSDimitry Andric #include "EHHeaderParser.hpp" 46*bdd1243dSDimitry Andric #include "libunwind.h" 47*bdd1243dSDimitry Andric #include "libunwind_ext.h" 48*bdd1243dSDimitry Andric #include "Registers.hpp" 49*bdd1243dSDimitry Andric #include "RWMutex.hpp" 50*bdd1243dSDimitry Andric #include "Unwind-EHABI.h" 51*bdd1243dSDimitry Andric 520b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 530b57cec5SDimitry Andric // Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and 540b57cec5SDimitry Andric // earlier) SDKs. 550b57cec5SDimitry Andric // MinGW-w64 has always provided this struct. 560b57cec5SDimitry Andric #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \ 570b57cec5SDimitry Andric !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000 580b57cec5SDimitry Andric struct _DISPATCHER_CONTEXT { 590b57cec5SDimitry Andric ULONG64 ControlPc; 600b57cec5SDimitry Andric ULONG64 ImageBase; 610b57cec5SDimitry Andric PRUNTIME_FUNCTION FunctionEntry; 620b57cec5SDimitry Andric ULONG64 EstablisherFrame; 630b57cec5SDimitry Andric ULONG64 TargetIp; 640b57cec5SDimitry Andric PCONTEXT ContextRecord; 650b57cec5SDimitry Andric PEXCEPTION_ROUTINE LanguageHandler; 660b57cec5SDimitry Andric PVOID HandlerData; 670b57cec5SDimitry Andric PUNWIND_HISTORY_TABLE HistoryTable; 680b57cec5SDimitry Andric ULONG ScopeIndex; 690b57cec5SDimitry Andric ULONG Fill0; 700b57cec5SDimitry Andric }; 710b57cec5SDimitry Andric #endif 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric struct UNWIND_INFO { 740b57cec5SDimitry Andric uint8_t Version : 3; 750b57cec5SDimitry Andric uint8_t Flags : 5; 760b57cec5SDimitry Andric uint8_t SizeOfProlog; 770b57cec5SDimitry Andric uint8_t CountOfCodes; 780b57cec5SDimitry Andric uint8_t FrameRegister : 4; 790b57cec5SDimitry Andric uint8_t FrameOffset : 4; 800b57cec5SDimitry Andric uint16_t UnwindCodes[2]; 810b57cec5SDimitry Andric }; 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric extern "C" _Unwind_Reason_Code __libunwind_seh_personality( 840b57cec5SDimitry Andric int, _Unwind_Action, uint64_t, _Unwind_Exception *, 850b57cec5SDimitry Andric struct _Unwind_Context *); 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric #endif 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric namespace libunwind { 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 920b57cec5SDimitry Andric /// Cache of recently found FDEs. 930b57cec5SDimitry Andric template <typename A> 940b57cec5SDimitry Andric class _LIBUNWIND_HIDDEN DwarfFDECache { 950b57cec5SDimitry Andric typedef typename A::pint_t pint_t; 960b57cec5SDimitry Andric public: 97e8d8bef9SDimitry Andric static constexpr pint_t kSearchAll = static_cast<pint_t>(-1); 980b57cec5SDimitry Andric static pint_t findFDE(pint_t mh, pint_t pc); 990b57cec5SDimitry Andric static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde); 1000b57cec5SDimitry Andric static void removeAllIn(pint_t mh); 1010b57cec5SDimitry Andric static void iterateCacheEntries(void (*func)(unw_word_t ip_start, 1020b57cec5SDimitry Andric unw_word_t ip_end, 1030b57cec5SDimitry Andric unw_word_t fde, unw_word_t mh)); 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric private: 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric struct entry { 1080b57cec5SDimitry Andric pint_t mh; 1090b57cec5SDimitry Andric pint_t ip_start; 1100b57cec5SDimitry Andric pint_t ip_end; 1110b57cec5SDimitry Andric pint_t fde; 1120b57cec5SDimitry Andric }; 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric // These fields are all static to avoid needing an initializer. 1150b57cec5SDimitry Andric // There is only one instance of this class per process. 1160b57cec5SDimitry Andric static RWMutex _lock; 1170b57cec5SDimitry Andric #ifdef __APPLE__ 1180b57cec5SDimitry Andric static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide); 1190b57cec5SDimitry Andric static bool _registeredForDyldUnloads; 1200b57cec5SDimitry Andric #endif 1210b57cec5SDimitry Andric static entry *_buffer; 1220b57cec5SDimitry Andric static entry *_bufferUsed; 1230b57cec5SDimitry Andric static entry *_bufferEnd; 1240b57cec5SDimitry Andric static entry _initialBuffer[64]; 1250b57cec5SDimitry Andric }; 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric template <typename A> 1280b57cec5SDimitry Andric typename DwarfFDECache<A>::entry * 1290b57cec5SDimitry Andric DwarfFDECache<A>::_buffer = _initialBuffer; 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric template <typename A> 1320b57cec5SDimitry Andric typename DwarfFDECache<A>::entry * 1330b57cec5SDimitry Andric DwarfFDECache<A>::_bufferUsed = _initialBuffer; 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric template <typename A> 1360b57cec5SDimitry Andric typename DwarfFDECache<A>::entry * 1370b57cec5SDimitry Andric DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64]; 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric template <typename A> 1400b57cec5SDimitry Andric typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64]; 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric template <typename A> 1430b57cec5SDimitry Andric RWMutex DwarfFDECache<A>::_lock; 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric #ifdef __APPLE__ 1460b57cec5SDimitry Andric template <typename A> 1470b57cec5SDimitry Andric bool DwarfFDECache<A>::_registeredForDyldUnloads = false; 1480b57cec5SDimitry Andric #endif 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric template <typename A> 1510b57cec5SDimitry Andric typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) { 1520b57cec5SDimitry Andric pint_t result = 0; 1530b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared()); 1540b57cec5SDimitry Andric for (entry *p = _buffer; p < _bufferUsed; ++p) { 155e8d8bef9SDimitry Andric if ((mh == p->mh) || (mh == kSearchAll)) { 1560b57cec5SDimitry Andric if ((p->ip_start <= pc) && (pc < p->ip_end)) { 1570b57cec5SDimitry Andric result = p->fde; 1580b57cec5SDimitry Andric break; 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared()); 1630b57cec5SDimitry Andric return result; 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric template <typename A> 1670b57cec5SDimitry Andric void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end, 1680b57cec5SDimitry Andric pint_t fde) { 1690b57cec5SDimitry Andric #if !defined(_LIBUNWIND_NO_HEAP) 1700b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); 1710b57cec5SDimitry Andric if (_bufferUsed >= _bufferEnd) { 1720b57cec5SDimitry Andric size_t oldSize = (size_t)(_bufferEnd - _buffer); 1730b57cec5SDimitry Andric size_t newSize = oldSize * 4; 1740b57cec5SDimitry Andric // Can't use operator new (we are below it). 1750b57cec5SDimitry Andric entry *newBuffer = (entry *)malloc(newSize * sizeof(entry)); 1760b57cec5SDimitry Andric memcpy(newBuffer, _buffer, oldSize * sizeof(entry)); 1770b57cec5SDimitry Andric if (_buffer != _initialBuffer) 1780b57cec5SDimitry Andric free(_buffer); 1790b57cec5SDimitry Andric _buffer = newBuffer; 1800b57cec5SDimitry Andric _bufferUsed = &newBuffer[oldSize]; 1810b57cec5SDimitry Andric _bufferEnd = &newBuffer[newSize]; 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric _bufferUsed->mh = mh; 1840b57cec5SDimitry Andric _bufferUsed->ip_start = ip_start; 1850b57cec5SDimitry Andric _bufferUsed->ip_end = ip_end; 1860b57cec5SDimitry Andric _bufferUsed->fde = fde; 1870b57cec5SDimitry Andric ++_bufferUsed; 1880b57cec5SDimitry Andric #ifdef __APPLE__ 1890b57cec5SDimitry Andric if (!_registeredForDyldUnloads) { 1900b57cec5SDimitry Andric _dyld_register_func_for_remove_image(&dyldUnloadHook); 1910b57cec5SDimitry Andric _registeredForDyldUnloads = true; 1920b57cec5SDimitry Andric } 1930b57cec5SDimitry Andric #endif 1940b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); 1950b57cec5SDimitry Andric #endif 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric template <typename A> 1990b57cec5SDimitry Andric void DwarfFDECache<A>::removeAllIn(pint_t mh) { 2000b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); 2010b57cec5SDimitry Andric entry *d = _buffer; 2020b57cec5SDimitry Andric for (const entry *s = _buffer; s < _bufferUsed; ++s) { 2030b57cec5SDimitry Andric if (s->mh != mh) { 2040b57cec5SDimitry Andric if (d != s) 2050b57cec5SDimitry Andric *d = *s; 2060b57cec5SDimitry Andric ++d; 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric _bufferUsed = d; 2100b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric #ifdef __APPLE__ 2140b57cec5SDimitry Andric template <typename A> 2150b57cec5SDimitry Andric void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) { 2160b57cec5SDimitry Andric removeAllIn((pint_t) mh); 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric #endif 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric template <typename A> 2210b57cec5SDimitry Andric void DwarfFDECache<A>::iterateCacheEntries(void (*func)( 2220b57cec5SDimitry Andric unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) { 2230b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); 2240b57cec5SDimitry Andric for (entry *p = _buffer; p < _bufferUsed; ++p) { 2250b57cec5SDimitry Andric (*func)(p->ip_start, p->ip_end, p->fde, p->mh); 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric #define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field)) 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 2350b57cec5SDimitry Andric template <typename A> class UnwindSectionHeader { 2360b57cec5SDimitry Andric public: 2370b57cec5SDimitry Andric UnwindSectionHeader(A &addressSpace, typename A::pint_t addr) 2380b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric uint32_t version() const { 2410b57cec5SDimitry Andric return _addressSpace.get32(_addr + 2420b57cec5SDimitry Andric offsetof(unwind_info_section_header, version)); 2430b57cec5SDimitry Andric } 2440b57cec5SDimitry Andric uint32_t commonEncodingsArraySectionOffset() const { 2450b57cec5SDimitry Andric return _addressSpace.get32(_addr + 2460b57cec5SDimitry Andric offsetof(unwind_info_section_header, 2470b57cec5SDimitry Andric commonEncodingsArraySectionOffset)); 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric uint32_t commonEncodingsArrayCount() const { 2500b57cec5SDimitry Andric return _addressSpace.get32(_addr + offsetof(unwind_info_section_header, 2510b57cec5SDimitry Andric commonEncodingsArrayCount)); 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric uint32_t personalityArraySectionOffset() const { 2540b57cec5SDimitry Andric return _addressSpace.get32(_addr + offsetof(unwind_info_section_header, 2550b57cec5SDimitry Andric personalityArraySectionOffset)); 2560b57cec5SDimitry Andric } 2570b57cec5SDimitry Andric uint32_t personalityArrayCount() const { 2580b57cec5SDimitry Andric return _addressSpace.get32( 2590b57cec5SDimitry Andric _addr + offsetof(unwind_info_section_header, personalityArrayCount)); 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric uint32_t indexSectionOffset() const { 2620b57cec5SDimitry Andric return _addressSpace.get32( 2630b57cec5SDimitry Andric _addr + offsetof(unwind_info_section_header, indexSectionOffset)); 2640b57cec5SDimitry Andric } 2650b57cec5SDimitry Andric uint32_t indexCount() const { 2660b57cec5SDimitry Andric return _addressSpace.get32( 2670b57cec5SDimitry Andric _addr + offsetof(unwind_info_section_header, indexCount)); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric private: 2710b57cec5SDimitry Andric A &_addressSpace; 2720b57cec5SDimitry Andric typename A::pint_t _addr; 2730b57cec5SDimitry Andric }; 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric template <typename A> class UnwindSectionIndexArray { 2760b57cec5SDimitry Andric public: 2770b57cec5SDimitry Andric UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr) 2780b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric uint32_t functionOffset(uint32_t index) const { 2810b57cec5SDimitry Andric return _addressSpace.get32( 2820b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, 2830b57cec5SDimitry Andric functionOffset)); 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric uint32_t secondLevelPagesSectionOffset(uint32_t index) const { 2860b57cec5SDimitry Andric return _addressSpace.get32( 2870b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, 2880b57cec5SDimitry Andric secondLevelPagesSectionOffset)); 2890b57cec5SDimitry Andric } 2900b57cec5SDimitry Andric uint32_t lsdaIndexArraySectionOffset(uint32_t index) const { 2910b57cec5SDimitry Andric return _addressSpace.get32( 2920b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, 2930b57cec5SDimitry Andric lsdaIndexArraySectionOffset)); 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric private: 2970b57cec5SDimitry Andric A &_addressSpace; 2980b57cec5SDimitry Andric typename A::pint_t _addr; 2990b57cec5SDimitry Andric }; 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric template <typename A> class UnwindSectionRegularPageHeader { 3020b57cec5SDimitry Andric public: 3030b57cec5SDimitry Andric UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr) 3040b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric uint32_t kind() const { 3070b57cec5SDimitry Andric return _addressSpace.get32( 3080b57cec5SDimitry Andric _addr + offsetof(unwind_info_regular_second_level_page_header, kind)); 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric uint16_t entryPageOffset() const { 3110b57cec5SDimitry Andric return _addressSpace.get16( 3120b57cec5SDimitry Andric _addr + offsetof(unwind_info_regular_second_level_page_header, 3130b57cec5SDimitry Andric entryPageOffset)); 3140b57cec5SDimitry Andric } 3150b57cec5SDimitry Andric uint16_t entryCount() const { 3160b57cec5SDimitry Andric return _addressSpace.get16( 3170b57cec5SDimitry Andric _addr + 3180b57cec5SDimitry Andric offsetof(unwind_info_regular_second_level_page_header, entryCount)); 3190b57cec5SDimitry Andric } 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric private: 3220b57cec5SDimitry Andric A &_addressSpace; 3230b57cec5SDimitry Andric typename A::pint_t _addr; 3240b57cec5SDimitry Andric }; 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric template <typename A> class UnwindSectionRegularArray { 3270b57cec5SDimitry Andric public: 3280b57cec5SDimitry Andric UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr) 3290b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric uint32_t functionOffset(uint32_t index) const { 3320b57cec5SDimitry Andric return _addressSpace.get32( 3330b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index, 3340b57cec5SDimitry Andric functionOffset)); 3350b57cec5SDimitry Andric } 3360b57cec5SDimitry Andric uint32_t encoding(uint32_t index) const { 3370b57cec5SDimitry Andric return _addressSpace.get32( 3380b57cec5SDimitry Andric _addr + 3390b57cec5SDimitry Andric arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding)); 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric private: 3430b57cec5SDimitry Andric A &_addressSpace; 3440b57cec5SDimitry Andric typename A::pint_t _addr; 3450b57cec5SDimitry Andric }; 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric template <typename A> class UnwindSectionCompressedPageHeader { 3480b57cec5SDimitry Andric public: 3490b57cec5SDimitry Andric UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr) 3500b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric uint32_t kind() const { 3530b57cec5SDimitry Andric return _addressSpace.get32( 3540b57cec5SDimitry Andric _addr + 3550b57cec5SDimitry Andric offsetof(unwind_info_compressed_second_level_page_header, kind)); 3560b57cec5SDimitry Andric } 3570b57cec5SDimitry Andric uint16_t entryPageOffset() const { 3580b57cec5SDimitry Andric return _addressSpace.get16( 3590b57cec5SDimitry Andric _addr + offsetof(unwind_info_compressed_second_level_page_header, 3600b57cec5SDimitry Andric entryPageOffset)); 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric uint16_t entryCount() const { 3630b57cec5SDimitry Andric return _addressSpace.get16( 3640b57cec5SDimitry Andric _addr + 3650b57cec5SDimitry Andric offsetof(unwind_info_compressed_second_level_page_header, entryCount)); 3660b57cec5SDimitry Andric } 3670b57cec5SDimitry Andric uint16_t encodingsPageOffset() const { 3680b57cec5SDimitry Andric return _addressSpace.get16( 3690b57cec5SDimitry Andric _addr + offsetof(unwind_info_compressed_second_level_page_header, 3700b57cec5SDimitry Andric encodingsPageOffset)); 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric uint16_t encodingsCount() const { 3730b57cec5SDimitry Andric return _addressSpace.get16( 3740b57cec5SDimitry Andric _addr + offsetof(unwind_info_compressed_second_level_page_header, 3750b57cec5SDimitry Andric encodingsCount)); 3760b57cec5SDimitry Andric } 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric private: 3790b57cec5SDimitry Andric A &_addressSpace; 3800b57cec5SDimitry Andric typename A::pint_t _addr; 3810b57cec5SDimitry Andric }; 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric template <typename A> class UnwindSectionCompressedArray { 3840b57cec5SDimitry Andric public: 3850b57cec5SDimitry Andric UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr) 3860b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric uint32_t functionOffset(uint32_t index) const { 3890b57cec5SDimitry Andric return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET( 3900b57cec5SDimitry Andric _addressSpace.get32(_addr + index * sizeof(uint32_t))); 3910b57cec5SDimitry Andric } 3920b57cec5SDimitry Andric uint16_t encodingIndex(uint32_t index) const { 3930b57cec5SDimitry Andric return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX( 3940b57cec5SDimitry Andric _addressSpace.get32(_addr + index * sizeof(uint32_t))); 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric private: 3980b57cec5SDimitry Andric A &_addressSpace; 3990b57cec5SDimitry Andric typename A::pint_t _addr; 4000b57cec5SDimitry Andric }; 4010b57cec5SDimitry Andric 4020b57cec5SDimitry Andric template <typename A> class UnwindSectionLsdaArray { 4030b57cec5SDimitry Andric public: 4040b57cec5SDimitry Andric UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr) 4050b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric uint32_t functionOffset(uint32_t index) const { 4080b57cec5SDimitry Andric return _addressSpace.get32( 4090b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry, 4100b57cec5SDimitry Andric index, functionOffset)); 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric uint32_t lsdaOffset(uint32_t index) const { 4130b57cec5SDimitry Andric return _addressSpace.get32( 4140b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry, 4150b57cec5SDimitry Andric index, lsdaOffset)); 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric private: 4190b57cec5SDimitry Andric A &_addressSpace; 4200b57cec5SDimitry Andric typename A::pint_t _addr; 4210b57cec5SDimitry Andric }; 4220b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric class _LIBUNWIND_HIDDEN AbstractUnwindCursor { 4250b57cec5SDimitry Andric public: 4260b57cec5SDimitry Andric // NOTE: provide a class specific placement deallocation function (S5.3.4 p20) 4270b57cec5SDimitry Andric // This avoids an unnecessary dependency to libc++abi. 4280b57cec5SDimitry Andric void operator delete(void *, size_t) {} 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric virtual ~AbstractUnwindCursor() {} 4310b57cec5SDimitry Andric virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); } 4320b57cec5SDimitry Andric virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); } 4330b57cec5SDimitry Andric virtual void setReg(int, unw_word_t) { 4340b57cec5SDimitry Andric _LIBUNWIND_ABORT("setReg not implemented"); 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric virtual bool validFloatReg(int) { 4370b57cec5SDimitry Andric _LIBUNWIND_ABORT("validFloatReg not implemented"); 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric virtual unw_fpreg_t getFloatReg(int) { 4400b57cec5SDimitry Andric _LIBUNWIND_ABORT("getFloatReg not implemented"); 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric virtual void setFloatReg(int, unw_fpreg_t) { 4430b57cec5SDimitry Andric _LIBUNWIND_ABORT("setFloatReg not implemented"); 4440b57cec5SDimitry Andric } 445*bdd1243dSDimitry Andric virtual int step(bool = false) { _LIBUNWIND_ABORT("step not implemented"); } 4460b57cec5SDimitry Andric virtual void getInfo(unw_proc_info_t *) { 4470b57cec5SDimitry Andric _LIBUNWIND_ABORT("getInfo not implemented"); 4480b57cec5SDimitry Andric } 4490b57cec5SDimitry Andric virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); } 4500b57cec5SDimitry Andric virtual bool isSignalFrame() { 4510b57cec5SDimitry Andric _LIBUNWIND_ABORT("isSignalFrame not implemented"); 4520b57cec5SDimitry Andric } 4530b57cec5SDimitry Andric virtual bool getFunctionName(char *, size_t, unw_word_t *) { 4540b57cec5SDimitry Andric _LIBUNWIND_ABORT("getFunctionName not implemented"); 4550b57cec5SDimitry Andric } 4560b57cec5SDimitry Andric virtual void setInfoBasedOnIPRegister(bool = false) { 4570b57cec5SDimitry Andric _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented"); 4580b57cec5SDimitry Andric } 4590b57cec5SDimitry Andric virtual const char *getRegisterName(int) { 4600b57cec5SDimitry Andric _LIBUNWIND_ABORT("getRegisterName not implemented"); 4610b57cec5SDimitry Andric } 4620b57cec5SDimitry Andric #ifdef __arm__ 4630b57cec5SDimitry Andric virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); } 4640b57cec5SDimitry Andric #endif 465349cc55cSDimitry Andric 46681ad6265SDimitry Andric #ifdef _AIX 46781ad6265SDimitry Andric virtual uintptr_t getDataRelBase() { 46881ad6265SDimitry Andric _LIBUNWIND_ABORT("getDataRelBase not implemented"); 46981ad6265SDimitry Andric } 47081ad6265SDimitry Andric #endif 47181ad6265SDimitry Andric 472349cc55cSDimitry Andric #if defined(_LIBUNWIND_USE_CET) 473349cc55cSDimitry Andric virtual void *get_registers() { 474349cc55cSDimitry Andric _LIBUNWIND_ABORT("get_registers not implemented"); 475349cc55cSDimitry Andric } 476349cc55cSDimitry Andric #endif 4770b57cec5SDimitry Andric }; 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32) 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric /// \c UnwindCursor contains all state (including all register values) during 4820b57cec5SDimitry Andric /// an unwind. This is normally stack-allocated inside a unw_cursor_t. 4830b57cec5SDimitry Andric template <typename A, typename R> 4840b57cec5SDimitry Andric class UnwindCursor : public AbstractUnwindCursor { 4850b57cec5SDimitry Andric typedef typename A::pint_t pint_t; 4860b57cec5SDimitry Andric public: 4870b57cec5SDimitry Andric UnwindCursor(unw_context_t *context, A &as); 4880b57cec5SDimitry Andric UnwindCursor(CONTEXT *context, A &as); 4890b57cec5SDimitry Andric UnwindCursor(A &as, void *threadArg); 4900b57cec5SDimitry Andric virtual ~UnwindCursor() {} 4910b57cec5SDimitry Andric virtual bool validReg(int); 4920b57cec5SDimitry Andric virtual unw_word_t getReg(int); 4930b57cec5SDimitry Andric virtual void setReg(int, unw_word_t); 4940b57cec5SDimitry Andric virtual bool validFloatReg(int); 4950b57cec5SDimitry Andric virtual unw_fpreg_t getFloatReg(int); 4960b57cec5SDimitry Andric virtual void setFloatReg(int, unw_fpreg_t); 497*bdd1243dSDimitry Andric virtual int step(bool = false); 4980b57cec5SDimitry Andric virtual void getInfo(unw_proc_info_t *); 4990b57cec5SDimitry Andric virtual void jumpto(); 5000b57cec5SDimitry Andric virtual bool isSignalFrame(); 5010b57cec5SDimitry Andric virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off); 5020b57cec5SDimitry Andric virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false); 5030b57cec5SDimitry Andric virtual const char *getRegisterName(int num); 5040b57cec5SDimitry Andric #ifdef __arm__ 5050b57cec5SDimitry Andric virtual void saveVFPAsX(); 5060b57cec5SDimitry Andric #endif 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; } 5090b57cec5SDimitry Andric void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; } 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andric // libunwind does not and should not depend on C++ library which means that we 512*bdd1243dSDimitry Andric // need our own definition of inline placement new. 5130b57cec5SDimitry Andric static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; } 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric private: 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric pint_t getLastPC() const { return _dispContext.ControlPc; } 5180b57cec5SDimitry Andric void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; } 5190b57cec5SDimitry Andric RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) { 52081ad6265SDimitry Andric #ifdef __arm__ 52181ad6265SDimitry Andric // Remove the thumb bit; FunctionEntry ranges don't include the thumb bit. 52281ad6265SDimitry Andric pc &= ~1U; 52381ad6265SDimitry Andric #endif 52481ad6265SDimitry Andric // If pc points exactly at the end of the range, we might resolve the 52581ad6265SDimitry Andric // next function instead. Decrement pc by 1 to fit inside the current 52681ad6265SDimitry Andric // function. 52781ad6265SDimitry Andric pc -= 1; 5280b57cec5SDimitry Andric _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc, 5290b57cec5SDimitry Andric &_dispContext.ImageBase, 5300b57cec5SDimitry Andric _dispContext.HistoryTable); 5310b57cec5SDimitry Andric *base = _dispContext.ImageBase; 5320b57cec5SDimitry Andric return _dispContext.FunctionEntry; 5330b57cec5SDimitry Andric } 5340b57cec5SDimitry Andric bool getInfoFromSEH(pint_t pc); 5350b57cec5SDimitry Andric int stepWithSEHData() { 5360b57cec5SDimitry Andric _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER, 5370b57cec5SDimitry Andric _dispContext.ImageBase, 5380b57cec5SDimitry Andric _dispContext.ControlPc, 5390b57cec5SDimitry Andric _dispContext.FunctionEntry, 5400b57cec5SDimitry Andric _dispContext.ContextRecord, 5410b57cec5SDimitry Andric &_dispContext.HandlerData, 5420b57cec5SDimitry Andric &_dispContext.EstablisherFrame, 5430b57cec5SDimitry Andric NULL); 5440b57cec5SDimitry Andric // Update some fields of the unwind info now, since we have them. 5450b57cec5SDimitry Andric _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData); 5460b57cec5SDimitry Andric if (_dispContext.LanguageHandler) { 5470b57cec5SDimitry Andric _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality); 5480b57cec5SDimitry Andric } else 5490b57cec5SDimitry Andric _info.handler = 0; 5500b57cec5SDimitry Andric return UNW_STEP_SUCCESS; 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric A &_addressSpace; 5540b57cec5SDimitry Andric unw_proc_info_t _info; 5550b57cec5SDimitry Andric DISPATCHER_CONTEXT _dispContext; 5560b57cec5SDimitry Andric CONTEXT _msContext; 5570b57cec5SDimitry Andric UNWIND_HISTORY_TABLE _histTable; 5580b57cec5SDimitry Andric bool _unwindInfoMissing; 5590b57cec5SDimitry Andric }; 5600b57cec5SDimitry Andric 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric template <typename A, typename R> 5630b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as) 5640b57cec5SDimitry Andric : _addressSpace(as), _unwindInfoMissing(false) { 5650b57cec5SDimitry Andric static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), 5660b57cec5SDimitry Andric "UnwindCursor<> does not fit in unw_cursor_t"); 567e8d8bef9SDimitry Andric static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)), 568e8d8bef9SDimitry Andric "UnwindCursor<> requires more alignment than unw_cursor_t"); 5690b57cec5SDimitry Andric memset(&_info, 0, sizeof(_info)); 5700b57cec5SDimitry Andric memset(&_histTable, 0, sizeof(_histTable)); 5710b57cec5SDimitry Andric _dispContext.ContextRecord = &_msContext; 5720b57cec5SDimitry Andric _dispContext.HistoryTable = &_histTable; 5730b57cec5SDimitry Andric // Initialize MS context from ours. 5740b57cec5SDimitry Andric R r(context); 5750b57cec5SDimitry Andric _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT; 5760b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 5770b57cec5SDimitry Andric _msContext.Rax = r.getRegister(UNW_X86_64_RAX); 5780b57cec5SDimitry Andric _msContext.Rcx = r.getRegister(UNW_X86_64_RCX); 5790b57cec5SDimitry Andric _msContext.Rdx = r.getRegister(UNW_X86_64_RDX); 5800b57cec5SDimitry Andric _msContext.Rbx = r.getRegister(UNW_X86_64_RBX); 5810b57cec5SDimitry Andric _msContext.Rsp = r.getRegister(UNW_X86_64_RSP); 5820b57cec5SDimitry Andric _msContext.Rbp = r.getRegister(UNW_X86_64_RBP); 5830b57cec5SDimitry Andric _msContext.Rsi = r.getRegister(UNW_X86_64_RSI); 5840b57cec5SDimitry Andric _msContext.Rdi = r.getRegister(UNW_X86_64_RDI); 5850b57cec5SDimitry Andric _msContext.R8 = r.getRegister(UNW_X86_64_R8); 5860b57cec5SDimitry Andric _msContext.R9 = r.getRegister(UNW_X86_64_R9); 5870b57cec5SDimitry Andric _msContext.R10 = r.getRegister(UNW_X86_64_R10); 5880b57cec5SDimitry Andric _msContext.R11 = r.getRegister(UNW_X86_64_R11); 5890b57cec5SDimitry Andric _msContext.R12 = r.getRegister(UNW_X86_64_R12); 5900b57cec5SDimitry Andric _msContext.R13 = r.getRegister(UNW_X86_64_R13); 5910b57cec5SDimitry Andric _msContext.R14 = r.getRegister(UNW_X86_64_R14); 5920b57cec5SDimitry Andric _msContext.R15 = r.getRegister(UNW_X86_64_R15); 5930b57cec5SDimitry Andric _msContext.Rip = r.getRegister(UNW_REG_IP); 5940b57cec5SDimitry Andric union { 5950b57cec5SDimitry Andric v128 v; 5960b57cec5SDimitry Andric M128A m; 5970b57cec5SDimitry Andric } t; 5980b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM0); 5990b57cec5SDimitry Andric _msContext.Xmm0 = t.m; 6000b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM1); 6010b57cec5SDimitry Andric _msContext.Xmm1 = t.m; 6020b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM2); 6030b57cec5SDimitry Andric _msContext.Xmm2 = t.m; 6040b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM3); 6050b57cec5SDimitry Andric _msContext.Xmm3 = t.m; 6060b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM4); 6070b57cec5SDimitry Andric _msContext.Xmm4 = t.m; 6080b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM5); 6090b57cec5SDimitry Andric _msContext.Xmm5 = t.m; 6100b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM6); 6110b57cec5SDimitry Andric _msContext.Xmm6 = t.m; 6120b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM7); 6130b57cec5SDimitry Andric _msContext.Xmm7 = t.m; 6140b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM8); 6150b57cec5SDimitry Andric _msContext.Xmm8 = t.m; 6160b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM9); 6170b57cec5SDimitry Andric _msContext.Xmm9 = t.m; 6180b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM10); 6190b57cec5SDimitry Andric _msContext.Xmm10 = t.m; 6200b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM11); 6210b57cec5SDimitry Andric _msContext.Xmm11 = t.m; 6220b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM12); 6230b57cec5SDimitry Andric _msContext.Xmm12 = t.m; 6240b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM13); 6250b57cec5SDimitry Andric _msContext.Xmm13 = t.m; 6260b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM14); 6270b57cec5SDimitry Andric _msContext.Xmm14 = t.m; 6280b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM15); 6290b57cec5SDimitry Andric _msContext.Xmm15 = t.m; 6300b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM) 6310b57cec5SDimitry Andric _msContext.R0 = r.getRegister(UNW_ARM_R0); 6320b57cec5SDimitry Andric _msContext.R1 = r.getRegister(UNW_ARM_R1); 6330b57cec5SDimitry Andric _msContext.R2 = r.getRegister(UNW_ARM_R2); 6340b57cec5SDimitry Andric _msContext.R3 = r.getRegister(UNW_ARM_R3); 6350b57cec5SDimitry Andric _msContext.R4 = r.getRegister(UNW_ARM_R4); 6360b57cec5SDimitry Andric _msContext.R5 = r.getRegister(UNW_ARM_R5); 6370b57cec5SDimitry Andric _msContext.R6 = r.getRegister(UNW_ARM_R6); 6380b57cec5SDimitry Andric _msContext.R7 = r.getRegister(UNW_ARM_R7); 6390b57cec5SDimitry Andric _msContext.R8 = r.getRegister(UNW_ARM_R8); 6400b57cec5SDimitry Andric _msContext.R9 = r.getRegister(UNW_ARM_R9); 6410b57cec5SDimitry Andric _msContext.R10 = r.getRegister(UNW_ARM_R10); 6420b57cec5SDimitry Andric _msContext.R11 = r.getRegister(UNW_ARM_R11); 6430b57cec5SDimitry Andric _msContext.R12 = r.getRegister(UNW_ARM_R12); 6440b57cec5SDimitry Andric _msContext.Sp = r.getRegister(UNW_ARM_SP); 6450b57cec5SDimitry Andric _msContext.Lr = r.getRegister(UNW_ARM_LR); 6460b57cec5SDimitry Andric _msContext.Pc = r.getRegister(UNW_ARM_IP); 6470b57cec5SDimitry Andric for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) { 6480b57cec5SDimitry Andric union { 6490b57cec5SDimitry Andric uint64_t w; 6500b57cec5SDimitry Andric double d; 6510b57cec5SDimitry Andric } d; 6520b57cec5SDimitry Andric d.d = r.getFloatRegister(i); 6530b57cec5SDimitry Andric _msContext.D[i - UNW_ARM_D0] = d.w; 6540b57cec5SDimitry Andric } 6550b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 656349cc55cSDimitry Andric for (int i = UNW_AARCH64_X0; i <= UNW_ARM64_X30; ++i) 657349cc55cSDimitry Andric _msContext.X[i - UNW_AARCH64_X0] = r.getRegister(i); 6580b57cec5SDimitry Andric _msContext.Sp = r.getRegister(UNW_REG_SP); 6590b57cec5SDimitry Andric _msContext.Pc = r.getRegister(UNW_REG_IP); 660349cc55cSDimitry Andric for (int i = UNW_AARCH64_V0; i <= UNW_ARM64_D31; ++i) 661349cc55cSDimitry Andric _msContext.V[i - UNW_AARCH64_V0].D[0] = r.getFloatRegister(i); 6620b57cec5SDimitry Andric #endif 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric 6650b57cec5SDimitry Andric template <typename A, typename R> 6660b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as) 6670b57cec5SDimitry Andric : _addressSpace(as), _unwindInfoMissing(false) { 6680b57cec5SDimitry Andric static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), 6690b57cec5SDimitry Andric "UnwindCursor<> does not fit in unw_cursor_t"); 6700b57cec5SDimitry Andric memset(&_info, 0, sizeof(_info)); 6710b57cec5SDimitry Andric memset(&_histTable, 0, sizeof(_histTable)); 6720b57cec5SDimitry Andric _dispContext.ContextRecord = &_msContext; 6730b57cec5SDimitry Andric _dispContext.HistoryTable = &_histTable; 6740b57cec5SDimitry Andric _msContext = *context; 6750b57cec5SDimitry Andric } 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric template <typename A, typename R> 6790b57cec5SDimitry Andric bool UnwindCursor<A, R>::validReg(int regNum) { 6800b57cec5SDimitry Andric if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true; 6810b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 6820b57cec5SDimitry Andric if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true; 6830b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM) 6840eae32dcSDimitry Andric if ((regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) || 6850eae32dcSDimitry Andric regNum == UNW_ARM_RA_AUTH_CODE) 6860eae32dcSDimitry Andric return true; 6870b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 688349cc55cSDimitry Andric if (regNum >= UNW_AARCH64_X0 && regNum <= UNW_ARM64_X30) return true; 6890b57cec5SDimitry Andric #endif 6900b57cec5SDimitry Andric return false; 6910b57cec5SDimitry Andric } 6920b57cec5SDimitry Andric 6930b57cec5SDimitry Andric template <typename A, typename R> 6940b57cec5SDimitry Andric unw_word_t UnwindCursor<A, R>::getReg(int regNum) { 6950b57cec5SDimitry Andric switch (regNum) { 6960b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 6970b57cec5SDimitry Andric case UNW_REG_IP: return _msContext.Rip; 6980b57cec5SDimitry Andric case UNW_X86_64_RAX: return _msContext.Rax; 6990b57cec5SDimitry Andric case UNW_X86_64_RDX: return _msContext.Rdx; 7000b57cec5SDimitry Andric case UNW_X86_64_RCX: return _msContext.Rcx; 7010b57cec5SDimitry Andric case UNW_X86_64_RBX: return _msContext.Rbx; 7020b57cec5SDimitry Andric case UNW_REG_SP: 7030b57cec5SDimitry Andric case UNW_X86_64_RSP: return _msContext.Rsp; 7040b57cec5SDimitry Andric case UNW_X86_64_RBP: return _msContext.Rbp; 7050b57cec5SDimitry Andric case UNW_X86_64_RSI: return _msContext.Rsi; 7060b57cec5SDimitry Andric case UNW_X86_64_RDI: return _msContext.Rdi; 7070b57cec5SDimitry Andric case UNW_X86_64_R8: return _msContext.R8; 7080b57cec5SDimitry Andric case UNW_X86_64_R9: return _msContext.R9; 7090b57cec5SDimitry Andric case UNW_X86_64_R10: return _msContext.R10; 7100b57cec5SDimitry Andric case UNW_X86_64_R11: return _msContext.R11; 7110b57cec5SDimitry Andric case UNW_X86_64_R12: return _msContext.R12; 7120b57cec5SDimitry Andric case UNW_X86_64_R13: return _msContext.R13; 7130b57cec5SDimitry Andric case UNW_X86_64_R14: return _msContext.R14; 7140b57cec5SDimitry Andric case UNW_X86_64_R15: return _msContext.R15; 7150b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM) 7160b57cec5SDimitry Andric case UNW_ARM_R0: return _msContext.R0; 7170b57cec5SDimitry Andric case UNW_ARM_R1: return _msContext.R1; 7180b57cec5SDimitry Andric case UNW_ARM_R2: return _msContext.R2; 7190b57cec5SDimitry Andric case UNW_ARM_R3: return _msContext.R3; 7200b57cec5SDimitry Andric case UNW_ARM_R4: return _msContext.R4; 7210b57cec5SDimitry Andric case UNW_ARM_R5: return _msContext.R5; 7220b57cec5SDimitry Andric case UNW_ARM_R6: return _msContext.R6; 7230b57cec5SDimitry Andric case UNW_ARM_R7: return _msContext.R7; 7240b57cec5SDimitry Andric case UNW_ARM_R8: return _msContext.R8; 7250b57cec5SDimitry Andric case UNW_ARM_R9: return _msContext.R9; 7260b57cec5SDimitry Andric case UNW_ARM_R10: return _msContext.R10; 7270b57cec5SDimitry Andric case UNW_ARM_R11: return _msContext.R11; 7280b57cec5SDimitry Andric case UNW_ARM_R12: return _msContext.R12; 7290b57cec5SDimitry Andric case UNW_REG_SP: 7300b57cec5SDimitry Andric case UNW_ARM_SP: return _msContext.Sp; 7310b57cec5SDimitry Andric case UNW_ARM_LR: return _msContext.Lr; 7320b57cec5SDimitry Andric case UNW_REG_IP: 7330b57cec5SDimitry Andric case UNW_ARM_IP: return _msContext.Pc; 7340b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 7350b57cec5SDimitry Andric case UNW_REG_SP: return _msContext.Sp; 7360b57cec5SDimitry Andric case UNW_REG_IP: return _msContext.Pc; 737349cc55cSDimitry Andric default: return _msContext.X[regNum - UNW_AARCH64_X0]; 7380b57cec5SDimitry Andric #endif 7390b57cec5SDimitry Andric } 7400b57cec5SDimitry Andric _LIBUNWIND_ABORT("unsupported register"); 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric template <typename A, typename R> 7440b57cec5SDimitry Andric void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) { 7450b57cec5SDimitry Andric switch (regNum) { 7460b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 7470b57cec5SDimitry Andric case UNW_REG_IP: _msContext.Rip = value; break; 7480b57cec5SDimitry Andric case UNW_X86_64_RAX: _msContext.Rax = value; break; 7490b57cec5SDimitry Andric case UNW_X86_64_RDX: _msContext.Rdx = value; break; 7500b57cec5SDimitry Andric case UNW_X86_64_RCX: _msContext.Rcx = value; break; 7510b57cec5SDimitry Andric case UNW_X86_64_RBX: _msContext.Rbx = value; break; 7520b57cec5SDimitry Andric case UNW_REG_SP: 7530b57cec5SDimitry Andric case UNW_X86_64_RSP: _msContext.Rsp = value; break; 7540b57cec5SDimitry Andric case UNW_X86_64_RBP: _msContext.Rbp = value; break; 7550b57cec5SDimitry Andric case UNW_X86_64_RSI: _msContext.Rsi = value; break; 7560b57cec5SDimitry Andric case UNW_X86_64_RDI: _msContext.Rdi = value; break; 7570b57cec5SDimitry Andric case UNW_X86_64_R8: _msContext.R8 = value; break; 7580b57cec5SDimitry Andric case UNW_X86_64_R9: _msContext.R9 = value; break; 7590b57cec5SDimitry Andric case UNW_X86_64_R10: _msContext.R10 = value; break; 7600b57cec5SDimitry Andric case UNW_X86_64_R11: _msContext.R11 = value; break; 7610b57cec5SDimitry Andric case UNW_X86_64_R12: _msContext.R12 = value; break; 7620b57cec5SDimitry Andric case UNW_X86_64_R13: _msContext.R13 = value; break; 7630b57cec5SDimitry Andric case UNW_X86_64_R14: _msContext.R14 = value; break; 7640b57cec5SDimitry Andric case UNW_X86_64_R15: _msContext.R15 = value; break; 7650b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM) 7660b57cec5SDimitry Andric case UNW_ARM_R0: _msContext.R0 = value; break; 7670b57cec5SDimitry Andric case UNW_ARM_R1: _msContext.R1 = value; break; 7680b57cec5SDimitry Andric case UNW_ARM_R2: _msContext.R2 = value; break; 7690b57cec5SDimitry Andric case UNW_ARM_R3: _msContext.R3 = value; break; 7700b57cec5SDimitry Andric case UNW_ARM_R4: _msContext.R4 = value; break; 7710b57cec5SDimitry Andric case UNW_ARM_R5: _msContext.R5 = value; break; 7720b57cec5SDimitry Andric case UNW_ARM_R6: _msContext.R6 = value; break; 7730b57cec5SDimitry Andric case UNW_ARM_R7: _msContext.R7 = value; break; 7740b57cec5SDimitry Andric case UNW_ARM_R8: _msContext.R8 = value; break; 7750b57cec5SDimitry Andric case UNW_ARM_R9: _msContext.R9 = value; break; 7760b57cec5SDimitry Andric case UNW_ARM_R10: _msContext.R10 = value; break; 7770b57cec5SDimitry Andric case UNW_ARM_R11: _msContext.R11 = value; break; 7780b57cec5SDimitry Andric case UNW_ARM_R12: _msContext.R12 = value; break; 7790b57cec5SDimitry Andric case UNW_REG_SP: 7800b57cec5SDimitry Andric case UNW_ARM_SP: _msContext.Sp = value; break; 7810b57cec5SDimitry Andric case UNW_ARM_LR: _msContext.Lr = value; break; 7820b57cec5SDimitry Andric case UNW_REG_IP: 7830b57cec5SDimitry Andric case UNW_ARM_IP: _msContext.Pc = value; break; 7840b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 7850b57cec5SDimitry Andric case UNW_REG_SP: _msContext.Sp = value; break; 7860b57cec5SDimitry Andric case UNW_REG_IP: _msContext.Pc = value; break; 787349cc55cSDimitry Andric case UNW_AARCH64_X0: 788349cc55cSDimitry Andric case UNW_AARCH64_X1: 789349cc55cSDimitry Andric case UNW_AARCH64_X2: 790349cc55cSDimitry Andric case UNW_AARCH64_X3: 791349cc55cSDimitry Andric case UNW_AARCH64_X4: 792349cc55cSDimitry Andric case UNW_AARCH64_X5: 793349cc55cSDimitry Andric case UNW_AARCH64_X6: 794349cc55cSDimitry Andric case UNW_AARCH64_X7: 795349cc55cSDimitry Andric case UNW_AARCH64_X8: 796349cc55cSDimitry Andric case UNW_AARCH64_X9: 797349cc55cSDimitry Andric case UNW_AARCH64_X10: 798349cc55cSDimitry Andric case UNW_AARCH64_X11: 799349cc55cSDimitry Andric case UNW_AARCH64_X12: 800349cc55cSDimitry Andric case UNW_AARCH64_X13: 801349cc55cSDimitry Andric case UNW_AARCH64_X14: 802349cc55cSDimitry Andric case UNW_AARCH64_X15: 803349cc55cSDimitry Andric case UNW_AARCH64_X16: 804349cc55cSDimitry Andric case UNW_AARCH64_X17: 805349cc55cSDimitry Andric case UNW_AARCH64_X18: 806349cc55cSDimitry Andric case UNW_AARCH64_X19: 807349cc55cSDimitry Andric case UNW_AARCH64_X20: 808349cc55cSDimitry Andric case UNW_AARCH64_X21: 809349cc55cSDimitry Andric case UNW_AARCH64_X22: 810349cc55cSDimitry Andric case UNW_AARCH64_X23: 811349cc55cSDimitry Andric case UNW_AARCH64_X24: 812349cc55cSDimitry Andric case UNW_AARCH64_X25: 813349cc55cSDimitry Andric case UNW_AARCH64_X26: 814349cc55cSDimitry Andric case UNW_AARCH64_X27: 815349cc55cSDimitry Andric case UNW_AARCH64_X28: 816349cc55cSDimitry Andric case UNW_AARCH64_FP: 817349cc55cSDimitry Andric case UNW_AARCH64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break; 8180b57cec5SDimitry Andric #endif 8190b57cec5SDimitry Andric default: 8200b57cec5SDimitry Andric _LIBUNWIND_ABORT("unsupported register"); 8210b57cec5SDimitry Andric } 8220b57cec5SDimitry Andric } 8230b57cec5SDimitry Andric 8240b57cec5SDimitry Andric template <typename A, typename R> 8250b57cec5SDimitry Andric bool UnwindCursor<A, R>::validFloatReg(int regNum) { 8260b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM) 8270b57cec5SDimitry Andric if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true; 8280b57cec5SDimitry Andric if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true; 8290b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 830349cc55cSDimitry Andric if (regNum >= UNW_AARCH64_V0 && regNum <= UNW_ARM64_D31) return true; 8310b57cec5SDimitry Andric #else 8320b57cec5SDimitry Andric (void)regNum; 8330b57cec5SDimitry Andric #endif 8340b57cec5SDimitry Andric return false; 8350b57cec5SDimitry Andric } 8360b57cec5SDimitry Andric 8370b57cec5SDimitry Andric template <typename A, typename R> 8380b57cec5SDimitry Andric unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) { 8390b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM) 8400b57cec5SDimitry Andric if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) { 8410b57cec5SDimitry Andric union { 8420b57cec5SDimitry Andric uint32_t w; 8430b57cec5SDimitry Andric float f; 8440b57cec5SDimitry Andric } d; 8450b57cec5SDimitry Andric d.w = _msContext.S[regNum - UNW_ARM_S0]; 8460b57cec5SDimitry Andric return d.f; 8470b57cec5SDimitry Andric } 8480b57cec5SDimitry Andric if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) { 8490b57cec5SDimitry Andric union { 8500b57cec5SDimitry Andric uint64_t w; 8510b57cec5SDimitry Andric double d; 8520b57cec5SDimitry Andric } d; 8530b57cec5SDimitry Andric d.w = _msContext.D[regNum - UNW_ARM_D0]; 8540b57cec5SDimitry Andric return d.d; 8550b57cec5SDimitry Andric } 8560b57cec5SDimitry Andric _LIBUNWIND_ABORT("unsupported float register"); 8570b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 858349cc55cSDimitry Andric return _msContext.V[regNum - UNW_AARCH64_V0].D[0]; 8590b57cec5SDimitry Andric #else 8600b57cec5SDimitry Andric (void)regNum; 8610b57cec5SDimitry Andric _LIBUNWIND_ABORT("float registers unimplemented"); 8620b57cec5SDimitry Andric #endif 8630b57cec5SDimitry Andric } 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric template <typename A, typename R> 8660b57cec5SDimitry Andric void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) { 8670b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM) 8680b57cec5SDimitry Andric if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) { 8690b57cec5SDimitry Andric union { 8700b57cec5SDimitry Andric uint32_t w; 8710b57cec5SDimitry Andric float f; 8720b57cec5SDimitry Andric } d; 87381ad6265SDimitry Andric d.f = (float)value; 8740b57cec5SDimitry Andric _msContext.S[regNum - UNW_ARM_S0] = d.w; 8750b57cec5SDimitry Andric } 8760b57cec5SDimitry Andric if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) { 8770b57cec5SDimitry Andric union { 8780b57cec5SDimitry Andric uint64_t w; 8790b57cec5SDimitry Andric double d; 8800b57cec5SDimitry Andric } d; 8810b57cec5SDimitry Andric d.d = value; 8820b57cec5SDimitry Andric _msContext.D[regNum - UNW_ARM_D0] = d.w; 8830b57cec5SDimitry Andric } 8840b57cec5SDimitry Andric _LIBUNWIND_ABORT("unsupported float register"); 8850b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 886349cc55cSDimitry Andric _msContext.V[regNum - UNW_AARCH64_V0].D[0] = value; 8870b57cec5SDimitry Andric #else 8880b57cec5SDimitry Andric (void)regNum; 8890b57cec5SDimitry Andric (void)value; 8900b57cec5SDimitry Andric _LIBUNWIND_ABORT("float registers unimplemented"); 8910b57cec5SDimitry Andric #endif 8920b57cec5SDimitry Andric } 8930b57cec5SDimitry Andric 8940b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::jumpto() { 8950b57cec5SDimitry Andric RtlRestoreContext(&_msContext, nullptr); 8960b57cec5SDimitry Andric } 8970b57cec5SDimitry Andric 8980b57cec5SDimitry Andric #ifdef __arm__ 8990b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {} 9000b57cec5SDimitry Andric #endif 9010b57cec5SDimitry Andric 9020b57cec5SDimitry Andric template <typename A, typename R> 9030b57cec5SDimitry Andric const char *UnwindCursor<A, R>::getRegisterName(int regNum) { 9040b57cec5SDimitry Andric return R::getRegisterName(regNum); 9050b57cec5SDimitry Andric } 9060b57cec5SDimitry Andric 9070b57cec5SDimitry Andric template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() { 9080b57cec5SDimitry Andric return false; 9090b57cec5SDimitry Andric } 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric #else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32) 9120b57cec5SDimitry Andric 9130b57cec5SDimitry Andric /// UnwindCursor contains all state (including all register values) during 9140b57cec5SDimitry Andric /// an unwind. This is normally stack allocated inside a unw_cursor_t. 9150b57cec5SDimitry Andric template <typename A, typename R> 9160b57cec5SDimitry Andric class UnwindCursor : public AbstractUnwindCursor{ 9170b57cec5SDimitry Andric typedef typename A::pint_t pint_t; 9180b57cec5SDimitry Andric public: 9190b57cec5SDimitry Andric UnwindCursor(unw_context_t *context, A &as); 9200b57cec5SDimitry Andric UnwindCursor(A &as, void *threadArg); 9210b57cec5SDimitry Andric virtual ~UnwindCursor() {} 9220b57cec5SDimitry Andric virtual bool validReg(int); 9230b57cec5SDimitry Andric virtual unw_word_t getReg(int); 9240b57cec5SDimitry Andric virtual void setReg(int, unw_word_t); 9250b57cec5SDimitry Andric virtual bool validFloatReg(int); 9260b57cec5SDimitry Andric virtual unw_fpreg_t getFloatReg(int); 9270b57cec5SDimitry Andric virtual void setFloatReg(int, unw_fpreg_t); 928*bdd1243dSDimitry Andric virtual int step(bool stage2 = false); 9290b57cec5SDimitry Andric virtual void getInfo(unw_proc_info_t *); 9300b57cec5SDimitry Andric virtual void jumpto(); 9310b57cec5SDimitry Andric virtual bool isSignalFrame(); 9320b57cec5SDimitry Andric virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off); 9330b57cec5SDimitry Andric virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false); 9340b57cec5SDimitry Andric virtual const char *getRegisterName(int num); 9350b57cec5SDimitry Andric #ifdef __arm__ 9360b57cec5SDimitry Andric virtual void saveVFPAsX(); 9370b57cec5SDimitry Andric #endif 9380b57cec5SDimitry Andric 93981ad6265SDimitry Andric #ifdef _AIX 94081ad6265SDimitry Andric virtual uintptr_t getDataRelBase(); 94181ad6265SDimitry Andric #endif 94281ad6265SDimitry Andric 943349cc55cSDimitry Andric #if defined(_LIBUNWIND_USE_CET) 944349cc55cSDimitry Andric virtual void *get_registers() { return &_registers; } 945349cc55cSDimitry Andric #endif 94681ad6265SDimitry Andric 9470b57cec5SDimitry Andric // libunwind does not and should not depend on C++ library which means that we 948*bdd1243dSDimitry Andric // need our own definition of inline placement new. 9490b57cec5SDimitry Andric static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; } 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric private: 9520b57cec5SDimitry Andric 9530b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI) 9540b57cec5SDimitry Andric bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections §s); 9550b57cec5SDimitry Andric 9560b57cec5SDimitry Andric int stepWithEHABI() { 9570b57cec5SDimitry Andric size_t len = 0; 9580b57cec5SDimitry Andric size_t off = 0; 9590b57cec5SDimitry Andric // FIXME: Calling decode_eht_entry() here is violating the libunwind 9600b57cec5SDimitry Andric // abstraction layer. 9610b57cec5SDimitry Andric const uint32_t *ehtp = 9620b57cec5SDimitry Andric decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info), 9630b57cec5SDimitry Andric &off, &len); 9640b57cec5SDimitry Andric if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) != 9650b57cec5SDimitry Andric _URC_CONTINUE_UNWIND) 9660b57cec5SDimitry Andric return UNW_STEP_END; 9670b57cec5SDimitry Andric return UNW_STEP_SUCCESS; 9680b57cec5SDimitry Andric } 9690b57cec5SDimitry Andric #endif 9700b57cec5SDimitry Andric 97181ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) 972e8d8bef9SDimitry Andric bool setInfoForSigReturn() { 973e8d8bef9SDimitry Andric R dummy; 974e8d8bef9SDimitry Andric return setInfoForSigReturn(dummy); 975e8d8bef9SDimitry Andric } 976e8d8bef9SDimitry Andric int stepThroughSigReturn() { 977e8d8bef9SDimitry Andric R dummy; 978e8d8bef9SDimitry Andric return stepThroughSigReturn(dummy); 979e8d8bef9SDimitry Andric } 98081ad6265SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64) 981e8d8bef9SDimitry Andric bool setInfoForSigReturn(Registers_arm64 &); 982e8d8bef9SDimitry Andric int stepThroughSigReturn(Registers_arm64 &); 98381ad6265SDimitry Andric #endif 98481ad6265SDimitry Andric #if defined(_LIBUNWIND_TARGET_S390X) 98581ad6265SDimitry Andric bool setInfoForSigReturn(Registers_s390x &); 98681ad6265SDimitry Andric int stepThroughSigReturn(Registers_s390x &); 98781ad6265SDimitry Andric #endif 988e8d8bef9SDimitry Andric template <typename Registers> bool setInfoForSigReturn(Registers &) { 989e8d8bef9SDimitry Andric return false; 990e8d8bef9SDimitry Andric } 991e8d8bef9SDimitry Andric template <typename Registers> int stepThroughSigReturn(Registers &) { 992e8d8bef9SDimitry Andric return UNW_STEP_END; 993e8d8bef9SDimitry Andric } 994e8d8bef9SDimitry Andric #endif 995e8d8bef9SDimitry Andric 9960b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 997e8d8bef9SDimitry Andric bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo, 998e8d8bef9SDimitry Andric const typename CFI_Parser<A>::CIE_Info &cieInfo, 999e8d8bef9SDimitry Andric pint_t pc, uintptr_t dso_base); 10000b57cec5SDimitry Andric bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections §s, 10010b57cec5SDimitry Andric uint32_t fdeSectionOffsetHint=0); 1002*bdd1243dSDimitry Andric int stepWithDwarfFDE(bool stage2) { 1003*bdd1243dSDimitry Andric return DwarfInstructions<A, R>::stepWithDwarf( 1004*bdd1243dSDimitry Andric _addressSpace, (pint_t)this->getReg(UNW_REG_IP), 1005*bdd1243dSDimitry Andric (pint_t)_info.unwind_info, _registers, _isSignalFrame, stage2); 10060b57cec5SDimitry Andric } 10070b57cec5SDimitry Andric #endif 10080b57cec5SDimitry Andric 10090b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 10100b57cec5SDimitry Andric bool getInfoFromCompactEncodingSection(pint_t pc, 10110b57cec5SDimitry Andric const UnwindInfoSections §s); 1012*bdd1243dSDimitry Andric int stepWithCompactEncoding(bool stage2 = false) { 10130b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 10140b57cec5SDimitry Andric if ( compactSaysUseDwarf() ) 1015*bdd1243dSDimitry Andric return stepWithDwarfFDE(stage2); 10160b57cec5SDimitry Andric #endif 10170b57cec5SDimitry Andric R dummy; 10180b57cec5SDimitry Andric return stepWithCompactEncoding(dummy); 10190b57cec5SDimitry Andric } 10200b57cec5SDimitry Andric 10210b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 10220b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_x86_64 &) { 10230b57cec5SDimitry Andric return CompactUnwinder_x86_64<A>::stepWithCompactEncoding( 10240b57cec5SDimitry Andric _info.format, _info.start_ip, _addressSpace, _registers); 10250b57cec5SDimitry Andric } 10260b57cec5SDimitry Andric #endif 10270b57cec5SDimitry Andric 10280b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386) 10290b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_x86 &) { 10300b57cec5SDimitry Andric return CompactUnwinder_x86<A>::stepWithCompactEncoding( 10310b57cec5SDimitry Andric _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers); 10320b57cec5SDimitry Andric } 10330b57cec5SDimitry Andric #endif 10340b57cec5SDimitry Andric 10350b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC) 10360b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_ppc &) { 10370b57cec5SDimitry Andric return UNW_EINVAL; 10380b57cec5SDimitry Andric } 10390b57cec5SDimitry Andric #endif 10400b57cec5SDimitry Andric 10410b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64) 10420b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_ppc64 &) { 10430b57cec5SDimitry Andric return UNW_EINVAL; 10440b57cec5SDimitry Andric } 10450b57cec5SDimitry Andric #endif 10460b57cec5SDimitry Andric 10470b57cec5SDimitry Andric 10480b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64) 10490b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_arm64 &) { 10500b57cec5SDimitry Andric return CompactUnwinder_arm64<A>::stepWithCompactEncoding( 10510b57cec5SDimitry Andric _info.format, _info.start_ip, _addressSpace, _registers); 10520b57cec5SDimitry Andric } 10530b57cec5SDimitry Andric #endif 10540b57cec5SDimitry Andric 10550b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_O32) 10560b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_mips_o32 &) { 10570b57cec5SDimitry Andric return UNW_EINVAL; 10580b57cec5SDimitry Andric } 10590b57cec5SDimitry Andric #endif 10600b57cec5SDimitry Andric 10610b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI) 10620b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_mips_newabi &) { 10630b57cec5SDimitry Andric return UNW_EINVAL; 10640b57cec5SDimitry Andric } 10650b57cec5SDimitry Andric #endif 10660b57cec5SDimitry Andric 1067*bdd1243dSDimitry Andric #if defined(_LIBUNWIND_TARGET_LOONGARCH) 1068*bdd1243dSDimitry Andric int stepWithCompactEncoding(Registers_loongarch &) { return UNW_EINVAL; } 1069*bdd1243dSDimitry Andric #endif 1070*bdd1243dSDimitry Andric 10710b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC) 10720b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; } 10730b57cec5SDimitry Andric #endif 10740b57cec5SDimitry Andric 1075d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64) 1076d56accc7SDimitry Andric int stepWithCompactEncoding(Registers_sparc64 &) { return UNW_EINVAL; } 1077d56accc7SDimitry Andric #endif 1078d56accc7SDimitry Andric 1079480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV) 1080480093f4SDimitry Andric int stepWithCompactEncoding(Registers_riscv &) { 1081480093f4SDimitry Andric return UNW_EINVAL; 1082480093f4SDimitry Andric } 1083480093f4SDimitry Andric #endif 1084480093f4SDimitry Andric 10850b57cec5SDimitry Andric bool compactSaysUseDwarf(uint32_t *offset=NULL) const { 10860b57cec5SDimitry Andric R dummy; 10870b57cec5SDimitry Andric return compactSaysUseDwarf(dummy, offset); 10880b57cec5SDimitry Andric } 10890b57cec5SDimitry Andric 10900b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 10910b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const { 10920b57cec5SDimitry Andric if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) { 10930b57cec5SDimitry Andric if (offset) 10940b57cec5SDimitry Andric *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET); 10950b57cec5SDimitry Andric return true; 10960b57cec5SDimitry Andric } 10970b57cec5SDimitry Andric return false; 10980b57cec5SDimitry Andric } 10990b57cec5SDimitry Andric #endif 11000b57cec5SDimitry Andric 11010b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386) 11020b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const { 11030b57cec5SDimitry Andric if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) { 11040b57cec5SDimitry Andric if (offset) 11050b57cec5SDimitry Andric *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET); 11060b57cec5SDimitry Andric return true; 11070b57cec5SDimitry Andric } 11080b57cec5SDimitry Andric return false; 11090b57cec5SDimitry Andric } 11100b57cec5SDimitry Andric #endif 11110b57cec5SDimitry Andric 11120b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC) 11130b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const { 11140b57cec5SDimitry Andric return true; 11150b57cec5SDimitry Andric } 11160b57cec5SDimitry Andric #endif 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64) 11190b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const { 11200b57cec5SDimitry Andric return true; 11210b57cec5SDimitry Andric } 11220b57cec5SDimitry Andric #endif 11230b57cec5SDimitry Andric 11240b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64) 11250b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const { 11260b57cec5SDimitry Andric if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) { 11270b57cec5SDimitry Andric if (offset) 11280b57cec5SDimitry Andric *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET); 11290b57cec5SDimitry Andric return true; 11300b57cec5SDimitry Andric } 11310b57cec5SDimitry Andric return false; 11320b57cec5SDimitry Andric } 11330b57cec5SDimitry Andric #endif 11340b57cec5SDimitry Andric 11350b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_O32) 11360b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const { 11370b57cec5SDimitry Andric return true; 11380b57cec5SDimitry Andric } 11390b57cec5SDimitry Andric #endif 11400b57cec5SDimitry Andric 11410b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI) 11420b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const { 11430b57cec5SDimitry Andric return true; 11440b57cec5SDimitry Andric } 11450b57cec5SDimitry Andric #endif 11460b57cec5SDimitry Andric 1147*bdd1243dSDimitry Andric #if defined(_LIBUNWIND_TARGET_LOONGARCH) 1148*bdd1243dSDimitry Andric bool compactSaysUseDwarf(Registers_loongarch &, uint32_t *) const { 1149*bdd1243dSDimitry Andric return true; 1150*bdd1243dSDimitry Andric } 1151*bdd1243dSDimitry Andric #endif 1152*bdd1243dSDimitry Andric 11530b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC) 11540b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; } 11550b57cec5SDimitry Andric #endif 11560b57cec5SDimitry Andric 1157d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64) 1158d56accc7SDimitry Andric bool compactSaysUseDwarf(Registers_sparc64 &, uint32_t *) const { 1159d56accc7SDimitry Andric return true; 1160d56accc7SDimitry Andric } 1161d56accc7SDimitry Andric #endif 1162d56accc7SDimitry Andric 1163480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV) 1164480093f4SDimitry Andric bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const { 1165480093f4SDimitry Andric return true; 1166480093f4SDimitry Andric } 1167480093f4SDimitry Andric #endif 1168480093f4SDimitry Andric 11690b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 11700b57cec5SDimitry Andric 11710b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 11720b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding() const { 11730b57cec5SDimitry Andric R dummy; 11740b57cec5SDimitry Andric return dwarfEncoding(dummy); 11750b57cec5SDimitry Andric } 11760b57cec5SDimitry Andric 11770b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 11780b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const { 11790b57cec5SDimitry Andric return UNWIND_X86_64_MODE_DWARF; 11800b57cec5SDimitry Andric } 11810b57cec5SDimitry Andric #endif 11820b57cec5SDimitry Andric 11830b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386) 11840b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const { 11850b57cec5SDimitry Andric return UNWIND_X86_MODE_DWARF; 11860b57cec5SDimitry Andric } 11870b57cec5SDimitry Andric #endif 11880b57cec5SDimitry Andric 11890b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC) 11900b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const { 11910b57cec5SDimitry Andric return 0; 11920b57cec5SDimitry Andric } 11930b57cec5SDimitry Andric #endif 11940b57cec5SDimitry Andric 11950b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64) 11960b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const { 11970b57cec5SDimitry Andric return 0; 11980b57cec5SDimitry Andric } 11990b57cec5SDimitry Andric #endif 12000b57cec5SDimitry Andric 12010b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64) 12020b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const { 12030b57cec5SDimitry Andric return UNWIND_ARM64_MODE_DWARF; 12040b57cec5SDimitry Andric } 12050b57cec5SDimitry Andric #endif 12060b57cec5SDimitry Andric 12070b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM) 12080b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const { 12090b57cec5SDimitry Andric return 0; 12100b57cec5SDimitry Andric } 12110b57cec5SDimitry Andric #endif 12120b57cec5SDimitry Andric 12130b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_OR1K) 12140b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const { 12150b57cec5SDimitry Andric return 0; 12160b57cec5SDimitry Andric } 12170b57cec5SDimitry Andric #endif 12180b57cec5SDimitry Andric 12195ffd83dbSDimitry Andric #if defined (_LIBUNWIND_TARGET_HEXAGON) 12205ffd83dbSDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const { 12215ffd83dbSDimitry Andric return 0; 12225ffd83dbSDimitry Andric } 12235ffd83dbSDimitry Andric #endif 12245ffd83dbSDimitry Andric 12250b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_MIPS_O32) 12260b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const { 12270b57cec5SDimitry Andric return 0; 12280b57cec5SDimitry Andric } 12290b57cec5SDimitry Andric #endif 12300b57cec5SDimitry Andric 12310b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_MIPS_NEWABI) 12320b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const { 12330b57cec5SDimitry Andric return 0; 12340b57cec5SDimitry Andric } 12350b57cec5SDimitry Andric #endif 12360b57cec5SDimitry Andric 1237*bdd1243dSDimitry Andric #if defined(_LIBUNWIND_TARGET_LOONGARCH) 1238*bdd1243dSDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_loongarch &) const { 1239*bdd1243dSDimitry Andric return 0; 1240*bdd1243dSDimitry Andric } 1241*bdd1243dSDimitry Andric #endif 1242*bdd1243dSDimitry Andric 12430b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC) 12440b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; } 12450b57cec5SDimitry Andric #endif 12460b57cec5SDimitry Andric 1247d56accc7SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC64) 1248d56accc7SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_sparc64 &) const { 1249d56accc7SDimitry Andric return 0; 1250d56accc7SDimitry Andric } 1251d56accc7SDimitry Andric #endif 1252d56accc7SDimitry Andric 1253480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV) 1254480093f4SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const { 1255480093f4SDimitry Andric return 0; 1256480093f4SDimitry Andric } 1257480093f4SDimitry Andric #endif 1258480093f4SDimitry Andric 125981ad6265SDimitry Andric #if defined (_LIBUNWIND_TARGET_S390X) 126081ad6265SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_s390x &) const { 126181ad6265SDimitry Andric return 0; 126281ad6265SDimitry Andric } 126381ad6265SDimitry Andric #endif 126481ad6265SDimitry Andric 12650b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 12660b57cec5SDimitry Andric 12670b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 12680b57cec5SDimitry Andric // For runtime environments using SEH unwind data without Windows runtime 12690b57cec5SDimitry Andric // support. 12700b57cec5SDimitry Andric pint_t getLastPC() const { /* FIXME: Implement */ return 0; } 12710b57cec5SDimitry Andric void setLastPC(pint_t pc) { /* FIXME: Implement */ } 12720b57cec5SDimitry Andric RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) { 12730b57cec5SDimitry Andric /* FIXME: Implement */ 12740b57cec5SDimitry Andric *base = 0; 12750b57cec5SDimitry Andric return nullptr; 12760b57cec5SDimitry Andric } 12770b57cec5SDimitry Andric bool getInfoFromSEH(pint_t pc); 12780b57cec5SDimitry Andric int stepWithSEHData() { /* FIXME: Implement */ return 0; } 12790b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 12800b57cec5SDimitry Andric 128181ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) 128281ad6265SDimitry Andric bool getInfoFromTBTable(pint_t pc, R ®isters); 128381ad6265SDimitry Andric int stepWithTBTable(pint_t pc, tbtable *TBTable, R ®isters, 128481ad6265SDimitry Andric bool &isSignalFrame); 128581ad6265SDimitry Andric int stepWithTBTableData() { 128681ad6265SDimitry Andric return stepWithTBTable(reinterpret_cast<pint_t>(this->getReg(UNW_REG_IP)), 128781ad6265SDimitry Andric reinterpret_cast<tbtable *>(_info.unwind_info), 128881ad6265SDimitry Andric _registers, _isSignalFrame); 128981ad6265SDimitry Andric } 129081ad6265SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) 12910b57cec5SDimitry Andric 12920b57cec5SDimitry Andric A &_addressSpace; 12930b57cec5SDimitry Andric R _registers; 12940b57cec5SDimitry Andric unw_proc_info_t _info; 12950b57cec5SDimitry Andric bool _unwindInfoMissing; 12960b57cec5SDimitry Andric bool _isSignalFrame; 129781ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) 1298e8d8bef9SDimitry Andric bool _isSigReturn = false; 1299e8d8bef9SDimitry Andric #endif 13000b57cec5SDimitry Andric }; 13010b57cec5SDimitry Andric 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andric template <typename A, typename R> 13040b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as) 13050b57cec5SDimitry Andric : _addressSpace(as), _registers(context), _unwindInfoMissing(false), 13060b57cec5SDimitry Andric _isSignalFrame(false) { 13070b57cec5SDimitry Andric static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), 13080b57cec5SDimitry Andric "UnwindCursor<> does not fit in unw_cursor_t"); 1309e8d8bef9SDimitry Andric static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)), 1310e8d8bef9SDimitry Andric "UnwindCursor<> requires more alignment than unw_cursor_t"); 13110b57cec5SDimitry Andric memset(&_info, 0, sizeof(_info)); 13120b57cec5SDimitry Andric } 13130b57cec5SDimitry Andric 13140b57cec5SDimitry Andric template <typename A, typename R> 13150b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(A &as, void *) 13160b57cec5SDimitry Andric : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) { 13170b57cec5SDimitry Andric memset(&_info, 0, sizeof(_info)); 13180b57cec5SDimitry Andric // FIXME 13190b57cec5SDimitry Andric // fill in _registers from thread arg 13200b57cec5SDimitry Andric } 13210b57cec5SDimitry Andric 13220b57cec5SDimitry Andric 13230b57cec5SDimitry Andric template <typename A, typename R> 13240b57cec5SDimitry Andric bool UnwindCursor<A, R>::validReg(int regNum) { 13250b57cec5SDimitry Andric return _registers.validRegister(regNum); 13260b57cec5SDimitry Andric } 13270b57cec5SDimitry Andric 13280b57cec5SDimitry Andric template <typename A, typename R> 13290b57cec5SDimitry Andric unw_word_t UnwindCursor<A, R>::getReg(int regNum) { 13300b57cec5SDimitry Andric return _registers.getRegister(regNum); 13310b57cec5SDimitry Andric } 13320b57cec5SDimitry Andric 13330b57cec5SDimitry Andric template <typename A, typename R> 13340b57cec5SDimitry Andric void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) { 13350b57cec5SDimitry Andric _registers.setRegister(regNum, (typename A::pint_t)value); 13360b57cec5SDimitry Andric } 13370b57cec5SDimitry Andric 13380b57cec5SDimitry Andric template <typename A, typename R> 13390b57cec5SDimitry Andric bool UnwindCursor<A, R>::validFloatReg(int regNum) { 13400b57cec5SDimitry Andric return _registers.validFloatRegister(regNum); 13410b57cec5SDimitry Andric } 13420b57cec5SDimitry Andric 13430b57cec5SDimitry Andric template <typename A, typename R> 13440b57cec5SDimitry Andric unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) { 13450b57cec5SDimitry Andric return _registers.getFloatRegister(regNum); 13460b57cec5SDimitry Andric } 13470b57cec5SDimitry Andric 13480b57cec5SDimitry Andric template <typename A, typename R> 13490b57cec5SDimitry Andric void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) { 13500b57cec5SDimitry Andric _registers.setFloatRegister(regNum, value); 13510b57cec5SDimitry Andric } 13520b57cec5SDimitry Andric 13530b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::jumpto() { 13540b57cec5SDimitry Andric _registers.jumpto(); 13550b57cec5SDimitry Andric } 13560b57cec5SDimitry Andric 13570b57cec5SDimitry Andric #ifdef __arm__ 13580b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() { 13590b57cec5SDimitry Andric _registers.saveVFPAsX(); 13600b57cec5SDimitry Andric } 13610b57cec5SDimitry Andric #endif 13620b57cec5SDimitry Andric 136381ad6265SDimitry Andric #ifdef _AIX 136481ad6265SDimitry Andric template <typename A, typename R> 136581ad6265SDimitry Andric uintptr_t UnwindCursor<A, R>::getDataRelBase() { 136681ad6265SDimitry Andric return reinterpret_cast<uintptr_t>(_info.extra); 136781ad6265SDimitry Andric } 136881ad6265SDimitry Andric #endif 136981ad6265SDimitry Andric 13700b57cec5SDimitry Andric template <typename A, typename R> 13710b57cec5SDimitry Andric const char *UnwindCursor<A, R>::getRegisterName(int regNum) { 13720b57cec5SDimitry Andric return _registers.getRegisterName(regNum); 13730b57cec5SDimitry Andric } 13740b57cec5SDimitry Andric 13750b57cec5SDimitry Andric template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() { 13760b57cec5SDimitry Andric return _isSignalFrame; 13770b57cec5SDimitry Andric } 13780b57cec5SDimitry Andric 13790b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 13800b57cec5SDimitry Andric 13810b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI) 13820b57cec5SDimitry Andric template<typename A> 13830b57cec5SDimitry Andric struct EHABISectionIterator { 13840b57cec5SDimitry Andric typedef EHABISectionIterator _Self; 13850b57cec5SDimitry Andric 13860b57cec5SDimitry Andric typedef typename A::pint_t value_type; 13870b57cec5SDimitry Andric typedef typename A::pint_t* pointer; 13880b57cec5SDimitry Andric typedef typename A::pint_t& reference; 13890b57cec5SDimitry Andric typedef size_t size_type; 13900b57cec5SDimitry Andric typedef size_t difference_type; 13910b57cec5SDimitry Andric 13920b57cec5SDimitry Andric static _Self begin(A& addressSpace, const UnwindInfoSections& sects) { 13930b57cec5SDimitry Andric return _Self(addressSpace, sects, 0); 13940b57cec5SDimitry Andric } 13950b57cec5SDimitry Andric static _Self end(A& addressSpace, const UnwindInfoSections& sects) { 13960b57cec5SDimitry Andric return _Self(addressSpace, sects, 13970b57cec5SDimitry Andric sects.arm_section_length / sizeof(EHABIIndexEntry)); 13980b57cec5SDimitry Andric } 13990b57cec5SDimitry Andric 14000b57cec5SDimitry Andric EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i) 14010b57cec5SDimitry Andric : _i(i), _addressSpace(&addressSpace), _sects(§s) {} 14020b57cec5SDimitry Andric 14030b57cec5SDimitry Andric _Self& operator++() { ++_i; return *this; } 14040b57cec5SDimitry Andric _Self& operator+=(size_t a) { _i += a; return *this; } 14050b57cec5SDimitry Andric _Self& operator--() { assert(_i > 0); --_i; return *this; } 14060b57cec5SDimitry Andric _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; } 14070b57cec5SDimitry Andric 14080b57cec5SDimitry Andric _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; } 14090b57cec5SDimitry Andric _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; } 14100b57cec5SDimitry Andric 14115ffd83dbSDimitry Andric size_t operator-(const _Self& other) const { return _i - other._i; } 14120b57cec5SDimitry Andric 14130b57cec5SDimitry Andric bool operator==(const _Self& other) const { 14140b57cec5SDimitry Andric assert(_addressSpace == other._addressSpace); 14150b57cec5SDimitry Andric assert(_sects == other._sects); 14160b57cec5SDimitry Andric return _i == other._i; 14170b57cec5SDimitry Andric } 14180b57cec5SDimitry Andric 14195ffd83dbSDimitry Andric bool operator!=(const _Self& other) const { 14205ffd83dbSDimitry Andric assert(_addressSpace == other._addressSpace); 14215ffd83dbSDimitry Andric assert(_sects == other._sects); 14225ffd83dbSDimitry Andric return _i != other._i; 14235ffd83dbSDimitry Andric } 14245ffd83dbSDimitry Andric 14250b57cec5SDimitry Andric typename A::pint_t operator*() const { return functionAddress(); } 14260b57cec5SDimitry Andric 14270b57cec5SDimitry Andric typename A::pint_t functionAddress() const { 14280b57cec5SDimitry Andric typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( 14290b57cec5SDimitry Andric EHABIIndexEntry, _i, functionOffset); 14300b57cec5SDimitry Andric return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr)); 14310b57cec5SDimitry Andric } 14320b57cec5SDimitry Andric 14330b57cec5SDimitry Andric typename A::pint_t dataAddress() { 14340b57cec5SDimitry Andric typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( 14350b57cec5SDimitry Andric EHABIIndexEntry, _i, data); 14360b57cec5SDimitry Andric return indexAddr; 14370b57cec5SDimitry Andric } 14380b57cec5SDimitry Andric 14390b57cec5SDimitry Andric private: 14400b57cec5SDimitry Andric size_t _i; 14410b57cec5SDimitry Andric A* _addressSpace; 14420b57cec5SDimitry Andric const UnwindInfoSections* _sects; 14430b57cec5SDimitry Andric }; 14440b57cec5SDimitry Andric 14450b57cec5SDimitry Andric namespace { 14460b57cec5SDimitry Andric 14470b57cec5SDimitry Andric template <typename A> 14480b57cec5SDimitry Andric EHABISectionIterator<A> EHABISectionUpperBound( 14490b57cec5SDimitry Andric EHABISectionIterator<A> first, 14500b57cec5SDimitry Andric EHABISectionIterator<A> last, 14510b57cec5SDimitry Andric typename A::pint_t value) { 14520b57cec5SDimitry Andric size_t len = last - first; 14530b57cec5SDimitry Andric while (len > 0) { 14540b57cec5SDimitry Andric size_t l2 = len / 2; 14550b57cec5SDimitry Andric EHABISectionIterator<A> m = first + l2; 14560b57cec5SDimitry Andric if (value < *m) { 14570b57cec5SDimitry Andric len = l2; 14580b57cec5SDimitry Andric } else { 14590b57cec5SDimitry Andric first = ++m; 14600b57cec5SDimitry Andric len -= l2 + 1; 14610b57cec5SDimitry Andric } 14620b57cec5SDimitry Andric } 14630b57cec5SDimitry Andric return first; 14640b57cec5SDimitry Andric } 14650b57cec5SDimitry Andric 14660b57cec5SDimitry Andric } 14670b57cec5SDimitry Andric 14680b57cec5SDimitry Andric template <typename A, typename R> 14690b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromEHABISection( 14700b57cec5SDimitry Andric pint_t pc, 14710b57cec5SDimitry Andric const UnwindInfoSections §s) { 14720b57cec5SDimitry Andric EHABISectionIterator<A> begin = 14730b57cec5SDimitry Andric EHABISectionIterator<A>::begin(_addressSpace, sects); 14740b57cec5SDimitry Andric EHABISectionIterator<A> end = 14750b57cec5SDimitry Andric EHABISectionIterator<A>::end(_addressSpace, sects); 14760b57cec5SDimitry Andric if (begin == end) 14770b57cec5SDimitry Andric return false; 14780b57cec5SDimitry Andric 14790b57cec5SDimitry Andric EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc); 14800b57cec5SDimitry Andric if (itNextPC == begin) 14810b57cec5SDimitry Andric return false; 14820b57cec5SDimitry Andric EHABISectionIterator<A> itThisPC = itNextPC - 1; 14830b57cec5SDimitry Andric 14840b57cec5SDimitry Andric pint_t thisPC = itThisPC.functionAddress(); 14850b57cec5SDimitry Andric // If an exception is thrown from a function, corresponding to the last entry 14860b57cec5SDimitry Andric // in the table, we don't really know the function extent and have to choose a 14870b57cec5SDimitry Andric // value for nextPC. Choosing max() will allow the range check during trace to 14880b57cec5SDimitry Andric // succeed. 14890b57cec5SDimitry Andric pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress(); 14900b57cec5SDimitry Andric pint_t indexDataAddr = itThisPC.dataAddress(); 14910b57cec5SDimitry Andric 14920b57cec5SDimitry Andric if (indexDataAddr == 0) 14930b57cec5SDimitry Andric return false; 14940b57cec5SDimitry Andric 14950b57cec5SDimitry Andric uint32_t indexData = _addressSpace.get32(indexDataAddr); 14960b57cec5SDimitry Andric if (indexData == UNW_EXIDX_CANTUNWIND) 14970b57cec5SDimitry Andric return false; 14980b57cec5SDimitry Andric 14990b57cec5SDimitry Andric // If the high bit is set, the exception handling table entry is inline inside 15000b57cec5SDimitry Andric // the index table entry on the second word (aka |indexDataAddr|). Otherwise, 1501473b61d3SDimitry Andric // the table points at an offset in the exception handling table (section 5 1502473b61d3SDimitry Andric // EHABI). 15030b57cec5SDimitry Andric pint_t exceptionTableAddr; 15040b57cec5SDimitry Andric uint32_t exceptionTableData; 15050b57cec5SDimitry Andric bool isSingleWordEHT; 15060b57cec5SDimitry Andric if (indexData & 0x80000000) { 15070b57cec5SDimitry Andric exceptionTableAddr = indexDataAddr; 15080b57cec5SDimitry Andric // TODO(ajwong): Should this data be 0? 15090b57cec5SDimitry Andric exceptionTableData = indexData; 15100b57cec5SDimitry Andric isSingleWordEHT = true; 15110b57cec5SDimitry Andric } else { 15120b57cec5SDimitry Andric exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData); 15130b57cec5SDimitry Andric exceptionTableData = _addressSpace.get32(exceptionTableAddr); 15140b57cec5SDimitry Andric isSingleWordEHT = false; 15150b57cec5SDimitry Andric } 15160b57cec5SDimitry Andric 15170b57cec5SDimitry Andric // Now we know the 3 things: 15180b57cec5SDimitry Andric // exceptionTableAddr -- exception handler table entry. 15190b57cec5SDimitry Andric // exceptionTableData -- the data inside the first word of the eht entry. 15200b57cec5SDimitry Andric // isSingleWordEHT -- whether the entry is in the index. 15210b57cec5SDimitry Andric unw_word_t personalityRoutine = 0xbadf00d; 15220b57cec5SDimitry Andric bool scope32 = false; 15230b57cec5SDimitry Andric uintptr_t lsda; 15240b57cec5SDimitry Andric 15250b57cec5SDimitry Andric // If the high bit in the exception handling table entry is set, the entry is 15260b57cec5SDimitry Andric // in compact form (section 6.3 EHABI). 15270b57cec5SDimitry Andric if (exceptionTableData & 0x80000000) { 15280b57cec5SDimitry Andric // Grab the index of the personality routine from the compact form. 15290b57cec5SDimitry Andric uint32_t choice = (exceptionTableData & 0x0f000000) >> 24; 15300b57cec5SDimitry Andric uint32_t extraWords = 0; 15310b57cec5SDimitry Andric switch (choice) { 15320b57cec5SDimitry Andric case 0: 15330b57cec5SDimitry Andric personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0; 15340b57cec5SDimitry Andric extraWords = 0; 15350b57cec5SDimitry Andric scope32 = false; 15360b57cec5SDimitry Andric lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4); 15370b57cec5SDimitry Andric break; 15380b57cec5SDimitry Andric case 1: 15390b57cec5SDimitry Andric personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1; 15400b57cec5SDimitry Andric extraWords = (exceptionTableData & 0x00ff0000) >> 16; 15410b57cec5SDimitry Andric scope32 = false; 15420b57cec5SDimitry Andric lsda = exceptionTableAddr + (extraWords + 1) * 4; 15430b57cec5SDimitry Andric break; 15440b57cec5SDimitry Andric case 2: 15450b57cec5SDimitry Andric personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2; 15460b57cec5SDimitry Andric extraWords = (exceptionTableData & 0x00ff0000) >> 16; 15470b57cec5SDimitry Andric scope32 = true; 15480b57cec5SDimitry Andric lsda = exceptionTableAddr + (extraWords + 1) * 4; 15490b57cec5SDimitry Andric break; 15500b57cec5SDimitry Andric default: 15510b57cec5SDimitry Andric _LIBUNWIND_ABORT("unknown personality routine"); 15520b57cec5SDimitry Andric return false; 15530b57cec5SDimitry Andric } 15540b57cec5SDimitry Andric 15550b57cec5SDimitry Andric if (isSingleWordEHT) { 15560b57cec5SDimitry Andric if (extraWords != 0) { 15570b57cec5SDimitry Andric _LIBUNWIND_ABORT("index inlined table detected but pr function " 15580b57cec5SDimitry Andric "requires extra words"); 15590b57cec5SDimitry Andric return false; 15600b57cec5SDimitry Andric } 15610b57cec5SDimitry Andric } 15620b57cec5SDimitry Andric } else { 15630b57cec5SDimitry Andric pint_t personalityAddr = 15640b57cec5SDimitry Andric exceptionTableAddr + signExtendPrel31(exceptionTableData); 15650b57cec5SDimitry Andric personalityRoutine = personalityAddr; 15660b57cec5SDimitry Andric 15670b57cec5SDimitry Andric // ARM EHABI # 6.2, # 9.2 15680b57cec5SDimitry Andric // 15690b57cec5SDimitry Andric // +---- ehtp 15700b57cec5SDimitry Andric // v 15710b57cec5SDimitry Andric // +--------------------------------------+ 15720b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 15730b57cec5SDimitry Andric // | |0| prel31 to personalityRoutine | | 15740b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 15750b57cec5SDimitry Andric // | | N | unwind opcodes | | <-- UnwindData 15760b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 15770b57cec5SDimitry Andric // | | Word 2 unwind opcodes | | 15780b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 15790b57cec5SDimitry Andric // | ... | 15800b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 15810b57cec5SDimitry Andric // | | Word N unwind opcodes | | 15820b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 15830b57cec5SDimitry Andric // | | LSDA | | <-- lsda 15840b57cec5SDimitry Andric // | | ... | | 15850b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 15860b57cec5SDimitry Andric // +--------------------------------------+ 15870b57cec5SDimitry Andric 15880b57cec5SDimitry Andric uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1; 15890b57cec5SDimitry Andric uint32_t FirstDataWord = *UnwindData; 15900b57cec5SDimitry Andric size_t N = ((FirstDataWord >> 24) & 0xff); 15910b57cec5SDimitry Andric size_t NDataWords = N + 1; 15920b57cec5SDimitry Andric lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords); 15930b57cec5SDimitry Andric } 15940b57cec5SDimitry Andric 15950b57cec5SDimitry Andric _info.start_ip = thisPC; 15960b57cec5SDimitry Andric _info.end_ip = nextPC; 15970b57cec5SDimitry Andric _info.handler = personalityRoutine; 15980b57cec5SDimitry Andric _info.unwind_info = exceptionTableAddr; 15990b57cec5SDimitry Andric _info.lsda = lsda; 16000b57cec5SDimitry Andric // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0. 1601473b61d3SDimitry Andric _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0); // Use enum? 16020b57cec5SDimitry Andric 16030b57cec5SDimitry Andric return true; 16040b57cec5SDimitry Andric } 16050b57cec5SDimitry Andric #endif 16060b57cec5SDimitry Andric 16070b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 16080b57cec5SDimitry Andric template <typename A, typename R> 1609e8d8bef9SDimitry Andric bool UnwindCursor<A, R>::getInfoFromFdeCie( 1610e8d8bef9SDimitry Andric const typename CFI_Parser<A>::FDE_Info &fdeInfo, 1611e8d8bef9SDimitry Andric const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc, 1612e8d8bef9SDimitry Andric uintptr_t dso_base) { 1613e8d8bef9SDimitry Andric typename CFI_Parser<A>::PrologInfo prolog; 1614e8d8bef9SDimitry Andric if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc, 1615e8d8bef9SDimitry Andric R::getArch(), &prolog)) { 1616e8d8bef9SDimitry Andric // Save off parsed FDE info 1617e8d8bef9SDimitry Andric _info.start_ip = fdeInfo.pcStart; 1618e8d8bef9SDimitry Andric _info.end_ip = fdeInfo.pcEnd; 1619e8d8bef9SDimitry Andric _info.lsda = fdeInfo.lsda; 1620e8d8bef9SDimitry Andric _info.handler = cieInfo.personality; 1621e8d8bef9SDimitry Andric // Some frameless functions need SP altered when resuming in function, so 1622e8d8bef9SDimitry Andric // propagate spExtraArgSize. 1623e8d8bef9SDimitry Andric _info.gp = prolog.spExtraArgSize; 1624e8d8bef9SDimitry Andric _info.flags = 0; 1625e8d8bef9SDimitry Andric _info.format = dwarfEncoding(); 1626e8d8bef9SDimitry Andric _info.unwind_info = fdeInfo.fdeStart; 1627e8d8bef9SDimitry Andric _info.unwind_info_size = static_cast<uint32_t>(fdeInfo.fdeLength); 1628e8d8bef9SDimitry Andric _info.extra = static_cast<unw_word_t>(dso_base); 1629e8d8bef9SDimitry Andric return true; 1630e8d8bef9SDimitry Andric } 1631e8d8bef9SDimitry Andric return false; 1632e8d8bef9SDimitry Andric } 1633e8d8bef9SDimitry Andric 1634e8d8bef9SDimitry Andric template <typename A, typename R> 16350b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc, 16360b57cec5SDimitry Andric const UnwindInfoSections §s, 16370b57cec5SDimitry Andric uint32_t fdeSectionOffsetHint) { 16380b57cec5SDimitry Andric typename CFI_Parser<A>::FDE_Info fdeInfo; 16390b57cec5SDimitry Andric typename CFI_Parser<A>::CIE_Info cieInfo; 16400b57cec5SDimitry Andric bool foundFDE = false; 16410b57cec5SDimitry Andric bool foundInCache = false; 16420b57cec5SDimitry Andric // If compact encoding table gave offset into dwarf section, go directly there 16430b57cec5SDimitry Andric if (fdeSectionOffsetHint != 0) { 16440b57cec5SDimitry Andric foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 1645e8d8bef9SDimitry Andric sects.dwarf_section_length, 16460b57cec5SDimitry Andric sects.dwarf_section + fdeSectionOffsetHint, 16470b57cec5SDimitry Andric &fdeInfo, &cieInfo); 16480b57cec5SDimitry Andric } 16490b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) 16500b57cec5SDimitry Andric if (!foundFDE && (sects.dwarf_index_section != 0)) { 16510b57cec5SDimitry Andric foundFDE = EHHeaderParser<A>::findFDE( 16520b57cec5SDimitry Andric _addressSpace, pc, sects.dwarf_index_section, 16530b57cec5SDimitry Andric (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo); 16540b57cec5SDimitry Andric } 16550b57cec5SDimitry Andric #endif 16560b57cec5SDimitry Andric if (!foundFDE) { 16570b57cec5SDimitry Andric // otherwise, search cache of previously found FDEs. 16580b57cec5SDimitry Andric pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc); 16590b57cec5SDimitry Andric if (cachedFDE != 0) { 16600b57cec5SDimitry Andric foundFDE = 16610b57cec5SDimitry Andric CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 1662e8d8bef9SDimitry Andric sects.dwarf_section_length, 16630b57cec5SDimitry Andric cachedFDE, &fdeInfo, &cieInfo); 16640b57cec5SDimitry Andric foundInCache = foundFDE; 16650b57cec5SDimitry Andric } 16660b57cec5SDimitry Andric } 16670b57cec5SDimitry Andric if (!foundFDE) { 16680b57cec5SDimitry Andric // Still not found, do full scan of __eh_frame section. 16690b57cec5SDimitry Andric foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 1670e8d8bef9SDimitry Andric sects.dwarf_section_length, 0, 16710b57cec5SDimitry Andric &fdeInfo, &cieInfo); 16720b57cec5SDimitry Andric } 16730b57cec5SDimitry Andric if (foundFDE) { 1674e8d8bef9SDimitry Andric if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) { 16750b57cec5SDimitry Andric // Add to cache (to make next lookup faster) if we had no hint 16760b57cec5SDimitry Andric // and there was no index. 16770b57cec5SDimitry Andric if (!foundInCache && (fdeSectionOffsetHint == 0)) { 16780b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) 16790b57cec5SDimitry Andric if (sects.dwarf_index_section == 0) 16800b57cec5SDimitry Andric #endif 16810b57cec5SDimitry Andric DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd, 16820b57cec5SDimitry Andric fdeInfo.fdeStart); 16830b57cec5SDimitry Andric } 16840b57cec5SDimitry Andric return true; 16850b57cec5SDimitry Andric } 16860b57cec5SDimitry Andric } 16870b57cec5SDimitry Andric //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc); 16880b57cec5SDimitry Andric return false; 16890b57cec5SDimitry Andric } 16900b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 16910b57cec5SDimitry Andric 16920b57cec5SDimitry Andric 16930b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 16940b57cec5SDimitry Andric template <typename A, typename R> 16950b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc, 16960b57cec5SDimitry Andric const UnwindInfoSections §s) { 16970b57cec5SDimitry Andric const bool log = false; 16980b57cec5SDimitry Andric if (log) 16990b57cec5SDimitry Andric fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n", 17000b57cec5SDimitry Andric (uint64_t)pc, (uint64_t)sects.dso_base); 17010b57cec5SDimitry Andric 17020b57cec5SDimitry Andric const UnwindSectionHeader<A> sectionHeader(_addressSpace, 17030b57cec5SDimitry Andric sects.compact_unwind_section); 17040b57cec5SDimitry Andric if (sectionHeader.version() != UNWIND_SECTION_VERSION) 17050b57cec5SDimitry Andric return false; 17060b57cec5SDimitry Andric 17070b57cec5SDimitry Andric // do a binary search of top level index to find page with unwind info 17080b57cec5SDimitry Andric pint_t targetFunctionOffset = pc - sects.dso_base; 17090b57cec5SDimitry Andric const UnwindSectionIndexArray<A> topIndex(_addressSpace, 17100b57cec5SDimitry Andric sects.compact_unwind_section 17110b57cec5SDimitry Andric + sectionHeader.indexSectionOffset()); 17120b57cec5SDimitry Andric uint32_t low = 0; 17130b57cec5SDimitry Andric uint32_t high = sectionHeader.indexCount(); 17140b57cec5SDimitry Andric uint32_t last = high - 1; 17150b57cec5SDimitry Andric while (low < high) { 17160b57cec5SDimitry Andric uint32_t mid = (low + high) / 2; 17170b57cec5SDimitry Andric //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n", 17180b57cec5SDimitry Andric //mid, low, high, topIndex.functionOffset(mid)); 17190b57cec5SDimitry Andric if (topIndex.functionOffset(mid) <= targetFunctionOffset) { 17200b57cec5SDimitry Andric if ((mid == last) || 17210b57cec5SDimitry Andric (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) { 17220b57cec5SDimitry Andric low = mid; 17230b57cec5SDimitry Andric break; 17240b57cec5SDimitry Andric } else { 17250b57cec5SDimitry Andric low = mid + 1; 17260b57cec5SDimitry Andric } 17270b57cec5SDimitry Andric } else { 17280b57cec5SDimitry Andric high = mid; 17290b57cec5SDimitry Andric } 17300b57cec5SDimitry Andric } 17310b57cec5SDimitry Andric const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low); 17320b57cec5SDimitry Andric const uint32_t firstLevelNextPageFunctionOffset = 17330b57cec5SDimitry Andric topIndex.functionOffset(low + 1); 17340b57cec5SDimitry Andric const pint_t secondLevelAddr = 17350b57cec5SDimitry Andric sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low); 17360b57cec5SDimitry Andric const pint_t lsdaArrayStartAddr = 17370b57cec5SDimitry Andric sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low); 17380b57cec5SDimitry Andric const pint_t lsdaArrayEndAddr = 17390b57cec5SDimitry Andric sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1); 17400b57cec5SDimitry Andric if (log) 17410b57cec5SDimitry Andric fprintf(stderr, "\tfirst level search for result index=%d " 17420b57cec5SDimitry Andric "to secondLevelAddr=0x%llX\n", 17430b57cec5SDimitry Andric low, (uint64_t) secondLevelAddr); 17440b57cec5SDimitry Andric // do a binary search of second level page index 17450b57cec5SDimitry Andric uint32_t encoding = 0; 17460b57cec5SDimitry Andric pint_t funcStart = 0; 17470b57cec5SDimitry Andric pint_t funcEnd = 0; 17480b57cec5SDimitry Andric pint_t lsda = 0; 17490b57cec5SDimitry Andric pint_t personality = 0; 17500b57cec5SDimitry Andric uint32_t pageKind = _addressSpace.get32(secondLevelAddr); 17510b57cec5SDimitry Andric if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) { 17520b57cec5SDimitry Andric // regular page 17530b57cec5SDimitry Andric UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace, 17540b57cec5SDimitry Andric secondLevelAddr); 17550b57cec5SDimitry Andric UnwindSectionRegularArray<A> pageIndex( 17560b57cec5SDimitry Andric _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); 17570b57cec5SDimitry Andric // binary search looks for entry with e where index[e].offset <= pc < 17580b57cec5SDimitry Andric // index[e+1].offset 17590b57cec5SDimitry Andric if (log) 17600b57cec5SDimitry Andric fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in " 17610b57cec5SDimitry Andric "regular page starting at secondLevelAddr=0x%llX\n", 17620b57cec5SDimitry Andric (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr); 17630b57cec5SDimitry Andric low = 0; 17640b57cec5SDimitry Andric high = pageHeader.entryCount(); 17650b57cec5SDimitry Andric while (low < high) { 17660b57cec5SDimitry Andric uint32_t mid = (low + high) / 2; 17670b57cec5SDimitry Andric if (pageIndex.functionOffset(mid) <= targetFunctionOffset) { 17680b57cec5SDimitry Andric if (mid == (uint32_t)(pageHeader.entryCount() - 1)) { 17690b57cec5SDimitry Andric // at end of table 17700b57cec5SDimitry Andric low = mid; 17710b57cec5SDimitry Andric funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; 17720b57cec5SDimitry Andric break; 17730b57cec5SDimitry Andric } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) { 17740b57cec5SDimitry Andric // next is too big, so we found it 17750b57cec5SDimitry Andric low = mid; 17760b57cec5SDimitry Andric funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base; 17770b57cec5SDimitry Andric break; 17780b57cec5SDimitry Andric } else { 17790b57cec5SDimitry Andric low = mid + 1; 17800b57cec5SDimitry Andric } 17810b57cec5SDimitry Andric } else { 17820b57cec5SDimitry Andric high = mid; 17830b57cec5SDimitry Andric } 17840b57cec5SDimitry Andric } 17850b57cec5SDimitry Andric encoding = pageIndex.encoding(low); 17860b57cec5SDimitry Andric funcStart = pageIndex.functionOffset(low) + sects.dso_base; 17870b57cec5SDimitry Andric if (pc < funcStart) { 17880b57cec5SDimitry Andric if (log) 17890b57cec5SDimitry Andric fprintf( 17900b57cec5SDimitry Andric stderr, 17910b57cec5SDimitry Andric "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", 17920b57cec5SDimitry Andric (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); 17930b57cec5SDimitry Andric return false; 17940b57cec5SDimitry Andric } 17950b57cec5SDimitry Andric if (pc > funcEnd) { 17960b57cec5SDimitry Andric if (log) 17970b57cec5SDimitry Andric fprintf( 17980b57cec5SDimitry Andric stderr, 17990b57cec5SDimitry Andric "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", 18000b57cec5SDimitry Andric (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); 18010b57cec5SDimitry Andric return false; 18020b57cec5SDimitry Andric } 18030b57cec5SDimitry Andric } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) { 18040b57cec5SDimitry Andric // compressed page 18050b57cec5SDimitry Andric UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace, 18060b57cec5SDimitry Andric secondLevelAddr); 18070b57cec5SDimitry Andric UnwindSectionCompressedArray<A> pageIndex( 18080b57cec5SDimitry Andric _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); 18090b57cec5SDimitry Andric const uint32_t targetFunctionPageOffset = 18100b57cec5SDimitry Andric (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset); 18110b57cec5SDimitry Andric // binary search looks for entry with e where index[e].offset <= pc < 18120b57cec5SDimitry Andric // index[e+1].offset 18130b57cec5SDimitry Andric if (log) 18140b57cec5SDimitry Andric fprintf(stderr, "\tbinary search of compressed page starting at " 18150b57cec5SDimitry Andric "secondLevelAddr=0x%llX\n", 18160b57cec5SDimitry Andric (uint64_t) secondLevelAddr); 18170b57cec5SDimitry Andric low = 0; 18180b57cec5SDimitry Andric last = pageHeader.entryCount() - 1; 18190b57cec5SDimitry Andric high = pageHeader.entryCount(); 18200b57cec5SDimitry Andric while (low < high) { 18210b57cec5SDimitry Andric uint32_t mid = (low + high) / 2; 18220b57cec5SDimitry Andric if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) { 18230b57cec5SDimitry Andric if ((mid == last) || 18240b57cec5SDimitry Andric (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) { 18250b57cec5SDimitry Andric low = mid; 18260b57cec5SDimitry Andric break; 18270b57cec5SDimitry Andric } else { 18280b57cec5SDimitry Andric low = mid + 1; 18290b57cec5SDimitry Andric } 18300b57cec5SDimitry Andric } else { 18310b57cec5SDimitry Andric high = mid; 18320b57cec5SDimitry Andric } 18330b57cec5SDimitry Andric } 18340b57cec5SDimitry Andric funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset 18350b57cec5SDimitry Andric + sects.dso_base; 18360b57cec5SDimitry Andric if (low < last) 18370b57cec5SDimitry Andric funcEnd = 18380b57cec5SDimitry Andric pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset 18390b57cec5SDimitry Andric + sects.dso_base; 18400b57cec5SDimitry Andric else 18410b57cec5SDimitry Andric funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; 18420b57cec5SDimitry Andric if (pc < funcStart) { 1843fe6060f1SDimitry Andric _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX " 1844fe6060f1SDimitry Andric "not in second level compressed unwind table. " 1845fe6060f1SDimitry Andric "funcStart=0x%llX", 18460b57cec5SDimitry Andric (uint64_t) pc, (uint64_t) funcStart); 18470b57cec5SDimitry Andric return false; 18480b57cec5SDimitry Andric } 18490b57cec5SDimitry Andric if (pc > funcEnd) { 1850fe6060f1SDimitry Andric _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX " 1851fe6060f1SDimitry Andric "not in second level compressed unwind table. " 1852fe6060f1SDimitry Andric "funcEnd=0x%llX", 18530b57cec5SDimitry Andric (uint64_t) pc, (uint64_t) funcEnd); 18540b57cec5SDimitry Andric return false; 18550b57cec5SDimitry Andric } 18560b57cec5SDimitry Andric uint16_t encodingIndex = pageIndex.encodingIndex(low); 18570b57cec5SDimitry Andric if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) { 18580b57cec5SDimitry Andric // encoding is in common table in section header 18590b57cec5SDimitry Andric encoding = _addressSpace.get32( 18600b57cec5SDimitry Andric sects.compact_unwind_section + 18610b57cec5SDimitry Andric sectionHeader.commonEncodingsArraySectionOffset() + 18620b57cec5SDimitry Andric encodingIndex * sizeof(uint32_t)); 18630b57cec5SDimitry Andric } else { 18640b57cec5SDimitry Andric // encoding is in page specific table 18650b57cec5SDimitry Andric uint16_t pageEncodingIndex = 18660b57cec5SDimitry Andric encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount(); 18670b57cec5SDimitry Andric encoding = _addressSpace.get32(secondLevelAddr + 18680b57cec5SDimitry Andric pageHeader.encodingsPageOffset() + 18690b57cec5SDimitry Andric pageEncodingIndex * sizeof(uint32_t)); 18700b57cec5SDimitry Andric } 18710b57cec5SDimitry Andric } else { 1872fe6060f1SDimitry Andric _LIBUNWIND_DEBUG_LOG( 1873fe6060f1SDimitry Andric "malformed __unwind_info at 0x%0llX bad second level page", 18740b57cec5SDimitry Andric (uint64_t)sects.compact_unwind_section); 18750b57cec5SDimitry Andric return false; 18760b57cec5SDimitry Andric } 18770b57cec5SDimitry Andric 18780b57cec5SDimitry Andric // look up LSDA, if encoding says function has one 18790b57cec5SDimitry Andric if (encoding & UNWIND_HAS_LSDA) { 18800b57cec5SDimitry Andric UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr); 18810b57cec5SDimitry Andric uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base); 18820b57cec5SDimitry Andric low = 0; 18830b57cec5SDimitry Andric high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) / 18840b57cec5SDimitry Andric sizeof(unwind_info_section_header_lsda_index_entry); 18850b57cec5SDimitry Andric // binary search looks for entry with exact match for functionOffset 18860b57cec5SDimitry Andric if (log) 18870b57cec5SDimitry Andric fprintf(stderr, 18880b57cec5SDimitry Andric "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n", 18890b57cec5SDimitry Andric funcStartOffset); 18900b57cec5SDimitry Andric while (low < high) { 18910b57cec5SDimitry Andric uint32_t mid = (low + high) / 2; 18920b57cec5SDimitry Andric if (lsdaIndex.functionOffset(mid) == funcStartOffset) { 18930b57cec5SDimitry Andric lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base; 18940b57cec5SDimitry Andric break; 18950b57cec5SDimitry Andric } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) { 18960b57cec5SDimitry Andric low = mid + 1; 18970b57cec5SDimitry Andric } else { 18980b57cec5SDimitry Andric high = mid; 18990b57cec5SDimitry Andric } 19000b57cec5SDimitry Andric } 19010b57cec5SDimitry Andric if (lsda == 0) { 19020b57cec5SDimitry Andric _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for " 19030b57cec5SDimitry Andric "pc=0x%0llX, but lsda table has no entry", 19040b57cec5SDimitry Andric encoding, (uint64_t) pc); 19050b57cec5SDimitry Andric return false; 19060b57cec5SDimitry Andric } 19070b57cec5SDimitry Andric } 19080b57cec5SDimitry Andric 1909e8d8bef9SDimitry Andric // extract personality routine, if encoding says function has one 19100b57cec5SDimitry Andric uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >> 19110b57cec5SDimitry Andric (__builtin_ctz(UNWIND_PERSONALITY_MASK)); 19120b57cec5SDimitry Andric if (personalityIndex != 0) { 19130b57cec5SDimitry Andric --personalityIndex; // change 1-based to zero-based index 1914e8d8bef9SDimitry Andric if (personalityIndex >= sectionHeader.personalityArrayCount()) { 19150b57cec5SDimitry Andric _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, " 19160b57cec5SDimitry Andric "but personality table has only %d entries", 19170b57cec5SDimitry Andric encoding, personalityIndex, 19180b57cec5SDimitry Andric sectionHeader.personalityArrayCount()); 19190b57cec5SDimitry Andric return false; 19200b57cec5SDimitry Andric } 19210b57cec5SDimitry Andric int32_t personalityDelta = (int32_t)_addressSpace.get32( 19220b57cec5SDimitry Andric sects.compact_unwind_section + 19230b57cec5SDimitry Andric sectionHeader.personalityArraySectionOffset() + 19240b57cec5SDimitry Andric personalityIndex * sizeof(uint32_t)); 19250b57cec5SDimitry Andric pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta; 19260b57cec5SDimitry Andric personality = _addressSpace.getP(personalityPointer); 19270b57cec5SDimitry Andric if (log) 19280b57cec5SDimitry Andric fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " 19290b57cec5SDimitry Andric "personalityDelta=0x%08X, personality=0x%08llX\n", 19300b57cec5SDimitry Andric (uint64_t) pc, personalityDelta, (uint64_t) personality); 19310b57cec5SDimitry Andric } 19320b57cec5SDimitry Andric 19330b57cec5SDimitry Andric if (log) 19340b57cec5SDimitry Andric fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " 19350b57cec5SDimitry Andric "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n", 19360b57cec5SDimitry Andric (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart); 19370b57cec5SDimitry Andric _info.start_ip = funcStart; 19380b57cec5SDimitry Andric _info.end_ip = funcEnd; 19390b57cec5SDimitry Andric _info.lsda = lsda; 19400b57cec5SDimitry Andric _info.handler = personality; 19410b57cec5SDimitry Andric _info.gp = 0; 19420b57cec5SDimitry Andric _info.flags = 0; 19430b57cec5SDimitry Andric _info.format = encoding; 19440b57cec5SDimitry Andric _info.unwind_info = 0; 19450b57cec5SDimitry Andric _info.unwind_info_size = 0; 19460b57cec5SDimitry Andric _info.extra = sects.dso_base; 19470b57cec5SDimitry Andric return true; 19480b57cec5SDimitry Andric } 19490b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 19500b57cec5SDimitry Andric 19510b57cec5SDimitry Andric 19520b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 19530b57cec5SDimitry Andric template <typename A, typename R> 19540b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) { 19550b57cec5SDimitry Andric pint_t base; 19560b57cec5SDimitry Andric RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base); 19570b57cec5SDimitry Andric if (!unwindEntry) { 19580b57cec5SDimitry Andric _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc); 19590b57cec5SDimitry Andric return false; 19600b57cec5SDimitry Andric } 19610b57cec5SDimitry Andric _info.gp = 0; 19620b57cec5SDimitry Andric _info.flags = 0; 19630b57cec5SDimitry Andric _info.format = 0; 19640b57cec5SDimitry Andric _info.unwind_info_size = sizeof(RUNTIME_FUNCTION); 19650b57cec5SDimitry Andric _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry); 19660b57cec5SDimitry Andric _info.extra = base; 19670b57cec5SDimitry Andric _info.start_ip = base + unwindEntry->BeginAddress; 19680b57cec5SDimitry Andric #ifdef _LIBUNWIND_TARGET_X86_64 19690b57cec5SDimitry Andric _info.end_ip = base + unwindEntry->EndAddress; 19700b57cec5SDimitry Andric // Only fill in the handler and LSDA if they're stale. 19710b57cec5SDimitry Andric if (pc != getLastPC()) { 19720b57cec5SDimitry Andric UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData); 19730b57cec5SDimitry Andric if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) { 19740b57cec5SDimitry Andric // The personality is given in the UNWIND_INFO itself. The LSDA immediately 19750b57cec5SDimitry Andric // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit 19760b57cec5SDimitry Andric // these structures.) 19770b57cec5SDimitry Andric // N.B. UNWIND_INFO structs are DWORD-aligned. 19780b57cec5SDimitry Andric uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1; 19790b57cec5SDimitry Andric const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]); 19800b57cec5SDimitry Andric _info.lsda = reinterpret_cast<unw_word_t>(handler+1); 19810b57cec5SDimitry Andric if (*handler) { 19820b57cec5SDimitry Andric _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality); 19830b57cec5SDimitry Andric } else 19840b57cec5SDimitry Andric _info.handler = 0; 19850b57cec5SDimitry Andric } else { 19860b57cec5SDimitry Andric _info.lsda = 0; 19870b57cec5SDimitry Andric _info.handler = 0; 19880b57cec5SDimitry Andric } 19890b57cec5SDimitry Andric } 19900b57cec5SDimitry Andric #endif 19910b57cec5SDimitry Andric setLastPC(pc); 19920b57cec5SDimitry Andric return true; 19930b57cec5SDimitry Andric } 19940b57cec5SDimitry Andric #endif 19950b57cec5SDimitry Andric 199681ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) 199781ad6265SDimitry Andric // Masks for traceback table field xtbtable. 199881ad6265SDimitry Andric enum xTBTableMask : uint8_t { 199981ad6265SDimitry Andric reservedBit = 0x02, // The traceback table was incorrectly generated if set 200081ad6265SDimitry Andric // (see comments in function getInfoFromTBTable(). 200181ad6265SDimitry Andric ehInfoBit = 0x08 // Exception handling info is present if set 200281ad6265SDimitry Andric }; 200381ad6265SDimitry Andric 200481ad6265SDimitry Andric enum frameType : unw_word_t { 200581ad6265SDimitry Andric frameWithXLEHStateTable = 0, 200681ad6265SDimitry Andric frameWithEHInfo = 1 200781ad6265SDimitry Andric }; 200881ad6265SDimitry Andric 200981ad6265SDimitry Andric extern "C" { 201081ad6265SDimitry Andric typedef _Unwind_Reason_Code __xlcxx_personality_v0_t(int, _Unwind_Action, 201181ad6265SDimitry Andric uint64_t, 201281ad6265SDimitry Andric _Unwind_Exception *, 201381ad6265SDimitry Andric struct _Unwind_Context *); 201481ad6265SDimitry Andric __attribute__((__weak__)) __xlcxx_personality_v0_t __xlcxx_personality_v0; 201581ad6265SDimitry Andric } 201681ad6265SDimitry Andric 201781ad6265SDimitry Andric static __xlcxx_personality_v0_t *xlcPersonalityV0; 201881ad6265SDimitry Andric static RWMutex xlcPersonalityV0InitLock; 201981ad6265SDimitry Andric 202081ad6265SDimitry Andric template <typename A, typename R> 202181ad6265SDimitry Andric bool UnwindCursor<A, R>::getInfoFromTBTable(pint_t pc, R ®isters) { 202281ad6265SDimitry Andric uint32_t *p = reinterpret_cast<uint32_t *>(pc); 202381ad6265SDimitry Andric 202481ad6265SDimitry Andric // Keep looking forward until a word of 0 is found. The traceback 202581ad6265SDimitry Andric // table starts at the following word. 202681ad6265SDimitry Andric while (*p) 202781ad6265SDimitry Andric ++p; 202881ad6265SDimitry Andric tbtable *TBTable = reinterpret_cast<tbtable *>(p + 1); 202981ad6265SDimitry Andric 203081ad6265SDimitry Andric if (_LIBUNWIND_TRACING_UNWINDING) { 203181ad6265SDimitry Andric char functionBuf[512]; 203281ad6265SDimitry Andric const char *functionName = functionBuf; 203381ad6265SDimitry Andric unw_word_t offset; 203481ad6265SDimitry Andric if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) { 203581ad6265SDimitry Andric functionName = ".anonymous."; 203681ad6265SDimitry Andric } 203781ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("%s: Look up traceback table of func=%s at %p", 203881ad6265SDimitry Andric __func__, functionName, 203981ad6265SDimitry Andric reinterpret_cast<void *>(TBTable)); 204081ad6265SDimitry Andric } 204181ad6265SDimitry Andric 204281ad6265SDimitry Andric // If the traceback table does not contain necessary info, bypass this frame. 204381ad6265SDimitry Andric if (!TBTable->tb.has_tboff) 204481ad6265SDimitry Andric return false; 204581ad6265SDimitry Andric 204681ad6265SDimitry Andric // Structure tbtable_ext contains important data we are looking for. 204781ad6265SDimitry Andric p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext); 204881ad6265SDimitry Andric 204981ad6265SDimitry Andric // Skip field parminfo if it exists. 205081ad6265SDimitry Andric if (TBTable->tb.fixedparms || TBTable->tb.floatparms) 205181ad6265SDimitry Andric ++p; 205281ad6265SDimitry Andric 205381ad6265SDimitry Andric // p now points to tb_offset, the offset from start of function to TB table. 205481ad6265SDimitry Andric unw_word_t start_ip = 205581ad6265SDimitry Andric reinterpret_cast<unw_word_t>(TBTable) - *p - sizeof(uint32_t); 205681ad6265SDimitry Andric unw_word_t end_ip = reinterpret_cast<unw_word_t>(TBTable); 205781ad6265SDimitry Andric ++p; 205881ad6265SDimitry Andric 205981ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("start_ip=%p, end_ip=%p\n", 206081ad6265SDimitry Andric reinterpret_cast<void *>(start_ip), 206181ad6265SDimitry Andric reinterpret_cast<void *>(end_ip)); 206281ad6265SDimitry Andric 206381ad6265SDimitry Andric // Skip field hand_mask if it exists. 206481ad6265SDimitry Andric if (TBTable->tb.int_hndl) 206581ad6265SDimitry Andric ++p; 206681ad6265SDimitry Andric 206781ad6265SDimitry Andric unw_word_t lsda = 0; 206881ad6265SDimitry Andric unw_word_t handler = 0; 206981ad6265SDimitry Andric unw_word_t flags = frameType::frameWithXLEHStateTable; 207081ad6265SDimitry Andric 207181ad6265SDimitry Andric if (TBTable->tb.lang == TB_CPLUSPLUS && TBTable->tb.has_ctl) { 207281ad6265SDimitry Andric // State table info is available. The ctl_info field indicates the 207381ad6265SDimitry Andric // number of CTL anchors. There should be only one entry for the C++ 207481ad6265SDimitry Andric // state table. 207581ad6265SDimitry Andric assert(*p == 1 && "libunwind: there must be only one ctl_info entry"); 207681ad6265SDimitry Andric ++p; 207781ad6265SDimitry Andric // p points to the offset of the state table into the stack. 207881ad6265SDimitry Andric pint_t stateTableOffset = *p++; 207981ad6265SDimitry Andric 208081ad6265SDimitry Andric int framePointerReg; 208181ad6265SDimitry Andric 208281ad6265SDimitry Andric // Skip fields name_len and name if exist. 208381ad6265SDimitry Andric if (TBTable->tb.name_present) { 208481ad6265SDimitry Andric const uint16_t name_len = *(reinterpret_cast<uint16_t *>(p)); 208581ad6265SDimitry Andric p = reinterpret_cast<uint32_t *>(reinterpret_cast<char *>(p) + name_len + 208681ad6265SDimitry Andric sizeof(uint16_t)); 208781ad6265SDimitry Andric } 208881ad6265SDimitry Andric 208981ad6265SDimitry Andric if (TBTable->tb.uses_alloca) 209081ad6265SDimitry Andric framePointerReg = *(reinterpret_cast<char *>(p)); 209181ad6265SDimitry Andric else 209281ad6265SDimitry Andric framePointerReg = 1; // default frame pointer == SP 209381ad6265SDimitry Andric 209481ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 209581ad6265SDimitry Andric "framePointerReg=%d, framePointer=%p, " 209681ad6265SDimitry Andric "stateTableOffset=%#lx\n", 209781ad6265SDimitry Andric framePointerReg, 209881ad6265SDimitry Andric reinterpret_cast<void *>(_registers.getRegister(framePointerReg)), 209981ad6265SDimitry Andric stateTableOffset); 210081ad6265SDimitry Andric lsda = _registers.getRegister(framePointerReg) + stateTableOffset; 210181ad6265SDimitry Andric 210281ad6265SDimitry Andric // Since the traceback table generated by the legacy XLC++ does not 210381ad6265SDimitry Andric // provide the location of the personality for the state table, 210481ad6265SDimitry Andric // function __xlcxx_personality_v0(), which is the personality for the state 210581ad6265SDimitry Andric // table and is exported from libc++abi, is directly assigned as the 210681ad6265SDimitry Andric // handler here. When a legacy XLC++ frame is encountered, the symbol 210781ad6265SDimitry Andric // is resolved dynamically using dlopen() to avoid hard dependency from 210881ad6265SDimitry Andric // libunwind on libc++abi. 210981ad6265SDimitry Andric 211081ad6265SDimitry Andric // Resolve the function pointer to the state table personality if it has 211181ad6265SDimitry Andric // not already. 211281ad6265SDimitry Andric if (xlcPersonalityV0 == NULL) { 211381ad6265SDimitry Andric xlcPersonalityV0InitLock.lock(); 211481ad6265SDimitry Andric if (xlcPersonalityV0 == NULL) { 211581ad6265SDimitry Andric // If libc++abi is statically linked in, symbol __xlcxx_personality_v0 211681ad6265SDimitry Andric // has been resolved at the link time. 211781ad6265SDimitry Andric xlcPersonalityV0 = &__xlcxx_personality_v0; 211881ad6265SDimitry Andric if (xlcPersonalityV0 == NULL) { 211981ad6265SDimitry Andric // libc++abi is dynamically linked. Resolve __xlcxx_personality_v0 212081ad6265SDimitry Andric // using dlopen(). 212181ad6265SDimitry Andric const char libcxxabi[] = "libc++abi.a(libc++abi.so.1)"; 212281ad6265SDimitry Andric void *libHandle; 2123*bdd1243dSDimitry Andric // The AIX dlopen() sets errno to 0 when it is successful, which 2124*bdd1243dSDimitry Andric // clobbers the value of errno from the user code. This is an AIX 2125*bdd1243dSDimitry Andric // bug because according to POSIX it should not set errno to 0. To 2126*bdd1243dSDimitry Andric // workaround before AIX fixes the bug, errno is saved and restored. 2127*bdd1243dSDimitry Andric int saveErrno = errno; 212881ad6265SDimitry Andric libHandle = dlopen(libcxxabi, RTLD_MEMBER | RTLD_NOW); 212981ad6265SDimitry Andric if (libHandle == NULL) { 213081ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("dlopen() failed with errno=%d\n", 213181ad6265SDimitry Andric errno); 213281ad6265SDimitry Andric assert(0 && "dlopen() failed"); 213381ad6265SDimitry Andric } 213481ad6265SDimitry Andric xlcPersonalityV0 = reinterpret_cast<__xlcxx_personality_v0_t *>( 213581ad6265SDimitry Andric dlsym(libHandle, "__xlcxx_personality_v0")); 213681ad6265SDimitry Andric if (xlcPersonalityV0 == NULL) { 213781ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("dlsym() failed with errno=%d\n", errno); 213881ad6265SDimitry Andric assert(0 && "dlsym() failed"); 213981ad6265SDimitry Andric } 214081ad6265SDimitry Andric dlclose(libHandle); 2141*bdd1243dSDimitry Andric errno = saveErrno; 214281ad6265SDimitry Andric } 214381ad6265SDimitry Andric } 214481ad6265SDimitry Andric xlcPersonalityV0InitLock.unlock(); 214581ad6265SDimitry Andric } 214681ad6265SDimitry Andric handler = reinterpret_cast<unw_word_t>(xlcPersonalityV0); 214781ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("State table: LSDA=%p, Personality=%p\n", 214881ad6265SDimitry Andric reinterpret_cast<void *>(lsda), 214981ad6265SDimitry Andric reinterpret_cast<void *>(handler)); 215081ad6265SDimitry Andric } else if (TBTable->tb.longtbtable) { 215181ad6265SDimitry Andric // This frame has the traceback table extension. Possible cases are 215281ad6265SDimitry Andric // 1) a C++ frame that has the 'eh_info' structure; 2) a C++ frame that 215381ad6265SDimitry Andric // is not EH aware; or, 3) a frame of other languages. We need to figure out 215481ad6265SDimitry Andric // if the traceback table extension contains the 'eh_info' structure. 215581ad6265SDimitry Andric // 215681ad6265SDimitry Andric // We also need to deal with the complexity arising from some XL compiler 215781ad6265SDimitry Andric // versions use the wrong ordering of 'longtbtable' and 'has_vec' bits 215881ad6265SDimitry Andric // where the 'longtbtable' bit is meant to be the 'has_vec' bit and vice 215981ad6265SDimitry Andric // versa. For frames of code generated by those compilers, the 'longtbtable' 216081ad6265SDimitry Andric // bit may be set but there isn't really a traceback table extension. 216181ad6265SDimitry Andric // 216281ad6265SDimitry Andric // In </usr/include/sys/debug.h>, there is the following definition of 216381ad6265SDimitry Andric // 'struct tbtable_ext'. It is not really a structure but a dummy to 216481ad6265SDimitry Andric // collect the description of optional parts of the traceback table. 216581ad6265SDimitry Andric // 216681ad6265SDimitry Andric // struct tbtable_ext { 216781ad6265SDimitry Andric // ... 216881ad6265SDimitry Andric // char alloca_reg; /* Register for alloca automatic storage */ 216981ad6265SDimitry Andric // struct vec_ext vec_ext; /* Vector extension (if has_vec is set) */ 217081ad6265SDimitry Andric // unsigned char xtbtable; /* More tbtable fields, if longtbtable is set*/ 217181ad6265SDimitry Andric // }; 217281ad6265SDimitry Andric // 217381ad6265SDimitry Andric // Depending on how the 'has_vec'/'longtbtable' bit is interpreted, the data 217481ad6265SDimitry Andric // following 'alloca_reg' can be treated either as 'struct vec_ext' or 217581ad6265SDimitry Andric // 'unsigned char xtbtable'. 'xtbtable' bits are defined in 217681ad6265SDimitry Andric // </usr/include/sys/debug.h> as flags. The 7th bit '0x02' is currently 217781ad6265SDimitry Andric // unused and should not be set. 'struct vec_ext' is defined in 217881ad6265SDimitry Andric // </usr/include/sys/debug.h> as follows: 217981ad6265SDimitry Andric // 218081ad6265SDimitry Andric // struct vec_ext { 218181ad6265SDimitry Andric // unsigned vr_saved:6; /* Number of non-volatile vector regs saved 218281ad6265SDimitry Andric // */ 218381ad6265SDimitry Andric // /* first register saved is assumed to be */ 218481ad6265SDimitry Andric // /* 32 - vr_saved */ 218581ad6265SDimitry Andric // unsigned saves_vrsave:1; /* Set if vrsave is saved on the stack */ 218681ad6265SDimitry Andric // unsigned has_varargs:1; 218781ad6265SDimitry Andric // ... 218881ad6265SDimitry Andric // }; 218981ad6265SDimitry Andric // 219081ad6265SDimitry Andric // Here, the 7th bit is used as 'saves_vrsave'. To determine whether it 219181ad6265SDimitry Andric // is 'struct vec_ext' or 'xtbtable' that follows 'alloca_reg', 219281ad6265SDimitry Andric // we checks if the 7th bit is set or not because 'xtbtable' should 219381ad6265SDimitry Andric // never have the 7th bit set. The 7th bit of 'xtbtable' will be reserved 219481ad6265SDimitry Andric // in the future to make sure the mitigation works. This mitigation 219581ad6265SDimitry Andric // is not 100% bullet proof because 'struct vec_ext' may not always have 219681ad6265SDimitry Andric // 'saves_vrsave' bit set. 219781ad6265SDimitry Andric // 219881ad6265SDimitry Andric // 'reservedBit' is defined in enum 'xTBTableMask' above as the mask for 219981ad6265SDimitry Andric // checking the 7th bit. 220081ad6265SDimitry Andric 220181ad6265SDimitry Andric // p points to field name len. 220281ad6265SDimitry Andric uint8_t *charPtr = reinterpret_cast<uint8_t *>(p); 220381ad6265SDimitry Andric 220481ad6265SDimitry Andric // Skip fields name_len and name if they exist. 220581ad6265SDimitry Andric if (TBTable->tb.name_present) { 220681ad6265SDimitry Andric const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr)); 220781ad6265SDimitry Andric charPtr = charPtr + name_len + sizeof(uint16_t); 220881ad6265SDimitry Andric } 220981ad6265SDimitry Andric 221081ad6265SDimitry Andric // Skip field alloc_reg if it exists. 221181ad6265SDimitry Andric if (TBTable->tb.uses_alloca) 221281ad6265SDimitry Andric ++charPtr; 221381ad6265SDimitry Andric 221481ad6265SDimitry Andric // Check traceback table bit has_vec. Skip struct vec_ext if it exists. 221581ad6265SDimitry Andric if (TBTable->tb.has_vec) 221681ad6265SDimitry Andric // Note struct vec_ext does exist at this point because whether the 221781ad6265SDimitry Andric // ordering of longtbtable and has_vec bits is correct or not, both 221881ad6265SDimitry Andric // are set. 221981ad6265SDimitry Andric charPtr += sizeof(struct vec_ext); 222081ad6265SDimitry Andric 222181ad6265SDimitry Andric // charPtr points to field 'xtbtable'. Check if the EH info is available. 222281ad6265SDimitry Andric // Also check if the reserved bit of the extended traceback table field 222381ad6265SDimitry Andric // 'xtbtable' is set. If it is, the traceback table was incorrectly 222481ad6265SDimitry Andric // generated by an XL compiler that uses the wrong ordering of 'longtbtable' 222581ad6265SDimitry Andric // and 'has_vec' bits and this is in fact 'struct vec_ext'. So skip the 222681ad6265SDimitry Andric // frame. 222781ad6265SDimitry Andric if ((*charPtr & xTBTableMask::ehInfoBit) && 222881ad6265SDimitry Andric !(*charPtr & xTBTableMask::reservedBit)) { 222981ad6265SDimitry Andric // Mark this frame has the new EH info. 223081ad6265SDimitry Andric flags = frameType::frameWithEHInfo; 223181ad6265SDimitry Andric 223281ad6265SDimitry Andric // eh_info is available. 223381ad6265SDimitry Andric charPtr++; 223481ad6265SDimitry Andric // The pointer is 4-byte aligned. 223581ad6265SDimitry Andric if (reinterpret_cast<uintptr_t>(charPtr) % 4) 223681ad6265SDimitry Andric charPtr += 4 - reinterpret_cast<uintptr_t>(charPtr) % 4; 223781ad6265SDimitry Andric uintptr_t *ehInfo = 223881ad6265SDimitry Andric reinterpret_cast<uintptr_t *>(*(reinterpret_cast<uintptr_t *>( 223981ad6265SDimitry Andric registers.getRegister(2) + 224081ad6265SDimitry Andric *(reinterpret_cast<uintptr_t *>(charPtr))))); 224181ad6265SDimitry Andric 224281ad6265SDimitry Andric // ehInfo points to structure en_info. The first member is version. 224381ad6265SDimitry Andric // Only version 0 is currently supported. 224481ad6265SDimitry Andric assert(*(reinterpret_cast<uint32_t *>(ehInfo)) == 0 && 224581ad6265SDimitry Andric "libunwind: ehInfo version other than 0 is not supported"); 224681ad6265SDimitry Andric 224781ad6265SDimitry Andric // Increment ehInfo to point to member lsda. 224881ad6265SDimitry Andric ++ehInfo; 224981ad6265SDimitry Andric lsda = *ehInfo++; 225081ad6265SDimitry Andric 225181ad6265SDimitry Andric // enInfo now points to member personality. 225281ad6265SDimitry Andric handler = *ehInfo; 225381ad6265SDimitry Andric 225481ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("Range table: LSDA=%#lx, Personality=%#lx\n", 225581ad6265SDimitry Andric lsda, handler); 225681ad6265SDimitry Andric } 225781ad6265SDimitry Andric } 225881ad6265SDimitry Andric 225981ad6265SDimitry Andric _info.start_ip = start_ip; 226081ad6265SDimitry Andric _info.end_ip = end_ip; 226181ad6265SDimitry Andric _info.lsda = lsda; 226281ad6265SDimitry Andric _info.handler = handler; 226381ad6265SDimitry Andric _info.gp = 0; 226481ad6265SDimitry Andric _info.flags = flags; 226581ad6265SDimitry Andric _info.format = 0; 226681ad6265SDimitry Andric _info.unwind_info = reinterpret_cast<unw_word_t>(TBTable); 226781ad6265SDimitry Andric _info.unwind_info_size = 0; 226881ad6265SDimitry Andric _info.extra = registers.getRegister(2); 226981ad6265SDimitry Andric 227081ad6265SDimitry Andric return true; 227181ad6265SDimitry Andric } 227281ad6265SDimitry Andric 227381ad6265SDimitry Andric // Step back up the stack following the frame back link. 227481ad6265SDimitry Andric template <typename A, typename R> 227581ad6265SDimitry Andric int UnwindCursor<A, R>::stepWithTBTable(pint_t pc, tbtable *TBTable, 227681ad6265SDimitry Andric R ®isters, bool &isSignalFrame) { 227781ad6265SDimitry Andric if (_LIBUNWIND_TRACING_UNWINDING) { 227881ad6265SDimitry Andric char functionBuf[512]; 227981ad6265SDimitry Andric const char *functionName = functionBuf; 228081ad6265SDimitry Andric unw_word_t offset; 228181ad6265SDimitry Andric if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) { 228281ad6265SDimitry Andric functionName = ".anonymous."; 228381ad6265SDimitry Andric } 228481ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("%s: Look up traceback table of func=%s at %p", 228581ad6265SDimitry Andric __func__, functionName, 228681ad6265SDimitry Andric reinterpret_cast<void *>(TBTable)); 228781ad6265SDimitry Andric } 228881ad6265SDimitry Andric 228981ad6265SDimitry Andric #if defined(__powerpc64__) 229081ad6265SDimitry Andric // Instruction to reload TOC register "l r2,40(r1)" 229181ad6265SDimitry Andric const uint32_t loadTOCRegInst = 0xe8410028; 229281ad6265SDimitry Andric const int32_t unwPPCF0Index = UNW_PPC64_F0; 229381ad6265SDimitry Andric const int32_t unwPPCV0Index = UNW_PPC64_V0; 229481ad6265SDimitry Andric #else 229581ad6265SDimitry Andric // Instruction to reload TOC register "l r2,20(r1)" 229681ad6265SDimitry Andric const uint32_t loadTOCRegInst = 0x80410014; 229781ad6265SDimitry Andric const int32_t unwPPCF0Index = UNW_PPC_F0; 229881ad6265SDimitry Andric const int32_t unwPPCV0Index = UNW_PPC_V0; 229981ad6265SDimitry Andric #endif 230081ad6265SDimitry Andric 230181ad6265SDimitry Andric R newRegisters = registers; 230281ad6265SDimitry Andric 230381ad6265SDimitry Andric // lastStack points to the stack frame of the next routine up. 230481ad6265SDimitry Andric pint_t lastStack = *(reinterpret_cast<pint_t *>(registers.getSP())); 230581ad6265SDimitry Andric 230681ad6265SDimitry Andric // Return address is the address after call site instruction. 230781ad6265SDimitry Andric pint_t returnAddress; 230881ad6265SDimitry Andric 230981ad6265SDimitry Andric if (isSignalFrame) { 231081ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("Possible signal handler frame: lastStack=%p", 231181ad6265SDimitry Andric reinterpret_cast<void *>(lastStack)); 231281ad6265SDimitry Andric 231381ad6265SDimitry Andric sigcontext *sigContext = reinterpret_cast<sigcontext *>( 231481ad6265SDimitry Andric reinterpret_cast<char *>(lastStack) + STKMIN); 231581ad6265SDimitry Andric returnAddress = sigContext->sc_jmpbuf.jmp_context.iar; 231681ad6265SDimitry Andric 231781ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("From sigContext=%p, returnAddress=%p\n", 231881ad6265SDimitry Andric reinterpret_cast<void *>(sigContext), 231981ad6265SDimitry Andric reinterpret_cast<void *>(returnAddress)); 232081ad6265SDimitry Andric 232181ad6265SDimitry Andric if (returnAddress < 0x10000000) { 232281ad6265SDimitry Andric // Try again using STKMINALIGN 232381ad6265SDimitry Andric sigContext = reinterpret_cast<sigcontext *>( 232481ad6265SDimitry Andric reinterpret_cast<char *>(lastStack) + STKMINALIGN); 232581ad6265SDimitry Andric returnAddress = sigContext->sc_jmpbuf.jmp_context.iar; 232681ad6265SDimitry Andric if (returnAddress < 0x10000000) { 232781ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("Bad returnAddress=%p\n", 232881ad6265SDimitry Andric reinterpret_cast<void *>(returnAddress)); 232981ad6265SDimitry Andric return UNW_EBADFRAME; 233081ad6265SDimitry Andric } else { 233181ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("Tried again using STKMINALIGN: " 233281ad6265SDimitry Andric "sigContext=%p, returnAddress=%p. " 233381ad6265SDimitry Andric "Seems to be a valid address\n", 233481ad6265SDimitry Andric reinterpret_cast<void *>(sigContext), 233581ad6265SDimitry Andric reinterpret_cast<void *>(returnAddress)); 233681ad6265SDimitry Andric } 233781ad6265SDimitry Andric } 233881ad6265SDimitry Andric // Restore the condition register from sigcontext. 233981ad6265SDimitry Andric newRegisters.setCR(sigContext->sc_jmpbuf.jmp_context.cr); 234081ad6265SDimitry Andric 234181ad6265SDimitry Andric // Restore GPRs from sigcontext. 234281ad6265SDimitry Andric for (int i = 0; i < 32; ++i) 234381ad6265SDimitry Andric newRegisters.setRegister(i, sigContext->sc_jmpbuf.jmp_context.gpr[i]); 234481ad6265SDimitry Andric 234581ad6265SDimitry Andric // Restore FPRs from sigcontext. 234681ad6265SDimitry Andric for (int i = 0; i < 32; ++i) 234781ad6265SDimitry Andric newRegisters.setFloatRegister(i + unwPPCF0Index, 234881ad6265SDimitry Andric sigContext->sc_jmpbuf.jmp_context.fpr[i]); 234981ad6265SDimitry Andric 235081ad6265SDimitry Andric // Restore vector registers if there is an associated extended context 235181ad6265SDimitry Andric // structure. 235281ad6265SDimitry Andric if (sigContext->sc_jmpbuf.jmp_context.msr & __EXTCTX) { 235381ad6265SDimitry Andric ucontext_t *uContext = reinterpret_cast<ucontext_t *>(sigContext); 235481ad6265SDimitry Andric if (uContext->__extctx->__extctx_magic == __EXTCTX_MAGIC) { 235581ad6265SDimitry Andric for (int i = 0; i < 32; ++i) 235681ad6265SDimitry Andric newRegisters.setVectorRegister( 235781ad6265SDimitry Andric i + unwPPCV0Index, *(reinterpret_cast<v128 *>( 235881ad6265SDimitry Andric &(uContext->__extctx->__vmx.__vr[i])))); 235981ad6265SDimitry Andric } 236081ad6265SDimitry Andric } 236181ad6265SDimitry Andric } else { 236281ad6265SDimitry Andric // Step up a normal frame. 236381ad6265SDimitry Andric returnAddress = reinterpret_cast<pint_t *>(lastStack)[2]; 236481ad6265SDimitry Andric 236581ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("Extract info from lastStack=%p, " 236681ad6265SDimitry Andric "returnAddress=%p\n", 236781ad6265SDimitry Andric reinterpret_cast<void *>(lastStack), 236881ad6265SDimitry Andric reinterpret_cast<void *>(returnAddress)); 236981ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("fpr_regs=%d, gpr_regs=%d, saves_cr=%d\n", 237081ad6265SDimitry Andric TBTable->tb.fpr_saved, TBTable->tb.gpr_saved, 237181ad6265SDimitry Andric TBTable->tb.saves_cr); 237281ad6265SDimitry Andric 237381ad6265SDimitry Andric // Restore FP registers. 237481ad6265SDimitry Andric char *ptrToRegs = reinterpret_cast<char *>(lastStack); 237581ad6265SDimitry Andric double *FPRegs = reinterpret_cast<double *>( 237681ad6265SDimitry Andric ptrToRegs - (TBTable->tb.fpr_saved * sizeof(double))); 237781ad6265SDimitry Andric for (int i = 0; i < TBTable->tb.fpr_saved; ++i) 237881ad6265SDimitry Andric newRegisters.setFloatRegister( 237981ad6265SDimitry Andric 32 - TBTable->tb.fpr_saved + i + unwPPCF0Index, FPRegs[i]); 238081ad6265SDimitry Andric 238181ad6265SDimitry Andric // Restore GP registers. 238281ad6265SDimitry Andric ptrToRegs = reinterpret_cast<char *>(FPRegs); 238381ad6265SDimitry Andric uintptr_t *GPRegs = reinterpret_cast<uintptr_t *>( 238481ad6265SDimitry Andric ptrToRegs - (TBTable->tb.gpr_saved * sizeof(uintptr_t))); 238581ad6265SDimitry Andric for (int i = 0; i < TBTable->tb.gpr_saved; ++i) 238681ad6265SDimitry Andric newRegisters.setRegister(32 - TBTable->tb.gpr_saved + i, GPRegs[i]); 238781ad6265SDimitry Andric 238881ad6265SDimitry Andric // Restore Vector registers. 238981ad6265SDimitry Andric ptrToRegs = reinterpret_cast<char *>(GPRegs); 239081ad6265SDimitry Andric 239181ad6265SDimitry Andric // Restore vector registers only if this is a Clang frame. Also 239281ad6265SDimitry Andric // check if traceback table bit has_vec is set. If it is, structure 239381ad6265SDimitry Andric // vec_ext is available. 239481ad6265SDimitry Andric if (_info.flags == frameType::frameWithEHInfo && TBTable->tb.has_vec) { 239581ad6265SDimitry Andric 239681ad6265SDimitry Andric // Get to the vec_ext structure to check if vector registers are saved. 239781ad6265SDimitry Andric uint32_t *p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext); 239881ad6265SDimitry Andric 239981ad6265SDimitry Andric // Skip field parminfo if exists. 240081ad6265SDimitry Andric if (TBTable->tb.fixedparms || TBTable->tb.floatparms) 240181ad6265SDimitry Andric ++p; 240281ad6265SDimitry Andric 240381ad6265SDimitry Andric // Skip field tb_offset if exists. 240481ad6265SDimitry Andric if (TBTable->tb.has_tboff) 240581ad6265SDimitry Andric ++p; 240681ad6265SDimitry Andric 240781ad6265SDimitry Andric // Skip field hand_mask if exists. 240881ad6265SDimitry Andric if (TBTable->tb.int_hndl) 240981ad6265SDimitry Andric ++p; 241081ad6265SDimitry Andric 241181ad6265SDimitry Andric // Skip fields ctl_info and ctl_info_disp if exist. 241281ad6265SDimitry Andric if (TBTable->tb.has_ctl) { 241381ad6265SDimitry Andric // Skip field ctl_info. 241481ad6265SDimitry Andric ++p; 241581ad6265SDimitry Andric // Skip field ctl_info_disp. 241681ad6265SDimitry Andric ++p; 241781ad6265SDimitry Andric } 241881ad6265SDimitry Andric 241981ad6265SDimitry Andric // Skip fields name_len and name if exist. 242081ad6265SDimitry Andric // p is supposed to point to field name_len now. 242181ad6265SDimitry Andric uint8_t *charPtr = reinterpret_cast<uint8_t *>(p); 242281ad6265SDimitry Andric if (TBTable->tb.name_present) { 242381ad6265SDimitry Andric const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr)); 242481ad6265SDimitry Andric charPtr = charPtr + name_len + sizeof(uint16_t); 242581ad6265SDimitry Andric } 242681ad6265SDimitry Andric 242781ad6265SDimitry Andric // Skip field alloc_reg if it exists. 242881ad6265SDimitry Andric if (TBTable->tb.uses_alloca) 242981ad6265SDimitry Andric ++charPtr; 243081ad6265SDimitry Andric 243181ad6265SDimitry Andric struct vec_ext *vec_ext = reinterpret_cast<struct vec_ext *>(charPtr); 243281ad6265SDimitry Andric 243381ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("vr_saved=%d\n", vec_ext->vr_saved); 243481ad6265SDimitry Andric 243581ad6265SDimitry Andric // Restore vector register(s) if saved on the stack. 243681ad6265SDimitry Andric if (vec_ext->vr_saved) { 243781ad6265SDimitry Andric // Saved vector registers are 16-byte aligned. 243881ad6265SDimitry Andric if (reinterpret_cast<uintptr_t>(ptrToRegs) % 16) 243981ad6265SDimitry Andric ptrToRegs -= reinterpret_cast<uintptr_t>(ptrToRegs) % 16; 244081ad6265SDimitry Andric v128 *VecRegs = reinterpret_cast<v128 *>(ptrToRegs - vec_ext->vr_saved * 244181ad6265SDimitry Andric sizeof(v128)); 244281ad6265SDimitry Andric for (int i = 0; i < vec_ext->vr_saved; ++i) { 244381ad6265SDimitry Andric newRegisters.setVectorRegister( 244481ad6265SDimitry Andric 32 - vec_ext->vr_saved + i + unwPPCV0Index, VecRegs[i]); 244581ad6265SDimitry Andric } 244681ad6265SDimitry Andric } 244781ad6265SDimitry Andric } 244881ad6265SDimitry Andric if (TBTable->tb.saves_cr) { 244981ad6265SDimitry Andric // Get the saved condition register. The condition register is only 245081ad6265SDimitry Andric // a single word. 245181ad6265SDimitry Andric newRegisters.setCR( 245281ad6265SDimitry Andric *(reinterpret_cast<uint32_t *>(lastStack + sizeof(uintptr_t)))); 245381ad6265SDimitry Andric } 245481ad6265SDimitry Andric 245581ad6265SDimitry Andric // Restore the SP. 245681ad6265SDimitry Andric newRegisters.setSP(lastStack); 245781ad6265SDimitry Andric 245881ad6265SDimitry Andric // The first instruction after return. 245981ad6265SDimitry Andric uint32_t firstInstruction = *(reinterpret_cast<uint32_t *>(returnAddress)); 246081ad6265SDimitry Andric 246181ad6265SDimitry Andric // Do we need to set the TOC register? 246281ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 246381ad6265SDimitry Andric "Current gpr2=%p\n", 246481ad6265SDimitry Andric reinterpret_cast<void *>(newRegisters.getRegister(2))); 246581ad6265SDimitry Andric if (firstInstruction == loadTOCRegInst) { 246681ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 246781ad6265SDimitry Andric "Set gpr2=%p from frame\n", 246881ad6265SDimitry Andric reinterpret_cast<void *>(reinterpret_cast<pint_t *>(lastStack)[5])); 246981ad6265SDimitry Andric newRegisters.setRegister(2, reinterpret_cast<pint_t *>(lastStack)[5]); 247081ad6265SDimitry Andric } 247181ad6265SDimitry Andric } 247281ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("lastStack=%p, returnAddress=%p, pc=%p\n", 247381ad6265SDimitry Andric reinterpret_cast<void *>(lastStack), 247481ad6265SDimitry Andric reinterpret_cast<void *>(returnAddress), 247581ad6265SDimitry Andric reinterpret_cast<void *>(pc)); 247681ad6265SDimitry Andric 247781ad6265SDimitry Andric // The return address is the address after call site instruction, so 2478*bdd1243dSDimitry Andric // setting IP to that simulates a return. 247981ad6265SDimitry Andric newRegisters.setIP(reinterpret_cast<uintptr_t>(returnAddress)); 248081ad6265SDimitry Andric 248181ad6265SDimitry Andric // Simulate the step by replacing the register set with the new ones. 248281ad6265SDimitry Andric registers = newRegisters; 248381ad6265SDimitry Andric 248481ad6265SDimitry Andric // Check if the next frame is a signal frame. 248581ad6265SDimitry Andric pint_t nextStack = *(reinterpret_cast<pint_t *>(registers.getSP())); 248681ad6265SDimitry Andric 248781ad6265SDimitry Andric // Return address is the address after call site instruction. 248881ad6265SDimitry Andric pint_t nextReturnAddress = reinterpret_cast<pint_t *>(nextStack)[2]; 248981ad6265SDimitry Andric 249081ad6265SDimitry Andric if (nextReturnAddress > 0x01 && nextReturnAddress < 0x10000) { 249181ad6265SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("The next is a signal handler frame: " 249281ad6265SDimitry Andric "nextStack=%p, next return address=%p\n", 249381ad6265SDimitry Andric reinterpret_cast<void *>(nextStack), 249481ad6265SDimitry Andric reinterpret_cast<void *>(nextReturnAddress)); 249581ad6265SDimitry Andric isSignalFrame = true; 249681ad6265SDimitry Andric } else { 249781ad6265SDimitry Andric isSignalFrame = false; 249881ad6265SDimitry Andric } 249981ad6265SDimitry Andric 250081ad6265SDimitry Andric return UNW_STEP_SUCCESS; 250181ad6265SDimitry Andric } 250281ad6265SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) 25030b57cec5SDimitry Andric 25040b57cec5SDimitry Andric template <typename A, typename R> 25050b57cec5SDimitry Andric void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) { 250681ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) 2507e8d8bef9SDimitry Andric _isSigReturn = false; 2508e8d8bef9SDimitry Andric #endif 2509e8d8bef9SDimitry Andric 2510e8d8bef9SDimitry Andric pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP)); 25110b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI) 25120b57cec5SDimitry Andric // Remove the thumb bit so the IP represents the actual instruction address. 25130b57cec5SDimitry Andric // This matches the behaviour of _Unwind_GetIP on arm. 25140b57cec5SDimitry Andric pc &= (pint_t)~0x1; 25150b57cec5SDimitry Andric #endif 25160b57cec5SDimitry Andric 25175ffd83dbSDimitry Andric // Exit early if at the top of the stack. 25185ffd83dbSDimitry Andric if (pc == 0) { 25195ffd83dbSDimitry Andric _unwindInfoMissing = true; 25205ffd83dbSDimitry Andric return; 25215ffd83dbSDimitry Andric } 25225ffd83dbSDimitry Andric 25230b57cec5SDimitry Andric // If the last line of a function is a "throw" the compiler sometimes 25240b57cec5SDimitry Andric // emits no instructions after the call to __cxa_throw. This means 25250b57cec5SDimitry Andric // the return address is actually the start of the next function. 25260b57cec5SDimitry Andric // To disambiguate this, back up the pc when we know it is a return 25270b57cec5SDimitry Andric // address. 25280b57cec5SDimitry Andric if (isReturnAddress) 252981ad6265SDimitry Andric #if defined(_AIX) 253081ad6265SDimitry Andric // PC needs to be a 4-byte aligned address to be able to look for a 253181ad6265SDimitry Andric // word of 0 that indicates the start of the traceback table at the end 253281ad6265SDimitry Andric // of a function on AIX. 253381ad6265SDimitry Andric pc -= 4; 253481ad6265SDimitry Andric #else 25350b57cec5SDimitry Andric --pc; 253681ad6265SDimitry Andric #endif 25370b57cec5SDimitry Andric 25380b57cec5SDimitry Andric // Ask address space object to find unwind sections for this pc. 25390b57cec5SDimitry Andric UnwindInfoSections sects; 25400b57cec5SDimitry Andric if (_addressSpace.findUnwindSections(pc, sects)) { 25410b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 25420b57cec5SDimitry Andric // If there is a compact unwind encoding table, look there first. 25430b57cec5SDimitry Andric if (sects.compact_unwind_section != 0) { 25440b57cec5SDimitry Andric if (this->getInfoFromCompactEncodingSection(pc, sects)) { 25450b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 25460b57cec5SDimitry Andric // Found info in table, done unless encoding says to use dwarf. 25470b57cec5SDimitry Andric uint32_t dwarfOffset; 25480b57cec5SDimitry Andric if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) { 25490b57cec5SDimitry Andric if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) { 25500b57cec5SDimitry Andric // found info in dwarf, done 25510b57cec5SDimitry Andric return; 25520b57cec5SDimitry Andric } 25530b57cec5SDimitry Andric } 25540b57cec5SDimitry Andric #endif 25550b57cec5SDimitry Andric // If unwind table has entry, but entry says there is no unwind info, 25560b57cec5SDimitry Andric // record that we have no unwind info. 25570b57cec5SDimitry Andric if (_info.format == 0) 25580b57cec5SDimitry Andric _unwindInfoMissing = true; 25590b57cec5SDimitry Andric return; 25600b57cec5SDimitry Andric } 25610b57cec5SDimitry Andric } 25620b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 25630b57cec5SDimitry Andric 25640b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 25650b57cec5SDimitry Andric // If there is SEH unwind info, look there next. 25660b57cec5SDimitry Andric if (this->getInfoFromSEH(pc)) 25670b57cec5SDimitry Andric return; 25680b57cec5SDimitry Andric #endif 25690b57cec5SDimitry Andric 257081ad6265SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) 257181ad6265SDimitry Andric // If there is unwind info in the traceback table, look there next. 257281ad6265SDimitry Andric if (this->getInfoFromTBTable(pc, _registers)) 257381ad6265SDimitry Andric return; 257481ad6265SDimitry Andric #endif 257581ad6265SDimitry Andric 25760b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 25770b57cec5SDimitry Andric // If there is dwarf unwind info, look there next. 25780b57cec5SDimitry Andric if (sects.dwarf_section != 0) { 25790b57cec5SDimitry Andric if (this->getInfoFromDwarfSection(pc, sects)) { 25800b57cec5SDimitry Andric // found info in dwarf, done 25810b57cec5SDimitry Andric return; 25820b57cec5SDimitry Andric } 25830b57cec5SDimitry Andric } 25840b57cec5SDimitry Andric #endif 25850b57cec5SDimitry Andric 25860b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI) 25870b57cec5SDimitry Andric // If there is ARM EHABI unwind info, look there next. 25880b57cec5SDimitry Andric if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects)) 25890b57cec5SDimitry Andric return; 25900b57cec5SDimitry Andric #endif 25910b57cec5SDimitry Andric } 25920b57cec5SDimitry Andric 25930b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 25940b57cec5SDimitry Andric // There is no static unwind info for this pc. Look to see if an FDE was 25950b57cec5SDimitry Andric // dynamically registered for it. 2596e8d8bef9SDimitry Andric pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll, 2597e8d8bef9SDimitry Andric pc); 25980b57cec5SDimitry Andric if (cachedFDE != 0) { 2599e8d8bef9SDimitry Andric typename CFI_Parser<A>::FDE_Info fdeInfo; 2600e8d8bef9SDimitry Andric typename CFI_Parser<A>::CIE_Info cieInfo; 2601e8d8bef9SDimitry Andric if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo)) 2602e8d8bef9SDimitry Andric if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0)) 26030b57cec5SDimitry Andric return; 26040b57cec5SDimitry Andric } 26050b57cec5SDimitry Andric 26060b57cec5SDimitry Andric // Lastly, ask AddressSpace object about platform specific ways to locate 26070b57cec5SDimitry Andric // other FDEs. 26080b57cec5SDimitry Andric pint_t fde; 26090b57cec5SDimitry Andric if (_addressSpace.findOtherFDE(pc, fde)) { 2610e8d8bef9SDimitry Andric typename CFI_Parser<A>::FDE_Info fdeInfo; 2611e8d8bef9SDimitry Andric typename CFI_Parser<A>::CIE_Info cieInfo; 26120b57cec5SDimitry Andric if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) { 26130b57cec5SDimitry Andric // Double check this FDE is for a function that includes the pc. 2614e8d8bef9SDimitry Andric if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) 2615e8d8bef9SDimitry Andric if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0)) 26160b57cec5SDimitry Andric return; 26170b57cec5SDimitry Andric } 26180b57cec5SDimitry Andric } 26190b57cec5SDimitry Andric #endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 26200b57cec5SDimitry Andric 262181ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) 2622e8d8bef9SDimitry Andric if (setInfoForSigReturn()) 2623e8d8bef9SDimitry Andric return; 2624e8d8bef9SDimitry Andric #endif 2625e8d8bef9SDimitry Andric 26260b57cec5SDimitry Andric // no unwind info, flag that we can't reliably unwind 26270b57cec5SDimitry Andric _unwindInfoMissing = true; 26280b57cec5SDimitry Andric } 26290b57cec5SDimitry Andric 263081ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && \ 263181ad6265SDimitry Andric defined(_LIBUNWIND_TARGET_AARCH64) 2632e8d8bef9SDimitry Andric template <typename A, typename R> 2633e8d8bef9SDimitry Andric bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_arm64 &) { 2634e8d8bef9SDimitry Andric // Look for the sigreturn trampoline. The trampoline's body is two 2635e8d8bef9SDimitry Andric // specific instructions (see below). Typically the trampoline comes from the 2636e8d8bef9SDimitry Andric // vDSO[1] (i.e. the __kernel_rt_sigreturn function). A libc might provide its 2637e8d8bef9SDimitry Andric // own restorer function, though, or user-mode QEMU might write a trampoline 2638e8d8bef9SDimitry Andric // onto the stack. 2639e8d8bef9SDimitry Andric // 2640e8d8bef9SDimitry Andric // This special code path is a fallback that is only used if the trampoline 2641e8d8bef9SDimitry Andric // lacks proper (e.g. DWARF) unwind info. On AArch64, a new DWARF register 2642e8d8bef9SDimitry Andric // constant for the PC needs to be defined before DWARF can handle a signal 2643e8d8bef9SDimitry Andric // trampoline. This code may segfault if the target PC is unreadable, e.g.: 2644e8d8bef9SDimitry Andric // - The PC points at a function compiled without unwind info, and which is 2645e8d8bef9SDimitry Andric // part of an execute-only mapping (e.g. using -Wl,--execute-only). 2646e8d8bef9SDimitry Andric // - The PC is invalid and happens to point to unreadable or unmapped memory. 2647e8d8bef9SDimitry Andric // 2648e8d8bef9SDimitry Andric // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/vdso/sigreturn.S 2649e8d8bef9SDimitry Andric const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP)); 265081ad6265SDimitry Andric // The PC might contain an invalid address if the unwind info is bad, so 265181ad6265SDimitry Andric // directly accessing it could cause a segfault. Use process_vm_readv to read 265281ad6265SDimitry Andric // the memory safely instead. process_vm_readv was added in Linux 3.2, and 265381ad6265SDimitry Andric // AArch64 supported was added in Linux 3.7, so the syscall is guaranteed to 265481ad6265SDimitry Andric // be present. Unfortunately, there are Linux AArch64 environments where the 265581ad6265SDimitry Andric // libc wrapper for the syscall might not be present (e.g. Android 5), so call 265681ad6265SDimitry Andric // the syscall directly instead. 265781ad6265SDimitry Andric uint32_t instructions[2]; 265881ad6265SDimitry Andric struct iovec local_iov = {&instructions, sizeof instructions}; 265981ad6265SDimitry Andric struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof instructions}; 266081ad6265SDimitry Andric long bytesRead = 266181ad6265SDimitry Andric syscall(SYS_process_vm_readv, getpid(), &local_iov, 1, &remote_iov, 1, 0); 2662e8d8bef9SDimitry Andric // Look for instructions: mov x8, #0x8b; svc #0x0 266381ad6265SDimitry Andric if (bytesRead != sizeof instructions || instructions[0] != 0xd2801168 || 266481ad6265SDimitry Andric instructions[1] != 0xd4000001) 266581ad6265SDimitry Andric return false; 266681ad6265SDimitry Andric 2667e8d8bef9SDimitry Andric _info = {}; 266881ad6265SDimitry Andric _info.start_ip = pc; 266981ad6265SDimitry Andric _info.end_ip = pc + 4; 2670e8d8bef9SDimitry Andric _isSigReturn = true; 2671e8d8bef9SDimitry Andric return true; 2672e8d8bef9SDimitry Andric } 2673e8d8bef9SDimitry Andric 2674e8d8bef9SDimitry Andric template <typename A, typename R> 2675e8d8bef9SDimitry Andric int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) { 2676e8d8bef9SDimitry Andric // In the signal trampoline frame, sp points to an rt_sigframe[1], which is: 2677e8d8bef9SDimitry Andric // - 128-byte siginfo struct 2678e8d8bef9SDimitry Andric // - ucontext struct: 2679e8d8bef9SDimitry Andric // - 8-byte long (uc_flags) 2680e8d8bef9SDimitry Andric // - 8-byte pointer (uc_link) 2681e8d8bef9SDimitry Andric // - 24-byte stack_t 2682e8d8bef9SDimitry Andric // - 128-byte signal set 2683e8d8bef9SDimitry Andric // - 8 bytes of padding because sigcontext has 16-byte alignment 2684e8d8bef9SDimitry Andric // - sigcontext/mcontext_t 2685e8d8bef9SDimitry Andric // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c 2686e8d8bef9SDimitry Andric const pint_t kOffsetSpToSigcontext = (128 + 8 + 8 + 24 + 128 + 8); // 304 2687e8d8bef9SDimitry Andric 2688e8d8bef9SDimitry Andric // Offsets from sigcontext to each register. 2689e8d8bef9SDimitry Andric const pint_t kOffsetGprs = 8; // offset to "__u64 regs[31]" field 2690e8d8bef9SDimitry Andric const pint_t kOffsetSp = 256; // offset to "__u64 sp" field 2691e8d8bef9SDimitry Andric const pint_t kOffsetPc = 264; // offset to "__u64 pc" field 2692e8d8bef9SDimitry Andric 2693e8d8bef9SDimitry Andric pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext; 2694e8d8bef9SDimitry Andric 2695e8d8bef9SDimitry Andric for (int i = 0; i <= 30; ++i) { 2696e8d8bef9SDimitry Andric uint64_t value = _addressSpace.get64(sigctx + kOffsetGprs + 2697e8d8bef9SDimitry Andric static_cast<pint_t>(i * 8)); 2698349cc55cSDimitry Andric _registers.setRegister(UNW_AARCH64_X0 + i, value); 2699e8d8bef9SDimitry Andric } 2700e8d8bef9SDimitry Andric _registers.setSP(_addressSpace.get64(sigctx + kOffsetSp)); 2701e8d8bef9SDimitry Andric _registers.setIP(_addressSpace.get64(sigctx + kOffsetPc)); 2702e8d8bef9SDimitry Andric _isSignalFrame = true; 2703e8d8bef9SDimitry Andric return UNW_STEP_SUCCESS; 2704e8d8bef9SDimitry Andric } 270581ad6265SDimitry Andric #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && 270681ad6265SDimitry Andric // defined(_LIBUNWIND_TARGET_AARCH64) 270781ad6265SDimitry Andric 270881ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && \ 270981ad6265SDimitry Andric defined(_LIBUNWIND_TARGET_S390X) 271081ad6265SDimitry Andric template <typename A, typename R> 271181ad6265SDimitry Andric bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_s390x &) { 271281ad6265SDimitry Andric // Look for the sigreturn trampoline. The trampoline's body is a 271381ad6265SDimitry Andric // specific instruction (see below). Typically the trampoline comes from the 271481ad6265SDimitry Andric // vDSO (i.e. the __kernel_[rt_]sigreturn function). A libc might provide its 271581ad6265SDimitry Andric // own restorer function, though, or user-mode QEMU might write a trampoline 271681ad6265SDimitry Andric // onto the stack. 271781ad6265SDimitry Andric const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP)); 2718fcaf7f86SDimitry Andric // The PC might contain an invalid address if the unwind info is bad, so 2719fcaf7f86SDimitry Andric // directly accessing it could cause a segfault. Use process_vm_readv to 2720fcaf7f86SDimitry Andric // read the memory safely instead. 2721fcaf7f86SDimitry Andric uint16_t inst; 2722fcaf7f86SDimitry Andric struct iovec local_iov = {&inst, sizeof inst}; 2723fcaf7f86SDimitry Andric struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof inst}; 2724fcaf7f86SDimitry Andric long bytesRead = process_vm_readv(getpid(), &local_iov, 1, &remote_iov, 1, 0); 2725fcaf7f86SDimitry Andric if (bytesRead == sizeof inst && (inst == 0x0a77 || inst == 0x0aad)) { 272681ad6265SDimitry Andric _info = {}; 272781ad6265SDimitry Andric _info.start_ip = pc; 272881ad6265SDimitry Andric _info.end_ip = pc + 2; 272981ad6265SDimitry Andric _isSigReturn = true; 273081ad6265SDimitry Andric return true; 273181ad6265SDimitry Andric } 273281ad6265SDimitry Andric return false; 273381ad6265SDimitry Andric } 273481ad6265SDimitry Andric 273581ad6265SDimitry Andric template <typename A, typename R> 273681ad6265SDimitry Andric int UnwindCursor<A, R>::stepThroughSigReturn(Registers_s390x &) { 273781ad6265SDimitry Andric // Determine current SP. 273881ad6265SDimitry Andric const pint_t sp = static_cast<pint_t>(this->getReg(UNW_REG_SP)); 273981ad6265SDimitry Andric // According to the s390x ABI, the CFA is at (incoming) SP + 160. 274081ad6265SDimitry Andric const pint_t cfa = sp + 160; 274181ad6265SDimitry Andric 274281ad6265SDimitry Andric // Determine current PC and instruction there (this must be either 274381ad6265SDimitry Andric // a "svc __NR_sigreturn" or "svc __NR_rt_sigreturn"). 274481ad6265SDimitry Andric const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP)); 274581ad6265SDimitry Andric const uint16_t inst = _addressSpace.get16(pc); 274681ad6265SDimitry Andric 274781ad6265SDimitry Andric // Find the addresses of the signo and sigcontext in the frame. 274881ad6265SDimitry Andric pint_t pSigctx = 0; 274981ad6265SDimitry Andric pint_t pSigno = 0; 275081ad6265SDimitry Andric 275181ad6265SDimitry Andric // "svc __NR_sigreturn" uses a non-RT signal trampoline frame. 275281ad6265SDimitry Andric if (inst == 0x0a77) { 275381ad6265SDimitry Andric // Layout of a non-RT signal trampoline frame, starting at the CFA: 275481ad6265SDimitry Andric // - 8-byte signal mask 275581ad6265SDimitry Andric // - 8-byte pointer to sigcontext, followed by signo 275681ad6265SDimitry Andric // - 4-byte signo 275781ad6265SDimitry Andric pSigctx = _addressSpace.get64(cfa + 8); 275881ad6265SDimitry Andric pSigno = pSigctx + 344; 275981ad6265SDimitry Andric } 276081ad6265SDimitry Andric 276181ad6265SDimitry Andric // "svc __NR_rt_sigreturn" uses a RT signal trampoline frame. 276281ad6265SDimitry Andric if (inst == 0x0aad) { 276381ad6265SDimitry Andric // Layout of a RT signal trampoline frame, starting at the CFA: 276481ad6265SDimitry Andric // - 8-byte retcode (+ alignment) 276581ad6265SDimitry Andric // - 128-byte siginfo struct (starts with signo) 276681ad6265SDimitry Andric // - ucontext struct: 276781ad6265SDimitry Andric // - 8-byte long (uc_flags) 276881ad6265SDimitry Andric // - 8-byte pointer (uc_link) 276981ad6265SDimitry Andric // - 24-byte stack_t 277081ad6265SDimitry Andric // - 8 bytes of padding because sigcontext has 16-byte alignment 277181ad6265SDimitry Andric // - sigcontext/mcontext_t 277281ad6265SDimitry Andric pSigctx = cfa + 8 + 128 + 8 + 8 + 24 + 8; 277381ad6265SDimitry Andric pSigno = cfa + 8; 277481ad6265SDimitry Andric } 277581ad6265SDimitry Andric 277681ad6265SDimitry Andric assert(pSigctx != 0); 277781ad6265SDimitry Andric assert(pSigno != 0); 277881ad6265SDimitry Andric 277981ad6265SDimitry Andric // Offsets from sigcontext to each register. 278081ad6265SDimitry Andric const pint_t kOffsetPc = 8; 278181ad6265SDimitry Andric const pint_t kOffsetGprs = 16; 278281ad6265SDimitry Andric const pint_t kOffsetFprs = 216; 278381ad6265SDimitry Andric 278481ad6265SDimitry Andric // Restore all registers. 278581ad6265SDimitry Andric for (int i = 0; i < 16; ++i) { 278681ad6265SDimitry Andric uint64_t value = _addressSpace.get64(pSigctx + kOffsetGprs + 278781ad6265SDimitry Andric static_cast<pint_t>(i * 8)); 278881ad6265SDimitry Andric _registers.setRegister(UNW_S390X_R0 + i, value); 278981ad6265SDimitry Andric } 279081ad6265SDimitry Andric for (int i = 0; i < 16; ++i) { 279181ad6265SDimitry Andric static const int fpr[16] = { 279281ad6265SDimitry Andric UNW_S390X_F0, UNW_S390X_F1, UNW_S390X_F2, UNW_S390X_F3, 279381ad6265SDimitry Andric UNW_S390X_F4, UNW_S390X_F5, UNW_S390X_F6, UNW_S390X_F7, 279481ad6265SDimitry Andric UNW_S390X_F8, UNW_S390X_F9, UNW_S390X_F10, UNW_S390X_F11, 279581ad6265SDimitry Andric UNW_S390X_F12, UNW_S390X_F13, UNW_S390X_F14, UNW_S390X_F15 279681ad6265SDimitry Andric }; 279781ad6265SDimitry Andric double value = _addressSpace.getDouble(pSigctx + kOffsetFprs + 279881ad6265SDimitry Andric static_cast<pint_t>(i * 8)); 279981ad6265SDimitry Andric _registers.setFloatRegister(fpr[i], value); 280081ad6265SDimitry Andric } 280181ad6265SDimitry Andric _registers.setIP(_addressSpace.get64(pSigctx + kOffsetPc)); 280281ad6265SDimitry Andric 280381ad6265SDimitry Andric // SIGILL, SIGFPE and SIGTRAP are delivered with psw_addr 280481ad6265SDimitry Andric // after the faulting instruction rather than before it. 280581ad6265SDimitry Andric // Do not set _isSignalFrame in that case. 280681ad6265SDimitry Andric uint32_t signo = _addressSpace.get32(pSigno); 280781ad6265SDimitry Andric _isSignalFrame = (signo != 4 && signo != 5 && signo != 8); 280881ad6265SDimitry Andric 280981ad6265SDimitry Andric return UNW_STEP_SUCCESS; 281081ad6265SDimitry Andric } 281181ad6265SDimitry Andric #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && 281281ad6265SDimitry Andric // defined(_LIBUNWIND_TARGET_S390X) 2813e8d8bef9SDimitry Andric 2814*bdd1243dSDimitry Andric template <typename A, typename R> int UnwindCursor<A, R>::step(bool stage2) { 2815*bdd1243dSDimitry Andric (void)stage2; 28160b57cec5SDimitry Andric // Bottom of stack is defined is when unwind info cannot be found. 28170b57cec5SDimitry Andric if (_unwindInfoMissing) 28180b57cec5SDimitry Andric return UNW_STEP_END; 28190b57cec5SDimitry Andric 28200b57cec5SDimitry Andric // Use unwinding info to modify register set as if function returned. 28210b57cec5SDimitry Andric int result; 282281ad6265SDimitry Andric #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) 2823e8d8bef9SDimitry Andric if (_isSigReturn) { 2824e8d8bef9SDimitry Andric result = this->stepThroughSigReturn(); 2825e8d8bef9SDimitry Andric } else 2826e8d8bef9SDimitry Andric #endif 2827e8d8bef9SDimitry Andric { 28280b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 2829*bdd1243dSDimitry Andric result = this->stepWithCompactEncoding(stage2); 28300b57cec5SDimitry Andric #elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 28310b57cec5SDimitry Andric result = this->stepWithSEHData(); 283281ad6265SDimitry Andric #elif defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) 283381ad6265SDimitry Andric result = this->stepWithTBTableData(); 28340b57cec5SDimitry Andric #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 2835*bdd1243dSDimitry Andric result = this->stepWithDwarfFDE(stage2); 28360b57cec5SDimitry Andric #elif defined(_LIBUNWIND_ARM_EHABI) 28370b57cec5SDimitry Andric result = this->stepWithEHABI(); 28380b57cec5SDimitry Andric #else 28390b57cec5SDimitry Andric #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \ 28400b57cec5SDimitry Andric _LIBUNWIND_SUPPORT_SEH_UNWIND or \ 28410b57cec5SDimitry Andric _LIBUNWIND_SUPPORT_DWARF_UNWIND or \ 28420b57cec5SDimitry Andric _LIBUNWIND_ARM_EHABI 28430b57cec5SDimitry Andric #endif 2844e8d8bef9SDimitry Andric } 28450b57cec5SDimitry Andric 28460b57cec5SDimitry Andric // update info based on new PC 28470b57cec5SDimitry Andric if (result == UNW_STEP_SUCCESS) { 28480b57cec5SDimitry Andric this->setInfoBasedOnIPRegister(true); 28490b57cec5SDimitry Andric if (_unwindInfoMissing) 28500b57cec5SDimitry Andric return UNW_STEP_END; 28510b57cec5SDimitry Andric } 28520b57cec5SDimitry Andric 28530b57cec5SDimitry Andric return result; 28540b57cec5SDimitry Andric } 28550b57cec5SDimitry Andric 28560b57cec5SDimitry Andric template <typename A, typename R> 28570b57cec5SDimitry Andric void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) { 2858c2c6a179SDimitry Andric if (_unwindInfoMissing) 2859c2c6a179SDimitry Andric memset(info, 0, sizeof(*info)); 2860c2c6a179SDimitry Andric else 28610b57cec5SDimitry Andric *info = _info; 28620b57cec5SDimitry Andric } 28630b57cec5SDimitry Andric 28640b57cec5SDimitry Andric template <typename A, typename R> 28650b57cec5SDimitry Andric bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen, 28660b57cec5SDimitry Andric unw_word_t *offset) { 28670b57cec5SDimitry Andric return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP), 28680b57cec5SDimitry Andric buf, bufLen, offset); 28690b57cec5SDimitry Andric } 28700b57cec5SDimitry Andric 2871349cc55cSDimitry Andric #if defined(_LIBUNWIND_USE_CET) 2872349cc55cSDimitry Andric extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) { 2873349cc55cSDimitry Andric AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 2874349cc55cSDimitry Andric return co->get_registers(); 2875349cc55cSDimitry Andric } 2876349cc55cSDimitry Andric #endif 28770b57cec5SDimitry Andric } // namespace libunwind 28780b57cec5SDimitry Andric 28790b57cec5SDimitry Andric #endif // __UNWINDCURSOR_HPP__ 2880