From 61b2667d7703be0bd442f463fad3f3b92bda4fe9 Mon Sep 17 00:00:00 2001 From: Sylvain Pelissier Date: Wed, 26 Jan 2022 16:37:19 +0100 Subject: [PATCH] Remove asm.6502_cs and merge disasm into the anal (#19632) --- dist/plugins-cfg/plugins.def.cfg | 1 - dist/plugins-cfg/plugins.mingw.cfg | 1 - dist/plugins-cfg/plugins.termux.cfg | 1 - libr/anal/p/anal_6502_cs.c | 26 ++++++++- libr/asm/meson.build | 1 - libr/asm/p/6502_cs.mk | 15 ----- libr/asm/p/asm_6502_cs.c | 90 ----------------------------- libr/include/r_asm.h | 1 - test/db/asm/6502.cs | 8 +++ 9 files changed, 31 insertions(+), 113 deletions(-) delete mode 100644 libr/asm/p/6502_cs.mk delete mode 100644 libr/asm/p/asm_6502_cs.c create mode 100644 test/db/asm/6502.cs diff --git a/dist/plugins-cfg/plugins.def.cfg b/dist/plugins-cfg/plugins.def.cfg index 1bee0ac13c..407e2b0c38 100644 --- a/dist/plugins-cfg/plugins.def.cfg +++ b/dist/plugins-cfg/plugins.def.cfg @@ -59,7 +59,6 @@ anal.pyc esil.dummy asm.8051 asm.6502 -asm.6502_cs asm.amd29k asm.arc asm.arm_cs diff --git a/dist/plugins-cfg/plugins.mingw.cfg b/dist/plugins-cfg/plugins.mingw.cfg index f8b3c9b817..f571aa57a4 100644 --- a/dist/plugins-cfg/plugins.mingw.cfg +++ b/dist/plugins-cfg/plugins.mingw.cfg @@ -54,7 +54,6 @@ anal.pyc esil.dummy asm.8051 asm.6502 -asm.6502_cs asm.amd29k asm.arc asm.arm_cs diff --git a/dist/plugins-cfg/plugins.termux.cfg b/dist/plugins-cfg/plugins.termux.cfg index 5d10e39b11..476d2bb6c6 100644 --- a/dist/plugins-cfg/plugins.termux.cfg +++ b/dist/plugins-cfg/plugins.termux.cfg @@ -32,7 +32,6 @@ anal.pyc esil.dummy asm.8051 asm.6502 -asm.6502_cs asm.amd29k asm.arc asm.arm_cs diff --git a/libr/anal/p/anal_6502_cs.c b/libr/anal/p/anal_6502_cs.c index e3bead00af..05239f6a27 100644 --- a/libr/anal/p/anal_6502_cs.c +++ b/libr/anal/p/anal_6502_cs.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2018 - pancake */ +/* radare - LGPL - Copyright 2018-2022 - pancake, Sylvain Pelissier */ #include #include @@ -7,7 +7,7 @@ #include #include -#if CS_API_MAJOR >= 4 && CS_API_MINOR >= 1 +#if CS_API_MAJOR >= 5 || (CS_API_MAJOR >= 4 && CS_API_MINOR >= 1) #define CAPSTONE_HAS_MOS65XX 1 #else #define CAPSTONE_HAS_MOS65XX 0 @@ -56,8 +56,15 @@ static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len, RAn n = cs_disasm (handle, (const ut8*)buf, len, addr, 1, &insn); #endif if (n < 1) { + if (mask & R_ANAL_OP_MASK_DISASM) { + op->mnemonic = strdup ("invalid"); + } op->type = R_ANAL_OP_TYPE_ILL; } else { + if (mask & R_ANAL_OP_MASK_DISASM) { + char *str = r_str_newf ("%s%s%s", insn->mnemonic, insn->op_str[0]? " ": "", insn->op_str); + op->mnemonic = str; + } op->nopcode = 1; op->size = insn->size; op->id = insn->id; @@ -91,13 +98,16 @@ static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len, RAn break; case MOS65XX_INS_BVC: case MOS65XX_INS_BVS: + op->type = R_ANAL_OP_TYPE_RCJMP; + break; case MOS65XX_INS_CLC: case MOS65XX_INS_CLD: case MOS65XX_INS_CLI: case MOS65XX_INS_CLV: + op->type = R_ANAL_OP_TYPE_MOV; + break; case MOS65XX_INS_CPX: case MOS65XX_INS_CPY: - break; case MOS65XX_INS_CMP: op->type = R_ANAL_OP_TYPE_CMP; break; @@ -136,6 +146,7 @@ static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len, RAn case MOS65XX_INS_PLA: case MOS65XX_INS_PHP: case MOS65XX_INS_PLP: + op->type = R_ANAL_OP_TYPE_PUSH; break; case MOS65XX_INS_ROL: op->type = R_ANAL_OP_TYPE_SHR; @@ -145,19 +156,28 @@ static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len, RAn break; case MOS65XX_INS_RTI: case MOS65XX_INS_RTS: + op->type = R_ANAL_OP_TYPE_RET; + break; case MOS65XX_INS_SBC: + op->type = R_ANAL_OP_TYPE_SUB; + break; case MOS65XX_INS_SEC: case MOS65XX_INS_SED: case MOS65XX_INS_SEI: + op->type = R_ANAL_OP_TYPE_MOV; + break; case MOS65XX_INS_STA: case MOS65XX_INS_STX: case MOS65XX_INS_STY: + op->type = R_ANAL_OP_TYPE_STORE; + break; case MOS65XX_INS_TAX: case MOS65XX_INS_TAY: case MOS65XX_INS_TSX: case MOS65XX_INS_TXA: case MOS65XX_INS_TXS: case MOS65XX_INS_TYA: + op->type = R_ANAL_OP_TYPE_MOV; break; } } diff --git a/libr/asm/meson.build b/libr/asm/meson.build index 9e3a8ccd03..71dce871de 100644 --- a/libr/asm/meson.build +++ b/libr/asm/meson.build @@ -7,7 +7,6 @@ r_asm_sources = [ 'aplugs.c', 'binutils_as.c', join_paths('p','asm_6502.c'), - join_paths('p','asm_6502_cs.c'), join_paths('p','asm_8051.c'), join_paths('p','asm_amd29k.c'), # join_paths('p','asm_arc.c'), diff --git a/libr/asm/p/6502_cs.mk b/libr/asm/p/6502_cs.mk deleted file mode 100644 index 633591ab27..0000000000 --- a/libr/asm/p/6502_cs.mk +++ /dev/null @@ -1,15 +0,0 @@ -OBJ_6502_CS=asm_6502_cs.o - -include p/capstone.mk - -STATIC_OBJ+=${OBJ_6502_CS} -SHARED_OBJ+=${SHARED_6502_CS} -TARGET_6502_CS=asm_6502_cs.${EXT_SO} - -ifeq ($(WITHPIC),1) -ALL_TARGETS+=${TARGET_6502_CS} - -${TARGET_6502_CS}: ${OBJ_6502_CS} - ${CC} $(call libname,asm_6502_cs) ${LDFLAGS} ${CFLAGS} $(CS_LDFLAGS) \ - -o ${TARGET_6502_CS} ${OBJ_6502_CS} ${SHARED2_6502_CS} -endif diff --git a/libr/asm/p/asm_6502_cs.c b/libr/asm/p/asm_6502_cs.c deleted file mode 100644 index 1720bb6098..0000000000 --- a/libr/asm/p/asm_6502_cs.c +++ /dev/null @@ -1,90 +0,0 @@ -/* radare2 - LGPL - Copyright 2018-2021 - pancake */ - -#include -#include -#include "cs_version.h" - -#if CS_API_MAJOR >= 5 -#define CAPSTONE_HAS_MOS65XX 1 -#else -#define CAPSTONE_HAS_MOS65XX 0 -#endif - -#if CAPSTONE_HAS_MOS65XX - -static csh cd = 0; - -static bool the_end(void *p) { - if (cd) { - cs_close (&cd); - cd = 0; - } - return 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 = CS_MODE_LITTLE_ENDIAN; - if (cd && mode != omode) { - cs_close (&cd); - cd = 0; - } - op->size = 0; - omode = mode; - if (cd == 0) { - ret = cs_open (CS_ARCH_MOS65XX, mode, &cd); - if (ret) { - return 0; - } - cs_option (cd, CS_OPT_DETAIL, CS_OPT_OFF); - } - n = cs_disasm (cd, (const ut8*)buf, len, off, 1, &insn); - if (n>0) { - if (insn->size > 0) { - op->size = insn->size; - char *buf_asm = r_str_newf ("%s%s%s", - insn->mnemonic, insn->op_str[0]?" ": "", - insn->op_str); - char *ptrstr = strstr (buf_asm, "ptr "); - if (ptrstr) { - memmove (ptrstr, ptrstr + 4, strlen (ptrstr + 4) + 1); - } - r_asm_op_set_asm (op, buf_asm); - free (buf_asm); - } - cs_free (insn, n); - } - return op->size; -} - -RAsmPlugin r_asm_plugin_6502_cs = { - .name = "6502.cs", - .desc = "Capstone "CAPSTONE_VERSION_STRING" mos65xx CPU disassembler", - .license = "BSD", - .arch = "6502", - .bits = 8 | 32, - .endian = R_SYS_ENDIAN_LITTLE, - .fini = the_end, - .disassemble = &disassemble, -}; - -#else -RAsmPlugin r_asm_plugin_6502_cs = { - .name = "6502.cs", - .desc = "Capstone mos65xx CPU disassembler (not supported)", - .license = "BSD", - .arch = "6502", - .bits = 8|32, -}; -#endif - -#ifndef R2_PLUGIN_INCORE -R_API RLibStruct radare_plugin = { - .type = R_LIB_TYPE_ASM, - .data = &r_asm_plugin_6502_cs, - .version = R2_VERSION -}; -#endif diff --git a/libr/include/r_asm.h b/libr/include/r_asm.h index 00a9aa7e31..3e5b9e1837 100644 --- a/libr/include/r_asm.h +++ b/libr/include/r_asm.h @@ -203,7 +203,6 @@ R_API ut8 *r_asm_op_get_buf(RAsmOp *op); /* plugin pointers */ extern RAsmPlugin r_asm_plugin_6502; -extern RAsmPlugin r_asm_plugin_6502_cs; extern RAsmPlugin r_asm_plugin_8051; extern RAsmPlugin r_asm_plugin_amd29k; extern RAsmPlugin r_asm_plugin_arc; diff --git a/test/db/asm/6502.cs b/test/db/asm/6502.cs new file mode 100644 index 0000000000..06bd405556 --- /dev/null +++ b/test/db/asm/6502.cs @@ -0,0 +1,8 @@ +d "brk 0x03" 0003 +d "bpl 0x0011" 100f +d "ora 0x0100, x" 1d0001 +d "and 0x01ef" 2def01 +d "adc 0x02" 6502 +d "sta 0x7000" 8d0070 +d "ldy 0x10, x" b410 +d "cpy 0x88" c488