1 //===- CodeViewRecordIO.cpp -------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h"
10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/DebugInfo/CodeView/CodeView.h"
12 #include "llvm/DebugInfo/CodeView/GUID.h"
13 #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
14 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
15 #include "llvm/Support/BinaryStreamReader.h"
16 #include "llvm/Support/BinaryStreamWriter.h"
19 using namespace llvm::codeview
;
21 Error
CodeViewRecordIO::beginRecord(std::optional
<uint32_t> MaxLength
) {
23 Limit
.MaxLength
= MaxLength
;
24 Limit
.BeginOffset
= getCurrentOffset();
25 Limits
.push_back(Limit
);
26 return Error::success();
29 Error
CodeViewRecordIO::endRecord() {
30 assert(!Limits
.empty() && "Not in a record!");
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.
42 // For streaming mode, add padding to align with 4 byte boundaries for each
44 uint32_t Align
= getStreamedLen() % 4;
46 return Error::success();
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
);
57 return Error::success();
60 uint32_t CodeViewRecordIO::maxFieldLength() const {
64 assert(!Limits
.empty() && "Not in a record!");
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
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
);
75 Min
= Min
? std::min(*Min
, *ThisMin
) : *ThisMin
;
77 assert(Min
&& "Every field must have a maximum length!");
82 Error
CodeViewRecordIO::padToAlignment(uint32_t Align
) {
84 return Reader
->padToAlignment(Align
);
85 return Writer
->padToAlignment(Align
);
88 Error
CodeViewRecordIO::skipPadding() {
89 assert(!isWriting() && "Cannot skip padding while writing!");
91 if (Reader
->bytesRemaining() == 0)
92 return Error::success();
94 uint8_t Leaf
= Reader
->peek();
96 return Error::success();
97 // Leaf is greater than 0xf0. We should advance by the number of bytes in
99 unsigned BytesToAdvance
= Leaf
& 0x0F;
100 return Reader
->skip(BytesToAdvance
);
103 Error
CodeViewRecordIO::mapByteVectorTail(ArrayRef
<uint8_t> &Bytes
,
104 const Twine
&Comment
) {
106 emitComment(Comment
);
107 Streamer
->emitBinaryData(toStringRef(Bytes
));
108 incrStreamedLen(Bytes
.size());
109 } else if (isWriting()) {
110 if (auto EC
= Writer
->writeBytes(Bytes
))
113 if (auto EC
= Reader
->readBytes(Bytes
, Reader
->bytesRemaining()))
116 return Error::success();
119 Error
CodeViewRecordIO::mapByteVectorTail(std::vector
<uint8_t> &Bytes
,
120 const Twine
&Comment
) {
121 ArrayRef
<uint8_t> BytesRef(Bytes
);
122 if (auto EC
= mapByteVectorTail(BytesRef
, Comment
))
125 Bytes
.assign(BytesRef
.begin(), BytesRef
.end());
127 return Error::success();
130 Error
CodeViewRecordIO::mapInteger(TypeIndex
&TypeInd
, const Twine
&Comment
) {
132 std::string TypeNameStr
= Streamer
->getTypeName(TypeInd
);
133 if (!TypeNameStr
.empty())
134 emitComment(Comment
+ ": " + TypeNameStr
);
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()))
144 if (auto EC
= Reader
->readInteger(I
))
148 return Error::success();
151 Error
CodeViewRecordIO::mapEncodedInteger(int64_t &Value
,
152 const Twine
&Comment
) {
155 emitEncodedUnsignedInteger(static_cast<uint64_t>(Value
), Comment
);
157 emitEncodedSignedInteger(Value
, Comment
);
158 } else if (isWriting()) {
160 if (auto EC
= writeEncodedUnsignedInteger(static_cast<uint64_t>(Value
)))
163 if (auto EC
= writeEncodedSignedInteger(Value
))
168 if (auto EC
= consume(*Reader
, N
))
170 Value
= N
.getExtValue();
173 return Error::success();
176 Error
CodeViewRecordIO::mapEncodedInteger(uint64_t &Value
,
177 const Twine
&Comment
) {
179 emitEncodedUnsignedInteger(Value
, Comment
);
180 else if (isWriting()) {
181 if (auto EC
= writeEncodedUnsignedInteger(Value
))
185 if (auto EC
= consume(*Reader
, N
))
187 Value
= N
.getZExtValue();
189 return Error::success();
192 Error
CodeViewRecordIO::mapEncodedInteger(APSInt
&Value
, const Twine
&Comment
) {
194 // FIXME: We also need to handle big values here, but it's
195 // not clear how we can excercise this code path yet.
196 if (Value
.isSigned())
197 emitEncodedSignedInteger(Value
.getSExtValue(), Comment
);
199 emitEncodedUnsignedInteger(Value
.getZExtValue(), Comment
);
200 } else if (isWriting()) {
201 if (Value
.isSigned())
202 return writeEncodedSignedInteger(
203 Value
.isSingleWord() ? Value
.getSExtValue() : INT64_MIN
);
204 return writeEncodedUnsignedInteger(Value
.getLimitedValue());
206 return consume(*Reader
, Value
);
207 return Error::success();
210 Error
CodeViewRecordIO::mapStringZ(StringRef
&Value
, const Twine
&Comment
) {
212 auto NullTerminatedString
= StringRef(Value
.data(), Value
.size() + 1);
213 emitComment(Comment
);
214 Streamer
->emitBytes(NullTerminatedString
);
215 incrStreamedLen(NullTerminatedString
.size());
216 } else if (isWriting()) {
217 // Truncate if we attempt to write too much.
218 StringRef S
= Value
.take_front(maxFieldLength() - 1);
219 if (auto EC
= Writer
->writeCString(S
))
222 if (auto EC
= Reader
->readCString(Value
))
225 return Error::success();
228 Error
CodeViewRecordIO::mapGuid(GUID
&Guid
, const Twine
&Comment
) {
229 constexpr uint32_t GuidSize
= 16;
233 StringRef((reinterpret_cast<const char *>(&Guid
)), GuidSize
);
234 emitComment(Comment
);
235 Streamer
->emitBytes(GuidSR
);
236 incrStreamedLen(GuidSize
);
237 return Error::success();
240 if (maxFieldLength() < GuidSize
)
241 return make_error
<CodeViewError
>(cv_error_code::insufficient_buffer
);
244 if (auto EC
= Writer
->writeBytes(Guid
.Guid
))
247 ArrayRef
<uint8_t> GuidBytes
;
248 if (auto EC
= Reader
->readBytes(GuidBytes
, GuidSize
))
250 memcpy(Guid
.Guid
, GuidBytes
.data(), GuidSize
);
252 return Error::success();
255 Error
CodeViewRecordIO::mapStringZVectorZ(std::vector
<StringRef
> &Value
,
256 const Twine
&Comment
) {
259 emitComment(Comment
);
260 for (auto V
: Value
) {
261 if (auto EC
= mapStringZ(V
))
264 uint8_t FinalZero
= 0;
265 if (auto EC
= mapInteger(FinalZero
))
269 if (auto EC
= mapStringZ(S
))
273 if (auto EC
= mapStringZ(S
))
277 return Error::success();
280 void CodeViewRecordIO::emitEncodedSignedInteger(const int64_t &Value
,
281 const Twine
&Comment
) {
282 // FIXME: There are no test cases covering this function.
283 // This may be because we always consider enumerators to be unsigned.
284 // See FIXME at CodeViewDebug.cpp : CodeViewDebug::lowerTypeEnum.
285 if (Value
< LF_NUMERIC
&& Value
>= 0) {
286 emitComment(Comment
);
287 Streamer
->emitIntValue(Value
, 2);
289 } else if (Value
>= std::numeric_limits
<int8_t>::min() &&
290 Value
<= std::numeric_limits
<int8_t>::max()) {
291 Streamer
->emitIntValue(LF_CHAR
, 2);
292 emitComment(Comment
);
293 Streamer
->emitIntValue(Value
, 1);
295 } else if (Value
>= std::numeric_limits
<int16_t>::min() &&
296 Value
<= std::numeric_limits
<int16_t>::max()) {
297 Streamer
->emitIntValue(LF_SHORT
, 2);
298 emitComment(Comment
);
299 Streamer
->emitIntValue(Value
, 2);
301 } else if (Value
>= std::numeric_limits
<int32_t>::min() &&
302 Value
<= std::numeric_limits
<int32_t>::max()) {
303 Streamer
->emitIntValue(LF_LONG
, 2);
304 emitComment(Comment
);
305 Streamer
->emitIntValue(Value
, 4);
308 Streamer
->emitIntValue(LF_QUADWORD
, 2);
309 emitComment(Comment
);
310 Streamer
->emitIntValue(Value
, 4); // FIXME: Why not 8 (size of quadword)?
311 incrStreamedLen(6); // FIXME: Why not 10 (8 + 2)?
315 void CodeViewRecordIO::emitEncodedUnsignedInteger(const uint64_t &Value
,
316 const Twine
&Comment
) {
317 if (Value
< LF_NUMERIC
) {
318 emitComment(Comment
);
319 Streamer
->emitIntValue(Value
, 2);
321 } else if (Value
<= std::numeric_limits
<uint16_t>::max()) {
322 Streamer
->emitIntValue(LF_USHORT
, 2);
323 emitComment(Comment
);
324 Streamer
->emitIntValue(Value
, 2);
326 } else if (Value
<= std::numeric_limits
<uint32_t>::max()) {
327 Streamer
->emitIntValue(LF_ULONG
, 2);
328 emitComment(Comment
);
329 Streamer
->emitIntValue(Value
, 4);
332 // FIXME: There are no test cases covering this block.
333 Streamer
->emitIntValue(LF_UQUADWORD
, 2);
334 emitComment(Comment
);
335 Streamer
->emitIntValue(Value
, 8);
336 incrStreamedLen(6); // FIXME: Why not 10 (8 + 2)?
340 Error
CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value
) {
341 if (Value
< LF_NUMERIC
&& Value
>= 0) {
342 if (auto EC
= Writer
->writeInteger
<int16_t>(Value
))
344 } else if (Value
>= std::numeric_limits
<int8_t>::min() &&
345 Value
<= std::numeric_limits
<int8_t>::max()) {
346 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_CHAR
))
348 if (auto EC
= Writer
->writeInteger
<int8_t>(Value
))
350 } else if (Value
>= std::numeric_limits
<int16_t>::min() &&
351 Value
<= std::numeric_limits
<int16_t>::max()) {
352 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_SHORT
))
354 if (auto EC
= Writer
->writeInteger
<int16_t>(Value
))
356 } else if (Value
>= std::numeric_limits
<int32_t>::min() &&
357 Value
<= std::numeric_limits
<int32_t>::max()) {
358 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_LONG
))
360 if (auto EC
= Writer
->writeInteger
<int32_t>(Value
))
363 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_QUADWORD
))
365 if (auto EC
= Writer
->writeInteger(Value
))
368 return Error::success();
371 Error
CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value
) {
372 if (Value
< LF_NUMERIC
) {
373 if (auto EC
= Writer
->writeInteger
<uint16_t>(Value
))
375 } else if (Value
<= std::numeric_limits
<uint16_t>::max()) {
376 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_USHORT
))
378 if (auto EC
= Writer
->writeInteger
<uint16_t>(Value
))
380 } else if (Value
<= std::numeric_limits
<uint32_t>::max()) {
381 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_ULONG
))
383 if (auto EC
= Writer
->writeInteger
<uint32_t>(Value
))
386 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_UQUADWORD
))
388 if (auto EC
= Writer
->writeInteger(Value
))
392 return Error::success();