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