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