[DWARF parser] Fix nasty memory corruption in .dwo files handling.
[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
30 class DWARFUnit {
31   DWARFContext &Context;
32
33   const DWARFDebugAbbrev *Abbrev;
34   StringRef InfoSection;
35   StringRef RangeSection;
36   uint32_t RangeSectionBase;
37   StringRef StringSection;
38   StringRef StringOffsetSection;
39   StringRef AddrOffsetSection;
40   uint32_t AddrOffsetSectionBase;
41   const RelocAddrMap *RelocMap;
42   bool isLittleEndian;
43
44   uint32_t Offset;
45   uint32_t Length;
46   uint16_t Version;
47   const DWARFAbbreviationDeclarationSet *Abbrevs;
48   uint8_t AddrSize;
49   uint64_t BaseAddr;
50   // The compile unit debug information entry items.
51   std::vector<DWARFDebugInfoEntryMinimal> DieArray;
52
53   class DWOHolder {
54     object::OwningBinary<object::ObjectFile> DWOFile;
55     std::unique_ptr<DWARFContext> DWOContext;
56     DWARFUnit *DWOU;
57   public:
58     DWOHolder(StringRef DWOPath);
59     DWARFUnit *getUnit() const { return DWOU; }
60   };
61   std::unique_ptr<DWOHolder> DWO;
62
63 protected:
64   virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
65   /// Size in bytes of the unit header.
66   virtual uint32_t getHeaderSize() const { return 11; }
67
68 public:
69   DWARFUnit(DWARFContext& Context, const DWARFDebugAbbrev *DA, StringRef IS,
70             StringRef RS, StringRef SS, StringRef SOS, StringRef AOS,
71             const RelocAddrMap *M, bool LE);
72
73   virtual ~DWARFUnit();
74
75   DWARFContext& getContext() const { return Context; }
76
77   StringRef getStringSection() const { return StringSection; }
78   StringRef getStringOffsetSection() const { return StringOffsetSection; }
79   void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
80     AddrOffsetSection = AOS;
81     AddrOffsetSectionBase = Base;
82   }
83   void setRangesSection(StringRef RS, uint32_t Base) {
84     RangeSection = RS;
85     RangeSectionBase = Base;
86   }
87
88   bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
89   // FIXME: Result should be uint64_t in DWARF64.
90   bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
91
92   DataExtractor getDebugInfoExtractor() const {
93     return DataExtractor(InfoSection, isLittleEndian, AddrSize);
94   }
95   DataExtractor getStringExtractor() const {
96     return DataExtractor(StringSection, false, 0);
97   }
98
99   const RelocAddrMap *getRelocMap() const { return RelocMap; }
100
101   bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
102
103   /// extractRangeList - extracts the range list referenced by this compile
104   /// unit from .debug_ranges section. Returns true on success.
105   /// Requires that compile unit is already extracted.
106   bool extractRangeList(uint32_t RangeListOffset,
107                         DWARFDebugRangeList &RangeList) const;
108   void clear();
109   uint32_t getOffset() const { return Offset; }
110   uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
111   uint32_t getLength() const { return Length; }
112   uint16_t getVersion() const { return Version; }
113   const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
114     return Abbrevs;
115   }
116   uint8_t getAddressByteSize() const { return AddrSize; }
117   uint64_t getBaseAddress() const { return BaseAddr; }
118
119   void setBaseAddress(uint64_t base_addr) {
120     BaseAddr = base_addr;
121   }
122
123   const DWARFDebugInfoEntryMinimal *
124   getCompileUnitDIE(bool extract_cu_die_only = true) {
125     extractDIEsIfNeeded(extract_cu_die_only);
126     return DieArray.empty() ? nullptr : &DieArray[0];
127   }
128
129   const char *getCompilationDir();
130   uint64_t getDWOId();
131
132   void collectAddressRanges(DWARFAddressRangesVector &CURanges);
133
134   /// getInlinedChainForAddress - fetches inlined chain for a given address.
135   /// Returns empty chain if there is no subprogram containing address. The
136   /// chain is valid as long as parsed compile unit DIEs are not cleared.
137   DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
138
139 private:
140   /// Size in bytes of the .debug_info data associated with this compile unit.
141   size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
142
143   /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
144   /// hasn't already been done. Returns the number of DIEs parsed at this call.
145   size_t extractDIEsIfNeeded(bool CUDieOnly);
146   /// extractDIEsToVector - Appends all parsed DIEs to a vector.
147   void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
148                            std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
149   /// setDIERelations - We read in all of the DIE entries into our flat list
150   /// of DIE entries and now we need to go back through all of them and set the
151   /// parent, sibling and child pointers for quick DIE navigation.
152   void setDIERelations();
153   /// clearDIEs - Clear parsed DIEs to keep memory usage low.
154   void clearDIEs(bool KeepCUDie);
155
156   /// parseDWO - Parses .dwo file for current compile unit. Returns true if
157   /// it was actually constructed.
158   bool parseDWO();
159
160   /// getSubprogramForAddress - Returns subprogram DIE with address range
161   /// encompassing the provided address. The pointer is alive as long as parsed
162   /// compile unit DIEs are not cleared.
163   const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);
164 };
165
166 }
167
168 #endif