Migrate the mips.cs plugin ##arch

This commit is contained in:
pancake 2023-06-15 18:45:35 +02:00
parent 02a3d44e6f
commit 5ad8cf97f2
33 changed files with 202 additions and 136 deletions

View File

@ -7,7 +7,7 @@ anal.null
anal.x86_cs anal.x86_cs
anal.arm_cs anal.arm_cs
arch.arm_v35 arch.arm_v35
anal.mips_cs arch.mips_cs
arch.java arch.java
anal.dalvik anal.dalvik
arch.gb arch.gb

View File

@ -27,7 +27,7 @@ anal.dalvik
arch.i8080 arch.i8080
arch.java arch.java
anal.m68k anal.m68k
anal.mips_cs arch.mips_cs
arch.ppc_cs arch.ppc_cs
arch.ppc_gnu arch.ppc_gnu
arch.sparc arch.sparc

View File

@ -7,7 +7,7 @@ anal.dalvik
arch.gb arch.gb
arch.i8080 arch.i8080
arch.java arch.java
anal.mips_cs arch.mips_cs
anal.null anal.null
arch.ppc_cs arch.ppc_cs
anal.x86_cs anal.x86_cs

View File

@ -7,7 +7,7 @@ arch.gb
arch.arm_gnu arch.arm_gnu
arch.i8080 arch.i8080
arch.java arch.java
anal.mips_cs arch.mips_cs
anal.null anal.null
anal.x86_cs anal.x86_cs
arch.6502 arch.6502

View File

@ -6,8 +6,8 @@ anal.arm_cs
arch.avr arch.avr
anal.dalvik anal.dalvik
arch.i8080 arch.i8080
anal.m68k arch.m68k
anal.mips_cs arch.mips_cs
arch.sparc arch.sparc
anal.x86_cs anal.x86_cs
anal.x86_im anal.x86_im

View File

@ -5,7 +5,7 @@ arch.bf
anal.null anal.null
anal.x86_cs anal.x86_cs
anal.arm_cs anal.arm_cs
anal.mips_cs arch.mips_cs
arch.java arch.java
anal.dalvik anal.dalvik
arch.gb arch.gb

View File

@ -6,7 +6,7 @@ arch.bf
anal.null anal.null
anal.x86_cs anal.x86_cs
anal.arm_cs anal.arm_cs
anal.mips_cs arch.mips_cs
arch.java arch.java
anal.dalvik anal.dalvik
arch.gb arch.gb

View File

@ -21,7 +21,7 @@ arch.lh5801
arch.m68k_cs arch.m68k_cs
arch.m680x_cs arch.m680x_cs
arch.mcore arch.mcore
anal.mips_cs arch.mips_cs
arch.mcs96 arch.mcs96
arch.mips_gnu arch.mips_gnu
arch.msp430 arch.msp430

View File

@ -11,7 +11,7 @@ arch.h8300
arch.i8080 arch.i8080
arch.java arch.java
arch.m680x_cs arch.m680x_cs
anal.mips_cs arch.mips_cs
arch.msp430 arch.msp430
anal.null anal.null
arch.sparc_cs arch.sparc_cs

View File

@ -17,7 +17,7 @@ arch.kvx
arch.lh5801 arch.lh5801
arch.lm32 arch.lm32
arch.m68k_cs arch.m68k_cs
anal.mips_cs arch.mips_cs
arch.mips_gnu arch.mips_gnu
anal.null anal.null
arch.ppc_cs arch.ppc_cs

View File

@ -10,7 +10,7 @@ arch.lm32
arch.i8080 arch.i8080
arch.java arch.java
arch.m68k_cs arch.m68k_cs
anal.mips_cs arch.mips_cs
arch.mcs96 arch.mcs96
anal.null anal.null
arch.sh arch.sh

View File

@ -7,7 +7,7 @@ anal.dalvik
arch.gb arch.gb
arch.java arch.java
arch.lh5801 arch.lh5801
anal.mips_cs arch.mips_cs
arch.mips_gnu arch.mips_gnu
anal.null anal.null
arch.pdp11 arch.pdp11

View File

@ -4,7 +4,7 @@ arch.x86_nz
anal.null anal.null
anal.x86_cs anal.x86_cs
anal.arm_cs anal.arm_cs
anal.mips_cs arch.mips_cs
arch.java arch.java
anal.dalvik anal.dalvik
arch.gb arch.gb

View File

@ -8,7 +8,7 @@ arch.bf
arch.bpf arch.bpf
arch.riscv arch.riscv
arch.ppc_gnu arch.ppc_gnu
anal.mips_cs arch.mips_cs
anal.x86_cs anal.x86_cs
anal.arm_cs anal.arm_cs
anal.null anal.null

View File

