1 //===-- DWARFContext.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_DWARFCONTEXT_H
11 #define LLVM_DEBUGINFO_DWARFCONTEXT_H
13 #include "DWARFCompileUnit.h"
14 #include "DWARFDebugAranges.h"
15 #include "DWARFDebugFrame.h"
16 #include "DWARFDebugLine.h"
17 #include "DWARFDebugLoc.h"
18 #include "DWARFDebugRangeList.h"
19 #include "DWARFTypeUnit.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/DebugInfo/DIContext.h"
27 /// This data structure is the top level entity that deals with dwarf debug
28 /// information parsing. The actual data is supplied through pure virtual
29 /// methods that a concrete implementation provides.
30 class DWARFContext : public DIContext {
31 typedef SmallVector<std::unique_ptr<DWARFCompileUnit>, 1> CUVector;
32 typedef SmallVector<std::unique_ptr<DWARFTypeUnit>, 1> TUVector;
36 std::unique_ptr<DWARFDebugAbbrev> Abbrev;
37 std::unique_ptr<DWARFDebugLoc> Loc;
38 std::unique_ptr<DWARFDebugAranges> Aranges;
39 std::unique_ptr<DWARFDebugLine> Line;
40 std::unique_ptr<DWARFDebugFrame> DebugFrame;
44 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
45 std::unique_ptr<DWARFDebugLocDWO> LocDWO;
47 DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
48 DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
50 /// Read compile units from the debug_info section (if necessary)
51 /// and store them in CUs.
52 void parseCompileUnits();
54 /// Read type units from the debug_types sections (if necessary)
55 /// and store them in TUs.
56 void parseTypeUnits();
58 /// Read compile units from the debug_info.dwo section (if necessary)
59 /// and store them in DWOCUs.
60 void parseDWOCompileUnits();
62 /// Read type units from the debug_types.dwo section (if necessary)
63 /// and store them in DWOTUs.
64 void parseDWOTypeUnits();
72 DWARFContext() : DIContext(CK_DWARF) {}
74 static bool classof(const DIContext *DICtx) {
75 return DICtx->getKind() == CK_DWARF;
78 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
80 typedef iterator_range<CUVector::iterator> cu_iterator_range;
81 typedef iterator_range<TUVector::iterator> tu_iterator_range;
83 /// Get compile units in this context.
84 cu_iterator_range compile_units() {
86 return cu_iterator_range(CUs.begin(), CUs.end());
89 /// Get type units in this context.
90 tu_iterator_range type_units() {
92 return tu_iterator_range(TUs.begin(), TUs.end());
95 /// Get compile units in the DWO context.
96 cu_iterator_range dwo_compile_units() {
97 parseDWOCompileUnits();
98 return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
101 /// Get type units in the DWO context.
102 tu_iterator_range dwo_type_units() {
104 return tu_iterator_range(DWOTUs.begin(), DWOTUs.end());
107 /// Get the number of compile units in this context.
108 unsigned getNumCompileUnits() {
113 /// Get the number of compile units in this context.
114 unsigned getNumTypeUnits() {
119 /// Get the number of compile units in the DWO context.
120 unsigned getNumDWOCompileUnits() {
121 parseDWOCompileUnits();
122 return DWOCUs.size();
125 /// Get the number of compile units in the DWO context.
126 unsigned getNumDWOTypeUnits() {
128 return DWOTUs.size();
131 /// Get the compile unit at the specified index for this compile unit.
132 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
134 return CUs[index].get();
137 /// Get the compile unit at the specified index for the DWO compile units.
138 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
139 parseDWOCompileUnits();
140 return DWOCUs[index].get();
143 /// Get a pointer to the parsed DebugAbbrev object.
144 const DWARFDebugAbbrev *getDebugAbbrev();
146 /// Get a pointer to the parsed DebugLoc object.
147 const DWARFDebugLoc *getDebugLoc();
149 /// Get a pointer to the parsed dwo abbreviations object.
150 const DWARFDebugAbbrev *getDebugAbbrevDWO();
152 /// Get a pointer to the parsed DebugLoc object.
153 const DWARFDebugLocDWO *getDebugLocDWO();
155 /// Get a pointer to the parsed DebugAranges object.
156 const DWARFDebugAranges *getDebugAranges();
158 /// Get a pointer to the parsed frame information object.
159 const DWARFDebugFrame *getDebugFrame();
161 /// Get a pointer to a parsed line table corresponding to a compile unit.
162 const DWARFDebugLine::LineTable *
163 getLineTableForCompileUnit(DWARFCompileUnit *cu);
165 DILineInfo getLineInfoForAddress(uint64_t Address,
166 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
167 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
168 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
169 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
170 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
172 virtual bool isLittleEndian() const = 0;
173 virtual uint8_t getAddressSize() const = 0;
174 virtual const Section &getInfoSection() = 0;
175 typedef MapVector<object::SectionRef, Section,
176 std::map<object::SectionRef, unsigned> > TypeSectionMap;
177 virtual const TypeSectionMap &getTypesSections() = 0;
178 virtual StringRef getAbbrevSection() = 0;
179 virtual const Section &getLocSection() = 0;
180 virtual const Section &getLocDWOSection() = 0;
181 virtual StringRef getARangeSection() = 0;
182 virtual StringRef getDebugFrameSection() = 0;
183 virtual const Section &getLineSection() = 0;
184 virtual const Section &getLineDWOSection() = 0;
185 virtual StringRef getStringSection() = 0;
186 virtual StringRef getRangeSection() = 0;
187 virtual StringRef getPubNamesSection() = 0;
188 virtual StringRef getPubTypesSection() = 0;
189 virtual StringRef getGnuPubNamesSection() = 0;
190 virtual StringRef getGnuPubTypesSection() = 0;
192 // Sections for DWARF5 split dwarf proposal.
193 virtual const Section &getInfoDWOSection() = 0;
194 virtual const TypeSectionMap &getTypesDWOSections() = 0;
195 virtual StringRef getAbbrevDWOSection() = 0;
196 virtual StringRef getStringDWOSection() = 0;
197 virtual StringRef getStringOffsetDWOSection() = 0;
198 virtual StringRef getRangeDWOSection() = 0;
199 virtual StringRef getAddrSection() = 0;
201 static bool isSupportedVersion(unsigned version) {
202 return version == 2 || version == 3 || version == 4;
205 /// Return the compile unit that includes an offset (relative to .debug_info).
206 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
208 /// Return the compile unit which contains instruction with provided
210 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
213 /// DWARFContextInMemory is the simplest possible implementation of a
214 /// DWARFContext. It assumes all content is available in memory and stores
216 class DWARFContextInMemory : public DWARFContext {
217 virtual void anchor();
221 TypeSectionMap TypesSections;
222 StringRef AbbrevSection;
224 Section LocDWOSection;
225 StringRef ARangeSection;
226 StringRef DebugFrameSection;
228 Section LineDWOSection;
229 StringRef StringSection;
230 StringRef RangeSection;
231 StringRef PubNamesSection;
232 StringRef PubTypesSection;
233 StringRef GnuPubNamesSection;
234 StringRef GnuPubTypesSection;
236 // Sections for DWARF5 split dwarf proposal.
237 Section InfoDWOSection;
238 TypeSectionMap TypesDWOSections;
239 StringRef AbbrevDWOSection;
240 StringRef StringDWOSection;
241 StringRef StringOffsetDWOSection;
242 StringRef RangeDWOSection;
243 StringRef AddrSection;
245 SmallVector<SmallString<32>, 4> UncompressedSections;
248 DWARFContextInMemory(object::ObjectFile *);
249 bool isLittleEndian() const override { return IsLittleEndian; }
250 uint8_t getAddressSize() const override { return AddressSize; }
251 const Section &getInfoSection() override { return InfoSection; }
252 const TypeSectionMap &getTypesSections() override { return TypesSections; }
253 StringRef getAbbrevSection() override { return AbbrevSection; }
254 const Section &getLocSection() override { return LocSection; }
255 const Section &getLocDWOSection() override { return LocDWOSection; }
256 StringRef getARangeSection() override { return ARangeSection; }
257 StringRef getDebugFrameSection() override { return DebugFrameSection; }
258 const Section &getLineSection() override { return LineSection; }
259 const Section &getLineDWOSection() override { return LineDWOSection; }
260 StringRef getStringSection() override { return StringSection; }
261 StringRef getRangeSection() override { return RangeSection; }
262 StringRef getPubNamesSection() override { return PubNamesSection; }
263 StringRef getPubTypesSection() override { return PubTypesSection; }
264 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
265 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
267 // Sections for DWARF5 split dwarf proposal.
268 const Section &getInfoDWOSection() override { return InfoDWOSection; }
269 const TypeSectionMap &getTypesDWOSections() override {
270 return TypesDWOSections;
272 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
273 StringRef getStringDWOSection() override { return StringDWOSection; }
274 StringRef getStringOffsetDWOSection() override {
275 return StringOffsetDWOSection;
277 StringRef getRangeDWOSection() override { return RangeDWOSection; }
278 StringRef getAddrSection() override {