CodeViewRecordIO.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. return Error::success();
  21. }
  22. Error CodeViewRecordIO::endRecord() {
  23. assert(!Limits.empty() && "Not in a record!");
  24. Limits.pop_back();
  25. // We would like to assert that we actually read / wrote all the bytes that we
  26. // expected to for this record, but unfortunately we can't do this. Some
  27. // producers such as MASM over-allocate for certain types of records and
  28. // commit the extraneous data, so when reading we can't be sure every byte
  29. // will have been read. And when writing we over-allocate temporarily since
  30. // we don't know how big the record is until we're finished writing it, so
  31. // even though we don't commit the extraneous data, we still can't guarantee
  32. // we're at the end of the allocated data.
  33. if (isStreaming()) {
  34. // For streaming mode, add padding to align with 4 byte boundaries for each
  35. // record
  36. uint32_t Align = getStreamedLen() % 4;
  37. if (Align == 0)
  38. return Error::success();
  39. int PaddingBytes = 4 - Align;
  40. while (PaddingBytes > 0) {
  41. char Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
  42. StringRef BytesSR = StringRef(&Pad, sizeof(Pad));
  43. Streamer->EmitBytes(BytesSR);
  44. --PaddingBytes;
  45. }
  46. resetStreamedLen();
  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. const Twine &Comment) {
  87. if (isStreaming()) {
  88. emitComment(Comment);
  89. Streamer->EmitBinaryData(toStringRef(Bytes));
  90. incrStreamedLen(Bytes.size());
  91. } else if (isWriting()) {
  92. if (auto EC = Writer->writeBytes(Bytes))
  93. return EC;
  94. } else {
  95. if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
  96. return EC;
  97. }
  98. return Error::success();
  99. }
  100. Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes,
  101. const Twine &Comment) {
  102. ArrayRef<uint8_t> BytesRef(Bytes);
  103. if (auto EC = mapByteVectorTail(BytesRef, Comment))
  104. return EC;
  105. if (!isWriting())
  106. Bytes.assign(BytesRef.begin(), BytesRef.end());
  107. return Error::success();
  108. }
  109. Error CodeViewRecordIO::mapInteger(TypeIndex &TypeInd, const Twine &Comment) {
  110. if (isStreaming()) {
  111. emitComment(Comment);
  112. Streamer->EmitIntValue(TypeInd.getIndex(), sizeof(TypeInd.getIndex()));
  113. incrStreamedLen(sizeof(TypeInd.getIndex()));
  114. } else if (isWriting()) {
  115. if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
  116. return EC;
  117. } else {
  118. uint32_t I;
  119. if (auto EC = Reader->readInteger(I))
  120. return EC;
  121. TypeInd.setIndex(I);
  122. }
  123. return Error::success();
  124. }
  125. Error CodeViewRecordIO::mapEncodedInteger(int64_t &Value,
  126. const Twine &Comment) {
  127. if (isStreaming()) {
  128. if (Value >= 0)
  129. emitEncodedUnsignedInteger(static_cast<uint64_t>(Value), Comment);
  130. else
  131. emitEncodedSignedInteger(Value, Comment);
  132. } else if (isWriting()) {
  133. if (Value >= 0) {
  134. if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
  135. return EC;
  136. } else {
  137. if (auto EC = writeEncodedSignedInteger(Value))
  138. return EC;
  139. }
  140. } else {
  141. APSInt N;
  142. if (auto EC = consume(*Reader, N))
  143. return EC;
  144. Value = N.getExtValue();
  145. }
  146. return Error::success();
  147. }
  148. Error CodeViewRecordIO::mapEncodedInteger(uint64_t &Value,
  149. const Twine &Comment) {
  150. if (isStreaming())
  151. emitEncodedUnsignedInteger(Value, Comment);
  152. else if (isWriting()) {
  153. if (auto EC = writeEncodedUnsignedInteger(Value))
  154. return EC;
  155. } else {
  156. APSInt N;
  157. if (auto EC = consume(*Reader, N))
  158. return EC;
  159. Value = N.getZExtValue();
  160. }
  161. return Error::success();
  162. }
  163. Error CodeViewRecordIO::mapEncodedInteger(APSInt &Value, const Twine &Comment) {
  164. if (isStreaming()) {
  165. if (Value.isSigned())
  166. emitEncodedSignedInteger(Value.getSExtValue(), Comment);
  167. else
  168. emitEncodedUnsignedInteger(Value.getZExtValue(), Comment);
  169. } else if (isWriting()) {
  170. if (Value.isSigned())
  171. return writeEncodedSignedInteger(Value.getSExtValue());
  172. return writeEncodedUnsignedInteger(Value.getZExtValue());
  173. } else
  174. return consume(*Reader, Value);
  175. return Error::success();
  176. }
  177. Error CodeViewRecordIO::mapStringZ(StringRef &Value, const Twine &Comment) {
  178. if (isStreaming()) {
  179. auto NullTerminatedString = StringRef(Value.data(), Value.size() + 1);
  180. emitComment(Comment);
  181. Streamer->EmitBytes(NullTerminatedString);
  182. incrStreamedLen(NullTerminatedString.size());
  183. } else if (isWriting()) {
  184. // Truncate if we attempt to write too much.
  185. StringRef S = Value.take_front(maxFieldLength() - 1);
  186. if (auto EC = Writer->writeCString(S))
  187. return EC;
  188. } else {
  189. if (auto EC = Reader->readCString(Value))
  190. return EC;
  191. }
  192. return Error::success();
  193. }
  194. Error CodeViewRecordIO::mapGuid(GUID &Guid, const Twine &Comment) {
  195. constexpr uint32_t GuidSize = 16;
  196. if (isStreaming()) {
  197. StringRef GuidSR =
  198. StringRef((reinterpret_cast<const char *>(&Guid)), GuidSize);
  199. emitComment(Comment);
  200. Streamer->EmitBytes(GuidSR);
  201. incrStreamedLen(GuidSize);
  202. return Error::success();
  203. }
  204. if (maxFieldLength() < GuidSize)
  205. return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
  206. if (isWriting()) {
  207. if (auto EC = Writer->writeBytes(Guid.Guid))
  208. return EC;
  209. } else {
  210. ArrayRef<uint8_t> GuidBytes;
  211. if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
  212. return EC;
  213. memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
  214. }
  215. return Error::success();
  216. }
  217. Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value,
  218. const Twine &Comment) {
  219. if (!isReading()) {
  220. emitComment(Comment);
  221. for (auto V : Value) {
  222. if (auto EC = mapStringZ(V))
  223. return EC;
  224. }
  225. uint8_t FinalZero = 0;
  226. if (auto EC = mapInteger(FinalZero))
  227. return EC;
  228. } else {
  229. StringRef S;
  230. if (auto EC = mapStringZ(S))
  231. return EC;
  232. while (!S.empty()) {
  233. Value.push_back(S);
  234. if (auto EC = mapStringZ(S))
  235. return EC;
  236. };
  237. }
  238. return Error::success();
  239. }
  240. void CodeViewRecordIO::emitEncodedSignedInteger(const int64_t &Value,
  241. const Twine &Comment) {
  242. assert(Value < 0 && "Encoded integer is not signed!");
  243. if (Value >= std::numeric_limits<int8_t>::min()) {
  244. Streamer->EmitIntValue(LF_CHAR, 2);
  245. emitComment(Comment);
  246. Streamer->EmitIntValue(Value, 1);
  247. incrStreamedLen(3);
  248. } else if (Value >= std::numeric_limits<int16_t>::min()) {
  249. Streamer->EmitIntValue(LF_SHORT, 2);
  250. emitComment(Comment);
  251. Streamer->EmitIntValue(Value, 2);
  252. incrStreamedLen(4);
  253. } else if (Value >= std::numeric_limits<int32_t>::min()) {
  254. Streamer->EmitIntValue(LF_LONG, 2);
  255. emitComment(Comment);
  256. Streamer->EmitIntValue(Value, 4);
  257. incrStreamedLen(6);
  258. } else {
  259. Streamer->EmitIntValue(LF_QUADWORD, 2);
  260. emitComment(Comment);
  261. Streamer->EmitIntValue(Value, 4);
  262. incrStreamedLen(6);
  263. }
  264. }
  265. void CodeViewRecordIO::emitEncodedUnsignedInteger(const uint64_t &Value,
  266. const Twine &Comment) {
  267. if (Value < LF_NUMERIC) {
  268. emitComment(Comment);
  269. Streamer->EmitIntValue(Value, 2);
  270. incrStreamedLen(2);
  271. } else if (Value <= std::numeric_limits<uint16_t>::max()) {
  272. Streamer->EmitIntValue(LF_USHORT, 2);
  273. emitComment(Comment);
  274. Streamer->EmitIntValue(Value, 2);
  275. incrStreamedLen(4);
  276. } else if (Value <= std::numeric_limits<uint32_t>::max()) {
  277. Streamer->EmitIntValue(LF_ULONG, 2);
  278. emitComment(Comment);
  279. Streamer->EmitIntValue(Value, 4);
  280. incrStreamedLen(6);
  281. } else {
  282. Streamer->EmitIntValue(LF_UQUADWORD, 2);
  283. emitComment(Comment);
  284. Streamer->EmitIntValue(Value, 8);
  285. incrStreamedLen(6);
  286. }
  287. }
  288. Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
  289. assert(Value < 0 && "Encoded integer is not signed!");
  290. if (Value >= std::numeric_limits<int8_t>::min()) {
  291. if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
  292. return EC;
  293. if (auto EC = Writer->writeInteger<int8_t>(Value))
  294. return EC;
  295. } else if (Value >= std::numeric_limits<int16_t>::min()) {
  296. if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
  297. return EC;
  298. if (auto EC = Writer->writeInteger<int16_t>(Value))
  299. return EC;
  300. } else if (Value >= std::numeric_limits<int32_t>::min()) {
  301. if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
  302. return EC;
  303. if (auto EC = Writer->writeInteger<int32_t>(Value))
  304. return EC;
  305. } else {
  306. if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
  307. return EC;
  308. if (auto EC = Writer->writeInteger(Value))
  309. return EC;
  310. }
  311. return Error::success();
  312. }
  313. Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
  314. if (Value < LF_NUMERIC) {
  315. if (auto EC = Writer->writeInteger<uint16_t>(Value))
  316. return EC;
  317. } else if (Value <= std::numeric_limits<uint16_t>::max()) {
  318. if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
  319. return EC;
  320. if (auto EC = Writer->writeInteger<uint16_t>(Value))
  321. return EC;
  322. } else if (Value <= std::numeric_limits<uint32_t>::max()) {
  323. if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
  324. return EC;
  325. if (auto EC = Writer->writeInteger<uint32_t>(Value))
  326. return EC;
  327. } else {
  328. if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
  329. return EC;
  330. if (auto EC = Writer->writeInteger(Value))
  331. return EC;
  332. }
  333. return Error::success();
  334. }