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/DWARFDebugRangeList.h"
22 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
23 #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
28 // In place of applying the relocations to the data we've read from disk we use
29 // a separate mapping table to the side and checking that at locations in the
30 // dwarf where we expect relocated values. This adds a bit of complexity to the
31 // dwarf parsing/extraction at the benefit of not allocating memory for the
32 // entire size of the debug info sections.
33 typedef DenseMap<uint64_t, std::pair<uint8_t, int64_t> > RelocAddrMap;
36 /// This data structure is the top level entity that deals with dwarf debug
37 /// information parsing. The actual data is supplied through pure virtual
38 /// methods that a concrete implementation provides.
39 class DWARFContext : public DIContext {
41 DWARFUnitSection<DWARFCompileUnit> CUs;
42 std::vector<DWARFUnitSection<DWARFTypeUnit>> TUs;
43 std::unique_ptr<DWARFDebugAbbrev> Abbrev;
44 std::unique_ptr<DWARFDebugLoc> Loc;
45 std::unique_ptr<DWARFDebugAranges> Aranges;
46 std::unique_ptr<DWARFDebugLine> Line;
47 std::unique_ptr<DWARFDebugFrame> DebugFrame;
49 DWARFUnitSection<DWARFCompileUnit> DWOCUs;
50 std::vector<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
51 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
52 std::unique_ptr<DWARFDebugLocDWO> LocDWO;
54 DWARFContext(DWARFContext &) = delete;
55 DWARFContext &operator=(DWARFContext &) = delete;
57 /// Read compile units from the debug_info section (if necessary)
58 /// and store them in CUs.
59 void parseCompileUnits();
61 /// Read type units from the debug_types sections (if necessary)
62 /// and store them in TUs.
63 void parseTypeUnits();
65 /// Read compile units from the debug_info.dwo section (if necessary)
66 /// and store them in DWOCUs.
67 void parseDWOCompileUnits();
69 /// Read type units from the debug_types.dwo section (if necessary)
70 /// and store them in DWOTUs.
71 void parseDWOTypeUnits();
74 DWARFContext() : DIContext(CK_DWARF) {}
76 static bool classof(const DIContext *DICtx) {
77 return DICtx->getKind() == CK_DWARF;
80 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
82 typedef DWARFUnitSection<DWARFCompileUnit>::iterator_range cu_iterator_range;
83 typedef DWARFUnitSection<DWARFTypeUnit>::iterator_range tu_iterator_range;
84 typedef iterator_range<std::vector<DWARFUnitSection<DWARFTypeUnit>>::iterator> tu_section_iterator_range;
86 /// Get compile units in this context.
87 cu_iterator_range compile_units() {
89 return cu_iterator_range(CUs.begin(), CUs.end());
92 /// Get type units in this context.
93 tu_section_iterator_range type_unit_sections() {
95 return tu_section_iterator_range(TUs.begin(), TUs.end());
98 /// Get compile units in the DWO context.
99 cu_iterator_range dwo_compile_units() {
100 parseDWOCompileUnits();
101 return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
104 /// Get type units in the DWO context.
105 tu_section_iterator_range dwo_type_unit_sections() {
107 return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
110 /// Get the number of compile units in this context.
111 unsigned getNumCompileUnits() {
116 /// Get the number of compile units in this context.
117 unsigned getNumTypeUnits() {
122 /// Get the number of compile units in the DWO context.
123 unsigned getNumDWOCompileUnits() {
124 parseDWOCompileUnits();
125 return DWOCUs.size();
128 /// Get the number of compile units in the DWO context.
129 unsigned getNumDWOTypeUnits() {
131 return DWOTUs.size();
134 /// Get the compile unit at the specified index for this compile unit.
135 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
137 return CUs[index].get();
140 /// Get the compile unit at the specified index for the DWO compile units.
141 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
142 parseDWOCompileUnits();
143 return DWOCUs[index].get();
146 /// Get a pointer to the parsed DebugAbbrev object.
147 const DWARFDebugAbbrev *getDebugAbbrev();
149 /// Get a pointer to the parsed DebugLoc object.
150 const DWARFDebugLoc *getDebugLoc();
152 /// Get a pointer to the parsed dwo abbreviations object.
153 const DWARFDebugAbbrev *getDebugAbbrevDWO();
155 /// Get a pointer to the parsed DebugLoc object.
156 const DWARFDebugLocDWO *getDebugLocDWO();
158 /// Get a pointer to the parsed DebugAranges object.
159 const DWARFDebugAranges *getDebugAranges();
161 /// Get a pointer to the parsed frame information object.
162 const DWARFDebugFrame *getDebugFrame();
164 /// Get a pointer to a parsed line table corresponding to a compile unit.
165 const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
167 DILineInfo getLineInfoForAddress(uint64_t Address,
168 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
169 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
170 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
171 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
172 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
174 virtual bool isLittleEndian() const = 0;
175 virtual uint8_t getAddressSize() const = 0;
176 virtual const DWARFSection &getInfoSection() = 0;
177 typedef MapVector<object::SectionRef, DWARFSection,
178 std::map<object::SectionRef, unsigned>> TypeSectionMap;
179 virtual const TypeSectionMap &getTypesSections() = 0;
180 virtual StringRef getAbbrevSection() = 0;
181 virtual const DWARFSection &getLocSection() = 0;
182 virtual StringRef getARangeSection() = 0;
183 virtual StringRef getDebugFrameSection() = 0;
184 virtual const DWARFSection &getLineSection() = 0;
185 virtual StringRef getStringSection() = 0;
186 virtual StringRef getRangeSection() = 0;
187 virtual StringRef getPubNamesSection() = 0;
188 virtual StringRef getPubTypesSection() = 0;
189 virtual StringRef getGnuPubNamesSection() = 0;
190 virtual StringRef getGnuPubTypesSection() = 0;
192 // Sections for DWARF5 split dwarf proposal.
193 virtual const DWARFSection &getInfoDWOSection() = 0;
194 virtual const TypeSectionMap &getTypesDWOSections() = 0;
195 virtual StringRef getAbbrevDWOSection() = 0;
196 virtual const DWARFSection &getLineDWOSection() = 0;
197 virtual const DWARFSection &getLocDWOSection() = 0;
198 virtual StringRef getStringDWOSection() = 0;
199 virtual StringRef getStringOffsetDWOSection() = 0;
200 virtual StringRef getRangeDWOSection() = 0;
201 virtual StringRef getAddrSection() = 0;
202 virtual const DWARFSection& getAppleNamesSection() = 0;
203 virtual const DWARFSection& getAppleTypesSection() = 0;
204 virtual const DWARFSection& getAppleNamespacesSection() = 0;
205 virtual const DWARFSection& getAppleObjCSection() = 0;
207 static bool isSupportedVersion(unsigned version) {
208 return version == 2 || version == 3 || version == 4;
211 /// Return the compile unit that includes an offset (relative to .debug_info).
212 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
214 /// Return the compile unit which contains instruction with provided
216 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
219 /// DWARFContextInMemory is the simplest possible implementation of a
220 /// DWARFContext. It assumes all content is available in memory and stores
222 class DWARFContextInMemory : public DWARFContext {
223 virtual void anchor();
226 DWARFSection InfoSection;
227 TypeSectionMap TypesSections;
228 StringRef AbbrevSection;
229 DWARFSection LocSection;
230 StringRef ARangeSection;
231 StringRef DebugFrameSection;
232 DWARFSection LineSection;
233 StringRef StringSection;
234 StringRef RangeSection;
235 StringRef PubNamesSection;
236 StringRef PubTypesSection;
237 StringRef GnuPubNamesSection;
238 StringRef GnuPubTypesSection;
240 // Sections for DWARF5 split dwarf proposal.
241 DWARFSection InfoDWOSection;
242 TypeSectionMap TypesDWOSections;
243 StringRef AbbrevDWOSection;
244 DWARFSection LineDWOSection;
245 DWARFSection LocDWOSection;
246 StringRef StringDWOSection;
247 StringRef StringOffsetDWOSection;
248 StringRef RangeDWOSection;
249 StringRef AddrSection;
250 DWARFSection AppleNamesSection;
251 DWARFSection AppleTypesSection;
252 DWARFSection AppleNamespacesSection;
253 DWARFSection AppleObjCSection;
255 SmallVector<SmallString<32>, 4> UncompressedSections;
258 DWARFContextInMemory(const object::ObjectFile &Obj,
259 const LoadedObjectInfo *L = nullptr);
260 bool isLittleEndian() const override { return IsLittleEndian; }
261 uint8_t getAddressSize() const override { return AddressSize; }
262 const DWARFSection &getInfoSection() override { return InfoSection; }
263 const TypeSectionMap &getTypesSections() override { return TypesSections; }
264 StringRef getAbbrevSection() override { return AbbrevSection; }
265 const DWARFSection &getLocSection() override { return LocSection; }
266 StringRef getARangeSection() override { return ARangeSection; }
267 StringRef getDebugFrameSection() override { return DebugFrameSection; }
268 const DWARFSection &getLineSection() override { return LineSection; }
269 StringRef getStringSection() override { return StringSection; }
270 StringRef getRangeSection() override { return RangeSection; }
271 StringRef getPubNamesSection() override { return PubNamesSection; }
272 StringRef getPubTypesSection() override { return PubTypesSection; }
273 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
274 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
275 const DWARFSection& getAppleNamesSection() override { return AppleNamesSection; }
276 const DWARFSection& getAppleTypesSection() override { return AppleTypesSection; }
277 const DWARFSection& getAppleNamespacesSection() override { return AppleNamespacesSection; }
278 const DWARFSection& getAppleObjCSection() override { return AppleObjCSection; }
280 // Sections for DWARF5 split dwarf proposal.
281 const DWARFSection &getInfoDWOSection() override { return InfoDWOSection; }
282 const TypeSectionMap &getTypesDWOSections() override {
283 return TypesDWOSections;
285 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
286 const DWARFSection &getLineDWOSection() override { return LineDWOSection; }
287 const DWARFSection &getLocDWOSection() override { return LocDWOSection; }
288 StringRef getStringDWOSection() override { return StringDWOSection; }
289 StringRef getStringOffsetDWOSection() override {
290 return StringOffsetDWOSection;
292 StringRef getRangeDWOSection() override { return RangeDWOSection; }
293 StringRef getAddrSection() override {