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