LLVM  13.0.0git
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 
38  if (isStreaming()) {
39  // For streaming mode, add padding to align with 4 byte boundaries for each
40  // record
42  if (Align == 0)
43  return Error::success();
44 
45  int PaddingBytes = 4 - Align;
46  while (PaddingBytes > 0) {
47  char Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
48  StringRef BytesSR = StringRef(&Pad, sizeof(Pad));
49  Streamer->emitBytes(BytesSR);
50  --PaddingBytes;
51  }
52  resetStreamedLen();
53  }
54  return Error::success();
55 }
56 
58  if (isStreaming())
59  return 0;
60 
61  assert(!Limits.empty() && "Not in a record!");
62 
63  // The max length of the next field is the minimum of all lengths that would
64  // be allowed by any of the sub-records we're in. In practice, we can only
65  // ever be at most 1 sub-record deep (in a FieldList), but this works for
66  // the general case.
67  uint32_t Offset = getCurrentOffset();
68  Optional<uint32_t> Min = Limits.front().bytesRemaining(Offset);
69  for (auto X : makeArrayRef(Limits).drop_front()) {
70  Optional<uint32_t> ThisMin = X.bytesRemaining(Offset);
71  if (ThisMin.hasValue())
72  Min = (Min.hasValue()) ? std::min(*Min, *ThisMin) : *ThisMin;
73  }
74  assert(Min.hasValue() && "Every field must have a maximum length!");
75 
76  return *Min;
77 }
78 
80  if (isReading())
81  return Reader->padToAlignment(Align);
82  return Writer->padToAlignment(Align);
83 }
84 
86  assert(!isWriting() && "Cannot skip padding while writing!");
87 
88  if (Reader->bytesRemaining() == 0)
89  return Error::success();
90 
91  uint8_t Leaf = Reader->peek();
92  if (Leaf < LF_PAD0)
93  return Error::success();
94  // Leaf is greater than 0xf0. We should advance by the number of bytes in
95  // the low 4 bits.
96  unsigned BytesToAdvance = Leaf & 0x0F;
97  return Reader->skip(BytesToAdvance);
98 }
99 
101  const Twine &Comment) {
102  if (isStreaming()) {
103  emitComment(Comment);
104  Streamer->emitBinaryData(toStringRef(Bytes));
105  incrStreamedLen(Bytes.size());
106  } else if (isWriting()) {
107  if (auto EC = Writer->writeBytes(Bytes))
108  return EC;
109  } else {
110  if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
111  return EC;
112  }
113  return Error::success();
114 }
115 
116 Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes,
117  const Twine &Comment) {
118  ArrayRef<uint8_t> BytesRef(Bytes);
119  if (auto EC = mapByteVectorTail(BytesRef, Comment))
120  return EC;
121  if (!isWriting())
122  Bytes.assign(BytesRef.begin(), BytesRef.end());
123 
124  return Error::success();
125 }
126 
128  if (isStreaming()) {
129  std::string TypeNameStr = Streamer->getTypeName(TypeInd);
130  if (!TypeNameStr.empty())
131  emitComment(Comment + ": " + TypeNameStr);
132  else
133  emitComment(Comment);
134  Streamer->emitIntValue(TypeInd.getIndex(), sizeof(TypeInd.getIndex()));
135  incrStreamedLen(sizeof(TypeInd.getIndex()));
136  } else if (isWriting()) {
137  if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
138  return EC;
139  } else {
140  uint32_t I;
141  if (auto EC = Reader->readInteger(I))
142  return EC;
143  TypeInd.setIndex(I);
144  }
145  return Error::success();
146 }
147 
149  const Twine &Comment) {
150  if (isStreaming()) {
151  if (Value >= 0)
152  emitEncodedUnsignedInteger(static_cast<uint64_t>(Value), Comment);
153  else
154  emitEncodedSignedInteger(Value, Comment);
155  } else if (isWriting()) {
156  if (Value >= 0) {
157  if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
158  return EC;
159  } else {
160  if (auto EC = writeEncodedSignedInteger(Value))
161  return EC;
162  }
163  } else {
164  APSInt N;
165  if (auto EC = consume(*Reader, N))
166  return EC;
167  Value = N.getExtValue();
168  }
169 
170  return Error::success();
171 }
172 
174  const Twine &Comment) {
175  if (isStreaming())
176  emitEncodedUnsignedInteger(Value, Comment);
177  else if (isWriting()) {
178  if (auto EC = writeEncodedUnsignedInteger(Value))
179  return EC;
180  } else {
181  APSInt N;
182  if (auto EC = consume(*Reader, N))
183  return EC;
184  Value = N.getZExtValue();
185  }
186  return Error::success();
187 }
188 
190  if (isStreaming()) {
191  if (Value.isSigned())
192  emitEncodedSignedInteger(Value.getSExtValue(), Comment);
193  else
194  emitEncodedUnsignedInteger(Value.getZExtValue(), Comment);
195  } else if (isWriting()) {
196  if (Value.isSigned())
197  return writeEncodedSignedInteger(Value.getSExtValue());
198  return writeEncodedUnsignedInteger(Value.getZExtValue());
199  } else
200  return consume(*Reader, Value);
201  return Error::success();
202 }
203 
205  if (isStreaming()) {
206  auto NullTerminatedString = StringRef(Value.data(), Value.size() + 1);
207  emitComment(Comment);
208  Streamer->emitBytes(NullTerminatedString);
209  incrStreamedLen(NullTerminatedString.size());
210  } else if (isWriting()) {
211  // Truncate if we attempt to write too much.
212  StringRef S = Value.take_front(maxFieldLength() - 1);
213  if (auto EC = Writer->writeCString(S))
214  return EC;
215  } else {
216  if (auto EC = Reader->readCString(Value))
217  return EC;
218  }
219  return Error::success();
220 }
221 
222 Error CodeViewRecordIO::mapGuid(GUID &Guid, const Twine &Comment) {
223  constexpr uint32_t GuidSize = 16;
224 
225  if (isStreaming()) {
226  StringRef GuidSR =
227  StringRef((reinterpret_cast<const char *>(&Guid)), GuidSize);
228  emitComment(Comment);
229  Streamer->emitBytes(GuidSR);
230  incrStreamedLen(GuidSize);
231  return Error::success();
232  }
233 
234  if (maxFieldLength() < GuidSize)
235  return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
236 
237  if (isWriting()) {
238  if (auto EC = Writer->writeBytes(Guid.Guid))
239  return EC;
240  } else {
241  ArrayRef<uint8_t> GuidBytes;
242  if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
243  return EC;
244  memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
245  }
246  return Error::success();
247 }
248 
250  const Twine &Comment) {
251 
252  if (!isReading()) {
253  emitComment(Comment);
254  for (auto V : Value) {
255  if (auto EC = mapStringZ(V))
256  return EC;
257  }
258  uint8_t FinalZero = 0;
259  if (auto EC = mapInteger(FinalZero))
260  return EC;
261  } else {
262  StringRef S;
263  if (auto EC = mapStringZ(S))
264  return EC;
265  while (!S.empty()) {
266  Value.push_back(S);
267  if (auto EC = mapStringZ(S))
268  return EC;
269  };
270  }
271  return Error::success();
272 }
273 
274 void CodeViewRecordIO::emitEncodedSignedInteger(const int64_t &Value,
275  const Twine &Comment) {
277  Streamer->emitIntValue(LF_CHAR, 2);
278  emitComment(Comment);
279  Streamer->emitIntValue(Value, 1);
280  incrStreamedLen(3);
281  } else if (Value >= std::numeric_limits<int16_t>::min()) {
282  Streamer->emitIntValue(LF_SHORT, 2);
283  emitComment(Comment);
284  Streamer->emitIntValue(Value, 2);
285  incrStreamedLen(4);
286  } else if (Value >= std::numeric_limits<int32_t>::min()) {
287  Streamer->emitIntValue(LF_LONG, 2);
288  emitComment(Comment);
289  Streamer->emitIntValue(Value, 4);
290  incrStreamedLen(6);
291  } else {
292  Streamer->emitIntValue(LF_QUADWORD, 2);
293  emitComment(Comment);
294  Streamer->emitIntValue(Value, 4);
295  incrStreamedLen(6);
296  }
297 }
298 
299 void CodeViewRecordIO::emitEncodedUnsignedInteger(const uint64_t &Value,
300  const Twine &Comment) {
301  if (Value < LF_NUMERIC) {
302  emitComment(Comment);
303  Streamer->emitIntValue(Value, 2);
304  incrStreamedLen(2);
305  } else if (Value <= std::numeric_limits<uint16_t>::max()) {
306  Streamer->emitIntValue(LF_USHORT, 2);
307  emitComment(Comment);
308  Streamer->emitIntValue(Value, 2);
309  incrStreamedLen(4);
310  } else if (Value <= std::numeric_limits<uint32_t>::max()) {
311  Streamer->emitIntValue(LF_ULONG, 2);
312  emitComment(Comment);
313  Streamer->emitIntValue(Value, 4);
314  incrStreamedLen(6);
315  } else {
316  Streamer->emitIntValue(LF_UQUADWORD, 2);
317  emitComment(Comment);
318  Streamer->emitIntValue(Value, 8);
319  incrStreamedLen(6);
320  }
321 }
322 
323 Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
325  if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
326  return EC;
327  if (auto EC = Writer->writeInteger<int8_t>(Value))
328  return EC;
329  } else if (Value >= std::numeric_limits<int16_t>::min()) {
330  if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
331  return EC;
332  if (auto EC = Writer->writeInteger<int16_t>(Value))
333  return EC;
334  } else if (Value >= std::numeric_limits<int32_t>::min()) {
335  if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
336  return EC;
337  if (auto EC = Writer->writeInteger<int32_t>(Value))
338  return EC;
339  } else {
340  if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
341  return EC;
342  if (auto EC = Writer->writeInteger(Value))
343  return EC;
344  }
345  return Error::success();
346 }
347 
348 Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
349  if (Value < LF_NUMERIC) {
350  if (auto EC = Writer->writeInteger<uint16_t>(Value))
351  return EC;
352  } else if (Value <= std::numeric_limits<uint16_t>::max()) {
353  if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
354  return EC;
355  if (auto EC = Writer->writeInteger<uint16_t>(Value))
356  return EC;
357  } else if (Value <= std::numeric_limits<uint32_t>::max()) {
358  if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
359  return EC;
360  if (auto EC = Writer->writeInteger<uint32_t>(Value))
361  return EC;
362  } else {
363  if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
364  return EC;
365  if (auto EC = Writer->writeInteger(Value))
366  return EC;
367  }
368 
369  return Error::success();
370 }
llvm::codeview::CodeViewRecordIO::getStreamedLen
uint64_t getStreamedLen()
Definition: CodeViewRecordIO.h:207
BinaryStreamReader.h
llvm::BinaryStreamReader::bytesRemaining
uint32_t bytesRemaining() const
Definition: BinaryStreamReader.h:258
llvm
This class represents lattice values for constants.
Definition: AllocatorList.h:23
llvm::BinaryStreamWriter::writeInteger
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
Definition: BinaryStreamWriter.h:64
RecordSerialization.h
llvm::BinaryStreamReader::readBytes
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 ...
Definition: BinaryStreamReader.cpp:37
llvm::codeview::TypeIndex::setIndex
void setIndex(uint32_t I)
Definition: TypeIndex.h:111
llvm::codeview::CodeViewRecordIO::isWriting
bool isWriting() const
Definition: CodeViewRecordIO.h:75
llvm::codeview::CodeViewRecordIO::mapByteVectorTail
Error mapByteVectorTail(ArrayRef< uint8_t > &Bytes, const Twine &Comment="")
Definition: CodeViewRecordIO.cpp:100
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
llvm::codeview::GUID
This represents the 'GUID' type from windows.h.
Definition: GUID.h:21
llvm::codeview::CodeViewRecordIO::mapStringZ
Error mapStringZ(StringRef &Value, const Twine &Comment="")
Definition: CodeViewRecordIO.cpp:204
llvm::Optional< uint32_t >
llvm::codeview::CodeViewRecordIO::padToAlignment
Error padToAlignment(uint32_t Align)
Definition: CodeViewRecordIO.cpp:79
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::codeview::cv_error_code::insufficient_buffer
@ insufficient_buffer
llvm::ArrayRef::data
const T * data() const
Definition: ArrayRef.h:153
llvm::Optional::hasValue
constexpr bool hasValue() const
Definition: Optional.h:286
llvm::codeview::CodeViewRecordIO::beginRecord
Error beginRecord(Optional< uint32_t > MaxLength)
Definition: CodeViewRecordIO.cpp:18
llvm::dwarf::toStringRef
StringRef toStringRef(const Optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
Definition: DWARFFormValue.h:177
llvm::BinaryStreamReader::readInteger
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream's offset.
Definition: BinaryStreamReader.h:76
llvm::APSInt
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:22
Align
uint64_t Align
Definition: ELFObjHandler.cpp:83
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
CodeView.h
llvm::BinaryStreamReader::padToAlignment
Error padToAlignment(uint32_t Align)
Definition: BinaryStreamReader.cpp:155
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::codeview::CodeViewRecordIO::mapStringZVectorZ
Error mapStringZVectorZ(std::vector< StringRef > &Value, const Twine &Comment="")
Definition: CodeViewRecordIO.cpp:249
llvm::codeview::CodeViewRecordIO::skipPadding
Error skipPadding()
Definition: CodeViewRecordIO.cpp:85
llvm::codeview::CodeViewRecordStreamer::getTypeName
virtual std::string getTypeName(TypeIndex TI)=0
llvm::BinaryStreamReader::readCString
Error readCString(StringRef &Dest)
Read a null terminated string from Dest.
Definition: BinaryStreamReader.cpp:74
I
#define I(x, y, z)
Definition: MD5.cpp:59
CodeViewRecordIO.h
llvm::codeview::CodeViewRecordStreamer::emitBinaryData
virtual void emitBinaryData(StringRef Data)=0
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::BinaryStreamWriter::writeCString
Error writeCString(StringRef Str)
Write the string Str to the underlying stream followed by a null terminator.
Definition: BinaryStreamWriter.cpp:47
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
llvm::codeview::CompileSym2Flags::EC
@ EC
llvm::codeview::consume
Error consume(BinaryStreamReader &Reader)
Definition: RecordSerialization.h:46
llvm::ArrayRef< uint8_t >
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:339
llvm::BinaryStreamWriter::writeBytes
Error writeBytes(ArrayRef< uint8_t > Buffer)
Write the bytes specified in Buffer to the underlying stream.
Definition: BinaryStreamWriter.cpp:28
llvm::codeview::CodeViewRecordIO::isStreaming
bool isStreaming() const
Definition: CodeViewRecordIO.h:69
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
uint32_t
llvm::codeview::CodeViewRecordIO::mapInteger
Error mapInteger(TypeIndex &TypeInd, const Twine &Comment="")
Definition: CodeViewRecordIO.cpp:127
llvm::codeview::CodeViewRecordIO::maxFieldLength
uint32_t maxFieldLength() const
Definition: CodeViewRecordIO.cpp:57
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::BinaryStreamReader::skip
Error skip(uint32_t Amount)
Advance the stream's offset by Amount bytes.
Definition: BinaryStreamReader.cpp:148
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
llvm::codeview::CodeViewRecordIO::mapEncodedInteger
Error mapEncodedInteger(int64_t &Value, const Twine &Comment="")
Definition: CodeViewRecordIO.cpp:148
uint16_t
llvm::codeview::CodeViewRecordIO::endRecord
Error endRecord()
Definition: CodeViewRecordIO.cpp:26
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:144
llvm::BinaryStreamWriter::padToAlignment
Error padToAlignment(uint32_t Align)
Definition: BinaryStreamWriter.cpp:95
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::codeview::GUID::Guid
uint8_t Guid[16]
Definition: GUID.h:22
llvm::codeview::TypeIndex::getIndex
uint32_t getIndex() const
Definition: TypeIndex.h:110
llvm::codeview
Definition: AppendingTypeTableBuilder.h:23
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:458
llvm::codeview::CodeViewRecordStreamer::emitIntValue
virtual void emitIntValue(uint64_t Value, unsigned Size)=0
llvm::codeview::CodeViewRecordIO::isReading
bool isReading() const
Definition: CodeViewRecordIO.h:72
N
#define N
llvm::BinaryStreamReader::peek
uint8_t peek() const
Examine the next byte of the underlying stream without advancing the stream's offset.
Definition: BinaryStreamReader.cpp:160
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:156
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:350
llvm::codeview::TypeIndex
A 32-bit type reference.
Definition: TypeIndex.h:95
llvm::codeview::CodeViewRecordIO::mapGuid
Error mapGuid(GUID &Guid, const Twine &Comment="")
Definition: CodeViewRecordIO.cpp:222
BinaryStreamWriter.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::codeview::CodeViewRecordStreamer::emitBytes
virtual void emitBytes(StringRef Data)=0
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:145