a3bae7927376f685c434707e485a214fb8aeb03a
[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_DEBUGINFO_DWARFCONTEXT_H
11 #define LLVM_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   typedef SmallVector<std::unique_ptr<DWARFCompileUnit>, 1> CUVector;
32   typedef SmallVector<std::unique_ptr<DWARFTypeUnit>, 1> TUVector;
33
34   CUVector CUs;
35   TUVector TUs;
36   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;
41
42   CUVector DWOCUs;
43   TUVector DWOTUs;
44   std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
45
46   DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
47   DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
48
49   /// Read compile units from the debug_info section (if necessary)
50   /// and store them in CUs.
51   void parseCompileUnits();
52
53   /// Read type units from the debug_types sections (if necessary)
54   /// and store them in TUs.
55   void parseTypeUnits();
56
57   /// Read compile units from the debug_info.dwo section (if necessary)
58   /// and store them in DWOCUs.
59   void parseDWOCompileUnits();
60
61   /// Read type units from the debug_types.dwo section (if necessary)
62   /// and store them in DWOTUs.
63   void parseDWOTypeUnits();
64
65 public:
66   struct Section {
67     StringRef Data;
68     RelocAddrMap Relocs;
69   };
70
71   DWARFContext() : DIContext(CK_DWARF) {}
72
73   static bool classof(const DIContext *DICtx) {
74     return DICtx->getKind() == CK_DWARF;
75   }
76
77   void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
78
79   typedef iterator_range<CUVector::iterator> cu_iterator_range;
80   typedef iterator_range<TUVector::iterator> tu_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_iterator_range type_units() {
90     parseTypeUnits();
91     return tu_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_iterator_range dwo_type_units() {
102     parseDWOTypeUnits();
103     return tu_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 DebugAranges object.
152   const DWARFDebugAranges *getDebugAranges();
153
154   /// Get a pointer to the parsed frame information object.
155   const DWARFDebugFrame *getDebugFrame();
156
157   /// Get a pointer to a parsed line table corresponding to a compile unit.
158   const DWARFDebugLine::LineTable *
159   getLineTableForCompileUnit(DWARFCompileUnit *cu);
160
161   DILineInfo getLineInfoForAddress(uint64_t Address,
162       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
163   DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
164       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
165   DIInliningInfo getInliningInfoForAddress(uint64_t Address,
166       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
167
168   virtual bool isLittleEndian() const = 0;
169   virtual uint8_t getAddressSize() const = 0;
170   virtual const Section &getInfoSection() = 0;
171   typedef MapVector<object::SectionRef, Section,
172                     std::map<object::SectionRef, unsigned> > TypeSectionMap;
173   virtual const TypeSectionMap &getTypesSections() = 0;
174   virtual StringRef getAbbrevSection() = 0;
175   virtual const Section &getLocSection() = 0;
176   virtual StringRef getARangeSection() = 0;
177   virtual StringRef getDebugFrameSection() = 0;
178   virtual const Section &getLineSection() = 0;
179   virtual const Section &getLineDWOSection() = 0;
180   virtual StringRef getStringSection() = 0;
181   virtual StringRef getRangeSection() = 0;
182   virtual StringRef getPubNamesSection() = 0;
183   virtual StringRef getPubTypesSection() = 0;
184   virtual StringRef getGnuPubNamesSection() = 0;
185   virtual StringRef getGnuPubTypesSection() = 0;
186
187   // Sections for DWARF5 split dwarf proposal.
188   virtual const Section &getInfoDWOSection() = 0;
189   virtual const TypeSectionMap &getTypesDWOSections() = 0;
190   virtual StringRef getAbbrevDWOSection() = 0;
191   virtual StringRef getStringDWOSection() = 0;
192   virtual StringRef getStringOffsetDWOSection() = 0;
193   virtual StringRef getRangeDWOSection() = 0;
194   virtual StringRef getAddrSection() = 0;
195
196   static bool isSupportedVersion(unsigned version) {
197     return version == 2 || version == 3 || version == 4;
198   }
199 private:
200   /// Return the compile unit that includes an offset (relative to .debug_info).
201   DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
202
203   /// Return the compile unit which contains instruction with provided
204   /// address.
205   DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
206 };
207
208 /// DWARFContextInMemory is the simplest possible implementation of a
209 /// DWARFContext. It assumes all content is available in memory and stores
210 /// pointers to it.
211 class DWARFContextInMemory : public DWARFContext {
212   virtual void anchor();
213   bool IsLittleEndian;
214   uint8_t AddressSize;
215   Section InfoSection;
216   TypeSectionMap TypesSections;
217   StringRef AbbrevSection;
218   Section LocSection;
219   StringRef ARangeSection;
220   StringRef DebugFrameSection;
221   Section LineSection;
222   Section LineDWOSection;
223   StringRef StringSection;
224   StringRef RangeSection;
225   StringRef PubNamesSection;
226   StringRef PubTypesSection;
227   StringRef GnuPubNamesSection;
228   StringRef GnuPubTypesSection;
229
230   // Sections for DWARF5 split dwarf proposal.
231   Section InfoDWOSection;
232   TypeSectionMap TypesDWOSections;
233   StringRef AbbrevDWOSection;
234   StringRef StringDWOSection;
235   StringRef StringOffsetDWOSection;
236   StringRef RangeDWOSection;
237   StringRef AddrSection;
238
239   SmallVector<std::unique_ptr<MemoryBuffer>, 4> UncompressedSections;
240
241 public:
242   DWARFContextInMemory(object::ObjectFile *);
243   bool isLittleEndian() const override { return IsLittleEndian; }
244   uint8_t getAddressSize() const override { return AddressSize; }
245   const Section &getInfoSection() override { return InfoSection; }
246   const TypeSectionMap &getTypesSections() override { return TypesSections; }
247   StringRef getAbbrevSection() override { return AbbrevSection; }
248   const Section &getLocSection() override { return LocSection; }
249   StringRef getARangeSection() override { return ARangeSection; }
250   StringRef getDebugFrameSection() override { return DebugFrameSection; }
251   const Section &getLineSection() override { return LineSection; }
252   const Section &getLineDWOSection() override { return LineDWOSection; }
253   StringRef getStringSection() override { return StringSection; }
254   StringRef getRangeSection() override { return RangeSection; }
255   StringRef getPubNamesSection() override { return PubNamesSection; }
256   StringRef getPubTypesSection() override { return PubTypesSection; }
257   StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
258   StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
259
260   // Sections for DWARF5 split dwarf proposal.
261   const Section &getInfoDWOSection() override { return InfoDWOSection; }
262   const TypeSectionMap &getTypesDWOSections() override {
263     return TypesDWOSections;
264   }
265   StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
266   StringRef getStringDWOSection() override { return StringDWOSection; }
267   StringRef getStringOffsetDWOSection() override {
268     return StringOffsetDWOSection;
269   }
270   StringRef getRangeDWOSection() override { return RangeDWOSection; }
271   StringRef getAddrSection() override {
272     return AddrSection;
273   }
274 };
275
276 }
277
278 #endif