1 //===-- DWARFUnit.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 "DWARFUnit.h" 10 11 #include "lldb/Core/Module.h" 12 #include "lldb/Host/StringConvert.h" 13 #include "lldb/Symbol/CompileUnit.h" 14 #include "lldb/Symbol/LineTable.h" 15 #include "lldb/Symbol/ObjectFile.h" 16 #include "lldb/Utility/LLDBAssert.h" 17 #include "lldb/Utility/StreamString.h" 18 #include "lldb/Utility/Timer.h" 19 20 #include "DWARFDebugAranges.h" 21 #include "DWARFDebugInfo.h" 22 #include "LogChannelDWARF.h" 23 #include "SymbolFileDWARFDebugMap.h" 24 #include "SymbolFileDWARFDwo.h" 25 26 using namespace lldb; 27 using namespace lldb_private; 28 using namespace std; 29 30 extern int g_verbose; 31 32 DWARFUnit::DWARFUnit(SymbolFileDWARF *dwarf) 33 : m_dwarf(dwarf), m_cancel_scopes(false) {} 34 35 DWARFUnit::~DWARFUnit() {} 36 37 // Parses first DIE of a compile unit. 38 void DWARFUnit::ExtractUnitDIEIfNeeded() { 39 { 40 llvm::sys::ScopedReader lock(m_first_die_mutex); 41 if (m_first_die) 42 return; // Already parsed 43 } 44 llvm::sys::ScopedWriter lock(m_first_die_mutex); 45 if (m_first_die) 46 return; // Already parsed 47 48 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 49 Timer scoped_timer( 50 func_cat, "%8.8x: DWARFUnit::ExtractUnitDIEIfNeeded()", m_offset); 51 52 // Set the offset to that of the first DIE and calculate the start of the 53 // next compilation unit header. 54 lldb::offset_t offset = GetFirstDIEOffset(); 55 56 // We are in our compile unit, parse starting at the offset we were told to 57 // parse 58 const DWARFDataExtractor &data = GetData(); 59 DWARFFormValue::FixedFormSizes fixed_form_sizes = 60 DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize()); 61 if (offset < GetNextCompileUnitOffset() && 62 m_first_die.FastExtract(data, this, fixed_form_sizes, &offset)) { 63 AddUnitDIE(m_first_die); 64 return; 65 } 66 } 67 68 // Parses a compile unit and indexes its DIEs if it hasn't already been done. 69 // It will leave this compile unit extracted forever. 70 void DWARFUnit::ExtractDIEsIfNeeded() { 71 m_cancel_scopes = true; 72 73 { 74 llvm::sys::ScopedReader lock(m_die_array_mutex); 75 if (!m_die_array.empty()) 76 return; // Already parsed 77 } 78 llvm::sys::ScopedWriter lock(m_die_array_mutex); 79 if (!m_die_array.empty()) 80 return; // Already parsed 81 82 ExtractDIEsRWLocked(); 83 } 84 85 // Parses a compile unit and indexes its DIEs if it hasn't already been done. 86 // It will clear this compile unit after returned instance gets out of scope, 87 // no other ScopedExtractDIEs instance is running for this compile unit 88 // and no ExtractDIEsIfNeeded() has been executed during this ScopedExtractDIEs 89 // lifetime. 90 DWARFUnit::ScopedExtractDIEs DWARFUnit::ExtractDIEsScoped() { 91 ScopedExtractDIEs scoped(this); 92 93 { 94 llvm::sys::ScopedReader lock(m_die_array_mutex); 95 if (!m_die_array.empty()) 96 return scoped; // Already parsed 97 } 98 llvm::sys::ScopedWriter lock(m_die_array_mutex); 99 if (!m_die_array.empty()) 100 return scoped; // Already parsed 101 102 // Otherwise m_die_array would be already populated. 103 lldbassert(!m_cancel_scopes); 104 105 ExtractDIEsRWLocked(); 106 scoped.m_clear_dies = true; 107 return scoped; 108 } 109 110 DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(DWARFUnit *cu) : m_cu(cu) { 111 lldbassert(m_cu); 112 m_cu->m_die_array_scoped_mutex.lock_shared(); 113 } 114 115 DWARFUnit::ScopedExtractDIEs::~ScopedExtractDIEs() { 116 if (!m_cu) 117 return; 118 m_cu->m_die_array_scoped_mutex.unlock_shared(); 119 if (!m_clear_dies || m_cu->m_cancel_scopes) 120 return; 121 // Be sure no other ScopedExtractDIEs is running anymore. 122 llvm::sys::ScopedWriter lock_scoped(m_cu->m_die_array_scoped_mutex); 123 llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex); 124 if (m_cu->m_cancel_scopes) 125 return; 126 m_cu->ClearDIEsRWLocked(); 127 } 128 129 DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(ScopedExtractDIEs &&rhs) 130 : m_cu(rhs.m_cu), m_clear_dies(rhs.m_clear_dies) { 131 rhs.m_cu = nullptr; 132 } 133 134 DWARFUnit::ScopedExtractDIEs &DWARFUnit::ScopedExtractDIEs::operator=( 135 DWARFUnit::ScopedExtractDIEs &&rhs) { 136 m_cu = rhs.m_cu; 137 rhs.m_cu = nullptr; 138 m_clear_dies = rhs.m_clear_dies; 139 return *this; 140 } 141 142 // Parses a compile unit and indexes its DIEs, m_die_array_mutex must be 143 // held R/W and m_die_array must be empty. 144 void DWARFUnit::ExtractDIEsRWLocked() { 145 llvm::sys::ScopedWriter first_die_lock(m_first_die_mutex); 146 147 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 148 Timer scoped_timer( 149 func_cat, "%8.8x: DWARFUnit::ExtractDIEsIfNeeded()", m_offset); 150 151 // Set the offset to that of the first DIE and calculate the start of the 152 // next compilation unit header. 153 lldb::offset_t offset = GetFirstDIEOffset(); 154 lldb::offset_t next_cu_offset = GetNextCompileUnitOffset(); 155 156 DWARFDebugInfoEntry die; 157 158 uint32_t depth = 0; 159 // We are in our compile unit, parse starting at the offset we were told to 160 // parse 161 const DWARFDataExtractor &data = GetData(); 162 std::vector<uint32_t> die_index_stack; 163 die_index_stack.reserve(32); 164 die_index_stack.push_back(0); 165 bool prev_die_had_children = false; 166 DWARFFormValue::FixedFormSizes fixed_form_sizes = 167 DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize()); 168 while (offset < next_cu_offset && 169 die.FastExtract(data, this, fixed_form_sizes, &offset)) { 170 const bool null_die = die.IsNULL(); 171 if (depth == 0) { 172 assert(m_die_array.empty() && "Compile unit DIE already added"); 173 174 // The average bytes per DIE entry has been seen to be around 14-20 so 175 // lets pre-reserve half of that since we are now stripping the NULL 176 // tags. 177 178 // Only reserve the memory if we are adding children of the main 179 // compile unit DIE. The compile unit DIE is always the first entry, so 180 // if our size is 1, then we are adding the first compile unit child 181 // DIE and should reserve the memory. 182 m_die_array.reserve(GetDebugInfoSize() / 24); 183 m_die_array.push_back(die); 184 185 if (!m_first_die) 186 AddUnitDIE(m_die_array.front()); 187 } else { 188 if (null_die) { 189 if (prev_die_had_children) { 190 // This will only happen if a DIE says is has children but all it 191 // contains is a NULL tag. Since we are removing the NULL DIEs from 192 // the list (saves up to 25% in C++ code), we need a way to let the 193 // DIE know that it actually doesn't have children. 194 if (!m_die_array.empty()) 195 m_die_array.back().SetHasChildren(false); 196 } 197 } else { 198 die.SetParentIndex(m_die_array.size() - die_index_stack[depth - 1]); 199 200 if (die_index_stack.back()) 201 m_die_array[die_index_stack.back()].SetSiblingIndex( 202 m_die_array.size() - die_index_stack.back()); 203 204 // Only push the DIE if it isn't a NULL DIE 205 m_die_array.push_back(die); 206 } 207 } 208 209 if (null_die) { 210 // NULL DIE. 211 if (!die_index_stack.empty()) 212 die_index_stack.pop_back(); 213 214 if (depth > 0) 215 --depth; 216 prev_die_had_children = false; 217 } else { 218 die_index_stack.back() = m_die_array.size() - 1; 219 // Normal DIE 220 const bool die_has_children = die.HasChildren(); 221 if (die_has_children) { 222 die_index_stack.push_back(0); 223 ++depth; 224 } 225 prev_die_had_children = die_has_children; 226 } 227 228 if (depth == 0) 229 break; // We are done with this compile unit! 230 } 231 232 if (!m_die_array.empty()) { 233 if (m_first_die) { 234 // Only needed for the assertion. 235 m_first_die.SetHasChildren(m_die_array.front().HasChildren()); 236 lldbassert(m_first_die == m_die_array.front()); 237 } 238 m_first_die = m_die_array.front(); 239 } 240 241 m_die_array.shrink_to_fit(); 242 243 if (m_dwo_symbol_file) { 244 DWARFUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit(); 245 dwo_cu->ExtractDIEsIfNeeded(); 246 } 247 } 248 249 // This is used when a split dwarf is enabled. 250 // A skeleton compilation unit may contain the DW_AT_str_offsets_base attribute 251 // that points to the first string offset of the CU contribution to the 252 // .debug_str_offsets. At the same time, the corresponding split debug unit also 253 // may use DW_FORM_strx* forms pointing to its own .debug_str_offsets.dwo and 254 // for that case, we should find the offset (skip the section header). 255 static void SetDwoStrOffsetsBase(DWARFUnit *dwo_cu) { 256 lldb::offset_t baseOffset = 0; 257 258 const DWARFDataExtractor &strOffsets = 259 dwo_cu->GetSymbolFileDWARF()->get_debug_str_offsets_data(); 260 uint64_t length = strOffsets.GetU32(&baseOffset); 261 if (length == 0xffffffff) 262 length = strOffsets.GetU64(&baseOffset); 263 264 // Check version. 265 if (strOffsets.GetU16(&baseOffset) < 5) 266 return; 267 268 // Skip padding. 269 baseOffset += 2; 270 271 dwo_cu->SetStrOffsetsBase(baseOffset); 272 } 273 274 // m_die_array_mutex must be already held as read/write. 275 void DWARFUnit::AddUnitDIE(const DWARFDebugInfoEntry &cu_die) { 276 dw_addr_t addr_base = cu_die.GetAttributeValueAsUnsigned( 277 m_dwarf, this, DW_AT_addr_base, LLDB_INVALID_ADDRESS); 278 if (addr_base != LLDB_INVALID_ADDRESS) 279 SetAddrBase(addr_base); 280 281 dw_addr_t ranges_base = cu_die.GetAttributeValueAsUnsigned( 282 m_dwarf, this, DW_AT_rnglists_base, LLDB_INVALID_ADDRESS); 283 if (ranges_base != LLDB_INVALID_ADDRESS) 284 SetRangesBase(ranges_base); 285 286 SetStrOffsetsBase(cu_die.GetAttributeValueAsUnsigned( 287 m_dwarf, this, DW_AT_str_offsets_base, 0)); 288 289 uint64_t base_addr = cu_die.GetAttributeValueAsAddress( 290 m_dwarf, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS); 291 if (base_addr == LLDB_INVALID_ADDRESS) 292 base_addr = cu_die.GetAttributeValueAsAddress( 293 m_dwarf, this, DW_AT_entry_pc, 0); 294 SetBaseAddress(base_addr); 295 296 std::unique_ptr<SymbolFileDWARFDwo> dwo_symbol_file = 297 m_dwarf->GetDwoSymbolFileForCompileUnit(*this, cu_die); 298 if (!dwo_symbol_file) 299 return; 300 301 DWARFUnit *dwo_cu = dwo_symbol_file->GetCompileUnit(); 302 if (!dwo_cu) 303 return; // Can't fetch the compile unit from the dwo file. 304 305 DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly(); 306 if (!dwo_cu_die.IsValid()) 307 return; // Can't fetch the compile unit DIE from the dwo file. 308 309 uint64_t main_dwo_id = 310 cu_die.GetAttributeValueAsUnsigned(m_dwarf, this, DW_AT_GNU_dwo_id, 0); 311 uint64_t sub_dwo_id = 312 dwo_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_dwo_id, 0); 313 if (main_dwo_id != sub_dwo_id) 314 return; // The 2 dwo ID isn't match. Don't use the dwo file as it belongs to 315 // a differectn compilation. 316 317 m_dwo_symbol_file = std::move(dwo_symbol_file); 318 319 // Here for DWO CU we want to use the address base set in the skeleton unit 320 // (DW_AT_addr_base) if it is available and use the DW_AT_GNU_addr_base 321 // otherwise. We do that because pre-DWARF v5 could use the DW_AT_GNU_* 322 // attributes which were applicable to the DWO units. The corresponding 323 // DW_AT_* attributes standardized in DWARF v5 are also applicable to the main 324 // unit in contrast. 325 if (addr_base == LLDB_INVALID_ADDRESS) 326 addr_base = cu_die.GetAttributeValueAsUnsigned(m_dwarf, this, 327 DW_AT_GNU_addr_base, 0); 328 dwo_cu->SetAddrBase(addr_base); 329 330 if (ranges_base == LLDB_INVALID_ADDRESS) 331 ranges_base = cu_die.GetAttributeValueAsUnsigned(m_dwarf, this, 332 DW_AT_GNU_ranges_base, 0); 333 dwo_cu->SetRangesBase(ranges_base); 334 335 dwo_cu->SetBaseObjOffset(m_offset); 336 337 SetDwoStrOffsetsBase(dwo_cu); 338 } 339 340 DWARFDIE DWARFUnit::LookupAddress(const dw_addr_t address) { 341 if (DIE()) { 342 const DWARFDebugAranges &func_aranges = GetFunctionAranges(); 343 344 // Re-check the aranges auto pointer contents in case it was created above 345 if (!func_aranges.IsEmpty()) 346 return GetDIE(func_aranges.FindAddress(address)); 347 } 348 return DWARFDIE(); 349 } 350 351 size_t DWARFUnit::AppendDIEsWithTag(const dw_tag_t tag, 352 std::vector<DWARFDIE> &dies, 353 uint32_t depth) const { 354 size_t old_size = dies.size(); 355 { 356 llvm::sys::ScopedReader lock(m_die_array_mutex); 357 DWARFDebugInfoEntry::const_iterator pos; 358 DWARFDebugInfoEntry::const_iterator end = m_die_array.end(); 359 for (pos = m_die_array.begin(); pos != end; ++pos) { 360 if (pos->Tag() == tag) 361 dies.emplace_back(this, &(*pos)); 362 } 363 } 364 365 // Return the number of DIEs added to the collection 366 return dies.size() - old_size; 367 } 368 369 lldb::user_id_t DWARFUnit::GetID() const { 370 dw_offset_t local_id = 371 m_base_obj_offset != DW_INVALID_OFFSET ? m_base_obj_offset : m_offset; 372 if (m_dwarf) 373 return DIERef(local_id, local_id).GetUID(m_dwarf); 374 else 375 return local_id; 376 } 377 378 dw_offset_t DWARFUnit::GetNextCompileUnitOffset() const { 379 return m_offset + GetLengthByteSize() + GetLength(); 380 } 381 382 size_t DWARFUnit::GetDebugInfoSize() const { 383 return GetLengthByteSize() + GetLength() - GetHeaderByteSize(); 384 } 385 386 const DWARFAbbreviationDeclarationSet *DWARFUnit::GetAbbreviations() const { 387 return m_abbrevs; 388 } 389 390 dw_offset_t DWARFUnit::GetAbbrevOffset() const { 391 return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET; 392 } 393 394 void DWARFUnit::SetAddrBase(dw_addr_t addr_base) { m_addr_base = addr_base; } 395 396 void DWARFUnit::SetRangesBase(dw_addr_t ranges_base) { 397 m_ranges_base = ranges_base; 398 } 399 400 void DWARFUnit::SetBaseObjOffset(dw_offset_t base_obj_offset) { 401 m_base_obj_offset = base_obj_offset; 402 } 403 404 void DWARFUnit::SetStrOffsetsBase(dw_offset_t str_offsets_base) { 405 m_str_offsets_base = str_offsets_base; 406 } 407 408 // It may be called only with m_die_array_mutex held R/W. 409 void DWARFUnit::ClearDIEsRWLocked() { 410 m_die_array.clear(); 411 m_die_array.shrink_to_fit(); 412 413 if (m_dwo_symbol_file) 414 m_dwo_symbol_file->GetCompileUnit()->ClearDIEsRWLocked(); 415 } 416 417 void DWARFUnit::BuildAddressRangeTable(SymbolFileDWARF *dwarf, 418 DWARFDebugAranges *debug_aranges) { 419 // This function is usually called if there in no .debug_aranges section in 420 // order to produce a compile unit level set of address ranges that is 421 // accurate. 422 423 size_t num_debug_aranges = debug_aranges->GetNumRanges(); 424 425 // First get the compile unit DIE only and check if it has a DW_AT_ranges 426 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); 427 428 const dw_offset_t cu_offset = GetOffset(); 429 if (die) { 430 DWARFRangeList ranges; 431 const size_t num_ranges = 432 die->GetAttributeAddressRanges(dwarf, this, ranges, false); 433 if (num_ranges > 0) { 434 // This compile unit has DW_AT_ranges, assume this is correct if it is 435 // present since clang no longer makes .debug_aranges by default and it 436 // emits DW_AT_ranges for DW_TAG_compile_units. GCC also does this with 437 // recent GCC builds. 438 for (size_t i = 0; i < num_ranges; ++i) { 439 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i); 440 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(), 441 range.GetRangeEnd()); 442 } 443 444 return; // We got all of our ranges from the DW_AT_ranges attribute 445 } 446 } 447 // We don't have a DW_AT_ranges attribute, so we need to parse the DWARF 448 449 // If the DIEs weren't parsed, then we don't want all dies for all compile 450 // units to stay loaded when they weren't needed. So we can end up parsing 451 // the DWARF and then throwing them all away to keep memory usage down. 452 ScopedExtractDIEs clear_dies(ExtractDIEsScoped()); 453 454 die = DIEPtr(); 455 if (die) 456 die->BuildAddressRangeTable(dwarf, this, debug_aranges); 457 458 if (debug_aranges->GetNumRanges() == num_debug_aranges) { 459 // We got nothing from the functions, maybe we have a line tables only 460 // situation. Check the line tables and build the arange table from this. 461 SymbolContext sc; 462 sc.comp_unit = dwarf->GetCompUnitForDWARFCompUnit(this); 463 if (sc.comp_unit) { 464 SymbolFileDWARFDebugMap *debug_map_sym_file = 465 m_dwarf->GetDebugMapSymfile(); 466 if (debug_map_sym_file == NULL) { 467 LineTable *line_table = sc.comp_unit->GetLineTable(); 468 469 if (line_table) { 470 LineTable::FileAddressRanges file_ranges; 471 const bool append = true; 472 const size_t num_ranges = 473 line_table->GetContiguousFileAddressRanges(file_ranges, append); 474 for (uint32_t idx = 0; idx < num_ranges; ++idx) { 475 const LineTable::FileAddressRanges::Entry &range = 476 file_ranges.GetEntryRef(idx); 477 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(), 478 range.GetRangeEnd()); 479 } 480 } 481 } else 482 debug_map_sym_file->AddOSOARanges(dwarf, debug_aranges); 483 } 484 } 485 486 if (debug_aranges->GetNumRanges() == num_debug_aranges) { 487 // We got nothing from the functions, maybe we have a line tables only 488 // situation. Check the line tables and build the arange table from this. 489 SymbolContext sc; 490 sc.comp_unit = dwarf->GetCompUnitForDWARFCompUnit(this); 491 if (sc.comp_unit) { 492 LineTable *line_table = sc.comp_unit->GetLineTable(); 493 494 if (line_table) { 495 LineTable::FileAddressRanges file_ranges; 496 const bool append = true; 497 const size_t num_ranges = 498 line_table->GetContiguousFileAddressRanges(file_ranges, append); 499 for (uint32_t idx = 0; idx < num_ranges; ++idx) { 500 const LineTable::FileAddressRanges::Entry &range = 501 file_ranges.GetEntryRef(idx); 502 debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(), 503 range.GetRangeEnd()); 504 } 505 } 506 } 507 } 508 } 509 510 lldb::ByteOrder DWARFUnit::GetByteOrder() const { 511 return m_dwarf->GetObjectFile()->GetByteOrder(); 512 } 513 514 TypeSystem *DWARFUnit::GetTypeSystem() { 515 if (m_dwarf) 516 return m_dwarf->GetTypeSystemForLanguage(GetLanguageType()); 517 else 518 return nullptr; 519 } 520 521 DWARFFormValue::FixedFormSizes DWARFUnit::GetFixedFormSizes() { 522 return DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize()); 523 } 524 525 void DWARFUnit::SetBaseAddress(dw_addr_t base_addr) { m_base_addr = base_addr; } 526 527 // Compare function DWARFDebugAranges::Range structures 528 static bool CompareDIEOffset(const DWARFDebugInfoEntry &die, 529 const dw_offset_t die_offset) { 530 return die.GetOffset() < die_offset; 531 } 532 533 // GetDIE() 534 // 535 // Get the DIE (Debug Information Entry) with the specified offset by first 536 // checking if the DIE is contained within this compile unit and grabbing the 537 // DIE from this compile unit. Otherwise we grab the DIE from the DWARF file. 538 DWARFDIE 539 DWARFUnit::GetDIE(dw_offset_t die_offset) { 540 if (die_offset != DW_INVALID_OFFSET) { 541 if (GetDwoSymbolFile()) 542 return GetDwoSymbolFile()->GetCompileUnit()->GetDIE(die_offset); 543 544 if (ContainsDIEOffset(die_offset)) { 545 ExtractDIEsIfNeeded(); 546 DWARFDebugInfoEntry::const_iterator end = m_die_array.cend(); 547 DWARFDebugInfoEntry::const_iterator pos = 548 lower_bound(m_die_array.cbegin(), end, die_offset, CompareDIEOffset); 549 if (pos != end) { 550 if (die_offset == (*pos).GetOffset()) 551 return DWARFDIE(this, &(*pos)); 552 } 553 } else { 554 // Don't specify the compile unit offset as we don't know it because the 555 // DIE belongs to 556 // a different compile unit in the same symbol file. 557 return m_dwarf->DebugInfo()->GetDIEForDIEOffset(die_offset); 558 } 559 } 560 return DWARFDIE(); // Not found 561 } 562 563 uint8_t DWARFUnit::GetAddressByteSize(const DWARFUnit *cu) { 564 if (cu) 565 return cu->GetAddressByteSize(); 566 return DWARFUnit::GetDefaultAddressSize(); 567 } 568 569 uint8_t DWARFUnit::GetDefaultAddressSize() { return 4; } 570 571 void *DWARFUnit::GetUserData() const { return m_user_data; } 572 573 void DWARFUnit::SetUserData(void *d) { 574 m_user_data = d; 575 if (m_dwo_symbol_file) 576 m_dwo_symbol_file->GetCompileUnit()->SetUserData(d); 577 } 578 579 bool DWARFUnit::Supports_DW_AT_APPLE_objc_complete_type() { 580 return GetProducer() != eProducerLLVMGCC; 581 } 582 583 bool DWARFUnit::DW_AT_decl_file_attributes_are_invalid() { 584 // llvm-gcc makes completely invalid decl file attributes and won't ever be 585 // fixed, so we need to know to ignore these. 586 return GetProducer() == eProducerLLVMGCC; 587 } 588 589 bool DWARFUnit::Supports_unnamed_objc_bitfields() { 590 if (GetProducer() == eProducerClang) { 591 const uint32_t major_version = GetProducerVersionMajor(); 592 return major_version > 425 || 593 (major_version == 425 && GetProducerVersionUpdate() >= 13); 594 } 595 return true; // Assume all other compilers didn't have incorrect ObjC bitfield 596 // info 597 } 598 599 SymbolFileDWARF *DWARFUnit::GetSymbolFileDWARF() const { return m_dwarf; } 600 601 void DWARFUnit::ParseProducerInfo() { 602 m_producer_version_major = UINT32_MAX; 603 m_producer_version_minor = UINT32_MAX; 604 m_producer_version_update = UINT32_MAX; 605 606 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); 607 if (die) { 608 609 const char *producer_cstr = 610 die->GetAttributeValueAsString(m_dwarf, this, DW_AT_producer, NULL); 611 if (producer_cstr) { 612 RegularExpression llvm_gcc_regex( 613 llvm::StringRef("^4\\.[012]\\.[01] \\(Based on Apple " 614 "Inc\\. build [0-9]+\\) \\(LLVM build " 615 "[\\.0-9]+\\)$")); 616 if (llvm_gcc_regex.Execute(llvm::StringRef(producer_cstr))) { 617 m_producer = eProducerLLVMGCC; 618 } else if (strstr(producer_cstr, "clang")) { 619 static RegularExpression g_clang_version_regex( 620 llvm::StringRef("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)")); 621 RegularExpression::Match regex_match(3); 622 if (g_clang_version_regex.Execute(llvm::StringRef(producer_cstr), 623 ®ex_match)) { 624 std::string str; 625 if (regex_match.GetMatchAtIndex(producer_cstr, 1, str)) 626 m_producer_version_major = 627 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10); 628 if (regex_match.GetMatchAtIndex(producer_cstr, 2, str)) 629 m_producer_version_minor = 630 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10); 631 if (regex_match.GetMatchAtIndex(producer_cstr, 3, str)) 632 m_producer_version_update = 633 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10); 634 } 635 m_producer = eProducerClang; 636 } else if (strstr(producer_cstr, "GNU")) 637 m_producer = eProducerGCC; 638 } 639 } 640 if (m_producer == eProducerInvalid) 641 m_producer = eProcucerOther; 642 } 643 644 DWARFProducer DWARFUnit::GetProducer() { 645 if (m_producer == eProducerInvalid) 646 ParseProducerInfo(); 647 return m_producer; 648 } 649 650 uint32_t DWARFUnit::GetProducerVersionMajor() { 651 if (m_producer_version_major == 0) 652 ParseProducerInfo(); 653 return m_producer_version_major; 654 } 655 656 uint32_t DWARFUnit::GetProducerVersionMinor() { 657 if (m_producer_version_minor == 0) 658 ParseProducerInfo(); 659 return m_producer_version_minor; 660 } 661 662 uint32_t DWARFUnit::GetProducerVersionUpdate() { 663 if (m_producer_version_update == 0) 664 ParseProducerInfo(); 665 return m_producer_version_update; 666 } 667 LanguageType DWARFUnit::LanguageTypeFromDWARF(uint64_t val) { 668 // Note: user languages between lo_user and hi_user must be handled 669 // explicitly here. 670 switch (val) { 671 case DW_LANG_Mips_Assembler: 672 return eLanguageTypeMipsAssembler; 673 case DW_LANG_GOOGLE_RenderScript: 674 return eLanguageTypeExtRenderScript; 675 default: 676 return static_cast<LanguageType>(val); 677 } 678 } 679 680 LanguageType DWARFUnit::GetLanguageType() { 681 if (m_language_type != eLanguageTypeUnknown) 682 return m_language_type; 683 684 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); 685 if (die) 686 m_language_type = LanguageTypeFromDWARF( 687 die->GetAttributeValueAsUnsigned(m_dwarf, this, DW_AT_language, 0)); 688 return m_language_type; 689 } 690 691 bool DWARFUnit::GetIsOptimized() { 692 if (m_is_optimized == eLazyBoolCalculate) { 693 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); 694 if (die) { 695 m_is_optimized = eLazyBoolNo; 696 if (die->GetAttributeValueAsUnsigned(m_dwarf, this, DW_AT_APPLE_optimized, 697 0) == 1) { 698 m_is_optimized = eLazyBoolYes; 699 } 700 } 701 } 702 return m_is_optimized == eLazyBoolYes; 703 } 704 705 FileSpec::Style DWARFUnit::GetPathStyle() { 706 if (!m_comp_dir) 707 ComputeCompDirAndGuessPathStyle(); 708 return m_comp_dir->GetPathStyle(); 709 } 710 711 const FileSpec &DWARFUnit::GetCompilationDirectory() { 712 if (!m_comp_dir) 713 ComputeCompDirAndGuessPathStyle(); 714 return *m_comp_dir; 715 } 716 717 // DWARF2/3 suggests the form hostname:pathname for compilation directory. 718 // Remove the host part if present. 719 static llvm::StringRef 720 removeHostnameFromPathname(llvm::StringRef path_from_dwarf) { 721 llvm::StringRef host, path; 722 std::tie(host, path) = path_from_dwarf.split(':'); 723 724 if (host.contains('/')) 725 return path_from_dwarf; 726 727 // check whether we have a windows path, and so the first character is a 728 // drive-letter not a hostname. 729 if (host.size() == 1 && llvm::isAlpha(host[0]) && path.startswith("\\")) 730 return path_from_dwarf; 731 732 return path; 733 } 734 735 static FileSpec resolveCompDir(const FileSpec &path) { 736 bool is_symlink = SymbolFileDWARF::GetSymlinkPaths().FindFileIndex( 737 0, path, /*full*/ true) != UINT32_MAX; 738 739 if (!is_symlink) 740 return path; 741 742 namespace fs = llvm::sys::fs; 743 if (fs::get_file_type(path.GetPath(), false) != fs::file_type::symlink_file) 744 return path; 745 746 FileSpec resolved_symlink; 747 const auto error = FileSystem::Instance().Readlink(path, resolved_symlink); 748 if (error.Success()) 749 return resolved_symlink; 750 751 return path; 752 } 753 754 void DWARFUnit::ComputeCompDirAndGuessPathStyle() { 755 m_comp_dir = FileSpec(); 756 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); 757 if (!die) 758 return; 759 760 llvm::StringRef comp_dir = removeHostnameFromPathname( 761 die->GetAttributeValueAsString(m_dwarf, this, DW_AT_comp_dir, NULL)); 762 if (!comp_dir.empty()) { 763 FileSpec::Style comp_dir_style = 764 FileSpec::GuessPathStyle(comp_dir).getValueOr(FileSpec::Style::native); 765 m_comp_dir = resolveCompDir(FileSpec(comp_dir, comp_dir_style)); 766 } else { 767 // Try to detect the style based on the DW_AT_name attribute, but just store 768 // the detected style in the m_comp_dir field. 769 const char *name = 770 die->GetAttributeValueAsString(m_dwarf, this, DW_AT_name, NULL); 771 m_comp_dir = FileSpec( 772 "", FileSpec::GuessPathStyle(name).getValueOr(FileSpec::Style::native)); 773 } 774 } 775 776 SymbolFileDWARFDwo *DWARFUnit::GetDwoSymbolFile() const { 777 return m_dwo_symbol_file.get(); 778 } 779 780 dw_offset_t DWARFUnit::GetBaseObjOffset() const { return m_base_obj_offset; } 781 782 const DWARFDebugAranges &DWARFUnit::GetFunctionAranges() { 783 if (m_func_aranges_up == NULL) { 784 m_func_aranges_up.reset(new DWARFDebugAranges()); 785 const DWARFDebugInfoEntry *die = DIEPtr(); 786 if (die) 787 die->BuildFunctionAddressRangeTable(m_dwarf, this, 788 m_func_aranges_up.get()); 789 790 if (m_dwo_symbol_file) { 791 DWARFUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit(); 792 const DWARFDebugInfoEntry *dwo_die = dwo_cu->DIEPtr(); 793 if (dwo_die) 794 dwo_die->BuildFunctionAddressRangeTable(m_dwo_symbol_file.get(), dwo_cu, 795 m_func_aranges_up.get()); 796 } 797 798 const bool minimize = false; 799 m_func_aranges_up->Sort(minimize); 800 } 801 return *m_func_aranges_up; 802 } 803 804