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, const DWARFSection *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, const DWARFSection *AOS,
93 StringRef LS, 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 const DWARFSection *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>
146 std::shared_ptr<DWARFUnit> DWO;
148 const DWARFUnitIndex::Entry *IndexEntry;
150 uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) {
151 auto First = DieArray.data();
152 assert(Die >= First && Die < First + DieArray.size());
157 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
159 /// Size in bytes of the unit header.
160 virtual uint32_t getHeaderSize() const { return Version <= 4 ? 11 : 12; }
163 DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
164 const DWARFDebugAbbrev *DA, const DWARFSection *RS, StringRef SS,
165 StringRef SOS, const DWARFSection *AOS, StringRef LS, bool LE,
166 bool IsDWO, const DWARFUnitSectionBase &UnitSection,
167 const DWARFUnitIndex::Entry *IndexEntry = nullptr);
169 virtual ~DWARFUnit();
171 DWARFContext& getContext() const { return Context; }
173 StringRef getLineSection() const { return LineSection; }
174 StringRef getStringSection() const { return StringSection; }
175 StringRef getStringOffsetSection() const { return StringOffsetSection; }
177 void setAddrOffsetSection(const DWARFSection *AOS, uint32_t Base) {
178 AddrOffsetSection = AOS;
179 AddrOffsetSectionBase = Base;
182 /// Recursively update address to Die map.
183 void updateAddressDieMap(DWARFDie Die);
185 void setRangesSection(const DWARFSection *RS, uint32_t Base) {
187 RangeSectionBase = Base;
190 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
191 // FIXME: Result should be uint64_t in DWARF64.
192 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
194 DataExtractor getDebugInfoExtractor() const {
195 return DataExtractor(InfoSection.Data, isLittleEndian, AddrSize);
198 DataExtractor getStringExtractor() const {
199 return DataExtractor(StringSection, false, 0);
202 const RelocAddrMap *getRelocMap() const { return &InfoSection.Relocs; }
204 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
206 /// extractRangeList - extracts the range list referenced by this compile
207 /// unit from .debug_ranges section. Returns true on success.
208 /// Requires that compile unit is already extracted.
209 bool extractRangeList(uint32_t RangeListOffset,
210 DWARFDebugRangeList &RangeList) const;
212 uint32_t getOffset() const { return Offset; }
213 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
214 uint32_t getLength() const { return Length; }
215 uint16_t getVersion() const { return Version; }
217 dwarf::DwarfFormat getFormat() const {
218 return dwarf::DwarfFormat::DWARF32; // FIXME: Support DWARF64.
221 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
225 uint8_t getUnitType() const { return UnitType; }
226 uint8_t getAddressByteSize() const { return AddrSize; }
228 uint8_t getRefAddrByteSize() const {
231 return getDwarfOffsetByteSize();
234 uint8_t getDwarfOffsetByteSize() const {
235 if (getFormat() == dwarf::DwarfFormat::DWARF64)
240 uint64_t getBaseAddress() const { return BaseAddr; }
242 void setBaseAddress(uint64_t base_addr) {
243 BaseAddr = base_addr;
246 DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
247 extractDIEsIfNeeded(ExtractUnitDIEOnly);
248 if (DieArray.empty())
250 return DWARFDie(this, &DieArray[0]);
253 const char *getCompilationDir();
254 Optional<uint64_t> getDWOId();
256 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
258 /// getInlinedChainForAddress - fetches inlined chain for a given address.
259 /// Returns empty chain if there is no subprogram containing address. The
260 /// chain is valid as long as parsed compile unit DIEs are not cleared.
261 void getInlinedChainForAddress(uint64_t Address,
262 SmallVectorImpl<DWARFDie> &InlinedChain);
264 /// getUnitSection - Return the DWARFUnitSection containing this unit.
265 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
267 /// \brief Returns the number of DIEs in the unit. Parses the unit
269 unsigned getNumDIEs() {
270 extractDIEsIfNeeded(false);
271 return DieArray.size();
274 /// \brief Return the index of a DIE inside the unit's DIE vector.
276 /// It is illegal to call this method with a DIE that hasn't be
277 /// created by this unit. In other word, it's illegal to call this
278 /// method on a DIE that isn't accessible by following
279 /// children/sibling links starting from this unit's getUnitDIE().
280 uint32_t getDIEIndex(const DWARFDie &D) {
281 return getDIEIndex(D.getDebugInfoEntry());
284 /// \brief Return the DIE object at the given index.
285 DWARFDie getDIEAtIndex(unsigned Index) {
286 assert(Index < DieArray.size());
287 return DWARFDie(this, &DieArray[Index]);
290 DWARFDie getParent(const DWARFDebugInfoEntry *Die);
291 DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
293 /// \brief Return the DIE object for a given offset inside the
294 /// unit's DIE vector.
296 /// The unit needs to have its DIEs extracted for this method to work.
297 DWARFDie getDIEForOffset(uint32_t Offset) {
298 extractDIEsIfNeeded(false);
299 assert(!DieArray.empty());
300 auto it = std::lower_bound(
301 DieArray.begin(), DieArray.end(), Offset,
302 [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) {
303 return LHS.getOffset() < Offset;
305 if (it != DieArray.end() && it->getOffset() == Offset)
306 return DWARFDie(this, &*it);
310 uint32_t getLineTableOffset() const {
312 if (const auto *Contrib = IndexEntry->getOffset(DW_SECT_LINE))
313 return Contrib->Offset;
317 die_iterator_range dies() {
318 extractDIEsIfNeeded(false);
319 return die_iterator_range(DieArray.begin(), DieArray.end());
323 /// Size in bytes of the .debug_info data associated with this compile unit.
324 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
326 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
327 /// hasn't already been done. Returns the number of DIEs parsed at this call.
328 size_t extractDIEsIfNeeded(bool CUDieOnly);
330 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
331 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
332 std::vector<DWARFDebugInfoEntry> &DIEs) const;
334 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
335 void clearDIEs(bool KeepCUDie);
337 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
338 /// it was actually constructed.
341 /// getSubroutineForAddress - Returns subprogram DIE with address range
342 /// encompassing the provided address. The pointer is alive as long as parsed
343 /// compile unit DIEs are not cleared.
344 DWARFDie getSubroutineForAddress(uint64_t Address);
347 } // end namespace llvm
349 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H