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/OwningPtr.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/DebugInfo/DIContext.h"
28 /// This data structure is the top level entity that deals with dwarf debug
29 /// information parsing. The actual data is supplied through pure virtual
30 /// methods that a concrete implementation provides.
31 class DWARFContext : public DIContext {
32 SmallVector<DWARFCompileUnit *, 1> CUs;
33 SmallVector<DWARFTypeUnit *, 1> TUs;
34 OwningPtr<DWARFDebugAbbrev> Abbrev;
35 OwningPtr<DWARFDebugLoc> Loc;
36 OwningPtr<DWARFDebugAranges> Aranges;
37 OwningPtr<DWARFDebugLine> Line;
38 OwningPtr<DWARFDebugFrame> DebugFrame;
40 SmallVector<DWARFCompileUnit *, 1> DWOCUs;
41 SmallVector<DWARFTypeUnit *, 1> DWOTUs;
42 OwningPtr<DWARFDebugAbbrev> AbbrevDWO;
44 DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
45 DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
47 /// Read compile units from the debug_info section and store them in CUs.
48 void parseCompileUnits();
50 /// Read type units from the debug_types sections and store them in CUs.
51 void parseTypeUnits();
53 /// Read compile units from the debug_info.dwo section and store them in
55 void parseDWOCompileUnits();
57 /// Read type units from the debug_types.dwo section and store them in
59 void parseDWOTypeUnits();
67 DWARFContext() : DIContext(CK_DWARF) {}
68 virtual ~DWARFContext();
70 static bool classof(const DIContext *DICtx) {
71 return DICtx->getKind() == CK_DWARF;
74 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
76 /// Get the number of compile units in this context.
77 unsigned getNumCompileUnits() {
83 /// Get the number of compile units in this context.
84 unsigned getNumTypeUnits() {
90 /// Get the number of compile units in the DWO context.
91 unsigned getNumDWOCompileUnits() {
93 parseDWOCompileUnits();
97 /// Get the number of compile units in the DWO context.
98 unsigned getNumDWOTypeUnits() {
101 return DWOTUs.size();
104 /// Get the compile unit at the specified index for this compile unit.
105 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
111 /// Get the type unit at the specified index for this compile unit.
112 DWARFTypeUnit *getTypeUnitAtIndex(unsigned index) {
118 /// Get the compile unit at the specified index for the DWO compile units.
119 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
121 parseDWOCompileUnits();
122 return DWOCUs[index];
125 /// Get the type unit at the specified index for the DWO type units.
126 DWARFTypeUnit *getDWOTypeUnitAtIndex(unsigned index) {
129 return DWOTUs[index];
132 /// Get a pointer to the parsed DebugAbbrev object.
133 const DWARFDebugAbbrev *getDebugAbbrev();
135 /// Get a pointer to the parsed DebugLoc object.
136 const DWARFDebugLoc *getDebugLoc();
138 /// Get a pointer to the parsed dwo abbreviations object.
139 const DWARFDebugAbbrev *getDebugAbbrevDWO();
141 /// Get a pointer to the parsed DebugAranges object.
142 const DWARFDebugAranges *getDebugAranges();
144 /// Get a pointer to the parsed frame information object.
145 const DWARFDebugFrame *getDebugFrame();
147 /// Get a pointer to a parsed line table corresponding to a compile unit.
148 const DWARFDebugLine::LineTable *
149 getLineTableForCompileUnit(DWARFCompileUnit *cu);
151 DILineInfo getLineInfoForAddress(uint64_t Address,
152 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
153 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
154 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
155 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
156 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
158 virtual bool isLittleEndian() const = 0;
159 virtual uint8_t getAddressSize() const = 0;
160 virtual const Section &getInfoSection() = 0;
161 typedef MapVector<object::SectionRef, Section,
162 std::map<object::SectionRef, unsigned> > TypeSectionMap;
163 virtual const TypeSectionMap &getTypesSections() = 0;
164 virtual StringRef getAbbrevSection() = 0;
165 virtual const Section &getLocSection() = 0;
166 virtual StringRef getARangeSection() = 0;
167 virtual StringRef getDebugFrameSection() = 0;
168 virtual const Section &getLineSection() = 0;
169 virtual const Section &getLineDWOSection() = 0;
170 virtual StringRef getStringSection() = 0;
171 virtual StringRef getRangeSection() = 0;
172 virtual StringRef getPubNamesSection() = 0;
173 virtual StringRef getPubTypesSection() = 0;
174 virtual StringRef getGnuPubNamesSection() = 0;
175 virtual StringRef getGnuPubTypesSection() = 0;
177 // Sections for DWARF5 split dwarf proposal.
178 virtual const Section &getInfoDWOSection() = 0;
179 virtual const TypeSectionMap &getTypesDWOSections() = 0;
180 virtual StringRef getAbbrevDWOSection() = 0;
181 virtual StringRef getStringDWOSection() = 0;
182 virtual StringRef getStringOffsetDWOSection() = 0;
183 virtual StringRef getRangeDWOSection() = 0;
184 virtual StringRef getAddrSection() = 0;
186 static bool isSupportedVersion(unsigned version) {
187 return version == 2 || version == 3 || version == 4;
190 /// Return the compile unit that includes an offset (relative to .debug_info).
191 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
193 /// Return the compile unit which contains instruction with provided
195 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
198 /// DWARFContextInMemory is the simplest possible implementation of a
199 /// DWARFContext. It assumes all content is available in memory and stores
201 class DWARFContextInMemory : public DWARFContext {
202 virtual void anchor();
206 TypeSectionMap TypesSections;
207 StringRef AbbrevSection;
209 StringRef ARangeSection;
210 StringRef DebugFrameSection;
212 Section LineDWOSection;
213 StringRef StringSection;
214 StringRef RangeSection;
215 StringRef PubNamesSection;
216 StringRef PubTypesSection;
217 StringRef GnuPubNamesSection;
218 StringRef GnuPubTypesSection;
220 // Sections for DWARF5 split dwarf proposal.
221 Section InfoDWOSection;
222 TypeSectionMap TypesDWOSections;
223 StringRef AbbrevDWOSection;
224 StringRef StringDWOSection;
225 StringRef StringOffsetDWOSection;
226 StringRef RangeDWOSection;
227 StringRef AddrSection;
229 SmallVector<MemoryBuffer*, 4> UncompressedSections;
232 DWARFContextInMemory(object::ObjectFile *);
233 ~DWARFContextInMemory();
234 bool isLittleEndian() const override { return IsLittleEndian; }
235 uint8_t getAddressSize() const override { return AddressSize; }
236 const Section &getInfoSection() override { return InfoSection; }
237 const TypeSectionMap &getTypesSections() override { return TypesSections; }
238 StringRef getAbbrevSection() override { return AbbrevSection; }
239 const Section &getLocSection() override { return LocSection; }
240 StringRef getARangeSection() override { return ARangeSection; }
241 StringRef getDebugFrameSection() override { return DebugFrameSection; }
242 const Section &getLineSection() override { return LineSection; }
243 const Section &getLineDWOSection() override { return LineDWOSection; }
244 StringRef getStringSection() override { return StringSection; }
245 StringRef getRangeSection() override { return RangeSection; }
246 StringRef getPubNamesSection() override { return PubNamesSection; }
247 StringRef getPubTypesSection() override { return PubTypesSection; }
248 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
249 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
251 // Sections for DWARF5 split dwarf proposal.
252 const Section &getInfoDWOSection() override { return InfoDWOSection; }
253 const TypeSectionMap &getTypesDWOSections() override {
254 return TypesDWOSections;
256 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
257 StringRef getStringDWOSection() override { return StringDWOSection; }
258 StringRef getStringOffsetDWOSection() override {
259 return StringOffsetDWOSection;
261 StringRef getRangeDWOSection() override { return RangeDWOSection; }
262 StringRef getAddrSection() override {