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/DenseMap.h"
14 #include "llvm/ADT/iterator_range.h"
15 #include "llvm/ADT/MapVector.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/DebugInfo/DIContext.h"
20 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
21 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
22 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
23 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
24 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
25 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
26 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
27 #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
28 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
29 #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
30 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
31 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
32 #include "llvm/Object/ObjectFile.h"
41 // In place of applying the relocations to the data we've read from disk we use
42 // a separate mapping table to the side and checking that at locations in the
43 // dwarf where we expect relocated values. This adds a bit of complexity to the
44 // dwarf parsing/extraction at the benefit of not allocating memory for the
45 // entire size of the debug info sections.
46 typedef DenseMap<uint64_t, std::pair<uint8_t, int64_t>> RelocAddrMap;
49 /// This data structure is the top level entity that deals with dwarf debug
50 /// information parsing. The actual data is supplied through pure virtual
51 /// methods that a concrete implementation provides.
52 class DWARFContext : public DIContext {
53 DWARFUnitSection<DWARFCompileUnit> CUs;
54 std::deque<DWARFUnitSection<DWARFTypeUnit>> TUs;
55 std::unique_ptr<DWARFUnitIndex> CUIndex;
56 std::unique_ptr<DWARFGdbIndex> GdbIndex;
57 std::unique_ptr<DWARFUnitIndex> TUIndex;
58 std::unique_ptr<DWARFDebugAbbrev> Abbrev;
59 std::unique_ptr<DWARFDebugLoc> Loc;
60 std::unique_ptr<DWARFDebugAranges> Aranges;
61 std::unique_ptr<DWARFDebugLine> Line;
62 std::unique_ptr<DWARFDebugFrame> DebugFrame;
63 std::unique_ptr<DWARFDebugFrame> EHFrame;
64 std::unique_ptr<DWARFDebugMacro> Macro;
66 DWARFUnitSection<DWARFCompileUnit> DWOCUs;
67 std::deque<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
68 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
69 std::unique_ptr<DWARFDebugLocDWO> LocDWO;
71 /// Read compile units from the debug_info section (if necessary)
72 /// and store them in CUs.
73 void parseCompileUnits();
75 /// Read type units from the debug_types sections (if necessary)
76 /// and store them in TUs.
77 void parseTypeUnits();
79 /// Read compile units from the debug_info.dwo section (if necessary)
80 /// and store them in DWOCUs.
81 void parseDWOCompileUnits();
83 /// Read type units from the debug_types.dwo section (if necessary)
84 /// and store them in DWOTUs.
85 void parseDWOTypeUnits();
88 DWARFContext() : DIContext(CK_DWARF) {}
89 DWARFContext(DWARFContext &) = delete;
90 DWARFContext &operator=(DWARFContext &) = delete;
92 static bool classof(const DIContext *DICtx) {
93 return DICtx->getKind() == CK_DWARF;
96 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All,
97 bool DumpEH = false, bool SummarizeTypes = false) override;
99 typedef DWARFUnitSection<DWARFCompileUnit>::iterator_range cu_iterator_range;
100 typedef DWARFUnitSection<DWARFTypeUnit>::iterator_range tu_iterator_range;
101 typedef iterator_range<decltype(TUs)::iterator> tu_section_iterator_range;
103 /// Get compile units in this context.
104 cu_iterator_range compile_units() {
106 return cu_iterator_range(CUs.begin(), CUs.end());
109 /// Get type units in this context.
110 tu_section_iterator_range type_unit_sections() {
112 return tu_section_iterator_range(TUs.begin(), TUs.end());
115 /// Get compile units in the DWO context.
116 cu_iterator_range dwo_compile_units() {
117 parseDWOCompileUnits();
118 return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
121 /// Get type units in the DWO context.
122 tu_section_iterator_range dwo_type_unit_sections() {
124 return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
127 /// Get the number of compile units in this context.
128 unsigned getNumCompileUnits() {
133 /// Get the number of compile units in this context.
134 unsigned getNumTypeUnits() {
139 /// Get the number of compile units in the DWO context.
140 unsigned getNumDWOCompileUnits() {
141 parseDWOCompileUnits();
142 return DWOCUs.size();
145 /// Get the number of compile units in the DWO context.
146 unsigned getNumDWOTypeUnits() {
148 return DWOTUs.size();
151 /// Get the compile unit at the specified index for this compile unit.
152 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
154 return CUs[index].get();
157 /// Get the compile unit at the specified index for the DWO compile units.
158 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
159 parseDWOCompileUnits();
160 return DWOCUs[index].get();
163 const DWARFUnitIndex &getCUIndex();
164 DWARFGdbIndex &getGdbIndex();
165 const DWARFUnitIndex &getTUIndex();
167 /// Get a pointer to the parsed DebugAbbrev object.
168 const DWARFDebugAbbrev *getDebugAbbrev();
170 /// Get a pointer to the parsed DebugLoc object.
171 const DWARFDebugLoc *getDebugLoc();
173 /// Get a pointer to the parsed dwo abbreviations object.
174 const DWARFDebugAbbrev *getDebugAbbrevDWO();
176 /// Get a pointer to the parsed DebugLoc object.
177 const DWARFDebugLocDWO *getDebugLocDWO();
179 /// Get a pointer to the parsed DebugAranges object.
180 const DWARFDebugAranges *getDebugAranges();
182 /// Get a pointer to the parsed frame information object.
183 const DWARFDebugFrame *getDebugFrame();
185 /// Get a pointer to the parsed eh frame information object.
186 const DWARFDebugFrame *getEHFrame();
188 /// Get a pointer to the parsed DebugMacro object.
189 const DWARFDebugMacro *getDebugMacro();
191 /// Get a pointer to a parsed line table corresponding to a compile unit.
192 const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
194 DILineInfo getLineInfoForAddress(uint64_t Address,
195 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
196 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
197 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
198 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
199 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
201 virtual bool isLittleEndian() const = 0;
202 virtual uint8_t getAddressSize() const = 0;
203 virtual const DWARFSection &getInfoSection() = 0;
204 typedef MapVector<object::SectionRef, DWARFSection,
205 std::map<object::SectionRef, unsigned>> TypeSectionMap;
206 virtual const TypeSectionMap &getTypesSections() = 0;
207 virtual StringRef getAbbrevSection() = 0;
208 virtual const DWARFSection &getLocSection() = 0;
209 virtual StringRef getARangeSection() = 0;
210 virtual StringRef getDebugFrameSection() = 0;
211 virtual StringRef getEHFrameSection() = 0;
212 virtual const DWARFSection &getLineSection() = 0;
213 virtual StringRef getStringSection() = 0;
214 virtual StringRef getRangeSection() = 0;
215 virtual StringRef getMacinfoSection() = 0;
216 virtual StringRef getPubNamesSection() = 0;
217 virtual StringRef getPubTypesSection() = 0;
218 virtual StringRef getGnuPubNamesSection() = 0;
219 virtual StringRef getGnuPubTypesSection() = 0;
221 // Sections for DWARF5 split dwarf proposal.
222 virtual const DWARFSection &getInfoDWOSection() = 0;
223 virtual const TypeSectionMap &getTypesDWOSections() = 0;
224 virtual StringRef getAbbrevDWOSection() = 0;
225 virtual const DWARFSection &getLineDWOSection() = 0;
226 virtual const DWARFSection &getLocDWOSection() = 0;
227 virtual StringRef getStringDWOSection() = 0;
228 virtual StringRef getStringOffsetDWOSection() = 0;
229 virtual StringRef getRangeDWOSection() = 0;
230 virtual StringRef getAddrSection() = 0;
231 virtual const DWARFSection& getAppleNamesSection() = 0;
232 virtual const DWARFSection& getAppleTypesSection() = 0;
233 virtual const DWARFSection& getAppleNamespacesSection() = 0;
234 virtual const DWARFSection& getAppleObjCSection() = 0;
235 virtual StringRef getCUIndexSection() = 0;
236 virtual StringRef getGdbIndexSection() = 0;
237 virtual StringRef getTUIndexSection() = 0;
239 static bool isSupportedVersion(unsigned version) {
240 return version == 2 || version == 3 || version == 4 || version == 5;
244 /// Return the compile unit that includes an offset (relative to .debug_info).
245 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
247 /// Return the compile unit which contains instruction with provided
249 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
252 /// DWARFContextInMemory is the simplest possible implementation of a
253 /// DWARFContext. It assumes all content is available in memory and stores
255 class DWARFContextInMemory : public DWARFContext {
256 virtual void anchor();
260 DWARFSection InfoSection;
261 TypeSectionMap TypesSections;
262 StringRef AbbrevSection;
263 DWARFSection LocSection;
264 StringRef ARangeSection;
265 StringRef DebugFrameSection;
266 StringRef EHFrameSection;
267 DWARFSection LineSection;
268 StringRef StringSection;
269 StringRef RangeSection;
270 StringRef MacinfoSection;
271 StringRef PubNamesSection;
272 StringRef PubTypesSection;
273 StringRef GnuPubNamesSection;
274 StringRef GnuPubTypesSection;
276 // Sections for DWARF5 split dwarf proposal.
277 DWARFSection InfoDWOSection;
278 TypeSectionMap TypesDWOSections;
279 StringRef AbbrevDWOSection;
280 DWARFSection LineDWOSection;
281 DWARFSection LocDWOSection;
282 StringRef StringDWOSection;
283 StringRef StringOffsetDWOSection;
284 StringRef RangeDWOSection;
285 StringRef AddrSection;
286 DWARFSection AppleNamesSection;
287 DWARFSection AppleTypesSection;
288 DWARFSection AppleNamespacesSection;
289 DWARFSection AppleObjCSection;
290 StringRef CUIndexSection;
291 StringRef GdbIndexSection;
292 StringRef TUIndexSection;
294 SmallVector<SmallString<32>, 4> UncompressedSections;
297 DWARFContextInMemory(const object::ObjectFile &Obj,
298 const LoadedObjectInfo *L = nullptr);
300 bool isLittleEndian() const override { return IsLittleEndian; }
301 uint8_t getAddressSize() const override { return AddressSize; }
302 const DWARFSection &getInfoSection() override { return InfoSection; }
303 const TypeSectionMap &getTypesSections() override { return TypesSections; }
304 StringRef getAbbrevSection() override { return AbbrevSection; }
305 const DWARFSection &getLocSection() override { return LocSection; }
306 StringRef getARangeSection() override { return ARangeSection; }
307 StringRef getDebugFrameSection() override { return DebugFrameSection; }
308 StringRef getEHFrameSection() override { return EHFrameSection; }
309 const DWARFSection &getLineSection() override { return LineSection; }
310 StringRef getStringSection() override { return StringSection; }
311 StringRef getRangeSection() override { return RangeSection; }
312 StringRef getMacinfoSection() override { return MacinfoSection; }
313 StringRef getPubNamesSection() override { return PubNamesSection; }
314 StringRef getPubTypesSection() override { return PubTypesSection; }
315 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
316 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
317 const DWARFSection& getAppleNamesSection() override { return AppleNamesSection; }
318 const DWARFSection& getAppleTypesSection() override { return AppleTypesSection; }
319 const DWARFSection& getAppleNamespacesSection() override { return AppleNamespacesSection; }
320 const DWARFSection& getAppleObjCSection() override { return AppleObjCSection; }
322 // Sections for DWARF5 split dwarf proposal.
323 const DWARFSection &getInfoDWOSection() override { return InfoDWOSection; }
324 const TypeSectionMap &getTypesDWOSections() override {
325 return TypesDWOSections;
327 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
328 const DWARFSection &getLineDWOSection() override { return LineDWOSection; }
329 const DWARFSection &getLocDWOSection() override { return LocDWOSection; }
330 StringRef getStringDWOSection() override { return StringDWOSection; }
331 StringRef getStringOffsetDWOSection() override {
332 return StringOffsetDWOSection;
334 StringRef getRangeDWOSection() override { return RangeDWOSection; }
335 StringRef getAddrSection() override {
338 StringRef getCUIndexSection() override { return CUIndexSection; }
339 StringRef getGdbIndexSection() override { return GdbIndexSection; }
340 StringRef getTUIndexSection() override { return TUIndexSection; }
343 } // end namespace llvm
345 #endif // LLVM_LIB_DEBUGINFO_DWARFCONTEXT_H