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