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