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