DWARFUnit.h revision 263508
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_DEBUGINFO_DWARFUNIT_H
11#define LLVM_DEBUGINFO_DWARFUNIT_H
12
13#include "llvm/ADT/OwningPtr.h"
14#include "DWARFDebugAbbrev.h"
15#include "DWARFDebugInfoEntry.h"
16#include "DWARFDebugRangeList.h"
17#include "DWARFRelocMap.h"
18#include <vector>
19
20namespace llvm {
21
22namespace object {
23class ObjectFile;
24}
25
26class DWARFDebugAbbrev;
27class StringRef;
28class raw_ostream;
29
30class DWARFUnit {
31  const DWARFDebugAbbrev *Abbrev;
32  StringRef InfoSection;
33  StringRef AbbrevSection;
34  StringRef RangeSection;
35  uint32_t RangeSectionBase;
36  StringRef StringSection;
37  StringRef StringOffsetSection;
38  StringRef AddrOffsetSection;
39  uint32_t AddrOffsetSectionBase;
40  const RelocAddrMap *RelocMap;
41  bool isLittleEndian;
42
43  uint32_t Offset;
44  uint32_t Length;
45  uint16_t Version;
46  const DWARFAbbreviationDeclarationSet *Abbrevs;
47  uint8_t AddrSize;
48  uint64_t BaseAddr;
49  // The compile unit debug information entry items.
50  std::vector<DWARFDebugInfoEntryMinimal> DieArray;
51
52  class DWOHolder {
53    OwningPtr<object::ObjectFile> DWOFile;
54    OwningPtr<DWARFContext> DWOContext;
55    DWARFUnit *DWOU;
56  public:
57    DWOHolder(object::ObjectFile *DWOFile);
58    DWARFUnit *getUnit() const { return DWOU; }
59  };
60  OwningPtr<DWOHolder> DWO;
61
62protected:
63  virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
64
65public:
66
67  DWARFUnit(const DWARFDebugAbbrev *DA, StringRef IS, StringRef AS,
68            StringRef RS, StringRef SS, StringRef SOS, StringRef AOS,
69            const RelocAddrMap *M, bool LE);
70
71  virtual ~DWARFUnit();
72
73  StringRef getStringSection() const { return StringSection; }
74  StringRef getStringOffsetSection() const { return StringOffsetSection; }
75  void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
76    AddrOffsetSection = AOS;
77    AddrOffsetSectionBase = Base;
78  }
79  void setRangesSection(StringRef RS, uint32_t Base) {
80    RangeSection = RS;
81    RangeSectionBase = Base;
82  }
83
84  bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
85  // FIXME: Result should be uint64_t in DWARF64.
86  bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
87
88  DataExtractor getDebugInfoExtractor() const {
89    return DataExtractor(InfoSection, isLittleEndian, AddrSize);
90  }
91  DataExtractor getStringExtractor() const {
92    return DataExtractor(StringSection, false, 0);
93  }
94
95  const RelocAddrMap *getRelocMap() const { return RelocMap; }
96
97  bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
98
99  /// extractRangeList - extracts the range list referenced by this compile
100  /// unit from .debug_ranges section. Returns true on success.
101  /// Requires that compile unit is already extracted.
102  bool extractRangeList(uint32_t RangeListOffset,
103                        DWARFDebugRangeList &RangeList) const;
104  void clear();
105  uint32_t getOffset() const { return Offset; }
106  /// Size in bytes of the compile unit header.
107  virtual uint32_t getSize() const { return 11; }
108  uint32_t getFirstDIEOffset() const { return Offset + getSize(); }
109  uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
110  /// Size in bytes of the .debug_info data associated with this compile unit.
111  size_t getDebugInfoSize() const { return Length + 4 - getSize(); }
112  uint32_t getLength() const { return Length; }
113  uint16_t getVersion() const { return Version; }
114  const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
115    return Abbrevs;
116  }
117  uint8_t getAddressByteSize() const { return AddrSize; }
118  uint64_t getBaseAddress() const { return BaseAddr; }
119
120  void setBaseAddress(uint64_t base_addr) {
121    BaseAddr = base_addr;
122  }
123
124  const DWARFDebugInfoEntryMinimal *
125  getCompileUnitDIE(bool extract_cu_die_only = true) {
126    extractDIEsIfNeeded(extract_cu_die_only);
127    return DieArray.empty() ? NULL : &DieArray[0];
128  }
129
130  const char *getCompilationDir();
131  uint64_t getDWOId();
132
133  void buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
134                              bool clear_dies_if_already_not_parsed,
135                              uint32_t CUOffsetInAranges);
136
137  /// getInlinedChainForAddress - fetches inlined chain for a given address.
138  /// Returns empty chain if there is no subprogram containing address. The
139  /// chain is valid as long as parsed compile unit DIEs are not cleared.
140  DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
141
142private:
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
169