1 //===-- CompactUnwindInfo.cpp -----------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 11 // C Includes 12 // C++ Includes 13 #include <algorithm> 14 15 #include "lldb/Core/Log.h" 16 #include "lldb/Core/Section.h" 17 #include "lldb/Core/ArchSpec.h" 18 #include "lldb/Core/Module.h" 19 #include "lldb/Core/Section.h" 20 #include "lldb/Symbol/CompactUnwindInfo.h" 21 #include "lldb/Symbol/ObjectFile.h" 22 #include "lldb/Symbol/UnwindPlan.h" 23 24 #include "llvm/Support/MathExtras.h" 25 26 using namespace lldb; 27 using namespace lldb_private; 28 29 30 namespace lldb_private { 31 32 // Constants from <mach-o/compact_unwind_encoding.h> 33 34 enum { 35 UNWIND_IS_NOT_FUNCTION_START = 0x80000000, 36 UNWIND_HAS_LSDA = 0x40000000, 37 UNWIND_PERSONALITY_MASK = 0x30000000, 38 }; 39 40 enum { 41 UNWIND_X86_MODE_MASK = 0x0F000000, 42 UNWIND_X86_MODE_EBP_FRAME = 0x01000000, 43 UNWIND_X86_MODE_STACK_IMMD = 0x02000000, 44 UNWIND_X86_MODE_STACK_IND = 0x03000000, 45 UNWIND_X86_MODE_DWARF = 0x04000000, 46 47 UNWIND_X86_EBP_FRAME_REGISTERS = 0x00007FFF, 48 UNWIND_X86_EBP_FRAME_OFFSET = 0x00FF0000, 49 50 UNWIND_X86_FRAMELESS_STACK_SIZE = 0x00FF0000, 51 UNWIND_X86_FRAMELESS_STACK_ADJUST = 0x0000E000, 52 UNWIND_X86_FRAMELESS_STACK_REG_COUNT = 0x00001C00, 53 UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION = 0x000003FF, 54 55 UNWIND_X86_DWARF_SECTION_OFFSET = 0x00FFFFFF, 56 }; 57 58 enum { 59 UNWIND_X86_REG_NONE = 0, 60 UNWIND_X86_REG_EBX = 1, 61 UNWIND_X86_REG_ECX = 2, 62 UNWIND_X86_REG_EDX = 3, 63 UNWIND_X86_REG_EDI = 4, 64 UNWIND_X86_REG_ESI = 5, 65 UNWIND_X86_REG_EBP = 6, 66 }; 67 enum { 68 UNWIND_X86_64_MODE_MASK = 0x0F000000, 69 UNWIND_X86_64_MODE_RBP_FRAME = 0x01000000, 70 UNWIND_X86_64_MODE_STACK_IMMD = 0x02000000, 71 UNWIND_X86_64_MODE_STACK_IND = 0x03000000, 72 UNWIND_X86_64_MODE_DWARF = 0x04000000, 73 74 UNWIND_X86_64_RBP_FRAME_REGISTERS = 0x00007FFF, 75 UNWIND_X86_64_RBP_FRAME_OFFSET = 0x00FF0000, 76 77 UNWIND_X86_64_FRAMELESS_STACK_SIZE = 0x00FF0000, 78 UNWIND_X86_64_FRAMELESS_STACK_ADJUST = 0x0000E000, 79 UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT = 0x00001C00, 80 UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION = 0x000003FF, 81 82 UNWIND_X86_64_DWARF_SECTION_OFFSET = 0x00FFFFFF, 83 }; 84 85 enum { 86 UNWIND_X86_64_REG_NONE = 0, 87 UNWIND_X86_64_REG_RBX = 1, 88 UNWIND_X86_64_REG_R12 = 2, 89 UNWIND_X86_64_REG_R13 = 3, 90 UNWIND_X86_64_REG_R14 = 4, 91 UNWIND_X86_64_REG_R15 = 5, 92 UNWIND_X86_64_REG_RBP = 6, 93 }; 94 }; 95 96 97 #ifndef UNWIND_SECOND_LEVEL_REGULAR 98 #define UNWIND_SECOND_LEVEL_REGULAR 2 99 #endif 100 101 #ifndef UNWIND_SECOND_LEVEL_COMPRESSED 102 #define UNWIND_SECOND_LEVEL_COMPRESSED 3 103 #endif 104 105 #ifndef UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET 106 #define UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(entry) (entry & 0x00FFFFFF) 107 #endif 108 109 #ifndef UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX 110 #define UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(entry) ((entry >> 24) & 0xFF) 111 #endif 112 113 #define EXTRACT_BITS(value, mask) \ 114 ( (value >> llvm::countTrailingZeros(static_cast<uint32_t>(mask), llvm::ZB_Width)) & \ 115 (((1 << llvm::CountPopulation_32(static_cast<uint32_t>(mask))))-1) ) 116 117 118 119 //---------------------- 120 // constructor 121 //---------------------- 122 123 124 CompactUnwindInfo::CompactUnwindInfo(ObjectFile& objfile, SectionSP& section_sp) : 125 m_objfile (objfile), 126 m_section_sp (section_sp), 127 m_mutex (), 128 m_indexes (), 129 m_indexes_computed (eLazyBoolCalculate), 130 m_unwindinfo_data (), 131 m_unwindinfo_data_computed (false), 132 m_unwind_header () 133 { 134 135 } 136 137 //---------------------- 138 // destructor 139 //---------------------- 140 141 CompactUnwindInfo::~CompactUnwindInfo() 142 { 143 } 144 145 bool 146 CompactUnwindInfo::GetUnwindPlan (Target &target, Address addr, UnwindPlan& unwind_plan) 147 { 148 if (!IsValid ()) 149 { 150 return false; 151 } 152 FunctionInfo function_info; 153 if (GetCompactUnwindInfoForFunction (target, addr, function_info)) 154 { 155 // shortcut return for functions that have no compact unwind 156 if (function_info.encoding == 0) 157 return false; 158 159 ArchSpec arch; 160 if (m_objfile.GetArchitecture (arch)) 161 { 162 if (arch.GetTriple().getArch() == llvm::Triple::x86_64) 163 { 164 return CreateUnwindPlan_x86_64 (target, function_info, unwind_plan, addr); 165 } 166 if (arch.GetTriple().getArch() == llvm::Triple::x86) 167 { 168 return CreateUnwindPlan_i386 (target, function_info, unwind_plan, addr); 169 } 170 } 171 } 172 return false; 173 } 174 175 bool 176 CompactUnwindInfo::IsValid () 177 { 178 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 179 return false; 180 181 if (m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed) 182 return true; 183 184 ScanIndex (); 185 186 return m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed; 187 } 188 189 void 190 CompactUnwindInfo::ScanIndex () 191 { 192 Mutex::Locker locker(m_mutex); 193 if (m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed) 194 return; 195 196 // We can't read the index for some reason. 197 if (m_indexes_computed == eLazyBoolNo) 198 { 199 return; 200 } 201 202 if (m_unwindinfo_data_computed == false) 203 { 204 m_objfile.ReadSectionData (m_section_sp.get(), m_unwindinfo_data); 205 m_unwindinfo_data_computed = true; 206 } 207 208 if (m_unwindinfo_data.GetByteSize() > 0) 209 { 210 offset_t offset = 0; 211 212 // struct unwind_info_section_header 213 // { 214 // uint32_t version; // UNWIND_SECTION_VERSION 215 // uint32_t commonEncodingsArraySectionOffset; 216 // uint32_t commonEncodingsArrayCount; 217 // uint32_t personalityArraySectionOffset; 218 // uint32_t personalityArrayCount; 219 // uint32_t indexSectionOffset; 220 // uint32_t indexCount; 221 222 m_unwind_header.version = m_unwindinfo_data.GetU32(&offset); 223 m_unwind_header.common_encodings_array_offset = m_unwindinfo_data.GetU32(&offset); 224 m_unwind_header.common_encodings_array_count = m_unwindinfo_data.GetU32(&offset); 225 m_unwind_header.personality_array_offset = m_unwindinfo_data.GetU32(&offset); 226 m_unwind_header.personality_array_count = m_unwindinfo_data.GetU32(&offset); 227 uint32_t indexSectionOffset = m_unwindinfo_data.GetU32(&offset); 228 229 uint32_t indexCount = m_unwindinfo_data.GetU32(&offset); 230 231 if (m_unwind_header.version != 1) 232 { 233 m_indexes_computed = eLazyBoolNo; 234 } 235 236 // Parse the basic information from the indexes 237 // We wait to scan the second level page info until it's needed 238 239 // struct unwind_info_section_header_index_entry 240 // { 241 // uint32_t functionOffset; 242 // uint32_t secondLevelPagesSectionOffset; 243 // uint32_t lsdaIndexArraySectionOffset; 244 // }; 245 246 offset = indexSectionOffset; 247 for (int idx = 0; idx < indexCount; idx++) 248 { 249 uint32_t function_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset 250 uint32_t second_level_offset = m_unwindinfo_data.GetU32(&offset); // secondLevelPagesSectionOffset 251 uint32_t lsda_offset = m_unwindinfo_data.GetU32(&offset); // lsdaIndexArraySectionOffset 252 253 if (second_level_offset > m_section_sp->GetByteSize() || lsda_offset > m_section_sp->GetByteSize()) 254 { 255 m_indexes_computed = eLazyBoolNo; 256 } 257 258 UnwindIndex this_index; 259 this_index.function_offset = function_offset; // 260 this_index.second_level = second_level_offset; 261 this_index.lsda_array_start = lsda_offset; 262 263 if (m_indexes.size() > 0) 264 { 265 m_indexes[m_indexes.size() - 1].lsda_array_end = lsda_offset; 266 } 267 268 if (second_level_offset == 0) 269 { 270 this_index.sentinal_entry = true; 271 } 272 273 m_indexes.push_back (this_index); 274 } 275 m_indexes_computed = eLazyBoolYes; 276 } 277 else 278 { 279 m_indexes_computed = eLazyBoolNo; 280 } 281 } 282 283 uint32_t 284 CompactUnwindInfo::GetLSDAForFunctionOffset (uint32_t lsda_offset, uint32_t lsda_count, uint32_t function_offset) 285 { 286 // struct unwind_info_section_header_lsda_index_entry 287 // { 288 // uint32_t functionOffset; 289 // uint32_t lsdaOffset; 290 // }; 291 292 offset_t first_entry = lsda_offset; 293 uint32_t low = 0; 294 uint32_t high = lsda_count; 295 while (low < high) 296 { 297 uint32_t mid = (low + high) / 2; 298 offset_t offset = first_entry + (mid * 8); 299 uint32_t mid_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset 300 uint32_t mid_lsda_offset = m_unwindinfo_data.GetU32(&offset); // lsdaOffset 301 if (mid_func_offset == function_offset) 302 { 303 return mid_lsda_offset; 304 } 305 if (mid_func_offset < function_offset) 306 { 307 low = mid + 1; 308 } 309 else 310 { 311 high = mid; 312 } 313 } 314 return 0; 315 } 316 317 lldb::offset_t 318 CompactUnwindInfo::BinarySearchRegularSecondPage (uint32_t entry_page_offset, uint32_t entry_count, uint32_t function_offset) 319 { 320 // typedef uint32_t compact_unwind_encoding_t; 321 // struct unwind_info_regular_second_level_entry 322 // { 323 // uint32_t functionOffset; 324 // compact_unwind_encoding_t encoding; 325 326 offset_t first_entry = entry_page_offset; 327 328 uint32_t low = 0; 329 uint32_t high = entry_count; 330 uint32_t last = high - 1; 331 while (low < high) 332 { 333 uint32_t mid = (low + high) / 2; 334 offset_t offset = first_entry + (mid * 8); 335 uint32_t mid_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset 336 uint32_t next_func_offset = 0; 337 if (mid < last) 338 { 339 offset = first_entry + ((mid + 1) * 8); 340 next_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset 341 } 342 if (mid_func_offset <= function_offset) 343 { 344 if (mid == last || (next_func_offset > function_offset)) 345 { 346 return first_entry + (mid * 8); 347 } 348 else 349 { 350 low = mid + 1; 351 } 352 } 353 else 354 { 355 high = mid; 356 } 357 } 358 return LLDB_INVALID_OFFSET; 359 } 360 361 uint32_t 362 CompactUnwindInfo::BinarySearchCompressedSecondPage (uint32_t entry_page_offset, uint32_t entry_count, uint32_t function_offset_to_find, uint32_t function_offset_base) 363 { 364 offset_t first_entry = entry_page_offset; 365 366 uint32_t low = 0; 367 uint32_t high = entry_count; 368 uint32_t last = high - 1; 369 while (low < high) 370 { 371 uint32_t mid = (low + high) / 2; 372 offset_t offset = first_entry + (mid * 4); 373 uint32_t entry = m_unwindinfo_data.GetU32(&offset); // entry 374 uint32_t mid_func_offset = UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET (entry); 375 mid_func_offset += function_offset_base; 376 uint32_t next_func_offset = 0; 377 if (mid < last) 378 { 379 offset = first_entry + ((mid + 1) * 4); 380 uint32_t next_entry = m_unwindinfo_data.GetU32(&offset); // entry 381 next_func_offset = UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET (next_entry); 382 next_func_offset += function_offset_base; 383 } 384 if (mid_func_offset <= function_offset_to_find) 385 { 386 if (mid == last || (next_func_offset > function_offset_to_find)) 387 { 388 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX (entry); 389 } 390 else 391 { 392 low = mid + 1; 393 } 394 } 395 else 396 { 397 high = mid; 398 } 399 } 400 401 return UINT32_MAX; 402 } 403 404 405 bool 406 CompactUnwindInfo::GetCompactUnwindInfoForFunction (Target &target, Address address, FunctionInfo &unwind_info) 407 { 408 unwind_info.encoding = 0; 409 unwind_info.lsda_address.Clear(); 410 unwind_info.personality_ptr_address.Clear(); 411 412 if (!IsValid ()) 413 return false; 414 415 // FIXME looking into a problem with getting the wrong compact unwind entry for 416 // _CFRunLoopRun from CoreFoundation in a live process; disabling the Compact 417 // Unwind plans until I get to the bottom of what's going on there. 418 return false; 419 420 addr_t text_section_file_address = LLDB_INVALID_ADDRESS; 421 SectionList *sl = m_objfile.GetSectionList (); 422 if (sl) 423 { 424 SectionSP text_sect = sl->FindSectionByType (eSectionTypeCode, true); 425 if (text_sect.get()) 426 { 427 text_section_file_address = text_sect->GetFileAddress(); 428 } 429 } 430 if (text_section_file_address == LLDB_INVALID_ADDRESS) 431 return false; 432 433 addr_t function_offset = address.GetFileAddress() - m_objfile.GetHeaderAddress().GetFileAddress(); 434 435 UnwindIndex key; 436 key.function_offset = function_offset; 437 438 std::vector<UnwindIndex>::const_iterator it; 439 it = std::lower_bound (m_indexes.begin(), m_indexes.end(), key); 440 if (it == m_indexes.end()) 441 { 442 return false; 443 } 444 445 if (it->function_offset != key.function_offset) 446 { 447 if (it != m_indexes.begin()) 448 --it; 449 } 450 451 if (it->sentinal_entry == true) 452 { 453 return false; 454 } 455 456 offset_t second_page_offset = it->second_level; 457 offset_t lsda_array_start = it->lsda_array_start; 458 offset_t lsda_array_count = (it->lsda_array_end - it->lsda_array_start) / 8; 459 460 offset_t offset = second_page_offset; 461 uint32_t kind = m_unwindinfo_data.GetU32(&offset); // UNWIND_SECOND_LEVEL_REGULAR or UNWIND_SECOND_LEVEL_COMPRESSED 462 463 if (kind == UNWIND_SECOND_LEVEL_REGULAR) 464 { 465 // struct unwind_info_regular_second_level_page_header 466 // { 467 // uint32_t kind; // UNWIND_SECOND_LEVEL_REGULAR 468 // uint16_t entryPageOffset; 469 // uint16_t entryCount; 470 471 // typedef uint32_t compact_unwind_encoding_t; 472 // struct unwind_info_regular_second_level_entry 473 // { 474 // uint32_t functionOffset; 475 // compact_unwind_encoding_t encoding; 476 477 uint16_t entry_page_offset = m_unwindinfo_data.GetU16(&offset); // entryPageOffset 478 uint16_t entry_count = m_unwindinfo_data.GetU16(&offset); // entryCount 479 480 offset_t entry_offset = BinarySearchRegularSecondPage (second_page_offset + entry_page_offset, entry_count, function_offset); 481 if (entry_offset == LLDB_INVALID_OFFSET) 482 { 483 return false; 484 } 485 entry_offset += 4; // skip over functionOffset 486 unwind_info.encoding = m_unwindinfo_data.GetU32(&entry_offset); // encoding 487 if (unwind_info.encoding & UNWIND_HAS_LSDA) 488 { 489 SectionList *sl = m_objfile.GetSectionList (); 490 if (sl) 491 { 492 uint32_t lsda_offset = GetLSDAForFunctionOffset (lsda_array_start, lsda_array_count, function_offset); 493 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress(); 494 unwind_info.lsda_address.ResolveAddressUsingFileSections (objfile_header_file_address + lsda_offset, sl); 495 } 496 } 497 if (unwind_info.encoding & UNWIND_PERSONALITY_MASK) 498 { 499 uint32_t personality_index = EXTRACT_BITS (unwind_info.encoding, UNWIND_PERSONALITY_MASK); 500 501 if (personality_index > 0) 502 { 503 personality_index--; 504 if (personality_index < m_unwind_header.personality_array_count) 505 { 506 offset_t offset = m_unwind_header.personality_array_offset; 507 offset += 4 * personality_index; 508 SectionList *sl = m_objfile.GetSectionList (); 509 if (sl) 510 { 511 uint32_t personality_offset = m_unwindinfo_data.GetU32(&offset); 512 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress(); 513 unwind_info.personality_ptr_address.ResolveAddressUsingFileSections (objfile_header_file_address + personality_offset, sl); 514 } 515 } 516 } 517 } 518 return true; 519 } 520 else if (kind == UNWIND_SECOND_LEVEL_COMPRESSED) 521 { 522 // struct unwind_info_compressed_second_level_page_header 523 // { 524 // uint32_t kind; // UNWIND_SECOND_LEVEL_COMPRESSED 525 // uint16_t entryPageOffset; // offset from this 2nd lvl page idx to array of entries 526 // // (an entry has a function offset and index into the encodings) 527 // // NB function offset from the entry in the compressed page 528 // // must be added to the index's functionOffset value. 529 // uint16_t entryCount; 530 // uint16_t encodingsPageOffset; // offset from this 2nd lvl page idx to array of encodings 531 // uint16_t encodingsCount; 532 533 uint16_t entry_page_offset = m_unwindinfo_data.GetU16(&offset); // entryPageOffset 534 uint16_t entry_count = m_unwindinfo_data.GetU16(&offset); // entryCount 535 uint16_t encodings_page_offset = m_unwindinfo_data.GetU16(&offset); // encodingsPageOffset 536 uint16_t encodings_count = m_unwindinfo_data.GetU16(&offset); // encodingsCount 537 538 uint32_t encoding_index = BinarySearchCompressedSecondPage (second_page_offset + entry_page_offset, entry_count, function_offset, it->function_offset); 539 if (encoding_index == UINT32_MAX || encoding_index >= encodings_count + m_unwind_header.common_encodings_array_count) 540 { 541 return false; 542 } 543 uint32_t encoding = 0; 544 if (encoding_index < m_unwind_header.common_encodings_array_count) 545 { 546 offset = m_unwind_header.common_encodings_array_offset + (encoding_index * sizeof (uint32_t)); 547 encoding = m_unwindinfo_data.GetU32(&offset); // encoding entry from the commonEncodingsArray 548 } 549 else 550 { 551 uint32_t page_specific_entry_index = encoding_index - m_unwind_header.common_encodings_array_count; 552 offset = second_page_offset + encodings_page_offset + (page_specific_entry_index * sizeof (uint32_t)); 553 encoding = m_unwindinfo_data.GetU32(&offset); // encoding entry from the page-specific encoding array 554 } 555 if (encoding == 0) 556 return false; 557 unwind_info.encoding = encoding; 558 559 unwind_info.encoding = encoding; 560 if (unwind_info.encoding & UNWIND_HAS_LSDA) 561 { 562 SectionList *sl = m_objfile.GetSectionList (); 563 if (sl) 564 { 565 uint32_t lsda_offset = GetLSDAForFunctionOffset (lsda_array_start, lsda_array_count, function_offset); 566 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress(); 567 unwind_info.lsda_address.ResolveAddressUsingFileSections (objfile_header_file_address + lsda_offset, sl); 568 } 569 } 570 if (unwind_info.encoding & UNWIND_PERSONALITY_MASK) 571 { 572 uint32_t personality_index = EXTRACT_BITS (unwind_info.encoding, UNWIND_PERSONALITY_MASK); 573 574 if (personality_index > 0) 575 { 576 personality_index--; 577 if (personality_index < m_unwind_header.personality_array_count) 578 { 579 offset_t offset = m_unwind_header.personality_array_offset; 580 offset += 4 * personality_index; 581 SectionList *sl = m_objfile.GetSectionList (); 582 if (sl) 583 { 584 uint32_t personality_offset = m_unwindinfo_data.GetU32(&offset); 585 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress(); 586 unwind_info.personality_ptr_address.ResolveAddressUsingFileSections (objfile_header_file_address + personality_offset, sl); 587 } 588 } 589 } 590 } 591 return true; 592 } 593 return false; 594 } 595 596 enum x86_64_eh_regnum { 597 rax = 0, 598 rdx = 1, 599 rcx = 2, 600 rbx = 3, 601 rsi = 4, 602 rdi = 5, 603 rbp = 6, 604 rsp = 7, 605 r8 = 8, 606 r9 = 9, 607 r10 = 10, 608 r11 = 11, 609 r12 = 12, 610 r13 = 13, 611 r14 = 14, 612 r15 = 15, 613 rip = 16 // this is officially the Return Address register number, but close enough 614 }; 615 616 // Convert the compact_unwind_info.h register numbering scheme 617 // to eRegisterKindGCC (eh_frame) register numbering scheme. 618 uint32_t 619 translate_to_eh_frame_regnum_x86_64 (uint32_t unwind_regno) 620 { 621 switch (unwind_regno) 622 { 623 case UNWIND_X86_64_REG_RBX: 624 return x86_64_eh_regnum::rbx; 625 case UNWIND_X86_64_REG_R12: 626 return x86_64_eh_regnum::r12; 627 case UNWIND_X86_64_REG_R13: 628 return x86_64_eh_regnum::r13; 629 case UNWIND_X86_64_REG_R14: 630 return x86_64_eh_regnum::r14; 631 case UNWIND_X86_64_REG_R15: 632 return x86_64_eh_regnum::r15; 633 case UNWIND_X86_64_REG_RBP: 634 return x86_64_eh_regnum::rbp; 635 default: 636 return LLDB_INVALID_REGNUM; 637 } 638 } 639 640 bool 641 CompactUnwindInfo::CreateUnwindPlan_x86_64 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start) 642 { 643 unwind_plan.SetSourceName ("compact unwind info"); 644 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes); 645 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); 646 unwind_plan.SetRegisterKind (eRegisterKindGCC); 647 648 unwind_plan.SetLSDAAddress (function_info.lsda_address); 649 unwind_plan.SetPersonalityFunctionPtr (function_info.personality_ptr_address); 650 651 UnwindPlan::RowSP row (new UnwindPlan::Row); 652 653 const int wordsize = 8; 654 int mode = function_info.encoding & UNWIND_X86_64_MODE_MASK; 655 switch (mode) 656 { 657 case UNWIND_X86_64_MODE_RBP_FRAME: 658 { 659 row->SetCFARegister (translate_to_eh_frame_regnum_x86_64 (UNWIND_X86_64_REG_RBP)); 660 row->SetCFAOffset (2 * wordsize); 661 row->SetOffset (0); 662 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rbp, wordsize * -2, true); 663 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rip, wordsize * -1, true); 664 row->SetRegisterLocationToIsCFAPlusOffset (x86_64_eh_regnum::rsp, 0, true); 665 666 uint32_t saved_registers_offset = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_RBP_FRAME_OFFSET); 667 668 uint32_t saved_registers_locations = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_RBP_FRAME_REGISTERS); 669 670 saved_registers_offset += 2; 671 672 for (int i = 0; i < 5; i++) 673 { 674 uint32_t regnum = saved_registers_locations & 0x7; 675 switch (regnum) 676 { 677 case UNWIND_X86_64_REG_NONE: 678 break; 679 case UNWIND_X86_64_REG_RBX: 680 case UNWIND_X86_64_REG_R12: 681 case UNWIND_X86_64_REG_R13: 682 case UNWIND_X86_64_REG_R14: 683 case UNWIND_X86_64_REG_R15: 684 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_x86_64 (regnum), wordsize * -saved_registers_offset, true); 685 break; 686 } 687 saved_registers_offset--; 688 saved_registers_locations >>= 3; 689 } 690 unwind_plan.AppendRow (row); 691 return true; 692 } 693 break; 694 695 case UNWIND_X86_64_MODE_STACK_IND: 696 { 697 // The clang in Xcode 6 is emitting incorrect compact unwind encodings for this 698 // style of unwind. It was fixed in llvm r217020 although the algorith being 699 // used to compute this style of unwind in generateCompactUnwindEncodingImpl() 700 // isn't as foolproof as I'm comfortable with -- if any instructions other than 701 // a push are scheduled before the subq, it will give bogus encoding results. 702 703 // The target and pc_or_function_start arguments will be needed to handle this 704 // encoding style correctly -- to find the start address of the function and 705 // read memory offset from there. 706 return false; 707 } 708 break; 709 710 #if 0 711 case UNWIND_X86_64_MODE_STACK_IMMD: 712 { 713 uint32_t stack_size = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_SIZE); 714 uint32_t register_count = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT); 715 uint32_t permutation = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION); 716 717 if (mode == UNWIND_X86_64_MODE_STACK_IND && function_start) 718 { 719 uint32_t stack_adjust = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_ADJUST); 720 721 // offset into the function instructions; 0 == beginning of first instruction 722 uint32_t offset_to_subl_insn = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_SIZE); 723 724 stack_size = *((uint32_t*) (function_start + offset_to_subl_insn)); 725 726 stack_size += stack_adjust * 8; 727 728 printf ("large stack "); 729 } 730 731 printf ("frameless function: stack size %d, register count %d ", stack_size * 8, register_count); 732 733 if (register_count == 0) 734 { 735 printf (" no registers saved"); 736 } 737 else 738 { 739 740 // We need to include (up to) 6 registers in 10 bits. 741 // That would be 18 bits if we just used 3 bits per reg to indicate 742 // the order they're saved on the stack. 743 // 744 // This is done with Lehmer code permutation, e.g. see 745 // http://stackoverflow.com/questions/1506078/fast-permutation-number-permutation-mapping-algorithms 746 int permunreg[6]; 747 748 // This decodes the variable-base number in the 10 bits 749 // and gives us the Lehmer code sequence which can then 750 // be decoded. 751 752 switch (register_count) 753 { 754 case 6: 755 permunreg[0] = permutation/120; // 120 == 5! 756 permutation -= (permunreg[0]*120); 757 permunreg[1] = permutation/24; // 24 == 4! 758 permutation -= (permunreg[1]*24); 759 permunreg[2] = permutation/6; // 6 == 3! 760 permutation -= (permunreg[2]*6); 761 permunreg[3] = permutation/2; // 2 == 2! 762 permutation -= (permunreg[3]*2); 763 permunreg[4] = permutation; // 1 == 1! 764 permunreg[5] = 0; 765 break; 766 case 5: 767 permunreg[0] = permutation/120; 768 permutation -= (permunreg[0]*120); 769 permunreg[1] = permutation/24; 770 permutation -= (permunreg[1]*24); 771 permunreg[2] = permutation/6; 772 permutation -= (permunreg[2]*6); 773 permunreg[3] = permutation/2; 774 permutation -= (permunreg[3]*2); 775 permunreg[4] = permutation; 776 break; 777 case 4: 778 permunreg[0] = permutation/60; 779 permutation -= (permunreg[0]*60); 780 permunreg[1] = permutation/12; 781 permutation -= (permunreg[1]*12); 782 permunreg[2] = permutation/3; 783 permutation -= (permunreg[2]*3); 784 permunreg[3] = permutation; 785 break; 786 case 3: 787 permunreg[0] = permutation/20; 788 permutation -= (permunreg[0]*20); 789 permunreg[1] = permutation/4; 790 permutation -= (permunreg[1]*4); 791 permunreg[2] = permutation; 792 break; 793 case 2: 794 permunreg[0] = permutation/5; 795 permutation -= (permunreg[0]*5); 796 permunreg[1] = permutation; 797 break; 798 case 1: 799 permunreg[0] = permutation; 800 break; 801 } 802 803 // Decode the Lehmer code for this permutation of 804 // the registers v. http://en.wikipedia.org/wiki/Lehmer_code 805 806 int registers[6]; 807 bool used[7] = { false, false, false, false, false, false, false }; 808 for (int i = 0; i < register_count; i++) 809 { 810 int renum = 0; 811 for (int j = 1; j < 7; j++) 812 { 813 if (used[j] == false) 814 { 815 if (renum == permunreg[i]) 816 { 817 registers[i] = j; 818 used[j] = true; 819 break; 820 } 821 renum++; 822 } 823 } 824 } 825 826 827 printf (" CFA is rsp+%d ", stack_size * 8); 828 829 uint32_t saved_registers_offset = 1; 830 printf (" rip=[CFA-%d]", saved_registers_offset * 8); 831 saved_registers_offset++; 832 833 for (int i = (sizeof (registers) / sizeof (int)) - 1; i >= 0; i--) 834 { 835 switch (registers[i]) 836 { 837 case UNWIND_X86_64_REG_NONE: 838 break; 839 case UNWIND_X86_64_REG_RBX: 840 printf (" rbx=[CFA-%d]", saved_registers_offset * 8); 841 break; 842 case UNWIND_X86_64_REG_R12: 843 printf (" r12=[CFA-%d]", saved_registers_offset * 8); 844 break; 845 case UNWIND_X86_64_REG_R13: 846 printf (" r13=[CFA-%d]", saved_registers_offset * 8); 847 break; 848 case UNWIND_X86_64_REG_R14: 849 printf (" r14=[CFA-%d]", saved_registers_offset * 8); 850 break; 851 case UNWIND_X86_64_REG_R15: 852 printf (" r15=[CFA-%d]", saved_registers_offset * 8); 853 break; 854 case UNWIND_X86_64_REG_RBP: 855 printf (" rbp=[CFA-%d]", saved_registers_offset * 8); 856 break; 857 } 858 saved_registers_offset++; 859 } 860 861 } 862 863 } 864 break; 865 #endif 866 867 case UNWIND_X86_64_MODE_DWARF: 868 { 869 return false; 870 } 871 break; 872 873 case 0: 874 { 875 return false; 876 } 877 break; 878 } 879 return false; 880 } 881 882 enum i386_eh_regnum { 883 eax = 0, 884 ecx = 1, 885 edx = 2, 886 ebx = 3, 887 ebp = 4, 888 esp = 5, 889 esi = 6, 890 edi = 7, 891 eip = 8 // this is officially the Return Address register number, but close enough 892 }; 893 894 // Convert the compact_unwind_info.h register numbering scheme 895 // to eRegisterKindGCC (eh_frame) register numbering scheme. 896 uint32_t 897 translate_to_eh_frame_regnum_i386 (uint32_t unwind_regno) 898 { 899 switch (unwind_regno) 900 { 901 case UNWIND_X86_REG_EBX: 902 return i386_eh_regnum::ebx; 903 case UNWIND_X86_REG_ECX: 904 return i386_eh_regnum::ecx; 905 case UNWIND_X86_REG_EDX: 906 return i386_eh_regnum::edx; 907 case UNWIND_X86_REG_EDI: 908 return i386_eh_regnum::edi; 909 case UNWIND_X86_REG_ESI: 910 return i386_eh_regnum::esi; 911 case UNWIND_X86_REG_EBP: 912 return i386_eh_regnum::ebp; 913 default: 914 return LLDB_INVALID_REGNUM; 915 } 916 } 917 918 919 bool 920 CompactUnwindInfo::CreateUnwindPlan_i386 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start) 921 { 922 unwind_plan.SetSourceName ("compact unwind info"); 923 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes); 924 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); 925 unwind_plan.SetRegisterKind (eRegisterKindGCC); 926 927 unwind_plan.SetLSDAAddress (function_info.lsda_address); 928 unwind_plan.SetPersonalityFunctionPtr (function_info.personality_ptr_address); 929 930 UnwindPlan::RowSP row (new UnwindPlan::Row); 931 932 const int wordsize = 4; 933 int mode = function_info.encoding & UNWIND_X86_MODE_MASK; 934 switch (mode) 935 { 936 case UNWIND_X86_MODE_EBP_FRAME: 937 { 938 row->SetCFARegister (translate_to_eh_frame_regnum_i386 (UNWIND_X86_REG_EBP)); 939 row->SetCFAOffset (2 * wordsize); 940 row->SetOffset (0); 941 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::ebp, wordsize * -2, true); 942 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::eip, wordsize * -1, true); 943 row->SetRegisterLocationToIsCFAPlusOffset (i386_eh_regnum::esp, 0, true); 944 945 uint32_t saved_registers_offset = EXTRACT_BITS (function_info.encoding, UNWIND_X86_EBP_FRAME_OFFSET); 946 947 uint32_t saved_registers_locations = EXTRACT_BITS (function_info.encoding, UNWIND_X86_EBP_FRAME_REGISTERS); 948 949 saved_registers_offset += 2; 950 951 for (int i = 0; i < 5; i++) 952 { 953 uint32_t regnum = saved_registers_locations & 0x7; 954 switch (regnum) 955 { 956 case UNWIND_X86_REG_NONE: 957 break; 958 case UNWIND_X86_REG_EBX: 959 case UNWIND_X86_REG_ECX: 960 case UNWIND_X86_REG_EDX: 961 case UNWIND_X86_REG_EDI: 962 case UNWIND_X86_REG_ESI: 963 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_i386 (regnum), wordsize * -saved_registers_offset, true); 964 break; 965 } 966 saved_registers_offset--; 967 saved_registers_locations >>= 3; 968 } 969 unwind_plan.AppendRow (row); 970 return true; 971 } 972 break; 973 974 case UNWIND_X86_MODE_STACK_IND: 975 case UNWIND_X86_MODE_STACK_IMMD: 976 case UNWIND_X86_MODE_DWARF: 977 { 978 return false; 979 } 980 break; 981 } 982 return false; 983 } 984