CodeViewRecordIO.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //===- CodeViewRecordIO.cpp -------------------------------------*- C++ -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h"
  9. #include "llvm/DebugInfo/CodeView/CodeView.h"
  10. #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
  11. #include "llvm/Support/BinaryStreamReader.h"
  12. #include "llvm/Support/BinaryStreamWriter.h"
  13. using namespace llvm;
  14. using namespace llvm::codeview;
  15. Error CodeViewRecordIO::beginRecord(Optional<uint32_t> MaxLength) {
  16. RecordLimit Limit;
  17. Limit.MaxLength = MaxLength;
  18. Limit.BeginOffset = getCurrentOffset();
  19. Limits.push_back(Limit);
  20. resetStreamedLen();
  21. return Error::success();
  22. }
  23. Error CodeViewRecordIO::endRecord() {
  24. assert(!Limits.empty() && "Not in a record!");
  25. Limits.pop_back();
  26. // We would like to assert that we actually read / wrote all the bytes that we
  27. // expected to for this record, but unfortunately we can't do this. Some
  28. // producers such as MASM over-allocate for certain types of records and
  29. // commit the extraneous data, so when reading we can't be sure every byte
  30. // will have been read. And when writing we over-allocate temporarily since
  31. // we don't know how big the record is until we're finished writing it, so
  32. // even though we don't commit the extraneous data, we still can't guarantee
  33. // we're at the end of the allocated data.
  34. if (isStreaming()) {
  35. // For streaming mode, add padding to align with 4 byte boundaries for each
  36. // record
  37. uint32_t Align = getStreamedLen() % 4;
  38. if (Align == 0)
  39. return Error::success();
  40. int PaddingBytes = 4 - Align;
  41. while (PaddingBytes > 0) {
  42. char Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
  43. StringRef BytesSR = StringRef(&Pad, sizeof(Pad));
  44. Streamer->EmitBytes(BytesSR);
  45. --PaddingBytes;
  46. }
  47. }
  48. return Error::success();
  49. }
  50. uint32_t CodeViewRecordIO::maxFieldLength() const {
  51. if (isStreaming())
  52. return 0;
  53. assert(!Limits.empty() && "Not in a record!");
  54. // The max length of the next field is the minimum of all lengths that would
  55. // be allowed by any of the sub-records we're in. In practice, we can only
  56. // ever be at most 1 sub-record deep (in a FieldList), but this works for
  57. // the general case.
  58. uint32_t Offset = getCurrentOffset();
  59. Optional<uint32_t> Min = Limits.front().bytesRemaining(Offset);
  60. for (auto X : makeArrayRef(Limits).drop_front()) {
  61. Optional<uint32_t> ThisMin = X.bytesRemaining(Offset);
  62. if (ThisMin.hasValue())
  63. Min = (Min.hasValue()) ? std::min(*Min, *ThisMin) : *ThisMin;
  64. }
  65. assert(Min.hasValue() && "Every field must have a maximum length!");
  66. return *Min;
  67. }
  68. Error CodeViewRecordIO::padToAlignment(uint32_t Align) {
  69. if (isReading())
  70. return Reader->padToAlignment(Align);
  71. return Writer->padToAlignment(Align);
  72. }
  73. Error CodeViewRecordIO::skipPadding() {
  74. assert(!isWriting() && "Cannot skip padding while writing!");
  75. if (Reader->bytesRemaining() == 0)
  76. return Error::success();
  77. uint8_t Leaf = Reader->peek();
  78. if (Leaf < LF_PAD0)
  79. return Error::success();
  80. // Leaf is greater than 0xf0. We should advance by the number of bytes in
  81. // the low 4 bits.
  82. unsigned BytesToAdvance = Leaf & 0x0F;
  83. return Reader->skip(BytesToAdvance);
  84. }
  85. Error CodeViewRecordIO::mapByteVectorTail(ArrayRef<uint8_t> &Bytes) {
  86. if (isStreaming()) {
  87. Streamer->EmitBinaryData(toStringRef(Bytes));
  88. incrStreamedLen(Bytes.size());
  89. } else if (isWriting()) {
  90. if (auto EC = Writer->writeBytes(Bytes))
  91. return EC;
  92. } else {
  93. if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
  94. return EC;
  95. }
  96. return Error::success();
  97. }
  98. Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes) {
  99. ArrayRef<uint8_t> BytesRef(Bytes);
  100. if (auto EC = mapByteVectorTail(BytesRef))
  101. return EC;
  102. if (!isWriting())
  103. Bytes.assign(BytesRef.begin(), BytesRef.end());
  104. return Error::success();
  105. }
  106. Error CodeViewRecordIO::mapInteger(TypeIndex &TypeInd) {
  107. if (isStreaming()) {
  108. Streamer->EmitIntValue(TypeInd.getIndex(), sizeof(TypeInd.getIndex()));
  109. incrStreamedLen(sizeof(TypeInd.getIndex()));
  110. } else if (isWriting()) {
  111. if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
  112. return EC;
  113. } else {
  114. uint32_t I;
  115. if (auto EC = Reader->readInteger(I))
  116. return EC;
  117. TypeInd.setIndex(I);
  118. }
  119. return Error::success();
  120. }
  121. Error CodeViewRecordIO::mapEncodedInteger(int64_t &Value) {
  122. if (isStreaming()) {
  123. if (Value >= 0)
  124. emitEncodedUnsignedInteger(static_cast<uint64_t>(Value));
  125. else
  126. emitEncodedSignedInteger(Value);
  127. } else if (isWriting()) {
  128. if (Value >= 0) {
  129. if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
  130. return EC;
  131. } else {
  132. if (auto EC = writeEncodedSignedInteger(Value))
  133. return EC;
  134. }
  135. } else {
  136. APSInt N;
  137. if (auto EC = consume(*Reader, N))
  138. return EC;
  139. Value = N.getExtValue();
  140. }
  141. return Error::success();
  142. }
  143. Error CodeViewRecordIO::mapEncodedInteger(uint64_t &Value) {
  144. if (isStreaming())
  145. emitEncodedUnsignedInteger(Value);
  146. else if (isWriting()) {
  147. if (auto EC = writeEncodedUnsignedInteger(Value))
  148. return EC;
  149. } else {
  150. APSInt N;
  151. if (auto EC = consume(*Reader, N))
  152. return EC;
  153. Value = N.getZExtValue();
  154. }
  155. return Error::success();
  156. }
  157. Error CodeViewRecordIO::mapEncodedInteger(APSInt &Value) {
  158. if (isStreaming()) {
  159. if (Value.isSigned())
  160. emitEncodedSignedInteger(Value.getSExtValue());
  161. else
  162. emitEncodedUnsignedInteger(Value.getZExtValue());
  163. } else if (isWriting()) {
  164. if (Value.isSigned())
  165. return writeEncodedSignedInteger(Value.getSExtValue());
  166. return writeEncodedUnsignedInteger(Value.getZExtValue());
  167. } else
  168. return consume(*Reader, Value);
  169. return Error::success();
  170. }
  171. Error CodeViewRecordIO::mapStringZ(StringRef &Value) {
  172. if (isStreaming()) {
  173. auto NullTerminatedString = StringRef(Value.data(), Value.size() + 1);
  174. Streamer->EmitBytes(NullTerminatedString);
  175. incrStreamedLen(NullTerminatedString.size());
  176. } else if (isWriting()) {
  177. // Truncate if we attempt to write too much.
  178. StringRef S = Value.take_front(maxFieldLength() - 1);
  179. if (auto EC = Writer->writeCString(S))
  180. return EC;
  181. } else {
  182. if (auto EC = Reader->readCString(Value))
  183. return EC;
  184. }
  185. return Error::success();
  186. }
  187. Error CodeViewRecordIO::mapGuid(GUID &Guid) {
  188. constexpr uint32_t GuidSize = 16;
  189. if (isStreaming()) {
  190. StringRef GuidSR =
  191. StringRef((reinterpret_cast<const char *>(&Guid)), GuidSize);
  192. Streamer->EmitBytes(GuidSR);
  193. incrStreamedLen(GuidSize);
  194. return Error::success();
  195. }
  196. if (maxFieldLength() < GuidSize)
  197. return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
  198. if (isWriting()) {
  199. if (auto EC = Writer->writeBytes(Guid.Guid))
  200. return EC;
  201. } else {
  202. ArrayRef<uint8_t> GuidBytes;
  203. if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
  204. return EC;
  205. memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
  206. }
  207. return Error::success();
  208. }
  209. Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value) {
  210. if (!isReading()) {
  211. for (auto V : Value) {
  212. if (auto EC = mapStringZ(V))
  213. return EC;
  214. }
  215. uint8_t FinalZero = 0;
  216. if (auto EC = mapInteger(FinalZero))
  217. return EC;
  218. } else {
  219. StringRef S;
  220. if (auto EC = mapStringZ(S))
  221. return EC;
  222. while (!S.empty()) {
  223. Value.push_back(S);
  224. if (auto EC = mapStringZ(S))
  225. return EC;
  226. };
  227. }
  228. return Error::success();
  229. }
  230. void CodeViewRecordIO::emitEncodedSignedInteger(const int64_t &Value) {
  231. assert(Value < 0 && "Encoded integer is not signed!");
  232. if (Value >= std::numeric_limits<int8_t>::min()) {
  233. Streamer->EmitIntValue(LF_CHAR, 2);
  234. Streamer->EmitIntValue(Value, 1);
  235. incrStreamedLen(3);
  236. } else if (Value >= std::numeric_limits<int16_t>::min()) {
  237. Streamer->EmitIntValue(LF_SHORT, 2);
  238. Streamer->EmitIntValue(Value, 2);
  239. incrStreamedLen(4);
  240. } else if (Value >= std::numeric_limits<int32_t>::min()) {
  241. Streamer->EmitIntValue(LF_LONG, 2);
  242. Streamer->EmitIntValue(Value, 4);
  243. incrStreamedLen(6);
  244. } else {
  245. Streamer->EmitIntValue(LF_QUADWORD, 2);
  246. Streamer->EmitIntValue(Value, 4);
  247. incrStreamedLen(6);
  248. }
  249. }
  250. void CodeViewRecordIO::emitEncodedUnsignedInteger(const uint64_t &Value) {
  251. if (Value < LF_NUMERIC) {
  252. Streamer->EmitIntValue(Value, 2);
  253. incrStreamedLen(2);
  254. } else if (Value <= std::numeric_limits<uint16_t>::max()) {
  255. Streamer->EmitIntValue(LF_USHORT, 2);
  256. Streamer->EmitIntValue(Value, 2);
  257. incrStreamedLen(4);
  258. } else if (Value <= std::numeric_limits<uint32_t>::max()) {
  259. Streamer->EmitIntValue(LF_ULONG, 2);
  260. Streamer->EmitIntValue(Value, 4);
  261. incrStreamedLen(6);
  262. } else {
  263. Streamer->EmitIntValue(LF_UQUADWORD, 2);
  264. Streamer->EmitIntValue(Value, 8);
  265. incrStreamedLen(6);
  266. }
  267. }
  268. Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
  269. assert(Value < 0 && "Encoded integer is not signed!");
  270. if (Value >= std::numeric_limits<int8_t>::min()) {
  271. if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
  272. return EC;
  273. if (auto EC = Writer->writeInteger<int8_t>(Value))
  274. return EC;
  275. } else if (Value >= std::numeric_limits<int16_t>::min()) {
  276. if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
  277. return EC;
  278. if (auto EC = Writer->writeInteger<int16_t>(Value))
  279. return EC;
  280. } else if (Value >= std::numeric_limits<int32_t>::min()) {
  281. if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
  282. return EC;
  283. if (auto EC = Writer->writeInteger<int32_t>(Value))
  284. return EC;
  285. } else {
  286. if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
  287. return EC;
  288. if (auto EC = Writer->writeInteger(Value))
  289. return EC;
  290. }
  291. return Error::success();
  292. }
  293. Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
  294. if (Value < LF_NUMERIC) {
  295. if (auto EC = Writer->writeInteger<uint16_t>(Value))
  296. return EC;
  297. } else if (Value <= std::numeric_limits<uint16_t>::max()) {
  298. if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
  299. return EC;
  300. if (auto EC = Writer->writeInteger<uint16_t>(Value))
  301. return EC;
  302. } else if (Value <= std::numeric_limits<uint32_t>::max()) {
  303. if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
  304. return EC;
  305. if (auto EC = Writer->writeInteger<uint32_t>(Value))
  306. return EC;
  307. } else {
  308. if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
  309. return EC;
  310. if (auto EC = Writer->writeInteger(Value))
  311. return EC;
  312. }
  313. return Error::success();
  314. }