]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h
Merge ^/head r318380 through r318559.
[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 /// Reads a value from data extractor and applies a relocation to the result if
47 /// one exists for the given offset.
48 uint64_t getRelocatedValue(const DataExtractor &Data, uint32_t Size,
49                            uint32_t *Off, const RelocAddrMap *Relocs);
50
51 /// DWARFContext
52 /// This data structure is the top level entity that deals with dwarf debug
53 /// information parsing. The actual data is supplied through pure virtual
54 /// methods that a concrete implementation provides.
55 class DWARFContext : public DIContext {
56   DWARFUnitSection<DWARFCompileUnit> CUs;
57   std::deque<DWARFUnitSection<DWARFTypeUnit>> TUs;
58   std::unique_ptr<DWARFUnitIndex> CUIndex;
59   std::unique_ptr<DWARFGdbIndex> GdbIndex;
60   std::unique_ptr<DWARFUnitIndex> TUIndex;
61   std::unique_ptr<DWARFDebugAbbrev> Abbrev;
62   std::unique_ptr<DWARFDebugLoc> Loc;
63   std::unique_ptr<DWARFDebugAranges> Aranges;
64   std::unique_ptr<DWARFDebugLine> Line;
65   std::unique_ptr<DWARFDebugFrame> DebugFrame;
66   std::unique_ptr<DWARFDebugFrame> EHFrame;
67   std::unique_ptr<DWARFDebugMacro> Macro;
68
69   DWARFUnitSection<DWARFCompileUnit> DWOCUs;
70   std::deque<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
71   std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
72   std::unique_ptr<DWARFDebugLocDWO> LocDWO;
73
74   /// Read compile units from the debug_info section (if necessary)
75   /// and store them in CUs.
76   void parseCompileUnits();
77
78   /// Read type units from the debug_types sections (if necessary)
79   /// and store them in TUs.
80   void parseTypeUnits();
81
82   /// Read compile units from the debug_info.dwo section (if necessary)
83   /// and store them in DWOCUs.
84   void parseDWOCompileUnits();
85
86   /// Read type units from the debug_types.dwo section (if necessary)
87   /// and store them in DWOTUs.
88   void parseDWOTypeUnits();
89
90 public:
91   DWARFContext() : DIContext(CK_DWARF) {}
92   DWARFContext(DWARFContext &) = delete;
93   DWARFContext &operator=(DWARFContext &) = delete;
94
95   static bool classof(const DIContext *DICtx) {
96     return DICtx->getKind() == CK_DWARF;
97   }
98
99   void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All,
100             bool DumpEH = false, bool SummarizeTypes = false) override;
101
102   bool verify(raw_ostream &OS, DIDumpType DumpType = DIDT_All) 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   /// Get a DIE given an exact offset.
169   DWARFDie getDIEForOffset(uint32_t Offset);
170
171   const DWARFUnitIndex &getCUIndex();
172   DWARFGdbIndex &getGdbIndex();
173   const DWARFUnitIndex &getTUIndex();
174
175   /// Get a pointer to the parsed DebugAbbrev object.
176   const DWARFDebugAbbrev *getDebugAbbrev();
177
178   /// Get a pointer to the parsed DebugLoc object.
179   const DWARFDebugLoc *getDebugLoc();
180
181   /// Get a pointer to the parsed dwo abbreviations object.
182   const DWARFDebugAbbrev *getDebugAbbrevDWO();
183
184   /// Get a pointer to the parsed DebugLoc object.
185   const DWARFDebugLocDWO *getDebugLocDWO();
186
187   /// Get a pointer to the parsed DebugAranges object.
188   const DWARFDebugAranges *getDebugAranges();
189
190   /// Get a pointer to the parsed frame information object.
191   const DWARFDebugFrame *getDebugFrame();
192
193   /// Get a pointer to the parsed eh frame information object.
194   const DWARFDebugFrame *getEHFrame();
195
196   /// Get a pointer to the parsed DebugMacro object.
197   const DWARFDebugMacro *getDebugMacro();
198
199   /// Get a pointer to a parsed line table corresponding to a compile unit.
200   const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
201
202   DILineInfo getLineInfoForAddress(uint64_t Address,
203       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
204   DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
205       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
206   DIInliningInfo getInliningInfoForAddress(uint64_t Address,
207       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
208
209   virtual bool isLittleEndian() const = 0;
210   virtual uint8_t getAddressSize() const = 0;
211   virtual const DWARFSection &getInfoSection() = 0;
212   typedef MapVector<object::SectionRef, DWARFSection,
213                     std::map<object::SectionRef, unsigned>> TypeSectionMap;
214   virtual const TypeSectionMap &getTypesSections() = 0;
215   virtual StringRef getAbbrevSection() = 0;
216   virtual const DWARFSection &getLocSection() = 0;
217   virtual StringRef getARangeSection() = 0;
218   virtual StringRef getDebugFrameSection() = 0;
219   virtual StringRef getEHFrameSection() = 0;
220   virtual const DWARFSection &getLineSection() = 0;
221   virtual StringRef getStringSection() = 0;
222   virtual const DWARFSection& getRangeSection() = 0;
223   virtual StringRef getMacinfoSection() = 0;
224   virtual StringRef getPubNamesSection() = 0;
225   virtual StringRef getPubTypesSection() = 0;
226   virtual StringRef getGnuPubNamesSection() = 0;
227   virtual StringRef getGnuPubTypesSection() = 0;
228
229   // Sections for DWARF5 split dwarf proposal.
230   virtual const DWARFSection &getInfoDWOSection() = 0;
231   virtual const TypeSectionMap &getTypesDWOSections() = 0;
232   virtual StringRef getAbbrevDWOSection() = 0;
233   virtual const DWARFSection &getLineDWOSection() = 0;
234   virtual const DWARFSection &getLocDWOSection() = 0;
235   virtual StringRef getStringDWOSection() = 0;
236   virtual StringRef getStringOffsetDWOSection() = 0;
237   virtual const DWARFSection &getRangeDWOSection() = 0;
238   virtual StringRef getAddrSection() = 0;
239   virtual const DWARFSection& getAppleNamesSection() = 0;
240   virtual const DWARFSection& getAppleTypesSection() = 0;
241   virtual const DWARFSection& getAppleNamespacesSection() = 0;
242   virtual const DWARFSection& getAppleObjCSection() = 0;
243   virtual StringRef getCUIndexSection() = 0;
244   virtual StringRef getGdbIndexSection() = 0;
245   virtual StringRef getTUIndexSection() = 0;
246
247   static bool isSupportedVersion(unsigned version) {
248     return version == 2 || version == 3 || version == 4 || version == 5;
249   }
250
251 private:
252   /// Return the compile unit that includes an offset (relative to .debug_info).
253   DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
254
255   /// Return the compile unit which contains instruction with provided
256   /// address.
257   DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
258 };
259
260 /// DWARFContextInMemory is the simplest possible implementation of a
261 /// DWARFContext. It assumes all content is available in memory and stores
262 /// pointers to it.
263 class DWARFContextInMemory : public DWARFContext {
264   virtual void anchor();
265
266   bool IsLittleEndian;
267   uint8_t AddressSize;
268   DWARFSection InfoSection;
269   TypeSectionMap TypesSections;
270   StringRef AbbrevSection;
271   DWARFSection LocSection;
272   StringRef ARangeSection;
273   StringRef DebugFrameSection;
274   StringRef EHFrameSection;
275   DWARFSection LineSection;
276   StringRef StringSection;
277   DWARFSection RangeSection;
278   StringRef MacinfoSection;
279   StringRef PubNamesSection;
280   StringRef PubTypesSection;
281   StringRef GnuPubNamesSection;
282   StringRef GnuPubTypesSection;
283
284   // Sections for DWARF5 split dwarf proposal.
285   DWARFSection InfoDWOSection;
286   TypeSectionMap TypesDWOSections;
287   StringRef AbbrevDWOSection;
288   DWARFSection LineDWOSection;
289   DWARFSection LocDWOSection;
290   StringRef StringDWOSection;
291   StringRef StringOffsetDWOSection;
292   DWARFSection RangeDWOSection;
293   StringRef AddrSection;
294   DWARFSection AppleNamesSection;
295   DWARFSection AppleTypesSection;
296   DWARFSection AppleNamespacesSection;
297   DWARFSection AppleObjCSection;
298   StringRef CUIndexSection;
299   StringRef GdbIndexSection;
300   StringRef TUIndexSection;
301
302   SmallVector<SmallString<32>, 4> UncompressedSections;
303
304   StringRef *MapSectionToMember(StringRef Name);
305
306   /// If Sec is compressed section, decompresses and updates its contents
307   /// provided by Data. Otherwise leaves it unchanged.
308   Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
309                         StringRef &Data);
310
311 public:
312   DWARFContextInMemory(const object::ObjectFile &Obj,
313     const LoadedObjectInfo *L = nullptr);
314
315   DWARFContextInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
316                        uint8_t AddrSize,
317                        bool isLittleEndian = sys::IsLittleEndianHost);
318
319   bool isLittleEndian() const override { return IsLittleEndian; }
320   uint8_t getAddressSize() const override { return AddressSize; }
321   const DWARFSection &getInfoSection() override { return InfoSection; }
322   const TypeSectionMap &getTypesSections() override { return TypesSections; }
323   StringRef getAbbrevSection() override { return AbbrevSection; }
324   const DWARFSection &getLocSection() override { return LocSection; }
325   StringRef getARangeSection() override { return ARangeSection; }
326   StringRef getDebugFrameSection() override { return DebugFrameSection; }
327   StringRef getEHFrameSection() override { return EHFrameSection; }
328   const DWARFSection &getLineSection() override { return LineSection; }
329   StringRef getStringSection() override { return StringSection; }
330   const DWARFSection &getRangeSection() override { return RangeSection; }
331   StringRef getMacinfoSection() override { return MacinfoSection; }
332   StringRef getPubNamesSection() override { return PubNamesSection; }
333   StringRef getPubTypesSection() override { return PubTypesSection; }
334   StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
335   StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
336   const DWARFSection& getAppleNamesSection() override { return AppleNamesSection; }
337   const DWARFSection& getAppleTypesSection() override { return AppleTypesSection; }
338   const DWARFSection& getAppleNamespacesSection() override { return AppleNamespacesSection; }
339   const DWARFSection& getAppleObjCSection() override { return AppleObjCSection; }
340
341   // Sections for DWARF5 split dwarf proposal.
342   const DWARFSection &getInfoDWOSection() override { return InfoDWOSection; }
343
344   const TypeSectionMap &getTypesDWOSections() override {
345     return TypesDWOSections;
346   }
347
348   StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
349   const DWARFSection &getLineDWOSection() override { return LineDWOSection; }
350   const DWARFSection &getLocDWOSection() override { return LocDWOSection; }
351   StringRef getStringDWOSection() override { return StringDWOSection; }
352
353   StringRef getStringOffsetDWOSection() override {
354     return StringOffsetDWOSection;
355   }
356
357   const DWARFSection &getRangeDWOSection() override { return RangeDWOSection; }
358
359   StringRef getAddrSection() override {
360     return AddrSection;
361   }
362
363   StringRef getCUIndexSection() override { return CUIndexSection; }
364   StringRef getGdbIndexSection() override { return GdbIndexSection; }
365   StringRef getTUIndexSection() override { return TUIndexSection; }
366 };
367
368 } // end namespace llvm
369
370 #endif // LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H