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
100 DWARFFormValue::GetFixedFormSizesForAddressSize (uint8_t addr_size)
104 case 4: return g_form_sizes_addr4;
105 case 8: return g_form_sizes_addr8;
110 DWARFFormValue::DWARFFormValue(dw_form_t form) :
117 DWARFFormValue::ExtractValue(const DWARFDataExtractor& data, lldb::offset_t* offset_ptr, const DWARFCompileUnit* cu)
119 bool indirect = false;
120 bool is_block = false;
122 // Read the value for the form into value and follow and DW_FORM_indirect instances we run into
128 case DW_FORM_addr: m_value.value.uval = data.GetMaxU64(offset_ptr, DWARFCompileUnit::GetAddressByteSize(cu)); break;
129 case DW_FORM_block2: m_value.value.uval = data.GetU16(offset_ptr); is_block = true; break;
130 case DW_FORM_block4: m_value.value.uval = data.GetU32(offset_ptr); is_block = true; break;
131 case DW_FORM_data2: m_value.value.uval = data.GetU16(offset_ptr); break;
132 case DW_FORM_data4: m_value.value.uval = data.GetU32(offset_ptr); break;
133 case DW_FORM_data8: m_value.value.uval = data.GetU64(offset_ptr); break;
134 case DW_FORM_string: m_value.value.cstr = data.GetCStr(offset_ptr);
135 // Set the string value to also be the data for inlined cstr form values only
136 // so we can tell the differnence between DW_FORM_string and DW_FORM_strp form
138 m_value.data = (uint8_t*)m_value.value.cstr; break;
139 case DW_FORM_exprloc:
140 case DW_FORM_block: m_value.value.uval = data.GetULEB128(offset_ptr); is_block = true; break;
141 case DW_FORM_block1: m_value.value.uval = data.GetU8(offset_ptr); is_block = true; break;
142 case DW_FORM_data1: m_value.value.uval = data.GetU8(offset_ptr); break;
143 case DW_FORM_flag: m_value.value.uval = data.GetU8(offset_ptr); break;
144 case DW_FORM_sdata: m_value.value.sval = data.GetSLEB128(offset_ptr); break;
145 case DW_FORM_strp: m_value.value.uval = data.GetU32(offset_ptr); break;
146 // case DW_FORM_APPLE_db_str:
147 case DW_FORM_udata: m_value.value.uval = data.GetULEB128(offset_ptr); break;
148 case DW_FORM_ref_addr:
149 if (cu->GetVersion() <= 2)
150 m_value.value.uval = data.GetMaxU64(offset_ptr, DWARFCompileUnit::GetAddressByteSize(cu));
152 m_value.value.uval = data.GetU32(offset_ptr); // 4 for DWARF32, 8 for DWARF64, but we don't support DWARF64 yet
154 case DW_FORM_ref1: m_value.value.uval = data.GetU8(offset_ptr); break;
155 case DW_FORM_ref2: m_value.value.uval = data.GetU16(offset_ptr); break;
156 case DW_FORM_ref4: m_value.value.uval = data.GetU32(offset_ptr); break;
157 case DW_FORM_ref8: m_value.value.uval = data.GetU64(offset_ptr); break;
158 case DW_FORM_ref_udata: m_value.value.uval = data.GetULEB128(offset_ptr); break;
159 case DW_FORM_indirect:
160 m_form = data.GetULEB128(offset_ptr);
164 case DW_FORM_sec_offset: m_value.value.uval = data.GetU32(offset_ptr); break;
165 case DW_FORM_flag_present: m_value.value.uval = 1; break;
166 case DW_FORM_ref_sig8: m_value.value.uval = data.GetU64(offset_ptr); break;
175 m_value.data = data.PeekData(*offset_ptr, m_value.value.uval);
176 if (m_value.data != NULL)
178 *offset_ptr += m_value.value.uval;
186 DWARFFormValue::SkipValue(const DWARFDataExtractor& debug_info_data, lldb::offset_t *offset_ptr, const DWARFCompileUnit* cu) const
188 return DWARFFormValue::SkipValue(m_form, debug_info_data, offset_ptr, cu);
192 DWARFFormValue::SkipValue(dw_form_t form, const DWARFDataExtractor& debug_info_data, lldb::offset_t *offset_ptr, const DWARFCompileUnit* cu)
196 // Blocks if inlined data that have a length field and the data bytes
197 // inlined in the .debug_info
198 case DW_FORM_exprloc:
199 case DW_FORM_block: { dw_uleb128_t size = debug_info_data.GetULEB128(offset_ptr); *offset_ptr += size; } return true;
200 case DW_FORM_block1: { dw_uleb128_t size = debug_info_data.GetU8(offset_ptr); *offset_ptr += size; } return true;
201 case DW_FORM_block2: { dw_uleb128_t size = debug_info_data.GetU16(offset_ptr); *offset_ptr += size; } return true;
202 case DW_FORM_block4: { dw_uleb128_t size = debug_info_data.GetU32(offset_ptr); *offset_ptr += size; } return true;
204 // Inlined NULL terminated C-strings
206 debug_info_data.GetCStr(offset_ptr);
209 // Compile unit address sized values
211 *offset_ptr += DWARFCompileUnit::GetAddressByteSize(cu);
214 case DW_FORM_ref_addr:
215 if (cu->GetVersion() <= 2)
216 *offset_ptr += DWARFCompileUnit::GetAddressByteSize(cu);
218 *offset_ptr += 4;// 4 for DWARF32, 8 for DWARF64, but we don't support DWARF64 yet
221 // 0 bytes values (implied from DW_FORM)
222 case DW_FORM_flag_present:
238 // 32 bit for DWARF 32, 64 for DWARF 64
239 case DW_FORM_sec_offset:
253 case DW_FORM_ref_sig8:
257 // signed or unsigned LEB 128 values
260 case DW_FORM_ref_udata:
261 debug_info_data.Skip_LEB128(offset_ptr);
264 case DW_FORM_indirect:
266 dw_form_t indirect_form = debug_info_data.GetULEB128(offset_ptr);
267 return DWARFFormValue::SkipValue (indirect_form,
281 DWARFFormValue::Dump(Stream &s, const DWARFDataExtractor* debug_str_data, const DWARFCompileUnit* cu) const
283 uint64_t uvalue = Unsigned();
284 bool cu_relative_offset = false;
286 bool verbose = s.GetVerbose();
290 case DW_FORM_addr: s.Address(uvalue, sizeof (uint64_t)); break;
292 case DW_FORM_data1: s.PutHex8(uvalue); break;
293 case DW_FORM_data2: s.PutHex16(uvalue); break;
294 case DW_FORM_sec_offset:
295 case DW_FORM_data4: s.PutHex32(uvalue); break;
296 case DW_FORM_ref_sig8:
297 case DW_FORM_data8: s.PutHex64(uvalue); break;
298 case DW_FORM_string: s.QuotedCString(AsCString(NULL)); break;
299 case DW_FORM_exprloc:
308 case DW_FORM_exprloc:
309 case DW_FORM_block: s.Printf("<0x%" PRIx64 "> ", uvalue); break;
310 case DW_FORM_block1: s.Printf("<0x%2.2x> ", (uint8_t)uvalue); break;
311 case DW_FORM_block2: s.Printf("<0x%4.4x> ", (uint16_t)uvalue); break;
312 case DW_FORM_block4: s.Printf("<0x%8.8x> ", (uint32_t)uvalue); break;
316 const uint8_t* data_ptr = m_value.data;
319 const uint8_t* end_data_ptr = data_ptr + uvalue; // uvalue contains size of block
320 while (data_ptr < end_data_ptr)
322 s.Printf("%2.2x ", *data_ptr);
327 s.PutCString("NULL");
331 case DW_FORM_sdata: s.PutSLEB128(uvalue); break;
332 case DW_FORM_udata: s.PutULEB128(uvalue); break;
337 s.Printf(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
339 const char* dbg_str = AsCString(debug_str_data);
341 s.QuotedCString(dbg_str);
349 case DW_FORM_ref_addr:
351 if (cu->GetVersion() <= 2)
352 s.Address(uvalue, sizeof (uint64_t) * 2);
354 s.Address(uvalue, 4 * 2);// 4 for DWARF32, 8 for DWARF64, but we don't support DWARF64 yet
357 case DW_FORM_ref1: cu_relative_offset = true; if (verbose) s.Printf("cu + 0x%2.2x", (uint8_t)uvalue); break;
358 case DW_FORM_ref2: cu_relative_offset = true; if (verbose) s.Printf("cu + 0x%4.4x", (uint16_t)uvalue); break;
359 case DW_FORM_ref4: cu_relative_offset = true; if (verbose) s.Printf("cu + 0x%4.4x", (uint32_t)uvalue); break;
360 case DW_FORM_ref8: cu_relative_offset = true; if (verbose) s.Printf("cu + 0x%8.8" PRIx64, uvalue); break;
361 case DW_FORM_ref_udata: cu_relative_offset = true; if (verbose) s.Printf("cu + 0x%" PRIx64, uvalue); break;
363 // All DW_FORM_indirect attributes should be resolved prior to calling this function
364 case DW_FORM_indirect: s.PutCString("DW_FORM_indirect"); break;
365 case DW_FORM_flag_present: break;
367 s.Printf("DW_FORM(0x%4.4x)", m_form);
371 if (cu_relative_offset)
374 s.PutCString(" => ");
376 s.Printf("{0x%8.8" PRIx64 "}", (uvalue + (cu ? cu->GetOffset() : 0)));
381 DWARFFormValue::AsCString(const DWARFDataExtractor* debug_str_data_ptr) const
384 return m_value.value.cstr;
385 else if (debug_str_data_ptr)
386 return debug_str_data_ptr->PeekCStr(m_value.value.uval);
391 DWARFFormValue::Reference(const DWARFCompileUnit* cu) const
393 uint64_t die_offset = m_value.value.uval;
400 case DW_FORM_ref_udata:
401 die_offset += (cu ? cu->GetOffset() : 0);
412 DWARFFormValue::Reference (dw_offset_t base_offset) const
414 uint64_t die_offset = m_value.value.uval;
421 case DW_FORM_ref_udata:
422 die_offset += base_offset;
432 //----------------------------------------------------------------------
433 // Resolve any compile unit specific references so that we don't need
434 // the compile unit at a later time in order to work with the form
436 //----------------------------------------------------------------------
438 DWARFFormValue::ResolveCompileUnitReferences(const DWARFCompileUnit* cu)
446 case DW_FORM_ref_udata:
447 m_value.value.uval += cu->GetOffset();
448 m_form = DW_FORM_ref_addr;
460 DWARFFormValue::BlockData() const
462 if (!IsInlinedCStr())
469 DWARFFormValue::IsBlockForm(const dw_form_t form)
483 DWARFFormValue::IsDataForm(const dw_form_t form)
499 DWARFFormValue::Compare (const DWARFFormValue& a_value, const DWARFFormValue& b_value, const DWARFCompileUnit* a_cu, const DWARFCompileUnit* b_cu, const DWARFDataExtractor* debug_str_data_ptr)
501 dw_form_t a_form = a_value.Form();
502 dw_form_t b_form = b_value.Form();
516 case DW_FORM_ref_addr:
517 case DW_FORM_sec_offset:
518 case DW_FORM_flag_present:
519 case DW_FORM_ref_sig8:
521 uint64_t a = a_value.Unsigned();
522 uint64_t b = b_value.Unsigned();
532 int64_t a = a_value.Signed();
533 int64_t b = b_value.Signed();
544 const char *a_string = a_value.AsCString(debug_str_data_ptr);
545 const char *b_string = b_value.AsCString(debug_str_data_ptr);
546 if (a_string == b_string)
548 else if (a_string && b_string)
549 return strcmp(a_string, b_string);
550 else if (a_string == NULL)
551 return -1; // A string is NULL, and B is valid
553 return 1; // A string valid, and B is NULL
561 case DW_FORM_exprloc:
563 uint64_t a_len = a_value.Unsigned();
564 uint64_t b_len = b_value.Unsigned();
569 // The block lengths are the same
570 return memcmp(a_value.BlockData(), b_value.BlockData(), a_value.Unsigned());
578 case DW_FORM_ref_udata:
580 uint64_t a = a_value.Reference(a_cu);
581 uint64_t b = b_value.Reference(b_cu);
589 case DW_FORM_indirect:
590 assert(!"This shouldn't happen after the form has been extracted...");
594 assert(!"Unhandled DW_FORM");