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/DWARF/DWARFFormValue.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/BinaryFormat/Dwarf.h"
16 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
17 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
18 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/WithColor.h"
22 #include "llvm/Support/raw_ostream.h"
28 using namespace dwarf;
30 static const DWARFFormValue::FormClass DWARF5FormClasses[] = {
31 DWARFFormValue::FC_Unknown, // 0x0
32 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
33 DWARFFormValue::FC_Unknown, // 0x02 unused
34 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
35 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
36 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
37 // --- These can be FC_SectionOffset in DWARF3 and below:
38 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
39 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
41 DWARFFormValue::FC_String, // 0x08 DW_FORM_string
42 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
43 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
44 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
45 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
46 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
47 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
48 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
49 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
50 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
51 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
52 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
53 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
54 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
55 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
56 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
57 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
58 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
59 DWARFFormValue::FC_String, // 0x1a DW_FORM_strx
60 DWARFFormValue::FC_Address, // 0x1b DW_FORM_addrx
61 DWARFFormValue::FC_Reference, // 0x1c DW_FORM_ref_sup4
62 DWARFFormValue::FC_String, // 0x1d DW_FORM_strp_sup
63 DWARFFormValue::FC_Constant, // 0x1e DW_FORM_data16
64 DWARFFormValue::FC_String, // 0x1f DW_FORM_line_strp
65 DWARFFormValue::FC_Reference, // 0x20 DW_FORM_ref_sig8
66 DWARFFormValue::FC_Constant, // 0x21 DW_FORM_implicit_const
67 DWARFFormValue::FC_SectionOffset, // 0x22 DW_FORM_loclistx
68 DWARFFormValue::FC_SectionOffset, // 0x23 DW_FORM_rnglistx
69 DWARFFormValue::FC_Reference, // 0x24 DW_FORM_ref_sup8
70 DWARFFormValue::FC_String, // 0x25 DW_FORM_strx1
71 DWARFFormValue::FC_String, // 0x26 DW_FORM_strx2
72 DWARFFormValue::FC_String, // 0x27 DW_FORM_strx3
73 DWARFFormValue::FC_String, // 0x28 DW_FORM_strx4
74 DWARFFormValue::FC_Address, // 0x29 DW_FORM_addrx1
75 DWARFFormValue::FC_Address, // 0x2a DW_FORM_addrx2
76 DWARFFormValue::FC_Address, // 0x2b DW_FORM_addrx3
77 DWARFFormValue::FC_Address, // 0x2c DW_FORM_addrx4
81 bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
83 const dwarf::FormParams Params) {
84 bool Indirect = false;
87 // Blocks of inlined data that have a length field and the data bytes
88 // inlined in the .debug_info.
91 uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
95 case DW_FORM_block1: {
96 uint8_t size = DebugInfoData.getU8(OffsetPtr);
100 case DW_FORM_block2: {
101 uint16_t size = DebugInfoData.getU16(OffsetPtr);
105 case DW_FORM_block4: {
106 uint32_t size = DebugInfoData.getU32(OffsetPtr);
111 // Inlined NULL terminated C-strings.
113 DebugInfoData.getCStr(OffsetPtr);
117 case DW_FORM_ref_addr:
118 case DW_FORM_flag_present:
129 case DW_FORM_ref_sig8:
130 case DW_FORM_ref_sup4:
131 case DW_FORM_ref_sup8:
138 case DW_FORM_sec_offset:
140 case DW_FORM_strp_sup:
141 case DW_FORM_line_strp:
142 case DW_FORM_GNU_ref_alt:
143 case DW_FORM_GNU_strp_alt:
144 if (Optional<uint8_t> FixedSize =
145 dwarf::getFixedFormByteSize(Form, Params)) {
146 *OffsetPtr += *FixedSize;
151 // signed or unsigned LEB 128 values.
153 DebugInfoData.getSLEB128(OffsetPtr);
157 case DW_FORM_ref_udata:
160 case DW_FORM_loclistx:
161 case DW_FORM_rnglistx:
162 case DW_FORM_GNU_addr_index:
163 case DW_FORM_GNU_str_index:
164 DebugInfoData.getULEB128(OffsetPtr);
167 case DW_FORM_indirect:
169 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
179 bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
180 // First, check DWARF5 form classes.
181 if (Form < makeArrayRef(DWARF5FormClasses).size() &&
182 DWARF5FormClasses[Form] == FC)
184 // Check more forms from extensions and proposals.
186 case DW_FORM_GNU_ref_alt:
187 return (FC == FC_Reference);
188 case DW_FORM_GNU_addr_index:
189 return (FC == FC_Address);
190 case DW_FORM_GNU_str_index:
191 case DW_FORM_GNU_strp_alt:
192 return (FC == FC_String);
196 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
197 // Don't check for DWARF version here, as some producers may still do this
198 // by mistake. Also accept DW_FORM_[line_]strp since these are
199 // .debug_[line_]str section offsets.
200 return (Form == DW_FORM_data4 || Form == DW_FORM_data8 ||
201 Form == DW_FORM_strp || Form == DW_FORM_line_strp) &&
202 FC == FC_SectionOffset;
205 bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data,
206 uint32_t *OffsetPtr, dwarf::FormParams FP,
207 const DWARFContext *Ctx,
208 const DWARFUnit *CU) {
210 Ctx = &CU->getContext();
213 bool Indirect = false;
214 bool IsBlock = false;
215 Value.data = nullptr;
216 // Read the value for the form into value and follow and DW_FORM_indirect
217 // instances we run into
222 case DW_FORM_ref_addr: {
224 (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
225 Value.uval = Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex);
228 case DW_FORM_exprloc:
230 Value.uval = Data.getULEB128(OffsetPtr);
234 Value.uval = Data.getU8(OffsetPtr);
238 Value.uval = Data.getU16(OffsetPtr);
242 Value.uval = Data.getU32(OffsetPtr);
250 Value.uval = Data.getU8(OffsetPtr);
256 Value.uval = Data.getU16(OffsetPtr);
259 Value.uval = Data.getU24(OffsetPtr);
263 case DW_FORM_ref_sup4:
266 Value.uval = Data.getRelocatedValue(4, OffsetPtr);
270 case DW_FORM_ref_sup8:
271 Value.uval = Data.getU64(OffsetPtr);
274 // Treat this like a 16-byte block.
279 Value.sval = Data.getSLEB128(OffsetPtr);
282 case DW_FORM_ref_udata:
283 case DW_FORM_rnglistx:
284 Value.uval = Data.getULEB128(OffsetPtr);
287 Value.cstr = Data.getCStr(OffsetPtr);
289 case DW_FORM_indirect:
290 Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr));
294 case DW_FORM_sec_offset:
295 case DW_FORM_GNU_ref_alt:
296 case DW_FORM_GNU_strp_alt:
297 case DW_FORM_line_strp:
298 case DW_FORM_strp_sup: {
300 Data.getRelocatedValue(FP.getDwarfOffsetByteSize(), OffsetPtr);
303 case DW_FORM_flag_present:
306 case DW_FORM_ref_sig8:
307 Value.uval = Data.getU64(OffsetPtr);
309 case DW_FORM_GNU_addr_index:
310 case DW_FORM_GNU_str_index:
313 Value.uval = Data.getULEB128(OffsetPtr);
316 // DWARFFormValue::skipValue() will have caught this and caused all
317 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
318 llvm_unreachable("unsupported form");
323 StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval);
324 Value.data = nullptr;
326 Value.data = reinterpret_cast<const uint8_t *>(Str.data());
327 *OffsetPtr += Value.uval;
334 void DWARFFormValue::dumpSectionedAddress(raw_ostream &OS,
335 DIDumpOptions DumpOpts,
336 SectionedAddress SA) const {
337 OS << format("0x%016" PRIx64, SA.Address);
338 dumpAddressSection(U->getContext().getDWARFObj(), OS, DumpOpts,
342 void DWARFFormValue::dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS,
343 DIDumpOptions DumpOpts,
344 uint64_t SectionIndex) {
345 if (!DumpOpts.Verbose || SectionIndex == -1ULL)
347 ArrayRef<SectionName> SectionNames = Obj.getSectionNames();
348 const auto &SecRef = SectionNames[SectionIndex];
350 OS << " \"" << SecRef.Name << '\"';
352 // Print section index if name is not unique.
353 if (!SecRef.IsNameUnique)
354 OS << format(" [%" PRIu64 "]", SectionIndex);
357 void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
358 uint64_t UValue = Value.uval;
359 bool CURelativeOffset = false;
360 raw_ostream &AddrOS = DumpOpts.ShowAddresses
361 ? WithColor(OS, HighlightColor::Address).get()
365 dumpSectionedAddress(AddrOS, DumpOpts, {Value.uval, Value.SectionIndex});
372 case DW_FORM_GNU_addr_index: {
373 Optional<SectionedAddress> A = U->getAddrOffsetSectionItem(UValue);
374 if (!A || DumpOpts.Verbose)
375 AddrOS << format("indexed (%8.8x) address = ", (uint32_t)UValue);
377 OS << "<invalid dwarf unit>";
379 dumpSectionedAddress(AddrOS, DumpOpts, *A);
381 OS << "<no .debug_addr section>";
384 case DW_FORM_flag_present:
389 OS << format("0x%02x", (uint8_t)UValue);
392 OS << format("0x%04x", (uint16_t)UValue);
395 OS << format("0x%08x", (uint32_t)UValue);
397 case DW_FORM_ref_sig8:
398 AddrOS << format("0x%016" PRIx64, UValue);
401 OS << format("0x%016" PRIx64, UValue);
404 OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16);
408 OS.write_escaped(Value.cstr);
411 case DW_FORM_exprloc:
418 case DW_FORM_exprloc:
420 AddrOS << format("<0x%" PRIx64 "> ", UValue);
423 AddrOS << format("<0x%2.2x> ", (uint8_t)UValue);
426 AddrOS << format("<0x%4.4x> ", (uint16_t)UValue);
429 AddrOS << format("<0x%8.8x> ", (uint32_t)UValue);
435 const uint8_t *DataPtr = Value.data;
437 // UValue contains size of block
438 const uint8_t *EndDataPtr = DataPtr + UValue;
439 while (DataPtr < EndDataPtr) {
440 AddrOS << format("%2.2x ", *DataPtr);
455 if (DumpOpts.Verbose)
456 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)UValue);
459 case DW_FORM_line_strp:
460 if (DumpOpts.Verbose)
461 OS << format(" .debug_line_str[0x%8.8x] = ", (uint32_t)UValue);
469 case DW_FORM_GNU_str_index:
470 if (DumpOpts.Verbose)
471 OS << format("indexed (%8.8x) string = ", (uint32_t)UValue);
474 case DW_FORM_GNU_strp_alt:
475 if (DumpOpts.Verbose)
476 OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
479 case DW_FORM_ref_addr:
480 AddrOS << format("0x%016" PRIx64, UValue);
483 CURelativeOffset = true;
484 if (DumpOpts.Verbose)
485 AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue);
488 CURelativeOffset = true;
489 if (DumpOpts.Verbose)
490 AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue);
493 CURelativeOffset = true;
494 if (DumpOpts.Verbose)
495 AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue);
498 CURelativeOffset = true;
499 if (DumpOpts.Verbose)
500 AddrOS << format("cu + 0x%8.8" PRIx64, UValue);
502 case DW_FORM_ref_udata:
503 CURelativeOffset = true;
504 if (DumpOpts.Verbose)
505 AddrOS << format("cu + 0x%" PRIx64, UValue);
507 case DW_FORM_GNU_ref_alt:
508 AddrOS << format("<alt 0x%" PRIx64 ">", UValue);
511 // All DW_FORM_indirect attributes should be resolved prior to calling
513 case DW_FORM_indirect:
514 OS << "DW_FORM_indirect";
517 case DW_FORM_rnglistx:
518 OS << format("indexed (0x%x) rangelist = ", (uint32_t)UValue);
521 // Should be formatted to 64-bit for DWARF64.
522 case DW_FORM_sec_offset:
523 AddrOS << format("0x%08x", (uint32_t)UValue);
527 OS << format("DW_FORM(0x%4.4x)", Form);
531 if (CURelativeOffset) {
532 if (DumpOpts.Verbose)
534 if (DumpOpts.ShowAddresses)
535 WithColor(OS, HighlightColor::Address).get()
536 << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
537 if (DumpOpts.Verbose)
542 void DWARFFormValue::dumpString(raw_ostream &OS) const {
543 Optional<const char *> DbgStr = getAsCString();
544 if (DbgStr.hasValue()) {
545 auto COS = WithColor(OS, HighlightColor::String);
547 COS.get().write_escaped(DbgStr.getValue());
552 Optional<const char *> DWARFFormValue::getAsCString() const {
553 if (!isFormClass(FC_String))
555 if (Form == DW_FORM_string)
557 // FIXME: Add support for DW_FORM_GNU_strp_alt
558 if (Form == DW_FORM_GNU_strp_alt || C == nullptr)
560 uint32_t Offset = Value.uval;
561 if (Form == DW_FORM_line_strp) {
562 // .debug_line_str is tracked in the Context.
563 if (const char *Str = C->getLineStringExtractor().getCStr(&Offset))
567 if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
568 Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
569 Form == DW_FORM_strx4) {
572 Optional<uint64_t> StrOffset = U->getStringOffsetSectionItem(Offset);
577 // Prefer the Unit's string extractor, because for .dwo it will point to
578 // .debug_str.dwo, while the Context's extractor always uses .debug_str.
580 if (const char *Str = U->getStringExtractor().getCStr(&Offset))
584 if (const char *Str = C->getStringExtractor().getCStr(&Offset))
589 Optional<uint64_t> DWARFFormValue::getAsAddress() const {
590 if (auto SA = getAsSectionedAddress())
594 Optional<SectionedAddress> DWARFFormValue::getAsSectionedAddress() const {
595 if (!isFormClass(FC_Address))
597 if (Form == DW_FORM_GNU_addr_index || Form == DW_FORM_addrx) {
598 uint32_t Index = Value.uval;
601 Optional<SectionedAddress> SA = U->getAddrOffsetSectionItem(Index);
606 return {{Value.uval, Value.SectionIndex}};
609 Optional<uint64_t> DWARFFormValue::getAsReference() const {
610 if (!isFormClass(FC_Reference))
617 case DW_FORM_ref_udata:
620 return Value.uval + U->getOffset();
621 case DW_FORM_ref_addr:
622 case DW_FORM_ref_sig8:
623 case DW_FORM_GNU_ref_alt:
630 Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
631 if (!isFormClass(FC_SectionOffset))
636 Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
637 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
638 Form == DW_FORM_sdata)
643 Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
644 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
645 (Form == DW_FORM_udata &&
646 uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
650 return int32_t(Value.uval);
652 return int16_t(Value.uval);
654 return int8_t(Value.uval);
662 Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
663 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) &&
664 Form != DW_FORM_data16)
666 return makeArrayRef(Value.data, Value.uval);
669 Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
670 if (!isFormClass(FC_String) && Form == DW_FORM_string)
675 Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
676 if (!isFormClass(FC_Reference))