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_LIB_DEBUGINFO_DWARFCONTEXT_H
11 #define LLVM_LIB_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 {
32 DWARFUnitSection<DWARFCompileUnit> CUs;
33 DWARFUnitSection<DWARFTypeUnit> TUs;
34 std::unique_ptr<DWARFDebugAbbrev> Abbrev;
35 std::unique_ptr<DWARFDebugLoc> Loc;
36 std::unique_ptr<DWARFDebugAranges> Aranges;
37 std::unique_ptr<DWARFDebugLine> Line;
38 std::unique_ptr<DWARFDebugFrame> DebugFrame;
40 DWARFUnitSection<DWARFCompileUnit> DWOCUs;
41 DWARFUnitSection<DWARFTypeUnit> DWOTUs;
42 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
43 std::unique_ptr<DWARFDebugLocDWO> LocDWO;
45 DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
46 DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
48 /// Read compile units from the debug_info section (if necessary)
49 /// and store them in CUs.
50 void parseCompileUnits();
52 /// Read type units from the debug_types sections (if necessary)
53 /// and store them in TUs.
54 void parseTypeUnits();
56 /// Read compile units from the debug_info.dwo section (if necessary)
57 /// and store them in DWOCUs.
58 void parseDWOCompileUnits();
60 /// Read type units from the debug_types.dwo section (if necessary)
61 /// and store them in DWOTUs.
62 void parseDWOTypeUnits();
70 DWARFContext() : DIContext(CK_DWARF) {}
72 static bool classof(const DIContext *DICtx) {
73 return DICtx->getKind() == CK_DWARF;
76 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
78 typedef DWARFUnitSection<DWARFCompileUnit>::iterator_range cu_iterator_range;
79 typedef DWARFUnitSection<DWARFTypeUnit>::iterator_range tu_iterator_range;
81 /// Get compile units in this context.
82 cu_iterator_range compile_units() {
84 return cu_iterator_range(CUs.begin(), CUs.end());
87 /// Get type units in this context.
88 tu_iterator_range type_units() {
90 return tu_iterator_range(TUs.begin(), TUs.end());
93 /// Get compile units in the DWO context.
94 cu_iterator_range dwo_compile_units() {
95 parseDWOCompileUnits();
96 return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
99 /// Get type units in the DWO context.
100 tu_iterator_range dwo_type_units() {
102 return tu_iterator_range(DWOTUs.begin(), DWOTUs.end());
105 /// Get the number of compile units in this context.
106 unsigned getNumCompileUnits() {
111 /// Get the number of compile units in this context.
112 unsigned getNumTypeUnits() {
117 /// Get the number of compile units in the DWO context.
118 unsigned getNumDWOCompileUnits() {
119 parseDWOCompileUnits();
120 return DWOCUs.size();
123 /// Get the number of compile units in the DWO context.
124 unsigned getNumDWOTypeUnits() {
126 return DWOTUs.size();
129 /// Get the compile unit at the specified index for this compile unit.
130 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
132 return CUs[index].get();
135 /// Get the compile unit at the specified index for the DWO compile units.
136 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
137 parseDWOCompileUnits();
138 return DWOCUs[index].get();
141 /// Get a pointer to the parsed DebugAbbrev object.
142 const DWARFDebugAbbrev *getDebugAbbrev();
144 /// Get a pointer to the parsed DebugLoc object.
145 const DWARFDebugLoc *getDebugLoc();
147 /// Get a pointer to the parsed dwo abbreviations object.
148 const DWARFDebugAbbrev *getDebugAbbrevDWO();
150 /// Get a pointer to the parsed DebugLoc object.
151 const DWARFDebugLocDWO *getDebugLocDWO();
153 /// Get a pointer to the parsed DebugAranges object.
154 const DWARFDebugAranges *getDebugAranges();
156 /// Get a pointer to the parsed frame information object.
157 const DWARFDebugFrame *getDebugFrame();
159 /// Get a pointer to a parsed line table corresponding to a compile unit.
160 const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
162 DILineInfo getLineInfoForAddress(uint64_t Address,
163 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
164 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
165 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
166 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
167 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
169 virtual bool isLittleEndian() const = 0;
170 virtual uint8_t getAddressSize() const = 0;
171 virtual const Section &getInfoSection() = 0;
172 typedef MapVector<object::SectionRef, Section,
173 std::map<object::SectionRef, unsigned> > TypeSectionMap;
174 virtual const TypeSectionMap &getTypesSections() = 0;
175 virtual StringRef getAbbrevSection() = 0;
176 virtual const Section &getLocSection() = 0;
177 virtual const Section &getLocDWOSection() = 0;
178 virtual StringRef getARangeSection() = 0;
179 virtual StringRef getDebugFrameSection() = 0;
180 virtual const Section &getLineSection() = 0;
181 virtual const Section &getLineDWOSection() = 0;
182 virtual StringRef getStringSection() = 0;
183 virtual StringRef getRangeSection() = 0;
184 virtual StringRef getPubNamesSection() = 0;
185 virtual StringRef getPubTypesSection() = 0;
186 virtual StringRef getGnuPubNamesSection() = 0;
187 virtual StringRef getGnuPubTypesSection() = 0;
189 // Sections for DWARF5 split dwarf proposal.
190 virtual const Section &getInfoDWOSection() = 0;
191 virtual const TypeSectionMap &getTypesDWOSections() = 0;
192 virtual StringRef getAbbrevDWOSection() = 0;
193 virtual StringRef getStringDWOSection() = 0;
194 virtual StringRef getStringOffsetDWOSection() = 0;
195 virtual StringRef getRangeDWOSection() = 0;
196 virtual StringRef getAddrSection() = 0;
198 static bool isSupportedVersion(unsigned version) {
199 return version == 2 || version == 3 || version == 4;
202 /// Return the compile unit that includes an offset (relative to .debug_info).
203 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
205 /// Return the compile unit which contains instruction with provided
207 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
210 /// DWARFContextInMemory is the simplest possible implementation of a
211 /// DWARFContext. It assumes all content is available in memory and stores
213 class DWARFContextInMemory : public DWARFContext {
214 virtual void anchor();
218 TypeSectionMap TypesSections;
219 StringRef AbbrevSection;
221 Section LocDWOSection;
222 StringRef ARangeSection;
223 StringRef DebugFrameSection;
225 Section LineDWOSection;
226 StringRef StringSection;
227 StringRef RangeSection;
228 StringRef PubNamesSection;
229 StringRef PubTypesSection;
230 StringRef GnuPubNamesSection;
231 StringRef GnuPubTypesSection;
233 // Sections for DWARF5 split dwarf proposal.
234 Section InfoDWOSection;
235 TypeSectionMap TypesDWOSections;
236 StringRef AbbrevDWOSection;
237 StringRef StringDWOSection;
238 StringRef StringOffsetDWOSection;
239 StringRef RangeDWOSection;
240 StringRef AddrSection;
242 SmallVector<SmallString<32>, 4> UncompressedSections;
245 DWARFContextInMemory(object::ObjectFile &);
246 bool isLittleEndian() const override { return IsLittleEndian; }
247 uint8_t getAddressSize() const override { return AddressSize; }
248 const Section &getInfoSection() override { return InfoSection; }
249 const TypeSectionMap &getTypesSections() override { return TypesSections; }
250 StringRef getAbbrevSection() override { return AbbrevSection; }
251 const Section &getLocSection() override { return LocSection; }
252 const Section &getLocDWOSection() override { return LocDWOSection; }
253 StringRef getARangeSection() override { return ARangeSection; }
254 StringRef getDebugFrameSection() override { return DebugFrameSection; }
255 const Section &getLineSection() override { return LineSection; }
256 const Section &getLineDWOSection() override { return LineDWOSection; }
257 StringRef getStringSection() override { return StringSection; }
258 StringRef getRangeSection() override { return RangeSection; }
259 StringRef getPubNamesSection() override { return PubNamesSection; }
260 StringRef getPubTypesSection() override { return PubTypesSection; }
261 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
262 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
264 // Sections for DWARF5 split dwarf proposal.
265 const Section &getInfoDWOSection() override { return InfoDWOSection; }
266 const TypeSectionMap &getTypesDWOSections() override {
267 return TypesDWOSections;
269 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
270 StringRef getStringDWOSection() override { return StringDWOSection; }
271 StringRef getStringOffsetDWOSection() override {
272 return StringOffsetDWOSection;
274 StringRef getRangeDWOSection() override { return RangeDWOSection; }
275 StringRef getAddrSection() override {