]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, 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, const DWARFSection *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, const DWARFSection *AOS,
93                  StringRef LS, 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   const DWARFSection *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   std::shared_ptr<DWARFUnit> DWO;
147
148   const DWARFUnitIndex::Entry *IndexEntry;
149
150   uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) {
151     auto First = DieArray.data();
152     assert(Die >= First && Die < First + DieArray.size());
153     return Die - First;
154   }
155
156 protected:
157   virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
158
159   /// Size in bytes of the unit header.
160   virtual uint32_t getHeaderSize() const { return Version <= 4 ? 11 : 12; }
161
162 public:
163   DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
164             const DWARFDebugAbbrev *DA, const DWARFSection *RS, StringRef SS,
165             StringRef SOS, const DWARFSection *AOS, StringRef LS, bool LE,
166             bool IsDWO, const DWARFUnitSectionBase &UnitSection,
167             const DWARFUnitIndex::Entry *IndexEntry = nullptr);
168
169   virtual ~DWARFUnit();
170
171   DWARFContext& getContext() const { return Context; }
172
173   StringRef getLineSection() const { return LineSection; }
174   StringRef getStringSection() const { return StringSection; }
175   StringRef getStringOffsetSection() const { return StringOffsetSection; }
176
177   void setAddrOffsetSection(const DWARFSection *AOS, uint32_t Base) {
178     AddrOffsetSection = AOS;
179     AddrOffsetSectionBase = Base;
180   }
181
182   /// Recursively update address to Die map.
183   void updateAddressDieMap(DWARFDie Die);
184
185   void setRangesSection(const DWARFSection *RS, uint32_t Base) {
186     RangeSection = RS;
187     RangeSectionBase = Base;
188   }
189
190   bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
191   // FIXME: Result should be uint64_t in DWARF64.
192   bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
193
194   DataExtractor getDebugInfoExtractor() const {
195     return DataExtractor(InfoSection.Data, isLittleEndian, AddrSize);
196   }
197
198   DataExtractor getStringExtractor() const {
199     return DataExtractor(StringSection, false, 0);
200   }
201
202   const RelocAddrMap *getRelocMap() const { return &InfoSection.Relocs; }
203
204   bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
205
206   /// extractRangeList - extracts the range list referenced by this compile
207   /// unit from .debug_ranges section. Returns true on success.
208   /// Requires that compile unit is already extracted.
209   bool extractRangeList(uint32_t RangeListOffset,
210                         DWARFDebugRangeList &RangeList) const;
211   void clear();
212   uint32_t getOffset() const { return Offset; }
213   uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
214   uint32_t getLength() const { return Length; }
215   uint16_t getVersion() const { return Version; }
216
217   dwarf::DwarfFormat getFormat() const {
218     return dwarf::DwarfFormat::DWARF32; // FIXME: Support DWARF64.
219   }
220
221   const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
222     return Abbrevs;
223   }
224
225   uint8_t getUnitType() const { return UnitType; }
226   uint8_t getAddressByteSize() const { return AddrSize; }
227
228   uint8_t getRefAddrByteSize() const {
229     if (Version == 2)
230       return AddrSize;
231     return getDwarfOffsetByteSize();
232   }
233
234   uint8_t getDwarfOffsetByteSize() const {
235     if (getFormat() == dwarf::DwarfFormat::DWARF64)
236       return 8;
237     return 4;
238   }
239
240   uint64_t getBaseAddress() const { return BaseAddr; }
241
242   void setBaseAddress(uint64_t base_addr) {
243     BaseAddr = base_addr;
244   }
245
246   DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
247     extractDIEsIfNeeded(ExtractUnitDIEOnly);
248     if (DieArray.empty())
249       return DWARFDie();
250     return DWARFDie(this, &DieArray[0]);
251   }
252
253   const char *getCompilationDir();
254   Optional<uint64_t> getDWOId();
255
256   void collectAddressRanges(DWARFAddressRangesVector &CURanges);
257
258   /// getInlinedChainForAddress - fetches inlined chain for a given address.
259   /// Returns empty chain if there is no subprogram containing address. The
260   /// chain is valid as long as parsed compile unit DIEs are not cleared.
261   void getInlinedChainForAddress(uint64_t Address,
262                                  SmallVectorImpl<DWARFDie> &InlinedChain);
263
264   /// getUnitSection - Return the DWARFUnitSection containing this unit.
265   const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
266
267   /// \brief Returns the number of DIEs in the unit. Parses the unit
268   /// if necessary.
269   unsigned getNumDIEs() {
270     extractDIEsIfNeeded(false);
271     return DieArray.size();
272   }
273
274   /// \brief Return the index of a DIE inside the unit's DIE vector.
275   ///
276   /// It is illegal to call this method with a DIE that hasn't be
277   /// created by this unit. In other word, it's illegal to call this
278   /// method on a DIE that isn't accessible by following
279   /// children/sibling links starting from this unit's getUnitDIE().
280   uint32_t getDIEIndex(const DWARFDie &D) {
281     return getDIEIndex(D.getDebugInfoEntry());
282   }
283
284   /// \brief Return the DIE object at the given index.
285   DWARFDie getDIEAtIndex(unsigned Index) {
286     assert(Index < DieArray.size());
287     return DWARFDie(this, &DieArray[Index]);
288   }
289
290   DWARFDie getParent(const DWARFDebugInfoEntry *Die);
291   DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
292
293   /// \brief Return the DIE object for a given offset inside the
294   /// unit's DIE vector.
295   ///
296   /// The unit needs to have its DIEs extracted for this method to work.
297   DWARFDie getDIEForOffset(uint32_t Offset) {
298     extractDIEsIfNeeded(false);
299     assert(!DieArray.empty());
300     auto it = std::lower_bound(
301         DieArray.begin(), DieArray.end(), Offset,
302         [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) {
303           return LHS.getOffset() < Offset;
304         });
305     if (it != DieArray.end() && it->getOffset() == Offset)
306       return DWARFDie(this, &*it);
307     return DWARFDie();
308   }
309
310   uint32_t getLineTableOffset() const {
311     if (IndexEntry)
312       if (const auto *Contrib = IndexEntry->getOffset(DW_SECT_LINE))
313         return Contrib->Offset;
314     return 0;
315   }
316
317   die_iterator_range dies() {
318     extractDIEsIfNeeded(false);
319     return die_iterator_range(DieArray.begin(), DieArray.end());
320   }
321
322 private:
323   /// Size in bytes of the .debug_info data associated with this compile unit.
324   size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
325
326   /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
327   /// hasn't already been done. Returns the number of DIEs parsed at this call.
328   size_t extractDIEsIfNeeded(bool CUDieOnly);
329
330   /// extractDIEsToVector - Appends all parsed DIEs to a vector.
331   void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
332                            std::vector<DWARFDebugInfoEntry> &DIEs) const;
333
334   /// clearDIEs - Clear parsed DIEs to keep memory usage low.
335   void clearDIEs(bool KeepCUDie);
336
337   /// parseDWO - Parses .dwo file for current compile unit. Returns true if
338   /// it was actually constructed.
339   bool parseDWO();
340
341   /// getSubroutineForAddress - Returns subprogram DIE with address range
342   /// encompassing the provided address. The pointer is alive as long as parsed
343   /// compile unit DIEs are not cleared.
344   DWARFDie getSubroutineForAddress(uint64_t Address);
345 };
346
347 } // end namespace llvm
348
349 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H