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 addr_t text_section_file_address = LLDB_INVALID_ADDRESS; 416 SectionList *sl = m_objfile.GetSectionList (); 417 if (sl) 418 { 419 SectionSP text_sect = sl->FindSectionByType (eSectionTypeCode, true); 420 if (text_sect.get()) 421 { 422 text_section_file_address = text_sect->GetFileAddress(); 423 } 424 } 425 if (text_section_file_address == LLDB_INVALID_ADDRESS) 426 return false; 427 428 addr_t function_offset = address.GetFileAddress() - m_objfile.GetHeaderAddress().GetFileAddress(); 429 430 UnwindIndex key; 431 key.function_offset = function_offset; 432 433 std::vector<UnwindIndex>::const_iterator it; 434 it = std::lower_bound (m_indexes.begin(), m_indexes.end(), key); 435 if (it == m_indexes.end()) 436 { 437 return false; 438 } 439 440 if (it->function_offset != key.function_offset) 441 { 442 if (it != m_indexes.begin()) 443 --it; 444 } 445 446 if (it->sentinal_entry == true) 447 { 448 return false; 449 } 450 451 offset_t second_page_offset = it->second_level; 452 offset_t lsda_array_start = it->lsda_array_start; 453 offset_t lsda_array_count = (it->lsda_array_end - it->lsda_array_start) / 8; 454 455 offset_t offset = second_page_offset; 456 uint32_t kind = m_unwindinfo_data.GetU32(&offset); // UNWIND_SECOND_LEVEL_REGULAR or UNWIND_SECOND_LEVEL_COMPRESSED 457 458 if (kind == UNWIND_SECOND_LEVEL_REGULAR) 459 { 460 // struct unwind_info_regular_second_level_page_header 461 // { 462 // uint32_t kind; // UNWIND_SECOND_LEVEL_REGULAR 463 // uint16_t entryPageOffset; 464 // uint16_t entryCount; 465 466 // typedef uint32_t compact_unwind_encoding_t; 467 // struct unwind_info_regular_second_level_entry 468 // { 469 // uint32_t functionOffset; 470 // compact_unwind_encoding_t encoding; 471 472 uint16_t entry_page_offset = m_unwindinfo_data.GetU16(&offset); // entryPageOffset 473 uint16_t entry_count = m_unwindinfo_data.GetU16(&offset); // entryCount 474 475 offset_t entry_offset = BinarySearchRegularSecondPage (second_page_offset + entry_page_offset, entry_count, function_offset); 476 if (entry_offset == LLDB_INVALID_OFFSET) 477 { 478 return false; 479 } 480 entry_offset += 4; // skip over functionOffset 481 unwind_info.encoding = m_unwindinfo_data.GetU32(&entry_offset); // encoding 482 if (unwind_info.encoding & UNWIND_HAS_LSDA) 483 { 484 SectionList *sl = m_objfile.GetSectionList (); 485 if (sl) 486 { 487 uint32_t lsda_offset = GetLSDAForFunctionOffset (lsda_array_start, lsda_array_count, function_offset); 488 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress(); 489 unwind_info.lsda_address.ResolveAddressUsingFileSections (objfile_header_file_address + lsda_offset, sl); 490 } 491 } 492 if (unwind_info.encoding & UNWIND_PERSONALITY_MASK) 493 { 494 uint32_t personality_index = EXTRACT_BITS (unwind_info.encoding, UNWIND_PERSONALITY_MASK); 495 496 if (personality_index > 0) 497 { 498 personality_index--; 499 if (personality_index < m_unwind_header.personality_array_count) 500 { 501 offset_t offset = m_unwind_header.personality_array_offset; 502 offset += 4 * personality_index; 503 SectionList *sl = m_objfile.GetSectionList (); 504 if (sl) 505 { 506 uint32_t personality_offset = m_unwindinfo_data.GetU32(&offset); 507 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress(); 508 unwind_info.personality_ptr_address.ResolveAddressUsingFileSections (objfile_header_file_address + personality_offset, sl); 509 } 510 } 511 } 512 } 513 return true; 514 } 515 else if (kind == UNWIND_SECOND_LEVEL_COMPRESSED) 516 { 517 // struct unwind_info_compressed_second_level_page_header 518 // { 519 // uint32_t kind; // UNWIND_SECOND_LEVEL_COMPRESSED 520 // uint16_t entryPageOffset; // offset from this 2nd lvl page idx to array of entries 521 // // (an entry has a function offset and index into the encodings) 522 // // NB function offset from the entry in the compressed page 523 // // must be added to the index's functionOffset value. 524 // uint16_t entryCount; 525 // uint16_t encodingsPageOffset; // offset from this 2nd lvl page idx to array of encodings 526 // uint16_t encodingsCount; 527 528 uint16_t entry_page_offset = m_unwindinfo_data.GetU16(&offset); // entryPageOffset 529 uint16_t entry_count = m_unwindinfo_data.GetU16(&offset); // entryCount 530 uint16_t encodings_page_offset = m_unwindinfo_data.GetU16(&offset); // encodingsPageOffset 531 uint16_t encodings_count = m_unwindinfo_data.GetU16(&offset); // encodingsCount 532 533 uint32_t encoding_index = BinarySearchCompressedSecondPage (second_page_offset + entry_page_offset, entry_count, function_offset, it->function_offset); 534 if (encoding_index == UINT32_MAX || encoding_index >= encodings_count + m_unwind_header.common_encodings_array_count) 535 { 536 return false; 537 } 538 uint32_t encoding = 0; 539 if (encoding_index < m_unwind_header.common_encodings_array_count) 540 { 541 offset = m_unwind_header.common_encodings_array_offset + (encoding_index * sizeof (uint32_t)); 542 encoding = m_unwindinfo_data.GetU32(&offset); // encoding entry from the commonEncodingsArray 543 } 544 else 545 { 546 uint32_t page_specific_entry_index = encoding_index - m_unwind_header.common_encodings_array_count; 547 offset = second_page_offset + encodings_page_offset + (page_specific_entry_index * sizeof (uint32_t)); 548 encoding = m_unwindinfo_data.GetU32(&offset); // encoding entry from the page-specific encoding array 549 } 550 if (encoding == 0) 551 return false; 552 unwind_info.encoding = encoding; 553 554 unwind_info.encoding = encoding; 555 if (unwind_info.encoding & UNWIND_HAS_LSDA) 556 { 557 SectionList *sl = m_objfile.GetSectionList (); 558 if (sl) 559 { 560 uint32_t lsda_offset = GetLSDAForFunctionOffset (lsda_array_start, lsda_array_count, function_offset); 561 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress(); 562 unwind_info.lsda_address.ResolveAddressUsingFileSections (objfile_header_file_address + lsda_offset, sl); 563 } 564 } 565 if (unwind_info.encoding & UNWIND_PERSONALITY_MASK) 566 { 567 uint32_t personality_index = EXTRACT_BITS (unwind_info.encoding, UNWIND_PERSONALITY_MASK); 568 569 if (personality_index > 0) 570 { 571 personality_index--; 572 if (personality_index < m_unwind_header.personality_array_count) 573 { 574 offset_t offset = m_unwind_header.personality_array_offset; 575 offset += 4 * personality_index; 576 SectionList *sl = m_objfile.GetSectionList (); 577 if (sl) 578 { 579 uint32_t personality_offset = m_unwindinfo_data.GetU32(&offset); 580 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress(); 581 unwind_info.personality_ptr_address.ResolveAddressUsingFileSections (objfile_header_file_address + personality_offset, sl); 582 } 583 } 584 } 585 } 586 return true; 587 } 588 return false; 589 } 590 591 enum x86_64_eh_regnum { 592 rax = 0, 593 rdx = 1, 594 rcx = 2, 595 rbx = 3, 596 rsi = 4, 597 rdi = 5, 598 rbp = 6, 599 rsp = 7, 600 r8 = 8, 601 r9 = 9, 602 r10 = 10, 603 r11 = 11, 604 r12 = 12, 605 r13 = 13, 606 r14 = 14, 607 r15 = 15, 608 rip = 16 // this is officially the Return Address register number, but close enough 609 }; 610 611 // Convert the compact_unwind_info.h register numbering scheme 612 // to eRegisterKindGCC (eh_frame) register numbering scheme. 613 uint32_t 614 translate_to_eh_frame_regnum_x86_64 (uint32_t unwind_regno) 615 { 616 switch (unwind_regno) 617 { 618 case UNWIND_X86_64_REG_RBX: 619 return x86_64_eh_regnum::rbx; 620 case UNWIND_X86_64_REG_R12: 621 return x86_64_eh_regnum::r12; 622 case UNWIND_X86_64_REG_R13: 623 return x86_64_eh_regnum::r13; 624 case UNWIND_X86_64_REG_R14: 625 return x86_64_eh_regnum::r14; 626 case UNWIND_X86_64_REG_R15: 627 return x86_64_eh_regnum::r15; 628 case UNWIND_X86_64_REG_RBP: 629 return x86_64_eh_regnum::rbp; 630 default: 631 return LLDB_INVALID_REGNUM; 632 } 633 } 634 635 bool 636 CompactUnwindInfo::CreateUnwindPlan_x86_64 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start) 637 { 638 unwind_plan.SetSourceName ("compact unwind info"); 639 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes); 640 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); 641 unwind_plan.SetRegisterKind (eRegisterKindGCC); 642 643 unwind_plan.SetLSDAAddress (function_info.lsda_address); 644 unwind_plan.SetPersonalityFunctionPtr (function_info.personality_ptr_address); 645 646 UnwindPlan::RowSP row (new UnwindPlan::Row); 647 648 const int wordsize = 8; 649 int mode = function_info.encoding & UNWIND_X86_64_MODE_MASK; 650 switch (mode) 651 { 652 case UNWIND_X86_64_MODE_RBP_FRAME: 653 { 654 row->SetCFARegister (translate_to_eh_frame_regnum_x86_64 (UNWIND_X86_64_REG_RBP)); 655 row->SetCFAOffset (2 * wordsize); 656 row->SetOffset (0); 657 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rbp, wordsize * -2, true); 658 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rip, wordsize * -1, true); 659 row->SetRegisterLocationToIsCFAPlusOffset (x86_64_eh_regnum::rsp, 0, true); 660 661 uint32_t saved_registers_offset = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_RBP_FRAME_OFFSET); 662 663 uint32_t saved_registers_locations = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_RBP_FRAME_REGISTERS); 664 665 saved_registers_offset += 2; 666 667 for (int i = 0; i < 5; i++) 668 { 669 uint32_t regnum = saved_registers_locations & 0x7; 670 switch (regnum) 671 { 672 case UNWIND_X86_64_REG_NONE: 673 break; 674 case UNWIND_X86_64_REG_RBX: 675 case UNWIND_X86_64_REG_R12: 676 case UNWIND_X86_64_REG_R13: 677 case UNWIND_X86_64_REG_R14: 678 case UNWIND_X86_64_REG_R15: 679 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_x86_64 (regnum), wordsize * -saved_registers_offset, true); 680 break; 681 } 682 saved_registers_offset--; 683 saved_registers_locations >>= 3; 684 } 685 unwind_plan.AppendRow (row); 686 return true; 687 } 688 break; 689 690 case UNWIND_X86_64_MODE_STACK_IND: 691 { 692 // The clang in Xcode 6 is emitting incorrect compact unwind encodings for this 693 // style of unwind. It was fixed in llvm r217020 although the algorith being 694 // used to compute this style of unwind in generateCompactUnwindEncodingImpl() 695 // isn't as foolproof as I'm comfortable with -- if any instructions other than 696 // a push are scheduled before the subq, it will give bogus encoding results. 697 698 // The target and pc_or_function_start arguments will be needed to handle this 699 // encoding style correctly -- to find the start address of the function and 700 // read memory offset from there. 701 return false; 702 } 703 break; 704 705 #if 0 706 case UNWIND_X86_64_MODE_STACK_IMMD: 707 { 708 uint32_t stack_size = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_SIZE); 709 uint32_t register_count = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT); 710 uint32_t permutation = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION); 711 712 if (mode == UNWIND_X86_64_MODE_STACK_IND && function_start) 713 { 714 uint32_t stack_adjust = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_ADJUST); 715 716 // offset into the function instructions; 0 == beginning of first instruction 717 uint32_t offset_to_subl_insn = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_SIZE); 718 719 stack_size = *((uint32_t*) (function_start + offset_to_subl_insn)); 720 721 stack_size += stack_adjust * 8; 722 723 printf ("large stack "); 724 } 725 726 printf ("frameless function: stack size %d, register count %d ", stack_size * 8, register_count); 727 728 if (register_count == 0) 729 { 730 printf (" no registers saved"); 731 } 732 else 733 { 734 735 // We need to include (up to) 6 registers in 10 bits. 736 // That would be 18 bits if we just used 3 bits per reg to indicate 737 // the order they're saved on the stack. 738 // 739 // This is done with Lehmer code permutation, e.g. see 740 // http://stackoverflow.com/questions/1506078/fast-permutation-number-permutation-mapping-algorithms 741 int permunreg[6]; 742 743 // This decodes the variable-base number in the 10 bits 744 // and gives us the Lehmer code sequence which can then 745 // be decoded. 746 747 switch (register_count) 748 { 749 case 6: 750 permunreg[0] = permutation/120; // 120 == 5! 751 permutation -= (permunreg[0]*120); 752 permunreg[1] = permutation/24; // 24 == 4! 753 permutation -= (permunreg[1]*24); 754 permunreg[2] = permutation/6; // 6 == 3! 755 permutation -= (permunreg[2]*6); 756 permunreg[3] = permutation/2; // 2 == 2! 757 permutation -= (permunreg[3]*2); 758 permunreg[4] = permutation; // 1 == 1! 759 permunreg[5] = 0; 760 break; 761 case 5: 762 permunreg[0] = permutation/120; 763 permutation -= (permunreg[0]*120); 764 permunreg[1] = permutation/24; 765 permutation -= (permunreg[1]*24); 766 permunreg[2] = permutation/6; 767 permutation -= (permunreg[2]*6); 768 permunreg[3] = permutation/2; 769 permutation -= (permunreg[3]*2); 770 permunreg[4] = permutation; 771 break; 772 case 4: 773 permunreg[0] = permutation/60; 774 permutation -= (permunreg[0]*60); 775 permunreg[1] = permutation/12; 776 permutation -= (permunreg[1]*12); 777 permunreg[2] = permutation/3; 778 permutation -= (permunreg[2]*3); 779 permunreg[3] = permutation; 780 break; 781 case 3: 782 permunreg[0] = permutation/20; 783 permutation -= (permunreg[0]*20); 784 permunreg[1] = permutation/4; 785 permutation -= (permunreg[1]*4); 786 permunreg[2] = permutation; 787 break; 788 case 2: 789 permunreg[0] = permutation/5; 790 permutation -= (permunreg[0]*5); 791 permunreg[1] = permutation; 792 break; 793 case 1: 794 permunreg[0] = permutation; 795 break; 796 } 797 798 // Decode the Lehmer code for this permutation of 799 // the registers v. http://en.wikipedia.org/wiki/Lehmer_code 800 801 int registers[6]; 802 bool used[7] = { false, false, false, false, false, false, false }; 803 for (int i = 0; i < register_count; i++) 804 { 805 int renum = 0; 806 for (int j = 1; j < 7; j++) 807 { 808 if (used[j] == false) 809 { 810 if (renum == permunreg[i]) 811 { 812 registers[i] = j; 813 used[j] = true; 814 break; 815 } 816 renum++; 817 } 818 } 819 } 820 821 822 printf (" CFA is rsp+%d ", stack_size * 8); 823 824 uint32_t saved_registers_offset = 1; 825 printf (" rip=[CFA-%d]", saved_registers_offset * 8); 826 saved_registers_offset++; 827 828 for (int i = (sizeof (registers) / sizeof (int)) - 1; i >= 0; i--) 829 { 830 switch (registers[i]) 831 { 832 case UNWIND_X86_64_REG_NONE: 833 break; 834 case UNWIND_X86_64_REG_RBX: 835 printf (" rbx=[CFA-%d]", saved_registers_offset * 8); 836 break; 837 case UNWIND_X86_64_REG_R12: 838 printf (" r12=[CFA-%d]", saved_registers_offset * 8); 839 break; 840 case UNWIND_X86_64_REG_R13: 841 printf (" r13=[CFA-%d]", saved_registers_offset * 8); 842 break; 843 case UNWIND_X86_64_REG_R14: 844 printf (" r14=[CFA-%d]", saved_registers_offset * 8); 845 break; 846 case UNWIND_X86_64_REG_R15: 847 printf (" r15=[CFA-%d]", saved_registers_offset * 8); 848 break; 849 case UNWIND_X86_64_REG_RBP: 850 printf (" rbp=[CFA-%d]", saved_registers_offset * 8); 851 break; 852 } 853 saved_registers_offset++; 854 } 855 856 } 857 858 } 859 break; 860 #endif 861 862 case UNWIND_X86_64_MODE_DWARF: 863 { 864 return false; 865 } 866 break; 867 868 case 0: 869 { 870 return false; 871 } 872 break; 873 } 874 return false; 875 } 876 877 enum i386_eh_regnum { 878 eax = 0, 879 ecx = 1, 880 edx = 2, 881 ebx = 3, 882 ebp = 4, 883 esp = 5, 884 esi = 6, 885 edi = 7, 886 eip = 8 // this is officially the Return Address register number, but close enough 887 }; 888 889 // Convert the compact_unwind_info.h register numbering scheme 890 // to eRegisterKindGCC (eh_frame) register numbering scheme. 891 uint32_t 892 translate_to_eh_frame_regnum_i386 (uint32_t unwind_regno) 893 { 894 switch (unwind_regno) 895 { 896 case UNWIND_X86_REG_EBX: 897 return i386_eh_regnum::ebx; 898 case UNWIND_X86_REG_ECX: 899 return i386_eh_regnum::ecx; 900 case UNWIND_X86_REG_EDX: 901 return i386_eh_regnum::edx; 902 case UNWIND_X86_REG_EDI: 903 return i386_eh_regnum::edi; 904 case UNWIND_X86_REG_ESI: 905 return i386_eh_regnum::esi; 906 case UNWIND_X86_REG_EBP: 907 return i386_eh_regnum::ebp; 908 default: 909 return LLDB_INVALID_REGNUM; 910 } 911 } 912 913 914 bool 915 CompactUnwindInfo::CreateUnwindPlan_i386 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start) 916 { 917 unwind_plan.SetSourceName ("compact unwind info"); 918 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes); 919 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); 920 unwind_plan.SetRegisterKind (eRegisterKindGCC); 921 922 unwind_plan.SetLSDAAddress (function_info.lsda_address); 923 unwind_plan.SetPersonalityFunctionPtr (function_info.personality_ptr_address); 924 925 UnwindPlan::RowSP row (new UnwindPlan::Row); 926 927 const int wordsize = 4; 928 int mode = function_info.encoding & UNWIND_X86_MODE_MASK; 929 switch (mode) 930 { 931 case UNWIND_X86_MODE_EBP_FRAME: 932 { 933 row->SetCFARegister (translate_to_eh_frame_regnum_i386 (UNWIND_X86_REG_EBP)); 934 row->SetCFAOffset (2 * wordsize); 935 row->SetOffset (0); 936 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::ebp, wordsize * -2, true); 937 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::eip, wordsize * -1, true); 938 row->SetRegisterLocationToIsCFAPlusOffset (i386_eh_regnum::esp, 0, true); 939 940 uint32_t saved_registers_offset = EXTRACT_BITS (function_info.encoding, UNWIND_X86_EBP_FRAME_OFFSET); 941 942 uint32_t saved_registers_locations = EXTRACT_BITS (function_info.encoding, UNWIND_X86_EBP_FRAME_REGISTERS); 943 944 saved_registers_offset += 2; 945 946 for (int i = 0; i < 5; i++) 947 { 948 uint32_t regnum = saved_registers_locations & 0x7; 949 switch (regnum) 950 { 951 case UNWIND_X86_REG_NONE: 952 break; 953 case UNWIND_X86_REG_EBX: 954 case UNWIND_X86_REG_ECX: 955 case UNWIND_X86_REG_EDX: 956 case UNWIND_X86_REG_EDI: 957 case UNWIND_X86_REG_ESI: 958 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_i386 (regnum), wordsize * -saved_registers_offset, true); 959 break; 960 } 961 saved_registers_offset--; 962 saved_registers_locations >>= 3; 963 } 964 unwind_plan.AppendRow (row); 965 return true; 966 } 967 break; 968 969 case UNWIND_X86_MODE_STACK_IND: 970 case UNWIND_X86_MODE_STACK_IMMD: 971 case UNWIND_X86_MODE_DWARF: 972 { 973 return false; 974 } 975 break; 976 } 977 return false; 978 } 979