1 //===- CodeViewRecordIO.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h"
11 #include "llvm/DebugInfo/CodeView/CodeView.h"
12 #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
13 #include "llvm/Support/BinaryStreamReader.h"
14 #include "llvm/Support/BinaryStreamWriter.h"
17 using namespace llvm::codeview;
19 Error CodeViewRecordIO::beginRecord(Optional<uint32_t> MaxLength) {
21 Limit.MaxLength = MaxLength;
22 Limit.BeginOffset = getCurrentOffset();
23 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.
38 return Error::success();
41 uint32_t CodeViewRecordIO::maxFieldLength() const {
42 assert(!Limits.empty() && "Not in a record!");
44 // The max length of the next field is the minimum of all lengths that would
45 // be allowed by any of the sub-records we're in. In practice, we can only
46 // ever be at most 1 sub-record deep (in a FieldList), but this works for
48 uint32_t Offset = getCurrentOffset();
49 Optional<uint32_t> Min = Limits.front().bytesRemaining(Offset);
50 for (auto X : makeArrayRef(Limits).drop_front()) {
51 Optional<uint32_t> ThisMin = X.bytesRemaining(Offset);
52 if (ThisMin.hasValue())
53 Min = (Min.hasValue()) ? std::min(*Min, *ThisMin) : *ThisMin;
55 assert(Min.hasValue() && "Every field must have a maximum length!");
60 Error CodeViewRecordIO::padToAlignment(uint32_t Align) {
62 return Reader->padToAlignment(Align);
63 return Writer->padToAlignment(Align);
66 Error CodeViewRecordIO::skipPadding() {
67 assert(!isWriting() && "Cannot skip padding while writing!");
69 if (Reader->bytesRemaining() == 0)
70 return Error::success();
72 uint8_t Leaf = Reader->peek();
74 return Error::success();
75 // Leaf is greater than 0xf0. We should advance by the number of bytes in
77 unsigned BytesToAdvance = Leaf & 0x0F;
78 return Reader->skip(BytesToAdvance);
81 Error CodeViewRecordIO::mapByteVectorTail(ArrayRef<uint8_t> &Bytes) {
83 if (auto EC = Writer->writeBytes(Bytes))
86 if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
89 return Error::success();
92 Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes) {
93 ArrayRef<uint8_t> BytesRef(Bytes);
94 if (auto EC = mapByteVectorTail(BytesRef))
97 Bytes.assign(BytesRef.begin(), BytesRef.end());
99 return Error::success();
102 Error CodeViewRecordIO::mapInteger(TypeIndex &TypeInd) {
104 if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
106 return Error::success();
110 if (auto EC = Reader->readInteger(I))
113 return Error::success();
116 Error CodeViewRecordIO::mapEncodedInteger(int64_t &Value) {
119 if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
122 if (auto EC = writeEncodedSignedInteger(Value))
127 if (auto EC = consume(*Reader, N))
129 Value = N.getExtValue();
132 return Error::success();
135 Error CodeViewRecordIO::mapEncodedInteger(uint64_t &Value) {
137 if (auto EC = writeEncodedUnsignedInteger(Value))
141 if (auto EC = consume(*Reader, N))
143 Value = N.getZExtValue();
145 return Error::success();
148 Error CodeViewRecordIO::mapEncodedInteger(APSInt &Value) {
150 if (Value.isSigned())
151 return writeEncodedSignedInteger(Value.getSExtValue());
152 return writeEncodedUnsignedInteger(Value.getZExtValue());
155 return consume(*Reader, Value);
158 Error CodeViewRecordIO::mapStringZ(StringRef &Value) {
160 // Truncate if we attempt to write too much.
161 StringRef S = Value.take_front(maxFieldLength() - 1);
162 if (auto EC = Writer->writeCString(S))
165 if (auto EC = Reader->readCString(Value))
168 return Error::success();
171 Error CodeViewRecordIO::mapGuid(GUID &Guid) {
172 constexpr uint32_t GuidSize = 16;
173 if (maxFieldLength() < GuidSize)
174 return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
177 if (auto EC = Writer->writeBytes(Guid.Guid))
180 ArrayRef<uint8_t> GuidBytes;
181 if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
183 memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
185 return Error::success();
188 Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value) {
190 for (auto V : Value) {
191 if (auto EC = mapStringZ(V))
194 if (auto EC = Writer->writeInteger<uint8_t>(0))
198 if (auto EC = mapStringZ(S))
202 if (auto EC = mapStringZ(S))
206 return Error::success();
209 Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
210 assert(Value < 0 && "Encoded integer is not signed!");
211 if (Value >= std::numeric_limits<int8_t>::min()) {
212 if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
214 if (auto EC = Writer->writeInteger<int8_t>(Value))
216 } else if (Value >= std::numeric_limits<int16_t>::min()) {
217 if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
219 if (auto EC = Writer->writeInteger<int16_t>(Value))
221 } else if (Value >= std::numeric_limits<int32_t>::min()) {
222 if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
224 if (auto EC = Writer->writeInteger<int32_t>(Value))
227 if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
229 if (auto EC = Writer->writeInteger(Value))
232 return Error::success();
235 Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
236 if (Value < LF_NUMERIC) {
237 if (auto EC = Writer->writeInteger<uint16_t>(Value))
239 } else if (Value <= std::numeric_limits<uint16_t>::max()) {
240 if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
242 if (auto EC = Writer->writeInteger<uint16_t>(Value))
244 } else if (Value <= std::numeric_limits<uint32_t>::max()) {
245 if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
247 if (auto EC = Writer->writeInteger<uint32_t>(Value))
250 if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
252 if (auto EC = Writer->writeInteger(Value))
256 return Error::success();