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_DWARF_DWARFCONTEXT_H
11 #define LLVM_DEBUGINFO_DWARF_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/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/DebugInfo/DIContext.h"
21 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
22 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
23 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
24 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
25 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
26 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
27 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
28 #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
29 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
30 #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
31 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
32 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
33 #include "llvm/Object/ObjectFile.h"
34 #include "llvm/Support/Host.h"
46 // In place of applying the relocations to the data we've read from disk we use
47 // a separate mapping table to the side and checking that at locations in the
48 // dwarf where we expect relocated values. This adds a bit of complexity to the
49 // dwarf parsing/extraction at the benefit of not allocating memory for the
50 // entire size of the debug info sections.
51 typedef DenseMap<uint64_t, std::pair<uint8_t, int64_t>> RelocAddrMap;
54 /// This data structure is the top level entity that deals with dwarf debug
55 /// information parsing. The actual data is supplied through pure virtual
56 /// methods that a concrete implementation provides.
57 class DWARFContext : public DIContext {
58 DWARFUnitSection<DWARFCompileUnit> CUs;
59 std::deque<DWARFUnitSection<DWARFTypeUnit>> TUs;
60 std::unique_ptr<DWARFUnitIndex> CUIndex;
61 std::unique_ptr<DWARFGdbIndex> GdbIndex;
62 std::unique_ptr<DWARFUnitIndex> TUIndex;
63 std::unique_ptr<DWARFDebugAbbrev> Abbrev;
64 std::unique_ptr<DWARFDebugLoc> Loc;
65 std::unique_ptr<DWARFDebugAranges> Aranges;
66 std::unique_ptr<DWARFDebugLine> Line;
67 std::unique_ptr<DWARFDebugFrame> DebugFrame;
68 std::unique_ptr<DWARFDebugFrame> EHFrame;
69 std::unique_ptr<DWARFDebugMacro> Macro;
71 DWARFUnitSection<DWARFCompileUnit> DWOCUs;
72 std::deque<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
73 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
74 std::unique_ptr<DWARFDebugLocDWO> LocDWO;
76 /// Read compile units from the debug_info section (if necessary)
77 /// and store them in CUs.
78 void parseCompileUnits();
80 /// Read type units from the debug_types sections (if necessary)
81 /// and store them in TUs.
82 void parseTypeUnits();
84 /// Read compile units from the debug_info.dwo section (if necessary)
85 /// and store them in DWOCUs.
86 void parseDWOCompileUnits();
88 /// Read type units from the debug_types.dwo section (if necessary)
89 /// and store them in DWOTUs.
90 void parseDWOTypeUnits();
93 DWARFContext() : DIContext(CK_DWARF) {}
94 DWARFContext(DWARFContext &) = delete;
95 DWARFContext &operator=(DWARFContext &) = delete;
97 static bool classof(const DIContext *DICtx) {
98 return DICtx->getKind() == CK_DWARF;
101 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All,
102 bool DumpEH = false, bool SummarizeTypes = false) override;
104 typedef DWARFUnitSection<DWARFCompileUnit>::iterator_range cu_iterator_range;
105 typedef DWARFUnitSection<DWARFTypeUnit>::iterator_range tu_iterator_range;
106 typedef iterator_range<decltype(TUs)::iterator> tu_section_iterator_range;
108 /// Get compile units in this context.
109 cu_iterator_range compile_units() {
111 return cu_iterator_range(CUs.begin(), CUs.end());
114 /// Get type units in this context.
115 tu_section_iterator_range type_unit_sections() {
117 return tu_section_iterator_range(TUs.begin(), TUs.end());
120 /// Get compile units in the DWO context.
121 cu_iterator_range dwo_compile_units() {
122 parseDWOCompileUnits();
123 return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
126 /// Get type units in the DWO context.
127 tu_section_iterator_range dwo_type_unit_sections() {
129 return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
132 /// Get the number of compile units in this context.
133 unsigned getNumCompileUnits() {
138 /// Get the number of compile units in this context.
139 unsigned getNumTypeUnits() {
144 /// Get the number of compile units in the DWO context.
145 unsigned getNumDWOCompileUnits() {
146 parseDWOCompileUnits();
147 return DWOCUs.size();
150 /// Get the number of compile units in the DWO context.
151 unsigned getNumDWOTypeUnits() {
153 return DWOTUs.size();
156 /// Get the compile unit at the specified index for this compile unit.
157 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
159 return CUs[index].get();
162 /// Get the compile unit at the specified index for the DWO compile units.
163 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
164 parseDWOCompileUnits();
165 return DWOCUs[index].get();
168 const DWARFUnitIndex &getCUIndex();
169 DWARFGdbIndex &getGdbIndex();
170 const DWARFUnitIndex &getTUIndex();
172 /// Get a pointer to the parsed DebugAbbrev object.
173 const DWARFDebugAbbrev *getDebugAbbrev();
175 /// Get a pointer to the parsed DebugLoc object.
176 const DWARFDebugLoc *getDebugLoc();
178 /// Get a pointer to the parsed dwo abbreviations object.
179 const DWARFDebugAbbrev *getDebugAbbrevDWO();
181 /// Get a pointer to the parsed DebugLoc object.
182 const DWARFDebugLocDWO *getDebugLocDWO();
184 /// Get a pointer to the parsed DebugAranges object.
185 const DWARFDebugAranges *getDebugAranges();
187 /// Get a pointer to the parsed frame information object.
188 const DWARFDebugFrame *getDebugFrame();
190 /// Get a pointer to the parsed eh frame information object.
191 const DWARFDebugFrame *getEHFrame();
193 /// Get a pointer to the parsed DebugMacro object.
194 const DWARFDebugMacro *getDebugMacro();
196 /// Get a pointer to a parsed line table corresponding to a compile unit.
197 const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
199 DILineInfo getLineInfoForAddress(uint64_t Address,
200 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
201 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
202 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
203 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
204 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
206 virtual bool isLittleEndian() const = 0;
207 virtual uint8_t getAddressSize() const = 0;
208 virtual const DWARFSection &getInfoSection() = 0;
209 typedef MapVector<object::SectionRef, DWARFSection,
210 std::map<object::SectionRef, unsigned>> TypeSectionMap;
211 virtual const TypeSectionMap &getTypesSections() = 0;
212 virtual StringRef getAbbrevSection() = 0;
213 virtual const DWARFSection &getLocSection() = 0;
214 virtual StringRef getARangeSection() = 0;
215 virtual StringRef getDebugFrameSection() = 0;
216 virtual StringRef getEHFrameSection() = 0;
217 virtual const DWARFSection &getLineSection() = 0;
218 virtual StringRef getStringSection() = 0;
219 virtual StringRef getRangeSection() = 0;
220 virtual StringRef getMacinfoSection() = 0;
221 virtual StringRef getPubNamesSection() = 0;
222 virtual StringRef getPubTypesSection() = 0;
223 virtual StringRef getGnuPubNamesSection() = 0;
224 virtual StringRef getGnuPubTypesSection() = 0;
226 // Sections for DWARF5 split dwarf proposal.
227 virtual const DWARFSection &getInfoDWOSection() = 0;
228 virtual const TypeSectionMap &getTypesDWOSections() = 0;
229 virtual StringRef getAbbrevDWOSection() = 0;
230 virtual const DWARFSection &getLineDWOSection() = 0;
231 virtual const DWARFSection &getLocDWOSection() = 0;
232 virtual StringRef getStringDWOSection() = 0;
233 virtual StringRef getStringOffsetDWOSection() = 0;
234 virtual StringRef getRangeDWOSection() = 0;
235 virtual StringRef getAddrSection() = 0;
236 virtual const DWARFSection& getAppleNamesSection() = 0;
237 virtual const DWARFSection& getAppleTypesSection() = 0;
238 virtual const DWARFSection& getAppleNamespacesSection() = 0;
239 virtual const DWARFSection& getAppleObjCSection() = 0;
240 virtual StringRef getCUIndexSection() = 0;
241 virtual StringRef getGdbIndexSection() = 0;
242 virtual StringRef getTUIndexSection() = 0;
244 static bool isSupportedVersion(unsigned version) {
245 return version == 2 || version == 3 || version == 4 || version == 5;
249 /// Return the compile unit that includes an offset (relative to .debug_info).
250 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
252 /// Return the compile unit which contains instruction with provided
254 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
257 /// DWARFContextInMemory is the simplest possible implementation of a
258 /// DWARFContext. It assumes all content is available in memory and stores
260 class DWARFContextInMemory : public DWARFContext {
261 virtual void anchor();
265 DWARFSection InfoSection;
266 TypeSectionMap TypesSections;
267 StringRef AbbrevSection;
268 DWARFSection LocSection;
269 StringRef ARangeSection;
270 StringRef DebugFrameSection;
271 StringRef EHFrameSection;
272 DWARFSection LineSection;
273 StringRef StringSection;
274 StringRef RangeSection;
275 StringRef MacinfoSection;
276 StringRef PubNamesSection;
277 StringRef PubTypesSection;
278 StringRef GnuPubNamesSection;
279 StringRef GnuPubTypesSection;
281 // Sections for DWARF5 split dwarf proposal.
282 DWARFSection InfoDWOSection;
283 TypeSectionMap TypesDWOSections;
284 StringRef AbbrevDWOSection;
285 DWARFSection LineDWOSection;
286 DWARFSection LocDWOSection;
287 StringRef StringDWOSection;
288 StringRef StringOffsetDWOSection;
289 StringRef RangeDWOSection;
290 StringRef AddrSection;
291 DWARFSection AppleNamesSection;
292 DWARFSection AppleTypesSection;
293 DWARFSection AppleNamespacesSection;
294 DWARFSection AppleObjCSection;
295 StringRef CUIndexSection;
296 StringRef GdbIndexSection;
297 StringRef TUIndexSection;
299 SmallVector<SmallString<32>, 4> UncompressedSections;
301 StringRef *MapSectionToMember(StringRef Name);
304 DWARFContextInMemory(const object::ObjectFile &Obj,
305 const LoadedObjectInfo *L = nullptr);
307 DWARFContextInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
309 bool isLittleEndian = sys::IsLittleEndianHost);
311 bool isLittleEndian() const override { return IsLittleEndian; }
312 uint8_t getAddressSize() const override { return AddressSize; }
313 const DWARFSection &getInfoSection() override { return InfoSection; }
314 const TypeSectionMap &getTypesSections() override { return TypesSections; }
315 StringRef getAbbrevSection() override { return AbbrevSection; }
316 const DWARFSection &getLocSection() override { return LocSection; }
317 StringRef getARangeSection() override { return ARangeSection; }
318 StringRef getDebugFrameSection() override { return DebugFrameSection; }
319 StringRef getEHFrameSection() override { return EHFrameSection; }
320 const DWARFSection &getLineSection() override { return LineSection; }
321 StringRef getStringSection() override { return StringSection; }
322 StringRef getRangeSection() override { return RangeSection; }
323 StringRef getMacinfoSection() override { return MacinfoSection; }
324 StringRef getPubNamesSection() override { return PubNamesSection; }
325 StringRef getPubTypesSection() override { return PubTypesSection; }
326 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
327 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
328 const DWARFSection& getAppleNamesSection() override { return AppleNamesSection; }
329 const DWARFSection& getAppleTypesSection() override { return AppleTypesSection; }
330 const DWARFSection& getAppleNamespacesSection() override { return AppleNamespacesSection; }
331 const DWARFSection& getAppleObjCSection() override { return AppleObjCSection; }
333 // Sections for DWARF5 split dwarf proposal.
334 const DWARFSection &getInfoDWOSection() override { return InfoDWOSection; }
336 const TypeSectionMap &getTypesDWOSections() override {
337 return TypesDWOSections;
340 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
341 const DWARFSection &getLineDWOSection() override { return LineDWOSection; }
342 const DWARFSection &getLocDWOSection() override { return LocDWOSection; }
343 StringRef getStringDWOSection() override { return StringDWOSection; }
345 StringRef getStringOffsetDWOSection() override {
346 return StringOffsetDWOSection;
349 StringRef getRangeDWOSection() override { return RangeDWOSection; }
351 StringRef getAddrSection() override {
355 StringRef getCUIndexSection() override { return CUIndexSection; }
356 StringRef getGdbIndexSection() override { return GdbIndexSection; }
357 StringRef getTUIndexSection() override { return TUIndexSection; }
360 } // end namespace llvm
362 #endif // LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H