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_LIB_DEBUGINFO_DWARFUNIT_H
11 #define LLVM_LIB_DEBUGINFO_DWARFUNIT_H
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
15 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
16 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
17 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
18 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
19 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
29 class DWARFDebugAbbrev;
34 /// Base class for all DWARFUnitSection classes. This provides the
35 /// functionality common to all unit types.
36 class DWARFUnitSectionBase {
38 /// Returns the Unit that contains the given section offset in the
39 /// same section this Unit originated from.
40 virtual DWARFUnit *getUnitForOffset(uint32_t Offset) const = 0;
42 void parse(DWARFContext &C, const DWARFSection &Section);
43 void parseDWO(DWARFContext &C, const DWARFSection &DWOSection,
44 DWARFUnitIndex *Index = nullptr);
47 virtual void parseImpl(DWARFContext &Context, const DWARFSection &Section,
48 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
49 StringRef SOS, StringRef AOS, bool isLittleEndian) = 0;
51 ~DWARFUnitSectionBase() = default;
54 const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
55 DWARFSectionKind Kind);
57 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
58 template<typename UnitType>
59 class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
60 public DWARFUnitSectionBase {
64 DWARFUnitSection() : Parsed(false) {}
65 DWARFUnitSection(DWARFUnitSection &&DUS) :
66 SmallVector<std::unique_ptr<UnitType>, 1>(std::move(DUS)), Parsed(DUS.Parsed) {}
68 typedef llvm::SmallVectorImpl<std::unique_ptr<UnitType>> UnitVector;
69 typedef typename UnitVector::iterator iterator;
70 typedef llvm::iterator_range<typename UnitVector::iterator> iterator_range;
72 UnitType *getUnitForOffset(uint32_t Offset) const override {
73 auto *CU = std::upper_bound(
74 this->begin(), this->end(), Offset,
75 [](uint32_t LHS, const std::unique_ptr<UnitType> &RHS) {
76 return LHS < RHS->getNextUnitOffset();
78 if (CU != this->end())
84 void parseImpl(DWARFContext &Context, const DWARFSection &Section,
85 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
86 StringRef SOS, StringRef AOS, bool LE) override {
89 const auto &Index = getDWARFUnitIndex(Context, UnitType::Section);
90 DataExtractor Data(Section.Data, LE, 0);
92 while (Data.isValidOffset(Offset)) {
94 llvm::make_unique<UnitType>(Context, Section, DA, RS, SS, SOS, AOS,
95 LE, *this, Index.getFromOffset(Offset));
96 if (!U->extract(Data, &Offset))
98 this->push_back(std::move(U));
99 Offset = this->back()->getNextUnitOffset();
106 DWARFContext &Context;
107 // Section containing this DWARFUnit.
108 const DWARFSection &InfoSection;
110 const DWARFDebugAbbrev *Abbrev;
111 StringRef RangeSection;
112 uint32_t RangeSectionBase;
113 StringRef StringSection;
114 StringRef StringOffsetSection;
115 StringRef AddrOffsetSection;
116 uint32_t AddrOffsetSectionBase;
118 const DWARFUnitSectionBase &UnitSection;
123 const DWARFAbbreviationDeclarationSet *Abbrevs;
126 // The compile unit debug information entry items.
127 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
130 object::OwningBinary<object::ObjectFile> DWOFile;
131 std::unique_ptr<DWARFContext> DWOContext;
134 DWOHolder(StringRef DWOPath);
135 DWARFUnit *getUnit() const { return DWOU; }
137 std::unique_ptr<DWOHolder> DWO;
139 const DWARFUnitIndex::Entry *IndexEntry;
142 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
143 /// Size in bytes of the unit header.
144 virtual uint32_t getHeaderSize() const { return 11; }
147 DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
148 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
149 StringRef SOS, StringRef AOS, bool LE,
150 const DWARFUnitSectionBase &UnitSection,
151 const DWARFUnitIndex::Entry *IndexEntry = nullptr);
153 virtual ~DWARFUnit();
155 DWARFContext& getContext() const { return Context; }
157 StringRef getStringSection() const { return StringSection; }
158 StringRef getStringOffsetSection() const { return StringOffsetSection; }
159 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
160 AddrOffsetSection = AOS;
161 AddrOffsetSectionBase = Base;
163 void setRangesSection(StringRef RS, uint32_t Base) {
165 RangeSectionBase = Base;
168 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
169 // FIXME: Result should be uint64_t in DWARF64.
170 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
172 DataExtractor getDebugInfoExtractor() const {
173 return DataExtractor(InfoSection.Data, isLittleEndian, AddrSize);
175 DataExtractor getStringExtractor() const {
176 return DataExtractor(StringSection, false, 0);
179 const RelocAddrMap *getRelocMap() const { return &InfoSection.Relocs; }
181 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
183 /// extractRangeList - extracts the range list referenced by this compile
184 /// unit from .debug_ranges section. Returns true on success.
185 /// Requires that compile unit is already extracted.
186 bool extractRangeList(uint32_t RangeListOffset,
187 DWARFDebugRangeList &RangeList) const;
189 uint32_t getOffset() const { return Offset; }
190 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
191 uint32_t getLength() const { return Length; }
192 uint16_t getVersion() const { return Version; }
193 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
196 uint8_t getAddressByteSize() const { return AddrSize; }
197 uint64_t getBaseAddress() const { return BaseAddr; }
199 void setBaseAddress(uint64_t base_addr) {
200 BaseAddr = base_addr;
203 const DWARFDebugInfoEntryMinimal *getUnitDIE(bool ExtractUnitDIEOnly = true) {
204 extractDIEsIfNeeded(ExtractUnitDIEOnly);
205 return DieArray.empty() ? nullptr : &DieArray[0];
208 const char *getCompilationDir();
211 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
213 /// getInlinedChainForAddress - fetches inlined chain for a given address.
214 /// Returns empty chain if there is no subprogram containing address. The
215 /// chain is valid as long as parsed compile unit DIEs are not cleared.
216 DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
218 /// getUnitSection - Return the DWARFUnitSection containing this unit.
219 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
221 /// \brief Returns the number of DIEs in the unit. Parses the unit
223 unsigned getNumDIEs() {
224 extractDIEsIfNeeded(false);
225 return DieArray.size();
228 /// \brief Return the index of a DIE inside the unit's DIE vector.
230 /// It is illegal to call this method with a DIE that hasn't be
231 /// created by this unit. In other word, it's illegal to call this
232 /// method on a DIE that isn't accessible by following
233 /// children/sibling links starting from this unit's getUnitDIE().
234 uint32_t getDIEIndex(const DWARFDebugInfoEntryMinimal *DIE) {
235 assert(!DieArray.empty() && DIE >= &DieArray[0] &&
236 DIE < &DieArray[0] + DieArray.size());
237 return DIE - &DieArray[0];
240 /// \brief Return the DIE object at the given index.
241 const DWARFDebugInfoEntryMinimal *getDIEAtIndex(unsigned Index) const {
242 assert(Index < DieArray.size());
243 return &DieArray[Index];
246 /// \brief Return the DIE object for a given offset inside the
247 /// unit's DIE vector.
249 /// The unit needs to have his DIEs extracted for this method to work.
250 const DWARFDebugInfoEntryMinimal *getDIEForOffset(uint32_t Offset) const {
251 assert(!DieArray.empty());
252 auto it = std::lower_bound(
253 DieArray.begin(), DieArray.end(), Offset,
254 [=](const DWARFDebugInfoEntryMinimal &LHS, uint32_t Offset) {
255 return LHS.getOffset() < Offset;
257 return it == DieArray.end() ? nullptr : &*it;
261 /// Size in bytes of the .debug_info data associated with this compile unit.
262 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
264 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
265 /// hasn't already been done. Returns the number of DIEs parsed at this call.
266 size_t extractDIEsIfNeeded(bool CUDieOnly);
267 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
268 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
269 std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
270 /// setDIERelations - We read in all of the DIE entries into our flat list
271 /// of DIE entries and now we need to go back through all of them and set the
272 /// parent, sibling and child pointers for quick DIE navigation.
273 void setDIERelations();
274 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
275 void clearDIEs(bool KeepCUDie);
277 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
278 /// it was actually constructed.
281 /// getSubprogramForAddress - Returns subprogram DIE with address range
282 /// encompassing the provided address. The pointer is alive as long as parsed
283 /// compile unit DIEs are not cleared.
284 const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);