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 "SyntaxHighlighting.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/None.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
16 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
17 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
18 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
19 #include "llvm/Support/Dwarf.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/raw_ostream.h"
28 using namespace dwarf;
29 using namespace syntax;
31 static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
32 DWARFFormValue::FC_Unknown, // 0x0
33 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
34 DWARFFormValue::FC_Unknown, // 0x02 unused
35 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
36 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
37 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
38 // --- These can be FC_SectionOffset in DWARF3 and below:
39 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
40 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
42 DWARFFormValue::FC_String, // 0x08 DW_FORM_string
43 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
44 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
45 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
46 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
47 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
48 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
49 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
50 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
51 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
52 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
53 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
54 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
55 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
56 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
57 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
58 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
59 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
64 /// A helper class that can be used in DWARFFormValue.cpp functions that need
65 /// to know the byte size of DW_FORM values that vary in size depending on the
66 /// DWARF version, address byte size, or DWARF32 or DWARF64.
67 class FormSizeHelper {
70 llvm::dwarf::DwarfFormat Format;
73 FormSizeHelper(uint16_t V, uint8_t A, llvm::dwarf::DwarfFormat F)
74 : Version(V), AddrSize(A), Format(F) {}
76 uint8_t getAddressByteSize() const { return AddrSize; }
78 uint8_t getRefAddrByteSize() const {
81 return getDwarfOffsetByteSize();
84 uint8_t getDwarfOffsetByteSize() const {
86 case dwarf::DwarfFormat::DWARF32:
88 case dwarf::DwarfFormat::DWARF64:
91 llvm_unreachable("Invalid Format value");
95 } // end anonymous namespace
98 static Optional<uint8_t> getFixedByteSize(dwarf::Form Form, const T *U) {
102 return U->getAddressByteSize();
105 case DW_FORM_block: // ULEB128 length L followed by L bytes.
106 case DW_FORM_block1: // 1 byte length L followed by L bytes.
107 case DW_FORM_block2: // 2 byte length L followed by L bytes.
108 case DW_FORM_block4: // 4 byte length L followed by L bytes.
109 case DW_FORM_string: // C-string with null terminator.
110 case DW_FORM_sdata: // SLEB128.
111 case DW_FORM_udata: // ULEB128.
112 case DW_FORM_ref_udata: // ULEB128.
113 case DW_FORM_indirect: // ULEB128.
114 case DW_FORM_exprloc: // ULEB128 length L followed by L bytes.
115 case DW_FORM_strx: // ULEB128.
116 case DW_FORM_addrx: // ULEB128.
117 case DW_FORM_loclistx: // ULEB128.
118 case DW_FORM_rnglistx: // ULEB128.
119 case DW_FORM_GNU_addr_index: // ULEB128.
120 case DW_FORM_GNU_str_index: // ULEB128.
123 case DW_FORM_ref_addr:
125 return U->getRefAddrByteSize();
143 case DW_FORM_ref_sup4:
149 case DW_FORM_GNU_ref_alt:
150 case DW_FORM_GNU_strp_alt:
151 case DW_FORM_line_strp:
152 case DW_FORM_sec_offset:
153 case DW_FORM_strp_sup:
155 return U->getDwarfOffsetByteSize();
160 case DW_FORM_ref_sig8:
161 case DW_FORM_ref_sup8:
164 case DW_FORM_flag_present:
170 case DW_FORM_implicit_const:
171 // The implicit value is stored in the abbreviation as a SLEB128, and
172 // there no data in debug info.
176 llvm_unreachable("Handle this form in this switch statement");
182 static bool skipFormValue(dwarf::Form Form, const DataExtractor &DebugInfoData,
183 uint32_t *OffsetPtr, const T *U) {
184 bool Indirect = false;
187 // Blocks of inlined data that have a length field and the data bytes
188 // inlined in the .debug_info.
189 case DW_FORM_exprloc:
190 case DW_FORM_block: {
191 uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
195 case DW_FORM_block1: {
196 uint8_t size = DebugInfoData.getU8(OffsetPtr);
200 case DW_FORM_block2: {
201 uint16_t size = DebugInfoData.getU16(OffsetPtr);
205 case DW_FORM_block4: {
206 uint32_t size = DebugInfoData.getU32(OffsetPtr);
211 // Inlined NULL terminated C-strings.
213 DebugInfoData.getCStr(OffsetPtr);
217 case DW_FORM_ref_addr:
218 case DW_FORM_flag_present:
228 case DW_FORM_ref_sig8:
229 case DW_FORM_ref_sup4:
230 case DW_FORM_ref_sup8:
237 case DW_FORM_sec_offset:
239 case DW_FORM_strp_sup:
240 case DW_FORM_line_strp:
241 case DW_FORM_GNU_ref_alt:
242 case DW_FORM_GNU_strp_alt:
243 if (Optional<uint8_t> FixedSize = ::getFixedByteSize(Form, U)) {
244 *OffsetPtr += *FixedSize;
249 // signed or unsigned LEB 128 values.
251 DebugInfoData.getSLEB128(OffsetPtr);
255 case DW_FORM_ref_udata:
258 case DW_FORM_loclistx:
259 case DW_FORM_rnglistx:
260 case DW_FORM_GNU_addr_index:
261 case DW_FORM_GNU_str_index:
262 DebugInfoData.getULEB128(OffsetPtr);
265 case DW_FORM_indirect:
267 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
277 Optional<uint8_t> DWARFFormValue::getFixedByteSize(dwarf::Form Form,
278 const DWARFUnit *U) {
279 return ::getFixedByteSize(Form, U);
283 DWARFFormValue::getFixedByteSize(dwarf::Form Form, uint16_t Version,
285 llvm::dwarf::DwarfFormat Format) {
286 FormSizeHelper FSH(Version, AddrSize, Format);
287 return ::getFixedByteSize(Form, &FSH);
290 bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
291 // First, check DWARF4 form classes.
292 if (Form < makeArrayRef(DWARF4FormClasses).size() &&
293 DWARF4FormClasses[Form] == FC)
295 // Check more forms from DWARF4 and DWARF5 proposals.
297 case DW_FORM_ref_sig8:
298 case DW_FORM_GNU_ref_alt:
299 return (FC == FC_Reference);
300 case DW_FORM_GNU_addr_index:
301 return (FC == FC_Address);
302 case DW_FORM_GNU_str_index:
303 case DW_FORM_GNU_strp_alt:
304 return (FC == FC_String);
305 case DW_FORM_implicit_const:
306 return (FC == FC_Constant);
310 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
311 // Don't check for DWARF version here, as some producers may still do this
313 return (Form == DW_FORM_data4 || Form == DW_FORM_data8) &&
314 FC == FC_SectionOffset;
317 bool DWARFFormValue::extractValue(const DataExtractor &data,
318 uint32_t *offset_ptr,
319 const DWARFUnit *cu) {
321 bool indirect = false;
322 bool is_block = false;
323 Value.data = nullptr;
324 // Read the value for the form into value and follow and DW_FORM_indirect
325 // instances we run into
330 case DW_FORM_ref_addr: {
334 (Form == DW_FORM_addr)
335 ? U->getAddressByteSize()
336 : U->getRefAddrByteSize();
338 getRelocatedValue(data, AddrSize, offset_ptr, U->getRelocMap());
341 case DW_FORM_exprloc:
343 Value.uval = data.getULEB128(offset_ptr);
347 Value.uval = data.getU8(offset_ptr);
351 Value.uval = data.getU16(offset_ptr);
355 Value.uval = data.getU32(offset_ptr);
363 Value.uval = data.getU8(offset_ptr);
369 Value.uval = data.getU16(offset_ptr);
373 case DW_FORM_ref_sup4:
375 case DW_FORM_addrx4: {
376 const RelocAddrMap* RelocMap = U ? U->getRelocMap() : nullptr;
377 Value.uval = getRelocatedValue(data, 4, offset_ptr, RelocMap);
382 case DW_FORM_ref_sup8:
383 Value.uval = data.getU64(offset_ptr);
386 Value.sval = data.getSLEB128(offset_ptr);
389 case DW_FORM_ref_udata:
390 Value.uval = data.getULEB128(offset_ptr);
393 Value.cstr = data.getCStr(offset_ptr);
395 case DW_FORM_indirect:
396 Form = static_cast<dwarf::Form>(data.getULEB128(offset_ptr));
400 case DW_FORM_sec_offset:
401 case DW_FORM_GNU_ref_alt:
402 case DW_FORM_GNU_strp_alt:
403 case DW_FORM_line_strp:
404 case DW_FORM_strp_sup: {
407 Value.uval = getRelocatedValue(data, U->getDwarfOffsetByteSize(),
408 offset_ptr, U->getRelocMap());
411 case DW_FORM_flag_present:
414 case DW_FORM_ref_sig8:
415 Value.uval = data.getU64(offset_ptr);
417 case DW_FORM_GNU_addr_index:
418 case DW_FORM_GNU_str_index:
419 Value.uval = data.getULEB128(offset_ptr);
422 // DWARFFormValue::skipValue() will have caught this and caused all
423 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
424 llvm_unreachable("unsupported form");
429 StringRef str = data.getData().substr(*offset_ptr, Value.uval);
430 Value.data = nullptr;
432 Value.data = reinterpret_cast<const uint8_t *>(str.data());
433 *offset_ptr += Value.uval;
440 bool DWARFFormValue::skipValue(DataExtractor DebugInfoData,
441 uint32_t *offset_ptr, const DWARFUnit *U) const {
442 return DWARFFormValue::skipValue(Form, DebugInfoData, offset_ptr, U);
445 bool DWARFFormValue::skipValue(dwarf::Form form, DataExtractor DebugInfoData,
446 uint32_t *offset_ptr, const DWARFUnit *U) {
447 return skipFormValue(form, DebugInfoData, offset_ptr, U);
450 bool DWARFFormValue::skipValue(dwarf::Form form, DataExtractor DebugInfoData,
451 uint32_t *offset_ptr, uint16_t Version,
453 llvm::dwarf::DwarfFormat Format) {
454 FormSizeHelper FSH(Version, AddrSize, Format);
455 return skipFormValue(form, DebugInfoData, offset_ptr, &FSH);
459 DWARFFormValue::dump(raw_ostream &OS) const {
460 uint64_t uvalue = Value.uval;
461 bool cu_relative_offset = false;
464 case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
465 case DW_FORM_GNU_addr_index: {
466 OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
469 OS << "<invalid dwarf unit>";
470 else if (U->getAddrOffsetSectionItem(uvalue, Address))
471 OS << format("0x%016" PRIx64, Address);
473 OS << "<no .debug_addr section>";
476 case DW_FORM_flag_present: OS << "true"; break;
478 case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
479 case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
480 case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
481 case DW_FORM_ref_sig8:
482 case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
485 OS.write_escaped(Value.cstr);
488 case DW_FORM_exprloc:
495 case DW_FORM_exprloc:
496 case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
497 case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
498 case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
499 case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
503 const uint8_t* data_ptr = Value.data;
505 // uvalue contains size of block
506 const uint8_t* end_data_ptr = data_ptr + uvalue;
507 while (data_ptr < end_data_ptr) {
508 OS << format("%2.2x ", *data_ptr);
517 case DW_FORM_sdata: OS << Value.sval; break;
518 case DW_FORM_udata: OS << Value.uval; break;
520 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
523 case DW_FORM_GNU_str_index:
524 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
527 case DW_FORM_GNU_strp_alt:
528 OS << format("alt indirect string, offset: 0x%" PRIx64 "", uvalue);
531 case DW_FORM_ref_addr:
532 OS << format("0x%016" PRIx64, uvalue);
535 cu_relative_offset = true;
536 OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
539 cu_relative_offset = true;
540 OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
543 cu_relative_offset = true;
544 OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
547 cu_relative_offset = true;
548 OS << format("cu + 0x%8.8" PRIx64, uvalue);
550 case DW_FORM_ref_udata:
551 cu_relative_offset = true;
552 OS << format("cu + 0x%" PRIx64, uvalue);
554 case DW_FORM_GNU_ref_alt:
555 OS << format("<alt 0x%" PRIx64 ">", uvalue);
558 // All DW_FORM_indirect attributes should be resolved prior to calling
560 case DW_FORM_indirect:
561 OS << "DW_FORM_indirect";
564 // Should be formatted to 64-bit for DWARF64.
565 case DW_FORM_sec_offset:
566 OS << format("0x%08x", (uint32_t)uvalue);
570 OS << format("DW_FORM(0x%4.4x)", Form);
574 if (cu_relative_offset) {
576 WithColor(OS, syntax::Address).get()
577 << format("0x%8.8" PRIx64, uvalue + (U ? U->getOffset() : 0));
582 void DWARFFormValue::dumpString(raw_ostream &OS) const {
583 Optional<const char *> DbgStr = getAsCString();
584 if (DbgStr.hasValue()) {
585 raw_ostream &COS = WithColor(OS, syntax::String);
587 COS.write_escaped(DbgStr.getValue());
592 Optional<const char *> DWARFFormValue::getAsCString() const {
593 if (!isFormClass(FC_String))
595 if (Form == DW_FORM_string)
597 // FIXME: Add support for DW_FORM_GNU_strp_alt
598 if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
600 uint32_t Offset = Value.uval;
601 if (Form == DW_FORM_GNU_str_index) {
603 if (!U->getStringOffsetSectionItem(Offset, StrOffset))
607 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
613 Optional<uint64_t> DWARFFormValue::getAsAddress() const {
614 if (!isFormClass(FC_Address))
616 if (Form == DW_FORM_GNU_addr_index) {
617 uint32_t Index = Value.uval;
619 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
626 Optional<uint64_t> DWARFFormValue::getAsReference() const {
627 if (!isFormClass(FC_Reference))
634 case DW_FORM_ref_udata:
637 return Value.uval + U->getOffset();
638 case DW_FORM_ref_addr:
639 case DW_FORM_ref_sig8:
640 case DW_FORM_GNU_ref_alt:
647 Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
648 if (!isFormClass(FC_SectionOffset))
653 Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
654 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag))
655 || Form == DW_FORM_sdata)
660 Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
661 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
662 (Form == DW_FORM_udata && uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
666 return int32_t(Value.uval);
668 return int16_t(Value.uval);
670 return int8_t(Value.uval);
678 Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
679 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
681 return makeArrayRef(Value.data, Value.uval);
684 Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
685 if (!isFormClass(FC_String) && Form == DW_FORM_string)
690 Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
691 if (!isFormClass(FC_Reference))