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