diff --git a/dist/plugins-cfg/plugins.def.cfg b/dist/plugins-cfg/plugins.def.cfg index 5268424f17..a194abac0c 100644 --- a/dist/plugins-cfg/plugins.def.cfg +++ b/dist/plugins-cfg/plugins.def.cfg @@ -57,7 +57,7 @@ arch.v850 arch.vax arch.ws anal.x86_cs -anal.xcore_cs +arch.xcore_cs arch.xtensa arch.z80 arch.amd29k diff --git a/dist/plugins-cfg/plugins.mingw.cfg b/dist/plugins-cfg/plugins.mingw.cfg index cbbd17566f..4bcddc67f6 100644 --- a/dist/plugins-cfg/plugins.mingw.cfg +++ b/dist/plugins-cfg/plugins.mingw.cfg @@ -44,7 +44,7 @@ arch.ws arch.xap anal.x86_cs arch.m68k_gnu -anal.xcore_cs +arch.xcore_cs arch.z80 arch.v810 arch.vax diff --git a/dist/plugins-cfg/plugins.nogpl.cfg b/dist/plugins-cfg/plugins.nogpl.cfg index 0b9a4ff5d9..adf8055d41 100644 --- a/dist/plugins-cfg/plugins.nogpl.cfg +++ b/dist/plugins-cfg/plugins.nogpl.cfg @@ -21,7 +21,7 @@ anal.tms320 arch.v850 arch.ws anal.x86_cs -anal.xcore_cs +arch.xcore_cs arch.null arch.i4004 esil.dummy diff --git a/dist/plugins-cfg/plugins.static.cfg b/dist/plugins-cfg/plugins.static.cfg index 228a9fa380..273c5556fd 100644 --- a/dist/plugins-cfg/plugins.static.cfg +++ b/dist/plugins-cfg/plugins.static.cfg @@ -32,7 +32,7 @@ arch.lanai arch.ws arch.xap anal.x86_cs -anal.xcore_cs +arch.xcore_cs arch.z80 arch.vax arch.6502 diff --git a/dist/plugins-cfg/plugins.static.nogpl.cfg b/dist/plugins-cfg/plugins.static.nogpl.cfg index 95fb384e44..7f038d6a4e 100644 --- a/dist/plugins-cfg/plugins.static.nogpl.cfg +++ b/dist/plugins-cfg/plugins.static.nogpl.cfg @@ -19,7 +19,7 @@ arch.s390_cs arch.ws arch.xap anal.x86_cs -anal.xcore_cs +arch.xcore_cs arch.lh5801 arch.6502 arch.snes diff --git a/dist/plugins-cfg/plugins.termux.cfg b/dist/plugins-cfg/plugins.termux.cfg index 4f4dcfc9ec..8425c98893 100644 --- a/dist/plugins-cfg/plugins.termux.cfg +++ b/dist/plugins-cfg/plugins.termux.cfg @@ -20,7 +20,7 @@ anal.sparc_cs arch.sparc_gnu arch.v850 anal.x86_cs -anal.xcore_cs +arch.xcore_cs arch.z80 arch.mcore arch.vax diff --git a/libr/anal/meson.build b/libr/anal/meson.build index fc62b42180..d3c6b0a9f0 100644 --- a/libr/anal/meson.build +++ b/libr/anal/meson.build @@ -77,7 +77,7 @@ r_anal_sources = [ 'p/anal_tms320.c', # join_paths('arch/whitespace/wsdis.c'), 'p/anal_x86_cs.c', - 'p/anal_xcore_cs.c', + '../arch/p/xcore_cs/plugin.c', #join_paths('arch','gb','meta_gb_cmt.c'), '../arch/p/msp430/plugin.c', '../arch/p/msp430/msp430_disas.c', diff --git a/libr/anal/p/xcore_cs.mk b/libr/anal/p/xcore_cs.mk deleted file mode 100644 index 11b53ede2c..0000000000 --- a/libr/anal/p/xcore_cs.mk +++ /dev/null @@ -1,12 +0,0 @@ -OBJ_XCORE_CS=anal_xcore_cs.o - -include p/capstone.mk - -STATIC_OBJ+=${OBJ_XCORE_CS} -TARGET_XCORE_CS=anal_xcore_cs.${EXT_SO} - -ALL_TARGETS+=${TARGET_XCORE_CS} - -${TARGET_XCORE_CS}: ${OBJ_XCORE_CS} - ${CC} ${CFLAGS} $(call libname,anal_xcore_cs) $(CS_CFLAGS) \ - -o anal_xcore_cs.${EXT_SO} ${OBJ_XCORE_CS} $(CS_LDFLAGS) diff --git a/libr/arch/meson.build b/libr/arch/meson.build index 9d1cec48f5..87fb627234 100644 --- a/libr/arch/meson.build +++ b/libr/arch/meson.build @@ -113,6 +113,7 @@ r_arch_sources = [ 'p/msp430/msp430_disas.c', 'p/h8300/plugin.c', 'p/h8300/h8300_disas.c', + 'p/xcore_cs/plugin.c', # python 'p/pyc/plugin.c', 'p/pyc/opcode_all.c', diff --git a/libr/arch/p/xcore_cs.mk b/libr/arch/p/xcore_cs.mk new file mode 100644 index 0000000000..7ed7da5723 --- /dev/null +++ b/libr/arch/p/xcore_cs.mk @@ -0,0 +1,12 @@ +OBJ_XCORE_CS=p/xcore_cs/plugin.o + +include p/capstone.mk + +STATIC_OBJ+=${OBJ_XCORE_CS} +TARGET_XCORE_CS=xcore_cs.${EXT_SO} + +ALL_TARGETS+=${TARGET_XCORE_CS} + +${TARGET_XCORE_CS}: ${OBJ_XCORE_CS} + ${CC} ${CFLAGS} $(call libname,xcore_cs) $(CS_CFLAGS) \ + -o xcore_cs.${EXT_SO} ${OBJ_XCORE_CS} $(CS_LDFLAGS) diff --git a/libr/anal/p/anal_xcore_cs.c b/libr/arch/p/xcore_cs/plugin.c similarity index 63% rename from libr/anal/p/anal_xcore_cs.c rename to libr/arch/p/xcore_cs/plugin.c index 089472c5bb..bc748a9320 100644 --- a/libr/anal/p/anal_xcore_cs.c +++ b/libr/arch/p/xcore_cs/plugin.c @@ -14,8 +14,8 @@ #define CSINC XCORE #define CSINC_MODE \ CS_MODE_BIG_ENDIAN \ - | (a->config->cpu != NULL && ((!strcmp (a->config->cpu, "v9"))) ? CS_MODE_V9 : 0) -#include "capstone.inc" + | (as->config->cpu != NULL && ((!strcmp (as->config->cpu, "v9"))) ? CS_MODE_V9 : 0) +#include "../capstone.inc" static void opex(RStrBuf *buf, csh handle, cs_insn *insn) { int i; @@ -59,10 +59,20 @@ static void opex(RStrBuf *buf, csh handle, cs_insn *insn) { pj_free (pj); } -static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len, RAnalOpMask mask) { - csh handle = init_capstone (a); +static csh cs_handle_for_session(RArchSession *as) { + r_return_val_if_fail (as && as->data, 0); + CapstonePluginData *pd = as->data; + return pd->cs_handle; +} + +static bool decode(RArchSession *as, RAnalOp *op, RAnalOpMask mask) { + const ut64 addr = op->addr; + const ut8 *buf = op->bytes; + const int len = op->size; + + csh handle = cs_handle_for_session (as); if (handle == 0) { - return -1; + return false; } cs_insn *insn; @@ -117,25 +127,60 @@ static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len, RAn } cs_free (insn, n); } - return op->size; + return op->size > 0; } -RAnalPlugin r_anal_plugin_xcore_cs = { +static int archinfo(RArchSession *as, ut32 q) { + return 0; +} + +static char *mnemonics(RArchSession *as, int id, bool json) { + CapstonePluginData *cpd = as->data; + return r_arch_cs_mnemonics (as, cpd->cs_handle, id, json); +} + +static bool init(RArchSession *as) { + r_return_val_if_fail (as, false); + if (as->data) { + R_LOG_WARN ("Already initialized"); + return false; + } + as->data = R_NEW0 (CapstonePluginData); + CapstonePluginData *cpd = (CapstonePluginData*)as->data; + if (!r_arch_cs_init (as, &cpd->cs_handle)) { + R_LOG_ERROR ("Cannot initialize capstone"); + R_FREE (as->data); + return false; + } + return true; +} + +static bool fini(RArchSession *as) { + r_return_val_if_fail (as, false); + CapstonePluginData *cpd = as->data; + cs_close (&cpd->cs_handle); + R_FREE (as->data); + return true; +} + +RArchPlugin r_arch_plugin_xcore_cs = { .name = "xcore", .desc = "Capstone XCORE analysis", .license = "BSD", - .esil = false, .arch = "xcore", - .bits = 32, - .op = &analop, - //.set_reg_profile = &set_reg_profile, - .mnemonics = cs_mnemonics, + .bits = R_SYS_BITS_PACK1 (32), + .decode = decode, + .info = archinfo, + //.regs = regs, + .mnemonics = mnemonics, + .init = init, + .fini = fini, }; #ifndef R2_PLUGIN_INCORE R_API RLibStruct radare_plugin = { - .type = R_LIB_TYPE_ANAL, - .data = &r_anal_plugin_xcore_cs, + .type = R_LIB_TYPE_ARCH, + .data = &r_arch_plugin_xcore_cs, .version = R2_VERSION }; #endif diff --git a/libr/include/r_anal.h b/libr/include/r_anal.h index 933e7e23c3..13c58bb4e5 100644 --- a/libr/include/r_anal.h +++ b/libr/include/r_anal.h @@ -1584,7 +1584,6 @@ extern RAnalPlugin r_anal_plugin_x86_cs; extern RAnalPlugin r_anal_plugin_x86_im; extern RAnalPlugin r_anal_plugin_x86_simple; extern RAnalPlugin r_anal_plugin_x86_udis; -extern RAnalPlugin r_anal_plugin_xcore_cs; extern RAnalPlugin r_anal_plugin_pickle; extern RAnalPlugin r_anal_plugin_evm_cs; diff --git a/libr/include/r_arch.h b/libr/include/r_arch.h index 26c7b66938..9939d8cc4b 100644 --- a/libr/include/r_arch.h +++ b/libr/include/r_arch.h @@ -340,6 +340,7 @@ extern RArchPlugin r_arch_plugin_ppc_gnu; extern RArchPlugin r_arch_plugin_loongarch_gnu; extern RArchPlugin r_arch_plugin_6502_cs; extern RArchPlugin r_arch_plugin_m680x_cs; +extern RArchPlugin r_arch_plugin_xcore_cs; #ifdef __cplusplus } diff --git a/libr/meson.build b/libr/meson.build index 9abd96ad6e..78093a9ba4 100644 --- a/libr/meson.build +++ b/libr/meson.build @@ -169,7 +169,6 @@ anal_plugins += [ 'sparc_cs', 'tms320', 'x86_cs', - 'xcore_cs' ] arch_plugins += [ @@ -178,6 +177,7 @@ arch_plugins += [ 's390_cs', 'm68k_cs', 'm680x_cs', + 'xcore_cs' ] if no_user_plugins