LLVM 23.0.0git
DWARFListTable.cpp
Go to the documentation of this file.
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
12#include "llvm/Support/Errc.h"
13#include "llvm/Support/Error.h"
14#include "llvm/Support/Format.h"
18
19using namespace llvm;
20
22 uint64_t *OffsetPtr) {
23 HeaderOffset = *OffsetPtr;
24 Error Err = Error::success();
25
26 std::tie(HeaderData.Length, Format) = Data.getInitialLength(OffsetPtr, &Err);
27 if (Err)
28 return createStringError(
29 errc::invalid_argument, "parsing %s table at offset 0x%" PRIx64 ": %s",
30 SectionName.data(), HeaderOffset, toString(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))
37 "%s table at offset 0x%" PRIx64
38 " has too small length (0x%" PRIx64
39 ") to contain a complete header",
40 SectionName.data(), HeaderOffset, FullLength);
41 assert(FullLength == length() && "Inconsistent calculation of length.");
42 uint64_t End = HeaderOffset + FullLength;
43 if (!Data.isValidOffsetForDataOfSize(HeaderOffset, FullLength))
45 "section is not large enough to contain a %s table "
46 "of length 0x%" PRIx64 " at offset 0x%" PRIx64,
47 SectionName.data(), FullLength, HeaderOffset);
48
49 HeaderData.Version = Data.getU16(OffsetPtr);
50 HeaderData.AddrSize = Data.getU8(OffsetPtr);
51 HeaderData.SegSize = Data.getU8(OffsetPtr);
52 HeaderData.OffsetEntryCount = Data.getU32(OffsetPtr);
53
54 // Perform basic validation of the remaining header fields.
55 if (HeaderData.Version < 5 || HeaderData.Version > 6)
57 "unrecognised %s table version %" PRIu16
58 " in table at offset 0x%" PRIx64,
59 SectionName.data(), HeaderData.Version, HeaderOffset);
61 HeaderData.AddrSize, errc::not_supported,
62 "%s table at offset 0x%" PRIx64, SectionName.data(), HeaderOffset))
63 return SizeErr;
64 if (HeaderData.SegSize != 0)
66 "%s table at offset 0x%" PRIx64
67 " has unsupported segment selector size %" PRIu8,
68 SectionName.data(), HeaderOffset, HeaderData.SegSize);
69 if (End < HeaderOffset + getHeaderSize(Format) +
70 HeaderData.OffsetEntryCount * OffsetByteSize)
72 "%s table at offset 0x%" PRIx64 " has more offset entries (%" PRIu32
73 ") than there is space for",
74 SectionName.data(), HeaderOffset, HeaderData.OffsetEntryCount);
75 Data.setAddressSize(HeaderData.AddrSize);
76 *OffsetPtr += HeaderData.OffsetEntryCount * OffsetByteSize;
77 return Error::success();
78}
79
81 DIDumpOptions DumpOpts) const {
82 if (DumpOpts.Verbose)
83 OS << formatv("{0:x8}: ", HeaderOffset);
84 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
85 OS << formatv("{0} list header: length = 0x{1:x-}", ListTypeString.data(),
86 fmt_align(HeaderData.Length, AlignStyle::Right, OffsetDumpWidth,
87 '0'))
88 << ", format = " << dwarf::FormatString(Format)
89 << formatv(", version = {0:x4}, addr_size = {1:x2}"
90 ", seg_size = {2:x2}"
91 ", offset_entry_count = {3:x8}\n",
92 HeaderData.Version, HeaderData.AddrSize, HeaderData.SegSize,
93 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, I);
99 OS << formatv("\n0x{0:x-}",
100 fmt_align(Off, AlignStyle::Right, OffsetDumpWidth, '0'));
101 if (DumpOpts.Verbose)
102 OS << formatv(" => {0:x8}", Off + HeaderOffset + getHeaderSize(Format));
103 }
104 OS << "\n]\n";
105 }
106}
107
109 if (HeaderData.Length == 0)
110 return 0;
111 return HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format);
112}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains constants used for implementing Dwarf debug support.
#define I(x, y, z)
Definition MD5.cpp:57
static Error checkAddressSizeSupported(unsigned AddressSize, std::error_code EC, char const *Fmt, const Ts &...Vals)
A DWARFDataExtractor (typically for an in-memory copy of an object-file section) plus a relocation ma...
static uint8_t getHeaderSize(dwarf::DwarfFormat Format)
Return the size of the table header including the length but not including the offsets.
LLVM_ABI Error extract(DWARFDataExtractor Data, uint64_t *OffsetPtr)
Extract the table header and the array of offsets.
std::optional< uint64_t > getOffsetEntry(DataExtractor Data, uint32_t Index) const
LLVM_ABI void dump(DataExtractor Data, raw_ostream &OS, DIDumpOptions DumpOpts={}) const
LLVM_ABI uint64_t length() const
Returns the length of the table, including the length field, or 0 if the length has not been determin...
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI StringRef FormatString(DwarfFormat Format)
Definition Dwarf.cpp:1023
uint8_t getUnitLengthFieldByteSize(DwarfFormat Format)
Get the byte size of the unit length field depending on the DWARF format.
Definition Dwarf.h:1139
@ DWARF64
Definition Dwarf.h:93
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition Dwarf.h:1097
This is an optimization pass for GlobalISel generic memory operations.
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
@ not_supported
Definition Errc.h:69
@ invalid_argument
Definition Errc.h:56
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
support::detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
Container for dump options that control which debug information will be dumped.
Definition DIContext.h:196