1 //===-- DWARFFormValue.cpp ------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DWARFFormValue.h"
11 #include "DWARFCompileUnit.h"
12 #include "DWARFContext.h"
13 #include "llvm/Support/Debug.h"
14 #include "llvm/Support/Dwarf.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/Support/raw_ostream.h"
19 using namespace dwarf;
21 static const uint8_t form_sizes_addr4[] = {
23 4, // 0x01 DW_FORM_addr
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
45 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
51 static const uint8_t form_sizes_addr8[] = {
53 8, // 0x01 DW_FORM_addr
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
75 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
82 DWARFFormValue::getFixedFormSizesForAddressSize(uint8_t addr_size) {
84 case 4: return form_sizes_addr4;
85 case 8: return form_sizes_addr8;
91 DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
92 const DWARFCompileUnit *cu) {
93 bool indirect = false;
94 bool is_block = false;
96 // Read the value for the form into value and follow and DW_FORM_indirect
97 // instances we run into
102 case DW_FORM_ref_addr: {
103 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;
114 Value.uval = data.getUnsigned(offset_ptr, cu->getAddressByteSize());
117 case DW_FORM_exprloc:
119 Value.uval = data.getULEB128(offset_ptr);
123 Value.uval = data.getU8(offset_ptr);
127 Value.uval = data.getU16(offset_ptr);
131 Value.uval = data.getU32(offset_ptr);
137 Value.uval = data.getU8(offset_ptr);
141 Value.uval = data.getU16(offset_ptr);
145 Value.uval = data.getU32(offset_ptr);
149 Value.uval = data.getU64(offset_ptr);
152 Value.sval = data.getSLEB128(offset_ptr);
155 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;
166 Value.uval = data.getU32(offset_ptr);
170 case DW_FORM_ref_udata:
171 Value.uval = data.getULEB128(offset_ptr);
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
178 Value.data = (const uint8_t*)Value.cstr;
180 case DW_FORM_indirect:
181 Form = data.getULEB128(offset_ptr);
184 case DW_FORM_sec_offset:
185 if (cu->getAddressByteSize() == 4)
186 Value.uval = data.getU32(offset_ptr);
188 Value.uval = data.getU64(offset_ptr);
190 case DW_FORM_flag_present:
193 case DW_FORM_ref_sig8:
194 Value.uval = data.getU64(offset_ptr);
202 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
205 Value.data = reinterpret_cast<const uint8_t *>(str.data());
206 *offset_ptr += Value.uval;
214 DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
215 const DWARFCompileUnit *cu) const {
216 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
220 DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
221 uint32_t *offset_ptr, const DWARFCompileUnit *cu) {
222 bool indirect = false;
226 // Blocks if inlined data that have a length field and the data bytes
227 // inlined in the .debug_info
228 case DW_FORM_exprloc:
229 case DW_FORM_block: {
230 uint64_t size = debug_info_data.getULEB128(offset_ptr);
234 case DW_FORM_block1: {
235 uint8_t size = debug_info_data.getU8(offset_ptr);
239 case DW_FORM_block2: {
240 uint16_t size = debug_info_data.getU16(offset_ptr);
244 case DW_FORM_block4: {
245 uint32_t size = debug_info_data.getU32(offset_ptr);
250 // Inlined NULL terminated C-strings
252 debug_info_data.getCStr(offset_ptr);
255 // Compile unit address sized values
257 case DW_FORM_ref_addr:
258 *offset_ptr += cu->getAddressByteSize();
261 // 0 byte values - implied from the form.
262 case DW_FORM_flag_present:
288 case DW_FORM_ref_sig8:
292 // signed or unsigned LEB 128 values
293 // case DW_FORM_APPLE_db_str:
296 case DW_FORM_ref_udata:
297 debug_info_data.getULEB128(offset_ptr);
300 case DW_FORM_indirect:
302 form = debug_info_data.getULEB128(offset_ptr);
305 // 4 for DWARF32, 8 for DWARF64.
306 case DW_FORM_sec_offset:
307 if (cu->getAddressByteSize() == 4)
321 DWARFFormValue::dump(raw_ostream &OS, const DWARFCompileUnit *cu) const {
322 DataExtractor debug_str_data(cu->getContext().getStringSection(), true, 0);
323 uint64_t uvalue = getUnsigned();
324 bool cu_relative_offset = false;
327 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
328 case DW_FORM_flag_present: OS << "true"; break;
330 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;
333 case DW_FORM_ref_sig8:
334 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
337 OS.write_escaped(getAsCString(NULL));
340 case DW_FORM_exprloc:
347 case DW_FORM_exprloc:
348 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
349 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;
355 const uint8_t* data_ptr = Value.data;
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);
369 case DW_FORM_sdata: OS << getSigned(); break;
370 case DW_FORM_udata: OS << getUnsigned(); break;
372 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
373 const char* dbg_str = getAsCString(&debug_str_data);
376 OS.write_escaped(dbg_str);
381 case DW_FORM_ref_addr:
382 OS << format("0x%016" PRIx64, uvalue);
385 cu_relative_offset = true;
386 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
389 cu_relative_offset = true;
390 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
393 cu_relative_offset = true;
394 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
397 cu_relative_offset = true;
398 OS << format("cu + 0x%8.8" PRIx64, uvalue);
400 case DW_FORM_ref_udata:
401 cu_relative_offset = true;
402 OS << format("cu + 0x%" PRIx64, uvalue);
405 // All DW_FORM_indirect attributes should be resolved prior to calling
407 case DW_FORM_indirect:
408 OS << "DW_FORM_indirect";
411 case DW_FORM_sec_offset:
412 if (cu->getAddressByteSize() == 4)
413 OS << format("0x%08x", (uint32_t)uvalue);
415 OS << format("0x%016" PRIx64, uvalue);
419 OS << format("DW_FORM(0x%4.4x)", Form);
423 if (cu_relative_offset)
424 OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
428 DWARFFormValue::getAsCString(const DataExtractor *debug_str_data_ptr) const {
429 if (isInlinedCStr()) {
431 } else if (debug_str_data_ptr) {
432 uint32_t offset = Value.uval;
433 return debug_str_data_ptr->getCStr(&offset);
438 uint64_t DWARFFormValue::getReference(const DWARFCompileUnit *cu) const {
439 uint64_t die_offset = Value.uval;
445 case DW_FORM_ref_udata:
446 die_offset += (cu ? cu->getOffset() : 0);
456 DWARFFormValue::resolveCompileUnitReferences(const DWARFCompileUnit *cu) {
462 case DW_FORM_ref_udata:
463 Value.uval += cu->getOffset();
464 Form = DW_FORM_ref_addr;
472 const uint8_t *DWARFFormValue::BlockData() const {
473 if (!isInlinedCStr())
478 bool DWARFFormValue::isBlockForm(uint16_t form) {
480 case DW_FORM_exprloc:
490 bool DWARFFormValue::isDataForm(uint16_t form) {