1 //===- DWARFUnit.h ----------------------------------------------*- C++ -*-===//
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 #ifndef LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
10 #define LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
11
12 #include "llvm/ADT/DenseSet.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/iterator_range.h"
17 #include "llvm/BinaryFormat/Dwarf.h"
18 #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
21 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
22 #include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
23 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
24 #include "llvm/Support/DataExtractor.h"
25 #include <cassert>
26 #include <cstddef>
27 #include <cstdint>
28 #include <map>
29 #include <memory>
30 #include <set>
31 #include <utility>
32 #include <vector>
33
34 namespace llvm {
35
36 class DWARFAbbreviationDeclarationSet;
37 class DWARFContext;
38 class DWARFDebugAbbrev;
39 class DWARFUnit;
40 class DWARFDebugRangeList;
41 class DWARFLocationTable;
42 class DWARFObject;
43 class raw_ostream;
44 struct DIDumpOptions;
45 struct DWARFSection;
46 namespace dwarf_linker {
47 namespace parallel {
48 class CompileUnit;
49 }
50 } // namespace dwarf_linker
51
52 /// Base class describing the header of any kind of "unit." Some information
53 /// is specific to certain unit types. We separate this class out so we can
54 /// parse the header before deciding what specific kind of unit to construct.
55 class DWARFUnitHeader {
56 // Offset within section.
57 uint64_t Offset = 0;
58 // Version, address size, and DWARF format.
59 dwarf::FormParams FormParams;
60 uint64_t Length = 0;
61 uint64_t AbbrOffset = 0;
62
63 // For DWO units only.
64 const DWARFUnitIndex::Entry *IndexEntry = nullptr;
65
66 // For type units only.
67 uint64_t TypeHash = 0;
68 uint64_t TypeOffset = 0;
69
70 // For v5 split or skeleton compile units only.
71 std::optional<uint64_t> DWOId;
72
73 // Unit type as parsed, or derived from the section kind.
74 uint8_t UnitType = 0;
75
76 // Size as parsed. uint8_t for compactness.
77 uint8_t Size = 0;
78
79 public:
80 /// Parse a unit header from \p debug_info starting at \p offset_ptr.
81 /// Note that \p SectionKind is used as a hint to guess the unit type
82 /// for DWARF formats prior to DWARFv5. In DWARFv5 the unit type is
83 /// explicitly defined in the header and the hint is ignored.
84 Error extract(DWARFContext &Context, const DWARFDataExtractor &debug_info,
85 uint64_t *offset_ptr, DWARFSectionKind SectionKind);
86 // For units in DWARF Package File, remember the index entry and update
87 // the abbreviation offset read by extract().
88 Error applyIndexEntry(const DWARFUnitIndex::Entry *Entry);
getOffset()89 uint64_t getOffset() const { return Offset; }
getFormParams()90 const dwarf::FormParams &getFormParams() const { return FormParams; }
getVersion()91 uint16_t getVersion() const { return FormParams.Version; }
getFormat()92 dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
getAddressByteSize()93 uint8_t getAddressByteSize() const { return FormParams.AddrSize; }
getRefAddrByteSize()94 uint8_t getRefAddrByteSize() const { return FormParams.getRefAddrByteSize(); }
getDwarfOffsetByteSize()95 uint8_t getDwarfOffsetByteSize() const {
96 return FormParams.getDwarfOffsetByteSize();
97 }
getLength()98 uint64_t getLength() const { return Length; }
getAbbrOffset()99 uint64_t getAbbrOffset() const { return AbbrOffset; }
getDWOId()100 std::optional<uint64_t> getDWOId() const { return DWOId; }
setDWOId(uint64_t Id)101 void setDWOId(uint64_t Id) {
102 assert((!DWOId || *DWOId == Id) && "setting DWOId to a different value");
103 DWOId = Id;
104 }
getIndexEntry()105 const DWARFUnitIndex::Entry *getIndexEntry() const { return IndexEntry; }
getTypeHash()106 uint64_t getTypeHash() const { return TypeHash; }
getTypeOffset()107 uint64_t getTypeOffset() const { return TypeOffset; }
getUnitType()108 uint8_t getUnitType() const { return UnitType; }
isTypeUnit()109 bool isTypeUnit() const {
110 return UnitType == dwarf::DW_UT_type || UnitType == dwarf::DW_UT_split_type;
111 }
getSize()112 uint8_t getSize() const { return Size; }
getUnitLengthFieldByteSize()113 uint8_t getUnitLengthFieldByteSize() const {
114 return dwarf::getUnitLengthFieldByteSize(FormParams.Format);
115 }
getNextUnitOffset()116 uint64_t getNextUnitOffset() const {
117 return Offset + Length + getUnitLengthFieldByteSize();
118 }
119 };
120
121 const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
122 DWARFSectionKind Kind);
123
124 bool isCompileUnit(const std::unique_ptr<DWARFUnit> &U);
125
126 /// Describe a collection of units. Intended to hold all units either from
127 /// .debug_info and .debug_types, or from .debug_info.dwo and .debug_types.dwo.
128 class DWARFUnitVector final : public SmallVector<std::unique_ptr<DWARFUnit>, 1> {
129 std::function<std::unique_ptr<DWARFUnit>(uint64_t, DWARFSectionKind,
130 const DWARFSection *,
131 const DWARFUnitIndex::Entry *)>
132 Parser;
133 int NumInfoUnits = -1;
134
135 public:
136 using UnitVector = SmallVectorImpl<std::unique_ptr<DWARFUnit>>;
137 using iterator = typename UnitVector::iterator;
138 using iterator_range = llvm::iterator_range<typename UnitVector::iterator>;
139
140 using compile_unit_range =
141 decltype(make_filter_range(std::declval<iterator_range>(), isCompileUnit));
142
143 DWARFUnit *getUnitForOffset(uint64_t Offset) const;
144 DWARFUnit *getUnitForIndexEntry(const DWARFUnitIndex::Entry &E);
145
146 /// Read units from a .debug_info or .debug_types section. Calls made
147 /// before finishedInfoUnits() are assumed to be for .debug_info sections,
148 /// calls after finishedInfoUnits() are for .debug_types sections. Caller
149 /// must not mix calls to addUnitsForSection and addUnitsForDWOSection.
150 void addUnitsForSection(DWARFContext &C, const DWARFSection &Section,
151 DWARFSectionKind SectionKind);
152 /// Read units from a .debug_info.dwo or .debug_types.dwo section. Calls
153 /// made before finishedInfoUnits() are assumed to be for .debug_info.dwo
154 /// sections, calls after finishedInfoUnits() are for .debug_types.dwo
155 /// sections. Caller must not mix calls to addUnitsForSection and
156 /// addUnitsForDWOSection.
157 void addUnitsForDWOSection(DWARFContext &C, const DWARFSection &DWOSection,
158 DWARFSectionKind SectionKind, bool Lazy = false);
159
160 /// Add an existing DWARFUnit to this UnitVector. This is used by the DWARF
161 /// verifier to process unit separately.
162 DWARFUnit *addUnit(std::unique_ptr<DWARFUnit> Unit);
163
164 /// Returns number of all units held by this instance.
getNumUnits()165 unsigned getNumUnits() const { return size(); }
166 /// Returns number of units from all .debug_info[.dwo] sections.
getNumInfoUnits()167 unsigned getNumInfoUnits() const {
168 return NumInfoUnits == -1 ? size() : NumInfoUnits;
169 }
170 /// Returns number of units from all .debug_types[.dwo] sections.
getNumTypesUnits()171 unsigned getNumTypesUnits() const { return size() - NumInfoUnits; }
172 /// Indicate that parsing .debug_info[.dwo] is done, and remaining units
173 /// will be from .debug_types[.dwo].
finishedInfoUnits()174 void finishedInfoUnits() { NumInfoUnits = size(); }
175
176 private:
177 void addUnitsImpl(DWARFContext &Context, const DWARFObject &Obj,
178 const DWARFSection &Section, const DWARFDebugAbbrev *DA,
179 const DWARFSection *RS, const DWARFSection *LocSection,
180 StringRef SS, const DWARFSection &SOS,
181 const DWARFSection *AOS, const DWARFSection &LS, bool LE,
182 bool IsDWO, bool Lazy, DWARFSectionKind SectionKind);
183 };
184
185 /// Represents base address of the CU.
186 /// Represents a unit's contribution to the string offsets table.
187 struct StrOffsetsContributionDescriptor {
188 uint64_t Base = 0;
189 /// The contribution size not including the header.
190 uint64_t Size = 0;
191 /// Format and version.
192 dwarf::FormParams FormParams = {0, 0, dwarf::DwarfFormat::DWARF32};
193
StrOffsetsContributionDescriptorStrOffsetsContributionDescriptor194 StrOffsetsContributionDescriptor(uint64_t Base, uint64_t Size,
195 uint8_t Version, dwarf::DwarfFormat Format)
196 : Base(Base), Size(Size), FormParams({Version, 0, Format}) {}
197 StrOffsetsContributionDescriptor() = default;
198
getVersionStrOffsetsContributionDescriptor199 uint8_t getVersion() const { return FormParams.Version; }
getFormatStrOffsetsContributionDescriptor200 dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
getDwarfOffsetByteSizeStrOffsetsContributionDescriptor201 uint8_t getDwarfOffsetByteSize() const {
202 return FormParams.getDwarfOffsetByteSize();
203 }
204 /// Determine whether a contribution to the string offsets table is
205 /// consistent with the relevant section size and that its length is
206 /// a multiple of the size of one of its entries.
207 Expected<StrOffsetsContributionDescriptor>
208 validateContributionSize(DWARFDataExtractor &DA);
209 };
210
211 class DWARFUnit {
212 DWARFContext &Context;
213 /// Section containing this DWARFUnit.
214 const DWARFSection &InfoSection;
215
216 DWARFUnitHeader Header;
217 const DWARFDebugAbbrev *Abbrev;
218 const DWARFSection *RangeSection;
219 uint64_t RangeSectionBase;
220 uint64_t LocSectionBase;
221
222 /// Location table of this unit.
223 std::unique_ptr<DWARFLocationTable> LocTable;
224
225 const DWARFSection &LineSection;
226 StringRef StringSection;
227 const DWARFSection &StringOffsetSection;
228 const DWARFSection *AddrOffsetSection;
229 DWARFUnit *SU;
230 std::optional<uint64_t> AddrOffsetSectionBase;
231 bool IsLittleEndian;
232 bool IsDWO;
233 const DWARFUnitVector &UnitVector;
234
235 /// Start, length, and DWARF format of the unit's contribution to the string
236 /// offsets table (DWARF v5).
237 std::optional<StrOffsetsContributionDescriptor>
238 StringOffsetsTableContribution;
239
240 mutable const DWARFAbbreviationDeclarationSet *Abbrevs;
241 std::optional<object::SectionedAddress> BaseAddr;
242 /// The compile unit debug information entry items.
243 std::vector<DWARFDebugInfoEntry> DieArray;
244
245 /// Map from range's start address to end address and corresponding DIE.
246 /// IntervalMap does not support range removal, as a result, we use the
247 /// std::map::upper_bound for address range lookup.
248 std::map<uint64_t, std::pair<uint64_t, DWARFDie>> AddrDieMap;
249
250 /// Map from the location (interpreted DW_AT_location) of a DW_TAG_variable,
251 /// to the end address and the corresponding DIE.
252 std::map<uint64_t, std::pair<uint64_t, DWARFDie>> VariableDieMap;
253 DenseSet<uint64_t> RootsParsedForVariables;
254
255 using die_iterator_range =
256 iterator_range<std::vector<DWARFDebugInfoEntry>::iterator>;
257
258 std::shared_ptr<DWARFUnit> DWO;
259
260 protected:
261 friend dwarf_linker::parallel::CompileUnit;
262
263 /// Return the index of a \p Die entry inside the unit's DIE vector.
264 ///
265 /// It is illegal to call this method with a DIE that hasn't be
266 /// created by this unit. In other word, it's illegal to call this
267 /// method on a DIE that isn't accessible by following
268 /// children/sibling links starting from this unit's getUnitDIE().
getDIEIndex(const DWARFDebugInfoEntry * Die)269 uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) const {
270 auto First = DieArray.data();
271 assert(Die >= First && Die < First + DieArray.size());
272 return Die - First;
273 }
274
275 /// Return DWARFDebugInfoEntry for the specified index \p Index.
getDebugInfoEntry(unsigned Index)276 const DWARFDebugInfoEntry *getDebugInfoEntry(unsigned Index) const {
277 assert(Index < DieArray.size());
278 return &DieArray[Index];
279 }
280
281 const DWARFDebugInfoEntry *
282 getParentEntry(const DWARFDebugInfoEntry *Die) const;
283 const DWARFDebugInfoEntry *
284 getSiblingEntry(const DWARFDebugInfoEntry *Die) const;
285 const DWARFDebugInfoEntry *
286 getPreviousSiblingEntry(const DWARFDebugInfoEntry *Die) const;
287 const DWARFDebugInfoEntry *
288 getFirstChildEntry(const DWARFDebugInfoEntry *Die) const;
289 const DWARFDebugInfoEntry *
290 getLastChildEntry(const DWARFDebugInfoEntry *Die) const;
291
getHeader()292 const DWARFUnitHeader &getHeader() const { return Header; }
293
294 /// Find the unit's contribution to the string offsets table and determine its
295 /// length and form. The given offset is expected to be derived from the unit
296 /// DIE's DW_AT_str_offsets_base attribute.
297 Expected<std::optional<StrOffsetsContributionDescriptor>>
298 determineStringOffsetsTableContribution(DWARFDataExtractor &DA);
299
300 /// Find the unit's contribution to the string offsets table and determine its
301 /// length and form. The given offset is expected to be 0 in a dwo file or,
302 /// in a dwp file, the start of the unit's contribution to the string offsets
303 /// table section (as determined by the index table).
304 Expected<std::optional<StrOffsetsContributionDescriptor>>
305 determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA);
306
307 public:
308 DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
309 const DWARFUnitHeader &Header, const DWARFDebugAbbrev *DA,
310 const DWARFSection *RS, const DWARFSection *LocSection,
311 StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS,
312 const DWARFSection &LS, bool LE, bool IsDWO,
313 const DWARFUnitVector &UnitVector);
314
315 virtual ~DWARFUnit();
316
isLittleEndian()317 bool isLittleEndian() const { return IsLittleEndian; }
isDWOUnit()318 bool isDWOUnit() const { return IsDWO; }
getContext()319 DWARFContext& getContext() const { return Context; }
getInfoSection()320 const DWARFSection &getInfoSection() const { return InfoSection; }
getOffset()321 uint64_t getOffset() const { return Header.getOffset(); }
getFormParams()322 const dwarf::FormParams &getFormParams() const {
323 return Header.getFormParams();
324 }
getVersion()325 uint16_t getVersion() const { return Header.getVersion(); }
getAddressByteSize()326 uint8_t getAddressByteSize() const { return Header.getAddressByteSize(); }
getRefAddrByteSize()327 uint8_t getRefAddrByteSize() const { return Header.getRefAddrByteSize(); }
getDwarfOffsetByteSize()328 uint8_t getDwarfOffsetByteSize() const {
329 return Header.getDwarfOffsetByteSize();
330 }
331 /// Size in bytes of the parsed unit header.
getHeaderSize()332 uint32_t getHeaderSize() const { return Header.getSize(); }
getLength()333 uint64_t getLength() const { return Header.getLength(); }
getFormat()334 dwarf::DwarfFormat getFormat() const { return Header.getFormat(); }
getUnitType()335 uint8_t getUnitType() const { return Header.getUnitType(); }
isTypeUnit()336 bool isTypeUnit() const { return Header.isTypeUnit(); }
getAbbrOffset()337 uint64_t getAbbrOffset() const { return Header.getAbbrOffset(); }
getNextUnitOffset()338 uint64_t getNextUnitOffset() const { return Header.getNextUnitOffset(); }
getLineSection()339 const DWARFSection &getLineSection() const { return LineSection; }
getStringSection()340 StringRef getStringSection() const { return StringSection; }
getStringOffsetSection()341 const DWARFSection &getStringOffsetSection() const {
342 return StringOffsetSection;
343 }
344
setSkeletonUnit(DWARFUnit * SU)345 void setSkeletonUnit(DWARFUnit *SU) { this->SU = SU; }
346 // Returns itself if not using Split DWARF, or if the unit is a skeleton unit
347 // - otherwise returns the split full unit's corresponding skeleton, if
348 // available.
getLinkedUnit()349 DWARFUnit *getLinkedUnit() { return IsDWO ? SU : this; }
350
setAddrOffsetSection(const DWARFSection * AOS,uint64_t Base)351 void setAddrOffsetSection(const DWARFSection *AOS, uint64_t Base) {
352 AddrOffsetSection = AOS;
353 AddrOffsetSectionBase = Base;
354 }
355
getAddrOffsetSectionBase()356 std::optional<uint64_t> getAddrOffsetSectionBase() const {
357 return AddrOffsetSectionBase;
358 }
359
360 /// Returns offset to the indexed address value inside .debug_addr section.
getIndexedAddressOffset(uint64_t Index)361 std::optional<uint64_t> getIndexedAddressOffset(uint64_t Index) {
362 if (std::optional<uint64_t> AddrOffsetSectionBase =
363 getAddrOffsetSectionBase())
364 return *AddrOffsetSectionBase + Index * getAddressByteSize();
365
366 return std::nullopt;
367 }
368
369 /// Recursively update address to Die map.
370 void updateAddressDieMap(DWARFDie Die);
371
372 /// Recursively update address to variable Die map.
373 void updateVariableDieMap(DWARFDie Die);
374
setRangesSection(const DWARFSection * RS,uint64_t Base)375 void setRangesSection(const DWARFSection *RS, uint64_t Base) {
376 RangeSection = RS;
377 RangeSectionBase = Base;
378 }
379
getLocSectionBase()380 uint64_t getLocSectionBase() const {
381 return LocSectionBase;
382 }
383
384 std::optional<object::SectionedAddress>
385 getAddrOffsetSectionItem(uint32_t Index) const;
386 Expected<uint64_t> getStringOffsetSectionItem(uint32_t Index) const;
387
388 DWARFDataExtractor getDebugInfoExtractor() const;
389
getStringExtractor()390 DataExtractor getStringExtractor() const {
391 return DataExtractor(StringSection, false, 0);
392 }
393
getLocationTable()394 const DWARFLocationTable &getLocationTable() { return *LocTable; }
395
396 /// Extract the range list referenced by this compile unit from the
397 /// .debug_ranges section. If the extraction is unsuccessful, an error
398 /// is returned. Successful extraction requires that the compile unit
399 /// has already been extracted.
400 Error extractRangeList(uint64_t RangeListOffset,
401 DWARFDebugRangeList &RangeList) const;
402 void clear();
403
404 const std::optional<StrOffsetsContributionDescriptor> &
getStringOffsetsTableContribution()405 getStringOffsetsTableContribution() {
406 extractDIEsIfNeeded(true /*CUDIeOnly*/);
407 return StringOffsetsTableContribution;
408 }
409
getDwarfStringOffsetsByteSize()410 uint8_t getDwarfStringOffsetsByteSize() const {
411 assert(StringOffsetsTableContribution);
412 return StringOffsetsTableContribution->getDwarfOffsetByteSize();
413 }
414
getStringOffsetsBase()415 uint64_t getStringOffsetsBase() const {
416 assert(StringOffsetsTableContribution);
417 return StringOffsetsTableContribution->Base;
418 }
419
getAbbreviationsOffset()420 uint64_t getAbbreviationsOffset() const { return Header.getAbbrOffset(); }
421
422 const DWARFAbbreviationDeclarationSet *getAbbreviations() const;
423
isMatchingUnitTypeAndTag(uint8_t UnitType,dwarf::Tag Tag)424 static bool isMatchingUnitTypeAndTag(uint8_t UnitType, dwarf::Tag Tag) {
425 switch (UnitType) {
426 case dwarf::DW_UT_compile:
427 return Tag == dwarf::DW_TAG_compile_unit;
428 case dwarf::DW_UT_type:
429 return Tag == dwarf::DW_TAG_type_unit;
430 case dwarf::DW_UT_partial:
431 return Tag == dwarf::DW_TAG_partial_unit;
432 case dwarf::DW_UT_skeleton:
433 return Tag == dwarf::DW_TAG_skeleton_unit;
434 case dwarf::DW_UT_split_compile:
435 case dwarf::DW_UT_split_type:
436 return dwarf::isUnitType(Tag);
437 }
438 return false;
439 }
440
441 std::optional<object::SectionedAddress> getBaseAddress();
442
443 DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
444 extractDIEsIfNeeded(ExtractUnitDIEOnly);
445 if (DieArray.empty())
446 return DWARFDie();
447 return DWARFDie(this, &DieArray[0]);
448 }
449
450 DWARFDie getNonSkeletonUnitDIE(bool ExtractUnitDIEOnly = true,
451 StringRef DWOAlternativeLocation = {}) {
452 parseDWO(DWOAlternativeLocation);
453 return DWO ? DWO->getUnitDIE(ExtractUnitDIEOnly)
454 : getUnitDIE(ExtractUnitDIEOnly);
455 }
456
457 const char *getCompilationDir();
getDWOId()458 std::optional<uint64_t> getDWOId() {
459 extractDIEsIfNeeded(/*CUDieOnly*/ true);
460 return getHeader().getDWOId();
461 }
setDWOId(uint64_t NewID)462 void setDWOId(uint64_t NewID) { Header.setDWOId(NewID); }
463
464 /// Return a vector of address ranges resulting from a (possibly encoded)
465 /// range list starting at a given offset in the appropriate ranges section.
466 Expected<DWARFAddressRangesVector> findRnglistFromOffset(uint64_t Offset);
467
468 /// Return a vector of address ranges retrieved from an encoded range
469 /// list whose offset is found via a table lookup given an index (DWARF v5
470 /// and later).
471 Expected<DWARFAddressRangesVector> findRnglistFromIndex(uint32_t Index);
472
473 /// Return a rangelist's offset based on an index. The index designates
474 /// an entry in the rangelist table's offset array and is supplied by
475 /// DW_FORM_rnglistx.
476 std::optional<uint64_t> getRnglistOffset(uint32_t Index);
477
478 std::optional<uint64_t> getLoclistOffset(uint32_t Index);
479
480 Expected<DWARFAddressRangesVector> collectAddressRanges();
481
482 Expected<DWARFLocationExpressionsVector>
483 findLoclistFromOffset(uint64_t Offset);
484
485 /// Returns subprogram DIE with address range encompassing the provided
486 /// address. The pointer is alive as long as parsed compile unit DIEs are not
487 /// cleared.
488 DWARFDie getSubroutineForAddress(uint64_t Address);
489
490 /// Returns variable DIE for the address provided. The pointer is alive as
491 /// long as parsed compile unit DIEs are not cleared.
492 DWARFDie getVariableForAddress(uint64_t Address);
493
494 /// getInlinedChainForAddress - fetches inlined chain for a given address.
495 /// Returns empty chain if there is no subprogram containing address. The
496 /// chain is valid as long as parsed compile unit DIEs are not cleared.
497 void getInlinedChainForAddress(uint64_t Address,
498 SmallVectorImpl<DWARFDie> &InlinedChain);
499
500 /// Return the DWARFUnitVector containing this unit.
getUnitVector()501 const DWARFUnitVector &getUnitVector() const { return UnitVector; }
502
503 /// Returns the number of DIEs in the unit. Parses the unit
504 /// if necessary.
getNumDIEs()505 unsigned getNumDIEs() {
506 extractDIEsIfNeeded(false);
507 return DieArray.size();
508 }
509
510 /// Return the index of a DIE inside the unit's DIE vector.
511 ///
512 /// It is illegal to call this method with a DIE that hasn't be
513 /// created by this unit. In other word, it's illegal to call this
514 /// method on a DIE that isn't accessible by following
515 /// children/sibling links starting from this unit's getUnitDIE().
getDIEIndex(const DWARFDie & D)516 uint32_t getDIEIndex(const DWARFDie &D) const {
517 return getDIEIndex(D.getDebugInfoEntry());
518 }
519
520 /// Return the DIE object at the given index \p Index.
getDIEAtIndex(unsigned Index)521 DWARFDie getDIEAtIndex(unsigned Index) {
522 return DWARFDie(this, getDebugInfoEntry(Index));
523 }
524
525 DWARFDie getParent(const DWARFDebugInfoEntry *Die);
526 DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
527 DWARFDie getPreviousSibling(const DWARFDebugInfoEntry *Die);
528 DWARFDie getFirstChild(const DWARFDebugInfoEntry *Die);
529 DWARFDie getLastChild(const DWARFDebugInfoEntry *Die);
530
531 /// Return the DIE object for a given offset \p Offset inside the
532 /// unit's DIE vector.
getDIEForOffset(uint64_t Offset)533 DWARFDie getDIEForOffset(uint64_t Offset) {
534 if (std::optional<uint32_t> DieIdx = getDIEIndexForOffset(Offset))
535 return DWARFDie(this, &DieArray[*DieIdx]);
536
537 return DWARFDie();
538 }
539
540 /// Return the DIE index for a given offset \p Offset inside the
541 /// unit's DIE vector.
getDIEIndexForOffset(uint64_t Offset)542 std::optional<uint32_t> getDIEIndexForOffset(uint64_t Offset) {
543 extractDIEsIfNeeded(false);
544 auto It =
545 llvm::partition_point(DieArray, [=](const DWARFDebugInfoEntry &DIE) {
546 return DIE.getOffset() < Offset;
547 });
548 if (It != DieArray.end() && It->getOffset() == Offset)
549 return It - DieArray.begin();
550 return std::nullopt;
551 }
552
getLineTableOffset()553 uint32_t getLineTableOffset() const {
554 if (auto IndexEntry = Header.getIndexEntry())
555 if (const auto *Contrib = IndexEntry->getContribution(DW_SECT_LINE))
556 return Contrib->getOffset32();
557 return 0;
558 }
559
dies()560 die_iterator_range dies() {
561 extractDIEsIfNeeded(false);
562 return die_iterator_range(DieArray.begin(), DieArray.end());
563 }
564
565 virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) = 0;
566
567 Error tryExtractDIEsIfNeeded(bool CUDieOnly);
568
569 private:
570 /// Size in bytes of the .debug_info data associated with this compile unit.
getDebugInfoSize()571 size_t getDebugInfoSize() const {
572 return Header.getLength() + Header.getUnitLengthFieldByteSize() -
573 getHeaderSize();
574 }
575
576 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
577 /// hasn't already been done
578 void extractDIEsIfNeeded(bool CUDieOnly);
579
580 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
581 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
582 std::vector<DWARFDebugInfoEntry> &DIEs) const;
583
584 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
585 void clearDIEs(bool KeepCUDie);
586
587 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
588 /// it was actually constructed.
589 /// The \p AlternativeLocation specifies an alternative location to get
590 /// the DWARF context for the DWO object; this is the case when it has
591 /// been moved from its original location.
592 bool parseDWO(StringRef AlternativeLocation = {});
593 };
594
isCompileUnit(const std::unique_ptr<DWARFUnit> & U)595 inline bool isCompileUnit(const std::unique_ptr<DWARFUnit> &U) {
596 return !U->isTypeUnit();
597 }
598
599 } // end namespace llvm
600
601 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
602