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 SmallVector<DWARFCompileUnit *, 1> CUs;
32 SmallVector<DWARFTypeUnit *, 1> TUs;
33 std::unique_ptr<DWARFDebugAbbrev> Abbrev;
34 std::unique_ptr<DWARFDebugLoc> Loc;
35 std::unique_ptr<DWARFDebugAranges> Aranges;
36 std::unique_ptr<DWARFDebugLine> Line;
37 std::unique_ptr<DWARFDebugFrame> DebugFrame;
39 SmallVector<DWARFCompileUnit *, 1> DWOCUs;
40 SmallVector<DWARFTypeUnit *, 1> DWOTUs;
41 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
43 DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
44 DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
46 /// Read compile units from the debug_info section and store them in CUs.
47 void parseCompileUnits();
49 /// Read type units from the debug_types sections and store them in CUs.
50 void parseTypeUnits();
52 /// Read compile units from the debug_info.dwo section and store them in
54 void parseDWOCompileUnits();
56 /// Read type units from the debug_types.dwo section and store them in
58 void parseDWOTypeUnits();
66 DWARFContext() : DIContext(CK_DWARF) {}
67 virtual ~DWARFContext();
69 static bool classof(const DIContext *DICtx) {
70 return DICtx->getKind() == CK_DWARF;
73 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
75 /// Get the number of compile units in this context.
76 unsigned getNumCompileUnits() {
82 /// Get the number of compile units in this context.
83 unsigned getNumTypeUnits() {
89 /// Get the number of compile units in the DWO context.
90 unsigned getNumDWOCompileUnits() {
92 parseDWOCompileUnits();
96 /// Get the number of compile units in the DWO context.
97 unsigned getNumDWOTypeUnits() {
100 return DWOTUs.size();
103 /// Get the compile unit at the specified index for this compile unit.
104 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
110 /// Get the type unit at the specified index for this compile unit.
111 DWARFTypeUnit *getTypeUnitAtIndex(unsigned index) {
117 /// Get the compile unit at the specified index for the DWO compile units.
118 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
120 parseDWOCompileUnits();
121 return DWOCUs[index];
124 /// Get the type unit at the specified index for the DWO type units.
125 DWARFTypeUnit *getDWOTypeUnitAtIndex(unsigned index) {
128 return DWOTUs[index];
131 /// Get a pointer to the parsed DebugAbbrev object.
132 const DWARFDebugAbbrev *getDebugAbbrev();
134 /// Get a pointer to the parsed DebugLoc object.
135 const DWARFDebugLoc *getDebugLoc();
137 /// Get a pointer to the parsed dwo abbreviations object.
138 const DWARFDebugAbbrev *getDebugAbbrevDWO();
140 /// Get a pointer to the parsed DebugAranges object.
141 const DWARFDebugAranges *getDebugAranges();
143 /// Get a pointer to the parsed frame information object.
144 const DWARFDebugFrame *getDebugFrame();
146 /// Get a pointer to a parsed line table corresponding to a compile unit.
147 const DWARFDebugLine::LineTable *
148 getLineTableForCompileUnit(DWARFCompileUnit *cu);
150 DILineInfo getLineInfoForAddress(uint64_t Address,
151 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
152 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
153 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
154 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
155 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
157 virtual bool isLittleEndian() const = 0;
158 virtual uint8_t getAddressSize() const = 0;
159 virtual const Section &getInfoSection() = 0;
160 typedef MapVector<object::SectionRef, Section,
161 std::map<object::SectionRef, unsigned> > TypeSectionMap;
162 virtual const TypeSectionMap &getTypesSections() = 0;
163 virtual StringRef getAbbrevSection() = 0;
164 virtual const Section &getLocSection() = 0;
165 virtual StringRef getARangeSection() = 0;
166 virtual StringRef getDebugFrameSection() = 0;
167 virtual const Section &getLineSection() = 0;
168 virtual const Section &getLineDWOSection() = 0;
169 virtual StringRef getStringSection() = 0;
170 virtual StringRef getRangeSection() = 0;
171 virtual StringRef getPubNamesSection() = 0;
172 virtual StringRef getPubTypesSection() = 0;
173 virtual StringRef getGnuPubNamesSection() = 0;
174 virtual StringRef getGnuPubTypesSection() = 0;
176 // Sections for DWARF5 split dwarf proposal.
177 virtual const Section &getInfoDWOSection() = 0;
178 virtual const TypeSectionMap &getTypesDWOSections() = 0;
179 virtual StringRef getAbbrevDWOSection() = 0;
180 virtual StringRef getStringDWOSection() = 0;
181 virtual StringRef getStringOffsetDWOSection() = 0;
182 virtual StringRef getRangeDWOSection() = 0;
183 virtual StringRef getAddrSection() = 0;
185 static bool isSupportedVersion(unsigned version) {
186 return version == 2 || version == 3 || version == 4;
189 /// Return the compile unit that includes an offset (relative to .debug_info).
190 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
192 /// Return the compile unit which contains instruction with provided
194 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
197 /// DWARFContextInMemory is the simplest possible implementation of a
198 /// DWARFContext. It assumes all content is available in memory and stores
200 class DWARFContextInMemory : public DWARFContext {
201 virtual void anchor();
205 TypeSectionMap TypesSections;
206 StringRef AbbrevSection;
208 StringRef ARangeSection;
209 StringRef DebugFrameSection;
211 Section LineDWOSection;
212 StringRef StringSection;
213 StringRef RangeSection;
214 StringRef PubNamesSection;
215 StringRef PubTypesSection;
216 StringRef GnuPubNamesSection;
217 StringRef GnuPubTypesSection;
219 // Sections for DWARF5 split dwarf proposal.
220 Section InfoDWOSection;
221 TypeSectionMap TypesDWOSections;
222 StringRef AbbrevDWOSection;
223 StringRef StringDWOSection;
224 StringRef StringOffsetDWOSection;
225 StringRef RangeDWOSection;
226 StringRef AddrSection;
228 SmallVector<MemoryBuffer*, 4> UncompressedSections;
231 DWARFContextInMemory(object::ObjectFile *);
232 ~DWARFContextInMemory();
233 bool isLittleEndian() const override { return IsLittleEndian; }
234 uint8_t getAddressSize() const override { return AddressSize; }
235 const Section &getInfoSection() override { return InfoSection; }
236 const TypeSectionMap &getTypesSections() override { return TypesSections; }
237 StringRef getAbbrevSection() override { return AbbrevSection; }
238 const Section &getLocSection() override { return LocSection; }
239 StringRef getARangeSection() override { return ARangeSection; }
240 StringRef getDebugFrameSection() override { return DebugFrameSection; }
241 const Section &getLineSection() override { return LineSection; }
242 const Section &getLineDWOSection() override { return LineDWOSection; }
243 StringRef getStringSection() override { return StringSection; }
244 StringRef getRangeSection() override { return RangeSection; }
245 StringRef getPubNamesSection() override { return PubNamesSection; }
246 StringRef getPubTypesSection() override { return PubTypesSection; }
247 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
248 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
250 // Sections for DWARF5 split dwarf proposal.
251 const Section &getInfoDWOSection() override { return InfoDWOSection; }
252 const TypeSectionMap &getTypesDWOSections() override {
253 return TypesDWOSections;
255 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
256 StringRef getStringDWOSection() override { return StringDWOSection; }
257 StringRef getStringOffsetDWOSection() override {
258 return StringOffsetDWOSection;
260 StringRef getRangeDWOSection() override { return RangeDWOSection; }
261 StringRef getAddrSection() override {