blob: ad6841ae9e31c54458ffde66df7b0f441d355401 [file] [log] [blame]
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00001//===-- DWARFContext.h ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===/
9
10#ifndef LLVM_DEBUGINFO_DWARFCONTEXT_H
11#define LLVM_DEBUGINFO_DWARFCONTEXT_H
12
13#include "DWARFCompileUnit.h"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000014#include "DWARFDebugAranges.h"
Eli Benderskyfd08bc12013-02-05 23:30:58 +000015#include "DWARFDebugFrame.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000016#include "DWARFDebugLine.h"
David Blaikie18e73502013-06-19 21:37:13 +000017#include "DWARFDebugLoc.h"
Alexey Samsonov034e57a2012-08-27 07:17:47 +000018#include "DWARFDebugRangeList.h"
David Blaikie03c089c2013-09-23 22:44:47 +000019#include "DWARFTypeUnit.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/ADT/MapVector.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000021#include "llvm/ADT/SmallVector.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000022#include "llvm/DebugInfo/DIContext.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000023
24namespace llvm {
25
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000026/// DWARFContext
27/// This data structure is the top level entity that deals with dwarf debug
28/// information parsing. The actual data is supplied through pure virtual
29/// methods that a concrete implementation provides.
30class DWARFContext : public DIContext {
Alexey Samsonov96dc29c2014-03-13 08:19:59 +000031 typedef SmallVector<std::unique_ptr<DWARFCompileUnit>, 1> CUVector;
32 typedef SmallVector<std::unique_ptr<DWARFTypeUnit>, 1> TUVector;
Alexey Samsonov1eabf982014-03-13 07:52:54 +000033
34 CUVector CUs;
35 TUVector TUs;
Ahmed Charles56440fd2014-03-06 05:51:42 +000036 std::unique_ptr<DWARFDebugAbbrev> Abbrev;
37 std::unique_ptr<DWARFDebugLoc> Loc;
38 std::unique_ptr<DWARFDebugAranges> Aranges;
39 std::unique_ptr<DWARFDebugLine> Line;
40 std::unique_ptr<DWARFDebugFrame> DebugFrame;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000041
Alexey Samsonov1eabf982014-03-13 07:52:54 +000042 CUVector DWOCUs;
43 TUVector DWOTUs;
Ahmed Charles56440fd2014-03-06 05:51:42 +000044 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
David Blaikie9c550ac2014-03-25 01:44:02 +000045 std::unique_ptr<DWARFDebugLocDWO> LocDWO;
Eric Christopherda4b2192013-01-02 23:52:13 +000046
Craig Topperb1d83e82012-09-18 02:01:41 +000047 DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
48 DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000049
Alexey Samsonov1eabf982014-03-13 07:52:54 +000050 /// Read compile units from the debug_info section (if necessary)
51 /// and store them in CUs.
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000052 void parseCompileUnits();
Eric Christopher7370b552012-11-12 21:40:38 +000053
Alexey Samsonov1eabf982014-03-13 07:52:54 +000054 /// Read type units from the debug_types sections (if necessary)
55 /// and store them in TUs.
David Blaikie03c089c2013-09-23 22:44:47 +000056 void parseTypeUnits();
57
Alexey Samsonov1eabf982014-03-13 07:52:54 +000058 /// Read compile units from the debug_info.dwo section (if necessary)
59 /// and store them in DWOCUs.
Eric Christopherda4b2192013-01-02 23:52:13 +000060 void parseDWOCompileUnits();
61
Alexey Samsonov1eabf982014-03-13 07:52:54 +000062 /// Read type units from the debug_types.dwo section (if necessary)
63 /// and store them in DWOTUs.
David Blaikie92d9d622014-01-09 05:08:24 +000064 void parseDWOTypeUnits();
65
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000066public:
David Blaikie1b5ee5d2013-09-23 17:42:01 +000067 struct Section {
68 StringRef Data;
69 RelocAddrMap Relocs;
70 };
71
Alexey Samsonovc2e00872013-08-06 10:32:39 +000072 DWARFContext() : DIContext(CK_DWARF) {}
73
74 static bool classof(const DIContext *DICtx) {
75 return DICtx->getKind() == CK_DWARF;
76 }
77
Craig Topper85482992014-03-05 07:52:44 +000078 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
Eric Christopher7c678de2012-11-07 23:22:07 +000079
Alexey Samsonov1eabf982014-03-13 07:52:54 +000080 typedef iterator_range<CUVector::iterator> cu_iterator_range;
81 typedef iterator_range<TUVector::iterator> tu_iterator_range;
82
83 /// Get compile units in this context.
84 cu_iterator_range compile_units() {
85 parseCompileUnits();
86 return cu_iterator_range(CUs.begin(), CUs.end());
87 }
88
89 /// Get type units in this context.
90 tu_iterator_range type_units() {
91 parseTypeUnits();
92 return tu_iterator_range(TUs.begin(), TUs.end());
93 }
94
95 /// Get compile units in the DWO context.
96 cu_iterator_range dwo_compile_units() {
97 parseDWOCompileUnits();
98 return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
99 }
100
101 /// Get type units in the DWO context.
102 tu_iterator_range dwo_type_units() {
103 parseDWOTypeUnits();
104 return tu_iterator_range(DWOTUs.begin(), DWOTUs.end());
105 }
106
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000107 /// Get the number of compile units in this context.
108 unsigned getNumCompileUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000109 parseCompileUnits();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000110 return CUs.size();
111 }
Eric Christopherda4b2192013-01-02 23:52:13 +0000112
David Blaikie03c089c2013-09-23 22:44:47 +0000113 /// Get the number of compile units in this context.
114 unsigned getNumTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000115 parseTypeUnits();
David Blaikie03c089c2013-09-23 22:44:47 +0000116 return TUs.size();
117 }
118
Eric Christopherda4b2192013-01-02 23:52:13 +0000119 /// Get the number of compile units in the DWO context.
120 unsigned getNumDWOCompileUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000121 parseDWOCompileUnits();
Eric Christopherda4b2192013-01-02 23:52:13 +0000122 return DWOCUs.size();
123 }
124
David Blaikie92d9d622014-01-09 05:08:24 +0000125 /// Get the number of compile units in the DWO context.
126 unsigned getNumDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000127 parseDWOTypeUnits();
David Blaikie92d9d622014-01-09 05:08:24 +0000128 return DWOTUs.size();
129 }
130
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000131 /// Get the compile unit at the specified index for this compile unit.
132 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000133 parseCompileUnits();
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000134 return CUs[index].get();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000135 }
136
Eric Christopherda4b2192013-01-02 23:52:13 +0000137 /// Get the compile unit at the specified index for the DWO compile units.
138 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000139 parseDWOCompileUnits();
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000140 return DWOCUs[index].get();
Eric Christopherda4b2192013-01-02 23:52:13 +0000141 }
142
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000143 /// Get a pointer to the parsed DebugAbbrev object.
144 const DWARFDebugAbbrev *getDebugAbbrev();
145
David Blaikie18e73502013-06-19 21:37:13 +0000146 /// Get a pointer to the parsed DebugLoc object.
147 const DWARFDebugLoc *getDebugLoc();
148
Eric Christopherda4b2192013-01-02 23:52:13 +0000149 /// Get a pointer to the parsed dwo abbreviations object.
150 const DWARFDebugAbbrev *getDebugAbbrevDWO();
151
David Blaikie9c550ac2014-03-25 01:44:02 +0000152 /// Get a pointer to the parsed DebugLoc object.
153 const DWARFDebugLocDWO *getDebugLocDWO();
154
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000155 /// Get a pointer to the parsed DebugAranges object.
156 const DWARFDebugAranges *getDebugAranges();
157
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000158 /// Get a pointer to the parsed frame information object.
159 const DWARFDebugFrame *getDebugFrame();
160
Benjamin Kramer679e1752011-09-15 20:43:18 +0000161 /// Get a pointer to a parsed line table corresponding to a compile unit.
162 const DWARFDebugLine::LineTable *
163 getLineTableForCompileUnit(DWARFCompileUnit *cu);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000164
Craig Topper85482992014-03-05 07:52:44 +0000165 DILineInfo getLineInfoForAddress(uint64_t Address,
166 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
167 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
168 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
169 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
170 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000171
Eric Christopher7370b552012-11-12 21:40:38 +0000172 virtual bool isLittleEndian() const = 0;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000173 virtual uint8_t getAddressSize() const = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000174 virtual const Section &getInfoSection() = 0;
David Blaikiebc563272013-12-13 21:33:40 +0000175 typedef MapVector<object::SectionRef, Section,
176 std::map<object::SectionRef, unsigned> > TypeSectionMap;
177 virtual const TypeSectionMap &getTypesSections() = 0;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000178 virtual StringRef getAbbrevSection() = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000179 virtual const Section &getLocSection() = 0;
David Blaikie9c550ac2014-03-25 01:44:02 +0000180 virtual const Section &getLocDWOSection() = 0;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000181 virtual StringRef getARangeSection() = 0;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000182 virtual StringRef getDebugFrameSection() = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000183 virtual const Section &getLineSection() = 0;
David Blaikie1d4736e2014-02-24 23:58:54 +0000184 virtual const Section &getLineDWOSection() = 0;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000185 virtual StringRef getStringSection() = 0;
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000186 virtual StringRef getRangeSection() = 0;
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000187 virtual StringRef getPubNamesSection() = 0;
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000188 virtual StringRef getPubTypesSection() = 0;
David Blaikie404d3042013-09-19 23:01:29 +0000189 virtual StringRef getGnuPubNamesSection() = 0;
David Blaikieecd21ff2013-09-24 19:50:00 +0000190 virtual StringRef getGnuPubTypesSection() = 0;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000191
Eric Christopherda4b2192013-01-02 23:52:13 +0000192 // Sections for DWARF5 split dwarf proposal.
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000193 virtual const Section &getInfoDWOSection() = 0;
David Blaikie92d9d622014-01-09 05:08:24 +0000194 virtual const TypeSectionMap &getTypesDWOSections() = 0;
Eric Christopherda4b2192013-01-02 23:52:13 +0000195 virtual StringRef getAbbrevDWOSection() = 0;
196 virtual StringRef getStringDWOSection() = 0;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000197 virtual StringRef getStringOffsetDWOSection() = 0;
Eric Christopherda4b2192013-01-02 23:52:13 +0000198 virtual StringRef getRangeDWOSection() = 0;
Eric Christopher962c9082013-01-15 23:56:56 +0000199 virtual StringRef getAddrSection() = 0;
Eric Christopherda4b2192013-01-02 23:52:13 +0000200
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000201 static bool isSupportedVersion(unsigned version) {
Eric Christopherf7d848d2013-08-06 01:38:27 +0000202 return version == 2 || version == 3 || version == 4;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000203 }
Alexey Samsonov45be7932012-08-30 07:49:50 +0000204private:
205 /// Return the compile unit that includes an offset (relative to .debug_info).
206 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000207
Alexey Samsonov45be7932012-08-30 07:49:50 +0000208 /// Return the compile unit which contains instruction with provided
209 /// address.
210 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000211};
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000212
213/// DWARFContextInMemory is the simplest possible implementation of a
214/// DWARFContext. It assumes all content is available in memory and stores
215/// pointers to it.
216class DWARFContextInMemory : public DWARFContext {
David Blaikiea379b1812011-12-20 02:50:00 +0000217 virtual void anchor();
Eric Christopher7370b552012-11-12 21:40:38 +0000218 bool IsLittleEndian;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000219 uint8_t AddressSize;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000220 Section InfoSection;
David Blaikiebc563272013-12-13 21:33:40 +0000221 TypeSectionMap TypesSections;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000222 StringRef AbbrevSection;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000223 Section LocSection;
David Blaikie9c550ac2014-03-25 01:44:02 +0000224 Section LocDWOSection;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000225 StringRef ARangeSection;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000226 StringRef DebugFrameSection;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000227 Section LineSection;
David Blaikie1d4736e2014-02-24 23:58:54 +0000228 Section LineDWOSection;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000229 StringRef StringSection;
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000230 StringRef RangeSection;
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000231 StringRef PubNamesSection;
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000232 StringRef PubTypesSection;
David Blaikie404d3042013-09-19 23:01:29 +0000233 StringRef GnuPubNamesSection;
David Blaikieecd21ff2013-09-24 19:50:00 +0000234 StringRef GnuPubTypesSection;
Eric Christopherda4b2192013-01-02 23:52:13 +0000235
236 // Sections for DWARF5 split dwarf proposal.
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000237 Section InfoDWOSection;
David Blaikie92d9d622014-01-09 05:08:24 +0000238 TypeSectionMap TypesDWOSections;
Eric Christopherda4b2192013-01-02 23:52:13 +0000239 StringRef AbbrevDWOSection;
240 StringRef StringDWOSection;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000241 StringRef StringOffsetDWOSection;
Eric Christopherda4b2192013-01-02 23:52:13 +0000242 StringRef RangeDWOSection;
Eric Christopher962c9082013-01-15 23:56:56 +0000243 StringRef AddrSection;
Eric Christopherda4b2192013-01-02 23:52:13 +0000244
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000245 SmallVector<std::unique_ptr<MemoryBuffer>, 4> UncompressedSections;
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000246
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000247public:
Eric Christopher7370b552012-11-12 21:40:38 +0000248 DWARFContextInMemory(object::ObjectFile *);
Craig Topper85482992014-03-05 07:52:44 +0000249 bool isLittleEndian() const override { return IsLittleEndian; }
250 uint8_t getAddressSize() const override { return AddressSize; }
251 const Section &getInfoSection() override { return InfoSection; }
252 const TypeSectionMap &getTypesSections() override { return TypesSections; }
253 StringRef getAbbrevSection() override { return AbbrevSection; }
254 const Section &getLocSection() override { return LocSection; }
David Blaikie9c550ac2014-03-25 01:44:02 +0000255 const Section &getLocDWOSection() override { return LocDWOSection; }
Craig Topper85482992014-03-05 07:52:44 +0000256 StringRef getARangeSection() override { return ARangeSection; }
257 StringRef getDebugFrameSection() override { return DebugFrameSection; }
258 const Section &getLineSection() override { return LineSection; }
259 const Section &getLineDWOSection() override { return LineDWOSection; }
260 StringRef getStringSection() override { return StringSection; }
261 StringRef getRangeSection() override { return RangeSection; }
262 StringRef getPubNamesSection() override { return PubNamesSection; }
263 StringRef getPubTypesSection() override { return PubTypesSection; }
264 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
265 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
Eric Christopherda4b2192013-01-02 23:52:13 +0000266
267 // Sections for DWARF5 split dwarf proposal.
Craig Topper85482992014-03-05 07:52:44 +0000268 const Section &getInfoDWOSection() override { return InfoDWOSection; }
269 const TypeSectionMap &getTypesDWOSections() override {
David Blaikie92d9d622014-01-09 05:08:24 +0000270 return TypesDWOSections;
271 }
Craig Topper85482992014-03-05 07:52:44 +0000272 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
273 StringRef getStringDWOSection() override { return StringDWOSection; }
274 StringRef getStringOffsetDWOSection() override {
Eric Christopher2cbd5762013-01-07 19:32:41 +0000275 return StringOffsetDWOSection;
276 }
Craig Topper85482992014-03-05 07:52:44 +0000277 StringRef getRangeDWOSection() override { return RangeDWOSection; }
278 StringRef getAddrSection() override {
Eric Christopher962c9082013-01-15 23:56:56 +0000279 return AddrSection;
280 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000281};
282
283}
284
285#endif