Import radare2-capstone asm/anal plugins

This commit is contained in:
pancake 2014-03-07 03:16:10 +01:00
parent 99cd0fee14
commit 784a1d2a72
30 changed files with 1015 additions and 22 deletions

View File

@ -19,6 +19,7 @@ TAREXT=tar.gz
CZ=gzip -f
endif
PWD=$(shell pwd)
MAKE_JOBS?=1
all: plugins.cfg
${MAKE} -C libr/util

View File

@ -41,6 +41,7 @@ LDFLAGS+=../../libr/util/libr_util.a
LDFLAGS+=../../libr/socket/libr_socket.a
# extra libs #
LDFLAGS+=../../shlr/grub/libgrubfs.a
LDFLAGS+=../../shlr/capstone/libcapstone.a
LDFLAGS+=../../shlr/sdb/src/libsdb.a
LDFLAGS+=../../shlr/zip/librz.a
LDFLAGS+=../../shlr/gdb/lib/libgdbr.a

View File

@ -22,6 +22,7 @@ ifeq ($(WITHNONPIC),1)
LDFLAGS+=$(shell for a in ${BINDEPS} ; do b=`echo $$a |sed -e s,r_,,g`; echo ../../libr/$$b/lib$$a.a ; done )
LDFLAGS+=../../shlr/sdb/src/libsdb.a
LDFLAGS+=../../shlr/grub/libgrubfs.a
LDFLAGS+=../../shlr/capstone/libcapstone.a
ifneq (${OSTYPE},haiku)
LDFLAGS+=-lm
endif

View File

@ -8,16 +8,15 @@ CFLAGS+=-DCORELIB -Iarch -I$(TOP)/shlr
LDFLAGS+=${BN_LIBS}
.PHONY: pre libs ${EXTRA_CLEAN}
.PHONY: all plugins libs ${EXTRA_CLEAN}
all: plugins
plugins: ${LIBSO} ${LIBAR}
@${MAKE} -C p all
#${LIBSO} ${LIBAR}: pre
include ${STATIC_ANAL_PLUGINS}
STATIC_OBJS=$(addprefix $(LTOP)/anal/p/,$(STATIC_OBJ))
OBJLIBS=meta.o reflines.o ref.o op.o fcn.o bb.o var.o
OBJLIBS+=cond.o value.o cc.o diff.o types.o fcnstore.o
@ -26,7 +25,4 @@ OBJLIBS+=anal_ex.o switch.o state.o kvesil.o
OBJS=${STATIC_OBJS} ${OBJLIBS} ${CPARSE_OBJS}
#pre:
# @cd $(TOP)/shlr && ${MAKE}
include $(LTOP)/rules.mk

74
libr/anal/p/anal_arm_cs.c Normal file
View File

@ -0,0 +1,74 @@
/* radare2 - LGPL - Copyright 2013-2014 - pancake */
#include <r_anal.h>
#include <r_lib.h>
#include <capstone.h>
#include <arm.h>
static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len) {
csh handle;
cs_insn *insn;
int mode = (a->bits==16)? CS_MODE_THUMB: CS_MODE_ARM;
int n, ret = (a->bits==64)?
cs_open (CS_ARCH_ARM64, mode, &handle):
cs_open (CS_ARCH_ARM, mode, &handle);
cs_option (handle, CS_OPT_DETAIL, CS_OPT_ON);
op->type = R_ANAL_OP_TYPE_NULL;
op->size = 0;
if (ret == CS_ERR_OK) {
n = cs_disasm_ex (handle, (ut8*)buf, len, addr, 1, &insn);
if (n<1) {
op->type = R_ANAL_OP_TYPE_ILL;
} else {
op->size = insn->size;
switch (insn->id) {
case ARM_INS_ADD:
op->type = R_ANAL_OP_TYPE_ADD;
break;
case ARM_INS_TST:
op->type = R_ANAL_OP_TYPE_CMP;
break;
case ARM_INS_ROR:
case ARM_INS_ORN:
case ARM_INS_LSL:
case ARM_INS_LSR:
break;
case ARM_INS_PUSH:
case ARM_INS_STR:
case ARM_INS_POP:
case ARM_INS_LDR:
break;
case ARM_INS_BL:
case ARM_INS_BLX:
op->type = R_ANAL_OP_TYPE_CALL;
break;
case ARM_INS_B:
case ARM_INS_BX:
case ARM_INS_BXJ:
op->type = R_ANAL_OP_TYPE_JMP;
op->jump = 0;
break;
}
}
}
beach:
cs_free (insn, n);
cs_close (&handle);
return op->size;
}
RAnalPlugin r_anal_plugin_arm_cs = {
.name = "arm.cs",
.desc = "Capstone ARM analyzer",
.license = "BSD",
.arch = R_SYS_ARCH_ARM,
.bits = 16|32|64,
.op = &analop,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ANAL,
.data = &r_anal_plugin_arm_cs
};
#endif

162
libr/anal/p/anal_mips_cs.c Normal file
View File

