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