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