@ -0,0 +1,162 @@
/* radare2 - LGPL - Copyright 2013-2014 - pancake */
#include <r_asm.h>
#include <r_lib.h>
#include <capstone.h>
#include <mips.h>
static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len) {
int n, ret, opsize = -1;
csh handle;
cs_insn* insn;
int mode = a->big_endian? CS_MODE_BIG_ENDIAN: CS_MODE_LITTLE_ENDIAN;
mode |= (a->bits==64)? CS_MODE_64: CS_MODE_32;
// XXX no arch->cpu ?!?! CS_MODE_MICRO, N64
ret = cs_open (CS_ARCH_MIPS, mode, &handle);
op->type = R_ANAL_OP_TYPE_ILL;
op->size = 4;
if (ret != CS_ERR_OK) goto fin;
cs_option (handle, CS_OPT_DETAIL, CS_OPT_ON);
n = cs_disasm_ex (handle, (ut8*)buf, len, addr, 1, &insn);
if (n<1 || insn->size<1)
goto beach;
op->type = R_ANAL_OP_TYPE_NULL;
opsize = op->size = insn->size;
switch (insn->id) {
case MIPS_INS_INVALID:
op->type = R_ANAL_OP_TYPE_ILL;
break;
case MIPS_INS_LB:
case MIPS_INS_LBU:
case MIPS_INS_LBUX:
case MIPS_INS_LW:
case MIPS_INS_LWC1:
case MIPS_INS_LWC2:
case MIPS_INS_LWL:
case MIPS_INS_LWR:
case MIPS_INS_LWXC1:
case MIPS_INS_LD:
case MIPS_INS_LDC1:
case MIPS_INS_LDC2:
case MIPS_INS_LDL:
case MIPS_INS_LDR:
case MIPS_INS_LDXC1:
op->type = R_ANAL_OP_TYPE_LOAD;
break;
case MIPS_INS_SW:
case MIPS_INS_SWC1:
case MIPS_INS_SWC2:
case MIPS_INS_SWL:
case MIPS_INS_SWR:
case MIPS_INS_SWXC1:
op->type = R_ANAL_OP_TYPE_STORE;
break;
case MIPS_INS_NOP:
op->type = R_ANAL_OP_TYPE_NOP;
break;
case MIPS_INS_SYSCALL:
case MIPS_INS_BREAK:
op->type = R_ANAL_OP_TYPE_TRAP;
break;
case MIPS_INS_JALR:
op->type = R_ANAL_OP_TYPE_UCALL;
break;
case MIPS_INS_JAL:
case MIPS_INS_JALRC:
op->type = R_ANAL_OP_TYPE_CALL;
break;
case MIPS_INS_MOVE:
op->type = R_ANAL_OP_TYPE_MOV;
break;
case MIPS_INS_ADD:
case MIPS_INS_ADDI:
case MIPS_INS_ADDIU:
case MIPS_INS_DADD:
case MIPS_INS_DADDI:
case MIPS_INS_DADDIU:
op->type = R_ANAL_OP_TYPE_ADD;
break;
case MIPS_INS_SUB:
case MIPS_INS_SUBV:
case MIPS_INS_DSUBU:
case MIPS_INS_FSUB:
op->type = R_ANAL_OP_TYPE_SUB;
break;
case MIPS_INS_MULV:
case MIPS_INS_MULT:
case MIPS_INS_MULSA:
case MIPS_INS_FMUL:
case MIPS_INS_MUL:
case MIPS_INS_DMULT:
case MIPS_INS_DMULTU:
op->type = R_ANAL_OP_TYPE_MUL;
break;
case MIPS_INS_XOR:
case MIPS_INS_XORI:
op->type = R_ANAL_OP_TYPE_XOR;
break;
case MIPS_INS_AND:
case MIPS_INS_ANDI:
op->type = R_ANAL_OP_TYPE_AND;
break;
case MIPS_INS_NOT:
op->type = R_ANAL_OP_TYPE_NOT;
break;
case MIPS_INS_OR:
case MIPS_INS_ORI:
op->type = R_ANAL_OP_TYPE_OR;
break;
case MIPS_INS_DIV:
case MIPS_INS_DIVU:
case MIPS_INS_DDIV:
case MIPS_INS_DDIVU:
case MIPS_INS_FDIV:
case MIPS_INS_DIV_S:
case MIPS_INS_DIV_U:
op->type = R_ANAL_OP_TYPE_DIV;
break;
case MIPS_INS_CMPGDU:
case MIPS_INS_CMPGU:
case MIPS_INS_CMPU:
case MIPS_INS_CMPI:
op->type = R_ANAL_OP_TYPE_CMP;
break;
case MIPS_INS_J:
case MIPS_INS_JR:
case MIPS_INS_JRC:
case MIPS_INS_B:
case MIPS_INS_BZ:
case MIPS_INS_BNE:
case MIPS_INS_BNZ:
case MIPS_INS_BEQZ:
case MIPS_INS_BNEG:
case MIPS_INS_BNEGI:
case MIPS_INS_BNEZ:
case MIPS_INS_BTEQZ:
case MIPS_INS_BTNEZ:
op->type = R_ANAL_OP_TYPE_JMP;
break;
}
beach:
cs_free (insn, n);
cs_close (&handle);
fin:
return opsize;
}
RAnalPlugin r_anal_plugin_mips_cs = {
.name = "mips.cs",
.desc = "Capstone MIPS analyzer",
.license = "BSD",
.arch = R_SYS_ARCH_MIPS,
.bits = 16|32|64,
.op = &analop,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ANAL,
.data = &r_anal_plugin_mips_cs
};
#endif

46
libr/anal/p/anal_ppc_cs.c Normal file
View File

