LLVM  14.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"
16 
17 using namespace llvm;
18 
20  uint64_t *OffsetPtr) {
21  HeaderOffset = *OffsetPtr;
22  Error Err = Error::success();
23 
24  std::tie(HeaderData.Length, Format) = Data.getInitialLength(OffsetPtr, &Err);
25  if (Err)
26  return createStringError(
27  errc::invalid_argument, "parsing %s table at offset 0x%" PRIx64 ": %s",
28  SectionName.data(), HeaderOffset, toString(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))
35  "%s table at offset 0x%" PRIx64
36  " has too small length (0x%" PRIx64
37  ") to contain a complete header",
38  SectionName.data(), HeaderOffset, FullLength);
39  assert(FullLength == length() && "Inconsistent calculation of length.");
40  uint64_t End = HeaderOffset + FullLength;
41  if (!Data.isValidOffsetForDataOfSize(HeaderOffset, FullLength))
43  "section is not large enough to contain a %s table "
44  "of length 0x%" PRIx64 " at offset 0x%" PRIx64,
45  SectionName.data(), FullLength, HeaderOffset);
46 
47  HeaderData.Version = Data.getU16(OffsetPtr);
48  HeaderData.AddrSize = Data.getU8(OffsetPtr);
49  HeaderData.SegSize = Data.getU8(OffsetPtr);
50  HeaderData.OffsetEntryCount = Data.getU32(OffsetPtr);
51 
52  // Perform basic validation of the remaining header fields.
53  if (HeaderData.Version != 5)
55  "unrecognised %s table version %" PRIu16
56  " in table at offset 0x%" PRIx64,
57  SectionName.data(), HeaderData.Version, HeaderOffset);
59  HeaderData.AddrSize, errc::not_supported,
60  "%s table at offset 0x%" PRIx64, SectionName.data(), HeaderOffset))
61  return SizeErr;
62  if (HeaderData.SegSize != 0)
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)
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 
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 
106  if (HeaderData.Length == 0)
107  return 0;
108  return HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format);
109 }
llvm::errc::invalid_argument
@ invalid_argument
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::toString
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:1029
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
DWARFContext.h
Error.h
Errc.h
llvm::DWARFListTableHeader::dump
void dump(DataExtractor Data, raw_ostream &OS, DIDumpOptions DumpOpts={}) const
Definition: DWARFListTable.cpp:78
llvm::DWARFDataExtractor
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
Definition: DWARFDataExtractor.h:21
Format.h
llvm::DWARFListTableHeader::length
uint64_t length() const
Returns the length of the table, including the length field, or 0 if the length has not been determin...
Definition: DWARFListTable.cpp:105
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::DIDumpOptions::Verbose
bool Verbose
Definition: DIContext.h:191
llvm::dwarf::FormatString
StringRef FormatString(DwarfFormat Format)
Definition: Dwarf.cpp:790
llvm::DWARFContext::checkAddressSizeSupported
static Error checkAddressSizeSupported(unsigned AddressSize, std::error_code EC, char const *Fmt, const Ts &...Vals)
Definition: DWARFContext.h:392
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
DWARFListTable.h
llvm::DWARFListTableHeader::getHeaderSize
static uint8_t getHeaderSize(dwarf::DwarfFormat Format)
Return the size of the table header including the length but not including the offsets.
Definition: DWARFListTable.h:104
uint64_t
llvm::DWARFListTableHeader::getOffsetEntry
Optional< uint64_t > getOffsetEntry(DataExtractor Data, uint32_t Index) const
Definition: DWARFListTable.h:116
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::dwarf::getUnitLengthFieldByteSize
uint8_t getUnitLengthFieldByteSize(DwarfFormat Format)
Get the byte size of the unit length field depending on the DWARF format.
Definition: Dwarf.h:674
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Dwarf.h
llvm::errc::not_supported
@ not_supported
uint32_t
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1240
llvm::SectionName
Definition: DWARFSection.h:21
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
llvm::c_str
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Definition: WindowsSupport.h:193
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:149
llvm::dwarf::DWARF64
@ DWARF64
Definition: Dwarf.h:92
raw_ostream.h
llvm::dwarf::getDwarfOffsetByteSize
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition: Dwarf.h:635
llvm::DWARFListTableHeader::extract
Error extract(DWARFDataExtractor Data, uint64_t *OffsetPtr)
Extract the table header and the array of offsets.
Definition: DWARFListTable.cpp:19
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:180