LLVM 24.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"
17
18using namespace llvm;
19
21 uint64_t *OffsetPtr) {
22 HeaderOffset = *OffsetPtr;
23 Error Err = Error::success();
24
25 std::tie(HeaderData.Length, Format) = Data.getInitialLength(OffsetPtr, &Err);
26 if (Err)
27 return createStringError(
28 errc::invalid_argument, "parsing %s table at offset 0x%" PRIx64 ": %s",
29 SectionName.data(), HeaderOffset, toString(std::move(Err)).c_str());
30
31 uint8_t OffsetByteSize = Format == dwarf::DWARF64 ? 8 : 4;
32 uint64_t FullLength =
33 HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format);
34 if (FullLength < getHeaderSize(Format))
36 "%s table at offset 0x%" PRIx64
37 " has too small length (0x%" PRIx64
38 ") to contain a complete header",
39 SectionName.data(), HeaderOffset, FullLength);
40 assert(FullLength == length() && "Inconsistent calculation of length.");
41 uint64_t End = HeaderOffset + FullLength;
42 if (!Data.isValidOffsetForDataOfSize(HeaderOffset, FullLength))
44 "section is not large enough to contain a %s table "
45 "of length 0x%" PRIx64 " at offset 0x%" PRIx64,
46 SectionName.data(), FullLength, HeaderOffset);
47
48 HeaderData.Version = Data.getU16(OffsetPtr);
49 HeaderData.AddrSize = Data.getU8(OffsetPtr);
50 HeaderData.SegSize = Data.getU8(OffsetPtr);
51 HeaderData.OffsetEntryCount = Data.getU32(OffsetPtr);
52
53 // Perform basic validation of the remaining header fields.
54 if (HeaderData.Version < 5 || HeaderData.Version > 6)
56 "unrecognised %s table version %" PRIu16
57 " in table at offset 0x%" PRIx64,
58 SectionName.data(), HeaderData.Version, HeaderOffset);
60 HeaderData.AddrSize, errc::not_supported,
61 "%s table at offset 0x%" PRIx64, SectionName.data(), HeaderOffset))
62 return SizeErr;
63 if (HeaderData.SegSize != 0)
65 "%s table at offset 0x%" PRIx64
66 " has unsupported segment selector size %" PRIu8,
67 SectionName.data(), HeaderOffset, HeaderData.SegSize);
68 if (End < HeaderOffset + getHeaderSize(Format) +
69 HeaderData.OffsetEntryCount * OffsetByteSize)
71 "%s table at offset 0x%" PRIx64 " has more offset entries (%" PRIu32
72 ") than there is space for",
73 SectionName.data(), HeaderOffset, HeaderData.OffsetEntryCount);
74 Data.setAddressSize(HeaderData.AddrSize);
75 *OffsetPtr += HeaderData.OffsetEntryCount * OffsetByteSize;
76 return Error::success();
77}
78
80 DIDumpOptions DumpOpts) const {
81 if (DumpOpts.Verbose)
82 OS << formatv("{0:x8}: ", HeaderOffset);
83 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
84 OS << formatv("{0} list header: length = 0x{1:x-}", ListTypeString.data(),
85 fmt_align(HeaderData.Length, AlignStyle::Right, OffsetDumpWidth,
86 '0'))
87 << ", format = " << dwarf::FormatString(Format)
88 << formatv(", version = {0:x4}, addr_size = {1:x2}"
89 ", seg_size = {2:x2}"
90 ", offset_entry_count = {3:x8}\n",
91 HeaderData.Version, HeaderData.AddrSize, HeaderData.SegSize,
92 HeaderData.OffsetEntryCount);
93
94 if (HeaderData.OffsetEntryCount > 0) {
95 OS << "offsets: [";
96 for (uint32_t I = 0; I < HeaderData.OffsetEntryCount; ++I) {
97 auto Off = *getOffsetEntry(Data, I);
98 OS << formatv("\n0x{0:x-}",
99 fmt_align(Off, AlignStyle::Right, OffsetDumpWidth, '0'));
100 if (DumpOpts.Verbose)
101 OS << formatv(" => {0:x8}", Off + HeaderOffset + getHeaderSize(Format));
102 }
103 OS << "\n]\n";
104 }
105}
106
108 if (HeaderData.Length == 0)
109 return 0;
110 return HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format);
111}
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:1062
uint8_t getUnitLengthFieldByteSize(DwarfFormat Format)
Get the byte size of the unit length field depending on the DWARF format.
Definition Dwarf.h:1228
@ 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:1186
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)
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