@ -0,0 +1,46 @@
/* radare2 - LGPL - Copyright 2013-2014 - pancake */
#include <r_anal.h>
#include <r_lib.h>
#include <capstone.h>
#include <ppc.h>
static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len) {
csh handle;
cs_insn *insn;
int mode = (a->bits==64)? CS_MODE_64:
(a->bits==32)? CS_MODE_32: 0;
int n, ret = cs_open (CS_ARCH_PPC, mode, &handle);
op->type = R_ANAL_OP_TYPE_NULL;
op->size = 0;
if (ret == CS_ERR_OK) {
cs_option (handle, CS_OPT_DETAIL, CS_OPT_ON);
// capstone-next
n = cs_disasm_ex (handle, (const ut8*)buf, len, addr, 1, &insn);
if (n<1) {
op->type = R_ANAL_OP_TYPE_ILL;
} else {
op->size = insn->size;
}
cs_free (insn, n);
cs_close (&handle);
}
return op->size;
}
RAnalPlugin r_anal_plugin_ppc_cs = {
.name = "ppc.cs",
.desc = "Capstone PowerPC analysis",
.license = "BSD",
.arch = R_SYS_ARCH_PPC,
.bits = 32|64,
.op = &analop,
//.set_reg_profile = &set_reg_profile,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ANAL,
.data = &r_anal_plugin_ppc_cs
};
#endif

167
libr/anal/p/anal_x86_cs.c Normal file
View File

@ -0,0 +1,167 @@
/* radare2 - LGPL - Copyright 2013-2014 - pancake */
#include <r_anal.h>
#include <r_lib.h>
#include <capstone.h>
#include <x86.h>
#if CS_API_MAJOR < 2
#error Old Capstone not supported
#endif
#if CS_API_MINOR < 1
#error Old Capstone not supported
#endif
static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len) {
csh handle;
cs_insn *insn;
int mode = (a->bits==64)? CS_MODE_64:
(a->bits==32)? CS_MODE_32:
(a->bits==16)? CS_MODE_16: 0;
int n, ret = cs_open (CS_ARCH_X86, mode, &handle);
op->type = R_ANAL_OP_TYPE_NULL;
op->size = 0;
if (ret == CS_ERR_OK) {
cs_option (handle, CS_OPT_DETAIL, CS_OPT_ON);
// capstone-next
n = cs_disasm_ex (handle, (const ut8*)buf, len, addr, 1, &insn);
if (n<1) {
op->type = R_ANAL_OP_TYPE_ILL;
} else {
op->size = insn->size;
switch (insn->id) {
case X86_INS_MOV:
case X86_INS_MOVZX:
case X86_INS_MOVABS:
case X86_INS_MOVBE:
case X86_INS_MOVSB:
case X86_INS_MOVSD:
case X86_INS_MOVSQ:
case X86_INS_MOVSS:
case X86_INS_MOVSW:
case X86_INS_MOVD:
case X86_INS_MOVQ:
case X86_INS_MOVDQ2Q:
op->type = R_ANAL_OP_TYPE_MOV;
break;
case X86_INS_CMP:
case X86_INS_VCMP:
case X86_INS_CMPPD:
case X86_INS_CMPPS:
case X86_INS_CMPSW:
case X86_INS_CMPSD:
case X86_INS_CMPSQ:
case X86_INS_CMPSB:
case X86_INS_CMPSS:
case X86_INS_TEST:
op->type = R_ANAL_OP_TYPE_CMP;
break;
case X86_INS_LEA:
op->type = R_ANAL_OP_TYPE_LEA;
break;
case X86_INS_PUSH:
case X86_INS_PUSHAW:
case X86_INS_PUSHAL:
case X86_INS_PUSHF:
op->type = R_ANAL_OP_TYPE_PUSH;
break;
case X86_INS_POP:
case X86_INS_POPAW:
case X86_INS_POPAL:
case X86_INS_POPF:
case X86_INS_POPCNT:
op->type = R_ANAL_OP_TYPE_POP;
break;
case X86_INS_RET:
case X86_INS_RETF:
case X86_INS_IRET:
case X86_INS_IRETD:
case X86_INS_IRETQ:
case X86_INS_SYSRET:
op->type = R_ANAL_OP_TYPE_RET;
break;
case X86_INS_INT1:
case X86_INS_INT3:
case X86_INS_INTO:
case X86_INS_INT:
case X86_INS_VMCALL:
case X86_INS_VMMCALL:
case X86_INS_SYSCALL:
op->type = R_ANAL_OP_TYPE_TRAP;
break;
case X86_INS_JL:
case X86_INS_JLE:
case X86_INS_JA:
case X86_INS_JAE:
case X86_INS_JB:
case X86_INS_JBE:
case X86_INS_JCXZ:
case X86_INS_JECXZ:
case X86_INS_JO:
case X86_INS_JNO:
case X86_INS_JS:
case X86_INS_JNS:
case X86_INS_JP:
case X86_INS_JNP:
case X86_INS_JE:
case X86_INS_JNE:
case X86_INS_JG:
case X86_INS_JGE:
op->type = R_ANAL_OP_TYPE_CJMP;
op->jump = insn->detail->x86.operands[0].imm;
op->fail = addr+op->size;
break;
case X86_INS_CALL:
case X86_INS_LCALL:
op->type = R_ANAL_OP_TYPE_CALL;
// TODO: what if UCALL?
// TODO: use imm_size
op->jump = insn->detail->x86.operands[0].imm;
op->fail = addr+op->size;
break;
case X86_INS_JMP:
case X86_INS_LJMP:
// TODO: what if UJMP?
op->jump = insn->detail->x86.operands[0].imm;
op->type = R_ANAL_OP_TYPE_JMP;
break;
case X86_INS_XOR:
op->type = R_ANAL_OP_TYPE_XOR;
break;
case X86_INS_AND:
case X86_INS_ANDN:
case X86_INS_ANDPD:
case X86_INS_ANDPS:
case X86_INS_ANDNPD:
case X86_INS_ANDNPS:
op->type = R_ANAL_OP_TYPE_AND;
break;
case X86_INS_ADD:
case X86_INS_FADD:
case X86_INS_ADDPD:
op->type = R_ANAL_OP_TYPE_ADD;
break;
}
}
cs_free (insn, n);
cs_close (&handle);
}
return op->size;
}
RAnalPlugin r_anal_plugin_x86_cs = {
.name = "x86.cs",
.desc = "Capstone X86 analysis",
.license = "BSD",
.arch = R_SYS_ARCH_X86,
.bits = 16|32|64,
.op = &analop,
//.set_reg_profile = &set_reg_profile,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ANAL,
.data = &r_anal_plugin_x86_cs
};
#endif

