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"
28 class DWARFDebugAbbrev;
33 /// Base class for all DWARFUnitSection classes. This provides the
34 /// functionality common to all unit types.
35 class DWARFUnitSectionBase {
37 /// Returns the Unit that contains the given section offset in the
38 /// same section this Unit originated from.
39 virtual DWARFUnit *getUnitForOffset(uint32_t Offset) const = 0;
41 void parse(DWARFContext &C, const DWARFSection &Section);
42 void parseDWO(DWARFContext &C, const DWARFSection &DWOSection);
45 virtual void parseImpl(DWARFContext &Context, const DWARFSection &Section,
46 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
47 StringRef SOS, StringRef AOS, bool isLittleEndian) = 0;
49 ~DWARFUnitSectionBase() = default;
52 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
53 template<typename UnitType>
54 class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
55 public DWARFUnitSectionBase {
57 struct UnitOffsetComparator {
58 bool operator()(uint32_t LHS,
59 const std::unique_ptr<UnitType> &RHS) const {
60 return LHS < RHS->getNextUnitOffset();
67 DWARFUnitSection() : Parsed(false) {}
68 DWARFUnitSection(DWARFUnitSection &&DUS) :
69 SmallVector<std::unique_ptr<UnitType>, 1>(std::move(DUS)), Parsed(DUS.Parsed) {}
71 typedef llvm::SmallVectorImpl<std::unique_ptr<UnitType>> UnitVector;
72 typedef typename UnitVector::iterator iterator;
73 typedef llvm::iterator_range<typename UnitVector::iterator> iterator_range;
75 UnitType *getUnitForOffset(uint32_t Offset) const override {
76 auto *CU = std::upper_bound(this->begin(), this->end(), Offset,
77 UnitOffsetComparator());
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 DataExtractor Data(Section.Data, LE, 0);
91 while (Data.isValidOffset(Offset)) {
92 auto U = llvm::make_unique<UnitType>(Context, Section, DA, RS, SS, SOS,
94 if (!U->extract(Data, &Offset))
96 this->push_back(std::move(U));
97 Offset = this->back()->getNextUnitOffset();
104 DWARFContext &Context;
105 // Section containing this DWARFUnit.
106 const DWARFSection &InfoSection;
108 const DWARFDebugAbbrev *Abbrev;
109 StringRef RangeSection;
110 uint32_t RangeSectionBase;
111 StringRef StringSection;
112 StringRef StringOffsetSection;
113 StringRef AddrOffsetSection;
114 uint32_t AddrOffsetSectionBase;
116 const DWARFUnitSectionBase &UnitSection;
121 const DWARFAbbreviationDeclarationSet *Abbrevs;
124 // The compile unit debug information entry items.
125 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
128 object::OwningBinary<object::ObjectFile> DWOFile;
129 std::unique_ptr<DWARFContext> DWOContext;
132 DWOHolder(StringRef DWOPath);
133 DWARFUnit *getUnit() const { return DWOU; }
135 std::unique_ptr<DWOHolder> DWO;
138 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
139 /// Size in bytes of the unit header.
140 virtual uint32_t getHeaderSize() const { return 11; }
143 DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
144 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
145 StringRef SOS, StringRef AOS, bool LE,
146 const DWARFUnitSectionBase &UnitSection);
148 virtual ~DWARFUnit();
150 DWARFContext& getContext() const { return Context; }
152 StringRef getStringSection() const { return StringSection; }
153 StringRef getStringOffsetSection() const { return StringOffsetSection; }
154 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
155 AddrOffsetSection = AOS;
156 AddrOffsetSectionBase = Base;
158 void setRangesSection(StringRef RS, uint32_t Base) {
160 RangeSectionBase = Base;
163 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
164 // FIXME: Result should be uint64_t in DWARF64.
165 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
167 DataExtractor getDebugInfoExtractor() const {
168 return DataExtractor(InfoSection.Data, isLittleEndian, AddrSize);
170 DataExtractor getStringExtractor() const {
171 return DataExtractor(StringSection, false, 0);
174 const RelocAddrMap *getRelocMap() const { return &InfoSection.Relocs; }
176 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
178 /// extractRangeList - extracts the range list referenced by this compile
179 /// unit from .debug_ranges section. Returns true on success.
180 /// Requires that compile unit is already extracted.
181 bool extractRangeList(uint32_t RangeListOffset,
182 DWARFDebugRangeList &RangeList) const;
184 uint32_t getOffset() const { return Offset; }
185 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
186 uint32_t getLength() const { return Length; }
187 uint16_t getVersion() const { return Version; }
188 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
191 uint8_t getAddressByteSize() const { return AddrSize; }
192 uint64_t getBaseAddress() const { return BaseAddr; }
194 void setBaseAddress(uint64_t base_addr) {
195 BaseAddr = base_addr;
198 const DWARFDebugInfoEntryMinimal *getUnitDIE(bool ExtractUnitDIEOnly = true) {
199 extractDIEsIfNeeded(ExtractUnitDIEOnly);
200 return DieArray.empty() ? nullptr : &DieArray[0];
203 const char *getCompilationDir();
206 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
208 /// getInlinedChainForAddress - fetches inlined chain for a given address.
209 /// Returns empty chain if there is no subprogram containing address. The
210 /// chain is valid as long as parsed compile unit DIEs are not cleared.
211 DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
213 /// getUnitSection - Return the DWARFUnitSection containing this unit.
214 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
216 /// \brief Returns the number of DIEs in the unit. Parses the unit
218 unsigned getNumDIEs() {
219 extractDIEsIfNeeded(false);
220 return DieArray.size();
223 /// \brief Return the index of a DIE inside the unit's DIE vector.
225 /// It is illegal to call this method with a DIE that hasn't be
226 /// created by this unit. In other word, it's illegal to call this
227 /// method on a DIE that isn't accessible by following
228 /// children/sibling links starting from this unit's getUnitDIE().
229 uint32_t getDIEIndex(const DWARFDebugInfoEntryMinimal *DIE) {
230 assert(!DieArray.empty() && DIE >= &DieArray[0] &&
231 DIE < &DieArray[0] + DieArray.size());
232 return DIE - &DieArray[0];
235 /// \brief Return the DIE object at the given index.
236 const DWARFDebugInfoEntryMinimal *getDIEAtIndex(unsigned Index) const {
237 assert(Index < DieArray.size());
238 return &DieArray[Index];
241 /// \brief Return the DIE object for a given offset inside the
242 /// unit's DIE vector.
244 /// The unit needs to have his DIEs extracted for this method to work.
245 const DWARFDebugInfoEntryMinimal *getDIEForOffset(uint32_t Offset) const {
246 assert(!DieArray.empty());
247 auto it = std::lower_bound(
248 DieArray.begin(), DieArray.end(), Offset,
249 [=](const DWARFDebugInfoEntryMinimal &LHS, uint32_t Offset) {
250 return LHS.getOffset() < Offset;
252 return it == DieArray.end() ? nullptr : &*it;
256 /// Size in bytes of the .debug_info data associated with this compile unit.
257 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
259 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
260 /// hasn't already been done. Returns the number of DIEs parsed at this call.
261 size_t extractDIEsIfNeeded(bool CUDieOnly);
262 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
263 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
264 std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
265 /// setDIERelations - We read in all of the DIE entries into our flat list
266 /// of DIE entries and now we need to go back through all of them and set the
267 /// parent, sibling and child pointers for quick DIE navigation.
268 void setDIERelations();
269 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
270 void clearDIEs(bool KeepCUDie);
272 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
273 /// it was actually constructed.
276 /// getSubprogramForAddress - Returns subprogram DIE with address range
277 /// encompassing the provided address. The pointer is alive as long as parsed
278 /// compile unit DIEs are not cleared.
279 const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);