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/Support/Debug.h"
14 #include "llvm/Support/Dwarf.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/Support/raw_ostream.h"
19 using namespace dwarf;
22 template <uint8_t AddrSize, uint8_t RefAddrSize> struct FixedFormSizes {
23 static const uint8_t sizes[];
27 template <uint8_t AddrSize, uint8_t RefAddrSize>
28 const uint8_t FixedFormSizes<AddrSize, RefAddrSize>::sizes[] = {
30 AddrSize, // 0x01 DW_FORM_addr
32 0, // 0x03 DW_FORM_block2
33 0, // 0x04 DW_FORM_block4
34 2, // 0x05 DW_FORM_data2
35 4, // 0x06 DW_FORM_data4
36 8, // 0x07 DW_FORM_data8
37 0, // 0x08 DW_FORM_string
38 0, // 0x09 DW_FORM_block
39 0, // 0x0a DW_FORM_block1
40 1, // 0x0b DW_FORM_data1
41 1, // 0x0c DW_FORM_flag
42 0, // 0x0d DW_FORM_sdata
43 4, // 0x0e DW_FORM_strp
44 0, // 0x0f DW_FORM_udata
45 RefAddrSize, // 0x10 DW_FORM_ref_addr
46 1, // 0x11 DW_FORM_ref1
47 2, // 0x12 DW_FORM_ref2
48 4, // 0x13 DW_FORM_ref4
49 8, // 0x14 DW_FORM_ref8
50 0, // 0x15 DW_FORM_ref_udata
51 0, // 0x16 DW_FORM_indirect
52 4, // 0x17 DW_FORM_sec_offset
53 0, // 0x18 DW_FORM_exprloc
54 0, // 0x19 DW_FORM_flag_present
55 8, // 0x20 DW_FORM_ref_sig8
58 static uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
59 // FIXME: Support DWARF64.
60 return (Version == 2) ? AddrSize : 4;
64 DWARFFormValue::getFixedFormSizes(uint8_t AddrSize, uint16_t Version) {
65 uint8_t RefAddrSize = getRefAddrSize(AddrSize, Version);
66 if (AddrSize == 4 && RefAddrSize == 4)
67 return FixedFormSizes<4, 4>::sizes;
68 if (AddrSize == 4 && RefAddrSize == 8)
69 return FixedFormSizes<4, 8>::sizes;
70 if (AddrSize == 8 && RefAddrSize == 4)
71 return FixedFormSizes<8, 4>::sizes;
72 if (AddrSize == 8 && RefAddrSize == 8)
73 return FixedFormSizes<8, 8>::sizes;
78 DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
79 const DWARFCompileUnit *cu) {
80 bool indirect = false;
81 bool is_block = false;
83 // Read the value for the form into value and follow and DW_FORM_indirect
84 // instances we run into
89 case DW_FORM_ref_addr: {
91 (Form == DW_FORM_addr)
92 ? cu->getAddressByteSize()
93 : getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
94 RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
95 if (AI != cu->getRelocMap()->end()) {
96 const std::pair<uint8_t, int64_t> &R = AI->second;
97 Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
99 Value.uval = data.getUnsigned(offset_ptr, AddrSize);
102 case DW_FORM_exprloc:
104 Value.uval = data.getULEB128(offset_ptr);
108 Value.uval = data.getU8(offset_ptr);
112 Value.uval = data.getU16(offset_ptr);
116 Value.uval = data.getU32(offset_ptr);
122 Value.uval = data.getU8(offset_ptr);
126 Value.uval = data.getU16(offset_ptr);
130 Value.uval = data.getU32(offset_ptr);
134 Value.uval = data.getU64(offset_ptr);
137 Value.sval = data.getSLEB128(offset_ptr);
140 RelocAddrMap::const_iterator AI
141 = cu->getRelocMap()->find(*offset_ptr);
142 if (AI != cu->getRelocMap()->end()) {
143 const std::pair<uint8_t, int64_t> &R = AI->second;
144 Value.uval = data.getU32(offset_ptr) + R.second;
146 Value.uval = data.getU32(offset_ptr);
150 case DW_FORM_ref_udata:
151 Value.uval = data.getULEB128(offset_ptr);
154 Value.cstr = data.getCStr(offset_ptr);
155 // Set the string value to also be the data for inlined cstr form
156 // values only so we can tell the differnence between DW_FORM_string
157 // and DW_FORM_strp form values
158 Value.data = (const uint8_t*)Value.cstr;
160 case DW_FORM_indirect:
161 Form = data.getULEB128(offset_ptr);
164 case DW_FORM_sec_offset: {
165 // FIXME: This is 64-bit for DWARF64.
166 RelocAddrMap::const_iterator AI
167 = cu->getRelocMap()->find(*offset_ptr);
168 if (AI != cu->getRelocMap()->end()) {
169 const std::pair<uint8_t, int64_t> &R = AI->second;
170 Value.uval = data.getU32(offset_ptr) + R.second;
172 Value.uval = data.getU32(offset_ptr);
175 case DW_FORM_flag_present:
178 case DW_FORM_ref_sig8:
179 Value.uval = data.getU64(offset_ptr);
181 case DW_FORM_GNU_addr_index:
182 Value.uval = data.getULEB128(offset_ptr);
184 case DW_FORM_GNU_str_index:
185 Value.uval = data.getULEB128(offset_ptr);
193 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
196 Value.data = reinterpret_cast<const uint8_t *>(str.data());
197 *offset_ptr += Value.uval;
205 DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
206 const DWARFCompileUnit *cu) const {
207 return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
211 DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
212 uint32_t *offset_ptr, const DWARFCompileUnit *cu) {
213 bool indirect = false;
217 // Blocks if inlined data that have a length field and the data bytes
218 // inlined in the .debug_info
219 case DW_FORM_exprloc:
220 case DW_FORM_block: {
221 uint64_t size = debug_info_data.getULEB128(offset_ptr);
225 case DW_FORM_block1: {
226 uint8_t size = debug_info_data.getU8(offset_ptr);
230 case DW_FORM_block2: {
231 uint16_t size = debug_info_data.getU16(offset_ptr);
235 case DW_FORM_block4: {
236 uint32_t size = debug_info_data.getU32(offset_ptr);
241 // Inlined NULL terminated C-strings
243 debug_info_data.getCStr(offset_ptr);
246 // Compile unit address sized values
248 *offset_ptr += cu->getAddressByteSize();
250 case DW_FORM_ref_addr:
251 *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
254 // 0 byte values - implied from the form.
255 case DW_FORM_flag_present:
281 case DW_FORM_ref_sig8:
285 // signed or unsigned LEB 128 values
286 // case DW_FORM_APPLE_db_str:
289 case DW_FORM_ref_udata:
290 case DW_FORM_GNU_str_index:
291 case DW_FORM_GNU_addr_index:
292 debug_info_data.getULEB128(offset_ptr);
295 case DW_FORM_indirect:
297 form = debug_info_data.getULEB128(offset_ptr);
300 // FIXME: 4 for DWARF32, 8 for DWARF64.
301 case DW_FORM_sec_offset:
313 DWARFFormValue::dump(raw_ostream &OS, const DWARFCompileUnit *cu) const {
314 DataExtractor debug_str_data(cu->getStringSection(), true, 0);
315 DataExtractor debug_str_offset_data(cu->getStringOffsetSection(), true, 0);
316 uint64_t uvalue = getUnsigned();
317 bool cu_relative_offset = false;
320 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
321 case DW_FORM_GNU_addr_index: {
322 StringRef AddrOffsetSec = cu->getAddrOffsetSection();
323 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
324 if (AddrOffsetSec.size() != 0) {
325 DataExtractor DA(AddrOffsetSec, true, cu->getAddressByteSize());
326 OS << format("0x%016" PRIx64, getIndirectAddress(&DA, cu));
328 OS << "<no .debug_addr section>";
331 case DW_FORM_flag_present: OS << "true"; break;
333 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
334 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
335 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
336 case DW_FORM_ref_sig8:
337 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
340 OS.write_escaped(getAsCString(NULL));
343 case DW_FORM_exprloc:
350 case DW_FORM_exprloc:
351 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
352 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
353 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
354 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
358 const uint8_t* data_ptr = Value.data;
360 // uvalue contains size of block
361 const uint8_t* end_data_ptr = data_ptr + uvalue;
362 while (data_ptr < end_data_ptr) {
363 OS << format("%2.2x ", *data_ptr);
372 case DW_FORM_sdata: OS << getSigned(); break;
373 case DW_FORM_udata: OS << getUnsigned(); break;
375 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
376 const char* dbg_str = getAsCString(&debug_str_data);
379 OS.write_escaped(dbg_str);
384 case DW_FORM_GNU_str_index: {
385 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
386 const char *dbg_str = getIndirectCString(&debug_str_data,
387 &debug_str_offset_data);
390 OS.write_escaped(dbg_str);
395 case DW_FORM_ref_addr:
396 OS << format("0x%016" PRIx64, uvalue);
399 cu_relative_offset = true;
400 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
403 cu_relative_offset = true;
404 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
407 cu_relative_offset = true;
408 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
411 cu_relative_offset = true;
412 OS << format("cu + 0x%8.8" PRIx64, uvalue);
414 case DW_FORM_ref_udata:
415 cu_relative_offset = true;
416 OS << format("cu + 0x%" PRIx64, uvalue);
419 // All DW_FORM_indirect attributes should be resolved prior to calling
421 case DW_FORM_indirect:
422 OS << "DW_FORM_indirect";
425 // Should be formatted to 64-bit for DWARF64.
426 case DW_FORM_sec_offset:
427 OS << format("0x%08x", (uint32_t)uvalue);
431 OS << format("DW_FORM(0x%4.4x)", Form);
435 if (cu_relative_offset)
436 OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
440 DWARFFormValue::getAsCString(const DataExtractor *debug_str_data_ptr) const {
441 if (isInlinedCStr()) {
443 } else if (debug_str_data_ptr) {
444 uint32_t offset = Value.uval;
445 return debug_str_data_ptr->getCStr(&offset);
451 DWARFFormValue::getIndirectCString(const DataExtractor *DS,
452 const DataExtractor *DSO) const {
453 if (!DS || !DSO) return NULL;
455 uint32_t offset = Value.uval * 4;
456 uint32_t soffset = DSO->getU32(&offset);
457 return DS->getCStr(&soffset);
461 DWARFFormValue::getIndirectAddress(const DataExtractor *DA,
462 const DWARFCompileUnit *cu) const {
465 uint32_t offset = Value.uval * cu->getAddressByteSize();
466 return DA->getAddress(&offset);
469 uint64_t DWARFFormValue::getReference(const DWARFCompileUnit *cu) const {
470 uint64_t die_offset = Value.uval;
476 case DW_FORM_ref_udata:
477 die_offset += (cu ? cu->getOffset() : 0);
487 DWARFFormValue::resolveCompileUnitReferences(const DWARFCompileUnit *cu) {
493 case DW_FORM_ref_udata:
494 Value.uval += cu->getOffset();
495 Form = DW_FORM_ref_addr;
503 const uint8_t *DWARFFormValue::BlockData() const {
504 if (!isInlinedCStr())
509 bool DWARFFormValue::isBlockForm(uint16_t form) {
511 case DW_FORM_exprloc:
521 bool DWARFFormValue::isDataForm(uint16_t form) {