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 "llvm/ADT/MapVector.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/DebugInfo/DIContext.h"
16 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
21 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
22 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
23 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
24 #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
29 // In place of applying the relocations to the data we've read from disk we use
30 // a separate mapping table to the side and checking that at locations in the
31 // dwarf where we expect relocated values. This adds a bit of complexity to the
32 // dwarf parsing/extraction at the benefit of not allocating memory for the
33 // entire size of the debug info sections.
34 typedef DenseMap<uint64_t, std::pair<uint8_t, int64_t> > RelocAddrMap;
37 /// This data structure is the top level entity that deals with dwarf debug
38 /// information parsing. The actual data is supplied through pure virtual
39 /// methods that a concrete implementation provides.
40 class DWARFContext : public DIContext {
42 DWARFUnitSection<DWARFCompileUnit> CUs;
43 std::vector<DWARFUnitSection<DWARFTypeUnit>> TUs;
44 std::unique_ptr<DWARFDebugAbbrev> Abbrev;
45 std::unique_ptr<DWARFDebugLoc> Loc;
46 std::unique_ptr<DWARFDebugAranges> Aranges;
47 std::unique_ptr<DWARFDebugLine> Line;
48 std::unique_ptr<DWARFDebugFrame> DebugFrame;
49 std::unique_ptr<DWARFDebugMacro> Macro;
51 DWARFUnitSection<DWARFCompileUnit> DWOCUs;
52 std::vector<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
53 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
54 std::unique_ptr<DWARFDebugLocDWO> LocDWO;
56 DWARFContext(DWARFContext &) = delete;
57 DWARFContext &operator=(DWARFContext &) = delete;
59 /// Read compile units from the debug_info section (if necessary)
60 /// and store them in CUs.
61 void parseCompileUnits();
63 /// Read type units from the debug_types sections (if necessary)
64 /// and store them in TUs.
65 void parseTypeUnits();
67 /// Read compile units from the debug_info.dwo section (if necessary)
68 /// and store them in DWOCUs.
69 void parseDWOCompileUnits();
71 /// Read type units from the debug_types.dwo section (if necessary)
72 /// and store them in DWOTUs.
73 void parseDWOTypeUnits();
76 DWARFContext() : DIContext(CK_DWARF) {}
78 static bool classof(const DIContext *DICtx) {
79 return DICtx->getKind() == CK_DWARF;
82 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
84 typedef DWARFUnitSection<DWARFCompileUnit>::iterator_range cu_iterator_range;
85 typedef DWARFUnitSection<DWARFTypeUnit>::iterator_range tu_iterator_range;
86 typedef iterator_range<std::vector<DWARFUnitSection<DWARFTypeUnit>>::iterator> tu_section_iterator_range;
88 /// Get compile units in this context.
89 cu_iterator_range compile_units() {
91 return cu_iterator_range(CUs.begin(), CUs.end());
94 /// Get type units in this context.
95 tu_section_iterator_range type_unit_sections() {
97 return tu_section_iterator_range(TUs.begin(), TUs.end());
100 /// Get compile units in the DWO context.
101 cu_iterator_range dwo_compile_units() {
102 parseDWOCompileUnits();
103 return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
106 /// Get type units in the DWO context.
107 tu_section_iterator_range dwo_type_unit_sections() {
109 return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
112 /// Get the number of compile units in this context.
113 unsigned getNumCompileUnits() {
118 /// Get the number of compile units in this context.
119 unsigned getNumTypeUnits() {
124 /// Get the number of compile units in the DWO context.
125 unsigned getNumDWOCompileUnits() {
126 parseDWOCompileUnits();
127 return DWOCUs.size();
130 /// Get the number of compile units in the DWO context.
131 unsigned getNumDWOTypeUnits() {
133 return DWOTUs.size();
136 /// Get the compile unit at the specified index for this compile unit.
137 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
139 return CUs[index].get();
142 /// Get the compile unit at the specified index for the DWO compile units.
143 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
144 parseDWOCompileUnits();
145 return DWOCUs[index].get();
148 /// Get a pointer to the parsed DebugAbbrev object.
149 const DWARFDebugAbbrev *getDebugAbbrev();
151 /// Get a pointer to the parsed DebugLoc object.
152 const DWARFDebugLoc *getDebugLoc();
154 /// Get a pointer to the parsed dwo abbreviations object.
155 const DWARFDebugAbbrev *getDebugAbbrevDWO();
157 /// Get a pointer to the parsed DebugLoc object.
158 const DWARFDebugLocDWO *getDebugLocDWO();
160 /// Get a pointer to the parsed DebugAranges object.
161 const DWARFDebugAranges *getDebugAranges();
163 /// Get a pointer to the parsed frame information object.
164 const DWARFDebugFrame *getDebugFrame();
166 /// Get a pointer to the parsed DebugMacro object.
167 const DWARFDebugMacro *getDebugMacro();
169 /// Get a pointer to a parsed line table corresponding to a compile unit.
170 const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
172 DILineInfo getLineInfoForAddress(uint64_t Address,
173 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
174 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
175 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
176 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
177 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
179 virtual bool isLittleEndian() const = 0;
180 virtual uint8_t getAddressSize() const = 0;
181 virtual const DWARFSection &getInfoSection() = 0;
182 typedef MapVector<object::SectionRef, DWARFSection,
183 std::map<object::SectionRef, unsigned>> TypeSectionMap;
184 virtual const TypeSectionMap &getTypesSections() = 0;
185 virtual StringRef getAbbrevSection() = 0;
186 virtual const DWARFSection &getLocSection() = 0;
187 virtual StringRef getARangeSection() = 0;
188 virtual StringRef getDebugFrameSection() = 0;
189 virtual const DWARFSection &getLineSection() = 0;
190 virtual StringRef getStringSection() = 0;
191 virtual StringRef getRangeSection() = 0;
192 virtual StringRef getMacinfoSection() = 0;
193 virtual StringRef getPubNamesSection() = 0;
194 virtual StringRef getPubTypesSection() = 0;
195 virtual StringRef getGnuPubNamesSection() = 0;
196 virtual StringRef getGnuPubTypesSection() = 0;
198 // Sections for DWARF5 split dwarf proposal.
199 virtual const DWARFSection &getInfoDWOSection() = 0;
200 virtual const TypeSectionMap &getTypesDWOSections() = 0;
201 virtual StringRef getAbbrevDWOSection() = 0;
202 virtual const DWARFSection &getLineDWOSection() = 0;
203 virtual const DWARFSection &getLocDWOSection() = 0;
204 virtual StringRef getStringDWOSection() = 0;
205 virtual StringRef getStringOffsetDWOSection() = 0;
206 virtual StringRef getRangeDWOSection() = 0;
207 virtual StringRef getAddrSection() = 0;
208 virtual const DWARFSection& getAppleNamesSection() = 0;
209 virtual const DWARFSection& getAppleTypesSection() = 0;
210 virtual const DWARFSection& getAppleNamespacesSection() = 0;
211 virtual const DWARFSection& getAppleObjCSection() = 0;
212 virtual StringRef getCUIndexSection() = 0;
213 virtual StringRef getTUIndexSection() = 0;
215 static bool isSupportedVersion(unsigned version) {
216 return version == 2 || version == 3 || version == 4;
219 /// Return the compile unit that includes an offset (relative to .debug_info).
220 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
222 /// Return the compile unit which contains instruction with provided
224 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
227 /// DWARFContextInMemory is the simplest possible implementation of a
228 /// DWARFContext. It assumes all content is available in memory and stores
230 class DWARFContextInMemory : public DWARFContext {
231 virtual void anchor();
234 DWARFSection InfoSection;
235 TypeSectionMap TypesSections;
236 StringRef AbbrevSection;
237 DWARFSection LocSection;
238 StringRef ARangeSection;
239 StringRef DebugFrameSection;
240 DWARFSection LineSection;
241 StringRef StringSection;
242 StringRef RangeSection;
243 StringRef MacinfoSection;
244 StringRef PubNamesSection;
245 StringRef PubTypesSection;
246 StringRef GnuPubNamesSection;
247 StringRef GnuPubTypesSection;
249 // Sections for DWARF5 split dwarf proposal.
250 DWARFSection InfoDWOSection;
251 TypeSectionMap TypesDWOSections;
252 StringRef AbbrevDWOSection;
253 DWARFSection LineDWOSection;
254 DWARFSection LocDWOSection;
255 StringRef StringDWOSection;
256 StringRef StringOffsetDWOSection;
257 StringRef RangeDWOSection;
258 StringRef AddrSection;
259 DWARFSection AppleNamesSection;
260 DWARFSection AppleTypesSection;
261 DWARFSection AppleNamespacesSection;
262 DWARFSection AppleObjCSection;
263 StringRef CUIndexSection;
264 StringRef TUIndexSection;
266 SmallVector<SmallString<32>, 4> UncompressedSections;
269 DWARFContextInMemory(const object::ObjectFile &Obj,
270 const LoadedObjectInfo *L = nullptr);
271 bool isLittleEndian() const override { return IsLittleEndian; }
272 uint8_t getAddressSize() const override { return AddressSize; }
273 const DWARFSection &getInfoSection() override { return InfoSection; }
274 const TypeSectionMap &getTypesSections() override { return TypesSections; }
275 StringRef getAbbrevSection() override { return AbbrevSection; }
276 const DWARFSection &getLocSection() override { return LocSection; }
277 StringRef getARangeSection() override { return ARangeSection; }
278 StringRef getDebugFrameSection() override { return DebugFrameSection; }
279 const DWARFSection &getLineSection() override { return LineSection; }
280 StringRef getStringSection() override { return StringSection; }
281 StringRef getRangeSection() override { return RangeSection; }
282 StringRef getMacinfoSection() override { return MacinfoSection; }
283 StringRef getPubNamesSection() override { return PubNamesSection; }
284 StringRef getPubTypesSection() override { return PubTypesSection; }
285 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
286 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
287 const DWARFSection& getAppleNamesSection() override { return AppleNamesSection; }
288 const DWARFSection& getAppleTypesSection() override { return AppleTypesSection; }
289 const DWARFSection& getAppleNamespacesSection() override { return AppleNamespacesSection; }
290 const DWARFSection& getAppleObjCSection() override { return AppleObjCSection; }
292 // Sections for DWARF5 split dwarf proposal.
293 const DWARFSection &getInfoDWOSection() override { return InfoDWOSection; }
294 const TypeSectionMap &getTypesDWOSections() override {
295 return TypesDWOSections;
297 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
298 const DWARFSection &getLineDWOSection() override { return LineDWOSection; }
299 const DWARFSection &getLocDWOSection() override { return LocDWOSection; }
300 StringRef getStringDWOSection() override { return StringDWOSection; }
301 StringRef getStringOffsetDWOSection() override {
302 return StringOffsetDWOSection;
304 StringRef getRangeDWOSection() override { return RangeDWOSection; }
305 StringRef getAddrSection() override {
308 StringRef getCUIndexSection() override { return CUIndexSection; }
309 StringRef getTUIndexSection() override { return TUIndexSection; }