Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1 | //===------------------------- UnwindCursor.hpp ---------------------------===// |
| 2 | // |
Chandler Carruth | 61860a5 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 6 | // |
| 7 | // |
Ed Maste | 6f72338 | 2016-08-30 13:08:21 +0000 | [diff] [blame] | 8 | // C++ interface to lower levels of libunwind |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef __UNWINDCURSOR_HPP__ |
| 12 | #define __UNWINDCURSOR_HPP__ |
| 13 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 17 | #include <unwind.h> |
| 18 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 19 | #ifdef _WIN32 |
| 20 | #include <windows.h> |
| 21 | #include <ntverp.h> |
| 22 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 23 | #ifdef __APPLE__ |
| 24 | #include <mach-o/dyld.h> |
| 25 | #endif |
| 26 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 27 | #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 28 | // Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and |
| 29 | // earlier) SDKs. |
| 30 | // MinGW-w64 has always provided this struct. |
| 31 | #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \ |
| 32 | !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000 |
| 33 | struct _DISPATCHER_CONTEXT { |
| 34 | ULONG64 ControlPc; |
| 35 | ULONG64 ImageBase; |
| 36 | PRUNTIME_FUNCTION FunctionEntry; |
| 37 | ULONG64 EstablisherFrame; |
| 38 | ULONG64 TargetIp; |
| 39 | PCONTEXT ContextRecord; |
| 40 | PEXCEPTION_ROUTINE LanguageHandler; |
| 41 | PVOID HandlerData; |
| 42 | PUNWIND_HISTORY_TABLE HistoryTable; |
| 43 | ULONG ScopeIndex; |
| 44 | ULONG Fill0; |
| 45 | }; |
| 46 | #endif |
| 47 | |
| 48 | struct UNWIND_INFO { |
| 49 | uint8_t Version : 3; |
| 50 | uint8_t Flags : 5; |
| 51 | uint8_t SizeOfProlog; |
| 52 | uint8_t CountOfCodes; |
| 53 | uint8_t FrameRegister : 4; |
| 54 | uint8_t FrameOffset : 4; |
| 55 | uint16_t UnwindCodes[2]; |
| 56 | }; |
| 57 | |
| 58 | extern "C" _Unwind_Reason_Code __libunwind_seh_personality( |
| 59 | int, _Unwind_Action, uint64_t, _Unwind_Exception *, |
| 60 | struct _Unwind_Context *); |
| 61 | |
| 62 | #endif |
| 63 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 64 | #include "config.h" |
| 65 | |
| 66 | #include "AddressSpace.hpp" |
| 67 | #include "CompactUnwinder.hpp" |
| 68 | #include "config.h" |
| 69 | #include "DwarfInstructions.hpp" |
| 70 | #include "EHHeaderParser.hpp" |
| 71 | #include "libunwind.h" |
| 72 | #include "Registers.hpp" |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 73 | #include "RWMutex.hpp" |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 74 | #include "Unwind-EHABI.h" |
| 75 | |
| 76 | namespace libunwind { |
| 77 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 78 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 79 | /// Cache of recently found FDEs. |
| 80 | template <typename A> |
| 81 | class _LIBUNWIND_HIDDEN DwarfFDECache { |
| 82 | typedef typename A::pint_t pint_t; |
| 83 | public: |
| 84 | static pint_t findFDE(pint_t mh, pint_t pc); |
| 85 | static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde); |
| 86 | static void removeAllIn(pint_t mh); |
| 87 | static void iterateCacheEntries(void (*func)(unw_word_t ip_start, |
| 88 | unw_word_t ip_end, |
| 89 | unw_word_t fde, unw_word_t mh)); |
| 90 | |
| 91 | private: |
| 92 | |
| 93 | struct entry { |
| 94 | pint_t mh; |
| 95 | pint_t ip_start; |
| 96 | pint_t ip_end; |
| 97 | pint_t fde; |
| 98 | }; |
| 99 | |
| 100 | // These fields are all static to avoid needing an initializer. |
| 101 | // There is only one instance of this class per process. |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 102 | static RWMutex _lock; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 103 | #ifdef __APPLE__ |
| 104 | static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide); |
| 105 | static bool _registeredForDyldUnloads; |
| 106 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 107 | static entry *_buffer; |
| 108 | static entry *_bufferUsed; |
| 109 | static entry *_bufferEnd; |
| 110 | static entry _initialBuffer[64]; |
| 111 | }; |
| 112 | |
| 113 | template <typename A> |
| 114 | typename DwarfFDECache<A>::entry * |
| 115 | DwarfFDECache<A>::_buffer = _initialBuffer; |
| 116 | |
| 117 | template <typename A> |
| 118 | typename DwarfFDECache<A>::entry * |
| 119 | DwarfFDECache<A>::_bufferUsed = _initialBuffer; |
| 120 | |
| 121 | template <typename A> |
| 122 | typename DwarfFDECache<A>::entry * |
| 123 | DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64]; |
| 124 | |
| 125 | template <typename A> |
| 126 | typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64]; |
| 127 | |
| 128 | template <typename A> |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 129 | RWMutex DwarfFDECache<A>::_lock; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 130 | |
| 131 | #ifdef __APPLE__ |
| 132 | template <typename A> |
| 133 | bool DwarfFDECache<A>::_registeredForDyldUnloads = false; |
| 134 | #endif |
| 135 | |
| 136 | template <typename A> |
| 137 | typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) { |
| 138 | pint_t result = 0; |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 139 | _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 140 | for (entry *p = _buffer; p < _bufferUsed; ++p) { |
| 141 | if ((mh == p->mh) || (mh == 0)) { |
| 142 | if ((p->ip_start <= pc) && (pc < p->ip_end)) { |
| 143 | result = p->fde; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 148 | _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 149 | return result; |
| 150 | } |
| 151 | |
| 152 | template <typename A> |
| 153 | void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end, |
| 154 | pint_t fde) { |
Peter Zotov | 0717a2e | 2015-11-09 06:57:29 +0000 | [diff] [blame] | 155 | #if !defined(_LIBUNWIND_NO_HEAP) |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 156 | _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 157 | if (_bufferUsed >= _bufferEnd) { |
| 158 | size_t oldSize = (size_t)(_bufferEnd - _buffer); |
| 159 | size_t newSize = oldSize * 4; |
| 160 | // Can't use operator new (we are below it). |
| 161 | entry *newBuffer = (entry *)malloc(newSize * sizeof(entry)); |
| 162 | memcpy(newBuffer, _buffer, oldSize * sizeof(entry)); |
| 163 | if (_buffer != _initialBuffer) |
| 164 | free(_buffer); |
| 165 | _buffer = newBuffer; |
| 166 | _bufferUsed = &newBuffer[oldSize]; |
| 167 | _bufferEnd = &newBuffer[newSize]; |
| 168 | } |
| 169 | _bufferUsed->mh = mh; |
| 170 | _bufferUsed->ip_start = ip_start; |
| 171 | _bufferUsed->ip_end = ip_end; |
| 172 | _bufferUsed->fde = fde; |
| 173 | ++_bufferUsed; |
| 174 | #ifdef __APPLE__ |
| 175 | if (!_registeredForDyldUnloads) { |
| 176 | _dyld_register_func_for_remove_image(&dyldUnloadHook); |
| 177 | _registeredForDyldUnloads = true; |
| 178 | } |
| 179 | #endif |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 180 | _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); |
Peter Zotov | 0717a2e | 2015-11-09 06:57:29 +0000 | [diff] [blame] | 181 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | template <typename A> |
| 185 | void DwarfFDECache<A>::removeAllIn(pint_t mh) { |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 186 | _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 187 | entry *d = _buffer; |
| 188 | for (const entry *s = _buffer; s < _bufferUsed; ++s) { |
| 189 | if (s->mh != mh) { |
| 190 | if (d != s) |
| 191 | *d = *s; |
| 192 | ++d; |
| 193 | } |
| 194 | } |
| 195 | _bufferUsed = d; |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 196 | _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | #ifdef __APPLE__ |
| 200 | template <typename A> |
| 201 | void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) { |
| 202 | removeAllIn((pint_t) mh); |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | template <typename A> |
| 207 | void DwarfFDECache<A>::iterateCacheEntries(void (*func)( |
| 208 | unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) { |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 209 | _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 210 | for (entry *p = _buffer; p < _bufferUsed; ++p) { |
| 211 | (*func)(p->ip_start, p->ip_end, p->fde, p->mh); |
| 212 | } |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 213 | _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 214 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 215 | #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 216 | |
| 217 | |
| 218 | #define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field)) |
| 219 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 220 | #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 221 | template <typename A> class UnwindSectionHeader { |
| 222 | public: |
| 223 | UnwindSectionHeader(A &addressSpace, typename A::pint_t addr) |
| 224 | : _addressSpace(addressSpace), _addr(addr) {} |
| 225 | |
| 226 | uint32_t version() const { |
| 227 | return _addressSpace.get32(_addr + |
| 228 | offsetof(unwind_info_section_header, version)); |
| 229 | } |
| 230 | uint32_t commonEncodingsArraySectionOffset() const { |
| 231 | return _addressSpace.get32(_addr + |
| 232 | offsetof(unwind_info_section_header, |
| 233 | commonEncodingsArraySectionOffset)); |
| 234 | } |
| 235 | uint32_t commonEncodingsArrayCount() const { |
| 236 | return _addressSpace.get32(_addr + offsetof(unwind_info_section_header, |
| 237 | commonEncodingsArrayCount)); |
| 238 | } |
| 239 | uint32_t personalityArraySectionOffset() const { |
| 240 | return _addressSpace.get32(_addr + offsetof(unwind_info_section_header, |
| 241 | personalityArraySectionOffset)); |
| 242 | } |
| 243 | uint32_t personalityArrayCount() const { |
| 244 | return _addressSpace.get32( |
| 245 | _addr + offsetof(unwind_info_section_header, personalityArrayCount)); |
| 246 | } |
| 247 | uint32_t indexSectionOffset() const { |
| 248 | return _addressSpace.get32( |
| 249 | _addr + offsetof(unwind_info_section_header, indexSectionOffset)); |
| 250 | } |
| 251 | uint32_t indexCount() const { |
| 252 | return _addressSpace.get32( |
| 253 | _addr + offsetof(unwind_info_section_header, indexCount)); |
| 254 | } |
| 255 | |
| 256 | private: |
| 257 | A &_addressSpace; |
| 258 | typename A::pint_t _addr; |
| 259 | }; |
| 260 | |
| 261 | template <typename A> class UnwindSectionIndexArray { |
| 262 | public: |
| 263 | UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr) |
| 264 | : _addressSpace(addressSpace), _addr(addr) {} |
| 265 | |
| 266 | uint32_t functionOffset(uint32_t index) const { |
| 267 | return _addressSpace.get32( |
| 268 | _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, |
| 269 | functionOffset)); |
| 270 | } |
| 271 | uint32_t secondLevelPagesSectionOffset(uint32_t index) const { |
| 272 | return _addressSpace.get32( |
| 273 | _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, |
| 274 | secondLevelPagesSectionOffset)); |
| 275 | } |
| 276 | uint32_t lsdaIndexArraySectionOffset(uint32_t index) const { |
| 277 | return _addressSpace.get32( |
| 278 | _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, |
| 279 | lsdaIndexArraySectionOffset)); |
| 280 | } |
| 281 | |
| 282 | private: |
| 283 | A &_addressSpace; |
| 284 | typename A::pint_t _addr; |
| 285 | }; |
| 286 | |
| 287 | template <typename A> class UnwindSectionRegularPageHeader { |
| 288 | public: |
| 289 | UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr) |
| 290 | : _addressSpace(addressSpace), _addr(addr) {} |
| 291 | |
| 292 | uint32_t kind() const { |
| 293 | return _addressSpace.get32( |
| 294 | _addr + offsetof(unwind_info_regular_second_level_page_header, kind)); |
| 295 | } |
| 296 | uint16_t entryPageOffset() const { |
| 297 | return _addressSpace.get16( |
| 298 | _addr + offsetof(unwind_info_regular_second_level_page_header, |
| 299 | entryPageOffset)); |
| 300 | } |
| 301 | uint16_t entryCount() const { |
| 302 | return _addressSpace.get16( |
| 303 | _addr + |
| 304 | offsetof(unwind_info_regular_second_level_page_header, entryCount)); |
| 305 | } |
| 306 | |
| 307 | private: |
| 308 | A &_addressSpace; |
| 309 | typename A::pint_t _addr; |
| 310 | }; |
| 311 | |
| 312 | template <typename A> class UnwindSectionRegularArray { |
| 313 | public: |
| 314 | UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr) |
| 315 | : _addressSpace(addressSpace), _addr(addr) {} |
| 316 | |
| 317 | uint32_t functionOffset(uint32_t index) const { |
| 318 | return _addressSpace.get32( |
| 319 | _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index, |
| 320 | functionOffset)); |
| 321 | } |
| 322 | uint32_t encoding(uint32_t index) const { |
| 323 | return _addressSpace.get32( |
| 324 | _addr + |
| 325 | arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding)); |
| 326 | } |
| 327 | |
| 328 | private: |
| 329 | A &_addressSpace; |
| 330 | typename A::pint_t _addr; |
| 331 | }; |
| 332 | |
| 333 | template <typename A> class UnwindSectionCompressedPageHeader { |
| 334 | public: |
| 335 | UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr) |
| 336 | : _addressSpace(addressSpace), _addr(addr) {} |
| 337 | |
| 338 | uint32_t kind() const { |
| 339 | return _addressSpace.get32( |
| 340 | _addr + |
| 341 | offsetof(unwind_info_compressed_second_level_page_header, kind)); |
| 342 | } |
| 343 | uint16_t entryPageOffset() const { |
| 344 | return _addressSpace.get16( |
| 345 | _addr + offsetof(unwind_info_compressed_second_level_page_header, |
| 346 | entryPageOffset)); |
| 347 | } |
| 348 | uint16_t entryCount() const { |
| 349 | return _addressSpace.get16( |
| 350 | _addr + |
| 351 | offsetof(unwind_info_compressed_second_level_page_header, entryCount)); |
| 352 | } |
| 353 | uint16_t encodingsPageOffset() const { |
| 354 | return _addressSpace.get16( |
| 355 | _addr + offsetof(unwind_info_compressed_second_level_page_header, |
| 356 | encodingsPageOffset)); |
| 357 | } |
| 358 | uint16_t encodingsCount() const { |
| 359 | return _addressSpace.get16( |
| 360 | _addr + offsetof(unwind_info_compressed_second_level_page_header, |
| 361 | encodingsCount)); |
| 362 | } |
| 363 | |
| 364 | private: |
| 365 | A &_addressSpace; |
| 366 | typename A::pint_t _addr; |
| 367 | }; |
| 368 | |
| 369 | template <typename A> class UnwindSectionCompressedArray { |
| 370 | public: |
| 371 | UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr) |
| 372 | : _addressSpace(addressSpace), _addr(addr) {} |
| 373 | |
| 374 | uint32_t functionOffset(uint32_t index) const { |
| 375 | return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET( |
| 376 | _addressSpace.get32(_addr + index * sizeof(uint32_t))); |
| 377 | } |
| 378 | uint16_t encodingIndex(uint32_t index) const { |
| 379 | return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX( |
| 380 | _addressSpace.get32(_addr + index * sizeof(uint32_t))); |
| 381 | } |
| 382 | |
| 383 | private: |
| 384 | A &_addressSpace; |
| 385 | typename A::pint_t _addr; |
| 386 | }; |
| 387 | |
| 388 | template <typename A> class UnwindSectionLsdaArray { |
| 389 | public: |
| 390 | UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr) |
| 391 | : _addressSpace(addressSpace), _addr(addr) {} |
| 392 | |
| 393 | uint32_t functionOffset(uint32_t index) const { |
| 394 | return _addressSpace.get32( |
| 395 | _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry, |
| 396 | index, functionOffset)); |
| 397 | } |
| 398 | uint32_t lsdaOffset(uint32_t index) const { |
| 399 | return _addressSpace.get32( |
| 400 | _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry, |
| 401 | index, lsdaOffset)); |
| 402 | } |
| 403 | |
| 404 | private: |
| 405 | A &_addressSpace; |
| 406 | typename A::pint_t _addr; |
| 407 | }; |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 408 | #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 409 | |
| 410 | class _LIBUNWIND_HIDDEN AbstractUnwindCursor { |
| 411 | public: |
| 412 | // NOTE: provide a class specific placement deallocation function (S5.3.4 p20) |
| 413 | // This avoids an unnecessary dependency to libc++abi. |
| 414 | void operator delete(void *, size_t) {} |
| 415 | |
| 416 | virtual ~AbstractUnwindCursor() {} |
| 417 | virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); } |
| 418 | virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); } |
| 419 | virtual void setReg(int, unw_word_t) { |
| 420 | _LIBUNWIND_ABORT("setReg not implemented"); |
| 421 | } |
| 422 | virtual bool validFloatReg(int) { |
| 423 | _LIBUNWIND_ABORT("validFloatReg not implemented"); |
| 424 | } |
| 425 | virtual unw_fpreg_t getFloatReg(int) { |
| 426 | _LIBUNWIND_ABORT("getFloatReg not implemented"); |
| 427 | } |
| 428 | virtual void setFloatReg(int, unw_fpreg_t) { |
| 429 | _LIBUNWIND_ABORT("setFloatReg not implemented"); |
| 430 | } |
| 431 | virtual int step() { _LIBUNWIND_ABORT("step not implemented"); } |
| 432 | virtual void getInfo(unw_proc_info_t *) { |
| 433 | _LIBUNWIND_ABORT("getInfo not implemented"); |
| 434 | } |
| 435 | virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); } |
| 436 | virtual bool isSignalFrame() { |
| 437 | _LIBUNWIND_ABORT("isSignalFrame not implemented"); |
| 438 | } |
| 439 | virtual bool getFunctionName(char *, size_t, unw_word_t *) { |
| 440 | _LIBUNWIND_ABORT("getFunctionName not implemented"); |
| 441 | } |
| 442 | virtual void setInfoBasedOnIPRegister(bool = false) { |
| 443 | _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented"); |
| 444 | } |
| 445 | virtual const char *getRegisterName(int) { |
| 446 | _LIBUNWIND_ABORT("getRegisterName not implemented"); |
| 447 | } |
| 448 | #ifdef __arm__ |
| 449 | virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); } |
| 450 | #endif |
| 451 | }; |
| 452 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 453 | #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32) |
| 454 | |
| 455 | /// \c UnwindCursor contains all state (including all register values) during |
| 456 | /// an unwind. This is normally stack-allocated inside a unw_cursor_t. |
| 457 | template <typename A, typename R> |
| 458 | class UnwindCursor : public AbstractUnwindCursor { |
| 459 | typedef typename A::pint_t pint_t; |
| 460 | public: |
| 461 | UnwindCursor(unw_context_t *context, A &as); |
| 462 | UnwindCursor(CONTEXT *context, A &as); |
| 463 | UnwindCursor(A &as, void *threadArg); |
| 464 | virtual ~UnwindCursor() {} |
| 465 | virtual bool validReg(int); |
| 466 | virtual unw_word_t getReg(int); |
| 467 | virtual void setReg(int, unw_word_t); |
| 468 | virtual bool validFloatReg(int); |
| 469 | virtual unw_fpreg_t getFloatReg(int); |
| 470 | virtual void setFloatReg(int, unw_fpreg_t); |
| 471 | virtual int step(); |
| 472 | virtual void getInfo(unw_proc_info_t *); |
| 473 | virtual void jumpto(); |
| 474 | virtual bool isSignalFrame(); |
| 475 | virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off); |
| 476 | virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false); |
| 477 | virtual const char *getRegisterName(int num); |
| 478 | #ifdef __arm__ |
| 479 | virtual void saveVFPAsX(); |
| 480 | #endif |
| 481 | |
| 482 | DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; } |
| 483 | void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; } |
| 484 | |
Martin Storsjo | 5f5036e | 2019-02-03 22:16:53 +0000 | [diff] [blame] | 485 | // libunwind does not and should not depend on C++ library which means that we |
| 486 | // need our own defition of inline placement new. |
| 487 | static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; } |
| 488 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 489 | private: |
| 490 | |
| 491 | pint_t getLastPC() const { return _dispContext.ControlPc; } |
| 492 | void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; } |
| 493 | RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) { |
| 494 | _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc, |
| 495 | &_dispContext.ImageBase, |
| 496 | _dispContext.HistoryTable); |
| 497 | *base = _dispContext.ImageBase; |
| 498 | return _dispContext.FunctionEntry; |
| 499 | } |
| 500 | bool getInfoFromSEH(pint_t pc); |
| 501 | int stepWithSEHData() { |
| 502 | _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER, |
| 503 | _dispContext.ImageBase, |
| 504 | _dispContext.ControlPc, |
| 505 | _dispContext.FunctionEntry, |
| 506 | _dispContext.ContextRecord, |
| 507 | &_dispContext.HandlerData, |
| 508 | &_dispContext.EstablisherFrame, |
| 509 | NULL); |
| 510 | // Update some fields of the unwind info now, since we have them. |
| 511 | _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData); |
| 512 | if (_dispContext.LanguageHandler) { |
| 513 | _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality); |
| 514 | } else |
| 515 | _info.handler = 0; |
| 516 | return UNW_STEP_SUCCESS; |
| 517 | } |
| 518 | |
| 519 | A &_addressSpace; |
| 520 | unw_proc_info_t _info; |
| 521 | DISPATCHER_CONTEXT _dispContext; |
| 522 | CONTEXT _msContext; |
| 523 | UNWIND_HISTORY_TABLE _histTable; |
| 524 | bool _unwindInfoMissing; |
| 525 | }; |
| 526 | |
| 527 | |
| 528 | template <typename A, typename R> |
| 529 | UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as) |
| 530 | : _addressSpace(as), _unwindInfoMissing(false) { |
| 531 | static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), |
| 532 | "UnwindCursor<> does not fit in unw_cursor_t"); |
Martin Storsjö | 1c0575e | 2020-08-17 22:41:58 +0300 | [diff] [blame] | 533 | static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)), |
| 534 | "UnwindCursor<> requires more alignment than unw_cursor_t"); |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 535 | memset(&_info, 0, sizeof(_info)); |
| 536 | memset(&_histTable, 0, sizeof(_histTable)); |
| 537 | _dispContext.ContextRecord = &_msContext; |
| 538 | _dispContext.HistoryTable = &_histTable; |
| 539 | // Initialize MS context from ours. |
| 540 | R r(context); |
| 541 | _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT; |
| 542 | #if defined(_LIBUNWIND_TARGET_X86_64) |
| 543 | _msContext.Rax = r.getRegister(UNW_X86_64_RAX); |
| 544 | _msContext.Rcx = r.getRegister(UNW_X86_64_RCX); |
| 545 | _msContext.Rdx = r.getRegister(UNW_X86_64_RDX); |
| 546 | _msContext.Rbx = r.getRegister(UNW_X86_64_RBX); |
| 547 | _msContext.Rsp = r.getRegister(UNW_X86_64_RSP); |
| 548 | _msContext.Rbp = r.getRegister(UNW_X86_64_RBP); |
| 549 | _msContext.Rsi = r.getRegister(UNW_X86_64_RSI); |
| 550 | _msContext.Rdi = r.getRegister(UNW_X86_64_RDI); |
| 551 | _msContext.R8 = r.getRegister(UNW_X86_64_R8); |
| 552 | _msContext.R9 = r.getRegister(UNW_X86_64_R9); |
| 553 | _msContext.R10 = r.getRegister(UNW_X86_64_R10); |
| 554 | _msContext.R11 = r.getRegister(UNW_X86_64_R11); |
| 555 | _msContext.R12 = r.getRegister(UNW_X86_64_R12); |
| 556 | _msContext.R13 = r.getRegister(UNW_X86_64_R13); |
| 557 | _msContext.R14 = r.getRegister(UNW_X86_64_R14); |
| 558 | _msContext.R15 = r.getRegister(UNW_X86_64_R15); |
| 559 | _msContext.Rip = r.getRegister(UNW_REG_IP); |
| 560 | union { |
| 561 | v128 v; |
| 562 | M128A m; |
| 563 | } t; |
| 564 | t.v = r.getVectorRegister(UNW_X86_64_XMM0); |
| 565 | _msContext.Xmm0 = t.m; |
| 566 | t.v = r.getVectorRegister(UNW_X86_64_XMM1); |
| 567 | _msContext.Xmm1 = t.m; |
| 568 | t.v = r.getVectorRegister(UNW_X86_64_XMM2); |
| 569 | _msContext.Xmm2 = t.m; |
| 570 | t.v = r.getVectorRegister(UNW_X86_64_XMM3); |
| 571 | _msContext.Xmm3 = t.m; |
| 572 | t.v = r.getVectorRegister(UNW_X86_64_XMM4); |
| 573 | _msContext.Xmm4 = t.m; |
| 574 | t.v = r.getVectorRegister(UNW_X86_64_XMM5); |
| 575 | _msContext.Xmm5 = t.m; |
| 576 | t.v = r.getVectorRegister(UNW_X86_64_XMM6); |
| 577 | _msContext.Xmm6 = t.m; |
| 578 | t.v = r.getVectorRegister(UNW_X86_64_XMM7); |
| 579 | _msContext.Xmm7 = t.m; |
| 580 | t.v = r.getVectorRegister(UNW_X86_64_XMM8); |
| 581 | _msContext.Xmm8 = t.m; |
| 582 | t.v = r.getVectorRegister(UNW_X86_64_XMM9); |
| 583 | _msContext.Xmm9 = t.m; |
| 584 | t.v = r.getVectorRegister(UNW_X86_64_XMM10); |
| 585 | _msContext.Xmm10 = t.m; |
| 586 | t.v = r.getVectorRegister(UNW_X86_64_XMM11); |
| 587 | _msContext.Xmm11 = t.m; |
| 588 | t.v = r.getVectorRegister(UNW_X86_64_XMM12); |
| 589 | _msContext.Xmm12 = t.m; |
| 590 | t.v = r.getVectorRegister(UNW_X86_64_XMM13); |
| 591 | _msContext.Xmm13 = t.m; |
| 592 | t.v = r.getVectorRegister(UNW_X86_64_XMM14); |
| 593 | _msContext.Xmm14 = t.m; |
| 594 | t.v = r.getVectorRegister(UNW_X86_64_XMM15); |
| 595 | _msContext.Xmm15 = t.m; |
| 596 | #elif defined(_LIBUNWIND_TARGET_ARM) |
| 597 | _msContext.R0 = r.getRegister(UNW_ARM_R0); |
| 598 | _msContext.R1 = r.getRegister(UNW_ARM_R1); |
| 599 | _msContext.R2 = r.getRegister(UNW_ARM_R2); |
| 600 | _msContext.R3 = r.getRegister(UNW_ARM_R3); |
| 601 | _msContext.R4 = r.getRegister(UNW_ARM_R4); |
| 602 | _msContext.R5 = r.getRegister(UNW_ARM_R5); |
| 603 | _msContext.R6 = r.getRegister(UNW_ARM_R6); |
| 604 | _msContext.R7 = r.getRegister(UNW_ARM_R7); |
| 605 | _msContext.R8 = r.getRegister(UNW_ARM_R8); |
| 606 | _msContext.R9 = r.getRegister(UNW_ARM_R9); |
| 607 | _msContext.R10 = r.getRegister(UNW_ARM_R10); |
| 608 | _msContext.R11 = r.getRegister(UNW_ARM_R11); |
| 609 | _msContext.R12 = r.getRegister(UNW_ARM_R12); |
| 610 | _msContext.Sp = r.getRegister(UNW_ARM_SP); |
| 611 | _msContext.Lr = r.getRegister(UNW_ARM_LR); |
Martin Storsjo | e5dbce2 | 2018-08-31 14:56:55 +0000 | [diff] [blame] | 612 | _msContext.Pc = r.getRegister(UNW_ARM_IP); |
| 613 | for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) { |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 614 | union { |
| 615 | uint64_t w; |
| 616 | double d; |
| 617 | } d; |
Martin Storsjo | e5dbce2 | 2018-08-31 14:56:55 +0000 | [diff] [blame] | 618 | d.d = r.getFloatRegister(i); |
| 619 | _msContext.D[i - UNW_ARM_D0] = d.w; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 620 | } |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 621 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
| 622 | for (int i = UNW_ARM64_X0; i <= UNW_ARM64_X30; ++i) |
| 623 | _msContext.X[i - UNW_ARM64_X0] = r.getRegister(i); |
| 624 | _msContext.Sp = r.getRegister(UNW_REG_SP); |
| 625 | _msContext.Pc = r.getRegister(UNW_REG_IP); |
| 626 | for (int i = UNW_ARM64_D0; i <= UNW_ARM64_D31; ++i) |
| 627 | _msContext.V[i - UNW_ARM64_D0].D[0] = r.getFloatRegister(i); |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 628 | #endif |
| 629 | } |
| 630 | |
| 631 | template <typename A, typename R> |
| 632 | UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as) |
| 633 | : _addressSpace(as), _unwindInfoMissing(false) { |
| 634 | static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), |
| 635 | "UnwindCursor<> does not fit in unw_cursor_t"); |
| 636 | memset(&_info, 0, sizeof(_info)); |
| 637 | memset(&_histTable, 0, sizeof(_histTable)); |
| 638 | _dispContext.ContextRecord = &_msContext; |
| 639 | _dispContext.HistoryTable = &_histTable; |
| 640 | _msContext = *context; |
| 641 | } |
| 642 | |
| 643 | |
| 644 | template <typename A, typename R> |
| 645 | bool UnwindCursor<A, R>::validReg(int regNum) { |
| 646 | if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true; |
| 647 | #if defined(_LIBUNWIND_TARGET_X86_64) |
| 648 | if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true; |
| 649 | #elif defined(_LIBUNWIND_TARGET_ARM) |
| 650 | if (regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) return true; |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 651 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
| 652 | if (regNum >= UNW_ARM64_X0 && regNum <= UNW_ARM64_X30) return true; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 653 | #endif |
| 654 | return false; |
| 655 | } |
| 656 | |
| 657 | template <typename A, typename R> |
| 658 | unw_word_t UnwindCursor<A, R>::getReg(int regNum) { |
| 659 | switch (regNum) { |
| 660 | #if defined(_LIBUNWIND_TARGET_X86_64) |
| 661 | case UNW_REG_IP: return _msContext.Rip; |
| 662 | case UNW_X86_64_RAX: return _msContext.Rax; |
| 663 | case UNW_X86_64_RDX: return _msContext.Rdx; |
| 664 | case UNW_X86_64_RCX: return _msContext.Rcx; |
| 665 | case UNW_X86_64_RBX: return _msContext.Rbx; |
| 666 | case UNW_REG_SP: |
| 667 | case UNW_X86_64_RSP: return _msContext.Rsp; |
| 668 | case UNW_X86_64_RBP: return _msContext.Rbp; |
| 669 | case UNW_X86_64_RSI: return _msContext.Rsi; |
| 670 | case UNW_X86_64_RDI: return _msContext.Rdi; |
| 671 | case UNW_X86_64_R8: return _msContext.R8; |
| 672 | case UNW_X86_64_R9: return _msContext.R9; |
| 673 | case UNW_X86_64_R10: return _msContext.R10; |
| 674 | case UNW_X86_64_R11: return _msContext.R11; |
| 675 | case UNW_X86_64_R12: return _msContext.R12; |
| 676 | case UNW_X86_64_R13: return _msContext.R13; |
| 677 | case UNW_X86_64_R14: return _msContext.R14; |
| 678 | case UNW_X86_64_R15: return _msContext.R15; |
| 679 | #elif defined(_LIBUNWIND_TARGET_ARM) |
| 680 | case UNW_ARM_R0: return _msContext.R0; |
| 681 | case UNW_ARM_R1: return _msContext.R1; |
| 682 | case UNW_ARM_R2: return _msContext.R2; |
| 683 | case UNW_ARM_R3: return _msContext.R3; |
| 684 | case UNW_ARM_R4: return _msContext.R4; |
| 685 | case UNW_ARM_R5: return _msContext.R5; |
| 686 | case UNW_ARM_R6: return _msContext.R6; |
| 687 | case UNW_ARM_R7: return _msContext.R7; |
| 688 | case UNW_ARM_R8: return _msContext.R8; |
| 689 | case UNW_ARM_R9: return _msContext.R9; |
| 690 | case UNW_ARM_R10: return _msContext.R10; |
| 691 | case UNW_ARM_R11: return _msContext.R11; |
| 692 | case UNW_ARM_R12: return _msContext.R12; |
| 693 | case UNW_REG_SP: |
| 694 | case UNW_ARM_SP: return _msContext.Sp; |
| 695 | case UNW_ARM_LR: return _msContext.Lr; |
| 696 | case UNW_REG_IP: |
Martin Storsjo | e5dbce2 | 2018-08-31 14:56:55 +0000 | [diff] [blame] | 697 | case UNW_ARM_IP: return _msContext.Pc; |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 698 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
| 699 | case UNW_REG_SP: return _msContext.Sp; |
| 700 | case UNW_REG_IP: return _msContext.Pc; |
| 701 | default: return _msContext.X[regNum - UNW_ARM64_X0]; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 702 | #endif |
| 703 | } |
| 704 | _LIBUNWIND_ABORT("unsupported register"); |
| 705 | } |
| 706 | |
| 707 | template <typename A, typename R> |
| 708 | void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) { |
| 709 | switch (regNum) { |
| 710 | #if defined(_LIBUNWIND_TARGET_X86_64) |
| 711 | case UNW_REG_IP: _msContext.Rip = value; break; |
| 712 | case UNW_X86_64_RAX: _msContext.Rax = value; break; |
| 713 | case UNW_X86_64_RDX: _msContext.Rdx = value; break; |
| 714 | case UNW_X86_64_RCX: _msContext.Rcx = value; break; |
| 715 | case UNW_X86_64_RBX: _msContext.Rbx = value; break; |
| 716 | case UNW_REG_SP: |
| 717 | case UNW_X86_64_RSP: _msContext.Rsp = value; break; |
| 718 | case UNW_X86_64_RBP: _msContext.Rbp = value; break; |
| 719 | case UNW_X86_64_RSI: _msContext.Rsi = value; break; |
| 720 | case UNW_X86_64_RDI: _msContext.Rdi = value; break; |
| 721 | case UNW_X86_64_R8: _msContext.R8 = value; break; |
| 722 | case UNW_X86_64_R9: _msContext.R9 = value; break; |
| 723 | case UNW_X86_64_R10: _msContext.R10 = value; break; |
| 724 | case UNW_X86_64_R11: _msContext.R11 = value; break; |
| 725 | case UNW_X86_64_R12: _msContext.R12 = value; break; |
| 726 | case UNW_X86_64_R13: _msContext.R13 = value; break; |
| 727 | case UNW_X86_64_R14: _msContext.R14 = value; break; |
| 728 | case UNW_X86_64_R15: _msContext.R15 = value; break; |
| 729 | #elif defined(_LIBUNWIND_TARGET_ARM) |
| 730 | case UNW_ARM_R0: _msContext.R0 = value; break; |
| 731 | case UNW_ARM_R1: _msContext.R1 = value; break; |
| 732 | case UNW_ARM_R2: _msContext.R2 = value; break; |
| 733 | case UNW_ARM_R3: _msContext.R3 = value; break; |
| 734 | case UNW_ARM_R4: _msContext.R4 = value; break; |
| 735 | case UNW_ARM_R5: _msContext.R5 = value; break; |
| 736 | case UNW_ARM_R6: _msContext.R6 = value; break; |
| 737 | case UNW_ARM_R7: _msContext.R7 = value; break; |
| 738 | case UNW_ARM_R8: _msContext.R8 = value; break; |
| 739 | case UNW_ARM_R9: _msContext.R9 = value; break; |
| 740 | case UNW_ARM_R10: _msContext.R10 = value; break; |
| 741 | case UNW_ARM_R11: _msContext.R11 = value; break; |
| 742 | case UNW_ARM_R12: _msContext.R12 = value; break; |
| 743 | case UNW_REG_SP: |
| 744 | case UNW_ARM_SP: _msContext.Sp = value; break; |
| 745 | case UNW_ARM_LR: _msContext.Lr = value; break; |
| 746 | case UNW_REG_IP: |
Martin Storsjo | e5dbce2 | 2018-08-31 14:56:55 +0000 | [diff] [blame] | 747 | case UNW_ARM_IP: _msContext.Pc = value; break; |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 748 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
| 749 | case UNW_REG_SP: _msContext.Sp = value; break; |
| 750 | case UNW_REG_IP: _msContext.Pc = value; break; |
| 751 | case UNW_ARM64_X0: |
| 752 | case UNW_ARM64_X1: |
| 753 | case UNW_ARM64_X2: |
| 754 | case UNW_ARM64_X3: |
| 755 | case UNW_ARM64_X4: |
| 756 | case UNW_ARM64_X5: |
| 757 | case UNW_ARM64_X6: |
| 758 | case UNW_ARM64_X7: |
| 759 | case UNW_ARM64_X8: |
| 760 | case UNW_ARM64_X9: |
| 761 | case UNW_ARM64_X10: |
| 762 | case UNW_ARM64_X11: |
| 763 | case UNW_ARM64_X12: |
| 764 | case UNW_ARM64_X13: |
| 765 | case UNW_ARM64_X14: |
| 766 | case UNW_ARM64_X15: |
| 767 | case UNW_ARM64_X16: |
| 768 | case UNW_ARM64_X17: |
| 769 | case UNW_ARM64_X18: |
| 770 | case UNW_ARM64_X19: |
| 771 | case UNW_ARM64_X20: |
| 772 | case UNW_ARM64_X21: |
| 773 | case UNW_ARM64_X22: |
| 774 | case UNW_ARM64_X23: |
| 775 | case UNW_ARM64_X24: |
| 776 | case UNW_ARM64_X25: |
| 777 | case UNW_ARM64_X26: |
| 778 | case UNW_ARM64_X27: |
| 779 | case UNW_ARM64_X28: |
| 780 | case UNW_ARM64_FP: |
| 781 | case UNW_ARM64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 782 | #endif |
| 783 | default: |
| 784 | _LIBUNWIND_ABORT("unsupported register"); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | template <typename A, typename R> |
| 789 | bool UnwindCursor<A, R>::validFloatReg(int regNum) { |
| 790 | #if defined(_LIBUNWIND_TARGET_ARM) |
| 791 | if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true; |
| 792 | if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true; |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 793 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
| 794 | if (regNum >= UNW_ARM64_D0 && regNum <= UNW_ARM64_D31) return true; |
Martin Storsjo | 059a163 | 2019-01-22 22:12:23 +0000 | [diff] [blame] | 795 | #else |
| 796 | (void)regNum; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 797 | #endif |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | template <typename A, typename R> |
| 802 | unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) { |
| 803 | #if defined(_LIBUNWIND_TARGET_ARM) |
| 804 | if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) { |
| 805 | union { |
| 806 | uint32_t w; |
| 807 | float f; |
| 808 | } d; |
| 809 | d.w = _msContext.S[regNum - UNW_ARM_S0]; |
| 810 | return d.f; |
| 811 | } |
| 812 | if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) { |
| 813 | union { |
| 814 | uint64_t w; |
| 815 | double d; |
| 816 | } d; |
| 817 | d.w = _msContext.D[regNum - UNW_ARM_D0]; |
| 818 | return d.d; |
| 819 | } |
| 820 | _LIBUNWIND_ABORT("unsupported float register"); |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 821 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
| 822 | return _msContext.V[regNum - UNW_ARM64_D0].D[0]; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 823 | #else |
Martin Storsjo | 059a163 | 2019-01-22 22:12:23 +0000 | [diff] [blame] | 824 | (void)regNum; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 825 | _LIBUNWIND_ABORT("float registers unimplemented"); |
| 826 | #endif |
| 827 | } |
| 828 | |
| 829 | template <typename A, typename R> |
| 830 | void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) { |
| 831 | #if defined(_LIBUNWIND_TARGET_ARM) |
| 832 | if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) { |
| 833 | union { |
| 834 | uint32_t w; |
| 835 | float f; |
| 836 | } d; |
| 837 | d.f = value; |
| 838 | _msContext.S[regNum - UNW_ARM_S0] = d.w; |
| 839 | } |
| 840 | if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) { |
| 841 | union { |
| 842 | uint64_t w; |
| 843 | double d; |
| 844 | } d; |
| 845 | d.d = value; |
| 846 | _msContext.D[regNum - UNW_ARM_D0] = d.w; |
| 847 | } |
| 848 | _LIBUNWIND_ABORT("unsupported float register"); |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 849 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
| 850 | _msContext.V[regNum - UNW_ARM64_D0].D[0] = value; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 851 | #else |
Martin Storsjo | 059a163 | 2019-01-22 22:12:23 +0000 | [diff] [blame] | 852 | (void)regNum; |
| 853 | (void)value; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 854 | _LIBUNWIND_ABORT("float registers unimplemented"); |
| 855 | #endif |
| 856 | } |
| 857 | |
| 858 | template <typename A, typename R> void UnwindCursor<A, R>::jumpto() { |
| 859 | RtlRestoreContext(&_msContext, nullptr); |
| 860 | } |
| 861 | |
| 862 | #ifdef __arm__ |
| 863 | template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {} |
| 864 | #endif |
| 865 | |
| 866 | template <typename A, typename R> |
| 867 | const char *UnwindCursor<A, R>::getRegisterName(int regNum) { |
Martin Storsjo | 43bb9f8 | 2018-12-12 22:24:42 +0000 | [diff] [blame] | 868 | return R::getRegisterName(regNum); |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() { |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | #else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32) |
| 876 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 877 | /// UnwindCursor contains all state (including all register values) during |
| 878 | /// an unwind. This is normally stack allocated inside a unw_cursor_t. |
| 879 | template <typename A, typename R> |
| 880 | class UnwindCursor : public AbstractUnwindCursor{ |
| 881 | typedef typename A::pint_t pint_t; |
| 882 | public: |
| 883 | UnwindCursor(unw_context_t *context, A &as); |
| 884 | UnwindCursor(A &as, void *threadArg); |
| 885 | virtual ~UnwindCursor() {} |
| 886 | virtual bool validReg(int); |
| 887 | virtual unw_word_t getReg(int); |
| 888 | virtual void setReg(int, unw_word_t); |
| 889 | virtual bool validFloatReg(int); |
| 890 | virtual unw_fpreg_t getFloatReg(int); |
| 891 | virtual void setFloatReg(int, unw_fpreg_t); |
| 892 | virtual int step(); |
| 893 | virtual void getInfo(unw_proc_info_t *); |
| 894 | virtual void jumpto(); |
| 895 | virtual bool isSignalFrame(); |
| 896 | virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off); |
| 897 | virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false); |
| 898 | virtual const char *getRegisterName(int num); |
| 899 | #ifdef __arm__ |
| 900 | virtual void saveVFPAsX(); |
| 901 | #endif |
| 902 | |
Petr Hosek | 36f6154 | 2019-02-02 21:15:49 +0000 | [diff] [blame] | 903 | // libunwind does not and should not depend on C++ library which means that we |
| 904 | // need our own defition of inline placement new. |
| 905 | static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; } |
| 906 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 907 | private: |
| 908 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 909 | #if defined(_LIBUNWIND_ARM_EHABI) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 910 | bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections §s); |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 911 | |
| 912 | int stepWithEHABI() { |
| 913 | size_t len = 0; |
| 914 | size_t off = 0; |
| 915 | // FIXME: Calling decode_eht_entry() here is violating the libunwind |
| 916 | // abstraction layer. |
| 917 | const uint32_t *ehtp = |
| 918 | decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info), |
| 919 | &off, &len); |
| 920 | if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) != |
| 921 | _URC_CONTINUE_UNWIND) |
| 922 | return UNW_STEP_END; |
| 923 | return UNW_STEP_SUCCESS; |
| 924 | } |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 925 | #endif |
| 926 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 927 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 928 | bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo, |
| 929 | const typename CFI_Parser<A>::CIE_Info &cieInfo, |
| 930 | pint_t pc, uintptr_t dso_base); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 931 | bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections §s, |
| 932 | uint32_t fdeSectionOffsetHint=0); |
| 933 | int stepWithDwarfFDE() { |
| 934 | return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace, |
| 935 | (pint_t)this->getReg(UNW_REG_IP), |
| 936 | (pint_t)_info.unwind_info, |
Sterling Augustine | b6a6639 | 2019-10-31 12:45:20 -0700 | [diff] [blame] | 937 | _registers, _isSignalFrame); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 938 | } |
| 939 | #endif |
| 940 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 941 | #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 942 | bool getInfoFromCompactEncodingSection(pint_t pc, |
| 943 | const UnwindInfoSections §s); |
| 944 | int stepWithCompactEncoding() { |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 945 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 946 | if ( compactSaysUseDwarf() ) |
| 947 | return stepWithDwarfFDE(); |
| 948 | #endif |
| 949 | R dummy; |
| 950 | return stepWithCompactEncoding(dummy); |
| 951 | } |
| 952 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 953 | #if defined(_LIBUNWIND_TARGET_X86_64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 954 | int stepWithCompactEncoding(Registers_x86_64 &) { |
| 955 | return CompactUnwinder_x86_64<A>::stepWithCompactEncoding( |
| 956 | _info.format, _info.start_ip, _addressSpace, _registers); |
| 957 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 958 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 959 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 960 | #if defined(_LIBUNWIND_TARGET_I386) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 961 | int stepWithCompactEncoding(Registers_x86 &) { |
| 962 | return CompactUnwinder_x86<A>::stepWithCompactEncoding( |
| 963 | _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers); |
| 964 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 965 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 966 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 967 | #if defined(_LIBUNWIND_TARGET_PPC) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 968 | int stepWithCompactEncoding(Registers_ppc &) { |
| 969 | return UNW_EINVAL; |
| 970 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 971 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 972 | |
Martin Storsjo | 8338b0a | 2018-01-02 22:11:30 +0000 | [diff] [blame] | 973 | #if defined(_LIBUNWIND_TARGET_PPC64) |
| 974 | int stepWithCompactEncoding(Registers_ppc64 &) { |
| 975 | return UNW_EINVAL; |
| 976 | } |
| 977 | #endif |
| 978 | |
| 979 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 980 | #if defined(_LIBUNWIND_TARGET_AARCH64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 981 | int stepWithCompactEncoding(Registers_arm64 &) { |
| 982 | return CompactUnwinder_arm64<A>::stepWithCompactEncoding( |
| 983 | _info.format, _info.start_ip, _addressSpace, _registers); |
| 984 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 985 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 986 | |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 987 | #if defined(_LIBUNWIND_TARGET_MIPS_O32) |
| 988 | int stepWithCompactEncoding(Registers_mips_o32 &) { |
| 989 | return UNW_EINVAL; |
| 990 | } |
| 991 | #endif |
| 992 | |
John Baldwin | 541a435 | 2018-01-09 17:07:18 +0000 | [diff] [blame] | 993 | #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI) |
| 994 | int stepWithCompactEncoding(Registers_mips_newabi &) { |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 995 | return UNW_EINVAL; |
| 996 | } |
| 997 | #endif |
| 998 | |
Daniel Cederman | 9f2f07a | 2019-01-14 10:15:20 +0000 | [diff] [blame] | 999 | #if defined(_LIBUNWIND_TARGET_SPARC) |
| 1000 | int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; } |
| 1001 | #endif |
| 1002 | |
Sam Elliott | 81f7e17 | 2019-12-16 16:35:17 +0000 | [diff] [blame] | 1003 | #if defined (_LIBUNWIND_TARGET_RISCV) |
| 1004 | int stepWithCompactEncoding(Registers_riscv &) { |
| 1005 | return UNW_EINVAL; |
| 1006 | } |
| 1007 | #endif |
| 1008 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1009 | bool compactSaysUseDwarf(uint32_t *offset=NULL) const { |
| 1010 | R dummy; |
| 1011 | return compactSaysUseDwarf(dummy, offset); |
| 1012 | } |
| 1013 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1014 | #if defined(_LIBUNWIND_TARGET_X86_64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1015 | bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const { |
| 1016 | if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) { |
| 1017 | if (offset) |
| 1018 | *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET); |
| 1019 | return true; |
| 1020 | } |
| 1021 | return false; |
| 1022 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1023 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1024 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1025 | #if defined(_LIBUNWIND_TARGET_I386) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1026 | bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const { |
| 1027 | if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) { |
| 1028 | if (offset) |
| 1029 | *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET); |
| 1030 | return true; |
| 1031 | } |
| 1032 | return false; |
| 1033 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1034 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1035 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1036 | #if defined(_LIBUNWIND_TARGET_PPC) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1037 | bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const { |
| 1038 | return true; |
| 1039 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1040 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1041 | |
Martin Storsjo | 8338b0a | 2018-01-02 22:11:30 +0000 | [diff] [blame] | 1042 | #if defined(_LIBUNWIND_TARGET_PPC64) |
| 1043 | bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const { |
| 1044 | return true; |
| 1045 | } |
| 1046 | #endif |
| 1047 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1048 | #if defined(_LIBUNWIND_TARGET_AARCH64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1049 | bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const { |
| 1050 | if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) { |
| 1051 | if (offset) |
| 1052 | *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET); |
| 1053 | return true; |
| 1054 | } |
| 1055 | return false; |
| 1056 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1057 | #endif |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1058 | |
| 1059 | #if defined(_LIBUNWIND_TARGET_MIPS_O32) |
| 1060 | bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const { |
| 1061 | return true; |
| 1062 | } |
| 1063 | #endif |
| 1064 | |
John Baldwin | 541a435 | 2018-01-09 17:07:18 +0000 | [diff] [blame] | 1065 | #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI) |
| 1066 | bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const { |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1067 | return true; |
| 1068 | } |
| 1069 | #endif |
Daniel Cederman | 9f2f07a | 2019-01-14 10:15:20 +0000 | [diff] [blame] | 1070 | |
| 1071 | #if defined(_LIBUNWIND_TARGET_SPARC) |
| 1072 | bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; } |
| 1073 | #endif |
| 1074 | |
Sam Elliott | 81f7e17 | 2019-12-16 16:35:17 +0000 | [diff] [blame] | 1075 | #if defined (_LIBUNWIND_TARGET_RISCV) |
| 1076 | bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const { |
| 1077 | return true; |
| 1078 | } |
| 1079 | #endif |
| 1080 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1081 | #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1082 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1083 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1084 | compact_unwind_encoding_t dwarfEncoding() const { |
| 1085 | R dummy; |
| 1086 | return dwarfEncoding(dummy); |
| 1087 | } |
| 1088 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1089 | #if defined(_LIBUNWIND_TARGET_X86_64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1090 | compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const { |
| 1091 | return UNWIND_X86_64_MODE_DWARF; |
| 1092 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1093 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1094 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1095 | #if defined(_LIBUNWIND_TARGET_I386) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1096 | compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const { |
| 1097 | return UNWIND_X86_MODE_DWARF; |
| 1098 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1099 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1100 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1101 | #if defined(_LIBUNWIND_TARGET_PPC) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1102 | compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const { |
| 1103 | return 0; |
| 1104 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1105 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1106 | |
Martin Storsjo | 8338b0a | 2018-01-02 22:11:30 +0000 | [diff] [blame] | 1107 | #if defined(_LIBUNWIND_TARGET_PPC64) |
| 1108 | compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const { |
| 1109 | return 0; |
| 1110 | } |
| 1111 | #endif |
| 1112 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1113 | #if defined(_LIBUNWIND_TARGET_AARCH64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1114 | compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const { |
| 1115 | return UNWIND_ARM64_MODE_DWARF; |
| 1116 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1117 | #endif |
Peter Zotov | 8d63999 | 2015-08-31 05:26:37 +0000 | [diff] [blame] | 1118 | |
Martin Storsjo | a72285f | 2017-11-02 08:16:16 +0000 | [diff] [blame] | 1119 | #if defined(_LIBUNWIND_TARGET_ARM) |
| 1120 | compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const { |
| 1121 | return 0; |
| 1122 | } |
| 1123 | #endif |
| 1124 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1125 | #if defined (_LIBUNWIND_TARGET_OR1K) |
Peter Zotov | 8d63999 | 2015-08-31 05:26:37 +0000 | [diff] [blame] | 1126 | compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const { |
| 1127 | return 0; |
| 1128 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1129 | #endif |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1130 | |
Brian Cain | b432c47 | 2020-04-09 00:14:02 -0500 | [diff] [blame] | 1131 | #if defined (_LIBUNWIND_TARGET_HEXAGON) |
| 1132 | compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const { |
| 1133 | return 0; |
| 1134 | } |
| 1135 | #endif |
| 1136 | |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1137 | #if defined (_LIBUNWIND_TARGET_MIPS_O32) |
| 1138 | compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const { |
| 1139 | return 0; |
| 1140 | } |
| 1141 | #endif |
| 1142 | |
John Baldwin | 541a435 | 2018-01-09 17:07:18 +0000 | [diff] [blame] | 1143 | #if defined (_LIBUNWIND_TARGET_MIPS_NEWABI) |
| 1144 | compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const { |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1145 | return 0; |
| 1146 | } |
| 1147 | #endif |
Daniel Cederman | 9f2f07a | 2019-01-14 10:15:20 +0000 | [diff] [blame] | 1148 | |
| 1149 | #if defined(_LIBUNWIND_TARGET_SPARC) |
| 1150 | compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; } |
| 1151 | #endif |
| 1152 | |
Sam Elliott | 81f7e17 | 2019-12-16 16:35:17 +0000 | [diff] [blame] | 1153 | #if defined (_LIBUNWIND_TARGET_RISCV) |
| 1154 | compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const { |
| 1155 | return 0; |
| 1156 | } |
| 1157 | #endif |
| 1158 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1159 | #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1160 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 1161 | #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 1162 | // For runtime environments using SEH unwind data without Windows runtime |
| 1163 | // support. |
| 1164 | pint_t getLastPC() const { /* FIXME: Implement */ return 0; } |
| 1165 | void setLastPC(pint_t pc) { /* FIXME: Implement */ } |
| 1166 | RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) { |
| 1167 | /* FIXME: Implement */ |
| 1168 | *base = 0; |
| 1169 | return nullptr; |
| 1170 | } |
| 1171 | bool getInfoFromSEH(pint_t pc); |
| 1172 | int stepWithSEHData() { /* FIXME: Implement */ return 0; } |
| 1173 | #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 1174 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1175 | |
| 1176 | A &_addressSpace; |
| 1177 | R _registers; |
| 1178 | unw_proc_info_t _info; |
| 1179 | bool _unwindInfoMissing; |
| 1180 | bool _isSignalFrame; |
| 1181 | }; |
| 1182 | |
| 1183 | |
| 1184 | template <typename A, typename R> |
| 1185 | UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as) |
| 1186 | : _addressSpace(as), _registers(context), _unwindInfoMissing(false), |
| 1187 | _isSignalFrame(false) { |
Asiri Rathnayake | 74d3525 | 2016-05-26 21:45:54 +0000 | [diff] [blame] | 1188 | static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1189 | "UnwindCursor<> does not fit in unw_cursor_t"); |
Martin Storsjö | 1c0575e | 2020-08-17 22:41:58 +0300 | [diff] [blame] | 1190 | static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)), |
| 1191 | "UnwindCursor<> requires more alignment than unw_cursor_t"); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1192 | memset(&_info, 0, sizeof(_info)); |
| 1193 | } |
| 1194 | |
| 1195 | template <typename A, typename R> |
| 1196 | UnwindCursor<A, R>::UnwindCursor(A &as, void *) |
| 1197 | : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) { |
| 1198 | memset(&_info, 0, sizeof(_info)); |
| 1199 | // FIXME |
| 1200 | // fill in _registers from thread arg |
| 1201 | } |
| 1202 | |
| 1203 | |
| 1204 | template <typename A, typename R> |
| 1205 | bool UnwindCursor<A, R>::validReg(int regNum) { |
| 1206 | return _registers.validRegister(regNum); |
| 1207 | } |
| 1208 | |
| 1209 | template <typename A, typename R> |
| 1210 | unw_word_t UnwindCursor<A, R>::getReg(int regNum) { |
| 1211 | return _registers.getRegister(regNum); |
| 1212 | } |
| 1213 | |
| 1214 | template <typename A, typename R> |
| 1215 | void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) { |
| 1216 | _registers.setRegister(regNum, (typename A::pint_t)value); |
| 1217 | } |
| 1218 | |
| 1219 | template <typename A, typename R> |
| 1220 | bool UnwindCursor<A, R>::validFloatReg(int regNum) { |
| 1221 | return _registers.validFloatRegister(regNum); |
| 1222 | } |
| 1223 | |
| 1224 | template <typename A, typename R> |
| 1225 | unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) { |
| 1226 | return _registers.getFloatRegister(regNum); |
| 1227 | } |
| 1228 | |
| 1229 | template <typename A, typename R> |
| 1230 | void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) { |
| 1231 | _registers.setFloatRegister(regNum, value); |
| 1232 | } |
| 1233 | |
| 1234 | template <typename A, typename R> void UnwindCursor<A, R>::jumpto() { |
| 1235 | _registers.jumpto(); |
| 1236 | } |
| 1237 | |
| 1238 | #ifdef __arm__ |
| 1239 | template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() { |
| 1240 | _registers.saveVFPAsX(); |
| 1241 | } |
| 1242 | #endif |
| 1243 | |
| 1244 | template <typename A, typename R> |
| 1245 | const char *UnwindCursor<A, R>::getRegisterName(int regNum) { |
| 1246 | return _registers.getRegisterName(regNum); |
| 1247 | } |
| 1248 | |
| 1249 | template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() { |
| 1250 | return _isSignalFrame; |
| 1251 | } |
| 1252 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 1253 | #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 1254 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1255 | #if defined(_LIBUNWIND_ARM_EHABI) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1256 | template<typename A> |
| 1257 | struct EHABISectionIterator { |
| 1258 | typedef EHABISectionIterator _Self; |
| 1259 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1260 | typedef typename A::pint_t value_type; |
| 1261 | typedef typename A::pint_t* pointer; |
| 1262 | typedef typename A::pint_t& reference; |
| 1263 | typedef size_t size_type; |
| 1264 | typedef size_t difference_type; |
| 1265 | |
| 1266 | static _Self begin(A& addressSpace, const UnwindInfoSections& sects) { |
| 1267 | return _Self(addressSpace, sects, 0); |
| 1268 | } |
| 1269 | static _Self end(A& addressSpace, const UnwindInfoSections& sects) { |
Ed Schouten | 5d3f35b | 2017-03-07 15:21:57 +0000 | [diff] [blame] | 1270 | return _Self(addressSpace, sects, |
| 1271 | sects.arm_section_length / sizeof(EHABIIndexEntry)); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i) |
| 1275 | : _i(i), _addressSpace(&addressSpace), _sects(§s) {} |
| 1276 | |
| 1277 | _Self& operator++() { ++_i; return *this; } |
| 1278 | _Self& operator+=(size_t a) { _i += a; return *this; } |
| 1279 | _Self& operator--() { assert(_i > 0); --_i; return *this; } |
| 1280 | _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; } |
| 1281 | |
| 1282 | _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; } |
| 1283 | _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; } |
| 1284 | |
Saleem Abdulrasool | fbff6b1 | 2020-06-18 08:51:44 -0700 | [diff] [blame] | 1285 | size_t operator-(const _Self& other) const { return _i - other._i; } |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1286 | |
| 1287 | bool operator==(const _Self& other) const { |
| 1288 | assert(_addressSpace == other._addressSpace); |
| 1289 | assert(_sects == other._sects); |
| 1290 | return _i == other._i; |
| 1291 | } |
| 1292 | |
Saleem Abdulrasool | fbff6b1 | 2020-06-18 08:51:44 -0700 | [diff] [blame] | 1293 | bool operator!=(const _Self& other) const { |
| 1294 | assert(_addressSpace == other._addressSpace); |
| 1295 | assert(_sects == other._sects); |
| 1296 | return _i != other._i; |
| 1297 | } |
| 1298 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1299 | typename A::pint_t operator*() const { return functionAddress(); } |
| 1300 | |
| 1301 | typename A::pint_t functionAddress() const { |
| 1302 | typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( |
| 1303 | EHABIIndexEntry, _i, functionOffset); |
| 1304 | return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr)); |
| 1305 | } |
| 1306 | |
| 1307 | typename A::pint_t dataAddress() { |
| 1308 | typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( |
| 1309 | EHABIIndexEntry, _i, data); |
| 1310 | return indexAddr; |
| 1311 | } |
| 1312 | |
| 1313 | private: |
| 1314 | size_t _i; |
| 1315 | A* _addressSpace; |
| 1316 | const UnwindInfoSections* _sects; |
| 1317 | }; |
| 1318 | |
Petr Hosek | ac0d9e0 | 2019-01-29 22:26:18 +0000 | [diff] [blame] | 1319 | namespace { |
| 1320 | |
| 1321 | template <typename A> |
| 1322 | EHABISectionIterator<A> EHABISectionUpperBound( |
| 1323 | EHABISectionIterator<A> first, |
| 1324 | EHABISectionIterator<A> last, |
| 1325 | typename A::pint_t value) { |
| 1326 | size_t len = last - first; |
| 1327 | while (len > 0) { |
| 1328 | size_t l2 = len / 2; |
| 1329 | EHABISectionIterator<A> m = first + l2; |
| 1330 | if (value < *m) { |
| 1331 | len = l2; |
| 1332 | } else { |
| 1333 | first = ++m; |
| 1334 | len -= l2 + 1; |
| 1335 | } |
| 1336 | } |
| 1337 | return first; |
| 1338 | } |
| 1339 | |
| 1340 | } |
| 1341 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1342 | template <typename A, typename R> |
| 1343 | bool UnwindCursor<A, R>::getInfoFromEHABISection( |
| 1344 | pint_t pc, |
| 1345 | const UnwindInfoSections §s) { |
| 1346 | EHABISectionIterator<A> begin = |
| 1347 | EHABISectionIterator<A>::begin(_addressSpace, sects); |
| 1348 | EHABISectionIterator<A> end = |
| 1349 | EHABISectionIterator<A>::end(_addressSpace, sects); |
Momchil Velikov | 064d69a | 2017-07-24 09:19:32 +0000 | [diff] [blame] | 1350 | if (begin == end) |
| 1351 | return false; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1352 | |
Petr Hosek | ac0d9e0 | 2019-01-29 22:26:18 +0000 | [diff] [blame] | 1353 | EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc); |
Momchil Velikov | 064d69a | 2017-07-24 09:19:32 +0000 | [diff] [blame] | 1354 | if (itNextPC == begin) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1355 | return false; |
| 1356 | EHABISectionIterator<A> itThisPC = itNextPC - 1; |
| 1357 | |
| 1358 | pint_t thisPC = itThisPC.functionAddress(); |
Momchil Velikov | 064d69a | 2017-07-24 09:19:32 +0000 | [diff] [blame] | 1359 | // If an exception is thrown from a function, corresponding to the last entry |
| 1360 | // in the table, we don't really know the function extent and have to choose a |
| 1361 | // value for nextPC. Choosing max() will allow the range check during trace to |
| 1362 | // succeed. |
Petr Hosek | ac0d9e0 | 2019-01-29 22:26:18 +0000 | [diff] [blame] | 1363 | pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress(); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1364 | pint_t indexDataAddr = itThisPC.dataAddress(); |
| 1365 | |
| 1366 | if (indexDataAddr == 0) |
| 1367 | return false; |
| 1368 | |
| 1369 | uint32_t indexData = _addressSpace.get32(indexDataAddr); |
| 1370 | if (indexData == UNW_EXIDX_CANTUNWIND) |
| 1371 | return false; |
| 1372 | |
| 1373 | // If the high bit is set, the exception handling table entry is inline inside |
| 1374 | // the index table entry on the second word (aka |indexDataAddr|). Otherwise, |
Nico Weber | d999d54 | 2020-02-03 14:16:52 -0500 | [diff] [blame] | 1375 | // the table points at an offset in the exception handling table (section 5 |
| 1376 | // EHABI). |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1377 | pint_t exceptionTableAddr; |
| 1378 | uint32_t exceptionTableData; |
| 1379 | bool isSingleWordEHT; |
| 1380 | if (indexData & 0x80000000) { |
| 1381 | exceptionTableAddr = indexDataAddr; |
| 1382 | // TODO(ajwong): Should this data be 0? |
| 1383 | exceptionTableData = indexData; |
| 1384 | isSingleWordEHT = true; |
| 1385 | } else { |
| 1386 | exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData); |
| 1387 | exceptionTableData = _addressSpace.get32(exceptionTableAddr); |
| 1388 | isSingleWordEHT = false; |
| 1389 | } |
| 1390 | |
| 1391 | // Now we know the 3 things: |
| 1392 | // exceptionTableAddr -- exception handler table entry. |
| 1393 | // exceptionTableData -- the data inside the first word of the eht entry. |
| 1394 | // isSingleWordEHT -- whether the entry is in the index. |
| 1395 | unw_word_t personalityRoutine = 0xbadf00d; |
| 1396 | bool scope32 = false; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 1397 | uintptr_t lsda; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1398 | |
| 1399 | // If the high bit in the exception handling table entry is set, the entry is |
| 1400 | // in compact form (section 6.3 EHABI). |
| 1401 | if (exceptionTableData & 0x80000000) { |
| 1402 | // Grab the index of the personality routine from the compact form. |
| 1403 | uint32_t choice = (exceptionTableData & 0x0f000000) >> 24; |
| 1404 | uint32_t extraWords = 0; |
| 1405 | switch (choice) { |
| 1406 | case 0: |
| 1407 | personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0; |
| 1408 | extraWords = 0; |
| 1409 | scope32 = false; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 1410 | lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1411 | break; |
| 1412 | case 1: |
| 1413 | personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1; |
| 1414 | extraWords = (exceptionTableData & 0x00ff0000) >> 16; |
| 1415 | scope32 = false; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 1416 | lsda = exceptionTableAddr + (extraWords + 1) * 4; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1417 | break; |
| 1418 | case 2: |
| 1419 | personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2; |
| 1420 | extraWords = (exceptionTableData & 0x00ff0000) >> 16; |
| 1421 | scope32 = true; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 1422 | lsda = exceptionTableAddr + (extraWords + 1) * 4; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1423 | break; |
| 1424 | default: |
| 1425 | _LIBUNWIND_ABORT("unknown personality routine"); |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
| 1429 | if (isSingleWordEHT) { |
| 1430 | if (extraWords != 0) { |
| 1431 | _LIBUNWIND_ABORT("index inlined table detected but pr function " |
| 1432 | "requires extra words"); |
| 1433 | return false; |
| 1434 | } |
| 1435 | } |
| 1436 | } else { |
| 1437 | pint_t personalityAddr = |
| 1438 | exceptionTableAddr + signExtendPrel31(exceptionTableData); |
| 1439 | personalityRoutine = personalityAddr; |
| 1440 | |
| 1441 | // ARM EHABI # 6.2, # 9.2 |
| 1442 | // |
| 1443 | // +---- ehtp |
| 1444 | // v |
| 1445 | // +--------------------------------------+ |
| 1446 | // | +--------+--------+--------+-------+ | |
| 1447 | // | |0| prel31 to personalityRoutine | | |
| 1448 | // | +--------+--------+--------+-------+ | |
| 1449 | // | | N | unwind opcodes | | <-- UnwindData |
| 1450 | // | +--------+--------+--------+-------+ | |
| 1451 | // | | Word 2 unwind opcodes | | |
| 1452 | // | +--------+--------+--------+-------+ | |
| 1453 | // | ... | |
| 1454 | // | +--------+--------+--------+-------+ | |
| 1455 | // | | Word N unwind opcodes | | |
| 1456 | // | +--------+--------+--------+-------+ | |
| 1457 | // | | LSDA | | <-- lsda |
| 1458 | // | | ... | | |
| 1459 | // | +--------+--------+--------+-------+ | |
| 1460 | // +--------------------------------------+ |
| 1461 | |
| 1462 | uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1; |
| 1463 | uint32_t FirstDataWord = *UnwindData; |
| 1464 | size_t N = ((FirstDataWord >> 24) & 0xff); |
| 1465 | size_t NDataWords = N + 1; |
| 1466 | lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords); |
| 1467 | } |
| 1468 | |
| 1469 | _info.start_ip = thisPC; |
| 1470 | _info.end_ip = nextPC; |
| 1471 | _info.handler = personalityRoutine; |
| 1472 | _info.unwind_info = exceptionTableAddr; |
| 1473 | _info.lsda = lsda; |
| 1474 | // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0. |
Nico Weber | d999d54 | 2020-02-03 14:16:52 -0500 | [diff] [blame] | 1475 | _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0); // Use enum? |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1476 | |
| 1477 | return true; |
| 1478 | } |
| 1479 | #endif |
| 1480 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1481 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1482 | template <typename A, typename R> |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 1483 | bool UnwindCursor<A, R>::getInfoFromFdeCie( |
| 1484 | const typename CFI_Parser<A>::FDE_Info &fdeInfo, |
| 1485 | const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc, |
| 1486 | uintptr_t dso_base) { |
| 1487 | typename CFI_Parser<A>::PrologInfo prolog; |
| 1488 | if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc, |
| 1489 | R::getArch(), &prolog)) { |
| 1490 | // Save off parsed FDE info |
| 1491 | _info.start_ip = fdeInfo.pcStart; |
| 1492 | _info.end_ip = fdeInfo.pcEnd; |
| 1493 | _info.lsda = fdeInfo.lsda; |
| 1494 | _info.handler = cieInfo.personality; |
| 1495 | // Some frameless functions need SP altered when resuming in function, so |
| 1496 | // propagate spExtraArgSize. |
| 1497 | _info.gp = prolog.spExtraArgSize; |
| 1498 | _info.flags = 0; |
| 1499 | _info.format = dwarfEncoding(); |
| 1500 | _info.unwind_info = fdeInfo.fdeStart; |
| 1501 | _info.unwind_info_size = static_cast<uint32_t>(fdeInfo.fdeLength); |
| 1502 | _info.extra = static_cast<unw_word_t>(dso_base); |
| 1503 | return true; |
| 1504 | } |
| 1505 | return false; |
| 1506 | } |
| 1507 | |
| 1508 | template <typename A, typename R> |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1509 | bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc, |
| 1510 | const UnwindInfoSections §s, |
| 1511 | uint32_t fdeSectionOffsetHint) { |
| 1512 | typename CFI_Parser<A>::FDE_Info fdeInfo; |
| 1513 | typename CFI_Parser<A>::CIE_Info cieInfo; |
| 1514 | bool foundFDE = false; |
| 1515 | bool foundInCache = false; |
| 1516 | // If compact encoding table gave offset into dwarf section, go directly there |
| 1517 | if (fdeSectionOffsetHint != 0) { |
| 1518 | foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, |
| 1519 | (uint32_t)sects.dwarf_section_length, |
| 1520 | sects.dwarf_section + fdeSectionOffsetHint, |
| 1521 | &fdeInfo, &cieInfo); |
| 1522 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1523 | #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1524 | if (!foundFDE && (sects.dwarf_index_section != 0)) { |
| 1525 | foundFDE = EHHeaderParser<A>::findFDE( |
| 1526 | _addressSpace, pc, sects.dwarf_index_section, |
| 1527 | (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo); |
| 1528 | } |
| 1529 | #endif |
| 1530 | if (!foundFDE) { |
| 1531 | // otherwise, search cache of previously found FDEs. |
| 1532 | pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc); |
| 1533 | if (cachedFDE != 0) { |
| 1534 | foundFDE = |
| 1535 | CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, |
| 1536 | (uint32_t)sects.dwarf_section_length, |
| 1537 | cachedFDE, &fdeInfo, &cieInfo); |
| 1538 | foundInCache = foundFDE; |
| 1539 | } |
| 1540 | } |
| 1541 | if (!foundFDE) { |
| 1542 | // Still not found, do full scan of __eh_frame section. |
| 1543 | foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, |
| 1544 | (uint32_t)sects.dwarf_section_length, 0, |
| 1545 | &fdeInfo, &cieInfo); |
| 1546 | } |
| 1547 | if (foundFDE) { |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 1548 | if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1549 | // Add to cache (to make next lookup faster) if we had no hint |
| 1550 | // and there was no index. |
| 1551 | if (!foundInCache && (fdeSectionOffsetHint == 0)) { |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1552 | #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1553 | if (sects.dwarf_index_section == 0) |
| 1554 | #endif |
| 1555 | DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd, |
| 1556 | fdeInfo.fdeStart); |
| 1557 | } |
| 1558 | return true; |
| 1559 | } |
| 1560 | } |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 1561 | //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1562 | return false; |
| 1563 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1564 | #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1565 | |
| 1566 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1567 | #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1568 | template <typename A, typename R> |
| 1569 | bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc, |
| 1570 | const UnwindInfoSections §s) { |
| 1571 | const bool log = false; |
| 1572 | if (log) |
| 1573 | fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n", |
| 1574 | (uint64_t)pc, (uint64_t)sects.dso_base); |
| 1575 | |
| 1576 | const UnwindSectionHeader<A> sectionHeader(_addressSpace, |
| 1577 | sects.compact_unwind_section); |
| 1578 | if (sectionHeader.version() != UNWIND_SECTION_VERSION) |
| 1579 | return false; |
| 1580 | |
| 1581 | // do a binary search of top level index to find page with unwind info |
| 1582 | pint_t targetFunctionOffset = pc - sects.dso_base; |
| 1583 | const UnwindSectionIndexArray<A> topIndex(_addressSpace, |
| 1584 | sects.compact_unwind_section |
| 1585 | + sectionHeader.indexSectionOffset()); |
| 1586 | uint32_t low = 0; |
| 1587 | uint32_t high = sectionHeader.indexCount(); |
| 1588 | uint32_t last = high - 1; |
| 1589 | while (low < high) { |
| 1590 | uint32_t mid = (low + high) / 2; |
| 1591 | //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n", |
| 1592 | //mid, low, high, topIndex.functionOffset(mid)); |
| 1593 | if (topIndex.functionOffset(mid) <= targetFunctionOffset) { |
| 1594 | if ((mid == last) || |
| 1595 | (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) { |
| 1596 | low = mid; |
| 1597 | break; |
| 1598 | } else { |
| 1599 | low = mid + 1; |
| 1600 | } |
| 1601 | } else { |
| 1602 | high = mid; |
| 1603 | } |
| 1604 | } |
| 1605 | const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low); |
| 1606 | const uint32_t firstLevelNextPageFunctionOffset = |
| 1607 | topIndex.functionOffset(low + 1); |
| 1608 | const pint_t secondLevelAddr = |
| 1609 | sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low); |
| 1610 | const pint_t lsdaArrayStartAddr = |
| 1611 | sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low); |
| 1612 | const pint_t lsdaArrayEndAddr = |
| 1613 | sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1); |
| 1614 | if (log) |
| 1615 | fprintf(stderr, "\tfirst level search for result index=%d " |
| 1616 | "to secondLevelAddr=0x%llX\n", |
| 1617 | low, (uint64_t) secondLevelAddr); |
| 1618 | // do a binary search of second level page index |
| 1619 | uint32_t encoding = 0; |
| 1620 | pint_t funcStart = 0; |
| 1621 | pint_t funcEnd = 0; |
| 1622 | pint_t lsda = 0; |
| 1623 | pint_t personality = 0; |
| 1624 | uint32_t pageKind = _addressSpace.get32(secondLevelAddr); |
| 1625 | if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) { |
| 1626 | // regular page |
| 1627 | UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace, |
| 1628 | secondLevelAddr); |
| 1629 | UnwindSectionRegularArray<A> pageIndex( |
| 1630 | _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); |
| 1631 | // binary search looks for entry with e where index[e].offset <= pc < |
| 1632 | // index[e+1].offset |
| 1633 | if (log) |
| 1634 | fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in " |
| 1635 | "regular page starting at secondLevelAddr=0x%llX\n", |
| 1636 | (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr); |
| 1637 | low = 0; |
| 1638 | high = pageHeader.entryCount(); |
| 1639 | while (low < high) { |
| 1640 | uint32_t mid = (low + high) / 2; |
| 1641 | if (pageIndex.functionOffset(mid) <= targetFunctionOffset) { |
| 1642 | if (mid == (uint32_t)(pageHeader.entryCount() - 1)) { |
| 1643 | // at end of table |
| 1644 | low = mid; |
| 1645 | funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; |
| 1646 | break; |
| 1647 | } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) { |
| 1648 | // next is too big, so we found it |
| 1649 | low = mid; |
| 1650 | funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base; |
| 1651 | break; |
| 1652 | } else { |
| 1653 | low = mid + 1; |
| 1654 | } |
| 1655 | } else { |
| 1656 | high = mid; |
| 1657 | } |
| 1658 | } |
| 1659 | encoding = pageIndex.encoding(low); |
| 1660 | funcStart = pageIndex.functionOffset(low) + sects.dso_base; |
| 1661 | if (pc < funcStart) { |
| 1662 | if (log) |
| 1663 | fprintf( |
| 1664 | stderr, |
| 1665 | "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", |
| 1666 | (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); |
| 1667 | return false; |
| 1668 | } |
| 1669 | if (pc > funcEnd) { |
| 1670 | if (log) |
| 1671 | fprintf( |
| 1672 | stderr, |
| 1673 | "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", |
| 1674 | (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); |
| 1675 | return false; |
| 1676 | } |
| 1677 | } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) { |
| 1678 | // compressed page |
| 1679 | UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace, |
| 1680 | secondLevelAddr); |
| 1681 | UnwindSectionCompressedArray<A> pageIndex( |
| 1682 | _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); |
| 1683 | const uint32_t targetFunctionPageOffset = |
| 1684 | (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset); |
| 1685 | // binary search looks for entry with e where index[e].offset <= pc < |
| 1686 | // index[e+1].offset |
| 1687 | if (log) |
| 1688 | fprintf(stderr, "\tbinary search of compressed page starting at " |
| 1689 | "secondLevelAddr=0x%llX\n", |
| 1690 | (uint64_t) secondLevelAddr); |
| 1691 | low = 0; |
| 1692 | last = pageHeader.entryCount() - 1; |
| 1693 | high = pageHeader.entryCount(); |
| 1694 | while (low < high) { |
| 1695 | uint32_t mid = (low + high) / 2; |
| 1696 | if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) { |
| 1697 | if ((mid == last) || |
| 1698 | (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) { |
| 1699 | low = mid; |
| 1700 | break; |
| 1701 | } else { |
| 1702 | low = mid + 1; |
| 1703 | } |
| 1704 | } else { |
| 1705 | high = mid; |
| 1706 | } |
| 1707 | } |
| 1708 | funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset |
| 1709 | + sects.dso_base; |
| 1710 | if (low < last) |
| 1711 | funcEnd = |
| 1712 | pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset |
| 1713 | + sects.dso_base; |
| 1714 | else |
| 1715 | funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; |
| 1716 | if (pc < funcStart) { |
| 1717 | _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 1718 | "level compressed unwind table. funcStart=0x%llX", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1719 | (uint64_t) pc, (uint64_t) funcStart); |
| 1720 | return false; |
| 1721 | } |
| 1722 | if (pc > funcEnd) { |
| 1723 | _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 1724 | "level compressed unwind table. funcEnd=0x%llX", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1725 | (uint64_t) pc, (uint64_t) funcEnd); |
| 1726 | return false; |
| 1727 | } |
| 1728 | uint16_t encodingIndex = pageIndex.encodingIndex(low); |
| 1729 | if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) { |
| 1730 | // encoding is in common table in section header |
| 1731 | encoding = _addressSpace.get32( |
| 1732 | sects.compact_unwind_section + |
| 1733 | sectionHeader.commonEncodingsArraySectionOffset() + |
| 1734 | encodingIndex * sizeof(uint32_t)); |
| 1735 | } else { |
| 1736 | // encoding is in page specific table |
| 1737 | uint16_t pageEncodingIndex = |
| 1738 | encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount(); |
| 1739 | encoding = _addressSpace.get32(secondLevelAddr + |
| 1740 | pageHeader.encodingsPageOffset() + |
| 1741 | pageEncodingIndex * sizeof(uint32_t)); |
| 1742 | } |
| 1743 | } else { |
| 1744 | _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 1745 | "level page", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1746 | (uint64_t) sects.compact_unwind_section); |
| 1747 | return false; |
| 1748 | } |
| 1749 | |
| 1750 | // look up LSDA, if encoding says function has one |
| 1751 | if (encoding & UNWIND_HAS_LSDA) { |
| 1752 | UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr); |
| 1753 | uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base); |
| 1754 | low = 0; |
| 1755 | high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) / |
| 1756 | sizeof(unwind_info_section_header_lsda_index_entry); |
| 1757 | // binary search looks for entry with exact match for functionOffset |
| 1758 | if (log) |
| 1759 | fprintf(stderr, |
| 1760 | "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n", |
| 1761 | funcStartOffset); |
| 1762 | while (low < high) { |
| 1763 | uint32_t mid = (low + high) / 2; |
| 1764 | if (lsdaIndex.functionOffset(mid) == funcStartOffset) { |
| 1765 | lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base; |
| 1766 | break; |
| 1767 | } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) { |
| 1768 | low = mid + 1; |
| 1769 | } else { |
| 1770 | high = mid; |
| 1771 | } |
| 1772 | } |
| 1773 | if (lsda == 0) { |
| 1774 | _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 1775 | "pc=0x%0llX, but lsda table has no entry", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1776 | encoding, (uint64_t) pc); |
| 1777 | return false; |
| 1778 | } |
| 1779 | } |
| 1780 | |
Louis Dionne | d3bbbc3 | 2020-08-11 15:24:21 -0400 | [diff] [blame] | 1781 | // extract personality routine, if encoding says function has one |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1782 | uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >> |
| 1783 | (__builtin_ctz(UNWIND_PERSONALITY_MASK)); |
| 1784 | if (personalityIndex != 0) { |
| 1785 | --personalityIndex; // change 1-based to zero-based index |
Louis Dionne | 8c360cb | 2020-08-11 15:29:00 -0400 | [diff] [blame] | 1786 | if (personalityIndex >= sectionHeader.personalityArrayCount()) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1787 | _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, " |
Louis Dionne | 103afa4 | 2019-04-22 15:40:50 +0000 | [diff] [blame] | 1788 | "but personality table has only %d entries", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1789 | encoding, personalityIndex, |
| 1790 | sectionHeader.personalityArrayCount()); |
| 1791 | return false; |
| 1792 | } |
| 1793 | int32_t personalityDelta = (int32_t)_addressSpace.get32( |
| 1794 | sects.compact_unwind_section + |
| 1795 | sectionHeader.personalityArraySectionOffset() + |
| 1796 | personalityIndex * sizeof(uint32_t)); |
| 1797 | pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta; |
| 1798 | personality = _addressSpace.getP(personalityPointer); |
| 1799 | if (log) |
| 1800 | fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " |
| 1801 | "personalityDelta=0x%08X, personality=0x%08llX\n", |
| 1802 | (uint64_t) pc, personalityDelta, (uint64_t) personality); |
| 1803 | } |
| 1804 | |
| 1805 | if (log) |
| 1806 | fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " |
| 1807 | "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n", |
| 1808 | (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart); |
| 1809 | _info.start_ip = funcStart; |
| 1810 | _info.end_ip = funcEnd; |
| 1811 | _info.lsda = lsda; |
| 1812 | _info.handler = personality; |
| 1813 | _info.gp = 0; |
| 1814 | _info.flags = 0; |
| 1815 | _info.format = encoding; |
| 1816 | _info.unwind_info = 0; |
| 1817 | _info.unwind_info_size = 0; |
| 1818 | _info.extra = sects.dso_base; |
| 1819 | return true; |
| 1820 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1821 | #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1822 | |
| 1823 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 1824 | #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 1825 | template <typename A, typename R> |
| 1826 | bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) { |
| 1827 | pint_t base; |
| 1828 | RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base); |
| 1829 | if (!unwindEntry) { |
| 1830 | _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc); |
| 1831 | return false; |
| 1832 | } |
| 1833 | _info.gp = 0; |
| 1834 | _info.flags = 0; |
| 1835 | _info.format = 0; |
| 1836 | _info.unwind_info_size = sizeof(RUNTIME_FUNCTION); |
| 1837 | _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry); |
| 1838 | _info.extra = base; |
| 1839 | _info.start_ip = base + unwindEntry->BeginAddress; |
| 1840 | #ifdef _LIBUNWIND_TARGET_X86_64 |
| 1841 | _info.end_ip = base + unwindEntry->EndAddress; |
| 1842 | // Only fill in the handler and LSDA if they're stale. |
| 1843 | if (pc != getLastPC()) { |
| 1844 | UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData); |
| 1845 | if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) { |
| 1846 | // The personality is given in the UNWIND_INFO itself. The LSDA immediately |
| 1847 | // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit |
| 1848 | // these structures.) |
| 1849 | // N.B. UNWIND_INFO structs are DWORD-aligned. |
| 1850 | uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1; |
| 1851 | const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]); |
| 1852 | _info.lsda = reinterpret_cast<unw_word_t>(handler+1); |
| 1853 | if (*handler) { |
| 1854 | _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality); |
| 1855 | } else |
| 1856 | _info.handler = 0; |
| 1857 | } else { |
| 1858 | _info.lsda = 0; |
| 1859 | _info.handler = 0; |
| 1860 | } |
| 1861 | } |
| 1862 | #elif defined(_LIBUNWIND_TARGET_ARM) |
| 1863 | _info.end_ip = _info.start_ip + unwindEntry->FunctionLength; |
| 1864 | _info.lsda = 0; // FIXME |
| 1865 | _info.handler = 0; // FIXME |
| 1866 | #endif |
| 1867 | setLastPC(pc); |
| 1868 | return true; |
| 1869 | } |
| 1870 | #endif |
| 1871 | |
| 1872 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1873 | template <typename A, typename R> |
| 1874 | void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) { |
| 1875 | pint_t pc = (pint_t)this->getReg(UNW_REG_IP); |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1876 | #if defined(_LIBUNWIND_ARM_EHABI) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1877 | // Remove the thumb bit so the IP represents the actual instruction address. |
| 1878 | // This matches the behaviour of _Unwind_GetIP on arm. |
| 1879 | pc &= (pint_t)~0x1; |
| 1880 | #endif |
| 1881 | |
Sterling Augustine | c100c4d | 2020-03-30 15:20:26 -0700 | [diff] [blame] | 1882 | // Exit early if at the top of the stack. |
| 1883 | if (pc == 0) { |
| 1884 | _unwindInfoMissing = true; |
| 1885 | return; |
| 1886 | } |
| 1887 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1888 | // If the last line of a function is a "throw" the compiler sometimes |
| 1889 | // emits no instructions after the call to __cxa_throw. This means |
| 1890 | // the return address is actually the start of the next function. |
| 1891 | // To disambiguate this, back up the pc when we know it is a return |
| 1892 | // address. |
| 1893 | if (isReturnAddress) |
| 1894 | --pc; |
| 1895 | |
| 1896 | // Ask address space object to find unwind sections for this pc. |
| 1897 | UnwindInfoSections sects; |
| 1898 | if (_addressSpace.findUnwindSections(pc, sects)) { |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1899 | #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1900 | // If there is a compact unwind encoding table, look there first. |
| 1901 | if (sects.compact_unwind_section != 0) { |
| 1902 | if (this->getInfoFromCompactEncodingSection(pc, sects)) { |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1903 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1904 | // Found info in table, done unless encoding says to use dwarf. |
| 1905 | uint32_t dwarfOffset; |
| 1906 | if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) { |
| 1907 | if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) { |
| 1908 | // found info in dwarf, done |
| 1909 | return; |
| 1910 | } |
| 1911 | } |
| 1912 | #endif |
| 1913 | // If unwind table has entry, but entry says there is no unwind info, |
| 1914 | // record that we have no unwind info. |
| 1915 | if (_info.format == 0) |
| 1916 | _unwindInfoMissing = true; |
| 1917 | return; |
| 1918 | } |
| 1919 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1920 | #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1921 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 1922 | #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 1923 | // If there is SEH unwind info, look there next. |
| 1924 | if (this->getInfoFromSEH(pc)) |
| 1925 | return; |
| 1926 | #endif |
| 1927 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1928 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1929 | // If there is dwarf unwind info, look there next. |
| 1930 | if (sects.dwarf_section != 0) { |
| 1931 | if (this->getInfoFromDwarfSection(pc, sects)) { |
| 1932 | // found info in dwarf, done |
| 1933 | return; |
| 1934 | } |
| 1935 | } |
| 1936 | #endif |
| 1937 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1938 | #if defined(_LIBUNWIND_ARM_EHABI) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1939 | // If there is ARM EHABI unwind info, look there next. |
| 1940 | if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects)) |
| 1941 | return; |
| 1942 | #endif |
| 1943 | } |
| 1944 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1945 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1946 | // There is no static unwind info for this pc. Look to see if an FDE was |
| 1947 | // dynamically registered for it. |
| 1948 | pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc); |
| 1949 | if (cachedFDE != 0) { |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 1950 | typename CFI_Parser<A>::FDE_Info fdeInfo; |
| 1951 | typename CFI_Parser<A>::CIE_Info cieInfo; |
| 1952 | if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo)) |
| 1953 | if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0)) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1954 | return; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | // Lastly, ask AddressSpace object about platform specific ways to locate |
| 1958 | // other FDEs. |
| 1959 | pint_t fde; |
| 1960 | if (_addressSpace.findOtherFDE(pc, fde)) { |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 1961 | typename CFI_Parser<A>::FDE_Info fdeInfo; |
| 1962 | typename CFI_Parser<A>::CIE_Info cieInfo; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1963 | if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) { |
| 1964 | // Double check this FDE is for a function that includes the pc. |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 1965 | if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) |
| 1966 | if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0)) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1967 | return; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1968 | } |
| 1969 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1970 | #endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1971 | |
| 1972 | // no unwind info, flag that we can't reliably unwind |
| 1973 | _unwindInfoMissing = true; |
| 1974 | } |
| 1975 | |
| 1976 | template <typename A, typename R> |
| 1977 | int UnwindCursor<A, R>::step() { |
| 1978 | // Bottom of stack is defined is when unwind info cannot be found. |
| 1979 | if (_unwindInfoMissing) |
| 1980 | return UNW_STEP_END; |
| 1981 | |
| 1982 | // Use unwinding info to modify register set as if function returned. |
| 1983 | int result; |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1984 | #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1985 | result = this->stepWithCompactEncoding(); |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 1986 | #elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 1987 | result = this->stepWithSEHData(); |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1988 | #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1989 | result = this->stepWithDwarfFDE(); |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1990 | #elif defined(_LIBUNWIND_ARM_EHABI) |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 1991 | result = this->stepWithEHABI(); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1992 | #else |
| 1993 | #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \ |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 1994 | _LIBUNWIND_SUPPORT_SEH_UNWIND or \ |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1995 | _LIBUNWIND_SUPPORT_DWARF_UNWIND or \ |
Logan Chien | 06b0c7a | 2015-07-19 15:23:10 +0000 | [diff] [blame] | 1996 | _LIBUNWIND_ARM_EHABI |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1997 | #endif |
| 1998 | |
| 1999 | // update info based on new PC |
| 2000 | if (result == UNW_STEP_SUCCESS) { |
| 2001 | this->setInfoBasedOnIPRegister(true); |
| 2002 | if (_unwindInfoMissing) |
| 2003 | return UNW_STEP_END; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | return result; |
| 2007 | } |
| 2008 | |
| 2009 | template <typename A, typename R> |
| 2010 | void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) { |
Saleem Abdulrasool | 78b42cc | 2019-09-20 15:53:42 +0000 | [diff] [blame] | 2011 | if (_unwindInfoMissing) |
| 2012 | memset(info, 0, sizeof(*info)); |
| 2013 | else |
| 2014 | *info = _info; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | template <typename A, typename R> |
| 2018 | bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen, |
| 2019 | unw_word_t *offset) { |
| 2020 | return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP), |
| 2021 | buf, bufLen, offset); |
| 2022 | } |
| 2023 | |
| 2024 | } // namespace libunwind |
| 2025 | |
| 2026 | #endif // __UNWINDCURSOR_HPP__ |