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/iterator_range.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
21 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
22 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
23 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
24 #include "llvm/Object/Binary.h"
25 #include "llvm/Object/ObjectFile.h"
26 #include "llvm/Support/DataExtractor.h"
27 #include "llvm/Support/Dwarf.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;
51 void parse(DWARFContext &C, const DWARFSection &Section);
52 void parseDWO(DWARFContext &C, const DWARFSection &DWOSection,
53 DWARFUnitIndex *Index = nullptr);
56 ~DWARFUnitSectionBase() = default;
58 virtual void parseImpl(DWARFContext &Context, const DWARFSection &Section,
59 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
60 StringRef SS, StringRef SOS, StringRef AOS,
61 StringRef LS, bool isLittleEndian, bool isDWO) = 0;
64 const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
65 DWARFSectionKind Kind);
67 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
68 template<typename UnitType>
69 class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
70 public DWARFUnitSectionBase {
74 typedef SmallVectorImpl<std::unique_ptr<UnitType>> UnitVector;
75 typedef typename UnitVector::iterator iterator;
76 typedef llvm::iterator_range<typename UnitVector::iterator> iterator_range;
78 UnitType *getUnitForOffset(uint32_t Offset) const override {
79 auto *CU = std::upper_bound(
80 this->begin(), this->end(), Offset,
81 [](uint32_t LHS, const std::unique_ptr<UnitType> &RHS) {
82 return LHS < RHS->getNextUnitOffset();
84 if (CU != this->end())
90 void parseImpl(DWARFContext &Context, const DWARFSection &Section,
91 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
92 StringRef SS, StringRef SOS, StringRef AOS, StringRef LS,
93 bool LE, bool IsDWO) override {
96 const auto &Index = getDWARFUnitIndex(Context, UnitType::Section);
97 DataExtractor Data(Section.Data, LE, 0);
99 while (Data.isValidOffset(Offset)) {
100 auto U = llvm::make_unique<UnitType>(Context, Section, DA, RS, SS, SOS,
101 AOS, LS, LE, IsDWO, *this,
102 Index.getFromOffset(Offset));
103 if (!U->extract(Data, &Offset))
105 this->push_back(std::move(U));
106 Offset = this->back()->getNextUnitOffset();
113 DWARFContext &Context;
114 // Section containing this DWARFUnit.
115 const DWARFSection &InfoSection;
117 const DWARFDebugAbbrev *Abbrev;
118 const DWARFSection *RangeSection;
119 uint32_t RangeSectionBase;
120 StringRef LineSection;
121 StringRef StringSection;
122 StringRef StringOffsetSection;
123 StringRef AddrOffsetSection;
124 uint32_t AddrOffsetSectionBase;
127 const DWARFUnitSectionBase &UnitSection;
131 const DWARFAbbreviationDeclarationSet *Abbrevs;
136 // The compile unit debug information entry items.
137 std::vector<DWARFDebugInfoEntry> DieArray;
139 // Map from range's start address to end address and corresponding DIE.
140 // IntervalMap does not support range removal, as a result, we use the
141 // std::map::upper_bound for address range lookup.
142 std::map<uint64_t, std::pair<uint64_t, DWARFDie>> AddrDieMap;
143 typedef iterator_range<std::vector<DWARFDebugInfoEntry>::iterator>
147 object::OwningBinary<object::ObjectFile> DWOFile;
148 std::unique_ptr<DWARFContext> DWOContext;
149 DWARFUnit *DWOU = nullptr;
152 DWOHolder(StringRef DWOPath);
154 DWARFUnit *getUnit() const { return DWOU; }
156 std::unique_ptr<DWOHolder> DWO;
158 const DWARFUnitIndex::Entry *IndexEntry;
160 uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) {
161 auto First = DieArray.data();
162 assert(Die >= First && Die < First + DieArray.size());
167 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
169 /// Size in bytes of the unit header.
170 virtual uint32_t getHeaderSize() const { return Version <= 4 ? 11 : 12; }
173 DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
174 const DWARFDebugAbbrev *DA, const DWARFSection *RS, StringRef SS,
175 StringRef SOS, StringRef AOS, StringRef LS, bool LE, bool IsDWO,
176 const DWARFUnitSectionBase &UnitSection,
177 const DWARFUnitIndex::Entry *IndexEntry = nullptr);
179 virtual ~DWARFUnit();
181 DWARFContext& getContext() const { return Context; }
183 StringRef getLineSection() const { return LineSection; }
184 StringRef getStringSection() const { return StringSection; }
185 StringRef getStringOffsetSection() const { return StringOffsetSection; }
187 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
188 AddrOffsetSection = AOS;
189 AddrOffsetSectionBase = Base;
192 // Recursively update address to Die map.
193 void updateAddressDieMap(DWARFDie Die);
195 void setRangesSection(const DWARFSection *RS, uint32_t Base) {
197 RangeSectionBase = Base;
200 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
201 // FIXME: Result should be uint64_t in DWARF64.
202 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
204 DataExtractor getDebugInfoExtractor() const {
205 return DataExtractor(InfoSection.Data, isLittleEndian, AddrSize);
208 DataExtractor getStringExtractor() const {
209 return DataExtractor(StringSection, false, 0);
212 const RelocAddrMap *getRelocMap() const { return &InfoSection.Relocs; }
214 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
216 /// extractRangeList - extracts the range list referenced by this compile
217 /// unit from .debug_ranges section. Returns true on success.
218 /// Requires that compile unit is already extracted.
219 bool extractRangeList(uint32_t RangeListOffset,
220 DWARFDebugRangeList &RangeList) const;
222 uint32_t getOffset() const { return Offset; }
223 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
224 uint32_t getLength() const { return Length; }
225 uint16_t getVersion() const { return Version; }
227 dwarf::DwarfFormat getFormat() const {
228 return dwarf::DwarfFormat::DWARF32; // FIXME: Support DWARF64.
231 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
235 uint8_t getUnitType() const { return UnitType; }
236 uint8_t getAddressByteSize() const { return AddrSize; }
238 uint8_t getRefAddrByteSize() const {
241 return getDwarfOffsetByteSize();
244 uint8_t getDwarfOffsetByteSize() const {
245 if (getFormat() == dwarf::DwarfFormat::DWARF64)
250 uint64_t getBaseAddress() const { return BaseAddr; }
252 void setBaseAddress(uint64_t base_addr) {
253 BaseAddr = base_addr;
256 DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
257 extractDIEsIfNeeded(ExtractUnitDIEOnly);
258 if (DieArray.empty())
260 return DWARFDie(this, &DieArray[0]);
263 const char *getCompilationDir();
264 Optional<uint64_t> getDWOId();
266 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
268 /// getInlinedChainForAddress - fetches inlined chain for a given address.
269 /// Returns empty chain if there is no subprogram containing address. The
270 /// chain is valid as long as parsed compile unit DIEs are not cleared.
271 void getInlinedChainForAddress(uint64_t Address,
272 SmallVectorImpl<DWARFDie> &InlinedChain);
274 /// getUnitSection - Return the DWARFUnitSection containing this unit.
275 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
277 /// \brief Returns the number of DIEs in the unit. Parses the unit
279 unsigned getNumDIEs() {
280 extractDIEsIfNeeded(false);
281 return DieArray.size();
284 /// \brief Return the index of a DIE inside the unit's DIE vector.
286 /// It is illegal to call this method with a DIE that hasn't be
287 /// created by this unit. In other word, it's illegal to call this
288 /// method on a DIE that isn't accessible by following
289 /// children/sibling links starting from this unit's getUnitDIE().
290 uint32_t getDIEIndex(const DWARFDie &D) {
291 return getDIEIndex(D.getDebugInfoEntry());
294 /// \brief Return the DIE object at the given index.
295 DWARFDie getDIEAtIndex(unsigned Index) {
296 assert(Index < DieArray.size());
297 return DWARFDie(this, &DieArray[Index]);
300 DWARFDie getParent(const DWARFDebugInfoEntry *Die);
301 DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
303 /// \brief Return the DIE object for a given offset inside the
304 /// unit's DIE vector.
306 /// The unit needs to have its DIEs extracted for this method to work.
307 DWARFDie getDIEForOffset(uint32_t Offset) {
308 extractDIEsIfNeeded(false);
309 assert(!DieArray.empty());
310 auto it = std::lower_bound(
311 DieArray.begin(), DieArray.end(), Offset,
312 [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) {
313 return LHS.getOffset() < Offset;
315 if (it == DieArray.end())
317 return DWARFDie(this, &*it);
320 uint32_t getLineTableOffset() const {
322 if (const auto *Contrib = IndexEntry->getOffset(DW_SECT_LINE))
323 return Contrib->Offset;
327 die_iterator_range dies() {
328 extractDIEsIfNeeded(false);
329 return die_iterator_range(DieArray.begin(), DieArray.end());
333 /// Size in bytes of the .debug_info data associated with this compile unit.
334 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
336 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
337 /// hasn't already been done. Returns the number of DIEs parsed at this call.
338 size_t extractDIEsIfNeeded(bool CUDieOnly);
340 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
341 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
342 std::vector<DWARFDebugInfoEntry> &DIEs) const;
344 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
345 void clearDIEs(bool KeepCUDie);
347 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
348 /// it was actually constructed.
351 /// getSubroutineForAddress - Returns subprogram DIE with address range
352 /// encompassing the provided address. The pointer is alive as long as parsed
353 /// compile unit DIEs are not cleared.
354 DWARFDie getSubroutineForAddress(uint64_t Address);
357 } // end namespace llvm
359 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H