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