@ -59,13 +59,6 @@ r_anal_sources = [
'../arch/p/m68k_gnu/plugin.c', '../arch/p/m68k_gnu/plugin.c',
'../arch/p/m68k_gnu/m68k-dis.c', '../arch/p/m68k_gnu/m68k-dis.c',
'../arch/p/m68k_gnu/m68k-opc.c', '../arch/p/m68k_gnu/m68k-opc.c',
'p/anal_mips_cs.c',
'../asm/arch/mips/mipsasm.c',
'../arch/p/mips_gnu/mips-dis.c',
'../arch/p/mips_gnu/mips16-opc.c',
'../arch/p/mips_gnu/micromips-opc.c',
'../arch/p/mips_gnu/mips-opc.c',
'../arch/p/mips_gnu/plugin.c',
'../arch/p/loongarch/plugin.c', '../arch/p/loongarch/plugin.c',
'../arch/p/loongarch/loongarch-coder.c', '../arch/p/loongarch/loongarch-coder.c',
'../arch/p/loongarch/loongarch-dis.c', '../arch/p/loongarch/loongarch-dis.c',

View File

@ -11,8 +11,6 @@ ifeq ($(WITHPIC),1)
all: ${ALL_TARGETS} all: ${ALL_TARGETS}
ALL_TARGETS= ALL_TARGETS=
# TODO: rename to enabled plugins
# ANAL_PLUGINS=null.mk avr.mk xap.mk bpf.mk dalvik.mk sh.mk ebc.mk lh5801.mk ws.mk h8300.mk cr16.mk v850.mk msp430.mk sparc_gnu.mk sparc_cs.mk x86_cs.mk cris.mk 6502.mk snes.mk riscv.mk vax.mk xtensa.mk rsp.mk tricore.mk s390_cs.mk pickle.mk
include $(ANAL_PLUGINS) include $(ANAL_PLUGINS)
clean: clean:

View File

@ -1,13 +0,0 @@
OBJ_MIPS_CS=anal_mips_cs.o
OBJ_MIPS_CS+=../../asm/arch/mips/mipsasm.o
include $(CURDIR)capstone.mk
STATIC_OBJ+=$(OBJ_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) $(CS_CFLAGS) \
-o anal_mips_cs.${EXT_SO} ${OBJ_MIPS_CS} $(CS_LDFLAGS)

View File

@ -137,6 +137,8 @@ r_arch_sources = [
'p/h8300/plugin.c', 'p/h8300/plugin.c',
'p/h8300/h8300_disas.c', 'p/h8300/h8300_disas.c',
'p/xcore_cs/plugin.c', 'p/xcore_cs/plugin.c',
'p/mips/plugin_cs.c',
'p/mips/mipsasm.c',
'p/mips_gnu/plugin.c', 'p/mips_gnu/plugin.c',
'p/mips_gnu/mips-dis.c', 'p/mips_gnu/mips-dis.c',
'p/mips_gnu/mips16-opc.c', 'p/mips_gnu/mips16-opc.c',
@ -207,8 +209,7 @@ r_arch = library('r_arch', r_arch_sources,
soversion: r2_libversion soversion: r2_libversion
) )
r_arch_dep = declare_dependency(link_with: r_arch, r_arch_dep = declare_dependency(link_with: r_arch, include_directories: [platform_inc])
include_directories: [platform_inc])
if get_option('blob') if get_option('blob')
r_arch_static = static_library('r_arch_static', r_arch_sources, r_arch_static = static_library('r_arch_static', r_arch_sources,
@ -235,6 +236,7 @@ pkgconfig_mod.generate(r_arch,
requires: [ requires: [
'r_util', 'r_util',
'r_esil', 'r_esil',
'r_crypto',
'r_reg', 'r_reg',
], ],
description: 'radare foundation libraries' description: 'radare foundation libraries'

View File

@ -2601,18 +2601,10 @@ static int archinfo(RArchSession *anal, ut32 q) {
} }
static RList *anal_preludes(RArchSession *as) { static RList *anal_preludes(RArchSession *as) {
#if 0
RList *l = r_list_newf ((RListFree)r_search_keyword_free);
#define KW(d,ds,m,ms) r_list_append (l, r_search_keyword_new ((const ut8*)d, ds, (const ut8*)m, ms, NULL))
KW ("\xf0\x00\x00\xd1", 4, "\xf0\x00\x00\xff", 4);
KW ("\xf0\x00\x00\xa9", 4, "\xf0\x00\x00\xff", 4);
KW ("\x7f\x23\x03\xd5\xff", 5, NULL, 0);
#else
RList *l = r_list_newf (free); RList *l = r_list_newf (free);
r_list_append (l, r_str_newf ("f00000d1 f00000ff")); r_list_append (l, r_str_newf ("f00000d1 f00000ff"));
r_list_append (l, r_str_newf ("f00000a9 f00000ff")); r_list_append (l, r_str_newf ("f00000a9 f00000ff"));
r_list_append (l, r_str_newf ("7f2303d5ff")); r_list_append (l, r_str_newf ("7f2303d5ff"));
#endif
return l; return l;
} }

View File

@ -16,6 +16,7 @@
typedef struct { typedef struct {
csh cs_handle; csh cs_handle;
// store cpu and endian too ?
} CapstonePluginData; } CapstonePluginData;
static void initcs(csh *ud) { static void initcs(csh *ud) {

View File

@ -1,4 +1,4 @@
/* radare2 - LGPL - Copyright 2013-2022 - pancake */ /* radare2 - LGPL - Copyright 2013-2023 - pancake */
#include <r_asm.h> #include <r_asm.h>
#include <r_lib.h> #include <r_lib.h>
@ -89,14 +89,14 @@ static R_TH_LOCAL ut64 t9_pre = UT64_MAX;
#define ES_W(x) "0xffffffff,"x",&" #define ES_W(x) "0xffffffff,"x",&"
// sign extend 32 -> 64 // sign extend 32 -> 64
#define ES_SIGN32_64(arg) es_sign_n_64 (a, op, arg, 32) #define ES_SIGN32_64(arg) es_sign_n_64 (as, op, arg, 32)
#define ES_SIGN16_64(arg) es_sign_n_64 (a, op, arg, 16) #define ES_SIGN16_64(arg) es_sign_n_64 (as, op, arg, 16)
#define ES_ADD_CK32_OVERF(x, y, z) es_add_ck (op, x, y, z, 32) #define ES_ADD_CK32_OVERF(x, y, z) es_add_ck (op, x, y, z, 32)
#define ES_ADD_CK64_OVERF(x, y, z) es_add_ck (op, x, y, z, 64) #define ES_ADD_CK64_OVERF(x, y, z) es_add_ck (op, x, y, z, 64)
static inline void es_sign_n_64(RAnal *a, RAnalOp *op, const char *arg, int bit) { static inline void es_sign_n_64(RArchSession *as, RAnalOp *op, const char *arg, int bit) {
if (a->config->bits == 64) { if (as->config->bits == 64) {
r_strbuf_appendf (&op->esil, ",%d,%s,~,%s,=,", bit, arg, arg); r_strbuf_appendf (&op->esil, ",%d,%s,~,%s,=,", bit, arg, arg);
} else { } else {
r_strbuf_append (&op->esil, ","); r_strbuf_append (&op->esil, ",");
@ -202,7 +202,7 @@ static const char *arg(csh *handle, cs_insn *insn, char *buf, size_t buf_sz, int
#define ARG(x) (*str[x] != 0)?str[x]:arg(handle, insn, str[x], sizeof (str[x]), x) #define ARG(x) (*str[x] != 0)?str[x]:arg(handle, insn, str[x], sizeof (str[x]), x)
static int analop_esil(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len, csh *handle, cs_insn *insn) { static int analop_esil(RArchSession *as, RAnalOp *op, csh *handle, cs_insn *insn) {
char str[8][32] = {{0}}; char str[8][32] = {{0}};
int i; int i;
@ -685,7 +685,7 @@ static const char *parse_reg_name(csh handle, cs_insn *insn, int reg_num) {
return NULL; return NULL;
} }
static void op_fillval(RAnal *anal, RAnalOp *op, csh *handle, cs_insn *insn) { static void op_fillval(RArchSession *as, RAnalOp *op, csh *handle, cs_insn *insn) {
RAnalValue *dst, *src0, *src1; RAnalValue *dst, *src0, *src1;
switch (op->type & R_ANAL_OP_TYPE_MASK) { switch (op->type & R_ANAL_OP_TYPE_MASK) {
case R_ANAL_OP_TYPE_LOAD: case R_ANAL_OP_TYPE_LOAD:
@ -779,9 +779,9 @@ static void set_opdir(RAnalOp *op) {
} }
} }
static int get_capstone_mode (RAnal *anal) { static int get_capstone_mode(RArchSession *as) {
int mode = R_ARCH_CONFIG_IS_BIG_ENDIAN (anal->config)? CS_MODE_BIG_ENDIAN: CS_MODE_LITTLE_ENDIAN; int mode = R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config)? CS_MODE_BIG_ENDIAN: CS_MODE_LITTLE_ENDIAN;
const char *cpu = anal->config->cpu; const char *cpu = as->config->cpu;
if (R_STR_ISNOTEMPTY (cpu)) { if (R_STR_ISNOTEMPTY (cpu)) {
if (!strcmp (cpu, "micro")) { if (!strcmp (cpu, "micro")) {
mode |= CS_MODE_MICRO; mode |= CS_MODE_MICRO;
@ -795,33 +795,95 @@ static int get_capstone_mode (RAnal *anal) {
#endif #endif
} }
} }
mode |= (anal->config->bits == 64)? CS_MODE_MIPS64: CS_MODE_MIPS32; mode |= (as->config->bits == 64)? CS_MODE_MIPS64: CS_MODE_MIPS32;
return mode; return mode;
} }
#define CSINC MIPS #define CSINC MIPS
#define CSINC_MODE get_capstone_mode(a) #define CSINC_MODE get_capstone_mode(as)
#include "capstone.inc.c" #include "../capstone.inc.c"
static int analop(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *buf, int len, RAnalOpMask mask) { typedef struct plugin_data_t {
csh hndl = init_capstone (anal); CapstonePluginData cpd;
if (hndl == 0) { RRegItem reg;
return -1; char *cpu;
int bigendian;
} PluginData;
static bool init(RArchSession *as) {
r_return_val_if_fail (as, false);
if (as->data) {
R_LOG_WARN ("Already initialized");
return false;
} }
PluginData *pd = R_NEW0 (PluginData);
if (!pd) {
return false;
}
pd->bigendian = R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config);
pd->cpu = as->config->cpu? strdup (as->config->cpu): NULL;
if (!r_arch_cs_init (as, &pd->cpd.cs_handle)) {
R_LOG_ERROR ("Cannot initialize capstone");
R_FREE (as->data);
return false;
}
as->data = pd;
return true;
}
if (anal->config->syntax == R_ARCH_SYNTAX_REGNUM) { static bool fini(RArchSession *as) {
cs_option (hndl, CS_OPT_SYNTAX, CS_OPT_SYNTAX_NOREGNAME); r_return_val_if_fail (as, false);
PluginData *pd = as->data;
cs_close (&pd->cpd.cs_handle);
R_FREE (as->data);
return true;
}
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 plugin_changed(RArchSession *as) {
PluginData *cpd = as->data;
if (R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config) != cpd->bigendian) {
return true;
}
if (cpd->cpu && as->config->cpu && strcmp (cpd->cpu, as->config->cpu)) {
eprintf ("cpudif\n");
return true;
}
return false;
}
static bool decode(RArchSession *as, RAnalOp *op, RAnalOpMask mask) {
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 false;
}
cs_insn *insn = NULL;
if (as->config->syntax == R_ARCH_SYNTAX_REGNUM) {
cs_option (handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_NOREGNAME);
} else { } else {
cs_option (hndl, CS_OPT_SYNTAX, CS_OPT_SYNTAX_DEFAULT); cs_option (handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_DEFAULT);
} }
if (plugin_changed (as)) {
fini (as);
init (as);
handle = cs_handle_for_session (as);
}
int n, opsize = -1; int n, opsize = -1;
cs_insn* insn;
// XXX no arch->cpu ?!?! CS_MODE_MICRO, N64 // XXX no arch->cpu ?!?! CS_MODE_MICRO, N64
op->addr = addr; op->addr = addr;
op->size = 4; op->size = 4;
n = cs_disasm (hndl, buf, len, addr, 1, &insn); n = cs_disasm (handle, buf, len, addr, 1, &insn);
if (n < 1 || insn->size < 1) { if (n < 1 || insn->size < 1) {
if (mask & R_ARCH_OP_MASK_DISASM) { if (mask & R_ARCH_OP_MASK_DISASM) {
op->mnemonic = strdup ("invalid"); op->mnemonic = strdup ("invalid");
@ -849,7 +911,7 @@ static int analop(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *buf, int len,
case MIPS_INS_LBU: case MIPS_INS_LBU:
case MIPS_INS_LBUX: case MIPS_INS_LBUX:
op->refptr = 1; op->refptr = 1;
/* fallthrough */ /* fallthrough */
case MIPS_INS_LW: case MIPS_INS_LW:
case MIPS_INS_LWC1: case MIPS_INS_LWC1:
case MIPS_INS_LWC2: case MIPS_INS_LWC2:
@ -859,7 +921,7 @@ static int analop(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *buf, int len,
if (!op->refptr) { if (!op->refptr) {
op->refptr = 4; op->refptr = 4;
} }
/* fallthrough */ /* fallthrough */
case MIPS_INS_LD: case MIPS_INS_LD:
case MIPS_INS_LDC1: case MIPS_INS_LDC1:
case MIPS_INS_LDC2: case MIPS_INS_LDC2:
@ -873,12 +935,12 @@ static int analop(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *buf, int len,
switch (OPERAND(1).type) { switch (OPERAND(1).type) {
case MIPS_OP_MEM: case MIPS_OP_MEM:
if (OPERAND(1).mem.base == MIPS_REG_GP) { if (OPERAND(1).mem.base == MIPS_REG_GP) {
op->ptr = anal->gp + OPERAND(1).mem.disp; op->ptr = as->config->gp + OPERAND(1).mem.disp;
if (REGID(0) == MIPS_REG_T9) { if (REGID(0) == MIPS_REG_T9) {
t9_pre = op->ptr; t9_pre = op->ptr;
} }
} else if (REGID(0) == MIPS_REG_T9) { } else if (REGID(0) == MIPS_REG_T9) {
t9_pre = UT64_MAX; t9_pre = UT64_MAX;
} }
break; break;
case MIPS_OP_IMM: case MIPS_OP_IMM:
@ -949,11 +1011,11 @@ static int analop(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *buf, int len,
case MIPS_INS_BGTZALC: case MIPS_INS_BGTZALC:
// compact versions (no delay) // compact versions (no delay)
op->delay = 0; op->delay = 0;
op->fail = addr+4; op->fail = addr + 4;
break; break;
default: default:
op->delay = 1; op->delay = 1;
op->fail = addr+8; op->fail = addr + 8;
break; break;
} }
break; break;
@ -1146,23 +1208,23 @@ static int analop(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *buf, int len,
beach: beach:
set_opdir (op); set_opdir (op);
if (insn && mask & R_ARCH_OP_MASK_OPEX) { if (insn && mask & R_ARCH_OP_MASK_OPEX) {
opex (&op->opex, hndl, insn); opex (&op->opex, handle, insn);
} }
if (mask & R_ARCH_OP_MASK_ESIL) { if (mask & R_ARCH_OP_MASK_ESIL) {
if (analop_esil (anal, op, addr, buf, len, &hndl, insn) != 0) { if (analop_esil (as, op, &handle, insn) != 0) {
r_strbuf_fini (&op->esil); r_strbuf_fini (&op->esil);
} }
} }
if (mask & R_ARCH_OP_MASK_VAL) { if (mask & R_ARCH_OP_MASK_VAL) {
op_fillval (anal, op, &hndl, insn); op_fillval (as, op, &handle, insn);
} }
cs_free (insn, n); cs_free (insn, n);
return opsize; return opsize;
} }
static char *get_reg_profile(RAnal *anal) { static char *get_reg_profile(RArchSession * as) {
const char *p = NULL; const char *p = NULL;
switch (anal->config->bits) { switch (as->config->bits) {
default: default:
case 32: p = case 32: p =
"=PC pc\n" "=PC pc\n"
@ -1264,9 +1326,9 @@ static char *get_reg_profile(RAnal *anal) {
return p? strdup (p): NULL; return p? strdup (p): NULL;
} }
static int archinfo(RAnal *anal, int q) { static int archinfo(RArchSession *as, ut32 q) {
if (q == R_ANAL_ARCHINFO_ALIGN || q == R_ANAL_ARCHINFO_MIN_OP_SIZE) { if (q == R_ANAL_ARCHINFO_ALIGN || q == R_ANAL_ARCHINFO_MIN_OP_SIZE) {
const char *cpu = anal->config->cpu; const char *cpu = as->config->cpu;
if (cpu && !strcmp (cpu, "micro")) { if (cpu && !strcmp (cpu, "micro")) {
return 2; // (anal->bits == 16) ? 2: 4; return 2; // (anal->bits == 16) ? 2: 4;
} }
@ -1274,16 +1336,25 @@ static int archinfo(RAnal *anal, int q) {
return 4; return 4;
} }
static RList *anal_preludes(RAnal *anal) { static char *mnemonics(RArchSession *as, int id, bool json) {
#define KW(d,ds,m,ms) r_list_append (l, r_search_keyword_new((const ut8*)d,ds,(const ut8*)m, ms, NULL)) r_return_val_if_fail (as && as->data, NULL);
RList *l = r_list_newf ((RListFree)r_search_keyword_free); CapstonePluginData *cpd = as->data;
KW ("\x27\xbd\x00", 3, NULL, 0); return r_arch_cs_mnemonics (as, cpd->cs_handle, id, json);
}
static RList *preludes(RArchSession *as) {
RList *l = r_list_newf (free);
r_list_append (l, r_str_newf ("27bd0000 ffffff00"));
return l; return l;
} }
static int mips_cs_opasm(RAnal *anal, ut64 addr, const char *s, ut8 *buf, int len) { static bool encode(RArchSession *as, RAnalOp *op, RArchEncodeMask mask) {
int ret = mips_assemble (s, addr, buf); ut8 buf[4] = {0};
if (R_ARCH_CONFIG_IS_BIG_ENDIAN (anal->config)) { int ret = mips_assemble (op->mnemonic, op->addr, buf);
if (ret < 1) {
return false;
}
if (R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config)) {
ut8 tmp = buf[0]; ut8 tmp = buf[0];
buf[0] = buf[3]; buf[0] = buf[3];
buf[3] = tmp; buf[3] = tmp;
@ -1291,30 +1362,36 @@ static int mips_cs_opasm(RAnal *anal, ut64 addr, const char *s, ut8 *buf, int le
buf[1] = buf[2]; buf[1] = buf[2];
buf[2] = tmp; buf[2] = tmp;
} }
return ret; free (op->bytes);
op->bytes = r_mem_dup (buf, 4);
op->size = 4;
return true;
} }
RAnalPlugin r_anal_plugin_mips_cs = { RArchPlugin r_arch_plugin_mips_cs = {
.name = "mips", .meta = {
.desc = "Capstone MIPS analyzer", .name = "mips",
.license = "BSD", .desc = "Capstone MIPS analyzer",
.esil = true, .license = "BSD",
},
.arch = "mips", .arch = "mips",
.cpus = "mips32/64,micro,r6,v3,v2", .cpus = "mips32/64,micro,r6,v3,v2",
.get_reg_profile = get_reg_profile, .regs = get_reg_profile,
.archinfo = archinfo, .info = archinfo,
.preludes = anal_preludes, .preludes = preludes,
.bits = 16 | 32 | 64, .bits = R_SYS_BITS_PACK3 (16, 32, 64),
.endian = R_SYS_ENDIAN_LITTLE | R_SYS_ENDIAN_BIG, .endian = R_SYS_ENDIAN_LITTLE | R_SYS_ENDIAN_BIG,
.op = &analop, .init = init,
.opasm = &mips_cs_opasm, .fini = fini,
.mnemonics = cs_mnemonics, .decode = decode,
.encode = encode,
.mnemonics = mnemonics,
}; };
#ifndef R2_PLUGIN_INCORE #ifndef R2_PLUGIN_INCORE
R_API RLibStruct radare_plugin = { R_API RLibStruct radare_plugin = {
.type = R_LIB_TYPE_ANAL, .type = R_LIB_TYPE_ARCH,
.data = &r_anal_plugin_mips_cs, .data = &r_arch_plugin_mips_cs,
.version = R2_VERSION .version = R2_VERSION
}; };
#endif #endif

11
libr/arch/p/mips_cs.mk Normal file
View File

@ -0,0 +1,11 @@
OBJ_MIPS_CS=p/mips/plugin_cs.o
OBJ_MIPS_CS+=p/mips/mipsasm.o
STATIC_OBJ+=$(OBJ_MIPS_CS)
TARGET_MIPS_CS=arch_mips_cs.${EXT_SO}
ALL_TARGETS+=${TARGET_MIPS_CS}
${TARGET_MIPS_CS}: ${OBJ_MIPS_CS}
${CC} ${CFLAGS} $(call libname,arch_mips_cs) $(CS_CFLAGS) \
-o arch_mips_cs.${EXT_SO} ${OBJ_MIPS_CS} $(CS_LDFLAGS)

View File

@ -873,16 +873,6 @@ static RList *preludes(RArchSession *as) {
return l; return l;
} }
#if 0
static RList *anal_preludes(RAnal *anal) {
#define KW(d,ds,m,ms) r_list_append (l, r_search_keyword_new((const ut8*)d,ds,(const ut8*)m, ms, NULL))
RList *l = r_list_newf ((RListFree)r_search_keyword_free);
KW ("\x80\x07", 2, "\xf0\xff", 2);
KW ("\x50\x1a\x63\x0f", 4, "\xf0\xff\xff\x0f", 4);
return l;
}
#endif
static int archinfo(RArchSession *as, ut32 q) { static int archinfo(RArchSession *as, ut32 q) {
switch (q) { switch (q) {
case R_ANAL_ARCHINFO_ALIGN: case R_ANAL_ARCHINFO_ALIGN:

View File

@ -5597,6 +5597,7 @@ R_API void r_core_anal_esil(RCore *core, const char *str /* len */, const char *
if (newstack) { if (newstack) {
opflags |= R_ARCH_OP_MASK_DISASM; opflags |= R_ARCH_OP_MASK_DISASM;
} }
opflags |= R_ARCH_OP_MASK_DISASM;
if (!r_anal_op (core->anal, &op, cur, buf + i, iend - i, opflags)) { if (!r_anal_op (core->anal, &op, cur, buf + i, iend - i, opflags)) {
i += minopsize - 1; // XXX dupe in op.size below i += minopsize - 1; // XXX dupe in op.size below
r_anal_op_fini (&op); r_anal_op_fini (&op);
@ -5646,6 +5647,7 @@ R_API void r_core_anal_esil(RCore *core, const char *str /* len */, const char *
goto repeat; goto repeat;
} }
} }
// R2_590 - do this once and before the loop
const char *sn = r_reg_get_name (core->anal->reg, R_REG_NAME_SN); const char *sn = r_reg_get_name (core->anal->reg, R_REG_NAME_SN);
if (!sn) { if (!sn) {
R_LOG_WARN ("No SN reg alias for '%s'", r_config_get (core->config, "asm.arch")); R_LOG_WARN ("No SN reg alias for '%s'", r_config_get (core->config, "asm.arch"));
@ -5673,6 +5675,7 @@ R_API void r_core_anal_esil(RCore *core, const char *str /* len */, const char *
goto repeat; goto repeat;
} }
r_esil_set_pc (ESIL, cur); r_esil_set_pc (ESIL, cur);
// R2_590 - if roregs is set we dont need to set that value everytime
r_reg_setv (core->anal->reg, pcname, cur + op.size); r_reg_setv (core->anal->reg, pcname, cur + op.size);
if (gp_fixed && gp_reg) { if (gp_fixed && gp_reg) {
r_reg_setv (core->anal->reg, gp_reg, gp); r_reg_setv (core->anal->reg, gp_reg, gp);
@ -5722,7 +5725,7 @@ R_API void r_core_anal_esil(RCore *core, const char *str /* len */, const char *
if (cfg_anal_strings) { if (cfg_anal_strings) {
add_string_ref (core, op.addr, dst); add_string_ref (core, op.addr, dst);
} }
} else if ((core->anal->config->bits == 32 && core->anal->cur && arch == R2_ARCH_MIPS)) { } else if ((core->anal->config->bits == 32 && arch == R2_ARCH_MIPS)) {
ut64 dst = ESIL->cur; ut64 dst = ESIL->cur;
RAnalValue *opsrc0 = r_vector_at (&op.srcs, 0); RAnalValue *opsrc0 = r_vector_at (&op.srcs, 0);
RAnalValue *opsrc1 = r_vector_at (&op.srcs, 1); RAnalValue *opsrc1 = r_vector_at (&op.srcs, 1);
@ -5759,6 +5762,13 @@ R_API void r_core_anal_esil(RCore *core, const char *str /* len */, const char *
} }
} }
} }
#if 0
} else {
R_LOG_DEBUG ("add aae string refs for this arch here");
if (cfg_anal_strings) {
add_string_ref (core, op.addr, dst);
}
#endif
} }
break; break;
case R_ANAL_OP_TYPE_LOAD: case R_ANAL_OP_TYPE_LOAD:

View File

@ -3349,6 +3349,9 @@ R_API void r_core_fini(RCore *c) {
r_unref (c->anal->reg->config); r_unref (c->anal->reg->config);
r_unref (c->anal->config); r_unref (c->anal->config);
*/ */
if (c->anal->esil) {
c->anal->esil->anal = NULL;
}
r_anal_free (c->anal); r_anal_free (c->anal);
r_asm_free (c->rasm); r_asm_free (c->rasm);
c->rasm = NULL; c->rasm = NULL;

View File

@ -743,13 +743,13 @@ static RDisasmState *ds_init(RCore *core) {
ds->asm_instr = r_config_get_i (core->config, "asm.instr"); ds->asm_instr = r_config_get_i (core->config, "asm.instr");
ds->show_emu = r_config_get_b (core->config, "asm.emu"); ds->show_emu = r_config_get_b (core->config, "asm.emu");
ds->show_emu_str = r_config_get_b (core->config, "emu.str"); ds->show_emu_str = r_config_get_b (core->config, "emu.str");
ds->show_emu_stroff = r_config_get_i (core->config, "emu.str.off"); ds->show_emu_stroff = r_config_get_b (core->config, "emu.str.off");
ds->show_emu_strinv = r_config_get_i (core->config, "emu.str.inv"); ds->show_emu_strinv = r_config_get_b (core->config, "emu.str.inv");
ds->show_emu_strflag = r_config_get_i (core->config, "emu.str.flag"); ds->show_emu_strflag = r_config_get_b (core->config, "emu.str.flag");
ds->show_emu_strlea = r_config_get_i (core->config, "emu.str.lea"); ds->show_emu_strlea = r_config_get_b (core->config, "emu.str.lea");
ds->show_emu_write = r_config_get_i (core->config, "emu.write"); ds->show_emu_write = r_config_get_b (core->config, "emu.write");
ds->show_emu_ssa = r_config_get_i (core->config, "emu.ssa"); ds->show_emu_ssa = r_config_get_b (core->config, "emu.ssa");
ds->show_emu_stack = r_config_get_i (core->config, "emu.stack"); ds->show_emu_stack = r_config_get_b (core->config, "emu.stack");
ds->stackFd = -1; ds->stackFd = -1;
if (ds->show_emu_stack) { if (ds->show_emu_stack) {
// TODO: initialize fake stack in here // TODO: initialize fake stack in here
@ -782,8 +782,8 @@ static RDisasmState *ds_init(RCore *core) {
ds->show_reloff = r_config_get_i (core->config, "asm.offset.relative"); ds->show_reloff = r_config_get_i (core->config, "asm.offset.relative");
ds->show_reloff_flags = r_config_get_i (core->config, "asm.offset.flags"); ds->show_reloff_flags = r_config_get_i (core->config, "asm.offset.flags");
ds->show_lines_fcn = ds->show_lines ? r_config_get_i (core->config, "asm.lines.fcn") : false; ds->show_lines_fcn = ds->show_lines ? r_config_get_i (core->config, "asm.lines.fcn") : false;
ds->show_comments = r_config_get_i (core->config, "asm.comments"); ds->show_comments = r_config_get_b (core->config, "asm.comments");
ds->show_usercomments = r_config_get_i (core->config, "asm.usercomments"); ds->show_usercomments = r_config_get_b (core->config, "asm.usercomments");
ds->asm_hint_jmp = r_config_get_i (core->config, "asm.hint.jmp"); ds->asm_hint_jmp = r_config_get_i (core->config, "asm.hint.jmp");
ds->asm_hint_call = r_config_get_i (core->config, "asm.hint.call"); ds->asm_hint_call = r_config_get_i (core->config, "asm.hint.call");
ds->asm_hint_call_indirect = r_config_get_i (core->config, "asm.hint.call.indirect"); ds->asm_hint_call_indirect = r_config_get_i (core->config, "asm.hint.call.indirect");
@ -4659,8 +4659,8 @@ static void ssa_set(REsil *esil, const char *reg) {
#define R_DISASM_MAX_STR 512 #define R_DISASM_MAX_STR 512
static bool myregread(REsil *esil, const char *name, ut64 *res, int *size) { static bool myregread(REsil *esil, const char *name, ut64 *res, int *size) {
RDisasmState *ds = esil->user; RDisasmState *ds = esil->user;
if (ds && ds->show_emu_ssa) { if (ds != NULL && ds->show_emu_ssa && name) {
if (!isdigit ((unsigned char)*name)) { if (!isdigit ((ut8)*name)) {
char *r = ssa_get (esil, name); char *r = ssa_get (esil, name);
ds_comment_esil (ds, true, false, "<%s", r); ds_comment_esil (ds, true, false, "<%s", r);
free (r); free (r);

View File

@ -1603,7 +1603,6 @@ extern RAnalPlugin r_anal_plugin_null;
extern RAnalPlugin r_anal_plugin_8051; extern RAnalPlugin r_anal_plugin_8051;
extern RAnalPlugin r_anal_plugin_arm_cs; extern RAnalPlugin r_anal_plugin_arm_cs;
extern RAnalPlugin r_anal_plugin_dalvik; extern RAnalPlugin r_anal_plugin_dalvik;
extern RAnalPlugin r_anal_plugin_mips_cs;
extern RAnalPlugin r_anal_plugin_x86; extern RAnalPlugin r_anal_plugin_x86;
extern RAnalPlugin r_anal_plugin_x86_cs; extern RAnalPlugin r_anal_plugin_x86_cs;

View File

@ -342,6 +342,7 @@ extern RArchPlugin r_arch_plugin_i8080;
extern RArchPlugin r_arch_plugin_java; extern RArchPlugin r_arch_plugin_java;
extern RArchPlugin r_arch_plugin_arm_gnu; extern RArchPlugin r_arch_plugin_arm_gnu;
extern RArchPlugin r_arch_plugin_gb; extern RArchPlugin r_arch_plugin_gb;
extern RArchPlugin r_arch_plugin_mips_cs;
extern RArchPlugin r_arch_plugin_avr; extern RArchPlugin r_arch_plugin_avr;
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -9,6 +9,7 @@ R2DEPS+=r_asm
R2DEPS+=r_crypto R2DEPS+=r_crypto
R2DEPS+=r_arch R2DEPS+=r_arch
R2DEPS+=r_esil R2DEPS+=r_esil
R2DEPS+=r_search
R2DEPS+=r_syscall R2DEPS+=r_syscall
R2DEPS+=r_flag R2DEPS+=r_flag
CFLAGS+=-DR2_PLUGIN_INCORE CFLAGS+=-DR2_PLUGIN_INCORE

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2012-2022 - pancake */ /* radare - LGPL - Copyright 2012-2023 - pancake */
#include <r_main.h> #include <r_main.h>

View File

@ -893,6 +893,10 @@ R_API int r_main_rasm2(int argc, const char *argv[]) {
goto beach; goto beach;
} }
if (cpu) {
r_asm_set_cpu (as->a, cpu);
// not necessary --- r_arch_config_set_cpu (as->a->config, cpu);
}
if (arch) { if (arch) {
if (!r_asm_use (as->a, arch)) { if (!r_asm_use (as->a, arch)) {
R_LOG_ERROR ("Unknown asm plugin '%s'", arch); R_LOG_ERROR ("Unknown asm plugin '%s'", arch);
@ -911,11 +915,8 @@ R_API int r_main_rasm2(int argc, const char *argv[]) {
ret = 0; ret = 0;
goto beach; goto beach;
} }
if (cpu) { r_asm_set_bits (as->a, R_STR_ISNOTEMPTY (env_bits)? atoi (env_bits): bits);
r_asm_set_cpu (as->a, cpu); r_anal_set_bits (as->anal, R_STR_ISNOTEMPTY (env_bits)? atoi (env_bits): bits);
}
r_asm_set_bits (as->a, (env_bits && *env_bits)? atoi (env_bits): bits);
r_anal_set_bits (as->anal, (env_bits && *env_bits)? atoi (env_bits): bits);
as->a->syscall = r_syscall_new (); as->a->syscall = r_syscall_new ();
r_syscall_setup (as->a->syscall, arch, bits, cpu, kernel); r_syscall_setup (as->a->syscall, arch, bits, cpu, kernel);
{ {

View File

@ -126,7 +126,7 @@ if not no_user_plugins
# missing parse_plugins += [ 'sparc_pseudo' ] # missing parse_plugins += [ 'sparc_pseudo' ]
endif endif
if user_plugins.contains('mips') if user_plugins.contains('mips')
anal_plugins += [ 'mips_cs' ] arch_plugins += [ 'mips_cs' ]
if use_gpl if use_gpl
arch_plugins += [ 'mips_gnu' ] arch_plugins += [ 'mips_gnu' ]
endif endif
@ -157,7 +157,6 @@ anal_plugins += [
'8051', '8051',
'arm_cs', 'arm_cs',
'dalvik', 'dalvik',
'mips_cs',
'x86_cs', 'x86_cs',
] ]
@ -167,6 +166,7 @@ arch_plugins += [
'chip8', 'chip8',
'wasm', 'wasm',
'i8080', 'i8080',
'mips_cs',
'6502_cs', '6502_cs',
'loongarch_gnu', 'loongarch_gnu',
's390_cs', 's390_cs',