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