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;
46 DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
47 DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
49 /// Read compile units from the debug_info section (if necessary)
50 /// and store them in CUs.
51 void parseCompileUnits();
53 /// Read type units from the debug_types sections (if necessary)
54 /// and store them in TUs.
55 void parseTypeUnits();
57 /// Read compile units from the debug_info.dwo section (if necessary)
58 /// and store them in DWOCUs.
59 void parseDWOCompileUnits();
61 /// Read type units from the debug_types.dwo section (if necessary)
62 /// and store them in DWOTUs.
63 void parseDWOTypeUnits();
71 DWARFContext() : DIContext(CK_DWARF) {}
73 static bool classof(const DIContext *DICtx) {
74 return DICtx->getKind() == CK_DWARF;
77 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
79 typedef iterator_range<CUVector::iterator> cu_iterator_range;
80 typedef iterator_range<TUVector::iterator> tu_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_iterator_range type_units() {
91 return tu_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_iterator_range dwo_type_units() {
103 return tu_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 DebugAranges object.
152 const DWARFDebugAranges *getDebugAranges();
154 /// Get a pointer to the parsed frame information object.
155 const DWARFDebugFrame *getDebugFrame();
157 /// Get a pointer to a parsed line table corresponding to a compile unit.
158 const DWARFDebugLine::LineTable *
159 getLineTableForCompileUnit(DWARFCompileUnit *cu);
161 DILineInfo getLineInfoForAddress(uint64_t Address,
162 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
163 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
164 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
165 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
166 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
168 virtual bool isLittleEndian() const = 0;
169 virtual uint8_t getAddressSize() const = 0;
170 virtual const Section &getInfoSection() = 0;
171 typedef MapVector<object::SectionRef, Section,
172 std::map<object::SectionRef, unsigned> > TypeSectionMap;
173 virtual const TypeSectionMap &getTypesSections() = 0;
174 virtual StringRef getAbbrevSection() = 0;
175 virtual const Section &getLocSection() = 0;
176 virtual StringRef getARangeSection() = 0;
177 virtual StringRef getDebugFrameSection() = 0;
178 virtual const Section &getLineSection() = 0;
179 virtual const Section &getLineDWOSection() = 0;
180 virtual StringRef getStringSection() = 0;
181 virtual StringRef getRangeSection() = 0;
182 virtual StringRef getPubNamesSection() = 0;
183 virtual StringRef getPubTypesSection() = 0;
184 virtual StringRef getGnuPubNamesSection() = 0;
185 virtual StringRef getGnuPubTypesSection() = 0;
187 // Sections for DWARF5 split dwarf proposal.
188 virtual const Section &getInfoDWOSection() = 0;
189 virtual const TypeSectionMap &getTypesDWOSections() = 0;
190 virtual StringRef getAbbrevDWOSection() = 0;
191 virtual StringRef getStringDWOSection() = 0;
192 virtual StringRef getStringOffsetDWOSection() = 0;
193 virtual StringRef getRangeDWOSection() = 0;
194 virtual StringRef getAddrSection() = 0;
196 static bool isSupportedVersion(unsigned version) {
197 return version == 2 || version == 3 || version == 4;
200 /// Return the compile unit that includes an offset (relative to .debug_info).
201 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
203 /// Return the compile unit which contains instruction with provided
205 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
208 /// DWARFContextInMemory is the simplest possible implementation of a
209 /// DWARFContext. It assumes all content is available in memory and stores
211 class DWARFContextInMemory : public DWARFContext {
212 virtual void anchor();
216 TypeSectionMap TypesSections;
217 StringRef AbbrevSection;
219 StringRef ARangeSection;
220 StringRef DebugFrameSection;
222 Section LineDWOSection;
223 StringRef StringSection;
224 StringRef RangeSection;
225 StringRef PubNamesSection;
226 StringRef PubTypesSection;
227 StringRef GnuPubNamesSection;
228 StringRef GnuPubTypesSection;
230 // Sections for DWARF5 split dwarf proposal.
231 Section InfoDWOSection;
232 TypeSectionMap TypesDWOSections;
233 StringRef AbbrevDWOSection;
234 StringRef StringDWOSection;
235 StringRef StringOffsetDWOSection;
236 StringRef RangeDWOSection;
237 StringRef AddrSection;
239 SmallVector<std::unique_ptr<MemoryBuffer>, 4> UncompressedSections;
242 DWARFContextInMemory(object::ObjectFile *);
243 bool isLittleEndian() const override { return IsLittleEndian; }
244 uint8_t getAddressSize() const override { return AddressSize; }
245 const Section &getInfoSection() override { return InfoSection; }
246 const TypeSectionMap &getTypesSections() override { return TypesSections; }
247 StringRef getAbbrevSection() override { return AbbrevSection; }
248 const Section &getLocSection() override { return LocSection; }
249 StringRef getARangeSection() override { return ARangeSection; }
250 StringRef getDebugFrameSection() override { return DebugFrameSection; }
251 const Section &getLineSection() override { return LineSection; }
252 const Section &getLineDWOSection() override { return LineDWOSection; }
253 StringRef getStringSection() override { return StringSection; }
254 StringRef getRangeSection() override { return RangeSection; }
255 StringRef getPubNamesSection() override { return PubNamesSection; }
256 StringRef getPubTypesSection() override { return PubTypesSection; }
257 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
258 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
260 // Sections for DWARF5 split dwarf proposal.
261 const Section &getInfoDWOSection() override { return InfoDWOSection; }
262 const TypeSectionMap &getTypesDWOSections() override {
263 return TypesDWOSections;
265 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
266 StringRef getStringDWOSection() override { return StringDWOSection; }
267 StringRef getStringOffsetDWOSection() override {
268 return StringOffsetDWOSection;
270 StringRef getRangeDWOSection() override { return RangeDWOSection; }
271 StringRef getAddrSection() override {