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 "llvm/DebugInfo/DWARFFormValue.h"
11 #include "DWARFCompileUnit.h"
12 #include "DWARFContext.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/Dwarf.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/raw_ostream.h"
21 using namespace dwarf;
24 template <uint8_t AddrSize, uint8_t RefAddrSize> struct FixedFormSizes {
25 // FIXME: do we need a template here? Will a stack-allocated struct with
26 // an initializer in getFixedFormSizes() work just fine?
27 static const uint8_t sizes[27];
31 template <uint8_t AddrSize, uint8_t RefAddrSize>
32 const uint8_t FixedFormSizes<AddrSize, RefAddrSize>::sizes[] = {
34 AddrSize, // 0x01 DW_FORM_addr
36 0, // 0x03 DW_FORM_block2
37 0, // 0x04 DW_FORM_block4
38 2, // 0x05 DW_FORM_data2
39 4, // 0x06 DW_FORM_data4
40 8, // 0x07 DW_FORM_data8
41 0, // 0x08 DW_FORM_string
42 0, // 0x09 DW_FORM_block
43 0, // 0x0a DW_FORM_block1
44 1, // 0x0b DW_FORM_data1
45 1, // 0x0c DW_FORM_flag
46 0, // 0x0d DW_FORM_sdata
47 4, // 0x0e DW_FORM_strp
48 0, // 0x0f DW_FORM_udata
49 RefAddrSize, // 0x10 DW_FORM_ref_addr
50 1, // 0x11 DW_FORM_ref1
51 2, // 0x12 DW_FORM_ref2
52 4, // 0x13 DW_FORM_ref4
53 8, // 0x14 DW_FORM_ref8
54 0, // 0x15 DW_FORM_ref_udata
55 0, // 0x16 DW_FORM_indirect
56 4, // 0x17 DW_FORM_sec_offset
57 0, // 0x18 DW_FORM_exprloc
58 0, // 0x19 DW_FORM_flag_present
59 8, // 0x20 DW_FORM_ref_sig8
62 static uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
63 // FIXME: Support DWARF64.
64 return (Version == 2) ? AddrSize : 4;
67 ArrayRef<uint8_t> DWARFFormValue::getFixedFormSizes(uint8_t AddrSize,
69 uint8_t RefAddrSize = getRefAddrSize(AddrSize, Version);
70 if (AddrSize == 4 && RefAddrSize == 4)
71 return makeArrayRef(FixedFormSizes<4, 4>::sizes);
72 if (AddrSize == 4 && RefAddrSize == 8)
73 return makeArrayRef(FixedFormSizes<4, 8>::sizes);
74 if (AddrSize == 8 && RefAddrSize == 4)
75 return makeArrayRef(FixedFormSizes<8, 4>::sizes);
76 if (AddrSize == 8 && RefAddrSize == 8)
77 return makeArrayRef(FixedFormSizes<8, 8>::sizes);
81 static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
82 DWARFFormValue::FC_Unknown, // 0x0
83 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
84 DWARFFormValue::FC_Unknown, // 0x02 unused
85 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
86 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
87 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
88 // --- These can be FC_SectionOffset in DWARF3 and below:
89 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
90 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
92 DWARFFormValue::FC_String, // 0x08 DW_FORM_string
93 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
94 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
95 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
96 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
97 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
98 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
99 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
100 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
101 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
102 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
103 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
104 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
105 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
106 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
107 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
108 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
109 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
110 DWARFFormValue::FC_Reference, // 0x20 DW_FORM_ref_sig8
113 bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
114 // First, check DWARF4 form classes.
115 if (Form < ArrayRef<FormClass>(DWARF4FormClasses).size() &&
116 DWARF4FormClasses[Form] == FC)
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;
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: {
145 (Form == DW_FORM_addr)
146 ? cu->getAddressByteSize()
147 : getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
148 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
149 if (AI != cu->getRelocMap()->end()) {
150 const std::pair<uint8_t, int64_t> &R = AI->second;
151 Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
153 Value.uval = data.getUnsigned(offset_ptr, AddrSize);
156 case DW_FORM_exprloc:
158 Value.uval = data.getULEB128(offset_ptr);
162 Value.uval = data.getU8(offset_ptr);
166 Value.uval = data.getU16(offset_ptr);
170 Value.uval = data.getU32(offset_ptr);
176 Value.uval = data.getU8(offset_ptr);
180 Value.uval = data.getU16(offset_ptr);
184 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
185 Value.uval = data.getU32(offset_ptr);
186 if (AI != cu->getRelocMap()->end())
187 Value.uval += AI->second.second;
192 Value.uval = data.getU64(offset_ptr);
195 Value.sval = data.getSLEB128(offset_ptr);
198 RelocAddrMap::const_iterator AI
199 = cu->getRelocMap()->find(*offset_ptr);
200 if (AI != cu->getRelocMap()->end()) {
201 const std::pair<uint8_t, int64_t> &R = AI->second;
202 Value.uval = data.getU32(offset_ptr) + R.second;
204 Value.uval = data.getU32(offset_ptr);
208 case DW_FORM_ref_udata:
209 Value.uval = data.getULEB128(offset_ptr);
212 Value.cstr = data.getCStr(offset_ptr);
214 case DW_FORM_indirect:
215 Form = data.getULEB128(offset_ptr);
218 case DW_FORM_sec_offset: {
219 // FIXME: This is 64-bit for DWARF64.
220 RelocAddrMap::const_iterator AI
221 = cu->getRelocMap()->find(*offset_ptr);
222 if (AI != cu->getRelocMap()->end()) {
223 const std::pair<uint8_t, int64_t> &R = AI->second;
224 Value.uval = data.getU32(offset_ptr) + R.second;
226 Value.uval = data.getU32(offset_ptr);
229 case DW_FORM_flag_present:
232 case DW_FORM_ref_sig8:
233 Value.uval = data.getU64(offset_ptr);
235 case DW_FORM_GNU_addr_index:
236 case DW_FORM_GNU_str_index:
237 Value.uval = data.getULEB128(offset_ptr);
245 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
248 Value.data = reinterpret_cast<const uint8_t *>(str.data());
249 *offset_ptr += Value.uval;
257 DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
258 const DWARFUnit *cu) const {
259 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
263 DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
264 uint32_t *offset_ptr, const DWARFUnit *cu) {
265 bool indirect = false;
268 // Blocks if inlined data that have a length field and the data bytes
269 // inlined in the .debug_info
270 case DW_FORM_exprloc:
271 case DW_FORM_block: {
272 uint64_t size = debug_info_data.getULEB128(offset_ptr);
276 case DW_FORM_block1: {
277 uint8_t size = debug_info_data.getU8(offset_ptr);
281 case DW_FORM_block2: {
282 uint16_t size = debug_info_data.getU16(offset_ptr);
286 case DW_FORM_block4: {
287 uint32_t size = debug_info_data.getU32(offset_ptr);
292 // Inlined NULL terminated C-strings
294 debug_info_data.getCStr(offset_ptr);
297 // Compile unit address sized values
299 *offset_ptr += cu->getAddressByteSize();
301 case DW_FORM_ref_addr:
302 *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
305 // 0 byte values - implied from the form.
306 case DW_FORM_flag_present:
332 case DW_FORM_ref_sig8:
336 // signed or unsigned LEB 128 values
337 // case DW_FORM_APPLE_db_str:
340 case DW_FORM_ref_udata:
341 case DW_FORM_GNU_str_index:
342 case DW_FORM_GNU_addr_index:
343 debug_info_data.getULEB128(offset_ptr);
346 case DW_FORM_indirect:
348 form = debug_info_data.getULEB128(offset_ptr);
351 // FIXME: 4 for DWARF32, 8 for DWARF64.
352 case DW_FORM_sec_offset:
364 DWARFFormValue::dump(raw_ostream &OS, const DWARFUnit *cu) const {
365 DataExtractor debug_str_data(cu->getStringSection(), true, 0);
366 DataExtractor debug_str_offset_data(cu->getStringOffsetSection(), true, 0);
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()) {
429 OS.write_escaped(DbgStr.getValue());
434 case DW_FORM_GNU_str_index: {
435 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
436 Optional<const char *> DbgStr = getAsCString(cu);
437 if (DbgStr.hasValue()) {
439 OS.write_escaped(DbgStr.getValue());
444 case DW_FORM_ref_addr:
445 OS << format("0x%016" PRIx64, uvalue);
448 cu_relative_offset = true;
449 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
452 cu_relative_offset = true;
453 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
456 cu_relative_offset = true;
457 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
460 cu_relative_offset = true;
461 OS << format("cu + 0x%8.8" PRIx64, uvalue);
463 case DW_FORM_ref_udata:
464 cu_relative_offset = true;
465 OS << format("cu + 0x%" PRIx64, uvalue);
468 // All DW_FORM_indirect attributes should be resolved prior to calling
470 case DW_FORM_indirect:
471 OS << "DW_FORM_indirect";
474 // Should be formatted to 64-bit for DWARF64.
475 case DW_FORM_sec_offset:
476 OS << format("0x%08x", (uint32_t)uvalue);
480 OS << format("DW_FORM(0x%4.4x)", Form);
484 if (cu_relative_offset)
485 OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
488 Optional<const char *> DWARFFormValue::getAsCString(const DWARFUnit *U) const {
489 if (!isFormClass(FC_String))
491 if (Form == DW_FORM_string)
495 uint32_t Offset = Value.uval;
496 if (Form == DW_FORM_GNU_str_index) {
498 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
502 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
508 Optional<uint64_t> DWARFFormValue::getAsAddress(const DWARFUnit *U) const {
509 if (!isFormClass(FC_Address))
511 if (Form == DW_FORM_GNU_addr_index) {
512 uint32_t Index = Value.uval;
514 if (U == 0 || !U->getAddrOffsetSectionItem(Index, Result))
521 Optional<uint64_t> DWARFFormValue::getAsReference(const DWARFUnit *U) const {
522 if (!isFormClass(FC_Reference))
529 case DW_FORM_ref_udata:
532 return Value.uval + U->getOffset();
533 case DW_FORM_ref_addr:
535 // FIXME: Add proper support for DW_FORM_ref_sig8
541 Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
542 if (!isFormClass(FC_SectionOffset))
547 Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
548 if (!isFormClass(FC_Constant) || Form == DW_FORM_sdata)