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
44 4, // 0x17 DW_FORM_sec_offset
45 0, // 0x18 DW_FORM_exprloc
46 0, // 0x19 DW_FORM_flag_present
47 8, // 0x20 DW_FORM_ref_sig8
50 static const uint8_t form_sizes_addr8
[] = {
52 8, // 0x01 DW_FORM_addr
54 0, // 0x03 DW_FORM_block2
55 0, // 0x04 DW_FORM_block4
56 2, // 0x05 DW_FORM_data2
57 4, // 0x06 DW_FORM_data4
58 8, // 0x07 DW_FORM_data8
59 0, // 0x08 DW_FORM_string
60 0, // 0x09 DW_FORM_block
61 0, // 0x0a DW_FORM_block1
62 1, // 0x0b DW_FORM_data1
63 1, // 0x0c DW_FORM_flag
64 0, // 0x0d DW_FORM_sdata
65 4, // 0x0e DW_FORM_strp
66 0, // 0x0f DW_FORM_udata
67 8, // 0x10 DW_FORM_ref_addr
68 1, // 0x11 DW_FORM_ref1
69 2, // 0x12 DW_FORM_ref2
70 4, // 0x13 DW_FORM_ref4
71 8, // 0x14 DW_FORM_ref8
72 0, // 0x15 DW_FORM_ref_udata
73 0, // 0x16 DW_FORM_indirect
74 8, // 0x17 DW_FORM_sec_offset
75 0, // 0x18 DW_FORM_exprloc
76 0, // 0x19 DW_FORM_flag_present
77 8, // 0x20 DW_FORM_ref_sig8
81 DWARFFormValue::getFixedFormSizesForAddressSize(uint8_t addr_size
) {
83 case 4: return form_sizes_addr4
;
84 case 8: return form_sizes_addr8
;
90 DWARFFormValue::extractValue(DataExtractor data
, uint32_t *offset_ptr
,
91 const DWARFCompileUnit
*cu
) {
92 bool indirect
= false;
93 bool is_block
= false;
95 // Read the value for the form into value and follow and DW_FORM_indirect
96 // instances we run into
101 case DW_FORM_ref_addr
:
102 Value
.uval
= data
.getUnsigned(offset_ptr
, cu
->getAddressByteSize());
104 case DW_FORM_exprloc
:
106 Value
.uval
= data
.getULEB128(offset_ptr
);
110 Value
.uval
= data
.getU8(offset_ptr
);
114 Value
.uval
= data
.getU16(offset_ptr
);
118 Value
.uval
= data
.getU32(offset_ptr
);
124 Value
.uval
= data
.getU8(offset_ptr
);
128 Value
.uval
= data
.getU16(offset_ptr
);
132 Value
.uval
= data
.getU32(offset_ptr
);
136 Value
.uval
= data
.getU64(offset_ptr
);
139 Value
.sval
= data
.getSLEB128(offset_ptr
);
142 Value
.uval
= data
.getU32(offset_ptr
);
145 case DW_FORM_ref_udata
:
146 Value
.uval
= data
.getULEB128(offset_ptr
);
149 Value
.cstr
= data
.getCStr(offset_ptr
);
150 // Set the string value to also be the data for inlined cstr form
151 // values only so we can tell the differnence between DW_FORM_string
152 // and DW_FORM_strp form values
153 Value
.data
= (const uint8_t*)Value
.cstr
;
155 case DW_FORM_indirect
:
156 Form
= data
.getULEB128(offset_ptr
);
159 case DW_FORM_sec_offset
:
160 if (cu
->getAddressByteSize() == 4)
161 Value
.uval
= data
.getU32(offset_ptr
);
163 Value
.uval
= data
.getU64(offset_ptr
);
165 case DW_FORM_flag_present
:
168 case DW_FORM_ref_sig8
:
169 Value
.uval
= data
.getU64(offset_ptr
);
177 StringRef str
= data
.getData().substr(*offset_ptr
, Value
.uval
);
180 Value
.data
= reinterpret_cast<const uint8_t *>(str
.data());
181 *offset_ptr
+= Value
.uval
;
189 DWARFFormValue::skipValue(DataExtractor debug_info_data
, uint32_t* offset_ptr
,
190 const DWARFCompileUnit
*cu
) const {
191 return DWARFFormValue::skipValue(Form
, debug_info_data
, offset_ptr
, cu
);
195 DWARFFormValue::skipValue(uint16_t form
, DataExtractor debug_info_data
,
196 uint32_t *offset_ptr
, const DWARFCompileUnit
*cu
) {
197 bool indirect
= false;
201 // Blocks if inlined data that have a length field and the data bytes
202 // inlined in the .debug_info
203 case DW_FORM_exprloc
:
204 case DW_FORM_block
: {
205 uint64_t size
= debug_info_data
.getULEB128(offset_ptr
);
209 case DW_FORM_block1
: {
210 uint8_t size
= debug_info_data
.getU8(offset_ptr
);
214 case DW_FORM_block2
: {
215 uint16_t size
= debug_info_data
.getU16(offset_ptr
);
219 case DW_FORM_block4
: {
220 uint32_t size
= debug_info_data
.getU32(offset_ptr
);
225 // Inlined NULL terminated C-strings
227 debug_info_data
.getCStr(offset_ptr
);
230 // Compile unit address sized values
232 case DW_FORM_ref_addr
:
233 *offset_ptr
+= cu
->getAddressByteSize();
236 // 0 byte values - implied from the form.
237 case DW_FORM_flag_present
:
263 case DW_FORM_ref_sig8
:
267 // signed or unsigned LEB 128 values
268 // case DW_FORM_APPLE_db_str:
271 case DW_FORM_ref_udata
:
272 debug_info_data
.getULEB128(offset_ptr
);
275 case DW_FORM_indirect
:
277 form
= debug_info_data
.getULEB128(offset_ptr
);
280 // 4 for DWARF32, 8 for DWARF64.
281 case DW_FORM_sec_offset
:
282 if (cu
->getAddressByteSize() == 4)
296 DWARFFormValue::dump(raw_ostream
&OS
, const DWARFCompileUnit
*cu
) const {
297 DataExtractor
debug_str_data(cu
->getContext().getStringSection(), true, 0);
298 uint64_t uvalue
= getUnsigned();
299 bool cu_relative_offset
= false;
302 case DW_FORM_addr
: OS
<< format("0x%016" PRIx64
, uvalue
); break;
303 case DW_FORM_flag_present
: OS
<< "true"; break;
305 case DW_FORM_data1
: OS
<< format("0x%02x", (uint8_t)uvalue
); break;
306 case DW_FORM_data2
: OS
<< format("0x%04x", (uint16_t)uvalue
); break;
307 case DW_FORM_data4
: OS
<< format("0x%08x", (uint32_t)uvalue
); break;
308 case DW_FORM_ref_sig8
:
309 case DW_FORM_data8
: OS
<< format("0x%016" PRIx64
, uvalue
); break;
312 OS
.write_escaped(getAsCString(NULL
));
315 case DW_FORM_exprloc
:
322 case DW_FORM_exprloc
:
323 case DW_FORM_block
: OS
<< format("<0x%" PRIx64
"> ", uvalue
); break;
324 case DW_FORM_block1
: OS
<< format("<0x%2.2x> ", (uint8_t)uvalue
); break;
325 case DW_FORM_block2
: OS
<< format("<0x%4.4x> ", (uint16_t)uvalue
); break;
326 case DW_FORM_block4
: OS
<< format("<0x%8.8x> ", (uint32_t)uvalue
); break;
330 const uint8_t* data_ptr
= Value
.data
;
332 // uvalue contains size of block
333 const uint8_t* end_data_ptr
= data_ptr
+ uvalue
;
334 while (data_ptr
< end_data_ptr
) {
335 OS
<< format("%2.2x ", *data_ptr
);
344 case DW_FORM_sdata
: OS
<< getSigned(); break;
345 case DW_FORM_udata
: OS
<< getUnsigned(); break;
347 OS
<< format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue
);
348 const char* dbg_str
= getAsCString(&debug_str_data
);
351 OS
.write_escaped(dbg_str
);
356 case DW_FORM_ref_addr
:
357 OS
<< format("0x%016" PRIx64
, uvalue
);
360 cu_relative_offset
= true;
361 OS
<< format("cu + 0x%2.2x", (uint8_t)uvalue
);
364 cu_relative_offset
= true;
365 OS
<< format("cu + 0x%4.4x", (uint16_t)uvalue
);
368 cu_relative_offset
= true;
369 OS
<< format("cu + 0x%4.4x", (uint32_t)uvalue
);
372 cu_relative_offset
= true;
373 OS
<< format("cu + 0x%8.8" PRIx64
, uvalue
);
375 case DW_FORM_ref_udata
:
376 cu_relative_offset
= true;
377 OS
<< format("cu + 0x%" PRIx64
, uvalue
);
380 // All DW_FORM_indirect attributes should be resolved prior to calling
382 case DW_FORM_indirect
:
383 OS
<< "DW_FORM_indirect";
386 case DW_FORM_sec_offset
:
387 if (cu
->getAddressByteSize() == 4)
388 OS
<< format("0x%08x", (uint32_t)uvalue
);
390 OS
<< format("0x%016" PRIx64
, uvalue
);
394 OS
<< format("DW_FORM(0x%4.4x)", Form
);
398 if (cu_relative_offset
)
399 OS
<< format(" => {0x%8.8" PRIx64
"}", uvalue
+ (cu
? cu
->getOffset() : 0));
403 DWARFFormValue::getAsCString(const DataExtractor
*debug_str_data_ptr
) const {
404 if (isInlinedCStr()) {
406 } else if (debug_str_data_ptr
) {
407 uint32_t offset
= Value
.uval
;
408 return debug_str_data_ptr
->getCStr(&offset
);
413 uint64_t DWARFFormValue::getReference(const DWARFCompileUnit
*cu
) const {
414 uint64_t die_offset
= Value
.uval
;
420 case DW_FORM_ref_udata
:
421 die_offset
+= (cu
? cu
->getOffset() : 0);
431 DWARFFormValue::resolveCompileUnitReferences(const DWARFCompileUnit
*cu
) {
437 case DW_FORM_ref_udata
:
438 Value
.uval
+= cu
->getOffset();
439 Form
= DW_FORM_ref_addr
;
447 const uint8_t *DWARFFormValue::BlockData() const {
448 if (!isInlinedCStr())
453 bool DWARFFormValue::isBlockForm(uint16_t form
) {
455 case DW_FORM_exprloc
:
465 bool DWARFFormValue::isDataForm(uint16_t form
) {