LLVM 24.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
17
18using namespace llvm;
19using namespace llvm::codeview;
20
21Error CodeViewRecordIO::beginRecord(std::optional<uint32_t> MaxLength) {
22 RecordLimit Limit;
23 Limit.MaxLength = MaxLength;
24 Limit.BeginOffset = getCurrentOffset();
25 Limits.push_back(Limit);
26 return Error::success();
27}
28
30 assert(!Limits.empty() && "Not in a record!");
31 Limits.pop_back();
32 // We would like to assert that we actually read / wrote all the bytes that we
33 // expected to for this record, but unfortunately we can't do this. Some
34 // producers such as MASM over-allocate for certain types of records and
35 // commit the extraneous data, so when reading we can't be sure every byte
36 // will have been read. And when writing we over-allocate temporarily since
37 // we don't know how big the record is until we're finished writing it, so
38 // even though we don't commit the extraneous data, we still can't guarantee
39 // we're at the end of the allocated data.
40
41 if (isStreaming()) {
42 // For streaming mode, add padding to align with 4 byte boundaries for each
43 // record
45 if (Align == 0)
46 return Error::success();
47
48 int PaddingBytes = 4 - Align;
49 while (PaddingBytes > 0) {
50 char Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
51 StringRef BytesSR = StringRef(&Pad, sizeof(Pad));
52 Streamer->emitBytes(BytesSR);
53 --PaddingBytes;
54 }
55 resetStreamedLen();
56 }
57 return Error::success();
58}
59
61 if (isStreaming())
62 return 0;
63
64 assert(!Limits.empty() && "Not in a record!");
65
66 // The max length of the next field is the minimum of all lengths that would
67 // be allowed by any of the sub-records we're in. In practice, we can only
68 // ever be at most 1 sub-record deep (in a FieldList), but this works for
69 // the general case.
70 uint32_t Offset = getCurrentOffset();
71 std::optional<uint32_t> Min = Limits.front().bytesRemaining(Offset);
72 for (auto X : ArrayRef(Limits).drop_front()) {
73 std::optional<uint32_t> ThisMin = X.bytesRemaining(Offset);
74 if (ThisMin)
75 Min = Min ? std::min(*Min, *ThisMin) : *ThisMin;
76 }
77 assert(Min && "Every field must have a maximum length!");
78
79 return *Min;
80}
81
83 if (isReading())
84 return Reader->padToAlignment(Align);
85 return Writer->padToAlignment(Align);
86}
87
89 assert(!isWriting() && "Cannot skip padding while writing!");
90
91 if (Reader->bytesRemaining() == 0)
92 return Error::success();
93
94 uint8_t Leaf = Reader->peek();
95 if (Leaf < LF_PAD0)
96 return Error::success();
97 // Leaf is greater than 0xf0. We should advance by the number of bytes in
98 // the low 4 bits.
99 unsigned BytesToAdvance = Leaf & 0x0F;
100 return Reader->skip(BytesToAdvance);
101}
102
104 const Twine &Comment) {
105 if (isStreaming()) {
106 emitComment(Comment);
107 Streamer->emitBinaryData(toStringRef(Bytes));
108 incrStreamedLen(Bytes.size());
109 } else if (isWriting()) {
110 if (auto EC = Writer->writeBytes(Bytes))
111 return EC;
112 } else {
113 if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
114 return EC;
115 }
116 return Error::success();
117}
118
119Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes,
120 const Twine &Comment) {
121 ArrayRef<uint8_t> BytesRef(Bytes);
122 if (auto EC = mapByteVectorTail(BytesRef, Comment))
123 return EC;
124 if (!isWriting())
125 Bytes.assign(BytesRef.begin(), BytesRef.end());
126
127 return Error::success();
128}
129
131 if (isStreaming()) {
132 std::string TypeNameStr = Streamer->getTypeName(TypeInd);
133 if (!TypeNameStr.empty())
134 emitComment(Comment + ": " + TypeNameStr);
135 else
136 emitComment(Comment);
137 Streamer->emitIntValue(TypeInd.getIndex(), sizeof(TypeInd.getIndex()));
138 incrStreamedLen(sizeof(TypeInd.getIndex()));
139 } else if (isWriting()) {
140 if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
141 return EC;
142 } else {
143 uint32_t I;
144 if (auto EC = Reader->readInteger(I))
145 return EC;
146 TypeInd.setIndex(I);
147 }
148 return Error::success();
149}
150
152 const Twine &Comment) {
153 APSInt V = Value.extOrTrunc(128);
154 if (isStreaming()) {
155 emitComment(Comment);
156 Streamer->emitAPSIntValue(V);
157 incrStreamedLen(16);
158 return Error::success();
159 }
160
161 return Writer->writeInt128(V);
162}
163
165 const Twine &Comment) {
166 if (isReading()) {
167 APSInt N;
168 if (auto EC = consume(*Reader, N))
169 return EC;
170 Value = N.getExtValue();
171 return Error::success();
172 }
173
174 if (Value >= 0)
175 return mapWriteEncodedUnsignedInteger(APSInt(APInt(64, Value), true),
176 Comment);
177
178 return mapWriteEncodedSignedInteger(APSInt(APInt(64, Value, true), false),
179 Comment);
180}
181
183 const Twine &Comment) {
184 if (isReading()) {
185 APSInt N;
186 if (auto EC = consume(*Reader, N))
187 return EC;
188 Value = N.getZExtValue();
189 return Error::success();
190 }
191 return mapWriteEncodedUnsignedInteger(APSInt(APInt(64, Value), true),
192 Comment);
193}
194
196 if (isReading())
197 return consume(*Reader, Value);
198
199 if (Value.isUnsigned())
200 return mapWriteEncodedUnsignedInteger(Value, Comment);
201
202 return mapWriteEncodedSignedInteger(Value, Comment);
203}
204
206 if (isStreaming()) {
207 assert(Value.data()[Value.size()] == '\0' && "Expected null terminator.");
208 auto NullTerminatedString = StringRef(Value.data(), Value.size() + 1);
209 emitComment(Comment);
210 Streamer->emitBytes(NullTerminatedString);
211 incrStreamedLen(NullTerminatedString.size());
212 } else if (isWriting()) {
213 // Truncate if we attempt to write too much.
214 StringRef S = Value.take_front(maxFieldLength() - 1);
215 if (auto EC = Writer->writeCString(S))
216 return EC;
217 } else {
218 if (auto EC = Reader->readCString(Value))
219 return EC;
220 }
221 return Error::success();
222}
223
225 constexpr uint32_t GuidSize = 16;
226
227 if (isStreaming()) {
228 StringRef GuidSR =
229 StringRef((reinterpret_cast<const char *>(&Guid)), GuidSize);
230 emitComment(Comment);
231 Streamer->emitBytes(GuidSR);
232 incrStreamedLen(GuidSize);
233 return Error::success();
234 }
235
236 if (maxFieldLength() < GuidSize)
238
239 if (isWriting()) {
240 if (auto EC = Writer->writeBytes(Guid.Guid))
241 return EC;
242 } else {
243 ArrayRef<uint8_t> GuidBytes;
244 if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
245 return EC;
246 memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
247 }
248 return Error::success();
249}
250
252 const Twine &Comment) {
253
254 if (!isReading()) {
255 emitComment(Comment);
256 for (auto V : Value) {
257 if (auto EC = mapStringZ(V))
258 return EC;
259 }
260 uint8_t FinalZero = 0;
261 if (auto EC = mapInteger(FinalZero))
262 return EC;
263 } else {
264 StringRef S;
265 if (auto EC = mapStringZ(S))
266 return EC;
267 while (!S.empty()) {
268 Value.push_back(S);
269 if (auto EC = mapStringZ(S))
270 return EC;
271 };
272 }
273 return Error::success();
274}
275
276Error CodeViewRecordIO::mapWriteEncodedSignedInteger(const APSInt &APValue,
277 const Twine &Comment) {
278 assert(APValue.isSigned());
279
280 if (LLVM_UNLIKELY(!APValue.isSingleWord())) {
281 // All values >64 bit are encoded as 128 bit integers (possibly truncated).
282 if (Error Err = mapWriteInteger<uint16_t>(LF_OCTWORD))
283 return Err;
284 return mapWriteInt128(APValue, Comment);
285 }
286
287 int64_t Value = APValue.getSExtValue();
288
290 return mapWriteInteger(static_cast<uint16_t>(Value), Comment);
291
292 if (Value >= std::numeric_limits<int8_t>::min() &&
293 Value <= std::numeric_limits<int8_t>::max()) {
294 if (Error Err = mapWriteInteger<uint16_t>(LF_CHAR))
295 return Err;
296 return mapWriteInteger(static_cast<int8_t>(Value), Comment);
297 }
298
299 if (Value >= std::numeric_limits<int16_t>::min() &&
300 Value <= std::numeric_limits<int16_t>::max()) {
301 if (Error Err = mapWriteInteger<uint16_t>(LF_SHORT))
302 return Err;
303 return mapWriteInteger(static_cast<int16_t>(Value), Comment);
304 }
305
306 if (Value >= std::numeric_limits<int32_t>::min() &&
307 Value <= std::numeric_limits<int32_t>::max()) {
308 if (Error Err = mapWriteInteger<uint16_t>(LF_LONG))
309 return Err;
310 return mapWriteInteger(static_cast<int32_t>(Value), Comment);
311 }
312
313 if (Error Err = mapWriteInteger<uint16_t>(LF_QUADWORD))
314 return Err;
315 return mapWriteInteger(Value, Comment);
316}
317
318Error CodeViewRecordIO::mapWriteEncodedUnsignedInteger(const APSInt &APValue,
319 const Twine &Comment) {
320 assert(APValue.isUnsigned());
321
322 if (LLVM_UNLIKELY(APValue.getActiveBits() > 64)) {
323 // All values >64 bit are encoded as 128 bit integers (possibly truncated).
324 if (Error Err = mapWriteInteger<uint16_t>(LF_UOCTWORD))
325 return Err;
326 return mapWriteInt128(APValue, Comment);
327 }
328
329 uint64_t Value = APValue.getZExtValue();
330
331 if (Value < LF_NUMERIC)
332 return mapWriteInteger(static_cast<uint16_t>(Value), Comment);
333
334 if (Value <= std::numeric_limits<uint16_t>::max()) {
335 if (Error Err = mapWriteInteger<uint16_t>(LF_USHORT))
336 return Err;
337 return mapWriteInteger(static_cast<uint16_t>(Value), Comment);
338 }
339
340 if (Value <= std::numeric_limits<uint32_t>::max()) {
341 if (Error Err = mapWriteInteger<uint16_t>(LF_ULONG))
342 return Err;
343 return mapWriteInteger(static_cast<uint32_t>(Value), Comment);
344 }
345
346 if (Error Err = mapWriteInteger<uint16_t>(LF_UQUADWORD))
347 return Err;
348 return mapWriteInteger(Value, Comment);
349}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:348
#define I(x, y, z)
Definition MD5.cpp:57
This file contains some functions that are useful when dealing with strings.
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1532
bool isSingleWord() const
Determine if this APInt just has one word to store value.
Definition APInt.h:318
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1582
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
bool isUnsigned() const
Definition APSInt.h:79
bool isSigned() const
Definition APSInt.h:78
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
const T * data() const
Definition ArrayRef.h:138
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI Error padToAlignment(uint32_t Align)
LLVM_ABI Error mapInteger(TypeIndex &TypeInd, const Twine &Comment="")
LLVM_ABI Error mapGuid(GUID &Guid, const Twine &Comment="")
LLVM_ABI Error mapStringZVectorZ(std::vector< StringRef > &Value, const Twine &Comment="")
LLVM_ABI Error mapStringZ(StringRef &Value, const Twine &Comment="")
LLVM_ABI Error mapWriteInt128(const APSInt &Value, const Twine &Comment="")
Error mapWriteInteger(const T &Value, const Twine &Comment="")
LLVM_ABI Error mapEncodedInteger(int64_t &Value, const Twine &Comment="")
LLVM_ABI Error beginRecord(std::optional< uint32_t > MaxLength)
LLVM_ABI Error mapByteVectorTail(ArrayRef< uint8_t > &Bytes, const Twine &Comment="")
LLVM_ABI uint32_t maxFieldLength() const
A 32-bit type reference.
Definition TypeIndex.h:97
void setIndex(uint32_t I)
Definition TypeIndex.h:113
uint32_t getIndex() const
Definition TypeIndex.h:112
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
ArrayRef(const T &OneElt) -> ArrayRef< T >
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
This represents the 'GUID' type from windows.h.
Definition GUID.h:22