12
libr/anal/p/arm_cs.mk Normal file
View File

@ -0,0 +1,12 @@
OBJ_ARM_CS=anal_arm_cs.o
SHARED_ARM_CS=../../shlr/capstone/libcapstone.a
STATIC_OBJ+=$(OBJ_ARM_CS)
SHARED_OBJ+=${SHARED_ARM_CS}
TARGET_ARM_CS=anal_arm_cs.${EXT_SO}
ALL_TARGETS+=${TARGET_ARM_CS}
${TARGET_ARM_CS}: ${OBJ_ARM_CS}
${CC} ${CFLAGS} $(call libname,anal_arm_cs) \
-o anal_arm_cs.${EXT_SO} ${OBJ_ARM_CS}

12
libr/anal/p/mips_cs.mk Normal file
View File

@ -0,0 +1,12 @@
OBJ_MIPS_CS=anal_mips_cs.o
SHARED_MIPS_CS=../../shlr/capstone/libcapstone.a
STATIC_OBJ+=$(OBJ_MIPS_CS)
SHARED_OBJ+=${SHARED_MIPS_CS}
TARGET_MIPS_CS=anal_mips_cs.${EXT_SO}
ALL_TARGETS+=${TARGET_MIPS_CS}
${TARGET_MIPS_CS}: ${OBJ_MIPS_CS}
${CC} ${CFLAGS} $(call libname,anal_mips_cs) \
-o anal_mips_cs.${EXT_SO} ${OBJ_MIPS_CS}

13
libr/anal/p/ppc_cs.mk Normal file
View File

@ -0,0 +1,13 @@
OBJ_PPC_CS=anal_ppc_cs.o
CFLAGS+=-I../../shlr/capstone/include
SHARED_PPC_CS=../../shlr/capstone/libcapstone.a
STATIC_OBJ+=${OBJ_PPC_CS}
SHARED_OBJ+=${SHARED_PPC_CS}
TARGET_PPC_CS=anal_ppc_cs.${EXT_SO}
ALL_TARGETS+=${TARGET_PPC_CS}
${TARGET_PPC_CS}: ${OBJ_PPC_CS}
${CC} ${CFLAGS} $(call libname,anal_ppc_cs) \
-o anal_ppc_cs.${EXT_SO} ${OBJ_PPC_CS}

13
libr/anal/p/x86_cs.mk Normal file
View File

@ -0,0 +1,13 @@
OBJ_X86_CS=anal_x86_cs.o
CFLAGS+=-I../../shlr/capstone/include
STATIC_OBJ+=${OBJ_X86_CS}
SHARED_X86_CS=../../shlr/capstone/libcapstone.a
SHARED_OBJ+=${SHARED_X86_CS}
TARGET_X86_CS=anal_x86_cs.${EXT_SO}
ALL_TARGETS+=${TARGET_X86_CS}
${TARGET_X86_CS}: ${OBJ_X86_CS}
${CC} ${CFLAGS} $(call libname,anal_x86_cs) \
-o anal_x86_cs.${EXT_SO} ${OBJ_X86_CS}

View File

@ -6,4 +6,5 @@ TARGET_Z80=anal_z80.${EXT_SO}
ALL_TARGETS+=${TARGET_Z80}
${TARGET_Z80}: ${OBJ_Z80}
${CC} $(call libname,anal_z80) ${LDFLAGS} ${CFLAGS} -o anal_z80.${EXT_SO} ${OBJ_Z80}
${CC} $(call libname,anal_z80) ${LDFLAGS} ${CFLAGS} \
-o anal_z80.${EXT_SO} ${OBJ_Z80}

17
libr/asm/p/arm_cs.mk Normal file
View File

@ -0,0 +1,17 @@
# capstone
OBJ_ARMCS=asm_arm_cs.o
CFLAGS+=-I../../shlr/capstone/include
SHARED_ARMCS=../../shlr/capstone/libcapstone.a
SHARED2_ARMCS=$(addprefix ../,${SHARED_ARMCS})
STATIC_OBJ+=${OBJ_ARMCS}
SHARED_OBJ+=${SHARED_ARMCS}
TARGET_ARMCS=asm_arm_cs.${EXT_SO}
ALL_TARGETS+=${TARGET_ARMCS}
${TARGET_ARMCS}: ${OBJ_ARMCS}
${CC} $(call libname,asm_arm) ${LDFLAGS} ${CFLAGS} \
-o ${TARGET_ARMCS} ${OBJ_ARMCS} ${SHARED2_ARMCS}

68
libr/asm/p/asm_arm_cs.c Normal file
View File

