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
24namespace 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.
30class 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
55public:
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 getPubTypesSection() = 0;
151  virtual StringRef getGnuPubNamesSection() = 0;
152  virtual StringRef getGnuPubTypesSection() = 0;
153
154  // Sections for DWARF5 split dwarf proposal.
155  virtual const Section &getInfoDWOSection() = 0;
156  virtual StringRef getAbbrevDWOSection() = 0;
157  virtual StringRef getStringDWOSection() = 0;
158  virtual StringRef getStringOffsetDWOSection() = 0;
159  virtual StringRef getRangeDWOSection() = 0;
160  virtual StringRef getAddrSection() = 0;
161
162  static bool isSupportedVersion(unsigned version) {
163    return version == 2 || version == 3 || version == 4;
164  }
165private:
166  /// Return the compile unit that includes an offset (relative to .debug_info).
167  DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
168
169  /// Return the compile unit which contains instruction with provided
170  /// address.
171  DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
172};
173
174/// DWARFContextInMemory is the simplest possible implementation of a
175/// DWARFContext. It assumes all content is available in memory and stores
176/// pointers to it.
177class DWARFContextInMemory : public DWARFContext {
178  virtual void anchor();
179  bool IsLittleEndian;
180  uint8_t AddressSize;
181  Section InfoSection;
182  std::map<object::SectionRef, Section> TypesSections;
183  StringRef AbbrevSection;
184  Section LocSection;
185  StringRef ARangeSection;
186  StringRef DebugFrameSection;
187  Section LineSection;
188  StringRef StringSection;
189  StringRef RangeSection;
190  StringRef PubNamesSection;
191  StringRef PubTypesSection;
192  StringRef GnuPubNamesSection;
193  StringRef GnuPubTypesSection;
194
195  // Sections for DWARF5 split dwarf proposal.
196  Section InfoDWOSection;
197  StringRef AbbrevDWOSection;
198  StringRef StringDWOSection;
199  StringRef StringOffsetDWOSection;
200  StringRef RangeDWOSection;
201  StringRef AddrSection;
202
203  SmallVector<MemoryBuffer*, 4> UncompressedSections;
204
205public:
206  DWARFContextInMemory(object::ObjectFile *);
207  ~DWARFContextInMemory();
208  virtual bool isLittleEndian() const { return IsLittleEndian; }
209  virtual uint8_t getAddressSize() const { return AddressSize; }
210  virtual const Section &getInfoSection() { return InfoSection; }
211  virtual const std::map<object::SectionRef, Section> &getTypesSections() {
212    return TypesSections;
213  }
214  virtual StringRef getAbbrevSection() { return AbbrevSection; }
215  virtual const Section &getLocSection() { return LocSection; }
216  virtual StringRef getARangeSection() { return ARangeSection; }
217  virtual StringRef getDebugFrameSection() { return DebugFrameSection; }
218  virtual const Section &getLineSection() { return LineSection; }
219  virtual StringRef getStringSection() { return StringSection; }
220  virtual StringRef getRangeSection() { return RangeSection; }
221  virtual StringRef getPubNamesSection() { return PubNamesSection; }
222  virtual StringRef getPubTypesSection() { return PubTypesSection; }
223  virtual StringRef getGnuPubNamesSection() { return GnuPubNamesSection; }
224  virtual StringRef getGnuPubTypesSection() { return GnuPubTypesSection; }
225
226  // Sections for DWARF5 split dwarf proposal.
227  virtual const Section &getInfoDWOSection() { return InfoDWOSection; }
228  virtual StringRef getAbbrevDWOSection() { return AbbrevDWOSection; }
229  virtual StringRef getStringDWOSection() { return StringDWOSection; }
230  virtual StringRef getStringOffsetDWOSection() {
231    return StringOffsetDWOSection;
232  }
233  virtual StringRef getRangeDWOSection() { return RangeDWOSection; }
234  virtual StringRef getAddrSection() {
235    return AddrSection;
236  }
237};
238
239}
240
241#endif
242