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 #ifndef _LIBUNWIND_HAS_NO_THREADS 20 #include <pthread.h> 21 #endif 22 #include <unwind.h> 23 24 #ifdef __APPLE__ 25 #include <mach-o/dyld.h> 26 #endif 27 28 #include "config.h" 29 30 #include "AddressSpace.hpp" 31 #include "CompactUnwinder.hpp" 32 #include "config.h" 33 #include "DwarfInstructions.hpp" 34 #include "EHHeaderParser.hpp" 35 #include "libunwind.h" 36 #include "Registers.hpp" 37 #include "Unwind-EHABI.h" 38 39 namespace libunwind { 40 41 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 42 /// Cache of recently found FDEs. 43 template <typename A> 44 class _LIBUNWIND_HIDDEN DwarfFDECache { 45 typedef typename A::pint_t pint_t; 46 public: 47 static pint_t findFDE(pint_t mh, pint_t pc); 48 static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde); 49 static void removeAllIn(pint_t mh); 50 static void iterateCacheEntries(void (*func)(unw_word_t ip_start, 51 unw_word_t ip_end, 52 unw_word_t fde, unw_word_t mh)); 53 54 private: 55 56 struct entry { 57 pint_t mh; 58 pint_t ip_start; 59 pint_t ip_end; 60 pint_t fde; 61 }; 62 63 // These fields are all static to avoid needing an initializer. 64 // There is only one instance of this class per process. 65 #ifndef _LIBUNWIND_HAS_NO_THREADS 66 static pthread_rwlock_t _lock; 67 #endif 68 #ifdef __APPLE__ 69 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide); 70 static bool _registeredForDyldUnloads; 71 #endif 72 // Can't use std::vector<> here because this code is below libc++. 73 static entry *_buffer; 74 static entry *_bufferUsed; 75 static entry *_bufferEnd; 76 static entry _initialBuffer[64]; 77 }; 78 79 template <typename A> 80 typename DwarfFDECache<A>::entry * 81 DwarfFDECache<A>::_buffer = _initialBuffer; 82 83 template <typename A> 84 typename DwarfFDECache<A>::entry * 85 DwarfFDECache<A>::_bufferUsed = _initialBuffer; 86 87 template <typename A> 88 typename DwarfFDECache<A>::entry * 89 DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64]; 90 91 template <typename A> 92 typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64]; 93 94 #ifndef _LIBUNWIND_HAS_NO_THREADS 95 template <typename A> 96 pthread_rwlock_t DwarfFDECache<A>::_lock = PTHREAD_RWLOCK_INITIALIZER; 97 #endif 98 99 #ifdef __APPLE__ 100 template <typename A> 101 bool DwarfFDECache<A>::_registeredForDyldUnloads = false; 102 #endif 103 104 template <typename A> 105 typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) { 106 pint_t result = 0; 107 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_rdlock(&_lock)); 108 for (entry *p = _buffer; p < _bufferUsed; ++p) { 109 if ((mh == p->mh) || (mh == 0)) { 110 if ((p->ip_start <= pc) && (pc < p->ip_end)) { 111 result = p->fde; 112 break; 113 } 114 } 115 } 116 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock)); 117 return result; 118 } 119 120 template <typename A> 121 void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end, 122 pint_t fde) { 123 #if !defined(_LIBUNWIND_NO_HEAP) 124 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock)); 125 if (_bufferUsed >= _bufferEnd) { 126 size_t oldSize = (size_t)(_bufferEnd - _buffer); 127 size_t newSize = oldSize * 4; 128 // Can't use operator new (we are below it). 129 entry *newBuffer = (entry *)malloc(newSize * sizeof(entry)); 130 memcpy(newBuffer, _buffer, oldSize * sizeof(entry)); 131 if (_buffer != _initialBuffer) 132 free(_buffer); 133 _buffer = newBuffer; 134 _bufferUsed = &newBuffer[oldSize]; 135 _bufferEnd = &newBuffer[newSize]; 136 } 137 _bufferUsed->mh = mh; 138 _bufferUsed->ip_start = ip_start; 139 _bufferUsed->ip_end = ip_end; 140 _bufferUsed->fde = fde; 141 ++_bufferUsed; 142 #ifdef __APPLE__ 143 if (!_registeredForDyldUnloads) { 144 _dyld_register_func_for_remove_image(&dyldUnloadHook); 145 _registeredForDyldUnloads = true; 146 } 147 #endif 148 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock)); 149 #endif 150 } 151 152 template <typename A> 153 void DwarfFDECache<A>::removeAllIn(pint_t mh) { 154 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock)); 155 entry *d = _buffer; 156 for (const entry *s = _buffer; s < _bufferUsed; ++s) { 157 if (s->mh != mh) { 158 if (d != s) 159 *d = *s; 160 ++d; 161 } 162 } 163 _bufferUsed = d; 164 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock)); 165 } 166 167 #ifdef __APPLE__ 168 template <typename A> 169 void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) { 170 removeAllIn((pint_t) mh); 171 } 172 #endif 173 174 template <typename A> 175 void DwarfFDECache<A>::iterateCacheEntries(void (*func)( 176 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) { 177 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock)); 178 for (entry *p = _buffer; p < _bufferUsed; ++p) { 179 (*func)(p->ip_start, p->ip_end, p->fde, p->mh); 180 } 181 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock)); 182 } 183 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 184 185 186 #define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field)) 187 188 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 189 template <typename A> class UnwindSectionHeader { 190 public: 191 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr) 192 : _addressSpace(addressSpace), _addr(addr) {} 193 194 uint32_t version() const { 195 return _addressSpace.get32(_addr + 196 offsetof(unwind_info_section_header, version)); 197 } 198 uint32_t commonEncodingsArraySectionOffset() const { 199 return _addressSpace.get32(_addr + 200 offsetof(unwind_info_section_header, 201 commonEncodingsArraySectionOffset)); 202 } 203 uint32_t commonEncodingsArrayCount() const { 204 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header, 205 commonEncodingsArrayCount)); 206 } 207 uint32_t personalityArraySectionOffset() const { 208 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header, 209 personalityArraySectionOffset)); 210 } 211 uint32_t personalityArrayCount() const { 212 return _addressSpace.get32( 213 _addr + offsetof(unwind_info_section_header, personalityArrayCount)); 214 } 215 uint32_t indexSectionOffset() const { 216 return _addressSpace.get32( 217 _addr + offsetof(unwind_info_section_header, indexSectionOffset)); 218 } 219 uint32_t indexCount() const { 220 return _addressSpace.get32( 221 _addr + offsetof(unwind_info_section_header, indexCount)); 222 } 223 224 private: 225 A &_addressSpace; 226 typename A::pint_t _addr; 227 }; 228 229 template <typename A> class UnwindSectionIndexArray { 230 public: 231 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr) 232 : _addressSpace(addressSpace), _addr(addr) {} 233 234 uint32_t functionOffset(uint32_t index) const { 235 return _addressSpace.get32( 236 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, 237 functionOffset)); 238 } 239 uint32_t secondLevelPagesSectionOffset(uint32_t index) const { 240 return _addressSpace.get32( 241 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, 242 secondLevelPagesSectionOffset)); 243 } 244 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const { 245 return _addressSpace.get32( 246 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, 247 lsdaIndexArraySectionOffset)); 248 } 249 250 private: 251 A &_addressSpace; 252 typename A::pint_t _addr; 253 }; 254 255 template <typename A> class UnwindSectionRegularPageHeader { 256 public: 257 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr) 258 : _addressSpace(addressSpace), _addr(addr) {} 259 260 uint32_t kind() const { 261 return _addressSpace.get32( 262 _addr + offsetof(unwind_info_regular_second_level_page_header, kind)); 263 } 264 uint16_t entryPageOffset() const { 265 return _addressSpace.get16( 266 _addr + offsetof(unwind_info_regular_second_level_page_header, 267 entryPageOffset)); 268 } 269 uint16_t entryCount() const { 270 return _addressSpace.get16( 271 _addr + 272 offsetof(unwind_info_regular_second_level_page_header, entryCount)); 273 } 274 275 private: 276 A &_addressSpace; 277 typename A::pint_t _addr; 278 }; 279 280 template <typename A> class UnwindSectionRegularArray { 281 public: 282 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr) 283 : _addressSpace(addressSpace), _addr(addr) {} 284 285 uint32_t functionOffset(uint32_t index) const { 286 return _addressSpace.get32( 287 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index, 288 functionOffset)); 289 } 290 uint32_t encoding(uint32_t index) const { 291 return _addressSpace.get32( 292 _addr + 293 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding)); 294 } 295 296 private: 297 A &_addressSpace; 298 typename A::pint_t _addr; 299 }; 300 301 template <typename A> class UnwindSectionCompressedPageHeader { 302 public: 303 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr) 304 : _addressSpace(addressSpace), _addr(addr) {} 305 306 uint32_t kind() const { 307 return _addressSpace.get32( 308 _addr + 309 offsetof(unwind_info_compressed_second_level_page_header, kind)); 310 } 311 uint16_t entryPageOffset() const { 312 return _addressSpace.get16( 313 _addr + offsetof(unwind_info_compressed_second_level_page_header, 314 entryPageOffset)); 315 } 316 uint16_t entryCount() const { 317 return _addressSpace.get16( 318 _addr + 319 offsetof(unwind_info_compressed_second_level_page_header, entryCount)); 320 } 321 uint16_t encodingsPageOffset() const { 322 return _addressSpace.get16( 323 _addr + offsetof(unwind_info_compressed_second_level_page_header, 324 encodingsPageOffset)); 325 } 326 uint16_t encodingsCount() const { 327 return _addressSpace.get16( 328 _addr + offsetof(unwind_info_compressed_second_level_page_header, 329 encodingsCount)); 330 } 331 332 private: 333 A &_addressSpace; 334 typename A::pint_t _addr; 335 }; 336 337 template <typename A> class UnwindSectionCompressedArray { 338 public: 339 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr) 340 : _addressSpace(addressSpace), _addr(addr) {} 341 342 uint32_t functionOffset(uint32_t index) const { 343 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET( 344 _addressSpace.get32(_addr + index * sizeof(uint32_t))); 345 } 346 uint16_t encodingIndex(uint32_t index) const { 347 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX( 348 _addressSpace.get32(_addr + index * sizeof(uint32_t))); 349 } 350 351 private: 352 A &_addressSpace; 353 typename A::pint_t _addr; 354 }; 355 356 template <typename A> class UnwindSectionLsdaArray { 357 public: 358 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr) 359 : _addressSpace(addressSpace), _addr(addr) {} 360 361 uint32_t functionOffset(uint32_t index) const { 362 return _addressSpace.get32( 363 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry, 364 index, functionOffset)); 365 } 366 uint32_t lsdaOffset(uint32_t index) const { 367 return _addressSpace.get32( 368 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry, 369 index, lsdaOffset)); 370 } 371 372 private: 373 A &_addressSpace; 374 typename A::pint_t _addr; 375 }; 376 #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 377 378 class _LIBUNWIND_HIDDEN AbstractUnwindCursor { 379 public: 380 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20) 381 // This avoids an unnecessary dependency to libc++abi. 382 void operator delete(void *, size_t) {} 383 384 virtual ~AbstractUnwindCursor() {} 385 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); } 386 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); } 387 virtual void setReg(int, unw_word_t) { 388 _LIBUNWIND_ABORT("setReg not implemented"); 389 } 390 virtual bool validFloatReg(int) { 391 _LIBUNWIND_ABORT("validFloatReg not implemented"); 392 } 393 virtual unw_fpreg_t getFloatReg(int) { 394 _LIBUNWIND_ABORT("getFloatReg not implemented"); 395 } 396 virtual void setFloatReg(int, unw_fpreg_t) { 397 _LIBUNWIND_ABORT("setFloatReg not implemented"); 398 } 399 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); } 400 virtual void getInfo(unw_proc_info_t *) { 401 _LIBUNWIND_ABORT("getInfo not implemented"); 402 } 403 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); } 404 virtual bool isSignalFrame() { 405 _LIBUNWIND_ABORT("isSignalFrame not implemented"); 406 } 407 virtual bool getFunctionName(char *, size_t, unw_word_t *) { 408 _LIBUNWIND_ABORT("getFunctionName not implemented"); 409 } 410 virtual void setInfoBasedOnIPRegister(bool = false) { 411 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented"); 412 } 413 virtual const char *getRegisterName(int) { 414 _LIBUNWIND_ABORT("getRegisterName not implemented"); 415 } 416 #ifdef __arm__ 417 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); } 418 #endif 419 }; 420 421 /// UnwindCursor contains all state (including all register values) during 422 /// an unwind. This is normally stack allocated inside a unw_cursor_t. 423 template <typename A, typename R> 424 class UnwindCursor : public AbstractUnwindCursor{ 425 typedef typename A::pint_t pint_t; 426 public: 427 UnwindCursor(unw_context_t *context, A &as); 428 UnwindCursor(A &as, void *threadArg); 429 virtual ~UnwindCursor() {} 430 virtual bool validReg(int); 431 virtual unw_word_t getReg(int); 432 virtual void setReg(int, unw_word_t); 433 virtual bool validFloatReg(int); 434 virtual unw_fpreg_t getFloatReg(int); 435 virtual void setFloatReg(int, unw_fpreg_t); 436 virtual int step(); 437 virtual void getInfo(unw_proc_info_t *); 438 virtual void jumpto(); 439 virtual bool isSignalFrame(); 440 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off); 441 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false); 442 virtual const char *getRegisterName(int num); 443 #ifdef __arm__ 444 virtual void saveVFPAsX(); 445 #endif 446 447 private: 448 449 #if defined(_LIBUNWIND_ARM_EHABI) 450 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections §s); 451 452 int stepWithEHABI() { 453 size_t len = 0; 454 size_t off = 0; 455 // FIXME: Calling decode_eht_entry() here is violating the libunwind 456 // abstraction layer. 457 const uint32_t *ehtp = 458 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info), 459 &off, &len); 460 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) != 461 _URC_CONTINUE_UNWIND) 462 return UNW_STEP_END; 463 return UNW_STEP_SUCCESS; 464 } 465 #endif 466 467 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 468 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections §s, 469 uint32_t fdeSectionOffsetHint=0); 470 int stepWithDwarfFDE() { 471 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace, 472 (pint_t)this->getReg(UNW_REG_IP), 473 (pint_t)_info.unwind_info, 474 _registers); 475 } 476 #endif 477 478 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 479 bool getInfoFromCompactEncodingSection(pint_t pc, 480 const UnwindInfoSections §s); 481 int stepWithCompactEncoding() { 482 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 483 if ( compactSaysUseDwarf() ) 484 return stepWithDwarfFDE(); 485 #endif 486 R dummy; 487 return stepWithCompactEncoding(dummy); 488 } 489 490 #if defined(_LIBUNWIND_TARGET_X86_64) 491 int stepWithCompactEncoding(Registers_x86_64 &) { 492 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding( 493 _info.format, _info.start_ip, _addressSpace, _registers); 494 } 495 #endif 496 497 #if defined(_LIBUNWIND_TARGET_I386) 498 int stepWithCompactEncoding(Registers_x86 &) { 499 return CompactUnwinder_x86<A>::stepWithCompactEncoding( 500 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers); 501 } 502 #endif 503 504 #if defined(_LIBUNWIND_TARGET_PPC) 505 int stepWithCompactEncoding(Registers_ppc &) { 506 return UNW_EINVAL; 507 } 508 #endif 509 510 #if defined(_LIBUNWIND_TARGET_AARCH64) 511 int stepWithCompactEncoding(Registers_arm64 &) { 512 return CompactUnwinder_arm64<A>::stepWithCompactEncoding( 513 _info.format, _info.start_ip, _addressSpace, _registers); 514 } 515 #endif 516 517 bool compactSaysUseDwarf(uint32_t *offset=NULL) const { 518 R dummy; 519 return compactSaysUseDwarf(dummy, offset); 520 } 521 522 #if defined(_LIBUNWIND_TARGET_X86_64) 523 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const { 524 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) { 525 if (offset) 526 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET); 527 return true; 528 } 529 return false; 530 } 531 #endif 532 533 #if defined(_LIBUNWIND_TARGET_I386) 534 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const { 535 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) { 536 if (offset) 537 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET); 538 return true; 539 } 540 return false; 541 } 542 #endif 543 544 #if defined(_LIBUNWIND_TARGET_PPC) 545 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const { 546 return true; 547 } 548 #endif 549 550 #if defined(_LIBUNWIND_TARGET_AARCH64) 551 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const { 552 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) { 553 if (offset) 554 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET); 555 return true; 556 } 557 return false; 558 } 559 #endif 560 #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 561 562 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 563 compact_unwind_encoding_t dwarfEncoding() const { 564 R dummy; 565 return dwarfEncoding(dummy); 566 } 567 568 #if defined(_LIBUNWIND_TARGET_X86_64) 569 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const { 570 return UNWIND_X86_64_MODE_DWARF; 571 } 572 #endif 573 574 #if defined(_LIBUNWIND_TARGET_I386) 575 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const { 576 return UNWIND_X86_MODE_DWARF; 577 } 578 #endif 579 580 #if defined(_LIBUNWIND_TARGET_PPC) 581 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const { 582 return 0; 583 } 584 #endif 585 586 #if defined(_LIBUNWIND_TARGET_AARCH64) 587 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const { 588 return UNWIND_ARM64_MODE_DWARF; 589 } 590 #endif 591 592 #if defined (_LIBUNWIND_TARGET_OR1K) 593 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const { 594 return 0; 595 } 596 #endif 597 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 598 599 600 A &_addressSpace; 601 R _registers; 602 unw_proc_info_t _info; 603 bool _unwindInfoMissing; 604 bool _isSignalFrame; 605 }; 606 607 608 template <typename A, typename R> 609 UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as) 610 : _addressSpace(as), _registers(context), _unwindInfoMissing(false), 611 _isSignalFrame(false) { 612 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), 613 "UnwindCursor<> does not fit in unw_cursor_t"); 614 memset(&_info, 0, sizeof(_info)); 615 } 616 617 template <typename A, typename R> 618 UnwindCursor<A, R>::UnwindCursor(A &as, void *) 619 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) { 620 memset(&_info, 0, sizeof(_info)); 621 // FIXME 622 // fill in _registers from thread arg 623 } 624 625 626 template <typename A, typename R> 627 bool UnwindCursor<A, R>::validReg(int regNum) { 628 return _registers.validRegister(regNum); 629 } 630 631 template <typename A, typename R> 632 unw_word_t UnwindCursor<A, R>::getReg(int regNum) { 633 return _registers.getRegister(regNum); 634 } 635 636 template <typename A, typename R> 637 void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) { 638 _registers.setRegister(regNum, (typename A::pint_t)value); 639 } 640 641 template <typename A, typename R> 642 bool UnwindCursor<A, R>::validFloatReg(int regNum) { 643 return _registers.validFloatRegister(regNum); 644 } 645 646 template <typename A, typename R> 647 unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) { 648 return _registers.getFloatRegister(regNum); 649 } 650 651 template <typename A, typename R> 652 void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) { 653 _registers.setFloatRegister(regNum, value); 654 } 655 656 template <typename A, typename R> void UnwindCursor<A, R>::jumpto() { 657 _registers.jumpto(); 658 } 659 660 #ifdef __arm__ 661 template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() { 662 _registers.saveVFPAsX(); 663 } 664 #endif 665 666 template <typename A, typename R> 667 const char *UnwindCursor<A, R>::getRegisterName(int regNum) { 668 return _registers.getRegisterName(regNum); 669 } 670 671 template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() { 672 return _isSignalFrame; 673 } 674 675 #if defined(_LIBUNWIND_ARM_EHABI) 676 struct EHABIIndexEntry { 677 uint32_t functionOffset; 678 uint32_t data; 679 }; 680 681 template<typename A> 682 struct EHABISectionIterator { 683 typedef EHABISectionIterator _Self; 684 685 typedef std::random_access_iterator_tag iterator_category; 686 typedef typename A::pint_t value_type; 687 typedef typename A::pint_t* pointer; 688 typedef typename A::pint_t& reference; 689 typedef size_t size_type; 690 typedef size_t difference_type; 691 692 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) { 693 return _Self(addressSpace, sects, 0); 694 } 695 static _Self end(A& addressSpace, const UnwindInfoSections& sects) { 696 return _Self(addressSpace, sects, 697 sects.arm_section_length / sizeof(EHABIIndexEntry)); 698 } 699 700 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i) 701 : _i(i), _addressSpace(&addressSpace), _sects(§s) {} 702 703 _Self& operator++() { ++_i; return *this; } 704 _Self& operator+=(size_t a) { _i += a; return *this; } 705 _Self& operator--() { assert(_i > 0); --_i; return *this; } 706 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; } 707 708 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; } 709 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; } 710 711 size_t operator-(const _Self& other) { return _i - other._i; } 712 713 bool operator==(const _Self& other) const { 714 assert(_addressSpace == other._addressSpace); 715 assert(_sects == other._sects); 716 return _i == other._i; 717 } 718 719 typename A::pint_t operator*() const { return functionAddress(); } 720 721 typename A::pint_t functionAddress() const { 722 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( 723 EHABIIndexEntry, _i, functionOffset); 724 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr)); 725 } 726 727 typename A::pint_t dataAddress() { 728 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( 729 EHABIIndexEntry, _i, data); 730 return indexAddr; 731 } 732 733 private: 734 size_t _i; 735 A* _addressSpace; 736 const UnwindInfoSections* _sects; 737 }; 738 739 template <typename A, typename R> 740 bool UnwindCursor<A, R>::getInfoFromEHABISection( 741 pint_t pc, 742 const UnwindInfoSections §s) { 743 EHABISectionIterator<A> begin = 744 EHABISectionIterator<A>::begin(_addressSpace, sects); 745 EHABISectionIterator<A> end = 746 EHABISectionIterator<A>::end(_addressSpace, sects); 747 748 EHABISectionIterator<A> itNextPC = std::upper_bound(begin, end, pc); 749 if (itNextPC == begin || itNextPC == end) 750 return false; 751 EHABISectionIterator<A> itThisPC = itNextPC - 1; 752 753 pint_t thisPC = itThisPC.functionAddress(); 754 pint_t nextPC = itNextPC.functionAddress(); 755 pint_t indexDataAddr = itThisPC.dataAddress(); 756 757 if (indexDataAddr == 0) 758 return false; 759 760 uint32_t indexData = _addressSpace.get32(indexDataAddr); 761 if (indexData == UNW_EXIDX_CANTUNWIND) 762 return false; 763 764 // If the high bit is set, the exception handling table entry is inline inside 765 // the index table entry on the second word (aka |indexDataAddr|). Otherwise, 766 // the table points at an offset in the exception handling table (section 5 EHABI). 767 pint_t exceptionTableAddr; 768 uint32_t exceptionTableData; 769 bool isSingleWordEHT; 770 if (indexData & 0x80000000) { 771 exceptionTableAddr = indexDataAddr; 772 // TODO(ajwong): Should this data be 0? 773 exceptionTableData = indexData; 774 isSingleWordEHT = true; 775 } else { 776 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData); 777 exceptionTableData = _addressSpace.get32(exceptionTableAddr); 778 isSingleWordEHT = false; 779 } 780 781 // Now we know the 3 things: 782 // exceptionTableAddr -- exception handler table entry. 783 // exceptionTableData -- the data inside the first word of the eht entry. 784 // isSingleWordEHT -- whether the entry is in the index. 785 unw_word_t personalityRoutine = 0xbadf00d; 786 bool scope32 = false; 787 uintptr_t lsda; 788 789 // If the high bit in the exception handling table entry is set, the entry is 790 // in compact form (section 6.3 EHABI). 791 if (exceptionTableData & 0x80000000) { 792 // Grab the index of the personality routine from the compact form. 793 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24; 794 uint32_t extraWords = 0; 795 switch (choice) { 796 case 0: 797 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0; 798 extraWords = 0; 799 scope32 = false; 800 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4); 801 break; 802 case 1: 803 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1; 804 extraWords = (exceptionTableData & 0x00ff0000) >> 16; 805 scope32 = false; 806 lsda = exceptionTableAddr + (extraWords + 1) * 4; 807 break; 808 case 2: 809 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2; 810 extraWords = (exceptionTableData & 0x00ff0000) >> 16; 811 scope32 = true; 812 lsda = exceptionTableAddr + (extraWords + 1) * 4; 813 break; 814 default: 815 _LIBUNWIND_ABORT("unknown personality routine"); 816 return false; 817 } 818 819 if (isSingleWordEHT) { 820 if (extraWords != 0) { 821 _LIBUNWIND_ABORT("index inlined table detected but pr function " 822 "requires extra words"); 823 return false; 824 } 825 } 826 } else { 827 pint_t personalityAddr = 828 exceptionTableAddr + signExtendPrel31(exceptionTableData); 829 personalityRoutine = personalityAddr; 830 831 // ARM EHABI # 6.2, # 9.2 832 // 833 // +---- ehtp 834 // v 835 // +--------------------------------------+ 836 // | +--------+--------+--------+-------+ | 837 // | |0| prel31 to personalityRoutine | | 838 // | +--------+--------+--------+-------+ | 839 // | | N | unwind opcodes | | <-- UnwindData 840 // | +--------+--------+--------+-------+ | 841 // | | Word 2 unwind opcodes | | 842 // | +--------+--------+--------+-------+ | 843 // | ... | 844 // | +--------+--------+--------+-------+ | 845 // | | Word N unwind opcodes | | 846 // | +--------+--------+--------+-------+ | 847 // | | LSDA | | <-- lsda 848 // | | ... | | 849 // | +--------+--------+--------+-------+ | 850 // +--------------------------------------+ 851 852 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1; 853 uint32_t FirstDataWord = *UnwindData; 854 size_t N = ((FirstDataWord >> 24) & 0xff); 855 size_t NDataWords = N + 1; 856 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords); 857 } 858 859 _info.start_ip = thisPC; 860 _info.end_ip = nextPC; 861 _info.handler = personalityRoutine; 862 _info.unwind_info = exceptionTableAddr; 863 _info.lsda = lsda; 864 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0. 865 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum? 866 867 return true; 868 } 869 #endif 870 871 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 872 template <typename A, typename R> 873 bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc, 874 const UnwindInfoSections §s, 875 uint32_t fdeSectionOffsetHint) { 876 typename CFI_Parser<A>::FDE_Info fdeInfo; 877 typename CFI_Parser<A>::CIE_Info cieInfo; 878 bool foundFDE = false; 879 bool foundInCache = false; 880 // If compact encoding table gave offset into dwarf section, go directly there 881 if (fdeSectionOffsetHint != 0) { 882 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 883 (uint32_t)sects.dwarf_section_length, 884 sects.dwarf_section + fdeSectionOffsetHint, 885 &fdeInfo, &cieInfo); 886 } 887 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) 888 if (!foundFDE && (sects.dwarf_index_section != 0)) { 889 foundFDE = EHHeaderParser<A>::findFDE( 890 _addressSpace, pc, sects.dwarf_index_section, 891 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo); 892 } 893 #endif 894 if (!foundFDE) { 895 // otherwise, search cache of previously found FDEs. 896 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc); 897 if (cachedFDE != 0) { 898 foundFDE = 899 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 900 (uint32_t)sects.dwarf_section_length, 901 cachedFDE, &fdeInfo, &cieInfo); 902 foundInCache = foundFDE; 903 } 904 } 905 if (!foundFDE) { 906 // Still not found, do full scan of __eh_frame section. 907 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, 908 (uint32_t)sects.dwarf_section_length, 0, 909 &fdeInfo, &cieInfo); 910 } 911 if (foundFDE) { 912 typename CFI_Parser<A>::PrologInfo prolog; 913 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc, 914 &prolog)) { 915 // Save off parsed FDE info 916 _info.start_ip = fdeInfo.pcStart; 917 _info.end_ip = fdeInfo.pcEnd; 918 _info.lsda = fdeInfo.lsda; 919 _info.handler = cieInfo.personality; 920 _info.gp = prolog.spExtraArgSize; 921 _info.flags = 0; 922 _info.format = dwarfEncoding(); 923 _info.unwind_info = fdeInfo.fdeStart; 924 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength; 925 _info.extra = (unw_word_t) sects.dso_base; 926 927 // Add to cache (to make next lookup faster) if we had no hint 928 // and there was no index. 929 if (!foundInCache && (fdeSectionOffsetHint == 0)) { 930 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) 931 if (sects.dwarf_index_section == 0) 932 #endif 933 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd, 934 fdeInfo.fdeStart); 935 } 936 return true; 937 } 938 } 939 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc); 940 return false; 941 } 942 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 943 944 945 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 946 template <typename A, typename R> 947 bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc, 948 const UnwindInfoSections §s) { 949 const bool log = false; 950 if (log) 951 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n", 952 (uint64_t)pc, (uint64_t)sects.dso_base); 953 954 const UnwindSectionHeader<A> sectionHeader(_addressSpace, 955 sects.compact_unwind_section); 956 if (sectionHeader.version() != UNWIND_SECTION_VERSION) 957 return false; 958 959 // do a binary search of top level index to find page with unwind info 960 pint_t targetFunctionOffset = pc - sects.dso_base; 961 const UnwindSectionIndexArray<A> topIndex(_addressSpace, 962 sects.compact_unwind_section 963 + sectionHeader.indexSectionOffset()); 964 uint32_t low = 0; 965 uint32_t high = sectionHeader.indexCount(); 966 uint32_t last = high - 1; 967 while (low < high) { 968 uint32_t mid = (low + high) / 2; 969 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n", 970 //mid, low, high, topIndex.functionOffset(mid)); 971 if (topIndex.functionOffset(mid) <= targetFunctionOffset) { 972 if ((mid == last) || 973 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) { 974 low = mid; 975 break; 976 } else { 977 low = mid + 1; 978 } 979 } else { 980 high = mid; 981 } 982 } 983 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low); 984 const uint32_t firstLevelNextPageFunctionOffset = 985 topIndex.functionOffset(low + 1); 986 const pint_t secondLevelAddr = 987 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low); 988 const pint_t lsdaArrayStartAddr = 989 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low); 990 const pint_t lsdaArrayEndAddr = 991 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1); 992 if (log) 993 fprintf(stderr, "\tfirst level search for result index=%d " 994 "to secondLevelAddr=0x%llX\n", 995 low, (uint64_t) secondLevelAddr); 996 // do a binary search of second level page index 997 uint32_t encoding = 0; 998 pint_t funcStart = 0; 999 pint_t funcEnd = 0; 1000 pint_t lsda = 0; 1001 pint_t personality = 0; 1002 uint32_t pageKind = _addressSpace.get32(secondLevelAddr); 1003 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) { 1004 // regular page 1005 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace, 1006 secondLevelAddr); 1007 UnwindSectionRegularArray<A> pageIndex( 1008 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); 1009 // binary search looks for entry with e where index[e].offset <= pc < 1010 // index[e+1].offset 1011 if (log) 1012 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in " 1013 "regular page starting at secondLevelAddr=0x%llX\n", 1014 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr); 1015 low = 0; 1016 high = pageHeader.entryCount(); 1017 while (low < high) { 1018 uint32_t mid = (low + high) / 2; 1019 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) { 1020 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) { 1021 // at end of table 1022 low = mid; 1023 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; 1024 break; 1025 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) { 1026 // next is too big, so we found it 1027 low = mid; 1028 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base; 1029 break; 1030 } else { 1031 low = mid + 1; 1032 } 1033 } else { 1034 high = mid; 1035 } 1036 } 1037 encoding = pageIndex.encoding(low); 1038 funcStart = pageIndex.functionOffset(low) + sects.dso_base; 1039 if (pc < funcStart) { 1040 if (log) 1041 fprintf( 1042 stderr, 1043 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", 1044 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); 1045 return false; 1046 } 1047 if (pc > funcEnd) { 1048 if (log) 1049 fprintf( 1050 stderr, 1051 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", 1052 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); 1053 return false; 1054 } 1055 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) { 1056 // compressed page 1057 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace, 1058 secondLevelAddr); 1059 UnwindSectionCompressedArray<A> pageIndex( 1060 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); 1061 const uint32_t targetFunctionPageOffset = 1062 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset); 1063 // binary search looks for entry with e where index[e].offset <= pc < 1064 // index[e+1].offset 1065 if (log) 1066 fprintf(stderr, "\tbinary search of compressed page starting at " 1067 "secondLevelAddr=0x%llX\n", 1068 (uint64_t) secondLevelAddr); 1069 low = 0; 1070 last = pageHeader.entryCount() - 1; 1071 high = pageHeader.entryCount(); 1072 while (low < high) { 1073 uint32_t mid = (low + high) / 2; 1074 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) { 1075 if ((mid == last) || 1076 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) { 1077 low = mid; 1078 break; 1079 } else { 1080 low = mid + 1; 1081 } 1082 } else { 1083 high = mid; 1084 } 1085 } 1086 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset 1087 + sects.dso_base; 1088 if (low < last) 1089 funcEnd = 1090 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset 1091 + sects.dso_base; 1092 else 1093 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; 1094 if (pc < funcStart) { 1095 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second " 1096 "level compressed unwind table. funcStart=0x%llX", 1097 (uint64_t) pc, (uint64_t) funcStart); 1098 return false; 1099 } 1100 if (pc > funcEnd) { 1101 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second " 1102 "level compressed unwind table. funcEnd=0x%llX", 1103 (uint64_t) pc, (uint64_t) funcEnd); 1104 return false; 1105 } 1106 uint16_t encodingIndex = pageIndex.encodingIndex(low); 1107 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) { 1108 // encoding is in common table in section header 1109 encoding = _addressSpace.get32( 1110 sects.compact_unwind_section + 1111 sectionHeader.commonEncodingsArraySectionOffset() + 1112 encodingIndex * sizeof(uint32_t)); 1113 } else { 1114 // encoding is in page specific table 1115 uint16_t pageEncodingIndex = 1116 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount(); 1117 encoding = _addressSpace.get32(secondLevelAddr + 1118 pageHeader.encodingsPageOffset() + 1119 pageEncodingIndex * sizeof(uint32_t)); 1120 } 1121 } else { 1122 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second " 1123 "level page", 1124 (uint64_t) sects.compact_unwind_section); 1125 return false; 1126 } 1127 1128 // look up LSDA, if encoding says function has one 1129 if (encoding & UNWIND_HAS_LSDA) { 1130 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr); 1131 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base); 1132 low = 0; 1133 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) / 1134 sizeof(unwind_info_section_header_lsda_index_entry); 1135 // binary search looks for entry with exact match for functionOffset 1136 if (log) 1137 fprintf(stderr, 1138 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n", 1139 funcStartOffset); 1140 while (low < high) { 1141 uint32_t mid = (low + high) / 2; 1142 if (lsdaIndex.functionOffset(mid) == funcStartOffset) { 1143 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base; 1144 break; 1145 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) { 1146 low = mid + 1; 1147 } else { 1148 high = mid; 1149 } 1150 } 1151 if (lsda == 0) { 1152 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for " 1153 "pc=0x%0llX, but lsda table has no entry", 1154 encoding, (uint64_t) pc); 1155 return false; 1156 } 1157 } 1158 1159 // extact personality routine, if encoding says function has one 1160 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >> 1161 (__builtin_ctz(UNWIND_PERSONALITY_MASK)); 1162 if (personalityIndex != 0) { 1163 --personalityIndex; // change 1-based to zero-based index 1164 if (personalityIndex > sectionHeader.personalityArrayCount()) { 1165 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, " 1166 "but personality table has only %d entires", 1167 encoding, personalityIndex, 1168 sectionHeader.personalityArrayCount()); 1169 return false; 1170 } 1171 int32_t personalityDelta = (int32_t)_addressSpace.get32( 1172 sects.compact_unwind_section + 1173 sectionHeader.personalityArraySectionOffset() + 1174 personalityIndex * sizeof(uint32_t)); 1175 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta; 1176 personality = _addressSpace.getP(personalityPointer); 1177 if (log) 1178 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " 1179 "personalityDelta=0x%08X, personality=0x%08llX\n", 1180 (uint64_t) pc, personalityDelta, (uint64_t) personality); 1181 } 1182 1183 if (log) 1184 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " 1185 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n", 1186 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart); 1187 _info.start_ip = funcStart; 1188 _info.end_ip = funcEnd; 1189 _info.lsda = lsda; 1190 _info.handler = personality; 1191 _info.gp = 0; 1192 _info.flags = 0; 1193 _info.format = encoding; 1194 _info.unwind_info = 0; 1195 _info.unwind_info_size = 0; 1196 _info.extra = sects.dso_base; 1197 return true; 1198 } 1199 #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 1200 1201 1202 template <typename A, typename R> 1203 void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) { 1204 pint_t pc = (pint_t)this->getReg(UNW_REG_IP); 1205 #if defined(_LIBUNWIND_ARM_EHABI) 1206 // Remove the thumb bit so the IP represents the actual instruction address. 1207 // This matches the behaviour of _Unwind_GetIP on arm. 1208 pc &= (pint_t)~0x1; 1209 #endif 1210 1211 // If the last line of a function is a "throw" the compiler sometimes 1212 // emits no instructions after the call to __cxa_throw. This means 1213 // the return address is actually the start of the next function. 1214 // To disambiguate this, back up the pc when we know it is a return 1215 // address. 1216 if (isReturnAddress) 1217 --pc; 1218 1219 // Ask address space object to find unwind sections for this pc. 1220 UnwindInfoSections sects; 1221 if (_addressSpace.findUnwindSections(pc, sects)) { 1222 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 1223 // If there is a compact unwind encoding table, look there first. 1224 if (sects.compact_unwind_section != 0) { 1225 if (this->getInfoFromCompactEncodingSection(pc, sects)) { 1226 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1227 // Found info in table, done unless encoding says to use dwarf. 1228 uint32_t dwarfOffset; 1229 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) { 1230 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) { 1231 // found info in dwarf, done 1232 return; 1233 } 1234 } 1235 #endif 1236 // If unwind table has entry, but entry says there is no unwind info, 1237 // record that we have no unwind info. 1238 if (_info.format == 0) 1239 _unwindInfoMissing = true; 1240 return; 1241 } 1242 } 1243 #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 1244 1245 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1246 // If there is dwarf unwind info, look there next. 1247 if (sects.dwarf_section != 0) { 1248 if (this->getInfoFromDwarfSection(pc, sects)) { 1249 // found info in dwarf, done 1250 return; 1251 } 1252 } 1253 #endif 1254 1255 #if defined(_LIBUNWIND_ARM_EHABI) 1256 // If there is ARM EHABI unwind info, look there next. 1257 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects)) 1258 return; 1259 #endif 1260 } 1261 1262 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1263 // There is no static unwind info for this pc. Look to see if an FDE was 1264 // dynamically registered for it. 1265 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc); 1266 if (cachedFDE != 0) { 1267 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; 1268 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; 1269 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace, 1270 cachedFDE, &fdeInfo, &cieInfo); 1271 if (msg == NULL) { 1272 typename CFI_Parser<A>::PrologInfo prolog; 1273 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, 1274 pc, &prolog)) { 1275 // save off parsed FDE info 1276 _info.start_ip = fdeInfo.pcStart; 1277 _info.end_ip = fdeInfo.pcEnd; 1278 _info.lsda = fdeInfo.lsda; 1279 _info.handler = cieInfo.personality; 1280 _info.gp = prolog.spExtraArgSize; 1281 // Some frameless functions need SP 1282 // altered when resuming in function. 1283 _info.flags = 0; 1284 _info.format = dwarfEncoding(); 1285 _info.unwind_info = fdeInfo.fdeStart; 1286 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength; 1287 _info.extra = 0; 1288 return; 1289 } 1290 } 1291 } 1292 1293 // Lastly, ask AddressSpace object about platform specific ways to locate 1294 // other FDEs. 1295 pint_t fde; 1296 if (_addressSpace.findOtherFDE(pc, fde)) { 1297 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; 1298 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; 1299 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) { 1300 // Double check this FDE is for a function that includes the pc. 1301 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) { 1302 typename CFI_Parser<A>::PrologInfo prolog; 1303 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, 1304 cieInfo, pc, &prolog)) { 1305 // save off parsed FDE info 1306 _info.start_ip = fdeInfo.pcStart; 1307 _info.end_ip = fdeInfo.pcEnd; 1308 _info.lsda = fdeInfo.lsda; 1309 _info.handler = cieInfo.personality; 1310 _info.gp = prolog.spExtraArgSize; 1311 _info.flags = 0; 1312 _info.format = dwarfEncoding(); 1313 _info.unwind_info = fdeInfo.fdeStart; 1314 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength; 1315 _info.extra = 0; 1316 return; 1317 } 1318 } 1319 } 1320 } 1321 #endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1322 1323 // no unwind info, flag that we can't reliably unwind 1324 _unwindInfoMissing = true; 1325 } 1326 1327 template <typename A, typename R> 1328 int UnwindCursor<A, R>::step() { 1329 // Bottom of stack is defined is when unwind info cannot be found. 1330 if (_unwindInfoMissing) 1331 return UNW_STEP_END; 1332 1333 // Use unwinding info to modify register set as if function returned. 1334 int result; 1335 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) 1336 result = this->stepWithCompactEncoding(); 1337 #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 1338 result = this->stepWithDwarfFDE(); 1339 #elif defined(_LIBUNWIND_ARM_EHABI) 1340 result = this->stepWithEHABI(); 1341 #else 1342 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \ 1343 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \ 1344 _LIBUNWIND_ARM_EHABI 1345 #endif 1346 1347 // update info based on new PC 1348 if (result == UNW_STEP_SUCCESS) { 1349 this->setInfoBasedOnIPRegister(true); 1350 if (_unwindInfoMissing) 1351 return UNW_STEP_END; 1352 if (_info.gp) 1353 setReg(UNW_REG_SP, getReg(UNW_REG_SP) + _info.gp); 1354 } 1355 1356 return result; 1357 } 1358 1359 template <typename A, typename R> 1360 void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) { 1361 *info = _info; 1362 } 1363 1364 template <typename A, typename R> 1365 bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen, 1366 unw_word_t *offset) { 1367 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP), 1368 buf, bufLen, offset); 1369 } 1370 1371 } // namespace libunwind 1372 1373 #endif // __UNWINDCURSOR_HPP__ 1374