Allow the C API users to keep relying on the OutMessages parameter.
[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 "DWARFSection.h"
20 #include "DWARFTypeUnit.h"
21 #include "llvm/ADT/MapVector.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/DebugInfo/DIContext.h"
24 #include <vector>
25
26 namespace llvm {
27
28 /// DWARFContext
29 /// This data structure is the top level entity that deals with dwarf debug
30 /// information parsing. The actual data is supplied through pure virtual
31 /// methods that a concrete implementation provides.
32 class DWARFContext : public DIContext {
33
34   DWARFUnitSection<DWARFCompileUnit> CUs;
35   std::vector<DWARFUnitSection<DWARFTypeUnit>> 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   DWARFUnitSection<DWARFCompileUnit> DWOCUs;
43   std::vector<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
44   std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
45   std::unique_ptr<DWARFDebugLocDWO> LocDWO;
46
47   DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
48   DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
49
50   /// Read compile units from the debug_info section (if necessary)
51   /// and store them in CUs.
52   void parseCompileUnits();
53
54   /// Read type units from the debug_types sections (if necessary)
55   /// and store them in TUs.
56   void parseTypeUnits();
57
58   /// Read compile units from the debug_info.dwo section (if necessary)
59   /// and store them in DWOCUs.
60   void parseDWOCompileUnits();
61
62   /// Read type units from the debug_types.dwo section (if necessary)
63   /// and store them in DWOTUs.
64   void parseDWOTypeUnits();
65
66 public:
67   DWARFContext() : DIContext(CK_DWARF) {}
68
69   static bool classof(const DIContext *DICtx) {
70     return DICtx->getKind() == CK_DWARF;
71   }
72
73   void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
74
75   typedef DWARFUnitSection<DWARFCompileUnit>::iterator_range cu_iterator_range;
76   typedef DWARFUnitSection<DWARFTypeUnit>::iterator_range tu_iterator_range;
77   typedef iterator_range<std::vector<DWARFUnitSection<DWARFTypeUnit>>::iterator> tu_section_iterator_range;
78
79   /// Get compile units in this context.
80   cu_iterator_range compile_units() {
81     parseCompileUnits();
82     return cu_iterator_range(CUs.begin(), CUs.end());
83   }
84
85   /// Get type units in this context.
86   tu_section_iterator_range type_unit_sections() {
87     parseTypeUnits();
88     return tu_section_iterator_range(TUs.begin(), TUs.end());
89   }
90
91   /// Get compile units in the DWO context.
92   cu_iterator_range dwo_compile_units() {
93     parseDWOCompileUnits();
94     return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
95   }
96
97   /// Get type units in the DWO context.
98   tu_section_iterator_range dwo_type_unit_sections() {
99     parseDWOTypeUnits();
100     return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
101   }
102
103   /// Get the number of compile units in this context.
104   unsigned getNumCompileUnits() {
105     parseCompileUnits();
106     return CUs.size();
107   }
108
109   /// Get the number of compile units in this context.
110   unsigned getNumTypeUnits() {
111     parseTypeUnits();
112     return TUs.size();
113   }
114
115   /// Get the number of compile units in the DWO context.
116   unsigned getNumDWOCompileUnits() {
117     parseDWOCompileUnits();
118     return DWOCUs.size();
119   }
120
121   /// Get the number of compile units in the DWO context.
122   unsigned getNumDWOTypeUnits() {
123     parseDWOTypeUnits();
124     return DWOTUs.size();
125   }
126
127   /// Get the compile unit at the specified index for this compile unit.
128   DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
129     parseCompileUnits();
130     return CUs[index].get();
131   }
132
133   /// Get the compile unit at the specified index for the DWO compile units.
134   DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
135     parseDWOCompileUnits();
136     return DWOCUs[index].get();
137   }
138
139   /// Get a pointer to the parsed DebugAbbrev object.
140   const DWARFDebugAbbrev *getDebugAbbrev();
141
142   /// Get a pointer to the parsed DebugLoc object.
143   const DWARFDebugLoc *getDebugLoc();
144
145   /// Get a pointer to the parsed dwo abbreviations object.
146   const DWARFDebugAbbrev *getDebugAbbrevDWO();
147
148   /// Get a pointer to the parsed DebugLoc object.
149   const DWARFDebugLocDWO *getDebugLocDWO();
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 *getLineTableForUnit(DWARFUnit *cu);
159
160   DILineInfo getLineInfoForAddress(uint64_t Address,
161       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
162   DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
163       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
164   DIInliningInfo getInliningInfoForAddress(uint64_t Address,
165       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
166
167   virtual bool isLittleEndian() const = 0;
168   virtual uint8_t getAddressSize() const = 0;
169   virtual const DWARFSection &getInfoSection() = 0;
170   typedef MapVector<object::SectionRef, DWARFSection,
171                     std::map<object::SectionRef, unsigned>> TypeSectionMap;
172   virtual const TypeSectionMap &getTypesSections() = 0;
173   virtual StringRef getAbbrevSection() = 0;
174   virtual const DWARFSection &getLocSection() = 0;
175   virtual StringRef getARangeSection() = 0;
176   virtual StringRef getDebugFrameSection() = 0;
177   virtual const DWARFSection &getLineSection() = 0;
178   virtual StringRef getStringSection() = 0;
179   virtual StringRef getRangeSection() = 0;
180   virtual StringRef getPubNamesSection() = 0;
181   virtual StringRef getPubTypesSection() = 0;
182   virtual StringRef getGnuPubNamesSection() = 0;
183   virtual StringRef getGnuPubTypesSection() = 0;
184
185   // Sections for DWARF5 split dwarf proposal.
186   virtual const DWARFSection &getInfoDWOSection() = 0;
187   virtual const TypeSectionMap &getTypesDWOSections() = 0;
188   virtual StringRef getAbbrevDWOSection() = 0;
189   virtual const DWARFSection &getLineDWOSection() = 0;
190   virtual const DWARFSection &getLocDWOSection() = 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   DWARFSection InfoSection;
216   TypeSectionMap TypesSections;
217   StringRef AbbrevSection;
218   DWARFSection LocSection;
219   StringRef ARangeSection;
220   StringRef DebugFrameSection;
221   DWARFSection LineSection;
222   StringRef StringSection;
223   StringRef RangeSection;
224   StringRef PubNamesSection;
225   StringRef PubTypesSection;
226   StringRef GnuPubNamesSection;
227   StringRef GnuPubTypesSection;
228
229   // Sections for DWARF5 split dwarf proposal.
230   DWARFSection InfoDWOSection;
231   TypeSectionMap TypesDWOSections;
232   StringRef AbbrevDWOSection;
233   DWARFSection LineDWOSection;
234   DWARFSection LocDWOSection;
235   StringRef StringDWOSection;
236   StringRef StringOffsetDWOSection;
237   StringRef RangeDWOSection;
238   StringRef AddrSection;
239
240   SmallVector<SmallString<32>, 4> UncompressedSections;
241
242 public:
243   DWARFContextInMemory(const object::ObjectFile &Obj);
244   bool isLittleEndian() const override { return IsLittleEndian; }
245   uint8_t getAddressSize() const override { return AddressSize; }
246   const DWARFSection &getInfoSection() override { return InfoSection; }
247   const TypeSectionMap &getTypesSections() override { return TypesSections; }
248   StringRef getAbbrevSection() override { return AbbrevSection; }
249   const DWARFSection &getLocSection() override { return LocSection; }
250   StringRef getARangeSection() override { return ARangeSection; }
251   StringRef getDebugFrameSection() override { return DebugFrameSection; }
252   const DWARFSection &getLineSection() override { return LineSection; }
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 DWARFSection &getInfoDWOSection() override { return InfoDWOSection; }
262   const TypeSectionMap &getTypesDWOSections() override {
263     return TypesDWOSections;
264   }
265   StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
266   const DWARFSection &getLineDWOSection() override { return LineDWOSection; }
267   const DWARFSection &getLocDWOSection() override { return LocDWOSection; }
268   StringRef getStringDWOSection() override { return StringDWOSection; }
269   StringRef getStringOffsetDWOSection() override {
270     return StringOffsetDWOSection;
271   }
272   StringRef getRangeDWOSection() override { return RangeDWOSection; }
273   StringRef getAddrSection() override {
274     return AddrSection;
275   }
276 };
277
278 }
279
280 #endif