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/Dwarf.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h"
18 using namespace dwarf;
20 static const uint8_t form_sizes_addr4[] = {
22 4, // 0x01 DW_FORM_addr
24 0, // 0x03 DW_FORM_block2
25 0, // 0x04 DW_FORM_block4
26 2, // 0x05 DW_FORM_data2
27 4, // 0x06 DW_FORM_data4
28 8, // 0x07 DW_FORM_data8
29 0, // 0x08 DW_FORM_string
30 0, // 0x09 DW_FORM_block
31 0, // 0x0a DW_FORM_block1
32 1, // 0x0b DW_FORM_data1
33 1, // 0x0c DW_FORM_flag
34 0, // 0x0d DW_FORM_sdata
35 4, // 0x0e DW_FORM_strp
36 0, // 0x0f DW_FORM_udata
37 4, // 0x10 DW_FORM_ref_addr
38 1, // 0x11 DW_FORM_ref1
39 2, // 0x12 DW_FORM_ref2
40 4, // 0x13 DW_FORM_ref4
41 8, // 0x14 DW_FORM_ref8
42 0, // 0x15 DW_FORM_ref_udata
43 0, // 0x16 DW_FORM_indirect
46 static const uint8_t form_sizes_addr8[] = {
48 8, // 0x01 DW_FORM_addr
50 0, // 0x03 DW_FORM_block2
51 0, // 0x04 DW_FORM_block4
52 2, // 0x05 DW_FORM_data2
53 4, // 0x06 DW_FORM_data4
54 8, // 0x07 DW_FORM_data8
55 0, // 0x08 DW_FORM_string
56 0, // 0x09 DW_FORM_block
57 0, // 0x0a DW_FORM_block1
58 1, // 0x0b DW_FORM_data1
59 1, // 0x0c DW_FORM_flag
60 0, // 0x0d DW_FORM_sdata
61 4, // 0x0e DW_FORM_strp
62 0, // 0x0f DW_FORM_udata
63 8, // 0x10 DW_FORM_ref_addr
64 1, // 0x11 DW_FORM_ref1
65 2, // 0x12 DW_FORM_ref2
66 4, // 0x13 DW_FORM_ref4
67 8, // 0x14 DW_FORM_ref8
68 0, // 0x15 DW_FORM_ref_udata
69 0, // 0x16 DW_FORM_indirect
73 DWARFFormValue::getFixedFormSizesForAddressSize(uint8_t addr_size) {
75 case 4: return form_sizes_addr4;
76 case 8: return form_sizes_addr8;
82 DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
83 const DWARFCompileUnit *cu) {
84 bool indirect = false;
85 bool is_block = false;
87 // Read the value for the form into value and follow and DW_FORM_indirect
88 // instances we run into
93 case DW_FORM_ref_addr:
94 Value.uval = data.getUnsigned(offset_ptr, cu->getAddressByteSize());
97 Value.uval = data.getULEB128(offset_ptr);
101 Value.uval = data.getU8(offset_ptr);
105 Value.uval = data.getU16(offset_ptr);
109 Value.uval = data.getU32(offset_ptr);
115 Value.uval = data.getU8(offset_ptr);
119 Value.uval = data.getU16(offset_ptr);
123 Value.uval = data.getU32(offset_ptr);
127 Value.uval = data.getU64(offset_ptr);
130 Value.sval = data.getSLEB128(offset_ptr);
133 Value.uval = data.getU32(offset_ptr);
136 case DW_FORM_ref_udata:
137 Value.uval = data.getULEB128(offset_ptr);
140 Value.cstr = data.getCStr(offset_ptr);
141 // Set the string value to also be the data for inlined cstr form
142 // values only so we can tell the differnence between DW_FORM_string
143 // and DW_FORM_strp form values
144 Value.data = (uint8_t*)Value.cstr;
146 case DW_FORM_indirect:
147 Form = data.getULEB128(offset_ptr);
156 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
159 Value.data = reinterpret_cast<const uint8_t *>(str.data());
160 *offset_ptr += Value.uval;
168 DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
169 const DWARFCompileUnit *cu) const {
170 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
174 DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
175 uint32_t *offset_ptr, const DWARFCompileUnit *cu) {
176 bool indirect = false;
180 // Blocks if inlined data that have a length field and the data bytes
181 // inlined in the .debug_info
182 case DW_FORM_block: {
183 uint64_t size = debug_info_data.getULEB128(offset_ptr);
187 case DW_FORM_block1: {
188 uint8_t size = debug_info_data.getU8(offset_ptr);
192 case DW_FORM_block2: {
193 uint16_t size = debug_info_data.getU16(offset_ptr);
197 case DW_FORM_block4: {
198 uint32_t size = debug_info_data.getU32(offset_ptr);
203 // Inlined NULL terminated C-strings
205 debug_info_data.getCStr(offset_ptr);
208 // Compile unit address sized values
210 case DW_FORM_ref_addr:
211 *offset_ptr += cu->getAddressByteSize();
240 // signed or unsigned LEB 128 values
241 // case DW_FORM_APPLE_db_str:
244 case DW_FORM_ref_udata:
245 debug_info_data.getULEB128(offset_ptr);
248 case DW_FORM_indirect:
250 form = debug_info_data.getULEB128(offset_ptr);
260 DWARFFormValue::dump(raw_ostream &OS, const DWARFCompileUnit *cu) const {
261 DataExtractor debug_str_data(cu->getContext().getStringSection(), true, 0);
262 uint64_t uvalue = getUnsigned();
263 bool cu_relative_offset = false;
266 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
268 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
269 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
270 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
271 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
274 OS.write_escaped(getAsCString(NULL));
283 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
284 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
285 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
286 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
290 const uint8_t* data_ptr = Value.data;
292 // uvalue contains size of block
293 const uint8_t* end_data_ptr = data_ptr + uvalue;
294 while (data_ptr < end_data_ptr) {
295 OS << format("%2.2x ", *data_ptr);
304 case DW_FORM_sdata: OS << getSigned(); break;
305 case DW_FORM_udata: OS << getUnsigned(); break;
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);
316 case DW_FORM_ref_addr:
317 OS << format("0x%016" PRIx64, uvalue);
320 cu_relative_offset = true;
321 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
324 cu_relative_offset = true;
325 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
328 cu_relative_offset = true;
329 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
332 cu_relative_offset = true;
333 OS << format("cu + 0x%8.8" PRIx64, uvalue);
335 case DW_FORM_ref_udata:
336 cu_relative_offset = true;
337 OS << format("cu + 0x%" PRIx64, uvalue);
340 // All DW_FORM_indirect attributes should be resolved prior to calling
342 case DW_FORM_indirect:
343 OS << "DW_FORM_indirect";
346 OS << format("DW_FORM(0x%4.4x)", Form);
350 if (cu_relative_offset)
351 OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
355 DWARFFormValue::getAsCString(const DataExtractor *debug_str_data_ptr) const {
356 if (isInlinedCStr()) {
358 } else if (debug_str_data_ptr) {
359 uint32_t offset = Value.uval;
360 return debug_str_data_ptr->getCStr(&offset);
365 uint64_t DWARFFormValue::getReference(const DWARFCompileUnit *cu) const {
366 uint64_t die_offset = Value.uval;
372 case DW_FORM_ref_udata:
373 die_offset += (cu ? cu->getOffset() : 0);
383 DWARFFormValue::resolveCompileUnitReferences(const DWARFCompileUnit *cu) {
389 case DW_FORM_ref_udata:
390 Value.uval += cu->getOffset();
391 Form = DW_FORM_ref_addr;
399 const uint8_t *DWARFFormValue::BlockData() const {
400 if (!isInlinedCStr())
405 bool DWARFFormValue::isBlockForm(uint16_t form) {
416 bool DWARFFormValue::isDataForm(uint16_t form) {