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 "llvm/DebugInfo/DWARF/DWARFUnit.h" 10 #include "llvm/ADT/SmallString.h" 11 #include "llvm/ADT/StringRef.h" 12 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h" 13 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" 14 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 15 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" 16 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h" 17 #include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h" 18 #include "llvm/DebugInfo/DWARF/DWARFDie.h" 19 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 20 #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h" 21 #include "llvm/Support/DataExtractor.h" 22 #include "llvm/Support/Errc.h" 23 #include "llvm/Support/Path.h" 24 #include "llvm/Support/WithColor.h" 25 #include <algorithm> 26 #include <cassert> 27 #include <cstddef> 28 #include <cstdint> 29 #include <cstdio> 30 #include <utility> 31 #include <vector> 32 33 using namespace llvm; 34 using namespace dwarf; 35 36 void DWARFUnitVector::addUnitsForSection(DWARFContext &C, 37 const DWARFSection &Section, 38 DWARFSectionKind SectionKind) { 39 const DWARFObject &D = C.getDWARFObj(); 40 addUnitsImpl(C, D, Section, C.getDebugAbbrev(), &D.getRangesSection(), 41 &D.getLocSection(), D.getStrSection(), 42 D.getStrOffsetsSection(), &D.getAddrSection(), 43 D.getLineSection(), D.isLittleEndian(), false, false, 44 SectionKind); 45 } 46 47 void DWARFUnitVector::addUnitsForDWOSection(DWARFContext &C, 48 const DWARFSection &DWOSection, 49 DWARFSectionKind SectionKind, 50 bool Lazy) { 51 const DWARFObject &D = C.getDWARFObj(); 52 addUnitsImpl(C, D, DWOSection, C.getDebugAbbrevDWO(), &D.getRangesDWOSection(), 53 &D.getLocDWOSection(), D.getStrDWOSection(), 54 D.getStrOffsetsDWOSection(), &D.getAddrSection(), 55 D.getLineDWOSection(), C.isLittleEndian(), true, Lazy, 56 SectionKind); 57 } 58 59 void DWARFUnitVector::addUnitsImpl( 60 DWARFContext &Context, const DWARFObject &Obj, const DWARFSection &Section, 61 const DWARFDebugAbbrev *DA, const DWARFSection *RS, 62 const DWARFSection *LocSection, StringRef SS, const DWARFSection &SOS, 63 const DWARFSection *AOS, const DWARFSection &LS, bool LE, bool IsDWO, 64 bool Lazy, DWARFSectionKind SectionKind) { 65 DWARFDataExtractor Data(Obj, Section, LE, 0); 66 // Lazy initialization of Parser, now that we have all section info. 67 if (!Parser) { 68 Parser = [=, &Context, &Obj, &Section, &SOS, 69 &LS](uint64_t Offset, DWARFSectionKind SectionKind, 70 const DWARFSection *CurSection, 71 const DWARFUnitIndex::Entry *IndexEntry) 72 -> std::unique_ptr<DWARFUnit> { 73 const DWARFSection &InfoSection = CurSection ? *CurSection : Section; 74 DWARFDataExtractor Data(Obj, InfoSection, LE, 0); 75 if (!Data.isValidOffset(Offset)) 76 return nullptr; 77 const DWARFUnitIndex *Index = nullptr; 78 if (IsDWO) 79 Index = &getDWARFUnitIndex(Context, SectionKind); 80 DWARFUnitHeader Header; 81 if (!Header.extract(Context, Data, &Offset, SectionKind, Index, 82 IndexEntry)) 83 return nullptr; 84 std::unique_ptr<DWARFUnit> U; 85 if (Header.isTypeUnit()) 86 U = std::make_unique<DWARFTypeUnit>(Context, InfoSection, Header, DA, 87 RS, LocSection, SS, SOS, AOS, LS, 88 LE, IsDWO, *this); 89 else 90 U = std::make_unique<DWARFCompileUnit>(Context, InfoSection, Header, 91 DA, RS, LocSection, SS, SOS, 92 AOS, LS, LE, IsDWO, *this); 93 return U; 94 }; 95 } 96 if (Lazy) 97 return; 98 // Find a reasonable insertion point within the vector. We skip over 99 // (a) units from a different section, (b) units from the same section 100 // but with lower offset-within-section. This keeps units in order 101 // within a section, although not necessarily within the object file, 102 // even if we do lazy parsing. 103 auto I = this->begin(); 104 uint64_t Offset = 0; 105 while (Data.isValidOffset(Offset)) { 106 if (I != this->end() && 107 (&(*I)->getInfoSection() != &Section || (*I)->getOffset() == Offset)) { 108 ++I; 109 continue; 110 } 111 auto U = Parser(Offset, SectionKind, &Section, nullptr); 112 // If parsing failed, we're done with this section. 113 if (!U) 114 break; 115 Offset = U->getNextUnitOffset(); 116 I = std::next(this->insert(I, std::move(U))); 117 } 118 } 119 120 DWARFUnit *DWARFUnitVector::addUnit(std::unique_ptr<DWARFUnit> Unit) { 121 auto I = std::upper_bound(begin(), end(), Unit, 122 [](const std::unique_ptr<DWARFUnit> &LHS, 123 const std::unique_ptr<DWARFUnit> &RHS) { 124 return LHS->getOffset() < RHS->getOffset(); 125 }); 126 return this->insert(I, std::move(Unit))->get(); 127 } 128 129 DWARFUnit *DWARFUnitVector::getUnitForOffset(uint64_t Offset) const { 130 auto end = begin() + getNumInfoUnits(); 131 auto *CU = 132 std::upper_bound(begin(), end, Offset, 133 [](uint64_t LHS, const std::unique_ptr<DWARFUnit> &RHS) { 134 return LHS < RHS->getNextUnitOffset(); 135 }); 136 if (CU != end && (*CU)->getOffset() <= Offset) 137 return CU->get(); 138 return nullptr; 139 } 140 141 DWARFUnit * 142 DWARFUnitVector::getUnitForIndexEntry(const DWARFUnitIndex::Entry &E) { 143 const auto *CUOff = E.getOffset(DW_SECT_INFO); 144 if (!CUOff) 145 return nullptr; 146 147 auto Offset = CUOff->Offset; 148 auto end = begin() + getNumInfoUnits(); 149 150 auto *CU = 151 std::upper_bound(begin(), end, CUOff->Offset, 152 [](uint64_t LHS, const std::unique_ptr<DWARFUnit> &RHS) { 153 return LHS < RHS->getNextUnitOffset(); 154 }); 155 if (CU != end && (*CU)->getOffset() <= Offset) 156 return CU->get(); 157 158 if (!Parser) 159 return nullptr; 160 161 auto U = Parser(Offset, DW_SECT_INFO, nullptr, &E); 162 if (!U) 163 U = nullptr; 164 165 auto *NewCU = U.get(); 166 this->insert(CU, std::move(U)); 167 ++NumInfoUnits; 168 return NewCU; 169 } 170 171 DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section, 172 const DWARFUnitHeader &Header, const DWARFDebugAbbrev *DA, 173 const DWARFSection *RS, const DWARFSection *LocSection, 174 StringRef SS, const DWARFSection &SOS, 175 const DWARFSection *AOS, const DWARFSection &LS, bool LE, 176 bool IsDWO, const DWARFUnitVector &UnitVector) 177 : Context(DC), InfoSection(Section), Header(Header), Abbrev(DA), 178 RangeSection(RS), LineSection(LS), StringSection(SS), 179 StringOffsetSection(SOS), AddrOffsetSection(AOS), isLittleEndian(LE), 180 IsDWO(IsDWO), UnitVector(UnitVector) { 181 clear(); 182 if (IsDWO) { 183 // If we are reading a package file, we need to adjust the location list 184 // data based on the index entries. 185 StringRef Data = LocSection->Data; 186 if (auto *IndexEntry = Header.getIndexEntry()) 187 if (const auto *C = IndexEntry->getOffset(DW_SECT_LOC)) 188 Data = Data.substr(C->Offset, C->Length); 189 190 DWARFDataExtractor DWARFData = 191 Header.getVersion() >= 5 192 ? DWARFDataExtractor(Context.getDWARFObj(), 193 Context.getDWARFObj().getLoclistsDWOSection(), 194 isLittleEndian, getAddressByteSize()) 195 : DWARFDataExtractor(Data, isLittleEndian, getAddressByteSize()); 196 LocTable = 197 std::make_unique<DWARFDebugLoclists>(DWARFData, Header.getVersion()); 198 199 } else if (Header.getVersion() >= 5) { 200 LocTable = std::make_unique<DWARFDebugLoclists>( 201 DWARFDataExtractor(Context.getDWARFObj(), 202 Context.getDWARFObj().getLoclistsSection(), 203 isLittleEndian, getAddressByteSize()), 204 Header.getVersion()); 205 } else { 206 LocTable = std::make_unique<DWARFDebugLoc>( 207 DWARFDataExtractor(Context.getDWARFObj(), *LocSection, isLittleEndian, 208 getAddressByteSize())); 209 } 210 } 211 212 DWARFUnit::~DWARFUnit() = default; 213 214 DWARFDataExtractor DWARFUnit::getDebugInfoExtractor() const { 215 return DWARFDataExtractor(Context.getDWARFObj(), InfoSection, isLittleEndian, 216 getAddressByteSize()); 217 } 218 219 Optional<object::SectionedAddress> 220 DWARFUnit::getAddrOffsetSectionItem(uint32_t Index) const { 221 if (IsDWO) { 222 auto R = Context.info_section_units(); 223 auto I = R.begin(); 224 // Surprising if a DWO file has more than one skeleton unit in it - this 225 // probably shouldn't be valid, but if a use case is found, here's where to 226 // support it (probably have to linearly search for the matching skeleton CU 227 // here) 228 if (I != R.end() && std::next(I) == R.end()) 229 return (*I)->getAddrOffsetSectionItem(Index); 230 } 231 if (!AddrOffsetSectionBase) 232 return None; 233 uint64_t Offset = *AddrOffsetSectionBase + Index * getAddressByteSize(); 234 if (AddrOffsetSection->Data.size() < Offset + getAddressByteSize()) 235 return None; 236 DWARFDataExtractor DA(Context.getDWARFObj(), *AddrOffsetSection, 237 isLittleEndian, getAddressByteSize()); 238 uint64_t Section; 239 uint64_t Address = DA.getRelocatedAddress(&Offset, &Section); 240 return {{Address, Section}}; 241 } 242 243 Optional<uint64_t> DWARFUnit::getStringOffsetSectionItem(uint32_t Index) const { 244 if (!StringOffsetsTableContribution) 245 return None; 246 unsigned ItemSize = getDwarfStringOffsetsByteSize(); 247 uint64_t Offset = getStringOffsetsBase() + Index * ItemSize; 248 if (StringOffsetSection.Data.size() < Offset + ItemSize) 249 return None; 250 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection, 251 isLittleEndian, 0); 252 return DA.getRelocatedValue(ItemSize, &Offset); 253 } 254 255 bool DWARFUnitHeader::extract(DWARFContext &Context, 256 const DWARFDataExtractor &debug_info, 257 uint64_t *offset_ptr, 258 DWARFSectionKind SectionKind, 259 const DWARFUnitIndex *Index, 260 const DWARFUnitIndex::Entry *Entry) { 261 Offset = *offset_ptr; 262 IndexEntry = Entry; 263 if (!IndexEntry && Index) 264 IndexEntry = Index->getFromOffset(*offset_ptr); 265 Length = debug_info.getRelocatedValue(4, offset_ptr); 266 FormParams.Format = DWARF32; 267 if (Length == dwarf::DW_LENGTH_DWARF64) { 268 Length = debug_info.getU64(offset_ptr); 269 FormParams.Format = DWARF64; 270 } 271 FormParams.Version = debug_info.getU16(offset_ptr); 272 if (FormParams.Version >= 5) { 273 UnitType = debug_info.getU8(offset_ptr); 274 FormParams.AddrSize = debug_info.getU8(offset_ptr); 275 AbbrOffset = debug_info.getRelocatedValue(FormParams.getDwarfOffsetByteSize(), offset_ptr); 276 } else { 277 AbbrOffset = debug_info.getRelocatedValue(FormParams.getDwarfOffsetByteSize(), offset_ptr); 278 FormParams.AddrSize = debug_info.getU8(offset_ptr); 279 // Fake a unit type based on the section type. This isn't perfect, 280 // but distinguishing compile and type units is generally enough. 281 if (SectionKind == DW_SECT_TYPES) 282 UnitType = DW_UT_type; 283 else 284 UnitType = DW_UT_compile; 285 } 286 if (IndexEntry) { 287 if (AbbrOffset) 288 return false; 289 auto *UnitContrib = IndexEntry->getOffset(); 290 if (!UnitContrib || UnitContrib->Length != (Length + 4)) 291 return false; 292 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV); 293 if (!AbbrEntry) 294 return false; 295 AbbrOffset = AbbrEntry->Offset; 296 } 297 if (isTypeUnit()) { 298 TypeHash = debug_info.getU64(offset_ptr); 299 TypeOffset = 300 debug_info.getUnsigned(offset_ptr, FormParams.getDwarfOffsetByteSize()); 301 } else if (UnitType == DW_UT_split_compile || UnitType == DW_UT_skeleton) 302 DWOId = debug_info.getU64(offset_ptr); 303 304 // Header fields all parsed, capture the size of this unit header. 305 assert(*offset_ptr - Offset <= 255 && "unexpected header size"); 306 Size = uint8_t(*offset_ptr - Offset); 307 308 // Type offset is unit-relative; should be after the header and before 309 // the end of the current unit. 310 bool TypeOffsetOK = 311 !isTypeUnit() 312 ? true 313 : TypeOffset >= Size && 314 TypeOffset < getLength() + getUnitLengthFieldByteSize(); 315 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1); 316 bool VersionOK = DWARFContext::isSupportedVersion(getVersion()); 317 bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8; 318 319 if (!LengthOK || !VersionOK || !AddrSizeOK || !TypeOffsetOK) 320 return false; 321 322 // Keep track of the highest DWARF version we encounter across all units. 323 Context.setMaxVersionIfGreater(getVersion()); 324 return true; 325 } 326 327 // Parse the rangelist table header, including the optional array of offsets 328 // following it (DWARF v5 and later). 329 template<typename ListTableType> 330 static Expected<ListTableType> 331 parseListTableHeader(DWARFDataExtractor &DA, uint64_t Offset, 332 DwarfFormat Format) { 333 // We are expected to be called with Offset 0 or pointing just past the table 334 // header. Correct Offset in the latter case so that it points to the start 335 // of the header. 336 if (Offset > 0) { 337 uint64_t HeaderSize = DWARFListTableHeader::getHeaderSize(Format); 338 if (Offset < HeaderSize) 339 return createStringError(errc::invalid_argument, "did not detect a valid" 340 " list table with base = 0x%" PRIx64 "\n", 341 Offset); 342 Offset -= HeaderSize; 343 } 344 ListTableType Table; 345 if (Error E = Table.extractHeaderAndOffsets(DA, &Offset)) 346 return std::move(E); 347 return Table; 348 } 349 350 Error DWARFUnit::extractRangeList(uint64_t RangeListOffset, 351 DWARFDebugRangeList &RangeList) const { 352 // Require that compile unit is extracted. 353 assert(!DieArray.empty()); 354 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection, 355 isLittleEndian, getAddressByteSize()); 356 uint64_t ActualRangeListOffset = RangeSectionBase + RangeListOffset; 357 return RangeList.extract(RangesData, &ActualRangeListOffset); 358 } 359 360 void DWARFUnit::clear() { 361 Abbrevs = nullptr; 362 BaseAddr.reset(); 363 RangeSectionBase = 0; 364 LocSectionBase = 0; 365 AddrOffsetSectionBase = None; 366 clearDIEs(false); 367 DWO.reset(); 368 } 369 370 const char *DWARFUnit::getCompilationDir() { 371 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr); 372 } 373 374 void DWARFUnit::extractDIEsToVector( 375 bool AppendCUDie, bool AppendNonCUDies, 376 std::vector<DWARFDebugInfoEntry> &Dies) const { 377 if (!AppendCUDie && !AppendNonCUDies) 378 return; 379 380 // Set the offset to that of the first DIE and calculate the start of the 381 // next compilation unit header. 382 uint64_t DIEOffset = getOffset() + getHeaderSize(); 383 uint64_t NextCUOffset = getNextUnitOffset(); 384 DWARFDebugInfoEntry DIE; 385 DWARFDataExtractor DebugInfoData = getDebugInfoExtractor(); 386 uint32_t Depth = 0; 387 bool IsCUDie = true; 388 389 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset, 390 Depth)) { 391 if (IsCUDie) { 392 if (AppendCUDie) 393 Dies.push_back(DIE); 394 if (!AppendNonCUDies) 395 break; 396 // The average bytes per DIE entry has been seen to be 397 // around 14-20 so let's pre-reserve the needed memory for 398 // our DIE entries accordingly. 399 Dies.reserve(Dies.size() + getDebugInfoSize() / 14); 400 IsCUDie = false; 401 } else { 402 Dies.push_back(DIE); 403 } 404 405 if (const DWARFAbbreviationDeclaration *AbbrDecl = 406 DIE.getAbbreviationDeclarationPtr()) { 407 // Normal DIE 408 if (AbbrDecl->hasChildren()) 409 ++Depth; 410 } else { 411 // NULL DIE. 412 if (Depth > 0) 413 --Depth; 414 if (Depth == 0) 415 break; // We are done with this compile unit! 416 } 417 } 418 419 // Give a little bit of info if we encounter corrupt DWARF (our offset 420 // should always terminate at or before the start of the next compilation 421 // unit header). 422 if (DIEOffset > NextCUOffset) 423 WithColor::warning() << format("DWARF compile unit extends beyond its " 424 "bounds cu 0x%8.8" PRIx64 " " 425 "at 0x%8.8" PRIx64 "\n", 426 getOffset(), DIEOffset); 427 } 428 429 void DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) { 430 if (Error e = tryExtractDIEsIfNeeded(CUDieOnly)) 431 WithColor::error() << toString(std::move(e)); 432 } 433 434 Error DWARFUnit::tryExtractDIEsIfNeeded(bool CUDieOnly) { 435 if ((CUDieOnly && !DieArray.empty()) || 436 DieArray.size() > 1) 437 return Error::success(); // Already parsed. 438 439 bool HasCUDie = !DieArray.empty(); 440 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray); 441 442 if (DieArray.empty()) 443 return Error::success(); 444 445 // If CU DIE was just parsed, copy several attribute values from it. 446 if (HasCUDie) 447 return Error::success(); 448 449 DWARFDie UnitDie(this, &DieArray[0]); 450 if (Optional<uint64_t> DWOId = toUnsigned(UnitDie.find(DW_AT_GNU_dwo_id))) 451 Header.setDWOId(*DWOId); 452 if (!IsDWO) { 453 assert(AddrOffsetSectionBase == None); 454 assert(RangeSectionBase == 0); 455 assert(LocSectionBase == 0); 456 AddrOffsetSectionBase = toSectionOffset(UnitDie.find(DW_AT_addr_base)); 457 if (!AddrOffsetSectionBase) 458 AddrOffsetSectionBase = 459 toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base)); 460 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0); 461 LocSectionBase = toSectionOffset(UnitDie.find(DW_AT_loclists_base), 0); 462 } 463 464 // In general, in DWARF v5 and beyond we derive the start of the unit's 465 // contribution to the string offsets table from the unit DIE's 466 // DW_AT_str_offsets_base attribute. Split DWARF units do not use this 467 // attribute, so we assume that there is a contribution to the string 468 // offsets table starting at offset 0 of the debug_str_offsets.dwo section. 469 // In both cases we need to determine the format of the contribution, 470 // which may differ from the unit's format. 471 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection, 472 isLittleEndian, 0); 473 if (IsDWO || getVersion() >= 5) { 474 auto StringOffsetOrError = 475 IsDWO ? determineStringOffsetsTableContributionDWO(DA) 476 : determineStringOffsetsTableContribution(DA); 477 if (!StringOffsetOrError) 478 return createStringError(errc::invalid_argument, 479 "invalid reference to or invalid content in " 480 ".debug_str_offsets[.dwo]: " + 481 toString(StringOffsetOrError.takeError())); 482 483 StringOffsetsTableContribution = *StringOffsetOrError; 484 } 485 486 // DWARF v5 uses the .debug_rnglists and .debug_rnglists.dwo sections to 487 // describe address ranges. 488 if (getVersion() >= 5) { 489 if (IsDWO) 490 setRangesSection(&Context.getDWARFObj().getRnglistsDWOSection(), 0); 491 else 492 setRangesSection(&Context.getDWARFObj().getRnglistsSection(), 493 toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0)); 494 if (RangeSection->Data.size()) { 495 // Parse the range list table header. Individual range lists are 496 // extracted lazily. 497 DWARFDataExtractor RangesDA(Context.getDWARFObj(), *RangeSection, 498 isLittleEndian, 0); 499 auto TableOrError = parseListTableHeader<DWARFDebugRnglistTable>( 500 RangesDA, RangeSectionBase, Header.getFormat()); 501 if (!TableOrError) 502 return createStringError(errc::invalid_argument, 503 "parsing a range list table: " + 504 toString(TableOrError.takeError())); 505 506 RngListTable = TableOrError.get(); 507 508 // In a split dwarf unit, there is no DW_AT_rnglists_base attribute. 509 // Adjust RangeSectionBase to point past the table header. 510 if (IsDWO && RngListTable) 511 RangeSectionBase = RngListTable->getHeaderSize(); 512 } 513 514 // In a split dwarf unit, there is no DW_AT_loclists_base attribute. 515 // Setting LocSectionBase to point past the table header. 516 if (IsDWO) 517 setLocSection(&Context.getDWARFObj().getLoclistsDWOSection(), 518 DWARFListTableHeader::getHeaderSize(Header.getFormat())); 519 else 520 setLocSection(&Context.getDWARFObj().getLoclistsSection(), 521 toSectionOffset(UnitDie.find(DW_AT_loclists_base), 0)); 522 523 if (LocSection->Data.size()) { 524 if (IsDWO) 525 LoclistTableHeader.emplace(".debug_loclists.dwo", "locations"); 526 else 527 LoclistTableHeader.emplace(".debug_loclists", "locations"); 528 529 uint64_t HeaderSize = DWARFListTableHeader::getHeaderSize(Header.getFormat()); 530 uint64_t Offset = getLocSectionBase(); 531 DWARFDataExtractor Data(Context.getDWARFObj(), *LocSection, 532 isLittleEndian, getAddressByteSize()); 533 if (Offset < HeaderSize) 534 return createStringError(errc::invalid_argument, 535 "did not detect a valid" 536 " list table with base = 0x%" PRIx64 "\n", 537 Offset); 538 Offset -= HeaderSize; 539 if (Error E = LoclistTableHeader->extract(Data, &Offset)) 540 return createStringError(errc::invalid_argument, 541 "parsing a loclist table: " + 542 toString(std::move(E))); 543 } 544 } 545 546 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for 547 // skeleton CU DIE, so that DWARF users not aware of it are not broken. 548 return Error::success(); 549 } 550 551 bool DWARFUnit::parseDWO() { 552 if (IsDWO) 553 return false; 554 if (DWO.get()) 555 return false; 556 DWARFDie UnitDie = getUnitDIE(); 557 if (!UnitDie) 558 return false; 559 auto DWOFileName = getVersion() >= 5 560 ? dwarf::toString(UnitDie.find(DW_AT_dwo_name)) 561 : dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name)); 562 if (!DWOFileName) 563 return false; 564 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir)); 565 SmallString<16> AbsolutePath; 566 if (sys::path::is_relative(*DWOFileName) && CompilationDir && 567 *CompilationDir) { 568 sys::path::append(AbsolutePath, *CompilationDir); 569 } 570 sys::path::append(AbsolutePath, *DWOFileName); 571 auto DWOId = getDWOId(); 572 if (!DWOId) 573 return false; 574 auto DWOContext = Context.getDWOContext(AbsolutePath); 575 if (!DWOContext) 576 return false; 577 578 DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId); 579 if (!DWOCU) 580 return false; 581 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU); 582 // Share .debug_addr and .debug_ranges section with compile unit in .dwo 583 if (AddrOffsetSectionBase) 584 DWO->setAddrOffsetSection(AddrOffsetSection, *AddrOffsetSectionBase); 585 if (getVersion() >= 5) { 586 DWO->setRangesSection(&Context.getDWARFObj().getRnglistsDWOSection(), 0); 587 DWARFDataExtractor RangesDA(Context.getDWARFObj(), *RangeSection, 588 isLittleEndian, 0); 589 if (auto TableOrError = parseListTableHeader<DWARFDebugRnglistTable>( 590 RangesDA, RangeSectionBase, Header.getFormat())) 591 DWO->RngListTable = TableOrError.get(); 592 else 593 WithColor::error() << "parsing a range list table: " 594 << toString(TableOrError.takeError()) 595 << '\n'; 596 if (DWO->RngListTable) 597 DWO->RangeSectionBase = DWO->RngListTable->getHeaderSize(); 598 } else { 599 auto DWORangesBase = UnitDie.getRangesBaseAttribute(); 600 DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0); 601 } 602 603 return true; 604 } 605 606 void DWARFUnit::clearDIEs(bool KeepCUDie) { 607 if (DieArray.size() > (unsigned)KeepCUDie) { 608 DieArray.resize((unsigned)KeepCUDie); 609 DieArray.shrink_to_fit(); 610 } 611 } 612 613 Expected<DWARFAddressRangesVector> 614 DWARFUnit::findRnglistFromOffset(uint64_t Offset) { 615 if (getVersion() <= 4) { 616 DWARFDebugRangeList RangeList; 617 if (Error E = extractRangeList(Offset, RangeList)) 618 return std::move(E); 619 return RangeList.getAbsoluteRanges(getBaseAddress()); 620 } 621 if (RngListTable) { 622 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection, 623 isLittleEndian, RngListTable->getAddrSize()); 624 auto RangeListOrError = RngListTable->findList(RangesData, Offset); 625 if (RangeListOrError) 626 return RangeListOrError.get().getAbsoluteRanges(getBaseAddress(), *this); 627 return RangeListOrError.takeError(); 628 } 629 630 return createStringError(errc::invalid_argument, 631 "missing or invalid range list table"); 632 } 633 634 Expected<DWARFAddressRangesVector> 635 DWARFUnit::findRnglistFromIndex(uint32_t Index) { 636 if (auto Offset = getRnglistOffset(Index)) 637 return findRnglistFromOffset(*Offset); 638 639 if (RngListTable) 640 return createStringError(errc::invalid_argument, 641 "invalid range list table index %d", Index); 642 643 return createStringError(errc::invalid_argument, 644 "missing or invalid range list table"); 645 } 646 647 Expected<DWARFAddressRangesVector> DWARFUnit::collectAddressRanges() { 648 DWARFDie UnitDie = getUnitDIE(); 649 if (!UnitDie) 650 return createStringError(errc::invalid_argument, "No unit DIE"); 651 652 // First, check if unit DIE describes address ranges for the whole unit. 653 auto CUDIERangesOrError = UnitDie.getAddressRanges(); 654 if (!CUDIERangesOrError) 655 return createStringError(errc::invalid_argument, 656 "decoding address ranges: %s", 657 toString(CUDIERangesOrError.takeError()).c_str()); 658 return *CUDIERangesOrError; 659 } 660 661 Expected<DWARFLocationExpressionsVector> 662 DWARFUnit::findLoclistFromOffset(uint64_t Offset) { 663 DWARFLocationExpressionsVector Result; 664 665 Error InterpretationError = Error::success(); 666 667 Error ParseError = getLocationTable().visitAbsoluteLocationList( 668 Offset, getBaseAddress(), 669 [this](uint32_t Index) { return getAddrOffsetSectionItem(Index); }, 670 [&](Expected<DWARFLocationExpression> L) { 671 if (L) 672 Result.push_back(std::move(*L)); 673 else 674 InterpretationError = 675 joinErrors(L.takeError(), std::move(InterpretationError)); 676 return !InterpretationError; 677 }); 678 679 if (ParseError || InterpretationError) 680 return joinErrors(std::move(ParseError), std::move(InterpretationError)); 681 682 return Result; 683 } 684 685 void DWARFUnit::updateAddressDieMap(DWARFDie Die) { 686 if (Die.isSubroutineDIE()) { 687 auto DIERangesOrError = Die.getAddressRanges(); 688 if (DIERangesOrError) { 689 for (const auto &R : DIERangesOrError.get()) { 690 // Ignore 0-sized ranges. 691 if (R.LowPC == R.HighPC) 692 continue; 693 auto B = AddrDieMap.upper_bound(R.LowPC); 694 if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) { 695 // The range is a sub-range of existing ranges, we need to split the 696 // existing range. 697 if (R.HighPC < B->second.first) 698 AddrDieMap[R.HighPC] = B->second; 699 if (R.LowPC > B->first) 700 AddrDieMap[B->first].first = R.LowPC; 701 } 702 AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die); 703 } 704 } else 705 llvm::consumeError(DIERangesOrError.takeError()); 706 } 707 // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to 708 // simplify the logic to update AddrDieMap. The child's range will always 709 // be equal or smaller than the parent's range. With this assumption, when 710 // adding one range into the map, it will at most split a range into 3 711 // sub-ranges. 712 for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling()) 713 updateAddressDieMap(Child); 714 } 715 716 DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) { 717 extractDIEsIfNeeded(false); 718 if (AddrDieMap.empty()) 719 updateAddressDieMap(getUnitDIE()); 720 auto R = AddrDieMap.upper_bound(Address); 721 if (R == AddrDieMap.begin()) 722 return DWARFDie(); 723 // upper_bound's previous item contains Address. 724 --R; 725 if (Address >= R->second.first) 726 return DWARFDie(); 727 return R->second.second; 728 } 729 730 void 731 DWARFUnit::getInlinedChainForAddress(uint64_t Address, 732 SmallVectorImpl<DWARFDie> &InlinedChain) { 733 assert(InlinedChain.empty()); 734 // Try to look for subprogram DIEs in the DWO file. 735 parseDWO(); 736 // First, find the subroutine that contains the given address (the leaf 737 // of inlined chain). 738 DWARFDie SubroutineDIE = 739 (DWO ? *DWO : *this).getSubroutineForAddress(Address); 740 741 if (!SubroutineDIE) 742 return; 743 744 while (!SubroutineDIE.isSubprogramDIE()) { 745 if (SubroutineDIE.getTag() == DW_TAG_inlined_subroutine) 746 InlinedChain.push_back(SubroutineDIE); 747 SubroutineDIE = SubroutineDIE.getParent(); 748 } 749 InlinedChain.push_back(SubroutineDIE); 750 } 751 752 const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context, 753 DWARFSectionKind Kind) { 754 if (Kind == DW_SECT_INFO) 755 return Context.getCUIndex(); 756 assert(Kind == DW_SECT_TYPES); 757 return Context.getTUIndex(); 758 } 759 760 DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) { 761 if (!Die) 762 return DWARFDie(); 763 const uint32_t Depth = Die->getDepth(); 764 // Unit DIEs always have a depth of zero and never have parents. 765 if (Depth == 0) 766 return DWARFDie(); 767 // Depth of 1 always means parent is the compile/type unit. 768 if (Depth == 1) 769 return getUnitDIE(); 770 // Look for previous DIE with a depth that is one less than the Die's depth. 771 const uint32_t ParentDepth = Depth - 1; 772 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) { 773 if (DieArray[I].getDepth() == ParentDepth) 774 return DWARFDie(this, &DieArray[I]); 775 } 776 return DWARFDie(); 777 } 778 779 DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) { 780 if (!Die) 781 return DWARFDie(); 782 uint32_t Depth = Die->getDepth(); 783 // Unit DIEs always have a depth of zero and never have siblings. 784 if (Depth == 0) 785 return DWARFDie(); 786 // NULL DIEs don't have siblings. 787 if (Die->getAbbreviationDeclarationPtr() == nullptr) 788 return DWARFDie(); 789 790 // Find the next DIE whose depth is the same as the Die's depth. 791 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx; 792 ++I) { 793 if (DieArray[I].getDepth() == Depth) 794 return DWARFDie(this, &DieArray[I]); 795 } 796 return DWARFDie(); 797 } 798 799 DWARFDie DWARFUnit::getPreviousSibling(const DWARFDebugInfoEntry *Die) { 800 if (!Die) 801 return DWARFDie(); 802 uint32_t Depth = Die->getDepth(); 803 // Unit DIEs always have a depth of zero and never have siblings. 804 if (Depth == 0) 805 return DWARFDie(); 806 807 // Find the previous DIE whose depth is the same as the Die's depth. 808 for (size_t I = getDIEIndex(Die); I > 0;) { 809 --I; 810 if (DieArray[I].getDepth() == Depth - 1) 811 return DWARFDie(); 812 if (DieArray[I].getDepth() == Depth) 813 return DWARFDie(this, &DieArray[I]); 814 } 815 return DWARFDie(); 816 } 817 818 DWARFDie DWARFUnit::getFirstChild(const DWARFDebugInfoEntry *Die) { 819 if (!Die->hasChildren()) 820 return DWARFDie(); 821 822 // We do not want access out of bounds when parsing corrupted debug data. 823 size_t I = getDIEIndex(Die) + 1; 824 if (I >= DieArray.size()) 825 return DWARFDie(); 826 return DWARFDie(this, &DieArray[I]); 827 } 828 829 DWARFDie DWARFUnit::getLastChild(const DWARFDebugInfoEntry *Die) { 830 if (!Die->hasChildren()) 831 return DWARFDie(); 832 833 uint32_t Depth = Die->getDepth(); 834 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx; 835 ++I) { 836 if (DieArray[I].getDepth() == Depth + 1 && 837 DieArray[I].getTag() == dwarf::DW_TAG_null) 838 return DWARFDie(this, &DieArray[I]); 839 assert(DieArray[I].getDepth() > Depth && "Not processing children?"); 840 } 841 return DWARFDie(); 842 } 843 844 const DWARFAbbreviationDeclarationSet *DWARFUnit::getAbbreviations() const { 845 if (!Abbrevs) 846 Abbrevs = Abbrev->getAbbreviationDeclarationSet(Header.getAbbrOffset()); 847 return Abbrevs; 848 } 849 850 llvm::Optional<object::SectionedAddress> DWARFUnit::getBaseAddress() { 851 if (BaseAddr) 852 return BaseAddr; 853 854 DWARFDie UnitDie = getUnitDIE(); 855 Optional<DWARFFormValue> PC = UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc}); 856 BaseAddr = toSectionedAddress(PC); 857 return BaseAddr; 858 } 859 860 Expected<StrOffsetsContributionDescriptor> 861 StrOffsetsContributionDescriptor::validateContributionSize( 862 DWARFDataExtractor &DA) { 863 uint8_t EntrySize = getDwarfOffsetByteSize(); 864 // In order to ensure that we don't read a partial record at the end of 865 // the section we validate for a multiple of the entry size. 866 uint64_t ValidationSize = alignTo(Size, EntrySize); 867 // Guard against overflow. 868 if (ValidationSize >= Size) 869 if (DA.isValidOffsetForDataOfSize((uint32_t)Base, ValidationSize)) 870 return *this; 871 return createStringError(errc::invalid_argument, "length exceeds section size"); 872 } 873 874 // Look for a DWARF64-formatted contribution to the string offsets table 875 // starting at a given offset and record it in a descriptor. 876 static Expected<StrOffsetsContributionDescriptor> 877 parseDWARF64StringOffsetsTableHeader(DWARFDataExtractor &DA, uint64_t Offset) { 878 if (!DA.isValidOffsetForDataOfSize(Offset, 16)) 879 return createStringError(errc::invalid_argument, "section offset exceeds section size"); 880 881 if (DA.getU32(&Offset) != dwarf::DW_LENGTH_DWARF64) 882 return createStringError(errc::invalid_argument, "32 bit contribution referenced from a 64 bit unit"); 883 884 uint64_t Size = DA.getU64(&Offset); 885 uint8_t Version = DA.getU16(&Offset); 886 (void)DA.getU16(&Offset); // padding 887 // The encoded length includes the 2-byte version field and the 2-byte 888 // padding, so we need to subtract them out when we populate the descriptor. 889 return StrOffsetsContributionDescriptor(Offset, Size - 4, Version, DWARF64); 890 } 891 892 // Look for a DWARF32-formatted contribution to the string offsets table 893 // starting at a given offset and record it in a descriptor. 894 static Expected<StrOffsetsContributionDescriptor> 895 parseDWARF32StringOffsetsTableHeader(DWARFDataExtractor &DA, uint64_t Offset) { 896 if (!DA.isValidOffsetForDataOfSize(Offset, 8)) 897 return createStringError(errc::invalid_argument, "section offset exceeds section size"); 898 899 uint32_t ContributionSize = DA.getU32(&Offset); 900 if (ContributionSize >= dwarf::DW_LENGTH_lo_reserved) 901 return createStringError(errc::invalid_argument, "invalid length"); 902 903 uint8_t Version = DA.getU16(&Offset); 904 (void)DA.getU16(&Offset); // padding 905 // The encoded length includes the 2-byte version field and the 2-byte 906 // padding, so we need to subtract them out when we populate the descriptor. 907 return StrOffsetsContributionDescriptor(Offset, ContributionSize - 4, Version, 908 DWARF32); 909 } 910 911 static Expected<StrOffsetsContributionDescriptor> 912 parseDWARFStringOffsetsTableHeader(DWARFDataExtractor &DA, 913 llvm::dwarf::DwarfFormat Format, 914 uint64_t Offset) { 915 StrOffsetsContributionDescriptor Desc; 916 switch (Format) { 917 case dwarf::DwarfFormat::DWARF64: { 918 if (Offset < 16) 919 return createStringError(errc::invalid_argument, "insufficient space for 64 bit header prefix"); 920 auto DescOrError = parseDWARF64StringOffsetsTableHeader(DA, Offset - 16); 921 if (!DescOrError) 922 return DescOrError.takeError(); 923 Desc = *DescOrError; 924 break; 925 } 926 case dwarf::DwarfFormat::DWARF32: { 927 if (Offset < 8) 928 return createStringError(errc::invalid_argument, "insufficient space for 32 bit header prefix"); 929 auto DescOrError = parseDWARF32StringOffsetsTableHeader(DA, Offset - 8); 930 if (!DescOrError) 931 return DescOrError.takeError(); 932 Desc = *DescOrError; 933 break; 934 } 935 } 936 return Desc.validateContributionSize(DA); 937 } 938 939 Expected<Optional<StrOffsetsContributionDescriptor>> 940 DWARFUnit::determineStringOffsetsTableContribution(DWARFDataExtractor &DA) { 941 uint64_t Offset; 942 if (IsDWO) { 943 Offset = 0; 944 if (DA.getData().data() == nullptr) 945 return None; 946 } else { 947 auto OptOffset = toSectionOffset(getUnitDIE().find(DW_AT_str_offsets_base)); 948 if (!OptOffset) 949 return None; 950 Offset = *OptOffset; 951 } 952 auto DescOrError = parseDWARFStringOffsetsTableHeader(DA, Header.getFormat(), Offset); 953 if (!DescOrError) 954 return DescOrError.takeError(); 955 return *DescOrError; 956 } 957 958 Expected<Optional<StrOffsetsContributionDescriptor>> 959 DWARFUnit::determineStringOffsetsTableContributionDWO(DWARFDataExtractor & DA) { 960 uint64_t Offset = 0; 961 auto IndexEntry = Header.getIndexEntry(); 962 const auto *C = 963 IndexEntry ? IndexEntry->getOffset(DW_SECT_STR_OFFSETS) : nullptr; 964 if (C) 965 Offset = C->Offset; 966 if (getVersion() >= 5) { 967 if (DA.getData().data() == nullptr) 968 return None; 969 Offset += Header.getFormat() == dwarf::DwarfFormat::DWARF32 ? 8 : 16; 970 // Look for a valid contribution at the given offset. 971 auto DescOrError = parseDWARFStringOffsetsTableHeader(DA, Header.getFormat(), Offset); 972 if (!DescOrError) 973 return DescOrError.takeError(); 974 return *DescOrError; 975 } 976 // Prior to DWARF v5, we derive the contribution size from the 977 // index table (in a package file). In a .dwo file it is simply 978 // the length of the string offsets section. 979 if (!IndexEntry) 980 return { 981 Optional<StrOffsetsContributionDescriptor>( 982 {0, StringOffsetSection.Data.size(), 4, DWARF32})}; 983 if (C) 984 return {Optional<StrOffsetsContributionDescriptor>( 985 {C->Offset, C->Length, 4, DWARF32})}; 986 return None; 987 } 988