1 //===- DWARFUnit.h ----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
10 #define LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
12 #include "llvm/ADT/Optional.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/DWARFDebugInfoEntry.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
21 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
22 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
23 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
24 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
25 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
26 #include "llvm/Support/DataExtractor.h"
38 class DWARFAbbreviationDeclarationSet
;
40 class DWARFDebugAbbrev
;
43 /// Base class describing the header of any kind of "unit." Some information
44 /// is specific to certain unit types. We separate this class out so we can
45 /// parse the header before deciding what specific kind of unit to construct.
46 class DWARFUnitHeader
{
47 // Offset within section.
49 // Version, address size, and DWARF format.
50 dwarf::FormParams FormParams
;
52 uint64_t AbbrOffset
= 0;
54 // For DWO units only.
55 const DWARFUnitIndex::Entry
*IndexEntry
= nullptr;
57 // For type units only.
58 uint64_t TypeHash
= 0;
59 uint64_t TypeOffset
= 0;
61 // For v5 split or skeleton compile units only.
62 Optional
<uint64_t> DWOId
;
64 // Unit type as parsed, or derived from the section kind.
67 // Size as parsed. uint8_t for compactness.
71 /// Parse a unit header from \p debug_info starting at \p offset_ptr.
72 bool extract(DWARFContext
&Context
, const DWARFDataExtractor
&debug_info
,
73 uint64_t *offset_ptr
, DWARFSectionKind Kind
= DW_SECT_INFO
,
74 const DWARFUnitIndex
*Index
= nullptr,
75 const DWARFUnitIndex::Entry
*Entry
= nullptr);
76 uint64_t getOffset() const { return Offset
; }
77 const dwarf::FormParams
&getFormParams() const { return FormParams
; }
78 uint16_t getVersion() const { return FormParams
.Version
; }
79 dwarf::DwarfFormat
getFormat() const { return FormParams
.Format
; }
80 uint8_t getAddressByteSize() const { return FormParams
.AddrSize
; }
81 uint8_t getRefAddrByteSize() const { return FormParams
.getRefAddrByteSize(); }
82 uint8_t getDwarfOffsetByteSize() const {
83 return FormParams
.getDwarfOffsetByteSize();
85 uint64_t getLength() const { return Length
; }
86 uint64_t getAbbrOffset() const { return AbbrOffset
; }
87 Optional
<uint64_t> getDWOId() const { return DWOId
; }
88 void setDWOId(uint64_t Id
) {
89 assert((!DWOId
|| *DWOId
== Id
) && "setting DWOId to a different value");
92 const DWARFUnitIndex::Entry
*getIndexEntry() const { return IndexEntry
; }
93 uint64_t getTypeHash() const { return TypeHash
; }
94 uint64_t getTypeOffset() const { return TypeOffset
; }
95 uint8_t getUnitType() const { return UnitType
; }
96 bool isTypeUnit() const {
97 return UnitType
== dwarf::DW_UT_type
|| UnitType
== dwarf::DW_UT_split_type
;
99 uint8_t getSize() const { return Size
; }
100 uint8_t getUnitLengthFieldByteSize() const {
101 return dwarf::getUnitLengthFieldByteSize(FormParams
.Format
);
103 uint64_t getNextUnitOffset() const {
104 return Offset
+ Length
+ getUnitLengthFieldByteSize();
108 const DWARFUnitIndex
&getDWARFUnitIndex(DWARFContext
&Context
,
109 DWARFSectionKind Kind
);
111 /// Describe a collection of units. Intended to hold all units either from
112 /// .debug_info and .debug_types, or from .debug_info.dwo and .debug_types.dwo.
113 class DWARFUnitVector final
: public SmallVector
<std::unique_ptr
<DWARFUnit
>, 1> {
114 std::function
<std::unique_ptr
<DWARFUnit
>(uint64_t, DWARFSectionKind
,
115 const DWARFSection
*,
116 const DWARFUnitIndex::Entry
*)>
118 int NumInfoUnits
= -1;
121 using UnitVector
= SmallVectorImpl
<std::unique_ptr
<DWARFUnit
>>;
122 using iterator
= typename
UnitVector::iterator
;
123 using iterator_range
= llvm::iterator_range
<typename
UnitVector::iterator
>;
125 DWARFUnit
*getUnitForOffset(uint64_t Offset
) const;
126 DWARFUnit
*getUnitForIndexEntry(const DWARFUnitIndex::Entry
&E
);
128 /// Read units from a .debug_info or .debug_types section. Calls made
129 /// before finishedInfoUnits() are assumed to be for .debug_info sections,
130 /// calls after finishedInfoUnits() are for .debug_types sections. Caller
131 /// must not mix calls to addUnitsForSection and addUnitsForDWOSection.
132 void addUnitsForSection(DWARFContext
&C
, const DWARFSection
&Section
,
133 DWARFSectionKind SectionKind
);
134 /// Read units from a .debug_info.dwo or .debug_types.dwo section. Calls
135 /// made before finishedInfoUnits() are assumed to be for .debug_info.dwo
136 /// sections, calls after finishedInfoUnits() are for .debug_types.dwo
137 /// sections. Caller must not mix calls to addUnitsForSection and
138 /// addUnitsForDWOSection.
139 void addUnitsForDWOSection(DWARFContext
&C
, const DWARFSection
&DWOSection
,
140 DWARFSectionKind SectionKind
, bool Lazy
= false);
142 /// Add an existing DWARFUnit to this UnitVector. This is used by the DWARF
143 /// verifier to process unit separately.
144 DWARFUnit
*addUnit(std::unique_ptr
<DWARFUnit
> Unit
);
146 /// Returns number of all units held by this instance.
147 unsigned getNumUnits() const { return size(); }
148 /// Returns number of units from all .debug_info[.dwo] sections.
149 unsigned getNumInfoUnits() const {
150 return NumInfoUnits
== -1 ? size() : NumInfoUnits
;
152 /// Returns number of units from all .debug_types[.dwo] sections.
153 unsigned getNumTypesUnits() const { return size() - NumInfoUnits
; }
154 /// Indicate that parsing .debug_info[.dwo] is done, and remaining units
155 /// will be from .debug_types[.dwo].
156 void finishedInfoUnits() { NumInfoUnits
= size(); }
159 void addUnitsImpl(DWARFContext
&Context
, const DWARFObject
&Obj
,
160 const DWARFSection
&Section
, const DWARFDebugAbbrev
*DA
,
161 const DWARFSection
*RS
, const DWARFSection
*LocSection
,
162 StringRef SS
, const DWARFSection
&SOS
,
163 const DWARFSection
*AOS
, const DWARFSection
&LS
, bool LE
,
164 bool IsDWO
, bool Lazy
, DWARFSectionKind SectionKind
);
167 /// Represents base address of the CU.
168 /// Represents a unit's contribution to the string offsets table.
169 struct StrOffsetsContributionDescriptor
{
171 /// The contribution size not including the header.
173 /// Format and version.
174 dwarf::FormParams FormParams
= {0, 0, dwarf::DwarfFormat::DWARF32
};
176 StrOffsetsContributionDescriptor(uint64_t Base
, uint64_t Size
,
177 uint8_t Version
, dwarf::DwarfFormat Format
)
178 : Base(Base
), Size(Size
), FormParams({Version
, 0, Format
}) {}
179 StrOffsetsContributionDescriptor() = default;
181 uint8_t getVersion() const { return FormParams
.Version
; }
182 dwarf::DwarfFormat
getFormat() const { return FormParams
.Format
; }
183 uint8_t getDwarfOffsetByteSize() const {
184 return FormParams
.getDwarfOffsetByteSize();
186 /// Determine whether a contribution to the string offsets table is
187 /// consistent with the relevant section size and that its length is
188 /// a multiple of the size of one of its entries.
189 Expected
<StrOffsetsContributionDescriptor
>
190 validateContributionSize(DWARFDataExtractor
&DA
);
194 DWARFContext
&Context
;
195 /// Section containing this DWARFUnit.
196 const DWARFSection
&InfoSection
;
198 DWARFUnitHeader Header
;
199 const DWARFDebugAbbrev
*Abbrev
;
200 const DWARFSection
*RangeSection
;
201 uint64_t RangeSectionBase
;
202 /// We either keep track of the location list section or its data, depending
203 /// on whether we are handling a split DWARF section or not.
205 const DWARFSection
*LocSection
;
206 StringRef LocSectionData
;
208 const DWARFSection
&LineSection
;
209 StringRef StringSection
;
210 const DWARFSection
&StringOffsetSection
;
211 const DWARFSection
*AddrOffsetSection
;
212 uint32_t AddrOffsetSectionBase
= 0;
215 const DWARFUnitVector
&UnitVector
;
217 /// Start, length, and DWARF format of the unit's contribution to the string
218 /// offsets table (DWARF v5).
219 Optional
<StrOffsetsContributionDescriptor
> StringOffsetsTableContribution
;
221 /// A table of range lists (DWARF v5 and later).
222 Optional
<DWARFDebugRnglistTable
> RngListTable
;
224 mutable const DWARFAbbreviationDeclarationSet
*Abbrevs
;
225 llvm::Optional
<object::SectionedAddress
> BaseAddr
;
226 /// The compile unit debug information entry items.
227 std::vector
<DWARFDebugInfoEntry
> DieArray
;
229 /// Map from range's start address to end address and corresponding DIE.
230 /// IntervalMap does not support range removal, as a result, we use the
231 /// std::map::upper_bound for address range lookup.
232 std::map
<uint64_t, std::pair
<uint64_t, DWARFDie
>> AddrDieMap
;
234 using die_iterator_range
=
235 iterator_range
<std::vector
<DWARFDebugInfoEntry
>::iterator
>;
237 std::shared_ptr
<DWARFUnit
> DWO
;
239 uint32_t getDIEIndex(const DWARFDebugInfoEntry
*Die
) {
240 auto First
= DieArray
.data();
241 assert(Die
>= First
&& Die
< First
+ DieArray
.size());
246 const DWARFUnitHeader
&getHeader() const { return Header
; }
248 /// Size in bytes of the parsed unit header.
249 uint32_t getHeaderSize() const { return Header
.getSize(); }
251 /// Find the unit's contribution to the string offsets table and determine its
252 /// length and form. The given offset is expected to be derived from the unit
253 /// DIE's DW_AT_str_offsets_base attribute.
254 Expected
<Optional
<StrOffsetsContributionDescriptor
>>
255 determineStringOffsetsTableContribution(DWARFDataExtractor
&DA
);
257 /// Find the unit's contribution to the string offsets table and determine its
258 /// length and form. The given offset is expected to be 0 in a dwo file or,
259 /// in a dwp file, the start of the unit's contribution to the string offsets
260 /// table section (as determined by the index table).
261 Expected
<Optional
<StrOffsetsContributionDescriptor
>>
262 determineStringOffsetsTableContributionDWO(DWARFDataExtractor
&DA
);
265 DWARFUnit(DWARFContext
&Context
, const DWARFSection
&Section
,
266 const DWARFUnitHeader
&Header
, const DWARFDebugAbbrev
*DA
,
267 const DWARFSection
*RS
, const DWARFSection
*LocSection
,
268 StringRef SS
, const DWARFSection
&SOS
, const DWARFSection
*AOS
,
269 const DWARFSection
&LS
, bool LE
, bool IsDWO
,
270 const DWARFUnitVector
&UnitVector
);
272 virtual ~DWARFUnit();
274 bool isDWOUnit() const { return IsDWO
; }
275 DWARFContext
& getContext() const { return Context
; }
276 const DWARFSection
&getInfoSection() const { return InfoSection
; }
277 const DWARFSection
*getLocSection() const { return LocSection
; }
278 StringRef
getLocSectionData() const { return LocSectionData
; }
279 uint64_t getOffset() const { return Header
.getOffset(); }
280 const dwarf::FormParams
&getFormParams() const {
281 return Header
.getFormParams();
283 uint16_t getVersion() const { return Header
.getVersion(); }
284 uint8_t getAddressByteSize() const { return Header
.getAddressByteSize(); }
285 uint8_t getRefAddrByteSize() const { return Header
.getRefAddrByteSize(); }
286 uint8_t getDwarfOffsetByteSize() const {
287 return Header
.getDwarfOffsetByteSize();
289 uint64_t getLength() const { return Header
.getLength(); }
290 uint8_t getUnitType() const { return Header
.getUnitType(); }
291 bool isTypeUnit() const { return Header
.isTypeUnit(); }
292 uint64_t getNextUnitOffset() const { return Header
.getNextUnitOffset(); }
293 const DWARFSection
&getLineSection() const { return LineSection
; }
294 StringRef
getStringSection() const { return StringSection
; }
295 const DWARFSection
&getStringOffsetSection() const {
296 return StringOffsetSection
;
299 void setAddrOffsetSection(const DWARFSection
*AOS
, uint32_t Base
) {
300 AddrOffsetSection
= AOS
;
301 AddrOffsetSectionBase
= Base
;
304 /// Recursively update address to Die map.
305 void updateAddressDieMap(DWARFDie Die
);
307 void setRangesSection(const DWARFSection
*RS
, uint64_t Base
) {
309 RangeSectionBase
= Base
;
312 Optional
<object::SectionedAddress
>
313 getAddrOffsetSectionItem(uint32_t Index
) const;
314 Optional
<uint64_t> getStringOffsetSectionItem(uint32_t Index
) const;
316 DWARFDataExtractor
getDebugInfoExtractor() const;
318 DataExtractor
getStringExtractor() const {
319 return DataExtractor(StringSection
, false, 0);
322 /// Extract the range list referenced by this compile unit from the
323 /// .debug_ranges section. If the extraction is unsuccessful, an error
324 /// is returned. Successful extraction requires that the compile unit
325 /// has already been extracted.
326 Error
extractRangeList(uint64_t RangeListOffset
,
327 DWARFDebugRangeList
&RangeList
) const;
330 const Optional
<StrOffsetsContributionDescriptor
> &
331 getStringOffsetsTableContribution() const {
332 return StringOffsetsTableContribution
;
335 uint8_t getDwarfStringOffsetsByteSize() const {
336 assert(StringOffsetsTableContribution
);
337 return StringOffsetsTableContribution
->getDwarfOffsetByteSize();
340 uint64_t getStringOffsetsBase() const {
341 assert(StringOffsetsTableContribution
);
342 return StringOffsetsTableContribution
->Base
;
345 const DWARFAbbreviationDeclarationSet
*getAbbreviations() const;
347 static bool isMatchingUnitTypeAndTag(uint8_t UnitType
, dwarf::Tag Tag
) {
349 case dwarf::DW_UT_compile
:
350 return Tag
== dwarf::DW_TAG_compile_unit
;
351 case dwarf::DW_UT_type
:
352 return Tag
== dwarf::DW_TAG_type_unit
;
353 case dwarf::DW_UT_partial
:
354 return Tag
== dwarf::DW_TAG_partial_unit
;
355 case dwarf::DW_UT_skeleton
:
356 return Tag
== dwarf::DW_TAG_skeleton_unit
;
357 case dwarf::DW_UT_split_compile
:
358 case dwarf::DW_UT_split_type
:
359 return dwarf::isUnitType(Tag
);
364 /// Return the number of bytes for the header of a unit of
367 /// This function must be called with a valid unit type which in
368 /// DWARF5 is defined as one of the following six types.
369 static uint32_t getDWARF5HeaderSize(uint8_t UnitType
) {
371 case dwarf::DW_UT_compile
:
372 case dwarf::DW_UT_partial
:
374 case dwarf::DW_UT_skeleton
:
375 case dwarf::DW_UT_split_compile
:
377 case dwarf::DW_UT_type
:
378 case dwarf::DW_UT_split_type
:
381 llvm_unreachable("Invalid UnitType.");
384 llvm::Optional
<object::SectionedAddress
> getBaseAddress();
386 DWARFDie
getUnitDIE(bool ExtractUnitDIEOnly
= true) {
387 extractDIEsIfNeeded(ExtractUnitDIEOnly
);
388 if (DieArray
.empty())
390 return DWARFDie(this, &DieArray
[0]);
393 DWARFDie
getNonSkeletonUnitDIE(bool ExtractUnitDIEOnly
= true) {
396 return DWO
->getUnitDIE(ExtractUnitDIEOnly
);
397 return getUnitDIE(ExtractUnitDIEOnly
);
400 const char *getCompilationDir();
401 Optional
<uint64_t> getDWOId() {
402 extractDIEsIfNeeded(/*CUDieOnly*/ true);
403 return getHeader().getDWOId();
405 void setDWOId(uint64_t NewID
) { Header
.setDWOId(NewID
); }
407 /// Return a vector of address ranges resulting from a (possibly encoded)
408 /// range list starting at a given offset in the appropriate ranges section.
409 Expected
<DWARFAddressRangesVector
> findRnglistFromOffset(uint64_t Offset
);
411 /// Return a vector of address ranges retrieved from an encoded range
412 /// list whose offset is found via a table lookup given an index (DWARF v5
414 Expected
<DWARFAddressRangesVector
> findRnglistFromIndex(uint32_t Index
);
416 /// Return a rangelist's offset based on an index. The index designates
417 /// an entry in the rangelist table's offset array and is supplied by
418 /// DW_FORM_rnglistx.
419 Optional
<uint64_t> getRnglistOffset(uint32_t Index
) {
421 return RngListTable
->getOffsetEntry(Index
);
425 Expected
<DWARFAddressRangesVector
> collectAddressRanges();
427 /// Returns subprogram DIE with address range encompassing the provided
428 /// address. The pointer is alive as long as parsed compile unit DIEs are not
430 DWARFDie
getSubroutineForAddress(uint64_t Address
);
432 /// getInlinedChainForAddress - fetches inlined chain for a given address.
433 /// Returns empty chain if there is no subprogram containing address. The
434 /// chain is valid as long as parsed compile unit DIEs are not cleared.
435 void getInlinedChainForAddress(uint64_t Address
,
436 SmallVectorImpl
<DWARFDie
> &InlinedChain
);
438 /// Return the DWARFUnitVector containing this unit.
439 const DWARFUnitVector
&getUnitVector() const { return UnitVector
; }
441 /// Returns the number of DIEs in the unit. Parses the unit
443 unsigned getNumDIEs() {
444 extractDIEsIfNeeded(false);
445 return DieArray
.size();
448 /// Return the index of a DIE inside the unit's DIE vector.
450 /// It is illegal to call this method with a DIE that hasn't be
451 /// created by this unit. In other word, it's illegal to call this
452 /// method on a DIE that isn't accessible by following
453 /// children/sibling links starting from this unit's getUnitDIE().
454 uint32_t getDIEIndex(const DWARFDie
&D
) {
455 return getDIEIndex(D
.getDebugInfoEntry());
458 /// Return the DIE object at the given index.
459 DWARFDie
getDIEAtIndex(unsigned Index
) {
460 assert(Index
< DieArray
.size());
461 return DWARFDie(this, &DieArray
[Index
]);
464 DWARFDie
getParent(const DWARFDebugInfoEntry
*Die
);
465 DWARFDie
getSibling(const DWARFDebugInfoEntry
*Die
);
466 DWARFDie
getPreviousSibling(const DWARFDebugInfoEntry
*Die
);
467 DWARFDie
getFirstChild(const DWARFDebugInfoEntry
*Die
);
468 DWARFDie
getLastChild(const DWARFDebugInfoEntry
*Die
);
470 /// Return the DIE object for a given offset inside the
471 /// unit's DIE vector.
473 /// The unit needs to have its DIEs extracted for this method to work.
474 DWARFDie
getDIEForOffset(uint64_t Offset
) {
475 extractDIEsIfNeeded(false);
476 assert(!DieArray
.empty());
478 llvm::partition_point(DieArray
, [=](const DWARFDebugInfoEntry
&DIE
) {
479 return DIE
.getOffset() < Offset
;
481 if (It
!= DieArray
.end() && It
->getOffset() == Offset
)
482 return DWARFDie(this, &*It
);
486 uint32_t getLineTableOffset() const {
487 if (auto IndexEntry
= Header
.getIndexEntry())
488 if (const auto *Contrib
= IndexEntry
->getOffset(DW_SECT_LINE
))
489 return Contrib
->Offset
;
493 die_iterator_range
dies() {
494 extractDIEsIfNeeded(false);
495 return die_iterator_range(DieArray
.begin(), DieArray
.end());
498 virtual void dump(raw_ostream
&OS
, DIDumpOptions DumpOpts
) = 0;
500 Error
tryExtractDIEsIfNeeded(bool CUDieOnly
);
503 /// Size in bytes of the .debug_info data associated with this compile unit.
504 size_t getDebugInfoSize() const {
505 return Header
.getLength() + Header
.getUnitLengthFieldByteSize() -
509 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
510 /// hasn't already been done
511 void extractDIEsIfNeeded(bool CUDieOnly
);
513 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
514 void extractDIEsToVector(bool AppendCUDie
, bool AppendNonCUDIEs
,
515 std::vector
<DWARFDebugInfoEntry
> &DIEs
) const;
517 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
518 void clearDIEs(bool KeepCUDie
);
520 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
521 /// it was actually constructed.
525 } // end namespace llvm
527 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H