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 "llvm/Support/Dwarf.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/raw_ostream.h"
17 using namespace dwarf;
19 static const uint8_t form_sizes_addr4[] = {
21 4, // 0x01 DW_FORM_addr
23 0, // 0x03 DW_FORM_block2
24 0, // 0x04 DW_FORM_block4
25 2, // 0x05 DW_FORM_data2
26 4, // 0x06 DW_FORM_data4
27 8, // 0x07 DW_FORM_data8
28 0, // 0x08 DW_FORM_string
29 0, // 0x09 DW_FORM_block
30 0, // 0x0a DW_FORM_block1
31 1, // 0x0b DW_FORM_data1
32 1, // 0x0c DW_FORM_flag
33 0, // 0x0d DW_FORM_sdata
34 4, // 0x0e DW_FORM_strp
35 0, // 0x0f DW_FORM_udata
36 4, // 0x10 DW_FORM_ref_addr
37 1, // 0x11 DW_FORM_ref1
38 2, // 0x12 DW_FORM_ref2
39 4, // 0x13 DW_FORM_ref4
40 8, // 0x14 DW_FORM_ref8
41 0, // 0x15 DW_FORM_ref_udata
42 0, // 0x16 DW_FORM_indirect
45 static const uint8_t form_sizes_addr8[] = {
47 8, // 0x01 DW_FORM_addr
49 0, // 0x03 DW_FORM_block2
50 0, // 0x04 DW_FORM_block4
51 2, // 0x05 DW_FORM_data2
52 4, // 0x06 DW_FORM_data4
53 8, // 0x07 DW_FORM_data8
54 0, // 0x08 DW_FORM_string
55 0, // 0x09 DW_FORM_block
56 0, // 0x0a DW_FORM_block1
57 1, // 0x0b DW_FORM_data1
58 1, // 0x0c DW_FORM_flag
59 0, // 0x0d DW_FORM_sdata
60 4, // 0x0e DW_FORM_strp
61 0, // 0x0f DW_FORM_udata
62 8, // 0x10 DW_FORM_ref_addr
63 1, // 0x11 DW_FORM_ref1
64 2, // 0x12 DW_FORM_ref2
65 4, // 0x13 DW_FORM_ref4
66 8, // 0x14 DW_FORM_ref8
67 0, // 0x15 DW_FORM_ref_udata
68 0, // 0x16 DW_FORM_indirect
72 DWARFFormValue::getFixedFormSizesForAddressSize(uint8_t addr_size) {
74 case 4: return form_sizes_addr4;
75 case 8: return form_sizes_addr8;
81 DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
82 const DWARFCompileUnit *cu) {
83 bool indirect = false;
84 bool is_block = false;
86 // Read the value for the form into value and follow and DW_FORM_indirect
87 // instances we run into
92 case DW_FORM_ref_addr:
93 Value.uval = data.getUnsigned(offset_ptr, cu->getAddressByteSize());
96 Value.uval = data.getULEB128(offset_ptr);
100 Value.uval = data.getU8(offset_ptr);
104 Value.uval = data.getU16(offset_ptr);
108 Value.uval = data.getU32(offset_ptr);
114 Value.uval = data.getU8(offset_ptr);
118 Value.uval = data.getU16(offset_ptr);
122 Value.uval = data.getU32(offset_ptr);
126 Value.uval = data.getU64(offset_ptr);
129 Value.sval = data.getSLEB128(offset_ptr);
132 Value.uval = data.getU32(offset_ptr);
135 case DW_FORM_ref_udata:
136 Value.uval = data.getULEB128(offset_ptr);
139 Value.cstr = data.getCStr(offset_ptr);
140 // Set the string value to also be the data for inlined cstr form
141 // values only so we can tell the differnence between DW_FORM_string
142 // and DW_FORM_strp form values
143 Value.data = (uint8_t*)Value.cstr;
145 case DW_FORM_indirect:
146 Form = data.getULEB128(offset_ptr);
155 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
158 Value.data = reinterpret_cast<const uint8_t *>(str.data());
159 *offset_ptr += Value.uval;
167 DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
168 const DWARFCompileUnit *cu) const {
169 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
173 DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
174 uint32_t *offset_ptr, const DWARFCompileUnit *cu) {
175 bool indirect = false;
179 // Blocks if inlined data that have a length field and the data bytes
180 // inlined in the .debug_info
181 case DW_FORM_block: {
182 uint64_t size = debug_info_data.getULEB128(offset_ptr);
186 case DW_FORM_block1: {
187 uint8_t size = debug_info_data.getU8(offset_ptr);
191 case DW_FORM_block2: {
192 uint16_t size = debug_info_data.getU16(offset_ptr);
196 case DW_FORM_block4: {
197 uint32_t size = debug_info_data.getU32(offset_ptr);
202 // Inlined NULL terminated C-strings
204 debug_info_data.getCStr(offset_ptr);
207 // Compile unit address sized values
209 case DW_FORM_ref_addr:
210 *offset_ptr += cu->getAddressByteSize();
239 // signed or unsigned LEB 128 values
240 // case DW_FORM_APPLE_db_str:
243 case DW_FORM_ref_udata:
244 debug_info_data.getULEB128(offset_ptr);
247 case DW_FORM_indirect:
249 form = debug_info_data.getULEB128(offset_ptr);
259 DWARFFormValue::dump(raw_ostream &OS, const DataExtractor *debug_str_data,
260 const DWARFCompileUnit *cu) const {
261 uint64_t uvalue = getUnsigned();
262 bool cu_relative_offset = false;
265 case DW_FORM_addr: OS << format("0x%016x", uvalue); break;
267 case DW_FORM_data1: OS << format("0x%02x", uvalue); break;
268 case DW_FORM_data2: OS << format("0x%04x", uvalue); break;
269 case DW_FORM_data4: OS << format("0x%08x", uvalue); break;
270 case DW_FORM_data8: OS << format("0x%016x", uvalue); break;
273 OS.write_escaped(getAsCString(NULL));
282 case DW_FORM_block: OS << format("<0x%llx> ", uvalue); break;
283 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
284 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
285 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
289 const uint8_t* data_ptr = Value.data;
291 // uvalue contains size of block
292 const uint8_t* end_data_ptr = data_ptr + uvalue;
293 while (data_ptr < end_data_ptr) {
294 OS << format("%2.2x ", *data_ptr);
303 case DW_FORM_sdata: OS << getSigned(); break;
304 case DW_FORM_udata: OS << getUnsigned(); break;
306 if (debug_str_data) {
307 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
308 const char* dbg_str = getAsCString(debug_str_data);
311 OS.write_escaped(dbg_str);
315 OS << format("0x%08x", uvalue);
318 case DW_FORM_ref_addr:
319 OS << format("0x%016x", uvalue);
322 cu_relative_offset = true;
323 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
326 cu_relative_offset = true;
327 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
330 cu_relative_offset = true;
331 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
334 cu_relative_offset = true;
335 OS << format("cu + 0x%8.8llx", uvalue);
337 case DW_FORM_ref_udata:
338 cu_relative_offset = true;
339 OS << format("cu + 0x%llx", uvalue);
342 // All DW_FORM_indirect attributes should be resolved prior to calling
344 case DW_FORM_indirect:
345 OS << "DW_FORM_indirect";
348 OS << format("DW_FORM(0x%4.4x)", Form);
352 if (cu_relative_offset)
353 OS << format(" => {0x%8.8x}", (uvalue + (cu ? cu->getOffset() : 0)));
357 DWARFFormValue::getAsCString(const DataExtractor *debug_str_data_ptr) const {
358 if (isInlinedCStr()) {
360 } else if (debug_str_data_ptr) {
361 uint32_t offset = Value.uval;
362 return debug_str_data_ptr->getCStr(&offset);
367 uint64_t DWARFFormValue::getReference(const DWARFCompileUnit *cu) const {
368 uint64_t die_offset = Value.uval;
374 case DW_FORM_ref_udata:
375 die_offset += (cu ? cu->getOffset() : 0);
385 DWARFFormValue::resolveCompileUnitReferences(const DWARFCompileUnit *cu) {
391 case DW_FORM_ref_udata:
392 Value.uval += cu->getOffset();
393 Form = DW_FORM_ref_addr;
401 const uint8_t *DWARFFormValue::BlockData() const {
402 if (!isInlinedCStr())
407 bool DWARFFormValue::isBlockForm(uint16_t form) {
418 bool DWARFFormValue::isDataForm(uint16_t form) {