2d05a520b50fd4273d3381fbaea85cf5cf89a23a
[oota-llvm.git] / 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_LIB_DEBUGINFO_DWARFUNIT_H
11 #define LLVM_LIB_DEBUGINFO_DWARFUNIT_H
12
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
15 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
16 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
17 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
18 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
19 #include <vector>
20
21 namespace llvm {
22
23 namespace object {
24 class ObjectFile;
25 }
26
27 class DWARFContext;
28 class DWARFDebugAbbrev;
29 class DWARFUnit;
30 class StringRef;
31 class raw_ostream;
32
33 /// Base class for all DWARFUnitSection classes. This provides the
34 /// functionality common to all unit types.
35 class DWARFUnitSectionBase {
36 public:
37   /// Returns the Unit that contains the given section offset in the
38   /// same section this Unit originated from.
39   virtual DWARFUnit *getUnitForOffset(uint32_t Offset) const = 0;
40
41   void parse(DWARFContext &C, const DWARFSection &Section);
42   void parseDWO(DWARFContext &C, const DWARFSection &DWOSection);
43
44 protected:
45   virtual void parseImpl(DWARFContext &Context, const DWARFSection &Section,
46                          const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
47                          StringRef SOS, StringRef AOS, bool isLittleEndian) = 0;
48
49   ~DWARFUnitSectionBase() = default;
50 };
51
52 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
53 template<typename UnitType>
54 class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
55                                public DWARFUnitSectionBase {
56   bool Parsed;
57
58 public:
59   DWARFUnitSection() : Parsed(false) {}
60   DWARFUnitSection(DWARFUnitSection &&DUS) :
61     SmallVector<std::unique_ptr<UnitType>, 1>(std::move(DUS)), Parsed(DUS.Parsed) {}
62
63   typedef llvm::SmallVectorImpl<std::unique_ptr<UnitType>> UnitVector;
64   typedef typename UnitVector::iterator iterator;
65   typedef llvm::iterator_range<typename UnitVector::iterator> iterator_range;
66
67   UnitType *getUnitForOffset(uint32_t Offset) const override {
68     auto *CU = std::upper_bound(
69         this->begin(), this->end(), Offset,
70         [](uint32_t LHS, const std::unique_ptr<UnitType> &RHS) {
71           return LHS < RHS->getNextUnitOffset();
72         });
73     if (CU != this->end())
74       return CU->get();
75     return nullptr;
76   }
77
78 private:
79   void parseImpl(DWARFContext &Context, const DWARFSection &Section,
80                  const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
81                  StringRef SOS, StringRef AOS, bool LE) override {
82     if (Parsed)
83       return;
84     DataExtractor Data(Section.Data, LE, 0);
85     uint32_t Offset = 0;
86     while (Data.isValidOffset(Offset)) {
87       auto U = llvm::make_unique<UnitType>(Context, Section, DA, RS, SS, SOS,
88                                            AOS, LE, *this);
89       if (!U->extract(Data, &Offset))
90         break;
91       this->push_back(std::move(U));
92       Offset = this->back()->getNextUnitOffset();
93     }
94     Parsed = true;
95   }
96 };
97
98 class DWARFUnit {
99   DWARFContext &Context;
100   // Section containing this DWARFUnit.
101   const DWARFSection &InfoSection;
102
103   const DWARFDebugAbbrev *Abbrev;
104   StringRef RangeSection;
105   uint32_t RangeSectionBase;
106   StringRef StringSection;
107   StringRef StringOffsetSection;
108   StringRef AddrOffsetSection;
109   uint32_t AddrOffsetSectionBase;
110   bool isLittleEndian;
111   const DWARFUnitSectionBase &UnitSection;
112
113   uint32_t Offset;
114   uint32_t Length;
115   uint16_t Version;
116   const DWARFAbbreviationDeclarationSet *Abbrevs;
117   uint8_t AddrSize;
118   uint64_t BaseAddr;
119   // The compile unit debug information entry items.
120   std::vector<DWARFDebugInfoEntryMinimal> DieArray;
121
122   class DWOHolder {
123     object::OwningBinary<object::ObjectFile> DWOFile;
124     std::unique_ptr<DWARFContext> DWOContext;
125     DWARFUnit *DWOU;
126   public:
127     DWOHolder(StringRef DWOPath);
128     DWARFUnit *getUnit() const { return DWOU; }
129   };
130   std::unique_ptr<DWOHolder> DWO;
131
132 protected:
133   virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
134   /// Size in bytes of the unit header.
135   virtual uint32_t getHeaderSize() const { return 11; }
136
137 public:
138   DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
139             const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
140             StringRef SOS, StringRef AOS, bool LE,
141             const DWARFUnitSectionBase &UnitSection);
142
143   virtual ~DWARFUnit();
144
145   DWARFContext& getContext() const { return Context; }
146
147   StringRef getStringSection() const { return StringSection; }
148   StringRef getStringOffsetSection() const { return StringOffsetSection; }
149   void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
150     AddrOffsetSection = AOS;
151     AddrOffsetSectionBase = Base;
152   }
153   void setRangesSection(StringRef RS, uint32_t Base) {
154     RangeSection = RS;
155     RangeSectionBase = Base;
156   }
157
158   bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
159   // FIXME: Result should be uint64_t in DWARF64.
160   bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
161
162   DataExtractor getDebugInfoExtractor() const {
163     return DataExtractor(InfoSection.Data, isLittleEndian, AddrSize);
164   }
165   DataExtractor getStringExtractor() const {
166     return DataExtractor(StringSection, false, 0);
167   }
168
169   const RelocAddrMap *getRelocMap() const { return &InfoSection.Relocs; }
170
171   bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
172
173   /// extractRangeList - extracts the range list referenced by this compile
174   /// unit from .debug_ranges section. Returns true on success.
175   /// Requires that compile unit is already extracted.
176   bool extractRangeList(uint32_t RangeListOffset,
177                         DWARFDebugRangeList &RangeList) const;
178   void clear();
179   uint32_t getOffset() const { return Offset; }
180   uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
181   uint32_t getLength() const { return Length; }
182   uint16_t getVersion() const { return Version; }
183   const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
184     return Abbrevs;
185   }
186   uint8_t getAddressByteSize() const { return AddrSize; }
187   uint64_t getBaseAddress() const { return BaseAddr; }
188
189   void setBaseAddress(uint64_t base_addr) {
190     BaseAddr = base_addr;
191   }
192
193   const DWARFDebugInfoEntryMinimal *getUnitDIE(bool ExtractUnitDIEOnly = true) {
194     extractDIEsIfNeeded(ExtractUnitDIEOnly);
195     return DieArray.empty() ? nullptr : &DieArray[0];
196   }
197
198   const char *getCompilationDir();
199   uint64_t getDWOId();
200
201   void collectAddressRanges(DWARFAddressRangesVector &CURanges);
202
203   /// getInlinedChainForAddress - fetches inlined chain for a given address.
204   /// Returns empty chain if there is no subprogram containing address. The
205   /// chain is valid as long as parsed compile unit DIEs are not cleared.
206   DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
207
208   /// getUnitSection - Return the DWARFUnitSection containing this unit.
209   const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
210
211   /// \brief Returns the number of DIEs in the unit. Parses the unit
212   /// if necessary.
213   unsigned getNumDIEs() {
214     extractDIEsIfNeeded(false);
215     return DieArray.size();
216   }
217
218   /// \brief Return the index of a DIE inside the unit's DIE vector.
219   ///
220   /// It is illegal to call this method with a DIE that hasn't be
221   /// created by this unit. In other word, it's illegal to call this
222   /// method on a DIE that isn't accessible by following
223   /// children/sibling links starting from this unit's getUnitDIE().
224   uint32_t getDIEIndex(const DWARFDebugInfoEntryMinimal *DIE) {
225     assert(!DieArray.empty() && DIE >= &DieArray[0] &&
226            DIE < &DieArray[0] + DieArray.size());
227     return DIE - &DieArray[0];
228   }
229
230   /// \brief Return the DIE object at the given index.
231   const DWARFDebugInfoEntryMinimal *getDIEAtIndex(unsigned Index) const {
232     assert(Index < DieArray.size());
233     return &DieArray[Index];
234   }
235
236   /// \brief Return the DIE object for a given offset inside the
237   /// unit's DIE vector.
238   ///
239   /// The unit needs to have his DIEs extracted for this method to work.
240   const DWARFDebugInfoEntryMinimal *getDIEForOffset(uint32_t Offset) const {
241     assert(!DieArray.empty());
242     auto it = std::lower_bound(
243         DieArray.begin(), DieArray.end(), Offset,
244         [=](const DWARFDebugInfoEntryMinimal &LHS, uint32_t Offset) {
245           return LHS.getOffset() < Offset;
246         });
247     return it == DieArray.end() ? nullptr : &*it;
248   }
249
250 private:
251   /// Size in bytes of the .debug_info data associated with this compile unit.
252   size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
253
254   /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
255   /// hasn't already been done. Returns the number of DIEs parsed at this call.
256   size_t extractDIEsIfNeeded(bool CUDieOnly);
257   /// extractDIEsToVector - Appends all parsed DIEs to a vector.
258   void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
259                            std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
260   /// setDIERelations - We read in all of the DIE entries into our flat list
261   /// of DIE entries and now we need to go back through all of them and set the
262   /// parent, sibling and child pointers for quick DIE navigation.
263   void setDIERelations();
264   /// clearDIEs - Clear parsed DIEs to keep memory usage low.
265   void clearDIEs(bool KeepCUDie);
266
267   /// parseDWO - Parses .dwo file for current compile unit. Returns true if
268   /// it was actually constructed.
269   bool parseDWO();
270
271   /// getSubprogramForAddress - Returns subprogram DIE with address range
272   /// encompassing the provided address. The pointer is alive as long as parsed
273   /// compile unit DIEs are not cleared.
274   const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);
275 };
276
277 }
278
279 #endif