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/BinaryFormat/Dwarf.h" 13 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h" 14 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" 15 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 16 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" 17 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h" 18 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h" 19 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" 20 #include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h" 21 #include "llvm/DebugInfo/DWARF/DWARFDie.h" 22 #include "llvm/DebugInfo/DWARF/DWARFExpression.h" 23 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 24 #include "llvm/DebugInfo/DWARF/DWARFListTable.h" 25 #include "llvm/DebugInfo/DWARF/DWARFObject.h" 26 #include "llvm/DebugInfo/DWARF/DWARFSection.h" 27 #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h" 28 #include "llvm/Object/ObjectFile.h" 29 #include "llvm/Support/DataExtractor.h" 30 #include "llvm/Support/Errc.h" 31 #include "llvm/Support/Path.h" 32 #include <algorithm> 33 #include <cassert> 34 #include <cstddef> 35 #include <cstdint> 36 #include <utility> 37 #include <vector> 38 39 using namespace llvm; 40 using namespace dwarf; 41 42 void DWARFUnitVector::addUnitsForSection(DWARFContext &C, 43 const DWARFSection &Section, 44 DWARFSectionKind SectionKind) { 45 const DWARFObject &D = C.getDWARFObj(); 46 addUnitsImpl(C, D, Section, C.getDebugAbbrev(), &D.getRangesSection(), 47 &D.getLocSection(), D.getStrSection(), 48 D.getStrOffsetsSection(), &D.getAddrSection(), 49 D.getLineSection(), D.isLittleEndian(), false, false, 50 SectionKind); 51 } 52 53 void DWARFUnitVector::addUnitsForDWOSection(DWARFContext &C, 54 const DWARFSection &DWOSection, 55 DWARFSectionKind SectionKind, 56 bool Lazy) { 57 const DWARFObject &D = C.getDWARFObj(); 58 addUnitsImpl(C, D, DWOSection, C.getDebugAbbrevDWO(), &D.getRangesDWOSection(), 59 &D.getLocDWOSection(), D.getStrDWOSection(), 60 D.getStrOffsetsDWOSection(), &D.getAddrSection(), 61 D.getLineDWOSection(), C.isLittleEndian(), true, Lazy, 62 SectionKind); 63 } 64 65 void DWARFUnitVector::addUnitsImpl( 66 DWARFContext &Context, const DWARFObject &Obj, const DWARFSection &Section, 67 const DWARFDebugAbbrev *DA, const DWARFSection *RS, 68 const DWARFSection *LocSection, StringRef SS, const DWARFSection &SOS, 69 const DWARFSection *AOS, const DWARFSection &LS, bool LE, bool IsDWO, 70 bool Lazy, DWARFSectionKind SectionKind) { 71 DWARFDataExtractor Data(Obj, Section, LE, 0); 72 // Lazy initialization of Parser, now that we have all section info. 73 if (!Parser) { 74 Parser = [=, &Context, &Obj, &Section, &SOS, 75 &LS](uint64_t Offset, DWARFSectionKind SectionKind, 76 const DWARFSection *CurSection, 77 const DWARFUnitIndex::Entry *IndexEntry) 78 -> std::unique_ptr<DWARFUnit> { 79 const DWARFSection &InfoSection = CurSection ? *CurSection : Section; 80 DWARFDataExtractor Data(Obj, InfoSection, LE, 0); 81 if (!Data.isValidOffset(Offset)) 82 return nullptr; 83 DWARFUnitHeader Header; 84 if (!Header.extract(Context, Data, &Offset, SectionKind)) 85 return nullptr; 86 if (!IndexEntry && IsDWO) { 87 const DWARFUnitIndex &Index = getDWARFUnitIndex( 88 Context, Header.isTypeUnit() ? DW_SECT_EXT_TYPES : DW_SECT_INFO); 89 if (Index) { 90 if (Header.isTypeUnit()) 91 IndexEntry = Index.getFromHash(Header.getTypeHash()); 92 else if (auto DWOId = Header.getDWOId()) 93 IndexEntry = Index.getFromHash(*DWOId); 94 } 95 if (!IndexEntry) 96 IndexEntry = Index.getFromOffset(Header.getOffset()); 97 } 98 if (IndexEntry && !Header.applyIndexEntry(IndexEntry)) 99 return nullptr; 100 std::unique_ptr<DWARFUnit> U; 101 if (Header.isTypeUnit()) 102 U = std::make_unique<DWARFTypeUnit>(Context, InfoSection, Header, DA, 103 RS, LocSection, SS, SOS, AOS, LS, 104 LE, IsDWO, *this); 105 else 106 U = std::make_unique<DWARFCompileUnit>(Context, InfoSection, Header, 107 DA, RS, LocSection, SS, SOS, 108 AOS, LS, LE, IsDWO, *this); 109 return U; 110 }; 111 } 112 if (Lazy) 113 return; 114 // Find a reasonable insertion point within the vector. We skip over 115 // (a) units from a different section, (b) units from the same section 116 // but with lower offset-within-section. This keeps units in order 117 // within a section, although not necessarily within the object file, 118 // even if we do lazy parsing. 119 auto I = this->begin(); 120 uint64_t Offset = 0; 121 while (Data.isValidOffset(Offset)) { 122 if (I != this->end() && 123 (&(*I)->getInfoSection() != &Section || (*I)->getOffset() == Offset)) { 124 ++I; 125 continue; 126 } 127 auto U = Parser(Offset, SectionKind, &Section, nullptr); 128 // If parsing failed, we're done with this section. 129 if (!U) 130 break; 131 Offset = U->getNextUnitOffset(); 132 I = std::next(this->insert(I, std::move(U))); 133 } 134 } 135 136 DWARFUnit *DWARFUnitVector::addUnit(std::unique_ptr<DWARFUnit> Unit) { 137 auto I = std::upper_bound(begin(), end(), Unit, 138 [](const std::unique_ptr<DWARFUnit> &LHS, 139 const std::unique_ptr<DWARFUnit> &RHS) { 140 return LHS->getOffset() < RHS->getOffset(); 141 }); 142 return this->insert(I, std::move(Unit))->get(); 143 } 144 145 DWARFUnit *DWARFUnitVector::getUnitForOffset(uint64_t Offset) const { 146 auto end = begin() + getNumInfoUnits(); 147 auto *CU = 148 std::upper_bound(begin(), end, Offset, 149 [](uint64_t LHS, const std::unique_ptr<DWARFUnit> &RHS) { 150 return LHS < RHS->getNextUnitOffset(); 151 }); 152 if (CU != end && (*CU)->getOffset() <= Offset) 153 return CU->get(); 154 return nullptr; 155 } 156 157 DWARFUnit * 158 DWARFUnitVector::getUnitForIndexEntry(const DWARFUnitIndex::Entry &E) { 159 const auto *CUOff = E.getContribution(DW_SECT_INFO); 160 if (!CUOff) 161 return nullptr; 162 163 auto Offset = CUOff->Offset; 164 auto end = begin() + getNumInfoUnits(); 165 166 auto *CU = 167 std::upper_bound(begin(), end, CUOff->Offset, 168 [](uint64_t LHS, const std::unique_ptr<DWARFUnit> &RHS) { 169 return LHS < RHS->getNextUnitOffset(); 170 }); 171 if (CU != end && (*CU)->getOffset() <= Offset) 172 return CU->get(); 173 174 if (!Parser) 175 return nullptr; 176 177 auto U = Parser(Offset, DW_SECT_INFO, nullptr, &E); 178 if (!U) 179 U = nullptr; 180 181 auto *NewCU = U.get(); 182 this->insert(CU, std::move(U)); 183 ++NumInfoUnits; 184 return NewCU; 185 } 186 187 DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section, 188 const DWARFUnitHeader &Header, const DWARFDebugAbbrev *DA, 189 const DWARFSection *RS, const DWARFSection *LocSection, 190 StringRef SS, const DWARFSection &SOS, 191 const DWARFSection *AOS, const DWARFSection &LS, bool LE, 192 bool IsDWO, const DWARFUnitVector &UnitVector) 193 : Context(DC), InfoSection(Section), Header(Header), Abbrev(DA), 194 RangeSection(RS), LineSection(LS), StringSection(SS), 195 StringOffsetSection(SOS), AddrOffsetSection(AOS), isLittleEndian(LE), 196 IsDWO(IsDWO), UnitVector(UnitVector) { 197 clear(); 198 } 199 200 DWARFUnit::~DWARFUnit() = default; 201 202 DWARFDataExtractor DWARFUnit::getDebugInfoExtractor() const { 203 return DWARFDataExtractor(Context.getDWARFObj(), InfoSection, isLittleEndian, 204 getAddressByteSize()); 205 } 206 207 Optional<object::SectionedAddress> 208 DWARFUnit::getAddrOffsetSectionItem(uint32_t Index) const { 209 if (!AddrOffsetSectionBase) { 210 auto R = Context.info_section_units(); 211 // Surprising if a DWO file has more than one skeleton unit in it - this 212 // probably shouldn't be valid, but if a use case is found, here's where to 213 // support it (probably have to linearly search for the matching skeleton CU 214 // here) 215 if (IsDWO && hasSingleElement(R)) 216 return (*R.begin())->getAddrOffsetSectionItem(Index); 217 218 return None; 219 } 220 221 uint64_t Offset = *AddrOffsetSectionBase + Index * getAddressByteSize(); 222 if (AddrOffsetSection->Data.size() < Offset + getAddressByteSize()) 223 return None; 224 DWARFDataExtractor DA(Context.getDWARFObj(), *AddrOffsetSection, 225 isLittleEndian, getAddressByteSize()); 226 uint64_t Section; 227 uint64_t Address = DA.getRelocatedAddress(&Offset, &Section); 228 return {{Address, Section}}; 229 } 230 231 Expected<uint64_t> DWARFUnit::getStringOffsetSectionItem(uint32_t Index) const { 232 if (!StringOffsetsTableContribution) 233 return make_error<StringError>( 234 "DW_FORM_strx used without a valid string offsets table", 235 inconvertibleErrorCode()); 236 unsigned ItemSize = getDwarfStringOffsetsByteSize(); 237 uint64_t Offset = getStringOffsetsBase() + Index * ItemSize; 238 if (StringOffsetSection.Data.size() < Offset + ItemSize) 239 return make_error<StringError>("DW_FORM_strx uses index " + Twine(Index) + 240 ", which is too large", 241 inconvertibleErrorCode()); 242 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection, 243 isLittleEndian, 0); 244 return DA.getRelocatedValue(ItemSize, &Offset); 245 } 246 247 bool DWARFUnitHeader::extract(DWARFContext &Context, 248 const DWARFDataExtractor &debug_info, 249 uint64_t *offset_ptr, 250 DWARFSectionKind SectionKind) { 251 Offset = *offset_ptr; 252 Error Err = Error::success(); 253 IndexEntry = nullptr; 254 std::tie(Length, FormParams.Format) = 255 debug_info.getInitialLength(offset_ptr, &Err); 256 FormParams.Version = debug_info.getU16(offset_ptr, &Err); 257 if (FormParams.Version >= 5) { 258 UnitType = debug_info.getU8(offset_ptr, &Err); 259 FormParams.AddrSize = debug_info.getU8(offset_ptr, &Err); 260 AbbrOffset = debug_info.getRelocatedValue( 261 FormParams.getDwarfOffsetByteSize(), offset_ptr, nullptr, &Err); 262 } else { 263 AbbrOffset = debug_info.getRelocatedValue( 264 FormParams.getDwarfOffsetByteSize(), offset_ptr, nullptr, &Err); 265 FormParams.AddrSize = debug_info.getU8(offset_ptr, &Err); 266 // Fake a unit type based on the section type. This isn't perfect, 267 // but distinguishing compile and type units is generally enough. 268 if (SectionKind == DW_SECT_EXT_TYPES) 269 UnitType = DW_UT_type; 270 else 271 UnitType = DW_UT_compile; 272 } 273 if (isTypeUnit()) { 274 TypeHash = debug_info.getU64(offset_ptr, &Err); 275 TypeOffset = debug_info.getUnsigned( 276 offset_ptr, FormParams.getDwarfOffsetByteSize(), &Err); 277 } else if (UnitType == DW_UT_split_compile || UnitType == DW_UT_skeleton) 278 DWOId = debug_info.getU64(offset_ptr, &Err); 279 280 if (Err) { 281 Context.getWarningHandler()(joinErrors( 282 createStringError( 283 errc::invalid_argument, 284 "DWARF unit at 0x%8.8" PRIx64 " cannot be parsed:", Offset), 285 std::move(Err))); 286 return false; 287 } 288 289 // Header fields all parsed, capture the size of this unit header. 290 assert(*offset_ptr - Offset <= 255 && "unexpected header size"); 291 Size = uint8_t(*offset_ptr - Offset); 292 uint64_t NextCUOffset = Offset + getUnitLengthFieldByteSize() + getLength(); 293 294 if (!debug_info.isValidOffset(getNextUnitOffset() - 1)) { 295 Context.getWarningHandler()( 296 createStringError(errc::invalid_argument, 297 "DWARF unit from offset 0x%8.8" PRIx64 " incl. " 298 "to offset 0x%8.8" PRIx64 " excl. " 299 "extends past section size 0x%8.8zx", 300 Offset, NextCUOffset, debug_info.size())); 301 return false; 302 } 303 304 if (!DWARFContext::isSupportedVersion(getVersion())) { 305 Context.getWarningHandler()(createStringError( 306 errc::invalid_argument, 307 "DWARF unit at offset 0x%8.8" PRIx64 " " 308 "has unsupported version %" PRIu16 ", supported are 2-%u", 309 Offset, getVersion(), DWARFContext::getMaxSupportedVersion())); 310 return false; 311 } 312 313 // Type offset is unit-relative; should be after the header and before 314 // the end of the current unit. 315 if (isTypeUnit() && TypeOffset < Size) { 316 Context.getWarningHandler()( 317 createStringError(errc::invalid_argument, 318 "DWARF type unit at offset " 319 "0x%8.8" PRIx64 " " 320 "has its relocated type_offset 0x%8.8" PRIx64 " " 321 "pointing inside the header", 322 Offset, Offset + TypeOffset)); 323 return false; 324 } 325 if (isTypeUnit() && 326 TypeOffset >= getUnitLengthFieldByteSize() + getLength()) { 327 Context.getWarningHandler()(createStringError( 328 errc::invalid_argument, 329 "DWARF type unit from offset 0x%8.8" PRIx64 " incl. " 330 "to offset 0x%8.8" PRIx64 " excl. has its " 331 "relocated type_offset 0x%8.8" PRIx64 " pointing past the unit end", 332 Offset, NextCUOffset, Offset + TypeOffset)); 333 return false; 334 } 335 336 if (Error SizeErr = DWARFContext::checkAddressSizeSupported( 337 getAddressByteSize(), errc::invalid_argument, 338 "DWARF unit at offset 0x%8.8" PRIx64, Offset)) { 339 Context.getWarningHandler()(std::move(SizeErr)); 340 return false; 341 } 342 343 // Keep track of the highest DWARF version we encounter across all units. 344 Context.setMaxVersionIfGreater(getVersion()); 345 return true; 346 } 347 348 bool DWARFUnitHeader::applyIndexEntry(const DWARFUnitIndex::Entry *Entry) { 349 assert(Entry); 350 assert(!IndexEntry); 351 IndexEntry = Entry; 352 if (AbbrOffset) 353 return false; 354 auto *UnitContrib = IndexEntry->getContribution(); 355 if (!UnitContrib || 356 UnitContrib->Length != (getLength() + getUnitLengthFieldByteSize())) 357 return false; 358 auto *AbbrEntry = IndexEntry->getContribution(DW_SECT_ABBREV); 359 if (!AbbrEntry) 360 return false; 361 AbbrOffset = AbbrEntry->Offset; 362 return true; 363 } 364 365 Error DWARFUnit::extractRangeList(uint64_t RangeListOffset, 366 DWARFDebugRangeList &RangeList) const { 367 // Require that compile unit is extracted. 368 assert(!DieArray.empty()); 369 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection, 370 isLittleEndian, getAddressByteSize()); 371 uint64_t ActualRangeListOffset = RangeSectionBase + RangeListOffset; 372 return RangeList.extract(RangesData, &ActualRangeListOffset); 373 } 374 375 void DWARFUnit::clear() { 376 Abbrevs = nullptr; 377 BaseAddr.reset(); 378 RangeSectionBase = 0; 379 LocSectionBase = 0; 380 AddrOffsetSectionBase = None; 381 SU = nullptr; 382 clearDIEs(false); 383 DWO.reset(); 384 } 385 386 const char *DWARFUnit::getCompilationDir() { 387 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr); 388 } 389 390 void DWARFUnit::extractDIEsToVector( 391 bool AppendCUDie, bool AppendNonCUDies, 392 std::vector<DWARFDebugInfoEntry> &Dies) const { 393 if (!AppendCUDie && !AppendNonCUDies) 394 return; 395 396 // Set the offset to that of the first DIE and calculate the start of the 397 // next compilation unit header. 398 uint64_t DIEOffset = getOffset() + getHeaderSize(); 399 uint64_t NextCUOffset = getNextUnitOffset(); 400 DWARFDebugInfoEntry DIE; 401 DWARFDataExtractor DebugInfoData = getDebugInfoExtractor(); 402 // The end offset has been already checked by DWARFUnitHeader::extract. 403 assert(DebugInfoData.isValidOffset(NextCUOffset - 1)); 404 std::vector<uint32_t> Parents; 405 std::vector<uint32_t> PrevSiblings; 406 bool IsCUDie = true; 407 408 assert( 409 ((AppendCUDie && Dies.empty()) || (!AppendCUDie && Dies.size() == 1)) && 410 "Dies array is not empty"); 411 412 // Fill Parents and Siblings stacks with initial value. 413 Parents.push_back(UINT32_MAX); 414 if (!AppendCUDie) 415 Parents.push_back(0); 416 PrevSiblings.push_back(0); 417 418 // Start to extract dies. 419 do { 420 assert(Parents.size() > 0 && "Empty parents stack"); 421 assert((Parents.back() == UINT32_MAX || Parents.back() <= Dies.size()) && 422 "Wrong parent index"); 423 424 // Extract die. Stop if any error occurred. 425 if (!DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset, 426 Parents.back())) 427 break; 428 429 // If previous sibling is remembered then update it`s SiblingIdx field. 430 if (PrevSiblings.back() > 0) { 431 assert(PrevSiblings.back() < Dies.size() && 432 "Previous sibling index is out of Dies boundaries"); 433 Dies[PrevSiblings.back()].setSiblingIdx(Dies.size()); 434 } 435 436 // Store die into the Dies vector. 437 if (IsCUDie) { 438 if (AppendCUDie) 439 Dies.push_back(DIE); 440 if (!AppendNonCUDies) 441 break; 442 // The average bytes per DIE entry has been seen to be 443 // around 14-20 so let's pre-reserve the needed memory for 444 // our DIE entries accordingly. 445 Dies.reserve(Dies.size() + getDebugInfoSize() / 14); 446 } else { 447 // Remember last previous sibling. 448 PrevSiblings.back() = Dies.size(); 449 450 Dies.push_back(DIE); 451 } 452 453 // Check for new children scope. 454 if (const DWARFAbbreviationDeclaration *AbbrDecl = 455 DIE.getAbbreviationDeclarationPtr()) { 456 if (AbbrDecl->hasChildren()) { 457 if (AppendCUDie || !IsCUDie) { 458 assert(Dies.size() > 0 && "Dies does not contain any die"); 459 Parents.push_back(Dies.size() - 1); 460 PrevSiblings.push_back(0); 461 } 462 } else if (IsCUDie) 463 // Stop if we have single compile unit die w/o children. 464 break; 465 } else { 466 // NULL DIE: finishes current children scope. 467 Parents.pop_back(); 468 PrevSiblings.pop_back(); 469 } 470 471 if (IsCUDie) 472 IsCUDie = false; 473 474 // Stop when compile unit die is removed from the parents stack. 475 } while (Parents.size() > 1); 476 } 477 478 void DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) { 479 if (Error e = tryExtractDIEsIfNeeded(CUDieOnly)) 480 Context.getRecoverableErrorHandler()(std::move(e)); 481 } 482 483 Error DWARFUnit::tryExtractDIEsIfNeeded(bool CUDieOnly) { 484 if ((CUDieOnly && !DieArray.empty()) || 485 DieArray.size() > 1) 486 return Error::success(); // Already parsed. 487 488 bool HasCUDie = !DieArray.empty(); 489 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray); 490 491 if (DieArray.empty()) 492 return Error::success(); 493 494 // If CU DIE was just parsed, copy several attribute values from it. 495 if (HasCUDie) 496 return Error::success(); 497 498 DWARFDie UnitDie(this, &DieArray[0]); 499 if (Optional<uint64_t> DWOId = toUnsigned(UnitDie.find(DW_AT_GNU_dwo_id))) 500 Header.setDWOId(*DWOId); 501 if (!IsDWO) { 502 assert(AddrOffsetSectionBase == None); 503 assert(RangeSectionBase == 0); 504 assert(LocSectionBase == 0); 505 AddrOffsetSectionBase = toSectionOffset(UnitDie.find(DW_AT_addr_base)); 506 if (!AddrOffsetSectionBase) 507 AddrOffsetSectionBase = 508 toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base)); 509 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0); 510 LocSectionBase = toSectionOffset(UnitDie.find(DW_AT_loclists_base), 0); 511 } 512 513 // In general, in DWARF v5 and beyond we derive the start of the unit's 514 // contribution to the string offsets table from the unit DIE's 515 // DW_AT_str_offsets_base attribute. Split DWARF units do not use this 516 // attribute, so we assume that there is a contribution to the string 517 // offsets table starting at offset 0 of the debug_str_offsets.dwo section. 518 // In both cases we need to determine the format of the contribution, 519 // which may differ from the unit's format. 520 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection, 521 isLittleEndian, 0); 522 if (IsDWO || getVersion() >= 5) { 523 auto StringOffsetOrError = 524 IsDWO ? determineStringOffsetsTableContributionDWO(DA) 525 : determineStringOffsetsTableContribution(DA); 526 if (!StringOffsetOrError) 527 return createStringError(errc::invalid_argument, 528 "invalid reference to or invalid content in " 529 ".debug_str_offsets[.dwo]: " + 530 toString(StringOffsetOrError.takeError())); 531 532 StringOffsetsTableContribution = *StringOffsetOrError; 533 } 534 535 // DWARF v5 uses the .debug_rnglists and .debug_rnglists.dwo sections to 536 // describe address ranges. 537 if (getVersion() >= 5) { 538 // In case of DWP, the base offset from the index has to be added. 539 if (IsDWO) { 540 uint64_t ContributionBaseOffset = 0; 541 if (auto *IndexEntry = Header.getIndexEntry()) 542 if (auto *Contrib = IndexEntry->getContribution(DW_SECT_RNGLISTS)) 543 ContributionBaseOffset = Contrib->Offset; 544 setRangesSection( 545 &Context.getDWARFObj().getRnglistsDWOSection(), 546 ContributionBaseOffset + 547 DWARFListTableHeader::getHeaderSize(Header.getFormat())); 548 } else 549 setRangesSection(&Context.getDWARFObj().getRnglistsSection(), 550 toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 551 DWARFListTableHeader::getHeaderSize( 552 Header.getFormat()))); 553 } 554 555 if (IsDWO) { 556 // If we are reading a package file, we need to adjust the location list 557 // data based on the index entries. 558 StringRef Data = Header.getVersion() >= 5 559 ? Context.getDWARFObj().getLoclistsDWOSection().Data 560 : Context.getDWARFObj().getLocDWOSection().Data; 561 if (auto *IndexEntry = Header.getIndexEntry()) 562 if (const auto *C = IndexEntry->getContribution( 563 Header.getVersion() >= 5 ? DW_SECT_LOCLISTS : DW_SECT_EXT_LOC)) 564 Data = Data.substr(C->Offset, C->Length); 565 566 DWARFDataExtractor DWARFData(Data, isLittleEndian, getAddressByteSize()); 567 LocTable = 568 std::make_unique<DWARFDebugLoclists>(DWARFData, Header.getVersion()); 569 LocSectionBase = DWARFListTableHeader::getHeaderSize(Header.getFormat()); 570 } else if (getVersion() >= 5) { 571 LocTable = std::make_unique<DWARFDebugLoclists>( 572 DWARFDataExtractor(Context.getDWARFObj(), 573 Context.getDWARFObj().getLoclistsSection(), 574 isLittleEndian, getAddressByteSize()), 575 getVersion()); 576 } else { 577 LocTable = std::make_unique<DWARFDebugLoc>(DWARFDataExtractor( 578 Context.getDWARFObj(), Context.getDWARFObj().getLocSection(), 579 isLittleEndian, getAddressByteSize())); 580 } 581 582 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for 583 // skeleton CU DIE, so that DWARF users not aware of it are not broken. 584 return Error::success(); 585 } 586 587 bool DWARFUnit::parseDWO() { 588 if (IsDWO) 589 return false; 590 if (DWO.get()) 591 return false; 592 DWARFDie UnitDie = getUnitDIE(); 593 if (!UnitDie) 594 return false; 595 auto DWOFileName = getVersion() >= 5 596 ? dwarf::toString(UnitDie.find(DW_AT_dwo_name)) 597 : dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name)); 598 if (!DWOFileName) 599 return false; 600 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir)); 601 SmallString<16> AbsolutePath; 602 if (sys::path::is_relative(*DWOFileName) && CompilationDir && 603 *CompilationDir) { 604 sys::path::append(AbsolutePath, *CompilationDir); 605 } 606 sys::path::append(AbsolutePath, *DWOFileName); 607 auto DWOId = getDWOId(); 608 if (!DWOId) 609 return false; 610 auto DWOContext = Context.getDWOContext(AbsolutePath); 611 if (!DWOContext) 612 return false; 613 614 DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId); 615 if (!DWOCU) 616 return false; 617 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU); 618 DWO->setSkeletonUnit(this); 619 // Share .debug_addr and .debug_ranges section with compile unit in .dwo 620 if (AddrOffsetSectionBase) 621 DWO->setAddrOffsetSection(AddrOffsetSection, *AddrOffsetSectionBase); 622 if (getVersion() == 4) { 623 auto DWORangesBase = UnitDie.getRangesBaseAttribute(); 624 DWO->setRangesSection(RangeSection, DWORangesBase.getValueOr(0)); 625 } 626 627 return true; 628 } 629 630 void DWARFUnit::clearDIEs(bool KeepCUDie) { 631 // Do not use resize() + shrink_to_fit() to free memory occupied by dies. 632 // shrink_to_fit() is a *non-binding* request to reduce capacity() to size(). 633 // It depends on the implementation whether the request is fulfilled. 634 // Create a new vector with a small capacity and assign it to the DieArray to 635 // have previous contents freed. 636 DieArray = (KeepCUDie && !DieArray.empty()) 637 ? std::vector<DWARFDebugInfoEntry>({DieArray[0]}) 638 : std::vector<DWARFDebugInfoEntry>(); 639 } 640 641 Expected<DWARFAddressRangesVector> 642 DWARFUnit::findRnglistFromOffset(uint64_t Offset) { 643 if (getVersion() <= 4) { 644 DWARFDebugRangeList RangeList; 645 if (Error E = extractRangeList(Offset, RangeList)) 646 return std::move(E); 647 return RangeList.getAbsoluteRanges(getBaseAddress()); 648 } 649 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection, 650 isLittleEndian, Header.getAddressByteSize()); 651 DWARFDebugRnglistTable RnglistTable; 652 auto RangeListOrError = RnglistTable.findList(RangesData, Offset); 653 if (RangeListOrError) 654 return RangeListOrError.get().getAbsoluteRanges(getBaseAddress(), *this); 655 return RangeListOrError.takeError(); 656 } 657 658 Expected<DWARFAddressRangesVector> 659 DWARFUnit::findRnglistFromIndex(uint32_t Index) { 660 if (auto Offset = getRnglistOffset(Index)) 661 return findRnglistFromOffset(*Offset); 662 663 return createStringError(errc::invalid_argument, 664 "invalid range list table index %d (possibly " 665 "missing the entire range list table)", 666 Index); 667 } 668 669 Expected<DWARFAddressRangesVector> DWARFUnit::collectAddressRanges() { 670 DWARFDie UnitDie = getUnitDIE(); 671 if (!UnitDie) 672 return createStringError(errc::invalid_argument, "No unit DIE"); 673 674 // First, check if unit DIE describes address ranges for the whole unit. 675 auto CUDIERangesOrError = UnitDie.getAddressRanges(); 676 if (!CUDIERangesOrError) 677 return createStringError(errc::invalid_argument, 678 "decoding address ranges: %s", 679 toString(CUDIERangesOrError.takeError()).c_str()); 680 return *CUDIERangesOrError; 681 } 682 683 Expected<DWARFLocationExpressionsVector> 684 DWARFUnit::findLoclistFromOffset(uint64_t Offset) { 685 DWARFLocationExpressionsVector Result; 686 687 Error InterpretationError = Error::success(); 688 689 Error ParseError = getLocationTable().visitAbsoluteLocationList( 690 Offset, getBaseAddress(), 691 [this](uint32_t Index) { return getAddrOffsetSectionItem(Index); }, 692 [&](Expected<DWARFLocationExpression> L) { 693 if (L) 694 Result.push_back(std::move(*L)); 695 else 696 InterpretationError = 697 joinErrors(L.takeError(), std::move(InterpretationError)); 698 return !InterpretationError; 699 }); 700 701 if (ParseError || InterpretationError) 702 return joinErrors(std::move(ParseError), std::move(InterpretationError)); 703 704 return Result; 705 } 706 707 void DWARFUnit::updateAddressDieMap(DWARFDie Die) { 708 if (Die.isSubroutineDIE()) { 709 auto DIERangesOrError = Die.getAddressRanges(); 710 if (DIERangesOrError) { 711 for (const auto &R : DIERangesOrError.get()) { 712 // Ignore 0-sized ranges. 713 if (R.LowPC == R.HighPC) 714 continue; 715 auto B = AddrDieMap.upper_bound(R.LowPC); 716 if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) { 717 // The range is a sub-range of existing ranges, we need to split the 718 // existing range. 719 if (R.HighPC < B->second.first) 720 AddrDieMap[R.HighPC] = B->second; 721 if (R.LowPC > B->first) 722 AddrDieMap[B->first].first = R.LowPC; 723 } 724 AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die); 725 } 726 } else 727 llvm::consumeError(DIERangesOrError.takeError()); 728 } 729 // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to 730 // simplify the logic to update AddrDieMap. The child's range will always 731 // be equal or smaller than the parent's range. With this assumption, when 732 // adding one range into the map, it will at most split a range into 3 733 // sub-ranges. 734 for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling()) 735 updateAddressDieMap(Child); 736 } 737 738 DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) { 739 extractDIEsIfNeeded(false); 740 if (AddrDieMap.empty()) 741 updateAddressDieMap(getUnitDIE()); 742 auto R = AddrDieMap.upper_bound(Address); 743 if (R == AddrDieMap.begin()) 744 return DWARFDie(); 745 // upper_bound's previous item contains Address. 746 --R; 747 if (Address >= R->second.first) 748 return DWARFDie(); 749 return R->second.second; 750 } 751 752 void DWARFUnit::updateVariableDieMap(DWARFDie Die) { 753 for (DWARFDie Child : Die) { 754 if (isType(Child.getTag())) 755 continue; 756 updateVariableDieMap(Child); 757 } 758 759 if (Die.getTag() != DW_TAG_variable) 760 return; 761 762 Expected<DWARFLocationExpressionsVector> Locations = 763 Die.getLocations(DW_AT_location); 764 if (!Locations) { 765 // Missing DW_AT_location is fine here. 766 consumeError(Locations.takeError()); 767 return; 768 } 769 770 uint64_t Address = UINT64_MAX; 771 772 for (const DWARFLocationExpression &Location : *Locations) { 773 uint8_t AddressSize = getAddressByteSize(); 774 DataExtractor Data(Location.Expr, /*IsLittleEndian=*/true, AddressSize); 775 DWARFExpression Expr(Data, AddressSize); 776 auto It = Expr.begin(); 777 if (It == Expr.end()) 778 continue; 779 780 // Match exactly the main sequence used to describe global variables: 781 // `DW_OP_addr[x] [+ DW_OP_plus_uconst]`. Currently, this is the sequence 782 // that LLVM produces for DILocalVariables and DIGlobalVariables. If, in 783 // future, the DWARF producer (`DwarfCompileUnit::addLocationAttribute()` is 784 // a good starting point) is extended to use further expressions, this code 785 // needs to be updated. 786 uint64_t LocationAddr; 787 if (It->getCode() == dwarf::DW_OP_addr) { 788 LocationAddr = It->getRawOperand(0); 789 } else if (It->getCode() == dwarf::DW_OP_addrx) { 790 uint64_t DebugAddrOffset = It->getRawOperand(0); 791 if (auto Pointer = getAddrOffsetSectionItem(DebugAddrOffset)) { 792 LocationAddr = Pointer->Address; 793 } 794 } else { 795 continue; 796 } 797 798 // Read the optional 2nd operand, a DW_OP_plus_uconst. 799 if (++It != Expr.end()) { 800 if (It->getCode() != dwarf::DW_OP_plus_uconst) 801 continue; 802 803 LocationAddr += It->getRawOperand(0); 804 805 // Probe for a 3rd operand, if it exists, bail. 806 if (++It != Expr.end()) 807 continue; 808 } 809 810 Address = LocationAddr; 811 break; 812 } 813 814 // Get the size of the global variable. If all else fails (i.e. the global has 815 // no type), then we use a size of one to still allow symbolization of the 816 // exact address. 817 uint64_t GVSize = 1; 818 if (DWARFDie BaseType = Die.getAttributeValueAsReferencedDie(DW_AT_type)) 819 if (Optional<uint64_t> Size = Die.getTypeSize(getAddressByteSize())) 820 GVSize = *Size; 821 822 if (Address != UINT64_MAX) 823 VariableDieMap[Address] = {Address + GVSize, Die}; 824 } 825 826 DWARFDie DWARFUnit::getVariableForAddress(uint64_t Address) { 827 extractDIEsIfNeeded(false); 828 829 auto RootDie = getUnitDIE(); 830 831 auto RootLookup = RootsParsedForVariables.insert(RootDie.getOffset()); 832 if (RootLookup.second) 833 updateVariableDieMap(RootDie); 834 835 auto R = VariableDieMap.upper_bound(Address); 836 if (R == VariableDieMap.begin()) 837 return DWARFDie(); 838 839 // upper_bound's previous item contains Address. 840 --R; 841 if (Address >= R->second.first) 842 return DWARFDie(); 843 return R->second.second; 844 } 845 846 void 847 DWARFUnit::getInlinedChainForAddress(uint64_t Address, 848 SmallVectorImpl<DWARFDie> &InlinedChain) { 849 assert(InlinedChain.empty()); 850 // Try to look for subprogram DIEs in the DWO file. 851 parseDWO(); 852 // First, find the subroutine that contains the given address (the leaf 853 // of inlined chain). 854 DWARFDie SubroutineDIE = 855 (DWO ? *DWO : *this).getSubroutineForAddress(Address); 856 857 while (SubroutineDIE) { 858 if (SubroutineDIE.isSubprogramDIE()) { 859 InlinedChain.push_back(SubroutineDIE); 860 return; 861 } 862 if (SubroutineDIE.getTag() == DW_TAG_inlined_subroutine) 863 InlinedChain.push_back(SubroutineDIE); 864 SubroutineDIE = SubroutineDIE.getParent(); 865 } 866 } 867 868 const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context, 869 DWARFSectionKind Kind) { 870 if (Kind == DW_SECT_INFO) 871 return Context.getCUIndex(); 872 assert(Kind == DW_SECT_EXT_TYPES); 873 return Context.getTUIndex(); 874 } 875 876 DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) { 877 if (!Die) 878 return DWARFDie(); 879 880 if (Optional<uint32_t> ParentIdx = Die->getParentIdx()) { 881 assert(*ParentIdx < DieArray.size() && 882 "ParentIdx is out of DieArray boundaries"); 883 return DWARFDie(this, &DieArray[*ParentIdx]); 884 } 885 886 return DWARFDie(); 887 } 888 889 DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) { 890 if (!Die) 891 return DWARFDie(); 892 893 if (Optional<uint32_t> SiblingIdx = Die->getSiblingIdx()) { 894 assert(*SiblingIdx < DieArray.size() && 895 "SiblingIdx is out of DieArray boundaries"); 896 return DWARFDie(this, &DieArray[*SiblingIdx]); 897 } 898 899 return DWARFDie(); 900 } 901 902 DWARFDie DWARFUnit::getPreviousSibling(const DWARFDebugInfoEntry *Die) { 903 if (!Die) 904 return DWARFDie(); 905 906 Optional<uint32_t> ParentIdx = Die->getParentIdx(); 907 if (!ParentIdx) 908 // Die is a root die, there is no previous sibling. 909 return DWARFDie(); 910 911 assert(*ParentIdx < DieArray.size() && 912 "ParentIdx is out of DieArray boundaries"); 913 assert(getDIEIndex(Die) > 0 && "Die is a root die"); 914 915 uint32_t PrevDieIdx = getDIEIndex(Die) - 1; 916 if (PrevDieIdx == *ParentIdx) 917 // Immediately previous node is parent, there is no previous sibling. 918 return DWARFDie(); 919 920 while (DieArray[PrevDieIdx].getParentIdx() != *ParentIdx) { 921 PrevDieIdx = *DieArray[PrevDieIdx].getParentIdx(); 922 923 assert(PrevDieIdx < DieArray.size() && 924 "PrevDieIdx is out of DieArray boundaries"); 925 assert(PrevDieIdx >= *ParentIdx && 926 "PrevDieIdx is not a child of parent of Die"); 927 } 928 929 return DWARFDie(this, &DieArray[PrevDieIdx]); 930 } 931 932 DWARFDie DWARFUnit::getFirstChild(const DWARFDebugInfoEntry *Die) { 933 if (!Die->hasChildren()) 934 return DWARFDie(); 935 936 // TODO: Instead of checking here for invalid die we might reject 937 // invalid dies at parsing stage(DWARFUnit::extractDIEsToVector). 938 // We do not want access out of bounds when parsing corrupted debug data. 939 size_t I = getDIEIndex(Die) + 1; 940 if (I >= DieArray.size()) 941 return DWARFDie(); 942 return DWARFDie(this, &DieArray[I]); 943 } 944 945 DWARFDie DWARFUnit::getLastChild(const DWARFDebugInfoEntry *Die) { 946 if (!Die->hasChildren()) 947 return DWARFDie(); 948 949 if (Optional<uint32_t> SiblingIdx = Die->getSiblingIdx()) { 950 assert(*SiblingIdx < DieArray.size() && 951 "SiblingIdx is out of DieArray boundaries"); 952 assert(DieArray[*SiblingIdx - 1].getTag() == dwarf::DW_TAG_null && 953 "Bad end of children marker"); 954 return DWARFDie(this, &DieArray[*SiblingIdx - 1]); 955 } 956 957 // If SiblingIdx is set for non-root dies we could be sure that DWARF is 958 // correct and "end of children marker" must be found. For root die we do not 959 // have such a guarantee(parsing root die might be stopped if "end of children 960 // marker" is missing, SiblingIdx is always zero for root die). That is why we 961 // do not use assertion for checking for "end of children marker" for root 962 // die. 963 964 // TODO: Instead of checking here for invalid die we might reject 965 // invalid dies at parsing stage(DWARFUnit::extractDIEsToVector). 966 if (getDIEIndex(Die) == 0 && DieArray.size() > 1 && 967 DieArray.back().getTag() == dwarf::DW_TAG_null) { 968 // For the unit die we might take last item from DieArray. 969 assert(getDIEIndex(Die) == getDIEIndex(getUnitDIE()) && "Bad unit die"); 970 return DWARFDie(this, &DieArray.back()); 971 } 972 973 return DWARFDie(); 974 } 975 976 const DWARFAbbreviationDeclarationSet *DWARFUnit::getAbbreviations() const { 977 if (!Abbrevs) 978 Abbrevs = Abbrev->getAbbreviationDeclarationSet(getAbbreviationsOffset()); 979 return Abbrevs; 980 } 981 982 llvm::Optional<object::SectionedAddress> DWARFUnit::getBaseAddress() { 983 if (BaseAddr) 984 return BaseAddr; 985 986 DWARFDie UnitDie = getUnitDIE(); 987 Optional<DWARFFormValue> PC = UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc}); 988 BaseAddr = toSectionedAddress(PC); 989 return BaseAddr; 990 } 991 992 Expected<StrOffsetsContributionDescriptor> 993 StrOffsetsContributionDescriptor::validateContributionSize( 994 DWARFDataExtractor &DA) { 995 uint8_t EntrySize = getDwarfOffsetByteSize(); 996 // In order to ensure that we don't read a partial record at the end of 997 // the section we validate for a multiple of the entry size. 998 uint64_t ValidationSize = alignTo(Size, EntrySize); 999 // Guard against overflow. 1000 if (ValidationSize >= Size) 1001 if (DA.isValidOffsetForDataOfSize((uint32_t)Base, ValidationSize)) 1002 return *this; 1003 return createStringError(errc::invalid_argument, "length exceeds section size"); 1004 } 1005 1006 // Look for a DWARF64-formatted contribution to the string offsets table 1007 // starting at a given offset and record it in a descriptor. 1008 static Expected<StrOffsetsContributionDescriptor> 1009 parseDWARF64StringOffsetsTableHeader(DWARFDataExtractor &DA, uint64_t Offset) { 1010 if (!DA.isValidOffsetForDataOfSize(Offset, 16)) 1011 return createStringError(errc::invalid_argument, "section offset exceeds section size"); 1012 1013 if (DA.getU32(&Offset) != dwarf::DW_LENGTH_DWARF64) 1014 return createStringError(errc::invalid_argument, "32 bit contribution referenced from a 64 bit unit"); 1015 1016 uint64_t Size = DA.getU64(&Offset); 1017 uint8_t Version = DA.getU16(&Offset); 1018 (void)DA.getU16(&Offset); // padding 1019 // The encoded length includes the 2-byte version field and the 2-byte 1020 // padding, so we need to subtract them out when we populate the descriptor. 1021 return StrOffsetsContributionDescriptor(Offset, Size - 4, Version, DWARF64); 1022 } 1023 1024 // Look for a DWARF32-formatted contribution to the string offsets table 1025 // starting at a given offset and record it in a descriptor. 1026 static Expected<StrOffsetsContributionDescriptor> 1027 parseDWARF32StringOffsetsTableHeader(DWARFDataExtractor &DA, uint64_t Offset) { 1028 if (!DA.isValidOffsetForDataOfSize(Offset, 8)) 1029 return createStringError(errc::invalid_argument, "section offset exceeds section size"); 1030 1031 uint32_t ContributionSize = DA.getU32(&Offset); 1032 if (ContributionSize >= dwarf::DW_LENGTH_lo_reserved) 1033 return createStringError(errc::invalid_argument, "invalid length"); 1034 1035 uint8_t Version = DA.getU16(&Offset); 1036 (void)DA.getU16(&Offset); // padding 1037 // The encoded length includes the 2-byte version field and the 2-byte 1038 // padding, so we need to subtract them out when we populate the descriptor. 1039 return StrOffsetsContributionDescriptor(Offset, ContributionSize - 4, Version, 1040 DWARF32); 1041 } 1042 1043 static Expected<StrOffsetsContributionDescriptor> 1044 parseDWARFStringOffsetsTableHeader(DWARFDataExtractor &DA, 1045 llvm::dwarf::DwarfFormat Format, 1046 uint64_t Offset) { 1047 StrOffsetsContributionDescriptor Desc; 1048 switch (Format) { 1049 case dwarf::DwarfFormat::DWARF64: { 1050 if (Offset < 16) 1051 return createStringError(errc::invalid_argument, "insufficient space for 64 bit header prefix"); 1052 auto DescOrError = parseDWARF64StringOffsetsTableHeader(DA, Offset - 16); 1053 if (!DescOrError) 1054 return DescOrError.takeError(); 1055 Desc = *DescOrError; 1056 break; 1057 } 1058 case dwarf::DwarfFormat::DWARF32: { 1059 if (Offset < 8) 1060 return createStringError(errc::invalid_argument, "insufficient space for 32 bit header prefix"); 1061 auto DescOrError = parseDWARF32StringOffsetsTableHeader(DA, Offset - 8); 1062 if (!DescOrError) 1063 return DescOrError.takeError(); 1064 Desc = *DescOrError; 1065 break; 1066 } 1067 } 1068 return Desc.validateContributionSize(DA); 1069 } 1070 1071 Expected<Optional<StrOffsetsContributionDescriptor>> 1072 DWARFUnit::determineStringOffsetsTableContribution(DWARFDataExtractor &DA) { 1073 assert(!IsDWO); 1074 auto OptOffset = toSectionOffset(getUnitDIE().find(DW_AT_str_offsets_base)); 1075 if (!OptOffset) 1076 return None; 1077 auto DescOrError = 1078 parseDWARFStringOffsetsTableHeader(DA, Header.getFormat(), *OptOffset); 1079 if (!DescOrError) 1080 return DescOrError.takeError(); 1081 return *DescOrError; 1082 } 1083 1084 Expected<Optional<StrOffsetsContributionDescriptor>> 1085 DWARFUnit::determineStringOffsetsTableContributionDWO(DWARFDataExtractor & DA) { 1086 assert(IsDWO); 1087 uint64_t Offset = 0; 1088 auto IndexEntry = Header.getIndexEntry(); 1089 const auto *C = 1090 IndexEntry ? IndexEntry->getContribution(DW_SECT_STR_OFFSETS) : nullptr; 1091 if (C) 1092 Offset = C->Offset; 1093 if (getVersion() >= 5) { 1094 if (DA.getData().data() == nullptr) 1095 return None; 1096 Offset += Header.getFormat() == dwarf::DwarfFormat::DWARF32 ? 8 : 16; 1097 // Look for a valid contribution at the given offset. 1098 auto DescOrError = parseDWARFStringOffsetsTableHeader(DA, Header.getFormat(), Offset); 1099 if (!DescOrError) 1100 return DescOrError.takeError(); 1101 return *DescOrError; 1102 } 1103 // Prior to DWARF v5, we derive the contribution size from the 1104 // index table (in a package file). In a .dwo file it is simply 1105 // the length of the string offsets section. 1106 StrOffsetsContributionDescriptor Desc; 1107 if (C) 1108 Desc = StrOffsetsContributionDescriptor(C->Offset, C->Length, 4, 1109 Header.getFormat()); 1110 else if (!IndexEntry && !StringOffsetSection.Data.empty()) 1111 Desc = StrOffsetsContributionDescriptor(0, StringOffsetSection.Data.size(), 1112 4, Header.getFormat()); 1113 else 1114 return None; 1115 auto DescOrError = Desc.validateContributionSize(DA); 1116 if (!DescOrError) 1117 return DescOrError.takeError(); 1118 return *DescOrError; 1119 } 1120 1121 Optional<uint64_t> DWARFUnit::getRnglistOffset(uint32_t Index) { 1122 DataExtractor RangesData(RangeSection->Data, isLittleEndian, 1123 getAddressByteSize()); 1124 DWARFDataExtractor RangesDA(Context.getDWARFObj(), *RangeSection, 1125 isLittleEndian, 0); 1126 if (Optional<uint64_t> Off = llvm::DWARFListTableHeader::getOffsetEntry( 1127 RangesData, RangeSectionBase, getFormat(), Index)) 1128 return *Off + RangeSectionBase; 1129 return None; 1130 } 1131 1132 Optional<uint64_t> DWARFUnit::getLoclistOffset(uint32_t Index) { 1133 if (Optional<uint64_t> Off = llvm::DWARFListTableHeader::getOffsetEntry( 1134 LocTable->getData(), LocSectionBase, getFormat(), Index)) 1135 return *Off + LocSectionBase; 1136 return None; 1137 } 1138