LLVM  9.0.0svn
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 
11 #include "llvm/Support/Errc.h"
12 #include "llvm/Support/Error.h"
13 #include "llvm/Support/Format.h"
15 
16 using namespace llvm;
17 
19  uint32_t *OffsetPtr) {
20  HeaderOffset = *OffsetPtr;
21  // Read and verify the length field.
22  if (!Data.isValidOffsetForDataOfSize(*OffsetPtr, sizeof(uint32_t)))
24  "section is not large enough to contain a "
25  "%s table length at offset 0x%" PRIx32,
26  SectionName.data(), *OffsetPtr);
27  // TODO: Add support for DWARF64.
28  HeaderData.Length = Data.getU32(OffsetPtr);
29  if (HeaderData.Length == 0xffffffffu)
31  "DWARF64 is not supported in %s at offset 0x%" PRIx32,
32  SectionName.data(), HeaderOffset);
34  if (HeaderData.Length + sizeof(uint32_t) < sizeof(Header))
36  "%s table at offset 0x%" PRIx32
37  " has too small length (0x%" PRIx32
38  ") to contain a complete header",
39  SectionName.data(), HeaderOffset, length());
40  uint32_t End = HeaderOffset + length();
41  if (!Data.isValidOffsetForDataOfSize(HeaderOffset, End - HeaderOffset))
43  "section is not large enough to contain a %s table "
44  "of length 0x%" PRIx32 " at offset 0x%" PRIx32,
45  SectionName.data(), length(), 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%" PRIx32,
57  SectionName.data(), HeaderData.Version, HeaderOffset);
58  if (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)
60  "%s table at offset 0x%" PRIx32
61  " has unsupported address size %" PRIu8,
62  SectionName.data(), HeaderOffset, HeaderData.AddrSize);
63  if (HeaderData.SegSize != 0)
65  "%s table at offset 0x%" PRIx32
66  " has unsupported segment selector size %" PRIu8,
67  SectionName.data(), HeaderOffset, HeaderData.SegSize);
68  if (End < HeaderOffset + sizeof(HeaderData) +
69  HeaderData.OffsetEntryCount * sizeof(uint32_t))
71  "%s table at offset 0x%" PRIx32 " has more offset entries (%" PRIu32
72  ") than there is space for",
73  SectionName.data(), HeaderOffset, HeaderData.OffsetEntryCount);
74  Data.setAddressSize(HeaderData.AddrSize);
75  for (uint32_t I = 0; I < HeaderData.OffsetEntryCount; ++I)
76  Offsets.push_back(Data.getU32(OffsetPtr));
77  return Error::success();
78 }
79 
81  if (DumpOpts.Verbose)
82  OS << format("0x%8.8" PRIx32 ": ", HeaderOffset);
83  OS << format(
84  "%s list header: length = 0x%8.8" PRIx32 ", version = 0x%4.4" PRIx16 ", "
85  "addr_size = 0x%2.2" PRIx8 ", seg_size = 0x%2.2" PRIx8
86  ", offset_entry_count = "
87  "0x%8.8" PRIx32 "\n",
88  ListTypeString.data(), HeaderData.Length, HeaderData.Version,
89  HeaderData.AddrSize, HeaderData.SegSize, HeaderData.OffsetEntryCount);
90 
91  if (HeaderData.OffsetEntryCount > 0) {
92  OS << "offsets: [";
93  for (const auto &Off : Offsets) {
94  OS << format("\n0x%8.8" PRIx32, Off);
95  if (DumpOpts.Verbose)
96  OS << format(" => 0x%8.8" PRIx32,
97  Off + HeaderOffset + sizeof(HeaderData));
98  }
99  OS << "\n]\n";
100  }
101 }
102 
104  if (HeaderData.Length == 0)
105  return 0;
106  // TODO: DWARF64 support.
107  return HeaderData.Length + sizeof(uint32_t);
108 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:123
Error extract(DWARFDataExtractor Data, uint32_t *OffsetPtr)
Extract the table header and the array of offsets.
uint16_t getU16(uint32_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
uint32_t getU32(uint32_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:158
bool isValidOffsetForDataOfSize(uint32_t offset, uint32_t length) const
Test the availability of length bytes of data from offset.
uint8_t getU8(uint32_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
void setAddressSize(uint8_t Size)
Set the address size for this extractor.
Definition: DataExtractor.h:60
This file contains constants used for implementing Dwarf debug support.
#define I(x, y, z)
Definition: MD5.cpp:58
uint32_t length() const
Returns the length of the table, including the length field, or 0 if the length has not been determin...
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:122
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
void dump(raw_ostream &OS, DIDumpOptions DumpOpts={}) const
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1163