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/raw_ostream.h" |
16 | |
17 | using namespace llvm; |
18 | |
19 | Error DWARFListTableHeader::(DWARFDataExtractor Data, |
20 | uint64_t *OffsetPtr) { |
21 | HeaderOffset = *OffsetPtr; |
22 | Error Err = Error::success(); |
23 | |
24 | std::tie(args&: HeaderData.Length, args&: Format) = Data.getInitialLength(Off: OffsetPtr, Err: &Err); |
25 | if (Err) |
26 | return createStringError( |
27 | EC: errc::invalid_argument, Fmt: "parsing %s table at offset 0x%" PRIx64 ": %s" , |
28 | Vals: SectionName.data(), Vals: HeaderOffset, Vals: toString(E: std::move(Err)).c_str()); |
29 | |
30 | uint8_t OffsetByteSize = Format == dwarf::DWARF64 ? 8 : 4; |
31 | uint64_t FullLength = |
32 | HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format); |
33 | if (FullLength < getHeaderSize(Format)) |
34 | return createStringError(EC: errc::invalid_argument, |
35 | Fmt: "%s table at offset 0x%" PRIx64 |
36 | " has too small length (0x%" PRIx64 |
37 | ") to contain a complete header" , |
38 | Vals: SectionName.data(), Vals: HeaderOffset, Vals: FullLength); |
39 | assert(FullLength == length() && "Inconsistent calculation of length." ); |
40 | uint64_t End = HeaderOffset + FullLength; |
41 | if (!Data.isValidOffsetForDataOfSize(offset: HeaderOffset, length: FullLength)) |
42 | return createStringError(EC: errc::invalid_argument, |
43 | Fmt: "section is not large enough to contain a %s table " |
44 | "of length 0x%" PRIx64 " at offset 0x%" PRIx64, |
45 | Vals: SectionName.data(), Vals: FullLength, Vals: HeaderOffset); |
46 | |
47 | HeaderData.Version = Data.getU16(offset_ptr: OffsetPtr); |
48 | HeaderData.AddrSize = Data.getU8(offset_ptr: OffsetPtr); |
49 | HeaderData.SegSize = Data.getU8(offset_ptr: OffsetPtr); |
50 | HeaderData.OffsetEntryCount = Data.getU32(offset_ptr: OffsetPtr); |
51 | |
52 | // Perform basic validation of the remaining header fields. |
53 | if (HeaderData.Version != 5) |
54 | return createStringError(EC: errc::invalid_argument, |
55 | Fmt: "unrecognised %s table version %" PRIu16 |
56 | " in table at offset 0x%" PRIx64, |
57 | Vals: SectionName.data(), Vals: HeaderData.Version, Vals: HeaderOffset); |
58 | if (Error SizeErr = DWARFContext::checkAddressSizeSupported( |
59 | AddressSize: HeaderData.AddrSize, EC: errc::not_supported, |
60 | Fmt: "%s table at offset 0x%" PRIx64, Vals: SectionName.data(), Vals: HeaderOffset)) |
61 | return SizeErr; |
62 | if (HeaderData.SegSize != 0) |
63 | return createStringError(EC: errc::not_supported, |
64 | Fmt: "%s table at offset 0x%" PRIx64 |
65 | " has unsupported segment selector size %" PRIu8, |
66 | Vals: SectionName.data(), Vals: HeaderOffset, Vals: HeaderData.SegSize); |
67 | if (End < HeaderOffset + getHeaderSize(Format) + |
68 | HeaderData.OffsetEntryCount * OffsetByteSize) |
69 | return createStringError(EC: errc::invalid_argument, |
70 | Fmt: "%s table at offset 0x%" PRIx64 " has more offset entries (%" PRIu32 |
71 | ") than there is space for" , |
72 | Vals: SectionName.data(), Vals: HeaderOffset, Vals: HeaderData.OffsetEntryCount); |
73 | Data.setAddressSize(HeaderData.AddrSize); |
74 | *OffsetPtr += HeaderData.OffsetEntryCount * OffsetByteSize; |
75 | return Error::success(); |
76 | } |
77 | |
78 | void DWARFListTableHeader::(DataExtractor Data, raw_ostream &OS, |
79 | DIDumpOptions DumpOpts) const { |
80 | if (DumpOpts.Verbose) |
81 | OS << format(Fmt: "0x%8.8" PRIx64 ": " , Vals: HeaderOffset); |
82 | int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format); |
83 | OS << format(Fmt: "%s list header: length = 0x%0*" PRIx64, Vals: ListTypeString.data(), |
84 | Vals: OffsetDumpWidth, Vals: HeaderData.Length) |
85 | << ", format = " << dwarf::FormatString(Format) |
86 | << format(Fmt: ", version = 0x%4.4" PRIx16 ", addr_size = 0x%2.2" PRIx8 |
87 | ", seg_size = 0x%2.2" PRIx8 |
88 | ", offset_entry_count = 0x%8.8" PRIx32 "\n" , |
89 | Vals: HeaderData.Version, Vals: HeaderData.AddrSize, Vals: HeaderData.SegSize, |
90 | Vals: HeaderData.OffsetEntryCount); |
91 | |
92 | if (HeaderData.OffsetEntryCount > 0) { |
93 | OS << "offsets: [" ; |
94 | for (uint32_t I = 0; I < HeaderData.OffsetEntryCount; ++I) { |
95 | auto Off = *getOffsetEntry(Data, Index: I); |
96 | OS << format(Fmt: "\n0x%0*" PRIx64, Vals: OffsetDumpWidth, Vals: Off); |
97 | if (DumpOpts.Verbose) |
98 | OS << format(Fmt: " => 0x%08" PRIx64, |
99 | Vals: Off + HeaderOffset + getHeaderSize(Format)); |
100 | } |
101 | OS << "\n]\n" ; |
102 | } |
103 | } |
104 | |
105 | uint64_t DWARFListTableHeader::() const { |
106 | if (HeaderData.Length == 0) |
107 | return 0; |
108 | return HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format); |
109 | } |
110 | |