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