Remove asm.6502_cs and merge disasm into the anal (#19632)

This commit is contained in:
Sylvain Pelissier 2022-01-26 16:37:19 +01:00 committed by GitHub
parent 54b1fcd23c
commit 61b2667d77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 31 additions and 113 deletions

View File

@ -59,7 +59,6 @@ anal.pyc
esil.dummy
asm.8051
asm.6502
asm.6502_cs
asm.amd29k
asm.arc
asm.arm_cs

View File

@ -54,7 +54,6 @@ anal.pyc
esil.dummy
asm.8051
asm.6502
asm.6502_cs
asm.amd29k
asm.arc
asm.arm_cs

View File

@ -32,7 +32,6 @@ anal.pyc
esil.dummy
asm.8051
asm.6502
asm.6502_cs
asm.amd29k
asm.arc
asm.arm_cs

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2018 - pancake */
/* radare - LGPL - Copyright 2018-2022 - pancake, Sylvain Pelissier */
#include <string.h>
#include <r_types.h>
@ -7,7 +7,7 @@
#include <r_anal.h>
#include <capstone.h>
#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;
}
}

View File

@ -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'),

View File

@ -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

View File

@ -1,90 +0,0 @@
/* radare2 - LGPL - Copyright 2018-2021 - pancake */
#include <r_asm.h>
#include <r_lib.h>
#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

View File

@ -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;

8
test/db/asm/6502.cs Normal file
View File

@ -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