@ -0,0 +1,68 @@
/* radare2 - LGPL - Copyright 2013-2014 - pancake */
#include <r_asm.h>
#include <r_lib.h>
#include <capstone.h>
static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
cs_insn* insn = NULL;
cs_mode mode = 0;
int ret, n = 0;
csh cd;
mode = (a->bits==16)? CS_MODE_THUMB: CS_MODE_ARM;
if (a->big_endian)
mode |= CS_MODE_BIG_ENDIAN;
else
mode |= CS_MODE_LITTLE_ENDIAN;
op->size = 4;
op->buf_asm[0] = 0;
ret = (a->bits==64)?
cs_open (CS_ARCH_ARM64, mode, &cd):
cs_open (CS_ARCH_ARM, mode, &cd);
if (ret) {
ret = -1;
goto beach;
}
cs_option (cd, CS_OPT_DETAIL, CS_OPT_OFF);
n = cs_disasm_ex (cd, buf, R_MIN (4, len),
a->pc, 1, &insn);
if (n<1) {
ret = -1;
goto beach;
}
if (insn->size<1) {
ret = -1;
goto beach;
}
op->size = insn->size;
snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s",
insn->mnemonic,
insn->op_str[0]?" ":"",
insn->op_str);
r_str_rmch (op->buf_asm, '#');
beach:
cs_free (insn, n);
cs_close (&cd);
if (!op->buf_asm[0])
strcpy (op->buf_asm, "invalid");
return op->size;
}
RAsmPlugin r_asm_plugin_arm_cs = {
.name = "arm.cs",
.desc = "Capstone ARM disassembler",
.license = "BSD",
.arch = "arm",
.bits = 16|32|64,
.init = NULL,
.fini = NULL,
.disassemble = &disassemble,
.assemble = NULL
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_arm_cs
};
#endif

65
libr/asm/p/asm_mips_cs.c Normal file
View File

@ -0,0 +1,65 @@
/* radare2 - LGPL - Copyright 2013-2014 - pancake */
#include <r_asm.h>
#include <r_lib.h>
#include <capstone.h>
static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
csh handle;
cs_insn* insn;
int mode, n, ret = -1;
mode = a->big_endian? CS_MODE_BIG_ENDIAN: CS_MODE_LITTLE_ENDIAN;
if (a->cpu) {
if (!strcmp (a->cpu, "n64")) {
mode |= CS_MODE_N64;
} else
if (!strcmp (a->cpu, "micro")) {
mode |= CS_MODE_MICRO;
}
}
mode |= (a->bits==64)? CS_MODE_64: CS_MODE_32;
memset (op, sizeof (RAsmOp), 0);
op->size = 4;
ret = cs_open (CS_ARCH_MIPS, mode, &handle);
if (ret) goto fin;
cs_option (handle, CS_OPT_DETAIL, CS_OPT_OFF);
n = cs_disasm_ex (handle, (ut8*)buf, len, a->pc, 1, &insn);
if (n<1) {
strcpy (op->buf_asm, "invalid");
op->size = 4;
ret = -1;
goto beach;
} else ret = 4;
if (insn->size<1)
goto beach;
op->size = insn->size;
snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s",
insn->mnemonic, insn->op_str[0]? " ": "",
insn->op_str);
// TODO: remove the '$'<registername> in the string
beach:
cs_free (insn, n);
cs_close (&handle);
fin:
return ret;
}
RAsmPlugin r_asm_plugin_mips_cs = {
.name = "mips.cs",
.desc = "Capstone MIPS disassembler",
.license = "BSD",
.arch = "mips",
.cpus = "n64,micro",
.bits = 16|32|64,
.init = NULL,
.fini = NULL,
.disassemble = &disassemble,
.assemble = NULL
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_mips_cs
};
#endif

66
libr/asm/p/asm_ppc_cs.c Normal file
View File

@ -0,0 +1,66 @@
/* radare2 - LGPL - Copyright 2014 - pancake */
#include <r_asm.h>
#include <r_lib.h>
#include <capstone.h>
static csh handle = 0;
static int the_end(void *p) {
if (handle) {
cs_close (&handle);
handle = 0;
}
return R_TRUE;
}
static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
static int omode = 0;
int mode, n, ret;
ut64 off = a->pc;
cs_insn* insn;
mode = (a->bits==64)? CS_MODE_64:
(a->bits==32)? CS_MODE_32: 0;
if (handle && mode != omode) {
cs_close (&handle);
handle = 0;
}
op->size = 0;
omode = mode;
if (handle == 0) {
ret = cs_open (CS_ARCH_PPC, mode, &handle);
if (ret) return 0;
}
cs_option (handle, CS_OPT_DETAIL, CS_OPT_OFF);
n = cs_disasm_ex (handle, (const ut8*)buf, len, off, 1, &insn);
if (n>0) {
if (insn->size>0) {
op->size = insn->size;
snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s",
insn->mnemonic, insn->op_str[0]?" ":"",
insn->op_str);
}
cs_free (insn, n);
}
return op->size;
}
RAsmPlugin r_asm_plugin_ppc_cs = {
.name = "ppc.cs",
.desc = "Capstone PowerPC disassembler",
.license = "BSD",
.arch = "ppc",
.bits = 32|64,
.init = NULL,
.fini = the_end,
.disassemble = &disassemble,
.assemble = NULL
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_ppc_cs
};
#endif

133
libr/asm/p/asm_x86_cs.c Normal file
View File

