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