LLVM  8.0.1
DWARFListTable.cpp
Go to the documentation of this file.
1 //===- DWARFListTable.cpp ---------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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  uint32_t *OffsetPtr) {
21  HeaderOffset = *OffsetPtr;
22  // Read and verify the length field.
23  if (!Data.isValidOffsetForDataOfSize(*OffsetPtr, sizeof(uint32_t)))
25  "section is not large enough to contain a "
26  "%s table length at offset 0x%" PRIx32,
27  SectionName.data(), *OffsetPtr);
28  // TODO: Add support for DWARF64.
29  HeaderData.Length = Data.getU32(OffsetPtr);
30  if (HeaderData.Length == 0xffffffffu)
32  "DWARF64 is not supported in %s at offset 0x%" PRIx32,
33  SectionName.data(), HeaderOffset);
35  if (HeaderData.Length + sizeof(uint32_t) < sizeof(Header))
37  "%s table at offset 0x%" PRIx32
38  " has too small length (0x%" PRIx32
39  ") to contain a complete header",
40  SectionName.data(), HeaderOffset, length());
41  uint32_t End = HeaderOffset + length();
42  if (!Data.isValidOffsetForDataOfSize(HeaderOffset, End - HeaderOffset))
44  "section is not large enough to contain a %s table "
45  "of length 0x%" PRIx32 " at offset 0x%" PRIx32,
46  SectionName.data(), length(), 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)
56  "unrecognised %s table version %" PRIu16
57  " in table at offset 0x%" PRIx32,
58  SectionName.data(), HeaderData.Version, HeaderOffset);
59  if (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)
61  "%s table at offset 0x%" PRIx32
62  " has unsupported address size %" PRIu8,
63  SectionName.data(), HeaderOffset, HeaderData.AddrSize);
64  if (HeaderData.SegSize != 0)
66  "%s table at offset 0x%" PRIx32
67  " has unsupported segment selector size %" PRIu8,
68  SectionName.data(), HeaderOffset, HeaderData.SegSize);
69  if (End < HeaderOffset + sizeof(HeaderData) +
70  HeaderData.OffsetEntryCount * sizeof(uint32_t))
72  "%s table at offset 0x%" PRIx32 " has more offset entries (%" PRIu32
73  ") than there is space for",
74  SectionName.data(), HeaderOffset, HeaderData.OffsetEntryCount);
75  Data.setAddressSize(HeaderData.AddrSize);
76  for (uint32_t I = 0; I < HeaderData.OffsetEntryCount; ++I)
77  Offsets.push_back(Data.getU32(OffsetPtr));
78  return Error::success();
79 }
80 
82  if (DumpOpts.Verbose)
83  OS << format("0x%8.8" PRIx32 ": ", HeaderOffset);
84  OS << format(
85  "%s list header: length = 0x%8.8" PRIx32 ", version = 0x%4.4" PRIx16 ", "
86  "addr_size = 0x%2.2" PRIx8 ", seg_size = 0x%2.2" PRIx8
87  ", offset_entry_count = "
88  "0x%8.8" PRIx32 "\n",
89  ListTypeString.data(), HeaderData.Length, HeaderData.Version,
90  HeaderData.AddrSize, HeaderData.SegSize, HeaderData.OffsetEntryCount);
91 
92  if (HeaderData.OffsetEntryCount > 0) {
93  OS << "offsets: [";
94  for (const auto &Off : Offsets) {
95  OS << format("\n0x%8.8" PRIx32, Off);
96  if (DumpOpts.Verbose)
97  OS << format(" => 0x%8.8" PRIx32,
98  Off + HeaderOffset + sizeof(HeaderData));
99  }
100  OS << "\n]\n";
101  }
102 }
103 
105  if (HeaderData.Length == 0)
106  return 0;
107  // TODO: DWARF64 support.
108  return HeaderData.Length + sizeof(uint32_t);
109 }
This class represents lattice values for constants.
Definition: AllocatorList.h:24
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
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.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
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:159
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:327
void setAddressSize(uint8_t Size)
Set the address size for this extractor.
Definition: DataExtractor.h:61
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...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
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:1164