CodeViewRecordIO.cpp
11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//===- CodeViewRecordIO.cpp -------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h"
#include "llvm/DebugInfo/CodeView/CodeView.h"
#include "llvm/DebugInfo/CodeView/RecordSerialization.h"
#include "llvm/Support/BinaryStreamReader.h"
#include "llvm/Support/BinaryStreamWriter.h"
using namespace llvm;
using namespace llvm::codeview;
Error CodeViewRecordIO::beginRecord(Optional<uint32_t> MaxLength) {
RecordLimit Limit;
Limit.MaxLength = MaxLength;
Limit.BeginOffset = getCurrentOffset();
Limits.push_back(Limit);
return Error::success();
}
Error CodeViewRecordIO::endRecord() {
assert(!Limits.empty() && "Not in a record!");
Limits.pop_back();
// We would like to assert that we actually read / wrote all the bytes that we
// expected to for this record, but unfortunately we can't do this. Some
// producers such as MASM over-allocate for certain types of records and
// commit the extraneous data, so when reading we can't be sure every byte
// will have been read. And when writing we over-allocate temporarily since
// we don't know how big the record is until we're finished writing it, so
// even though we don't commit the extraneous data, we still can't guarantee
// we're at the end of the allocated data.
if (isStreaming()) {
// For streaming mode, add padding to align with 4 byte boundaries for each
// record
uint32_t Align = getStreamedLen() % 4;
if (Align == 0)
return Error::success();
int PaddingBytes = 4 - Align;
while (PaddingBytes > 0) {
char Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
StringRef BytesSR = StringRef(&Pad, sizeof(Pad));
Streamer->emitBytes(BytesSR);
--PaddingBytes;
}
resetStreamedLen();
}
return Error::success();
}
uint32_t CodeViewRecordIO::maxFieldLength() const {
if (isStreaming())
return 0;
assert(!Limits.empty() && "Not in a record!");
// The max length of the next field is the minimum of all lengths that would
// be allowed by any of the sub-records we're in. In practice, we can only
// ever be at most 1 sub-record deep (in a FieldList), but this works for
// the general case.
uint32_t Offset = getCurrentOffset();
Optional<uint32_t> Min = Limits.front().bytesRemaining(Offset);
for (auto X : makeArrayRef(Limits).drop_front()) {
Optional<uint32_t> ThisMin = X.bytesRemaining(Offset);
if (ThisMin.hasValue())
Min = (Min.hasValue()) ? std::min(*Min, *ThisMin) : *ThisMin;
}
assert(Min.hasValue() && "Every field must have a maximum length!");
return *Min;
}
Error CodeViewRecordIO::padToAlignment(uint32_t Align) {
if (isReading())
return Reader->padToAlignment(Align);
return Writer->padToAlignment(Align);
}
Error CodeViewRecordIO::skipPadding() {
assert(!isWriting() && "Cannot skip padding while writing!");
if (Reader->bytesRemaining() == 0)
return Error::success();
uint8_t Leaf = Reader->peek();
if (Leaf < LF_PAD0)
return Error::success();
// Leaf is greater than 0xf0. We should advance by the number of bytes in
// the low 4 bits.
unsigned BytesToAdvance = Leaf & 0x0F;
return Reader->skip(BytesToAdvance);
}
Error CodeViewRecordIO::mapByteVectorTail(ArrayRef<uint8_t> &Bytes,
const Twine &Comment) {
if (isStreaming()) {
emitComment(Comment);
Streamer->emitBinaryData(toStringRef(Bytes));
incrStreamedLen(Bytes.size());
} else if (isWriting()) {
if (auto EC = Writer->writeBytes(Bytes))
return EC;
} else {
if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
return EC;
}
return Error::success();
}
Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes,
const Twine &Comment) {
ArrayRef<uint8_t> BytesRef(Bytes);
if (auto EC = mapByteVectorTail(BytesRef, Comment))
return EC;
if (!isWriting())
Bytes.assign(BytesRef.begin(), BytesRef.end());
return Error::success();
}
Error CodeViewRecordIO::mapInteger(TypeIndex &TypeInd, const Twine &Comment) {
if (isStreaming()) {
std::string TypeNameStr = Streamer->getTypeName(TypeInd);
if (!TypeNameStr.empty())
emitComment(Comment + ": " + TypeNameStr);
else
emitComment(Comment);
Streamer->emitIntValue(TypeInd.getIndex(), sizeof(TypeInd.getIndex()));
incrStreamedLen(sizeof(TypeInd.getIndex()));
} else if (isWriting()) {
if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
return EC;
} else {
uint32_t I;
if (auto EC = Reader->readInteger(I))
return EC;
TypeInd.setIndex(I);
}
return Error::success();
}
Error CodeViewRecordIO::mapEncodedInteger(int64_t &Value,
const Twine &Comment) {
if (isStreaming()) {
if (Value >= 0)
emitEncodedUnsignedInteger(static_cast<uint64_t>(Value), Comment);
else
emitEncodedSignedInteger(Value, Comment);
} else if (isWriting()) {
if (Value >= 0) {
if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
return EC;
} else {
if (auto EC = writeEncodedSignedInteger(Value))
return EC;
}
} else {
APSInt N;
if (auto EC = consume(*Reader, N))
return EC;
Value = N.getExtValue();
}
return Error::success();
}
Error CodeViewRecordIO::mapEncodedInteger(uint64_t &Value,
const Twine &Comment) {
if (isStreaming())
emitEncodedUnsignedInteger(Value, Comment);
else if (isWriting()) {
if (auto EC = writeEncodedUnsignedInteger(Value))
return EC;
} else {
APSInt N;
if (auto EC = consume(*Reader, N))
return EC;
Value = N.getZExtValue();
}
return Error::success();
}
Error CodeViewRecordIO::mapEncodedInteger(APSInt &Value, const Twine &Comment) {
if (isStreaming()) {
if (Value.isSigned())
emitEncodedSignedInteger(Value.getSExtValue(), Comment);
else
emitEncodedUnsignedInteger(Value.getZExtValue(), Comment);
} else if (isWriting()) {
if (Value.isSigned())
return writeEncodedSignedInteger(Value.getSExtValue());
return writeEncodedUnsignedInteger(Value.getZExtValue());
} else
return consume(*Reader, Value);
return Error::success();
}
Error CodeViewRecordIO::mapStringZ(StringRef &Value, const Twine &Comment) {
if (isStreaming()) {
auto NullTerminatedString = StringRef(Value.data(), Value.size() + 1);
emitComment(Comment);
Streamer->emitBytes(NullTerminatedString);
incrStreamedLen(NullTerminatedString.size());
} else if (isWriting()) {
// Truncate if we attempt to write too much.
StringRef S = Value.take_front(maxFieldLength() - 1);
if (auto EC = Writer->writeCString(S))
return EC;
} else {
if (auto EC = Reader->readCString(Value))
return EC;
}
return Error::success();
}
Error CodeViewRecordIO::mapGuid(GUID &Guid, const Twine &Comment) {
constexpr uint32_t GuidSize = 16;
if (isStreaming()) {
StringRef GuidSR =
StringRef((reinterpret_cast<const char *>(&Guid)), GuidSize);
emitComment(Comment);
Streamer->emitBytes(GuidSR);
incrStreamedLen(GuidSize);
return Error::success();
}
if (maxFieldLength() < GuidSize)
return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
if (isWriting()) {
if (auto EC = Writer->writeBytes(Guid.Guid))
return EC;
} else {
ArrayRef<uint8_t> GuidBytes;
if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
return EC;
memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
}
return Error::success();
}
Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value,
const Twine &Comment) {
if (!isReading()) {
emitComment(Comment);
for (auto V : Value) {
if (auto EC = mapStringZ(V))
return EC;
}
uint8_t FinalZero = 0;
if (auto EC = mapInteger(FinalZero))
return EC;
} else {
StringRef S;
if (auto EC = mapStringZ(S))
return EC;
while (!S.empty()) {
Value.push_back(S);
if (auto EC = mapStringZ(S))
return EC;
};
}
return Error::success();
}
void CodeViewRecordIO::emitEncodedSignedInteger(const int64_t &Value,
const Twine &Comment) {
assert(Value < 0 && "Encoded integer is not signed!");
if (Value >= std::numeric_limits<int8_t>::min()) {
Streamer->emitIntValue(LF_CHAR, 2);
emitComment(Comment);
Streamer->emitIntValue(Value, 1);
incrStreamedLen(3);
} else if (Value >= std::numeric_limits<int16_t>::min()) {
Streamer->emitIntValue(LF_SHORT, 2);
emitComment(Comment);
Streamer->emitIntValue(Value, 2);
incrStreamedLen(4);
} else if (Value >= std::numeric_limits<int32_t>::min()) {
Streamer->emitIntValue(LF_LONG, 2);
emitComment(Comment);
Streamer->emitIntValue(Value, 4);
incrStreamedLen(6);
} else {
Streamer->emitIntValue(LF_QUADWORD, 2);
emitComment(Comment);
Streamer->emitIntValue(Value, 4);
incrStreamedLen(6);
}
}
void CodeViewRecordIO::emitEncodedUnsignedInteger(const uint64_t &Value,
const Twine &Comment) {
if (Value < LF_NUMERIC) {
emitComment(Comment);
Streamer->emitIntValue(Value, 2);
incrStreamedLen(2);
} else if (Value <= std::numeric_limits<uint16_t>::max()) {
Streamer->emitIntValue(LF_USHORT, 2);
emitComment(Comment);
Streamer->emitIntValue(Value, 2);
incrStreamedLen(4);
} else if (Value <= std::numeric_limits<uint32_t>::max()) {
Streamer->emitIntValue(LF_ULONG, 2);
emitComment(Comment);
Streamer->emitIntValue(Value, 4);
incrStreamedLen(6);
} else {
Streamer->emitIntValue(LF_UQUADWORD, 2);
emitComment(Comment);
Streamer->emitIntValue(Value, 8);
incrStreamedLen(6);
}
}
Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
assert(Value < 0 && "Encoded integer is not signed!");
if (Value >= std::numeric_limits<int8_t>::min()) {
if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
return EC;
if (auto EC = Writer->writeInteger<int8_t>(Value))
return EC;
} else if (Value >= std::numeric_limits<int16_t>::min()) {
if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
return EC;
if (auto EC = Writer->writeInteger<int16_t>(Value))
return EC;
} else if (Value >= std::numeric_limits<int32_t>::min()) {
if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
return EC;
if (auto EC = Writer->writeInteger<int32_t>(Value))
return EC;
} else {
if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
return EC;
if (auto EC = Writer->writeInteger(Value))
return EC;
}
return Error::success();
}
Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
if (Value < LF_NUMERIC) {
if (auto EC = Writer->writeInteger<uint16_t>(Value))
return EC;
} else if (Value <= std::numeric_limits<uint16_t>::max()) {
if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
return EC;
if (auto EC = Writer->writeInteger<uint16_t>(Value))
return EC;
} else if (Value <= std::numeric_limits<uint32_t>::max()) {
if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
return EC;
if (auto EC = Writer->writeInteger<uint32_t>(Value))
return EC;
} else {
if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
return EC;
if (auto EC = Writer->writeInteger(Value))
return EC;
}
return Error::success();
}