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/Optional.h"
14 #include "llvm/ADT/iterator_range.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
20 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
21 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
22 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
32 class DWARFDebugAbbrev;
37 /// Base class for all DWARFUnitSection classes. This provides the
38 /// functionality common to all unit types.
39 class DWARFUnitSectionBase {
41 /// Returns the Unit that contains the given section offset in the
42 /// same section this Unit originated from.
43 virtual DWARFUnit *getUnitForOffset(uint32_t Offset) const = 0;
45 void parse(DWARFContext &C, const DWARFSection &Section);
46 void parseDWO(DWARFContext &C, const DWARFSection &DWOSection,
47 DWARFUnitIndex *Index = nullptr);
50 virtual void parseImpl(DWARFContext &Context, const DWARFSection &Section,
51 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
52 StringRef SOS, StringRef AOS, StringRef LS,
53 bool isLittleEndian, bool isDWO) = 0;
55 ~DWARFUnitSectionBase() = default;
58 const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
59 DWARFSectionKind Kind);
61 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
62 template<typename UnitType>
63 class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
64 public DWARFUnitSectionBase {
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, StringRef LS, bool LE,
87 bool IsDWO) override {
90 const auto &Index = getDWARFUnitIndex(Context, UnitType::Section);
91 DataExtractor Data(Section.Data, LE, 0);
93 while (Data.isValidOffset(Offset)) {
94 auto U = llvm::make_unique<UnitType>(Context, Section, DA, RS, SS, SOS,
95 AOS, LS, LE, IsDWO, *this,
96 Index.getFromOffset(Offset));
97 if (!U->extract(Data, &Offset))
99 this->push_back(std::move(U));
100 Offset = this->back()->getNextUnitOffset();
107 DWARFContext &Context;
108 // Section containing this DWARFUnit.
109 const DWARFSection &InfoSection;
111 const DWARFDebugAbbrev *Abbrev;
112 StringRef RangeSection;
113 uint32_t RangeSectionBase;
114 StringRef LineSection;
115 StringRef StringSection;
116 StringRef StringOffsetSection;
117 StringRef AddrOffsetSection;
118 uint32_t AddrOffsetSectionBase;
121 const DWARFUnitSectionBase &UnitSection;
126 const DWARFAbbreviationDeclarationSet *Abbrevs;
129 // The compile unit debug information entry items.
130 std::vector<DWARFDebugInfoEntry> DieArray;
131 typedef iterator_range<std::vector<DWARFDebugInfoEntry>::iterator>
135 object::OwningBinary<object::ObjectFile> DWOFile;
136 std::unique_ptr<DWARFContext> DWOContext;
139 DWOHolder(StringRef DWOPath);
140 DWARFUnit *getUnit() const { return DWOU; }
142 std::unique_ptr<DWOHolder> DWO;
144 const DWARFUnitIndex::Entry *IndexEntry;
146 uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) {
147 auto First = DieArray.data();
148 assert(Die >= First && Die < First + DieArray.size());
153 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
154 /// Size in bytes of the unit header.
155 virtual uint32_t getHeaderSize() const { return 11; }
158 DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
159 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
160 StringRef SOS, StringRef AOS, StringRef LS, bool LE, bool IsDWO,
161 const DWARFUnitSectionBase &UnitSection,
162 const DWARFUnitIndex::Entry *IndexEntry = nullptr);
164 virtual ~DWARFUnit();
166 DWARFContext& getContext() const { return Context; }
168 StringRef getLineSection() const { return LineSection; }
169 StringRef getStringSection() const { return StringSection; }
170 StringRef getStringOffsetSection() const { return StringOffsetSection; }
171 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
172 AddrOffsetSection = AOS;
173 AddrOffsetSectionBase = Base;
175 void setRangesSection(StringRef RS, uint32_t Base) {
177 RangeSectionBase = Base;
180 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
181 // FIXME: Result should be uint64_t in DWARF64.
182 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
184 DataExtractor getDebugInfoExtractor() const {
185 return DataExtractor(InfoSection.Data, isLittleEndian, AddrSize);
187 DataExtractor getStringExtractor() const {
188 return DataExtractor(StringSection, false, 0);
191 const RelocAddrMap *getRelocMap() const { return &InfoSection.Relocs; }
193 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
195 /// extractRangeList - extracts the range list referenced by this compile
196 /// unit from .debug_ranges section. Returns true on success.
197 /// Requires that compile unit is already extracted.
198 bool extractRangeList(uint32_t RangeListOffset,
199 DWARFDebugRangeList &RangeList) const;
201 uint32_t getOffset() const { return Offset; }
202 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
203 uint32_t getLength() const { return Length; }
204 uint16_t getVersion() const { return Version; }
205 dwarf::DwarfFormat getFormat() const {
206 return dwarf::DwarfFormat::DWARF32; // FIXME: Support DWARF64.
208 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
211 uint8_t getAddressByteSize() const { return AddrSize; }
212 uint8_t getRefAddrByteSize() const {
215 return getDwarfOffsetByteSize();
217 uint8_t getDwarfOffsetByteSize() const {
218 if (getFormat() == dwarf::DwarfFormat::DWARF64)
222 uint64_t getBaseAddress() const { return BaseAddr; }
224 void setBaseAddress(uint64_t base_addr) {
225 BaseAddr = base_addr;
228 DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
229 extractDIEsIfNeeded(ExtractUnitDIEOnly);
230 if (DieArray.empty())
232 return DWARFDie(this, &DieArray[0]);
235 const char *getCompilationDir();
236 Optional<uint64_t> getDWOId();
238 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
240 /// getInlinedChainForAddress - fetches inlined chain for a given address.
241 /// Returns empty chain if there is no subprogram containing address. The
242 /// chain is valid as long as parsed compile unit DIEs are not cleared.
243 void getInlinedChainForAddress(uint64_t Address,
244 SmallVectorImpl<DWARFDie> &InlinedChain);
246 /// getUnitSection - Return the DWARFUnitSection containing this unit.
247 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
249 /// \brief Returns the number of DIEs in the unit. Parses the unit
251 unsigned getNumDIEs() {
252 extractDIEsIfNeeded(false);
253 return DieArray.size();
256 /// \brief Return the index of a DIE inside the unit's DIE vector.
258 /// It is illegal to call this method with a DIE that hasn't be
259 /// created by this unit. In other word, it's illegal to call this
260 /// method on a DIE that isn't accessible by following
261 /// children/sibling links starting from this unit's getUnitDIE().
262 uint32_t getDIEIndex(const DWARFDie &D) {
263 return getDIEIndex(D.getDebugInfoEntry());
266 /// \brief Return the DIE object at the given index.
267 DWARFDie getDIEAtIndex(unsigned Index) {
268 assert(Index < DieArray.size());
269 return DWARFDie(this, &DieArray[Index]);
272 DWARFDie getParent(const DWARFDebugInfoEntry *Die);
273 DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
275 /// \brief Return the DIE object for a given offset inside the
276 /// unit's DIE vector.
278 /// The unit needs to have its DIEs extracted for this method to work.
279 DWARFDie getDIEForOffset(uint32_t Offset) {
280 extractDIEsIfNeeded(false);
281 assert(!DieArray.empty());
282 auto it = std::lower_bound(
283 DieArray.begin(), DieArray.end(), Offset,
284 [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) {
285 return LHS.getOffset() < Offset;
287 if (it == DieArray.end())
289 return DWARFDie(this, &*it);
292 uint32_t getLineTableOffset() const {
294 if (const auto *Contrib = IndexEntry->getOffset(DW_SECT_LINE))
295 return Contrib->Offset;
299 die_iterator_range dies() {
300 extractDIEsIfNeeded(false);
301 return die_iterator_range(DieArray.begin(), DieArray.end());
305 /// Size in bytes of the .debug_info data associated with this compile unit.
306 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
308 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
309 /// hasn't already been done. Returns the number of DIEs parsed at this call.
310 size_t extractDIEsIfNeeded(bool CUDieOnly);
311 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
312 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
313 std::vector<DWARFDebugInfoEntry> &DIEs) const;
314 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
315 void clearDIEs(bool KeepCUDie);
317 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
318 /// it was actually constructed.
321 /// getSubprogramForAddress - Returns subprogram DIE with address range
322 /// encompassing the provided address. The pointer is alive as long as parsed
323 /// compile unit DIEs are not cleared.
324 DWARFDie getSubprogramForAddress(uint64_t Address);