Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame^] | 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 llvm::MCDisassembler &DisAsm, |
| 51 | llvm::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); |
| 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 | llvm::OwningPtr<const llvm::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 | llvm::OwningPtr<const llvm::MCDisassembler> DisAsm(T.createMCDisassembler()); |
| 90 | if (!DisAsm) { |
| 91 | errs() << "error: no disassembler for target " << Triple << "\n"; |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | llvm::MCInstPrinter *InstPrinter = T.createMCInstPrinter(0, *AsmInfo, outs()); |
| 96 | |
| 97 | if (!InstPrinter) { |
| 98 | errs() << "error: no instruction printer for target " << Triple << '\n'; |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | bool ErrorOccurred = false; |
| 103 | |
| 104 | SourceMgr SM; |
| 105 | SM.AddNewSourceBuffer(&Buffer, SMLoc()); |
| 106 | |
| 107 | // Convert the input to a vector for disassembly. |
| 108 | ByteArrayTy ByteArray; |
| 109 | |
| 110 | StringRef Str = Buffer.getBuffer(); |
| 111 | while (!Str.empty()) { |
| 112 | // Strip horizontal whitespace. |
| 113 | if (size_t Pos = Str.find_first_not_of(" \t\r")) { |
| 114 | Str = Str.substr(Pos); |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | // If this is the end of a line or start of a comment, remove the rest of |
| 119 | // the line. |
| 120 | if (Str[0] == '\n' || Str[0] == '#') { |
| 121 | // Strip to the end of line if we already processed any bytes on this |
| 122 | // line. This strips the comment and/or the \n. |
| 123 | if (Str[0] == '\n') |
| 124 | Str = Str.substr(1); |
| 125 | else { |
| 126 | Str = Str.substr(Str.find_first_of('\n')); |
| 127 | if (!Str.empty()) |
| 128 | Str = Str.substr(1); |
| 129 | } |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | // Get the current token. |
| 134 | size_t Next = Str.find_first_of(" \t\n\r#"); |
| 135 | StringRef Value = Str.substr(0, Next); |
| 136 | |
| 137 | // Convert to a byte and add to the byte vector. |
| 138 | unsigned ByteVal; |
| 139 | if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) { |
| 140 | // If we have an error, print it and skip to the end of line. |
| 141 | SM.PrintMessage(SMLoc::getFromPointer(Value.data()), |
| 142 | "invalid input token", "error"); |
| 143 | ErrorOccurred = true; |
| 144 | Str = Str.substr(Str.find('\n')); |
| 145 | ByteArray.clear(); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data())); |
| 150 | Str = Str.substr(Next); |
| 151 | } |
| 152 | |
| 153 | if (!ByteArray.empty()) |
| 154 | ErrorOccurred |= PrintInsts(*DisAsm, *InstPrinter, ByteArray, SM); |
| 155 | |
| 156 | return ErrorOccurred; |
| 157 | } |