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