]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / DebugInfo / DWARF / DWARFUnit.h
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/iterator_range.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
21 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
22 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
23 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
24 #include "llvm/Object/Binary.h"
25 #include "llvm/Object/ObjectFile.h"
26 #include "llvm/Support/DataExtractor.h"
27 #include "llvm/Support/Dwarf.h"
28 #include <algorithm>
29 #include <cassert>
30 #include <cstddef>
31 #include <cstdint>
32 #include <memory>
33 #include <vector>
34 #include <map>
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.
45 class DWARFUnitSectionBase {
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, StringRef SOS, StringRef AOS,
61                          StringRef LS, bool isLittleEndian, bool isDWO) = 0;
62 };
63
64 const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
65                                         DWARFSectionKind Kind);
66
67 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
68 template<typename UnitType>
69 class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
70                                public DWARFUnitSectionBase {
71   bool Parsed = false;
72
73 public:
74   typedef SmallVectorImpl<std::unique_ptr<UnitType>> UnitVector;
75   typedef typename UnitVector::iterator iterator;
76   typedef llvm::iterator_range<typename UnitVector::iterator> iterator_range;
77
78   UnitType *getUnitForOffset(uint32_t Offset) const override {
79     auto *CU = std::upper_bound(
80         this->begin(), this->end(), Offset,
81         [](uint32_t LHS, const std::unique_ptr<UnitType> &RHS) {
82           return LHS < RHS->getNextUnitOffset();
83         });
84     if (CU != this->end())
85       return CU->get();
86     return nullptr;
87   }
88
89 private:
90   void parseImpl(DWARFContext &Context, const DWARFSection &Section,
91                  const DWARFDebugAbbrev *DA, const DWARFSection *RS,
92                  StringRef SS, StringRef SOS, StringRef AOS, StringRef LS,
93                  bool LE, bool IsDWO) override {
94     if (Parsed)
95       return;
96     const auto &Index = getDWARFUnitIndex(Context, UnitType::Section);
97     DataExtractor Data(Section.Data, LE, 0);
98     uint32_t Offset = 0;
99     while (Data.isValidOffset(Offset)) {
100       auto U = llvm::make_unique<UnitType>(Context, Section, DA, RS, SS, SOS,
101                                            AOS, LS, LE, IsDWO, *this,
102                                            Index.getFromOffset(Offset));
103       if (!U->extract(Data, &Offset))
104         break;
105       this->push_back(std::move(U));
106       Offset = this->back()->getNextUnitOffset();
107     }
108     Parsed = true;
109   }
110 };
111
112 class DWARFUnit {
113   DWARFContext &Context;
114   // Section containing this DWARFUnit.
115   const DWARFSection &InfoSection;
116
117   const DWARFDebugAbbrev *Abbrev;
118   const DWARFSection *RangeSection;
119   uint32_t RangeSectionBase;
120   StringRef LineSection;
121   StringRef StringSection;
122   StringRef StringOffsetSection;
123   StringRef AddrOffsetSection;
124   uint32_t AddrOffsetSectionBase;
125   bool isLittleEndian;
126   bool isDWO;
127   const DWARFUnitSectionBase &UnitSection;
128
129   uint32_t Offset;
130   uint32_t Length;
131   const DWARFAbbreviationDeclarationSet *Abbrevs;
132   uint16_t Version;
133   uint8_t UnitType;
134   uint8_t AddrSize;
135   uint64_t BaseAddr;
136   // The compile unit debug information entry items.
137   std::vector<DWARFDebugInfoEntry> DieArray;
138
139   // Map from range's start address to end address and corresponding DIE.
140   // IntervalMap does not support range removal, as a result, we use the
141   // std::map::upper_bound for address range lookup.
142   std::map<uint64_t, std::pair<uint64_t, DWARFDie>> AddrDieMap;
143   typedef iterator_range<std::vector<DWARFDebugInfoEntry>::iterator>
144       die_iterator_range;
145
146   class DWOHolder {
147     object::OwningBinary<object::ObjectFile> DWOFile;
148     std::unique_ptr<DWARFContext> DWOContext;
149     DWARFUnit *DWOU = nullptr;
150
151   public:
152     DWOHolder(StringRef DWOPath);
153
154     DWARFUnit *getUnit() const { return DWOU; }
155   };
156   std::unique_ptr<DWOHolder> DWO;
157
158   const DWARFUnitIndex::Entry *IndexEntry;
159
160   uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) {
161     auto First = DieArray.data();
162     assert(Die >= First && Die < First + DieArray.size());
163     return Die - First;
164   }
165
166 protected:
167   virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
168
169   /// Size in bytes of the unit header.
170   virtual uint32_t getHeaderSize() const { return Version <= 4 ? 11 : 12; }
171
172 public:
173   DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
174             const DWARFDebugAbbrev *DA, const DWARFSection *RS, StringRef SS,
175             StringRef SOS, StringRef AOS, StringRef LS, bool LE, bool IsDWO,
176             const DWARFUnitSectionBase &UnitSection,
177             const DWARFUnitIndex::Entry *IndexEntry = nullptr);
178
179   virtual ~DWARFUnit();
180
181   DWARFContext& getContext() const { return Context; }
182
183   StringRef getLineSection() const { return LineSection; }
184   StringRef getStringSection() const { return StringSection; }
185   StringRef getStringOffsetSection() const { return StringOffsetSection; }
186
187   void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
188     AddrOffsetSection = AOS;
189     AddrOffsetSectionBase = Base;
190   }
191
192   // Recursively update address to Die map.
193   void updateAddressDieMap(DWARFDie Die);
194
195   void setRangesSection(const DWARFSection *RS, uint32_t Base) {
196     RangeSection = RS;
197     RangeSectionBase = Base;
198   }
199
200   bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
201   // FIXME: Result should be uint64_t in DWARF64.
202   bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
203
204   DataExtractor getDebugInfoExtractor() const {
205     return DataExtractor(InfoSection.Data, isLittleEndian, AddrSize);
206   }
207
208   DataExtractor getStringExtractor() const {
209     return DataExtractor(StringSection, false, 0);
210   }
211
212   const RelocAddrMap *getRelocMap() const { return &InfoSection.Relocs; }
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   uint16_t getVersion() const { return Version; }
226
227   dwarf::DwarfFormat getFormat() const {
228     return dwarf::DwarfFormat::DWARF32; // FIXME: Support DWARF64.
229   }
230
231   const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
232     return Abbrevs;
233   }
234
235   uint8_t getUnitType() const { return UnitType; }
236   uint8_t getAddressByteSize() const { return AddrSize; }
237
238   uint8_t getRefAddrByteSize() const {
239     if (Version == 2)
240       return AddrSize;
241     return getDwarfOffsetByteSize();
242   }
243
244   uint8_t getDwarfOffsetByteSize() const {
245     if (getFormat() == dwarf::DwarfFormat::DWARF64)
246       return 8;
247     return 4;
248   }
249
250   uint64_t getBaseAddress() const { return BaseAddr; }
251
252   void setBaseAddress(uint64_t base_addr) {
253     BaseAddr = base_addr;
254   }
255
256   DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
257     extractDIEsIfNeeded(ExtractUnitDIEOnly);
258     if (DieArray.empty())
259       return DWARFDie();
260     return DWARFDie(this, &DieArray[0]);
261   }
262
263   const char *getCompilationDir();
264   Optional<uint64_t> getDWOId();
265
266   void collectAddressRanges(DWARFAddressRangesVector &CURanges);
267
268   /// getInlinedChainForAddress - fetches inlined chain for a given address.
269   /// Returns empty chain if there is no subprogram containing address. The
270   /// chain is valid as long as parsed compile unit DIEs are not cleared.
271   void getInlinedChainForAddress(uint64_t Address,
272                                  SmallVectorImpl<DWARFDie> &InlinedChain);
273
274   /// getUnitSection - Return the DWARFUnitSection containing this unit.
275   const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
276
277   /// \brief Returns the number of DIEs in the unit. Parses the unit
278   /// if necessary.
279   unsigned getNumDIEs() {
280     extractDIEsIfNeeded(false);
281     return DieArray.size();
282   }
283
284   /// \brief Return the index of a DIE inside the unit's DIE vector.
285   ///
286   /// It is illegal to call this method with a DIE that hasn't be
287   /// created by this unit. In other word, it's illegal to call this
288   /// method on a DIE that isn't accessible by following
289   /// children/sibling links starting from this unit's getUnitDIE().
290   uint32_t getDIEIndex(const DWARFDie &D) {
291     return getDIEIndex(D.getDebugInfoEntry());
292   }
293
294   /// \brief Return the DIE object at the given index.
295   DWARFDie getDIEAtIndex(unsigned Index) {
296     assert(Index < DieArray.size());
297     return DWARFDie(this, &DieArray[Index]);
298   }
299
300   DWARFDie getParent(const DWARFDebugInfoEntry *Die);
301   DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
302
303   /// \brief Return the DIE object for a given offset inside the
304   /// unit's DIE vector.
305   ///
306   /// The unit needs to have its DIEs extracted for this method to work.
307   DWARFDie getDIEForOffset(uint32_t Offset) {
308     extractDIEsIfNeeded(false);
309     assert(!DieArray.empty());
310     auto it = std::lower_bound(
311         DieArray.begin(), DieArray.end(), Offset,
312         [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) {
313           return LHS.getOffset() < Offset;
314         });
315     if (it == DieArray.end())
316       return DWARFDie();
317     return DWARFDie(this, &*it);
318   }
319
320   uint32_t getLineTableOffset() const {
321     if (IndexEntry)
322       if (const auto *Contrib = IndexEntry->getOffset(DW_SECT_LINE))
323         return Contrib->Offset;
324     return 0;
325   }
326
327   die_iterator_range dies() {
328     extractDIEsIfNeeded(false);
329     return die_iterator_range(DieArray.begin(), DieArray.end());
330   }
331
332 private:
333   /// Size in bytes of the .debug_info data associated with this compile unit.
334   size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
335
336   /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
337   /// hasn't already been done. Returns the number of DIEs parsed at this call.
338   size_t extractDIEsIfNeeded(bool CUDieOnly);
339
340   /// extractDIEsToVector - Appends all parsed DIEs to a vector.
341   void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
342                            std::vector<DWARFDebugInfoEntry> &DIEs) const;
343
344   /// clearDIEs - Clear parsed DIEs to keep memory usage low.
345   void clearDIEs(bool KeepCUDie);
346
347   /// parseDWO - Parses .dwo file for current compile unit. Returns true if
348   /// it was actually constructed.
349   bool parseDWO();
350
351   /// getSubroutineForAddress - Returns subprogram DIE with address range
352   /// encompassing the provided address. The pointer is alive as long as parsed
353   /// compile unit DIEs are not cleared.
354   DWARFDie getSubroutineForAddress(uint64_t Address);
355 };
356
357 } // end namespace llvm
358
359 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H