Disassembler.cpp 6.3 KB

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