blob: bdd65b77e4b6240709ef31f2660879a1a01fe599 [file] [log] [blame]
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00001//===-- DWARFCompileUnit.cpp ----------------------------------------------===//
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#include "DWARFCompileUnit.h"
11#include "DWARFContext.h"
12#include "DWARFFormValue.h"
13#include "llvm/Support/Dwarf.h"
14#include "llvm/Support/Format.h"
15#include "llvm/Support/raw_ostream.h"
16using namespace llvm;
17using namespace dwarf;
18
19DataExtractor DWARFCompileUnit::getDebugInfoExtractor() const {
20 return DataExtractor(Context.getInfoSection(),
21 Context.isLittleEndian(), getAddressByteSize());
22}
23
24bool DWARFCompileUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
25 clear();
26
27 Offset = *offset_ptr;
28
29 if (debug_info.isValidOffset(*offset_ptr)) {
30 uint64_t abbrOffset;
31 const DWARFDebugAbbrev *abbr = Context.getDebugAbbrev();
32 Length = debug_info.getU32(offset_ptr);
33 Version = debug_info.getU16(offset_ptr);
34 abbrOffset = debug_info.getU32(offset_ptr);
35 AddrSize = debug_info.getU8(offset_ptr);
36
37 bool lengthOK = debug_info.isValidOffset(getNextCompileUnitOffset()-1);
38 bool versionOK = DWARFContext::isSupportedVersion(Version);
39 bool abbrOffsetOK = Context.getAbbrevSection().size() > abbrOffset;
40 bool addrSizeOK = AddrSize == 4 || AddrSize == 8;
41
42 if (lengthOK && versionOK && addrSizeOK && abbrOffsetOK && abbr != NULL) {
43 Abbrevs = abbr->getAbbreviationDeclarationSet(abbrOffset);
44 return true;
45 }
46
47 // reset the offset to where we tried to parse from if anything went wrong
48 *offset_ptr = Offset;
49 }
50
51 return false;
52}
53
54uint32_t
55DWARFCompileUnit::extract(uint32_t offset, DataExtractor debug_info_data,
56 const DWARFAbbreviationDeclarationSet *abbrevs) {
57 clear();
58
59 Offset = offset;
60
61 if (debug_info_data.isValidOffset(offset)) {
62 Length = debug_info_data.getU32(&offset);
63 Version = debug_info_data.getU16(&offset);
64 bool abbrevsOK = debug_info_data.getU32(&offset) == abbrevs->getOffset();
65 Abbrevs = abbrevs;
Eric Christopher5d04a3a2012-08-23 23:26:57 +000066 AddrSize = debug_info_data.getU8(&offset);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000067
68 bool versionOK = DWARFContext::isSupportedVersion(Version);
69 bool addrSizeOK = AddrSize == 4 || AddrSize == 8;
70
71 if (versionOK && addrSizeOK && abbrevsOK &&
72 debug_info_data.isValidOffset(offset))
73 return offset;
74 }
75 return 0;
76}
77
Alexey Samsonov5eae90d2012-09-04 08:12:33 +000078bool DWARFCompileUnit::extractRangeList(uint32_t RangeListOffset,
79 DWARFDebugRangeList &RangeList) const {
80 // Require that compile unit is extracted.
81 assert(DieArray.size() > 0);
82 DataExtractor RangesData(Context.getRangeSection(),
83 Context.isLittleEndian(), AddrSize);
84 return RangeList.extract(RangesData, &RangeListOffset);
85}
86
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000087void DWARFCompileUnit::clear() {
88 Offset = 0;
89 Length = 0;
90 Version = 0;
91 Abbrevs = 0;
92 AddrSize = 0;
93 BaseAddr = 0;
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +000094 clearDIEs(false);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000095}
96
97void DWARFCompileUnit::dump(raw_ostream &OS) {
98 OS << format("0x%08x", Offset) << ": Compile Unit:"
99 << " length = " << format("0x%08x", Length)
100 << " version = " << format("0x%04x", Version)
101 << " abbr_offset = " << format("0x%04x", Abbrevs->getOffset())
102 << " addr_size = " << format("0x%02x", AddrSize)
103 << " (next CU at " << format("0x%08x", getNextCompileUnitOffset())
104 << ")\n";
105
Eric Christopherfa76f222012-08-23 23:21:11 +0000106 const DWARFDebugInfoEntryMinimal *CU = getCompileUnitDIE(false);
107 assert(CU && "Null Compile Unit?");
108 CU->dump(OS, this, -1U);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000109}
110
Alexey Samsonov71d94f82012-07-19 07:03:58 +0000111const char *DWARFCompileUnit::getCompilationDir() {
112 extractDIEsIfNeeded(true);
113 if (DieArray.empty())
114 return 0;
115 return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, 0);
116}
117
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000118void DWARFCompileUnit::setDIERelations() {
119 if (DieArray.empty())
120 return;
121 DWARFDebugInfoEntryMinimal *die_array_begin = &DieArray.front();
122 DWARFDebugInfoEntryMinimal *die_array_end = &DieArray.back();
123 DWARFDebugInfoEntryMinimal *curr_die;
124 // We purposely are skipping the last element in the array in the loop below
125 // so that we can always have a valid next item
126 for (curr_die = die_array_begin; curr_die < die_array_end; ++curr_die) {
127 // Since our loop doesn't include the last element, we can always
128 // safely access the next die in the array.
129 DWARFDebugInfoEntryMinimal *next_die = curr_die + 1;
130
131 const DWARFAbbreviationDeclaration *curr_die_abbrev =
132 curr_die->getAbbreviationDeclarationPtr();
133
134 if (curr_die_abbrev) {
135 // Normal DIE
136 if (curr_die_abbrev->hasChildren())
137 next_die->setParent(curr_die);
138 else
139 curr_die->setSibling(next_die);
140 } else {
141 // NULL DIE that terminates a sibling chain
142 DWARFDebugInfoEntryMinimal *parent = curr_die->getParent();
143 if (parent)
144 parent->setSibling(next_die);
145 }
146 }
147
148 // Since we skipped the last element, we need to fix it up!
149 if (die_array_begin < die_array_end)
150 curr_die->setParent(die_array_begin);
151}
152
153size_t DWARFCompileUnit::extractDIEsIfNeeded(bool cu_die_only) {
154 const size_t initial_die_array_size = DieArray.size();
155 if ((cu_die_only && initial_die_array_size > 0) ||
156 initial_die_array_size > 1)
157 return 0; // Already parsed
158
159 // Set the offset to that of the first DIE and calculate the start of the
160 // next compilation unit header.
161 uint32_t offset = getFirstDIEOffset();
162 uint32_t next_cu_offset = getNextCompileUnitOffset();
163
164 DWARFDebugInfoEntryMinimal die;
165 // Keep a flat array of the DIE for binary lookup by DIE offset
166 uint32_t depth = 0;
167 // We are in our compile unit, parse starting at the offset
168 // we were told to parse
169
170 const uint8_t *fixed_form_sizes =
171 DWARFFormValue::getFixedFormSizesForAddressSize(getAddressByteSize());
172
173 while (offset < next_cu_offset &&
174 die.extractFast(this, fixed_form_sizes, &offset)) {
175
176 if (depth == 0) {
177 uint64_t base_addr =
178 die.getAttributeValueAsUnsigned(this, DW_AT_low_pc, -1U);
179 if (base_addr == -1U)
180 base_addr = die.getAttributeValueAsUnsigned(this, DW_AT_entry_pc, 0);
181 setBaseAddress(base_addr);
182 }
183
184 if (cu_die_only) {
185 addDIE(die);
186 return 1;
187 }
Eric Christopher5d04a3a2012-08-23 23:26:57 +0000188 else if (depth == 0 && initial_die_array_size == 1)
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000189 // Don't append the CU die as we already did that
Eric Christopher5d04a3a2012-08-23 23:26:57 +0000190 ;
191 else
192 addDIE(die);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000193
194 const DWARFAbbreviationDeclaration *abbrDecl =
195 die.getAbbreviationDeclarationPtr();
196 if (abbrDecl) {
197 // Normal DIE
198 if (abbrDecl->hasChildren())
199 ++depth;
200 } else {
201 // NULL DIE.
202 if (depth > 0)
203 --depth;
204 if (depth == 0)
205 break; // We are done with this compile unit!
206 }
207
208 }
209
210 // Give a little bit of info if we encounter corrupt DWARF (our offset
211 // should always terminate at or before the start of the next compilation
212 // unit header).
Eric Christopher1a145c42012-08-23 00:52:49 +0000213 if (offset > next_cu_offset)
Eric Christopher5d04a3a2012-08-23 23:26:57 +0000214 fprintf(stderr, "warning: DWARF compile unit extends beyond its"
215 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), offset);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000216
217 setDIERelations();
218 return DieArray.size();
219}
Benjamin Kramer10df8062011-09-14 20:52:27 +0000220
221void DWARFCompileUnit::clearDIEs(bool keep_compile_unit_die) {
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000222 if (DieArray.size() > (unsigned)keep_compile_unit_die) {
Benjamin Kramer10df8062011-09-14 20:52:27 +0000223 // std::vectors never get any smaller when resized to a smaller size,
224 // or when clear() or erase() are called, the size will report that it
225 // is smaller, but the memory allocated remains intact (call capacity()
226 // to see this). So we need to create a temporary vector and swap the
227 // contents which will cause just the internal pointers to be swapped
228 // so that when "tmp_array" goes out of scope, it will destroy the
229 // contents.
230
231 // Save at least the compile unit DIE
232 std::vector<DWARFDebugInfoEntryMinimal> tmpArray;
233 DieArray.swap(tmpArray);
234 if (keep_compile_unit_die)
235 DieArray.push_back(tmpArray.front());
236 }
237}
238
239void
240DWARFCompileUnit::buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
241 bool clear_dies_if_already_not_parsed){
242 // This function is usually called if there in no .debug_aranges section
243 // in order to produce a compile unit level set of address ranges that
244 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
245 // all compile units to stay loaded when they weren't needed. So we can end
246 // up parsing the DWARF and then throwing them all away to keep memory usage
247 // down.
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000248 const bool clear_dies = extractDIEsIfNeeded(false) > 1 &&
249 clear_dies_if_already_not_parsed;
Benjamin Kramer10df8062011-09-14 20:52:27 +0000250 DieArray[0].buildAddressRangeTable(this, debug_aranges);
251
252 // Keep memory down by clearing DIEs if this generate function
253 // caused them to be parsed.
254 if (clear_dies)
255 clearDIEs(true);
256}
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000257
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000258DWARFDebugInfoEntryMinimal::InlinedChain
259DWARFCompileUnit::getInlinedChainForAddress(uint64_t Address) {
260 // First, find a subprogram that contains the given address (the root
261 // of inlined chain).
Alexey Samsonova9543aa2012-07-04 09:42:54 +0000262 extractDIEsIfNeeded(false);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000263 const DWARFDebugInfoEntryMinimal *SubprogramDIE = 0;
Alexey Samsonova9543aa2012-07-04 09:42:54 +0000264 for (size_t i = 0, n = DieArray.size(); i != n; i++) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000265 if (DieArray[i].isSubprogramDIE() &&
266 DieArray[i].addressRangeContainsAddress(this, Address)) {
267 SubprogramDIE = &DieArray[i];
268 break;
269 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000270 }
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000271 // Get inlined chain rooted at this subprogram DIE.
272 if (!SubprogramDIE)
273 return DWARFDebugInfoEntryMinimal::InlinedChain();
274 return SubprogramDIE->getInlinedChainForAddress(this, Address);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000275}