Disassembler.cpp 6.2 KB

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