LLVM  7.0.0
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/Error.h"
13 #include "llvm/Support/Format.h"
15 
16 using namespace llvm;
17 
18 template <typename... Ts>
19 static Error createError(char const *Fmt, const Ts &... Vals) {
20  std::string Buffer;
21  raw_string_ostream Stream(Buffer);
22  Stream << format(Fmt, Vals...);
23  return make_error<StringError>(Stream.str(), inconvertibleErrorCode());
24 }
25 
27  uint32_t *OffsetPtr) {
28  HeaderOffset = *OffsetPtr;
29  // Read and verify the length field.
30  if (!Data.isValidOffsetForDataOfSize(*OffsetPtr, sizeof(uint32_t)))
31  return createError("section is not large enough to contain a "
32  "%s table length at offset 0x%" PRIx32,
33  SectionName.data(), *OffsetPtr);
34  // TODO: Add support for DWARF64.
35  HeaderData.Length = Data.getU32(OffsetPtr);
36  if (HeaderData.Length == 0xffffffffu)
37  return createError("DWARF64 is not supported in %s at offset 0x%" PRIx32,
38  SectionName.data(), HeaderOffset);
40  if (HeaderData.Length + sizeof(uint32_t) < sizeof(Header))
41  return createError("%s table at offset 0x%" PRIx32
42  " has too small length (0x%" PRIx32
43  ") to contain a complete header",
44  SectionName.data(), HeaderOffset, length());
45  uint32_t End = HeaderOffset + length();
46  if (!Data.isValidOffsetForDataOfSize(HeaderOffset, End - HeaderOffset))
47  return createError("section is not large enough to contain a %s table "
48  "of length 0x%" PRIx32 " at offset 0x%" PRIx32,
49  SectionName.data(), length(), HeaderOffset);
50 
51  HeaderData.Version = Data.getU16(OffsetPtr);
52  HeaderData.AddrSize = Data.getU8(OffsetPtr);
53  HeaderData.SegSize = Data.getU8(OffsetPtr);
54  HeaderData.OffsetEntryCount = Data.getU32(OffsetPtr);
55 
56  // Perform basic validation of the remaining header fields.
57  if (HeaderData.Version != 5)
58  return createError("unrecognised %s table version %" PRIu16
59  " in table at offset 0x%" PRIx32,
60  SectionName.data(), HeaderData.Version, HeaderOffset);
61  if (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)
62  return createError("%s table at offset 0x%" PRIx32
63  " has unsupported address size %hhu",
64  SectionName.data(), HeaderOffset, HeaderData.AddrSize);
65  if (HeaderData.SegSize != 0)
66  return createError("%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))
71  return createError(
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 }
Compute iterated dominance frontiers using a linear time algorithm.
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.
static Error createError(char const *Fmt, const Ts &... Vals)
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:154
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.
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:493
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:322
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...
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:477
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:46
void dump(raw_ostream &OS, DIDumpOptions DumpOpts={}) const
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:73