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