blob: c28f0dd5997883a499a310a7cd68ea5e46b42cc0 [file] [log] [blame]
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00001//===-- DWARFFormValue.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
Alexey Samsonovcd614552013-04-17 08:29:02 +000010#include "llvm/DebugInfo/DWARFFormValue.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000011#include "DWARFCompileUnit.h"
Benjamin Kramer34f864f2011-09-15 16:57:13 +000012#include "DWARFContext.h"
Alexey Samsonovc5253232013-10-28 23:01:48 +000013#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/StringRef.h"
Eric Christopher806e03d2012-11-07 23:22:07 +000015#include "llvm/Support/Debug.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000016#include "llvm/Support/Dwarf.h"
17#include "llvm/Support/Format.h"
18#include "llvm/Support/raw_ostream.h"
19#include <cassert>
20using namespace llvm;
21using namespace dwarf;
22
Benjamin Krameracc897a2013-04-11 11:36:36 +000023namespace {
24template <uint8_t AddrSize, uint8_t RefAddrSize> struct FixedFormSizes {
Timur Iskhodzhanov45d2e642013-10-29 12:13:22 +000025 // FIXME: do we need a template here? Will a stack-allocated struct with
26 // an initializer in getFixedFormSizes() work just fine?
27 static const uint8_t sizes[27];
Alexey Samsonov32a3e782013-04-09 14:09:42 +000028};
Benjamin Krameracc897a2013-04-11 11:36:36 +000029}
Alexey Samsonov32a3e782013-04-09 14:09:42 +000030
31template <uint8_t AddrSize, uint8_t RefAddrSize>
32const uint8_t FixedFormSizes<AddrSize, RefAddrSize>::sizes[] = {
Eric Christopher86a0f192013-05-30 00:43:30 +000033 0, // 0x00 unused
34 AddrSize, // 0x01 DW_FORM_addr
35 0, // 0x02 unused
36 0, // 0x03 DW_FORM_block2
37 0, // 0x04 DW_FORM_block4
38 2, // 0x05 DW_FORM_data2
39 4, // 0x06 DW_FORM_data4
40 8, // 0x07 DW_FORM_data8
41 0, // 0x08 DW_FORM_string
42 0, // 0x09 DW_FORM_block
43 0, // 0x0a DW_FORM_block1
44 1, // 0x0b DW_FORM_data1
45 1, // 0x0c DW_FORM_flag
46 0, // 0x0d DW_FORM_sdata
47 4, // 0x0e DW_FORM_strp
48 0, // 0x0f DW_FORM_udata
Alexey Samsonov32a3e782013-04-09 14:09:42 +000049 RefAddrSize, // 0x10 DW_FORM_ref_addr
Eric Christopher86a0f192013-05-30 00:43:30 +000050 1, // 0x11 DW_FORM_ref1
51 2, // 0x12 DW_FORM_ref2
52 4, // 0x13 DW_FORM_ref4
53 8, // 0x14 DW_FORM_ref8
54 0, // 0x15 DW_FORM_ref_udata
55 0, // 0x16 DW_FORM_indirect
56 4, // 0x17 DW_FORM_sec_offset
57 0, // 0x18 DW_FORM_exprloc
58 0, // 0x19 DW_FORM_flag_present
59 8, // 0x20 DW_FORM_ref_sig8
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000060};
61
Alexey Samsonov32a3e782013-04-09 14:09:42 +000062static uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
63 // FIXME: Support DWARF64.
64 return (Version == 2) ? AddrSize : 4;
65}
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000066
Alexey Samsonov6faff482013-10-28 23:41:49 +000067ArrayRef<uint8_t> DWARFFormValue::getFixedFormSizes(uint8_t AddrSize,
68 uint16_t Version) {
Alexey Samsonov32a3e782013-04-09 14:09:42 +000069 uint8_t RefAddrSize = getRefAddrSize(AddrSize, Version);
70 if (AddrSize == 4 && RefAddrSize == 4)
Richard Smith8fea6c82013-10-29 01:44:23 +000071 return makeArrayRef(FixedFormSizes<4, 4>::sizes);
Alexey Samsonov32a3e782013-04-09 14:09:42 +000072 if (AddrSize == 4 && RefAddrSize == 8)
Richard Smith8fea6c82013-10-29 01:44:23 +000073 return makeArrayRef(FixedFormSizes<4, 8>::sizes);
Alexey Samsonov32a3e782013-04-09 14:09:42 +000074 if (AddrSize == 8 && RefAddrSize == 4)
Richard Smith8fea6c82013-10-29 01:44:23 +000075 return makeArrayRef(FixedFormSizes<8, 4>::sizes);
Alexey Samsonov32a3e782013-04-09 14:09:42 +000076 if (AddrSize == 8 && RefAddrSize == 8)
Richard Smith8fea6c82013-10-29 01:44:23 +000077 return makeArrayRef(FixedFormSizes<8, 8>::sizes);
Alexey Samsonov6faff482013-10-28 23:41:49 +000078 return None;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000079}
80
Alexey Samsonovc5253232013-10-28 23:01:48 +000081static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
82 DWARFFormValue::FC_Unknown, // 0x0
83 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
84 DWARFFormValue::FC_Unknown, // 0x02 unused
85 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
86 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
87 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
88 // --- These can be FC_SectionOffset in DWARF3 and below:
89 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
90 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
91 // ---
92 DWARFFormValue::FC_String, // 0x08 DW_FORM_string
93 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
94 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
95 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
96 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
97 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
98 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
99 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
100 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
101 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
102 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
103 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
104 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
105 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
106 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
107 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
108 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
109 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
110 DWARFFormValue::FC_Reference, // 0x20 DW_FORM_ref_sig8
111};
112
113bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
114 // First, check DWARF4 form classes.
115 if (Form < ArrayRef<FormClass>(DWARF4FormClasses).size() &&
116 DWARF4FormClasses[Form] == FC)
117 return true;
118 // Check for some DWARF5 forms.
119 if (Form == DW_FORM_GNU_addr_index)
120 return (FC == FC_Address);
121 if (Form == DW_FORM_GNU_str_index)
122 return (FC == FC_String);
123 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
124 // Don't check for DWARF version here, as some producers may still do this
125 // by mistake.
126 if ((Form == DW_FORM_data4 || Form == DW_FORM_data8) &&
127 FC == FC_SectionOffset)
128 return true;
129 return false;
130}
131
David Blaikiecd7c4982013-09-23 22:44:40 +0000132bool DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
133 const DWARFUnit *cu) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000134 bool indirect = false;
135 bool is_block = false;
136 Value.data = NULL;
137 // Read the value for the form into value and follow and DW_FORM_indirect
138 // instances we run into
139 do {
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000140 indirect = false;
141 switch (Form) {
142 case DW_FORM_addr:
Eric Christopher806e03d2012-11-07 23:22:07 +0000143 case DW_FORM_ref_addr: {
Alexey Samsonov32a3e782013-04-09 14:09:42 +0000144 uint16_t AddrSize =
145 (Form == DW_FORM_addr)
146 ? cu->getAddressByteSize()
147 : getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
148 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
Eric Christopher82de10a2013-01-02 23:52:13 +0000149 if (AI != cu->getRelocMap()->end()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000150 const std::pair<uint8_t, int64_t> &R = AI->second;
Alexey Samsonov32a3e782013-04-09 14:09:42 +0000151 Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
Eric Christopherd1726a42012-11-12 21:40:38 +0000152 } else
Alexey Samsonov32a3e782013-04-09 14:09:42 +0000153 Value.uval = data.getUnsigned(offset_ptr, AddrSize);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000154 break;
Alexey Samsonov4c0ae902012-11-12 14:25:36 +0000155 }
Eric Christopher3887a902012-08-24 01:14:23 +0000156 case DW_FORM_exprloc:
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000157 case DW_FORM_block:
158 Value.uval = data.getULEB128(offset_ptr);
159 is_block = true;
160 break;
161 case DW_FORM_block1:
162 Value.uval = data.getU8(offset_ptr);
163 is_block = true;
164 break;
165 case DW_FORM_block2:
166 Value.uval = data.getU16(offset_ptr);
167 is_block = true;
168 break;
169 case DW_FORM_block4:
170 Value.uval = data.getU32(offset_ptr);
171 is_block = true;
172 break;
173 case DW_FORM_data1:
174 case DW_FORM_ref1:
175 case DW_FORM_flag:
176 Value.uval = data.getU8(offset_ptr);
177 break;
178 case DW_FORM_data2:
179 case DW_FORM_ref2:
180 Value.uval = data.getU16(offset_ptr);
181 break;
182 case DW_FORM_data4:
David Blaikie3df7d2f2013-06-19 21:37:13 +0000183 case DW_FORM_ref4: {
184 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000185 Value.uval = data.getU32(offset_ptr);
David Blaikie3df7d2f2013-06-19 21:37:13 +0000186 if (AI != cu->getRelocMap()->end())
187 Value.uval += AI->second.second;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000188 break;
David Blaikie3df7d2f2013-06-19 21:37:13 +0000189 }
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000190 case DW_FORM_data8:
191 case DW_FORM_ref8:
192 Value.uval = data.getU64(offset_ptr);
193 break;
194 case DW_FORM_sdata:
195 Value.sval = data.getSLEB128(offset_ptr);
196 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000197 case DW_FORM_strp: {
Eric Christopherd1726a42012-11-12 21:40:38 +0000198 RelocAddrMap::const_iterator AI
Eric Christopher82de10a2013-01-02 23:52:13 +0000199 = cu->getRelocMap()->find(*offset_ptr);
200 if (AI != cu->getRelocMap()->end()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000201 const std::pair<uint8_t, int64_t> &R = AI->second;
Eric Christopher32b37682012-12-27 01:07:07 +0000202 Value.uval = data.getU32(offset_ptr) + R.second;
Eric Christopherd1726a42012-11-12 21:40:38 +0000203 } else
Eric Christopher806e03d2012-11-07 23:22:07 +0000204 Value.uval = data.getU32(offset_ptr);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000205 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000206 }
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000207 case DW_FORM_udata:
208 case DW_FORM_ref_udata:
209 Value.uval = data.getULEB128(offset_ptr);
210 break;
211 case DW_FORM_string:
212 Value.cstr = data.getCStr(offset_ptr);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000213 break;
214 case DW_FORM_indirect:
215 Form = data.getULEB128(offset_ptr);
216 indirect = true;
217 break;
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000218 case DW_FORM_sec_offset: {
Eric Christopherd96c72a2013-01-17 02:59:59 +0000219 // FIXME: This is 64-bit for DWARF64.
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000220 RelocAddrMap::const_iterator AI
221 = cu->getRelocMap()->find(*offset_ptr);
222 if (AI != cu->getRelocMap()->end()) {
223 const std::pair<uint8_t, int64_t> &R = AI->second;
224 Value.uval = data.getU32(offset_ptr) + R.second;
225 } else
226 Value.uval = data.getU32(offset_ptr);
Eric Christopher3887a902012-08-24 01:14:23 +0000227 break;
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000228 }
Eric Christopher3887a902012-08-24 01:14:23 +0000229 case DW_FORM_flag_present:
230 Value.uval = 1;
231 break;
232 case DW_FORM_ref_sig8:
233 Value.uval = data.getU64(offset_ptr);
234 break;
Eric Christopher205e60b2012-11-16 23:44:11 +0000235 case DW_FORM_GNU_addr_index:
Eric Christopher205e60b2012-11-16 23:44:11 +0000236 case DW_FORM_GNU_str_index:
237 Value.uval = data.getULEB128(offset_ptr);
238 break;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000239 default:
240 return false;
241 }
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000242 } while (indirect);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000243
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000244 if (is_block) {
245 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
246 Value.data = NULL;
247 if (!str.empty()) {
248 Value.data = reinterpret_cast<const uint8_t *>(str.data());
249 *offset_ptr += Value.uval;
250 }
251 }
252
253 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000254}
255
256bool
257DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
David Blaikiecd7c4982013-09-23 22:44:40 +0000258 const DWARFUnit *cu) const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000259 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000260}
261
262bool
263DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
David Blaikiecd7c4982013-09-23 22:44:40 +0000264 uint32_t *offset_ptr, const DWARFUnit *cu) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000265 bool indirect = false;
266 do {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000267 switch (form) {
268 // Blocks if inlined data that have a length field and the data bytes
269 // inlined in the .debug_info
Eric Christopher3887a902012-08-24 01:14:23 +0000270 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000271 case DW_FORM_block: {
272 uint64_t size = debug_info_data.getULEB128(offset_ptr);
273 *offset_ptr += size;
274 return true;
275 }
276 case DW_FORM_block1: {
277 uint8_t size = debug_info_data.getU8(offset_ptr);
278 *offset_ptr += size;
279 return true;
280 }
281 case DW_FORM_block2: {
282 uint16_t size = debug_info_data.getU16(offset_ptr);
283 *offset_ptr += size;
284 return true;
285 }
286 case DW_FORM_block4: {
287 uint32_t size = debug_info_data.getU32(offset_ptr);
288 *offset_ptr += size;
289 return true;
290 }
291
292 // Inlined NULL terminated C-strings
293 case DW_FORM_string:
294 debug_info_data.getCStr(offset_ptr);
295 return true;
296
297 // Compile unit address sized values
298 case DW_FORM_addr:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000299 *offset_ptr += cu->getAddressByteSize();
300 return true;
Alexey Samsonov32a3e782013-04-09 14:09:42 +0000301 case DW_FORM_ref_addr:
302 *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
303 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000304
Eric Christopher3887a902012-08-24 01:14:23 +0000305 // 0 byte values - implied from the form.
306 case DW_FORM_flag_present:
307 return true;
Eric Christophere7285c72013-01-07 22:40:48 +0000308
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000309 // 1 byte values
310 case DW_FORM_data1:
311 case DW_FORM_flag:
312 case DW_FORM_ref1:
313 *offset_ptr += 1;
314 return true;
315
316 // 2 byte values
317 case DW_FORM_data2:
318 case DW_FORM_ref2:
319 *offset_ptr += 2;
320 return true;
321
322 // 4 byte values
323 case DW_FORM_strp:
324 case DW_FORM_data4:
325 case DW_FORM_ref4:
326 *offset_ptr += 4;
327 return true;
328
329 // 8 byte values
330 case DW_FORM_data8:
331 case DW_FORM_ref8:
Eric Christopher3887a902012-08-24 01:14:23 +0000332 case DW_FORM_ref_sig8:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000333 *offset_ptr += 8;
334 return true;
335
336 // signed or unsigned LEB 128 values
337 // case DW_FORM_APPLE_db_str:
338 case DW_FORM_sdata:
339 case DW_FORM_udata:
340 case DW_FORM_ref_udata:
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000341 case DW_FORM_GNU_str_index:
342 case DW_FORM_GNU_addr_index:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000343 debug_info_data.getULEB128(offset_ptr);
344 return true;
345
346 case DW_FORM_indirect:
347 indirect = true;
348 form = debug_info_data.getULEB128(offset_ptr);
349 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000350
Eric Christopher446b88f2013-01-17 03:00:04 +0000351 // FIXME: 4 for DWARF32, 8 for DWARF64.
Eric Christopher3887a902012-08-24 01:14:23 +0000352 case DW_FORM_sec_offset:
Eric Christopher446b88f2013-01-17 03:00:04 +0000353 *offset_ptr += 4;
Eric Christopher3887a902012-08-24 01:14:23 +0000354 return true;
Eric Christophere7285c72013-01-07 22:40:48 +0000355
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000356 default:
357 return false;
358 }
359 } while (indirect);
360 return true;
361}
362
363void
David Blaikiecd7c4982013-09-23 22:44:40 +0000364DWARFFormValue::dump(raw_ostream &OS, const DWARFUnit *cu) const {
Eric Christopher82de10a2013-01-02 23:52:13 +0000365 DataExtractor debug_str_data(cu->getStringSection(), true, 0);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000366 DataExtractor debug_str_offset_data(cu->getStringOffsetSection(), true, 0);
Alexey Samsonovc5253232013-10-28 23:01:48 +0000367 uint64_t uvalue = Value.uval;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000368 bool cu_relative_offset = false;
369
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000370 switch (Form) {
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000371 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000372 case DW_FORM_GNU_addr_index: {
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000373 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000374 uint64_t Address;
375 if (cu->getAddrOffsetSectionItem(uvalue, Address))
376 OS << format("0x%016" PRIx64, Address);
377 else
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000378 OS << "<no .debug_addr section>";
379 break;
380 }
Eric Christopher3887a902012-08-24 01:14:23 +0000381 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000382 case DW_FORM_flag:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000383 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
384 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
385 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopher3887a902012-08-24 01:14:23 +0000386 case DW_FORM_ref_sig8:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000387 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000388 case DW_FORM_string:
389 OS << '"';
Alexey Samsonovc5253232013-10-28 23:01:48 +0000390 OS.write_escaped(Value.cstr);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000391 OS << '"';
392 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000393 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000394 case DW_FORM_block:
395 case DW_FORM_block1:
396 case DW_FORM_block2:
397 case DW_FORM_block4:
398 if (uvalue > 0) {
399 switch (Form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000400 case DW_FORM_exprloc:
Benjamin Kramer41a96492011-11-05 08:57:40 +0000401 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000402 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
403 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
404 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
405 default: break;
406 }
407
408 const uint8_t* data_ptr = Value.data;
409 if (data_ptr) {
410 // uvalue contains size of block
411 const uint8_t* end_data_ptr = data_ptr + uvalue;
412 while (data_ptr < end_data_ptr) {
413 OS << format("%2.2x ", *data_ptr);
414 ++data_ptr;
415 }
416 }
417 else
418 OS << "NULL";
419 }
420 break;
421
Alexey Samsonovc5253232013-10-28 23:01:48 +0000422 case DW_FORM_sdata: OS << Value.sval; break;
423 case DW_FORM_udata: OS << Value.uval; break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000424 case DW_FORM_strp: {
425 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
Alexey Samsonovc5253232013-10-28 23:01:48 +0000426 Optional<const char *> DbgStr = getAsCString(cu);
427 if (DbgStr.hasValue()) {
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000428 OS << '"';
Alexey Samsonovc5253232013-10-28 23:01:48 +0000429 OS.write_escaped(DbgStr.getValue());
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000430 OS << '"';
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000431 }
432 break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000433 }
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000434 case DW_FORM_GNU_str_index: {
435 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
Alexey Samsonovc5253232013-10-28 23:01:48 +0000436 Optional<const char *> DbgStr = getAsCString(cu);
437 if (DbgStr.hasValue()) {
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000438 OS << '"';
Alexey Samsonovc5253232013-10-28 23:01:48 +0000439 OS.write_escaped(DbgStr.getValue());
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000440 OS << '"';
441 }
442 break;
443 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000444 case DW_FORM_ref_addr:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000445 OS << format("0x%016" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000446 break;
447 case DW_FORM_ref1:
448 cu_relative_offset = true;
449 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
450 break;
451 case DW_FORM_ref2:
452 cu_relative_offset = true;
453 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
454 break;
455 case DW_FORM_ref4:
456 cu_relative_offset = true;
457 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
458 break;
459 case DW_FORM_ref8:
460 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000461 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000462 break;
463 case DW_FORM_ref_udata:
464 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000465 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000466 break;
467
468 // All DW_FORM_indirect attributes should be resolved prior to calling
469 // this function
470 case DW_FORM_indirect:
471 OS << "DW_FORM_indirect";
472 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000473
Eric Christopher446b88f2013-01-17 03:00:04 +0000474 // Should be formatted to 64-bit for DWARF64.
Eric Christopher3887a902012-08-24 01:14:23 +0000475 case DW_FORM_sec_offset:
Eric Christopher446b88f2013-01-17 03:00:04 +0000476 OS << format("0x%08x", (uint32_t)uvalue);
Eric Christopher3887a902012-08-24 01:14:23 +0000477 break;
Eric Christophere7285c72013-01-07 22:40:48 +0000478
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000479 default:
480 OS << format("DW_FORM(0x%4.4x)", Form);
481 break;
482 }
483
484 if (cu_relative_offset)
Benjamin Kramere25a2bd2012-04-04 20:33:56 +0000485 OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000486}
487
Alexey Samsonovc5253232013-10-28 23:01:48 +0000488Optional<const char *> DWARFFormValue::getAsCString(const DWARFUnit *U) const {
489 if (!isFormClass(FC_String))
490 return None;
491 if (Form == DW_FORM_string)
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000492 return Value.cstr;
Alexey Samsonovc5253232013-10-28 23:01:48 +0000493 if (U == 0)
494 return None;
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000495 uint32_t Offset = Value.uval;
Alexey Samsonov8d433bd2013-10-18 07:13:32 +0000496 if (Form == DW_FORM_GNU_str_index) {
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000497 uint32_t StrOffset;
Alexey Samsonovc5253232013-10-28 23:01:48 +0000498 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
499 return None;
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000500 Offset = StrOffset;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000501 }
Alexey Samsonovc5253232013-10-28 23:01:48 +0000502 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
503 return Str;
504 }
505 return None;
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000506}
507
Alexey Samsonovc5253232013-10-28 23:01:48 +0000508Optional<uint64_t> DWARFFormValue::getAsAddress(const DWARFUnit *U) const {
509 if (!isFormClass(FC_Address))
510 return None;
Alexey Samsonov8d433bd2013-10-18 07:13:32 +0000511 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000512 uint32_t Index = Value.uval;
Alexey Samsonovc5253232013-10-28 23:01:48 +0000513 uint64_t Result;
514 if (U == 0 || !U->getAddrOffsetSectionItem(Index, Result))
515 return None;
516 return Result;
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000517 }
518 return Value.uval;
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000519}
520
Alexey Samsonovc5253232013-10-28 23:01:48 +0000521Optional<uint64_t> DWARFFormValue::getAsReference(const DWARFUnit *U) const {
522 if (!isFormClass(FC_Reference))
523 return None;
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000524 switch (Form) {
525 case DW_FORM_ref1:
526 case DW_FORM_ref2:
527 case DW_FORM_ref4:
528 case DW_FORM_ref8:
529 case DW_FORM_ref_udata:
Alexey Samsonovc5253232013-10-28 23:01:48 +0000530 if (U == 0)
531 return None;
532 return Value.uval + U->getOffset();
533 case DW_FORM_ref_addr:
534 return Value.uval;
535 // FIXME: Add proper support for DW_FORM_ref_sig8
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000536 default:
Alexey Samsonovc5253232013-10-28 23:01:48 +0000537 return Value.uval;
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000538 }
Alexey Samsonovc5253232013-10-28 23:01:48 +0000539}
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000540
Alexey Samsonovc5253232013-10-28 23:01:48 +0000541Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
542 if (!isFormClass(FC_SectionOffset))
543 return None;
544 return Value.uval;
545}
546
547Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
548 if (!isFormClass(FC_Constant) || Form == DW_FORM_sdata)
549 return None;
550 return Value.uval;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000551}