@ -0,0 +1,133 @@
/* radare2 - LGPL - Copyright 2013-2014 - pancake */
#include <r_asm.h>
#include <r_lib.h>
#include <capstone.h>
#define USE_CUSTOM_ALLOC 0
#if USE_CUSTOM_ALLOC
static int bufi = 0;
static char buf[65535];
#define D if(0)
static void *my_malloc(size_t s) {
char *ret;
D printf ("MALLOC %d / %d\n", (int)s, bufi);
ret = buf+bufi;
bufi += (s*3);
if (bufi>sizeof (buf)) {
eprintf ("MALLOC FAIL\n");
return NULL;
}
return ret;
}
static void *my_calloc(size_t c, size_t s) {
ut8 *p = my_malloc (c*s);
memset (p, 0, c*s);
return p;
}
static void *my_realloc(void *p, size_t s) {
if (!p) return my_malloc (s);
D eprintf ("REALLOC %p %d\n", p, (int)s);
return p;
}
static void my_free(void *p) {
D eprintf ("FREE %d bytes\n", bufi);
D printf ("FREE %p\n", p);
}
#endif
static csh cd = 0;
static int the_end(void *p) {
if (cd) {
cs_close (&cd);
cd = 0;
}
return R_TRUE;
}
static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
static int omode = 0;
int mode, n, ret;
ut64 off = a->pc;
cs_insn* insn = NULL;
mode = (a->bits==64)? CS_MODE_64:
(a->bits==32)? CS_MODE_32:
(a->bits==16)? CS_MODE_16: 0;
if (cd && mode != omode) {
//if (cd) {
#if USE_CUSTOM_ALLOC
bufi = 0;
cs_opt_mem mem = {
.malloc = &malloc,
.calloc = &calloc,
.realloc = &realloc,
.free = &free
};
cs_option (cd, CS_OPT_MEM, (size_t)&mem);
#endif
cs_close (&cd);
cd = 0;
}
op->size = 0;
omode = mode;
if (cd == 0) {
ret = cs_open (CS_ARCH_X86, mode, &cd);
if (ret) return 0;
#if USE_CUSTOM_ALLOC
bufi = 0;
cs_opt_mem mem = {
.malloc = &my_malloc,
.calloc = &my_calloc,
.realloc = &my_realloc,
.free = &my_free
};
cs_option (cd, CS_OPT_MEM, (size_t)&mem);
#endif
cs_option (cd, CS_OPT_DETAIL, CS_OPT_OFF);
}
n = cs_disasm_ex (cd, (const ut8*)buf, len, off, 1, &insn);
if (n>0) {
if (insn->size>0) {
op->size = insn->size;
if (insn->op_str) {
snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s",
insn->mnemonic, insn->op_str[0]?" ":"",
insn->op_str);
} else {
eprintf ("op_str is null wtf\n");
}
}
}
cs_free (insn, n);
#if USE_CUSTOM_ALLOC
bufi = 0;
#endif
return op->size;
}
RAsmPlugin r_asm_plugin_x86_cs = {
.name = "x86.cs",
.desc = "Capstone X86 disassembler",
.license = "BSD",
.arch = "x86",
.bits = 16|32|64,
.init = NULL,
.fini = the_end,
.disassemble = &disassemble,
.assemble = NULL
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_x86_cs
};
#endif

17
libr/asm/p/mips_cs.mk Normal file
View File

@ -0,0 +1,17 @@
# capstone
OBJ_MIPSCS=asm_mips_cs.o
CFLAGS+=-I../../shlr/capstone/include
SHARED_MIPSCS=../../shlr/capstone/libcapstone.a
SHARED2_MIPSCS=$(addprefix ../,${SHARED_MIPSCS})
STATIC_OBJ+=${OBJ_MIPSCS}
SHARED_OBJ+=${SHARED_MIPSCS}
TARGET_MIPSCS=asm_mips_cs.${EXT_SO}
ALL_TARGETS+=${TARGET_MIPSCS}
${TARGET_MIPSCS}: ${OBJ_MIPSCS}
${CC} $(call libname,asm_mips) ${LDFLAGS} ${CFLAGS} \
-o ${TARGET_MIPSCS} ${OBJ_MIPSCS} ${SHARED2_MIPSCS}

17
libr/asm/p/ppc_cs.mk Normal file
View File

@ -0,0 +1,17 @@
# capstone
OBJ_PPCCS=asm_ppc_cs.o
CFLAGS+=-I../../shlr/capstone/include
SHARED_PPCCS=../../shlr/capstone/libcapstone.a
SHARED2_PPCCS=$(addprefix ../,${SHARED_PPCCS})
STATIC_OBJ+=${OBJ_PPCCS}
SHARED_OBJ+=${SHARED_PPCCS}
TARGET_PPCCS=asm_ppc_cs.${EXT_SO}
ALL_TARGETS+=${TARGET_PPCCS}
${TARGET_PPCCS}: ${OBJ_PPCCS}
${CC} $(call libname,asm_ppc) ${LDFLAGS} ${CFLAGS} \
-o ${TARGET_PPCCS} ${OBJ_PPCCS} ${SHARED2_PPCCS}

17
libr/asm/p/x86_cs.mk Normal file
View File

@ -0,0 +1,17 @@
# capstone
OBJ_X86CS=asm_x86_cs.o
CFLAGS+=-I../../shlr/capstone/include
SHARED_X86CS=../../shlr/capstone/libcapstone.a
SHARED2_X86CS=$(addprefix ../,${SHARED_X86CS})
STATIC_OBJ+=${OBJ_X86CS}
SHARED_OBJ+=${SHARED_X86CS}
TARGET_X86CS=asm_x86_cs.${EXT_SO}
ALL_TARGETS+=${TARGET_X86CS}
${TARGET_X86CS}: ${OBJ_X86CS}
${CC} $(call libname,asm_x86) ${LDFLAGS} ${CFLAGS} \
-o ${TARGET_X86CS} ${OBJ_X86CS} ${SHARED2_X86CS}

