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 /// Reads a value from data extractor and applies a relocation to the result if
47 /// one exists for the given offset.
48 uint64_t getRelocatedValue(const DataExtractor &Data, uint32_t Size,
49 uint32_t *Off, const RelocAddrMap *Relocs,
50 uint64_t *SecNdx = nullptr);
53 /// This data structure is the top level entity that deals with dwarf debug
54 /// information parsing. The actual data is supplied through pure virtual
55 /// methods that a concrete implementation provides.
56 class DWARFContext : public DIContext {
57 DWARFUnitSection<DWARFCompileUnit> CUs;
58 std::deque<DWARFUnitSection<DWARFTypeUnit>> TUs;
59 std::unique_ptr<DWARFUnitIndex> CUIndex;
60 std::unique_ptr<DWARFGdbIndex> GdbIndex;
61 std::unique_ptr<DWARFUnitIndex> TUIndex;
62 std::unique_ptr<DWARFDebugAbbrev> Abbrev;
63 std::unique_ptr<DWARFDebugLoc> Loc;
64 std::unique_ptr<DWARFDebugAranges> Aranges;
65 std::unique_ptr<DWARFDebugLine> Line;
66 std::unique_ptr<DWARFDebugFrame> DebugFrame;
67 std::unique_ptr<DWARFDebugFrame> EHFrame;
68 std::unique_ptr<DWARFDebugMacro> Macro;
70 DWARFUnitSection<DWARFCompileUnit> DWOCUs;
71 std::deque<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
72 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
73 std::unique_ptr<DWARFDebugLocDWO> LocDWO;
76 object::OwningBinary<object::ObjectFile> File;
77 std::unique_ptr<DWARFContext> Context;
79 StringMap<std::weak_ptr<DWOFile>> DWOFiles;
80 std::weak_ptr<DWOFile> DWP;
81 bool CheckedForDWP = false;
83 /// Read compile units from the debug_info section (if necessary)
84 /// and store them in CUs.
85 void parseCompileUnits();
87 /// Read type units from the debug_types sections (if necessary)
88 /// and store them in TUs.
89 void parseTypeUnits();
91 /// Read compile units from the debug_info.dwo section (if necessary)
92 /// and store them in DWOCUs.
93 void parseDWOCompileUnits();
95 /// Read type units from the debug_types.dwo section (if necessary)
96 /// and store them in DWOTUs.
97 void parseDWOTypeUnits();
100 DWARFContext() : DIContext(CK_DWARF) {}
101 DWARFContext(DWARFContext &) = delete;
102 DWARFContext &operator=(DWARFContext &) = delete;
104 static bool classof(const DIContext *DICtx) {
105 return DICtx->getKind() == CK_DWARF;
108 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All,
109 bool DumpEH = false, bool SummarizeTypes = false) override;
111 bool verify(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
113 typedef DWARFUnitSection<DWARFCompileUnit>::iterator_range cu_iterator_range;
114 typedef DWARFUnitSection<DWARFTypeUnit>::iterator_range tu_iterator_range;
115 typedef iterator_range<decltype(TUs)::iterator> tu_section_iterator_range;
117 /// Get compile units in this context.
118 cu_iterator_range compile_units() {
120 return cu_iterator_range(CUs.begin(), CUs.end());
123 /// Get type units in this context.
124 tu_section_iterator_range type_unit_sections() {
126 return tu_section_iterator_range(TUs.begin(), TUs.end());
129 /// Get compile units in the DWO context.
130 cu_iterator_range dwo_compile_units() {
131 parseDWOCompileUnits();
132 return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
135 /// Get type units in the DWO context.
136 tu_section_iterator_range dwo_type_unit_sections() {
138 return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
141 /// Get the number of compile units in this context.
142 unsigned getNumCompileUnits() {
147 /// Get the number of compile units in this context.
148 unsigned getNumTypeUnits() {
153 /// Get the number of compile units in the DWO context.
154 unsigned getNumDWOCompileUnits() {
155 parseDWOCompileUnits();
156 return DWOCUs.size();
159 /// Get the number of compile units in the DWO context.
160 unsigned getNumDWOTypeUnits() {
162 return DWOTUs.size();
165 /// Get the compile unit at the specified index for this compile unit.
166 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
168 return CUs[index].get();
171 /// Get the compile unit at the specified index for the DWO compile units.
172 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
173 parseDWOCompileUnits();
174 return DWOCUs[index].get();
177 DWARFCompileUnit *getDWOCompileUnitForHash(uint64_t Hash);
179 /// Get a DIE given an exact offset.
180 DWARFDie getDIEForOffset(uint32_t Offset);
182 const DWARFUnitIndex &getCUIndex();
183 DWARFGdbIndex &getGdbIndex();
184 const DWARFUnitIndex &getTUIndex();
186 /// Get a pointer to the parsed DebugAbbrev object.
187 const DWARFDebugAbbrev *getDebugAbbrev();
189 /// Get a pointer to the parsed DebugLoc object.
190 const DWARFDebugLoc *getDebugLoc();
192 /// Get a pointer to the parsed dwo abbreviations object.
193 const DWARFDebugAbbrev *getDebugAbbrevDWO();
195 /// Get a pointer to the parsed DebugLoc object.
196 const DWARFDebugLocDWO *getDebugLocDWO();
198 /// Get a pointer to the parsed DebugAranges object.
199 const DWARFDebugAranges *getDebugAranges();
201 /// Get a pointer to the parsed frame information object.
202 const DWARFDebugFrame *getDebugFrame();
204 /// Get a pointer to the parsed eh frame information object.
205 const DWARFDebugFrame *getEHFrame();
207 /// Get a pointer to the parsed DebugMacro object.
208 const DWARFDebugMacro *getDebugMacro();
210 /// Get a pointer to a parsed line table corresponding to a compile unit.
211 const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
213 DILineInfo getLineInfoForAddress(uint64_t Address,
214 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
215 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
216 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
217 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
218 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
220 virtual StringRef getFileName() const = 0;
221 virtual bool isLittleEndian() const = 0;
222 virtual uint8_t getAddressSize() const = 0;
223 virtual const DWARFSection &getInfoSection() = 0;
224 typedef MapVector<object::SectionRef, DWARFSection,
225 std::map<object::SectionRef, unsigned>> TypeSectionMap;
226 virtual const TypeSectionMap &getTypesSections() = 0;
227 virtual StringRef getAbbrevSection() = 0;
228 virtual const DWARFSection &getLocSection() = 0;
229 virtual StringRef getARangeSection() = 0;
230 virtual StringRef getDebugFrameSection() = 0;
231 virtual StringRef getEHFrameSection() = 0;
232 virtual const DWARFSection &getLineSection() = 0;
233 virtual StringRef getStringSection() = 0;
234 virtual const DWARFSection& getRangeSection() = 0;
235 virtual StringRef getMacinfoSection() = 0;
236 virtual StringRef getPubNamesSection() = 0;
237 virtual StringRef getPubTypesSection() = 0;
238 virtual StringRef getGnuPubNamesSection() = 0;
239 virtual StringRef getGnuPubTypesSection() = 0;
241 // Sections for DWARF5 split dwarf proposal.
242 virtual const DWARFSection &getInfoDWOSection() = 0;
243 virtual const TypeSectionMap &getTypesDWOSections() = 0;
244 virtual StringRef getAbbrevDWOSection() = 0;
245 virtual const DWARFSection &getLineDWOSection() = 0;
246 virtual const DWARFSection &getLocDWOSection() = 0;
247 virtual StringRef getStringDWOSection() = 0;
248 virtual StringRef getStringOffsetDWOSection() = 0;
249 virtual const DWARFSection &getRangeDWOSection() = 0;
250 virtual const DWARFSection &getAddrSection() = 0;
251 virtual const DWARFSection& getAppleNamesSection() = 0;
252 virtual const DWARFSection& getAppleTypesSection() = 0;
253 virtual const DWARFSection& getAppleNamespacesSection() = 0;
254 virtual const DWARFSection& getAppleObjCSection() = 0;
255 virtual StringRef getCUIndexSection() = 0;
256 virtual StringRef getGdbIndexSection() = 0;
257 virtual StringRef getTUIndexSection() = 0;
259 static bool isSupportedVersion(unsigned version) {
260 return version == 2 || version == 3 || version == 4 || version == 5;
263 std::shared_ptr<DWARFContext> getDWOContext(StringRef AbsolutePath);
266 /// Return the compile unit that includes an offset (relative to .debug_info).
267 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
269 /// Return the compile unit which contains instruction with provided
271 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
274 /// DWARFContextInMemory is the simplest possible implementation of a
275 /// DWARFContext. It assumes all content is available in memory and stores
277 class DWARFContextInMemory : public DWARFContext {
278 virtual void anchor();
283 DWARFSection InfoSection;
284 TypeSectionMap TypesSections;
285 StringRef AbbrevSection;
286 DWARFSection LocSection;
287 StringRef ARangeSection;
288 StringRef DebugFrameSection;
289 StringRef EHFrameSection;
290 DWARFSection LineSection;
291 StringRef StringSection;
292 DWARFSection RangeSection;
293 StringRef MacinfoSection;
294 StringRef PubNamesSection;
295 StringRef PubTypesSection;
296 StringRef GnuPubNamesSection;
297 StringRef GnuPubTypesSection;
299 // Sections for DWARF5 split dwarf proposal.
300 DWARFSection InfoDWOSection;
301 TypeSectionMap TypesDWOSections;
302 StringRef AbbrevDWOSection;
303 DWARFSection LineDWOSection;
304 DWARFSection LocDWOSection;
305 StringRef StringDWOSection;
306 StringRef StringOffsetDWOSection;
307 DWARFSection RangeDWOSection;
308 DWARFSection AddrSection;
309 DWARFSection AppleNamesSection;
310 DWARFSection AppleTypesSection;
311 DWARFSection AppleNamespacesSection;
312 DWARFSection AppleObjCSection;
313 StringRef CUIndexSection;
314 StringRef GdbIndexSection;
315 StringRef TUIndexSection;
317 SmallVector<SmallString<32>, 4> UncompressedSections;
319 StringRef *MapSectionToMember(StringRef Name);
321 /// If Sec is compressed section, decompresses and updates its contents
322 /// provided by Data. Otherwise leaves it unchanged.
323 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
327 DWARFContextInMemory(const object::ObjectFile &Obj,
328 const LoadedObjectInfo *L = nullptr);
330 DWARFContextInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
332 bool isLittleEndian = sys::IsLittleEndianHost);
334 StringRef getFileName() const override { return FileName; }
335 bool isLittleEndian() const override { return IsLittleEndian; }
336 uint8_t getAddressSize() const override { return AddressSize; }
337 const DWARFSection &getInfoSection() override { return InfoSection; }
338 const TypeSectionMap &getTypesSections() override { return TypesSections; }
339 StringRef getAbbrevSection() override { return AbbrevSection; }
340 const DWARFSection &getLocSection() override { return LocSection; }
341 StringRef getARangeSection() override { return ARangeSection; }
342 StringRef getDebugFrameSection() override { return DebugFrameSection; }
343 StringRef getEHFrameSection() override { return EHFrameSection; }
344 const DWARFSection &getLineSection() override { return LineSection; }
345 StringRef getStringSection() override { return StringSection; }
346 const DWARFSection &getRangeSection() override { return RangeSection; }
347 StringRef getMacinfoSection() override { return MacinfoSection; }
348 StringRef getPubNamesSection() override { return PubNamesSection; }
349 StringRef getPubTypesSection() override { return PubTypesSection; }
350 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
351 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
352 const DWARFSection& getAppleNamesSection() override { return AppleNamesSection; }
353 const DWARFSection& getAppleTypesSection() override { return AppleTypesSection; }
354 const DWARFSection& getAppleNamespacesSection() override { return AppleNamespacesSection; }
355 const DWARFSection& getAppleObjCSection() override { return AppleObjCSection; }
357 // Sections for DWARF5 split dwarf proposal.
358 const DWARFSection &getInfoDWOSection() override { return InfoDWOSection; }
360 const TypeSectionMap &getTypesDWOSections() override {
361 return TypesDWOSections;
364 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
365 const DWARFSection &getLineDWOSection() override { return LineDWOSection; }
366 const DWARFSection &getLocDWOSection() override { return LocDWOSection; }
367 StringRef getStringDWOSection() override { return StringDWOSection; }
369 StringRef getStringOffsetDWOSection() override {
370 return StringOffsetDWOSection;
373 const DWARFSection &getRangeDWOSection() override { return RangeDWOSection; }
375 const DWARFSection &getAddrSection() override { return AddrSection; }
377 StringRef getCUIndexSection() override { return CUIndexSection; }
378 StringRef getGdbIndexSection() override { return GdbIndexSection; }
379 StringRef getTUIndexSection() override { return TUIndexSection; }
382 } // end namespace llvm
384 #endif // LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H