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 "SyntaxHighlighting.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/DebugInfo/DWARFCompileUnit.h"
14 #include "llvm/DebugInfo/DWARFContext.h"
15 #include "llvm/DebugInfo/DWARFFormValue.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/Dwarf.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/raw_ostream.h"
22 using namespace dwarf;
23 using namespace syntax;
26 uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
27 // FIXME: Support DWARF64.
28 return (Version == 2) ? AddrSize : 4;
31 template <uint8_t AddrSize, uint8_t RefAddrSize>
32 ArrayRef<uint8_t> makeFixedFormSizesArrayRef() {
33 static const uint8_t sizes[] = {
35 AddrSize, // 0x01 DW_FORM_addr
37 0, // 0x03 DW_FORM_block2
38 0, // 0x04 DW_FORM_block4
39 2, // 0x05 DW_FORM_data2
40 4, // 0x06 DW_FORM_data4
41 8, // 0x07 DW_FORM_data8
42 0, // 0x08 DW_FORM_string
43 0, // 0x09 DW_FORM_block
44 0, // 0x0a DW_FORM_block1
45 1, // 0x0b DW_FORM_data1
46 1, // 0x0c DW_FORM_flag
47 0, // 0x0d DW_FORM_sdata
48 4, // 0x0e DW_FORM_strp
49 0, // 0x0f DW_FORM_udata
50 RefAddrSize, // 0x10 DW_FORM_ref_addr
51 1, // 0x11 DW_FORM_ref1
52 2, // 0x12 DW_FORM_ref2
53 4, // 0x13 DW_FORM_ref4
54 8, // 0x14 DW_FORM_ref8
55 0, // 0x15 DW_FORM_ref_udata
56 0, // 0x16 DW_FORM_indirect
57 4, // 0x17 DW_FORM_sec_offset
58 0, // 0x18 DW_FORM_exprloc
59 0, // 0x19 DW_FORM_flag_present
61 return makeArrayRef(sizes);
65 ArrayRef<uint8_t> DWARFFormValue::getFixedFormSizes(uint8_t AddrSize,
67 uint8_t RefAddrSize = getRefAddrSize(AddrSize, Version);
68 if (AddrSize == 4 && RefAddrSize == 4)
69 return makeFixedFormSizesArrayRef<4, 4>();
70 if (AddrSize == 4 && RefAddrSize == 8)
71 return makeFixedFormSizesArrayRef<4, 8>();
72 if (AddrSize == 8 && RefAddrSize == 4)
73 return makeFixedFormSizesArrayRef<8, 4>();
74 if (AddrSize == 8 && RefAddrSize == 8)
75 return makeFixedFormSizesArrayRef<8, 8>();
79 static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
80 DWARFFormValue::FC_Unknown, // 0x0
81 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
82 DWARFFormValue::FC_Unknown, // 0x02 unused
83 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
84 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
85 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
86 // --- These can be FC_SectionOffset in DWARF3 and below:
87 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
88 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
90 DWARFFormValue::FC_String, // 0x08 DW_FORM_string
91 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
92 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
93 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
94 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
95 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
96 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
97 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
98 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
99 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
100 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
101 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
102 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
103 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
104 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
105 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
106 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
107 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
110 bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
111 // First, check DWARF4 form classes.
112 if (Form < ArrayRef<FormClass>(DWARF4FormClasses).size() &&
113 DWARF4FormClasses[Form] == FC)
115 // Check DW_FORM_ref_sig8 from DWARF4.
116 if (Form == DW_FORM_ref_sig8)
117 return (FC == FC_Reference);
118 // Check for some DWARF5 forms.
119 if (Form == DW_FORM_GNU_addr_index)
120 return (FC == FC_Address);
121 if (Form == DW_FORM_GNU_str_index)
122 return (FC == FC_String);
123 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
124 // Don't check for DWARF version here, as some producers may still do this
126 if ((Form == DW_FORM_data4 || Form == DW_FORM_data8) &&
127 FC == FC_SectionOffset)
132 bool DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
133 const DWARFUnit *cu) {
134 bool indirect = false;
135 bool is_block = false;
136 Value.data = nullptr;
137 // Read the value for the form into value and follow and DW_FORM_indirect
138 // instances we run into
143 case DW_FORM_ref_addr: {
147 (Form == DW_FORM_addr)
148 ? cu->getAddressByteSize()
149 : getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
150 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
151 if (AI != cu->getRelocMap()->end()) {
152 const std::pair<uint8_t, int64_t> &R = AI->second;
153 Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
155 Value.uval = data.getUnsigned(offset_ptr, AddrSize);
158 case DW_FORM_exprloc:
160 Value.uval = data.getULEB128(offset_ptr);
164 Value.uval = data.getU8(offset_ptr);
168 Value.uval = data.getU16(offset_ptr);
172 Value.uval = data.getU32(offset_ptr);
178 Value.uval = data.getU8(offset_ptr);
182 Value.uval = data.getU16(offset_ptr);
186 Value.uval = data.getU32(offset_ptr);
189 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr-4);
190 if (AI != cu->getRelocMap()->end())
191 Value.uval += AI->second.second;
196 Value.uval = data.getU64(offset_ptr);
199 Value.sval = data.getSLEB128(offset_ptr);
202 Value.uval = data.getU32(offset_ptr);
205 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr-4);
206 if (AI != cu->getRelocMap()->end())
207 Value.uval += AI->second.second;
211 case DW_FORM_ref_udata:
212 Value.uval = data.getULEB128(offset_ptr);
215 Value.cstr = data.getCStr(offset_ptr);
217 case DW_FORM_indirect:
218 Form = data.getULEB128(offset_ptr);
221 case DW_FORM_sec_offset: {
222 // FIXME: This is 64-bit for DWARF64.
223 Value.uval = data.getU32(offset_ptr);
226 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr-4);
227 if (AI != cu->getRelocMap()->end())
228 Value.uval += AI->second.second;
231 case DW_FORM_flag_present:
234 case DW_FORM_ref_sig8:
235 Value.uval = data.getU64(offset_ptr);
237 case DW_FORM_GNU_addr_index:
238 case DW_FORM_GNU_str_index:
239 Value.uval = data.getULEB128(offset_ptr);
247 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
248 Value.data = nullptr;
250 Value.data = reinterpret_cast<const uint8_t *>(str.data());
251 *offset_ptr += Value.uval;
259 DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
260 const DWARFUnit *cu) const {
261 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
265 DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
266 uint32_t *offset_ptr, const DWARFUnit *cu) {
267 bool indirect = false;
270 // Blocks if inlined data that have a length field and the data bytes
271 // inlined in the .debug_info
272 case DW_FORM_exprloc:
273 case DW_FORM_block: {
274 uint64_t size = debug_info_data.getULEB128(offset_ptr);
278 case DW_FORM_block1: {
279 uint8_t size = debug_info_data.getU8(offset_ptr);
283 case DW_FORM_block2: {
284 uint16_t size = debug_info_data.getU16(offset_ptr);
288 case DW_FORM_block4: {
289 uint32_t size = debug_info_data.getU32(offset_ptr);
294 // Inlined NULL terminated C-strings
296 debug_info_data.getCStr(offset_ptr);
299 // Compile unit address sized values
301 *offset_ptr += cu->getAddressByteSize();
303 case DW_FORM_ref_addr:
304 *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
307 // 0 byte values - implied from the form.
308 case DW_FORM_flag_present:
334 case DW_FORM_ref_sig8:
338 // signed or unsigned LEB 128 values
339 // case DW_FORM_APPLE_db_str:
342 case DW_FORM_ref_udata:
343 case DW_FORM_GNU_str_index:
344 case DW_FORM_GNU_addr_index:
345 debug_info_data.getULEB128(offset_ptr);
348 case DW_FORM_indirect:
350 form = debug_info_data.getULEB128(offset_ptr);
353 // FIXME: 4 for DWARF32, 8 for DWARF64.
354 case DW_FORM_sec_offset:
366 DWARFFormValue::dump(raw_ostream &OS, const DWARFUnit *cu) const {
367 uint64_t uvalue = Value.uval;
368 bool cu_relative_offset = false;
371 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
372 case DW_FORM_GNU_addr_index: {
373 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
375 if (cu->getAddrOffsetSectionItem(uvalue, Address))
376 OS << format("0x%016" PRIx64, Address);
378 OS << "<no .debug_addr section>";
381 case DW_FORM_flag_present: OS << "true"; break;
383 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
384 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
385 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
386 case DW_FORM_ref_sig8:
387 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
390 OS.write_escaped(Value.cstr);
393 case DW_FORM_exprloc:
400 case DW_FORM_exprloc:
401 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
402 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
403 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
404 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
408 const uint8_t* data_ptr = Value.data;
410 // uvalue contains size of block
411 const uint8_t* end_data_ptr = data_ptr + uvalue;
412 while (data_ptr < end_data_ptr) {
413 OS << format("%2.2x ", *data_ptr);
422 case DW_FORM_sdata: OS << Value.sval; break;
423 case DW_FORM_udata: OS << Value.uval; break;
425 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
426 Optional<const char *> DbgStr = getAsCString(cu);
427 if (DbgStr.hasValue()) {
428 raw_ostream &COS = WithColor(OS, syntax::String);
430 COS.write_escaped(DbgStr.getValue());
435 case DW_FORM_GNU_str_index: {
436 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
437 Optional<const char *> DbgStr = getAsCString(cu);
438 if (DbgStr.hasValue()) {
439 raw_ostream &COS = WithColor(OS, syntax::String);
441 COS.write_escaped(DbgStr.getValue());
446 case DW_FORM_ref_addr:
447 OS << format("0x%016" PRIx64, uvalue);
450 cu_relative_offset = true;
451 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
454 cu_relative_offset = true;
455 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
458 cu_relative_offset = true;
459 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
462 cu_relative_offset = true;
463 OS << format("cu + 0x%8.8" PRIx64, uvalue);
465 case DW_FORM_ref_udata:
466 cu_relative_offset = true;
467 OS << format("cu + 0x%" PRIx64, uvalue);
470 // All DW_FORM_indirect attributes should be resolved prior to calling
472 case DW_FORM_indirect:
473 OS << "DW_FORM_indirect";
476 // Should be formatted to 64-bit for DWARF64.
477 case DW_FORM_sec_offset:
478 OS << format("0x%08x", (uint32_t)uvalue);
482 OS << format("DW_FORM(0x%4.4x)", Form);
486 if (cu_relative_offset) {
488 WithColor(OS, syntax::Address).get()
489 << format("0x%8.8" PRIx64, uvalue + (cu ? cu->getOffset() : 0));
494 Optional<const char *> DWARFFormValue::getAsCString(const DWARFUnit *U) const {
495 if (!isFormClass(FC_String))
497 if (Form == DW_FORM_string)
501 uint32_t Offset = Value.uval;
502 if (Form == DW_FORM_GNU_str_index) {
504 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
508 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
514 Optional<uint64_t> DWARFFormValue::getAsAddress(const DWARFUnit *U) const {
515 if (!isFormClass(FC_Address))
517 if (Form == DW_FORM_GNU_addr_index) {
518 uint32_t Index = Value.uval;
520 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
527 Optional<uint64_t> DWARFFormValue::getAsReference(const DWARFUnit *U) const {
528 if (!isFormClass(FC_Reference))
535 case DW_FORM_ref_udata:
538 return Value.uval + U->getOffset();
539 case DW_FORM_ref_addr:
541 // FIXME: Add proper support for DW_FORM_ref_sig8
547 Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
548 if (!isFormClass(FC_SectionOffset))
553 Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
554 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag))
555 || Form == DW_FORM_sdata)
560 Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
561 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
563 return ArrayRef<uint8_t>(Value.data, Value.uval);