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