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