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);
52 /// This data structure is the top level entity that deals with dwarf debug
53 /// information parsing. The actual data is supplied through pure virtual
54 /// methods that a concrete implementation provides.
55 class DWARFContext : public DIContext {
56 DWARFUnitSection<DWARFCompileUnit> CUs;
57 std::deque<DWARFUnitSection<DWARFTypeUnit>> TUs;
58 std::unique_ptr<DWARFUnitIndex> CUIndex;
59 std::unique_ptr<DWARFGdbIndex> GdbIndex;
60 std::unique_ptr<DWARFUnitIndex> TUIndex;
61 std::unique_ptr<DWARFDebugAbbrev> Abbrev;
62 std::unique_ptr<DWARFDebugLoc> Loc;
63 std::unique_ptr<DWARFDebugAranges> Aranges;
64 std::unique_ptr<DWARFDebugLine> Line;
65 std::unique_ptr<DWARFDebugFrame> DebugFrame;
66 std::unique_ptr<DWARFDebugFrame> EHFrame;
67 std::unique_ptr<DWARFDebugMacro> Macro;
69 DWARFUnitSection<DWARFCompileUnit> DWOCUs;
70 std::deque<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
71 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
72 std::unique_ptr<DWARFDebugLocDWO> LocDWO;
74 /// Read compile units from the debug_info section (if necessary)
75 /// and store them in CUs.
76 void parseCompileUnits();
78 /// Read type units from the debug_types sections (if necessary)
79 /// and store them in TUs.
80 void parseTypeUnits();
82 /// Read compile units from the debug_info.dwo section (if necessary)
83 /// and store them in DWOCUs.
84 void parseDWOCompileUnits();
86 /// Read type units from the debug_types.dwo section (if necessary)
87 /// and store them in DWOTUs.
88 void parseDWOTypeUnits();
91 DWARFContext() : DIContext(CK_DWARF) {}
92 DWARFContext(DWARFContext &) = delete;
93 DWARFContext &operator=(DWARFContext &) = delete;
95 static bool classof(const DIContext *DICtx) {
96 return DICtx->getKind() == CK_DWARF;
99 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All,
100 bool DumpEH = false, bool SummarizeTypes = false) override;
102 bool verify(raw_ostream &OS, DIDumpType DumpType = DIDT_All) 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 /// Get a DIE given an exact offset.
169 DWARFDie getDIEForOffset(uint32_t Offset);
171 const DWARFUnitIndex &getCUIndex();
172 DWARFGdbIndex &getGdbIndex();
173 const DWARFUnitIndex &getTUIndex();
175 /// Get a pointer to the parsed DebugAbbrev object.
176 const DWARFDebugAbbrev *getDebugAbbrev();
178 /// Get a pointer to the parsed DebugLoc object.
179 const DWARFDebugLoc *getDebugLoc();
181 /// Get a pointer to the parsed dwo abbreviations object.
182 const DWARFDebugAbbrev *getDebugAbbrevDWO();
184 /// Get a pointer to the parsed DebugLoc object.
185 const DWARFDebugLocDWO *getDebugLocDWO();
187 /// Get a pointer to the parsed DebugAranges object.
188 const DWARFDebugAranges *getDebugAranges();
190 /// Get a pointer to the parsed frame information object.
191 const DWARFDebugFrame *getDebugFrame();
193 /// Get a pointer to the parsed eh frame information object.
194 const DWARFDebugFrame *getEHFrame();
196 /// Get a pointer to the parsed DebugMacro object.
197 const DWARFDebugMacro *getDebugMacro();
199 /// Get a pointer to a parsed line table corresponding to a compile unit.
200 const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
202 DILineInfo getLineInfoForAddress(uint64_t Address,
203 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
204 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
205 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
206 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
207 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
209 virtual bool isLittleEndian() const = 0;
210 virtual uint8_t getAddressSize() const = 0;
211 virtual const DWARFSection &getInfoSection() = 0;
212 typedef MapVector<object::SectionRef, DWARFSection,
213 std::map<object::SectionRef, unsigned>> TypeSectionMap;
214 virtual const TypeSectionMap &getTypesSections() = 0;
215 virtual StringRef getAbbrevSection() = 0;
216 virtual const DWARFSection &getLocSection() = 0;
217 virtual StringRef getARangeSection() = 0;
218 virtual StringRef getDebugFrameSection() = 0;
219 virtual StringRef getEHFrameSection() = 0;
220 virtual const DWARFSection &getLineSection() = 0;
221 virtual StringRef getStringSection() = 0;
222 virtual const DWARFSection& getRangeSection() = 0;
223 virtual StringRef getMacinfoSection() = 0;
224 virtual StringRef getPubNamesSection() = 0;
225 virtual StringRef getPubTypesSection() = 0;
226 virtual StringRef getGnuPubNamesSection() = 0;
227 virtual StringRef getGnuPubTypesSection() = 0;
229 // Sections for DWARF5 split dwarf proposal.
230 virtual const DWARFSection &getInfoDWOSection() = 0;
231 virtual const TypeSectionMap &getTypesDWOSections() = 0;
232 virtual StringRef getAbbrevDWOSection() = 0;
233 virtual const DWARFSection &getLineDWOSection() = 0;
234 virtual const DWARFSection &getLocDWOSection() = 0;
235 virtual StringRef getStringDWOSection() = 0;
236 virtual StringRef getStringOffsetDWOSection() = 0;
237 virtual const DWARFSection &getRangeDWOSection() = 0;
238 virtual StringRef getAddrSection() = 0;
239 virtual const DWARFSection& getAppleNamesSection() = 0;
240 virtual const DWARFSection& getAppleTypesSection() = 0;
241 virtual const DWARFSection& getAppleNamespacesSection() = 0;
242 virtual const DWARFSection& getAppleObjCSection() = 0;
243 virtual StringRef getCUIndexSection() = 0;
244 virtual StringRef getGdbIndexSection() = 0;
245 virtual StringRef getTUIndexSection() = 0;
247 static bool isSupportedVersion(unsigned version) {
248 return version == 2 || version == 3 || version == 4 || version == 5;
252 /// Return the compile unit that includes an offset (relative to .debug_info).
253 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
255 /// Return the compile unit which contains instruction with provided
257 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
260 /// DWARFContextInMemory is the simplest possible implementation of a
261 /// DWARFContext. It assumes all content is available in memory and stores
263 class DWARFContextInMemory : public DWARFContext {
264 virtual void anchor();
268 DWARFSection InfoSection;
269 TypeSectionMap TypesSections;
270 StringRef AbbrevSection;
271 DWARFSection LocSection;
272 StringRef ARangeSection;
273 StringRef DebugFrameSection;
274 StringRef EHFrameSection;
275 DWARFSection LineSection;
276 StringRef StringSection;
277 DWARFSection RangeSection;
278 StringRef MacinfoSection;
279 StringRef PubNamesSection;
280 StringRef PubTypesSection;
281 StringRef GnuPubNamesSection;
282 StringRef GnuPubTypesSection;
284 // Sections for DWARF5 split dwarf proposal.
285 DWARFSection InfoDWOSection;
286 TypeSectionMap TypesDWOSections;
287 StringRef AbbrevDWOSection;
288 DWARFSection LineDWOSection;
289 DWARFSection LocDWOSection;
290 StringRef StringDWOSection;
291 StringRef StringOffsetDWOSection;
292 DWARFSection RangeDWOSection;
293 StringRef AddrSection;
294 DWARFSection AppleNamesSection;
295 DWARFSection AppleTypesSection;
296 DWARFSection AppleNamespacesSection;
297 DWARFSection AppleObjCSection;
298 StringRef CUIndexSection;
299 StringRef GdbIndexSection;
300 StringRef TUIndexSection;
302 SmallVector<SmallString<32>, 4> UncompressedSections;
304 StringRef *MapSectionToMember(StringRef Name);
306 /// If Sec is compressed section, decompresses and updates its contents
307 /// provided by Data. Otherwise leaves it unchanged.
308 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
312 DWARFContextInMemory(const object::ObjectFile &Obj,
313 const LoadedObjectInfo *L = nullptr);
315 DWARFContextInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
317 bool isLittleEndian = sys::IsLittleEndianHost);
319 bool isLittleEndian() const override { return IsLittleEndian; }
320 uint8_t getAddressSize() const override { return AddressSize; }
321 const DWARFSection &getInfoSection() override { return InfoSection; }
322 const TypeSectionMap &getTypesSections() override { return TypesSections; }
323 StringRef getAbbrevSection() override { return AbbrevSection; }
324 const DWARFSection &getLocSection() override { return LocSection; }
325 StringRef getARangeSection() override { return ARangeSection; }
326 StringRef getDebugFrameSection() override { return DebugFrameSection; }
327 StringRef getEHFrameSection() override { return EHFrameSection; }
328 const DWARFSection &getLineSection() override { return LineSection; }
329 StringRef getStringSection() override { return StringSection; }
330 const DWARFSection &getRangeSection() override { return RangeSection; }
331 StringRef getMacinfoSection() override { return MacinfoSection; }
332 StringRef getPubNamesSection() override { return PubNamesSection; }
333 StringRef getPubTypesSection() override { return PubTypesSection; }
334 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
335 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
336 const DWARFSection& getAppleNamesSection() override { return AppleNamesSection; }
337 const DWARFSection& getAppleTypesSection() override { return AppleTypesSection; }
338 const DWARFSection& getAppleNamespacesSection() override { return AppleNamespacesSection; }
339 const DWARFSection& getAppleObjCSection() override { return AppleObjCSection; }
341 // Sections for DWARF5 split dwarf proposal.
342 const DWARFSection &getInfoDWOSection() override { return InfoDWOSection; }
344 const TypeSectionMap &getTypesDWOSections() override {
345 return TypesDWOSections;
348 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
349 const DWARFSection &getLineDWOSection() override { return LineDWOSection; }
350 const DWARFSection &getLocDWOSection() override { return LocDWOSection; }
351 StringRef getStringDWOSection() override { return StringDWOSection; }
353 StringRef getStringOffsetDWOSection() override {
354 return StringOffsetDWOSection;
357 const DWARFSection &getRangeDWOSection() override { return RangeDWOSection; }
359 StringRef getAddrSection() override {
363 StringRef getCUIndexSection() override { return CUIndexSection; }
364 StringRef getGdbIndexSection() override { return GdbIndexSection; }
365 StringRef getTUIndexSection() override { return TUIndexSection; }
368 } // end namespace llvm
370 #endif // LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H