Replace OwningPtr<T> with std::unique_ptr<T>.
[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_DEBUGINFO_DWARFCONTEXT_H
11 #define LLVM_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 "DWARFTypeUnit.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/DebugInfo/DIContext.h"
23
24 namespace llvm {
25
26 /// DWARFContext
27 /// This data structure is the top level entity that deals with dwarf debug
28 /// information parsing. The actual data is supplied through pure virtual
29 /// methods that a concrete implementation provides.
30 class DWARFContext : public DIContext {
31   SmallVector<DWARFCompileUnit *, 1> CUs;
32   SmallVector<DWARFTypeUnit *, 1> TUs;
33   std::unique_ptr<DWARFDebugAbbrev> Abbrev;
34   std::unique_ptr<DWARFDebugLoc> Loc;
35   std::unique_ptr<DWARFDebugAranges> Aranges;
36   std::unique_ptr<DWARFDebugLine> Line;
37   std::unique_ptr<DWARFDebugFrame> DebugFrame;
38
39   SmallVector<DWARFCompileUnit *, 1> DWOCUs;
40   SmallVector<DWARFTypeUnit *, 1> DWOTUs;
41   std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
42
43   DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
44   DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
45
46   /// Read compile units from the debug_info section and store them in CUs.
47   void parseCompileUnits();
48
49   /// Read type units from the debug_types sections and store them in CUs.
50   void parseTypeUnits();
51
52   /// Read compile units from the debug_info.dwo section and store them in
53   /// DWOCUs.
54   void parseDWOCompileUnits();
55
56   /// Read type units from the debug_types.dwo section and store them in
57   /// DWOTUs.
58   void parseDWOTypeUnits();
59
60 public:
61   struct Section {
62     StringRef Data;
63     RelocAddrMap Relocs;
64   };
65
66   DWARFContext() : DIContext(CK_DWARF) {}
67   virtual ~DWARFContext();
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   /// Get the number of compile units in this context.
76   unsigned getNumCompileUnits() {
77     if (CUs.empty())
78       parseCompileUnits();
79     return CUs.size();
80   }
81
82   /// Get the number of compile units in this context.
83   unsigned getNumTypeUnits() {
84     if (TUs.empty())
85       parseTypeUnits();
86     return TUs.size();
87   }
88
89   /// Get the number of compile units in the DWO context.
90   unsigned getNumDWOCompileUnits() {
91     if (DWOCUs.empty())
92       parseDWOCompileUnits();
93     return DWOCUs.size();
94   }
95
96   /// Get the number of compile units in the DWO context.
97   unsigned getNumDWOTypeUnits() {
98     if (DWOTUs.empty())
99       parseDWOTypeUnits();
100     return DWOTUs.size();
101   }
102
103   /// Get the compile unit at the specified index for this compile unit.
104   DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
105     if (CUs.empty())
106       parseCompileUnits();
107     return CUs[index];
108   }
109
110   /// Get the type unit at the specified index for this compile unit.
111   DWARFTypeUnit *getTypeUnitAtIndex(unsigned index) {
112     if (TUs.empty())
113       parseTypeUnits();
114     return TUs[index];
115   }
116
117   /// Get the compile unit at the specified index for the DWO compile units.
118   DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
119     if (DWOCUs.empty())
120       parseDWOCompileUnits();
121     return DWOCUs[index];
122   }
123
124   /// Get the type unit at the specified index for the DWO type units.
125   DWARFTypeUnit *getDWOTypeUnitAtIndex(unsigned index) {
126     if (DWOTUs.empty())
127       parseDWOTypeUnits();
128     return DWOTUs[index];
129   }
130
131   /// Get a pointer to the parsed DebugAbbrev object.
132   const DWARFDebugAbbrev *getDebugAbbrev();
133
134   /// Get a pointer to the parsed DebugLoc object.
135   const DWARFDebugLoc *getDebugLoc();
136
137   /// Get a pointer to the parsed dwo abbreviations object.
138   const DWARFDebugAbbrev *getDebugAbbrevDWO();
139
140   /// Get a pointer to the parsed DebugAranges object.
141   const DWARFDebugAranges *getDebugAranges();
142
143   /// Get a pointer to the parsed frame information object.
144   const DWARFDebugFrame *getDebugFrame();
145
146   /// Get a pointer to a parsed line table corresponding to a compile unit.
147   const DWARFDebugLine::LineTable *
148   getLineTableForCompileUnit(DWARFCompileUnit *cu);
149
150   DILineInfo getLineInfoForAddress(uint64_t Address,
151       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
152   DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
153       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
154   DIInliningInfo getInliningInfoForAddress(uint64_t Address,
155       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
156
157   virtual bool isLittleEndian() const = 0;
158   virtual uint8_t getAddressSize() const = 0;
159   virtual const Section &getInfoSection() = 0;
160   typedef MapVector<object::SectionRef, Section,
161                     std::map<object::SectionRef, unsigned> > TypeSectionMap;
162   virtual const TypeSectionMap &getTypesSections() = 0;
163   virtual StringRef getAbbrevSection() = 0;
164   virtual const Section &getLocSection() = 0;
165   virtual StringRef getARangeSection() = 0;
166   virtual StringRef getDebugFrameSection() = 0;
167   virtual const Section &getLineSection() = 0;
168   virtual const Section &getLineDWOSection() = 0;
169   virtual StringRef getStringSection() = 0;
170   virtual StringRef getRangeSection() = 0;
171   virtual StringRef getPubNamesSection() = 0;
172   virtual StringRef getPubTypesSection() = 0;
173   virtual StringRef getGnuPubNamesSection() = 0;
174   virtual StringRef getGnuPubTypesSection() = 0;
175
176   // Sections for DWARF5 split dwarf proposal.
177   virtual const Section &getInfoDWOSection() = 0;
178   virtual const TypeSectionMap &getTypesDWOSections() = 0;
179   virtual StringRef getAbbrevDWOSection() = 0;
180   virtual StringRef getStringDWOSection() = 0;
181   virtual StringRef getStringOffsetDWOSection() = 0;
182   virtual StringRef getRangeDWOSection() = 0;
183   virtual StringRef getAddrSection() = 0;
184
185   static bool isSupportedVersion(unsigned version) {
186     return version == 2 || version == 3 || version == 4;
187   }
188 private:
189   /// Return the compile unit that includes an offset (relative to .debug_info).
190   DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
191
192   /// Return the compile unit which contains instruction with provided
193   /// address.
194   DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
195 };
196
197 /// DWARFContextInMemory is the simplest possible implementation of a
198 /// DWARFContext. It assumes all content is available in memory and stores
199 /// pointers to it.
200 class DWARFContextInMemory : public DWARFContext {
201   virtual void anchor();
202   bool IsLittleEndian;
203   uint8_t AddressSize;
204   Section InfoSection;
205   TypeSectionMap TypesSections;
206   StringRef AbbrevSection;
207   Section LocSection;
208   StringRef ARangeSection;
209   StringRef DebugFrameSection;
210   Section LineSection;
211   Section LineDWOSection;
212   StringRef StringSection;
213   StringRef RangeSection;
214   StringRef PubNamesSection;
215   StringRef PubTypesSection;
216   StringRef GnuPubNamesSection;
217   StringRef GnuPubTypesSection;
218
219   // Sections for DWARF5 split dwarf proposal.
220   Section InfoDWOSection;
221   TypeSectionMap TypesDWOSections;
222   StringRef AbbrevDWOSection;
223   StringRef StringDWOSection;
224   StringRef StringOffsetDWOSection;
225   StringRef RangeDWOSection;
226   StringRef AddrSection;
227
228   SmallVector<MemoryBuffer*, 4> UncompressedSections;
229
230 public:
231   DWARFContextInMemory(object::ObjectFile *);
232   ~DWARFContextInMemory();
233   bool isLittleEndian() const override { return IsLittleEndian; }
234   uint8_t getAddressSize() const override { return AddressSize; }
235   const Section &getInfoSection() override { return InfoSection; }
236   const TypeSectionMap &getTypesSections() override { return TypesSections; }
237   StringRef getAbbrevSection() override { return AbbrevSection; }
238   const Section &getLocSection() override { return LocSection; }
239   StringRef getARangeSection() override { return ARangeSection; }
240   StringRef getDebugFrameSection() override { return DebugFrameSection; }
241   const Section &getLineSection() override { return LineSection; }
242   const Section &getLineDWOSection() override { return LineDWOSection; }
243   StringRef getStringSection() override { return StringSection; }
244   StringRef getRangeSection() override { return RangeSection; }
245   StringRef getPubNamesSection() override { return PubNamesSection; }
246   StringRef getPubTypesSection() override { return PubTypesSection; }
247   StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
248   StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
249
250   // Sections for DWARF5 split dwarf proposal.
251   const Section &getInfoDWOSection() override { return InfoDWOSection; }
252   const TypeSectionMap &getTypesDWOSections() override {
253     return TypesDWOSections;
254   }
255   StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
256   StringRef getStringDWOSection() override { return StringDWOSection; }
257   StringRef getStringOffsetDWOSection() override {
258     return StringOffsetDWOSection;
259   }
260   StringRef getRangeDWOSection() override { return RangeDWOSection; }
261   StringRef getAddrSection() override {
262     return AddrSection;
263   }
264 };
265
266 }
267
268 #endif