blob: efc2d9661307111c6a4acba395386e145c9fef7f [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
Eric Christopher205e60b2012-11-16 23:44:11 +000049 4, // 0x1f01 DW_FORM_GNU_addr_index
50 4, // 0x1f02 DW_FORM_GNU_str_index
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000051};
52
53static const uint8_t form_sizes_addr8[] = {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000054 0, // 0x00 unused
55 8, // 0x01 DW_FORM_addr
56 0, // 0x02 unused
57 0, // 0x03 DW_FORM_block2
58 0, // 0x04 DW_FORM_block4
59 2, // 0x05 DW_FORM_data2
60 4, // 0x06 DW_FORM_data4
61 8, // 0x07 DW_FORM_data8
62 0, // 0x08 DW_FORM_string
63 0, // 0x09 DW_FORM_block
64 0, // 0x0a DW_FORM_block1
65 1, // 0x0b DW_FORM_data1
66 1, // 0x0c DW_FORM_flag
67 0, // 0x0d DW_FORM_sdata
68 4, // 0x0e DW_FORM_strp
69 0, // 0x0f DW_FORM_udata
70 8, // 0x10 DW_FORM_ref_addr
71 1, // 0x11 DW_FORM_ref1
72 2, // 0x12 DW_FORM_ref2
73 4, // 0x13 DW_FORM_ref4
74 8, // 0x14 DW_FORM_ref8
75 0, // 0x15 DW_FORM_ref_udata
76 0, // 0x16 DW_FORM_indirect
Eric Christopher3887a902012-08-24 01:14:23 +000077 8, // 0x17 DW_FORM_sec_offset
78 0, // 0x18 DW_FORM_exprloc
79 0, // 0x19 DW_FORM_flag_present
80 8, // 0x20 DW_FORM_ref_sig8
Eric Christopher205e60b2012-11-16 23:44:11 +000081 8, // 0x1f01 DW_FORM_GNU_addr_index
82 8, // 0x1f01 DW_FORM_GNU_str_index
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000083};
84
85const uint8_t *
86DWARFFormValue::getFixedFormSizesForAddressSize(uint8_t addr_size) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000087 switch (addr_size) {
88 case 4: return form_sizes_addr4;
89 case 8: return form_sizes_addr8;
90 }
91 return NULL;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000092}
93
94bool
95DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
Benjamin Kramer89aedba2011-09-15 03:11:09 +000096 const DWARFCompileUnit *cu) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000097 bool indirect = false;
98 bool is_block = false;
99 Value.data = NULL;
100 // Read the value for the form into value and follow and DW_FORM_indirect
101 // instances we run into
102 do {
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000103 indirect = false;
104 switch (Form) {
105 case DW_FORM_addr:
Eric Christopher806e03d2012-11-07 23:22:07 +0000106 case DW_FORM_ref_addr: {
Eric Christopherd1726a42012-11-12 21:40:38 +0000107 RelocAddrMap::const_iterator AI
108 = cu->getContext().relocMap().find(*offset_ptr);
109 if (AI != cu->getContext().relocMap().end()) {
110 const std::pair<uint8_t, int64_t> &R = AI->second;
Eric Christopher32b37682012-12-27 01:07:07 +0000111 Value.uval = data.getUnsigned(offset_ptr, cu->getAddressByteSize()) +
112 R.second;
Eric Christopherd1726a42012-11-12 21:40:38 +0000113 } else
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: {
Eric Christopherd1726a42012-11-12 21:40:38 +0000155 RelocAddrMap::const_iterator AI
156 = cu->getContext().relocMap().find(*offset_ptr);
157 if (AI != cu->getContext().relocMap().end()) {
158 const std::pair<uint8_t, int64_t> &R = AI->second;
Eric Christopher32b37682012-12-27 01:07:07 +0000159 Value.uval = data.getU32(offset_ptr) + R.second;
Eric Christopherd1726a42012-11-12 21:40:38 +0000160 } else
Eric Christopher806e03d2012-11-07 23:22:07 +0000161 Value.uval = data.getU32(offset_ptr);
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000162 break;
Eric Christopher806e03d2012-11-07 23:22:07 +0000163 }
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000164 case DW_FORM_udata:
165 case DW_FORM_ref_udata:
166 Value.uval = data.getULEB128(offset_ptr);
167 break;
168 case DW_FORM_string:
169 Value.cstr = data.getCStr(offset_ptr);
170 // Set the string value to also be the data for inlined cstr form
171 // values only so we can tell the differnence between DW_FORM_string
172 // and DW_FORM_strp form values
Roman Divacky59324292012-09-05 22:26:57 +0000173 Value.data = (const uint8_t*)Value.cstr;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000174 break;
175 case DW_FORM_indirect:
176 Form = data.getULEB128(offset_ptr);
177 indirect = true;
178 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000179 case DW_FORM_sec_offset:
180 if (cu->getAddressByteSize() == 4)
181 Value.uval = data.getU32(offset_ptr);
182 else
183 Value.uval = data.getU64(offset_ptr);
184 break;
185 case DW_FORM_flag_present:
186 Value.uval = 1;
187 break;
188 case DW_FORM_ref_sig8:
189 Value.uval = data.getU64(offset_ptr);
190 break;
Eric Christopher205e60b2012-11-16 23:44:11 +0000191 case DW_FORM_GNU_addr_index:
192 Value.uval = data.getULEB128(offset_ptr);
193 break;
194 case DW_FORM_GNU_str_index:
195 Value.uval = data.getULEB128(offset_ptr);
196 break;
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000197 default:
198 return false;
199 }
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000200 } while (indirect);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000201
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000202 if (is_block) {
203 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
204 Value.data = NULL;
205 if (!str.empty()) {
206 Value.data = reinterpret_cast<const uint8_t *>(str.data());
207 *offset_ptr += Value.uval;
208 }
209 }
210
211 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000212}
213
214bool
215DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000216 const DWARFCompileUnit *cu) const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000217 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000218}
219
220bool
221DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
222 uint32_t *offset_ptr, const DWARFCompileUnit *cu) {
223 bool indirect = false;
224 do {
225 indirect = false;
226 switch (form) {
227 // Blocks if inlined data that have a length field and the data bytes
228 // inlined in the .debug_info
Eric Christopher3887a902012-08-24 01:14:23 +0000229 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000230 case DW_FORM_block: {
231 uint64_t size = debug_info_data.getULEB128(offset_ptr);
232 *offset_ptr += size;
233 return true;
234 }
235 case DW_FORM_block1: {
236 uint8_t size = debug_info_data.getU8(offset_ptr);
237 *offset_ptr += size;
238 return true;
239 }
240 case DW_FORM_block2: {
241 uint16_t size = debug_info_data.getU16(offset_ptr);
242 *offset_ptr += size;
243 return true;
244 }
245 case DW_FORM_block4: {
246 uint32_t size = debug_info_data.getU32(offset_ptr);
247 *offset_ptr += size;
248 return true;
249 }
250
251 // Inlined NULL terminated C-strings
252 case DW_FORM_string:
253 debug_info_data.getCStr(offset_ptr);
254 return true;
255
256 // Compile unit address sized values
257 case DW_FORM_addr:
258 case DW_FORM_ref_addr:
259 *offset_ptr += cu->getAddressByteSize();
260 return true;
261
Eric Christopher3887a902012-08-24 01:14:23 +0000262 // 0 byte values - implied from the form.
263 case DW_FORM_flag_present:
264 return true;
265
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000266 // 1 byte values
267 case DW_FORM_data1:
268 case DW_FORM_flag:
269 case DW_FORM_ref1:
270 *offset_ptr += 1;
271 return true;
272
273 // 2 byte values
274 case DW_FORM_data2:
275 case DW_FORM_ref2:
276 *offset_ptr += 2;
277 return true;
278
279 // 4 byte values
280 case DW_FORM_strp:
281 case DW_FORM_data4:
282 case DW_FORM_ref4:
283 *offset_ptr += 4;
284 return true;
285
286 // 8 byte values
287 case DW_FORM_data8:
288 case DW_FORM_ref8:
Eric Christopher3887a902012-08-24 01:14:23 +0000289 case DW_FORM_ref_sig8:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000290 *offset_ptr += 8;
291 return true;
292
293 // signed or unsigned LEB 128 values
294 // case DW_FORM_APPLE_db_str:
295 case DW_FORM_sdata:
296 case DW_FORM_udata:
297 case DW_FORM_ref_udata:
298 debug_info_data.getULEB128(offset_ptr);
299 return true;
300
301 case DW_FORM_indirect:
302 indirect = true;
303 form = debug_info_data.getULEB128(offset_ptr);
304 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000305
306 // 4 for DWARF32, 8 for DWARF64.
307 case DW_FORM_sec_offset:
308 if (cu->getAddressByteSize() == 4)
309 *offset_ptr += 4;
310 else
311 *offset_ptr += 8;
312 return true;
313
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000314 default:
315 return false;
316 }
317 } while (indirect);
318 return true;
319}
320
321void
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000322DWARFFormValue::dump(raw_ostream &OS, const DWARFCompileUnit *cu) const {
323 DataExtractor debug_str_data(cu->getContext().getStringSection(), true, 0);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000324 uint64_t uvalue = getUnsigned();
325 bool cu_relative_offset = false;
326
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000327 switch (Form) {
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000328 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
Eric Christopher3887a902012-08-24 01:14:23 +0000329 case DW_FORM_flag_present: OS << "true"; break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000330 case DW_FORM_flag:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000331 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
332 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
333 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
Eric Christopher3887a902012-08-24 01:14:23 +0000334 case DW_FORM_ref_sig8:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000335 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000336 case DW_FORM_string:
337 OS << '"';
338 OS.write_escaped(getAsCString(NULL));
339 OS << '"';
340 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000341 case DW_FORM_exprloc:
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000342 case DW_FORM_block:
343 case DW_FORM_block1:
344 case DW_FORM_block2:
345 case DW_FORM_block4:
346 if (uvalue > 0) {
347 switch (Form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000348 case DW_FORM_exprloc:
Benjamin Kramer41a96492011-11-05 08:57:40 +0000349 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000350 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
351 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
352 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
353 default: break;
354 }
355
356 const uint8_t* data_ptr = Value.data;
357 if (data_ptr) {
358 // uvalue contains size of block
359 const uint8_t* end_data_ptr = data_ptr + uvalue;
360 while (data_ptr < end_data_ptr) {
361 OS << format("%2.2x ", *data_ptr);
362 ++data_ptr;
363 }
364 }
365 else
366 OS << "NULL";
367 }
368 break;
369
370 case DW_FORM_sdata: OS << getSigned(); break;
371 case DW_FORM_udata: OS << getUnsigned(); break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000372 case DW_FORM_strp: {
373 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
374 const char* dbg_str = getAsCString(&debug_str_data);
375 if (dbg_str) {
376 OS << '"';
377 OS.write_escaped(dbg_str);
378 OS << '"';
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000379 }
380 break;
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000381 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000382 case DW_FORM_ref_addr:
Benjamin Kramer5eccd362011-11-05 16:01:13 +0000383 OS << format("0x%016" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000384 break;
385 case DW_FORM_ref1:
386 cu_relative_offset = true;
387 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
388 break;
389 case DW_FORM_ref2:
390 cu_relative_offset = true;
391 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
392 break;
393 case DW_FORM_ref4:
394 cu_relative_offset = true;
395 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
396 break;
397 case DW_FORM_ref8:
398 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000399 OS << format("cu + 0x%8.8" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000400 break;
401 case DW_FORM_ref_udata:
402 cu_relative_offset = true;
Benjamin Kramer41a96492011-11-05 08:57:40 +0000403 OS << format("cu + 0x%" PRIx64, uvalue);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000404 break;
405
406 // All DW_FORM_indirect attributes should be resolved prior to calling
407 // this function
408 case DW_FORM_indirect:
409 OS << "DW_FORM_indirect";
410 break;
Eric Christopher3887a902012-08-24 01:14:23 +0000411
412 case DW_FORM_sec_offset:
413 if (cu->getAddressByteSize() == 4)
414 OS << format("0x%08x", (uint32_t)uvalue);
415 else
416 OS << format("0x%016" PRIx64, uvalue);
417 break;
418
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000419 default:
420 OS << format("DW_FORM(0x%4.4x)", Form);
421 break;
422 }
423
424 if (cu_relative_offset)
Benjamin Kramere25a2bd2012-04-04 20:33:56 +0000425 OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000426}
427
428const char*
429DWARFFormValue::getAsCString(const DataExtractor *debug_str_data_ptr) const {
430 if (isInlinedCStr()) {
431 return Value.cstr;
432 } else if (debug_str_data_ptr) {
433 uint32_t offset = Value.uval;
434 return debug_str_data_ptr->getCStr(&offset);
435 }
436 return NULL;
437}
438
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000439uint64_t DWARFFormValue::getReference(const DWARFCompileUnit *cu) const {
440 uint64_t die_offset = Value.uval;
441 switch (Form) {
442 case DW_FORM_ref1:
443 case DW_FORM_ref2:
444 case DW_FORM_ref4:
445 case DW_FORM_ref8:
446 case DW_FORM_ref_udata:
447 die_offset += (cu ? cu->getOffset() : 0);
448 break;
449 default:
450 break;
451 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000452
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000453 return die_offset;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000454}
455
456bool
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000457DWARFFormValue::resolveCompileUnitReferences(const DWARFCompileUnit *cu) {
458 switch (Form) {
459 case DW_FORM_ref1:
460 case DW_FORM_ref2:
461 case DW_FORM_ref4:
462 case DW_FORM_ref8:
463 case DW_FORM_ref_udata:
464 Value.uval += cu->getOffset();
465 Form = DW_FORM_ref_addr;
466 return true;
467 default:
468 break;
469 }
470 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000471}
472
473const uint8_t *DWARFFormValue::BlockData() const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000474 if (!isInlinedCStr())
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000475 return Value.data;
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000476 return NULL;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000477}
478
479bool DWARFFormValue::isBlockForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000480 switch (form) {
Eric Christopher3887a902012-08-24 01:14:23 +0000481 case DW_FORM_exprloc:
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000482 case DW_FORM_block:
483 case DW_FORM_block1:
484 case DW_FORM_block2:
485 case DW_FORM_block4:
486 return true;
487 }
488 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000489}
490
491bool DWARFFormValue::isDataForm(uint16_t form) {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000492 switch (form) {
493 case DW_FORM_sdata:
494 case DW_FORM_udata:
495 case DW_FORM_data1:
496 case DW_FORM_data2:
497 case DW_FORM_data4:
498 case DW_FORM_data8:
499 return true;
500 }
501 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000502}