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);
24 return Error::success();
27 Error CodeViewRecordIO::endRecord() {
28 assert(!Limits.empty() && "Not in a record!");
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.
40 // For streaming mode, add padding to align with 4 byte boundaries for each
42 uint32_t Align = getStreamedLen() % 4;
44 return Error::success();
46 int PaddingBytes = 4 - Align;
47 while (PaddingBytes > 0) {
48 char Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
49 StringRef BytesSR = StringRef(&Pad, sizeof(Pad));
50 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 emitComment(Comment);
130 Streamer->EmitIntValue(TypeInd.getIndex(), sizeof(TypeInd.getIndex()));
131 incrStreamedLen(sizeof(TypeInd.getIndex()));
132 } else if (isWriting()) {
133 if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
137 if (auto EC = Reader->readInteger(I))
141 return Error::success();
144 Error CodeViewRecordIO::mapEncodedInteger(int64_t &Value,
145 const Twine &Comment) {
148 emitEncodedUnsignedInteger(static_cast<uint64_t>(Value), Comment);
150 emitEncodedSignedInteger(Value, Comment);
151 } else if (isWriting()) {
153 if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
156 if (auto EC = writeEncodedSignedInteger(Value))
161 if (auto EC = consume(*Reader, N))
163 Value = N.getExtValue();
166 return Error::success();
169 Error CodeViewRecordIO::mapEncodedInteger(uint64_t &Value,
170 const Twine &Comment) {
172 emitEncodedUnsignedInteger(Value, Comment);
173 else if (isWriting()) {
174 if (auto EC = writeEncodedUnsignedInteger(Value))
178 if (auto EC = consume(*Reader, N))
180 Value = N.getZExtValue();
182 return Error::success();
185 Error CodeViewRecordIO::mapEncodedInteger(APSInt &Value, const Twine &Comment) {
187 if (Value.isSigned())
188 emitEncodedSignedInteger(Value.getSExtValue(), Comment);
190 emitEncodedUnsignedInteger(Value.getZExtValue(), Comment);
191 } else if (isWriting()) {
192 if (Value.isSigned())
193 return writeEncodedSignedInteger(Value.getSExtValue());
194 return writeEncodedUnsignedInteger(Value.getZExtValue());
196 return consume(*Reader, Value);
197 return Error::success();
200 Error CodeViewRecordIO::mapStringZ(StringRef &Value, const Twine &Comment) {
202 auto NullTerminatedString = StringRef(Value.data(), Value.size() + 1);
203 emitComment(Comment);
204 Streamer->EmitBytes(NullTerminatedString);
205 incrStreamedLen(NullTerminatedString.size());
206 } else if (isWriting()) {
207 // Truncate if we attempt to write too much.
208 StringRef S = Value.take_front(maxFieldLength() - 1);
209 if (auto EC = Writer->writeCString(S))
212 if (auto EC = Reader->readCString(Value))
215 return Error::success();
218 Error CodeViewRecordIO::mapGuid(GUID &Guid, const Twine &Comment) {
219 constexpr uint32_t GuidSize = 16;
223 StringRef((reinterpret_cast<const char *>(&Guid)), GuidSize);
224 emitComment(Comment);
225 Streamer->EmitBytes(GuidSR);
226 incrStreamedLen(GuidSize);
227 return Error::success();
230 if (maxFieldLength() < GuidSize)
231 return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
234 if (auto EC = Writer->writeBytes(Guid.Guid))
237 ArrayRef<uint8_t> GuidBytes;
238 if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
240 memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
242 return Error::success();
245 Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value,
246 const Twine &Comment) {
249 emitComment(Comment);
250 for (auto V : Value) {
251 if (auto EC = mapStringZ(V))
254 uint8_t FinalZero = 0;
255 if (auto EC = mapInteger(FinalZero))
259 if (auto EC = mapStringZ(S))
263 if (auto EC = mapStringZ(S))
267 return Error::success();
270 void CodeViewRecordIO::emitEncodedSignedInteger(const int64_t &Value,
271 const Twine &Comment) {
272 assert(Value < 0 && "Encoded integer is not signed!");
273 if (Value >= std::numeric_limits<int8_t>::min()) {
274 Streamer->EmitIntValue(LF_CHAR, 2);
275 emitComment(Comment);
276 Streamer->EmitIntValue(Value, 1);
278 } else if (Value >= std::numeric_limits<int16_t>::min()) {
279 Streamer->EmitIntValue(LF_SHORT, 2);
280 emitComment(Comment);
281 Streamer->EmitIntValue(Value, 2);
283 } else if (Value >= std::numeric_limits<int32_t>::min()) {
284 Streamer->EmitIntValue(LF_LONG, 2);
285 emitComment(Comment);
286 Streamer->EmitIntValue(Value, 4);
289 Streamer->EmitIntValue(LF_QUADWORD, 2);
290 emitComment(Comment);
291 Streamer->EmitIntValue(Value, 4);
296 void CodeViewRecordIO::emitEncodedUnsignedInteger(const uint64_t &Value,
297 const Twine &Comment) {
298 if (Value < LF_NUMERIC) {
299 emitComment(Comment);
300 Streamer->EmitIntValue(Value, 2);
302 } else if (Value <= std::numeric_limits<uint16_t>::max()) {
303 Streamer->EmitIntValue(LF_USHORT, 2);
304 emitComment(Comment);
305 Streamer->EmitIntValue(Value, 2);
307 } else if (Value <= std::numeric_limits<uint32_t>::max()) {
308 Streamer->EmitIntValue(LF_ULONG, 2);
309 emitComment(Comment);
310 Streamer->EmitIntValue(Value, 4);
313 Streamer->EmitIntValue(LF_UQUADWORD, 2);
314 emitComment(Comment);
315 Streamer->EmitIntValue(Value, 8);
320 Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
321 assert(Value < 0 && "Encoded integer is not signed!");
322 if (Value >= std::numeric_limits<int8_t>::min()) {
323 if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
325 if (auto EC = Writer->writeInteger<int8_t>(Value))
327 } else if (Value >= std::numeric_limits<int16_t>::min()) {
328 if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
330 if (auto EC = Writer->writeInteger<int16_t>(Value))
332 } else if (Value >= std::numeric_limits<int32_t>::min()) {
333 if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
335 if (auto EC = Writer->writeInteger<int32_t>(Value))
338 if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
340 if (auto EC = Writer->writeInteger(Value))
343 return Error::success();
346 Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
347 if (Value < LF_NUMERIC) {
348 if (auto EC = Writer->writeInteger<uint16_t>(Value))
350 } else if (Value <= std::numeric_limits<uint16_t>::max()) {
351 if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
353 if (auto EC = Writer->writeInteger<uint16_t>(Value))
355 } else if (Value <= std::numeric_limits<uint32_t>::max()) {
356 if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
358 if (auto EC = Writer->writeInteger<uint32_t>(Value))
361 if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
363 if (auto EC = Writer->writeInteger(Value))
367 return Error::success();