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