View File

@ -1113,13 +1113,17 @@ extern RAnalPlugin r_anal_plugin_csr;
extern RAnalPlugin r_anal_plugin_tms320;
extern RAnalPlugin r_anal_plugin_avr;
extern RAnalPlugin r_anal_plugin_arm;
extern RAnalPlugin r_anal_plugin_arm_cs;
extern RAnalPlugin r_anal_plugin_x86;
extern RAnalPlugin r_anal_plugin_x86_cs;
extern RAnalPlugin r_anal_plugin_x86_im;
extern RAnalPlugin r_anal_plugin_x86_udis;
extern RAnalPlugin r_anal_plugin_x86_simple;
extern RAnalPlugin r_anal_plugin_ppc;
extern RAnalPlugin r_anal_plugin_ppc_cs;
extern RAnalPlugin r_anal_plugin_java;
extern RAnalPlugin r_anal_plugin_mips;
extern RAnalPlugin r_anal_plugin_mips_cs;
extern RAnalPlugin r_anal_plugin_dalvik;
extern RAnalPlugin r_anal_plugin_sh;
extern RAnalPlugin r_anal_plugin_sparc;

View File

@ -158,17 +158,21 @@ R_API int r_asm_op_get_size(RAsmOp *op);
extern RAsmPlugin r_asm_plugin_bf;
extern RAsmPlugin r_asm_plugin_java;
extern RAsmPlugin r_asm_plugin_mips;
extern RAsmPlugin r_asm_plugin_mips_cs;
extern RAsmPlugin r_asm_plugin_x86;
extern RAsmPlugin r_asm_plugin_x86_as;
extern RAsmPlugin r_asm_plugin_x86_nz;
extern RAsmPlugin r_asm_plugin_x86_olly;
extern RAsmPlugin r_asm_plugin_x86_nasm;
extern RAsmPlugin r_asm_plugin_x86_cs;
extern RAsmPlugin r_asm_plugin_arm;
extern RAsmPlugin r_asm_plugin_arm_cs;
extern RAsmPlugin r_asm_plugin_armthumb;
extern RAsmPlugin r_asm_plugin_arm_winedbg;
extern RAsmPlugin r_asm_plugin_csr;
extern RAsmPlugin r_asm_plugin_m68k;
extern RAsmPlugin r_asm_plugin_ppc;
extern RAsmPlugin r_asm_plugin_ppc_cs;
extern RAsmPlugin r_asm_plugin_sparc;
extern RAsmPlugin r_asm_plugin_psosvm;
extern RAsmPlugin r_asm_plugin_avr;

View File

@ -8,6 +8,10 @@
#undef __UNIX__
#undef __WINDOWS__
// HACK to fix capstone-android-mips build
#undef mips
#define mips mips
#ifdef __HAIKU__
# define __UNIX__ 1
#endif

View File

