2009-12-23 06:50:29 +08:00
|
|
|
//===- Disassembler.cpp - Disassembler for hex strings --------------------===//
|
2009-12-17 09:49:59 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class implements the disassembler of strings of bytes written in
|
|
|
|
// hexadecimal, from standard input or from a file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-12-23 06:50:29 +08:00
|
|
|
#include "Disassembler.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2014-04-15 12:40:56 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2016-01-27 00:44:37 +08:00
|
|
|
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
|
2009-12-17 09:49:59 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2014-04-15 12:40:56 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2012-04-16 19:32:10 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2011-09-08 01:24:38 +08:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2009-12-17 09:49:59 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-12-22 14:45:48 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2011-08-25 02:08:43 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-04-16 19:32:10 +08:00
|
|
|
|
2009-12-17 09:49:59 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-11-08 01:59:05 +08:00
|
|
|
typedef std::pair<std::vector<unsigned char>, std::vector<const char *>>
|
|
|
|
ByteArrayTy;
|
2009-12-17 09:49:59 +08:00
|
|
|
|
2010-03-21 06:36:35 +08:00
|
|
|
static bool PrintInsts(const MCDisassembler &DisAsm,
|
2012-04-16 19:32:10 +08:00
|
|
|
const ByteArrayTy &Bytes,
|
|
|
|
SourceMgr &SM, raw_ostream &Out,
|
2014-01-29 07:12:42 +08:00
|
|
|
MCStreamer &Streamer, bool InAtomicBlock,
|
|
|
|
const MCSubtargetInfo &STI) {
|
2014-11-12 10:04:27 +08:00
|
|
|
ArrayRef<uint8_t> Data(Bytes.first.data(), Bytes.first.size());
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2010-02-03 11:46:41 +08:00
|
|
|
// Disassemble it to strings.
|
2009-12-22 14:56:51 +08:00
|
|
|
uint64_t Size;
|
2010-02-03 11:46:41 +08:00
|
|
|
uint64_t Index;
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2014-11-08 01:59:05 +08:00
|
|
|
for (Index = 0; Index < Bytes.first.size(); Index += Size) {
|
2010-02-03 11:46:41 +08:00
|
|
|
MCInst Inst;
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2011-08-18 01:44:15 +08:00
|
|
|
MCDisassembler::DecodeStatus S;
|
2014-11-12 10:04:27 +08:00
|
|
|
S = DisAsm.getInstruction(Inst, Size, Data.slice(Index), Index,
|
2011-09-16 07:38:46 +08:00
|
|
|
/*REMOVE*/ nulls(), nulls());
|
2011-08-18 01:44:15 +08:00
|
|
|
switch (S) {
|
|
|
|
case MCDisassembler::Fail:
|
2014-11-08 01:59:05 +08:00
|
|
|
SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
|
2011-10-16 13:43:57 +08:00
|
|
|
SourceMgr::DK_Warning,
|
|
|
|
"invalid instruction encoding");
|
2013-07-19 18:05:04 +08:00
|
|
|
// Don't try to resynchronise the stream in a block
|
|
|
|
if (InAtomicBlock)
|
|
|
|
return true;
|
|
|
|
|
2010-02-03 11:46:41 +08:00
|
|
|
if (Size == 0)
|
|
|
|
Size = 1; // skip illegible bytes
|
2013-07-19 18:05:04 +08:00
|
|
|
|
2011-08-18 01:44:15 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MCDisassembler::SoftFail:
|
2014-11-08 01:59:05 +08:00
|
|
|
SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
|
2011-10-16 13:43:57 +08:00
|
|
|
SourceMgr::DK_Warning,
|
|
|
|
"potentially undefined instruction encoding");
|
2011-08-18 01:44:15 +08:00
|
|
|
// Fall through
|
|
|
|
|
|
|
|
case MCDisassembler::Success:
|
2014-01-29 07:12:42 +08:00
|
|
|
Streamer.EmitInstruction(Inst, STI);
|
2011-08-18 01:44:15 +08:00
|
|
|
break;
|
2010-02-03 11:46:41 +08:00
|
|
|
}
|
2009-12-17 09:49:59 +08:00
|
|
|
}
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2009-12-22 14:56:51 +08:00
|
|
|
return false;
|
2009-12-17 09:49:59 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 18:05:04 +08:00
|
|
|
static bool SkipToToken(StringRef &Str) {
|
2014-11-12 05:03:09 +08:00
|
|
|
for (;;) {
|
|
|
|
if (Str.empty())
|
|
|
|
return false;
|
|
|
|
|
2013-07-19 18:05:04 +08:00
|
|
|
// Strip horizontal whitespace and commas.
|
2014-11-12 05:03:09 +08:00
|
|
|
if (size_t Pos = Str.find_first_not_of(" \t\r\n,")) {
|
2009-12-22 14:37:58 +08:00
|
|
|
Str = Str.substr(Pos);
|
2014-11-12 05:03:09 +08:00
|
|
|
continue;
|
2009-12-22 14:37:58 +08:00
|
|
|
}
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2014-11-12 05:03:09 +08:00
|
|
|
// If this is the start of a comment, remove the rest of the line.
|
|
|
|
if (Str[0] == '#') {
|
2009-12-22 14:37:58 +08:00
|
|
|
Str = Str.substr(Str.find_first_of('\n'));
|
|
|
|
continue;
|
2009-12-17 09:49:59 +08:00
|
|
|
}
|
2014-11-12 05:03:09 +08:00
|
|
|
return true;
|
2013-07-19 18:05:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool ByteArrayFromString(ByteArrayTy &ByteArray,
|
|
|
|
StringRef &Str,
|
|
|
|
SourceMgr &SM) {
|
|
|
|
while (SkipToToken(Str)) {
|
|
|
|
// Handled by higher level
|
|
|
|
if (Str[0] == '[' || Str[0] == ']')
|
|
|
|
return false;
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2009-12-17 09:49:59 +08:00
|
|
|
// Get the current token.
|
2013-07-19 18:05:04 +08:00
|
|
|
size_t Next = Str.find_first_of(" \t\n\r,#[]");
|
2009-12-22 14:37:58 +08:00
|
|
|
StringRef Value = Str.substr(0, Next);
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2009-12-17 09:49:59 +08:00
|
|
|
// Convert to a byte and add to the byte vector.
|
2009-12-22 14:24:00 +08:00
|
|
|
unsigned ByteVal;
|
|
|
|
if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
|
2009-12-22 14:45:48 +08:00
|
|
|
// If we have an error, print it and skip to the end of line.
|
2011-10-16 13:43:57 +08:00
|
|
|
SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
|
|
|
|
"invalid input token");
|
2009-12-22 14:45:48 +08:00
|
|
|
Str = Str.substr(Str.find('\n'));
|
2014-11-08 01:59:05 +08:00
|
|
|
ByteArray.first.clear();
|
|
|
|
ByteArray.second.clear();
|
2009-12-22 14:45:48 +08:00
|
|
|
continue;
|
2009-12-17 09:49:59 +08:00
|
|
|
}
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2014-11-08 01:59:05 +08:00
|
|
|
ByteArray.first.push_back(ByteVal);
|
|
|
|
ByteArray.second.push_back(Value.data());
|
2009-12-22 14:24:00 +08:00
|
|
|
Str = Str.substr(Next);
|
2009-12-17 09:49:59 +08:00
|
|
|
}
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2010-04-13 03:43:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-07 03:45:42 +08:00
|
|
|
int Disassembler::disassemble(const Target &T,
|
2011-03-21 12:13:46 +08:00
|
|
|
const std::string &Triple,
|
2012-04-16 19:32:10 +08:00
|
|
|
MCSubtargetInfo &STI,
|
|
|
|
MCStreamer &Streamer,
|
2010-08-20 09:07:01 +08:00
|
|
|
MemoryBuffer &Buffer,
|
2012-04-16 19:32:10 +08:00
|
|
|
SourceMgr &SM,
|
2010-08-20 09:07:01 +08:00
|
|
|
raw_ostream &Out) {
|
2014-04-15 12:40:56 +08:00
|
|
|
|
|
|
|
std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(Triple));
|
|
|
|
if (!MRI) {
|
|
|
|
errs() << "error: no register info for target " << Triple << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<const MCAsmInfo> MAI(T.createMCAsmInfo(*MRI, Triple));
|
|
|
|
if (!MAI) {
|
|
|
|
errs() << "error: no assembly info for target " << Triple << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the MCContext for creating symbols and MCExpr's.
|
2014-04-25 12:24:47 +08:00
|
|
|
MCContext Ctx(MAI.get(), MRI.get(), nullptr);
|
2014-04-15 12:40:56 +08:00
|
|
|
|
|
|
|
std::unique_ptr<const MCDisassembler> DisAsm(
|
|
|
|
T.createMCDisassembler(STI, Ctx));
|
2010-04-13 03:43:00 +08:00
|
|
|
if (!DisAsm) {
|
|
|
|
errs() << "error: no disassembler for target " << Triple << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2012-04-16 19:32:10 +08:00
|
|
|
// Set up initial section manually here
|
2014-10-16 00:12:52 +08:00
|
|
|
Streamer.InitSections(false);
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2010-04-13 03:43:00 +08:00
|
|
|
bool ErrorOccurred = false;
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2010-04-13 03:43:00 +08:00
|
|
|
// Convert the input to a vector for disassembly.
|
|
|
|
ByteArrayTy ByteArray;
|
|
|
|
StringRef Str = Buffer.getBuffer();
|
2013-07-19 18:05:04 +08:00
|
|
|
bool InAtomicBlock = false;
|
|
|
|
|
|
|
|
while (SkipToToken(Str)) {
|
2014-11-08 01:59:05 +08:00
|
|
|
ByteArray.first.clear();
|
|
|
|
ByteArray.second.clear();
|
2013-07-19 18:05:04 +08:00
|
|
|
|
|
|
|
if (Str[0] == '[') {
|
|
|
|
if (InAtomicBlock) {
|
|
|
|
SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
|
|
|
|
"nested atomic blocks make no sense");
|
|
|
|
ErrorOccurred = true;
|
|
|
|
}
|
|
|
|
InAtomicBlock = true;
|
|
|
|
Str = Str.drop_front();
|
|
|
|
continue;
|
|
|
|
} else if (Str[0] == ']') {
|
|
|
|
if (!InAtomicBlock) {
|
|
|
|
SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
|
|
|
|
"attempt to close atomic block without opening");
|
|
|
|
ErrorOccurred = true;
|
|
|
|
}
|
|
|
|
InAtomicBlock = false;
|
|
|
|
Str = Str.drop_front();
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2013-07-19 18:05:04 +08:00
|
|
|
// It's a real token, get the bytes and emit them
|
|
|
|
ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2014-11-08 01:59:05 +08:00
|
|
|
if (!ByteArray.first.empty())
|
2013-07-19 18:05:04 +08:00
|
|
|
ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer,
|
2014-01-29 07:12:42 +08:00
|
|
|
InAtomicBlock, STI);
|
2013-07-19 18:05:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (InAtomicBlock) {
|
|
|
|
SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
|
|
|
|
"unclosed atomic block");
|
|
|
|
ErrorOccurred = true;
|
|
|
|
}
|
2011-05-10 04:05:25 +08:00
|
|
|
|
2009-12-22 14:56:51 +08:00
|
|
|
return ErrorOccurred;
|
2009-12-17 09:49:59 +08:00
|
|
|
}
|