blob: 4d93de9e100a7669060cdd44e64ade352893e26a [file] [log] [blame]
David Blaikie07e22442013-09-23 22:44:40 +00001//===-- 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 uint32_t extract(uint32_t offset, DataExtractor debug_info_data,
99 const DWARFAbbreviationDeclarationSet *abbrevs);
100
101 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
102 /// hasn't already been done. Returns the number of DIEs parsed at this call.
103 size_t extractDIEsIfNeeded(bool CUDieOnly);
104 /// extractRangeList - extracts the range list referenced by this compile
105 /// unit from .debug_ranges section. Returns true on success.
106 /// Requires that compile unit is already extracted.
107 bool extractRangeList(uint32_t RangeListOffset,
108 DWARFDebugRangeList &RangeList) const;
109 void clear();
110 uint32_t getOffset() const { return Offset; }
111 /// Size in bytes of the compile unit header.
112 virtual uint32_t getSize() const { return 11; }
113 bool containsDIEOffset(uint32_t die_offset) const {
114 return die_offset >= getFirstDIEOffset() &&
115 die_offset < getNextUnitOffset();
116 }
117 uint32_t getFirstDIEOffset() const { return Offset + getSize(); }
118 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
119 /// Size in bytes of the .debug_info data associated with this compile unit.
120 size_t getDebugInfoSize() const { return Length + 4 - getSize(); }
121 uint32_t getLength() const { return Length; }
122 uint16_t getVersion() const { return Version; }
123 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
124 return Abbrevs;
125 }
126 uint8_t getAddressByteSize() const { return AddrSize; }
127 uint64_t getBaseAddress() const { return BaseAddr; }
128
129 void setBaseAddress(uint64_t base_addr) {
130 BaseAddr = base_addr;
131 }
132
133 const DWARFDebugInfoEntryMinimal *
134 getCompileUnitDIE(bool extract_cu_die_only = true) {
135 extractDIEsIfNeeded(extract_cu_die_only);
136 return DieArray.empty() ? NULL : &DieArray[0];
137 }
138
139 const char *getCompilationDir();
140 uint64_t getDWOId();
141
142 /// setDIERelations - We read in all of the DIE entries into our flat list
143 /// of DIE entries and now we need to go back through all of them and set the
144 /// parent, sibling and child pointers for quick DIE navigation.
145 void setDIERelations();
146
147 void buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
148 bool clear_dies_if_already_not_parsed,
149 uint32_t CUOffsetInAranges);
150
151 /// getInlinedChainForAddress - fetches inlined chain for a given address.
152 /// Returns empty chain if there is no subprogram containing address. The
153 /// chain is valid as long as parsed compile unit DIEs are not cleared.
154 DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
155
156private:
157 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
158 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
159 std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
160 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
161 void clearDIEs(bool KeepCUDie);
162
163 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
164 /// it was actually constructed.
165 bool parseDWO();
166
167 /// getSubprogramForAddress - Returns subprogram DIE with address range
168 /// encompassing the provided address. The pointer is alive as long as parsed
169 /// compile unit DIEs are not cleared.
170 const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);
171};
172
173}
174
175#endif