Factor the Unit section parsing into the DWARFUnitSection class.
[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 = make_unique<UnitType>(Context, DA, Section, RS, SS, SOS, AOS, &M,
92                                      Data.isLittleEndian(), *this);
93       if (!U->extract(Data, &Offset))
94         break;
95       this->push_back(std::move(U));
96       Offset = this->back()->getNextUnitOffset();
97     }
98     Parsed = true;
99   }
100 };
101
102 class DWARFUnit {
103   DWARFContext &Context;
104
105   const DWARFDebugAbbrev *Abbrev;
106   StringRef InfoSection;
107   StringRef RangeSection;
108   uint32_t RangeSectionBase;
109   StringRef StringSection;
110   StringRef StringOffsetSection;
111   StringRef AddrOffsetSection;
112   uint32_t AddrOffsetSectionBase;
113   const RelocAddrMap *RelocMap;
114   bool isLittleEndian;
115   const DWARFUnitSectionBase &UnitSection;
116
117   uint32_t Offset;
118   uint32_t Length;
119   uint16_t Version;
120   const DWARFAbbreviationDeclarationSet *Abbrevs;
121   uint8_t AddrSize;
122   uint64_t BaseAddr;
123   // The compile unit debug information entry items.
124   std::vector<DWARFDebugInfoEntryMinimal> DieArray;
125
126   class DWOHolder {
127     object::OwningBinary<object::ObjectFile> DWOFile;
128     std::unique_ptr<DWARFContext> DWOContext;
129     DWARFUnit *DWOU;
130   public:
131     DWOHolder(StringRef DWOPath);
132     DWARFUnit *getUnit() const { return DWOU; }
133   };
134   std::unique_ptr<DWOHolder> DWO;
135
136 protected:
137   virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
138   /// Size in bytes of the unit header.
139   virtual uint32_t getHeaderSize() const { return 11; }
140
141 public:
142   DWARFUnit(DWARFContext& Context, const DWARFDebugAbbrev *DA, StringRef IS,
143             StringRef RS, StringRef SS, StringRef SOS, StringRef AOS,
144             const RelocAddrMap *M, bool LE, const DWARFUnitSectionBase &UnitSection);
145
146   virtual ~DWARFUnit();
147
148   DWARFContext& getContext() const { return Context; }
149
150   StringRef getStringSection() const { return StringSection; }
151   StringRef getStringOffsetSection() const { return StringOffsetSection; }
152   void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
153     AddrOffsetSection = AOS;
154     AddrOffsetSectionBase = Base;
155   }
156   void setRangesSection(StringRef RS, uint32_t Base) {
157     RangeSection = RS;
158     RangeSectionBase = Base;
159   }
160
161   bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
162   // FIXME: Result should be uint64_t in DWARF64.
163   bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
164
165   DataExtractor getDebugInfoExtractor() const {
166     return DataExtractor(InfoSection, isLittleEndian, AddrSize);
167   }
168   DataExtractor getStringExtractor() const {
169     return DataExtractor(StringSection, false, 0);
170   }
171
172   const RelocAddrMap *getRelocMap() const { return RelocMap; }
173
174   bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
175
176   /// extractRangeList - extracts the range list referenced by this compile
177   /// unit from .debug_ranges section. Returns true on success.
178   /// Requires that compile unit is already extracted.
179   bool extractRangeList(uint32_t RangeListOffset,
180                         DWARFDebugRangeList &RangeList) const;
181   void clear();
182   uint32_t getOffset() const { return Offset; }
183   uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
184   uint32_t getLength() const { return Length; }
185   uint16_t getVersion() const { return Version; }
186   const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
187     return Abbrevs;
188   }
189   uint8_t getAddressByteSize() const { return AddrSize; }
190   uint64_t getBaseAddress() const { return BaseAddr; }
191
192   void setBaseAddress(uint64_t base_addr) {
193     BaseAddr = base_addr;
194   }
195
196   const DWARFDebugInfoEntryMinimal *
197   getCompileUnitDIE(bool extract_cu_die_only = true) {
198     extractDIEsIfNeeded(extract_cu_die_only);
199     return DieArray.empty() ? nullptr : &DieArray[0];
200   }
201
202   const char *getCompilationDir();
203   uint64_t getDWOId();
204
205   void collectAddressRanges(DWARFAddressRangesVector &CURanges);
206
207   /// getInlinedChainForAddress - fetches inlined chain for a given address.
208   /// Returns empty chain if there is no subprogram containing address. The
209   /// chain is valid as long as parsed compile unit DIEs are not cleared.
210   DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
211
212   /// getUnitSection - Return the DWARFUnitSection containing this unit.
213   const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
214
215 private:
216   /// Size in bytes of the .debug_info data associated with this compile unit.
217   size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
218
219   /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
220   /// hasn't already been done. Returns the number of DIEs parsed at this call.
221   size_t extractDIEsIfNeeded(bool CUDieOnly);
222   /// extractDIEsToVector - Appends all parsed DIEs to a vector.
223   void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
224                            std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
225   /// setDIERelations - We read in all of the DIE entries into our flat list
226   /// of DIE entries and now we need to go back through all of them and set the
227   /// parent, sibling and child pointers for quick DIE navigation.
228   void setDIERelations();
229   /// clearDIEs - Clear parsed DIEs to keep memory usage low.
230   void clearDIEs(bool KeepCUDie);
231
232   /// parseDWO - Parses .dwo file for current compile unit. Returns true if
233   /// it was actually constructed.
234   bool parseDWO();
235
236   /// getSubprogramForAddress - Returns subprogram DIE with address range
237   /// encompassing the provided address. The pointer is alive as long as parsed
238   /// compile unit DIEs are not cleared.
239   const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);
240 };
241
242 }
243
244 #endif