RecordSerialization.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //===-- RecordSerialization.cpp -------------------------------------------===//
  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. //
  9. // Utilities for serializing and deserializing CodeView records.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
  13. #include "llvm/ADT/APInt.h"
  14. #include "llvm/ADT/APSInt.h"
  15. #include "llvm/DebugInfo/CodeView/CVRecord.h"
  16. #include "llvm/DebugInfo/CodeView/CodeViewError.h"
  17. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  18. #include "llvm/Support/BinaryByteStream.h"
  19. using namespace llvm;
  20. using namespace llvm::codeview;
  21. using namespace llvm::support;
  22. /// Reinterpret a byte array as an array of characters. Does not interpret as
  23. /// a C string, as StringRef has several helpers (split) that make that easy.
  24. StringRef llvm::codeview::getBytesAsCharacters(ArrayRef<uint8_t> LeafData) {
  25. return StringRef(reinterpret_cast<const char *>(LeafData.data()),
  26. LeafData.size());
  27. }
  28. StringRef llvm::codeview::getBytesAsCString(ArrayRef<uint8_t> LeafData) {
  29. return getBytesAsCharacters(LeafData).split('\0').first;
  30. }
  31. Error llvm::codeview::consume(BinaryStreamReader &Reader, APSInt &Num) {
  32. // Used to avoid overload ambiguity on APInt constructor.
  33. bool FalseVal = false;
  34. uint16_t Short;
  35. if (auto EC = Reader.readInteger(Short))
  36. return EC;
  37. if (Short < LF_NUMERIC) {
  38. Num = APSInt(APInt(/*numBits=*/16, Short, /*isSigned=*/false),
  39. /*isUnsigned=*/true);
  40. return Error::success();
  41. }
  42. switch (Short) {
  43. case LF_CHAR: {
  44. int8_t N;
  45. if (auto EC = Reader.readInteger(N))
  46. return EC;
  47. Num = APSInt(APInt(8, N, true), false);
  48. return Error::success();
  49. }
  50. case LF_SHORT: {
  51. int16_t N;
  52. if (auto EC = Reader.readInteger(N))
  53. return EC;
  54. Num = APSInt(APInt(16, N, true), false);
  55. return Error::success();
  56. }
  57. case LF_USHORT: {
  58. uint16_t N;
  59. if (auto EC = Reader.readInteger(N))
  60. return EC;
  61. Num = APSInt(APInt(16, N, false), true);
  62. return Error::success();
  63. }
  64. case LF_LONG: {
  65. int32_t N;
  66. if (auto EC = Reader.readInteger(N))
  67. return EC;
  68. Num = APSInt(APInt(32, N, true), false);
  69. return Error::success();
  70. }
  71. case LF_ULONG: {
  72. uint32_t N;
  73. if (auto EC = Reader.readInteger(N))
  74. return EC;
  75. Num = APSInt(APInt(32, N, FalseVal), true);
  76. return Error::success();
  77. }
  78. case LF_QUADWORD: {
  79. int64_t N;
  80. if (auto EC = Reader.readInteger(N))
  81. return EC;
  82. Num = APSInt(APInt(64, N, true), false);
  83. return Error::success();
  84. }
  85. case LF_UQUADWORD: {
  86. uint64_t N;
  87. if (auto EC = Reader.readInteger(N))
  88. return EC;
  89. Num = APSInt(APInt(64, N, false), true);
  90. return Error::success();
  91. }
  92. }
  93. return make_error<CodeViewError>(cv_error_code::corrupt_record,
  94. "Buffer contains invalid APSInt type");
  95. }
  96. Error llvm::codeview::consume(StringRef &Data, APSInt &Num) {
  97. ArrayRef<uint8_t> Bytes(Data.bytes_begin(), Data.bytes_end());
  98. BinaryByteStream S(Bytes, llvm::support::little);
  99. BinaryStreamReader SR(S);
  100. auto EC = consume(SR, Num);
  101. Data = Data.take_back(SR.bytesRemaining());
  102. return EC;
  103. }
  104. /// Decode a numeric leaf value that is known to be a uint64_t.
  105. Error llvm::codeview::consume_numeric(BinaryStreamReader &Reader,
  106. uint64_t &Num) {
  107. APSInt N;
  108. if (auto EC = consume(Reader, N))
  109. return EC;
  110. if (N.isSigned() || !N.isIntN(64))
  111. return make_error<CodeViewError>(cv_error_code::corrupt_record,
  112. "Data is not a numeric value!");
  113. Num = N.getLimitedValue();
  114. return Error::success();
  115. }
  116. Error llvm::codeview::consume(BinaryStreamReader &Reader, uint32_t &Item) {
  117. return Reader.readInteger(Item);
  118. }
  119. Error llvm::codeview::consume(StringRef &Data, uint32_t &Item) {
  120. ArrayRef<uint8_t> Bytes(Data.bytes_begin(), Data.bytes_end());
  121. BinaryByteStream S(Bytes, llvm::support::little);
  122. BinaryStreamReader SR(S);
  123. auto EC = consume(SR, Item);
  124. Data = Data.take_back(SR.bytesRemaining());
  125. return EC;
  126. }
  127. Error llvm::codeview::consume(BinaryStreamReader &Reader, int32_t &Item) {
  128. return Reader.readInteger(Item);
  129. }
  130. Error llvm::codeview::consume(BinaryStreamReader &Reader, StringRef &Item) {
  131. if (Reader.empty())
  132. return make_error<CodeViewError>(cv_error_code::corrupt_record,
  133. "Null terminated string buffer is empty!");
  134. return Reader.readCString(Item);
  135. }
  136. Expected<CVSymbol> llvm::codeview::readSymbolFromStream(BinaryStreamRef Stream,
  137. uint32_t Offset) {
  138. return readCVRecordFromStream<SymbolKind>(Stream, Offset);
  139. }