1 //===- DWARFUnit.cpp ------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.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/DWARFDie.h"
18 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
19 #include "llvm/Support/DataExtractor.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/WithColor.h"
22 #include <algorithm>
23 #include <cassert>
24 #include <cstddef>
25 #include <cstdint>
26 #include <cstdio>
27 #include <utility>
28 #include <vector>
29 
30 using namespace llvm;
31 using namespace dwarf;
32 
33 void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) {
34   const DWARFObject &D = C.getDWARFObj();
35   parseImpl(C, D, Section, C.getDebugAbbrev(), &D.getRangeSection(),
36             D.getStringSection(), D.getStringOffsetSection(),
37             &D.getAddrSection(), D.getLineSection(), D.isLittleEndian(), false,
38             false);
39 }
40 
41 void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
42                                     const DWARFSection &DWOSection, bool Lazy) {
43   const DWARFObject &D = C.getDWARFObj();
44   parseImpl(C, D, DWOSection, C.getDebugAbbrevDWO(), &D.getRangeDWOSection(),
45             D.getStringDWOSection(), D.getStringOffsetDWOSection(),
46             &D.getAddrSection(), D.getLineDWOSection(), C.isLittleEndian(),
47             true, Lazy);
48 }
49 
50 DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
51                      const DWARFDebugAbbrev *DA, const DWARFSection *RS,
52                      StringRef SS, const DWARFSection &SOS,
53                      const DWARFSection *AOS, const DWARFSection &LS, bool LE,
54                      bool IsDWO, const DWARFUnitSectionBase &UnitSection,
55                      const DWARFUnitIndex::Entry *IndexEntry)
56     : Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS),
57       LineSection(LS), StringSection(SS), StringOffsetSection(SOS),
58       AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO),
59       UnitSection(UnitSection), IndexEntry(IndexEntry) {
60   clear();
61 }
62 
63 DWARFUnit::~DWARFUnit() = default;
64 
65 DWARFDataExtractor DWARFUnit::getDebugInfoExtractor() const {
66   return DWARFDataExtractor(Context.getDWARFObj(), InfoSection, isLittleEndian,
67                             getAddressByteSize());
68 }
69 
70 bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
71                                                 uint64_t &Result) const {
72   uint32_t Offset = AddrOffsetSectionBase + Index * getAddressByteSize();
73   if (AddrOffsetSection->Data.size() < Offset + getAddressByteSize())
74     return false;
75   DWARFDataExtractor DA(Context.getDWARFObj(), *AddrOffsetSection,
76                         isLittleEndian, getAddressByteSize());
77   Result = DA.getRelocatedAddress(&Offset);
78   return true;
79 }
80 
81 bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
82                                            uint64_t &Result) const {
83   if (!StringOffsetsTableContribution)
84     return false;
85   unsigned ItemSize = getDwarfStringOffsetsByteSize();
86   uint32_t Offset = getStringOffsetsBase() + Index * ItemSize;
87   if (StringOffsetSection.Data.size() < Offset + ItemSize)
88     return false;
89   DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection,
90                         isLittleEndian, 0);
91   Result = DA.getRelocatedValue(ItemSize, &Offset);
92   return true;
93 }
94 
95 bool DWARFUnit::extractImpl(const DWARFDataExtractor &debug_info,
96                             uint32_t *offset_ptr) {
97   Length = debug_info.getU32(offset_ptr);
98   // FIXME: Support DWARF64.
99   FormParams.Format = DWARF32;
100   FormParams.Version = debug_info.getU16(offset_ptr);
101   if (FormParams.Version >= 5) {
102     UnitType = debug_info.getU8(offset_ptr);
103     FormParams.AddrSize = debug_info.getU8(offset_ptr);
104     AbbrOffset = debug_info.getU32(offset_ptr);
105   } else {
106     AbbrOffset = debug_info.getRelocatedValue(4, offset_ptr);
107     FormParams.AddrSize = debug_info.getU8(offset_ptr);
108   }
109   if (IndexEntry) {
110     if (AbbrOffset)
111       return false;
112     auto *UnitContrib = IndexEntry->getOffset();
113     if (!UnitContrib || UnitContrib->Length != (Length + 4))
114       return false;
115     auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
116     if (!AbbrEntry)
117       return false;
118     AbbrOffset = AbbrEntry->Offset;
119   }
120 
121   bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
122   bool VersionOK = DWARFContext::isSupportedVersion(getVersion());
123   bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8;
124 
125   if (!LengthOK || !VersionOK || !AddrSizeOK)
126     return false;
127 
128   // Keep track of the highest DWARF version we encounter across all units.
129   Context.setMaxVersionIfGreater(getVersion());
130   return true;
131 }
132 
133 bool DWARFUnit::extract(const DWARFDataExtractor &debug_info,
134                         uint32_t *offset_ptr) {
135   clear();
136 
137   Offset = *offset_ptr;
138 
139   if (debug_info.isValidOffset(*offset_ptr)) {
140     if (extractImpl(debug_info, offset_ptr))
141       return true;
142 
143     // reset the offset to where we tried to parse from if anything went wrong
144     *offset_ptr = Offset;
145   }
146 
147   return false;
148 }
149 
150 bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
151                                  DWARFDebugRangeList &RangeList) const {
152   // Require that compile unit is extracted.
153   assert(!DieArray.empty());
154   DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection,
155                                 isLittleEndian, getAddressByteSize());
156   uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
157   return RangeList.extract(RangesData, &ActualRangeListOffset);
158 }
159 
160 void DWARFUnit::clear() {
161   Offset = 0;
162   Length = 0;
163   Abbrevs = nullptr;
164   FormParams = dwarf::FormParams({0, 0, DWARF32});
165   BaseAddr.reset();
166   RangeSectionBase = 0;
167   AddrOffsetSectionBase = 0;
168   clearDIEs(false);
169   DWO.reset();
170 }
171 
172 const char *DWARFUnit::getCompilationDir() {
173   return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
174 }
175 
176 Optional<uint64_t> DWARFUnit::getDWOId() {
177   return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id));
178 }
179 
180 void DWARFUnit::extractDIEsToVector(
181     bool AppendCUDie, bool AppendNonCUDies,
182     std::vector<DWARFDebugInfoEntry> &Dies) const {
183   if (!AppendCUDie && !AppendNonCUDies)
184     return;
185 
186   // Set the offset to that of the first DIE and calculate the start of the
187   // next compilation unit header.
188   uint32_t DIEOffset = Offset + getHeaderSize();
189   uint32_t NextCUOffset = getNextUnitOffset();
190   DWARFDebugInfoEntry DIE;
191   DWARFDataExtractor DebugInfoData = getDebugInfoExtractor();
192   uint32_t Depth = 0;
193   bool IsCUDie = true;
194 
195   while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
196                          Depth)) {
197     if (IsCUDie) {
198       if (AppendCUDie)
199         Dies.push_back(DIE);
200       if (!AppendNonCUDies)
201         break;
202       // The average bytes per DIE entry has been seen to be
203       // around 14-20 so let's pre-reserve the needed memory for
204       // our DIE entries accordingly.
205       Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
206       IsCUDie = false;
207     } else {
208       Dies.push_back(DIE);
209     }
210 
211     if (const DWARFAbbreviationDeclaration *AbbrDecl =
212             DIE.getAbbreviationDeclarationPtr()) {
213       // Normal DIE
214       if (AbbrDecl->hasChildren())
215         ++Depth;
216     } else {
217       // NULL DIE.
218       if (Depth > 0)
219         --Depth;
220       if (Depth == 0)
221         break;  // We are done with this compile unit!
222     }
223   }
224 
225   // Give a little bit of info if we encounter corrupt DWARF (our offset
226   // should always terminate at or before the start of the next compilation
227   // unit header).
228   if (DIEOffset > NextCUOffset)
229     WithColor::warning() << format("DWARF compile unit extends beyond its "
230                                    "bounds cu 0x%8.8x at 0x%8.8x\n",
231                                    getOffset(), DIEOffset);
232 }
233 
234 size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
235   if ((CUDieOnly && !DieArray.empty()) ||
236       DieArray.size() > 1)
237     return 0; // Already parsed.
238 
239   bool HasCUDie = !DieArray.empty();
240   extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
241 
242   if (DieArray.empty())
243     return 0;
244 
245   // If CU DIE was just parsed, copy several attribute values from it.
246   if (!HasCUDie) {
247     DWARFDie UnitDie = getUnitDIE();
248     Optional<DWARFFormValue> PC = UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc});
249     if (Optional<uint64_t> Addr = toAddress(PC))
250         setBaseAddress({*Addr, PC->getSectionIndex()});
251 
252     if (!isDWO) {
253       assert(AddrOffsetSectionBase == 0);
254       assert(RangeSectionBase == 0);
255       AddrOffsetSectionBase =
256           toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
257       RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
258     }
259 
260     // In general, in DWARF v5 and beyond we derive the start of the unit's
261     // contribution to the string offsets table from the unit DIE's
262     // DW_AT_str_offsets_base attribute. Split DWARF units do not use this
263     // attribute, so we assume that there is a contribution to the string
264     // offsets table starting at offset 0 of the debug_str_offsets.dwo section.
265     // In both cases we need to determine the format of the contribution,
266     // which may differ from the unit's format.
267     uint64_t StringOffsetsContributionBase =
268         isDWO ? 0 : toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0);
269     if (IndexEntry)
270       if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
271         StringOffsetsContributionBase += C->Offset;
272 
273     DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection,
274                           isLittleEndian, 0);
275     if (isDWO)
276       StringOffsetsTableContribution =
277           determineStringOffsetsTableContributionDWO(
278               DA, StringOffsetsContributionBase);
279     else if (getVersion() >= 5)
280       StringOffsetsTableContribution = determineStringOffsetsTableContribution(
281           DA, StringOffsetsContributionBase);
282 
283     // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
284     // skeleton CU DIE, so that DWARF users not aware of it are not broken.
285   }
286 
287   return DieArray.size();
288 }
289 
290 bool DWARFUnit::parseDWO() {
291   if (isDWO)
292     return false;
293   if (DWO.get())
294     return false;
295   DWARFDie UnitDie = getUnitDIE();
296   if (!UnitDie)
297     return false;
298   auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
299   if (!DWOFileName)
300     return false;
301   auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
302   SmallString<16> AbsolutePath;
303   if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
304       *CompilationDir) {
305     sys::path::append(AbsolutePath, *CompilationDir);
306   }
307   sys::path::append(AbsolutePath, *DWOFileName);
308   auto DWOId = getDWOId();
309   if (!DWOId)
310     return false;
311   auto DWOContext = Context.getDWOContext(AbsolutePath);
312   if (!DWOContext)
313     return false;
314 
315   DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId);
316   if (!DWOCU)
317     return false;
318   DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU);
319   // Share .debug_addr and .debug_ranges section with compile unit in .dwo
320   DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
321   auto DWORangesBase = UnitDie.getRangesBaseAttribute();
322   DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
323   return true;
324 }
325 
326 void DWARFUnit::clearDIEs(bool KeepCUDie) {
327   if (DieArray.size() > (unsigned)KeepCUDie) {
328     DieArray.resize((unsigned)KeepCUDie);
329     DieArray.shrink_to_fit();
330   }
331 }
332 
333 void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
334   DWARFDie UnitDie = getUnitDIE();
335   if (!UnitDie)
336     return;
337   // First, check if unit DIE describes address ranges for the whole unit.
338   const auto &CUDIERanges = UnitDie.getAddressRanges();
339   if (!CUDIERanges.empty()) {
340     CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
341     return;
342   }
343 
344   // This function is usually called if there in no .debug_aranges section
345   // in order to produce a compile unit level set of address ranges that
346   // is accurate. If the DIEs weren't parsed, then we don't want all dies for
347   // all compile units to stay loaded when they weren't needed. So we can end
348   // up parsing the DWARF and then throwing them all away to keep memory usage
349   // down.
350   const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
351   getUnitDIE().collectChildrenAddressRanges(CURanges);
352 
353   // Collect address ranges from DIEs in .dwo if necessary.
354   bool DWOCreated = parseDWO();
355   if (DWO)
356     DWO->collectAddressRanges(CURanges);
357   if (DWOCreated)
358     DWO.reset();
359 
360   // Keep memory down by clearing DIEs if this generate function
361   // caused them to be parsed.
362   if (ClearDIEs)
363     clearDIEs(true);
364 }
365 
366 void DWARFUnit::updateAddressDieMap(DWARFDie Die) {
367   if (Die.isSubroutineDIE()) {
368     for (const auto &R : Die.getAddressRanges()) {
369       // Ignore 0-sized ranges.
370       if (R.LowPC == R.HighPC)
371         continue;
372       auto B = AddrDieMap.upper_bound(R.LowPC);
373       if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) {
374         // The range is a sub-range of existing ranges, we need to split the
375         // existing range.
376         if (R.HighPC < B->second.first)
377           AddrDieMap[R.HighPC] = B->second;
378         if (R.LowPC > B->first)
379           AddrDieMap[B->first].first = R.LowPC;
380       }
381       AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die);
382     }
383   }
384   // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to
385   // simplify the logic to update AddrDieMap. The child's range will always
386   // be equal or smaller than the parent's range. With this assumption, when
387   // adding one range into the map, it will at most split a range into 3
388   // sub-ranges.
389   for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling())
390     updateAddressDieMap(Child);
391 }
392 
393 DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) {
394   extractDIEsIfNeeded(false);
395   if (AddrDieMap.empty())
396     updateAddressDieMap(getUnitDIE());
397   auto R = AddrDieMap.upper_bound(Address);
398   if (R == AddrDieMap.begin())
399     return DWARFDie();
400   // upper_bound's previous item contains Address.
401   --R;
402   if (Address >= R->second.first)
403     return DWARFDie();
404   return R->second.second;
405 }
406 
407 void
408 DWARFUnit::getInlinedChainForAddress(uint64_t Address,
409                                      SmallVectorImpl<DWARFDie> &InlinedChain) {
410   assert(InlinedChain.empty());
411   // Try to look for subprogram DIEs in the DWO file.
412   parseDWO();
413   // First, find the subroutine that contains the given address (the leaf
414   // of inlined chain).
415   DWARFDie SubroutineDIE =
416       (DWO ? DWO.get() : this)->getSubroutineForAddress(Address);
417 
418   if (!SubroutineDIE)
419     return;
420 
421   while (!SubroutineDIE.isSubprogramDIE()) {
422     if (SubroutineDIE.getTag() == DW_TAG_inlined_subroutine)
423       InlinedChain.push_back(SubroutineDIE);
424     SubroutineDIE  = SubroutineDIE.getParent();
425   }
426   InlinedChain.push_back(SubroutineDIE);
427 }
428 
429 const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
430                                               DWARFSectionKind Kind) {
431   if (Kind == DW_SECT_INFO)
432     return Context.getCUIndex();
433   assert(Kind == DW_SECT_TYPES);
434   return Context.getTUIndex();
435 }
436 
437 DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
438   if (!Die)
439     return DWARFDie();
440   const uint32_t Depth = Die->getDepth();
441   // Unit DIEs always have a depth of zero and never have parents.
442   if (Depth == 0)
443     return DWARFDie();
444   // Depth of 1 always means parent is the compile/type unit.
445   if (Depth == 1)
446     return getUnitDIE();
447   // Look for previous DIE with a depth that is one less than the Die's depth.
448   const uint32_t ParentDepth = Depth - 1;
449   for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
450     if (DieArray[I].getDepth() == ParentDepth)
451       return DWARFDie(this, &DieArray[I]);
452   }
453   return DWARFDie();
454 }
455 
456 DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
457   if (!Die)
458     return DWARFDie();
459   uint32_t Depth = Die->getDepth();
460   // Unit DIEs always have a depth of zero and never have siblings.
461   if (Depth == 0)
462     return DWARFDie();
463   // NULL DIEs don't have siblings.
464   if (Die->getAbbreviationDeclarationPtr() == nullptr)
465     return DWARFDie();
466 
467   // Find the next DIE whose depth is the same as the Die's depth.
468   for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
469        ++I) {
470     if (DieArray[I].getDepth() == Depth)
471       return DWARFDie(this, &DieArray[I]);
472   }
473   return DWARFDie();
474 }
475 
476 DWARFDie DWARFUnit::getFirstChild(const DWARFDebugInfoEntry *Die) {
477   if (!Die->hasChildren())
478     return DWARFDie();
479 
480   // We do not want access out of bounds when parsing corrupted debug data.
481   size_t I = getDIEIndex(Die) + 1;
482   if (I >= DieArray.size())
483     return DWARFDie();
484   return DWARFDie(this, &DieArray[I]);
485 }
486 
487 const DWARFAbbreviationDeclarationSet *DWARFUnit::getAbbreviations() const {
488   if (!Abbrevs)
489     Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
490   return Abbrevs;
491 }
492 
493 Optional<StrOffsetsContributionDescriptor>
494 StrOffsetsContributionDescriptor::validateContributionSize(
495     DWARFDataExtractor &DA) {
496   uint8_t EntrySize = getDwarfOffsetByteSize();
497   // In order to ensure that we don't read a partial record at the end of
498   // the section we validate for a multiple of the entry size.
499   uint64_t ValidationSize = alignTo(Size, EntrySize);
500   // Guard against overflow.
501   if (ValidationSize >= Size)
502     if (DA.isValidOffsetForDataOfSize((uint32_t)Base, ValidationSize))
503       return *this;
504   return Optional<StrOffsetsContributionDescriptor>();
505 }
506 
507 // Look for a DWARF64-formatted contribution to the string offsets table
508 // starting at a given offset and record it in a descriptor.
509 static Optional<StrOffsetsContributionDescriptor>
510 parseDWARF64StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
511   if (!DA.isValidOffsetForDataOfSize(Offset, 16))
512     return Optional<StrOffsetsContributionDescriptor>();
513 
514   if (DA.getU32(&Offset) != 0xffffffff)
515     return Optional<StrOffsetsContributionDescriptor>();
516 
517   uint64_t Size = DA.getU64(&Offset);
518   uint8_t Version = DA.getU16(&Offset);
519   (void)DA.getU16(&Offset); // padding
520   return StrOffsetsContributionDescriptor(Offset, Size, Version, DWARF64);
521   //return Optional<StrOffsetsContributionDescriptor>(Descriptor);
522 }
523 
524 // Look for a DWARF32-formatted contribution to the string offsets table
525 // starting at a given offset and record it in a descriptor.
526 static Optional<StrOffsetsContributionDescriptor>
527 parseDWARF32StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
528   if (!DA.isValidOffsetForDataOfSize(Offset, 8))
529     return Optional<StrOffsetsContributionDescriptor>();
530   uint32_t ContributionSize = DA.getU32(&Offset);
531   if (ContributionSize >= 0xfffffff0)
532     return Optional<StrOffsetsContributionDescriptor>();
533   uint8_t Version = DA.getU16(&Offset);
534   (void)DA.getU16(&Offset); // padding
535   return StrOffsetsContributionDescriptor(Offset, ContributionSize, Version, DWARF32);
536   //return Optional<StrOffsetsContributionDescriptor>(Descriptor);
537 }
538 
539 Optional<StrOffsetsContributionDescriptor>
540 DWARFUnit::determineStringOffsetsTableContribution(DWARFDataExtractor &DA,
541                                                    uint64_t Offset) {
542   Optional<StrOffsetsContributionDescriptor> Descriptor;
543   // Attempt to find a DWARF64 contribution 16 bytes before the base.
544   if (Offset >= 16)
545     Descriptor =
546         parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset - 16);
547   // Try to find a DWARF32 contribution 8 bytes before the base.
548   if (!Descriptor && Offset >= 8)
549     Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset - 8);
550   return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor;
551 }
552 
553 Optional<StrOffsetsContributionDescriptor>
554 DWARFUnit::determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA,
555                                                       uint64_t Offset) {
556   if (getVersion() >= 5) {
557     // Look for a valid contribution at the given offset.
558     auto Descriptor =
559         parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset);
560     if (!Descriptor)
561       Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset);
562     return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor;
563   }
564   // Prior to DWARF v5, we derive the contribution size from the
565   // index table (in a package file). In a .dwo file it is simply
566   // the length of the string offsets section.
567   uint64_t Size = 0;
568   if (!IndexEntry)
569     Size = StringOffsetSection.Data.size();
570   else if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
571     Size = C->Length;
572   // Return a descriptor with the given offset as base, version 4 and
573   // DWARF32 format.
574   //return Optional<StrOffsetsContributionDescriptor>(
575       //StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32));
576   return StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32);
577 }
578