10b57cec5SDimitry Andric //===------------------------- UnwindCursor.hpp ---------------------------===// 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 140b57cec5SDimitry Andric #include <stdint.h> 150b57cec5SDimitry Andric #include <stdio.h> 160b57cec5SDimitry Andric #include <stdlib.h> 170b57cec5SDimitry Andric #include <unwind.h> 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric #ifdef _WIN32 200b57cec5SDimitry Andric #include <windows.h> 210b57cec5SDimitry Andric #include <ntverp.h> 220b57cec5SDimitry Andric #endif 230b57cec5SDimitry Andric #ifdef __APPLE__ 240b57cec5SDimitry Andric #include <mach-o/dyld.h> 250b57cec5SDimitry Andric #endif 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 280b57cec5SDimitry Andric // Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and 290b57cec5SDimitry Andric // earlier) SDKs. 300b57cec5SDimitry Andric // MinGW-w64 has always provided this struct. 310b57cec5SDimitry Andric #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \ 320b57cec5SDimitry Andric !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000 330b57cec5SDimitry Andric struct _DISPATCHER_CONTEXT { 340b57cec5SDimitry Andric ULONG64 ControlPc; 350b57cec5SDimitry Andric ULONG64 ImageBase; 360b57cec5SDimitry Andric PRUNTIME_FUNCTION FunctionEntry; 370b57cec5SDimitry Andric ULONG64 EstablisherFrame; 380b57cec5SDimitry Andric ULONG64 TargetIp; 390b57cec5SDimitry Andric PCONTEXT ContextRecord; 400b57cec5SDimitry Andric PEXCEPTION_ROUTINE LanguageHandler; 410b57cec5SDimitry Andric PVOID HandlerData; 420b57cec5SDimitry Andric PUNWIND_HISTORY_TABLE HistoryTable; 430b57cec5SDimitry Andric ULONG ScopeIndex; 440b57cec5SDimitry Andric ULONG Fill0; 450b57cec5SDimitry Andric }; 460b57cec5SDimitry Andric #endif 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric struct UNWIND_INFO { 490b57cec5SDimitry Andric uint8_t Version : 3; 500b57cec5SDimitry Andric uint8_t Flags : 5; 510b57cec5SDimitry Andric uint8_t SizeOfProlog; 520b57cec5SDimitry Andric uint8_t CountOfCodes; 530b57cec5SDimitry Andric uint8_t FrameRegister : 4; 540b57cec5SDimitry Andric uint8_t FrameOffset : 4; 550b57cec5SDimitry Andric uint16_t UnwindCodes[2]; 560b57cec5SDimitry Andric }; 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric extern "C" _Unwind_Reason_Code __libunwind_seh_personality( 590b57cec5SDimitry Andric int, _Unwind_Action, uint64_t, _Unwind_Exception *, 600b57cec5SDimitry Andric struct _Unwind_Context *); 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric #endif 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric #include "config.h" 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric #include "AddressSpace.hpp" 670b57cec5SDimitry Andric #include "CompactUnwinder.hpp" 680b57cec5SDimitry Andric #include "config.h" 690b57cec5SDimitry Andric #include "DwarfInstructions.hpp" 700b57cec5SDimitry Andric #include "EHHeaderParser.hpp" 710b57cec5SDimitry Andric #include "libunwind.h" 720b57cec5SDimitry Andric #include "Registers.hpp" 730b57cec5SDimitry Andric #include "RWMutex.hpp" 740b57cec5SDimitry Andric #include "Unwind-EHABI.h" 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric namespace libunwind { 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 790b57cec5SDimitry Andric /// Cache of recently found FDEs. 800b57cec5SDimitry Andric template <typename A> 810b57cec5SDimitry Andric class _LIBUNWIND_HIDDEN DwarfFDECache { 820b57cec5SDimitry Andric typedef typename A::pint_t pint_t; 830b57cec5SDimitry Andric public: 84e8d8bef9SDimitry Andric static constexpr pint_t kSearchAll = static_cast<pint_t>(-1); 850b57cec5SDimitry Andric static pint_t findFDE(pint_t mh, pint_t pc); 860b57cec5SDimitry Andric static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde); 870b57cec5SDimitry Andric static void removeAllIn(pint_t mh); 880b57cec5SDimitry Andric static void iterateCacheEntries(void (*func)(unw_word_t ip_start, 890b57cec5SDimitry Andric unw_word_t ip_end, 900b57cec5SDimitry Andric unw_word_t fde, unw_word_t mh)); 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric private: 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric struct entry { 950b57cec5SDimitry Andric pint_t mh; 960b57cec5SDimitry Andric pint_t ip_start; 970b57cec5SDimitry Andric pint_t ip_end; 980b57cec5SDimitry Andric pint_t fde; 990b57cec5SDimitry Andric }; 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric // These fields are all static to avoid needing an initializer. 1020b57cec5SDimitry Andric // There is only one instance of this class per process. 1030b57cec5SDimitry Andric static RWMutex _lock; 1040b57cec5SDimitry Andric #ifdef __APPLE__ 1050b57cec5SDimitry Andric static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide); 1060b57cec5SDimitry Andric static bool _registeredForDyldUnloads; 1070b57cec5SDimitry Andric #endif 1080b57cec5SDimitry Andric static entry *_buffer; 1090b57cec5SDimitry Andric static entry *_bufferUsed; 1100b57cec5SDimitry Andric static entry *_bufferEnd; 1110b57cec5SDimitry Andric static entry _initialBuffer[64]; 1120b57cec5SDimitry Andric }; 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric template <typename A> 1150b57cec5SDimitry Andric typename DwarfFDECache<A>::entry * 1160b57cec5SDimitry Andric DwarfFDECache<A>::_buffer = _initialBuffer; 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric template <typename A> 1190b57cec5SDimitry Andric typename DwarfFDECache<A>::entry * 1200b57cec5SDimitry Andric DwarfFDECache<A>::_bufferUsed = _initialBuffer; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric template <typename A> 1230b57cec5SDimitry Andric typename DwarfFDECache<A>::entry * 1240b57cec5SDimitry Andric DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64]; 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric template <typename A> 1270b57cec5SDimitry Andric typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64]; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric template <typename A> 1300b57cec5SDimitry Andric RWMutex DwarfFDECache<A>::_lock; 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric #ifdef __APPLE__ 1330b57cec5SDimitry Andric template <typename A> 1340b57cec5SDimitry Andric bool DwarfFDECache<A>::_registeredForDyldUnloads = false; 1350b57cec5SDimitry Andric #endif 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric template <typename A> 1380b57cec5SDimitry Andric typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) { 1390b57cec5SDimitry Andric pint_t result = 0; 1400b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared()); 1410b57cec5SDimitry Andric for (entry *p = _buffer; p < _bufferUsed; ++p) { 142e8d8bef9SDimitry Andric if ((mh == p->mh) || (mh == kSearchAll)) { 1430b57cec5SDimitry Andric if ((p->ip_start <= pc) && (pc < p->ip_end)) { 1440b57cec5SDimitry Andric result = p->fde; 1450b57cec5SDimitry Andric break; 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric } 1490b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared()); 1500b57cec5SDimitry Andric return result; 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric template <typename A> 1540b57cec5SDimitry Andric void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end, 1550b57cec5SDimitry Andric pint_t fde) { 1560b57cec5SDimitry Andric #if !defined(_LIBUNWIND_NO_HEAP) 1570b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); 1580b57cec5SDimitry Andric if (_bufferUsed >= _bufferEnd) { 1590b57cec5SDimitry Andric size_t oldSize = (size_t)(_bufferEnd - _buffer); 1600b57cec5SDimitry Andric size_t newSize = oldSize * 4; 1610b57cec5SDimitry Andric // Can't use operator new (we are below it). 1620b57cec5SDimitry Andric entry *newBuffer = (entry *)malloc(newSize * sizeof(entry)); 1630b57cec5SDimitry Andric memcpy(newBuffer, _buffer, oldSize * sizeof(entry)); 1640b57cec5SDimitry Andric if (_buffer != _initialBuffer) 1650b57cec5SDimitry Andric free(_buffer); 1660b57cec5SDimitry Andric _buffer = newBuffer; 1670b57cec5SDimitry Andric _bufferUsed = &newBuffer[oldSize]; 1680b57cec5SDimitry Andric _bufferEnd = &newBuffer[newSize]; 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric _bufferUsed->mh = mh; 1710b57cec5SDimitry Andric _bufferUsed->ip_start = ip_start; 1720b57cec5SDimitry Andric _bufferUsed->ip_end = ip_end; 1730b57cec5SDimitry Andric _bufferUsed->fde = fde; 1740b57cec5SDimitry Andric ++_bufferUsed; 1750b57cec5SDimitry Andric #ifdef __APPLE__ 1760b57cec5SDimitry Andric if (!_registeredForDyldUnloads) { 1770b57cec5SDimitry Andric _dyld_register_func_for_remove_image(&dyldUnloadHook); 1780b57cec5SDimitry Andric _registeredForDyldUnloads = true; 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric #endif 1810b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); 1820b57cec5SDimitry Andric #endif 1830b57cec5SDimitry Andric } 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric template <typename A> 1860b57cec5SDimitry Andric void DwarfFDECache<A>::removeAllIn(pint_t mh) { 1870b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); 1880b57cec5SDimitry Andric entry *d = _buffer; 1890b57cec5SDimitry Andric for (const entry *s = _buffer; s < _bufferUsed; ++s) { 1900b57cec5SDimitry Andric if (s->mh != mh) { 1910b57cec5SDimitry Andric if (d != s) 1920b57cec5SDimitry Andric *d = *s; 1930b57cec5SDimitry Andric ++d; 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric _bufferUsed = d; 1970b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric #ifdef __APPLE__ 2010b57cec5SDimitry Andric template <typename A> 2020b57cec5SDimitry Andric void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) { 2030b57cec5SDimitry Andric removeAllIn((pint_t) mh); 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric #endif 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric template <typename A> 2080b57cec5SDimitry Andric void DwarfFDECache<A>::iterateCacheEntries(void (*func)( 2090b57cec5SDimitry Andric unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) { 2100b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); 2110b57cec5SDimitry Andric for (entry *p = _buffer; p < _bufferUsed; ++p) { 2120b57cec5SDimitry Andric (*func)(p->ip_start, p->ip_end, p->fde, p->mh); 2130b57cec5SDimitry Andric } 2140b57cec5SDimitry Andric _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric #define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field)) 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 2220b57cec5SDimitry Andric template <typename A> class UnwindSectionHeader { 2230b57cec5SDimitry Andric public: 2240b57cec5SDimitry Andric UnwindSectionHeader(A &addressSpace, typename A::pint_t addr) 2250b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric uint32_t version() const { 2280b57cec5SDimitry Andric return _addressSpace.get32(_addr + 2290b57cec5SDimitry Andric offsetof(unwind_info_section_header, version)); 2300b57cec5SDimitry Andric } 2310b57cec5SDimitry Andric uint32_t commonEncodingsArraySectionOffset() const { 2320b57cec5SDimitry Andric return _addressSpace.get32(_addr + 2330b57cec5SDimitry Andric offsetof(unwind_info_section_header, 2340b57cec5SDimitry Andric commonEncodingsArraySectionOffset)); 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric uint32_t commonEncodingsArrayCount() const { 2370b57cec5SDimitry Andric return _addressSpace.get32(_addr + offsetof(unwind_info_section_header, 2380b57cec5SDimitry Andric commonEncodingsArrayCount)); 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric uint32_t personalityArraySectionOffset() const { 2410b57cec5SDimitry Andric return _addressSpace.get32(_addr + offsetof(unwind_info_section_header, 2420b57cec5SDimitry Andric personalityArraySectionOffset)); 2430b57cec5SDimitry Andric } 2440b57cec5SDimitry Andric uint32_t personalityArrayCount() const { 2450b57cec5SDimitry Andric return _addressSpace.get32( 2460b57cec5SDimitry Andric _addr + offsetof(unwind_info_section_header, personalityArrayCount)); 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric uint32_t indexSectionOffset() const { 2490b57cec5SDimitry Andric return _addressSpace.get32( 2500b57cec5SDimitry Andric _addr + offsetof(unwind_info_section_header, indexSectionOffset)); 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric uint32_t indexCount() const { 2530b57cec5SDimitry Andric return _addressSpace.get32( 2540b57cec5SDimitry Andric _addr + offsetof(unwind_info_section_header, indexCount)); 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric private: 2580b57cec5SDimitry Andric A &_addressSpace; 2590b57cec5SDimitry Andric typename A::pint_t _addr; 2600b57cec5SDimitry Andric }; 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric template <typename A> class UnwindSectionIndexArray { 2630b57cec5SDimitry Andric public: 2640b57cec5SDimitry Andric UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr) 2650b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric uint32_t functionOffset(uint32_t index) const { 2680b57cec5SDimitry Andric return _addressSpace.get32( 2690b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, 2700b57cec5SDimitry Andric functionOffset)); 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric uint32_t secondLevelPagesSectionOffset(uint32_t index) const { 2730b57cec5SDimitry Andric return _addressSpace.get32( 2740b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, 2750b57cec5SDimitry Andric secondLevelPagesSectionOffset)); 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric uint32_t lsdaIndexArraySectionOffset(uint32_t index) const { 2780b57cec5SDimitry Andric return _addressSpace.get32( 2790b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, 2800b57cec5SDimitry Andric lsdaIndexArraySectionOffset)); 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric private: 2840b57cec5SDimitry Andric A &_addressSpace; 2850b57cec5SDimitry Andric typename A::pint_t _addr; 2860b57cec5SDimitry Andric }; 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric template <typename A> class UnwindSectionRegularPageHeader { 2890b57cec5SDimitry Andric public: 2900b57cec5SDimitry Andric UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr) 2910b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric uint32_t kind() const { 2940b57cec5SDimitry Andric return _addressSpace.get32( 2950b57cec5SDimitry Andric _addr + offsetof(unwind_info_regular_second_level_page_header, kind)); 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric uint16_t entryPageOffset() const { 2980b57cec5SDimitry Andric return _addressSpace.get16( 2990b57cec5SDimitry Andric _addr + offsetof(unwind_info_regular_second_level_page_header, 3000b57cec5SDimitry Andric entryPageOffset)); 3010b57cec5SDimitry Andric } 3020b57cec5SDimitry Andric uint16_t entryCount() const { 3030b57cec5SDimitry Andric return _addressSpace.get16( 3040b57cec5SDimitry Andric _addr + 3050b57cec5SDimitry Andric offsetof(unwind_info_regular_second_level_page_header, entryCount)); 3060b57cec5SDimitry Andric } 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric private: 3090b57cec5SDimitry Andric A &_addressSpace; 3100b57cec5SDimitry Andric typename A::pint_t _addr; 3110b57cec5SDimitry Andric }; 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric template <typename A> class UnwindSectionRegularArray { 3140b57cec5SDimitry Andric public: 3150b57cec5SDimitry Andric UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr) 3160b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric uint32_t functionOffset(uint32_t index) const { 3190b57cec5SDimitry Andric return _addressSpace.get32( 3200b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index, 3210b57cec5SDimitry Andric functionOffset)); 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric uint32_t encoding(uint32_t index) const { 3240b57cec5SDimitry Andric return _addressSpace.get32( 3250b57cec5SDimitry Andric _addr + 3260b57cec5SDimitry Andric arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding)); 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric private: 3300b57cec5SDimitry Andric A &_addressSpace; 3310b57cec5SDimitry Andric typename A::pint_t _addr; 3320b57cec5SDimitry Andric }; 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric template <typename A> class UnwindSectionCompressedPageHeader { 3350b57cec5SDimitry Andric public: 3360b57cec5SDimitry Andric UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr) 3370b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric uint32_t kind() const { 3400b57cec5SDimitry Andric return _addressSpace.get32( 3410b57cec5SDimitry Andric _addr + 3420b57cec5SDimitry Andric offsetof(unwind_info_compressed_second_level_page_header, kind)); 3430b57cec5SDimitry Andric } 3440b57cec5SDimitry Andric uint16_t entryPageOffset() const { 3450b57cec5SDimitry Andric return _addressSpace.get16( 3460b57cec5SDimitry Andric _addr + offsetof(unwind_info_compressed_second_level_page_header, 3470b57cec5SDimitry Andric entryPageOffset)); 3480b57cec5SDimitry Andric } 3490b57cec5SDimitry Andric uint16_t entryCount() const { 3500b57cec5SDimitry Andric return _addressSpace.get16( 3510b57cec5SDimitry Andric _addr + 3520b57cec5SDimitry Andric offsetof(unwind_info_compressed_second_level_page_header, entryCount)); 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric uint16_t encodingsPageOffset() const { 3550b57cec5SDimitry Andric return _addressSpace.get16( 3560b57cec5SDimitry Andric _addr + offsetof(unwind_info_compressed_second_level_page_header, 3570b57cec5SDimitry Andric encodingsPageOffset)); 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric uint16_t encodingsCount() const { 3600b57cec5SDimitry Andric return _addressSpace.get16( 3610b57cec5SDimitry Andric _addr + offsetof(unwind_info_compressed_second_level_page_header, 3620b57cec5SDimitry Andric encodingsCount)); 3630b57cec5SDimitry Andric } 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andric private: 3660b57cec5SDimitry Andric A &_addressSpace; 3670b57cec5SDimitry Andric typename A::pint_t _addr; 3680b57cec5SDimitry Andric }; 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric template <typename A> class UnwindSectionCompressedArray { 3710b57cec5SDimitry Andric public: 3720b57cec5SDimitry Andric UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr) 3730b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric uint32_t functionOffset(uint32_t index) const { 3760b57cec5SDimitry Andric return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET( 3770b57cec5SDimitry Andric _addressSpace.get32(_addr + index * sizeof(uint32_t))); 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric uint16_t encodingIndex(uint32_t index) const { 3800b57cec5SDimitry Andric return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX( 3810b57cec5SDimitry Andric _addressSpace.get32(_addr + index * sizeof(uint32_t))); 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric private: 3850b57cec5SDimitry Andric A &_addressSpace; 3860b57cec5SDimitry Andric typename A::pint_t _addr; 3870b57cec5SDimitry Andric }; 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric template <typename A> class UnwindSectionLsdaArray { 3900b57cec5SDimitry Andric public: 3910b57cec5SDimitry Andric UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr) 3920b57cec5SDimitry Andric : _addressSpace(addressSpace), _addr(addr) {} 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric uint32_t functionOffset(uint32_t index) const { 3950b57cec5SDimitry Andric return _addressSpace.get32( 3960b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry, 3970b57cec5SDimitry Andric index, functionOffset)); 3980b57cec5SDimitry Andric } 3990b57cec5SDimitry Andric uint32_t lsdaOffset(uint32_t index) const { 4000b57cec5SDimitry Andric return _addressSpace.get32( 4010b57cec5SDimitry Andric _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry, 4020b57cec5SDimitry Andric index, lsdaOffset)); 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric private: 4060b57cec5SDimitry Andric A &_addressSpace; 4070b57cec5SDimitry Andric typename A::pint_t _addr; 4080b57cec5SDimitry Andric }; 4090b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric class _LIBUNWIND_HIDDEN AbstractUnwindCursor { 4120b57cec5SDimitry Andric public: 4130b57cec5SDimitry Andric // NOTE: provide a class specific placement deallocation function (S5.3.4 p20) 4140b57cec5SDimitry Andric // This avoids an unnecessary dependency to libc++abi. 4150b57cec5SDimitry Andric void operator delete(void *, size_t) {} 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric virtual ~AbstractUnwindCursor() {} 4180b57cec5SDimitry Andric virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); } 4190b57cec5SDimitry Andric virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); } 4200b57cec5SDimitry Andric virtual void setReg(int, unw_word_t) { 4210b57cec5SDimitry Andric _LIBUNWIND_ABORT("setReg not implemented"); 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric virtual bool validFloatReg(int) { 4240b57cec5SDimitry Andric _LIBUNWIND_ABORT("validFloatReg not implemented"); 4250b57cec5SDimitry Andric } 4260b57cec5SDimitry Andric virtual unw_fpreg_t getFloatReg(int) { 4270b57cec5SDimitry Andric _LIBUNWIND_ABORT("getFloatReg not implemented"); 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric virtual void setFloatReg(int, unw_fpreg_t) { 4300b57cec5SDimitry Andric _LIBUNWIND_ABORT("setFloatReg not implemented"); 4310b57cec5SDimitry Andric } 4320b57cec5SDimitry Andric virtual int step() { _LIBUNWIND_ABORT("step not implemented"); } 4330b57cec5SDimitry Andric virtual void getInfo(unw_proc_info_t *) { 4340b57cec5SDimitry Andric _LIBUNWIND_ABORT("getInfo not implemented"); 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); } 4370b57cec5SDimitry Andric virtual bool isSignalFrame() { 4380b57cec5SDimitry Andric _LIBUNWIND_ABORT("isSignalFrame not implemented"); 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric virtual bool getFunctionName(char *, size_t, unw_word_t *) { 4410b57cec5SDimitry Andric _LIBUNWIND_ABORT("getFunctionName not implemented"); 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric virtual void setInfoBasedOnIPRegister(bool = false) { 4440b57cec5SDimitry Andric _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented"); 4450b57cec5SDimitry Andric } 4460b57cec5SDimitry Andric virtual const char *getRegisterName(int) { 4470b57cec5SDimitry Andric _LIBUNWIND_ABORT("getRegisterName not implemented"); 4480b57cec5SDimitry Andric } 4490b57cec5SDimitry Andric #ifdef __arm__ 4500b57cec5SDimitry Andric virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); } 4510b57cec5SDimitry Andric #endif 4520b57cec5SDimitry Andric }; 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32) 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric /// \c UnwindCursor contains all state (including all register values) during 4570b57cec5SDimitry Andric /// an unwind. This is normally stack-allocated inside a unw_cursor_t. 4580b57cec5SDimitry Andric template <typename A, typename R> 4590b57cec5SDimitry Andric class UnwindCursor : public AbstractUnwindCursor { 4600b57cec5SDimitry Andric typedef typename A::pint_t pint_t; 4610b57cec5SDimitry Andric public: 4620b57cec5SDimitry Andric UnwindCursor(unw_context_t *context, A &as); 4630b57cec5SDimitry Andric UnwindCursor(CONTEXT *context, A &as); 4640b57cec5SDimitry Andric UnwindCursor(A &as, void *threadArg); 4650b57cec5SDimitry Andric virtual ~UnwindCursor() {} 4660b57cec5SDimitry Andric virtual bool validReg(int); 4670b57cec5SDimitry Andric virtual unw_word_t getReg(int); 4680b57cec5SDimitry Andric virtual void setReg(int, unw_word_t); 4690b57cec5SDimitry Andric virtual bool validFloatReg(int); 4700b57cec5SDimitry Andric virtual unw_fpreg_t getFloatReg(int); 4710b57cec5SDimitry Andric virtual void setFloatReg(int, unw_fpreg_t); 4720b57cec5SDimitry Andric virtual int step(); 4730b57cec5SDimitry Andric virtual void getInfo(unw_proc_info_t *); 4740b57cec5SDimitry Andric virtual void jumpto(); 4750b57cec5SDimitry Andric virtual bool isSignalFrame(); 4760b57cec5SDimitry Andric virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off); 4770b57cec5SDimitry Andric virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false); 4780b57cec5SDimitry Andric virtual const char *getRegisterName(int num); 4790b57cec5SDimitry Andric #ifdef __arm__ 4800b57cec5SDimitry Andric virtual void saveVFPAsX(); 4810b57cec5SDimitry Andric #endif 4820b57cec5SDimitry Andric 4830b57cec5SDimitry Andric DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; } 4840b57cec5SDimitry Andric void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; } 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric // libunwind does not and should not depend on C++ library which means that we 4870b57cec5SDimitry Andric // need our own defition of inline placement new. 4880b57cec5SDimitry Andric static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; } 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric private: 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric pint_t getLastPC() const { return _dispContext.ControlPc; } 4930b57cec5SDimitry Andric void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; } 4940b57cec5SDimitry Andric RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) { 4950b57cec5SDimitry Andric _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc, 4960b57cec5SDimitry Andric &_dispContext.ImageBase, 4970b57cec5SDimitry Andric _dispContext.HistoryTable); 4980b57cec5SDimitry Andric *base = _dispContext.ImageBase; 4990b57cec5SDimitry Andric return _dispContext.FunctionEntry; 5000b57cec5SDimitry Andric } 5010b57cec5SDimitry Andric bool getInfoFromSEH(pint_t pc); 5020b57cec5SDimitry Andric int stepWithSEHData() { 5030b57cec5SDimitry Andric _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER, 5040b57cec5SDimitry Andric _dispContext.ImageBase, 5050b57cec5SDimitry Andric _dispContext.ControlPc, 5060b57cec5SDimitry Andric _dispContext.FunctionEntry, 5070b57cec5SDimitry Andric _dispContext.ContextRecord, 5080b57cec5SDimitry Andric &_dispContext.HandlerData, 5090b57cec5SDimitry Andric &_dispContext.EstablisherFrame, 5100b57cec5SDimitry Andric NULL); 5110b57cec5SDimitry Andric // Update some fields of the unwind info now, since we have them. 5120b57cec5SDimitry Andric _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData); 5130b57cec5SDimitry Andric if (_dispContext.LanguageHandler) { 5140b57cec5SDimitry Andric _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality); 5150b57cec5SDimitry Andric } else 5160b57cec5SDimitry Andric _info.handler = 0; 5170b57cec5SDimitry Andric return UNW_STEP_SUCCESS; 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric A &_addressSpace; 5210b57cec5SDimitry Andric unw_proc_info_t _info; 5220b57cec5SDimitry Andric DISPATCHER_CONTEXT _dispContext; 5230b57cec5SDimitry Andric CONTEXT _msContext; 5240b57cec5SDimitry Andric UNWIND_HISTORY_TABLE _histTable; 5250b57cec5SDimitry Andric bool _unwindInfoMissing; 5260b57cec5SDimitry Andric }; 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric template <typename A, typename R> 5300b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as) 5310b57cec5SDimitry Andric : _addressSpace(as), _unwindInfoMissing(false) { 5320b57cec5SDimitry Andric static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), 5330b57cec5SDimitry Andric "UnwindCursor<> does not fit in unw_cursor_t"); 534e8d8bef9SDimitry Andric static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)), 535e8d8bef9SDimitry Andric "UnwindCursor<> requires more alignment than unw_cursor_t"); 5360b57cec5SDimitry Andric memset(&_info, 0, sizeof(_info)); 5370b57cec5SDimitry Andric memset(&_histTable, 0, sizeof(_histTable)); 5380b57cec5SDimitry Andric _dispContext.ContextRecord = &_msContext; 5390b57cec5SDimitry Andric _dispContext.HistoryTable = &_histTable; 5400b57cec5SDimitry Andric // Initialize MS context from ours. 5410b57cec5SDimitry Andric R r(context); 5420b57cec5SDimitry Andric _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT; 5430b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 5440b57cec5SDimitry Andric _msContext.Rax = r.getRegister(UNW_X86_64_RAX); 5450b57cec5SDimitry Andric _msContext.Rcx = r.getRegister(UNW_X86_64_RCX); 5460b57cec5SDimitry Andric _msContext.Rdx = r.getRegister(UNW_X86_64_RDX); 5470b57cec5SDimitry Andric _msContext.Rbx = r.getRegister(UNW_X86_64_RBX); 5480b57cec5SDimitry Andric _msContext.Rsp = r.getRegister(UNW_X86_64_RSP); 5490b57cec5SDimitry Andric _msContext.Rbp = r.getRegister(UNW_X86_64_RBP); 5500b57cec5SDimitry Andric _msContext.Rsi = r.getRegister(UNW_X86_64_RSI); 5510b57cec5SDimitry Andric _msContext.Rdi = r.getRegister(UNW_X86_64_RDI); 5520b57cec5SDimitry Andric _msContext.R8 = r.getRegister(UNW_X86_64_R8); 5530b57cec5SDimitry Andric _msContext.R9 = r.getRegister(UNW_X86_64_R9); 5540b57cec5SDimitry Andric _msContext.R10 = r.getRegister(UNW_X86_64_R10); 5550b57cec5SDimitry Andric _msContext.R11 = r.getRegister(UNW_X86_64_R11); 5560b57cec5SDimitry Andric _msContext.R12 = r.getRegister(UNW_X86_64_R12); 5570b57cec5SDimitry Andric _msContext.R13 = r.getRegister(UNW_X86_64_R13); 5580b57cec5SDimitry Andric _msContext.R14 = r.getRegister(UNW_X86_64_R14); 5590b57cec5SDimitry Andric _msContext.R15 = r.getRegister(UNW_X86_64_R15); 5600b57cec5SDimitry Andric _msContext.Rip = r.getRegister(UNW_REG_IP); 5610b57cec5SDimitry Andric union { 5620b57cec5SDimitry Andric v128 v; 5630b57cec5SDimitry Andric M128A m; 5640b57cec5SDimitry Andric } t; 5650b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM0); 5660b57cec5SDimitry Andric _msContext.Xmm0 = t.m; 5670b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM1); 5680b57cec5SDimitry Andric _msContext.Xmm1 = t.m; 5690b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM2); 5700b57cec5SDimitry Andric _msContext.Xmm2 = t.m; 5710b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM3); 5720b57cec5SDimitry Andric _msContext.Xmm3 = t.m; 5730b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM4); 5740b57cec5SDimitry Andric _msContext.Xmm4 = t.m; 5750b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM5); 5760b57cec5SDimitry Andric _msContext.Xmm5 = t.m; 5770b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM6); 5780b57cec5SDimitry Andric _msContext.Xmm6 = t.m; 5790b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM7); 5800b57cec5SDimitry Andric _msContext.Xmm7 = t.m; 5810b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM8); 5820b57cec5SDimitry Andric _msContext.Xmm8 = t.m; 5830b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM9); 5840b57cec5SDimitry Andric _msContext.Xmm9 = t.m; 5850b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM10); 5860b57cec5SDimitry Andric _msContext.Xmm10 = t.m; 5870b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM11); 5880b57cec5SDimitry Andric _msContext.Xmm11 = t.m; 5890b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM12); 5900b57cec5SDimitry Andric _msContext.Xmm12 = t.m; 5910b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM13); 5920b57cec5SDimitry Andric _msContext.Xmm13 = t.m; 5930b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM14); 5940b57cec5SDimitry Andric _msContext.Xmm14 = t.m; 5950b57cec5SDimitry Andric t.v = r.getVectorRegister(UNW_X86_64_XMM15); 5960b57cec5SDimitry Andric _msContext.Xmm15 = t.m; 5970b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM) 5980b57cec5SDimitry Andric _msContext.R0 = r.getRegister(UNW_ARM_R0); 5990b57cec5SDimitry Andric _msContext.R1 = r.getRegister(UNW_ARM_R1); 6000b57cec5SDimitry Andric _msContext.R2 = r.getRegister(UNW_ARM_R2); 6010b57cec5SDimitry Andric _msContext.R3 = r.getRegister(UNW_ARM_R3); 6020b57cec5SDimitry Andric _msContext.R4 = r.getRegister(UNW_ARM_R4); 6030b57cec5SDimitry Andric _msContext.R5 = r.getRegister(UNW_ARM_R5); 6040b57cec5SDimitry Andric _msContext.R6 = r.getRegister(UNW_ARM_R6); 6050b57cec5SDimitry Andric _msContext.R7 = r.getRegister(UNW_ARM_R7); 6060b57cec5SDimitry Andric _msContext.R8 = r.getRegister(UNW_ARM_R8); 6070b57cec5SDimitry Andric _msContext.R9 = r.getRegister(UNW_ARM_R9); 6080b57cec5SDimitry Andric _msContext.R10 = r.getRegister(UNW_ARM_R10); 6090b57cec5SDimitry Andric _msContext.R11 = r.getRegister(UNW_ARM_R11); 6100b57cec5SDimitry Andric _msContext.R12 = r.getRegister(UNW_ARM_R12); 6110b57cec5SDimitry Andric _msContext.Sp = r.getRegister(UNW_ARM_SP); 6120b57cec5SDimitry Andric _msContext.Lr = r.getRegister(UNW_ARM_LR); 6130b57cec5SDimitry Andric _msContext.Pc = r.getRegister(UNW_ARM_IP); 6140b57cec5SDimitry Andric for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) { 6150b57cec5SDimitry Andric union { 6160b57cec5SDimitry Andric uint64_t w; 6170b57cec5SDimitry Andric double d; 6180b57cec5SDimitry Andric } d; 6190b57cec5SDimitry Andric d.d = r.getFloatRegister(i); 6200b57cec5SDimitry Andric _msContext.D[i - UNW_ARM_D0] = d.w; 6210b57cec5SDimitry Andric } 6220b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 6230b57cec5SDimitry Andric for (int i = UNW_ARM64_X0; i <= UNW_ARM64_X30; ++i) 6240b57cec5SDimitry Andric _msContext.X[i - UNW_ARM64_X0] = r.getRegister(i); 6250b57cec5SDimitry Andric _msContext.Sp = r.getRegister(UNW_REG_SP); 6260b57cec5SDimitry Andric _msContext.Pc = r.getRegister(UNW_REG_IP); 6270b57cec5SDimitry Andric for (int i = UNW_ARM64_D0; i <= UNW_ARM64_D31; ++i) 6280b57cec5SDimitry Andric _msContext.V[i - UNW_ARM64_D0].D[0] = r.getFloatRegister(i); 6290b57cec5SDimitry Andric #endif 6300b57cec5SDimitry Andric } 6310b57cec5SDimitry Andric 6320b57cec5SDimitry Andric template <typename A, typename R> 6330b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as) 6340b57cec5SDimitry Andric : _addressSpace(as), _unwindInfoMissing(false) { 6350b57cec5SDimitry Andric static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), 6360b57cec5SDimitry Andric "UnwindCursor<> does not fit in unw_cursor_t"); 6370b57cec5SDimitry Andric memset(&_info, 0, sizeof(_info)); 6380b57cec5SDimitry Andric memset(&_histTable, 0, sizeof(_histTable)); 6390b57cec5SDimitry Andric _dispContext.ContextRecord = &_msContext; 6400b57cec5SDimitry Andric _dispContext.HistoryTable = &_histTable; 6410b57cec5SDimitry Andric _msContext = *context; 6420b57cec5SDimitry Andric } 6430b57cec5SDimitry Andric 6440b57cec5SDimitry Andric 6450b57cec5SDimitry Andric template <typename A, typename R> 6460b57cec5SDimitry Andric bool UnwindCursor<A, R>::validReg(int regNum) { 6470b57cec5SDimitry Andric if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true; 6480b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 6490b57cec5SDimitry Andric if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true; 6500b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM) 6510b57cec5SDimitry Andric if (regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) return true; 6520b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 6530b57cec5SDimitry Andric if (regNum >= UNW_ARM64_X0 && regNum <= UNW_ARM64_X30) return true; 6540b57cec5SDimitry Andric #endif 6550b57cec5SDimitry Andric return false; 6560b57cec5SDimitry Andric } 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric template <typename A, typename R> 6590b57cec5SDimitry Andric unw_word_t UnwindCursor<A, R>::getReg(int regNum) { 6600b57cec5SDimitry Andric switch (regNum) { 6610b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 6620b57cec5SDimitry Andric case UNW_REG_IP: return _msContext.Rip; 6630b57cec5SDimitry Andric case UNW_X86_64_RAX: return _msContext.Rax; 6640b57cec5SDimitry Andric case UNW_X86_64_RDX: return _msContext.Rdx; 6650b57cec5SDimitry Andric case UNW_X86_64_RCX: return _msContext.Rcx; 6660b57cec5SDimitry Andric case UNW_X86_64_RBX: return _msContext.Rbx; 6670b57cec5SDimitry Andric case UNW_REG_SP: 6680b57cec5SDimitry Andric case UNW_X86_64_RSP: return _msContext.Rsp; 6690b57cec5SDimitry Andric case UNW_X86_64_RBP: return _msContext.Rbp; 6700b57cec5SDimitry Andric case UNW_X86_64_RSI: return _msContext.Rsi; 6710b57cec5SDimitry Andric case UNW_X86_64_RDI: return _msContext.Rdi; 6720b57cec5SDimitry Andric case UNW_X86_64_R8: return _msContext.R8; 6730b57cec5SDimitry Andric case UNW_X86_64_R9: return _msContext.R9; 6740b57cec5SDimitry Andric case UNW_X86_64_R10: return _msContext.R10; 6750b57cec5SDimitry Andric case UNW_X86_64_R11: return _msContext.R11; 6760b57cec5SDimitry Andric case UNW_X86_64_R12: return _msContext.R12; 6770b57cec5SDimitry Andric case UNW_X86_64_R13: return _msContext.R13; 6780b57cec5SDimitry Andric case UNW_X86_64_R14: return _msContext.R14; 6790b57cec5SDimitry Andric case UNW_X86_64_R15: return _msContext.R15; 6800b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM) 6810b57cec5SDimitry Andric case UNW_ARM_R0: return _msContext.R0; 6820b57cec5SDimitry Andric case UNW_ARM_R1: return _msContext.R1; 6830b57cec5SDimitry Andric case UNW_ARM_R2: return _msContext.R2; 6840b57cec5SDimitry Andric case UNW_ARM_R3: return _msContext.R3; 6850b57cec5SDimitry Andric case UNW_ARM_R4: return _msContext.R4; 6860b57cec5SDimitry Andric case UNW_ARM_R5: return _msContext.R5; 6870b57cec5SDimitry Andric case UNW_ARM_R6: return _msContext.R6; 6880b57cec5SDimitry Andric case UNW_ARM_R7: return _msContext.R7; 6890b57cec5SDimitry Andric case UNW_ARM_R8: return _msContext.R8; 6900b57cec5SDimitry Andric case UNW_ARM_R9: return _msContext.R9; 6910b57cec5SDimitry Andric case UNW_ARM_R10: return _msContext.R10; 6920b57cec5SDimitry Andric case UNW_ARM_R11: return _msContext.R11; 6930b57cec5SDimitry Andric case UNW_ARM_R12: return _msContext.R12; 6940b57cec5SDimitry Andric case UNW_REG_SP: 6950b57cec5SDimitry Andric case UNW_ARM_SP: return _msContext.Sp; 6960b57cec5SDimitry Andric case UNW_ARM_LR: return _msContext.Lr; 6970b57cec5SDimitry Andric case UNW_REG_IP: 6980b57cec5SDimitry Andric case UNW_ARM_IP: return _msContext.Pc; 6990b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 7000b57cec5SDimitry Andric case UNW_REG_SP: return _msContext.Sp; 7010b57cec5SDimitry Andric case UNW_REG_IP: return _msContext.Pc; 7020b57cec5SDimitry Andric default: return _msContext.X[regNum - UNW_ARM64_X0]; 7030b57cec5SDimitry Andric #endif 7040b57cec5SDimitry Andric } 7050b57cec5SDimitry Andric _LIBUNWIND_ABORT("unsupported register"); 7060b57cec5SDimitry Andric } 7070b57cec5SDimitry Andric 7080b57cec5SDimitry Andric template <typename A, typename R> 7090b57cec5SDimitry Andric void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) { 7100b57cec5SDimitry Andric switch (regNum) { 7110b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 7120b57cec5SDimitry Andric case UNW_REG_IP: _msContext.Rip = value; break; 7130b57cec5SDimitry Andric case UNW_X86_64_RAX: _msContext.Rax = value; break; 7140b57cec5SDimitry Andric case UNW_X86_64_RDX: _msContext.Rdx = value; break; 7150b57cec5SDimitry Andric case UNW_X86_64_RCX: _msContext.Rcx = value; break; 7160b57cec5SDimitry Andric case UNW_X86_64_RBX: _msContext.Rbx = value; break; 7170b57cec5SDimitry Andric case UNW_REG_SP: 7180b57cec5SDimitry Andric case UNW_X86_64_RSP: _msContext.Rsp = value; break; 7190b57cec5SDimitry Andric case UNW_X86_64_RBP: _msContext.Rbp = value; break; 7200b57cec5SDimitry Andric case UNW_X86_64_RSI: _msContext.Rsi = value; break; 7210b57cec5SDimitry Andric case UNW_X86_64_RDI: _msContext.Rdi = value; break; 7220b57cec5SDimitry Andric case UNW_X86_64_R8: _msContext.R8 = value; break; 7230b57cec5SDimitry Andric case UNW_X86_64_R9: _msContext.R9 = value; break; 7240b57cec5SDimitry Andric case UNW_X86_64_R10: _msContext.R10 = value; break; 7250b57cec5SDimitry Andric case UNW_X86_64_R11: _msContext.R11 = value; break; 7260b57cec5SDimitry Andric case UNW_X86_64_R12: _msContext.R12 = value; break; 7270b57cec5SDimitry Andric case UNW_X86_64_R13: _msContext.R13 = value; break; 7280b57cec5SDimitry Andric case UNW_X86_64_R14: _msContext.R14 = value; break; 7290b57cec5SDimitry Andric case UNW_X86_64_R15: _msContext.R15 = value; break; 7300b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM) 7310b57cec5SDimitry Andric case UNW_ARM_R0: _msContext.R0 = value; break; 7320b57cec5SDimitry Andric case UNW_ARM_R1: _msContext.R1 = value; break; 7330b57cec5SDimitry Andric case UNW_ARM_R2: _msContext.R2 = value; break; 7340b57cec5SDimitry Andric case UNW_ARM_R3: _msContext.R3 = value; break; 7350b57cec5SDimitry Andric case UNW_ARM_R4: _msContext.R4 = value; break; 7360b57cec5SDimitry Andric case UNW_ARM_R5: _msContext.R5 = value; break; 7370b57cec5SDimitry Andric case UNW_ARM_R6: _msContext.R6 = value; break; 7380b57cec5SDimitry Andric case UNW_ARM_R7: _msContext.R7 = value; break; 7390b57cec5SDimitry Andric case UNW_ARM_R8: _msContext.R8 = value; break; 7400b57cec5SDimitry Andric case UNW_ARM_R9: _msContext.R9 = value; break; 7410b57cec5SDimitry Andric case UNW_ARM_R10: _msContext.R10 = value; break; 7420b57cec5SDimitry Andric case UNW_ARM_R11: _msContext.R11 = value; break; 7430b57cec5SDimitry Andric case UNW_ARM_R12: _msContext.R12 = value; break; 7440b57cec5SDimitry Andric case UNW_REG_SP: 7450b57cec5SDimitry Andric case UNW_ARM_SP: _msContext.Sp = value; break; 7460b57cec5SDimitry Andric case UNW_ARM_LR: _msContext.Lr = value; break; 7470b57cec5SDimitry Andric case UNW_REG_IP: 7480b57cec5SDimitry Andric case UNW_ARM_IP: _msContext.Pc = value; break; 7490b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 7500b57cec5SDimitry Andric case UNW_REG_SP: _msContext.Sp = value; break; 7510b57cec5SDimitry Andric case UNW_REG_IP: _msContext.Pc = value; break; 7520b57cec5SDimitry Andric case UNW_ARM64_X0: 7530b57cec5SDimitry Andric case UNW_ARM64_X1: 7540b57cec5SDimitry Andric case UNW_ARM64_X2: 7550b57cec5SDimitry Andric case UNW_ARM64_X3: 7560b57cec5SDimitry Andric case UNW_ARM64_X4: 7570b57cec5SDimitry Andric case UNW_ARM64_X5: 7580b57cec5SDimitry Andric case UNW_ARM64_X6: 7590b57cec5SDimitry Andric case UNW_ARM64_X7: 7600b57cec5SDimitry Andric case UNW_ARM64_X8: 7610b57cec5SDimitry Andric case UNW_ARM64_X9: 7620b57cec5SDimitry Andric case UNW_ARM64_X10: 7630b57cec5SDimitry Andric case UNW_ARM64_X11: 7640b57cec5SDimitry Andric case UNW_ARM64_X12: 7650b57cec5SDimitry Andric case UNW_ARM64_X13: 7660b57cec5SDimitry Andric case UNW_ARM64_X14: 7670b57cec5SDimitry Andric case UNW_ARM64_X15: 7680b57cec5SDimitry Andric case UNW_ARM64_X16: 7690b57cec5SDimitry Andric case UNW_ARM64_X17: 7700b57cec5SDimitry Andric case UNW_ARM64_X18: 7710b57cec5SDimitry Andric case UNW_ARM64_X19: 7720b57cec5SDimitry Andric case UNW_ARM64_X20: 7730b57cec5SDimitry Andric case UNW_ARM64_X21: 7740b57cec5SDimitry Andric case UNW_ARM64_X22: 7750b57cec5SDimitry Andric case UNW_ARM64_X23: 7760b57cec5SDimitry Andric case UNW_ARM64_X24: 7770b57cec5SDimitry Andric case UNW_ARM64_X25: 7780b57cec5SDimitry Andric case UNW_ARM64_X26: 7790b57cec5SDimitry Andric case UNW_ARM64_X27: 7800b57cec5SDimitry Andric case UNW_ARM64_X28: 7810b57cec5SDimitry Andric case UNW_ARM64_FP: 7820b57cec5SDimitry Andric case UNW_ARM64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break; 7830b57cec5SDimitry Andric #endif 7840b57cec5SDimitry Andric default: 7850b57cec5SDimitry Andric _LIBUNWIND_ABORT("unsupported register"); 7860b57cec5SDimitry Andric } 7870b57cec5SDimitry Andric } 7880b57cec5SDimitry Andric 7890b57cec5SDimitry Andric template <typename A, typename R> 7900b57cec5SDimitry Andric bool UnwindCursor<A, R>::validFloatReg(int regNum) { 7910b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM) 7920b57cec5SDimitry Andric if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true; 7930b57cec5SDimitry Andric if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true; 7940b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 7950b57cec5SDimitry Andric if (regNum >= UNW_ARM64_D0 && regNum <= UNW_ARM64_D31) return true; 7960b57cec5SDimitry Andric #else 7970b57cec5SDimitry Andric (void)regNum; 7980b57cec5SDimitry Andric #endif 7990b57cec5SDimitry Andric return false; 8000b57cec5SDimitry Andric } 8010b57cec5SDimitry Andric 8020b57cec5SDimitry Andric template <typename A, typename R> 8030b57cec5SDimitry Andric unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) { 8040b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM) 8050b57cec5SDimitry Andric if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) { 8060b57cec5SDimitry Andric union { 8070b57cec5SDimitry Andric uint32_t w; 8080b57cec5SDimitry Andric float f; 8090b57cec5SDimitry Andric } d; 8100b57cec5SDimitry Andric d.w = _msContext.S[regNum - UNW_ARM_S0]; 8110b57cec5SDimitry Andric return d.f; 8120b57cec5SDimitry Andric } 8130b57cec5SDimitry Andric if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) { 8140b57cec5SDimitry Andric union { 8150b57cec5SDimitry Andric uint64_t w; 8160b57cec5SDimitry Andric double d; 8170b57cec5SDimitry Andric } d; 8180b57cec5SDimitry Andric d.w = _msContext.D[regNum - UNW_ARM_D0]; 8190b57cec5SDimitry Andric return d.d; 8200b57cec5SDimitry Andric } 8210b57cec5SDimitry Andric _LIBUNWIND_ABORT("unsupported float register"); 8220b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 8230b57cec5SDimitry Andric return _msContext.V[regNum - UNW_ARM64_D0].D[0]; 8240b57cec5SDimitry Andric #else 8250b57cec5SDimitry Andric (void)regNum; 8260b57cec5SDimitry Andric _LIBUNWIND_ABORT("float registers unimplemented"); 8270b57cec5SDimitry Andric #endif 8280b57cec5SDimitry Andric } 8290b57cec5SDimitry Andric 8300b57cec5SDimitry Andric template <typename A, typename R> 8310b57cec5SDimitry Andric void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) { 8320b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM) 8330b57cec5SDimitry Andric if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) { 8340b57cec5SDimitry Andric union { 8350b57cec5SDimitry Andric uint32_t w; 8360b57cec5SDimitry Andric float f; 8370b57cec5SDimitry Andric } d; 8380b57cec5SDimitry Andric d.f = value; 8390b57cec5SDimitry Andric _msContext.S[regNum - UNW_ARM_S0] = d.w; 8400b57cec5SDimitry Andric } 8410b57cec5SDimitry Andric if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) { 8420b57cec5SDimitry Andric union { 8430b57cec5SDimitry Andric uint64_t w; 8440b57cec5SDimitry Andric double d; 8450b57cec5SDimitry Andric } d; 8460b57cec5SDimitry Andric d.d = value; 8470b57cec5SDimitry Andric _msContext.D[regNum - UNW_ARM_D0] = d.w; 8480b57cec5SDimitry Andric } 8490b57cec5SDimitry Andric _LIBUNWIND_ABORT("unsupported float register"); 8500b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 8510b57cec5SDimitry Andric _msContext.V[regNum - UNW_ARM64_D0].D[0] = value; 8520b57cec5SDimitry Andric #else 8530b57cec5SDimitry Andric (void)regNum; 8540b57cec5SDimitry Andric (void)value; 8550b57cec5SDimitry Andric _LIBUNWIND_ABORT("float registers unimplemented"); 8560b57cec5SDimitry Andric #endif 8570b57cec5SDimitry Andric } 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::jumpto() { 8600b57cec5SDimitry Andric RtlRestoreContext(&_msContext, nullptr); 8610b57cec5SDimitry Andric } 8620b57cec5SDimitry Andric 8630b57cec5SDimitry Andric #ifdef __arm__ 8640b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {} 8650b57cec5SDimitry Andric #endif 8660b57cec5SDimitry Andric 8670b57cec5SDimitry Andric template <typename A, typename R> 8680b57cec5SDimitry Andric const char *UnwindCursor<A, R>::getRegisterName(int regNum) { 8690b57cec5SDimitry Andric return R::getRegisterName(regNum); 8700b57cec5SDimitry Andric } 8710b57cec5SDimitry Andric 8720b57cec5SDimitry Andric template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() { 8730b57cec5SDimitry Andric return false; 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric 8760b57cec5SDimitry Andric #else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32) 8770b57cec5SDimitry Andric 8780b57cec5SDimitry Andric /// UnwindCursor contains all state (including all register values) during 8790b57cec5SDimitry Andric /// an unwind. This is normally stack allocated inside a unw_cursor_t. 8800b57cec5SDimitry Andric template <typename A, typename R> 8810b57cec5SDimitry Andric class UnwindCursor : public AbstractUnwindCursor{ 8820b57cec5SDimitry Andric typedef typename A::pint_t pint_t; 8830b57cec5SDimitry Andric public: 8840b57cec5SDimitry Andric UnwindCursor(unw_context_t *context, A &as); 8850b57cec5SDimitry Andric UnwindCursor(A &as, void *threadArg); 8860b57cec5SDimitry Andric virtual ~UnwindCursor() {} 8870b57cec5SDimitry Andric virtual bool validReg(int); 8880b57cec5SDimitry Andric virtual unw_word_t getReg(int); 8890b57cec5SDimitry Andric virtual void setReg(int, unw_word_t); 8900b57cec5SDimitry Andric virtual bool validFloatReg(int); 8910b57cec5SDimitry Andric virtual unw_fpreg_t getFloatReg(int); 8920b57cec5SDimitry Andric virtual void setFloatReg(int, unw_fpreg_t); 8930b57cec5SDimitry Andric virtual int step(); 8940b57cec5SDimitry Andric virtual void getInfo(unw_proc_info_t *); 8950b57cec5SDimitry Andric virtual void jumpto(); 8960b57cec5SDimitry Andric virtual bool isSignalFrame(); 8970b57cec5SDimitry Andric virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off); 8980b57cec5SDimitry Andric virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false); 8990b57cec5SDimitry Andric virtual const char *getRegisterName(int num); 9000b57cec5SDimitry Andric #ifdef __arm__ 9010b57cec5SDimitry Andric virtual void saveVFPAsX(); 9020b57cec5SDimitry Andric #endif 9030b57cec5SDimitry Andric 9040b57cec5SDimitry Andric // libunwind does not and should not depend on C++ library which means that we 9050b57cec5SDimitry Andric // need our own defition of inline placement new. 9060b57cec5SDimitry Andric static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; } 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andric private: 9090b57cec5SDimitry Andric 9100b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI) 9110b57cec5SDimitry Andric bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections §s); 9120b57cec5SDimitry Andric 9130b57cec5SDimitry Andric int stepWithEHABI() { 9140b57cec5SDimitry Andric size_t len = 0; 9150b57cec5SDimitry Andric size_t off = 0; 9160b57cec5SDimitry Andric // FIXME: Calling decode_eht_entry() here is violating the libunwind 9170b57cec5SDimitry Andric // abstraction layer. 9180b57cec5SDimitry Andric const uint32_t *ehtp = 9190b57cec5SDimitry Andric decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info), 9200b57cec5SDimitry Andric &off, &len); 9210b57cec5SDimitry Andric if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) != 9220b57cec5SDimitry Andric _URC_CONTINUE_UNWIND) 9230b57cec5SDimitry Andric return UNW_STEP_END; 9240b57cec5SDimitry Andric return UNW_STEP_SUCCESS; 9250b57cec5SDimitry Andric } 9260b57cec5SDimitry Andric #endif 9270b57cec5SDimitry Andric 928e8d8bef9SDimitry Andric #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64) 929e8d8bef9SDimitry Andric bool setInfoForSigReturn() { 930e8d8bef9SDimitry Andric R dummy; 931e8d8bef9SDimitry Andric return setInfoForSigReturn(dummy); 932e8d8bef9SDimitry Andric } 933e8d8bef9SDimitry Andric int stepThroughSigReturn() { 934e8d8bef9SDimitry Andric R dummy; 935e8d8bef9SDimitry Andric return stepThroughSigReturn(dummy); 936e8d8bef9SDimitry Andric } 937e8d8bef9SDimitry Andric bool setInfoForSigReturn(Registers_arm64 &); 938e8d8bef9SDimitry Andric int stepThroughSigReturn(Registers_arm64 &); 939e8d8bef9SDimitry Andric template <typename Registers> bool setInfoForSigReturn(Registers &) { 940e8d8bef9SDimitry Andric return false; 941e8d8bef9SDimitry Andric } 942e8d8bef9SDimitry Andric template <typename Registers> int stepThroughSigReturn(Registers &) { 943e8d8bef9SDimitry Andric return UNW_STEP_END; 944e8d8bef9SDimitry Andric } 945e8d8bef9SDimitry Andric #endif 946e8d8bef9SDimitry Andric 9470b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 948e8d8bef9SDimitry Andric bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo, 949e8d8bef9SDimitry Andric const typename CFI_Parser<A>::CIE_Info &cieInfo, 950e8d8bef9SDimitry Andric pint_t pc, uintptr_t dso_base); 9510b57cec5SDimitry Andric bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections §s, 9520b57cec5SDimitry Andric uint32_t fdeSectionOffsetHint=0); 9530b57cec5SDimitry Andric int stepWithDwarfFDE() { 9540b57cec5SDimitry Andric return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace, 9550b57cec5SDimitry Andric (pint_t)this->getReg(UNW_REG_IP), 9560b57cec5SDimitry Andric (pint_t)_info.unwind_info, 957480093f4SDimitry Andric _registers, _isSignalFrame); 9580b57cec5SDimitry Andric } 9590b57cec5SDimitry Andric #endif 9600b57cec5SDimitry Andric 9610b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 9620b57cec5SDimitry Andric bool getInfoFromCompactEncodingSection(pint_t pc, 9630b57cec5SDimitry Andric const UnwindInfoSections §s); 9640b57cec5SDimitry Andric int stepWithCompactEncoding() { 9650b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 9660b57cec5SDimitry Andric if ( compactSaysUseDwarf() ) 9670b57cec5SDimitry Andric return stepWithDwarfFDE(); 9680b57cec5SDimitry Andric #endif 9690b57cec5SDimitry Andric R dummy; 9700b57cec5SDimitry Andric return stepWithCompactEncoding(dummy); 9710b57cec5SDimitry Andric } 9720b57cec5SDimitry Andric 9730b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 9740b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_x86_64 &) { 9750b57cec5SDimitry Andric return CompactUnwinder_x86_64<A>::stepWithCompactEncoding( 9760b57cec5SDimitry Andric _info.format, _info.start_ip, _addressSpace, _registers); 9770b57cec5SDimitry Andric } 9780b57cec5SDimitry Andric #endif 9790b57cec5SDimitry Andric 9800b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386) 9810b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_x86 &) { 9820b57cec5SDimitry Andric return CompactUnwinder_x86<A>::stepWithCompactEncoding( 9830b57cec5SDimitry Andric _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers); 9840b57cec5SDimitry Andric } 9850b57cec5SDimitry Andric #endif 9860b57cec5SDimitry Andric 9870b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC) 9880b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_ppc &) { 9890b57cec5SDimitry Andric return UNW_EINVAL; 9900b57cec5SDimitry Andric } 9910b57cec5SDimitry Andric #endif 9920b57cec5SDimitry Andric 9930b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64) 9940b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_ppc64 &) { 9950b57cec5SDimitry Andric return UNW_EINVAL; 9960b57cec5SDimitry Andric } 9970b57cec5SDimitry Andric #endif 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric 10000b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64) 10010b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_arm64 &) { 10020b57cec5SDimitry Andric return CompactUnwinder_arm64<A>::stepWithCompactEncoding( 10030b57cec5SDimitry Andric _info.format, _info.start_ip, _addressSpace, _registers); 10040b57cec5SDimitry Andric } 10050b57cec5SDimitry Andric #endif 10060b57cec5SDimitry Andric 10070b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_O32) 10080b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_mips_o32 &) { 10090b57cec5SDimitry Andric return UNW_EINVAL; 10100b57cec5SDimitry Andric } 10110b57cec5SDimitry Andric #endif 10120b57cec5SDimitry Andric 10130b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI) 10140b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_mips_newabi &) { 10150b57cec5SDimitry Andric return UNW_EINVAL; 10160b57cec5SDimitry Andric } 10170b57cec5SDimitry Andric #endif 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC) 10200b57cec5SDimitry Andric int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; } 10210b57cec5SDimitry Andric #endif 10220b57cec5SDimitry Andric 1023480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV) 1024480093f4SDimitry Andric int stepWithCompactEncoding(Registers_riscv &) { 1025480093f4SDimitry Andric return UNW_EINVAL; 1026480093f4SDimitry Andric } 1027480093f4SDimitry Andric #endif 1028480093f4SDimitry Andric 10290b57cec5SDimitry Andric bool compactSaysUseDwarf(uint32_t *offset=NULL) const { 10300b57cec5SDimitry Andric R dummy; 10310b57cec5SDimitry Andric return compactSaysUseDwarf(dummy, offset); 10320b57cec5SDimitry Andric } 10330b57cec5SDimitry Andric 10340b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 10350b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const { 10360b57cec5SDimitry Andric if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) { 10370b57cec5SDimitry Andric if (offset) 10380b57cec5SDimitry Andric *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET); 10390b57cec5SDimitry Andric return true; 10400b57cec5SDimitry Andric } 10410b57cec5SDimitry Andric return false; 10420b57cec5SDimitry Andric } 10430b57cec5SDimitry Andric #endif 10440b57cec5SDimitry Andric 10450b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386) 10460b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const { 10470b57cec5SDimitry Andric if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) { 10480b57cec5SDimitry Andric if (offset) 10490b57cec5SDimitry Andric *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET); 10500b57cec5SDimitry Andric return true; 10510b57cec5SDimitry Andric } 10520b57cec5SDimitry Andric return false; 10530b57cec5SDimitry Andric } 10540b57cec5SDimitry Andric #endif 10550b57cec5SDimitry Andric 10560b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC) 10570b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const { 10580b57cec5SDimitry Andric return true; 10590b57cec5SDimitry Andric } 10600b57cec5SDimitry Andric #endif 10610b57cec5SDimitry Andric 10620b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64) 10630b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const { 10640b57cec5SDimitry Andric return true; 10650b57cec5SDimitry Andric } 10660b57cec5SDimitry Andric #endif 10670b57cec5SDimitry Andric 10680b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64) 10690b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const { 10700b57cec5SDimitry Andric if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) { 10710b57cec5SDimitry Andric if (offset) 10720b57cec5SDimitry Andric *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET); 10730b57cec5SDimitry Andric return true; 10740b57cec5SDimitry Andric } 10750b57cec5SDimitry Andric return false; 10760b57cec5SDimitry Andric } 10770b57cec5SDimitry Andric #endif 10780b57cec5SDimitry Andric 10790b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_O32) 10800b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const { 10810b57cec5SDimitry Andric return true; 10820b57cec5SDimitry Andric } 10830b57cec5SDimitry Andric #endif 10840b57cec5SDimitry Andric 10850b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI) 10860b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const { 10870b57cec5SDimitry Andric return true; 10880b57cec5SDimitry Andric } 10890b57cec5SDimitry Andric #endif 10900b57cec5SDimitry Andric 10910b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC) 10920b57cec5SDimitry Andric bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; } 10930b57cec5SDimitry Andric #endif 10940b57cec5SDimitry Andric 1095480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV) 1096480093f4SDimitry Andric bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const { 1097480093f4SDimitry Andric return true; 1098480093f4SDimitry Andric } 1099480093f4SDimitry Andric #endif 1100480093f4SDimitry Andric 11010b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 11020b57cec5SDimitry Andric 11030b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 11040b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding() const { 11050b57cec5SDimitry Andric R dummy; 11060b57cec5SDimitry Andric return dwarfEncoding(dummy); 11070b57cec5SDimitry Andric } 11080b57cec5SDimitry Andric 11090b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_X86_64) 11100b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const { 11110b57cec5SDimitry Andric return UNWIND_X86_64_MODE_DWARF; 11120b57cec5SDimitry Andric } 11130b57cec5SDimitry Andric #endif 11140b57cec5SDimitry Andric 11150b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_I386) 11160b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const { 11170b57cec5SDimitry Andric return UNWIND_X86_MODE_DWARF; 11180b57cec5SDimitry Andric } 11190b57cec5SDimitry Andric #endif 11200b57cec5SDimitry Andric 11210b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC) 11220b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const { 11230b57cec5SDimitry Andric return 0; 11240b57cec5SDimitry Andric } 11250b57cec5SDimitry Andric #endif 11260b57cec5SDimitry Andric 11270b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_PPC64) 11280b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const { 11290b57cec5SDimitry Andric return 0; 11300b57cec5SDimitry Andric } 11310b57cec5SDimitry Andric #endif 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_AARCH64) 11340b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const { 11350b57cec5SDimitry Andric return UNWIND_ARM64_MODE_DWARF; 11360b57cec5SDimitry Andric } 11370b57cec5SDimitry Andric #endif 11380b57cec5SDimitry Andric 11390b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_ARM) 11400b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const { 11410b57cec5SDimitry Andric return 0; 11420b57cec5SDimitry Andric } 11430b57cec5SDimitry Andric #endif 11440b57cec5SDimitry Andric 11450b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_OR1K) 11460b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const { 11470b57cec5SDimitry Andric return 0; 11480b57cec5SDimitry Andric } 11490b57cec5SDimitry Andric #endif 11500b57cec5SDimitry Andric 11515ffd83dbSDimitry Andric #if defined (_LIBUNWIND_TARGET_HEXAGON) 11525ffd83dbSDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const { 11535ffd83dbSDimitry Andric return 0; 11545ffd83dbSDimitry Andric } 11555ffd83dbSDimitry Andric #endif 11565ffd83dbSDimitry Andric 11570b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_MIPS_O32) 11580b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const { 11590b57cec5SDimitry Andric return 0; 11600b57cec5SDimitry Andric } 11610b57cec5SDimitry Andric #endif 11620b57cec5SDimitry Andric 11630b57cec5SDimitry Andric #if defined (_LIBUNWIND_TARGET_MIPS_NEWABI) 11640b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const { 11650b57cec5SDimitry Andric return 0; 11660b57cec5SDimitry Andric } 11670b57cec5SDimitry Andric #endif 11680b57cec5SDimitry Andric 11690b57cec5SDimitry Andric #if defined(_LIBUNWIND_TARGET_SPARC) 11700b57cec5SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; } 11710b57cec5SDimitry Andric #endif 11720b57cec5SDimitry Andric 1173480093f4SDimitry Andric #if defined (_LIBUNWIND_TARGET_RISCV) 1174480093f4SDimitry Andric compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const { 1175480093f4SDimitry Andric return 0; 1176480093f4SDimitry Andric } 1177480093f4SDimitry Andric #endif 1178480093f4SDimitry Andric 11790b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 11800b57cec5SDimitry Andric 11810b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 11820b57cec5SDimitry Andric // For runtime environments using SEH unwind data without Windows runtime 11830b57cec5SDimitry Andric // support. 11840b57cec5SDimitry Andric pint_t getLastPC() const { /* FIXME: Implement */ return 0; } 11850b57cec5SDimitry Andric void setLastPC(pint_t pc) { /* FIXME: Implement */ } 11860b57cec5SDimitry Andric RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) { 11870b57cec5SDimitry Andric /* FIXME: Implement */ 11880b57cec5SDimitry Andric *base = 0; 11890b57cec5SDimitry Andric return nullptr; 11900b57cec5SDimitry Andric } 11910b57cec5SDimitry Andric bool getInfoFromSEH(pint_t pc); 11920b57cec5SDimitry Andric int stepWithSEHData() { /* FIXME: Implement */ return 0; } 11930b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 11940b57cec5SDimitry Andric 11950b57cec5SDimitry Andric 11960b57cec5SDimitry Andric A &_addressSpace; 11970b57cec5SDimitry Andric R _registers; 11980b57cec5SDimitry Andric unw_proc_info_t _info; 11990b57cec5SDimitry Andric bool _unwindInfoMissing; 12000b57cec5SDimitry Andric bool _isSignalFrame; 1201e8d8bef9SDimitry Andric #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64) 1202e8d8bef9SDimitry Andric bool _isSigReturn = false; 1203e8d8bef9SDimitry Andric #endif 12040b57cec5SDimitry Andric }; 12050b57cec5SDimitry Andric 12060b57cec5SDimitry Andric 12070b57cec5SDimitry Andric template <typename A, typename R> 12080b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as) 12090b57cec5SDimitry Andric : _addressSpace(as), _registers(context), _unwindInfoMissing(false), 12100b57cec5SDimitry Andric _isSignalFrame(false) { 12110b57cec5SDimitry Andric static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), 12120b57cec5SDimitry Andric "UnwindCursor<> does not fit in unw_cursor_t"); 1213e8d8bef9SDimitry Andric static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)), 1214e8d8bef9SDimitry Andric "UnwindCursor<> requires more alignment than unw_cursor_t"); 12150b57cec5SDimitry Andric memset(&_info, 0, sizeof(_info)); 12160b57cec5SDimitry Andric } 12170b57cec5SDimitry Andric 12180b57cec5SDimitry Andric template <typename A, typename R> 12190b57cec5SDimitry Andric UnwindCursor<A, R>::UnwindCursor(A &as, void *) 12200b57cec5SDimitry Andric : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) { 12210b57cec5SDimitry Andric memset(&_info, 0, sizeof(_info)); 12220b57cec5SDimitry Andric // FIXME 12230b57cec5SDimitry Andric // fill in _registers from thread arg 12240b57cec5SDimitry Andric } 12250b57cec5SDimitry Andric 12260b57cec5SDimitry Andric 12270b57cec5SDimitry Andric template <typename A, typename R> 12280b57cec5SDimitry Andric bool UnwindCursor<A, R>::validReg(int regNum) { 12290b57cec5SDimitry Andric return _registers.validRegister(regNum); 12300b57cec5SDimitry Andric } 12310b57cec5SDimitry Andric 12320b57cec5SDimitry Andric template <typename A, typename R> 12330b57cec5SDimitry Andric unw_word_t UnwindCursor<A, R>::getReg(int regNum) { 12340b57cec5SDimitry Andric return _registers.getRegister(regNum); 12350b57cec5SDimitry Andric } 12360b57cec5SDimitry Andric 12370b57cec5SDimitry Andric template <typename A, typename R> 12380b57cec5SDimitry Andric void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) { 12390b57cec5SDimitry Andric _registers.setRegister(regNum, (typename A::pint_t)value); 12400b57cec5SDimitry Andric } 12410b57cec5SDimitry Andric 12420b57cec5SDimitry Andric template <typename A, typename R> 12430b57cec5SDimitry Andric bool UnwindCursor<A, R>::validFloatReg(int regNum) { 12440b57cec5SDimitry Andric return _registers.validFloatRegister(regNum); 12450b57cec5SDimitry Andric } 12460b57cec5SDimitry Andric 12470b57cec5SDimitry Andric template <typename A, typename R> 12480b57cec5SDimitry Andric unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) { 12490b57cec5SDimitry Andric return _registers.getFloatRegister(regNum); 12500b57cec5SDimitry Andric } 12510b57cec5SDimitry Andric 12520b57cec5SDimitry Andric template <typename A, typename R> 12530b57cec5SDimitry Andric void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) { 12540b57cec5SDimitry Andric _registers.setFloatRegister(regNum, value); 12550b57cec5SDimitry Andric } 12560b57cec5SDimitry Andric 12570b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::jumpto() { 12580b57cec5SDimitry Andric _registers.jumpto(); 12590b57cec5SDimitry Andric } 12600b57cec5SDimitry Andric 12610b57cec5SDimitry Andric #ifdef __arm__ 12620b57cec5SDimitry Andric template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() { 12630b57cec5SDimitry Andric _registers.saveVFPAsX(); 12640b57cec5SDimitry Andric } 12650b57cec5SDimitry Andric #endif 12660b57cec5SDimitry Andric 12670b57cec5SDimitry Andric template <typename A, typename R> 12680b57cec5SDimitry Andric const char *UnwindCursor<A, R>::getRegisterName(int regNum) { 12690b57cec5SDimitry Andric return _registers.getRegisterName(regNum); 12700b57cec5SDimitry Andric } 12710b57cec5SDimitry Andric 12720b57cec5SDimitry Andric template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() { 12730b57cec5SDimitry Andric return _isSignalFrame; 12740b57cec5SDimitry Andric } 12750b57cec5SDimitry Andric 12760b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 12770b57cec5SDimitry Andric 12780b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI) 12790b57cec5SDimitry Andric template<typename A> 12800b57cec5SDimitry Andric struct EHABISectionIterator { 12810b57cec5SDimitry Andric typedef EHABISectionIterator _Self; 12820b57cec5SDimitry Andric 12830b57cec5SDimitry Andric typedef typename A::pint_t value_type; 12840b57cec5SDimitry Andric typedef typename A::pint_t* pointer; 12850b57cec5SDimitry Andric typedef typename A::pint_t& reference; 12860b57cec5SDimitry Andric typedef size_t size_type; 12870b57cec5SDimitry Andric typedef size_t difference_type; 12880b57cec5SDimitry Andric 12890b57cec5SDimitry Andric static _Self begin(A& addressSpace, const UnwindInfoSections& sects) { 12900b57cec5SDimitry Andric return _Self(addressSpace, sects, 0); 12910b57cec5SDimitry Andric } 12920b57cec5SDimitry Andric static _Self end(A& addressSpace, const UnwindInfoSections& sects) { 12930b57cec5SDimitry Andric return _Self(addressSpace, sects, 12940b57cec5SDimitry Andric sects.arm_section_length / sizeof(EHABIIndexEntry)); 12950b57cec5SDimitry Andric } 12960b57cec5SDimitry Andric 12970b57cec5SDimitry Andric EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i) 12980b57cec5SDimitry Andric : _i(i), _addressSpace(&addressSpace), _sects(§s) {} 12990b57cec5SDimitry Andric 13000b57cec5SDimitry Andric _Self& operator++() { ++_i; return *this; } 13010b57cec5SDimitry Andric _Self& operator+=(size_t a) { _i += a; return *this; } 13020b57cec5SDimitry Andric _Self& operator--() { assert(_i > 0); --_i; return *this; } 13030b57cec5SDimitry Andric _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; } 13040b57cec5SDimitry Andric 13050b57cec5SDimitry Andric _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; } 13060b57cec5SDimitry Andric _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; } 13070b57cec5SDimitry Andric 13085ffd83dbSDimitry Andric size_t operator-(const _Self& other) const { return _i - other._i; } 13090b57cec5SDimitry Andric 13100b57cec5SDimitry Andric bool operator==(const _Self& other) const { 13110b57cec5SDimitry Andric assert(_addressSpace == other._addressSpace); 13120b57cec5SDimitry Andric assert(_sects == other._sects); 13130b57cec5SDimitry Andric return _i == other._i; 13140b57cec5SDimitry Andric } 13150b57cec5SDimitry Andric 13165ffd83dbSDimitry Andric bool operator!=(const _Self& other) const { 13175ffd83dbSDimitry Andric assert(_addressSpace == other._addressSpace); 13185ffd83dbSDimitry Andric assert(_sects == other._sects); 13195ffd83dbSDimitry Andric return _i != other._i; 13205ffd83dbSDimitry Andric } 13215ffd83dbSDimitry Andric 13220b57cec5SDimitry Andric typename A::pint_t operator*() const { return functionAddress(); } 13230b57cec5SDimitry Andric 13240b57cec5SDimitry Andric typename A::pint_t functionAddress() const { 13250b57cec5SDimitry Andric typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( 13260b57cec5SDimitry Andric EHABIIndexEntry, _i, functionOffset); 13270b57cec5SDimitry Andric return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr)); 13280b57cec5SDimitry Andric } 13290b57cec5SDimitry Andric 13300b57cec5SDimitry Andric typename A::pint_t dataAddress() { 13310b57cec5SDimitry Andric typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( 13320b57cec5SDimitry Andric EHABIIndexEntry, _i, data); 13330b57cec5SDimitry Andric return indexAddr; 13340b57cec5SDimitry Andric } 13350b57cec5SDimitry Andric 13360b57cec5SDimitry Andric private: 13370b57cec5SDimitry Andric size_t _i; 13380b57cec5SDimitry Andric A* _addressSpace; 13390b57cec5SDimitry Andric const UnwindInfoSections* _sects; 13400b57cec5SDimitry Andric }; 13410b57cec5SDimitry Andric 13420b57cec5SDimitry Andric namespace { 13430b57cec5SDimitry Andric 13440b57cec5SDimitry Andric template <typename A> 13450b57cec5SDimitry Andric EHABISectionIterator<A> EHABISectionUpperBound( 13460b57cec5SDimitry Andric EHABISectionIterator<A> first, 13470b57cec5SDimitry Andric EHABISectionIterator<A> last, 13480b57cec5SDimitry Andric typename A::pint_t value) { 13490b57cec5SDimitry Andric size_t len = last - first; 13500b57cec5SDimitry Andric while (len > 0) { 13510b57cec5SDimitry Andric size_t l2 = len / 2; 13520b57cec5SDimitry Andric EHABISectionIterator<A> m = first + l2; 13530b57cec5SDimitry Andric if (value < *m) { 13540b57cec5SDimitry Andric len = l2; 13550b57cec5SDimitry Andric } else { 13560b57cec5SDimitry Andric first = ++m; 13570b57cec5SDimitry Andric len -= l2 + 1; 13580b57cec5SDimitry Andric } 13590b57cec5SDimitry Andric } 13600b57cec5SDimitry Andric return first; 13610b57cec5SDimitry Andric } 13620b57cec5SDimitry Andric 13630b57cec5SDimitry Andric } 13640b57cec5SDimitry Andric 13650b57cec5SDimitry Andric template <typename A, typename R> 13660b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromEHABISection( 13670b57cec5SDimitry Andric pint_t pc, 13680b57cec5SDimitry Andric const UnwindInfoSections §s) { 13690b57cec5SDimitry Andric EHABISectionIterator<A> begin = 13700b57cec5SDimitry Andric EHABISectionIterator<A>::begin(_addressSpace, sects); 13710b57cec5SDimitry Andric EHABISectionIterator<A> end = 13720b57cec5SDimitry Andric EHABISectionIterator<A>::end(_addressSpace, sects); 13730b57cec5SDimitry Andric if (begin == end) 13740b57cec5SDimitry Andric return false; 13750b57cec5SDimitry Andric 13760b57cec5SDimitry Andric EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc); 13770b57cec5SDimitry Andric if (itNextPC == begin) 13780b57cec5SDimitry Andric return false; 13790b57cec5SDimitry Andric EHABISectionIterator<A> itThisPC = itNextPC - 1; 13800b57cec5SDimitry Andric 13810b57cec5SDimitry Andric pint_t thisPC = itThisPC.functionAddress(); 13820b57cec5SDimitry Andric // If an exception is thrown from a function, corresponding to the last entry 13830b57cec5SDimitry Andric // in the table, we don't really know the function extent and have to choose a 13840b57cec5SDimitry Andric // value for nextPC. Choosing max() will allow the range check during trace to 13850b57cec5SDimitry Andric // succeed. 13860b57cec5SDimitry Andric pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress(); 13870b57cec5SDimitry Andric pint_t indexDataAddr = itThisPC.dataAddress(); 13880b57cec5SDimitry Andric 13890b57cec5SDimitry Andric if (indexDataAddr == 0) 13900b57cec5SDimitry Andric return false; 13910b57cec5SDimitry Andric 13920b57cec5SDimitry Andric uint32_t indexData = _addressSpace.get32(indexDataAddr); 13930b57cec5SDimitry Andric if (indexData == UNW_EXIDX_CANTUNWIND) 13940b57cec5SDimitry Andric return false; 13950b57cec5SDimitry Andric 13960b57cec5SDimitry Andric // If the high bit is set, the exception handling table entry is inline inside 13970b57cec5SDimitry Andric // the index table entry on the second word (aka |indexDataAddr|). Otherwise, 1398473b61d3SDimitry Andric // the table points at an offset in the exception handling table (section 5 1399473b61d3SDimitry Andric // EHABI). 14000b57cec5SDimitry Andric pint_t exceptionTableAddr; 14010b57cec5SDimitry Andric uint32_t exceptionTableData; 14020b57cec5SDimitry Andric bool isSingleWordEHT; 14030b57cec5SDimitry Andric if (indexData & 0x80000000) { 14040b57cec5SDimitry Andric exceptionTableAddr = indexDataAddr; 14050b57cec5SDimitry Andric // TODO(ajwong): Should this data be 0? 14060b57cec5SDimitry Andric exceptionTableData = indexData; 14070b57cec5SDimitry Andric isSingleWordEHT = true; 14080b57cec5SDimitry Andric } else { 14090b57cec5SDimitry Andric exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData); 14100b57cec5SDimitry Andric exceptionTableData = _addressSpace.get32(exceptionTableAddr); 14110b57cec5SDimitry Andric isSingleWordEHT = false; 14120b57cec5SDimitry Andric } 14130b57cec5SDimitry Andric 14140b57cec5SDimitry Andric // Now we know the 3 things: 14150b57cec5SDimitry Andric // exceptionTableAddr -- exception handler table entry. 14160b57cec5SDimitry Andric // exceptionTableData -- the data inside the first word of the eht entry. 14170b57cec5SDimitry Andric // isSingleWordEHT -- whether the entry is in the index. 14180b57cec5SDimitry Andric unw_word_t personalityRoutine = 0xbadf00d; 14190b57cec5SDimitry Andric bool scope32 = false; 14200b57cec5SDimitry Andric uintptr_t lsda; 14210b57cec5SDimitry Andric 14220b57cec5SDimitry Andric // If the high bit in the exception handling table entry is set, the entry is 14230b57cec5SDimitry Andric // in compact form (section 6.3 EHABI). 14240b57cec5SDimitry Andric if (exceptionTableData & 0x80000000) { 14250b57cec5SDimitry Andric // Grab the index of the personality routine from the compact form. 14260b57cec5SDimitry Andric uint32_t choice = (exceptionTableData & 0x0f000000) >> 24; 14270b57cec5SDimitry Andric uint32_t extraWords = 0; 14280b57cec5SDimitry Andric switch (choice) { 14290b57cec5SDimitry Andric case 0: 14300b57cec5SDimitry Andric personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0; 14310b57cec5SDimitry Andric extraWords = 0; 14320b57cec5SDimitry Andric scope32 = false; 14330b57cec5SDimitry Andric lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4); 14340b57cec5SDimitry Andric break; 14350b57cec5SDimitry Andric case 1: 14360b57cec5SDimitry Andric personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1; 14370b57cec5SDimitry Andric extraWords = (exceptionTableData & 0x00ff0000) >> 16; 14380b57cec5SDimitry Andric scope32 = false; 14390b57cec5SDimitry Andric lsda = exceptionTableAddr + (extraWords + 1) * 4; 14400b57cec5SDimitry Andric break; 14410b57cec5SDimitry Andric case 2: 14420b57cec5SDimitry Andric personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2; 14430b57cec5SDimitry Andric extraWords = (exceptionTableData & 0x00ff0000) >> 16; 14440b57cec5SDimitry Andric scope32 = true; 14450b57cec5SDimitry Andric lsda = exceptionTableAddr + (extraWords + 1) * 4; 14460b57cec5SDimitry Andric break; 14470b57cec5SDimitry Andric default: 14480b57cec5SDimitry Andric _LIBUNWIND_ABORT("unknown personality routine"); 14490b57cec5SDimitry Andric return false; 14500b57cec5SDimitry Andric } 14510b57cec5SDimitry Andric 14520b57cec5SDimitry Andric if (isSingleWordEHT) { 14530b57cec5SDimitry Andric if (extraWords != 0) { 14540b57cec5SDimitry Andric _LIBUNWIND_ABORT("index inlined table detected but pr function " 14550b57cec5SDimitry Andric "requires extra words"); 14560b57cec5SDimitry Andric return false; 14570b57cec5SDimitry Andric } 14580b57cec5SDimitry Andric } 14590b57cec5SDimitry Andric } else { 14600b57cec5SDimitry Andric pint_t personalityAddr = 14610b57cec5SDimitry Andric exceptionTableAddr + signExtendPrel31(exceptionTableData); 14620b57cec5SDimitry Andric personalityRoutine = personalityAddr; 14630b57cec5SDimitry Andric 14640b57cec5SDimitry Andric // ARM EHABI # 6.2, # 9.2 14650b57cec5SDimitry Andric // 14660b57cec5SDimitry Andric // +---- ehtp 14670b57cec5SDimitry Andric // v 14680b57cec5SDimitry Andric // +--------------------------------------+ 14690b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 14700b57cec5SDimitry Andric // | |0| prel31 to personalityRoutine | | 14710b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 14720b57cec5SDimitry Andric // | | N | unwind opcodes | | <-- UnwindData 14730b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 14740b57cec5SDimitry Andric // | | Word 2 unwind opcodes | | 14750b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 14760b57cec5SDimitry Andric // | ... | 14770b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 14780b57cec5SDimitry Andric // | | Word N unwind opcodes | | 14790b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 14800b57cec5SDimitry Andric // | | LSDA | | <-- lsda 14810b57cec5SDimitry Andric // | | ... | | 14820b57cec5SDimitry Andric // | +--------+--------+--------+-------+ | 14830b57cec5SDimitry Andric // +--------------------------------------+ 14840b57cec5SDimitry Andric 14850b57cec5SDimitry Andric uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1; 14860b57cec5SDimitry Andric uint32_t FirstDataWord = *UnwindData; 14870b57cec5SDimitry Andric size_t N = ((FirstDataWord >> 24) & 0xff); 14880b57cec5SDimitry Andric size_t NDataWords = N + 1; 14890b57cec5SDimitry Andric lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords); 14900b57cec5SDimitry Andric } 14910b57cec5SDimitry Andric 14920b57cec5SDimitry Andric _info.start_ip = thisPC; 14930b57cec5SDimitry Andric _info.end_ip = nextPC; 14940b57cec5SDimitry Andric _info.handler = personalityRoutine; 14950b57cec5SDimitry Andric _info.unwind_info = exceptionTableAddr; 14960b57cec5SDimitry Andric _info.lsda = lsda; 14970b57cec5SDimitry Andric // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0. 1498473b61d3SDimitry Andric _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0); // Use enum? 14990b57cec5SDimitry Andric 15000b57cec5SDimitry Andric return true; 15010b57cec5SDimitry Andric } 15020b57cec5SDimitry Andric #endif 15030b57cec5SDimitry Andric 15040b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 15050b57cec5SDimitry Andric template <typename A, typename R> 1506e8d8bef9SDimitry Andric bool UnwindCursor<A, R>::getInfoFromFdeCie( 1507e8d8bef9SDimitry Andric const typename CFI_Parser<A>::FDE_Info &fdeInfo, 1508e8d8bef9SDimitry Andric const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc, 1509e8d8bef9SDimitry Andric uintptr_t dso_base) { 1510e8d8bef9SDimitry Andric typename CFI_Parser<A>::PrologInfo prolog; 1511e8d8bef9SDimitry Andric if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc, 1512e8d8bef9SDimitry Andric R::getArch(), &prolog)) { 1513e8d8bef9SDimitry Andric // Save off parsed FDE info 1514e8d8bef9SDimitry Andric _info.start_ip = fdeInfo.pcStart; 1515e8d8bef9SDimitry Andric _info.end_ip = fdeInfo.pcEnd; 1516e8d8bef9SDimitry Andric _info.lsda = fdeInfo.lsda; 1517e8d8bef9SDimitry Andric _info.handler = cieInfo.personality; 1518e8d8bef9SDimitry Andric // Some frameless functions need SP altered when resuming in function, so 1519e8d8bef9SDimitry Andric // propagate spExtraArgSize. 1520e8d8bef9SDimitry Andric _info.gp = prolog.spExtraArgSize; 1521e8d8bef9SDimitry Andric _info.flags = 0; 1522e8d8bef9SDimitry Andric _info.format = dwarfEncoding(); 1523e8d8bef9SDimitry Andric _info.unwind_info = fdeInfo.fdeStart; 1524e8d8bef9SDimitry Andric _info.unwind_info_size = static_cast<uint32_t>(fdeInfo.fdeLength); 1525e8d8bef9SDimitry Andric _info.extra = static_cast<unw_word_t>(dso_base); 1526e8d8bef9SDimitry Andric return true; 1527e8d8bef9SDimitry Andric } 1528e8d8bef9SDimitry Andric return false; 1529e8d8bef9SDimitry Andric } 1530e8d8bef9SDimitry Andric 1531e8d8bef9SDimitry Andric template <typename A, typename R> 15320b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc, 15330b57cec5SDimitry Andric const UnwindInfoSections §s, 15340b57cec5SDimitry Andric uint32_t fdeSectionOffsetHint) { 15350b57cec5SDimitry Andric typename CFI_Parser<A>::FDE_Info fdeInfo; 15360b57cec5SDimitry Andric typename CFI_Parser<A>::CIE_Info cieInfo; 15370b57cec5SDimitry Andric bool foundFDE = false; 15380b57cec5SDimitry Andric bool foundInCache = false; 15390b57cec5SDimitry Andric // If compact encoding table gave offset into dwarf section, go directly there 15400b57cec5SDimitry Andric if (fdeSectionOffsetHint != 0) { 15410b57cec5SDimitry Andric foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 1542e8d8bef9SDimitry Andric sects.dwarf_section_length, 15430b57cec5SDimitry Andric sects.dwarf_section + fdeSectionOffsetHint, 15440b57cec5SDimitry Andric &fdeInfo, &cieInfo); 15450b57cec5SDimitry Andric } 15460b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) 15470b57cec5SDimitry Andric if (!foundFDE && (sects.dwarf_index_section != 0)) { 15480b57cec5SDimitry Andric foundFDE = EHHeaderParser<A>::findFDE( 15490b57cec5SDimitry Andric _addressSpace, pc, sects.dwarf_index_section, 15500b57cec5SDimitry Andric (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo); 15510b57cec5SDimitry Andric } 15520b57cec5SDimitry Andric #endif 15530b57cec5SDimitry Andric if (!foundFDE) { 15540b57cec5SDimitry Andric // otherwise, search cache of previously found FDEs. 15550b57cec5SDimitry Andric pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc); 15560b57cec5SDimitry Andric if (cachedFDE != 0) { 15570b57cec5SDimitry Andric foundFDE = 15580b57cec5SDimitry Andric CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 1559e8d8bef9SDimitry Andric sects.dwarf_section_length, 15600b57cec5SDimitry Andric cachedFDE, &fdeInfo, &cieInfo); 15610b57cec5SDimitry Andric foundInCache = foundFDE; 15620b57cec5SDimitry Andric } 15630b57cec5SDimitry Andric } 15640b57cec5SDimitry Andric if (!foundFDE) { 15650b57cec5SDimitry Andric // Still not found, do full scan of __eh_frame section. 15660b57cec5SDimitry Andric foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 1567e8d8bef9SDimitry Andric sects.dwarf_section_length, 0, 15680b57cec5SDimitry Andric &fdeInfo, &cieInfo); 15690b57cec5SDimitry Andric } 15700b57cec5SDimitry Andric if (foundFDE) { 1571e8d8bef9SDimitry Andric if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) { 15720b57cec5SDimitry Andric // Add to cache (to make next lookup faster) if we had no hint 15730b57cec5SDimitry Andric // and there was no index. 15740b57cec5SDimitry Andric if (!foundInCache && (fdeSectionOffsetHint == 0)) { 15750b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) 15760b57cec5SDimitry Andric if (sects.dwarf_index_section == 0) 15770b57cec5SDimitry Andric #endif 15780b57cec5SDimitry Andric DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd, 15790b57cec5SDimitry Andric fdeInfo.fdeStart); 15800b57cec5SDimitry Andric } 15810b57cec5SDimitry Andric return true; 15820b57cec5SDimitry Andric } 15830b57cec5SDimitry Andric } 15840b57cec5SDimitry Andric //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc); 15850b57cec5SDimitry Andric return false; 15860b57cec5SDimitry Andric } 15870b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 15880b57cec5SDimitry Andric 15890b57cec5SDimitry Andric 15900b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 15910b57cec5SDimitry Andric template <typename A, typename R> 15920b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc, 15930b57cec5SDimitry Andric const UnwindInfoSections §s) { 15940b57cec5SDimitry Andric const bool log = false; 15950b57cec5SDimitry Andric if (log) 15960b57cec5SDimitry Andric fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n", 15970b57cec5SDimitry Andric (uint64_t)pc, (uint64_t)sects.dso_base); 15980b57cec5SDimitry Andric 15990b57cec5SDimitry Andric const UnwindSectionHeader<A> sectionHeader(_addressSpace, 16000b57cec5SDimitry Andric sects.compact_unwind_section); 16010b57cec5SDimitry Andric if (sectionHeader.version() != UNWIND_SECTION_VERSION) 16020b57cec5SDimitry Andric return false; 16030b57cec5SDimitry Andric 16040b57cec5SDimitry Andric // do a binary search of top level index to find page with unwind info 16050b57cec5SDimitry Andric pint_t targetFunctionOffset = pc - sects.dso_base; 16060b57cec5SDimitry Andric const UnwindSectionIndexArray<A> topIndex(_addressSpace, 16070b57cec5SDimitry Andric sects.compact_unwind_section 16080b57cec5SDimitry Andric + sectionHeader.indexSectionOffset()); 16090b57cec5SDimitry Andric uint32_t low = 0; 16100b57cec5SDimitry Andric uint32_t high = sectionHeader.indexCount(); 16110b57cec5SDimitry Andric uint32_t last = high - 1; 16120b57cec5SDimitry Andric while (low < high) { 16130b57cec5SDimitry Andric uint32_t mid = (low + high) / 2; 16140b57cec5SDimitry Andric //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n", 16150b57cec5SDimitry Andric //mid, low, high, topIndex.functionOffset(mid)); 16160b57cec5SDimitry Andric if (topIndex.functionOffset(mid) <= targetFunctionOffset) { 16170b57cec5SDimitry Andric if ((mid == last) || 16180b57cec5SDimitry Andric (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) { 16190b57cec5SDimitry Andric low = mid; 16200b57cec5SDimitry Andric break; 16210b57cec5SDimitry Andric } else { 16220b57cec5SDimitry Andric low = mid + 1; 16230b57cec5SDimitry Andric } 16240b57cec5SDimitry Andric } else { 16250b57cec5SDimitry Andric high = mid; 16260b57cec5SDimitry Andric } 16270b57cec5SDimitry Andric } 16280b57cec5SDimitry Andric const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low); 16290b57cec5SDimitry Andric const uint32_t firstLevelNextPageFunctionOffset = 16300b57cec5SDimitry Andric topIndex.functionOffset(low + 1); 16310b57cec5SDimitry Andric const pint_t secondLevelAddr = 16320b57cec5SDimitry Andric sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low); 16330b57cec5SDimitry Andric const pint_t lsdaArrayStartAddr = 16340b57cec5SDimitry Andric sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low); 16350b57cec5SDimitry Andric const pint_t lsdaArrayEndAddr = 16360b57cec5SDimitry Andric sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1); 16370b57cec5SDimitry Andric if (log) 16380b57cec5SDimitry Andric fprintf(stderr, "\tfirst level search for result index=%d " 16390b57cec5SDimitry Andric "to secondLevelAddr=0x%llX\n", 16400b57cec5SDimitry Andric low, (uint64_t) secondLevelAddr); 16410b57cec5SDimitry Andric // do a binary search of second level page index 16420b57cec5SDimitry Andric uint32_t encoding = 0; 16430b57cec5SDimitry Andric pint_t funcStart = 0; 16440b57cec5SDimitry Andric pint_t funcEnd = 0; 16450b57cec5SDimitry Andric pint_t lsda = 0; 16460b57cec5SDimitry Andric pint_t personality = 0; 16470b57cec5SDimitry Andric uint32_t pageKind = _addressSpace.get32(secondLevelAddr); 16480b57cec5SDimitry Andric if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) { 16490b57cec5SDimitry Andric // regular page 16500b57cec5SDimitry Andric UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace, 16510b57cec5SDimitry Andric secondLevelAddr); 16520b57cec5SDimitry Andric UnwindSectionRegularArray<A> pageIndex( 16530b57cec5SDimitry Andric _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); 16540b57cec5SDimitry Andric // binary search looks for entry with e where index[e].offset <= pc < 16550b57cec5SDimitry Andric // index[e+1].offset 16560b57cec5SDimitry Andric if (log) 16570b57cec5SDimitry Andric fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in " 16580b57cec5SDimitry Andric "regular page starting at secondLevelAddr=0x%llX\n", 16590b57cec5SDimitry Andric (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr); 16600b57cec5SDimitry Andric low = 0; 16610b57cec5SDimitry Andric high = pageHeader.entryCount(); 16620b57cec5SDimitry Andric while (low < high) { 16630b57cec5SDimitry Andric uint32_t mid = (low + high) / 2; 16640b57cec5SDimitry Andric if (pageIndex.functionOffset(mid) <= targetFunctionOffset) { 16650b57cec5SDimitry Andric if (mid == (uint32_t)(pageHeader.entryCount() - 1)) { 16660b57cec5SDimitry Andric // at end of table 16670b57cec5SDimitry Andric low = mid; 16680b57cec5SDimitry Andric funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; 16690b57cec5SDimitry Andric break; 16700b57cec5SDimitry Andric } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) { 16710b57cec5SDimitry Andric // next is too big, so we found it 16720b57cec5SDimitry Andric low = mid; 16730b57cec5SDimitry Andric funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base; 16740b57cec5SDimitry Andric break; 16750b57cec5SDimitry Andric } else { 16760b57cec5SDimitry Andric low = mid + 1; 16770b57cec5SDimitry Andric } 16780b57cec5SDimitry Andric } else { 16790b57cec5SDimitry Andric high = mid; 16800b57cec5SDimitry Andric } 16810b57cec5SDimitry Andric } 16820b57cec5SDimitry Andric encoding = pageIndex.encoding(low); 16830b57cec5SDimitry Andric funcStart = pageIndex.functionOffset(low) + sects.dso_base; 16840b57cec5SDimitry Andric if (pc < funcStart) { 16850b57cec5SDimitry Andric if (log) 16860b57cec5SDimitry Andric fprintf( 16870b57cec5SDimitry Andric stderr, 16880b57cec5SDimitry Andric "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", 16890b57cec5SDimitry Andric (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); 16900b57cec5SDimitry Andric return false; 16910b57cec5SDimitry Andric } 16920b57cec5SDimitry Andric if (pc > funcEnd) { 16930b57cec5SDimitry Andric if (log) 16940b57cec5SDimitry Andric fprintf( 16950b57cec5SDimitry Andric stderr, 16960b57cec5SDimitry Andric "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", 16970b57cec5SDimitry Andric (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); 16980b57cec5SDimitry Andric return false; 16990b57cec5SDimitry Andric } 17000b57cec5SDimitry Andric } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) { 17010b57cec5SDimitry Andric // compressed page 17020b57cec5SDimitry Andric UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace, 17030b57cec5SDimitry Andric secondLevelAddr); 17040b57cec5SDimitry Andric UnwindSectionCompressedArray<A> pageIndex( 17050b57cec5SDimitry Andric _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); 17060b57cec5SDimitry Andric const uint32_t targetFunctionPageOffset = 17070b57cec5SDimitry Andric (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset); 17080b57cec5SDimitry Andric // binary search looks for entry with e where index[e].offset <= pc < 17090b57cec5SDimitry Andric // index[e+1].offset 17100b57cec5SDimitry Andric if (log) 17110b57cec5SDimitry Andric fprintf(stderr, "\tbinary search of compressed page starting at " 17120b57cec5SDimitry Andric "secondLevelAddr=0x%llX\n", 17130b57cec5SDimitry Andric (uint64_t) secondLevelAddr); 17140b57cec5SDimitry Andric low = 0; 17150b57cec5SDimitry Andric last = pageHeader.entryCount() - 1; 17160b57cec5SDimitry Andric high = pageHeader.entryCount(); 17170b57cec5SDimitry Andric while (low < high) { 17180b57cec5SDimitry Andric uint32_t mid = (low + high) / 2; 17190b57cec5SDimitry Andric if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) { 17200b57cec5SDimitry Andric if ((mid == last) || 17210b57cec5SDimitry Andric (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) { 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 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset 17320b57cec5SDimitry Andric + sects.dso_base; 17330b57cec5SDimitry Andric if (low < last) 17340b57cec5SDimitry Andric funcEnd = 17350b57cec5SDimitry Andric pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset 17360b57cec5SDimitry Andric + sects.dso_base; 17370b57cec5SDimitry Andric else 17380b57cec5SDimitry Andric funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; 17390b57cec5SDimitry Andric if (pc < funcStart) { 1740*fe6060f1SDimitry Andric _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX " 1741*fe6060f1SDimitry Andric "not in second level compressed unwind table. " 1742*fe6060f1SDimitry Andric "funcStart=0x%llX", 17430b57cec5SDimitry Andric (uint64_t) pc, (uint64_t) funcStart); 17440b57cec5SDimitry Andric return false; 17450b57cec5SDimitry Andric } 17460b57cec5SDimitry Andric if (pc > funcEnd) { 1747*fe6060f1SDimitry Andric _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX " 1748*fe6060f1SDimitry Andric "not in second level compressed unwind table. " 1749*fe6060f1SDimitry Andric "funcEnd=0x%llX", 17500b57cec5SDimitry Andric (uint64_t) pc, (uint64_t) funcEnd); 17510b57cec5SDimitry Andric return false; 17520b57cec5SDimitry Andric } 17530b57cec5SDimitry Andric uint16_t encodingIndex = pageIndex.encodingIndex(low); 17540b57cec5SDimitry Andric if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) { 17550b57cec5SDimitry Andric // encoding is in common table in section header 17560b57cec5SDimitry Andric encoding = _addressSpace.get32( 17570b57cec5SDimitry Andric sects.compact_unwind_section + 17580b57cec5SDimitry Andric sectionHeader.commonEncodingsArraySectionOffset() + 17590b57cec5SDimitry Andric encodingIndex * sizeof(uint32_t)); 17600b57cec5SDimitry Andric } else { 17610b57cec5SDimitry Andric // encoding is in page specific table 17620b57cec5SDimitry Andric uint16_t pageEncodingIndex = 17630b57cec5SDimitry Andric encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount(); 17640b57cec5SDimitry Andric encoding = _addressSpace.get32(secondLevelAddr + 17650b57cec5SDimitry Andric pageHeader.encodingsPageOffset() + 17660b57cec5SDimitry Andric pageEncodingIndex * sizeof(uint32_t)); 17670b57cec5SDimitry Andric } 17680b57cec5SDimitry Andric } else { 1769*fe6060f1SDimitry Andric _LIBUNWIND_DEBUG_LOG( 1770*fe6060f1SDimitry Andric "malformed __unwind_info at 0x%0llX bad second level page", 17710b57cec5SDimitry Andric (uint64_t)sects.compact_unwind_section); 17720b57cec5SDimitry Andric return false; 17730b57cec5SDimitry Andric } 17740b57cec5SDimitry Andric 17750b57cec5SDimitry Andric // look up LSDA, if encoding says function has one 17760b57cec5SDimitry Andric if (encoding & UNWIND_HAS_LSDA) { 17770b57cec5SDimitry Andric UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr); 17780b57cec5SDimitry Andric uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base); 17790b57cec5SDimitry Andric low = 0; 17800b57cec5SDimitry Andric high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) / 17810b57cec5SDimitry Andric sizeof(unwind_info_section_header_lsda_index_entry); 17820b57cec5SDimitry Andric // binary search looks for entry with exact match for functionOffset 17830b57cec5SDimitry Andric if (log) 17840b57cec5SDimitry Andric fprintf(stderr, 17850b57cec5SDimitry Andric "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n", 17860b57cec5SDimitry Andric funcStartOffset); 17870b57cec5SDimitry Andric while (low < high) { 17880b57cec5SDimitry Andric uint32_t mid = (low + high) / 2; 17890b57cec5SDimitry Andric if (lsdaIndex.functionOffset(mid) == funcStartOffset) { 17900b57cec5SDimitry Andric lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base; 17910b57cec5SDimitry Andric break; 17920b57cec5SDimitry Andric } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) { 17930b57cec5SDimitry Andric low = mid + 1; 17940b57cec5SDimitry Andric } else { 17950b57cec5SDimitry Andric high = mid; 17960b57cec5SDimitry Andric } 17970b57cec5SDimitry Andric } 17980b57cec5SDimitry Andric if (lsda == 0) { 17990b57cec5SDimitry Andric _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for " 18000b57cec5SDimitry Andric "pc=0x%0llX, but lsda table has no entry", 18010b57cec5SDimitry Andric encoding, (uint64_t) pc); 18020b57cec5SDimitry Andric return false; 18030b57cec5SDimitry Andric } 18040b57cec5SDimitry Andric } 18050b57cec5SDimitry Andric 1806e8d8bef9SDimitry Andric // extract personality routine, if encoding says function has one 18070b57cec5SDimitry Andric uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >> 18080b57cec5SDimitry Andric (__builtin_ctz(UNWIND_PERSONALITY_MASK)); 18090b57cec5SDimitry Andric if (personalityIndex != 0) { 18100b57cec5SDimitry Andric --personalityIndex; // change 1-based to zero-based index 1811e8d8bef9SDimitry Andric if (personalityIndex >= sectionHeader.personalityArrayCount()) { 18120b57cec5SDimitry Andric _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, " 18130b57cec5SDimitry Andric "but personality table has only %d entries", 18140b57cec5SDimitry Andric encoding, personalityIndex, 18150b57cec5SDimitry Andric sectionHeader.personalityArrayCount()); 18160b57cec5SDimitry Andric return false; 18170b57cec5SDimitry Andric } 18180b57cec5SDimitry Andric int32_t personalityDelta = (int32_t)_addressSpace.get32( 18190b57cec5SDimitry Andric sects.compact_unwind_section + 18200b57cec5SDimitry Andric sectionHeader.personalityArraySectionOffset() + 18210b57cec5SDimitry Andric personalityIndex * sizeof(uint32_t)); 18220b57cec5SDimitry Andric pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta; 18230b57cec5SDimitry Andric personality = _addressSpace.getP(personalityPointer); 18240b57cec5SDimitry Andric if (log) 18250b57cec5SDimitry Andric fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " 18260b57cec5SDimitry Andric "personalityDelta=0x%08X, personality=0x%08llX\n", 18270b57cec5SDimitry Andric (uint64_t) pc, personalityDelta, (uint64_t) personality); 18280b57cec5SDimitry Andric } 18290b57cec5SDimitry Andric 18300b57cec5SDimitry Andric if (log) 18310b57cec5SDimitry Andric fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " 18320b57cec5SDimitry Andric "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n", 18330b57cec5SDimitry Andric (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart); 18340b57cec5SDimitry Andric _info.start_ip = funcStart; 18350b57cec5SDimitry Andric _info.end_ip = funcEnd; 18360b57cec5SDimitry Andric _info.lsda = lsda; 18370b57cec5SDimitry Andric _info.handler = personality; 18380b57cec5SDimitry Andric _info.gp = 0; 18390b57cec5SDimitry Andric _info.flags = 0; 18400b57cec5SDimitry Andric _info.format = encoding; 18410b57cec5SDimitry Andric _info.unwind_info = 0; 18420b57cec5SDimitry Andric _info.unwind_info_size = 0; 18430b57cec5SDimitry Andric _info.extra = sects.dso_base; 18440b57cec5SDimitry Andric return true; 18450b57cec5SDimitry Andric } 18460b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 18470b57cec5SDimitry Andric 18480b57cec5SDimitry Andric 18490b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 18500b57cec5SDimitry Andric template <typename A, typename R> 18510b57cec5SDimitry Andric bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) { 18520b57cec5SDimitry Andric pint_t base; 18530b57cec5SDimitry Andric RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base); 18540b57cec5SDimitry Andric if (!unwindEntry) { 18550b57cec5SDimitry Andric _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc); 18560b57cec5SDimitry Andric return false; 18570b57cec5SDimitry Andric } 18580b57cec5SDimitry Andric _info.gp = 0; 18590b57cec5SDimitry Andric _info.flags = 0; 18600b57cec5SDimitry Andric _info.format = 0; 18610b57cec5SDimitry Andric _info.unwind_info_size = sizeof(RUNTIME_FUNCTION); 18620b57cec5SDimitry Andric _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry); 18630b57cec5SDimitry Andric _info.extra = base; 18640b57cec5SDimitry Andric _info.start_ip = base + unwindEntry->BeginAddress; 18650b57cec5SDimitry Andric #ifdef _LIBUNWIND_TARGET_X86_64 18660b57cec5SDimitry Andric _info.end_ip = base + unwindEntry->EndAddress; 18670b57cec5SDimitry Andric // Only fill in the handler and LSDA if they're stale. 18680b57cec5SDimitry Andric if (pc != getLastPC()) { 18690b57cec5SDimitry Andric UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData); 18700b57cec5SDimitry Andric if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) { 18710b57cec5SDimitry Andric // The personality is given in the UNWIND_INFO itself. The LSDA immediately 18720b57cec5SDimitry Andric // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit 18730b57cec5SDimitry Andric // these structures.) 18740b57cec5SDimitry Andric // N.B. UNWIND_INFO structs are DWORD-aligned. 18750b57cec5SDimitry Andric uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1; 18760b57cec5SDimitry Andric const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]); 18770b57cec5SDimitry Andric _info.lsda = reinterpret_cast<unw_word_t>(handler+1); 18780b57cec5SDimitry Andric if (*handler) { 18790b57cec5SDimitry Andric _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality); 18800b57cec5SDimitry Andric } else 18810b57cec5SDimitry Andric _info.handler = 0; 18820b57cec5SDimitry Andric } else { 18830b57cec5SDimitry Andric _info.lsda = 0; 18840b57cec5SDimitry Andric _info.handler = 0; 18850b57cec5SDimitry Andric } 18860b57cec5SDimitry Andric } 18870b57cec5SDimitry Andric #elif defined(_LIBUNWIND_TARGET_ARM) 18880b57cec5SDimitry Andric _info.end_ip = _info.start_ip + unwindEntry->FunctionLength; 18890b57cec5SDimitry Andric _info.lsda = 0; // FIXME 18900b57cec5SDimitry Andric _info.handler = 0; // FIXME 18910b57cec5SDimitry Andric #endif 18920b57cec5SDimitry Andric setLastPC(pc); 18930b57cec5SDimitry Andric return true; 18940b57cec5SDimitry Andric } 18950b57cec5SDimitry Andric #endif 18960b57cec5SDimitry Andric 18970b57cec5SDimitry Andric 18980b57cec5SDimitry Andric template <typename A, typename R> 18990b57cec5SDimitry Andric void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) { 1900e8d8bef9SDimitry Andric #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64) 1901e8d8bef9SDimitry Andric _isSigReturn = false; 1902e8d8bef9SDimitry Andric #endif 1903e8d8bef9SDimitry Andric 1904e8d8bef9SDimitry Andric pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP)); 19050b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI) 19060b57cec5SDimitry Andric // Remove the thumb bit so the IP represents the actual instruction address. 19070b57cec5SDimitry Andric // This matches the behaviour of _Unwind_GetIP on arm. 19080b57cec5SDimitry Andric pc &= (pint_t)~0x1; 19090b57cec5SDimitry Andric #endif 19100b57cec5SDimitry Andric 19115ffd83dbSDimitry Andric // Exit early if at the top of the stack. 19125ffd83dbSDimitry Andric if (pc == 0) { 19135ffd83dbSDimitry Andric _unwindInfoMissing = true; 19145ffd83dbSDimitry Andric return; 19155ffd83dbSDimitry Andric } 19165ffd83dbSDimitry Andric 19170b57cec5SDimitry Andric // If the last line of a function is a "throw" the compiler sometimes 19180b57cec5SDimitry Andric // emits no instructions after the call to __cxa_throw. This means 19190b57cec5SDimitry Andric // the return address is actually the start of the next function. 19200b57cec5SDimitry Andric // To disambiguate this, back up the pc when we know it is a return 19210b57cec5SDimitry Andric // address. 19220b57cec5SDimitry Andric if (isReturnAddress) 19230b57cec5SDimitry Andric --pc; 19240b57cec5SDimitry Andric 19250b57cec5SDimitry Andric // Ask address space object to find unwind sections for this pc. 19260b57cec5SDimitry Andric UnwindInfoSections sects; 19270b57cec5SDimitry Andric if (_addressSpace.findUnwindSections(pc, sects)) { 19280b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 19290b57cec5SDimitry Andric // If there is a compact unwind encoding table, look there first. 19300b57cec5SDimitry Andric if (sects.compact_unwind_section != 0) { 19310b57cec5SDimitry Andric if (this->getInfoFromCompactEncodingSection(pc, sects)) { 19320b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 19330b57cec5SDimitry Andric // Found info in table, done unless encoding says to use dwarf. 19340b57cec5SDimitry Andric uint32_t dwarfOffset; 19350b57cec5SDimitry Andric if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) { 19360b57cec5SDimitry Andric if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) { 19370b57cec5SDimitry Andric // found info in dwarf, done 19380b57cec5SDimitry Andric return; 19390b57cec5SDimitry Andric } 19400b57cec5SDimitry Andric } 19410b57cec5SDimitry Andric #endif 19420b57cec5SDimitry Andric // If unwind table has entry, but entry says there is no unwind info, 19430b57cec5SDimitry Andric // record that we have no unwind info. 19440b57cec5SDimitry Andric if (_info.format == 0) 19450b57cec5SDimitry Andric _unwindInfoMissing = true; 19460b57cec5SDimitry Andric return; 19470b57cec5SDimitry Andric } 19480b57cec5SDimitry Andric } 19490b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 19500b57cec5SDimitry Andric 19510b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 19520b57cec5SDimitry Andric // If there is SEH unwind info, look there next. 19530b57cec5SDimitry Andric if (this->getInfoFromSEH(pc)) 19540b57cec5SDimitry Andric return; 19550b57cec5SDimitry Andric #endif 19560b57cec5SDimitry Andric 19570b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 19580b57cec5SDimitry Andric // If there is dwarf unwind info, look there next. 19590b57cec5SDimitry Andric if (sects.dwarf_section != 0) { 19600b57cec5SDimitry Andric if (this->getInfoFromDwarfSection(pc, sects)) { 19610b57cec5SDimitry Andric // found info in dwarf, done 19620b57cec5SDimitry Andric return; 19630b57cec5SDimitry Andric } 19640b57cec5SDimitry Andric } 19650b57cec5SDimitry Andric #endif 19660b57cec5SDimitry Andric 19670b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI) 19680b57cec5SDimitry Andric // If there is ARM EHABI unwind info, look there next. 19690b57cec5SDimitry Andric if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects)) 19700b57cec5SDimitry Andric return; 19710b57cec5SDimitry Andric #endif 19720b57cec5SDimitry Andric } 19730b57cec5SDimitry Andric 19740b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 19750b57cec5SDimitry Andric // There is no static unwind info for this pc. Look to see if an FDE was 19760b57cec5SDimitry Andric // dynamically registered for it. 1977e8d8bef9SDimitry Andric pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll, 1978e8d8bef9SDimitry Andric pc); 19790b57cec5SDimitry Andric if (cachedFDE != 0) { 1980e8d8bef9SDimitry Andric typename CFI_Parser<A>::FDE_Info fdeInfo; 1981e8d8bef9SDimitry Andric typename CFI_Parser<A>::CIE_Info cieInfo; 1982e8d8bef9SDimitry Andric if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo)) 1983e8d8bef9SDimitry Andric if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0)) 19840b57cec5SDimitry Andric return; 19850b57cec5SDimitry Andric } 19860b57cec5SDimitry Andric 19870b57cec5SDimitry Andric // Lastly, ask AddressSpace object about platform specific ways to locate 19880b57cec5SDimitry Andric // other FDEs. 19890b57cec5SDimitry Andric pint_t fde; 19900b57cec5SDimitry Andric if (_addressSpace.findOtherFDE(pc, fde)) { 1991e8d8bef9SDimitry Andric typename CFI_Parser<A>::FDE_Info fdeInfo; 1992e8d8bef9SDimitry Andric typename CFI_Parser<A>::CIE_Info cieInfo; 19930b57cec5SDimitry Andric if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) { 19940b57cec5SDimitry Andric // Double check this FDE is for a function that includes the pc. 1995e8d8bef9SDimitry Andric if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) 1996e8d8bef9SDimitry Andric if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0)) 19970b57cec5SDimitry Andric return; 19980b57cec5SDimitry Andric } 19990b57cec5SDimitry Andric } 20000b57cec5SDimitry Andric #endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 20010b57cec5SDimitry Andric 2002e8d8bef9SDimitry Andric #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64) 2003e8d8bef9SDimitry Andric if (setInfoForSigReturn()) 2004e8d8bef9SDimitry Andric return; 2005e8d8bef9SDimitry Andric #endif 2006e8d8bef9SDimitry Andric 20070b57cec5SDimitry Andric // no unwind info, flag that we can't reliably unwind 20080b57cec5SDimitry Andric _unwindInfoMissing = true; 20090b57cec5SDimitry Andric } 20100b57cec5SDimitry Andric 2011e8d8bef9SDimitry Andric #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64) 2012e8d8bef9SDimitry Andric template <typename A, typename R> 2013e8d8bef9SDimitry Andric bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_arm64 &) { 2014e8d8bef9SDimitry Andric // Look for the sigreturn trampoline. The trampoline's body is two 2015e8d8bef9SDimitry Andric // specific instructions (see below). Typically the trampoline comes from the 2016e8d8bef9SDimitry Andric // vDSO[1] (i.e. the __kernel_rt_sigreturn function). A libc might provide its 2017e8d8bef9SDimitry Andric // own restorer function, though, or user-mode QEMU might write a trampoline 2018e8d8bef9SDimitry Andric // onto the stack. 2019e8d8bef9SDimitry Andric // 2020e8d8bef9SDimitry Andric // This special code path is a fallback that is only used if the trampoline 2021e8d8bef9SDimitry Andric // lacks proper (e.g. DWARF) unwind info. On AArch64, a new DWARF register 2022e8d8bef9SDimitry Andric // constant for the PC needs to be defined before DWARF can handle a signal 2023e8d8bef9SDimitry Andric // trampoline. This code may segfault if the target PC is unreadable, e.g.: 2024e8d8bef9SDimitry Andric // - The PC points at a function compiled without unwind info, and which is 2025e8d8bef9SDimitry Andric // part of an execute-only mapping (e.g. using -Wl,--execute-only). 2026e8d8bef9SDimitry Andric // - The PC is invalid and happens to point to unreadable or unmapped memory. 2027e8d8bef9SDimitry Andric // 2028e8d8bef9SDimitry Andric // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/vdso/sigreturn.S 2029e8d8bef9SDimitry Andric const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP)); 2030e8d8bef9SDimitry Andric // Look for instructions: mov x8, #0x8b; svc #0x0 2031e8d8bef9SDimitry Andric if (_addressSpace.get32(pc) == 0xd2801168 && 2032e8d8bef9SDimitry Andric _addressSpace.get32(pc + 4) == 0xd4000001) { 2033e8d8bef9SDimitry Andric _info = {}; 2034e8d8bef9SDimitry Andric _isSigReturn = true; 2035e8d8bef9SDimitry Andric return true; 2036e8d8bef9SDimitry Andric } 2037e8d8bef9SDimitry Andric return false; 2038e8d8bef9SDimitry Andric } 2039e8d8bef9SDimitry Andric 2040e8d8bef9SDimitry Andric template <typename A, typename R> 2041e8d8bef9SDimitry Andric int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) { 2042e8d8bef9SDimitry Andric // In the signal trampoline frame, sp points to an rt_sigframe[1], which is: 2043e8d8bef9SDimitry Andric // - 128-byte siginfo struct 2044e8d8bef9SDimitry Andric // - ucontext struct: 2045e8d8bef9SDimitry Andric // - 8-byte long (uc_flags) 2046e8d8bef9SDimitry Andric // - 8-byte pointer (uc_link) 2047e8d8bef9SDimitry Andric // - 24-byte stack_t 2048e8d8bef9SDimitry Andric // - 128-byte signal set 2049e8d8bef9SDimitry Andric // - 8 bytes of padding because sigcontext has 16-byte alignment 2050e8d8bef9SDimitry Andric // - sigcontext/mcontext_t 2051e8d8bef9SDimitry Andric // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c 2052e8d8bef9SDimitry Andric const pint_t kOffsetSpToSigcontext = (128 + 8 + 8 + 24 + 128 + 8); // 304 2053e8d8bef9SDimitry Andric 2054e8d8bef9SDimitry Andric // Offsets from sigcontext to each register. 2055e8d8bef9SDimitry Andric const pint_t kOffsetGprs = 8; // offset to "__u64 regs[31]" field 2056e8d8bef9SDimitry Andric const pint_t kOffsetSp = 256; // offset to "__u64 sp" field 2057e8d8bef9SDimitry Andric const pint_t kOffsetPc = 264; // offset to "__u64 pc" field 2058e8d8bef9SDimitry Andric 2059e8d8bef9SDimitry Andric pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext; 2060e8d8bef9SDimitry Andric 2061e8d8bef9SDimitry Andric for (int i = 0; i <= 30; ++i) { 2062e8d8bef9SDimitry Andric uint64_t value = _addressSpace.get64(sigctx + kOffsetGprs + 2063e8d8bef9SDimitry Andric static_cast<pint_t>(i * 8)); 2064e8d8bef9SDimitry Andric _registers.setRegister(UNW_ARM64_X0 + i, value); 2065e8d8bef9SDimitry Andric } 2066e8d8bef9SDimitry Andric _registers.setSP(_addressSpace.get64(sigctx + kOffsetSp)); 2067e8d8bef9SDimitry Andric _registers.setIP(_addressSpace.get64(sigctx + kOffsetPc)); 2068e8d8bef9SDimitry Andric _isSignalFrame = true; 2069e8d8bef9SDimitry Andric return UNW_STEP_SUCCESS; 2070e8d8bef9SDimitry Andric } 2071e8d8bef9SDimitry Andric #endif // defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64) 2072e8d8bef9SDimitry Andric 20730b57cec5SDimitry Andric template <typename A, typename R> 20740b57cec5SDimitry Andric int UnwindCursor<A, R>::step() { 20750b57cec5SDimitry Andric // Bottom of stack is defined is when unwind info cannot be found. 20760b57cec5SDimitry Andric if (_unwindInfoMissing) 20770b57cec5SDimitry Andric return UNW_STEP_END; 20780b57cec5SDimitry Andric 20790b57cec5SDimitry Andric // Use unwinding info to modify register set as if function returned. 20800b57cec5SDimitry Andric int result; 2081e8d8bef9SDimitry Andric #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64) 2082e8d8bef9SDimitry Andric if (_isSigReturn) { 2083e8d8bef9SDimitry Andric result = this->stepThroughSigReturn(); 2084e8d8bef9SDimitry Andric } else 2085e8d8bef9SDimitry Andric #endif 2086e8d8bef9SDimitry Andric { 20870b57cec5SDimitry Andric #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 20880b57cec5SDimitry Andric result = this->stepWithCompactEncoding(); 20890b57cec5SDimitry Andric #elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 20900b57cec5SDimitry Andric result = this->stepWithSEHData(); 20910b57cec5SDimitry Andric #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 20920b57cec5SDimitry Andric result = this->stepWithDwarfFDE(); 20930b57cec5SDimitry Andric #elif defined(_LIBUNWIND_ARM_EHABI) 20940b57cec5SDimitry Andric result = this->stepWithEHABI(); 20950b57cec5SDimitry Andric #else 20960b57cec5SDimitry Andric #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \ 20970b57cec5SDimitry Andric _LIBUNWIND_SUPPORT_SEH_UNWIND or \ 20980b57cec5SDimitry Andric _LIBUNWIND_SUPPORT_DWARF_UNWIND or \ 20990b57cec5SDimitry Andric _LIBUNWIND_ARM_EHABI 21000b57cec5SDimitry Andric #endif 2101e8d8bef9SDimitry Andric } 21020b57cec5SDimitry Andric 21030b57cec5SDimitry Andric // update info based on new PC 21040b57cec5SDimitry Andric if (result == UNW_STEP_SUCCESS) { 21050b57cec5SDimitry Andric this->setInfoBasedOnIPRegister(true); 21060b57cec5SDimitry Andric if (_unwindInfoMissing) 21070b57cec5SDimitry Andric return UNW_STEP_END; 21080b57cec5SDimitry Andric } 21090b57cec5SDimitry Andric 21100b57cec5SDimitry Andric return result; 21110b57cec5SDimitry Andric } 21120b57cec5SDimitry Andric 21130b57cec5SDimitry Andric template <typename A, typename R> 21140b57cec5SDimitry Andric void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) { 2115c2c6a179SDimitry Andric if (_unwindInfoMissing) 2116c2c6a179SDimitry Andric memset(info, 0, sizeof(*info)); 2117c2c6a179SDimitry Andric else 21180b57cec5SDimitry Andric *info = _info; 21190b57cec5SDimitry Andric } 21200b57cec5SDimitry Andric 21210b57cec5SDimitry Andric template <typename A, typename R> 21220b57cec5SDimitry Andric bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen, 21230b57cec5SDimitry Andric unw_word_t *offset) { 21240b57cec5SDimitry Andric return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP), 21250b57cec5SDimitry Andric buf, bufLen, offset); 21260b57cec5SDimitry Andric } 21270b57cec5SDimitry Andric 21280b57cec5SDimitry Andric } // namespace libunwind 21290b57cec5SDimitry Andric 21300b57cec5SDimitry Andric #endif // __UNWINDCURSOR_HPP__ 2131