xref: /llvm-project/llvm/lib/DebugInfo/CodeView/CodeViewRecordIO.cpp (revision ebd3ae8371b4ca7b4c9c0bd62f28babaaf25a633)
1 //===- CodeViewRecordIO.cpp -------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
15 
16 using namespace llvm;
17 using namespace llvm::codeview;
18 
19 Error CodeViewRecordIO::beginRecord(Optional<uint32_t> MaxLength) {
20   RecordLimit Limit;
21   Limit.MaxLength = MaxLength;
22   Limit.BeginOffset = getCurrentOffset();
23   Limits.push_back(Limit);
24   return Error::success();
25 }
26 
27 Error CodeViewRecordIO::endRecord() {
28   assert(!Limits.empty() && "Not in a record!");
29   Limits.pop_back();
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();
39 }
40 
41 uint32_t CodeViewRecordIO::maxFieldLength() const {
42   assert(!Limits.empty() && "Not in a record!");
43 
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
47   // the general case.
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;
54   }
55   assert(Min.hasValue() && "Every field must have a maximum length!");
56 
57   return *Min;
58 }
59 
60 Error CodeViewRecordIO::padToAlignment(uint32_t Align) {
61   if (isReading())
62     return Reader->padToAlignment(Align);
63   return Writer->padToAlignment(Align);
64 }
65 
66 Error CodeViewRecordIO::skipPadding() {
67   assert(!isWriting() && "Cannot skip padding while writing!");
68 
69   if (Reader->bytesRemaining() == 0)
70     return Error::success();
71 
72   uint8_t Leaf = Reader->peek();
73   if (Leaf < LF_PAD0)
74     return Error::success();
75   // Leaf is greater than 0xf0. We should advance by the number of bytes in
76   // the low 4 bits.
77   unsigned BytesToAdvance = Leaf & 0x0F;
78   return Reader->skip(BytesToAdvance);
79 }
80 
81 Error CodeViewRecordIO::mapByteVectorTail(ArrayRef<uint8_t> &Bytes) {
82   if (isWriting()) {
83     if (auto EC = Writer->writeBytes(Bytes))
84       return EC;
85   } else {
86     if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
87       return EC;
88   }
89   return Error::success();
90 }
91 
92 Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes) {
93   ArrayRef<uint8_t> BytesRef(Bytes);
94   if (auto EC = mapByteVectorTail(BytesRef))
95     return EC;
96   if (!isWriting())
97     Bytes.assign(BytesRef.begin(), BytesRef.end());
98 
99   return Error::success();
100 }
101 
102 Error CodeViewRecordIO::mapInteger(TypeIndex &TypeInd) {
103   if (isWriting()) {
104     if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
105       return EC;
106     return Error::success();
107   }
108 
109   uint32_t I;
110   if (auto EC = Reader->readInteger(I))
111     return EC;
112   TypeInd.setIndex(I);
113   return Error::success();
114 }
115 
116 Error CodeViewRecordIO::mapEncodedInteger(int64_t &Value) {
117   if (isWriting()) {
118     if (Value >= 0) {
119       if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
120         return EC;
121     } else {
122       if (auto EC = writeEncodedSignedInteger(Value))
123         return EC;
124     }
125   } else {
126     APSInt N;
127     if (auto EC = consume(*Reader, N))
128       return EC;
129     Value = N.getExtValue();
130   }
131 
132   return Error::success();
133 }
134 
135 Error CodeViewRecordIO::mapEncodedInteger(uint64_t &Value) {
136   if (isWriting()) {
137     if (auto EC = writeEncodedUnsignedInteger(Value))
138       return EC;
139   } else {
140     APSInt N;
141     if (auto EC = consume(*Reader, N))
142       return EC;
143     Value = N.getZExtValue();
144   }
145   return Error::success();
146 }
147 
148 Error CodeViewRecordIO::mapEncodedInteger(APSInt &Value) {
149   if (isWriting()) {
150     if (Value.isSigned())
151       return writeEncodedSignedInteger(Value.getSExtValue());
152     return writeEncodedUnsignedInteger(Value.getZExtValue());
153   }
154 
155   return consume(*Reader, Value);
156 }
157 
158 Error CodeViewRecordIO::mapStringZ(StringRef &Value) {
159   if (isWriting()) {
160     // Truncate if we attempt to write too much.
161     StringRef S = Value.take_front(maxFieldLength() - 1);
162     if (auto EC = Writer->writeCString(S))
163       return EC;
164   } else {
165     if (auto EC = Reader->readCString(Value))
166       return EC;
167   }
168   return Error::success();
169 }
170 
171 Error CodeViewRecordIO::mapGuid(StringRef &Guid) {
172   constexpr uint32_t GuidSize = 16;
173   if (maxFieldLength() < GuidSize)
174     return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
175 
176   if (isWriting()) {
177     assert(Guid.size() == 16 && "Invalid Guid Size!");
178     if (auto EC = Writer->writeFixedString(Guid))
179       return EC;
180   } else {
181     if (auto EC = Reader->readFixedString(Guid, 16))
182       return EC;
183   }
184   return Error::success();
185 }
186 
187 Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value) {
188   if (isWriting()) {
189     for (auto V : Value) {
190       if (auto EC = mapStringZ(V))
191         return EC;
192     }
193     if (auto EC = Writer->writeInteger<uint8_t>(0))
194       return EC;
195   } else {
196     StringRef S;
197     if (auto EC = mapStringZ(S))
198       return EC;
199     while (!S.empty()) {
200       Value.push_back(S);
201       if (auto EC = mapStringZ(S))
202         return EC;
203     };
204   }
205   return Error::success();
206 }
207 
208 Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
209   assert(Value < 0 && "Encoded integer is not signed!");
210   if (Value >= std::numeric_limits<int8_t>::min()) {
211     if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
212       return EC;
213     if (auto EC = Writer->writeInteger<int8_t>(Value))
214       return EC;
215   } else if (Value >= std::numeric_limits<int16_t>::min()) {
216     if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
217       return EC;
218     if (auto EC = Writer->writeInteger<int16_t>(Value))
219       return EC;
220   } else if (Value >= std::numeric_limits<int32_t>::min()) {
221     if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
222       return EC;
223     if (auto EC = Writer->writeInteger<int32_t>(Value))
224       return EC;
225   } else {
226     if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
227       return EC;
228     if (auto EC = Writer->writeInteger(Value))
229       return EC;
230   }
231   return Error::success();
232 }
233 
234 Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
235   if (Value < LF_NUMERIC) {
236     if (auto EC = Writer->writeInteger<uint16_t>(Value))
237       return EC;
238   } else if (Value <= std::numeric_limits<uint16_t>::max()) {
239     if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
240       return EC;
241     if (auto EC = Writer->writeInteger<uint16_t>(Value))
242       return EC;
243   } else if (Value <= std::numeric_limits<uint32_t>::max()) {
244     if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
245       return EC;
246     if (auto EC = Writer->writeInteger<uint32_t>(Value))
247       return EC;
248   } else {
249     if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
250       return EC;
251     if (auto EC = Writer->writeInteger(Value))
252       return EC;
253   }
254 
255   return Error::success();
256 }
257