1 //===------------------------- UnwindCursor.hpp ---------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 // 9 // C++ interface to lower levels of libunwind 10 //===----------------------------------------------------------------------===// 11 12 #ifndef __UNWINDCURSOR_HPP__ 13 #define __UNWINDCURSOR_HPP__ 14 15 #include <algorithm> 16 #include <stdint.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <unwind.h> 20 21 #ifdef _WIN32 22 #include <windows.h> 23 #include <ntverp.h> 24 #endif 25 #ifdef __APPLE__ 26 #include <mach-o/dyld.h> 27 #endif 28 29 #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 30 // Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and 31 // earlier) SDKs. 32 // MinGW-w64 has always provided this struct. 33 #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \ 34 !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000 35 struct _DISPATCHER_CONTEXT { 36 ULONG64 ControlPc; 37 ULONG64 ImageBase; 38 PRUNTIME_FUNCTION FunctionEntry; 39 ULONG64 EstablisherFrame; 40 ULONG64 TargetIp; 41 PCONTEXT ContextRecord; 42 PEXCEPTION_ROUTINE LanguageHandler; 43 PVOID HandlerData; 44 PUNWIND_HISTORY_TABLE HistoryTable; 45 ULONG ScopeIndex; 46 ULONG Fill0; 47 }; 48 #endif 49 50 struct UNWIND_INFO { 51 uint8_t Version : 3; 52 uint8_t Flags : 5; 53 uint8_t SizeOfProlog; 54 uint8_t CountOfCodes; 55 uint8_t FrameRegister : 4; 56 uint8_t FrameOffset : 4; 57 uint16_t UnwindCodes[2]; 58 }; 59 60 extern "C" _Unwind_Reason_Code __libunwind_seh_personality( 61 int, _Unwind_Action, uint64_t, _Unwind_Exception *, 62 struct _Unwind_Context *); 63 64 #endif 65 66 #include "config.h" 67 68 #include "AddressSpace.hpp" 69 #include "CompactUnwinder.hpp" 70 #include "config.h" 71 #include "DwarfInstructions.hpp" 72 #include "EHHeaderParser.hpp" 73 #include "libunwind.h" 74 #include "Registers.hpp" 75 #include "RWMutex.hpp" 76 #include "Unwind-EHABI.h" 77 78 namespace libunwind { 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 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 // Can't use std::vector<> here because this code is below libc++. 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 == 0)) { 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 private: 489 490 pint_t getLastPC() const { return _dispContext.ControlPc; } 491 void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; } 492 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) { 493 _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc, 494 &_dispContext.ImageBase, 495 _dispContext.HistoryTable); 496 *base = _dispContext.ImageBase; 497 return _dispContext.FunctionEntry; 498 } 499 bool getInfoFromSEH(pint_t pc); 500 int stepWithSEHData() { 501 _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER, 502 _dispContext.ImageBase, 503 _dispContext.ControlPc, 504 _dispContext.FunctionEntry, 505 _dispContext.ContextRecord, 506 &_dispContext.HandlerData, 507 &_dispContext.EstablisherFrame, 508 NULL); 509 // Update some fields of the unwind info now, since we have them. 510 _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData); 511 if (_dispContext.LanguageHandler) { 512 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality); 513 } else 514 _info.handler = 0; 515 return UNW_STEP_SUCCESS; 516 } 517 518 A &_addressSpace; 519 unw_proc_info_t _info; 520 DISPATCHER_CONTEXT _dispContext; 521 CONTEXT _msContext; 522 UNWIND_HISTORY_TABLE _histTable; 523 bool _unwindInfoMissing; 524 }; 525 526 527 template <typename A, typename R> 528 UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as) 529 : _addressSpace(as), _unwindInfoMissing(false) { 530 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), 531 "UnwindCursor<> does not fit in unw_cursor_t"); 532 memset(&_info, 0, sizeof(_info)); 533 memset(&_histTable, 0, sizeof(_histTable)); 534 _dispContext.ContextRecord = &_msContext; 535 _dispContext.HistoryTable = &_histTable; 536 // Initialize MS context from ours. 537 R r(context); 538 _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT; 539 #if defined(_LIBUNWIND_TARGET_X86_64) 540 _msContext.Rax = r.getRegister(UNW_X86_64_RAX); 541 _msContext.Rcx = r.getRegister(UNW_X86_64_RCX); 542 _msContext.Rdx = r.getRegister(UNW_X86_64_RDX); 543 _msContext.Rbx = r.getRegister(UNW_X86_64_RBX); 544 _msContext.Rsp = r.getRegister(UNW_X86_64_RSP); 545 _msContext.Rbp = r.getRegister(UNW_X86_64_RBP); 546 _msContext.Rsi = r.getRegister(UNW_X86_64_RSI); 547 _msContext.Rdi = r.getRegister(UNW_X86_64_RDI); 548 _msContext.R8 = r.getRegister(UNW_X86_64_R8); 549 _msContext.R9 = r.getRegister(UNW_X86_64_R9); 550 _msContext.R10 = r.getRegister(UNW_X86_64_R10); 551 _msContext.R11 = r.getRegister(UNW_X86_64_R11); 552 _msContext.R12 = r.getRegister(UNW_X86_64_R12); 553 _msContext.R13 = r.getRegister(UNW_X86_64_R13); 554 _msContext.R14 = r.getRegister(UNW_X86_64_R14); 555 _msContext.R15 = r.getRegister(UNW_X86_64_R15); 556 _msContext.Rip = r.getRegister(UNW_REG_IP); 557 union { 558 v128 v; 559 M128A m; 560 } t; 561 t.v = r.getVectorRegister(UNW_X86_64_XMM0); 562 _msContext.Xmm0 = t.m; 563 t.v = r.getVectorRegister(UNW_X86_64_XMM1); 564 _msContext.Xmm1 = t.m; 565 t.v = r.getVectorRegister(UNW_X86_64_XMM2); 566 _msContext.Xmm2 = t.m; 567 t.v = r.getVectorRegister(UNW_X86_64_XMM3); 568 _msContext.Xmm3 = t.m; 569 t.v = r.getVectorRegister(UNW_X86_64_XMM4); 570 _msContext.Xmm4 = t.m; 571 t.v = r.getVectorRegister(UNW_X86_64_XMM5); 572 _msContext.Xmm5 = t.m; 573 t.v = r.getVectorRegister(UNW_X86_64_XMM6); 574 _msContext.Xmm6 = t.m; 575 t.v = r.getVectorRegister(UNW_X86_64_XMM7); 576 _msContext.Xmm7 = t.m; 577 t.v = r.getVectorRegister(UNW_X86_64_XMM8); 578 _msContext.Xmm8 = t.m; 579 t.v = r.getVectorRegister(UNW_X86_64_XMM9); 580 _msContext.Xmm9 = t.m; 581 t.v = r.getVectorRegister(UNW_X86_64_XMM10); 582 _msContext.Xmm10 = t.m; 583 t.v = r.getVectorRegister(UNW_X86_64_XMM11); 584 _msContext.Xmm11 = t.m; 585 t.v = r.getVectorRegister(UNW_X86_64_XMM12); 586 _msContext.Xmm12 = t.m; 587 t.v = r.getVectorRegister(UNW_X86_64_XMM13); 588 _msContext.Xmm13 = t.m; 589 t.v = r.getVectorRegister(UNW_X86_64_XMM14); 590 _msContext.Xmm14 = t.m; 591 t.v = r.getVectorRegister(UNW_X86_64_XMM15); 592 _msContext.Xmm15 = t.m; 593 #elif defined(_LIBUNWIND_TARGET_ARM) 594 _msContext.R0 = r.getRegister(UNW_ARM_R0); 595 _msContext.R1 = r.getRegister(UNW_ARM_R1); 596 _msContext.R2 = r.getRegister(UNW_ARM_R2); 597 _msContext.R3 = r.getRegister(UNW_ARM_R3); 598 _msContext.R4 = r.getRegister(UNW_ARM_R4); 599 _msContext.R5 = r.getRegister(UNW_ARM_R5); 600 _msContext.R6 = r.getRegister(UNW_ARM_R6); 601 _msContext.R7 = r.getRegister(UNW_ARM_R7); 602 _msContext.R8 = r.getRegister(UNW_ARM_R8); 603 _msContext.R9 = r.getRegister(UNW_ARM_R9); 604 _msContext.R10 = r.getRegister(UNW_ARM_R10); 605 _msContext.R11 = r.getRegister(UNW_ARM_R11); 606 _msContext.R12 = r.getRegister(UNW_ARM_R12); 607 _msContext.Sp = r.getRegister(UNW_ARM_SP); 608 _msContext.Lr = r.getRegister(UNW_ARM_LR); 609 _msContext.Pc = r.getRegister(UNW_ARM_IP); 610 for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) { 611 union { 612 uint64_t w; 613 double d; 614 } d; 615 d.d = r.getFloatRegister(i); 616 _msContext.D[i - UNW_ARM_D0] = d.w; 617 } 618 #elif defined(_LIBUNWIND_TARGET_AARCH64) 619 for (int i = UNW_ARM64_X0; i <= UNW_ARM64_X30; ++i) 620 _msContext.X[i - UNW_ARM64_X0] = r.getRegister(i); 621 _msContext.Sp = r.getRegister(UNW_REG_SP); 622 _msContext.Pc = r.getRegister(UNW_REG_IP); 623 for (int i = UNW_ARM64_D0; i <= UNW_ARM64_D31; ++i) 624 _msContext.V[i - UNW_ARM64_D0].D[0] = r.getFloatRegister(i); 625 #endif 626 } 627 628 template <typename A, typename R> 629 UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as) 630 : _addressSpace(as), _unwindInfoMissing(false) { 631 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), 632 "UnwindCursor<> does not fit in unw_cursor_t"); 633 memset(&_info, 0, sizeof(_info)); 634 memset(&_histTable, 0, sizeof(_histTable)); 635 _dispContext.ContextRecord = &_msContext; 636 _dispContext.HistoryTable = &_histTable; 637 _msContext = *context; 638 } 639 640 641 template <typename A, typename R> 642 bool UnwindCursor<A, R>::validReg(int regNum) { 643 if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true; 644 #if defined(_LIBUNWIND_TARGET_X86_64) 645 if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true; 646 #elif defined(_LIBUNWIND_TARGET_ARM) 647 if (regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) return true; 648 #elif defined(_LIBUNWIND_TARGET_AARCH64) 649 if (regNum >= UNW_ARM64_X0 && regNum <= UNW_ARM64_X30) return true; 650 #endif 651 return false; 652 } 653 654 template <typename A, typename R> 655 unw_word_t UnwindCursor<A, R>::getReg(int regNum) { 656 switch (regNum) { 657 #if defined(_LIBUNWIND_TARGET_X86_64) 658 case UNW_REG_IP: return _msContext.Rip; 659 case UNW_X86_64_RAX: return _msContext.Rax; 660 case UNW_X86_64_RDX: return _msContext.Rdx; 661 case UNW_X86_64_RCX: return _msContext.Rcx; 662 case UNW_X86_64_RBX: return _msContext.Rbx; 663 case UNW_REG_SP: 664 case UNW_X86_64_RSP: return _msContext.Rsp; 665 case UNW_X86_64_RBP: return _msContext.Rbp; 666 case UNW_X86_64_RSI: return _msContext.Rsi; 667 case UNW_X86_64_RDI: return _msContext.Rdi; 668 case UNW_X86_64_R8: return _msContext.R8; 669 case UNW_X86_64_R9: return _msContext.R9; 670 case UNW_X86_64_R10: return _msContext.R10; 671 case UNW_X86_64_R11: return _msContext.R11; 672 case UNW_X86_64_R12: return _msContext.R12; 673 case UNW_X86_64_R13: return _msContext.R13; 674 case UNW_X86_64_R14: return _msContext.R14; 675 case UNW_X86_64_R15: return _msContext.R15; 676 #elif defined(_LIBUNWIND_TARGET_ARM) 677 case UNW_ARM_R0: return _msContext.R0; 678 case UNW_ARM_R1: return _msContext.R1; 679 case UNW_ARM_R2: return _msContext.R2; 680 case UNW_ARM_R3: return _msContext.R3; 681 case UNW_ARM_R4: return _msContext.R4; 682 case UNW_ARM_R5: return _msContext.R5; 683 case UNW_ARM_R6: return _msContext.R6; 684 case UNW_ARM_R7: return _msContext.R7; 685 case UNW_ARM_R8: return _msContext.R8; 686 case UNW_ARM_R9: return _msContext.R9; 687 case UNW_ARM_R10: return _msContext.R10; 688 case UNW_ARM_R11: return _msContext.R11; 689 case UNW_ARM_R12: return _msContext.R12; 690 case UNW_REG_SP: 691 case UNW_ARM_SP: return _msContext.Sp; 692 case UNW_ARM_LR: return _msContext.Lr; 693 case UNW_REG_IP: 694 case UNW_ARM_IP: return _msContext.Pc; 695 #elif defined(_LIBUNWIND_TARGET_AARCH64) 696 case UNW_REG_SP: return _msContext.Sp; 697 case UNW_REG_IP: return _msContext.Pc; 698 default: return _msContext.X[regNum - UNW_ARM64_X0]; 699 #endif 700 } 701 _LIBUNWIND_ABORT("unsupported register"); 702 } 703 704 template <typename A, typename R> 705 void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) { 706 switch (regNum) { 707 #if defined(_LIBUNWIND_TARGET_X86_64) 708 case UNW_REG_IP: _msContext.Rip = value; break; 709 case UNW_X86_64_RAX: _msContext.Rax = value; break; 710 case UNW_X86_64_RDX: _msContext.Rdx = value; break; 711 case UNW_X86_64_RCX: _msContext.Rcx = value; break; 712 case UNW_X86_64_RBX: _msContext.Rbx = value; break; 713 case UNW_REG_SP: 714 case UNW_X86_64_RSP: _msContext.Rsp = value; break; 715 case UNW_X86_64_RBP: _msContext.Rbp = value; break; 716 case UNW_X86_64_RSI: _msContext.Rsi = value; break; 717 case UNW_X86_64_RDI: _msContext.Rdi = value; break; 718 case UNW_X86_64_R8: _msContext.R8 = value; break; 719 case UNW_X86_64_R9: _msContext.R9 = value; break; 720 case UNW_X86_64_R10: _msContext.R10 = value; break; 721 case UNW_X86_64_R11: _msContext.R11 = value; break; 722 case UNW_X86_64_R12: _msContext.R12 = value; break; 723 case UNW_X86_64_R13: _msContext.R13 = value; break; 724 case UNW_X86_64_R14: _msContext.R14 = value; break; 725 case UNW_X86_64_R15: _msContext.R15 = value; break; 726 #elif defined(_LIBUNWIND_TARGET_ARM) 727 case UNW_ARM_R0: _msContext.R0 = value; break; 728 case UNW_ARM_R1: _msContext.R1 = value; break; 729 case UNW_ARM_R2: _msContext.R2 = value; break; 730 case UNW_ARM_R3: _msContext.R3 = value; break; 731 case UNW_ARM_R4: _msContext.R4 = value; break; 732 case UNW_ARM_R5: _msContext.R5 = value; break; 733 case UNW_ARM_R6: _msContext.R6 = value; break; 734 case UNW_ARM_R7: _msContext.R7 = value; break; 735 case UNW_ARM_R8: _msContext.R8 = value; break; 736 case UNW_ARM_R9: _msContext.R9 = value; break; 737 case UNW_ARM_R10: _msContext.R10 = value; break; 738 case UNW_ARM_R11: _msContext.R11 = value; break; 739 case UNW_ARM_R12: _msContext.R12 = value; break; 740 case UNW_REG_SP: 741 case UNW_ARM_SP: _msContext.Sp = value; break; 742 case UNW_ARM_LR: _msContext.Lr = value; break; 743 case UNW_REG_IP: 744 case UNW_ARM_IP: _msContext.Pc = value; break; 745 #elif defined(_LIBUNWIND_TARGET_AARCH64) 746 case UNW_REG_SP: _msContext.Sp = value; break; 747 case UNW_REG_IP: _msContext.Pc = value; break; 748 case UNW_ARM64_X0: 749 case UNW_ARM64_X1: 750 case UNW_ARM64_X2: 751 case UNW_ARM64_X3: 752 case UNW_ARM64_X4: 753 case UNW_ARM64_X5: 754 case UNW_ARM64_X6: 755 case UNW_ARM64_X7: 756 case UNW_ARM64_X8: 757 case UNW_ARM64_X9: 758 case UNW_ARM64_X10: 759 case UNW_ARM64_X11: 760 case UNW_ARM64_X12: 761 case UNW_ARM64_X13: 762 case UNW_ARM64_X14: 763 case UNW_ARM64_X15: 764 case UNW_ARM64_X16: 765 case UNW_ARM64_X17: 766 case UNW_ARM64_X18: 767 case UNW_ARM64_X19: 768 case UNW_ARM64_X20: 769 case UNW_ARM64_X21: 770 case UNW_ARM64_X22: 771 case UNW_ARM64_X23: 772 case UNW_ARM64_X24: 773 case UNW_ARM64_X25: 774 case UNW_ARM64_X26: 775 case UNW_ARM64_X27: 776 case UNW_ARM64_X28: 777 case UNW_ARM64_FP: 778 case UNW_ARM64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break; 779 #endif 780 default: 781 _LIBUNWIND_ABORT("unsupported register"); 782 } 783 } 784 785 template <typename A, typename R> 786 bool UnwindCursor<A, R>::validFloatReg(int regNum) { 787 #if defined(_LIBUNWIND_TARGET_ARM) 788 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true; 789 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true; 790 #elif defined(_LIBUNWIND_TARGET_AARCH64) 791 if (regNum >= UNW_ARM64_D0 && regNum <= UNW_ARM64_D31) return true; 792 #endif 793 return false; 794 } 795 796 template <typename A, typename R> 797 unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) { 798 #if defined(_LIBUNWIND_TARGET_ARM) 799 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) { 800 union { 801 uint32_t w; 802 float f; 803 } d; 804 d.w = _msContext.S[regNum - UNW_ARM_S0]; 805 return d.f; 806 } 807 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) { 808 union { 809 uint64_t w; 810 double d; 811 } d; 812 d.w = _msContext.D[regNum - UNW_ARM_D0]; 813 return d.d; 814 } 815 _LIBUNWIND_ABORT("unsupported float register"); 816 #elif defined(_LIBUNWIND_TARGET_AARCH64) 817 return _msContext.V[regNum - UNW_ARM64_D0].D[0]; 818 #else 819 _LIBUNWIND_ABORT("float registers unimplemented"); 820 #endif 821 } 822 823 template <typename A, typename R> 824 void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) { 825 #if defined(_LIBUNWIND_TARGET_ARM) 826 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) { 827 union { 828 uint32_t w; 829 float f; 830 } d; 831 d.f = value; 832 _msContext.S[regNum - UNW_ARM_S0] = d.w; 833 } 834 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) { 835 union { 836 uint64_t w; 837 double d; 838 } d; 839 d.d = value; 840 _msContext.D[regNum - UNW_ARM_D0] = d.w; 841 } 842 _LIBUNWIND_ABORT("unsupported float register"); 843 #elif defined(_LIBUNWIND_TARGET_AARCH64) 844 _msContext.V[regNum - UNW_ARM64_D0].D[0] = value; 845 #else 846 _LIBUNWIND_ABORT("float registers unimplemented"); 847 #endif 848 } 849 850 template <typename A, typename R> void UnwindCursor<A, R>::jumpto() { 851 RtlRestoreContext(&_msContext, nullptr); 852 } 853 854 #ifdef __arm__ 855 template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {} 856 #endif 857 858 template <typename A, typename R> 859 const char *UnwindCursor<A, R>::getRegisterName(int regNum) { 860 return R::getRegisterName(regNum); 861 } 862 863 template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() { 864 return false; 865 } 866 867 #else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32) 868 869 /// UnwindCursor contains all state (including all register values) during 870 /// an unwind. This is normally stack allocated inside a unw_cursor_t. 871 template <typename A, typename R> 872 class UnwindCursor : public AbstractUnwindCursor{ 873 typedef typename A::pint_t pint_t; 874 public: 875 UnwindCursor(unw_context_t *context, A &as); 876 UnwindCursor(A &as, void *threadArg); 877 virtual ~UnwindCursor() {} 878 virtual bool validReg(int); 879 virtual unw_word_t getReg(int); 880 virtual void setReg(int, unw_word_t); 881 virtual bool validFloatReg(int); 882 virtual unw_fpreg_t getFloatReg(int); 883 virtual void setFloatReg(int, unw_fpreg_t); 884 virtual int step(); 885 virtual void getInfo(unw_proc_info_t *); 886 virtual void jumpto(); 887 virtual bool isSignalFrame(); 888 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off); 889 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false); 890 virtual const char *getRegisterName(int num); 891 #ifdef __arm__ 892 virtual void saveVFPAsX(); 893 #endif 894 895 private: 896 897 #if defined(_LIBUNWIND_ARM_EHABI) 898 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections §s); 899 900 int stepWithEHABI() { 901 size_t len = 0; 902 size_t off = 0; 903 // FIXME: Calling decode_eht_entry() here is violating the libunwind 904 // abstraction layer. 905 const uint32_t *ehtp = 906 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info), 907 &off, &len); 908 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) != 909 _URC_CONTINUE_UNWIND) 910 return UNW_STEP_END; 911 return UNW_STEP_SUCCESS; 912 } 913 #endif 914 915 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 916 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections §s, 917 uint32_t fdeSectionOffsetHint=0); 918 int stepWithDwarfFDE() { 919 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace, 920 (pint_t)this->getReg(UNW_REG_IP), 921 (pint_t)_info.unwind_info, 922 _registers); 923 } 924 #endif 925 926 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 927 bool getInfoFromCompactEncodingSection(pint_t pc, 928 const UnwindInfoSections §s); 929 int stepWithCompactEncoding() { 930 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 931 if ( compactSaysUseDwarf() ) 932 return stepWithDwarfFDE(); 933 #endif 934 R dummy; 935 return stepWithCompactEncoding(dummy); 936 } 937 938 #if defined(_LIBUNWIND_TARGET_X86_64) 939 int stepWithCompactEncoding(Registers_x86_64 &) { 940 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding( 941 _info.format, _info.start_ip, _addressSpace, _registers); 942 } 943 #endif 944 945 #if defined(_LIBUNWIND_TARGET_I386) 946 int stepWithCompactEncoding(Registers_x86 &) { 947 return CompactUnwinder_x86<A>::stepWithCompactEncoding( 948 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers); 949 } 950 #endif 951 952 #if defined(_LIBUNWIND_TARGET_PPC) 953 int stepWithCompactEncoding(Registers_ppc &) { 954 return UNW_EINVAL; 955 } 956 #endif 957 958 #if defined(_LIBUNWIND_TARGET_PPC64) 959 int stepWithCompactEncoding(Registers_ppc64 &) { 960 return UNW_EINVAL; 961 } 962 #endif 963 964 965 #if defined(_LIBUNWIND_TARGET_AARCH64) 966 int stepWithCompactEncoding(Registers_arm64 &) { 967 return CompactUnwinder_arm64<A>::stepWithCompactEncoding( 968 _info.format, _info.start_ip, _addressSpace, _registers); 969 } 970 #endif 971 972 #if defined(_LIBUNWIND_TARGET_MIPS_O32) 973 int stepWithCompactEncoding(Registers_mips_o32 &) { 974 return UNW_EINVAL; 975 } 976 #endif 977 978 #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI) 979 int stepWithCompactEncoding(Registers_mips_newabi &) { 980 return UNW_EINVAL; 981 } 982 #endif 983 984 #if defined(_LIBUNWIND_TARGET_SPARC) 985 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; } 986 #endif 987 988 bool compactSaysUseDwarf(uint32_t *offset=NULL) const { 989 R dummy; 990 return compactSaysUseDwarf(dummy, offset); 991 } 992 993 #if defined(_LIBUNWIND_TARGET_X86_64) 994 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const { 995 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) { 996 if (offset) 997 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET); 998 return true; 999 } 1000 return false; 1001 } 1002 #endif 1003 1004 #if defined(_LIBUNWIND_TARGET_I386) 1005 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const { 1006 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) { 1007 if (offset) 1008 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET); 1009 return true; 1010 } 1011 return false; 1012 } 1013 #endif 1014 1015 #if defined(_LIBUNWIND_TARGET_PPC) 1016 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const { 1017 return true; 1018 } 1019 #endif 1020 1021 #if defined(_LIBUNWIND_TARGET_PPC64) 1022 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const { 1023 return true; 1024 } 1025 #endif 1026 1027 #if defined(_LIBUNWIND_TARGET_AARCH64) 1028 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const { 1029 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) { 1030 if (offset) 1031 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET); 1032 return true; 1033 } 1034 return false; 1035 } 1036 #endif 1037 1038 #if defined(_LIBUNWIND_TARGET_MIPS_O32) 1039 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const { 1040 return true; 1041 } 1042 #endif 1043 1044 #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI) 1045 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const { 1046 return true; 1047 } 1048 #endif 1049 1050 #if defined(_LIBUNWIND_TARGET_SPARC) 1051 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; } 1052 #endif 1053 1054 #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 1055 1056 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1057 compact_unwind_encoding_t dwarfEncoding() const { 1058 R dummy; 1059 return dwarfEncoding(dummy); 1060 } 1061 1062 #if defined(_LIBUNWIND_TARGET_X86_64) 1063 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const { 1064 return UNWIND_X86_64_MODE_DWARF; 1065 } 1066 #endif 1067 1068 #if defined(_LIBUNWIND_TARGET_I386) 1069 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const { 1070 return UNWIND_X86_MODE_DWARF; 1071 } 1072 #endif 1073 1074 #if defined(_LIBUNWIND_TARGET_PPC) 1075 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const { 1076 return 0; 1077 } 1078 #endif 1079 1080 #if defined(_LIBUNWIND_TARGET_PPC64) 1081 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const { 1082 return 0; 1083 } 1084 #endif 1085 1086 #if defined(_LIBUNWIND_TARGET_AARCH64) 1087 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const { 1088 return UNWIND_ARM64_MODE_DWARF; 1089 } 1090 #endif 1091 1092 #if defined(_LIBUNWIND_TARGET_ARM) 1093 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const { 1094 return 0; 1095 } 1096 #endif 1097 1098 #if defined (_LIBUNWIND_TARGET_OR1K) 1099 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const { 1100 return 0; 1101 } 1102 #endif 1103 1104 #if defined (_LIBUNWIND_TARGET_RISCV) 1105 compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const { 1106 return 0; 1107 } 1108 #endif 1109 1110 #if defined (_LIBUNWIND_TARGET_MIPS_O32) 1111 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const { 1112 return 0; 1113 } 1114 #endif 1115 1116 #if defined (_LIBUNWIND_TARGET_MIPS_NEWABI) 1117 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const { 1118 return 0; 1119 } 1120 #endif 1121 1122 #if defined(_LIBUNWIND_TARGET_SPARC) 1123 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; } 1124 #endif 1125 1126 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1127 1128 #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 1129 // For runtime environments using SEH unwind data without Windows runtime 1130 // support. 1131 pint_t getLastPC() const { /* FIXME: Implement */ return 0; } 1132 void setLastPC(pint_t pc) { /* FIXME: Implement */ } 1133 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) { 1134 /* FIXME: Implement */ 1135 *base = 0; 1136 return nullptr; 1137 } 1138 bool getInfoFromSEH(pint_t pc); 1139 int stepWithSEHData() { /* FIXME: Implement */ return 0; } 1140 #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 1141 1142 1143 A &_addressSpace; 1144 R _registers; 1145 unw_proc_info_t _info; 1146 bool _unwindInfoMissing; 1147 bool _isSignalFrame; 1148 }; 1149 1150 1151 template <typename A, typename R> 1152 UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as) 1153 : _addressSpace(as), _registers(context), _unwindInfoMissing(false), 1154 _isSignalFrame(false) { 1155 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), 1156 "UnwindCursor<> does not fit in unw_cursor_t"); 1157 memset(&_info, 0, sizeof(_info)); 1158 } 1159 1160 template <typename A, typename R> 1161 UnwindCursor<A, R>::UnwindCursor(A &as, void *) 1162 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) { 1163 memset(&_info, 0, sizeof(_info)); 1164 // FIXME 1165 // fill in _registers from thread arg 1166 } 1167 1168 1169 template <typename A, typename R> 1170 bool UnwindCursor<A, R>::validReg(int regNum) { 1171 return _registers.validRegister(regNum); 1172 } 1173 1174 template <typename A, typename R> 1175 unw_word_t UnwindCursor<A, R>::getReg(int regNum) { 1176 return _registers.getRegister(regNum); 1177 } 1178 1179 template <typename A, typename R> 1180 void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) { 1181 _registers.setRegister(regNum, (typename A::pint_t)value); 1182 } 1183 1184 template <typename A, typename R> 1185 bool UnwindCursor<A, R>::validFloatReg(int regNum) { 1186 return _registers.validFloatRegister(regNum); 1187 } 1188 1189 template <typename A, typename R> 1190 unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) { 1191 return _registers.getFloatRegister(regNum); 1192 } 1193 1194 template <typename A, typename R> 1195 void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) { 1196 _registers.setFloatRegister(regNum, value); 1197 } 1198 1199 template <typename A, typename R> void UnwindCursor<A, R>::jumpto() { 1200 _registers.jumpto(); 1201 } 1202 1203 #ifdef __arm__ 1204 template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() { 1205 _registers.saveVFPAsX(); 1206 } 1207 #endif 1208 1209 template <typename A, typename R> 1210 const char *UnwindCursor<A, R>::getRegisterName(int regNum) { 1211 return _registers.getRegisterName(regNum); 1212 } 1213 1214 template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() { 1215 return _isSignalFrame; 1216 } 1217 1218 #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 1219 1220 #if defined(_LIBUNWIND_ARM_EHABI) 1221 struct EHABIIndexEntry { 1222 uint32_t functionOffset; 1223 uint32_t data; 1224 }; 1225 1226 template<typename A> 1227 struct EHABISectionIterator { 1228 typedef EHABISectionIterator _Self; 1229 1230 typedef std::random_access_iterator_tag iterator_category; 1231 typedef typename A::pint_t value_type; 1232 typedef typename A::pint_t* pointer; 1233 typedef typename A::pint_t& reference; 1234 typedef size_t size_type; 1235 typedef size_t difference_type; 1236 1237 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) { 1238 return _Self(addressSpace, sects, 0); 1239 } 1240 static _Self end(A& addressSpace, const UnwindInfoSections& sects) { 1241 return _Self(addressSpace, sects, 1242 sects.arm_section_length / sizeof(EHABIIndexEntry)); 1243 } 1244 1245 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i) 1246 : _i(i), _addressSpace(&addressSpace), _sects(§s) {} 1247 1248 _Self& operator++() { ++_i; return *this; } 1249 _Self& operator+=(size_t a) { _i += a; return *this; } 1250 _Self& operator--() { assert(_i > 0); --_i; return *this; } 1251 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; } 1252 1253 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; } 1254 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; } 1255 1256 size_t operator-(const _Self& other) { return _i - other._i; } 1257 1258 bool operator==(const _Self& other) const { 1259 assert(_addressSpace == other._addressSpace); 1260 assert(_sects == other._sects); 1261 return _i == other._i; 1262 } 1263 1264 typename A::pint_t operator*() const { return functionAddress(); } 1265 1266 typename A::pint_t functionAddress() const { 1267 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( 1268 EHABIIndexEntry, _i, functionOffset); 1269 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr)); 1270 } 1271 1272 typename A::pint_t dataAddress() { 1273 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( 1274 EHABIIndexEntry, _i, data); 1275 return indexAddr; 1276 } 1277 1278 private: 1279 size_t _i; 1280 A* _addressSpace; 1281 const UnwindInfoSections* _sects; 1282 }; 1283 1284 template <typename A, typename R> 1285 bool UnwindCursor<A, R>::getInfoFromEHABISection( 1286 pint_t pc, 1287 const UnwindInfoSections §s) { 1288 EHABISectionIterator<A> begin = 1289 EHABISectionIterator<A>::begin(_addressSpace, sects); 1290 EHABISectionIterator<A> end = 1291 EHABISectionIterator<A>::end(_addressSpace, sects); 1292 if (begin == end) 1293 return false; 1294 1295 EHABISectionIterator<A> itNextPC = std::upper_bound(begin, end, pc); 1296 if (itNextPC == begin) 1297 return false; 1298 EHABISectionIterator<A> itThisPC = itNextPC - 1; 1299 1300 pint_t thisPC = itThisPC.functionAddress(); 1301 // If an exception is thrown from a function, corresponding to the last entry 1302 // in the table, we don't really know the function extent and have to choose a 1303 // value for nextPC. Choosing max() will allow the range check during trace to 1304 // succeed. 1305 pint_t nextPC = (itNextPC == end) ? std::numeric_limits<pint_t>::max() 1306 : itNextPC.functionAddress(); 1307 pint_t indexDataAddr = itThisPC.dataAddress(); 1308 1309 if (indexDataAddr == 0) 1310 return false; 1311 1312 uint32_t indexData = _addressSpace.get32(indexDataAddr); 1313 if (indexData == UNW_EXIDX_CANTUNWIND) 1314 return false; 1315 1316 // If the high bit is set, the exception handling table entry is inline inside 1317 // the index table entry on the second word (aka |indexDataAddr|). Otherwise, 1318 // the table points at an offset in the exception handling table (section 5 EHABI). 1319 pint_t exceptionTableAddr; 1320 uint32_t exceptionTableData; 1321 bool isSingleWordEHT; 1322 if (indexData & 0x80000000) { 1323 exceptionTableAddr = indexDataAddr; 1324 // TODO(ajwong): Should this data be 0? 1325 exceptionTableData = indexData; 1326 isSingleWordEHT = true; 1327 } else { 1328 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData); 1329 exceptionTableData = _addressSpace.get32(exceptionTableAddr); 1330 isSingleWordEHT = false; 1331 } 1332 1333 // Now we know the 3 things: 1334 // exceptionTableAddr -- exception handler table entry. 1335 // exceptionTableData -- the data inside the first word of the eht entry. 1336 // isSingleWordEHT -- whether the entry is in the index. 1337 unw_word_t personalityRoutine = 0xbadf00d; 1338 bool scope32 = false; 1339 uintptr_t lsda; 1340 1341 // If the high bit in the exception handling table entry is set, the entry is 1342 // in compact form (section 6.3 EHABI). 1343 if (exceptionTableData & 0x80000000) { 1344 // Grab the index of the personality routine from the compact form. 1345 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24; 1346 uint32_t extraWords = 0; 1347 switch (choice) { 1348 case 0: 1349 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0; 1350 extraWords = 0; 1351 scope32 = false; 1352 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4); 1353 break; 1354 case 1: 1355 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1; 1356 extraWords = (exceptionTableData & 0x00ff0000) >> 16; 1357 scope32 = false; 1358 lsda = exceptionTableAddr + (extraWords + 1) * 4; 1359 break; 1360 case 2: 1361 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2; 1362 extraWords = (exceptionTableData & 0x00ff0000) >> 16; 1363 scope32 = true; 1364 lsda = exceptionTableAddr + (extraWords + 1) * 4; 1365 break; 1366 default: 1367 _LIBUNWIND_ABORT("unknown personality routine"); 1368 return false; 1369 } 1370 1371 if (isSingleWordEHT) { 1372 if (extraWords != 0) { 1373 _LIBUNWIND_ABORT("index inlined table detected but pr function " 1374 "requires extra words"); 1375 return false; 1376 } 1377 } 1378 } else { 1379 pint_t personalityAddr = 1380 exceptionTableAddr + signExtendPrel31(exceptionTableData); 1381 personalityRoutine = personalityAddr; 1382 1383 // ARM EHABI # 6.2, # 9.2 1384 // 1385 // +---- ehtp 1386 // v 1387 // +--------------------------------------+ 1388 // | +--------+--------+--------+-------+ | 1389 // | |0| prel31 to personalityRoutine | | 1390 // | +--------+--------+--------+-------+ | 1391 // | | N | unwind opcodes | | <-- UnwindData 1392 // | +--------+--------+--------+-------+ | 1393 // | | Word 2 unwind opcodes | | 1394 // | +--------+--------+--------+-------+ | 1395 // | ... | 1396 // | +--------+--------+--------+-------+ | 1397 // | | Word N unwind opcodes | | 1398 // | +--------+--------+--------+-------+ | 1399 // | | LSDA | | <-- lsda 1400 // | | ... | | 1401 // | +--------+--------+--------+-------+ | 1402 // +--------------------------------------+ 1403 1404 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1; 1405 uint32_t FirstDataWord = *UnwindData; 1406 size_t N = ((FirstDataWord >> 24) & 0xff); 1407 size_t NDataWords = N + 1; 1408 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords); 1409 } 1410 1411 _info.start_ip = thisPC; 1412 _info.end_ip = nextPC; 1413 _info.handler = personalityRoutine; 1414 _info.unwind_info = exceptionTableAddr; 1415 _info.lsda = lsda; 1416 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0. 1417 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum? 1418 1419 return true; 1420 } 1421 #endif 1422 1423 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1424 template <typename A, typename R> 1425 bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc, 1426 const UnwindInfoSections §s, 1427 uint32_t fdeSectionOffsetHint) { 1428 typename CFI_Parser<A>::FDE_Info fdeInfo; 1429 typename CFI_Parser<A>::CIE_Info cieInfo; 1430 bool foundFDE = false; 1431 bool foundInCache = false; 1432 // If compact encoding table gave offset into dwarf section, go directly there 1433 if (fdeSectionOffsetHint != 0) { 1434 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 1435 (uint32_t)sects.dwarf_section_length, 1436 sects.dwarf_section + fdeSectionOffsetHint, 1437 &fdeInfo, &cieInfo); 1438 } 1439 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) 1440 if (!foundFDE && (sects.dwarf_index_section != 0)) { 1441 foundFDE = EHHeaderParser<A>::findFDE( 1442 _addressSpace, pc, sects.dwarf_index_section, 1443 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo); 1444 } 1445 #endif 1446 if (!foundFDE) { 1447 // otherwise, search cache of previously found FDEs. 1448 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc); 1449 if (cachedFDE != 0) { 1450 foundFDE = 1451 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 1452 (uint32_t)sects.dwarf_section_length, 1453 cachedFDE, &fdeInfo, &cieInfo); 1454 foundInCache = foundFDE; 1455 } 1456 } 1457 if (!foundFDE) { 1458 // Still not found, do full scan of __eh_frame section. 1459 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 1460 (uint32_t)sects.dwarf_section_length, 0, 1461 &fdeInfo, &cieInfo); 1462 } 1463 if (foundFDE) { 1464 typename CFI_Parser<A>::PrologInfo prolog; 1465 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc, 1466 R::getArch(), &prolog)) { 1467 // Save off parsed FDE info 1468 _info.start_ip = fdeInfo.pcStart; 1469 _info.end_ip = fdeInfo.pcEnd; 1470 _info.lsda = fdeInfo.lsda; 1471 _info.handler = cieInfo.personality; 1472 _info.gp = prolog.spExtraArgSize; 1473 _info.flags = 0; 1474 _info.format = dwarfEncoding(); 1475 _info.unwind_info = fdeInfo.fdeStart; 1476 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength; 1477 _info.extra = (unw_word_t) sects.dso_base; 1478 1479 // Add to cache (to make next lookup faster) if we had no hint 1480 // and there was no index. 1481 if (!foundInCache && (fdeSectionOffsetHint == 0)) { 1482 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) 1483 if (sects.dwarf_index_section == 0) 1484 #endif 1485 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd, 1486 fdeInfo.fdeStart); 1487 } 1488 return true; 1489 } 1490 } 1491 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc); 1492 return false; 1493 } 1494 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1495 1496 1497 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 1498 template <typename A, typename R> 1499 bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc, 1500 const UnwindInfoSections §s) { 1501 const bool log = false; 1502 if (log) 1503 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n", 1504 (uint64_t)pc, (uint64_t)sects.dso_base); 1505 1506 const UnwindSectionHeader<A> sectionHeader(_addressSpace, 1507 sects.compact_unwind_section); 1508 if (sectionHeader.version() != UNWIND_SECTION_VERSION) 1509 return false; 1510 1511 // do a binary search of top level index to find page with unwind info 1512 pint_t targetFunctionOffset = pc - sects.dso_base; 1513 const UnwindSectionIndexArray<A> topIndex(_addressSpace, 1514 sects.compact_unwind_section 1515 + sectionHeader.indexSectionOffset()); 1516 uint32_t low = 0; 1517 uint32_t high = sectionHeader.indexCount(); 1518 uint32_t last = high - 1; 1519 while (low < high) { 1520 uint32_t mid = (low + high) / 2; 1521 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n", 1522 //mid, low, high, topIndex.functionOffset(mid)); 1523 if (topIndex.functionOffset(mid) <= targetFunctionOffset) { 1524 if ((mid == last) || 1525 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) { 1526 low = mid; 1527 break; 1528 } else { 1529 low = mid + 1; 1530 } 1531 } else { 1532 high = mid; 1533 } 1534 } 1535 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low); 1536 const uint32_t firstLevelNextPageFunctionOffset = 1537 topIndex.functionOffset(low + 1); 1538 const pint_t secondLevelAddr = 1539 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low); 1540 const pint_t lsdaArrayStartAddr = 1541 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low); 1542 const pint_t lsdaArrayEndAddr = 1543 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1); 1544 if (log) 1545 fprintf(stderr, "\tfirst level search for result index=%d " 1546 "to secondLevelAddr=0x%llX\n", 1547 low, (uint64_t) secondLevelAddr); 1548 // do a binary search of second level page index 1549 uint32_t encoding = 0; 1550 pint_t funcStart = 0; 1551 pint_t funcEnd = 0; 1552 pint_t lsda = 0; 1553 pint_t personality = 0; 1554 uint32_t pageKind = _addressSpace.get32(secondLevelAddr); 1555 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) { 1556 // regular page 1557 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace, 1558 secondLevelAddr); 1559 UnwindSectionRegularArray<A> pageIndex( 1560 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); 1561 // binary search looks for entry with e where index[e].offset <= pc < 1562 // index[e+1].offset 1563 if (log) 1564 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in " 1565 "regular page starting at secondLevelAddr=0x%llX\n", 1566 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr); 1567 low = 0; 1568 high = pageHeader.entryCount(); 1569 while (low < high) { 1570 uint32_t mid = (low + high) / 2; 1571 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) { 1572 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) { 1573 // at end of table 1574 low = mid; 1575 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; 1576 break; 1577 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) { 1578 // next is too big, so we found it 1579 low = mid; 1580 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base; 1581 break; 1582 } else { 1583 low = mid + 1; 1584 } 1585 } else { 1586 high = mid; 1587 } 1588 } 1589 encoding = pageIndex.encoding(low); 1590 funcStart = pageIndex.functionOffset(low) + sects.dso_base; 1591 if (pc < funcStart) { 1592 if (log) 1593 fprintf( 1594 stderr, 1595 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", 1596 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); 1597 return false; 1598 } 1599 if (pc > funcEnd) { 1600 if (log) 1601 fprintf( 1602 stderr, 1603 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", 1604 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); 1605 return false; 1606 } 1607 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) { 1608 // compressed page 1609 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace, 1610 secondLevelAddr); 1611 UnwindSectionCompressedArray<A> pageIndex( 1612 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); 1613 const uint32_t targetFunctionPageOffset = 1614 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset); 1615 // binary search looks for entry with e where index[e].offset <= pc < 1616 // index[e+1].offset 1617 if (log) 1618 fprintf(stderr, "\tbinary search of compressed page starting at " 1619 "secondLevelAddr=0x%llX\n", 1620 (uint64_t) secondLevelAddr); 1621 low = 0; 1622 last = pageHeader.entryCount() - 1; 1623 high = pageHeader.entryCount(); 1624 while (low < high) { 1625 uint32_t mid = (low + high) / 2; 1626 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) { 1627 if ((mid == last) || 1628 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) { 1629 low = mid; 1630 break; 1631 } else { 1632 low = mid + 1; 1633 } 1634 } else { 1635 high = mid; 1636 } 1637 } 1638 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset 1639 + sects.dso_base; 1640 if (low < last) 1641 funcEnd = 1642 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset 1643 + sects.dso_base; 1644 else 1645 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; 1646 if (pc < funcStart) { 1647 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second " 1648 "level compressed unwind table. funcStart=0x%llX", 1649 (uint64_t) pc, (uint64_t) funcStart); 1650 return false; 1651 } 1652 if (pc > funcEnd) { 1653 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second " 1654 "level compressed unwind table. funcEnd=0x%llX", 1655 (uint64_t) pc, (uint64_t) funcEnd); 1656 return false; 1657 } 1658 uint16_t encodingIndex = pageIndex.encodingIndex(low); 1659 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) { 1660 // encoding is in common table in section header 1661 encoding = _addressSpace.get32( 1662 sects.compact_unwind_section + 1663 sectionHeader.commonEncodingsArraySectionOffset() + 1664 encodingIndex * sizeof(uint32_t)); 1665 } else { 1666 // encoding is in page specific table 1667 uint16_t pageEncodingIndex = 1668 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount(); 1669 encoding = _addressSpace.get32(secondLevelAddr + 1670 pageHeader.encodingsPageOffset() + 1671 pageEncodingIndex * sizeof(uint32_t)); 1672 } 1673 } else { 1674 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second " 1675 "level page", 1676 (uint64_t) sects.compact_unwind_section); 1677 return false; 1678 } 1679 1680 // look up LSDA, if encoding says function has one 1681 if (encoding & UNWIND_HAS_LSDA) { 1682 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr); 1683 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base); 1684 low = 0; 1685 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) / 1686 sizeof(unwind_info_section_header_lsda_index_entry); 1687 // binary search looks for entry with exact match for functionOffset 1688 if (log) 1689 fprintf(stderr, 1690 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n", 1691 funcStartOffset); 1692 while (low < high) { 1693 uint32_t mid = (low + high) / 2; 1694 if (lsdaIndex.functionOffset(mid) == funcStartOffset) { 1695 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base; 1696 break; 1697 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) { 1698 low = mid + 1; 1699 } else { 1700 high = mid; 1701 } 1702 } 1703 if (lsda == 0) { 1704 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for " 1705 "pc=0x%0llX, but lsda table has no entry", 1706 encoding, (uint64_t) pc); 1707 return false; 1708 } 1709 } 1710 1711 // extact personality routine, if encoding says function has one 1712 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >> 1713 (__builtin_ctz(UNWIND_PERSONALITY_MASK)); 1714 if (personalityIndex != 0) { 1715 --personalityIndex; // change 1-based to zero-based index 1716 if (personalityIndex > sectionHeader.personalityArrayCount()) { 1717 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, " 1718 "but personality table has only %d entires", 1719 encoding, personalityIndex, 1720 sectionHeader.personalityArrayCount()); 1721 return false; 1722 } 1723 int32_t personalityDelta = (int32_t)_addressSpace.get32( 1724 sects.compact_unwind_section + 1725 sectionHeader.personalityArraySectionOffset() + 1726 personalityIndex * sizeof(uint32_t)); 1727 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta; 1728 personality = _addressSpace.getP(personalityPointer); 1729 if (log) 1730 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " 1731 "personalityDelta=0x%08X, personality=0x%08llX\n", 1732 (uint64_t) pc, personalityDelta, (uint64_t) personality); 1733 } 1734 1735 if (log) 1736 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " 1737 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n", 1738 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart); 1739 _info.start_ip = funcStart; 1740 _info.end_ip = funcEnd; 1741 _info.lsda = lsda; 1742 _info.handler = personality; 1743 _info.gp = 0; 1744 _info.flags = 0; 1745 _info.format = encoding; 1746 _info.unwind_info = 0; 1747 _info.unwind_info_size = 0; 1748 _info.extra = sects.dso_base; 1749 return true; 1750 } 1751 #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 1752 1753 1754 #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 1755 template <typename A, typename R> 1756 bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) { 1757 pint_t base; 1758 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base); 1759 if (!unwindEntry) { 1760 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc); 1761 return false; 1762 } 1763 _info.gp = 0; 1764 _info.flags = 0; 1765 _info.format = 0; 1766 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION); 1767 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry); 1768 _info.extra = base; 1769 _info.start_ip = base + unwindEntry->BeginAddress; 1770 #ifdef _LIBUNWIND_TARGET_X86_64 1771 _info.end_ip = base + unwindEntry->EndAddress; 1772 // Only fill in the handler and LSDA if they're stale. 1773 if (pc != getLastPC()) { 1774 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData); 1775 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) { 1776 // The personality is given in the UNWIND_INFO itself. The LSDA immediately 1777 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit 1778 // these structures.) 1779 // N.B. UNWIND_INFO structs are DWORD-aligned. 1780 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1; 1781 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]); 1782 _info.lsda = reinterpret_cast<unw_word_t>(handler+1); 1783 if (*handler) { 1784 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality); 1785 } else 1786 _info.handler = 0; 1787 } else { 1788 _info.lsda = 0; 1789 _info.handler = 0; 1790 } 1791 } 1792 #elif defined(_LIBUNWIND_TARGET_ARM) 1793 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength; 1794 _info.lsda = 0; // FIXME 1795 _info.handler = 0; // FIXME 1796 #endif 1797 setLastPC(pc); 1798 return true; 1799 } 1800 #endif 1801 1802 1803 template <typename A, typename R> 1804 void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) { 1805 pint_t pc = (pint_t)this->getReg(UNW_REG_IP); 1806 #if defined(_LIBUNWIND_ARM_EHABI) 1807 // Remove the thumb bit so the IP represents the actual instruction address. 1808 // This matches the behaviour of _Unwind_GetIP on arm. 1809 pc &= (pint_t)~0x1; 1810 #endif 1811 1812 // If the last line of a function is a "throw" the compiler sometimes 1813 // emits no instructions after the call to __cxa_throw. This means 1814 // the return address is actually the start of the next function. 1815 // To disambiguate this, back up the pc when we know it is a return 1816 // address. 1817 if (isReturnAddress) 1818 --pc; 1819 1820 // Ask address space object to find unwind sections for this pc. 1821 UnwindInfoSections sects; 1822 if (_addressSpace.findUnwindSections(pc, sects)) { 1823 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 1824 // If there is a compact unwind encoding table, look there first. 1825 if (sects.compact_unwind_section != 0) { 1826 if (this->getInfoFromCompactEncodingSection(pc, sects)) { 1827 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1828 // Found info in table, done unless encoding says to use dwarf. 1829 uint32_t dwarfOffset; 1830 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) { 1831 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) { 1832 // found info in dwarf, done 1833 return; 1834 } 1835 } 1836 #endif 1837 // If unwind table has entry, but entry says there is no unwind info, 1838 // record that we have no unwind info. 1839 if (_info.format == 0) 1840 _unwindInfoMissing = true; 1841 return; 1842 } 1843 } 1844 #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 1845 1846 #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 1847 // If there is SEH unwind info, look there next. 1848 if (this->getInfoFromSEH(pc)) 1849 return; 1850 #endif 1851 1852 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1853 // If there is dwarf unwind info, look there next. 1854 if (sects.dwarf_section != 0) { 1855 if (this->getInfoFromDwarfSection(pc, sects)) { 1856 // found info in dwarf, done 1857 return; 1858 } 1859 } 1860 #endif 1861 1862 #if defined(_LIBUNWIND_ARM_EHABI) 1863 // If there is ARM EHABI unwind info, look there next. 1864 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects)) 1865 return; 1866 #endif 1867 } 1868 1869 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1870 // There is no static unwind info for this pc. Look to see if an FDE was 1871 // dynamically registered for it. 1872 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc); 1873 if (cachedFDE != 0) { 1874 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; 1875 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; 1876 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace, 1877 cachedFDE, &fdeInfo, &cieInfo); 1878 if (msg == NULL) { 1879 typename CFI_Parser<A>::PrologInfo prolog; 1880 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, 1881 pc, R::getArch(), &prolog)) { 1882 // save off parsed FDE info 1883 _info.start_ip = fdeInfo.pcStart; 1884 _info.end_ip = fdeInfo.pcEnd; 1885 _info.lsda = fdeInfo.lsda; 1886 _info.handler = cieInfo.personality; 1887 _info.gp = prolog.spExtraArgSize; 1888 // Some frameless functions need SP 1889 // altered when resuming in function. 1890 _info.flags = 0; 1891 _info.format = dwarfEncoding(); 1892 _info.unwind_info = fdeInfo.fdeStart; 1893 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength; 1894 _info.extra = 0; 1895 return; 1896 } 1897 } 1898 } 1899 1900 // Lastly, ask AddressSpace object about platform specific ways to locate 1901 // other FDEs. 1902 pint_t fde; 1903 if (_addressSpace.findOtherFDE(pc, fde)) { 1904 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; 1905 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; 1906 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) { 1907 // Double check this FDE is for a function that includes the pc. 1908 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) { 1909 typename CFI_Parser<A>::PrologInfo prolog; 1910 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, 1911 pc, R::getArch(), &prolog)) { 1912 // save off parsed FDE info 1913 _info.start_ip = fdeInfo.pcStart; 1914 _info.end_ip = fdeInfo.pcEnd; 1915 _info.lsda = fdeInfo.lsda; 1916 _info.handler = cieInfo.personality; 1917 _info.gp = prolog.spExtraArgSize; 1918 _info.flags = 0; 1919 _info.format = dwarfEncoding(); 1920 _info.unwind_info = fdeInfo.fdeStart; 1921 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength; 1922 _info.extra = 0; 1923 return; 1924 } 1925 } 1926 } 1927 } 1928 #endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1929 1930 // no unwind info, flag that we can't reliably unwind 1931 _unwindInfoMissing = true; 1932 } 1933 1934 template <typename A, typename R> 1935 int UnwindCursor<A, R>::step() { 1936 // Bottom of stack is defined is when unwind info cannot be found. 1937 if (_unwindInfoMissing) 1938 return UNW_STEP_END; 1939 1940 // Use unwinding info to modify register set as if function returned. 1941 int result; 1942 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 1943 result = this->stepWithCompactEncoding(); 1944 #elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) 1945 result = this->stepWithSEHData(); 1946 #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1947 result = this->stepWithDwarfFDE(); 1948 #elif defined(_LIBUNWIND_ARM_EHABI) 1949 result = this->stepWithEHABI(); 1950 #else 1951 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \ 1952 _LIBUNWIND_SUPPORT_SEH_UNWIND or \ 1953 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \ 1954 _LIBUNWIND_ARM_EHABI 1955 #endif 1956 1957 // update info based on new PC 1958 if (result == UNW_STEP_SUCCESS) { 1959 this->setInfoBasedOnIPRegister(true); 1960 if (_unwindInfoMissing) 1961 return UNW_STEP_END; 1962 } 1963 1964 return result; 1965 } 1966 1967 template <typename A, typename R> 1968 void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) { 1969 *info = _info; 1970 } 1971 1972 template <typename A, typename R> 1973 bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen, 1974 unw_word_t *offset) { 1975 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP), 1976 buf, bufLen, offset); 1977 } 1978 1979 } // namespace libunwind 1980 1981 #endif // __UNWINDCURSOR_HPP__ 1982