blob: acc5a5f4cab2dfd58c8bc46b03d55d3f90af6040 [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"
Chandler Carruthf010c462012-12-04 10:44:52 +000016#include "llvm/ADT/Triple.h"
Lang Hames508bd632014-04-15 04:40:56 +000017#include "llvm/MC/MCAsmInfo.h"
18#include "llvm/MC/MCContext.h"
Benjamin Kramerb6242a82016-01-26 16:44:37 +000019#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Sean Callananba847da2009-12-17 01:49:59 +000020#include "llvm/MC/MCInst.h"
Lang Hames508bd632014-04-15 04:40:56 +000021#include "llvm/MC/MCRegisterInfo.h"
Richard Bartond0c478d2012-04-16 11:32:10 +000022#include "llvm/MC/MCStreamer.h"
James Molloyb9505852011-09-07 17:24:38 +000023#include "llvm/MC/MCSubtargetInfo.h"
Sean Callananba847da2009-12-17 01:49:59 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerc3de94f2009-12-22 06:45:48 +000025#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
27#include "llvm/Support/raw_ostream.h"
Richard Bartond0c478d2012-04-16 11:32:10 +000028
Sean Callananba847da2009-12-17 01:49:59 +000029using namespace llvm;
30
Rafael Espindola0cb58202014-11-07 17:59:05 +000031typedef std::pair<std::vector<unsigned char>, std::vector<const char *>>
32 ByteArrayTy;
Sean Callananba847da2009-12-17 01:49:59 +000033
Daniel Dunbarc6ab1902010-03-20 22:36:35 +000034static bool PrintInsts(const MCDisassembler &DisAsm,
Richard Bartond0c478d2012-04-16 11:32:10 +000035 const ByteArrayTy &Bytes,
36 SourceMgr &SM, raw_ostream &Out,
David Woodhouse4396f5d2014-01-28 23:12:42 +000037 MCStreamer &Streamer, bool InAtomicBlock,
38 const MCSubtargetInfo &STI) {
Rafael Espindola6a222ec2014-11-12 02:04:27 +000039 ArrayRef<uint8_t> Data(Bytes.first.data(), Bytes.first.size());
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +000040
Sean Callanan2e235a82010-02-03 03:46:41 +000041 // Disassemble it to strings.
Chris Lattner665e9472009-12-22 06:56:51 +000042 uint64_t Size;
Sean Callanan2e235a82010-02-03 03:46:41 +000043 uint64_t Index;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +000044
Rafael Espindola0cb58202014-11-07 17:59:05 +000045 for (Index = 0; Index < Bytes.first.size(); Index += Size) {
Sean Callanan2e235a82010-02-03 03:46:41 +000046 MCInst Inst;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +000047
Owen Anderson83e3f672011-08-17 17:44:15 +000048 MCDisassembler::DecodeStatus S;
Rafael Espindola6a222ec2014-11-12 02:04:27 +000049 S = DisAsm.getInstruction(Inst, Size, Data.slice(Index), Index,
Owen Anderson98c5dda2011-09-15 23:38:46 +000050 /*REMOVE*/ nulls(), nulls());
Owen Anderson83e3f672011-08-17 17:44:15 +000051 switch (S) {
52 case MCDisassembler::Fail:
Rafael Espindola0cb58202014-11-07 17:59:05 +000053 SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
Chris Lattner3f2d5f62011-10-16 05:43:57 +000054 SourceMgr::DK_Warning,
55 "invalid instruction encoding");
Tim Northover38c6ff62013-07-19 10:05:04 +000056 // Don't try to resynchronise the stream in a block
57 if (InAtomicBlock)
58 return true;
59
Sean Callanan2e235a82010-02-03 03:46:41 +000060 if (Size == 0)
61 Size = 1; // skip illegible bytes
Tim Northover38c6ff62013-07-19 10:05:04 +000062
Owen Anderson83e3f672011-08-17 17:44:15 +000063 break;
64
65 case MCDisassembler::SoftFail:
Rafael Espindola0cb58202014-11-07 17:59:05 +000066 SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
Chris Lattner3f2d5f62011-10-16 05:43:57 +000067 SourceMgr::DK_Warning,
68 "potentially undefined instruction encoding");
Justin Bogner7d7a23e2016-08-17 20:30:52 +000069 LLVM_FALLTHROUGH;
Owen Anderson83e3f672011-08-17 17:44:15 +000070
71 case MCDisassembler::Success:
David Woodhouse4396f5d2014-01-28 23:12:42 +000072 Streamer.EmitInstruction(Inst, STI);
Owen Anderson83e3f672011-08-17 17:44:15 +000073 break;
Sean Callanan2e235a82010-02-03 03:46:41 +000074 }
Chris Lattner665e9472009-12-22 06:56:51 +000075 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +000076
Chris Lattner665e9472009-12-22 06:56:51 +000077 return false;
Sean Callananba847da2009-12-17 01:49:59 +000078}
79
Tim Northover38c6ff62013-07-19 10:05:04 +000080static bool SkipToToken(StringRef &Str) {
Colin LeMahieu6d093fd2014-11-11 21:03:09 +000081 for (;;) {
82 if (Str.empty())
83 return false;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +000084
Colin LeMahieu6d093fd2014-11-11 21:03:09 +000085 // Strip horizontal whitespace and commas.
86 if (size_t Pos = Str.find_first_not_of(" \t\r\n,")) {
87 Str = Str.substr(Pos);
Sean Callanan668b1542010-04-12 19:43:00 +000088 continue;
89 }
Tim Northover38c6ff62013-07-19 10:05:04 +000090
Colin LeMahieu6d093fd2014-11-11 21:03:09 +000091 // If this is the start of a comment, remove the rest of the line.
92 if (Str[0] == '#') {
93 Str = Str.substr(Str.find_first_of('\n'));
94 continue;
95 }
96 return true;
97 }
Tim Northover38c6ff62013-07-19 10:05:04 +000098}
99
100
101static bool ByteArrayFromString(ByteArrayTy &ByteArray,
102 StringRef &Str,
103 SourceMgr &SM) {
104 while (SkipToToken(Str)) {
105 // Handled by higher level
106 if (Str[0] == '[' || Str[0] == ']')
107 return false;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000108
Sean Callanan668b1542010-04-12 19:43:00 +0000109 // Get the current token.
Tim Northover38c6ff62013-07-19 10:05:04 +0000110 size_t Next = Str.find_first_of(" \t\n\r,#[]");
Sean Callanan668b1542010-04-12 19:43:00 +0000111 StringRef Value = Str.substr(0, Next);
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000112
Sean Callanan668b1542010-04-12 19:43:00 +0000113 // Convert to a byte and add to the byte vector.
114 unsigned ByteVal;
115 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
116 // If we have an error, print it and skip to the end of line.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000117 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
118 "invalid input token");
Sean Callanan668b1542010-04-12 19:43:00 +0000119 Str = Str.substr(Str.find('\n'));
Rafael Espindola0cb58202014-11-07 17:59:05 +0000120 ByteArray.first.clear();
121 ByteArray.second.clear();
Sean Callanan668b1542010-04-12 19:43:00 +0000122 continue;
123 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000124
Rafael Espindola0cb58202014-11-07 17:59:05 +0000125 ByteArray.first.push_back(ByteVal);
126 ByteArray.second.push_back(Value.data());
Sean Callanan668b1542010-04-12 19:43:00 +0000127 Str = Str.substr(Next);
128 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000129
Sean Callanan668b1542010-04-12 19:43:00 +0000130 return false;
131}
132
Evan Chengb2627992011-07-06 19:45:42 +0000133int Disassembler::disassemble(const Target &T,
Bill Wendlinga5c177e2011-03-21 04:13:46 +0000134 const std::string &Triple,
Richard Bartond0c478d2012-04-16 11:32:10 +0000135 MCSubtargetInfo &STI,
136 MCStreamer &Streamer,
Dan Gohmand5826a32010-08-20 01:07:01 +0000137 MemoryBuffer &Buffer,
Richard Bartond0c478d2012-04-16 11:32:10 +0000138 SourceMgr &SM,
Dan Gohmand5826a32010-08-20 01:07:01 +0000139 raw_ostream &Out) {
Lang Hames508bd632014-04-15 04:40:56 +0000140
141 std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(Triple));
142 if (!MRI) {
143 errs() << "error: no register info for target " << Triple << "\n";
144 return -1;
145 }
146
147 std::unique_ptr<const MCAsmInfo> MAI(T.createMCAsmInfo(*MRI, Triple));
148 if (!MAI) {
149 errs() << "error: no assembly info for target " << Triple << "\n";
150 return -1;
151 }
152
153 // Set up the MCContext for creating symbols and MCExpr's.
Craig Topper573faec2014-04-25 04:24:47 +0000154 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
Lang Hames508bd632014-04-15 04:40:56 +0000155
156 std::unique_ptr<const MCDisassembler> DisAsm(
157 T.createMCDisassembler(STI, Ctx));
Chris Lattner222af462009-12-22 06:24:00 +0000158 if (!DisAsm) {
159 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +0000160 return -1;
161 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000162
Richard Bartond0c478d2012-04-16 11:32:10 +0000163 // Set up initial section manually here
Rafael Espindola90ce9f72014-10-15 16:12:52 +0000164 Streamer.InitSections(false);
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000165
Chris Lattner665e9472009-12-22 06:56:51 +0000166 bool ErrorOccurred = false;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000167
Sean Callananba847da2009-12-17 01:49:59 +0000168 // Convert the input to a vector for disassembly.
Chris Lattner665e9472009-12-22 06:56:51 +0000169 ByteArrayTy ByteArray;
Chris Lattnercfc99a92010-04-09 04:24:20 +0000170 StringRef Str = Buffer.getBuffer();
Tim Northover38c6ff62013-07-19 10:05:04 +0000171 bool InAtomicBlock = false;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000172
Tim Northover38c6ff62013-07-19 10:05:04 +0000173 while (SkipToToken(Str)) {
Rafael Espindola0cb58202014-11-07 17:59:05 +0000174 ByteArray.first.clear();
175 ByteArray.second.clear();
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000176
Tim Northover38c6ff62013-07-19 10:05:04 +0000177 if (Str[0] == '[') {
178 if (InAtomicBlock) {
179 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
180 "nested atomic blocks make no sense");
181 ErrorOccurred = true;
182 }
183 InAtomicBlock = true;
184 Str = Str.drop_front();
185 continue;
186 } else if (Str[0] == ']') {
187 if (!InAtomicBlock) {
188 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
189 "attempt to close atomic block without opening");
190 ErrorOccurred = true;
191 }
192 InAtomicBlock = false;
193 Str = Str.drop_front();
194 continue;
195 }
196
197 // It's a real token, get the bytes and emit them
198 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
199
Rafael Espindola0cb58202014-11-07 17:59:05 +0000200 if (!ByteArray.first.empty())
Tim Northover38c6ff62013-07-19 10:05:04 +0000201 ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer,
David Woodhouse4396f5d2014-01-28 23:12:42 +0000202 InAtomicBlock, STI);
Tim Northover38c6ff62013-07-19 10:05:04 +0000203 }
204
205 if (InAtomicBlock) {
206 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
207 "unclosed atomic block");
208 ErrorOccurred = true;
209 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000210
Chris Lattner665e9472009-12-22 06:56:51 +0000211 return ErrorOccurred;
Sean Callananba847da2009-12-17 01:49:59 +0000212}