LLVM  9.0.0svn
CodeViewRecordIO.cpp
Go to the documentation of this file.
1 //===- CodeViewRecordIO.cpp -------------------------------------*- C++ -*-===//
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 
14 
15 using namespace llvm;
16 using namespace llvm::codeview;
17 
19  RecordLimit Limit;
20  Limit.MaxLength = MaxLength;
21  Limit.BeginOffset = getCurrentOffset();
22  Limits.push_back(Limit);
23  return Error::success();
24 }
25 
27  assert(!Limits.empty() && "Not in a record!");
28  Limits.pop_back();
29  // We would like to assert that we actually read / wrote all the bytes that we
30  // expected to for this record, but unfortunately we can't do this. Some
31  // producers such as MASM over-allocate for certain types of records and
32  // commit the extraneous data, so when reading we can't be sure every byte
33  // will have been read. And when writing we over-allocate temporarily since
34  // we don't know how big the record is until we're finished writing it, so
35  // even though we don't commit the extraneous data, we still can't guarantee
36  // we're at the end of the allocated data.
37  return Error::success();
38 }
39 
41  assert(!Limits.empty() && "Not in a record!");
42 
43  // The max length of the next field is the minimum of all lengths that would
44  // be allowed by any of the sub-records we're in. In practice, we can only
45  // ever be at most 1 sub-record deep (in a FieldList), but this works for
46  // the general case.
47  uint32_t Offset = getCurrentOffset();
48  Optional<uint32_t> Min = Limits.front().bytesRemaining(Offset);
49  for (auto X : makeArrayRef(Limits).drop_front()) {
50  Optional<uint32_t> ThisMin = X.bytesRemaining(Offset);
51  if (ThisMin.hasValue())
52  Min = (Min.hasValue()) ? std::min(*Min, *ThisMin) : *ThisMin;
53  }
54  assert(Min.hasValue() && "Every field must have a maximum length!");
55 
56  return *Min;
57 }
58 
60  if (isReading())
61  return Reader->padToAlignment(Align);
62  return Writer->padToAlignment(Align);
63 }
64 
66  assert(!isWriting() && "Cannot skip padding while writing!");
67 
68  if (Reader->bytesRemaining() == 0)
69  return Error::success();
70 
71  uint8_t Leaf = Reader->peek();
72  if (Leaf < LF_PAD0)
73  return Error::success();
74  // Leaf is greater than 0xf0. We should advance by the number of bytes in
75  // the low 4 bits.
76  unsigned BytesToAdvance = Leaf & 0x0F;
77  return Reader->skip(BytesToAdvance);
78 }
79 
81  if (isWriting()) {
82  if (auto EC = Writer->writeBytes(Bytes))
83  return EC;
84  } else {
85  if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
86  return EC;
87  }
88  return Error::success();
89 }
90 
91 Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes) {
92  ArrayRef<uint8_t> BytesRef(Bytes);
93  if (auto EC = mapByteVectorTail(BytesRef))
94  return EC;
95  if (!isWriting())
96  Bytes.assign(BytesRef.begin(), BytesRef.end());
97 
98  return Error::success();
99 }
100 
102  if (isWriting()) {
103  if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
104  return EC;
105  return Error::success();
106  }
107 
108  uint32_t I;
109  if (auto EC = Reader->readInteger(I))
110  return EC;
111  TypeInd.setIndex(I);
112  return Error::success();
113 }
114 
116  if (isWriting()) {
117  if (Value >= 0) {
118  if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
119  return EC;
120  } else {
121  if (auto EC = writeEncodedSignedInteger(Value))
122  return EC;
123  }
124  } else {
125  APSInt N;
126  if (auto EC = consume(*Reader, N))
127  return EC;
128  Value = N.getExtValue();
129  }
130 
131  return Error::success();
132 }
133 
135  if (isWriting()) {
136  if (auto EC = writeEncodedUnsignedInteger(Value))
137  return EC;
138  } else {
139  APSInt N;
140  if (auto EC = consume(*Reader, N))
141  return EC;
142  Value = N.getZExtValue();
143  }
144  return Error::success();
145 }
146 
148  if (isWriting()) {
149  if (Value.isSigned())
150  return writeEncodedSignedInteger(Value.getSExtValue());
151  return writeEncodedUnsignedInteger(Value.getZExtValue());
152  }
153 
154  return consume(*Reader, Value);
155 }
156 
158  if (isWriting()) {
159  // Truncate if we attempt to write too much.
160  StringRef S = Value.take_front(maxFieldLength() - 1);
161  if (auto EC = Writer->writeCString(S))
162  return EC;
163  } else {
164  if (auto EC = Reader->readCString(Value))
165  return EC;
166  }
167  return Error::success();
168 }
169 
171  constexpr uint32_t GuidSize = 16;
172  if (maxFieldLength() < GuidSize)
173  return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
174 
175  if (isWriting()) {
176  if (auto EC = Writer->writeBytes(Guid.Guid))
177  return EC;
178  } else {
179  ArrayRef<uint8_t> GuidBytes;
180  if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
181  return EC;
182  memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
183  }
184  return Error::success();
185 }
186 
188  if (isWriting()) {
189  for (auto V : Value) {
190  if (auto EC = mapStringZ(V))
191  return EC;
192  }
193  if (auto EC = Writer->writeInteger<uint8_t>(0))
194  return EC;
195  } else {
196  StringRef S;
197  if (auto EC = mapStringZ(S))
198  return EC;
199  while (!S.empty()) {
200  Value.push_back(S);
201  if (auto EC = mapStringZ(S))
202  return EC;
203  };
204  }
205  return Error::success();
206 }
207 
208 Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
209  assert(Value < 0 && "Encoded integer is not signed!");
210  if (Value >= std::numeric_limits<int8_t>::min()) {
211  if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
212  return EC;
213  if (auto EC = Writer->writeInteger<int8_t>(Value))
214  return EC;
215  } else if (Value >= std::numeric_limits<int16_t>::min()) {
216  if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
217  return EC;
218  if (auto EC = Writer->writeInteger<int16_t>(Value))
219  return EC;
220  } else if (Value >= std::numeric_limits<int32_t>::min()) {
221  if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
222  return EC;
223  if (auto EC = Writer->writeInteger<int32_t>(Value))
224  return EC;
225  } else {
226  if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
227  return EC;
228  if (auto EC = Writer->writeInteger(Value))
229  return EC;
230  }
231  return Error::success();
232 }
233 
234 Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
235  if (Value < LF_NUMERIC) {
236  if (auto EC = Writer->writeInteger<uint16_t>(Value))
237  return EC;
238  } else if (Value <= std::numeric_limits<uint16_t>::max()) {
239  if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
240  return EC;
241  if (auto EC = Writer->writeInteger<uint16_t>(Value))
242  return EC;
243  } else if (Value <= std::numeric_limits<uint32_t>::max()) {
244  if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
245  return EC;
246  if (auto EC = Writer->writeInteger<uint32_t>(Value))
247  return EC;
248  } else {
249  if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
250  return EC;
251  if (auto EC = Writer->writeInteger(Value))
252  return EC;
253  }
254 
255  return Error::success();
256 }
Error mapByteVectorTail(ArrayRef< uint8_t > &Bytes)
LLVM_NODISCARD StringRef take_front(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with only the first N elements remaining.
Definition: StringRef.h:587
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Error padToAlignment(uint32_t Align)
Error writeBytes(ArrayRef< uint8_t > Buffer)
Write the bytes specified in Buffer to the underlying stream.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1562
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream&#39;s offset.
iterator begin() const
Definition: ArrayRef.h:136
void push_back(const T &Elt)
Definition: SmallVector.h:211
Error mapInteger(TypeIndex &TypeInd)
Error mapStringZVectorZ(std::vector< StringRef > &Value)
uint8_t peek() const
Examine the next byte of the underlying stream without advancing the stream&#39;s offset.
void setIndex(uint32_t I)
Definition: TypeIndex.h:111
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
Error beginRecord(Optional< uint32_t > MaxLength)
This represents the &#39;GUID&#39; type from windows.h.
Definition: GUID.h:21
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
Error readCString(StringRef &Dest)
Read a null terminated string from Dest.
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1574
A 32-bit type reference.
Definition: TypeIndex.h:95
Error padToAlignment(uint32_t Align)
uint8_t Guid[16]
Definition: GUID.h:22
bool isSigned() const
Definition: APSInt.h:58
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
const T * data() const
Definition: ArrayRef.h:145
uint32_t getIndex() const
Definition: TypeIndex.h:110
Error writeCString(StringRef Str)
Write the string Str to the underlying stream followed by a null terminator.
int64_t getExtValue() const
Get the correctly-extended int64_t value.
Definition: APSInt.h:75
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
Error mapEncodedInteger(int64_t &Value)
iterator end() const
Definition: ArrayRef.h:137
bool hasValue() const
Definition: Optional.h:259
Error readBytes(ArrayRef< uint8_t > &Buffer, uint32_t Size)
Read Size bytes from the underlying stream at the current offset and and set Buffer to the resulting ...
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
Error padToAlignment(uint32_t Align)
Error skip(uint32_t Amount)
Advance the stream&#39;s offset by Amount bytes.
uint32_t bytesRemaining() const
Error mapStringZ(StringRef &Value)
Error consume(BinaryStreamReader &Reader)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48