LLVM  3.7.0
DWARFContext.h
Go to the documentation of this file.
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_LIB_DEBUGINFO_DWARFCONTEXT_H
11 #define LLVM_LIB_DEBUGINFO_DWARFCONTEXT_H
12 
13 #include "llvm/ADT/MapVector.h"
14 #include "llvm/ADT/SmallVector.h"
24 #include <vector>
25 
26 namespace llvm {
27 
28 // In place of applying the relocations to the data we've read from disk we use
29 // a separate mapping table to the side and checking that at locations in the
30 // dwarf where we expect relocated values. This adds a bit of complexity to the
31 // dwarf parsing/extraction at the benefit of not allocating memory for the
32 // entire size of the debug info sections.
34 
35 /// DWARFContext
36 /// This data structure is the top level entity that deals with dwarf debug
37 /// information parsing. The actual data is supplied through pure virtual
38 /// methods that a concrete implementation provides.
39 class DWARFContext : public DIContext {
40 
42  std::vector<DWARFUnitSection<DWARFTypeUnit>> TUs;
43  std::unique_ptr<DWARFDebugAbbrev> Abbrev;
44  std::unique_ptr<DWARFDebugLoc> Loc;
45  std::unique_ptr<DWARFDebugAranges> Aranges;
46  std::unique_ptr<DWARFDebugLine> Line;
47  std::unique_ptr<DWARFDebugFrame> DebugFrame;
48 
50  std::vector<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
51  std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
52  std::unique_ptr<DWARFDebugLocDWO> LocDWO;
53 
54  DWARFContext(DWARFContext &) = delete;
55  DWARFContext &operator=(DWARFContext &) = delete;
56 
57  /// Read compile units from the debug_info section (if necessary)
58  /// and store them in CUs.
59  void parseCompileUnits();
60 
61  /// Read type units from the debug_types sections (if necessary)
62  /// and store them in TUs.
63  void parseTypeUnits();
64 
65  /// Read compile units from the debug_info.dwo section (if necessary)
66  /// and store them in DWOCUs.
67  void parseDWOCompileUnits();
68 
69  /// Read type units from the debug_types.dwo section (if necessary)
70  /// and store them in DWOTUs.
71  void parseDWOTypeUnits();
72 
73 public:
75 
76  static bool classof(const DIContext *DICtx) {
77  return DICtx->getKind() == CK_DWARF;
78  }
79 
80  void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
81 
85 
86  /// Get compile units in this context.
88  parseCompileUnits();
89  return cu_iterator_range(CUs.begin(), CUs.end());
90  }
91 
92  /// Get type units in this context.
94  parseTypeUnits();
95  return tu_section_iterator_range(TUs.begin(), TUs.end());
96  }
97 
98  /// Get compile units in the DWO context.
100  parseDWOCompileUnits();
101  return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
102  }
103 
104  /// Get type units in the DWO context.
106  parseDWOTypeUnits();
107  return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
108  }
109 
110  /// Get the number of compile units in this context.
111  unsigned getNumCompileUnits() {
112  parseCompileUnits();
113  return CUs.size();
114  }
115 
116  /// Get the number of compile units in this context.
117  unsigned getNumTypeUnits() {
118  parseTypeUnits();
119  return TUs.size();
120  }
121 
122  /// Get the number of compile units in the DWO context.
124  parseDWOCompileUnits();
125  return DWOCUs.size();
126  }
127 
128  /// Get the number of compile units in the DWO context.
129  unsigned getNumDWOTypeUnits() {
130  parseDWOTypeUnits();
131  return DWOTUs.size();
132  }
133 
134  /// Get the compile unit at the specified index for this compile unit.
136  parseCompileUnits();
137  return CUs[index].get();
138  }
139 
140  /// Get the compile unit at the specified index for the DWO compile units.
142  parseDWOCompileUnits();
143  return DWOCUs[index].get();
144  }
145 
146  /// Get a pointer to the parsed DebugAbbrev object.
148 
149  /// Get a pointer to the parsed DebugLoc object.
150  const DWARFDebugLoc *getDebugLoc();
151 
152  /// Get a pointer to the parsed dwo abbreviations object.
154 
155  /// Get a pointer to the parsed DebugLoc object.
157 
158  /// Get a pointer to the parsed DebugAranges object.
160 
161  /// Get a pointer to the parsed frame information object.
163 
164  /// Get a pointer to a parsed line table corresponding to a compile unit.
166 
168  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
169  DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
170  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
171  DIInliningInfo getInliningInfoForAddress(uint64_t Address,
172  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
173 
174  virtual bool isLittleEndian() const = 0;
175  virtual uint8_t getAddressSize() const = 0;
176  virtual const DWARFSection &getInfoSection() = 0;
178  std::map<object::SectionRef, unsigned>> TypeSectionMap;
179  virtual const TypeSectionMap &getTypesSections() = 0;
180  virtual StringRef getAbbrevSection() = 0;
181  virtual const DWARFSection &getLocSection() = 0;
182  virtual StringRef getARangeSection() = 0;
183  virtual StringRef getDebugFrameSection() = 0;
184  virtual const DWARFSection &getLineSection() = 0;
185  virtual StringRef getStringSection() = 0;
186  virtual StringRef getRangeSection() = 0;
187  virtual StringRef getPubNamesSection() = 0;
188  virtual StringRef getPubTypesSection() = 0;
189  virtual StringRef getGnuPubNamesSection() = 0;
190  virtual StringRef getGnuPubTypesSection() = 0;
191 
192  // Sections for DWARF5 split dwarf proposal.
193  virtual const DWARFSection &getInfoDWOSection() = 0;
194  virtual const TypeSectionMap &getTypesDWOSections() = 0;
195  virtual StringRef getAbbrevDWOSection() = 0;
196  virtual const DWARFSection &getLineDWOSection() = 0;
197  virtual const DWARFSection &getLocDWOSection() = 0;
198  virtual StringRef getStringDWOSection() = 0;
199  virtual StringRef getStringOffsetDWOSection() = 0;
200  virtual StringRef getRangeDWOSection() = 0;
201  virtual StringRef getAddrSection() = 0;
202  virtual const DWARFSection& getAppleNamesSection() = 0;
203  virtual const DWARFSection& getAppleTypesSection() = 0;
204  virtual const DWARFSection& getAppleNamespacesSection() = 0;
205  virtual const DWARFSection& getAppleObjCSection() = 0;
206 
207  static bool isSupportedVersion(unsigned version) {
208  return version == 2 || version == 3 || version == 4;
209  }
210 private:
211  /// Return the compile unit that includes an offset (relative to .debug_info).
212  DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
213 
214  /// Return the compile unit which contains instruction with provided
215  /// address.
216  DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
217 };
218 
219 /// DWARFContextInMemory is the simplest possible implementation of a
220 /// DWARFContext. It assumes all content is available in memory and stores
221 /// pointers to it.
223  virtual void anchor();
224  bool IsLittleEndian;
225  uint8_t AddressSize;
226  DWARFSection InfoSection;
227  TypeSectionMap TypesSections;
228  StringRef AbbrevSection;
229  DWARFSection LocSection;
230  StringRef ARangeSection;
231  StringRef DebugFrameSection;
232  DWARFSection LineSection;
233  StringRef StringSection;
234  StringRef RangeSection;
235  StringRef PubNamesSection;
236  StringRef PubTypesSection;
237  StringRef GnuPubNamesSection;
238  StringRef GnuPubTypesSection;
239 
240  // Sections for DWARF5 split dwarf proposal.
241  DWARFSection InfoDWOSection;
242  TypeSectionMap TypesDWOSections;
243  StringRef AbbrevDWOSection;
244  DWARFSection LineDWOSection;
245  DWARFSection LocDWOSection;
246  StringRef StringDWOSection;
247  StringRef StringOffsetDWOSection;
248  StringRef RangeDWOSection;
249  StringRef AddrSection;
250  DWARFSection AppleNamesSection;
251  DWARFSection AppleTypesSection;
252  DWARFSection AppleNamespacesSection;
253  DWARFSection AppleObjCSection;
254 
255  SmallVector<SmallString<32>, 4> UncompressedSections;
256 
257 public:
259  const LoadedObjectInfo *L = nullptr);
260  bool isLittleEndian() const override { return IsLittleEndian; }
261  uint8_t getAddressSize() const override { return AddressSize; }
262  const DWARFSection &getInfoSection() override { return InfoSection; }
263  const TypeSectionMap &getTypesSections() override { return TypesSections; }
264  StringRef getAbbrevSection() override { return AbbrevSection; }
265  const DWARFSection &getLocSection() override { return LocSection; }
266  StringRef getARangeSection() override { return ARangeSection; }
267  StringRef getDebugFrameSection() override { return DebugFrameSection; }
268  const DWARFSection &getLineSection() override { return LineSection; }
269  StringRef getStringSection() override { return StringSection; }
270  StringRef getRangeSection() override { return RangeSection; }
271  StringRef getPubNamesSection() override { return PubNamesSection; }
272  StringRef getPubTypesSection() override { return PubTypesSection; }
273  StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
274  StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
275  const DWARFSection& getAppleNamesSection() override { return AppleNamesSection; }
276  const DWARFSection& getAppleTypesSection() override { return AppleTypesSection; }
277  const DWARFSection& getAppleNamespacesSection() override { return AppleNamespacesSection; }
278  const DWARFSection& getAppleObjCSection() override { return AppleObjCSection; }
279 
280  // Sections for DWARF5 split dwarf proposal.
281  const DWARFSection &getInfoDWOSection() override { return InfoDWOSection; }
283  return TypesDWOSections;
284  }
285  StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
286  const DWARFSection &getLineDWOSection() override { return LineDWOSection; }
287  const DWARFSection &getLocDWOSection() override { return LocDWOSection; }
288  StringRef getStringDWOSection() override { return StringDWOSection; }
290  return StringOffsetDWOSection;
291  }
292  StringRef getRangeDWOSection() override { return RangeDWOSection; }
294  return AddrSection;
295  }
296 };
297 
298 }
299 
300 #endif
virtual StringRef getAbbrevSection()=0
StringRef getPubTypesSection() override
Definition: DWARFContext.h:272
virtual StringRef getAddrSection()=0
DWARFCompileUnit * getDWOCompileUnitAtIndex(unsigned index)
Get the compile unit at the specified index for the DWO compile units.
Definition: DWARFContext.h:141
StringRef getStringOffsetDWOSection() override
Definition: DWARFContext.h:289
const DWARFDebugFrame * getDebugFrame()
Get a pointer to the parsed frame information object.
A parsed .debug_frame section.
const DWARFDebugLocDWO * getDebugLocDWO()
Get a pointer to the parsed DebugLoc object.
StringRef getDebugFrameSection() override
Definition: DWARFContext.h:267
const DWARFDebugLoc * getDebugLoc()
Get a pointer to the parsed DebugLoc object.
virtual StringRef getStringOffsetDWOSection()=0
virtual StringRef getRangeDWOSection()=0
virtual const DWARFSection & getLineSection()=0
virtual const DWARFSection & getAppleTypesSection()=0
DWARFUnitSection< DWARFCompileUnit >::iterator_range cu_iterator_range
Definition: DWARFContext.h:82
unsigned getNumDWOCompileUnits()
Get the number of compile units in the DWO context.
Definition: DWARFContext.h:123
This class is the base class for all object file types.
Definition: ObjectFile.h:176
StringRef getRangeSection() override
Definition: DWARFContext.h:270
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:32
unsigned getNumTypeUnits()
Get the number of compile units in this context.
Definition: DWARFContext.h:117
const DWARFDebugAbbrev * getDebugAbbrevDWO()
Get a pointer to the parsed dwo abbreviations object.
virtual StringRef getStringDWOSection()=0
virtual uint8_t getAddressSize() const =0
virtual const DWARFSection & getAppleNamesSection()=0
bool isLittleEndian() const override
Definition: DWARFContext.h:260
virtual const DWARFSection & getInfoDWOSection()=0
const DWARFSection & getLocSection() override
Definition: DWARFContext.h:265
virtual const DWARFSection & getAppleObjCSection()=0
StringRef getGnuPubTypesSection() override
Definition: DWARFContext.h:274
virtual StringRef getGnuPubTypesSection()=0
virtual const TypeSectionMap & getTypesDWOSections()=0
DILineInfo - a format-neutral container for source line information.
Definition: DIContext.h:31
DIContextKind getKind() const
Definition: DIContext.h:122
const DWARFSection & getInfoDWOSection() override
Definition: DWARFContext.h:281
StringRef getARangeSection() override
Definition: DWARFContext.h:266
void dump(raw_ostream &OS, DIDumpType DumpType=DIDT_All) override
virtual const DWARFSection & getLocSection()=0
unsigned getNumCompileUnits()
Get the number of compile units in this context.
Definition: DWARFContext.h:111
virtual StringRef getPubTypesSection()=0
unsigned getNumDWOTypeUnits()
Get the number of compile units in the DWO context.
Definition: DWARFContext.h:129
StringRef getGnuPubNamesSection() override
Definition: DWARFContext.h:273
const DWARFSection & getLocDWOSection() override
Definition: DWARFContext.h:287
virtual StringRef getRangeSection()=0
const DWARFDebugAranges * getDebugAranges()
Get a pointer to the parsed DebugAranges object.
const DWARFDebugAbbrev * getDebugAbbrev()
Get a pointer to the parsed DebugAbbrev object.
DWARFCompileUnit * getCompileUnitAtIndex(unsigned index)
Get the compile unit at the specified index for this compile unit.
Definition: DWARFContext.h:135
DWARFContextInMemory is the simplest possible implementation of a DWARFContext.
Definition: DWARFContext.h:222
StringRef getAbbrevDWOSection() override
Definition: DWARFContext.h:285
StringRef getAbbrevSection() override
Definition: DWARFContext.h:264
virtual StringRef getPubNamesSection()=0
const TypeSectionMap & getTypesDWOSections() override
Definition: DWARFContext.h:282
virtual const TypeSectionMap & getTypesSections()=0
DILineInfoSpecifier - controls which fields of DILineInfo container should be filled with data...
Definition: DIContext.h:74
DIInliningInfo - a format-neutral container for inlined code description.
Definition: DIContext.h:52
DIDumpType
Selects which debug sections get dumped.
Definition: DIContext.h:87
DILineInfo getLineInfoForAddress(uint64_t Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
const DWARFSection & getAppleNamespacesSection() override
Definition: DWARFContext.h:277
const TypeSectionMap & getTypesSections() override
Definition: DWARFContext.h:263
StringRef getStringSection() override
Definition: DWARFContext.h:269
StringRef getRangeDWOSection() override
Definition: DWARFContext.h:292
StringRef getAddrSection() override
Definition: DWARFContext.h:293
MapVector< object::SectionRef, DWARFSection, std::map< object::SectionRef, unsigned > > TypeSectionMap
Definition: DWARFContext.h:178
iterator_range< std::vector< DWARFUnitSection< DWARFTypeUnit > >::iterator > tu_section_iterator_range
Definition: DWARFContext.h:84
const DWARFSection & getAppleTypesSection() override
Definition: DWARFContext.h:276
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Concrete instance of DWARFUnitSection, specialized for one Unit type.
Definition: DWARFUnit.h:54
DIInliningInfo getInliningInfoForAddress(uint64_t Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:39
const DWARFSection & getLineDWOSection() override
Definition: DWARFContext.h:286
tu_section_iterator_range dwo_type_unit_sections()
Get type units in the DWO context.
Definition: DWARFContext.h:105
StringRef getPubNamesSection() override
Definition: DWARFContext.h:271
A range adaptor for a pair of iterators.
DWARFUnitSection< DWARFTypeUnit >::iterator_range tu_iterator_range
Definition: DWARFContext.h:83
An inferface for inquiring the load address of a loaded object file to be used by the DIContext imple...
Definition: DIContext.h:142
DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
virtual const DWARFSection & getInfoSection()=0
virtual StringRef getAbbrevDWOSection()=0
static bool isSupportedVersion(unsigned version)
Definition: DWARFContext.h:207
StringRef getStringDWOSection() override
Definition: DWARFContext.h:288
DWARFContextInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L=nullptr)
cu_iterator_range compile_units()
Get compile units in this context.
Definition: DWARFContext.h:87
const DWARFSection & getInfoSection() override
Definition: DWARFContext.h:262
virtual StringRef getARangeSection()=0
static bool classof(const DIContext *DICtx)
Definition: DWARFContext.h:76
cu_iterator_range dwo_compile_units()
Get compile units in the DWO context.
Definition: DWARFContext.h:99
virtual const DWARFSection & getLineDWOSection()=0
virtual const DWARFSection & getAppleNamespacesSection()=0
const DWARFDebugLine::LineTable * getLineTableForUnit(DWARFUnit *cu)
Get a pointer to a parsed line table corresponding to a compile unit.
virtual const DWARFSection & getLocDWOSection()=0
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
virtual StringRef getStringSection()=0
const DWARFSection & getLineSection() override
Definition: DWARFContext.h:268
tu_section_iterator_range type_unit_sections()
Get type units in this context.
Definition: DWARFContext.h:93
virtual StringRef getDebugFrameSection()=0
virtual StringRef getGnuPubNamesSection()=0
DenseMap< uint64_t, std::pair< uint8_t, int64_t > > RelocAddrMap
Definition: DWARFContext.h:33
virtual bool isLittleEndian() const =0
const DWARFSection & getAppleNamesSection() override
Definition: DWARFContext.h:275
const DWARFSection & getAppleObjCSection() override
Definition: DWARFContext.h:278
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:69
uint8_t getAddressSize() const override
Definition: DWARFContext.h:261