blob: 4a63e99a6e7a62ee5ecfbc56fb284acaba67b26e [file] [log] [blame]
Chris Lattnera3dcfb12009-12-22 22:50:29 +00001//===- Disassembler.cpp - Disassembler for hex strings --------------------===//
Sean Callananba847da2009-12-17 01:49:59 +00002//
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
Chris Lattnera3dcfb12009-12-22 22:50:29 +000015#include "Disassembler.h"
Sean Callananba847da2009-12-17 01:49:59 +000016
17#include "llvm/ADT/OwningPtr.h"
Sean Callanan668b1542010-04-12 19:43:00 +000018#include "llvm/ADT/Triple.h"
Sean Callananba847da2009-12-17 01:49:59 +000019#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCDisassembler.h"
21#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCInstPrinter.h"
23#include "llvm/Target/TargetRegistry.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/MemoryObject.h"
26#include "llvm/Support/raw_ostream.h"
Chris Lattnerc3de94f2009-12-22 06:45:48 +000027#include "llvm/Support/SourceMgr.h"
Sean Callanan668b1542010-04-12 19:43:00 +000028
29#include "llvm-c/EnhancedDisassembly.h"
30
Sean Callananba847da2009-12-17 01:49:59 +000031using namespace llvm;
32
Chris Lattner665e9472009-12-22 06:56:51 +000033typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
34
35namespace {
Sean Callananba847da2009-12-17 01:49:59 +000036class VectorMemoryObject : public MemoryObject {
37private:
Chris Lattner665e9472009-12-22 06:56:51 +000038 const ByteArrayTy &Bytes;
Sean Callananba847da2009-12-17 01:49:59 +000039public:
Chris Lattner665e9472009-12-22 06:56:51 +000040 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
Sean Callananba847da2009-12-17 01:49:59 +000041
Chris Lattner665e9472009-12-22 06:56:51 +000042 uint64_t getBase() const { return 0; }
43 uint64_t getExtent() const { return Bytes.size(); }
Sean Callananba847da2009-12-17 01:49:59 +000044
Chris Lattner665e9472009-12-22 06:56:51 +000045 int readByte(uint64_t Addr, uint8_t *Byte) const {
46 if (Addr > getExtent())
Sean Callananba847da2009-12-17 01:49:59 +000047 return -1;
Chris Lattner665e9472009-12-22 06:56:51 +000048 *Byte = Bytes[Addr].first;
Sean Callananba847da2009-12-17 01:49:59 +000049 return 0;
50 }
51};
Chris Lattner665e9472009-12-22 06:56:51 +000052}
Sean Callananba847da2009-12-17 01:49:59 +000053
Daniel Dunbarc6ab1902010-03-20 22:36:35 +000054static bool PrintInsts(const MCDisassembler &DisAsm,
Chris Lattnerd3740872010-04-04 05:04:31 +000055 MCInstPrinter &Printer, const ByteArrayTy &Bytes,
56 SourceMgr &SM) {
Sean Callananba847da2009-12-17 01:49:59 +000057 // Wrap the vector in a MemoryObject.
Chris Lattner665e9472009-12-22 06:56:51 +000058 VectorMemoryObject memoryObject(Bytes);
Sean Callananba847da2009-12-17 01:49:59 +000059
Sean Callanan2e235a82010-02-03 03:46:41 +000060 // Disassemble it to strings.
Chris Lattner665e9472009-12-22 06:56:51 +000061 uint64_t Size;
Sean Callanan2e235a82010-02-03 03:46:41 +000062 uint64_t Index;
Sean Callananba847da2009-12-17 01:49:59 +000063
Sean Callanan2e235a82010-02-03 03:46:41 +000064 for (Index = 0; Index < Bytes.size(); Index += Size) {
65 MCInst Inst;
66
67 if (DisAsm.getInstruction(Inst, Size, memoryObject, Index,
68 /*REMOVE*/ nulls())) {
Chris Lattnerd3740872010-04-04 05:04:31 +000069 Printer.printInst(&Inst, outs());
Sean Callanan2e235a82010-02-03 03:46:41 +000070 outs() << "\n";
Sean Callanan668b1542010-04-12 19:43:00 +000071 } else {
Sean Callanan2e235a82010-02-03 03:46:41 +000072 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
73 "invalid instruction encoding", "warning");
74 if (Size == 0)
75 Size = 1; // skip illegible bytes
76 }
Chris Lattner665e9472009-12-22 06:56:51 +000077 }
78
79 return false;
Sean Callananba847da2009-12-17 01:49:59 +000080}
81
Sean Callanan668b1542010-04-12 19:43:00 +000082static bool ByteArrayFromString(ByteArrayTy &ByteArray,
83 StringRef &Str,
84 SourceMgr &SM) {
85 while (!Str.empty()) {
86 // Strip horizontal whitespace.
87 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
88 Str = Str.substr(Pos);
89 continue;
90 }
91
92 // If this is the end of a line or start of a comment, remove the rest of
93 // the line.
94 if (Str[0] == '\n' || Str[0] == '#') {
95 // Strip to the end of line if we already processed any bytes on this
96 // line. This strips the comment and/or the \n.
97 if (Str[0] == '\n') {
98 Str = Str.substr(1);
99 } else {
100 Str = Str.substr(Str.find_first_of('\n'));
101 if (!Str.empty())
102 Str = Str.substr(1);
103 }
104 continue;
105 }
106
107 // Get the current token.
108 size_t Next = Str.find_first_of(" \t\n\r#");
109 StringRef Value = Str.substr(0, Next);
110
111 // Convert to a byte and add to the byte vector.
112 unsigned ByteVal;
113 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
114 // If we have an error, print it and skip to the end of line.
115 SM.PrintMessage(SMLoc::getFromPointer(Value.data()),
116 "invalid input token", "error");
117 Str = Str.substr(Str.find('\n'));
118 ByteArray.clear();
119 continue;
120 }
121
122 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
123 Str = Str.substr(Next);
124 }
125
126 return false;
127}
128
Chris Lattnera3dcfb12009-12-22 22:50:29 +0000129int Disassembler::disassemble(const Target &T, const std::string &Triple,
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000130 MemoryBuffer &Buffer) {
Chris Lattner222af462009-12-22 06:24:00 +0000131 // Set up disassembler.
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000132 OwningPtr<const MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
Sean Callananba847da2009-12-17 01:49:59 +0000133
Chris Lattner222af462009-12-22 06:24:00 +0000134 if (!AsmInfo) {
135 errs() << "error: no assembly info for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +0000136 return -1;
137 }
138
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000139 OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler());
Chris Lattner222af462009-12-22 06:24:00 +0000140 if (!DisAsm) {
141 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +0000142 return -1;
143 }
144
Chris Lattnerd3740872010-04-04 05:04:31 +0000145 OwningPtr<MCInstPrinter> IP(T.createMCInstPrinter(0, *AsmInfo));
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000146 if (!IP) {
Chris Lattner665e9472009-12-22 06:56:51 +0000147 errs() << "error: no instruction printer for target " << Triple << '\n';
Sean Callananba847da2009-12-17 01:49:59 +0000148 return -1;
149 }
150
Chris Lattner665e9472009-12-22 06:56:51 +0000151 bool ErrorOccurred = false;
152
153 SourceMgr SM;
154 SM.AddNewSourceBuffer(&Buffer, SMLoc());
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000155
Sean Callananba847da2009-12-17 01:49:59 +0000156 // Convert the input to a vector for disassembly.
Chris Lattner665e9472009-12-22 06:56:51 +0000157 ByteArrayTy ByteArray;
Chris Lattnercfc99a92010-04-09 04:24:20 +0000158 StringRef Str = Buffer.getBuffer();
Sean Callanan668b1542010-04-12 19:43:00 +0000159
160 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
Sean Callananba847da2009-12-17 01:49:59 +0000161
Chris Lattner222af462009-12-22 06:24:00 +0000162 if (!ByteArray.empty())
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000163 ErrorOccurred |= PrintInsts(*DisAsm, *IP, ByteArray, SM);
Sean Callananba847da2009-12-17 01:49:59 +0000164
Chris Lattner665e9472009-12-22 06:56:51 +0000165 return ErrorOccurred;
Sean Callananba847da2009-12-17 01:49:59 +0000166}
Sean Callanan668b1542010-04-12 19:43:00 +0000167
168static int byteArrayReader(uint8_t *B, uint64_t A, void *Arg) {
169 ByteArrayTy &ByteArray = *((ByteArrayTy*)Arg);
170
171 if (A >= ByteArray.size())
172 return -1;
173
174 *B = ByteArray[A].first;
175
176 return 0;
177}
178
179static int verboseEvaluator(uint64_t *V, unsigned R, void *Arg) {
180 EDDisassemblerRef &disassembler = *((EDDisassemblerRef*)Arg);
181
182 const char *regName;
183
184 if (!EDGetRegisterName(&regName,
185 disassembler,
186 R))
187 outs() << "[" << regName << "/" << R << "]";
188 if (EDRegisterIsStackPointer(disassembler, R))
189 outs() << "(sp)";
190 if (EDRegisterIsProgramCounter(disassembler, R))
191 outs() << "(pc)";
192
193 *V = 0;
194
195 return 0;
196}
197
198int Disassembler::disassembleEnhanced(const std::string &TS,
199 MemoryBuffer &Buffer) {
200 ByteArrayTy ByteArray;
201 StringRef Str = Buffer.getBuffer();
202 SourceMgr SM;
203
204 SM.AddNewSourceBuffer(&Buffer, SMLoc());
205
206 if (ByteArrayFromString(ByteArray, Str, SM)) {
207 return -1;
208 }
209
210 EDDisassemblerRef disassembler;
211
212 Triple T(TS);
213 EDAssemblySyntax_t AS;
214
215 switch (T.getArch()) {
216 default:
217 errs() << "error: no default assembly syntax for " << TS.c_str() << "\n";
218 return -1;
219 case Triple::arm:
220 case Triple::thumb:
221 AS = kEDAssemblySyntaxARMUAL;
222 break;
223 case Triple::x86:
224 case Triple::x86_64:
225 AS = kEDAssemblySyntaxX86ATT;
226 break;
227 }
228
229 if (EDGetDisassembler(&disassembler,
230 TS.c_str(),
231 AS)) {
232 errs() << "error: couldn't get disassembler for " << TS.c_str() << "\n";
233 return -1;
234 }
235
236 EDInstRef inst;
237
238 if (EDCreateInsts(&inst, 1, disassembler, byteArrayReader, 0,&ByteArray)
239 != 1) {
240 errs() << "error: Didn't get an instruction\n";
241 return -1;
242 }
243
244 int numTokens = EDNumTokens(inst);
245
246 if (numTokens < 0) {
247 errs() << "error: Couldn't count the instruction's tokens\n";
248 return -1;
249 }
250
251 int tokenIndex;
252
253 for (tokenIndex = 0; tokenIndex < numTokens; ++tokenIndex) {
254 EDTokenRef token;
255
256 if (EDGetToken(&token, inst, tokenIndex)) {
257 errs() << "error: Couldn't get token\n";
258 return -1;
259 }
260
261 const char *buf;
262
263 if (EDGetTokenString(&buf, token)) {
264 errs() << "error: Couldn't get string for token\n";
265 return -1;
266 }
267
268 outs() << "[";
269
270 int operandIndex = EDOperandIndexForToken(token);
271
272 if (operandIndex >= 0)
273 outs() << operandIndex << "-";
274
275 if (EDTokenIsWhitespace(token)) {
276 outs() << "w";
277 } else if (EDTokenIsPunctuation(token)) {
278 outs() << "p";
279 } else if (EDTokenIsOpcode(token)) {
280 outs() << "o";
281 } else if (EDTokenIsLiteral(token)) {
282 outs() << "l";
283 } else if (EDTokenIsRegister(token)) {
284 outs() << "r";
285 } else {
286 outs() << "?";
287 }
288
289 outs() << ":" << buf;
290
291 if (EDTokenIsLiteral(token)) {
292 outs() << "=";
293 if (EDTokenIsNegativeLiteral(token))
294 outs() << "-";
295 uint64_t absoluteValue;
296 if (EDLiteralTokenAbsoluteValue(&absoluteValue, token)) {
297 errs() << "error: Couldn't get the value of a literal token\n";
298 return -1;
299 }
300 outs() << absoluteValue;
301 } else if (EDTokenIsRegister(token)) {
302 outs() << "=";
303 unsigned regID;
304 if (EDRegisterTokenValue(&regID, token)) {
305 errs() << "error: Couldn't get the ID of a register token\n";
306 return -1;
307 }
308 outs() << "r" << regID;
309 }
310
311 outs() << "]";
312 }
313
314 outs() << " ";
315
316 int numOperands = EDNumOperands(inst);
317
318 if (numOperands < 0) {
319 errs() << "error: Couldn't count operands\n";
320 return -1;
321 }
322
323 int operandIndex;
324
325 for (operandIndex = 0; operandIndex < numOperands; ++operandIndex) {
326 outs() << operandIndex << ":";
327
328 EDOperandRef operand;
329
330 if (EDGetOperand(&operand,
331 inst,
332 operandIndex)) {
333 errs() << "error: Couldn't get operand\n";
334 return -1;
335 }
336
337 uint64_t evaluatedResult;
338
339 EDEvaluateOperand(&evaluatedResult,
340 operand,
341 verboseEvaluator,
342 &disassembler);
343
344 outs() << " ";
345 }
346
347 outs() << "\n";
348
349 return 0;
350}
351