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