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