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/Utility/Stream.h"
15 #include "DWARFUnit.h"
16 #include "DWARFFormValue.h"
20 using namespace lldb_private;
22 static uint8_t g_form_sizes_addr4[] = {
24 4, // 0x01 DW_FORM_addr
26 0, // 0x03 DW_FORM_block2
27 0, // 0x04 DW_FORM_block4
28 2, // 0x05 DW_FORM_data2
29 4, // 0x06 DW_FORM_data4
30 8, // 0x07 DW_FORM_data8
31 0, // 0x08 DW_FORM_string
32 0, // 0x09 DW_FORM_block
33 0, // 0x0a DW_FORM_block1
34 1, // 0x0b DW_FORM_data1
35 1, // 0x0c DW_FORM_flag
36 0, // 0x0d DW_FORM_sdata
37 4, // 0x0e DW_FORM_strp
38 0, // 0x0f DW_FORM_udata
39 0, // 0x10 DW_FORM_ref_addr (addr size for DWARF2 and earlier, 4 bytes for
40 // DWARF32, 8 bytes for DWARF32 in DWARF 3 and later
41 1, // 0x11 DW_FORM_ref1
42 2, // 0x12 DW_FORM_ref2
43 4, // 0x13 DW_FORM_ref4
44 8, // 0x14 DW_FORM_ref8
45 0, // 0x15 DW_FORM_ref_udata
46 0, // 0x16 DW_FORM_indirect
47 4, // 0x17 DW_FORM_sec_offset
48 0, // 0x18 DW_FORM_exprloc
49 0, // 0x19 DW_FORM_flag_present
56 8, // 0x20 DW_FORM_ref_sig8
60 static uint8_t g_form_sizes_addr8[] = {
62 8, // 0x01 DW_FORM_addr
64 0, // 0x03 DW_FORM_block2
65 0, // 0x04 DW_FORM_block4
66 2, // 0x05 DW_FORM_data2
67 4, // 0x06 DW_FORM_data4
68 8, // 0x07 DW_FORM_data8
69 0, // 0x08 DW_FORM_string
70 0, // 0x09 DW_FORM_block
71 0, // 0x0a DW_FORM_block1
72 1, // 0x0b DW_FORM_data1
73 1, // 0x0c DW_FORM_flag
74 0, // 0x0d DW_FORM_sdata
75 4, // 0x0e DW_FORM_strp
76 0, // 0x0f DW_FORM_udata
77 0, // 0x10 DW_FORM_ref_addr (addr size for DWARF2 and earlier, 4 bytes for
78 // DWARF32, 8 bytes for DWARF32 in DWARF 3 and later
79 1, // 0x11 DW_FORM_ref1
80 2, // 0x12 DW_FORM_ref2
81 4, // 0x13 DW_FORM_ref4
82 8, // 0x14 DW_FORM_ref8
83 0, // 0x15 DW_FORM_ref_udata
84 0, // 0x16 DW_FORM_indirect
85 4, // 0x17 DW_FORM_sec_offset
86 0, // 0x18 DW_FORM_exprloc
87 0, // 0x19 DW_FORM_flag_present
94 8, // 0x20 DW_FORM_ref_sig8
97 // Difference with g_form_sizes_addr8:
98 // DW_FORM_strp and DW_FORM_sec_offset are 8 instead of 4
99 static uint8_t g_form_sizes_addr8_dwarf64[] = {
101 8, // 0x01 DW_FORM_addr
103 0, // 0x03 DW_FORM_block2
104 0, // 0x04 DW_FORM_block4
105 2, // 0x05 DW_FORM_data2
106 4, // 0x06 DW_FORM_data4
107 8, // 0x07 DW_FORM_data8
108 0, // 0x08 DW_FORM_string
109 0, // 0x09 DW_FORM_block
110 0, // 0x0a DW_FORM_block1
111 1, // 0x0b DW_FORM_data1
112 1, // 0x0c DW_FORM_flag
113 0, // 0x0d DW_FORM_sdata
114 8, // 0x0e DW_FORM_strp
115 0, // 0x0f DW_FORM_udata
116 0, // 0x10 DW_FORM_ref_addr (addr size for DWARF2 and earlier, 4 bytes for
117 // DWARF32, 8 bytes for DWARF32 in DWARF 3 and later
118 1, // 0x11 DW_FORM_ref1
119 2, // 0x12 DW_FORM_ref2
120 4, // 0x13 DW_FORM_ref4
121 8, // 0x14 DW_FORM_ref8
122 0, // 0x15 DW_FORM_ref_udata
123 0, // 0x16 DW_FORM_indirect
124 8, // 0x17 DW_FORM_sec_offset
125 0, // 0x18 DW_FORM_exprloc
126 0, // 0x19 DW_FORM_flag_present
133 8, // 0x20 DW_FORM_ref_sig8
136 DWARFFormValue::FixedFormSizes
137 DWARFFormValue::GetFixedFormSizesForAddressSize(uint8_t addr_size,
142 return FixedFormSizes(g_form_sizes_addr4, sizeof(g_form_sizes_addr4));
144 return FixedFormSizes(g_form_sizes_addr8, sizeof(g_form_sizes_addr8));
148 return FixedFormSizes(g_form_sizes_addr8_dwarf64,
149 sizeof(g_form_sizes_addr8_dwarf64));
150 // is_dwarf64 && addr_size == 4 : no provider does this.
152 return FixedFormSizes();
155 DWARFFormValue::DWARFFormValue() : m_cu(NULL), m_form(0), m_value() {}
157 DWARFFormValue::DWARFFormValue(const DWARFUnit *cu)
158 : m_cu(cu), m_form(0), m_value() {}
160 DWARFFormValue::DWARFFormValue(const DWARFUnit *cu, dw_form_t form)
161 : m_cu(cu), m_form(form), m_value() {}
163 void DWARFFormValue::Clear() {
166 memset(&m_value, 0, sizeof(m_value));
169 bool DWARFFormValue::ExtractValue(const DWARFDataExtractor &data,
170 lldb::offset_t *offset_ptr) {
171 if (m_form == DW_FORM_implicit_const)
174 bool indirect = false;
175 bool is_block = false;
177 uint8_t ref_addr_size;
178 // Read the value for the form into value and follow and DW_FORM_indirect
179 // instances we run into
186 data.GetMaxU64(offset_ptr, DWARFUnit::GetAddressByteSize(m_cu));
189 m_value.value.uval = data.GetU8(offset_ptr);
193 m_value.value.uval = data.GetU16(offset_ptr);
197 m_value.value.uval = data.GetU32(offset_ptr);
201 m_value.value.uval = 16;
204 case DW_FORM_exprloc:
206 m_value.value.uval = data.GetULEB128(offset_ptr);
210 m_value.value.cstr = data.GetCStr(offset_ptr);
213 m_value.value.sval = data.GetSLEB128(offset_ptr);
216 case DW_FORM_line_strp:
217 case DW_FORM_sec_offset:
220 data.GetMaxU64(offset_ptr, DWARFUnit::IsDWARF64(m_cu) ? 8 : 4);
227 m_value.value.uval = data.GetU8(offset_ptr);
233 m_value.value.uval = data.GetU16(offset_ptr);
237 m_value.value.uval = data.GetMaxU64(offset_ptr, 3);
243 m_value.value.uval = data.GetU32(offset_ptr);
247 case DW_FORM_ref_sig8:
248 m_value.value.uval = data.GetU64(offset_ptr);
251 case DW_FORM_rnglistx:
254 case DW_FORM_ref_udata:
255 case DW_FORM_GNU_str_index:
256 case DW_FORM_GNU_addr_index:
257 m_value.value.uval = data.GetULEB128(offset_ptr);
259 case DW_FORM_ref_addr:
261 if (m_cu->GetVersion() <= 2)
262 ref_addr_size = m_cu->GetAddressByteSize();
264 ref_addr_size = m_cu->IsDWARF64() ? 8 : 4;
265 m_value.value.uval = data.GetMaxU64(offset_ptr, ref_addr_size);
267 case DW_FORM_indirect:
268 m_form = data.GetULEB128(offset_ptr);
271 case DW_FORM_flag_present:
272 m_value.value.uval = 1;
280 m_value.data = data.PeekData(*offset_ptr, m_value.value.uval);
281 if (m_value.data != NULL) {
282 *offset_ptr += m_value.value.uval;
289 bool DWARFFormValue::SkipValue(const DWARFDataExtractor &debug_info_data,
290 lldb::offset_t *offset_ptr) const {
291 return DWARFFormValue::SkipValue(m_form, debug_info_data, offset_ptr, m_cu);
294 bool DWARFFormValue::SkipValue(dw_form_t form,
295 const DWARFDataExtractor &debug_info_data,
296 lldb::offset_t *offset_ptr,
297 const DWARFUnit *cu) {
298 uint8_t ref_addr_size;
300 // Blocks if inlined data that have a length field and the data bytes inlined
301 // in the .debug_info
302 case DW_FORM_exprloc:
303 case DW_FORM_block: {
304 dw_uleb128_t size = debug_info_data.GetULEB128(offset_ptr);
308 case DW_FORM_block1: {
309 dw_uleb128_t size = debug_info_data.GetU8(offset_ptr);
313 case DW_FORM_block2: {
314 dw_uleb128_t size = debug_info_data.GetU16(offset_ptr);
318 case DW_FORM_block4: {
319 dw_uleb128_t size = debug_info_data.GetU32(offset_ptr);
324 // Inlined NULL terminated C-strings
326 debug_info_data.GetCStr(offset_ptr);
329 // Compile unit address sized values
331 *offset_ptr += DWARFUnit::GetAddressByteSize(cu);
334 case DW_FORM_ref_addr:
336 assert(cu); // CU must be valid for DW_FORM_ref_addr objects or we will get
338 if (cu->GetVersion() <= 2)
339 ref_addr_size = cu->GetAddressByteSize();
341 ref_addr_size = cu->IsDWARF64() ? 8 : 4;
342 *offset_ptr += ref_addr_size;
345 // 0 bytes values (implied from DW_FORM)
346 case DW_FORM_flag_present:
347 case DW_FORM_implicit_const:
373 // 32 bit for DWARF 32, 64 for DWARF 64
374 case DW_FORM_sec_offset:
377 *offset_ptr += (cu->IsDWARF64() ? 8 : 4);
391 case DW_FORM_ref_sig8:
395 // signed or unsigned LEB 128 values
397 case DW_FORM_rnglistx:
400 case DW_FORM_ref_udata:
401 case DW_FORM_GNU_addr_index:
402 case DW_FORM_GNU_str_index:
404 debug_info_data.Skip_LEB128(offset_ptr);
407 case DW_FORM_indirect: {
408 dw_form_t indirect_form = debug_info_data.GetULEB128(offset_ptr);
409 return DWARFFormValue::SkipValue(indirect_form, debug_info_data, offset_ptr,
419 void DWARFFormValue::Dump(Stream &s) const {
420 uint64_t uvalue = Unsigned();
421 bool cu_relative_offset = false;
425 s.Address(uvalue, sizeof(uint64_t));
434 case DW_FORM_sec_offset:
438 case DW_FORM_ref_sig8:
443 s.QuotedCString(AsCString());
445 case DW_FORM_exprloc:
452 case DW_FORM_exprloc:
454 s.Printf("<0x%" PRIx64 "> ", uvalue);
457 s.Printf("<0x%2.2x> ", (uint8_t)uvalue);
460 s.Printf("<0x%4.4x> ", (uint16_t)uvalue);
463 s.Printf("<0x%8.8x> ", (uint32_t)uvalue);
469 const uint8_t *data_ptr = m_value.data;
471 const uint8_t *end_data_ptr =
472 data_ptr + uvalue; // uvalue contains size of block
473 while (data_ptr < end_data_ptr) {
474 s.Printf("%2.2x ", *data_ptr);
478 s.PutCString("NULL");
483 s.PutSLEB128(uvalue);
486 s.PutULEB128(uvalue);
489 const char *dbg_str = AsCString();
491 s.QuotedCString(dbg_str);
497 case DW_FORM_ref_addr: {
498 assert(m_cu); // CU must be valid for DW_FORM_ref_addr objects or we will
500 if (m_cu->GetVersion() <= 2)
501 s.Address(uvalue, sizeof(uint64_t) * 2);
503 s.Address(uvalue, 4 * 2); // 4 for DWARF32, 8 for DWARF64, but we don't
504 // support DWARF64 yet
508 cu_relative_offset = true;
511 cu_relative_offset = true;
514 cu_relative_offset = true;
517 cu_relative_offset = true;
519 case DW_FORM_ref_udata:
520 cu_relative_offset = true;
523 // All DW_FORM_indirect attributes should be resolved prior to calling this
525 case DW_FORM_indirect:
526 s.PutCString("DW_FORM_indirect");
528 case DW_FORM_flag_present:
531 s.Printf("DW_FORM(0x%4.4x)", m_form);
535 if (cu_relative_offset) {
536 assert(m_cu); // CU must be valid for DW_FORM_ref forms that are compile
537 // unit relative or we will get this wrong
538 s.Printf("{0x%8.8" PRIx64 "}", uvalue + m_cu->GetOffset());
542 const char *DWARFFormValue::AsCString() const {
543 SymbolFileDWARF *symbol_file = m_cu->GetSymbolFileDWARF();
545 if (m_form == DW_FORM_string) {
546 return m_value.value.cstr;
547 } else if (m_form == DW_FORM_strp) {
551 return symbol_file->get_debug_str_data().PeekCStr(m_value.value.uval);
552 } else if (m_form == DW_FORM_GNU_str_index) {
556 uint32_t index_size = m_cu->IsDWARF64() ? 8 : 4;
557 lldb::offset_t offset = m_value.value.uval * index_size;
558 dw_offset_t str_offset =
559 symbol_file->get_debug_str_offsets_data().GetMaxU64(&offset,
561 return symbol_file->get_debug_str_data().PeekCStr(str_offset);
564 if (m_form == DW_FORM_strx || m_form == DW_FORM_strx1 ||
565 m_form == DW_FORM_strx2 || m_form == DW_FORM_strx3 ||
566 m_form == DW_FORM_strx4) {
568 // The same code as above.
572 uint32_t indexSize = m_cu->IsDWARF64() ? 8 : 4;
573 lldb::offset_t offset =
574 m_cu->GetStrOffsetsBase() + m_value.value.uval * indexSize;
575 dw_offset_t strOffset =
576 symbol_file->get_debug_str_offsets_data().GetMaxU64(&offset, indexSize);
577 return symbol_file->get_debug_str_data().PeekCStr(strOffset);
580 if (m_form == DW_FORM_line_strp)
581 return symbol_file->get_debug_line_str_data().PeekCStr(m_value.value.uval);
586 dw_addr_t DWARFFormValue::Address() const {
587 SymbolFileDWARF *symbol_file = m_cu->GetSymbolFileDWARF();
589 if (m_form == DW_FORM_addr)
593 assert(m_form == DW_FORM_GNU_addr_index || m_form == DW_FORM_addrx ||
594 m_form == DW_FORM_addrx1 || m_form == DW_FORM_addrx2 ||
595 m_form == DW_FORM_addrx3 || m_form == DW_FORM_addrx4);
600 uint32_t index_size = m_cu->GetAddressByteSize();
601 dw_offset_t addr_base = m_cu->GetAddrBase();
602 lldb::offset_t offset = addr_base + m_value.value.uval * index_size;
603 return symbol_file->get_debug_addr_data().GetMaxU64(&offset, index_size);
606 uint64_t DWARFFormValue::Reference() const {
607 uint64_t value = m_value.value.uval;
613 case DW_FORM_ref_udata:
614 assert(m_cu); // CU must be valid for DW_FORM_ref forms that are compile
615 // unit relative or we will get this wrong
616 return value + m_cu->GetOffset();
618 case DW_FORM_ref_addr:
619 case DW_FORM_ref_sig8:
620 case DW_FORM_GNU_ref_alt:
624 return DW_INVALID_OFFSET;
628 uint64_t DWARFFormValue::Reference(dw_offset_t base_offset) const {
629 uint64_t value = m_value.value.uval;
635 case DW_FORM_ref_udata:
636 return value + base_offset;
638 case DW_FORM_ref_addr:
639 case DW_FORM_ref_sig8:
640 case DW_FORM_GNU_ref_alt:
644 return DW_INVALID_OFFSET;
648 const uint8_t *DWARFFormValue::BlockData() const { return m_value.data; }
650 bool DWARFFormValue::IsBlockForm(const dw_form_t form) {
652 case DW_FORM_exprloc:
662 bool DWARFFormValue::IsDataForm(const dw_form_t form) {
675 int DWARFFormValue::Compare(const DWARFFormValue &a_value,
676 const DWARFFormValue &b_value) {
677 dw_form_t a_form = a_value.Form();
678 dw_form_t b_form = b_value.Form();
691 case DW_FORM_ref_addr:
692 case DW_FORM_sec_offset:
693 case DW_FORM_flag_present:
694 case DW_FORM_ref_sig8:
695 case DW_FORM_GNU_addr_index: {
696 uint64_t a = a_value.Unsigned();
697 uint64_t b = b_value.Unsigned();
705 case DW_FORM_sdata: {
706 int64_t a = a_value.Signed();
707 int64_t b = b_value.Signed();
717 case DW_FORM_GNU_str_index: {
718 const char *a_string = a_value.AsCString();
719 const char *b_string = b_value.AsCString();
720 if (a_string == b_string)
722 else if (a_string && b_string)
723 return strcmp(a_string, b_string);
724 else if (a_string == NULL)
725 return -1; // A string is NULL, and B is valid
727 return 1; // A string valid, and B is NULL
734 case DW_FORM_exprloc: {
735 uint64_t a_len = a_value.Unsigned();
736 uint64_t b_len = b_value.Unsigned();
741 // The block lengths are the same
742 return memcmp(a_value.BlockData(), b_value.BlockData(), a_value.Unsigned());
749 case DW_FORM_ref_udata: {
750 uint64_t a = a_value.Reference();
751 uint64_t b = b_value.Reference();
759 case DW_FORM_indirect:
761 "This shouldn't happen after the form has been extracted...");
764 llvm_unreachable("Unhandled DW_FORM");
769 bool DWARFFormValue::FormIsSupported(dw_form_t form) {
773 case DW_FORM_rnglistx:
792 case DW_FORM_ref_addr:
797 case DW_FORM_ref_udata:
798 case DW_FORM_indirect:
799 case DW_FORM_sec_offset:
800 case DW_FORM_exprloc:
801 case DW_FORM_flag_present:
802 case DW_FORM_ref_sig8:
803 case DW_FORM_GNU_str_index:
804 case DW_FORM_GNU_addr_index:
805 case DW_FORM_implicit_const: