Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 1 | //===- HexDisassembler.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 "HexDisassembler.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" |
Chris Lattner | c3de94f | 2009-12-22 06:45:48 +0000 | [diff] [blame^] | 26 | #include "llvm/Support/SourceMgr.h" |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | class VectorMemoryObject : public MemoryObject { |
| 31 | private: |
| 32 | const std::vector<unsigned char> &Bytes; |
| 33 | public: |
| 34 | VectorMemoryObject(const std::vector<unsigned char> &bytes) : |
| 35 | Bytes(bytes) { |
| 36 | } |
| 37 | |
| 38 | uint64_t getBase() const { |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | uint64_t getExtent() const { |
| 43 | return Bytes.size(); |
| 44 | } |
| 45 | |
| 46 | int readByte(uint64_t addr, uint8_t *byte) const { |
| 47 | if (addr > getExtent()) |
| 48 | return -1; |
Chris Lattner | c3de94f | 2009-12-22 06:45:48 +0000 | [diff] [blame^] | 49 | *byte = Bytes[addr]; |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | void printInst(const llvm::MCDisassembler &disassembler, |
| 55 | llvm::MCInstPrinter &instPrinter, |
| 56 | const std::vector<unsigned char> &bytes) { |
| 57 | // Wrap the vector in a MemoryObject. |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 58 | VectorMemoryObject memoryObject(bytes); |
| 59 | |
| 60 | // Disassemble it. |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 61 | MCInst inst; |
| 62 | uint64_t size; |
| 63 | |
| 64 | std::string verboseOStr; |
| 65 | llvm::raw_string_ostream verboseOS(verboseOStr); |
| 66 | |
Chris Lattner | c3de94f | 2009-12-22 06:45:48 +0000 | [diff] [blame^] | 67 | if (disassembler.getInstruction(inst, size, memoryObject, 0, verboseOS)) { |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 68 | instPrinter.printInst(&inst); |
| 69 | outs() << "\n"; |
Chris Lattner | c3de94f | 2009-12-22 06:45:48 +0000 | [diff] [blame^] | 70 | } else { |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 71 | errs() << "error: invalid instruction" << "\n"; |
| 72 | errs() << "Diagnostic log:" << "\n"; |
| 73 | errs() << verboseOStr.c_str() << "\n"; |
| 74 | } |
| 75 | } |
| 76 | |
Chris Lattner | 222af46 | 2009-12-22 06:24:00 +0000 | [diff] [blame] | 77 | int HexDisassembler::disassemble(const Target &T, const std::string &Triple, |
| 78 | MemoryBuffer &Buffer) { |
| 79 | // Set up disassembler. |
| 80 | llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo(T.createAsmInfo(Triple)); |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 222af46 | 2009-12-22 06:24:00 +0000 | [diff] [blame] | 82 | if (!AsmInfo) { |
| 83 | errs() << "error: no assembly info for target " << Triple << "\n"; |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 84 | return -1; |
| 85 | } |
| 86 | |
Chris Lattner | 222af46 | 2009-12-22 06:24:00 +0000 | [diff] [blame] | 87 | llvm::OwningPtr<const llvm::MCDisassembler> DisAsm(T.createMCDisassembler()); |
| 88 | if (!DisAsm) { |
| 89 | errs() << "error: no disassembler for target " << Triple << "\n"; |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | |
Chris Lattner | 222af46 | 2009-12-22 06:24:00 +0000 | [diff] [blame] | 93 | llvm::MCInstPrinter *InstPrinter = T.createMCInstPrinter(0, *AsmInfo, outs()); |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 222af46 | 2009-12-22 06:24:00 +0000 | [diff] [blame] | 95 | if (!InstPrinter) { |
| 96 | errs() << "error: no instruction printer for target " << Triple |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 97 | << "\n"; |
| 98 | return -1; |
| 99 | } |
| 100 | |
Chris Lattner | c3de94f | 2009-12-22 06:45:48 +0000 | [diff] [blame^] | 101 | SourceMgr SourceManager; |
| 102 | SourceManager.AddNewSourceBuffer(&Buffer, SMLoc()); |
| 103 | |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 104 | // Convert the input to a vector for disassembly. |
Chris Lattner | 222af46 | 2009-12-22 06:24:00 +0000 | [diff] [blame] | 105 | std::vector<unsigned char> ByteArray; |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 222af46 | 2009-12-22 06:24:00 +0000 | [diff] [blame] | 107 | StringRef Str = Buffer.getBuffer(); |
Chris Lattner | 222af46 | 2009-12-22 06:24:00 +0000 | [diff] [blame] | 108 | while (!Str.empty()) { |
Chris Lattner | 2adbef0 | 2009-12-22 06:37:58 +0000 | [diff] [blame] | 109 | // Strip horizontal whitespace. |
| 110 | if (size_t Pos = Str.find_first_not_of(" \t\r")) { |
| 111 | Str = Str.substr(Pos); |
| 112 | continue; |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chris Lattner | 2adbef0 | 2009-12-22 06:37:58 +0000 | [diff] [blame] | 115 | // If this is the end of a line or start of a comment, process the |
| 116 | // instruction we have so far. |
| 117 | if (Str[0] == '\n' || Str[0] == '#') { |
| 118 | // If we have bytes to process, do so. |
| 119 | if (!ByteArray.empty()) { |
| 120 | printInst(*DisAsm, *InstPrinter, ByteArray); |
| 121 | ByteArray.clear(); |
| 122 | } |
| 123 | |
| 124 | // Strip to the end of line if we already processed any bytes on this |
| 125 | // line. This strips the comment and/or the \n. |
| 126 | if (Str[0] == '\n') |
| 127 | Str = Str.substr(1); |
| 128 | else { |
| 129 | Str = Str.substr(Str.find_first_of('\n')); |
| 130 | if (!Str.empty()) |
| 131 | Str = Str.substr(1); |
| 132 | } |
| 133 | continue; |
| 134 | } |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 135 | |
| 136 | // Get the current token. |
Chris Lattner | 2adbef0 | 2009-12-22 06:37:58 +0000 | [diff] [blame] | 137 | size_t Next = Str.find_first_of(" \t\n\r#"); |
| 138 | StringRef Value = Str.substr(0, Next); |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 139 | |
| 140 | // Convert to a byte and add to the byte vector. |
Chris Lattner | 222af46 | 2009-12-22 06:24:00 +0000 | [diff] [blame] | 141 | unsigned ByteVal; |
| 142 | if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) { |
Chris Lattner | c3de94f | 2009-12-22 06:45:48 +0000 | [diff] [blame^] | 143 | // If we have an error, print it and skip to the end of line. |
| 144 | SourceManager.PrintMessage(SMLoc::getFromPointer(Value.data()), |
| 145 | "invalid input token", "error"); |
| 146 | Str = Str.substr(Str.find('\n')); |
| 147 | ByteArray.clear(); |
| 148 | continue; |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | c3de94f | 2009-12-22 06:45:48 +0000 | [diff] [blame^] | 150 | |
| 151 | ByteArray.push_back((unsigned char)ByteVal); |
Chris Lattner | 222af46 | 2009-12-22 06:24:00 +0000 | [diff] [blame] | 152 | Str = Str.substr(Next); |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Chris Lattner | 222af46 | 2009-12-22 06:24:00 +0000 | [diff] [blame] | 155 | if (!ByteArray.empty()) |
| 156 | printInst(*DisAsm, *InstPrinter, ByteArray); |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 157 | |
| 158 | return 0; |
| 159 | } |