blob: d535df05d3b5bb29d7db3b10b5c10ae9a94d3a2f [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;
66 AddrSize = debug_info_data.getU8 (&offset);
67
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
78void DWARFCompileUnit::clear() {
79 Offset = 0;
80 Length = 0;
81 Version = 0;
82 Abbrevs = 0;
83 AddrSize = 0;
84 BaseAddr = 0;
85 DieArray.clear();
86}
87
88void DWARFCompileUnit::dump(raw_ostream &OS) {
89 OS << format("0x%08x", Offset) << ": Compile Unit:"
90 << " length = " << format("0x%08x", Length)
91 << " version = " << format("0x%04x", Version)
92 << " abbr_offset = " << format("0x%04x", Abbrevs->getOffset())
93 << " addr_size = " << format("0x%02x", AddrSize)
94 << " (next CU at " << format("0x%08x", getNextCompileUnitOffset())
95 << ")\n";
96
97 extractDIEsIfNeeded(false);
Benjamin Kramer15ec0852011-09-14 00:15:32 +000098 DieArray[0].dump(OS, this, -1U);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000099}
100
101void DWARFCompileUnit::setDIERelations() {
102 if (DieArray.empty())
103 return;
104 DWARFDebugInfoEntryMinimal *die_array_begin = &DieArray.front();
105 DWARFDebugInfoEntryMinimal *die_array_end = &DieArray.back();
106 DWARFDebugInfoEntryMinimal *curr_die;
107 // We purposely are skipping the last element in the array in the loop below
108 // so that we can always have a valid next item
109 for (curr_die = die_array_begin; curr_die < die_array_end; ++curr_die) {
110 // Since our loop doesn't include the last element, we can always
111 // safely access the next die in the array.
112 DWARFDebugInfoEntryMinimal *next_die = curr_die + 1;
113
114 const DWARFAbbreviationDeclaration *curr_die_abbrev =
115 curr_die->getAbbreviationDeclarationPtr();
116
117 if (curr_die_abbrev) {
118 // Normal DIE
119 if (curr_die_abbrev->hasChildren())
120 next_die->setParent(curr_die);
121 else
122 curr_die->setSibling(next_die);
123 } else {
124 // NULL DIE that terminates a sibling chain
125 DWARFDebugInfoEntryMinimal *parent = curr_die->getParent();
126 if (parent)
127 parent->setSibling(next_die);
128 }
129 }
130
131 // Since we skipped the last element, we need to fix it up!
132 if (die_array_begin < die_array_end)
133 curr_die->setParent(die_array_begin);
134}
135
136size_t DWARFCompileUnit::extractDIEsIfNeeded(bool cu_die_only) {
137 const size_t initial_die_array_size = DieArray.size();
138 if ((cu_die_only && initial_die_array_size > 0) ||
139 initial_die_array_size > 1)
140 return 0; // Already parsed
141
142 // Set the offset to that of the first DIE and calculate the start of the
143 // next compilation unit header.
144 uint32_t offset = getFirstDIEOffset();
145 uint32_t next_cu_offset = getNextCompileUnitOffset();
146
147 DWARFDebugInfoEntryMinimal die;
148 // Keep a flat array of the DIE for binary lookup by DIE offset
149 uint32_t depth = 0;
150 // We are in our compile unit, parse starting at the offset
151 // we were told to parse
152
153 const uint8_t *fixed_form_sizes =
154 DWARFFormValue::getFixedFormSizesForAddressSize(getAddressByteSize());
155
156 while (offset < next_cu_offset &&
157 die.extractFast(this, fixed_form_sizes, &offset)) {
158
159 if (depth == 0) {
160 uint64_t base_addr =
161 die.getAttributeValueAsUnsigned(this, DW_AT_low_pc, -1U);
162 if (base_addr == -1U)
163 base_addr = die.getAttributeValueAsUnsigned(this, DW_AT_entry_pc, 0);
164 setBaseAddress(base_addr);
165 }
166
167 if (cu_die_only) {
168 addDIE(die);
169 return 1;
170 }
171 else if (depth == 0 && initial_die_array_size == 1) {
172 // Don't append the CU die as we already did that
173 } else {
174 addDIE (die);
175 }
176
177 const DWARFAbbreviationDeclaration *abbrDecl =
178 die.getAbbreviationDeclarationPtr();
179 if (abbrDecl) {
180 // Normal DIE
181 if (abbrDecl->hasChildren())
182 ++depth;
183 } else {
184 // NULL DIE.
185 if (depth > 0)
186 --depth;
187 if (depth == 0)
188 break; // We are done with this compile unit!
189 }
190
191 }
192
193 // Give a little bit of info if we encounter corrupt DWARF (our offset
194 // should always terminate at or before the start of the next compilation
195 // unit header).
196 if (offset > next_cu_offset) {
197 fprintf (stderr, "warning: DWARF compile unit extends beyond its bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), offset);
198 }
199
200 setDIERelations();
201 return DieArray.size();
202}
Benjamin Kramer10df8062011-09-14 20:52:27 +0000203
204void DWARFCompileUnit::clearDIEs(bool keep_compile_unit_die) {
205 if (DieArray.size() > 1) {
206 // std::vectors never get any smaller when resized to a smaller size,
207 // or when clear() or erase() are called, the size will report that it
208 // is smaller, but the memory allocated remains intact (call capacity()
209 // to see this). So we need to create a temporary vector and swap the
210 // contents which will cause just the internal pointers to be swapped
211 // so that when "tmp_array" goes out of scope, it will destroy the
212 // contents.
213
214 // Save at least the compile unit DIE
215 std::vector<DWARFDebugInfoEntryMinimal> tmpArray;
216 DieArray.swap(tmpArray);
217 if (keep_compile_unit_die)
218 DieArray.push_back(tmpArray.front());
219 }
220}
221
222void
223DWARFCompileUnit::buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
224 bool clear_dies_if_already_not_parsed){
225 // This function is usually called if there in no .debug_aranges section
226 // in order to produce a compile unit level set of address ranges that
227 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
228 // all compile units to stay loaded when they weren't needed. So we can end
229 // up parsing the DWARF and then throwing them all away to keep memory usage
230 // down.
231 const bool clear_dies = extractDIEsIfNeeded(false) > 1;
232
233 DieArray[0].buildAddressRangeTable(this, debug_aranges);
234
235 // Keep memory down by clearing DIEs if this generate function
236 // caused them to be parsed.
237 if (clear_dies)
238 clearDIEs(true);
239}