@ -18,7 +18,7 @@ void** r_flist_get(void **x) {
#if 1
#define r_flist_t void**
#define RFList void**
#define r_flist_rewind(it) for (; it!=*it; it--); it++
#define r_flist_rewind(it) for (; it!=*it; it--) {} it++
#define r_flist_next(it) *it!=0
#define r_flist_get(it) *(it++)
#define r_flist_iterator(x) x

View File

@ -66,7 +66,7 @@ R_API int r_print_format(RPrint *p, ut64 seek, const ut8* b, int len, const char
char namefmt[8];
ut8 *buf, buffer[256];
nargs = endian = i = j = 0;
nexti = nargs = endian = i = j = 0;
if (len<1) return 0;
buf = malloc (len);

View File

@ -10,6 +10,7 @@ asm.java
asm.bf
asm.arc
asm.arm
asm.arm_cs
asm.arm_winedbg
asm.sh
asm.csr
@ -17,14 +18,17 @@ asm.avr
asm.dalvik
asm.sparc
asm.ppc
asm.ppc_cs
asm.nios2
asm.dcpu16
asm.m68k
asm.mips
asm.mips_cs
asm.rar
asm.x86
asm.x86_olly
asm.x86_nz
asm.x86_cs
asm.z80
asm.i8080
asm.8051
@ -36,14 +40,17 @@ asm.h8300
asm.malbolge
asm.ws
anal.sh
anal.x86_cs
anal.x86_udis
anal.z80
anal.8051
anal.i8080
anal.arm
anal.arm_cs
anal.arc
anal.bf
anal.mips
anal.mips_cs
anal.java
anal.dalvik
anal.csr
@ -52,6 +59,7 @@ anal.tms320
anal.avr
anal.m68k
anal.ppc
anal.ppc_cs
anal.sparc
anal.ebc
anal.gb

View File

@ -1,10 +1,13 @@
include ../config-user.mk
include ../mk/${COMPILER}.mk
CS_VER=2.1
CS_TAR=https://github.com/aquynh/capstone/archive/$(CS_VER).tar.gz
CS_URL=https://github.com/aquynh/capstone.git
CS_UPD=20140307
CS_BRA=next
CS_TIP=c76d8d03ba32cd9ab81266a99011220009c720d6
MAKE_JOBS?=1
.PHONY: capstone-sync capstone-build all clean mrproper libgdbr
@ -111,12 +114,15 @@ uninstall deinstall:
#rm -f ${D}/lib/libr_tcc*
#rm -f ${D}/lib/libr_z*
ifeq ($(CS_TAR),)
capstone:
$(MAKE) capstone-sync
$(MAKE) $(MAKEFLAGS) capstone-build
$(MAKE) -j$(MAKE_JOBS) capstone-build
capstone-build: capstone
cd capstone ; $(MAKE) $(MAKEFLAGS) all CC="$(CC)"
cd capstone && CFLAGS="-Dmips=mips" $(CFLAGS) \
$(MAKE) -j$(MAKE_JOBS) libcapstone.a AR_EXT=a CC="$(CC)"
capstone-sync:
if [ -d capstone ]; then \
@ -127,3 +133,19 @@ capstone-sync:
cd capstone ; \
git co $(CS_BRA) ; \
git reset --hard $(CS_TIP)
else
capstone-build: capstone
cd capstone && CFLAGS="-Dmips=mips $(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
$(MAKE) -j$(MAKE_JOBS) CC="$(CC)" AR_EXT=a libcapstone.a
capstone-sync: capstone
capstone: capstone-$(CS_VER).tar.gz
tar xzvf capstone-$(CS_VER).tar.gz
rm -rf capstone
mv capstone-$(CS_VER) capstone
cd capstone ; for a in ../capstone-patches/* ; do patch -p1 < $$a ; done
capstone-$(CS_VER).tar.gz:
wget -O capstone-$(CS_VER).tar.gz -c $(CS_TAR)
endif

View File

@ -0,0 +1,52 @@
diff --git a/MathExtras.h b/MathExtras.h
index 78150c8..484faf4 100644
--- a/MathExtras.h
+++ b/MathExtras.h
@@ -87,7 +87,7 @@ static inline bool isPowerOf2_32(uint32_t Value) {
/// bit. Ex. CountLeadingZeros_32(0x00F000FF) == 8.
/// Returns 32 if the word is zero.
static inline unsigned CountLeadingZeros_32(uint32_t Value) {
- unsigned Count; // result
+ unsigned Shift, Count; // result
#if __GNUC__ >= 4
// PowerPC is defined for __builtin_clz(0)
#if !defined(__ppc__) && !defined(__ppc64__)
@@ -98,7 +98,7 @@ static inline unsigned CountLeadingZeros_32(uint32_t Value) {
if (!Value) return 32;
Count = 0;
// bisection method for count leading zeros
- for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) {
+ for (Shift = 32 >> 1; Shift; Shift >>= 1) {
uint32_t Tmp = Value >> Shift;
if (Tmp) {
Value = Tmp;
@@ -123,7 +123,7 @@ static inline unsigned CountLeadingOnes_32(uint32_t Value) {
/// one bit (64 bit edition.)
/// Returns 64 if the word is zero.
static inline unsigned CountLeadingZeros_64(uint64_t Value) {
- unsigned Count; // result
+ unsigned Shift, Count; // result
#if __GNUC__ >= 4
// PowerPC is defined for __builtin_clzll(0)
#if !defined(__ppc__) && !defined(__ppc64__)
@@ -137,7 +137,7 @@ static inline unsigned CountLeadingZeros_64(uint64_t Value) {
if (!Value) return 64;
Count = 0;
// bisection method for count leading zeros
- for (unsigned Shift = 64 >> 1; Shift; Shift >>= 1) {
+ for (Shift = 64 >> 1; Shift; Shift >>= 1) {
uint64_t Tmp = Value >> Shift;
if (Tmp) {
Value = Tmp;
@@ -242,7 +242,7 @@ static inline unsigned CountPopulation_32(uint32_t Value) {
#else
uint32_t v = Value - ((Value >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
- return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
+ return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
#endif
}
--
1.8.5.3

View File

@ -1,4 +1,4 @@
#include "r_util.h"
#include "r_types.h"
#include "utils.h"
// XXX: most of those functions are already implemented in r_util. reuse!
@ -19,11 +19,11 @@ uint8_t cmd_checksum(const char* command) {
/**
* Converts str to ut64
* Converts str to uint64_t
*/
ut64 unpack_uint64(char *buff, int len) {
uint64_t unpack_uint64(char *buff, int len) {
int nibble;
ut64 retval = 0;
uint64_t retval = 0;
while (len) {
nibble = hex2int(*buff++);
retval |= nibble;
@ -36,10 +36,10 @@ ut64 unpack_uint64(char *buff, int len) {
/**
* Changed byte order and
* converts the value into ut64
* converts the value into uint64_t
*/
ut64 unpack_uint64_co(char* buff, int len) {
ut64 result = 0;
uint64_t unpack_uint64_co(char* buff, int len) {
uint64_t result = 0;
int i;
for (i = len - 2; i >= 0; i-=2) {
result |= unpack_uint64(&buff[i], 2);
@ -80,7 +80,7 @@ char hex2char(char* hex) {
}
int unpack_hex(char* src, ut64 len, char* dst) {
int unpack_hex(char* src, uint64_t len, char* dst) {
int i = 0;
while (i < (len / 2)) {
int val = hex2int(src[(i*2)]);
@ -93,7 +93,7 @@ int unpack_hex(char* src, ut64 len, char* dst) {
}
int pack_hex(char* src, ut64 len, char* dst) {
int pack_hex(char* src, uint64_t len, char* dst) {
int i = 0;
int x = 0;
while (i < (len*2)) {
@ -106,12 +106,12 @@ int pack_hex(char* src, ut64 len, char* dst) {
}
void hexdump(void* ptr, ut64 len, ut64 offset) {
void hexdump(void* ptr, uint64_t len, uint64_t offset) {
unsigned char* data = (unsigned char*)ptr;
int x = 0;
char hex[49], *p;
char txt[17], *c;
ut64 curr_offset;
uint64_t curr_offset;
while (x < len) {
p = hex;
c = txt;