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