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