Skip to content

Commit

Permalink
Initial support for capstone-m68k
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Oct 7, 2015
1 parent 58fd60b commit d629131
Show file tree
Hide file tree
Showing 9 changed files with 742 additions and 4 deletions.
593 changes: 593 additions & 0 deletions libr/anal/p/anal_m68k_cs.c

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion libr/anal/p/anal_mips_cs.c
Expand Up @@ -326,7 +326,6 @@ static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len) {
op->size = 4;
if (handle == 0) {
ret = cs_open (CS_ARCH_MIPS, mode, &handle);
eprintf ("Plantilla\n");
if (ret != CS_ERR_OK) goto fin;
cs_option (handle, CS_OPT_DETAIL, CS_OPT_ON);
}
Expand Down
13 changes: 13 additions & 0 deletions libr/anal/p/m68k_cs.mk
@@ -0,0 +1,13 @@
OBJ_M68K_CS=anal_m68k_cs.o

include ${CURDIR}capstone.mk

STATIC_OBJ+=$(OBJ_M68K_CS)

TARGET_M68K_CS=anal_m68k_cs.${EXT_SO}

ALL_TARGETS+=${TARGET_M68K_CS}

${TARGET_M68K_CS}: ${OBJ_M68K_CS}
${CC} ${CFLAGS} $(call libname,anal_m68k_cs) $(CS_CFLAGS) \
-o anal_m68k_cs.${EXT_SO} ${OBJ_M68K_CS} $(CS_LDFLAGS)
2 changes: 0 additions & 2 deletions libr/asm/p/asm_m68k.c
Expand Up @@ -43,8 +43,6 @@ RAsmPlugin r_asm_plugin_m68k = {
.license = "BSD",
.bits = 16|32,
.desc = "Motorola 68000",
.init = NULL,
.fini = NULL,
.disassemble = &disassemble,
.assemble = NULL
};
Expand Down
115 changes: 115 additions & 0 deletions libr/asm/p/asm_m68k_cs.c
@@ -0,0 +1,115 @@
/* radare2 - LGPL - Copyright 2015 - pancake */

#include <r_asm.h>
#include <r_lib.h>
#include <capstone/capstone.h>

#if CS_API_MAJOR>=4 && CS_NEXT_VERSION>=3
#define CAPSTONE_HAS_M68K 1
#else
#define CAPSTONE_HAS_M68K 0
#warning Cannot find capstone-m68k support
#endif

#if CAPSTONE_HAS_M68K

static bool check_features(RAsm *a, cs_insn *insn);
static csh cd;

static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
cs_insn* insn = NULL;
cs_mode mode = 0;
int ret, n = 0;
mode |= (a->big_endian)? CS_MODE_BIG_ENDIAN: CS_MODE_LITTLE_ENDIAN;

// replace this with the asm.features?
if (a->cpu && strstr (a->cpu, "mclass"))
mode |= CS_MODE_MCLASS;
if (a->cpu && strstr (a->cpu, "v8"))
mode |= CS_MODE_V8;
op->size = 4;
op->buf_asm[0] = 0;
ret = cs_open (CS_ARCH_M68K, mode, &cd);
if (ret) {
ret = -1;
goto beach;
}
if (a->syntax == R_ASM_SYNTAX_REGNUM) {
cs_option (cd, CS_OPT_SYNTAX, CS_OPT_SYNTAX_NOREGNAME);
} else cs_option (cd, CS_OPT_SYNTAX, CS_OPT_SYNTAX_DEFAULT);
if (a->features && *a->features) {
cs_option (cd, CS_OPT_DETAIL, CS_OPT_ON);
} else {
cs_option (cd, CS_OPT_DETAIL, CS_OPT_OFF);
}
n = cs_disasm (cd, buf, R_MIN (4, len),
a->pc, 1, &insn);
if (n<1) {
ret = -1;
goto beach;
}
op->size = 0;
if (insn->size<1) {
ret = -1;
goto beach;
}
if (a->features && *a->features) {
if (!check_features (a, insn)) {
op->size = insn->size;
strcpy (op->buf_asm, "illegal");
}
}
if (!op->size) {
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, '#');
}
cs_free (insn, n);
beach:
cs_close (&cd);
if (!op->buf_asm[0])
strcpy (op->buf_asm, "invalid");
return op->size;
}

