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/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 for all DWARFUnitSection classes. This provides the
44 /// functionality common to all unit types.
45 class DWARFUnitSectionBase {
47 /// Returns the Unit that contains the given section offset in the
48 /// same section this Unit originated from.
49 virtual DWARFUnit *getUnitForOffset(uint32_t Offset) const = 0;
50 virtual DWARFUnit *getUnitForIndexEntry(const DWARFUnitIndex::Entry &E) = 0;
52 void parse(DWARFContext &C, const DWARFSection &Section);
53 void parseDWO(DWARFContext &C, const DWARFSection &DWOSection,
57 ~DWARFUnitSectionBase() = default;
59 virtual void parseImpl(DWARFContext &Context, const DWARFSection &Section,
60 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
61 StringRef SS, const DWARFSection &SOS,
62 const DWARFSection *AOS, const DWARFSection &LS,
63 bool isLittleEndian, bool isDWO, bool Lazy) = 0;
66 const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
67 DWARFSectionKind Kind);
69 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
70 template<typename UnitType>
71 class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
72 public DWARFUnitSectionBase {
74 std::function<std::unique_ptr<UnitType>(uint32_t)> Parser;
77 using UnitVector = SmallVectorImpl<std::unique_ptr<UnitType>>;
78 using iterator = typename UnitVector::iterator;
79 using iterator_range = llvm::iterator_range<typename UnitVector::iterator>;
81 UnitType *getUnitForOffset(uint32_t Offset) const override {
82 auto *CU = std::upper_bound(
83 this->begin(), this->end(), Offset,
84 [](uint32_t LHS, const std::unique_ptr<UnitType> &RHS) {
85 return LHS < RHS->getNextUnitOffset();
87 if (CU != this->end() && (*CU)->getOffset() <= Offset)
91 UnitType *getUnitForIndexEntry(const DWARFUnitIndex::Entry &E) override {
92 const auto *CUOff = E.getOffset(DW_SECT_INFO);
96 auto Offset = CUOff->Offset;
98 auto *CU = std::upper_bound(
99 this->begin(), this->end(), CUOff->Offset,
100 [](uint32_t LHS, const std::unique_ptr<UnitType> &RHS) {
101 return LHS < RHS->getNextUnitOffset();
103 if (CU != this->end() && (*CU)->getOffset() <= Offset)
109 auto U = Parser(Offset);
113 auto *NewCU = U.get();
114 this->insert(CU, std::move(U));
119 void parseImpl(DWARFContext &Context, const DWARFSection &Section,
120 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
121 StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS,
122 const DWARFSection &LS, bool LE, bool IsDWO,
123 bool Lazy) override {
126 DataExtractor Data(Section.Data, LE, 0);
128 const DWARFUnitIndex *Index = nullptr;
130 Index = &getDWARFUnitIndex(Context, UnitType::Section);
131 Parser = [=, &Context, &Section, &SOS,
132 &LS](uint32_t Offset) -> std::unique_ptr<UnitType> {
133 if (!Data.isValidOffset(Offset))
135 auto U = llvm::make_unique<UnitType>(
136 Context, Section, DA, RS, SS, SOS, AOS, LS, LE, IsDWO, *this,
137 Index ? Index->getFromOffset(Offset) : nullptr);
138 if (!U->extract(Data, &Offset))
145 auto I = this->begin();
147 while (Data.isValidOffset(Offset)) {
148 if (I != this->end() && (*I)->getOffset() == Offset) {
152 auto U = Parser(Offset);
155 Offset = U->getNextUnitOffset();
156 I = std::next(this->insert(I, std::move(U)));
162 /// Represents base address of the CU.
165 uint64_t SectionIndex;
169 DWARFContext &Context;
170 /// Section containing this DWARFUnit.
171 const DWARFSection &InfoSection;
173 const DWARFDebugAbbrev *Abbrev;
174 const DWARFSection *RangeSection;
175 uint32_t RangeSectionBase;
176 const DWARFSection &LineSection;
177 StringRef StringSection;
178 const DWARFSection &StringOffsetSection;
179 uint64_t StringOffsetSectionBase = 0;
180 const DWARFSection *AddrOffsetSection;
181 uint32_t AddrOffsetSectionBase = 0;
184 const DWARFUnitSectionBase &UnitSection;
186 // Version, address size, and DWARF format.
187 DWARFFormParams FormParams;
191 mutable const DWARFAbbreviationDeclarationSet *Abbrevs;
194 llvm::Optional<BaseAddress> BaseAddr;
195 /// The compile unit debug information entry items.
196 std::vector<DWARFDebugInfoEntry> DieArray;
198 /// Map from range's start address to end address and corresponding DIE.
199 /// IntervalMap does not support range removal, as a result, we use the
200 /// std::map::upper_bound for address range lookup.
201 std::map<uint64_t, std::pair<uint64_t, DWARFDie>> AddrDieMap;
203 using die_iterator_range =
204 iterator_range<std::vector<DWARFDebugInfoEntry>::iterator>;
206 std::shared_ptr<DWARFUnit> DWO;
208 const DWARFUnitIndex::Entry *IndexEntry;
210 uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) {
211 auto First = DieArray.data();
212 assert(Die >= First && Die < First + DieArray.size());
217 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
219 /// Size in bytes of the unit header.
220 virtual uint32_t getHeaderSize() const { return getVersion() <= 4 ? 11 : 12; }
223 DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
224 const DWARFDebugAbbrev *DA, const DWARFSection *RS, StringRef SS,
225 const DWARFSection &SOS, const DWARFSection *AOS,
226 const DWARFSection &LS, bool LE, bool IsDWO,
227 const DWARFUnitSectionBase &UnitSection,
228 const DWARFUnitIndex::Entry *IndexEntry = nullptr);
230 virtual ~DWARFUnit();
232 DWARFContext& getContext() const { return Context; }
234 const DWARFSection &getLineSection() const { return LineSection; }
235 StringRef getStringSection() const { return StringSection; }
236 const DWARFSection &getStringOffsetSection() const {
237 return StringOffsetSection;
240 void setAddrOffsetSection(const DWARFSection *AOS, uint32_t Base) {
241 AddrOffsetSection = AOS;
242 AddrOffsetSectionBase = Base;
245 /// Recursively update address to Die map.
246 void updateAddressDieMap(DWARFDie Die);
248 void setRangesSection(const DWARFSection *RS, uint32_t Base) {
250 RangeSectionBase = Base;
253 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
254 bool getStringOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
256 DWARFDataExtractor getDebugInfoExtractor() const;
258 DataExtractor getStringExtractor() const {
259 return DataExtractor(StringSection, false, 0);
263 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
265 /// extractRangeList - extracts the range list referenced by this compile
266 /// unit from .debug_ranges section. Returns true on success.
267 /// Requires that compile unit is already extracted.
268 bool extractRangeList(uint32_t RangeListOffset,
269 DWARFDebugRangeList &RangeList) const;
271 uint32_t getOffset() const { return Offset; }
272 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
273 uint32_t getLength() const { return Length; }
275 const DWARFFormParams &getFormParams() const { return FormParams; }
276 uint16_t getVersion() const { return FormParams.Version; }
277 dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
278 uint8_t getAddressByteSize() const { return FormParams.AddrSize; }
279 uint8_t getRefAddrByteSize() const { return FormParams.getRefAddrByteSize(); }
280 uint8_t getDwarfOffsetByteSize() const {
281 return FormParams.getDwarfOffsetByteSize();
284 const DWARFAbbreviationDeclarationSet *getAbbreviations() const;
286 uint8_t getUnitType() const { return UnitType; }
288 static bool isMatchingUnitTypeAndTag(uint8_t UnitType, dwarf::Tag Tag) {
290 case dwarf::DW_UT_compile:
291 return Tag == dwarf::DW_TAG_compile_unit;
292 case dwarf::DW_UT_type:
293 return Tag == dwarf::DW_TAG_type_unit;
294 case dwarf::DW_UT_partial:
295 return Tag == dwarf::DW_TAG_partial_unit;
296 case dwarf::DW_UT_skeleton:
297 return Tag == dwarf::DW_TAG_skeleton_unit;
298 case dwarf::DW_UT_split_compile:
299 case dwarf::DW_UT_split_type:
300 return dwarf::isUnitType(Tag);
305 /// \brief Return the number of bytes for the header of a unit of
308 /// This function must be called with a valid unit type which in
309 /// DWARF5 is defined as one of the following six types.
310 static uint32_t getDWARF5HeaderSize(uint8_t UnitType) {
312 case dwarf::DW_UT_compile:
313 case dwarf::DW_UT_partial:
315 case dwarf::DW_UT_skeleton:
316 case dwarf::DW_UT_split_compile:
318 case dwarf::DW_UT_type:
319 case dwarf::DW_UT_split_type:
322 llvm_unreachable("Invalid UnitType.");
325 llvm::Optional<BaseAddress> getBaseAddress() const { return BaseAddr; }
327 void setBaseAddress(BaseAddress BaseAddr) { this->BaseAddr = BaseAddr; }
329 DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
330 extractDIEsIfNeeded(ExtractUnitDIEOnly);
331 if (DieArray.empty())
333 return DWARFDie(this, &DieArray[0]);
336 const char *getCompilationDir();
337 Optional<uint64_t> getDWOId();
339 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
341 /// getInlinedChainForAddress - fetches inlined chain for a given address.
342 /// Returns empty chain if there is no subprogram containing address. The
343 /// chain is valid as long as parsed compile unit DIEs are not cleared.
344 void getInlinedChainForAddress(uint64_t Address,
345 SmallVectorImpl<DWARFDie> &InlinedChain);
347 /// getUnitSection - Return the DWARFUnitSection containing this unit.
348 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
350 /// \brief Returns the number of DIEs in the unit. Parses the unit
352 unsigned getNumDIEs() {
353 extractDIEsIfNeeded(false);
354 return DieArray.size();
357 /// \brief Return the index of a DIE inside the unit's DIE vector.
359 /// It is illegal to call this method with a DIE that hasn't be
360 /// created by this unit. In other word, it's illegal to call this
361 /// method on a DIE that isn't accessible by following
362 /// children/sibling links starting from this unit's getUnitDIE().
363 uint32_t getDIEIndex(const DWARFDie &D) {
364 return getDIEIndex(D.getDebugInfoEntry());
367 /// \brief Return the DIE object at the given index.
368 DWARFDie getDIEAtIndex(unsigned Index) {
369 assert(Index < DieArray.size());
370 return DWARFDie(this, &DieArray[Index]);
373 DWARFDie getParent(const DWARFDebugInfoEntry *Die);
374 DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
375 DWARFDie getFirstChild(const DWARFDebugInfoEntry *Die);
377 /// \brief Return the DIE object for a given offset inside the
378 /// unit's DIE vector.
380 /// The unit needs to have its DIEs extracted for this method to work.
381 DWARFDie getDIEForOffset(uint32_t Offset) {
382 extractDIEsIfNeeded(false);
383 assert(!DieArray.empty());
384 auto it = std::lower_bound(
385 DieArray.begin(), DieArray.end(), Offset,
386 [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) {
387 return LHS.getOffset() < Offset;
389 if (it != DieArray.end() && it->getOffset() == Offset)
390 return DWARFDie(this, &*it);
394 uint32_t getLineTableOffset() const {
396 if (const auto *Contrib = IndexEntry->getOffset(DW_SECT_LINE))
397 return Contrib->Offset;
401 die_iterator_range dies() {
402 extractDIEsIfNeeded(false);
403 return die_iterator_range(DieArray.begin(), DieArray.end());
407 /// Size in bytes of the .debug_info data associated with this compile unit.
408 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
410 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
411 /// hasn't already been done. Returns the number of DIEs parsed at this call.
412 size_t extractDIEsIfNeeded(bool CUDieOnly);
414 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
415 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
416 std::vector<DWARFDebugInfoEntry> &DIEs) const;
418 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
419 void clearDIEs(bool KeepCUDie);
421 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
422 /// it was actually constructed.
425 /// getSubroutineForAddress - Returns subprogram DIE with address range
426 /// encompassing the provided address. The pointer is alive as long as parsed
427 /// compile unit DIEs are not cleared.
428 DWARFDie getSubroutineForAddress(uint64_t Address);
431 } // end namespace llvm
433 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H