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