1 //===- DWARFUnit.h ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
11 #define LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/iterator_range.h"
18 #include "llvm/BinaryFormat/Dwarf.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
21 #include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
22 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
23 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
24 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
25 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
26 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
27 #include "llvm/Support/DataExtractor.h"
39 class DWARFAbbreviationDeclarationSet;
41 class DWARFDebugAbbrev;
44 /// Base class describing the header of any kind of "unit." Some information
45 /// is specific to certain unit types. We separate this class out so we can
46 /// parse the header before deciding what specific kind of unit to construct.
47 class DWARFUnitHeader {
48 // Offset within section.
50 // Version, address size, and DWARF format.
51 dwarf::FormParams FormParams;
53 uint64_t AbbrOffset = 0;
55 // For DWO units only.
56 const DWARFUnitIndex::Entry *IndexEntry = nullptr;
58 // For type units only.
59 uint64_t TypeHash = 0;
60 uint32_t TypeOffset = 0;
62 // For v5 split or skeleton compile units only.
63 Optional<uint64_t> DWOId;
65 // Unit type as parsed, or derived from the section kind.
68 // Size as parsed. uint8_t for compactness.
72 /// Parse a unit header from \p debug_info starting at \p offset_ptr.
73 bool extract(DWARFContext &Context, const DWARFDataExtractor &debug_info,
74 uint32_t *offset_ptr, DWARFSectionKind Kind = DW_SECT_INFO,
75 const DWARFUnitIndex *Index = nullptr,
76 const DWARFUnitIndex::Entry *Entry = nullptr);
77 uint32_t getOffset() const { return Offset; }
78 const dwarf::FormParams &getFormParams() const { return FormParams; }
79 uint16_t getVersion() const { return FormParams.Version; }
80 dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
81 uint8_t getAddressByteSize() const { return FormParams.AddrSize; }
82 uint8_t getRefAddrByteSize() const { return FormParams.getRefAddrByteSize(); }
83 uint8_t getDwarfOffsetByteSize() const {
84 return FormParams.getDwarfOffsetByteSize();
86 uint32_t getLength() const { return Length; }
87 uint64_t getAbbrOffset() const { return AbbrOffset; }
88 Optional<uint64_t> getDWOId() const { return DWOId; }
89 void setDWOId(uint64_t Id) {
90 assert((!DWOId || *DWOId == Id) && "setting DWOId to a different value");
93 const DWARFUnitIndex::Entry *getIndexEntry() const { return IndexEntry; }
94 uint64_t getTypeHash() const { return TypeHash; }
95 uint32_t getTypeOffset() const { return TypeOffset; }
96 uint8_t getUnitType() const { return UnitType; }
97 bool isTypeUnit() const {
98 return UnitType == dwarf::DW_UT_type || UnitType == dwarf::DW_UT_split_type;
100 uint8_t getSize() const { return Size; }
101 // FIXME: Support DWARF64.
102 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
105 const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
106 DWARFSectionKind Kind);
108 /// Describe a collection of units. Intended to hold all units either from
109 /// .debug_info and .debug_types, or from .debug_info.dwo and .debug_types.dwo.
110 class DWARFUnitVector final : public SmallVector<std::unique_ptr<DWARFUnit>, 1> {
111 std::function<std::unique_ptr<DWARFUnit>(uint32_t, DWARFSectionKind,
112 const DWARFSection *,
113 const DWARFUnitIndex::Entry *)>
115 int NumInfoUnits = -1;
118 using UnitVector = SmallVectorImpl<std::unique_ptr<DWARFUnit>>;
119 using iterator = typename UnitVector::iterator;
120 using iterator_range = llvm::iterator_range<typename UnitVector::iterator>;
122 DWARFUnit *getUnitForOffset(uint32_t Offset) const;
123 DWARFUnit *getUnitForIndexEntry(const DWARFUnitIndex::Entry &E);
125 /// Read units from a .debug_info or .debug_types section. Calls made
126 /// before finishedInfoUnits() are assumed to be for .debug_info sections,
127 /// calls after finishedInfoUnits() are for .debug_types sections. Caller
128 /// must not mix calls to addUnitsForSection and addUnitsForDWOSection.
129 void addUnitsForSection(DWARFContext &C, const DWARFSection &Section,
130 DWARFSectionKind SectionKind);
131 /// Read units from a .debug_info.dwo or .debug_types.dwo section. Calls
132 /// made before finishedInfoUnits() are assumed to be for .debug_info.dwo
133 /// sections, calls after finishedInfoUnits() are for .debug_types.dwo
134 /// sections. Caller must not mix calls to addUnitsForSection and
135 /// addUnitsForDWOSection.
136 void addUnitsForDWOSection(DWARFContext &C, const DWARFSection &DWOSection,
137 DWARFSectionKind SectionKind, bool Lazy = false);
139 /// Add an existing DWARFUnit to this UnitVector. This is used by the DWARF
140 /// verifier to process unit separately.
141 DWARFUnit *addUnit(std::unique_ptr<DWARFUnit> Unit);
143 /// Returns number of all units held by this instance.
144 unsigned getNumUnits() const { return size(); }
145 /// Returns number of units from all .debug_info[.dwo] sections.
146 unsigned getNumInfoUnits() const {
147 return NumInfoUnits == -1 ? size() : NumInfoUnits;
149 /// Returns number of units from all .debug_types[.dwo] sections.
150 unsigned getNumTypesUnits() const { return size() - NumInfoUnits; }
151 /// Indicate that parsing .debug_info[.dwo] is done, and remaining units
152 /// will be from .debug_types[.dwo].
153 void finishedInfoUnits() { NumInfoUnits = size(); }
156 void addUnitsImpl(DWARFContext &Context, const DWARFObject &Obj,
157 const DWARFSection &Section, const DWARFDebugAbbrev *DA,
158 const DWARFSection *RS, const DWARFSection *LocSection,
159 StringRef SS, const DWARFSection &SOS,
160 const DWARFSection *AOS, const DWARFSection &LS, bool LE,
161 bool IsDWO, bool Lazy, DWARFSectionKind SectionKind);
164 /// Represents base address of the CU.
165 /// Represents a unit's contribution to the string offsets table.
166 struct StrOffsetsContributionDescriptor {
168 /// The contribution size not including the header.
170 /// Format and version.
171 dwarf::FormParams FormParams = {0, 0, dwarf::DwarfFormat::DWARF32};
173 StrOffsetsContributionDescriptor(uint64_t Base, uint64_t Size,
174 uint8_t Version, dwarf::DwarfFormat Format)
175 : Base(Base), Size(Size), FormParams({Version, 0, Format}) {}
177 uint8_t getVersion() const { return FormParams.Version; }
178 dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
179 uint8_t getDwarfOffsetByteSize() const {
180 return FormParams.getDwarfOffsetByteSize();
182 /// Determine whether a contribution to the string offsets table is
183 /// consistent with the relevant section size and that its length is
184 /// a multiple of the size of one of its entries.
185 Optional<StrOffsetsContributionDescriptor>
186 validateContributionSize(DWARFDataExtractor &DA);
190 DWARFContext &Context;
191 /// Section containing this DWARFUnit.
192 const DWARFSection &InfoSection;
194 DWARFUnitHeader Header;
195 const DWARFDebugAbbrev *Abbrev;
196 const DWARFSection *RangeSection;
197 uint32_t RangeSectionBase;
198 /// We either keep track of the location list section or its data, depending
199 /// on whether we are handling a split DWARF section or not.
201 const DWARFSection *LocSection;
202 StringRef LocSectionData;
204 const DWARFSection &LineSection;
205 StringRef StringSection;
206 const DWARFSection &StringOffsetSection;
207 const DWARFSection *AddrOffsetSection;
208 uint32_t AddrOffsetSectionBase = 0;
211 const DWARFUnitVector &UnitVector;
213 /// Start, length, and DWARF format of the unit's contribution to the string
214 /// offsets table (DWARF v5).
215 Optional<StrOffsetsContributionDescriptor> StringOffsetsTableContribution;
217 /// A table of range lists (DWARF v5 and later).
218 Optional<DWARFDebugRnglistTable> RngListTable;
220 mutable const DWARFAbbreviationDeclarationSet *Abbrevs;
221 llvm::Optional<SectionedAddress> BaseAddr;
222 /// The compile unit debug information entry items.
223 std::vector<DWARFDebugInfoEntry> DieArray;
225 /// Map from range's start address to end address and corresponding DIE.
226 /// IntervalMap does not support range removal, as a result, we use the
227 /// std::map::upper_bound for address range lookup.
228 std::map<uint64_t, std::pair<uint64_t, DWARFDie>> AddrDieMap;
230 using die_iterator_range =
231 iterator_range<std::vector<DWARFDebugInfoEntry>::iterator>;
233 std::shared_ptr<DWARFUnit> DWO;
235 uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) {
236 auto First = DieArray.data();
237 assert(Die >= First && Die < First + DieArray.size());
242 const DWARFUnitHeader &getHeader() const { return Header; }
244 /// Size in bytes of the parsed unit header.
245 uint32_t getHeaderSize() const { return Header.getSize(); }
247 /// Find the unit's contribution to the string offsets table and determine its
248 /// length and form. The given offset is expected to be derived from the unit
249 /// DIE's DW_AT_str_offsets_base attribute.
250 Optional<StrOffsetsContributionDescriptor>
251 determineStringOffsetsTableContribution(DWARFDataExtractor &DA);
253 /// Find the unit's contribution to the string offsets table and determine its
254 /// length and form. The given offset is expected to be 0 in a dwo file or,
255 /// in a dwp file, the start of the unit's contribution to the string offsets
256 /// table section (as determined by the index table).
257 Optional<StrOffsetsContributionDescriptor>
258 determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA);
261 DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
262 const DWARFUnitHeader &Header, const DWARFDebugAbbrev *DA,
263 const DWARFSection *RS, const DWARFSection *LocSection,
264 StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS,
265 const DWARFSection &LS, bool LE, bool IsDWO,
266 const DWARFUnitVector &UnitVector);
268 virtual ~DWARFUnit();
270 bool isDWOUnit() const { return IsDWO; }
271 DWARFContext& getContext() const { return Context; }
272 const DWARFSection &getInfoSection() const { return InfoSection; }
273 const DWARFSection *getLocSection() const { return LocSection; }
274 StringRef getLocSectionData() const { return LocSectionData; }
275 uint32_t getOffset() const { return Header.getOffset(); }
276 const dwarf::FormParams &getFormParams() const {
277 return Header.getFormParams();
279 uint16_t getVersion() const { return Header.getVersion(); }
280 uint8_t getAddressByteSize() const { return Header.getAddressByteSize(); }
281 uint8_t getRefAddrByteSize() const { return Header.getRefAddrByteSize(); }
282 uint8_t getDwarfOffsetByteSize() const {
283 return Header.getDwarfOffsetByteSize();
285 uint32_t getLength() const { return Header.getLength(); }
286 uint8_t getUnitType() const { return Header.getUnitType(); }
287 bool isTypeUnit() const { return Header.isTypeUnit(); }
288 uint32_t getNextUnitOffset() const { return Header.getNextUnitOffset(); }
289 const DWARFSection &getLineSection() const { return LineSection; }
290 StringRef getStringSection() const { return StringSection; }
291 const DWARFSection &getStringOffsetSection() const {
292 return StringOffsetSection;
295 void setAddrOffsetSection(const DWARFSection *AOS, uint32_t Base) {
296 AddrOffsetSection = AOS;
297 AddrOffsetSectionBase = Base;
300 /// Recursively update address to Die map.
301 void updateAddressDieMap(DWARFDie Die);
303 void setRangesSection(const DWARFSection *RS, uint32_t Base) {
305 RangeSectionBase = Base;
308 Optional<SectionedAddress> getAddrOffsetSectionItem(uint32_t Index) const;
309 Optional<uint64_t> getStringOffsetSectionItem(uint32_t Index) const;
311 DWARFDataExtractor getDebugInfoExtractor() const;
313 DataExtractor getStringExtractor() const {
314 return DataExtractor(StringSection, false, 0);
317 /// Extract the range list referenced by this compile unit from the
318 /// .debug_ranges section. If the extraction is unsuccessful, an error
319 /// is returned. Successful extraction requires that the compile unit
320 /// has already been extracted.
321 Error extractRangeList(uint32_t RangeListOffset,
322 DWARFDebugRangeList &RangeList) const;
325 const Optional<StrOffsetsContributionDescriptor> &
326 getStringOffsetsTableContribution() const {
327 return StringOffsetsTableContribution;
330 uint8_t getDwarfStringOffsetsByteSize() const {
331 assert(StringOffsetsTableContribution);
332 return StringOffsetsTableContribution->getDwarfOffsetByteSize();
335 uint64_t getStringOffsetsBase() const {
336 assert(StringOffsetsTableContribution);
337 return StringOffsetsTableContribution->Base;
340 const DWARFAbbreviationDeclarationSet *getAbbreviations() const;
342 static bool isMatchingUnitTypeAndTag(uint8_t UnitType, dwarf::Tag Tag) {
344 case dwarf::DW_UT_compile:
345 return Tag == dwarf::DW_TAG_compile_unit;
346 case dwarf::DW_UT_type:
347 return Tag == dwarf::DW_TAG_type_unit;
348 case dwarf::DW_UT_partial:
349 return Tag == dwarf::DW_TAG_partial_unit;
350 case dwarf::DW_UT_skeleton:
351 return Tag == dwarf::DW_TAG_skeleton_unit;
352 case dwarf::DW_UT_split_compile:
353 case dwarf::DW_UT_split_type:
354 return dwarf::isUnitType(Tag);
359 /// Return the number of bytes for the header of a unit of
362 /// This function must be called with a valid unit type which in
363 /// DWARF5 is defined as one of the following six types.
364 static uint32_t getDWARF5HeaderSize(uint8_t UnitType) {
366 case dwarf::DW_UT_compile:
367 case dwarf::DW_UT_partial:
369 case dwarf::DW_UT_skeleton:
370 case dwarf::DW_UT_split_compile:
372 case dwarf::DW_UT_type:
373 case dwarf::DW_UT_split_type:
376 llvm_unreachable("Invalid UnitType.");
379 llvm::Optional<SectionedAddress> getBaseAddress();
381 DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
382 extractDIEsIfNeeded(ExtractUnitDIEOnly);
383 if (DieArray.empty())
385 return DWARFDie(this, &DieArray[0]);
388 const char *getCompilationDir();
389 Optional<uint64_t> getDWOId() {
390 extractDIEsIfNeeded(/*CUDieOnly*/ true);
391 return getHeader().getDWOId();
393 void setDWOId(uint64_t NewID) { Header.setDWOId(NewID); }
395 /// Return a vector of address ranges resulting from a (possibly encoded)
396 /// range list starting at a given offset in the appropriate ranges section.
397 Expected<DWARFAddressRangesVector> findRnglistFromOffset(uint32_t Offset);
399 /// Return a vector of address ranges retrieved from an encoded range
400 /// list whose offset is found via a table lookup given an index (DWARF v5
402 Expected<DWARFAddressRangesVector> findRnglistFromIndex(uint32_t Index);
404 /// Return a rangelist's offset based on an index. The index designates
405 /// an entry in the rangelist table's offset array and is supplied by
406 /// DW_FORM_rnglistx.
407 Optional<uint32_t> getRnglistOffset(uint32_t Index) {
409 return RngListTable->getOffsetEntry(Index);
413 Expected<DWARFAddressRangesVector> collectAddressRanges();
415 /// Returns subprogram DIE with address range encompassing the provided
416 /// address. The pointer is alive as long as parsed compile unit DIEs are not
418 DWARFDie getSubroutineForAddress(uint64_t Address);
420 /// getInlinedChainForAddress - fetches inlined chain for a given address.
421 /// Returns empty chain if there is no subprogram containing address. The
422 /// chain is valid as long as parsed compile unit DIEs are not cleared.
423 void getInlinedChainForAddress(uint64_t Address,
424 SmallVectorImpl<DWARFDie> &InlinedChain);
426 /// Return the DWARFUnitVector containing this unit.
427 const DWARFUnitVector &getUnitVector() const { return UnitVector; }
429 /// Returns the number of DIEs in the unit. Parses the unit
431 unsigned getNumDIEs() {
432 extractDIEsIfNeeded(false);
433 return DieArray.size();
436 /// Return the index of a DIE inside the unit's DIE vector.
438 /// It is illegal to call this method with a DIE that hasn't be
439 /// created by this unit. In other word, it's illegal to call this
440 /// method on a DIE that isn't accessible by following
441 /// children/sibling links starting from this unit's getUnitDIE().
442 uint32_t getDIEIndex(const DWARFDie &D) {
443 return getDIEIndex(D.getDebugInfoEntry());
446 /// Return the DIE object at the given index.
447 DWARFDie getDIEAtIndex(unsigned Index) {
448 assert(Index < DieArray.size());
449 return DWARFDie(this, &DieArray[Index]);
452 DWARFDie getParent(const DWARFDebugInfoEntry *Die);
453 DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
454 DWARFDie getPreviousSibling(const DWARFDebugInfoEntry *Die);
455 DWARFDie getFirstChild(const DWARFDebugInfoEntry *Die);
456 DWARFDie getLastChild(const DWARFDebugInfoEntry *Die);
458 /// Return the DIE object for a given offset inside the
459 /// unit's DIE vector.
461 /// The unit needs to have its DIEs extracted for this method to work.
462 DWARFDie getDIEForOffset(uint32_t Offset) {
463 extractDIEsIfNeeded(false);
464 assert(!DieArray.empty());
465 auto it = std::lower_bound(
466 DieArray.begin(), DieArray.end(), Offset,
467 [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) {
468 return LHS.getOffset() < Offset;
470 if (it != DieArray.end() && it->getOffset() == Offset)
471 return DWARFDie(this, &*it);
475 uint32_t getLineTableOffset() const {
476 if (auto IndexEntry = Header.getIndexEntry())
477 if (const auto *Contrib = IndexEntry->getOffset(DW_SECT_LINE))
478 return Contrib->Offset;
482 die_iterator_range dies() {
483 extractDIEsIfNeeded(false);
484 return die_iterator_range(DieArray.begin(), DieArray.end());
487 virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) = 0;
489 /// Size in bytes of the .debug_info data associated with this compile unit.
490 size_t getDebugInfoSize() const {
491 return Header.getLength() + 4 - getHeaderSize();
494 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
495 /// hasn't already been done. Returns the number of DIEs parsed at this call.
496 size_t extractDIEsIfNeeded(bool CUDieOnly);
498 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
499 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
500 std::vector<DWARFDebugInfoEntry> &DIEs) const;
502 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
503 void clearDIEs(bool KeepCUDie);
505 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
506 /// it was actually constructed.
510 } // end namespace llvm
512 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H