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 "DWARFDebugAbbrev.h"
14 #include "DWARFDebugInfoEntry.h"
15 #include "DWARFDebugRangeList.h"
16 #include "DWARFRelocMap.h"
26 class DWARFDebugAbbrev;
31 /// Base class for all DWARFUnitSection classes. This provides the
32 /// functionality common to all unit types.
33 class DWARFUnitSectionBase {
35 /// Returns the Unit that contains the given section offset in the
36 /// same section this Unit originated from.
37 virtual DWARFUnit *getUnitForOffset(uint32_t Offset) const = 0;
39 void parse(DWARFContext &C, StringRef SectionData, const RelocAddrMap &Map);
40 void parseDWO(DWARFContext &C, StringRef SectionData, const RelocAddrMap &Map);
43 virtual void parseImpl(DWARFContext &Context, const DWARFDebugAbbrev *DA,
44 StringRef Section, StringRef RS, StringRef SS,
45 StringRef SOS, StringRef AOS, const RelocAddrMap &M,
46 bool isLittleEndian) = 0;
48 ~DWARFUnitSectionBase() {}
51 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
52 template<typename UnitType>
53 class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
54 public DWARFUnitSectionBase {
56 struct UnitOffsetComparator {
57 bool operator()(uint32_t LHS,
58 const std::unique_ptr<UnitType> &RHS) const {
59 return LHS < RHS->getNextUnitOffset();
66 DWARFUnitSection() : Parsed(false) {}
67 DWARFUnitSection(DWARFUnitSection &&DUS) :
68 SmallVector<std::unique_ptr<UnitType>, 1>(std::move(DUS)), Parsed(DUS.Parsed) {}
70 typedef llvm::SmallVectorImpl<std::unique_ptr<UnitType>> UnitVector;
71 typedef typename UnitVector::iterator iterator;
72 typedef llvm::iterator_range<typename UnitVector::iterator> iterator_range;
74 UnitType *getUnitForOffset(uint32_t Offset) const {
75 auto *CU = std::upper_bound(this->begin(), this->end(), Offset,
76 UnitOffsetComparator());
77 if (CU != this->end())
83 void parseImpl(DWARFContext &Context, const DWARFDebugAbbrev *DA,
84 StringRef Section, StringRef RS, StringRef SS, StringRef SOS,
85 StringRef AOS, const RelocAddrMap &M, bool LE) override {
88 DataExtractor Data(Section, LE, 0);
90 while (Data.isValidOffset(Offset)) {
91 auto U = make_unique<UnitType>(Context, DA, Section, RS, SS, SOS, AOS, &M,
92 Data.isLittleEndian(), *this);
93 if (!U->extract(Data, &Offset))
95 this->push_back(std::move(U));
96 Offset = this->back()->getNextUnitOffset();
103 DWARFContext &Context;
105 const DWARFDebugAbbrev *Abbrev;
106 StringRef InfoSection;
107 StringRef RangeSection;
108 uint32_t RangeSectionBase;
109 StringRef StringSection;
110 StringRef StringOffsetSection;
111 StringRef AddrOffsetSection;
112 uint32_t AddrOffsetSectionBase;
113 const RelocAddrMap *RelocMap;
115 const DWARFUnitSectionBase &UnitSection;
120 const DWARFAbbreviationDeclarationSet *Abbrevs;
123 // The compile unit debug information entry items.
124 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
127 object::OwningBinary<object::ObjectFile> DWOFile;
128 std::unique_ptr<DWARFContext> DWOContext;
131 DWOHolder(StringRef DWOPath);
132 DWARFUnit *getUnit() const { return DWOU; }
134 std::unique_ptr<DWOHolder> DWO;
137 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
138 /// Size in bytes of the unit header.
139 virtual uint32_t getHeaderSize() const { return 11; }
142 DWARFUnit(DWARFContext& Context, const DWARFDebugAbbrev *DA, StringRef IS,
143 StringRef RS, StringRef SS, StringRef SOS, StringRef AOS,
144 const RelocAddrMap *M, bool LE, const DWARFUnitSectionBase &UnitSection);
146 virtual ~DWARFUnit();
148 DWARFContext& getContext() const { return Context; }
150 StringRef getStringSection() const { return StringSection; }
151 StringRef getStringOffsetSection() const { return StringOffsetSection; }
152 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
153 AddrOffsetSection = AOS;
154 AddrOffsetSectionBase = Base;
156 void setRangesSection(StringRef RS, uint32_t Base) {
158 RangeSectionBase = Base;
161 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
162 // FIXME: Result should be uint64_t in DWARF64.
163 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
165 DataExtractor getDebugInfoExtractor() const {
166 return DataExtractor(InfoSection, isLittleEndian, AddrSize);
168 DataExtractor getStringExtractor() const {
169 return DataExtractor(StringSection, false, 0);
172 const RelocAddrMap *getRelocMap() const { return RelocMap; }
174 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
176 /// extractRangeList - extracts the range list referenced by this compile
177 /// unit from .debug_ranges section. Returns true on success.
178 /// Requires that compile unit is already extracted.
179 bool extractRangeList(uint32_t RangeListOffset,
180 DWARFDebugRangeList &RangeList) const;
182 uint32_t getOffset() const { return Offset; }
183 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
184 uint32_t getLength() const { return Length; }
185 uint16_t getVersion() const { return Version; }
186 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
189 uint8_t getAddressByteSize() const { return AddrSize; }
190 uint64_t getBaseAddress() const { return BaseAddr; }
192 void setBaseAddress(uint64_t base_addr) {
193 BaseAddr = base_addr;
196 const DWARFDebugInfoEntryMinimal *
197 getCompileUnitDIE(bool extract_cu_die_only = true) {
198 extractDIEsIfNeeded(extract_cu_die_only);
199 return DieArray.empty() ? nullptr : &DieArray[0];
202 const char *getCompilationDir();
205 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
207 /// getInlinedChainForAddress - fetches inlined chain for a given address.
208 /// Returns empty chain if there is no subprogram containing address. The
209 /// chain is valid as long as parsed compile unit DIEs are not cleared.
210 DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
212 /// getUnitSection - Return the DWARFUnitSection containing this unit.
213 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
216 /// Size in bytes of the .debug_info data associated with this compile unit.
217 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
219 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
220 /// hasn't already been done. Returns the number of DIEs parsed at this call.
221 size_t extractDIEsIfNeeded(bool CUDieOnly);
222 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
223 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
224 std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
225 /// setDIERelations - We read in all of the DIE entries into our flat list
226 /// of DIE entries and now we need to go back through all of them and set the
227 /// parent, sibling and child pointers for quick DIE navigation.
228 void setDIERelations();
229 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
230 void clearDIEs(bool KeepCUDie);
232 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
233 /// it was actually constructed.
236 /// getSubprogramForAddress - Returns subprogram DIE with address range
237 /// encompassing the provided address. The pointer is alive as long as parsed
238 /// compile unit DIEs are not cleared.
239 const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);