blob: 28629c10c1997d5d9246eb2ec7f22ab231c2b6e5 [file] [log] [blame]
Chris Lattnerb257d242009-12-22 22:50:29 +00001//===- Disassembler.cpp - Disassembler for hex strings --------------------===//
Sean Callanan7e645502009-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 Lattnerb257d242009-12-22 22:50:29 +000015#include "Disassembler.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000016#include "llvm/ADT/Triple.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000017#include "llvm/MC/MCAsmInfo.h"
18#include "llvm/MC/MCContext.h"
Sean Callanan7e645502009-12-17 01:49:59 +000019#include "llvm/MC/MCDisassembler.h"
20#include "llvm/MC/MCInst.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000021#include "llvm/MC/MCRegisterInfo.h"
Richard Bartondef81b92012-04-16 11:32:10 +000022#include "llvm/MC/MCStreamer.h"
James Molloy4c493e82011-09-07 17:24:38 +000023#include "llvm/MC/MCSubtargetInfo.h"
Sean Callanan7e645502009-12-17 01:49:59 +000024#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/MemoryObject.h"
Chris Lattner88799182009-12-22 06:45:48 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
28#include "llvm/Support/raw_ostream.h"
Richard Bartondef81b92012-04-16 11:32:10 +000029
Sean Callanan7e645502009-12-17 01:49:59 +000030using namespace llvm;
31
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000032typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
33
34namespace {
Sean Callanan7e645502009-12-17 01:49:59 +000035class VectorMemoryObject : public MemoryObject {
36private:
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000037 const ByteArrayTy &Bytes;
Sean Callanan7e645502009-12-17 01:49:59 +000038public:
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000039 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
Jim Grosbach112a2de2011-05-09 20:05:25 +000040
Craig Toppere56917c2014-03-08 08:27:28 +000041 uint64_t getBase() const override { return 0; }
42 uint64_t getExtent() const override { return Bytes.size(); }
Sean Callanan7e645502009-12-17 01:49:59 +000043
Craig Toppere56917c2014-03-08 08:27:28 +000044 int readByte(uint64_t Addr, uint8_t *Byte) const override {
Rafael Espindola9f9a1062011-01-06 16:48:42 +000045 if (Addr >= getExtent())
Sean Callanan7e645502009-12-17 01:49:59 +000046 return -1;
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000047 *Byte = Bytes[Addr].first;
Sean Callanan7e645502009-12-17 01:49:59 +000048 return 0;
49 }
50};
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000051}
Sean Callanan7e645502009-12-17 01:49:59 +000052
Daniel Dunbard9d5b312010-03-20 22:36:35 +000053static bool PrintInsts(const MCDisassembler &DisAsm,
Richard Bartondef81b92012-04-16 11:32:10 +000054 const ByteArrayTy &Bytes,
55 SourceMgr &SM, raw_ostream &Out,
David Woodhousee6c13e42014-01-28 23:12:42 +000056 MCStreamer &Streamer, bool InAtomicBlock,
57 const MCSubtargetInfo &STI) {
Sean Callanan7e645502009-12-17 01:49:59 +000058 // Wrap the vector in a MemoryObject.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000059 VectorMemoryObject memoryObject(Bytes);
Jim Grosbach112a2de2011-05-09 20:05:25 +000060
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000061 // Disassemble it to strings.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000062 uint64_t Size;
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000063 uint64_t Index;
Jim Grosbach112a2de2011-05-09 20:05:25 +000064
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000065 for (Index = 0; Index < Bytes.size(); Index += Size) {
66 MCInst Inst;
Jim Grosbach112a2de2011-05-09 20:05:25 +000067
Owen Andersona4043c42011-08-17 17:44:15 +000068 MCDisassembler::DecodeStatus S;
69 S = DisAsm.getInstruction(Inst, Size, memoryObject, Index,
Owen Andersona0c3b972011-09-15 23:38:46 +000070 /*REMOVE*/ nulls(), nulls());
Owen Andersona4043c42011-08-17 17:44:15 +000071 switch (S) {
72 case MCDisassembler::Fail:
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000073 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
Chris Lattner03b80a42011-10-16 05:43:57 +000074 SourceMgr::DK_Warning,
75 "invalid instruction encoding");
Tim Northover48cf6cc2013-07-19 10:05:04 +000076 // Don't try to resynchronise the stream in a block
77 if (InAtomicBlock)
78 return true;
79
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000080 if (Size == 0)
81 Size = 1; // skip illegible bytes
Tim Northover48cf6cc2013-07-19 10:05:04 +000082
Owen Andersona4043c42011-08-17 17:44:15 +000083 break;
84
85 case MCDisassembler::SoftFail:
86 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
Chris Lattner03b80a42011-10-16 05:43:57 +000087 SourceMgr::DK_Warning,
88 "potentially undefined instruction encoding");
Owen Andersona4043c42011-08-17 17:44:15 +000089 // Fall through
90
91 case MCDisassembler::Success:
David Woodhousee6c13e42014-01-28 23:12:42 +000092 Streamer.EmitInstruction(Inst, STI);
Owen Andersona4043c42011-08-17 17:44:15 +000093 break;
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000094 }
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000095 }
Jim Grosbach112a2de2011-05-09 20:05:25 +000096
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000097 return false;
Sean Callanan7e645502009-12-17 01:49:59 +000098}
99
Tim Northover48cf6cc2013-07-19 10:05:04 +0000100static bool SkipToToken(StringRef &Str) {
101 while (!Str.empty() && Str.find_first_not_of(" \t\r\n#,") != 0) {
102 // Strip horizontal whitespace and commas.
103 if (size_t Pos = Str.find_first_not_of(" \t\r,")) {
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000104 Str = Str.substr(Pos);
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000105 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000106
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000107 // If this is the end of a line or start of a comment, remove the rest of
108 // the line.
109 if (Str[0] == '\n' || Str[0] == '#') {
110 // Strip to the end of line if we already processed any bytes on this
111 // line. This strips the comment and/or the \n.
112 if (Str[0] == '\n') {
113 Str = Str.substr(1);
114 } else {
115 Str = Str.substr(Str.find_first_of('\n'));
116 if (!Str.empty())
117 Str = Str.substr(1);
118 }
119 continue;
120 }
Tim Northover48cf6cc2013-07-19 10:05:04 +0000121 }
122
123 return !Str.empty();
124}
125
126
127static bool ByteArrayFromString(ByteArrayTy &ByteArray,
128 StringRef &Str,
129 SourceMgr &SM) {
130 while (SkipToToken(Str)) {
131 // Handled by higher level
132 if (Str[0] == '[' || Str[0] == ']')
133 return false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000134
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000135 // Get the current token.
Tim Northover48cf6cc2013-07-19 10:05:04 +0000136 size_t Next = Str.find_first_of(" \t\n\r,#[]");
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000137 StringRef Value = Str.substr(0, Next);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000138
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000139 // Convert to a byte and add to the byte vector.
140 unsigned ByteVal;
141 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
142 // If we have an error, print it and skip to the end of line.
Chris Lattner03b80a42011-10-16 05:43:57 +0000143 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
144 "invalid input token");
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000145 Str = Str.substr(Str.find('\n'));
146 ByteArray.clear();
147 continue;
148 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000149
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000150 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
151 Str = Str.substr(Next);
152 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000153
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000154 return false;
155}
156
Evan Chengab37af92011-07-06 19:45:42 +0000157int Disassembler::disassemble(const Target &T,
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000158 const std::string &Triple,
Richard Bartondef81b92012-04-16 11:32:10 +0000159 MCSubtargetInfo &STI,
160 MCStreamer &Streamer,
Dan Gohman268b0f42010-08-20 01:07:01 +0000161 MemoryBuffer &Buffer,
Richard Bartondef81b92012-04-16 11:32:10 +0000162 SourceMgr &SM,
Dan Gohman268b0f42010-08-20 01:07:01 +0000163 raw_ostream &Out) {
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000164
165 std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(Triple));
166 if (!MRI) {
167 errs() << "error: no register info for target " << Triple << "\n";
168 return -1;
169 }
170
171 std::unique_ptr<const MCAsmInfo> MAI(T.createMCAsmInfo(*MRI, Triple));
172 if (!MAI) {
173 errs() << "error: no assembly info for target " << Triple << "\n";
174 return -1;
175 }
176
177 // Set up the MCContext for creating symbols and MCExpr's.
178 MCContext Ctx(MAI.get(), MRI.get(), 0);
179
180 std::unique_ptr<const MCDisassembler> DisAsm(
181 T.createMCDisassembler(STI, Ctx));
Chris Lattner72cbaa22009-12-22 06:24:00 +0000182 if (!DisAsm) {
183 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callanan7e645502009-12-17 01:49:59 +0000184 return -1;
185 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000186
Richard Bartondef81b92012-04-16 11:32:10 +0000187 // Set up initial section manually here
188 Streamer.InitSections();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000189
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000190 bool ErrorOccurred = false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000191
Sean Callanan7e645502009-12-17 01:49:59 +0000192 // Convert the input to a vector for disassembly.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000193 ByteArrayTy ByteArray;
Chris Lattner55ddc302010-04-09 04:24:20 +0000194 StringRef Str = Buffer.getBuffer();
Tim Northover48cf6cc2013-07-19 10:05:04 +0000195 bool InAtomicBlock = false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000196
Tim Northover48cf6cc2013-07-19 10:05:04 +0000197 while (SkipToToken(Str)) {
198 ByteArray.clear();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000199
Tim Northover48cf6cc2013-07-19 10:05:04 +0000200 if (Str[0] == '[') {
201 if (InAtomicBlock) {
202 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
203 "nested atomic blocks make no sense");
204 ErrorOccurred = true;
205 }
206 InAtomicBlock = true;
207 Str = Str.drop_front();
208 continue;
209 } else if (Str[0] == ']') {
210 if (!InAtomicBlock) {
211 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
212 "attempt to close atomic block without opening");
213 ErrorOccurred = true;
214 }
215 InAtomicBlock = false;
216 Str = Str.drop_front();
217 continue;
218 }
219
220 // It's a real token, get the bytes and emit them
221 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
222
223 if (!ByteArray.empty())
224 ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer,
David Woodhousee6c13e42014-01-28 23:12:42 +0000225 InAtomicBlock, STI);
Tim Northover48cf6cc2013-07-19 10:05:04 +0000226 }
227
228 if (InAtomicBlock) {
229 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
230 "unclosed atomic block");
231 ErrorOccurred = true;
232 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000233
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000234 return ErrorOccurred;
Sean Callanan7e645502009-12-17 01:49:59 +0000235}