blob: 96eaa70bd563d2fbb660630966a22c4e4ccf28d9 [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 Samsonova9debbf2013-08-23 06:56:01 +000031 SmallVector<DWARFCompileUnit *, 1> CUs;
David Blaikie03c089c2013-09-23 22:44:47 +000032 SmallVector<DWARFTypeUnit *, 1> TUs;
Ahmed Charles56440fd2014-03-06 05:51:42 +000033 std::unique_ptr<DWARFDebugAbbrev> Abbrev;
34 std::unique_ptr<DWARFDebugLoc> Loc;
35 std::unique_ptr<DWARFDebugAranges> Aranges;
36 std::unique_ptr<DWARFDebugLine> Line;
37 std::unique_ptr<DWARFDebugFrame> DebugFrame;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000038
Alexey Samsonova9debbf2013-08-23 06:56:01 +000039 SmallVector<DWARFCompileUnit *, 1> DWOCUs;
David Blaikie92d9d622014-01-09 05:08:24 +000040 SmallVector<DWARFTypeUnit *, 1> DWOTUs;
Ahmed Charles56440fd2014-03-06 05:51:42 +000041 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
Eric Christopherda4b2192013-01-02 23:52:13 +000042
Craig Topperb1d83e82012-09-18 02:01:41 +000043 DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
44 DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000045
46 /// Read compile units from the debug_info section and store them in CUs.
47 void parseCompileUnits();
Eric Christopher7370b552012-11-12 21:40:38 +000048
David Blaikie03c089c2013-09-23 22:44:47 +000049 /// Read type units from the debug_types sections and store them in CUs.
50 void parseTypeUnits();
51
Eric Christopherda4b2192013-01-02 23:52:13 +000052 /// Read compile units from the debug_info.dwo section and store them in
53 /// DWOCUs.
54 void parseDWOCompileUnits();
55
David Blaikie92d9d622014-01-09 05:08:24 +000056 /// Read type units from the debug_types.dwo section and store them in
57 /// DWOTUs.
58 void parseDWOTypeUnits();
59
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000060public:
David Blaikie1b5ee5d2013-09-23 17:42:01 +000061 struct Section {
62 StringRef Data;
63 RelocAddrMap Relocs;
64 };
65
Alexey Samsonovc2e00872013-08-06 10:32:39 +000066 DWARFContext() : DIContext(CK_DWARF) {}
Alexey Samsonova9debbf2013-08-23 06:56:01 +000067 virtual ~DWARFContext();
Alexey Samsonovc2e00872013-08-06 10:32:39 +000068
69 static bool classof(const DIContext *DICtx) {
70 return DICtx->getKind() == CK_DWARF;
71 }
72
Craig Topper85482992014-03-05 07:52:44 +000073 void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
Eric Christopher7c678de2012-11-07 23:22:07 +000074
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000075 /// Get the number of compile units in this context.
76 unsigned getNumCompileUnits() {
77 if (CUs.empty())
78 parseCompileUnits();
79 return CUs.size();
80 }
Eric Christopherda4b2192013-01-02 23:52:13 +000081
David Blaikie03c089c2013-09-23 22:44:47 +000082 /// Get the number of compile units in this context.
83 unsigned getNumTypeUnits() {
84 if (TUs.empty())
85 parseTypeUnits();
86 return TUs.size();
87 }
88
Eric Christopherda4b2192013-01-02 23:52:13 +000089 /// Get the number of compile units in the DWO context.
90 unsigned getNumDWOCompileUnits() {
91 if (DWOCUs.empty())
92 parseDWOCompileUnits();
93 return DWOCUs.size();
94 }
95
David Blaikie92d9d622014-01-09 05:08:24 +000096 /// Get the number of compile units in the DWO context.
97 unsigned getNumDWOTypeUnits() {
98 if (DWOTUs.empty())
99 parseDWOTypeUnits();
100 return DWOTUs.size();
101 }
102
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000103 /// Get the compile unit at the specified index for this compile unit.
104 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
105 if (CUs.empty())
106 parseCompileUnits();
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000107 return CUs[index];
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000108 }
109
David Blaikie03c089c2013-09-23 22:44:47 +0000110 /// Get the type unit at the specified index for this compile unit.
111 DWARFTypeUnit *getTypeUnitAtIndex(unsigned index) {
112 if (TUs.empty())
113 parseTypeUnits();
114 return TUs[index];
115 }
116
Eric Christopherda4b2192013-01-02 23:52:13 +0000117 /// Get the compile unit at the specified index for the DWO compile units.
118 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
119 if (DWOCUs.empty())
120 parseDWOCompileUnits();
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000121 return DWOCUs[index];
Eric Christopherda4b2192013-01-02 23:52:13 +0000122 }
123
David Blaikie92d9d622014-01-09 05:08:24 +0000124 /// Get the type unit at the specified index for the DWO type units.
125 DWARFTypeUnit *getDWOTypeUnitAtIndex(unsigned index) {
126 if (DWOTUs.empty())
127 parseDWOTypeUnits();
128 return DWOTUs[index];
129 }
130
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000131 /// Get a pointer to the parsed DebugAbbrev object.
132 const DWARFDebugAbbrev *getDebugAbbrev();
133
David Blaikie18e73502013-06-19 21:37:13 +0000134 /// Get a pointer to the parsed DebugLoc object.
135 const DWARFDebugLoc *getDebugLoc();
136
Eric Christopherda4b2192013-01-02 23:52:13 +0000137 /// Get a pointer to the parsed dwo abbreviations object.
138 const DWARFDebugAbbrev *getDebugAbbrevDWO();
139
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000140 /// Get a pointer to the parsed DebugAranges object.
141 const DWARFDebugAranges *getDebugAranges();
142
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000143 /// Get a pointer to the parsed frame information object.
144 const DWARFDebugFrame *getDebugFrame();
145
Benjamin Kramer679e1752011-09-15 20:43:18 +0000146 /// Get a pointer to a parsed line table corresponding to a compile unit.
147 const DWARFDebugLine::LineTable *
148 getLineTableForCompileUnit(DWARFCompileUnit *cu);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000149
Craig Topper85482992014-03-05 07:52:44 +0000150 DILineInfo getLineInfoForAddress(uint64_t Address,
151 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
152 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
153 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
154 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
155 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000156
Eric Christopher7370b552012-11-12 21:40:38 +0000157 virtual bool isLittleEndian() const = 0;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000158 virtual uint8_t getAddressSize() const = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000159 virtual const Section &getInfoSection() = 0;
David Blaikiebc563272013-12-13 21:33:40 +0000160 typedef MapVector<object::SectionRef, Section,
161 std::map<object::SectionRef, unsigned> > TypeSectionMap;
162 virtual const TypeSectionMap &getTypesSections() = 0;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000163 virtual StringRef getAbbrevSection() = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000164 virtual const Section &getLocSection() = 0;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000165 virtual StringRef getARangeSection() = 0;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000166 virtual StringRef getDebugFrameSection() = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000167 virtual const Section &getLineSection() = 0;
David Blaikie1d4736e2014-02-24 23:58:54 +0000168 virtual const Section &getLineDWOSection() = 0;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000169 virtual StringRef getStringSection() = 0;
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000170 virtual StringRef getRangeSection() = 0;
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000171 virtual StringRef getPubNamesSection() = 0;
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000172 virtual StringRef getPubTypesSection() = 0;
David Blaikie404d3042013-09-19 23:01:29 +0000173 virtual StringRef getGnuPubNamesSection() = 0;
David Blaikieecd21ff2013-09-24 19:50:00 +0000174 virtual StringRef getGnuPubTypesSection() = 0;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000175
Eric Christopherda4b2192013-01-02 23:52:13 +0000176 // Sections for DWARF5 split dwarf proposal.
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000177 virtual const Section &getInfoDWOSection() = 0;
David Blaikie92d9d622014-01-09 05:08:24 +0000178 virtual const TypeSectionMap &getTypesDWOSections() = 0;
Eric Christopherda4b2192013-01-02 23:52:13 +0000179 virtual StringRef getAbbrevDWOSection() = 0;
180 virtual StringRef getStringDWOSection() = 0;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000181 virtual StringRef getStringOffsetDWOSection() = 0;
Eric Christopherda4b2192013-01-02 23:52:13 +0000182 virtual StringRef getRangeDWOSection() = 0;
Eric Christopher962c9082013-01-15 23:56:56 +0000183 virtual StringRef getAddrSection() = 0;
Eric Christopherda4b2192013-01-02 23:52:13 +0000184
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000185 static bool isSupportedVersion(unsigned version) {
Eric Christopherf7d848d2013-08-06 01:38:27 +0000186 return version == 2 || version == 3 || version == 4;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000187 }
Alexey Samsonov45be7932012-08-30 07:49:50 +0000188private:
189 /// Return the compile unit that includes an offset (relative to .debug_info).
190 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000191
Alexey Samsonov45be7932012-08-30 07:49:50 +0000192 /// Return the compile unit which contains instruction with provided
193 /// address.
194 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000195};
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000196
197/// DWARFContextInMemory is the simplest possible implementation of a
198/// DWARFContext. It assumes all content is available in memory and stores
199/// pointers to it.
200class DWARFContextInMemory : public DWARFContext {
David Blaikiea379b1812011-12-20 02:50:00 +0000201 virtual void anchor();
Eric Christopher7370b552012-11-12 21:40:38 +0000202 bool IsLittleEndian;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000203 uint8_t AddressSize;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000204 Section InfoSection;
David Blaikiebc563272013-12-13 21:33:40 +0000205 TypeSectionMap TypesSections;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000206 StringRef AbbrevSection;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000207 Section LocSection;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000208 StringRef ARangeSection;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000209 StringRef DebugFrameSection;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000210 Section LineSection;
David Blaikie1d4736e2014-02-24 23:58:54 +0000211 Section LineDWOSection;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000212 StringRef StringSection;
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000213 StringRef RangeSection;
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000214 StringRef PubNamesSection;
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000215 StringRef PubTypesSection;
David Blaikie404d3042013-09-19 23:01:29 +0000216 StringRef GnuPubNamesSection;
David Blaikieecd21ff2013-09-24 19:50:00 +0000217 StringRef GnuPubTypesSection;
Eric Christopherda4b2192013-01-02 23:52:13 +0000218
219 // Sections for DWARF5 split dwarf proposal.
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000220 Section InfoDWOSection;
David Blaikie92d9d622014-01-09 05:08:24 +0000221 TypeSectionMap TypesDWOSections;
Eric Christopherda4b2192013-01-02 23:52:13 +0000222 StringRef AbbrevDWOSection;
223 StringRef StringDWOSection;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000224 StringRef StringOffsetDWOSection;
Eric Christopherda4b2192013-01-02 23:52:13 +0000225 StringRef RangeDWOSection;
Eric Christopher962c9082013-01-15 23:56:56 +0000226 StringRef AddrSection;
Eric Christopherda4b2192013-01-02 23:52:13 +0000227
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000228 SmallVector<MemoryBuffer*, 4> UncompressedSections;
229
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000230public:
Eric Christopher7370b552012-11-12 21:40:38 +0000231 DWARFContextInMemory(object::ObjectFile *);
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000232 ~DWARFContextInMemory();
Craig Topper85482992014-03-05 07:52:44 +0000233 bool isLittleEndian() const override { return IsLittleEndian; }
234 uint8_t getAddressSize() const override { return AddressSize; }
235 const Section &getInfoSection() override { return InfoSection; }
236 const TypeSectionMap &getTypesSections() override { return TypesSections; }
237 StringRef getAbbrevSection() override { return AbbrevSection; }
238 const Section &getLocSection() override { return LocSection; }
239 StringRef getARangeSection() override { return ARangeSection; }
240 StringRef getDebugFrameSection() override { return DebugFrameSection; }
241 const Section &getLineSection() override { return LineSection; }
242 const Section &getLineDWOSection() override { return LineDWOSection; }
243 StringRef getStringSection() override { return StringSection; }
244 StringRef getRangeSection() override { return RangeSection; }
245 StringRef getPubNamesSection() override { return PubNamesSection; }
246 StringRef getPubTypesSection() override { return PubTypesSection; }
247 StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
248 StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
Eric Christopherda4b2192013-01-02 23:52:13 +0000249
250 // Sections for DWARF5 split dwarf proposal.
Craig Topper85482992014-03-05 07:52:44 +0000251 const Section &getInfoDWOSection() override { return InfoDWOSection; }
252 const TypeSectionMap &getTypesDWOSections() override {
David Blaikie92d9d622014-01-09 05:08:24 +0000253 return TypesDWOSections;
254 }
Craig Topper85482992014-03-05 07:52:44 +0000255 StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
256 StringRef getStringDWOSection() override { return StringDWOSection; }
257 StringRef getStringOffsetDWOSection() override {
Eric Christopher2cbd5762013-01-07 19:32:41 +0000258 return StringOffsetDWOSection;
259 }
Craig Topper85482992014-03-05 07:52:44 +0000260 StringRef getRangeDWOSection() override { return RangeDWOSection; }
261 StringRef getAddrSection() override {
Eric Christopher962c9082013-01-15 23:56:56 +0000262 return AddrSection;
263 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000264};
265
266}
267
268#endif