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 SmallVector<DWARFUnitSection<DWARFTypeUnit>,1> 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 SmallVector<DWARFUnitSection<DWARFTypeUnit>,1> 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;
80 typedef iterator_range<SmallVectorImpl<DWARFUnitSection<DWARFTypeUnit>>::iterator> tu_section_iterator_range;
82 /// Get compile units in this context.
83 cu_iterator_range compile_units() {
85 return cu_iterator_range(CUs.begin(), CUs.end());
88 /// Get type units in this context.
89 tu_section_iterator_range type_unit_sections() {
91 return tu_section_iterator_range(TUs.begin(), TUs.end());
94 /// Get compile units in the DWO context.
95 cu_iterator_range dwo_compile_units() {
96 parseDWOCompileUnits();
97 return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
100 /// Get type units in the DWO context.
101 tu_section_iterator_range dwo_type_unit_sections() {
103 return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
106 /// Get the number of compile units in this context.
107 unsigned getNumCompileUnits() {
112 /// Get the number of compile units in this context.
113 unsigned getNumTypeUnits() {
118 /// Get the number of compile units in the DWO context.
119 unsigned getNumDWOCompileUnits() {
120 parseDWOCompileUnits();
121 return DWOCUs.size();
124 /// Get the number of compile units in the DWO context.
125 unsigned getNumDWOTypeUnits() {
127 return DWOTUs.size();
130 /// Get the compile unit at the specified index for this compile unit.
131 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
133 return CUs[index].get();
136 /// Get the compile unit at the specified index for the DWO compile units.
137 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
138 parseDWOCompileUnits();
139 return DWOCUs[index].get();
142 /// Get a pointer to the parsed DebugAbbrev object.
143 const DWARFDebugAbbrev *getDebugAbbrev();
145 /// Get a pointer to the parsed DebugLoc object.
146 const DWARFDebugLoc *getDebugLoc();
148 /// Get a pointer to the parsed dwo abbreviations object.
149 const DWARFDebugAbbrev *getDebugAbbrevDWO();
151 /// Get a pointer to the parsed DebugLoc object.
152 const DWARFDebugLocDWO *getDebugLocDWO();
154 /// Get a pointer to the parsed DebugAranges object.
155 const DWARFDebugAranges *getDebugAranges();
157 /// Get a pointer to the parsed frame information object.
158 const DWARFDebugFrame *getDebugFrame();
160 /// Get a pointer to a parsed line table corresponding to a compile unit.
161 const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
163 DILineInfo getLineInfoForAddress(uint64_t Address,
164 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
165 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
166 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
167 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
168 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
170 virtual bool isLittleEndian() const = 0;
171 virtual uint8_t getAddressSize() const = 0;
172 virtual const Section &getInfoSection() = 0;
173 typedef MapVector<object::SectionRef, Section,
174 std::map<object::SectionRef, unsigned> > TypeSectionMap;
175 virtual const TypeSectionMap &getTypesSections() = 0;
176 virtual StringRef getAbbrevSection() = 0;
177 virtual const Section &getLocSection() = 0;
178 virtual const Section &getLocDWOSection() = 0;
179 virtual StringRef getARangeSection() = 0;
180 virtual StringRef getDebugFrameSection() = 0;
181 virtual const Section &getLineSection() = 0;
182 virtual const Section &getLineDWOSection() = 0;
183 virtual StringRef getStringSection() = 0;
184 virtual StringRef getRangeSection() = 0;
185 virtual StringRef getPubNamesSection() = 0;
186 virtual StringRef getPubTypesSection() = 0;
187 virtual StringRef getGnuPubNamesSection() = 0;
188 virtual StringRef getGnuPubTypesSection() = 0;
190 // Sections for DWARF5 split dwarf proposal.
191 virtual const Section &getInfoDWOSection() = 0;
192 virtual const TypeSectionMap &getTypesDWOSections() = 0;
193 virtual StringRef getAbbrevDWOSection() = 0;
194 virtual StringRef getStringDWOSection() = 0;
195 virtual StringRef getStringOffsetDWOSection() = 0;
196 virtual StringRef getRangeDWOSection() = 0;
197 virtual StringRef getAddrSection() = 0;
199 static bool isSupportedVersion(unsigned version) {
200 return version == 2 || version == 3 || version == 4;
203 /// Return the compile unit that includes an offset (relative to .debug_info).
204 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
206 /// Return the compile unit which contains instruction with provided
208 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
211 /// DWARFContextInMemory is the simplest possible implementation of a
212 /// DWARFContext. It assumes all content is available in memory and stores
214 class DWARFContextInMemory : public DWARFContext {
215 virtual void anchor();
219 TypeSectionMap TypesSections;
220 StringRef AbbrevSection;
222 Section LocDWOSection;
223 StringRef ARangeSection;
224 StringRef DebugFrameSection;
226 Section LineDWOSection;
227 StringRef StringSection;
228 StringRef RangeSection;
229 StringRef PubNamesSection;
230 StringRef PubTypesSection;
231 StringRef GnuPubNamesSection;
232 StringRef GnuPubTypesSection;
234 // Sections for DWARF5 split dwarf proposal.
235 Section InfoDWOSection;
236 TypeSectionMap TypesDWOSections;
237 StringRef AbbrevDWOSection;
238 StringRef StringDWOSection;
239 StringRef StringOffsetDWOSection;
240 StringRef RangeDWOSection;
241 StringRef AddrSection;
243 SmallVector<SmallString<32>, 4> UncompressedSections;
246 DWARFContextInMemory(object::ObjectFile &);
247 bool isLittleEndian() const override { return IsLittleEndian; }
248 uint8_t getAddressSize() const override { return AddressSize; }
249 const Section &getInfoSection() override { return InfoSection; }
250 const TypeSectionMap &getTypesSections() override { return TypesSections; }
251 StringRef getAbbrevSection() override { return AbbrevSection; }
252 const Section &getLocSection() override { return LocSection; }
253 const Section &getLocDWOSection() override { return LocDWOSection; }
254 StringRef getARangeSection() override { return ARangeSection; }
255 StringRef getDebugFrameSection() override { return DebugFrameSection; }
256 const Section &getLineSection() override { return LineSection; }
257 const Section &getLineDWOSection() override { return LineDWOSection; }
258 StringRef getStringSection() override { return StringSection; }
259 StringRef getRangeSection() override { return RangeSection; }
260 StringRef getPubNamesSection() override { return PubNamesSection; }
261 StringRef getPubTypesSection() override { return PubTypesSection; }
262 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
263 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
265 // Sections for DWARF5 split dwarf proposal.
266 const Section &getInfoDWOSection() override { return InfoDWOSection; }
267 const TypeSectionMap &getTypesDWOSections() override {
268 return TypesDWOSections;
270 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
271 StringRef getStringDWOSection() override { return StringDWOSection; }
272 StringRef getStringOffsetDWOSection() override {
273 return StringOffsetDWOSection;
275 StringRef getRangeDWOSection() override { return RangeDWOSection; }
276 StringRef getAddrSection() override {