2011-09-01 21:09:04 +00:00
|
|
|
//===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
|
2011-03-28 18:25:07 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-05-22 04:52:24 +00:00
|
|
|
|
2011-03-28 18:25:07 +00:00
|
|
|
#include "Disassembler.h"
|
|
|
|
#include "llvm-c/Disassembler.h"
|
|
|
|
|
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2011-07-20 06:54:19 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2011-03-28 18:25:07 +00:00
|
|
|
#include "llvm/MC/MCDisassembler.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
2011-07-20 06:54:19 +00:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2011-03-28 18:25:07 +00:00
|
|
|
#include "llvm/Support/MemoryObject.h"
|
2011-08-24 18:08:43 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2011-03-28 18:25:07 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class Target;
|
|
|
|
} // namespace llvm
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
|
|
|
|
// disassembly is supported by passing a block of information in the DisInfo
|
2011-05-22 04:52:24 +00:00
|
|
|
// parameter and specifying the TagType and callback functions as described in
|
2011-03-28 18:25:07 +00:00
|
|
|
// the header llvm-c/Disassembler.h . The pointer to the block and the
|
2011-05-22 04:52:24 +00:00
|
|
|
// functions can all be passed as NULL. If successful, this returns a
|
|
|
|
// disassembler context. If not, it returns NULL.
|
2011-03-28 18:25:07 +00:00
|
|
|
//
|
|
|
|
LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
|
|
|
|
int TagType, LLVMOpInfoCallback GetOpInfo,
|
|
|
|
LLVMSymbolLookupCallback SymbolLookUp) {
|
|
|
|
// Initialize targets and assembly printers/parsers.
|
|
|
|
llvm::InitializeAllTargetInfos();
|
2011-07-22 21:58:54 +00:00
|
|
|
llvm::InitializeAllTargetMCs();
|
2011-03-28 18:25:07 +00:00
|
|
|
llvm::InitializeAllAsmParsers();
|
|
|
|
llvm::InitializeAllDisassemblers();
|
|
|
|
|
|
|
|
// Get the target.
|
|
|
|
std::string Error;
|
|
|
|
const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
|
|
|
|
assert(TheTarget && "Unable to create target!");
|
|
|
|
|
|
|
|
// Get the assembler info needed to setup the MCContext.
|
2011-07-14 23:50:31 +00:00
|
|
|
const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
|
2011-03-28 18:25:07 +00:00
|
|
|
assert(MAI && "Unable to create target asm info!");
|
|
|
|
|
2011-07-18 20:57:22 +00:00
|
|
|
const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
|
|
|
|
assert(MRI && "Unable to create target register info!");
|
|
|
|
|
2011-03-28 18:25:07 +00:00
|
|
|
// Package up features to be passed to target/subtarget
|
|
|
|
std::string FeaturesStr;
|
2011-06-30 01:53:36 +00:00
|
|
|
std::string CPU;
|
2011-03-28 18:25:07 +00:00
|
|
|
|
2011-09-07 17:24:38 +00:00
|
|
|
const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU,
|
|
|
|
FeaturesStr);
|
|
|
|
assert(STI && "Unable to create subtarget info!");
|
|
|
|
|
2011-03-28 18:25:07 +00:00
|
|
|
// Set up the MCContext for creating symbols and MCExpr's.
|
2011-07-20 19:50:42 +00:00
|
|
|
MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
|
2011-03-28 18:25:07 +00:00
|
|
|
assert(Ctx && "Unable to create MCContext!");
|
|
|
|
|
|
|
|
// Set up disassembler.
|
2011-09-07 17:24:38 +00:00
|
|
|
MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
|
2011-03-28 18:25:07 +00:00
|
|
|
assert(DisAsm && "Unable to create disassembler!");
|
2011-04-11 18:08:50 +00:00
|
|
|
DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
|
2011-03-28 18:25:07 +00:00
|
|
|
|
|
|
|
// Set up the instruction printer.
|
|
|
|
int AsmPrinterVariant = MAI->getAssemblerDialect();
|
2011-07-06 19:45:42 +00:00
|
|
|
MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
|
2011-09-07 17:24:38 +00:00
|
|
|
*MAI, *STI);
|
2011-03-28 18:25:07 +00:00
|
|
|
assert(IP && "Unable to create instruction printer!");
|
|
|
|
|
|
|
|
LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
|
|
|
|
GetOpInfo, SymbolLookUp,
|
2011-07-20 19:50:42 +00:00
|
|
|
TheTarget, MAI, MRI,
|
2011-07-18 20:57:22 +00:00
|
|
|
Ctx, DisAsm, IP);
|
2011-03-28 18:25:07 +00:00
|
|
|
assert(DC && "Allocation failure!");
|
2011-09-15 18:37:20 +00:00
|
|
|
|
|
|
|
IP->setCommentStream(DC->CommentStream);
|
|
|
|
|
2011-03-28 18:25:07 +00:00
|
|
|
return DC;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// LLVMDisasmDispose() disposes of the disassembler specified by the context.
|
|
|
|
//
|
|
|
|
void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
|
|
|
|
LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
|
|
|
|
delete DC;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
//
|
|
|
|
// The memory object created by LLVMDisasmInstruction().
|
|
|
|
//
|
|
|
|
class DisasmMemoryObject : public MemoryObject {
|
|
|
|
uint8_t *Bytes;
|
|
|
|
uint64_t Size;
|
|
|
|
uint64_t BasePC;
|
|
|
|
public:
|
|
|
|
DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
|
|
|
|
Bytes(bytes), Size(size), BasePC(basePC) {}
|
|
|
|
|
|
|
|
uint64_t getBase() const { return BasePC; }
|
|
|
|
uint64_t getExtent() const { return Size; }
|
|
|
|
|
|
|
|
int readByte(uint64_t Addr, uint8_t *Byte) const {
|
|
|
|
if (Addr - BasePC >= Size)
|
|
|
|
return -1;
|
|
|
|
*Byte = Bytes[Addr - BasePC];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
2011-05-22 04:52:24 +00:00
|
|
|
} // end anonymous namespace
|
2011-03-28 18:25:07 +00:00
|
|
|
|
|
|
|
//
|
2011-04-09 14:06:12 +00:00
|
|
|
// LLVMDisasmInstruction() disassembles a single instruction using the
|
2011-03-28 18:25:07 +00:00
|
|
|
// disassembler context specified in the parameter DC. The bytes of the
|
2011-04-09 14:06:12 +00:00
|
|
|
// instruction are specified in the parameter Bytes, and contains at least
|
2011-03-28 18:25:07 +00:00
|
|
|
// BytesSize number of bytes. The instruction is at the address specified by
|
|
|
|
// the PC parameter. If a valid instruction can be disassembled its string is
|
|
|
|
// returned indirectly in OutString which whos size is specified in the
|
|
|
|
// parameter OutStringSize. This function returns the number of bytes in the
|
|
|
|
// instruction or zero if there was no valid instruction. If this function
|
|
|
|
// returns zero the caller will have to pick how many bytes they want to step
|
|
|
|
// over by printing a .byte, .long etc. to continue.
|
|
|
|
//
|
|
|
|
size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
|
|
|
|
uint64_t BytesSize, uint64_t PC, char *OutString,
|
|
|
|
size_t OutStringSize){
|
|
|
|
LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
|
|
|
|
// Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
|
|
|
|
DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
|
|
|
|
|
|
|
|
uint64_t Size;
|
|
|
|
MCInst Inst;
|
|
|
|
const MCDisassembler *DisAsm = DC->getDisAsm();
|
|
|
|
MCInstPrinter *IP = DC->getIP();
|
2011-09-01 22:01:14 +00:00
|
|
|
MCDisassembler::DecodeStatus S;
|
|
|
|
S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
|
|
|
|
/*REMOVE*/ nulls());
|
|
|
|
switch (S) {
|
|
|
|
case MCDisassembler::Fail:
|
|
|
|
case MCDisassembler::SoftFail:
|
2011-09-01 18:02:14 +00:00
|
|
|
// FIXME: Do something different for soft failure modes?
|
2011-03-28 18:25:07 +00:00
|
|
|
return 0;
|
2011-09-01 21:09:04 +00:00
|
|
|
|
2011-09-01 22:01:14 +00:00
|
|
|
case MCDisassembler::Success: {
|
|
|
|
SmallVector<char, 64> InsnStr;
|
|
|
|
raw_svector_ostream OS(InsnStr);
|
|
|
|
IP->printInst(&Inst, OS);
|
|
|
|
OS.flush();
|
2011-09-01 21:09:04 +00:00
|
|
|
|
2011-09-15 18:37:20 +00:00
|
|
|
DC->CommentStream.flush();
|
|
|
|
assert(DC->CommentsToEmit.back() == '\n');
|
|
|
|
|
|
|
|
DC->CommentsToEmit.push_back('\n');
|
|
|
|
StringRef Comments = DC->CommentsToEmit.str();
|
|
|
|
|
|
|
|
do {
|
|
|
|
// Emit a line of comments.
|
|
|
|
size_t Position = Comments.find('\n');
|
|
|
|
OS << ' ' << DC->getAsmInfo()->getCommentString()
|
|
|
|
<< ' ' << Comments.substr(0, Position) << '\n';
|
|
|
|
|
|
|
|
Comments = Comments.substr(Position+1);
|
|
|
|
} while (!Comments.empty());
|
|
|
|
|
|
|
|
DC->CommentsToEmit.clear();
|
|
|
|
// Tell the comment stream that the vector changed underneath it.
|
|
|
|
DC->CommentStream.resync();
|
|
|
|
|
2011-09-01 22:01:14 +00:00
|
|
|
assert(OutStringSize != 0 && "Output buffer cannot be zero size");
|
|
|
|
size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
|
|
|
|
std::memcpy(OutString, InsnStr.data(), OutputSize);
|
|
|
|
OutString[OutputSize] = '\0'; // Terminate string.
|
2011-09-01 21:09:04 +00:00
|
|
|
|
2011-09-01 22:01:14 +00:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2011-03-28 18:25:07 +00:00
|
|
|
}
|