Disassembler.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //===- Disassembler.cpp - Disassembler for hex strings --------------------===//
  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. // This class implements the disassembler of strings of bytes written in
  10. // hexadecimal, from standard input or from a file.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "Disassembler.h"
  14. #include "llvm/ADT/Triple.h"
  15. #include "llvm/MC/MCAsmInfo.h"
  16. #include "llvm/MC/MCContext.h"
  17. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  18. #include "llvm/MC/MCInst.h"
  19. #include "llvm/MC/MCRegisterInfo.h"
  20. #include "llvm/MC/MCStreamer.h"
  21. #include "llvm/MC/MCSubtargetInfo.h"
  22. #include "llvm/MC/TargetRegistry.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include "llvm/Support/SourceMgr.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. using namespace llvm;
  27. typedef std::pair<std::vector<unsigned char>, std::vector<const char *>>
  28. ByteArrayTy;
  29. static bool PrintInsts(const MCDisassembler &DisAsm, const ByteArrayTy &Bytes,
  30. SourceMgr &SM, raw_ostream &Out, MCStreamer &Streamer,
  31. bool InAtomicBlock, const MCSubtargetInfo &STI) {
  32. ArrayRef<uint8_t> Data(Bytes.first.data(), Bytes.first.size());
  33. // Disassemble it to strings.
  34. uint64_t Size;
  35. uint64_t Index;
  36. for (Index = 0; Index < Bytes.first.size(); Index += Size) {
  37. MCInst Inst;
  38. MCDisassembler::DecodeStatus S;
  39. S = DisAsm.getInstruction(Inst, Size, Data.slice(Index), Index, nulls());
  40. switch (S) {
  41. case MCDisassembler::Fail:
  42. SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
  43. SourceMgr::DK_Warning, "invalid instruction encoding");
  44. // Don't try to resynchronise the stream in a block
  45. if (InAtomicBlock)
  46. return true;
  47. if (Size == 0)
  48. Size = 1; // skip illegible bytes
  49. break;
  50. case MCDisassembler::SoftFail:
  51. SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
  52. SourceMgr::DK_Warning,
  53. "potentially undefined instruction encoding");
  54. [[fallthrough]];
  55. case MCDisassembler::Success:
  56. Streamer.emitInstruction(Inst, STI);
  57. break;
  58. }
  59. }
  60. return false;
  61. }
  62. static bool SkipToToken(StringRef &Str) {
  63. for (;;) {
  64. if (Str.empty())
  65. return false;
  66. // Strip horizontal whitespace and commas.
  67. if (size_t Pos = Str.find_first_not_of(" \t\r\n,")) {
  68. Str = Str.substr(Pos);
  69. continue;
  70. }
  71. // If this is the start of a comment, remove the rest of the line.
  72. if (Str[0] == '#') {
  73. Str = Str.substr(Str.find_first_of('\n'));
  74. continue;
  75. }
  76. return true;
  77. }
  78. }
  79. static bool ByteArrayFromString(ByteArrayTy &ByteArray, StringRef &Str,
  80. SourceMgr &SM) {
  81. while (SkipToToken(Str)) {
  82. // Handled by higher level
  83. if (Str[0] == '[' || Str[0] == ']')
  84. return false;
  85. // Get the current token.
  86. size_t Next = Str.find_first_of(" \t\n\r,#[]");
  87. StringRef Value = Str.substr(0, Next);
  88. // Convert to a byte and add to the byte vector.
  89. unsigned ByteVal;
  90. if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
  91. // If we have an error, print it and skip to the end of line.
  92. SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
  93. "invalid input token");
  94. Str = Str.substr(Str.find('\n'));
  95. ByteArray.first.clear();
  96. ByteArray.second.clear();
  97. continue;
  98. }
  99. ByteArray.first.push_back(ByteVal);
  100. ByteArray.second.push_back(Value.data());
  101. Str = Str.substr(Next);
  102. }
  103. return false;
  104. }
  105. int Disassembler::disassemble(const Target &T, const std::string &TripleName,
  106. MCSubtargetInfo &STI, MCStreamer &Streamer,
  107. MemoryBuffer &Buffer, SourceMgr &SM,
  108. raw_ostream &Out) {
  109. std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(TripleName));
  110. if (!MRI) {
  111. errs() << "error: no register info for target " << TripleName << "\n";
  112. return -1;
  113. }
  114. MCTargetOptions MCOptions;
  115. std::unique_ptr<const MCAsmInfo> MAI(
  116. T.createMCAsmInfo(*MRI, TripleName, MCOptions));
  117. if (!MAI) {
  118. errs() << "error: no assembly info for target " << TripleName << "\n";
  119. return -1;
  120. }
  121. // Set up the MCContext for creating symbols and MCExpr's.
  122. MCContext Ctx(Triple(TripleName), MAI.get(), MRI.get(), &STI);
  123. std::unique_ptr<const MCDisassembler> DisAsm(
  124. T.createMCDisassembler(STI, Ctx));
  125. if (!DisAsm) {
  126. errs() << "error: no disassembler for target " << TripleName << "\n";
  127. return -1;
  128. }
  129. // Set up initial section manually here
  130. Streamer.initSections(false, STI);
  131. bool ErrorOccurred = false;
  132. // Convert the input to a vector for disassembly.
  133. ByteArrayTy ByteArray;
  134. StringRef Str = Buffer.getBuffer();
  135. bool InAtomicBlock = false;
  136. while (SkipToToken(Str)) {
  137. ByteArray.first.clear();
  138. ByteArray.second.clear();
  139. if (Str[0] == '[') {
  140. if (InAtomicBlock) {
  141. SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
  142. "nested atomic blocks make no sense");
  143. ErrorOccurred = true;
  144. }
  145. InAtomicBlock = true;
  146. Str = Str.drop_front();
  147. continue;
  148. } else if (Str[0] == ']') {
  149. if (!InAtomicBlock) {
  150. SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
  151. "attempt to close atomic block without opening");
  152. ErrorOccurred = true;
  153. }
  154. InAtomicBlock = false;
  155. Str = Str.drop_front();
  156. continue;
  157. }
  158. // It's a real token, get the bytes and emit them
  159. ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
  160. if (!ByteArray.first.empty())
  161. ErrorOccurred |=
  162. PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer, InAtomicBlock, STI);
  163. }
  164. if (InAtomicBlock) {
  165. SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
  166. "unclosed atomic block");
  167. ErrorOccurred = true;
  168. }
  169. return ErrorOccurred;
  170. }