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