1 //===-- DWARFUnit.cpp -----------------------------------------------------===// 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 "DWARFUnit.h" 10 11 #include "lldb/Core/Module.h" 12 #include "lldb/Symbol/ObjectFile.h" 13 #include "lldb/Utility/LLDBAssert.h" 14 #include "lldb/Utility/StreamString.h" 15 #include "lldb/Utility/Timer.h" 16 #include "llvm/Object/Error.h" 17 18 #include "DWARFCompileUnit.h" 19 #include "DWARFDebugAranges.h" 20 #include "DWARFDebugInfo.h" 21 #include "DWARFTypeUnit.h" 22 #include "LogChannelDWARF.h" 23 #include "SymbolFileDWARFDwo.h" 24 25 using namespace lldb; 26 using namespace lldb_private; 27 using namespace std; 28 29 extern int g_verbose; 30 31 DWARFUnit::DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid, 32 const DWARFUnitHeader &header, 33 const DWARFAbbreviationDeclarationSet &abbrevs, 34 DIERef::Section section, bool is_dwo) 35 : UserID(uid), m_dwarf(dwarf), m_header(header), m_abbrevs(&abbrevs), 36 m_cancel_scopes(false), m_section(section), m_is_dwo(is_dwo), 37 m_has_parsed_non_skeleton_unit(false), m_dwo_id(header.GetDWOId()) {} 38 39 DWARFUnit::~DWARFUnit() = default; 40 41 // Parses first DIE of a compile unit, excluding DWO. 42 void DWARFUnit::ExtractUnitDIENoDwoIfNeeded() { 43 { 44 llvm::sys::ScopedReader lock(m_first_die_mutex); 45 if (m_first_die) 46 return; // Already parsed 47 } 48 llvm::sys::ScopedWriter lock(m_first_die_mutex); 49 if (m_first_die) 50 return; // Already parsed 51 52 ElapsedTime elapsed(m_dwarf.GetDebugInfoParseTimeRef()); 53 LLDB_SCOPED_TIMERF("%8.8x: DWARFUnit::ExtractUnitDIENoDwoIfNeeded()", 54 GetOffset()); 55 56 // Set the offset to that of the first DIE and calculate the start of the 57 // next compilation unit header. 58 lldb::offset_t offset = GetFirstDIEOffset(); 59 60 // We are in our compile unit, parse starting at the offset we were told to 61 // parse 62 const DWARFDataExtractor &data = GetData(); 63 if (offset < GetNextUnitOffset() && 64 m_first_die.Extract(data, this, &offset)) { 65 AddUnitDIE(m_first_die); 66 return; 67 } 68 } 69 70 // Parses first DIE of a compile unit including DWO. 71 void DWARFUnit::ExtractUnitDIEIfNeeded() { 72 ExtractUnitDIENoDwoIfNeeded(); 73 74 if (m_has_parsed_non_skeleton_unit) 75 return; 76 77 m_has_parsed_non_skeleton_unit = true; 78 79 std::shared_ptr<SymbolFileDWARFDwo> dwo_symbol_file = 80 m_dwarf.GetDwoSymbolFileForCompileUnit(*this, m_first_die); 81 if (!dwo_symbol_file) 82 return; 83 84 DWARFUnit *dwo_cu = dwo_symbol_file->GetDWOCompileUnitForHash(m_dwo_id); 85 86 if (!dwo_cu) 87 return; // Can't fetch the compile unit from the dwo file. 88 dwo_cu->SetUserData(this); 89 90 DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly(); 91 if (!dwo_cu_die.IsValid()) 92 return; // Can't fetch the compile unit DIE from the dwo file. 93 94 // Here for DWO CU we want to use the address base set in the skeleton unit 95 // (DW_AT_addr_base) if it is available and use the DW_AT_GNU_addr_base 96 // otherwise. We do that because pre-DWARF v5 could use the DW_AT_GNU_* 97 // attributes which were applicable to the DWO units. The corresponding 98 // DW_AT_* attributes standardized in DWARF v5 are also applicable to the 99 // main unit in contrast. 100 if (m_addr_base) 101 dwo_cu->SetAddrBase(*m_addr_base); 102 else if (m_gnu_addr_base) 103 dwo_cu->SetAddrBase(*m_gnu_addr_base); 104 105 if (GetVersion() <= 4 && m_gnu_ranges_base) 106 dwo_cu->SetRangesBase(*m_gnu_ranges_base); 107 else if (dwo_symbol_file->GetDWARFContext() 108 .getOrLoadRngListsData() 109 .GetByteSize() > 0) 110 dwo_cu->SetRangesBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32)); 111 112 if (GetVersion() >= 5 && 113 dwo_symbol_file->GetDWARFContext().getOrLoadLocListsData().GetByteSize() > 114 0) 115 dwo_cu->SetLoclistsBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32)); 116 117 dwo_cu->SetBaseAddress(GetBaseAddress()); 118 119 m_dwo = std::shared_ptr<DWARFUnit>(std::move(dwo_symbol_file), dwo_cu); 120 } 121 122 // Parses a compile unit and indexes its DIEs if it hasn't already been done. 123 // It will leave this compile unit extracted forever. 124 void DWARFUnit::ExtractDIEsIfNeeded() { 125 m_cancel_scopes = true; 126 127 { 128 llvm::sys::ScopedReader lock(m_die_array_mutex); 129 if (!m_die_array.empty()) 130 return; // Already parsed 131 } 132 llvm::sys::ScopedWriter lock(m_die_array_mutex); 133 if (!m_die_array.empty()) 134 return; // Already parsed 135 136 ExtractDIEsRWLocked(); 137 } 138 139 // Parses a compile unit and indexes its DIEs if it hasn't already been done. 140 // It will clear this compile unit after returned instance gets out of scope, 141 // no other ScopedExtractDIEs instance is running for this compile unit 142 // and no ExtractDIEsIfNeeded() has been executed during this ScopedExtractDIEs 143 // lifetime. 144 DWARFUnit::ScopedExtractDIEs DWARFUnit::ExtractDIEsScoped() { 145 ScopedExtractDIEs scoped(*this); 146 147 { 148 llvm::sys::ScopedReader lock(m_die_array_mutex); 149 if (!m_die_array.empty()) 150 return scoped; // Already parsed 151 } 152 llvm::sys::ScopedWriter lock(m_die_array_mutex); 153 if (!m_die_array.empty()) 154 return scoped; // Already parsed 155 156 // Otherwise m_die_array would be already populated. 157 lldbassert(!m_cancel_scopes); 158 159 ExtractDIEsRWLocked(); 160 scoped.m_clear_dies = true; 161 return scoped; 162 } 163 164 DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(DWARFUnit &cu) : m_cu(&cu) { 165 m_cu->m_die_array_scoped_mutex.lock_shared(); 166 } 167 168 DWARFUnit::ScopedExtractDIEs::~ScopedExtractDIEs() { 169 if (!m_cu) 170 return; 171 m_cu->m_die_array_scoped_mutex.unlock_shared(); 172 if (!m_clear_dies || m_cu->m_cancel_scopes) 173 return; 174 // Be sure no other ScopedExtractDIEs is running anymore. 175 llvm::sys::ScopedWriter lock_scoped(m_cu->m_die_array_scoped_mutex); 176 llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex); 177 if (m_cu->m_cancel_scopes) 178 return; 179 m_cu->ClearDIEsRWLocked(); 180 } 181 182 DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(ScopedExtractDIEs &&rhs) 183 : m_cu(rhs.m_cu), m_clear_dies(rhs.m_clear_dies) { 184 rhs.m_cu = nullptr; 185 } 186 187 DWARFUnit::ScopedExtractDIEs &DWARFUnit::ScopedExtractDIEs::operator=( 188 DWARFUnit::ScopedExtractDIEs &&rhs) { 189 m_cu = rhs.m_cu; 190 rhs.m_cu = nullptr; 191 m_clear_dies = rhs.m_clear_dies; 192 return *this; 193 } 194 195 // Parses a compile unit and indexes its DIEs, m_die_array_mutex must be 196 // held R/W and m_die_array must be empty. 197 void DWARFUnit::ExtractDIEsRWLocked() { 198 llvm::sys::ScopedWriter first_die_lock(m_first_die_mutex); 199 200 ElapsedTime elapsed(m_dwarf.GetDebugInfoParseTimeRef()); 201 LLDB_SCOPED_TIMERF("%8.8x: DWARFUnit::ExtractDIEsIfNeeded()", GetOffset()); 202 203 // Set the offset to that of the first DIE and calculate the start of the 204 // next compilation unit header. 205 lldb::offset_t offset = GetFirstDIEOffset(); 206 lldb::offset_t next_cu_offset = GetNextUnitOffset(); 207 208 DWARFDebugInfoEntry die; 209 210 uint32_t depth = 0; 211 // We are in our compile unit, parse starting at the offset we were told to 212 // parse 213 const DWARFDataExtractor &data = GetData(); 214 std::vector<uint32_t> die_index_stack; 215 die_index_stack.reserve(32); 216 die_index_stack.push_back(0); 217 bool prev_die_had_children = false; 218 while (offset < next_cu_offset && die.Extract(data, this, &offset)) { 219 const bool null_die = die.IsNULL(); 220 if (depth == 0) { 221 assert(m_die_array.empty() && "Compile unit DIE already added"); 222 223 // The average bytes per DIE entry has been seen to be around 14-20 so 224 // lets pre-reserve half of that since we are now stripping the NULL 225 // tags. 226 227 // Only reserve the memory if we are adding children of the main 228 // compile unit DIE. The compile unit DIE is always the first entry, so 229 // if our size is 1, then we are adding the first compile unit child 230 // DIE and should reserve the memory. 231 m_die_array.reserve(GetDebugInfoSize() / 24); 232 m_die_array.push_back(die); 233 234 if (!m_first_die) 235 AddUnitDIE(m_die_array.front()); 236 237 // With -fsplit-dwarf-inlining, clang will emit non-empty skeleton compile 238 // units. We are not able to access these DIE *and* the dwo file 239 // simultaneously. We also don't need to do that as the dwo file will 240 // contain a superset of information. So, we don't even attempt to parse 241 // any remaining DIEs. 242 if (m_dwo) { 243 m_die_array.front().SetHasChildren(false); 244 break; 245 } 246 247 } else { 248 if (null_die) { 249 if (prev_die_had_children) { 250 // This will only happen if a DIE says is has children but all it 251 // contains is a NULL tag. Since we are removing the NULL DIEs from 252 // the list (saves up to 25% in C++ code), we need a way to let the 253 // DIE know that it actually doesn't have children. 254 if (!m_die_array.empty()) 255 m_die_array.back().SetHasChildren(false); 256 } 257 } else { 258 die.SetParentIndex(m_die_array.size() - die_index_stack[depth - 1]); 259 260 if (die_index_stack.back()) 261 m_die_array[die_index_stack.back()].SetSiblingIndex( 262 m_die_array.size() - die_index_stack.back()); 263 264 // Only push the DIE if it isn't a NULL DIE 265 m_die_array.push_back(die); 266 } 267 } 268 269 if (null_die) { 270 // NULL DIE. 271 if (!die_index_stack.empty()) 272 die_index_stack.pop_back(); 273 274 if (depth > 0) 275 --depth; 276 prev_die_had_children = false; 277 } else { 278 die_index_stack.back() = m_die_array.size() - 1; 279 // Normal DIE 280 const bool die_has_children = die.HasChildren(); 281 if (die_has_children) { 282 die_index_stack.push_back(0); 283 ++depth; 284 } 285 prev_die_had_children = die_has_children; 286 } 287 288 if (depth == 0) 289 break; // We are done with this compile unit! 290 } 291 292 if (!m_die_array.empty()) { 293 // The last die cannot have children (if it did, it wouldn't be the last one). 294 // This only makes a difference for malformed dwarf that does not have a 295 // terminating null die. 296 m_die_array.back().SetHasChildren(false); 297 298 if (m_first_die) { 299 // Only needed for the assertion. 300 m_first_die.SetHasChildren(m_die_array.front().HasChildren()); 301 lldbassert(m_first_die == m_die_array.front()); 302 } 303 m_first_die = m_die_array.front(); 304 } 305 306 m_die_array.shrink_to_fit(); 307 308 if (m_dwo) 309 m_dwo->ExtractDIEsIfNeeded(); 310 } 311 312 // This is used when a split dwarf is enabled. 313 // A skeleton compilation unit may contain the DW_AT_str_offsets_base attribute 314 // that points to the first string offset of the CU contribution to the 315 // .debug_str_offsets. At the same time, the corresponding split debug unit also 316 // may use DW_FORM_strx* forms pointing to its own .debug_str_offsets.dwo and 317 // for that case, we should find the offset (skip the section header). 318 void DWARFUnit::SetDwoStrOffsetsBase() { 319 lldb::offset_t baseOffset = 0; 320 321 if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) { 322 if (const auto *contribution = 323 entry->getContribution(llvm::DW_SECT_STR_OFFSETS)) 324 baseOffset = contribution->Offset; 325 else 326 return; 327 } 328 329 if (GetVersion() >= 5) { 330 const DWARFDataExtractor &strOffsets = 331 GetSymbolFileDWARF().GetDWARFContext().getOrLoadStrOffsetsData(); 332 uint64_t length = strOffsets.GetU32(&baseOffset); 333 if (length == 0xffffffff) 334 length = strOffsets.GetU64(&baseOffset); 335 336 // Check version. 337 if (strOffsets.GetU16(&baseOffset) < 5) 338 return; 339 340 // Skip padding. 341 baseOffset += 2; 342 } 343 344 SetStrOffsetsBase(baseOffset); 345 } 346 347 uint64_t DWARFUnit::GetDWOId() { 348 ExtractUnitDIENoDwoIfNeeded(); 349 return m_dwo_id; 350 } 351 352 // m_die_array_mutex must be already held as read/write. 353 void DWARFUnit::AddUnitDIE(const DWARFDebugInfoEntry &cu_die) { 354 DWARFAttributes attributes; 355 size_t num_attributes = cu_die.GetAttributes(this, attributes); 356 357 // Extract DW_AT_addr_base first, as other attributes may need it. 358 for (size_t i = 0; i < num_attributes; ++i) { 359 if (attributes.AttributeAtIndex(i) != DW_AT_addr_base) 360 continue; 361 DWARFFormValue form_value; 362 if (attributes.ExtractFormValueAtIndex(i, form_value)) { 363 SetAddrBase(form_value.Unsigned()); 364 break; 365 } 366 } 367 368 for (size_t i = 0; i < num_attributes; ++i) { 369 dw_attr_t attr = attributes.AttributeAtIndex(i); 370 DWARFFormValue form_value; 371 if (!attributes.ExtractFormValueAtIndex(i, form_value)) 372 continue; 373 switch (attr) { 374 case DW_AT_loclists_base: 375 SetLoclistsBase(form_value.Unsigned()); 376 break; 377 case DW_AT_rnglists_base: 378 SetRangesBase(form_value.Unsigned()); 379 break; 380 case DW_AT_str_offsets_base: 381 SetStrOffsetsBase(form_value.Unsigned()); 382 break; 383 case DW_AT_low_pc: 384 SetBaseAddress(form_value.Address()); 385 break; 386 case DW_AT_entry_pc: 387 // If the value was already set by DW_AT_low_pc, don't update it. 388 if (m_base_addr == LLDB_INVALID_ADDRESS) 389 SetBaseAddress(form_value.Address()); 390 break; 391 case DW_AT_stmt_list: 392 m_line_table_offset = form_value.Unsigned(); 393 break; 394 case DW_AT_GNU_addr_base: 395 m_gnu_addr_base = form_value.Unsigned(); 396 break; 397 case DW_AT_GNU_ranges_base: 398 m_gnu_ranges_base = form_value.Unsigned(); 399 break; 400 case DW_AT_GNU_dwo_id: 401 m_dwo_id = form_value.Unsigned(); 402 break; 403 } 404 } 405 406 if (m_is_dwo) { 407 m_has_parsed_non_skeleton_unit = true; 408 SetDwoStrOffsetsBase(); 409 return; 410 } 411 } 412 413 size_t DWARFUnit::GetDebugInfoSize() const { 414 return GetLengthByteSize() + GetLength() - GetHeaderByteSize(); 415 } 416 417 const DWARFAbbreviationDeclarationSet *DWARFUnit::GetAbbreviations() const { 418 return m_abbrevs; 419 } 420 421 dw_offset_t DWARFUnit::GetAbbrevOffset() const { 422 return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET; 423 } 424 425 dw_offset_t DWARFUnit::GetLineTableOffset() { 426 ExtractUnitDIENoDwoIfNeeded(); 427 return m_line_table_offset; 428 } 429 430 void DWARFUnit::SetAddrBase(dw_addr_t addr_base) { m_addr_base = addr_base; } 431 432 // Parse the rangelist table header, including the optional array of offsets 433 // following it (DWARF v5 and later). 434 template <typename ListTableType> 435 static llvm::Expected<ListTableType> 436 ParseListTableHeader(const llvm::DWARFDataExtractor &data, uint64_t offset, 437 DwarfFormat format) { 438 // We are expected to be called with Offset 0 or pointing just past the table 439 // header. Correct Offset in the latter case so that it points to the start 440 // of the header. 441 if (offset == 0) { 442 // This means DW_AT_rnglists_base is missing and therefore DW_FORM_rnglistx 443 // cannot be handled. Returning a default-constructed ListTableType allows 444 // DW_FORM_sec_offset to be supported. 445 return ListTableType(); 446 } 447 448 uint64_t HeaderSize = llvm::DWARFListTableHeader::getHeaderSize(format); 449 if (offset < HeaderSize) 450 return llvm::createStringError(errc::invalid_argument, 451 "did not detect a valid" 452 " list table with base = 0x%" PRIx64 "\n", 453 offset); 454 offset -= HeaderSize; 455 ListTableType Table; 456 if (llvm::Error E = Table.extractHeaderAndOffsets(data, &offset)) 457 return std::move(E); 458 return Table; 459 } 460 461 void DWARFUnit::SetLoclistsBase(dw_addr_t loclists_base) { 462 uint64_t offset = 0; 463 if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) { 464 const auto *contribution = entry->getContribution(llvm::DW_SECT_LOCLISTS); 465 if (!contribution) { 466 GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError( 467 "Failed to find location list contribution for CU with DWO Id " 468 "0x%" PRIx64, 469 this->GetDWOId()); 470 return; 471 } 472 offset += contribution->Offset; 473 } 474 m_loclists_base = loclists_base; 475 476 uint64_t header_size = llvm::DWARFListTableHeader::getHeaderSize(DWARF32); 477 if (loclists_base < header_size) 478 return; 479 480 m_loclist_table_header.emplace(".debug_loclists", "locations"); 481 offset += loclists_base - header_size; 482 if (llvm::Error E = m_loclist_table_header->extract( 483 m_dwarf.GetDWARFContext().getOrLoadLocListsData().GetAsLLVM(), 484 &offset)) { 485 GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError( 486 "Failed to extract location list table at offset 0x%" PRIx64 487 " (location list base: 0x%" PRIx64 "): %s", 488 offset, loclists_base, toString(std::move(E)).c_str()); 489 } 490 } 491 492 std::unique_ptr<llvm::DWARFLocationTable> 493 DWARFUnit::GetLocationTable(const DataExtractor &data) const { 494 llvm::DWARFDataExtractor llvm_data( 495 data.GetData(), data.GetByteOrder() == lldb::eByteOrderLittle, 496 data.GetAddressByteSize()); 497 498 if (m_is_dwo || GetVersion() >= 5) 499 return std::make_unique<llvm::DWARFDebugLoclists>(llvm_data, GetVersion()); 500 return std::make_unique<llvm::DWARFDebugLoc>(llvm_data); 501 } 502 503 DWARFDataExtractor DWARFUnit::GetLocationData() const { 504 DWARFContext &Ctx = GetSymbolFileDWARF().GetDWARFContext(); 505 const DWARFDataExtractor &data = 506 GetVersion() >= 5 ? Ctx.getOrLoadLocListsData() : Ctx.getOrLoadLocData(); 507 if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) { 508 if (const auto *contribution = entry->getContribution( 509 GetVersion() >= 5 ? llvm::DW_SECT_LOCLISTS : llvm::DW_SECT_EXT_LOC)) 510 return DWARFDataExtractor(data, contribution->Offset, 511 contribution->Length); 512 return DWARFDataExtractor(); 513 } 514 return data; 515 } 516 517 DWARFDataExtractor DWARFUnit::GetRnglistData() const { 518 DWARFContext &Ctx = GetSymbolFileDWARF().GetDWARFContext(); 519 const DWARFDataExtractor &data = Ctx.getOrLoadRngListsData(); 520 if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) { 521 if (const auto *contribution = 522 entry->getContribution(llvm::DW_SECT_RNGLISTS)) 523 return DWARFDataExtractor(data, contribution->Offset, 524 contribution->Length); 525 GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError( 526 "Failed to find range list contribution for CU with signature " 527 "0x%" PRIx64, 528 entry->getSignature()); 529 530 return DWARFDataExtractor(); 531 } 532 return data; 533 } 534 535 void DWARFUnit::SetRangesBase(dw_addr_t ranges_base) { 536 lldbassert(!m_rnglist_table_done); 537 538 m_ranges_base = ranges_base; 539 } 540 541 const llvm::Optional<llvm::DWARFDebugRnglistTable> & 542 DWARFUnit::GetRnglistTable() { 543 if (GetVersion() >= 5 && !m_rnglist_table_done) { 544 m_rnglist_table_done = true; 545 if (auto table_or_error = 546 ParseListTableHeader<llvm::DWARFDebugRnglistTable>( 547 GetRnglistData().GetAsLLVM(), m_ranges_base, DWARF32)) 548 m_rnglist_table = std::move(table_or_error.get()); 549 else 550 GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError( 551 "Failed to extract range list table at offset 0x%" PRIx64 ": %s", 552 m_ranges_base, toString(table_or_error.takeError()).c_str()); 553 } 554 return m_rnglist_table; 555 } 556 557 // This function is called only for DW_FORM_rnglistx. 558 llvm::Expected<uint64_t> DWARFUnit::GetRnglistOffset(uint32_t Index) { 559 if (!GetRnglistTable()) 560 return llvm::createStringError(errc::invalid_argument, 561 "missing or invalid range list table"); 562 if (!m_ranges_base) 563 return llvm::createStringError(errc::invalid_argument, 564 "DW_FORM_rnglistx cannot be used without " 565 "DW_AT_rnglists_base for CU at 0x%8.8x", 566 GetOffset()); 567 if (llvm::Optional<uint64_t> off = GetRnglistTable()->getOffsetEntry( 568 GetRnglistData().GetAsLLVM(), Index)) 569 return *off + m_ranges_base; 570 return llvm::createStringError( 571 errc::invalid_argument, 572 "invalid range list table index %u; OffsetEntryCount is %u, " 573 "DW_AT_rnglists_base is %" PRIu64, 574 Index, GetRnglistTable()->getOffsetEntryCount(), m_ranges_base); 575 } 576 577 void DWARFUnit::SetStrOffsetsBase(dw_offset_t str_offsets_base) { 578 m_str_offsets_base = str_offsets_base; 579 } 580 581 // It may be called only with m_die_array_mutex held R/W. 582 void DWARFUnit::ClearDIEsRWLocked() { 583 m_die_array.clear(); 584 m_die_array.shrink_to_fit(); 585 586 if (m_dwo) 587 m_dwo->ClearDIEsRWLocked(); 588 } 589 590 lldb::ByteOrder DWARFUnit::GetByteOrder() const { 591 return m_dwarf.GetObjectFile()->GetByteOrder(); 592 } 593 594 void DWARFUnit::SetBaseAddress(dw_addr_t base_addr) { m_base_addr = base_addr; } 595 596 // Compare function DWARFDebugAranges::Range structures 597 static bool CompareDIEOffset(const DWARFDebugInfoEntry &die, 598 const dw_offset_t die_offset) { 599 return die.GetOffset() < die_offset; 600 } 601 602 // GetDIE() 603 // 604 // Get the DIE (Debug Information Entry) with the specified offset by first 605 // checking if the DIE is contained within this compile unit and grabbing the 606 // DIE from this compile unit. Otherwise we grab the DIE from the DWARF file. 607 DWARFDIE 608 DWARFUnit::GetDIE(dw_offset_t die_offset) { 609 if (die_offset == DW_INVALID_OFFSET) 610 return DWARFDIE(); // Not found 611 612 if (!ContainsDIEOffset(die_offset)) { 613 GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError( 614 "GetDIE for DIE 0x%" PRIx32 " is outside of its CU 0x%" PRIx32, 615 die_offset, GetOffset()); 616 return DWARFDIE(); // Not found 617 } 618 619 ExtractDIEsIfNeeded(); 620 DWARFDebugInfoEntry::const_iterator end = m_die_array.cend(); 621 DWARFDebugInfoEntry::const_iterator pos = 622 lower_bound(m_die_array.cbegin(), end, die_offset, CompareDIEOffset); 623 624 if (pos != end && die_offset == (*pos).GetOffset()) 625 return DWARFDIE(this, &(*pos)); 626 return DWARFDIE(); // Not found 627 } 628 629 DWARFUnit &DWARFUnit::GetNonSkeletonUnit() { 630 ExtractUnitDIEIfNeeded(); 631 if (m_dwo) 632 return *m_dwo; 633 return *this; 634 } 635 636 uint8_t DWARFUnit::GetAddressByteSize(const DWARFUnit *cu) { 637 if (cu) 638 return cu->GetAddressByteSize(); 639 return DWARFUnit::GetDefaultAddressSize(); 640 } 641 642 uint8_t DWARFUnit::GetDefaultAddressSize() { return 4; } 643 644 void *DWARFUnit::GetUserData() const { return m_user_data; } 645 646 void DWARFUnit::SetUserData(void *d) { m_user_data = d; } 647 648 bool DWARFUnit::Supports_DW_AT_APPLE_objc_complete_type() { 649 return GetProducer() != eProducerLLVMGCC; 650 } 651 652 bool DWARFUnit::DW_AT_decl_file_attributes_are_invalid() { 653 // llvm-gcc makes completely invalid decl file attributes and won't ever be 654 // fixed, so we need to know to ignore these. 655 return GetProducer() == eProducerLLVMGCC; 656 } 657 658 bool DWARFUnit::Supports_unnamed_objc_bitfields() { 659 if (GetProducer() == eProducerClang) 660 return GetProducerVersion() >= llvm::VersionTuple(425, 0, 13); 661 // Assume all other compilers didn't have incorrect ObjC bitfield info. 662 return true; 663 } 664 665 void DWARFUnit::ParseProducerInfo() { 666 m_producer = eProducerOther; 667 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); 668 if (!die) 669 return; 670 671 llvm::StringRef producer( 672 die->GetAttributeValueAsString(this, DW_AT_producer, nullptr)); 673 if (producer.empty()) 674 return; 675 676 static const RegularExpression g_swiftlang_version_regex( 677 llvm::StringRef(R"(swiftlang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))")); 678 static const RegularExpression g_clang_version_regex( 679 llvm::StringRef(R"(clang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))")); 680 static const RegularExpression g_llvm_gcc_regex( 681 llvm::StringRef(R"(4\.[012]\.[01] )" 682 R"(\(Based on Apple Inc\. build [0-9]+\) )" 683 R"(\(LLVM build [\.0-9]+\)$)")); 684 685 llvm::SmallVector<llvm::StringRef, 3> matches; 686 if (g_swiftlang_version_regex.Execute(producer, &matches)) { 687 m_producer_version.tryParse(matches[1]); 688 m_producer = eProducerSwift; 689 } else if (producer.contains("clang")) { 690 if (g_clang_version_regex.Execute(producer, &matches)) 691 m_producer_version.tryParse(matches[1]); 692 m_producer = eProducerClang; 693 } else if (producer.contains("GNU")) { 694 m_producer = eProducerGCC; 695 } else if (g_llvm_gcc_regex.Execute(producer)) { 696 m_producer = eProducerLLVMGCC; 697 } 698 } 699 700 DWARFProducer DWARFUnit::GetProducer() { 701 if (m_producer == eProducerInvalid) 702 ParseProducerInfo(); 703 return m_producer; 704 } 705 706 llvm::VersionTuple DWARFUnit::GetProducerVersion() { 707 if (m_producer_version.empty()) 708 ParseProducerInfo(); 709 return m_producer_version; 710 } 711 712 uint64_t DWARFUnit::GetDWARFLanguageType() { 713 if (m_language_type) 714 return *m_language_type; 715 716 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); 717 if (!die) 718 m_language_type = 0; 719 else 720 m_language_type = die->GetAttributeValueAsUnsigned(this, DW_AT_language, 0); 721 return *m_language_type; 722 } 723 724 bool DWARFUnit::GetIsOptimized() { 725 if (m_is_optimized == eLazyBoolCalculate) { 726 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); 727 if (die) { 728 m_is_optimized = eLazyBoolNo; 729 if (die->GetAttributeValueAsUnsigned(this, DW_AT_APPLE_optimized, 0) == 730 1) { 731 m_is_optimized = eLazyBoolYes; 732 } 733 } 734 } 735 return m_is_optimized == eLazyBoolYes; 736 } 737 738 FileSpec::Style DWARFUnit::GetPathStyle() { 739 if (!m_comp_dir) 740 ComputeCompDirAndGuessPathStyle(); 741 return m_comp_dir->GetPathStyle(); 742 } 743 744 const FileSpec &DWARFUnit::GetCompilationDirectory() { 745 if (!m_comp_dir) 746 ComputeCompDirAndGuessPathStyle(); 747 return *m_comp_dir; 748 } 749 750 const FileSpec &DWARFUnit::GetAbsolutePath() { 751 if (!m_file_spec) 752 ComputeAbsolutePath(); 753 return *m_file_spec; 754 } 755 756 FileSpec DWARFUnit::GetFile(size_t file_idx) { 757 return m_dwarf.GetFile(*this, file_idx); 758 } 759 760 // DWARF2/3 suggests the form hostname:pathname for compilation directory. 761 // Remove the host part if present. 762 static llvm::StringRef 763 removeHostnameFromPathname(llvm::StringRef path_from_dwarf) { 764 if (!path_from_dwarf.contains(':')) 765 return path_from_dwarf; 766 llvm::StringRef host, path; 767 std::tie(host, path) = path_from_dwarf.split(':'); 768 769 if (host.contains('/')) 770 return path_from_dwarf; 771 772 // check whether we have a windows path, and so the first character is a 773 // drive-letter not a hostname. 774 if (host.size() == 1 && llvm::isAlpha(host[0]) && path.startswith("\\")) 775 return path_from_dwarf; 776 777 return path; 778 } 779 780 void DWARFUnit::ComputeCompDirAndGuessPathStyle() { 781 m_comp_dir = FileSpec(); 782 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); 783 if (!die) 784 return; 785 786 llvm::StringRef comp_dir = removeHostnameFromPathname( 787 die->GetAttributeValueAsString(this, DW_AT_comp_dir, nullptr)); 788 if (!comp_dir.empty()) { 789 FileSpec::Style comp_dir_style = 790 FileSpec::GuessPathStyle(comp_dir).getValueOr(FileSpec::Style::native); 791 m_comp_dir = FileSpec(comp_dir, comp_dir_style); 792 } else { 793 // Try to detect the style based on the DW_AT_name attribute, but just store 794 // the detected style in the m_comp_dir field. 795 const char *name = 796 die->GetAttributeValueAsString(this, DW_AT_name, nullptr); 797 m_comp_dir = FileSpec( 798 "", FileSpec::GuessPathStyle(name).getValueOr(FileSpec::Style::native)); 799 } 800 } 801 802 void DWARFUnit::ComputeAbsolutePath() { 803 m_file_spec = FileSpec(); 804 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); 805 if (!die) 806 return; 807 808 m_file_spec = 809 FileSpec(die->GetAttributeValueAsString(this, DW_AT_name, nullptr), 810 GetPathStyle()); 811 812 if (m_file_spec->IsRelative()) 813 m_file_spec->MakeAbsolute(GetCompilationDirectory()); 814 } 815 816 SymbolFileDWARFDwo *DWARFUnit::GetDwoSymbolFile() { 817 ExtractUnitDIEIfNeeded(); 818 if (m_dwo) 819 return &llvm::cast<SymbolFileDWARFDwo>(m_dwo->GetSymbolFileDWARF()); 820 return nullptr; 821 } 822 823 const DWARFDebugAranges &DWARFUnit::GetFunctionAranges() { 824 if (m_func_aranges_up == nullptr) { 825 m_func_aranges_up = std::make_unique<DWARFDebugAranges>(); 826 const DWARFDebugInfoEntry *die = DIEPtr(); 827 if (die) 828 die->BuildFunctionAddressRangeTable(this, m_func_aranges_up.get()); 829 830 if (m_dwo) { 831 const DWARFDebugInfoEntry *dwo_die = m_dwo->DIEPtr(); 832 if (dwo_die) 833 dwo_die->BuildFunctionAddressRangeTable(m_dwo.get(), 834 m_func_aranges_up.get()); 835 } 836 837 const bool minimize = false; 838 m_func_aranges_up->Sort(minimize); 839 } 840 return *m_func_aranges_up; 841 } 842 843 llvm::Expected<DWARFUnitHeader> 844 DWARFUnitHeader::extract(const DWARFDataExtractor &data, 845 DIERef::Section section, 846 lldb_private::DWARFContext &context, 847 lldb::offset_t *offset_ptr) { 848 DWARFUnitHeader header; 849 header.m_offset = *offset_ptr; 850 header.m_length = data.GetDWARFInitialLength(offset_ptr); 851 header.m_version = data.GetU16(offset_ptr); 852 if (header.m_version == 5) { 853 header.m_unit_type = data.GetU8(offset_ptr); 854 header.m_addr_size = data.GetU8(offset_ptr); 855 header.m_abbr_offset = data.GetDWARFOffset(offset_ptr); 856 if (header.m_unit_type == llvm::dwarf::DW_UT_skeleton || 857 header.m_unit_type == llvm::dwarf::DW_UT_split_compile) 858 header.m_dwo_id = data.GetU64(offset_ptr); 859 } else { 860 header.m_abbr_offset = data.GetDWARFOffset(offset_ptr); 861 header.m_addr_size = data.GetU8(offset_ptr); 862 header.m_unit_type = 863 section == DIERef::Section::DebugTypes ? DW_UT_type : DW_UT_compile; 864 } 865 866 if (context.isDwo()) { 867 if (header.IsTypeUnit()) { 868 header.m_index_entry = 869 context.GetAsLLVM().getTUIndex().getFromOffset(header.m_offset); 870 } else { 871 header.m_index_entry = 872 context.GetAsLLVM().getCUIndex().getFromOffset(header.m_offset); 873 } 874 } 875 876 if (header.m_index_entry) { 877 if (header.m_abbr_offset) { 878 return llvm::createStringError( 879 llvm::inconvertibleErrorCode(), 880 "Package unit with a non-zero abbreviation offset"); 881 } 882 auto *unit_contrib = header.m_index_entry->getContribution(); 883 if (!unit_contrib || unit_contrib->Length != header.m_length + 4) { 884 return llvm::createStringError(llvm::inconvertibleErrorCode(), 885 "Inconsistent DWARF package unit index"); 886 } 887 auto *abbr_entry = 888 header.m_index_entry->getContribution(llvm::DW_SECT_ABBREV); 889 if (!abbr_entry) { 890 return llvm::createStringError( 891 llvm::inconvertibleErrorCode(), 892 "DWARF package index missing abbreviation column"); 893 } 894 header.m_abbr_offset = abbr_entry->Offset; 895 } 896 if (header.IsTypeUnit()) { 897 header.m_type_hash = data.GetU64(offset_ptr); 898 header.m_type_offset = data.GetDWARFOffset(offset_ptr); 899 } 900 901 bool length_OK = data.ValidOffset(header.GetNextUnitOffset() - 1); 902 bool version_OK = SymbolFileDWARF::SupportedVersion(header.m_version); 903 bool addr_size_OK = (header.m_addr_size == 4) || (header.m_addr_size == 8); 904 bool type_offset_OK = 905 !header.IsTypeUnit() || (header.m_type_offset <= header.GetLength()); 906 907 if (!length_OK) 908 return llvm::make_error<llvm::object::GenericBinaryError>( 909 "Invalid unit length"); 910 if (!version_OK) 911 return llvm::make_error<llvm::object::GenericBinaryError>( 912 "Unsupported unit version"); 913 if (!addr_size_OK) 914 return llvm::make_error<llvm::object::GenericBinaryError>( 915 "Invalid unit address size"); 916 if (!type_offset_OK) 917 return llvm::make_error<llvm::object::GenericBinaryError>( 918 "Type offset out of range"); 919 920 return header; 921 } 922 923 llvm::Expected<DWARFUnitSP> 924 DWARFUnit::extract(SymbolFileDWARF &dwarf, user_id_t uid, 925 const DWARFDataExtractor &debug_info, 926 DIERef::Section section, lldb::offset_t *offset_ptr) { 927 assert(debug_info.ValidOffset(*offset_ptr)); 928 929 auto expected_header = DWARFUnitHeader::extract( 930 debug_info, section, dwarf.GetDWARFContext(), offset_ptr); 931 if (!expected_header) 932 return expected_header.takeError(); 933 934 const DWARFDebugAbbrev *abbr = dwarf.DebugAbbrev(); 935 if (!abbr) 936 return llvm::make_error<llvm::object::GenericBinaryError>( 937 "No debug_abbrev data"); 938 939 bool abbr_offset_OK = 940 dwarf.GetDWARFContext().getOrLoadAbbrevData().ValidOffset( 941 expected_header->GetAbbrOffset()); 942 if (!abbr_offset_OK) 943 return llvm::make_error<llvm::object::GenericBinaryError>( 944 "Abbreviation offset for unit is not valid"); 945 946 const DWARFAbbreviationDeclarationSet *abbrevs = 947 abbr->GetAbbreviationDeclarationSet(expected_header->GetAbbrOffset()); 948 if (!abbrevs) 949 return llvm::make_error<llvm::object::GenericBinaryError>( 950 "No abbrev exists at the specified offset."); 951 952 bool is_dwo = dwarf.GetDWARFContext().isDwo(); 953 if (expected_header->IsTypeUnit()) 954 return DWARFUnitSP(new DWARFTypeUnit(dwarf, uid, *expected_header, *abbrevs, 955 section, is_dwo)); 956 return DWARFUnitSP(new DWARFCompileUnit(dwarf, uid, *expected_header, 957 *abbrevs, section, is_dwo)); 958 } 959 960 const lldb_private::DWARFDataExtractor &DWARFUnit::GetData() const { 961 return m_section == DIERef::Section::DebugTypes 962 ? m_dwarf.GetDWARFContext().getOrLoadDebugTypesData() 963 : m_dwarf.GetDWARFContext().getOrLoadDebugInfoData(); 964 } 965 966 uint32_t DWARFUnit::GetHeaderByteSize() const { 967 switch (m_header.GetUnitType()) { 968 case llvm::dwarf::DW_UT_compile: 969 case llvm::dwarf::DW_UT_partial: 970 return GetVersion() < 5 ? 11 : 12; 971 case llvm::dwarf::DW_UT_skeleton: 972 case llvm::dwarf::DW_UT_split_compile: 973 return 20; 974 case llvm::dwarf::DW_UT_type: 975 case llvm::dwarf::DW_UT_split_type: 976 return GetVersion() < 5 ? 23 : 24; 977 } 978 llvm_unreachable("invalid UnitType."); 979 } 980 981 llvm::Optional<uint64_t> 982 DWARFUnit::GetStringOffsetSectionItem(uint32_t index) const { 983 offset_t offset = GetStrOffsetsBase() + index * 4; 984 return m_dwarf.GetDWARFContext().getOrLoadStrOffsetsData().GetU32(&offset); 985 } 986 987 llvm::Expected<DWARFRangeList> 988 DWARFUnit::FindRnglistFromOffset(dw_offset_t offset) { 989 if (GetVersion() <= 4) { 990 const DWARFDebugRanges *debug_ranges = m_dwarf.GetDebugRanges(); 991 if (!debug_ranges) 992 return llvm::make_error<llvm::object::GenericBinaryError>( 993 "No debug_ranges section"); 994 DWARFRangeList ranges; 995 debug_ranges->FindRanges(this, offset, ranges); 996 return ranges; 997 } 998 999 if (!GetRnglistTable()) 1000 return llvm::createStringError(errc::invalid_argument, 1001 "missing or invalid range list table"); 1002 1003 llvm::DWARFDataExtractor data = GetRnglistData().GetAsLLVM(); 1004 1005 // As DW_AT_rnglists_base may be missing we need to call setAddressSize. 1006 data.setAddressSize(m_header.GetAddressByteSize()); 1007 auto range_list_or_error = GetRnglistTable()->findList(data, offset); 1008 if (!range_list_or_error) 1009 return range_list_or_error.takeError(); 1010 1011 llvm::Expected<llvm::DWARFAddressRangesVector> llvm_ranges = 1012 range_list_or_error->getAbsoluteRanges( 1013 llvm::object::SectionedAddress{GetBaseAddress()}, 1014 GetAddressByteSize(), [&](uint32_t index) { 1015 uint32_t index_size = GetAddressByteSize(); 1016 dw_offset_t addr_base = GetAddrBase(); 1017 lldb::offset_t offset = addr_base + index * index_size; 1018 return llvm::object::SectionedAddress{ 1019 m_dwarf.GetDWARFContext().getOrLoadAddrData().GetMaxU64( 1020 &offset, index_size)}; 1021 }); 1022 if (!llvm_ranges) 1023 return llvm_ranges.takeError(); 1024 1025 DWARFRangeList ranges; 1026 for (const llvm::DWARFAddressRange &llvm_range : *llvm_ranges) { 1027 ranges.Append(DWARFRangeList::Entry(llvm_range.LowPC, 1028 llvm_range.HighPC - llvm_range.LowPC)); 1029 } 1030 return ranges; 1031 } 1032 1033 llvm::Expected<DWARFRangeList> 1034 DWARFUnit::FindRnglistFromIndex(uint32_t index) { 1035 llvm::Expected<uint64_t> maybe_offset = GetRnglistOffset(index); 1036 if (!maybe_offset) 1037 return maybe_offset.takeError(); 1038 return FindRnglistFromOffset(*maybe_offset); 1039 } 1040