blob: 88464a5a5a4d7ce37e5ad9911d38059d154ad628 [file] [log] [blame]
Sean Callananba847da2009-12-17 01:49:59 +00001//===- 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
29using namespace llvm;
30
31class VectorMemoryObject : public MemoryObject {
32private:
33 const std::vector<unsigned char> &Bytes;
34public:
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
57void 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
87int HexDisassembler::disassemble(const Target &target,
88 const std::string &tripleString,
89 MemoryBuffer &buffer) {
90 // Set up disassembler
91
92 llvm::OwningPtr<const llvm::MCAsmInfo> asmInfo
93 (target.createAsmInfo(tripleString));
94
95 if (!asmInfo) {
96 errs() << "error: no assembly info for target " << tripleString << "\n";
97 return -1;
98 }
99
100 llvm::OwningPtr<const llvm::MCDisassembler> disassembler
101 (target.createMCDisassembler());
102
103 if (!disassembler) {
104 errs() << "error: no disassembler for target " << tripleString << "\n";
105 return -1;
106 }
107
108 llvm::MCInstPrinter *instPrinter = target.createMCInstPrinter(0,
109 *asmInfo,
110 outs());
111
112 if (!instPrinter) {
113 errs() << "error: no instruction printer for target " << tripleString
114 << "\n";
115 return -1;
116 }
117
118 // Convert the input to a vector for disassembly.
119
120 std::vector<unsigned char> bytes;
121
122 StringRef str = buffer.getBuffer();
123
124 while (!str.empty()) {
125 if(str.find_first_of("\n") < str.find_first_not_of(" \t\n\r")) {
126 printInst(*disassembler, *instPrinter, bytes);
127
128 bytes.clear();
129 }
130
131 // Skip leading space.
132 str = str.substr(str.find_first_not_of(" \t\n\r"));
133
134 // Get the current token.
135 size_t next = str.find_first_of(" \t\n\r");
136
137 if(next == (size_t)StringRef::npos)
138 break;
139
140 StringRef value = str.slice(0, next);
141
142 // Convert to a byte and add to the byte vector.
143 unsigned byte;
144 if (value.getAsInteger(0, byte) || byte > 255) {
145 errs() << "warning: invalid input token '" << value << "' of length "
146 << next << "\n";
147 }
148 else {
149 bytes.push_back((unsigned char)byte);
150 }
151 str = str.substr(next);
152 }
153
154 if (!bytes.empty())
155 printInst(*disassembler, *instPrinter, bytes);
156
157 return 0;
158}