Disassembler.cpp 7.0 KB

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