08006d0d5e5be85439ad47db0349e5ee99ddbfc8
[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/OwningPtr.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/MapVector.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   OwningPtr<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 public:
57   struct Section {
58     StringRef Data;
59     RelocAddrMap Relocs;
60   };
61
62   DWARFContext() : DIContext(CK_DWARF) {}
63   virtual ~DWARFContext();
64
65   static bool classof(const DIContext *DICtx) {
66     return DICtx->getKind() == CK_DWARF;
67   }
68
69   virtual void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All);
70
71   /// Get the number of compile units in this context.
72   unsigned getNumCompileUnits() {
73     if (CUs.empty())
74       parseCompileUnits();
75     return CUs.size();
76   }
77
78   /// Get the number of compile units in this context.
79   unsigned getNumTypeUnits() {
80     if (TUs.empty())
81       parseTypeUnits();
82     return TUs.size();
83   }
84
85   /// Get the number of compile units in the DWO context.
86   unsigned getNumDWOCompileUnits() {
87     if (DWOCUs.empty())
88       parseDWOCompileUnits();
89     return DWOCUs.size();
90   }
91
92   /// Get the compile unit at the specified index for this compile unit.
93   DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
94     if (CUs.empty())
95       parseCompileUnits();
96     return CUs[index];
97   }
98
99   /// Get the type unit at the specified index for this compile unit.
100   DWARFTypeUnit *getTypeUnitAtIndex(unsigned index) {
101     if (TUs.empty())
102       parseTypeUnits();
103     return TUs[index];
104   }
105
106   /// Get the compile unit at the specified index for the DWO compile units.
107   DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
108     if (DWOCUs.empty())
109       parseDWOCompileUnits();
110     return DWOCUs[index];
111   }
112
113   /// Get a pointer to the parsed DebugAbbrev object.
114   const DWARFDebugAbbrev *getDebugAbbrev();
115
116   /// Get a pointer to the parsed DebugLoc object.
117   const DWARFDebugLoc *getDebugLoc();
118
119   /// Get a pointer to the parsed dwo abbreviations object.
120   const DWARFDebugAbbrev *getDebugAbbrevDWO();
121
122   /// Get a pointer to the parsed DebugAranges object.
123   const DWARFDebugAranges *getDebugAranges();
124
125   /// Get a pointer to the parsed frame information object.
126   const DWARFDebugFrame *getDebugFrame();
127
128   /// Get a pointer to a parsed line table corresponding to a compile unit.
129   const DWARFDebugLine::LineTable *
130   getLineTableForCompileUnit(DWARFCompileUnit *cu);
131
132   virtual DILineInfo getLineInfoForAddress(uint64_t Address,
133       DILineInfoSpecifier Specifier = DILineInfoSpecifier());
134   virtual DILineInfoTable getLineInfoForAddressRange(uint64_t Address,
135       uint64_t Size, DILineInfoSpecifier Specifier = DILineInfoSpecifier());
136   virtual DIInliningInfo getInliningInfoForAddress(uint64_t Address,
137       DILineInfoSpecifier Specifier = DILineInfoSpecifier());
138
139   virtual bool isLittleEndian() const = 0;
140   virtual uint8_t getAddressSize() const = 0;
141   virtual const Section &getInfoSection() = 0;
142   typedef MapVector<object::SectionRef, Section,
143                     std::map<object::SectionRef, unsigned> > TypeSectionMap;
144   virtual const TypeSectionMap &getTypesSections() = 0;
145   virtual StringRef getAbbrevSection() = 0;
146   virtual const Section &getLocSection() = 0;
147   virtual StringRef getARangeSection() = 0;
148   virtual StringRef getDebugFrameSection() = 0;
149   virtual const Section &getLineSection() = 0;
150   virtual StringRef getStringSection() = 0;
151   virtual StringRef getRangeSection() = 0;
152   virtual StringRef getPubNamesSection() = 0;
153   virtual StringRef getPubTypesSection() = 0;
154   virtual StringRef getGnuPubNamesSection() = 0;
155   virtual StringRef getGnuPubTypesSection() = 0;
156
157   // Sections for DWARF5 split dwarf proposal.
158   virtual const Section &getInfoDWOSection() = 0;
159   virtual StringRef getAbbrevDWOSection() = 0;
160   virtual StringRef getStringDWOSection() = 0;
161   virtual StringRef getStringOffsetDWOSection() = 0;
162   virtual StringRef getRangeDWOSection() = 0;
163   virtual StringRef getAddrSection() = 0;
164
165   static bool isSupportedVersion(unsigned version) {
166     return version == 2 || version == 3 || version == 4;
167   }
168 private:
169   /// Return the compile unit that includes an offset (relative to .debug_info).
170   DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
171
172   /// Return the compile unit which contains instruction with provided
173   /// address.
174   DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
175 };
176
177 /// DWARFContextInMemory is the simplest possible implementation of a
178 /// DWARFContext. It assumes all content is available in memory and stores
179 /// pointers to it.
180 class DWARFContextInMemory : public DWARFContext {
181   virtual void anchor();
182   bool IsLittleEndian;
183   uint8_t AddressSize;
184   Section InfoSection;
185   TypeSectionMap TypesSections;
186   StringRef AbbrevSection;
187   Section LocSection;
188   StringRef ARangeSection;
189   StringRef DebugFrameSection;
190   Section LineSection;
191   StringRef StringSection;
192   StringRef RangeSection;
193   StringRef PubNamesSection;
194   StringRef PubTypesSection;
195   StringRef GnuPubNamesSection;
196   StringRef GnuPubTypesSection;
197
198   // Sections for DWARF5 split dwarf proposal.
199   Section InfoDWOSection;
200   StringRef AbbrevDWOSection;
201   StringRef StringDWOSection;
202   StringRef StringOffsetDWOSection;
203   StringRef RangeDWOSection;
204   StringRef AddrSection;
205
206   SmallVector<MemoryBuffer*, 4> UncompressedSections;
207
208 public:
209   DWARFContextInMemory(object::ObjectFile *);
210   ~DWARFContextInMemory();
211   virtual bool isLittleEndian() const { return IsLittleEndian; }
212   virtual uint8_t getAddressSize() const { return AddressSize; }
213   virtual const Section &getInfoSection() { return InfoSection; }
214   virtual const TypeSectionMap &getTypesSections() { return TypesSections; }
215   virtual StringRef getAbbrevSection() { return AbbrevSection; }
216   virtual const Section &getLocSection() { return LocSection; }
217   virtual StringRef getARangeSection() { return ARangeSection; }
218   virtual StringRef getDebugFrameSection() { return DebugFrameSection; }
219   virtual const Section &getLineSection() { return LineSection; }
220   virtual StringRef getStringSection() { return StringSection; }
221   virtual StringRef getRangeSection() { return RangeSection; }
222   virtual StringRef getPubNamesSection() { return PubNamesSection; }
223   virtual StringRef getPubTypesSection() { return PubTypesSection; }
224   virtual StringRef getGnuPubNamesSection() { return GnuPubNamesSection; }
225   virtual StringRef getGnuPubTypesSection() { return GnuPubTypesSection; }
226
227   // Sections for DWARF5 split dwarf proposal.
228   virtual const Section &getInfoDWOSection() { return InfoDWOSection; }
229   virtual StringRef getAbbrevDWOSection() { return AbbrevDWOSection; }
230   virtual StringRef getStringDWOSection() { return StringDWOSection; }
231   virtual StringRef getStringOffsetDWOSection() {
232     return StringOffsetDWOSection;
233   }
234   virtual StringRef getRangeDWOSection() { return RangeDWOSection; }
235   virtual StringRef getAddrSection() {
236     return AddrSection;
237   }
238 };
239
240 }
241
242 #endif