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