| 1 | //===- DWARFListTable.cpp ---------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/DebugInfo/DWARF/DWARFListTable.h" |
| 10 | #include "llvm/BinaryFormat/Dwarf.h" |
| 11 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 12 | #include "llvm/Support/Errc.h" |
| 13 | #include "llvm/Support/Error.h" |
| 14 | #include "llvm/Support/Format.h" |
| 15 | #include "llvm/Support/FormatAdapters.h" |
| 16 | #include "llvm/Support/FormatVariadic.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | Error DWARFListTableHeader::(DWARFDataExtractor Data, |
| 22 | uint64_t *OffsetPtr) { |
| 23 | HeaderOffset = *OffsetPtr; |
| 24 | Error Err = Error::success(); |
| 25 | |
| 26 | std::tie(args&: HeaderData.Length, args&: Format) = Data.getInitialLength(Off: OffsetPtr, Err: &Err); |
| 27 | if (Err) |
| 28 | return createStringError( |
| 29 | EC: errc::invalid_argument, Fmt: "parsing %s table at offset 0x%" PRIx64 ": %s" , |
| 30 | Vals: SectionName.data(), Vals: HeaderOffset, Vals: toString(E: std::move(Err)).c_str()); |
| 31 | |
| 32 | uint8_t OffsetByteSize = Format == dwarf::DWARF64 ? 8 : 4; |
| 33 | uint64_t FullLength = |
| 34 | HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format); |
| 35 | if (FullLength < getHeaderSize(Format)) |
| 36 | return createStringError(EC: errc::invalid_argument, |
| 37 | Fmt: "%s table at offset 0x%" PRIx64 |
| 38 | " has too small length (0x%" PRIx64 |
| 39 | ") to contain a complete header" , |
| 40 | Vals: SectionName.data(), Vals: HeaderOffset, Vals: FullLength); |
| 41 | assert(FullLength == length() && "Inconsistent calculation of length." ); |
| 42 | uint64_t End = HeaderOffset + FullLength; |
| 43 | if (!Data.isValidOffsetForDataOfSize(offset: HeaderOffset, length: FullLength)) |
| 44 | return createStringError(EC: errc::invalid_argument, |
| 45 | Fmt: "section is not large enough to contain a %s table " |
| 46 | "of length 0x%" PRIx64 " at offset 0x%" PRIx64, |
| 47 | Vals: SectionName.data(), Vals: FullLength, Vals: HeaderOffset); |
| 48 | |
| 49 | HeaderData.Version = Data.getU16(offset_ptr: OffsetPtr); |
| 50 | HeaderData.AddrSize = Data.getU8(offset_ptr: OffsetPtr); |
| 51 | HeaderData.SegSize = Data.getU8(offset_ptr: OffsetPtr); |
| 52 | HeaderData.OffsetEntryCount = Data.getU32(offset_ptr: OffsetPtr); |
| 53 | |
| 54 | // Perform basic validation of the remaining header fields. |
| 55 | if (HeaderData.Version < 5 || HeaderData.Version > 6) |
| 56 | return createStringError(EC: errc::invalid_argument, |
| 57 | Fmt: "unrecognised %s table version %" PRIu16 |
| 58 | " in table at offset 0x%" PRIx64, |
| 59 | Vals: SectionName.data(), Vals: HeaderData.Version, Vals: HeaderOffset); |
| 60 | if (Error SizeErr = DWARFContext::checkAddressSizeSupported( |
| 61 | AddressSize: HeaderData.AddrSize, EC: errc::not_supported, |
| 62 | Fmt: "%s table at offset 0x%" PRIx64, Vals: SectionName.data(), Vals: HeaderOffset)) |
| 63 | return SizeErr; |
| 64 | if (HeaderData.SegSize != 0) |
| 65 | return createStringError(EC: errc::not_supported, |
| 66 | Fmt: "%s table at offset 0x%" PRIx64 |
| 67 | " has unsupported segment selector size %" PRIu8, |
| 68 | Vals: SectionName.data(), Vals: HeaderOffset, Vals: HeaderData.SegSize); |
| 69 | if (End < HeaderOffset + getHeaderSize(Format) + |
| 70 | HeaderData.OffsetEntryCount * OffsetByteSize) |
| 71 | return createStringError(EC: errc::invalid_argument, |
| 72 | Fmt: "%s table at offset 0x%" PRIx64 " has more offset entries (%" PRIu32 |
| 73 | ") than there is space for" , |
| 74 | Vals: SectionName.data(), Vals: HeaderOffset, Vals: HeaderData.OffsetEntryCount); |
| 75 | Data.setAddressSize(HeaderData.AddrSize); |
| 76 | *OffsetPtr += HeaderData.OffsetEntryCount * OffsetByteSize; |
| 77 | return Error::success(); |
| 78 | } |
| 79 | |
| 80 | void DWARFListTableHeader::(DataExtractor Data, raw_ostream &OS, |
| 81 | DIDumpOptions DumpOpts) const { |
| 82 | if (DumpOpts.Verbose) |
| 83 | OS << formatv(Fmt: "{0:x8}: " , Vals: HeaderOffset); |
| 84 | int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format); |
| 85 | OS << formatv(Fmt: "{0} list header: length = 0x{1:x-}" , Vals: ListTypeString.data(), |
| 86 | Vals: fmt_align(Item: HeaderData.Length, Where: AlignStyle::Right, Amount: OffsetDumpWidth, |
| 87 | Fill: '0')) |
| 88 | << ", format = " << dwarf::FormatString(Format) |
| 89 | << formatv(Fmt: ", version = {0:x4}, addr_size = {1:x2}" |
| 90 | ", seg_size = {2:x2}" |
| 91 | ", offset_entry_count = {3:x8}\n" , |
| 92 | Vals: HeaderData.Version, Vals: HeaderData.AddrSize, Vals: HeaderData.SegSize, |
| 93 | Vals: HeaderData.OffsetEntryCount); |
| 94 | |
| 95 | if (HeaderData.OffsetEntryCount > 0) { |
| 96 | OS << "offsets: [" ; |
| 97 | for (uint32_t I = 0; I < HeaderData.OffsetEntryCount; ++I) { |
| 98 | auto Off = *getOffsetEntry(Data, Index: I); |
| 99 | OS << formatv(Fmt: "\n0x{0:x-}" , |
| 100 | Vals: fmt_align(Item&: Off, Where: AlignStyle::Right, Amount: OffsetDumpWidth, Fill: '0')); |
| 101 | if (DumpOpts.Verbose) |
| 102 | OS << formatv(Fmt: " => {0:x8}" , Vals: Off + HeaderOffset + getHeaderSize(Format)); |
| 103 | } |
| 104 | OS << "\n]\n" ; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | uint64_t DWARFListTableHeader::() const { |
| 109 | if (HeaderData.Length == 0) |
| 110 | return 0; |
| 111 | return HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format); |
| 112 | } |
| 113 | |