972f313c622d0cda723bf10e8eb66c5cf74e1503
[oota-llvm.git] / lib / DebugInfo / DWARFContext.h
1 //===-- 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_LIB_DEBUGINFO_DWARFCONTEXT_H
11 #define LLVM_LIB_DEBUGINFO_DWARFCONTEXT_H
12
13 #include "DWARFCompileUnit.h"
14 #include "DWARFDebugAranges.h"
15 #include "DWARFDebugFrame.h"
16 #include "DWARFDebugLine.h"
17 #include "DWARFDebugLoc.h"
18 #include "DWARFDebugRangeList.h"
19 #include "DWARFTypeUnit.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/DebugInfo/DIContext.h"
23
24 namespace llvm {
25
26 /// 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.
30 class DWARFContext : public DIContext {
31
32   DWARFUnitSection<DWARFCompileUnit> CUs;
33   SmallVector<DWARFUnitSection<DWARFTypeUnit>,1> TUs;
34   std::unique_ptr<DWARFDebugAbbrev> Abbrev;
35   std::unique_ptr<DWARFDebugLoc> Loc;
36   std::unique_ptr<DWARFDebugAranges> Aranges;
37   std::unique_ptr<DWARFDebugLine> Line;
38   std::unique_ptr<DWARFDebugFrame> DebugFrame;
39
40   DWARFUnitSection<DWARFCompileUnit> DWOCUs;
41   SmallVector<DWARFUnitSection<DWARFTypeUnit>,1> DWOTUs;
42   std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
43   std::unique_ptr<DWARFDebugLocDWO> LocDWO;
44
45   DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
46   DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
47
48   /// Read compile units from the debug_info section (if necessary)
49   /// and store them in CUs.
50   void parseCompileUnits();
51
52   /// Read type units from the debug_types sections (if necessary)
53   /// and store them in TUs.
54   void parseTypeUnits();
55
56   /// Read compile units from the debug_info.dwo section (if necessary)
57   /// and store them in DWOCUs.
58   void parseDWOCompileUnits();
59
60   /// Read type units from the debug_types.dwo section (if necessary)
61   /// and store them in DWOTUs.
62   void parseDWOTypeUnits();
63
64 public:
65   struct Section {
66     StringRef Data;
67     RelocAddrMap Relocs;
68   };
69
70   DWARFContext() : DIContext(CK_DWARF) {}
71
72   static bool classof(const DIContext *DICtx) {
73     return DICtx->getKind() == CK_DWARF;
74   }
75
76   void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
77
78   typedef DWARFUnitSection<DWARFCompileUnit>::iterator_range cu_iterator_range;
79   typedef DWARFUnitSection<DWARFTypeUnit>::iterator_range tu_iterator_range;
80   typedef iterator_range<SmallVectorImpl<DWARFUnitSection<DWARFTypeUnit>>::iterator> tu_section_iterator_range;
81
82   /// Get compile units in this context.
83   cu_iterator_range compile_units() {
84     parseCompileUnits();
85     return cu_iterator_range(CUs.begin(), CUs.end());
86   }
87
88   /// Get type units in this context.
89   tu_section_iterator_range type_unit_sections() {
90     parseTypeUnits();
91     return tu_section_iterator_range(TUs.begin(), TUs.end());
92   }
93
94   /// Get compile units in the DWO context.
95   cu_iterator_range dwo_compile_units() {
96     parseDWOCompileUnits();
97     return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
98   }
99
100   /// Get type units in the DWO context.
101   tu_section_iterator_range dwo_type_unit_sections() {
102     parseDWOTypeUnits();
103     return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
104   }
105
106   /// Get the number of compile units in this context.
107   unsigned getNumCompileUnits() {
108     parseCompileUnits();
109     return CUs.size();
110   }
111
112   /// Get the number of compile units in this context.
113   unsigned getNumTypeUnits() {
114     parseTypeUnits();
115     return TUs.size();
116   }
117
118   /// Get the number of compile units in the DWO context.
119   unsigned getNumDWOCompileUnits() {
120     parseDWOCompileUnits();
121     return DWOCUs.size();
122   }
123
124   /// Get the number of compile units in the DWO context.
125   unsigned getNumDWOTypeUnits() {
126     parseDWOTypeUnits();
127     return DWOTUs.size();
128   }
129
130   /// Get the compile unit at the specified index for this compile unit.
131   DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
132     parseCompileUnits();
133     return CUs[index].get();
134   }
135
136   /// Get the compile unit at the specified index for the DWO compile units.
137   DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
138     parseDWOCompileUnits();
139     return DWOCUs[index].get();
140   }
141
142   /// Get a pointer to the parsed DebugAbbrev object.
143   const DWARFDebugAbbrev *getDebugAbbrev();
144
145   /// Get a pointer to the parsed DebugLoc object.
146   const DWARFDebugLoc *getDebugLoc();
147
148   /// Get a pointer to the parsed dwo abbreviations object.
149   const DWARFDebugAbbrev *getDebugAbbrevDWO();
150
151   /// Get a pointer to the parsed DebugLoc object.
152   const DWARFDebugLocDWO *getDebugLocDWO();
153
154   /// Get a pointer to the parsed DebugAranges object.
155   const DWARFDebugAranges *getDebugAranges();
156
157   /// Get a pointer to the parsed frame information object.
158   const DWARFDebugFrame *getDebugFrame();
159
160   /// Get a pointer to a parsed line table corresponding to a compile unit.
161   const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
162
163   DILineInfo getLineInfoForAddress(uint64_t Address,
164       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
165   DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
166       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
167   DIInliningInfo getInliningInfoForAddress(uint64_t Address,
168       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
169
170   virtual bool isLittleEndian() const = 0;
171   virtual uint8_t getAddressSize() const = 0;
172   virtual const Section &getInfoSection() = 0;
173   typedef MapVector<object::SectionRef, Section,
174                     std::map<object::SectionRef, unsigned> > TypeSectionMap;
175   virtual const TypeSectionMap &getTypesSections() = 0;
176   virtual StringRef getAbbrevSection() = 0;
177   virtual const Section &getLocSection() = 0;
178   virtual const Section &getLocDWOSection() = 0;
179   virtual StringRef getARangeSection() = 0;
180   virtual StringRef getDebugFrameSection() = 0;
181   virtual const Section &getLineSection() = 0;
182   virtual const Section &getLineDWOSection() = 0;
183   virtual StringRef getStringSection() = 0;
184   virtual StringRef getRangeSection() = 0;
185   virtual StringRef getPubNamesSection() = 0;
186   virtual StringRef getPubTypesSection() = 0;
187   virtual StringRef getGnuPubNamesSection() = 0;
188   virtual StringRef getGnuPubTypesSection() = 0;
189
190   // Sections for DWARF5 split dwarf proposal.
191   virtual const Section &getInfoDWOSection() = 0;
192   virtual const TypeSectionMap &getTypesDWOSections() = 0;
193   virtual StringRef getAbbrevDWOSection() = 0;
194   virtual StringRef getStringDWOSection() = 0;
195   virtual StringRef getStringOffsetDWOSection() = 0;
196   virtual StringRef getRangeDWOSection() = 0;
197   virtual StringRef getAddrSection() = 0;
198
199   static bool isSupportedVersion(unsigned version) {
200     return version == 2 || version == 3 || version == 4;
201   }
202 private:
203   /// Return the compile unit that includes an offset (relative to .debug_info).
204   DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
205
206   /// Return the compile unit which contains instruction with provided
207   /// address.
208   DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
209 };
210
211 /// DWARFContextInMemory is the simplest possible implementation of a
212 /// DWARFContext. It assumes all content is available in memory and stores
213 /// pointers to it.
214 class DWARFContextInMemory : public DWARFContext {
215   virtual void anchor();
216   bool IsLittleEndian;
217   uint8_t AddressSize;
218   Section InfoSection;
219   TypeSectionMap TypesSections;
220   StringRef AbbrevSection;
221   Section LocSection;
222   Section LocDWOSection;
223   StringRef ARangeSection;
224   StringRef DebugFrameSection;
225   Section LineSection;
226   Section LineDWOSection;
227   StringRef StringSection;
228   StringRef RangeSection;
229   StringRef PubNamesSection;
230   StringRef PubTypesSection;
231   StringRef GnuPubNamesSection;
232   StringRef GnuPubTypesSection;
233
234   // Sections for DWARF5 split dwarf proposal.
235   Section InfoDWOSection;
236   TypeSectionMap TypesDWOSections;
237   StringRef AbbrevDWOSection;
238   StringRef StringDWOSection;
239   StringRef StringOffsetDWOSection;
240   StringRef RangeDWOSection;
241   StringRef AddrSection;
242
243   SmallVector<SmallString<32>, 4> UncompressedSections;
244
245 public:
246   DWARFContextInMemory(object::ObjectFile &);
247   bool isLittleEndian() const override { return IsLittleEndian; }
248   uint8_t getAddressSize() const override { return AddressSize; }
249   const Section &getInfoSection() override { return InfoSection; }
250   const TypeSectionMap &getTypesSections() override { return TypesSections; }
251   StringRef getAbbrevSection() override { return AbbrevSection; }
252   const Section &getLocSection() override { return LocSection; }
253   const Section &getLocDWOSection() override { return LocDWOSection; }
254   StringRef getARangeSection() override { return ARangeSection; }
255   StringRef getDebugFrameSection() override { return DebugFrameSection; }
256   const Section &getLineSection() override { return LineSection; }
257   const Section &getLineDWOSection() override { return LineDWOSection; }
258   StringRef getStringSection() override { return StringSection; }
259   StringRef getRangeSection() override { return RangeSection; }
260   StringRef getPubNamesSection() override { return PubNamesSection; }
261   StringRef getPubTypesSection() override { return PubTypesSection; }
262   StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
263   StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
264
265   // Sections for DWARF5 split dwarf proposal.
266   const Section &getInfoDWOSection() override { return InfoDWOSection; }
267   const TypeSectionMap &getTypesDWOSections() override {
268     return TypesDWOSections;
269   }
270   StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
271   StringRef getStringDWOSection() override { return StringDWOSection; }
272   StringRef getStringOffsetDWOSection() override {
273     return StringOffsetDWOSection;
274   }
275   StringRef getRangeDWOSection() override { return RangeDWOSection; }
276   StringRef getAddrSection() override {
277     return AddrSection;
278   }
279 };
280
281 }
282
283 #endif