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 {
59 DWARFUnitSection() : Parsed(false) {}
60 DWARFUnitSection(DWARFUnitSection &&DUS) :
61 SmallVector<std::unique_ptr<UnitType>, 1>(std::move(DUS)), Parsed(DUS.Parsed) {}
63 typedef llvm::SmallVectorImpl<std::unique_ptr<UnitType>> UnitVector;
64 typedef typename UnitVector::iterator iterator;
65 typedef llvm::iterator_range<typename UnitVector::iterator> iterator_range;
67 UnitType *getUnitForOffset(uint32_t Offset) const override {
68 auto *CU = std::upper_bound(
69 this->begin(), this->end(), Offset,
70 [](uint32_t LHS, const std::unique_ptr<UnitType> &RHS) {
71 return LHS < RHS->getNextUnitOffset();
73 if (CU != this->end())
79 void parseImpl(DWARFContext &Context, const DWARFSection &Section,
80 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
81 StringRef SOS, StringRef AOS, bool LE) override {
84 DataExtractor Data(Section.Data, LE, 0);
86 while (Data.isValidOffset(Offset)) {
87 auto U = llvm::make_unique<UnitType>(Context, Section, DA, RS, SS, SOS,
89 if (!U->extract(Data, &Offset))
91 this->push_back(std::move(U));
92 Offset = this->back()->getNextUnitOffset();
99 DWARFContext &Context;
100 // Section containing this DWARFUnit.
101 const DWARFSection &InfoSection;
103 const DWARFDebugAbbrev *Abbrev;
104 StringRef RangeSection;
105 uint32_t RangeSectionBase;
106 StringRef StringSection;
107 StringRef StringOffsetSection;
108 StringRef AddrOffsetSection;
109 uint32_t AddrOffsetSectionBase;
111 const DWARFUnitSectionBase &UnitSection;
116 const DWARFAbbreviationDeclarationSet *Abbrevs;
119 // The compile unit debug information entry items.
120 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
123 object::OwningBinary<object::ObjectFile> DWOFile;
124 std::unique_ptr<DWARFContext> DWOContext;
127 DWOHolder(StringRef DWOPath);
128 DWARFUnit *getUnit() const { return DWOU; }
130 std::unique_ptr<DWOHolder> DWO;
133 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
134 /// Size in bytes of the unit header.
135 virtual uint32_t getHeaderSize() const { return 11; }
138 DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
139 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
140 StringRef SOS, StringRef AOS, bool LE,
141 const DWARFUnitSectionBase &UnitSection);
143 virtual ~DWARFUnit();
145 DWARFContext& getContext() const { return Context; }
147 StringRef getStringSection() const { return StringSection; }
148 StringRef getStringOffsetSection() const { return StringOffsetSection; }
149 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
150 AddrOffsetSection = AOS;
151 AddrOffsetSectionBase = Base;
153 void setRangesSection(StringRef RS, uint32_t Base) {
155 RangeSectionBase = Base;
158 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
159 // FIXME: Result should be uint64_t in DWARF64.
160 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
162 DataExtractor getDebugInfoExtractor() const {
163 return DataExtractor(InfoSection.Data, isLittleEndian, AddrSize);
165 DataExtractor getStringExtractor() const {
166 return DataExtractor(StringSection, false, 0);
169 const RelocAddrMap *getRelocMap() const { return &InfoSection.Relocs; }
171 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
173 /// extractRangeList - extracts the range list referenced by this compile
174 /// unit from .debug_ranges section. Returns true on success.
175 /// Requires that compile unit is already extracted.
176 bool extractRangeList(uint32_t RangeListOffset,
177 DWARFDebugRangeList &RangeList) const;
179 uint32_t getOffset() const { return Offset; }
180 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
181 uint32_t getLength() const { return Length; }
182 uint16_t getVersion() const { return Version; }
183 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
186 uint8_t getAddressByteSize() const { return AddrSize; }
187 uint64_t getBaseAddress() const { return BaseAddr; }
189 void setBaseAddress(uint64_t base_addr) {
190 BaseAddr = base_addr;
193 const DWARFDebugInfoEntryMinimal *getUnitDIE(bool ExtractUnitDIEOnly = true) {
194 extractDIEsIfNeeded(ExtractUnitDIEOnly);
195 return DieArray.empty() ? nullptr : &DieArray[0];
198 const char *getCompilationDir();
201 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
203 /// getInlinedChainForAddress - fetches inlined chain for a given address.
204 /// Returns empty chain if there is no subprogram containing address. The
205 /// chain is valid as long as parsed compile unit DIEs are not cleared.
206 DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
208 /// getUnitSection - Return the DWARFUnitSection containing this unit.
209 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
211 /// \brief Returns the number of DIEs in the unit. Parses the unit
213 unsigned getNumDIEs() {
214 extractDIEsIfNeeded(false);
215 return DieArray.size();
218 /// \brief Return the index of a DIE inside the unit's DIE vector.
220 /// It is illegal to call this method with a DIE that hasn't be
221 /// created by this unit. In other word, it's illegal to call this
222 /// method on a DIE that isn't accessible by following
223 /// children/sibling links starting from this unit's getUnitDIE().
224 uint32_t getDIEIndex(const DWARFDebugInfoEntryMinimal *DIE) {
225 assert(!DieArray.empty() && DIE >= &DieArray[0] &&
226 DIE < &DieArray[0] + DieArray.size());
227 return DIE - &DieArray[0];
230 /// \brief Return the DIE object at the given index.
231 const DWARFDebugInfoEntryMinimal *getDIEAtIndex(unsigned Index) const {
232 assert(Index < DieArray.size());
233 return &DieArray[Index];
236 /// \brief Return the DIE object for a given offset inside the
237 /// unit's DIE vector.
239 /// The unit needs to have his DIEs extracted for this method to work.
240 const DWARFDebugInfoEntryMinimal *getDIEForOffset(uint32_t Offset) const {
241 assert(!DieArray.empty());
242 auto it = std::lower_bound(
243 DieArray.begin(), DieArray.end(), Offset,
244 [=](const DWARFDebugInfoEntryMinimal &LHS, uint32_t Offset) {
245 return LHS.getOffset() < Offset;
247 return it == DieArray.end() ? nullptr : &*it;
251 /// Size in bytes of the .debug_info data associated with this compile unit.
252 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
254 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
255 /// hasn't already been done. Returns the number of DIEs parsed at this call.
256 size_t extractDIEsIfNeeded(bool CUDieOnly);
257 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
258 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
259 std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
260 /// setDIERelations - We read in all of the DIE entries into our flat list
261 /// of DIE entries and now we need to go back through all of them and set the
262 /// parent, sibling and child pointers for quick DIE navigation.
263 void setDIERelations();
264 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
265 void clearDIEs(bool KeepCUDie);
267 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
268 /// it was actually constructed.
271 /// getSubprogramForAddress - Returns subprogram DIE with address range
272 /// encompassing the provided address. The pointer is alive as long as parsed
273 /// compile unit DIEs are not cleared.
274 const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);