blob: 07f7b446431d9026d2648510368e1cb563e8e77a [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
10#include "DWARFFormValue.h"
11#include "DWARFCompileUnit.h"
Benjamin Kramer34f864f2011-09-15 16:57:13 +000012#include "DWARFContext.h"
Eric Christopher806e03d2012-11-07 23:22:07 +000013#include "llvm/Support/Debug.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000014#include "llvm/Support/Dwarf.h"
15#include "llvm/Support/Format.h"
16#include "llvm/Support/raw_ostream.h"
17#include <cassert>
18using namespace llvm;
19using namespace dwarf;
20
21static const uint8_t form_sizes_addr4[] = {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000022 0, // 0x00 unused
23 4, // 0x01 DW_FORM_addr
24 0, // 0x02 unused
25 0, // 0x03 DW_FORM_block2
26 0, // 0x04 DW_FORM_block4
27 2, // 0x05 DW_FORM_data2
28 4, // 0x06 DW_FORM_data4
29 8, // 0x07 DW_FORM_data8
30 0, // 0x08 DW_FORM_string
31 0, // 0x09 DW_FORM_block
32 0, // 0x0a DW_FORM_block1
33 1, // 0x0b DW_FORM_data1
34 1, // 0x0c DW_FORM_flag
35 0, // 0x0d DW_FORM_sdata
36 4, // 0x0e DW_FORM_strp
37 0, // 0x0f DW_FORM_udata
38 4, // 0x10 DW_FORM_ref_addr
39 1, // 0x11 DW_FORM_ref1
40 2, // 0x12 DW_FORM_ref2
41 4, // 0x13 DW_FORM_ref4
42 8, // 0x14 DW_FORM_ref8
43 0, // 0x15 DW_FORM_ref_udata
44 0, // 0x16 DW_FORM_indirect
Eric Christopher3887a902012-08-24 01:14:23 +000045 4, // 0x17 DW_FORM_sec_offset
46 0, // 0x18 DW_FORM_exprloc
47 0, // 0x19 DW_FORM_flag_present
48 8, // 0x20 DW_FORM_ref_sig8
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000049};
50
51static const uint8_t form_sizes_addr8[] = {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000052 0, // 0x00 unused
53 8, // 0x01 DW_FORM_addr
54 0, // 0x02 unused
55 0, // 0x03 DW_FORM_block2
56 0, // 0x04 DW_FORM_block4
57 2, // 0x05 DW_FORM_data2
58 4, // 0x06 DW_FORM_data4
59 8, // 0x07 DW_FORM_data8
60 0, // 0x08 DW_FORM_string
61 0, // 0x09 DW_FORM_block
62 0, // 0x0a DW_FORM_block1
63 1, // 0x0b DW_FORM_data1
64 1, // 0x0c DW_FORM_flag
65 0, // 0x0d DW_FORM_sdata
66 4, // 0x0e DW_FORM_strp
67 0, // 0x0f DW_FORM_udata
68 8, // 0x10 DW_FORM_ref_addr
69 1, // 0x11 DW_FORM_ref1
70 2, // 0x12 DW_FORM_ref2
71 4, // 0x13 DW_FORM_ref4
72 8, // 0x14 DW_FORM_ref8
73 0, // 0x15 DW_FORM_ref_udata
74 0, // 0x16 DW_FORM_indirect
Eric Christopher3887a902012-08-24 01:14:23 +000075 8, // 0x17 DW_FORM_sec_offset
76 0, // 0x18 DW_FORM_exprloc
77 0, // 0x19 DW_FORM_flag_present
78 8, // 0x20 DW_FORM_ref_sig8
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000079};
80
81const uint8_t *
82DWARFFormValue::getFixedFormSizesForAddressSize(uint8_t addr_size) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000083 switch (addr_size) {
84 case 4: return form_sizes_addr4;
85 case 8: return form_sizes_addr8;
86 }
87 return NULL;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000088}
89
90bool
91DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
Benjamin Kramer89aedba2011-09-15 03:11:09 +000092 const DWARFCompileUnit *cu) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000093 bool indirect = false;
94 bool is_block = false;
95 Value.data = NULL;
96 // Read the value for the form into value and follow and DW_FORM_indirect
97 // instances we run into
98 do {
Benjamin Kramer89aedba2011-09-15 03:11:09 +000099 indirect = false;
100 switch (Form) {
101 case DW_FORM_addr:
Eric Christopher806e03d2012-11-07 23:22:07 +0000102 case DW_FORM_ref_addr: {
Eric Christopherd1726a42012-11-12 21:40:38 +0000103 RelocAddrMap::const_iterator AI
104 = cu->getContext().relocMap().find(*offset_ptr);
105 if (AI != cu->getContext().relocMap().end()) {
106 const std::pair<uint8_t, int64_t> &R = AI->second;
107 Value.uval = R.second;
108 *offset_ptr += R.first;
109 } else
Eric Christopher806e03d2012-11-07 23:22:07 +0000110 Value.uval = data.getUnsigned(offset_ptr, cu->getAddressByteSize());
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000111 break;
Alexey Samsonov4c0ae902012-11-12 14:25:36 +0000112 }
Eric Christopher3887a902012-08-24 01:14:23 +0000113 case DW_FORM_exprloc:
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000114 case DW_FORM_block:
115 Value.uval = data.getULEB128(offset_ptr);
116 is_block = true;
117 break;
118 case DW_FORM_block1:
119 Value.uval = data.getU8(offset_ptr);
120 is_block = true;
121 break;
122 case DW_FORM_block2:
123 Value.uval = data.getU16(offset_ptr);
124 is_block = true;
125 break;
126 case DW_FORM_block4:
127 Value.uval = data.getU32(offset_ptr);
128 is_block = true;
129 break;
130 case DW_FORM_data1:
131 case DW_FORM_ref1:
132 case DW_FORM_flag:
133 Value.uval = data.getU8(offset_ptr);
134 break;
135 case DW_FORM_data2:
136 case DW_FORM_ref2:
137 Value.uval = data.getU16(offset_ptr);
138 break;
139 case DW_FORM_data4:
140 case DW_FORM_ref4:
141 Value.uval = data.getU32(offset_ptr);
142 break;
143 case DW_FORM_data8:
144 case DW_FORM_ref8:
145 Value.uval = data.getU64(offset_ptr);
146 break;
147 case DW_FORM_sdata:
148 Value.sval = data.getSLEB128(offset_ptr);
149 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000150 case DW_FORM_strp: {
Eric Christopherd1726a42012-11-12 21:40:38 +0000151 RelocAddrMap::const_iterator AI
152 = cu->getContext().relocMap().find(*offset_ptr);
153 if (AI != cu->getContext().relocMap().end()) {
154 const std::pair<uint8_t, int64_t> &R = AI->second;
155 Value.uval = R.second;
156 *offset_ptr += R.first;
157 } else
Eric Christopher806e03d2012-11-07 23:22:07 +0000158 Value.uval = data.getU32(offset_ptr);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000159 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000160 }
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000161 case DW_FORM_udata:
162 case DW_FORM_ref_udata:
163 Value.uval = data.getULEB128(offset_ptr);
164 break;
165 case DW_FORM_string:
166 Value.cstr = data.getCStr(offset_ptr);
167 // Set the string value to also be the data for inlined cstr form
168 // values only so we can tell the differnence between DW_FORM_string
169 // and DW_FORM_strp form values
Roman Divacky59324292012-09-05 22:26:57 +0000170 Value.data = (const uint8_t*)Value.cstr;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000171 break;
172 case DW_FORM_indirect:
173 Form = data.getULEB128(offset_ptr);
174 indirect = true;
175 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000176 case DW_FORM_sec_offset:
177 if (cu->getAddressByteSize() == 4)
178 Value.uval = data.getU32(offset_ptr);
179 else
180 Value.uval = data.getU64(offset_ptr);
181 break;
182 case DW_FORM_flag_present:
183 Value.uval = 1;
184 break;
185 case DW_FORM_ref_sig8:
186 Value.uval = data.getU64(offset_ptr);
187 break;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000188 default:
189 return false;
190 }
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000191 } while (indirect);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000192
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000193 if (is_block) {
194 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
195 Value.data = NULL;
196 if (!str.empty()) {
197 Value.data = reinterpret_cast<const uint8_t *>(str.data());
198 *offset_ptr += Value.uval;
199 }
200 }
201
202 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000203}
204
205bool
206DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000207 const DWARFCompileUnit *cu) const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000208 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000209}
210
211bool
212DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
213 uint32_t *offset_ptr, const DWARFCompileUnit *cu) {
214 bool indirect = false;
215 do {
216 indirect = false;
217 switch (form) {
218 // Blocks if inlined data that have a length field and the data bytes
219 // inlined in the .debug_info
Eric Christopher3887a902012-08-24 01:14:23 +0000220 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000221 case DW_FORM_block: {
222 uint64_t size = debug_info_data.getULEB128(offset_ptr);
223 *offset_ptr += size;
224 return true;
225 }
226 case DW_FORM_block1: {
227 uint8_t size = debug_info_data.getU8(offset_ptr);
228 *offset_ptr += size;
229 return true;
230 }
231 case DW_FORM_block2: {
232 uint16_t size = debug_info_data.getU16(offset_ptr);
233 *offset_ptr += size;
234 return true;
235 }
236 case DW_FORM_block4: {
237 uint32_t size = debug_info_data.getU32(offset_ptr);
238 *offset_ptr += size;
239 return true;
240 }
241
242 // Inlined NULL terminated C-strings
243 case DW_FORM_string:
244 debug_info_data.getCStr(offset_ptr);
245 return true;
246
247 // Compile unit address sized values
248 case DW_FORM_addr:
249 case DW_FORM_ref_addr:
250 *offset_ptr += cu->getAddressByteSize();
251 return true;
252
Eric Christopher3887a902012-08-24 01:14:23 +0000253 // 0 byte values - implied from the form.
254 case DW_FORM_flag_present:
255 return true;
256
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000257 // 1 byte values
258 case DW_FORM_data1:
259 case DW_FORM_flag:
260 case DW_FORM_ref1:
261 *offset_ptr += 1;
262 return true;
263
264 // 2 byte values
265 case DW_FORM_data2:
266 case DW_FORM_ref2:
267 *offset_ptr += 2;
268 return true;
269
270 // 4 byte values
271 case DW_FORM_strp:
272 case DW_FORM_data4:
273 case DW_FORM_ref4:
274 *offset_ptr += 4;
275 return true;
276
277 // 8 byte values
278 case DW_FORM_data8:
279 case DW_FORM_ref8:
Eric Christopher3887a902012-08-24 01:14:23 +0000280 case DW_FORM_ref_sig8:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000281 *offset_ptr += 8;
282 return true;
283
284 // signed or unsigned LEB 128 values
285 // case DW_FORM_APPLE_db_str:
286 case DW_FORM_sdata:
287 case DW_FORM_udata:
288 case DW_FORM_ref_udata:
289 debug_info_data.getULEB128(offset_ptr);
290 return true;
291
292 case DW_FORM_indirect:
293 indirect = true;
294 form = debug_info_data.getULEB128(offset_ptr);
295 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000296
297 // 4 for DWARF32, 8 for DWARF64.
298 case DW_FORM_sec_offset:
299 if (cu->getAddressByteSize() == 4)
300 *offset_ptr += 4;
301 else
302 *offset_ptr += 8;
303 return true;
304
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000305 default:
306 return false;
307 }
308 } while (indirect);
309 return true;
310}
311
312void
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000313DWARFFormValue::dump(raw_ostream &OS, const DWARFCompileUnit *cu) const {
314 DataExtractor debug_str_data(cu->getContext().getStringSection(), true, 0);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000315 uint64_t uvalue = getUnsigned();
316 bool cu_relative_offset = false;
317
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000318 switch (Form) {
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000319 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher3887a902012-08-24 01:14:23 +0000320 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000321 case DW_FORM_flag:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000322 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
323 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
324 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopher3887a902012-08-24 01:14:23 +0000325 case DW_FORM_ref_sig8:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000326 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000327 case DW_FORM_string:
328 OS << '"';
329 OS.write_escaped(getAsCString(NULL));
330 OS << '"';
331 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000332 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000333 case DW_FORM_block:
334 case DW_FORM_block1:
335 case DW_FORM_block2:
336 case DW_FORM_block4:
337 if (uvalue > 0) {
338 switch (Form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000339 case DW_FORM_exprloc:
Benjamin Kramer41a96492011-11-05 08:57:40 +0000340 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000341 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
342 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
343 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
344 default: break;
345 }
346
347 const uint8_t* data_ptr = Value.data;
348 if (data_ptr) {
349 // uvalue contains size of block
350 const uint8_t* end_data_ptr = data_ptr + uvalue;
351 while (data_ptr < end_data_ptr) {
352 OS << format("%2.2x ", *data_ptr);
353 ++data_ptr;
354 }
355 }
356 else
357 OS << "NULL";
358 }
359 break;
360
361 case DW_FORM_sdata: OS << getSigned(); break;
362 case DW_FORM_udata: OS << getUnsigned(); break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000363 case DW_FORM_strp: {
364 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
365 const char* dbg_str = getAsCString(&debug_str_data);
366 if (dbg_str) {
367 OS << '"';
368 OS.write_escaped(dbg_str);
369 OS << '"';
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000370 }
371 break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000372 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000373 case DW_FORM_ref_addr:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000374 OS << format("0x%016" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000375 break;
376 case DW_FORM_ref1:
377 cu_relative_offset = true;
378 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
379 break;
380 case DW_FORM_ref2:
381 cu_relative_offset = true;
382 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
383 break;
384 case DW_FORM_ref4:
385 cu_relative_offset = true;
386 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
387 break;
388 case DW_FORM_ref8:
389 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000390 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000391 break;
392 case DW_FORM_ref_udata:
393 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000394 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000395 break;
396
397 // All DW_FORM_indirect attributes should be resolved prior to calling
398 // this function
399 case DW_FORM_indirect:
400 OS << "DW_FORM_indirect";
401 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000402
403 case DW_FORM_sec_offset:
404 if (cu->getAddressByteSize() == 4)
405 OS << format("0x%08x", (uint32_t)uvalue);
406 else
407 OS << format("0x%016" PRIx64, uvalue);
408 break;
409
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000410 default:
411 OS << format("DW_FORM(0x%4.4x)", Form);
412 break;
413 }
414
415 if (cu_relative_offset)
Benjamin Kramere25a2bd2012-04-04 20:33:56 +0000416 OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000417}
418
419const char*
420DWARFFormValue::getAsCString(const DataExtractor *debug_str_data_ptr) const {
421 if (isInlinedCStr()) {
422 return Value.cstr;
423 } else if (debug_str_data_ptr) {
424 uint32_t offset = Value.uval;
425 return debug_str_data_ptr->getCStr(&offset);
426 }
427 return NULL;
428}
429
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000430uint64_t DWARFFormValue::getReference(const DWARFCompileUnit *cu) const {
431 uint64_t die_offset = Value.uval;
432 switch (Form) {
433 case DW_FORM_ref1:
434 case DW_FORM_ref2:
435 case DW_FORM_ref4:
436 case DW_FORM_ref8:
437 case DW_FORM_ref_udata:
438 die_offset += (cu ? cu->getOffset() : 0);
439 break;
440 default:
441 break;
442 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000443
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000444 return die_offset;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000445}
446
447bool
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000448DWARFFormValue::resolveCompileUnitReferences(const DWARFCompileUnit *cu) {
449 switch (Form) {
450 case DW_FORM_ref1:
451 case DW_FORM_ref2:
452 case DW_FORM_ref4:
453 case DW_FORM_ref8:
454 case DW_FORM_ref_udata:
455 Value.uval += cu->getOffset();
456 Form = DW_FORM_ref_addr;
457 return true;
458 default:
459 break;
460 }
461 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000462}
463
464const uint8_t *DWARFFormValue::BlockData() const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000465 if (!isInlinedCStr())
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000466 return Value.data;
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000467 return NULL;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000468}
469
470bool DWARFFormValue::isBlockForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000471 switch (form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000472 case DW_FORM_exprloc:
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000473 case DW_FORM_block:
474 case DW_FORM_block1:
475 case DW_FORM_block2:
476 case DW_FORM_block4:
477 return true;
478 }
479 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000480}
481
482bool DWARFFormValue::isDataForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000483 switch (form) {
484 case DW_FORM_sdata:
485 case DW_FORM_udata:
486 case DW_FORM_data1:
487 case DW_FORM_data2:
488 case DW_FORM_data4:
489 case DW_FORM_data8:
490 return true;
491 }
492 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000493}