1 //===-- DWARFCallFrameInfo.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 #include "lldb/Symbol/DWARFCallFrameInfo.h" 11 #include "lldb/Core/Module.h" 12 #include "lldb/Core/Section.h" 13 #include "lldb/Core/dwarf.h" 14 #include "lldb/Host/Host.h" 15 #include "lldb/Symbol/ObjectFile.h" 16 #include "lldb/Symbol/UnwindPlan.h" 17 #include "lldb/Target/RegisterContext.h" 18 #include "lldb/Target/Thread.h" 19 #include "lldb/Utility/ArchSpec.h" 20 #include "lldb/Utility/Log.h" 21 #include "lldb/Utility/Timer.h" 22 #include <list> 23 24 using namespace lldb; 25 using namespace lldb_private; 26 27 //---------------------------------------------------------------------- 28 // GetDwarfEHPtr 29 // 30 // Used for calls when the value type is specified by a DWARF EH Frame 31 // pointer encoding. 32 //---------------------------------------------------------------------- 33 static uint64_t 34 GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr, 35 uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr, 36 addr_t data_addr) //, BSDRelocs *data_relocs) const 37 { 38 if (eh_ptr_enc == DW_EH_PE_omit) 39 return ULLONG_MAX; // Value isn't in the buffer... 40 41 uint64_t baseAddress = 0; 42 uint64_t addressValue = 0; 43 const uint32_t addr_size = DE.GetAddressByteSize(); 44 #ifdef LLDB_CONFIGURATION_DEBUG 45 assert(addr_size == 4 || addr_size == 8); 46 #endif 47 48 bool signExtendValue = false; 49 // Decode the base part or adjust our offset 50 switch (eh_ptr_enc & 0x70) { 51 case DW_EH_PE_pcrel: 52 signExtendValue = true; 53 baseAddress = *offset_ptr; 54 if (pc_rel_addr != LLDB_INVALID_ADDRESS) 55 baseAddress += pc_rel_addr; 56 // else 57 // Log::GlobalWarning ("PC relative pointer encoding found with 58 // invalid pc relative address."); 59 break; 60 61 case DW_EH_PE_textrel: 62 signExtendValue = true; 63 if (text_addr != LLDB_INVALID_ADDRESS) 64 baseAddress = text_addr; 65 // else 66 // Log::GlobalWarning ("text relative pointer encoding being 67 // decoded with invalid text section address, setting base address 68 // to zero."); 69 break; 70 71 case DW_EH_PE_datarel: 72 signExtendValue = true; 73 if (data_addr != LLDB_INVALID_ADDRESS) 74 baseAddress = data_addr; 75 // else 76 // Log::GlobalWarning ("data relative pointer encoding being 77 // decoded with invalid data section address, setting base address 78 // to zero."); 79 break; 80 81 case DW_EH_PE_funcrel: 82 signExtendValue = true; 83 break; 84 85 case DW_EH_PE_aligned: { 86 // SetPointerSize should be called prior to extracting these so the 87 // pointer size is cached 88 assert(addr_size != 0); 89 if (addr_size) { 90 // Align to a address size boundary first 91 uint32_t alignOffset = *offset_ptr % addr_size; 92 if (alignOffset) 93 offset_ptr += addr_size - alignOffset; 94 } 95 } break; 96 97 default: 98 break; 99 } 100 101 // Decode the value part 102 switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) { 103 case DW_EH_PE_absptr: { 104 addressValue = DE.GetAddress(offset_ptr); 105 // if (data_relocs) 106 // addressValue = data_relocs->Relocate(*offset_ptr - 107 // addr_size, *this, addressValue); 108 } break; 109 case DW_EH_PE_uleb128: 110 addressValue = DE.GetULEB128(offset_ptr); 111 break; 112 case DW_EH_PE_udata2: 113 addressValue = DE.GetU16(offset_ptr); 114 break; 115 case DW_EH_PE_udata4: 116 addressValue = DE.GetU32(offset_ptr); 117 break; 118 case DW_EH_PE_udata8: 119 addressValue = DE.GetU64(offset_ptr); 120 break; 121 case DW_EH_PE_sleb128: 122 addressValue = DE.GetSLEB128(offset_ptr); 123 break; 124 case DW_EH_PE_sdata2: 125 addressValue = (int16_t)DE.GetU16(offset_ptr); 126 break; 127 case DW_EH_PE_sdata4: 128 addressValue = (int32_t)DE.GetU32(offset_ptr); 129 break; 130 case DW_EH_PE_sdata8: 131 addressValue = (int64_t)DE.GetU64(offset_ptr); 132 break; 133 default: 134 // Unhandled encoding type 135 assert(eh_ptr_enc); 136 break; 137 } 138 139 // Since we promote everything to 64 bit, we may need to sign extend 140 if (signExtendValue && addr_size < sizeof(baseAddress)) { 141 uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull); 142 if (sign_bit & addressValue) { 143 uint64_t mask = ~sign_bit + 1; 144 addressValue |= mask; 145 } 146 } 147 return baseAddress + addressValue; 148 } 149 150 DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile &objfile, 151 SectionSP §ion_sp, Type type) 152 : m_objfile(objfile), m_section_sp(section_sp), m_type(type) {} 153 154 bool DWARFCallFrameInfo::GetUnwindPlan(Address addr, UnwindPlan &unwind_plan) { 155 FDEEntryMap::Entry fde_entry; 156 157 // Make sure that the Address we're searching for is the same object file 158 // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index. 159 ModuleSP module_sp = addr.GetModule(); 160 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || 161 module_sp->GetObjectFile() != &m_objfile) 162 return false; 163 164 if (GetFDEEntryByFileAddress(addr.GetFileAddress(), fde_entry) == false) 165 return false; 166 return FDEToUnwindPlan(fde_entry.data, addr, unwind_plan); 167 } 168 169 bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) { 170 171 // Make sure that the Address we're searching for is the same object file 172 // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index. 173 ModuleSP module_sp = addr.GetModule(); 174 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || 175 module_sp->GetObjectFile() != &m_objfile) 176 return false; 177 178 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 179 return false; 180 GetFDEIndex(); 181 FDEEntryMap::Entry *fde_entry = 182 m_fde_index.FindEntryThatContains(addr.GetFileAddress()); 183 if (!fde_entry) 184 return false; 185 186 range = AddressRange(fde_entry->base, fde_entry->size, 187 m_objfile.GetSectionList()); 188 return true; 189 } 190 191 bool DWARFCallFrameInfo::GetFDEEntryByFileAddress( 192 addr_t file_addr, FDEEntryMap::Entry &fde_entry) { 193 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 194 return false; 195 196 GetFDEIndex(); 197 198 if (m_fde_index.IsEmpty()) 199 return false; 200 201 FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains(file_addr); 202 203 if (fde == nullptr) 204 return false; 205 206 fde_entry = *fde; 207 return true; 208 } 209 210 void DWARFCallFrameInfo::GetFunctionAddressAndSizeVector( 211 FunctionAddressAndSizeVector &function_info) { 212 GetFDEIndex(); 213 const size_t count = m_fde_index.GetSize(); 214 function_info.Clear(); 215 if (count > 0) 216 function_info.Reserve(count); 217 for (size_t i = 0; i < count; ++i) { 218 const FDEEntryMap::Entry *func_offset_data_entry = 219 m_fde_index.GetEntryAtIndex(i); 220 if (func_offset_data_entry) { 221 FunctionAddressAndSizeVector::Entry function_offset_entry( 222 func_offset_data_entry->base, func_offset_data_entry->size); 223 function_info.Append(function_offset_entry); 224 } 225 } 226 } 227 228 const DWARFCallFrameInfo::CIE * 229 DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) { 230 cie_map_t::iterator pos = m_cie_map.find(cie_offset); 231 232 if (pos != m_cie_map.end()) { 233 // Parse and cache the CIE 234 if (pos->second.get() == nullptr) 235 pos->second = ParseCIE(cie_offset); 236 237 return pos->second.get(); 238 } 239 return nullptr; 240 } 241 242 DWARFCallFrameInfo::CIESP 243 DWARFCallFrameInfo::ParseCIE(const dw_offset_t cie_offset) { 244 CIESP cie_sp(new CIE(cie_offset)); 245 lldb::offset_t offset = cie_offset; 246 if (m_cfi_data_initialized == false) 247 GetCFIData(); 248 uint32_t length = m_cfi_data.GetU32(&offset); 249 dw_offset_t cie_id, end_offset; 250 bool is_64bit = (length == UINT32_MAX); 251 if (is_64bit) { 252 length = m_cfi_data.GetU64(&offset); 253 cie_id = m_cfi_data.GetU64(&offset); 254 end_offset = cie_offset + length + 12; 255 } else { 256 cie_id = m_cfi_data.GetU32(&offset); 257 end_offset = cie_offset + length + 4; 258 } 259 if (length > 0 && ((m_type == DWARF && cie_id == UINT32_MAX) || 260 (m_type == EH && cie_id == 0ul))) { 261 size_t i; 262 // cie.offset = cie_offset; 263 // cie.length = length; 264 // cie.cieID = cieID; 265 cie_sp->ptr_encoding = DW_EH_PE_absptr; // default 266 cie_sp->version = m_cfi_data.GetU8(&offset); 267 if (cie_sp->version > CFI_VERSION4) { 268 Host::SystemLog(Host::eSystemLogError, 269 "CIE parse error: CFI version %d is not supported\n", 270 cie_sp->version); 271 return nullptr; 272 } 273 274 for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) { 275 cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset); 276 if (cie_sp->augmentation[i] == '\0') { 277 // Zero out remaining bytes in augmentation string 278 for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j) 279 cie_sp->augmentation[j] = '\0'; 280 281 break; 282 } 283 } 284 285 if (i == CFI_AUG_MAX_SIZE && 286 cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') { 287 Host::SystemLog(Host::eSystemLogError, 288 "CIE parse error: CIE augmentation string was too large " 289 "for the fixed sized buffer of %d bytes.\n", 290 CFI_AUG_MAX_SIZE); 291 return nullptr; 292 } 293 294 // m_cfi_data uses address size from target architecture of the process 295 // may ignore these fields? 296 if (m_type == DWARF && cie_sp->version >= CFI_VERSION4) { 297 cie_sp->address_size = m_cfi_data.GetU8(&offset); 298 cie_sp->segment_size = m_cfi_data.GetU8(&offset); 299 } 300 301 cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset); 302 cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset); 303 304 cie_sp->return_addr_reg_num = 305 m_type == DWARF && cie_sp->version >= CFI_VERSION3 306 ? static_cast<uint32_t>(m_cfi_data.GetULEB128(&offset)) 307 : m_cfi_data.GetU8(&offset); 308 309 if (cie_sp->augmentation[0]) { 310 // Get the length of the eh_frame augmentation data 311 // which starts with a ULEB128 length in bytes 312 const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset); 313 const size_t aug_data_end = offset + aug_data_len; 314 const size_t aug_str_len = strlen(cie_sp->augmentation); 315 // A 'z' may be present as the first character of the string. 316 // If present, the Augmentation Data field shall be present. 317 // The contents of the Augmentation Data shall be interpreted 318 // according to other characters in the Augmentation String. 319 if (cie_sp->augmentation[0] == 'z') { 320 // Extract the Augmentation Data 321 size_t aug_str_idx = 0; 322 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) { 323 char aug = cie_sp->augmentation[aug_str_idx]; 324 switch (aug) { 325 case 'L': 326 // Indicates the presence of one argument in the 327 // Augmentation Data of the CIE, and a corresponding 328 // argument in the Augmentation Data of the FDE. The 329 // argument in the Augmentation Data of the CIE is 330 // 1-byte and represents the pointer encoding used 331 // for the argument in the Augmentation Data of the 332 // FDE, which is the address of a language-specific 333 // data area (LSDA). The size of the LSDA pointer is 334 // specified by the pointer encoding used. 335 cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset); 336 break; 337 338 case 'P': 339 // Indicates the presence of two arguments in the 340 // Augmentation Data of the CIE. The first argument 341 // is 1-byte and represents the pointer encoding 342 // used for the second argument, which is the 343 // address of a personality routine handler. The 344 // size of the personality routine pointer is 345 // specified by the pointer encoding used. 346 // 347 // The address of the personality function will 348 // be stored at this location. Pre-execution, it 349 // will be all zero's so don't read it until we're 350 // trying to do an unwind & the reloc has been 351 // resolved. 352 { 353 uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset); 354 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 355 cie_sp->personality_loc = GetGNUEHPointer( 356 m_cfi_data, &offset, arg_ptr_encoding, pc_rel_addr, 357 LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS); 358 } 359 break; 360 361 case 'R': 362 // A 'R' may be present at any position after the 363 // first character of the string. The Augmentation 364 // Data shall include a 1 byte argument that 365 // represents the pointer encoding for the address 366 // pointers used in the FDE. 367 // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4 368 cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset); 369 break; 370 } 371 } 372 } else if (strcmp(cie_sp->augmentation, "eh") == 0) { 373 // If the Augmentation string has the value "eh", then 374 // the EH Data field shall be present 375 } 376 377 // Set the offset to be the end of the augmentation data just in case 378 // we didn't understand any of the data. 379 offset = (uint32_t)aug_data_end; 380 } 381 382 if (end_offset > offset) { 383 cie_sp->inst_offset = offset; 384 cie_sp->inst_length = end_offset - offset; 385 } 386 while (offset < end_offset) { 387 uint8_t inst = m_cfi_data.GetU8(&offset); 388 uint8_t primary_opcode = inst & 0xC0; 389 uint8_t extended_opcode = inst & 0x3F; 390 391 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, 392 cie_sp->data_align, offset, 393 cie_sp->initial_row)) 394 break; // Stop if we hit an unrecognized opcode 395 } 396 } 397 398 return cie_sp; 399 } 400 401 void DWARFCallFrameInfo::GetCFIData() { 402 if (m_cfi_data_initialized == false) { 403 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND)); 404 if (log) 405 m_objfile.GetModule()->LogMessage(log, "Reading EH frame info"); 406 m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data); 407 m_cfi_data_initialized = true; 408 } 409 } 410 // Scan through the eh_frame or debug_frame section looking for FDEs and noting 411 // the start/end addresses 412 // of the functions and a pointer back to the function's FDE for later 413 // expansion. 414 // Internalize CIEs as we come across them. 415 416 void DWARFCallFrameInfo::GetFDEIndex() { 417 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 418 return; 419 420 if (m_fde_index_initialized) 421 return; 422 423 std::lock_guard<std::mutex> guard(m_fde_index_mutex); 424 425 if (m_fde_index_initialized) // if two threads hit the locker 426 return; 427 428 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 429 Timer scoped_timer(func_cat, "%s - %s", LLVM_PRETTY_FUNCTION, 430 m_objfile.GetFileSpec().GetFilename().AsCString("")); 431 432 bool clear_address_zeroth_bit = false; 433 ArchSpec arch; 434 if (m_objfile.GetArchitecture(arch)) { 435 if (arch.GetTriple().getArch() == llvm::Triple::arm || 436 arch.GetTriple().getArch() == llvm::Triple::thumb) 437 clear_address_zeroth_bit = true; 438 } 439 440 lldb::offset_t offset = 0; 441 if (m_cfi_data_initialized == false) 442 GetCFIData(); 443 while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) { 444 const dw_offset_t current_entry = offset; 445 dw_offset_t cie_id, next_entry, cie_offset; 446 uint32_t len = m_cfi_data.GetU32(&offset); 447 bool is_64bit = (len == UINT32_MAX); 448 if (is_64bit) { 449 len = m_cfi_data.GetU64(&offset); 450 cie_id = m_cfi_data.GetU64(&offset); 451 next_entry = current_entry + len + 12; 452 cie_offset = current_entry + 12 - cie_id; 453 } else { 454 cie_id = m_cfi_data.GetU32(&offset); 455 next_entry = current_entry + len + 4; 456 cie_offset = current_entry + 4 - cie_id; 457 } 458 459 if (next_entry > m_cfi_data.GetByteSize() + 1) { 460 Host::SystemLog(Host::eSystemLogError, "error: Invalid fde/cie next " 461 "entry offset of 0x%x found in " 462 "cie/fde at 0x%x\n", 463 next_entry, current_entry); 464 // Don't trust anything in this eh_frame section if we find blatantly 465 // invalid data. 466 m_fde_index.Clear(); 467 m_fde_index_initialized = true; 468 return; 469 } 470 471 // An FDE entry contains CIE_pointer in debug_frame in same place as cie_id 472 // in eh_frame. CIE_pointer is an offset into the .debug_frame section. 473 // So, variable cie_offset should be equal to cie_id for debug_frame. 474 // FDE entries with cie_id == 0 shouldn't be ignored for it. 475 if ((cie_id == 0 && m_type == EH) || cie_id == UINT32_MAX || len == 0) { 476 auto cie_sp = ParseCIE(current_entry); 477 if (!cie_sp) { 478 // Cannot parse, the reason is already logged 479 m_fde_index.Clear(); 480 m_fde_index_initialized = true; 481 return; 482 } 483 484 m_cie_map[current_entry] = std::move(cie_sp); 485 offset = next_entry; 486 continue; 487 } 488 489 if (m_type == DWARF) 490 cie_offset = cie_id; 491 492 if (cie_offset > m_cfi_data.GetByteSize()) { 493 Host::SystemLog(Host::eSystemLogError, 494 "error: Invalid cie offset of 0x%x " 495 "found in cie/fde at 0x%x\n", 496 cie_offset, current_entry); 497 // Don't trust anything in this eh_frame section if we find blatantly 498 // invalid data. 499 m_fde_index.Clear(); 500 m_fde_index_initialized = true; 501 return; 502 } 503 504 const CIE *cie = GetCIE(cie_offset); 505 if (cie) { 506 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 507 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; 508 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; 509 510 lldb::addr_t addr = 511 GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr, 512 text_addr, data_addr); 513 if (clear_address_zeroth_bit) 514 addr &= ~1ull; 515 516 lldb::addr_t length = GetGNUEHPointer( 517 m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, 518 pc_rel_addr, text_addr, data_addr); 519 FDEEntryMap::Entry fde(addr, length, current_entry); 520 m_fde_index.Append(fde); 521 } else { 522 Host::SystemLog(Host::eSystemLogError, "error: unable to find CIE at " 523 "0x%8.8x for cie_id = 0x%8.8x for " 524 "entry at 0x%8.8x.\n", 525 cie_offset, cie_id, current_entry); 526 } 527 offset = next_entry; 528 } 529 m_fde_index.Sort(); 530 m_fde_index_initialized = true; 531 } 532 533 bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset, 534 Address startaddr, 535 UnwindPlan &unwind_plan) { 536 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND); 537 lldb::offset_t offset = dwarf_offset; 538 lldb::offset_t current_entry = offset; 539 540 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 541 return false; 542 543 if (m_cfi_data_initialized == false) 544 GetCFIData(); 545 546 uint32_t length = m_cfi_data.GetU32(&offset); 547 dw_offset_t cie_offset; 548 bool is_64bit = (length == UINT32_MAX); 549 if (is_64bit) { 550 length = m_cfi_data.GetU64(&offset); 551 cie_offset = m_cfi_data.GetU64(&offset); 552 } else { 553 cie_offset = m_cfi_data.GetU32(&offset); 554 } 555 556 // FDE entries with zeroth cie_offset may occur for debug_frame. 557 assert(!(m_type == EH && 0 == cie_offset) && cie_offset != UINT32_MAX); 558 559 // Translate the CIE_id from the eh_frame format, which 560 // is relative to the FDE offset, into a __eh_frame section 561 // offset 562 if (m_type == EH) { 563 unwind_plan.SetSourceName("eh_frame CFI"); 564 cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset; 565 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 566 } else { 567 unwind_plan.SetSourceName("DWARF CFI"); 568 // In theory the debug_frame info should be valid at all call sites 569 // ("asynchronous unwind info" as it is sometimes called) but in practice 570 // gcc et al all emit call frame info for the prologue and call sites, but 571 // not for the epilogue or all the other locations during the function 572 // reliably. 573 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 574 } 575 unwind_plan.SetSourcedFromCompiler(eLazyBoolYes); 576 577 const CIE *cie = GetCIE(cie_offset); 578 assert(cie != nullptr); 579 580 const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4); 581 582 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 583 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; 584 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; 585 lldb::addr_t range_base = 586 GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr, 587 text_addr, data_addr); 588 lldb::addr_t range_len = GetGNUEHPointer( 589 m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, 590 pc_rel_addr, text_addr, data_addr); 591 AddressRange range(range_base, m_objfile.GetAddressByteSize(), 592 m_objfile.GetSectionList()); 593 range.SetByteSize(range_len); 594 595 addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS; 596 597 if (cie->augmentation[0] == 'z') { 598 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 599 if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) { 600 offset_t saved_offset = offset; 601 lsda_data_file_address = 602 GetGNUEHPointer(m_cfi_data, &offset, cie->lsda_addr_encoding, 603 pc_rel_addr, text_addr, data_addr); 604 if (offset - saved_offset != aug_data_len) { 605 // There is more in the augmentation region than we know how to process; 606 // don't read anything. 607 lsda_data_file_address = LLDB_INVALID_ADDRESS; 608 } 609 offset = saved_offset; 610 } 611 offset += aug_data_len; 612 } 613 Address lsda_data; 614 Address personality_function_ptr; 615 616 if (lsda_data_file_address != LLDB_INVALID_ADDRESS && 617 cie->personality_loc != LLDB_INVALID_ADDRESS) { 618 m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address, 619 lsda_data); 620 m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc, 621 personality_function_ptr); 622 } 623 624 if (lsda_data.IsValid() && personality_function_ptr.IsValid()) { 625 unwind_plan.SetLSDAAddress(lsda_data); 626 unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr); 627 } 628 629 uint32_t code_align = cie->code_align; 630 int32_t data_align = cie->data_align; 631 632 unwind_plan.SetPlanValidAddressRange(range); 633 UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row; 634 *cie_initial_row = cie->initial_row; 635 UnwindPlan::RowSP row(cie_initial_row); 636 637 unwind_plan.SetRegisterKind(GetRegisterKind()); 638 unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num); 639 640 std::vector<UnwindPlan::RowSP> stack; 641 642 UnwindPlan::Row::RegisterLocation reg_location; 643 while (m_cfi_data.ValidOffset(offset) && offset < end_offset) { 644 uint8_t inst = m_cfi_data.GetU8(&offset); 645 uint8_t primary_opcode = inst & 0xC0; 646 uint8_t extended_opcode = inst & 0x3F; 647 648 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align, 649 offset, *row)) { 650 if (primary_opcode) { 651 switch (primary_opcode) { 652 case DW_CFA_advance_loc: // (Row Creation Instruction) 653 { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta 654 // takes a single argument that represents a constant delta. The 655 // required action is to create a new table row with a location 656 // value that is computed by taking the current entry's location 657 // value and adding (delta * code_align). All other 658 // values in the new row are initially identical to the current row. 659 unwind_plan.AppendRow(row); 660 UnwindPlan::Row *newrow = new UnwindPlan::Row; 661 *newrow = *row.get(); 662 row.reset(newrow); 663 row->SlideOffset(extended_opcode * code_align); 664 break; 665 } 666 667 case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are 668 // register 669 // takes a single argument that represents a register number. The 670 // required action is to change the rule for the indicated register 671 // to the rule assigned it by the initial_instructions in the CIE. 672 uint32_t reg_num = extended_opcode; 673 // We only keep enough register locations around to 674 // unwind what is in our thread, and these are organized 675 // by the register index in that state, so we need to convert our 676 // eh_frame register number from the EH frame info, to a register 677 // index 678 679 if (unwind_plan.IsValidRowIndex(0) && 680 unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, 681 reg_location)) 682 row->SetRegisterInfo(reg_num, reg_location); 683 break; 684 } 685 } 686 } else { 687 switch (extended_opcode) { 688 case DW_CFA_set_loc: // 0x1 (Row Creation Instruction) 689 { 690 // DW_CFA_set_loc takes a single argument that represents an address. 691 // The required action is to create a new table row using the 692 // specified address as the location. All other values in the new row 693 // are initially identical to the current row. The new location value 694 // should always be greater than the current one. 695 unwind_plan.AppendRow(row); 696 UnwindPlan::Row *newrow = new UnwindPlan::Row; 697 *newrow = *row.get(); 698 row.reset(newrow); 699 row->SetOffset(m_cfi_data.GetPointer(&offset) - 700 startaddr.GetFileAddress()); 701 break; 702 } 703 704 case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction) 705 { 706 // takes a single uword argument that represents a constant delta. 707 // This instruction is identical to DW_CFA_advance_loc except for the 708 // encoding and size of the delta argument. 709 unwind_plan.AppendRow(row); 710 UnwindPlan::Row *newrow = new UnwindPlan::Row; 711 *newrow = *row.get(); 712 row.reset(newrow); 713 row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align); 714 break; 715 } 716 717 case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction) 718 { 719 // takes a single uword argument that represents a constant delta. 720 // This instruction is identical to DW_CFA_advance_loc except for the 721 // encoding and size of the delta argument. 722 unwind_plan.AppendRow(row); 723 UnwindPlan::Row *newrow = new UnwindPlan::Row; 724 *newrow = *row.get(); 725 row.reset(newrow); 726 row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align); 727 break; 728 } 729 730 case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction) 731 { 732 // takes a single uword argument that represents a constant delta. 733 // This instruction is identical to DW_CFA_advance_loc except for the 734 // encoding and size of the delta argument. 735 unwind_plan.AppendRow(row); 736 UnwindPlan::Row *newrow = new UnwindPlan::Row; 737 *newrow = *row.get(); 738 row.reset(newrow); 739 row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align); 740 break; 741 } 742 743 case DW_CFA_restore_extended: // 0x6 744 { 745 // takes a single unsigned LEB128 argument that represents a register 746 // number. This instruction is identical to DW_CFA_restore except for 747 // the encoding and size of the register argument. 748 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 749 if (unwind_plan.IsValidRowIndex(0) && 750 unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, 751 reg_location)) 752 row->SetRegisterInfo(reg_num, reg_location); 753 break; 754 } 755 756 case DW_CFA_remember_state: // 0xA 757 { 758 // These instructions define a stack of information. Encountering the 759 // DW_CFA_remember_state instruction means to save the rules for every 760 // register on the current row on the stack. Encountering the 761 // DW_CFA_restore_state instruction means to pop the set of rules off 762 // the stack and place them in the current row. (This operation is 763 // useful for compilers that move epilogue code into the body of a 764 // function.) 765 stack.push_back(row); 766 UnwindPlan::Row *newrow = new UnwindPlan::Row; 767 *newrow = *row.get(); 768 row.reset(newrow); 769 break; 770 } 771 772 case DW_CFA_restore_state: // 0xB 773 { 774 // These instructions define a stack of information. Encountering the 775 // DW_CFA_remember_state instruction means to save the rules for every 776 // register on the current row on the stack. Encountering the 777 // DW_CFA_restore_state instruction means to pop the set of rules off 778 // the stack and place them in the current row. (This operation is 779 // useful for compilers that move epilogue code into the body of a 780 // function.) 781 if (stack.empty()) { 782 if (log) 783 log->Printf("DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32 784 ", startaddr: %" PRIx64 785 " encountered DW_CFA_restore_state but state stack " 786 "is empty. Corrupt unwind info?", 787 __FUNCTION__, dwarf_offset, 788 startaddr.GetFileAddress()); 789 break; 790 } 791 lldb::addr_t offset = row->GetOffset(); 792 row = stack.back(); 793 stack.pop_back(); 794 row->SetOffset(offset); 795 break; 796 } 797 798 case DW_CFA_GNU_args_size: // 0x2e 799 { 800 // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128 801 // operand 802 // representing an argument size. This instruction specifies the total 803 // of 804 // the size of the arguments which have been pushed onto the stack. 805 806 // TODO: Figure out how we should handle this. 807 m_cfi_data.GetULEB128(&offset); 808 break; 809 } 810 811 case DW_CFA_val_offset: // 0x14 812 case DW_CFA_val_offset_sf: // 0x15 813 default: 814 break; 815 } 816 } 817 } 818 } 819 unwind_plan.AppendRow(row); 820 821 return true; 822 } 823 824 bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode, 825 uint8_t extended_opcode, 826 int32_t data_align, 827 lldb::offset_t &offset, 828 UnwindPlan::Row &row) { 829 UnwindPlan::Row::RegisterLocation reg_location; 830 831 if (primary_opcode) { 832 switch (primary_opcode) { 833 case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are 834 // register 835 // takes two arguments: an unsigned LEB128 constant representing a 836 // factored offset and a register number. The required action is to 837 // change the rule for the register indicated by the register number 838 // to be an offset(N) rule with a value of 839 // (N = factored offset * data_align). 840 uint8_t reg_num = extended_opcode; 841 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 842 reg_location.SetAtCFAPlusOffset(op_offset); 843 row.SetRegisterInfo(reg_num, reg_location); 844 return true; 845 } 846 } 847 } else { 848 switch (extended_opcode) { 849 case DW_CFA_nop: // 0x0 850 return true; 851 852 case DW_CFA_offset_extended: // 0x5 853 { 854 // takes two unsigned LEB128 arguments representing a register number 855 // and a factored offset. This instruction is identical to DW_CFA_offset 856 // except for the encoding and size of the register argument. 857 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 858 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 859 UnwindPlan::Row::RegisterLocation reg_location; 860 reg_location.SetAtCFAPlusOffset(op_offset); 861 row.SetRegisterInfo(reg_num, reg_location); 862 return true; 863 } 864 865 case DW_CFA_undefined: // 0x7 866 { 867 // takes a single unsigned LEB128 argument that represents a register 868 // number. The required action is to set the rule for the specified 869 // register to undefined. 870 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 871 UnwindPlan::Row::RegisterLocation reg_location; 872 reg_location.SetUndefined(); 873 row.SetRegisterInfo(reg_num, reg_location); 874 return true; 875 } 876 877 case DW_CFA_same_value: // 0x8 878 { 879 // takes a single unsigned LEB128 argument that represents a register 880 // number. The required action is to set the rule for the specified 881 // register to same value. 882 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 883 UnwindPlan::Row::RegisterLocation reg_location; 884 reg_location.SetSame(); 885 row.SetRegisterInfo(reg_num, reg_location); 886 return true; 887 } 888 889 case DW_CFA_register: // 0x9 890 { 891 // takes two unsigned LEB128 arguments representing register numbers. 892 // The required action is to set the rule for the first register to be 893 // the second register. 894 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 895 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 896 UnwindPlan::Row::RegisterLocation reg_location; 897 reg_location.SetInRegister(other_reg_num); 898 row.SetRegisterInfo(reg_num, reg_location); 899 return true; 900 } 901 902 case DW_CFA_def_cfa: // 0xC (CFA Definition Instruction) 903 { 904 // Takes two unsigned LEB128 operands representing a register 905 // number and a (non-factored) offset. The required action 906 // is to define the current CFA rule to use the provided 907 // register and offset. 908 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 909 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 910 row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset); 911 return true; 912 } 913 914 case DW_CFA_def_cfa_register: // 0xD (CFA Definition Instruction) 915 { 916 // takes a single unsigned LEB128 argument representing a register 917 // number. The required action is to define the current CFA rule to 918 // use the provided register (but to keep the old offset). 919 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 920 row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, 921 row.GetCFAValue().GetOffset()); 922 return true; 923 } 924 925 case DW_CFA_def_cfa_offset: // 0xE (CFA Definition Instruction) 926 { 927 // Takes a single unsigned LEB128 operand representing a 928 // (non-factored) offset. The required action is to define 929 // the current CFA rule to use the provided offset (but 930 // to keep the old register). 931 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 932 row.GetCFAValue().SetIsRegisterPlusOffset( 933 row.GetCFAValue().GetRegisterNumber(), op_offset); 934 return true; 935 } 936 937 case DW_CFA_def_cfa_expression: // 0xF (CFA Definition Instruction) 938 { 939 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset); 940 const uint8_t *block_data = 941 static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len)); 942 row.GetCFAValue().SetIsDWARFExpression(block_data, block_len); 943 return true; 944 } 945 946 case DW_CFA_expression: // 0x10 947 { 948 // Takes two operands: an unsigned LEB128 value representing 949 // a register number, and a DW_FORM_block value representing a DWARF 950 // expression. The required action is to change the rule for the 951 // register indicated by the register number to be an expression(E) 952 // rule where E is the DWARF expression. That is, the DWARF 953 // expression computes the address. The value of the CFA is 954 // pushed on the DWARF evaluation stack prior to execution of 955 // the DWARF expression. 956 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 957 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 958 const uint8_t *block_data = 959 static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len)); 960 UnwindPlan::Row::RegisterLocation reg_location; 961 reg_location.SetAtDWARFExpression(block_data, block_len); 962 row.SetRegisterInfo(reg_num, reg_location); 963 return true; 964 } 965 966 case DW_CFA_offset_extended_sf: // 0x11 967 { 968 // takes two operands: an unsigned LEB128 value representing a 969 // register number and a signed LEB128 factored offset. This 970 // instruction is identical to DW_CFA_offset_extended except 971 // that the second operand is signed and factored. 972 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 973 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 974 UnwindPlan::Row::RegisterLocation reg_location; 975 reg_location.SetAtCFAPlusOffset(op_offset); 976 row.SetRegisterInfo(reg_num, reg_location); 977 return true; 978 } 979 980 case DW_CFA_def_cfa_sf: // 0x12 (CFA Definition Instruction) 981 { 982 // Takes two operands: an unsigned LEB128 value representing 983 // a register number and a signed LEB128 factored offset. 984 // This instruction is identical to DW_CFA_def_cfa except 985 // that the second operand is signed and factored. 986 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 987 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 988 row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset); 989 return true; 990 } 991 992 case DW_CFA_def_cfa_offset_sf: // 0x13 (CFA Definition Instruction) 993 { 994 // takes a signed LEB128 operand representing a factored 995 // offset. This instruction is identical to DW_CFA_def_cfa_offset 996 // except that the operand is signed and factored. 997 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 998 uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber(); 999 row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset); 1000 return true; 1001 } 1002 1003 case DW_CFA_val_expression: // 0x16 1004 { 1005 // takes two operands: an unsigned LEB128 value representing a register 1006 // number, and a DW_FORM_block value representing a DWARF expression. 1007 // The required action is to change the rule for the register indicated 1008 // by the register number to be a val_expression(E) rule where E is the 1009 // DWARF expression. That is, the DWARF expression computes the value of 1010 // the given register. The value of the CFA is pushed on the DWARF 1011 // evaluation stack prior to execution of the DWARF expression. 1012 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 1013 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 1014 const uint8_t *block_data = 1015 (const uint8_t *)m_cfi_data.GetData(&offset, block_len); 1016 //#if defined(__i386__) || defined(__x86_64__) 1017 // // The EH frame info for EIP and RIP contains code that 1018 // looks for traps to 1019 // // be a specific type and increments the PC. 1020 // // For i386: 1021 // // DW_CFA_val_expression where: 1022 // // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, 1023 // DW_OP_plus_uconst(0x34), 1024 // // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), 1025 // DW_OP_deref, 1026 // // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, 1027 // DW_OP_lit4, DW_OP_ne, 1028 // // DW_OP_and, DW_OP_plus 1029 // // This basically does a: 1030 // // eip = ucontenxt.mcontext32->gpr.eip; 1031 // // if (ucontenxt.mcontext32->exc.trapno != 3 && 1032 // ucontenxt.mcontext32->exc.trapno != 4) 1033 // // eip++; 1034 // // 1035 // // For x86_64: 1036 // // DW_CFA_val_expression where: 1037 // // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, 1038 // DW_OP_plus_uconst(0x90), DW_OP_deref, 1039 // // DW_OP_swap, DW_OP_plus_uconst(0), 1040 // DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3, 1041 // // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, 1042 // DW_OP_and, DW_OP_plus 1043 // // This basically does a: 1044 // // rip = ucontenxt.mcontext64->gpr.rip; 1045 // // if (ucontenxt.mcontext64->exc.trapno != 3 && 1046 // ucontenxt.mcontext64->exc.trapno != 4) 1047 // // rip++; 1048 // // The trap comparisons and increments are not needed as 1049 // it hoses up the unwound PC which 1050 // // is expected to point at least past the instruction that 1051 // causes the fault/trap. So we 1052 // // take it out by trimming the expression right at the 1053 // first "DW_OP_swap" opcodes 1054 // if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) 1055 // == reg_num) 1056 // { 1057 // if (thread->Is64Bit()) 1058 // { 1059 // if (block_len > 9 && block_data[8] == DW_OP_swap 1060 // && block_data[9] == DW_OP_plus_uconst) 1061 // block_len = 8; 1062 // } 1063 // else 1064 // { 1065 // if (block_len > 8 && block_data[7] == DW_OP_swap 1066 // && block_data[8] == DW_OP_plus_uconst) 1067 // block_len = 7; 1068 // } 1069 // } 1070 //#endif 1071 reg_location.SetIsDWARFExpression(block_data, block_len); 1072 row.SetRegisterInfo(reg_num, reg_location); 1073 return true; 1074 } 1075 } 1076 } 1077 return false; 1078 } 1079 1080 void DWARFCallFrameInfo::ForEachFDEEntries( 1081 const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) { 1082 GetFDEIndex(); 1083 1084 for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) { 1085 const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i); 1086 if (!callback(entry.base, entry.size, entry.data)) 1087 break; 1088 } 1089 } 1090