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/DebugInfo/CodeView/CodeView.h"
11 #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
12 #include "llvm/Support/BinaryStreamReader.h"
13 #include "llvm/Support/BinaryStreamWriter.h"
16 using namespace llvm::codeview
;
18 Error
CodeViewRecordIO::beginRecord(Optional
<uint32_t> MaxLength
) {
20 Limit
.MaxLength
= MaxLength
;
21 Limit
.BeginOffset
= getCurrentOffset();
22 Limits
.push_back(Limit
);
23 return Error::success();
26 Error
CodeViewRecordIO::endRecord() {
27 assert(!Limits
.empty() && "Not in a record!");
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.
39 // For streaming mode, add padding to align with 4 byte boundaries for each
41 uint32_t Align
= getStreamedLen() % 4;
43 return Error::success();
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
);
54 return Error::success();
57 uint32_t CodeViewRecordIO::maxFieldLength() const {
61 assert(!Limits
.empty() && "Not in a record!");
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
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
;
74 assert(Min
.hasValue() && "Every field must have a maximum length!");
79 Error
CodeViewRecordIO::padToAlignment(uint32_t Align
) {
81 return Reader
->padToAlignment(Align
);
82 return Writer
->padToAlignment(Align
);
85 Error
CodeViewRecordIO::skipPadding() {
86 assert(!isWriting() && "Cannot skip padding while writing!");
88 if (Reader
->bytesRemaining() == 0)
89 return Error::success();
91 uint8_t Leaf
= Reader
->peek();
93 return Error::success();
94 // Leaf is greater than 0xf0. We should advance by the number of bytes in
96 unsigned BytesToAdvance
= Leaf
& 0x0F;
97 return Reader
->skip(BytesToAdvance
);
100 Error
CodeViewRecordIO::mapByteVectorTail(ArrayRef
<uint8_t> &Bytes
,
101 const Twine
&Comment
) {
103 emitComment(Comment
);
104 Streamer
->emitBinaryData(toStringRef(Bytes
));
105 incrStreamedLen(Bytes
.size());
106 } else if (isWriting()) {
107 if (auto EC
= Writer
->writeBytes(Bytes
))
110 if (auto EC
= Reader
->readBytes(Bytes
, Reader
->bytesRemaining()))
113 return Error::success();
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
))
122 Bytes
.assign(BytesRef
.begin(), BytesRef
.end());
124 return Error::success();
127 Error
CodeViewRecordIO::mapInteger(TypeIndex
&TypeInd
, const Twine
&Comment
) {
129 std::string TypeNameStr
= Streamer
->getTypeName(TypeInd
);
130 if (!TypeNameStr
.empty())
131 emitComment(Comment
+ ": " + TypeNameStr
);
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()))
141 if (auto EC
= Reader
->readInteger(I
))
145 return Error::success();
148 Error
CodeViewRecordIO::mapEncodedInteger(int64_t &Value
,
149 const Twine
&Comment
) {
152 emitEncodedUnsignedInteger(static_cast<uint64_t>(Value
), Comment
);
154 emitEncodedSignedInteger(Value
, Comment
);
155 } else if (isWriting()) {
157 if (auto EC
= writeEncodedUnsignedInteger(static_cast<uint64_t>(Value
)))
160 if (auto EC
= writeEncodedSignedInteger(Value
))
165 if (auto EC
= consume(*Reader
, N
))
167 Value
= N
.getExtValue();
170 return Error::success();
173 Error
CodeViewRecordIO::mapEncodedInteger(uint64_t &Value
,
174 const Twine
&Comment
) {
176 emitEncodedUnsignedInteger(Value
, Comment
);
177 else if (isWriting()) {
178 if (auto EC
= writeEncodedUnsignedInteger(Value
))
182 if (auto EC
= consume(*Reader
, N
))
184 Value
= N
.getZExtValue();
186 return Error::success();
189 Error
CodeViewRecordIO::mapEncodedInteger(APSInt
&Value
, const Twine
&Comment
) {
191 // FIXME: We also need to handle big values here, but it's
192 // not clear how we can excercise this code path yet.
193 if (Value
.isSigned())
194 emitEncodedSignedInteger(Value
.getSExtValue(), Comment
);
196 emitEncodedUnsignedInteger(Value
.getZExtValue(), Comment
);
197 } else if (isWriting()) {
198 if (Value
.isSigned())
199 return writeEncodedSignedInteger(
200 Value
.isSingleWord() ? Value
.getSExtValue() : INT64_MIN
);
201 return writeEncodedUnsignedInteger(Value
.getLimitedValue());
203 return consume(*Reader
, Value
);
204 return Error::success();
207 Error
CodeViewRecordIO::mapStringZ(StringRef
&Value
, const Twine
&Comment
) {
209 auto NullTerminatedString
= StringRef(Value
.data(), Value
.size() + 1);
210 emitComment(Comment
);
211 Streamer
->emitBytes(NullTerminatedString
);
212 incrStreamedLen(NullTerminatedString
.size());
213 } else if (isWriting()) {
214 // Truncate if we attempt to write too much.
215 StringRef S
= Value
.take_front(maxFieldLength() - 1);
216 if (auto EC
= Writer
->writeCString(S
))
219 if (auto EC
= Reader
->readCString(Value
))
222 return Error::success();
225 Error
CodeViewRecordIO::mapGuid(GUID
&Guid
, const Twine
&Comment
) {
226 constexpr uint32_t GuidSize
= 16;
230 StringRef((reinterpret_cast<const char *>(&Guid
)), GuidSize
);
231 emitComment(Comment
);
232 Streamer
->emitBytes(GuidSR
);
233 incrStreamedLen(GuidSize
);
234 return Error::success();
237 if (maxFieldLength() < GuidSize
)
238 return make_error
<CodeViewError
>(cv_error_code::insufficient_buffer
);
241 if (auto EC
= Writer
->writeBytes(Guid
.Guid
))
244 ArrayRef
<uint8_t> GuidBytes
;
245 if (auto EC
= Reader
->readBytes(GuidBytes
, GuidSize
))
247 memcpy(Guid
.Guid
, GuidBytes
.data(), GuidSize
);
249 return Error::success();
252 Error
CodeViewRecordIO::mapStringZVectorZ(std::vector
<StringRef
> &Value
,
253 const Twine
&Comment
) {
256 emitComment(Comment
);
257 for (auto V
: Value
) {
258 if (auto EC
= mapStringZ(V
))
261 uint8_t FinalZero
= 0;
262 if (auto EC
= mapInteger(FinalZero
))
266 if (auto EC
= mapStringZ(S
))
270 if (auto EC
= mapStringZ(S
))
274 return Error::success();
277 void CodeViewRecordIO::emitEncodedSignedInteger(const int64_t &Value
,
278 const Twine
&Comment
) {
279 // FIXME: There are no test cases covering this function.
280 // This may be because we always consider enumerators to be unsigned.
281 // See FIXME at CodeViewDebug.cpp : CodeViewDebug::lowerTypeEnum.
282 if (Value
>= std::numeric_limits
<int8_t>::min()) {
283 Streamer
->emitIntValue(LF_CHAR
, 2);
284 emitComment(Comment
);
285 Streamer
->emitIntValue(Value
, 1);
287 } else if (Value
>= std::numeric_limits
<int16_t>::min()) {
288 Streamer
->emitIntValue(LF_SHORT
, 2);
289 emitComment(Comment
);
290 Streamer
->emitIntValue(Value
, 2);
292 } else if (Value
>= std::numeric_limits
<int32_t>::min()) {
293 Streamer
->emitIntValue(LF_LONG
, 2);
294 emitComment(Comment
);
295 Streamer
->emitIntValue(Value
, 4);
298 Streamer
->emitIntValue(LF_QUADWORD
, 2);
299 emitComment(Comment
);
300 Streamer
->emitIntValue(Value
, 4); // FIXME: Why not 8 (size of quadword)?
301 incrStreamedLen(6); // FIXME: Why not 10 (8 + 2)?
305 void CodeViewRecordIO::emitEncodedUnsignedInteger(const uint64_t &Value
,
306 const Twine
&Comment
) {
307 if (Value
< LF_NUMERIC
) {
308 emitComment(Comment
);
309 Streamer
->emitIntValue(Value
, 2);
311 } else if (Value
<= std::numeric_limits
<uint16_t>::max()) {
312 Streamer
->emitIntValue(LF_USHORT
, 2);
313 emitComment(Comment
);
314 Streamer
->emitIntValue(Value
, 2);
316 } else if (Value
<= std::numeric_limits
<uint32_t>::max()) {
317 Streamer
->emitIntValue(LF_ULONG
, 2);
318 emitComment(Comment
);
319 Streamer
->emitIntValue(Value
, 4);
322 // FIXME: There are no test cases covering this block.
323 Streamer
->emitIntValue(LF_UQUADWORD
, 2);
324 emitComment(Comment
);
325 Streamer
->emitIntValue(Value
, 8);
326 incrStreamedLen(6); // FIXME: Why not 10 (8 + 2)?
330 Error
CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value
) {
331 if (Value
>= std::numeric_limits
<int8_t>::min()) {
332 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_CHAR
))
334 if (auto EC
= Writer
->writeInteger
<int8_t>(Value
))
336 } else if (Value
>= std::numeric_limits
<int16_t>::min()) {
337 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_SHORT
))
339 if (auto EC
= Writer
->writeInteger
<int16_t>(Value
))
341 } else if (Value
>= std::numeric_limits
<int32_t>::min()) {
342 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_LONG
))
344 if (auto EC
= Writer
->writeInteger
<int32_t>(Value
))
347 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_QUADWORD
))
349 if (auto EC
= Writer
->writeInteger(Value
))
352 return Error::success();
355 Error
CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value
) {
356 if (Value
< LF_NUMERIC
) {
357 if (auto EC
= Writer
->writeInteger
<uint16_t>(Value
))
359 } else if (Value
<= std::numeric_limits
<uint16_t>::max()) {
360 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_USHORT
))
362 if (auto EC
= Writer
->writeInteger
<uint16_t>(Value
))
364 } else if (Value
<= std::numeric_limits
<uint32_t>::max()) {
365 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_ULONG
))
367 if (auto EC
= Writer
->writeInteger
<uint32_t>(Value
))
370 if (auto EC
= Writer
->writeInteger
<uint16_t>(LF_UQUADWORD
))
372 if (auto EC
= Writer
->writeInteger(Value
))
376 return Error::success();