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