LLVM  5.0.0
DWARFUnit.h
Go to the documentation of this file.
1 //===- DWARFUnit.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_DWARF_DWARFUNIT_H
11 #define LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
12 
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
27 #include <algorithm>
28 #include <cassert>
29 #include <cstddef>
30 #include <cstdint>
31 #include <map>
32 #include <memory>
33 #include <utility>
34 #include <vector>
35 
36 namespace llvm {
37 
38 class DWARFAbbreviationDeclarationSet;
39 class DWARFContext;
40 class DWARFDebugAbbrev;
41 class DWARFUnit;
42 
43 /// Base class for all DWARFUnitSection classes. This provides the
44 /// functionality common to all unit types.
46 public:
47  /// Returns the Unit that contains the given section offset in the
48  /// same section this Unit originated from.
49  virtual DWARFUnit *getUnitForOffset(uint32_t Offset) const = 0;
50 
51  void parse(DWARFContext &C, const DWARFSection &Section);
52  void parseDWO(DWARFContext &C, const DWARFSection &DWOSection,
53  DWARFUnitIndex *Index = nullptr);
54 
55 protected:
56  ~DWARFUnitSectionBase() = default;
57 
58  virtual void parseImpl(DWARFContext &Context, const DWARFSection &Section,
59  const DWARFDebugAbbrev *DA, const DWARFSection *RS,
60  StringRef SS, const DWARFSection &SOS,
61  const DWARFSection *AOS, const DWARFSection &LS,
62  bool isLittleEndian, bool isDWO) = 0;
63 };
64 
67 
68 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
69 template<typename UnitType>
70 class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
71  public DWARFUnitSectionBase {
72  bool Parsed = false;
73 
74 public:
76  using iterator = typename UnitVector::iterator;
78 
80  auto *CU = std::upper_bound(
81  this->begin(), this->end(), Offset,
82  [](uint32_t LHS, const std::unique_ptr<UnitType> &RHS) {
83  return LHS < RHS->getNextUnitOffset();
84  });
85  if (CU != this->end())
86  return CU->get();
87  return nullptr;
88  }
89 
90 private:
91  void parseImpl(DWARFContext &Context, const DWARFSection &Section,
92  const DWARFDebugAbbrev *DA, const DWARFSection *RS,
93  StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS,
94  const DWARFSection &LS, bool LE, bool IsDWO) override {
95  if (Parsed)
96  return;
97  const auto &Index = getDWARFUnitIndex(Context, UnitType::Section);
98  DataExtractor Data(Section.Data, LE, 0);
99  uint32_t Offset = 0;
100  while (Data.isValidOffset(Offset)) {
101  auto U = llvm::make_unique<UnitType>(Context, Section, DA, RS, SS, SOS,
102  AOS, LS, LE, IsDWO, *this,
103  Index.getFromOffset(Offset));
104  if (!U->extract(Data, &Offset))
105  break;
106  this->push_back(std::move(U));
107  Offset = this->back()->getNextUnitOffset();
108  }
109  Parsed = true;
110  }
111 };
112 
113 class DWARFUnit {
114  DWARFContext &Context;
115  /// Section containing this DWARFUnit.
116  const DWARFSection &InfoSection;
117 
118  const DWARFDebugAbbrev *Abbrev;
119  const DWARFSection *RangeSection;
120  uint32_t RangeSectionBase;
121  const DWARFSection &LineSection;
122  StringRef StringSection;
123  const DWARFSection &StringOffsetSection;
124  uint64_t StringOffsetSectionBase = 0;
125  const DWARFSection *AddrOffsetSection;
126  uint32_t AddrOffsetSectionBase;
127  bool isLittleEndian;
128  bool isDWO;
129  const DWARFUnitSectionBase &UnitSection;
130 
131  // Version, address size, and DWARF format.
132  DWARFFormParams FormParams;
133 
134  uint32_t Offset;
135  uint32_t Length;
136  const DWARFAbbreviationDeclarationSet *Abbrevs;
137  uint8_t UnitType;
138  uint64_t BaseAddr;
139  /// The compile unit debug information entry items.
140  std::vector<DWARFDebugInfoEntry> DieArray;
141 
142  /// Map from range's start address to end address and corresponding DIE.
143  /// IntervalMap does not support range removal, as a result, we use the
144  /// std::map::upper_bound for address range lookup.
145  std::map<uint64_t, std::pair<uint64_t, DWARFDie>> AddrDieMap;
146 
147  using die_iterator_range =
149 
150  std::shared_ptr<DWARFUnit> DWO;
151 
152  const DWARFUnitIndex::Entry *IndexEntry;
153 
154  uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) {
155  auto First = DieArray.data();
156  assert(Die >= First && Die < First + DieArray.size());
157  return Die - First;
158  }
159 
160 protected:
161  virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
162 
163  /// Size in bytes of the unit header.
164  virtual uint32_t getHeaderSize() const { return getVersion() <= 4 ? 11 : 12; }
165 
166 public:
167  DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
168  const DWARFDebugAbbrev *DA, const DWARFSection *RS, StringRef SS,
169  const DWARFSection &SOS, const DWARFSection *AOS,
170  const DWARFSection &LS, bool LE, bool IsDWO,
171  const DWARFUnitSectionBase &UnitSection,
172  const DWARFUnitIndex::Entry *IndexEntry = nullptr);
173 
174  virtual ~DWARFUnit();
175 
176  DWARFContext& getContext() const { return Context; }
177 
178  const DWARFSection &getLineSection() const { return LineSection; }
179  StringRef getStringSection() const { return StringSection; }
181  return StringOffsetSection;
182  }
183 
185  AddrOffsetSection = AOS;
186  AddrOffsetSectionBase = Base;
187  }
188 
189  /// Recursively update address to Die map.
190  void updateAddressDieMap(DWARFDie Die);
191 
193  RangeSection = RS;
194  RangeSectionBase = Base;
195  }
196 
197  bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
198  bool getStringOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
199 
201  return DWARFDataExtractor(InfoSection, isLittleEndian,
203  }
204 
206  return DataExtractor(StringSection, false, 0);
207  }
208 
209  const RelocAddrMap *getRelocMap() const { return &InfoSection.Relocs; }
211  return StringOffsetSection.Relocs;
212  }
213 
214  bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
215 
216  /// extractRangeList - extracts the range list referenced by this compile
217  /// unit from .debug_ranges section. Returns true on success.
218  /// Requires that compile unit is already extracted.
219  bool extractRangeList(uint32_t RangeListOffset,
220  DWARFDebugRangeList &RangeList) const;
221  void clear();
222  uint32_t getOffset() const { return Offset; }
223  uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
224  uint32_t getLength() const { return Length; }
225 
226  const DWARFFormParams &getFormParams() const { return FormParams; }
227  uint16_t getVersion() const { return FormParams.Version; }
228  dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
229  uint8_t getAddressByteSize() const { return FormParams.AddrSize; }
230  uint8_t getRefAddrByteSize() const { return FormParams.getRefAddrByteSize(); }
231  uint8_t getDwarfOffsetByteSize() const {
232  return FormParams.getDwarfOffsetByteSize();
233  }
234 
236  return Abbrevs;
237  }
238 
239  uint8_t getUnitType() const { return UnitType; }
240 
241  static bool isValidUnitType(uint8_t UnitType) {
242  return UnitType == dwarf::DW_UT_compile || UnitType == dwarf::DW_UT_type ||
243  UnitType == dwarf::DW_UT_partial ||
244  UnitType == dwarf::DW_UT_skeleton ||
245  UnitType == dwarf::DW_UT_split_compile ||
246  UnitType == dwarf::DW_UT_split_type;
247  }
248 
249  /// \brief Return the number of bytes for the header of a unit of
250  /// UnitType type.
251  ///
252  /// This function must be called with a valid unit type which in
253  /// DWARF5 is defined as one of the following six types.
254  static uint32_t getDWARF5HeaderSize(uint8_t UnitType) {
255  switch (UnitType) {
256  case dwarf::DW_UT_compile:
257  case dwarf::DW_UT_partial:
258  return 12;
259  case dwarf::DW_UT_skeleton:
260  case dwarf::DW_UT_split_compile:
261  return 20;
262  case dwarf::DW_UT_type:
263  case dwarf::DW_UT_split_type:
264  return 24;
265  }
266  llvm_unreachable("Invalid UnitType.");
267  }
268 
269  uint64_t getBaseAddress() const { return BaseAddr; }
270 
271  void setBaseAddress(uint64_t base_addr) {
272  BaseAddr = base_addr;
273  }
274 
275  DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
276  extractDIEsIfNeeded(ExtractUnitDIEOnly);
277  if (DieArray.empty())
278  return DWARFDie();
279  return DWARFDie(this, &DieArray[0]);
280  }
281 
282  const char *getCompilationDir();
284 
286 
287  /// getInlinedChainForAddress - fetches inlined chain for a given address.
288  /// Returns empty chain if there is no subprogram containing address. The
289  /// chain is valid as long as parsed compile unit DIEs are not cleared.
290  void getInlinedChainForAddress(uint64_t Address,
291  SmallVectorImpl<DWARFDie> &InlinedChain);
292 
293  /// getUnitSection - Return the DWARFUnitSection containing this unit.
294  const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
295 
296  /// \brief Returns the number of DIEs in the unit. Parses the unit
297  /// if necessary.
298  unsigned getNumDIEs() {
299  extractDIEsIfNeeded(false);
300  return DieArray.size();
301  }
302 
303  /// \brief Return the index of a DIE inside the unit's DIE vector.
304  ///
305  /// It is illegal to call this method with a DIE that hasn't be
306  /// created by this unit. In other word, it's illegal to call this
307  /// method on a DIE that isn't accessible by following
308  /// children/sibling links starting from this unit's getUnitDIE().
310  return getDIEIndex(D.getDebugInfoEntry());
311  }
312 
313  /// \brief Return the DIE object at the given index.
314  DWARFDie getDIEAtIndex(unsigned Index) {
315  assert(Index < DieArray.size());
316  return DWARFDie(this, &DieArray[Index]);
317  }
318 
321 
322  /// \brief Return the DIE object for a given offset inside the
323  /// unit's DIE vector.
324  ///
325  /// The unit needs to have its DIEs extracted for this method to work.
327  extractDIEsIfNeeded(false);
328  assert(!DieArray.empty());
329  auto it = std::lower_bound(
330  DieArray.begin(), DieArray.end(), Offset,
331  [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) {
332  return LHS.getOffset() < Offset;
333  });
334  if (it != DieArray.end() && it->getOffset() == Offset)
335  return DWARFDie(this, &*it);
336  return DWARFDie();
337  }
338 
340  if (IndexEntry)
341  if (const auto *Contrib = IndexEntry->getOffset(DW_SECT_LINE))
342  return Contrib->Offset;
343  return 0;
344  }
345 
347  extractDIEsIfNeeded(false);
348  return die_iterator_range(DieArray.begin(), DieArray.end());
349  }
350 
351 private:
352  /// Size in bytes of the .debug_info data associated with this compile unit.
353  size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
354 
355  /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
356  /// hasn't already been done. Returns the number of DIEs parsed at this call.
357  size_t extractDIEsIfNeeded(bool CUDieOnly);
358 
359  /// extractDIEsToVector - Appends all parsed DIEs to a vector.
360  void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
361  std::vector<DWARFDebugInfoEntry> &DIEs) const;
362 
363  /// clearDIEs - Clear parsed DIEs to keep memory usage low.
364  void clearDIEs(bool KeepCUDie);
365 
366  /// parseDWO - Parses .dwo file for current compile unit. Returns true if
367  /// it was actually constructed.
368  bool parseDWO();
369 
370  /// getSubroutineForAddress - Returns subprogram DIE with address range
371  /// encompassing the provided address. The pointer is alive as long as parsed
372  /// compile unit DIEs are not cleared.
373  DWARFDie getSubroutineForAddress(uint64_t Address);
374 };
375 
376 } // end namespace llvm
377 
378 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
void getInlinedChainForAddress(uint64_t Address, SmallVectorImpl< DWARFDie > &InlinedChain)
getInlinedChainForAddress - fetches inlined chain for a given address.
Definition: DWARFUnit.cpp:388
virtual ~DWARFUnit()
void push_back(const T &Elt)
Definition: SmallVector.h:211
void parseDWO(DWARFContext &C, const DWARFSection &DWOSection, DWARFUnitIndex *Index=nullptr)
Definition: DWARFUnit.cpp:38
uint8_t getDwarfOffsetByteSize() const
Definition: DWARFUnit.h:231
LLVMContext & Context
const RelocAddrMap & getStringOffsetsRelocMap() const
Definition: DWARFUnit.h:210
void setBaseAddress(uint64_t base_addr)
Definition: DWARFUnit.h:271
uint8_t getUnitType() const
Definition: DWARFUnit.h:239
uint64_t getBaseAddress() const
Definition: DWARFUnit.h:269
RelocAddrMap Relocs
Definition: DWARFSection.h:20
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
void collectAddressRanges(DWARFAddressRangesVector &CURanges)
Definition: DWARFUnit.cpp:313
bool getStringOffsetSectionItem(uint32_t Index, uint64_t &Result) const
Definition: DWARFUnit.cpp:73
virtual uint32_t getHeaderSize() const
Size in bytes of the unit header.
Definition: DWARFUnit.h:164
void setAddrOffsetSection(const DWARFSection *AOS, uint32_t Base)
Definition: DWARFUnit.h:184
DWARFDie getSibling(const DWARFDebugInfoEntry *Die)
Definition: DWARFUnit.cpp:432
bool extractRangeList(uint32_t RangeListOffset, DWARFDebugRangeList &RangeList) const
extractRangeList - extracts the range list referenced by this compile unit from .debug_ranges section...
Definition: DWARFUnit.cpp:140
uint32_t getNextUnitOffset() const
Definition: DWARFUnit.h:223
const DWARFDebugInfoEntry * getDebugInfoEntry() const
Definition: DWARFDie.h:53
uint8_t getRefAddrByteSize() const
Definition: DWARFUnit.h:230
dwarf::DwarfFormat Format
DWARFDataExtractor getDebugInfoExtractor() const
Definition: DWARFUnit.h:200
const DWARFAbbreviationDeclarationSet * getAbbreviations() const
Definition: DWARFUnit.h:235
uint32_t getLength() const
Definition: DWARFUnit.h:224
DWARFUnit(DWARFContext &Context, const DWARFSection &Section, const DWARFDebugAbbrev *DA, const DWARFSection *RS, StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS, const DWARFSection &LS, bool LE, bool IsDWO, const DWARFUnitSectionBase &UnitSection, const DWARFUnitIndex::Entry *IndexEntry=nullptr)
Definition: DWARFUnit.cpp:47
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:490
const char * getCompilationDir()
Definition: DWARFUnit.cpp:162
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
UnitType * getUnitForOffset(uint32_t Offset) const override
Returns the Unit that contains the given section offset in the same section this Unit originated from...
Definition: DWARFUnit.h:79
unsigned getNumDIEs()
Returns the number of DIEs in the unit.
Definition: DWARFUnit.h:298
void parse(DWARFContext &C, const DWARFSection &Section)
Definition: DWARFUnit.cpp:32
uint16_t getVersion() const
Definition: DWARFUnit.h:227
dwarf::DwarfFormat getFormat() const
Definition: DWARFUnit.h:228
virtual DWARFUnit * getUnitForOffset(uint32_t Offset) const =0
Returns the Unit that contains the given section offset in the same section this Unit originated from...
die_iterator_range dies()
Definition: DWARFUnit.h:346
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
Definition: DWARFDie.h:43
StringRef getStringSection() const
Definition: DWARFUnit.h:179
DWARFSectionKind
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
const DWARFUnitSectionBase & getUnitSection() const
getUnitSection - Return the DWARFUnitSection containing this unit.
Definition: DWARFUnit.h:294
static uint32_t getDWARF5HeaderSize(uint8_t UnitType)
Return the number of bytes for the header of a unit of UnitType type.
Definition: DWARFUnit.h:254
const SectionContribution * getOffset(DWARFSectionKind Sec) const
DWARFDie getParent(const DWARFDebugInfoEntry *Die)
Definition: DWARFUnit.cpp:413
uint32_t getDIEIndex(const DWARFDie &D)
Return the index of a DIE inside the unit's DIE vector.
Definition: DWARFUnit.h:309
void setRangesSection(const DWARFSection *RS, uint32_t Base)
Definition: DWARFUnit.h:192
const DWARFUnitIndex & getDWARFUnitIndex(DWARFContext &Context, DWARFSectionKind Kind)
Definition: DWARFUnit.cpp:405
const RelocAddrMap * getRelocMap() const
Definition: DWARFUnit.h:209
UnitType
Constants for unit types in DWARF v5.
Definition: Dwarf.h:319
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
DataExtractor getStringExtractor() const
Definition: DWARFUnit.h:205
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:275
bool extract(DataExtractor debug_info, uint32_t *offset_ptr)
Definition: DWARFUnit.cpp:124
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:70
const DWARFFormParams & getFormParams() const
Definition: DWARFUnit.h:226
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:52
A helper struct for DWARFFormValue methods, providing information that allows it to know the byte siz...
const DWARFSection & getLineSection() const
Definition: DWARFUnit.h:178
A range adaptor for a pair of iterators.
llvm_unreachable("Unknown RegBank!")
bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const
Definition: DWARFUnit.cpp:62
DWARFContext & getContext() const
Definition: DWARFUnit.h:176
static bool isValidUnitType(uint8_t UnitType)
Definition: DWARFUnit.h:241
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
uint8_t getRefAddrByteSize() const
The definition of the size of form DW_FORM_ref_addr depends on the version.
DWARFDie getDIEAtIndex(unsigned Index)
Return the DIE object at the given index.
Definition: DWARFUnit.h:314
uint8_t getDwarfOffsetByteSize() const
The size of a reference is determined by the DWARF 32/64-bit format.
virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr)
Definition: DWARFUnit.cpp:84
#define C
Definition: LargeTest.cpp:25
Optional< uint64_t > getDWOId()
Definition: DWARFUnit.cpp:166
const DWARFSection & getStringOffsetSection() const
Definition: DWARFUnit.h:180
virtual void parseImpl(DWARFContext &Context, const DWARFSection &Section, const DWARFDebugAbbrev *DA, const DWARFSection *RS, StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS, const DWARFSection &LS, bool isLittleEndian, bool isDWO)=0
void updateAddressDieMap(DWARFDie Die)
Recursively update address to Die map.
Definition: DWARFUnit.cpp:346
uint32_t getOffset() const
Definition: DWARFUnit.h:222
uint8_t getAddressByteSize() const
Definition: DWARFUnit.h:229
uint32_t getLineTableOffset() const
Definition: DWARFUnit.h:339
const unsigned Kind
DWARFDie getDIEForOffset(uint32_t Offset)
Return the DIE object for a given offset inside the unit's DIE vector.
Definition: DWARFUnit.h:326
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Base class for all DWARFUnitSection classes.
Definition: DWARFUnit.h:45
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
DWARFDebugInfoEntry - A DIE with only the minimum required data.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
#define D
Definition: LargeTest.cpp:26