1 //===-- DWARFFormValue.cpp --------------------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
12 #include "lldb/Core/dwarf.h"
13 #include "lldb/Core/Stream.h"
15 #include "DWARFFormValue.h"
16 #include "DWARFCompileUnit.h"
18 class DWARFCompileUnit;
20 using namespace lldb_private;
23 static uint8_t g_form_sizes_addr4[] =
26 4, // 0x01 DW_FORM_addr
28 0, // 0x03 DW_FORM_block2
29 0, // 0x04 DW_FORM_block4
30 2, // 0x05 DW_FORM_data2
31 4, // 0x06 DW_FORM_data4
32 8, // 0x07 DW_FORM_data8
33 0, // 0x08 DW_FORM_string
34 0, // 0x09 DW_FORM_block
35 0, // 0x0a DW_FORM_block1
36 1, // 0x0b DW_FORM_data1
37 1, // 0x0c DW_FORM_flag
38 0, // 0x0d DW_FORM_sdata
39 4, // 0x0e DW_FORM_strp
40 0, // 0x0f DW_FORM_udata
41 0, // 0x10 DW_FORM_ref_addr (addr size for DWARF2 and earlier, 4 bytes for DWARF32, 8 bytes for DWARF32 in DWARF 3 and later
42 1, // 0x11 DW_FORM_ref1
43 2, // 0x12 DW_FORM_ref2
44 4, // 0x13 DW_FORM_ref4
45 8, // 0x14 DW_FORM_ref8
46 0, // 0x15 DW_FORM_ref_udata
47 0, // 0x16 DW_FORM_indirect
48 4, // 0x17 DW_FORM_sec_offset
49 0, // 0x18 DW_FORM_exprloc
50 0, // 0x19 DW_FORM_flag_present
57 8, // 0x20 DW_FORM_ref_sig8
62 g_form_sizes_addr8[] =
65 8, // 0x01 DW_FORM_addr
67 0, // 0x03 DW_FORM_block2
68 0, // 0x04 DW_FORM_block4
69 2, // 0x05 DW_FORM_data2
70 4, // 0x06 DW_FORM_data4
71 8, // 0x07 DW_FORM_data8
72 0, // 0x08 DW_FORM_string
73 0, // 0x09 DW_FORM_block
74 0, // 0x0a DW_FORM_block1
75 1, // 0x0b DW_FORM_data1
76 1, // 0x0c DW_FORM_flag
77 0, // 0x0d DW_FORM_sdata
78 4, // 0x0e DW_FORM_strp
79 0, // 0x0f DW_FORM_udata
80 0, // 0x10 DW_FORM_ref_addr (addr size for DWARF2 and earlier, 4 bytes for DWARF32, 8 bytes for DWARF32 in DWARF 3 and later
81 1, // 0x11 DW_FORM_ref1
82 2, // 0x12 DW_FORM_ref2
83 4, // 0x13 DW_FORM_ref4
84 8, // 0x14 DW_FORM_ref8
85 0, // 0x15 DW_FORM_ref_udata
86 0, // 0x16 DW_FORM_indirect
87 4, // 0x17 DW_FORM_sec_offset
88 0, // 0x18 DW_FORM_exprloc
89 0, // 0x19 DW_FORM_flag_present
96 8, // 0x20 DW_FORM_ref_sig8
99 // Difference with g_form_sizes_addr8:
100 // DW_FORM_strp and DW_FORM_sec_offset are 8 instead of 4
102 g_form_sizes_addr8_dwarf64[] =
105 8, // 0x01 DW_FORM_addr
107 0, // 0x03 DW_FORM_block2
108 0, // 0x04 DW_FORM_block4
109 2, // 0x05 DW_FORM_data2
110 4, // 0x06 DW_FORM_data4
111 8, // 0x07 DW_FORM_data8
112 0, // 0x08 DW_FORM_string
113 0, // 0x09 DW_FORM_block
114 0, // 0x0a DW_FORM_block1
115 1, // 0x0b DW_FORM_data1
116 1, // 0x0c DW_FORM_flag
117 0, // 0x0d DW_FORM_sdata
118 8, // 0x0e DW_FORM_strp
119 0, // 0x0f DW_FORM_udata
120 0, // 0x10 DW_FORM_ref_addr (addr size for DWARF2 and earlier, 4 bytes for DWARF32, 8 bytes for DWARF32 in DWARF 3 and later
121 1, // 0x11 DW_FORM_ref1
122 2, // 0x12 DW_FORM_ref2
123 4, // 0x13 DW_FORM_ref4
124 8, // 0x14 DW_FORM_ref8
125 0, // 0x15 DW_FORM_ref_udata
126 0, // 0x16 DW_FORM_indirect
127 8, // 0x17 DW_FORM_sec_offset
128 0, // 0x18 DW_FORM_exprloc
129 0, // 0x19 DW_FORM_flag_present
136 8, // 0x20 DW_FORM_ref_sig8
139 DWARFFormValue::FixedFormSizes
140 DWARFFormValue::GetFixedFormSizesForAddressSize (uint8_t addr_size, bool is_dwarf64)
145 case 4: return FixedFormSizes(g_form_sizes_addr4, sizeof(g_form_sizes_addr4));
146 case 8: return FixedFormSizes(g_form_sizes_addr8, sizeof(g_form_sizes_addr8));
150 return FixedFormSizes(g_form_sizes_addr8_dwarf64, sizeof(g_form_sizes_addr8_dwarf64));
151 // is_dwarf64 && addr_size == 4 : no provider does this.
153 return FixedFormSizes();
156 DWARFFormValue::DWARFFormValue() :
163 DWARFFormValue::DWARFFormValue(const DWARFCompileUnit* cu, dw_form_t form) :
171 DWARFFormValue::Clear()
175 memset(&m_value, 0, sizeof(m_value));
179 DWARFFormValue::ExtractValue(const DWARFDataExtractor& data, lldb::offset_t* offset_ptr)
181 bool indirect = false;
182 bool is_block = false;
184 uint8_t ref_addr_size;
185 // Read the value for the form into value and follow and DW_FORM_indirect instances we run into
191 case DW_FORM_addr: assert(m_cu);
192 m_value.value.uval = data.GetMaxU64(offset_ptr, DWARFCompileUnit::GetAddressByteSize(m_cu)); break;
193 case DW_FORM_block2: m_value.value.uval = data.GetU16(offset_ptr); is_block = true; break;
194 case DW_FORM_block4: m_value.value.uval = data.GetU32(offset_ptr); is_block = true; break;
195 case DW_FORM_data2: m_value.value.uval = data.GetU16(offset_ptr); break;
196 case DW_FORM_data4: m_value.value.uval = data.GetU32(offset_ptr); break;
197 case DW_FORM_data8: m_value.value.uval = data.GetU64(offset_ptr); break;
198 case DW_FORM_string: m_value.value.cstr = data.GetCStr(offset_ptr); break;
199 case DW_FORM_exprloc:
200 case DW_FORM_block: m_value.value.uval = data.GetULEB128(offset_ptr); is_block = true; break;
201 case DW_FORM_block1: m_value.value.uval = data.GetU8(offset_ptr); is_block = true; break;
202 case DW_FORM_data1: m_value.value.uval = data.GetU8(offset_ptr); break;
203 case DW_FORM_flag: m_value.value.uval = data.GetU8(offset_ptr); break;
204 case DW_FORM_sdata: m_value.value.sval = data.GetSLEB128(offset_ptr); break;
205 case DW_FORM_strp: assert(m_cu);
206 m_value.value.uval = data.GetMaxU64(offset_ptr, DWARFCompileUnit::IsDWARF64(m_cu) ? 8 : 4); break;
207 // case DW_FORM_APPLE_db_str:
208 case DW_FORM_udata: m_value.value.uval = data.GetULEB128(offset_ptr); break;
209 case DW_FORM_ref_addr: assert(m_cu);
211 if (m_cu->GetVersion() <= 2)
212 ref_addr_size = m_cu->GetAddressByteSize();
214 ref_addr_size = m_cu->IsDWARF64() ? 8 : 4;
215 m_value.value.uval = data.GetMaxU64(offset_ptr, ref_addr_size); break;
216 case DW_FORM_ref1: m_value.value.uval = data.GetU8(offset_ptr); break;
217 case DW_FORM_ref2: m_value.value.uval = data.GetU16(offset_ptr); break;
218 case DW_FORM_ref4: m_value.value.uval = data.GetU32(offset_ptr); break;
219 case DW_FORM_ref8: m_value.value.uval = data.GetU64(offset_ptr); break;
220 case DW_FORM_ref_udata: m_value.value.uval = data.GetULEB128(offset_ptr); break;
221 case DW_FORM_indirect:
222 m_form = data.GetULEB128(offset_ptr);
226 case DW_FORM_sec_offset: assert(m_cu);
227 m_value.value.uval = data.GetMaxU64(offset_ptr, DWARFCompileUnit::IsDWARF64(m_cu) ? 8 : 4); break;
228 case DW_FORM_flag_present: m_value.value.uval = 1; break;
229 case DW_FORM_ref_sig8: m_value.value.uval = data.GetU64(offset_ptr); break;
230 case DW_FORM_GNU_str_index: m_value.value.uval = data.GetULEB128(offset_ptr); break;
231 case DW_FORM_GNU_addr_index: m_value.value.uval = data.GetULEB128(offset_ptr); break;
240 m_value.data = data.PeekData(*offset_ptr, m_value.value.uval);
241 if (m_value.data != NULL)
243 *offset_ptr += m_value.value.uval;
251 DWARFFormValue::SkipValue(const DWARFDataExtractor& debug_info_data, lldb::offset_t *offset_ptr) const
253 return DWARFFormValue::SkipValue(m_form, debug_info_data, offset_ptr, m_cu);
257 DWARFFormValue::SkipValue(dw_form_t form, const DWARFDataExtractor& debug_info_data, lldb::offset_t *offset_ptr, const DWARFCompileUnit* cu)
259 uint8_t ref_addr_size;
262 // Blocks if inlined data that have a length field and the data bytes
263 // inlined in the .debug_info
264 case DW_FORM_exprloc:
265 case DW_FORM_block: { dw_uleb128_t size = debug_info_data.GetULEB128(offset_ptr); *offset_ptr += size; } return true;
266 case DW_FORM_block1: { dw_uleb128_t size = debug_info_data.GetU8(offset_ptr); *offset_ptr += size; } return true;
267 case DW_FORM_block2: { dw_uleb128_t size = debug_info_data.GetU16(offset_ptr); *offset_ptr += size; } return true;
268 case DW_FORM_block4: { dw_uleb128_t size = debug_info_data.GetU32(offset_ptr); *offset_ptr += size; } return true;
270 // Inlined NULL terminated C-strings
272 debug_info_data.GetCStr(offset_ptr);
275 // Compile unit address sized values
277 *offset_ptr += DWARFCompileUnit::GetAddressByteSize(cu);
280 case DW_FORM_ref_addr:
282 assert (cu); // CU must be valid for DW_FORM_ref_addr objects or we will get this wrong
283 if (cu->GetVersion() <= 2)
284 ref_addr_size = cu->GetAddressByteSize();
286 ref_addr_size = cu->IsDWARF64() ? 8 : 4;
287 *offset_ptr += ref_addr_size;
290 // 0 bytes values (implied from DW_FORM)
291 case DW_FORM_flag_present:
307 // 32 bit for DWARF 32, 64 for DWARF 64
308 case DW_FORM_sec_offset:
311 *offset_ptr += (cu->IsDWARF64() ? 8 : 4);
323 case DW_FORM_ref_sig8:
327 // signed or unsigned LEB 128 values
330 case DW_FORM_ref_udata:
331 case DW_FORM_GNU_addr_index:
332 case DW_FORM_GNU_str_index:
333 debug_info_data.Skip_LEB128(offset_ptr);
336 case DW_FORM_indirect:
338 dw_form_t indirect_form = debug_info_data.GetULEB128(offset_ptr);
339 return DWARFFormValue::SkipValue (indirect_form,
353 DWARFFormValue::Dump(Stream &s) const
355 uint64_t uvalue = Unsigned();
356 bool cu_relative_offset = false;
358 bool verbose = s.GetVerbose();
362 case DW_FORM_addr: s.Address(uvalue, sizeof (uint64_t)); break;
364 case DW_FORM_data1: s.PutHex8(uvalue); break;
365 case DW_FORM_data2: s.PutHex16(uvalue); break;
366 case DW_FORM_sec_offset:
367 case DW_FORM_data4: s.PutHex32(uvalue); break;
368 case DW_FORM_ref_sig8:
369 case DW_FORM_data8: s.PutHex64(uvalue); break;
370 case DW_FORM_string: s.QuotedCString(AsCString()); break;
371 case DW_FORM_exprloc:
380 case DW_FORM_exprloc:
381 case DW_FORM_block: s.Printf("<0x%" PRIx64 "> ", uvalue); break;
382 case DW_FORM_block1: s.Printf("<0x%2.2x> ", (uint8_t)uvalue); break;
383 case DW_FORM_block2: s.Printf("<0x%4.4x> ", (uint16_t)uvalue); break;
384 case DW_FORM_block4: s.Printf("<0x%8.8x> ", (uint32_t)uvalue); break;
388 const uint8_t* data_ptr = m_value.data;
391 const uint8_t* end_data_ptr = data_ptr + uvalue; // uvalue contains size of block
392 while (data_ptr < end_data_ptr)
394 s.Printf("%2.2x ", *data_ptr);
399 s.PutCString("NULL");
403 case DW_FORM_sdata: s.PutSLEB128(uvalue); break;
404 case DW_FORM_udata: s.PutULEB128(uvalue); break;
407 const char* dbg_str = AsCString();
411 s.Printf(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
412 s.QuotedCString(dbg_str);
421 case DW_FORM_ref_addr:
423 assert (m_cu); // CU must be valid for DW_FORM_ref_addr objects or we will get this wrong
424 if (m_cu->GetVersion() <= 2)
425 s.Address(uvalue, sizeof (uint64_t) * 2);
427 s.Address(uvalue, 4 * 2);// 4 for DWARF32, 8 for DWARF64, but we don't support DWARF64 yet
430 case DW_FORM_ref1: cu_relative_offset = true; if (verbose) s.Printf("cu + 0x%2.2x", (uint8_t)uvalue); break;
431 case DW_FORM_ref2: cu_relative_offset = true; if (verbose) s.Printf("cu + 0x%4.4x", (uint16_t)uvalue); break;
432 case DW_FORM_ref4: cu_relative_offset = true; if (verbose) s.Printf("cu + 0x%4.4x", (uint32_t)uvalue); break;
433 case DW_FORM_ref8: cu_relative_offset = true; if (verbose) s.Printf("cu + 0x%8.8" PRIx64, uvalue); break;
434 case DW_FORM_ref_udata: cu_relative_offset = true; if (verbose) s.Printf("cu + 0x%" PRIx64, uvalue); break;
436 // All DW_FORM_indirect attributes should be resolved prior to calling this function
437 case DW_FORM_indirect: s.PutCString("DW_FORM_indirect"); break;
438 case DW_FORM_flag_present: break;
440 s.Printf("DW_FORM(0x%4.4x)", m_form);
444 if (cu_relative_offset)
446 assert (m_cu); // CU must be valid for DW_FORM_ref forms that are compile unit relative or we will get this wrong
448 s.PutCString(" => ");
450 s.Printf("{0x%8.8" PRIx64 "}", uvalue + m_cu->GetOffset());
455 DWARFFormValue::AsCString() const
457 SymbolFileDWARF* symbol_file = m_cu->GetSymbolFileDWARF();
459 if (m_form == DW_FORM_string)
461 return m_value.value.cstr;
463 else if (m_form == DW_FORM_strp)
468 return symbol_file->get_debug_str_data().PeekCStr(m_value.value.uval);
470 else if (m_form == DW_FORM_GNU_str_index)
475 uint32_t index_size = m_cu->IsDWARF64() ? 8 : 4;
476 lldb::offset_t offset = m_value.value.uval * index_size;
477 dw_offset_t str_offset = symbol_file->get_debug_str_offsets_data().GetMaxU64(&offset, index_size);
478 return symbol_file->get_debug_str_data().PeekCStr(str_offset);
484 DWARFFormValue::Address() const
486 SymbolFileDWARF* symbol_file = m_cu->GetSymbolFileDWARF();
488 if (m_form == DW_FORM_addr)
492 assert(m_form == DW_FORM_GNU_addr_index);
497 uint32_t index_size = m_cu->GetAddressByteSize();
498 dw_offset_t addr_base = m_cu->GetAddrBase();
499 lldb::offset_t offset = addr_base + m_value.value.uval * index_size;
500 return symbol_file->get_debug_addr_data().GetMaxU64(&offset, index_size);
504 DWARFFormValue::Reference() const
506 uint64_t die_offset = m_value.value.uval;
513 case DW_FORM_ref_udata:
514 assert (m_cu); // CU must be valid for DW_FORM_ref forms that are compile unit relative or we will get this wrong
515 die_offset += m_cu->GetOffset();
526 DWARFFormValue::Reference (dw_offset_t base_offset) const
528 uint64_t die_offset = m_value.value.uval;
535 case DW_FORM_ref_udata:
536 die_offset += base_offset;
548 DWARFFormValue::BlockData() const
555 DWARFFormValue::IsBlockForm(const dw_form_t form)
569 DWARFFormValue::IsDataForm(const dw_form_t form)
585 DWARFFormValue::Compare (const DWARFFormValue& a_value,
586 const DWARFFormValue& b_value)
588 dw_form_t a_form = a_value.Form();
589 dw_form_t b_form = b_value.Form();
603 case DW_FORM_ref_addr:
604 case DW_FORM_sec_offset:
605 case DW_FORM_flag_present:
606 case DW_FORM_ref_sig8:
607 case DW_FORM_GNU_addr_index:
609 uint64_t a = a_value.Unsigned();
610 uint64_t b = b_value.Unsigned();
620 int64_t a = a_value.Signed();
621 int64_t b = b_value.Signed();
631 case DW_FORM_GNU_str_index:
633 const char *a_string = a_value.AsCString();
634 const char *b_string = b_value.AsCString();
635 if (a_string == b_string)
637 else if (a_string && b_string)
638 return strcmp(a_string, b_string);
639 else if (a_string == NULL)
640 return -1; // A string is NULL, and B is valid
642 return 1; // A string valid, and B is NULL
650 case DW_FORM_exprloc:
652 uint64_t a_len = a_value.Unsigned();
653 uint64_t b_len = b_value.Unsigned();
658 // The block lengths are the same
659 return memcmp(a_value.BlockData(), b_value.BlockData(), a_value.Unsigned());
667 case DW_FORM_ref_udata:
669 uint64_t a = a_value.Reference();
670 uint64_t b = b_value.Reference();
678 case DW_FORM_indirect:
679 assert(!"This shouldn't happen after the form has been extracted...");
683 assert(!"Unhandled DW_FORM");