blob: b75b0c17b38b19c07265480526298be57cc79f2e [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: {
Alexey Samsonov4c0ae902012-11-12 14:25:36 +0000103 bool InRelocMap = false;
104 if (const RelocAddrMap *RelocMap = cu->getContext().relocMap()) {
105 RelocAddrMap::const_iterator AI = RelocMap->find(*offset_ptr);
106 if (AI != RelocMap->end()) {
107 const std::pair<uint8_t, int64_t> &R = AI->second;
108 Value.uval = R.second;
109 *offset_ptr += R.first;
110 InRelocMap = true;
111 }
112 }
113 if (!InRelocMap)
Eric Christopher806e03d2012-11-07 23:22:07 +0000114 Value.uval = data.getUnsigned(offset_ptr, cu->getAddressByteSize());
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000115 break;
Alexey Samsonov4c0ae902012-11-12 14:25:36 +0000116 }
Eric Christopher3887a902012-08-24 01:14:23 +0000117 case DW_FORM_exprloc:
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000118 case DW_FORM_block:
119 Value.uval = data.getULEB128(offset_ptr);
120 is_block = true;
121 break;
122 case DW_FORM_block1:
123 Value.uval = data.getU8(offset_ptr);
124 is_block = true;
125 break;
126 case DW_FORM_block2:
127 Value.uval = data.getU16(offset_ptr);
128 is_block = true;
129 break;
130 case DW_FORM_block4:
131 Value.uval = data.getU32(offset_ptr);
132 is_block = true;
133 break;
134 case DW_FORM_data1:
135 case DW_FORM_ref1:
136 case DW_FORM_flag:
137 Value.uval = data.getU8(offset_ptr);
138 break;
139 case DW_FORM_data2:
140 case DW_FORM_ref2:
141 Value.uval = data.getU16(offset_ptr);
142 break;
143 case DW_FORM_data4:
144 case DW_FORM_ref4:
145 Value.uval = data.getU32(offset_ptr);
146 break;
147 case DW_FORM_data8:
148 case DW_FORM_ref8:
149 Value.uval = data.getU64(offset_ptr);
150 break;
151 case DW_FORM_sdata:
152 Value.sval = data.getSLEB128(offset_ptr);
153 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000154 case DW_FORM_strp: {
Alexey Samsonov4c0ae902012-11-12 14:25:36 +0000155 bool InRelocMap = false;
156 if (const RelocAddrMap *RelocMap = cu->getContext().relocMap()) {
157 RelocAddrMap::const_iterator AI = RelocMap->find(*offset_ptr);
158 if (AI != RelocMap->end()) {
159 const std::pair<uint8_t, int64_t> &R = AI->second;
160 Value.uval = R.second;
161 *offset_ptr += R.first;
162 InRelocMap = true;
163 }
164 }
165 if (!InRelocMap)
Eric Christopher806e03d2012-11-07 23:22:07 +0000166 Value.uval = data.getU32(offset_ptr);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000167 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000168 }
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000169 case DW_FORM_udata:
170 case DW_FORM_ref_udata:
171 Value.uval = data.getULEB128(offset_ptr);
172 break;
173 case DW_FORM_string:
174 Value.cstr = data.getCStr(offset_ptr);
175 // Set the string value to also be the data for inlined cstr form
176 // values only so we can tell the differnence between DW_FORM_string
177 // and DW_FORM_strp form values
Roman Divacky59324292012-09-05 22:26:57 +0000178 Value.data = (const uint8_t*)Value.cstr;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000179 break;
180 case DW_FORM_indirect:
181 Form = data.getULEB128(offset_ptr);
182 indirect = true;
183 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000184 case DW_FORM_sec_offset:
185 if (cu->getAddressByteSize() == 4)
186 Value.uval = data.getU32(offset_ptr);
187 else
188 Value.uval = data.getU64(offset_ptr);
189 break;
190 case DW_FORM_flag_present:
191 Value.uval = 1;
192 break;
193 case DW_FORM_ref_sig8:
194 Value.uval = data.getU64(offset_ptr);
195 break;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000196 default:
197 return false;
198 }
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000199 } while (indirect);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000200
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000201 if (is_block) {
202 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
203 Value.data = NULL;
204 if (!str.empty()) {
205 Value.data = reinterpret_cast<const uint8_t *>(str.data());
206 *offset_ptr += Value.uval;
207 }
208 }
209
210 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000211}
212
213bool
214DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000215 const DWARFCompileUnit *cu) const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000216 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000217}
218
219bool
220DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
221 uint32_t *offset_ptr, const DWARFCompileUnit *cu) {
222 bool indirect = false;
223 do {
224 indirect = false;
225 switch (form) {
226 // Blocks if inlined data that have a length field and the data bytes
227 // inlined in the .debug_info
Eric Christopher3887a902012-08-24 01:14:23 +0000228 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000229 case DW_FORM_block: {
230 uint64_t size = debug_info_data.getULEB128(offset_ptr);
231 *offset_ptr += size;
232 return true;
233 }
234 case DW_FORM_block1: {
235 uint8_t size = debug_info_data.getU8(offset_ptr);
236 *offset_ptr += size;
237 return true;
238 }
239 case DW_FORM_block2: {
240 uint16_t size = debug_info_data.getU16(offset_ptr);
241 *offset_ptr += size;
242 return true;
243 }
244 case DW_FORM_block4: {
245 uint32_t size = debug_info_data.getU32(offset_ptr);
246 *offset_ptr += size;
247 return true;
248 }
249
250 // Inlined NULL terminated C-strings
251 case DW_FORM_string:
252 debug_info_data.getCStr(offset_ptr);
253 return true;
254
255 // Compile unit address sized values
256 case DW_FORM_addr:
257 case DW_FORM_ref_addr:
258 *offset_ptr += cu->getAddressByteSize();
259 return true;
260
Eric Christopher3887a902012-08-24 01:14:23 +0000261 // 0 byte values - implied from the form.
262 case DW_FORM_flag_present:
263 return true;
264
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000265 // 1 byte values
266 case DW_FORM_data1:
267 case DW_FORM_flag:
268 case DW_FORM_ref1:
269 *offset_ptr += 1;
270 return true;
271
272 // 2 byte values
273 case DW_FORM_data2:
274 case DW_FORM_ref2:
275 *offset_ptr += 2;
276 return true;
277
278 // 4 byte values
279 case DW_FORM_strp:
280 case DW_FORM_data4:
281 case DW_FORM_ref4:
282 *offset_ptr += 4;
283 return true;
284
285 // 8 byte values
286 case DW_FORM_data8:
287 case DW_FORM_ref8:
Eric Christopher3887a902012-08-24 01:14:23 +0000288 case DW_FORM_ref_sig8:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000289 *offset_ptr += 8;
290 return true;
291
292 // signed or unsigned LEB 128 values
293 // case DW_FORM_APPLE_db_str:
294 case DW_FORM_sdata:
295 case DW_FORM_udata:
296 case DW_FORM_ref_udata:
297 debug_info_data.getULEB128(offset_ptr);
298 return true;
299
300 case DW_FORM_indirect:
301 indirect = true;
302 form = debug_info_data.getULEB128(offset_ptr);
303 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000304
305 // 4 for DWARF32, 8 for DWARF64.
306 case DW_FORM_sec_offset:
307 if (cu->getAddressByteSize() == 4)
308 *offset_ptr += 4;
309 else
310 *offset_ptr += 8;
311 return true;
312
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000313 default:
314 return false;
315 }
316 } while (indirect);
317 return true;
318}
319
320void
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000321DWARFFormValue::dump(raw_ostream &OS, const DWARFCompileUnit *cu) const {
322 DataExtractor debug_str_data(cu->getContext().getStringSection(), true, 0);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000323 uint64_t uvalue = getUnsigned();
324 bool cu_relative_offset = false;
325
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000326 switch (Form) {
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000327 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher3887a902012-08-24 01:14:23 +0000328 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000329 case DW_FORM_flag:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000330 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
331 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
332 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopher3887a902012-08-24 01:14:23 +0000333 case DW_FORM_ref_sig8:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000334 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000335 case DW_FORM_string:
336 OS << '"';
337 OS.write_escaped(getAsCString(NULL));
338 OS << '"';
339 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000340 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000341 case DW_FORM_block:
342 case DW_FORM_block1:
343 case DW_FORM_block2:
344 case DW_FORM_block4:
345 if (uvalue > 0) {
346 switch (Form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000347 case DW_FORM_exprloc:
Benjamin Kramer41a96492011-11-05 08:57:40 +0000348 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000349 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
350 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
351 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
352 default: break;
353 }
354
355 const uint8_t* data_ptr = Value.data;
356 if (data_ptr) {
357 // uvalue contains size of block
358 const uint8_t* end_data_ptr = data_ptr + uvalue;
359 while (data_ptr < end_data_ptr) {
360 OS << format("%2.2x ", *data_ptr);
361 ++data_ptr;
362 }
363 }
364 else
365 OS << "NULL";
366 }
367 break;
368
369 case DW_FORM_sdata: OS << getSigned(); break;
370 case DW_FORM_udata: OS << getUnsigned(); break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000371 case DW_FORM_strp: {
372 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
373 const char* dbg_str = getAsCString(&debug_str_data);
374 if (dbg_str) {
375 OS << '"';
376 OS.write_escaped(dbg_str);
377 OS << '"';
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000378 }
379 break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000380 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000381 case DW_FORM_ref_addr:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000382 OS << format("0x%016" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000383 break;
384 case DW_FORM_ref1:
385 cu_relative_offset = true;
386 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
387 break;
388 case DW_FORM_ref2:
389 cu_relative_offset = true;
390 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
391 break;
392 case DW_FORM_ref4:
393 cu_relative_offset = true;
394 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
395 break;
396 case DW_FORM_ref8:
397 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000398 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000399 break;
400 case DW_FORM_ref_udata:
401 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000402 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000403 break;
404
405 // All DW_FORM_indirect attributes should be resolved prior to calling
406 // this function
407 case DW_FORM_indirect:
408 OS << "DW_FORM_indirect";
409 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000410
411 case DW_FORM_sec_offset:
412 if (cu->getAddressByteSize() == 4)
413 OS << format("0x%08x", (uint32_t)uvalue);
414 else
415 OS << format("0x%016" PRIx64, uvalue);
416 break;
417
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000418 default:
419 OS << format("DW_FORM(0x%4.4x)", Form);
420 break;
421 }
422
423 if (cu_relative_offset)
Benjamin Kramere25a2bd2012-04-04 20:33:56 +0000424 OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000425}
426
427const char*
428DWARFFormValue::getAsCString(const DataExtractor *debug_str_data_ptr) const {
429 if (isInlinedCStr()) {
430 return Value.cstr;
431 } else if (debug_str_data_ptr) {
432 uint32_t offset = Value.uval;
433 return debug_str_data_ptr->getCStr(&offset);
434 }
435 return NULL;
436}
437
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000438uint64_t DWARFFormValue::getReference(const DWARFCompileUnit *cu) const {
439 uint64_t die_offset = Value.uval;
440 switch (Form) {
441 case DW_FORM_ref1:
442 case DW_FORM_ref2:
443 case DW_FORM_ref4:
444 case DW_FORM_ref8:
445 case DW_FORM_ref_udata:
446 die_offset += (cu ? cu->getOffset() : 0);
447 break;
448 default:
449 break;
450 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000451
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000452 return die_offset;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000453}
454
455bool
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000456DWARFFormValue::resolveCompileUnitReferences(const DWARFCompileUnit *cu) {
457 switch (Form) {
458 case DW_FORM_ref1:
459 case DW_FORM_ref2:
460 case DW_FORM_ref4:
461 case DW_FORM_ref8:
462 case DW_FORM_ref_udata:
463 Value.uval += cu->getOffset();
464 Form = DW_FORM_ref_addr;
465 return true;
466 default:
467 break;
468 }
469 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000470}
471
472const uint8_t *DWARFFormValue::BlockData() const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000473 if (!isInlinedCStr())
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000474 return Value.data;
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000475 return NULL;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000476}
477
478bool DWARFFormValue::isBlockForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000479 switch (form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000480 case DW_FORM_exprloc:
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000481 case DW_FORM_block:
482 case DW_FORM_block1:
483 case DW_FORM_block2:
484 case DW_FORM_block4:
485 return true;
486 }
487 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000488}
489
490bool DWARFFormValue::isDataForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000491 switch (form) {
492 case DW_FORM_sdata:
493 case DW_FORM_udata:
494 case DW_FORM_data1:
495 case DW_FORM_data2:
496 case DW_FORM_data4:
497 case DW_FORM_data8:
498 return true;
499 }
500 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000501}