Introduce the DWARFUnitSection abstraction.
[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   DWARFUnitSection<DWARFTypeUnit> 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   DWARFUnitSection<DWARFTypeUnit> 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
81   /// Get compile units in this context.
82   cu_iterator_range compile_units() {
83     parseCompileUnits();
84     return cu_iterator_range(CUs.begin(), CUs.end());
85   }
86
87   /// Get type units in this context.
88   tu_iterator_range type_units() {
89     parseTypeUnits();
90     return tu_iterator_range(TUs.begin(), TUs.end());
91   }
92
93   /// Get compile units in the DWO context.
94   cu_iterator_range dwo_compile_units() {
95     parseDWOCompileUnits();
96     return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
97   }
98
99   /// Get type units in the DWO context.
100   tu_iterator_range dwo_type_units() {
101     parseDWOTypeUnits();
102     return tu_iterator_range(DWOTUs.begin(), DWOTUs.end());
103   }
104
105   /// Get the number of compile units in this context.
106   unsigned getNumCompileUnits() {
107     parseCompileUnits();
108     return CUs.size();
109   }
110
111   /// Get the number of compile units in this context.
112   unsigned getNumTypeUnits() {
113     parseTypeUnits();
114     return TUs.size();
115   }
116
117   /// Get the number of compile units in the DWO context.
118   unsigned getNumDWOCompileUnits() {
119     parseDWOCompileUnits();
120     return DWOCUs.size();
121   }
122
123   /// Get the number of compile units in the DWO context.
124   unsigned getNumDWOTypeUnits() {
125     parseDWOTypeUnits();
126     return DWOTUs.size();
127   }
128
129   /// Get the compile unit at the specified index for this compile unit.
130   DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
131     parseCompileUnits();
132     return CUs[index].get();
133   }
134
135   /// Get the compile unit at the specified index for the DWO compile units.
136   DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
137     parseDWOCompileUnits();
138     return DWOCUs[index].get();
139   }
140
141   /// Get a pointer to the parsed DebugAbbrev object.
142   const DWARFDebugAbbrev *getDebugAbbrev();
143
144   /// Get a pointer to the parsed DebugLoc object.
145   const DWARFDebugLoc *getDebugLoc();
146
147   /// Get a pointer to the parsed dwo abbreviations object.
148   const DWARFDebugAbbrev *getDebugAbbrevDWO();
149
150   /// Get a pointer to the parsed DebugLoc object.
151   const DWARFDebugLocDWO *getDebugLocDWO();
152
153   /// Get a pointer to the parsed DebugAranges object.
154   const DWARFDebugAranges *getDebugAranges();
155
156   /// Get a pointer to the parsed frame information object.
157   const DWARFDebugFrame *getDebugFrame();
158
159   /// Get a pointer to a parsed line table corresponding to a compile unit.
160   const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
161
162   DILineInfo getLineInfoForAddress(uint64_t Address,
163       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
164   DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
165       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
166   DIInliningInfo getInliningInfoForAddress(uint64_t Address,
167       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
168
169   virtual bool isLittleEndian() const = 0;
170   virtual uint8_t getAddressSize() const = 0;
171   virtual const Section &getInfoSection() = 0;
172   typedef MapVector<object::SectionRef, Section,
173                     std::map<object::SectionRef, unsigned> > TypeSectionMap;
174   virtual const TypeSectionMap &getTypesSections() = 0;
175   virtual StringRef getAbbrevSection() = 0;
176   virtual const Section &getLocSection() = 0;
177   virtual const Section &getLocDWOSection() = 0;
178   virtual StringRef getARangeSection() = 0;
179   virtual StringRef getDebugFrameSection() = 0;
180   virtual const Section &getLineSection() = 0;
181   virtual const Section &getLineDWOSection() = 0;
182   virtual StringRef getStringSection() = 0;
183   virtual StringRef getRangeSection() = 0;
184   virtual StringRef getPubNamesSection() = 0;
185   virtual StringRef getPubTypesSection() = 0;
186   virtual StringRef getGnuPubNamesSection() = 0;
187   virtual StringRef getGnuPubTypesSection() = 0;
188
189   // Sections for DWARF5 split dwarf proposal.
190   virtual const Section &getInfoDWOSection() = 0;
191   virtual const TypeSectionMap &getTypesDWOSections() = 0;
192   virtual StringRef getAbbrevDWOSection() = 0;
193   virtual StringRef getStringDWOSection() = 0;
194   virtual StringRef getStringOffsetDWOSection() = 0;
195   virtual StringRef getRangeDWOSection() = 0;
196   virtual StringRef getAddrSection() = 0;
197
198   static bool isSupportedVersion(unsigned version) {
199     return version == 2 || version == 3 || version == 4;
200   }
201 private:
202   /// Return the compile unit that includes an offset (relative to .debug_info).
203   DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
204
205   /// Return the compile unit which contains instruction with provided
206   /// address.
207   DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
208 };
209
210 /// DWARFContextInMemory is the simplest possible implementation of a
211 /// DWARFContext. It assumes all content is available in memory and stores
212 /// pointers to it.
213 class DWARFContextInMemory : public DWARFContext {
214   virtual void anchor();
215   bool IsLittleEndian;
216   uint8_t AddressSize;
217   Section InfoSection;
218   TypeSectionMap TypesSections;
219   StringRef AbbrevSection;
220   Section LocSection;
221   Section LocDWOSection;
222   StringRef ARangeSection;
223   StringRef DebugFrameSection;
224   Section LineSection;
225   Section LineDWOSection;
226   StringRef StringSection;
227   StringRef RangeSection;
228   StringRef PubNamesSection;
229   StringRef PubTypesSection;
230   StringRef GnuPubNamesSection;
231   StringRef GnuPubTypesSection;
232
233   // Sections for DWARF5 split dwarf proposal.
234   Section InfoDWOSection;
235   TypeSectionMap TypesDWOSections;
236   StringRef AbbrevDWOSection;
237   StringRef StringDWOSection;
238   StringRef StringOffsetDWOSection;
239   StringRef RangeDWOSection;
240   StringRef AddrSection;
241
242   SmallVector<SmallString<32>, 4> UncompressedSections;
243
244 public:
245   DWARFContextInMemory(object::ObjectFile &);
246   bool isLittleEndian() const override { return IsLittleEndian; }
247   uint8_t getAddressSize() const override { return AddressSize; }
248   const Section &getInfoSection() override { return InfoSection; }
249   const TypeSectionMap &getTypesSections() override { return TypesSections; }
250   StringRef getAbbrevSection() override { return AbbrevSection; }
251   const Section &getLocSection() override { return LocSection; }
252   const Section &getLocDWOSection() override { return LocDWOSection; }
253   StringRef getARangeSection() override { return ARangeSection; }
254   StringRef getDebugFrameSection() override { return DebugFrameSection; }
255   const Section &getLineSection() override { return LineSection; }
256   const Section &getLineDWOSection() override { return LineDWOSection; }
257   StringRef getStringSection() override { return StringSection; }
258   StringRef getRangeSection() override { return RangeSection; }
259   StringRef getPubNamesSection() override { return PubNamesSection; }
260   StringRef getPubTypesSection() override { return PubTypesSection; }
261   StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
262   StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
263
264   // Sections for DWARF5 split dwarf proposal.
265   const Section &getInfoDWOSection() override { return InfoDWOSection; }
266   const TypeSectionMap &getTypesDWOSections() override {
267     return TypesDWOSections;
268   }
269   StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
270   StringRef getStringDWOSection() override { return StringDWOSection; }
271   StringRef getStringOffsetDWOSection() override {
272     return StringOffsetDWOSection;
273   }
274   StringRef getRangeDWOSection() override { return RangeDWOSection; }
275   StringRef getAddrSection() override {
276     return AddrSection;
277   }
278 };
279
280 }
281
282 #endif