RAsmPlugin r_asm_plugin_m68k_cs = {
.name = "m68k.cs",
.desc = "Capstone M68K disassembler",
.cpus = "68000,68010,68020,68030,68040,68060",
.license = "BSD",
.arch = "m68k",
.bits = 32,
.disassemble = &disassemble,
.features = NULL
};

static bool check_features(RAsm *a, cs_insn *insn) {
/* TODO: Implement support for m68k */
return true;
}

#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_m68k_cs,
.version = R2_VERSION
};
#endif

#else
RAsmPlugin r_asm_plugin_m68k_cs = {
.name = "m68k.cs (unsupported)",
.desc = "Capstone M68K disassembler (unsupported)",
.license = "BSD",
.arch = "m68k",
.bits = 32,
};
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_m68k_cs,
.version = R2_VERSION
};
#endif
17 changes: 17 additions & 0 deletions libr/asm/p/m68k_cs.mk
@@ -0,0 +1,17 @@
# capstone-m68k

OBJ_M68KCS=asm_m68k_cs.o

include p/capstone.mk

STATIC_OBJ+=${OBJ_M68KCS}
SHARED_OBJ+=${SHARED_M68KCS}
TARGET_M68KCS=asm_m68k_cs.${EXT_SO}

ifeq ($(WITHPIC),1)
ALL_TARGETS+=${TARGET_M68KCS}

${TARGET_M68KCS}: ${OBJ_M68KCS}
${CC} $(call libname,asm_m68k_cs) ${LDFLAGS} ${CFLAGS} ${CS_CFLAGS} \
-o ${TARGET_M68KCS} ${OBJ_M68KCS} ${CS_LDFLAGS}
endif
1 change: 1 addition & 0 deletions libr/include/r_anal.h
Expand Up @@ -1477,6 +1477,7 @@ extern RAnalPlugin r_anal_plugin_sh;
extern RAnalPlugin r_anal_plugin_sparc_gnu;
extern RAnalPlugin r_anal_plugin_bf;
extern RAnalPlugin r_anal_plugin_m68k;
extern RAnalPlugin r_anal_plugin_m68k_cs;
extern RAnalPlugin r_anal_plugin_z80;
extern RAnalPlugin r_anal_plugin_i8080;
extern RAnalPlugin r_anal_plugin_8051;
Expand Down
2 changes: 1 addition & 1 deletion libr/include/r_asm.h
Expand Up @@ -180,7 +180,6 @@ extern RAsmPlugin r_asm_plugin_arm_as;
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_gnu;
extern RAsmPlugin r_asm_plugin_ppc_cs;
extern RAsmPlugin r_asm_plugin_sparc_gnu;
Expand All @@ -191,6 +190,7 @@ extern RAsmPlugin r_asm_plugin_sh;
extern RAsmPlugin r_asm_plugin_z80;
extern RAsmPlugin r_asm_plugin_i8080;
extern RAsmPlugin r_asm_plugin_m68k;
extern RAsmPlugin r_asm_plugin_m68k_cs;
extern RAsmPlugin r_asm_plugin_arc;
extern RAsmPlugin r_asm_plugin_rar;
extern RAsmPlugin r_asm_plugin_dcpu16;
Expand Down
2 changes: 2 additions & 0 deletions plugins.def.cfg
Expand Up @@ -14,6 +14,7 @@ anal.h8300
anal.i8080
anal.java
anal.m68k
anal.m68k_cs
anal.malbolge
anal.mips_cs
anal.mips_gnu
Expand Down Expand Up @@ -58,6 +59,7 @@ asm.i8080
asm.java
asm.lm32
asm.m68k
asm.m68k_cs
asm.malbolge
asm.mips_cs
asm.mips_gnu
Expand Down

0 comments on commit d629131

Please sign in to comment.