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 15 #include "Disassembler.h" 16 17 #include "llvm/ADT/OwningPtr.h" 18 #include "llvm/MC/MCAsmInfo.h" 19 #include "llvm/MC/MCDisassembler.h" 20 #include "llvm/MC/MCInst.h" 21 #include "llvm/MC/MCInstPrinter.h" 22 #include "llvm/Target/TargetRegistry.h" 23 #include "llvm/Support/MemoryBuffer.h" 24 #include "llvm/Support/MemoryObject.h" 25 #include "llvm/Support/raw_ostream.h" 26 #include "llvm/Support/SourceMgr.h" 27 using namespace llvm; 28 29 typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy; 30 31 namespace { 32 class VectorMemoryObject : public MemoryObject { 33 private: 34 const ByteArrayTy &Bytes; 35 public: 36 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {} 37 38 uint64_t getBase() const { return 0; } 39 uint64_t getExtent() const { return Bytes.size(); } 40 41 int readByte(uint64_t Addr, uint8_t *Byte) const { 42 if (Addr > getExtent()) 43 return -1; 44 *Byte = Bytes[Addr].first; 45 return 0; 46 } 47 }; 48 } 49 50 static bool PrintInsts(const MCDisassembler &DisAsm, 51 MCInstPrinter &Printer, const ByteArrayTy &Bytes, 52 SourceMgr &SM) { 53 // Wrap the vector in a MemoryObject. 54 VectorMemoryObject memoryObject(Bytes); 55 56 // Disassemble it to strings. 57 uint64_t Size; 58 uint64_t Index; 59 60 for (Index = 0; Index < Bytes.size(); Index += Size) { 61 MCInst Inst; 62 63 if (DisAsm.getInstruction(Inst, Size, memoryObject, Index, 64 /*REMOVE*/ nulls())) { 65 Printer.printInst(&Inst, outs()); 66 outs() << "\n"; 67 } 68 else { 69 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second), 70 "invalid instruction encoding", "warning"); 71 if (Size == 0) 72 Size = 1; // skip illegible bytes 73 } 74 } 75 76 return false; 77 } 78 79 int Disassembler::disassemble(const Target &T, const std::string &Triple, 80 MemoryBuffer &Buffer) { 81 // Set up disassembler. 82 OwningPtr<const MCAsmInfo> AsmInfo(T.createAsmInfo(Triple)); 83 84 if (!AsmInfo) { 85 errs() << "error: no assembly info for target " << Triple << "\n"; 86 return -1; 87 } 88 89 OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler()); 90 if (!DisAsm) { 91 errs() << "error: no disassembler for target " << Triple << "\n"; 92 return -1; 93 } 94 95 OwningPtr<MCInstPrinter> IP(T.createMCInstPrinter(0, *AsmInfo)); 96 if (!IP) { 97 errs() << "error: no instruction printer for target " << Triple << '\n'; 98 return -1; 99 } 100 101 bool ErrorOccurred = false; 102 103 SourceMgr SM; 104 SM.AddNewSourceBuffer(&Buffer, SMLoc()); 105 106 // Convert the input to a vector for disassembly. 107 ByteArrayTy ByteArray; 108 109 StringRef Str = Buffer.getBuffer(); 110 while (!Str.empty()) { 111 // Strip horizontal whitespace. 112 if (size_t Pos = Str.find_first_not_of(" \t\r")) { 113 Str = Str.substr(Pos); 114 continue; 115 } 116 117 // If this is the end of a line or start of a comment, remove the rest of 118 // the line. 119 if (Str[0] == '\n' || Str[0] == '#') { 120 // Strip to the end of line if we already processed any bytes on this 121 // line. This strips the comment and/or the \n. 122 if (Str[0] == '\n') 123 Str = Str.substr(1); 124 else { 125 Str = Str.substr(Str.find_first_of('\n')); 126 if (!Str.empty()) 127 Str = Str.substr(1); 128 } 129 continue; 130 } 131 132 // Get the current token. 133 size_t Next = Str.find_first_of(" \t\n\r#"); 134 StringRef Value = Str.substr(0, Next); 135 136 // Convert to a byte and add to the byte vector. 137 unsigned ByteVal; 138 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) { 139 // If we have an error, print it and skip to the end of line. 140 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), 141 "invalid input token", "error"); 142 ErrorOccurred = true; 143 Str = Str.substr(Str.find('\n')); 144 ByteArray.clear(); 145 continue; 146 } 147 148 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data())); 149 Str = Str.substr(Next); 150 } 151 152 if (!ByteArray.empty()) 153 ErrorOccurred |= PrintInsts(*DisAsm, *IP, ByteArray, SM); 154 155 return ErrorOccurred; 156 } 157