Migrate the arm.gnu plugin ##arch

This commit is contained in:
pancake 2023-06-14 19:52:25 +02:00 committed by GitHub
parent 264682931b
commit af8a1a365d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 107 additions and 87 deletions

View File

@ -1,7 +1,7 @@
STATIC="
anal.8051
anal.arm_cs
anal.arm_gnu
arch.arm_gnu
anal.avr
anal.dalvik
arch.gb

View File

@ -1,10 +1,10 @@
STATIC="
anal.8051
anal.arm_cs
anal.arm_gnu
anal.avr
anal.dalvik
arch.gb
arch.arm_gnu
arch.i8080
arch.java
anal.mips_cs

View File

@ -3,7 +3,7 @@ arch.alpha
arch.xtensa
arch.arc
anal.arm_cs
anal.arm_gnu
arch.arm_gnu
anal.avr
arch.bf
arch.chip8

View File

@ -1,6 +1,6 @@
STATIC="anal.8051
arch.arc
anal.arm_gnu
arch.arm_gnu
arch.arm_v35
anal.avr
arch.bf

View File

@ -3,7 +3,7 @@
STATIC="anal.8051
arch.arc
anal.arm_cs
anal.arm_gnu
arch.arm_gnu
arch.bf
arch.chip8
arch.cris

View File

@ -1,6 +1,6 @@
STATIC="anal.8051
anal.arm_cs
anal.arm_gnu
arch.arm_gnu
anal.avr
arch.bf
anal.dalvik

View File

@ -43,7 +43,6 @@ r_anal_sources = [
'p/anal_8051.c',
'p/anal_arm_cs.c',
'p/anal_arm_gnu.c',
'p/anal_avr.c',
'p/anal_dalvik.c',
'../arch/p/ebc/plugin.c',

View File

@ -12,7 +12,7 @@ all: ${ALL_TARGETS}
ALL_TARGETS=
# TODO: rename to enabled plugins
ANAL_PLUGINS=null.mk arc.mk ppc_cs.mk arm_gnu.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
# 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)
clean:

View File

@ -6,7 +6,7 @@
#include <capstone/capstone.h>
#include <capstone/arm.h>
#include <r_util/r_assert.h>
#include "./anal_arm_hacks.inc.c"
#include "../../arch/p/arm/anal_arm_hacks.inc.c"
#include "./anal_asm_arm_hacks.inc.c"
typedef char RStringShort[32];

View File

@ -1,22 +0,0 @@
N=anal_arm_gnu
OBJ_ARM_GNU=anal_arm_gnu.o
OBJ_ARM_GNU+=../../arch/p/arm/winedbg/be_arm.o
#arm32
OBJ_ARM_GNU+=../../arch/p/arm/gnu/arm-dis.o
OBJ_ARM_GNU+=../../arch/p/arm/gnu/floatformat.o
#arm64
OBJ_ARM_GNU+=../../arch/p/arm/aarch64/aarch64-dis.o
OBJ_ARM_GNU+=../../arch/p/arm/aarch64/aarch64-dis-2.o
OBJ_ARM_GNU+=../../arch/p/arm/aarch64/aarch64-opc.o
OBJ_ARM_GNU+=../../arch/p/arm/aarch64/aarch64-opc-2.o
STATIC_OBJ+=${OBJ_ARM_GNU}
TARGET_ARM=$(N).${EXT_SO}
ALL_TARGETS+=${TARGET_ARM}
CFLAGS +=-I../asm/arch/include
${TARGET_ARM}: ${OBJ_ARM_GNU}
${CC} $(call libname,$(N)) ${LDFLAGS} ${CFLAGS} \
-o $(TARGET_ARM) $(OBJ_ARM_GNU)

View File

@ -69,6 +69,15 @@ r_arch_sources = [
'p/arm/plugin.c',
'p/arm/armass.c',
'p/arm/armass64.c',
'p/arm/plugin_gnu.c',
'p/arm/winedbg/be_arm.c',
'p/arm/gnu/arm-dis.c',
'p/arm/aarch64/aarch64-dis.c',
'p/arm/aarch64/aarch64-dis-2.c',
'p/arm/aarch64/aarch64-opc.c',
'p/arm/aarch64/aarch64-opc-2.c',
'p/any_as/plugin.c',
'p/any_vasm/plugin.c',
'p/i4004/plugin.c',

View File

@ -1,17 +1,20 @@
/* radare - LGPL - Copyright 2007-2022 - pancake */
/* radare - LGPL - Copyright 2007-2023 - pancake */
#include <r_lib.h>
#include <r_asm.h>
#include <r_arch.h>
#include <r_anal.h>
// R2R db/anal/arm.gnu_16 db/tools/rasm2 db/anal/arm
/* DEPRECATE ?? */
#include "wine-arm.h"
#include "../arch/p/arm/asm-arm.h"
#include "../arch/p/arm/winedbg/be_arm.h"
#include "./anal_arm_hacks.inc.c"
#include "../arch/p/arm/anal_arm_hacks.inc.c"
#include "disas-asm.h"
#include "../../arch/p/arm/gnu/opcode-arm.h"
#include "../arch/p/arm/gnu/opcode-arm.h"
// R2_590 - eliminate those globals!
static R_TH_LOCAL char *oldcpu = NULL;
static R_TH_LOCAL int oldcpucode = 0;
@ -35,7 +38,7 @@ static ut32 disarm_branch_offset(ut32 pc, ut32 insoff) {
#define API static
static int op_thumb(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len, ut32 mask) {
static int op_thumb(RArchSession *as, RAnalOp *op, ut64 addr, const ut8 *data, int len, ut32 mask) {
int op_code;
ut16 *_ins = (ut16 *) data;
ut16 ins = *_ins;
@ -51,7 +54,7 @@ static int op_thumb(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int le
op->jump = arminsn->jmp;
op->fail = arminsn->fail;
if (mask & R_ARCH_OP_MASK_DISASM) {
const char *cpu = r_str_get_fail (anal->config->cpu, "");
const char *cpu = r_str_get_fail (as->config->cpu, "");
if (!strcmp (cpu, "wd")) {
const char *asmstr = winedbg_arm_insn_asm (arminsn);
if (asmstr) {
@ -218,8 +221,8 @@ static const struct {
{ "iWMMXt2", bfd_mach_arm_iWMMXt2 },
};
static int disassemble(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len) {
const int bits = a->config->bits;
static int disassemble(RArchSession *as, RAnalOp *op, ut64 addr, const ut8 *buf, int len) {
const int bits = as->config->bits;
ut8 bytes[4] = {0};
struct disassemble_info obj;
int opsize;
@ -239,7 +242,7 @@ static int disassemble(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len
/* select cpu */
// XXX oldcpu leaks
char *cpu = a->config->cpu;
char *cpu = as->config->cpu;
if (oldcpu != cpu) {
int cpucode = 0;
if (cpu) {
@ -268,7 +271,7 @@ static int disassemble(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len
obj.symbol_at_address_func = &symbol_at_address;
obj.memory_error_func = &memory_error_func;
obj.print_address_func = &generic_print_address_func;
obj.endian = !R_ARCH_CONFIG_IS_BIG_ENDIAN (a->config);
obj.endian = !R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config);
obj.fprintf_func = &generic_fprintf_func;
obj.stream = insn_buffer;
obj.bytes_per_chunk = obj.bytes_per_line = (bits / 8);
@ -303,7 +306,7 @@ static int disassemble(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len
}
static int arm_op32(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len, ut32 mask) {
static int arm_op32(RArchSession *as, RAnalOp *op, ut64 addr, const ut8 *data, int len, ut32 mask) {
const ut8 *b = (ut8 *) data;
ut8 ndata[4] = {0};
ut32 branch_dst_addr, i = 0;
@ -320,7 +323,7 @@ static int arm_op32(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int le
op->addr = addr;
op->type = R_ANAL_OP_TYPE_UNK;
if (R_ARCH_CONFIG_IS_BIG_ENDIAN (anal->config)) {
if (R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config)) {
b = data = ndata;
ut8 tmp = data[3];
ndata[0] = data[3];
@ -328,13 +331,13 @@ static int arm_op32(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int le
ndata[2] = data[1];
ndata[3] = tmp;
}
if (anal->config->bits == 16) {
if (as->config->bits == 16) {
arm_free (arminsn);
return op_thumb (anal, op, addr, data, len, mask);
return op_thumb (as, op, addr, data, len, mask);
}
op->size = arm_disasm_one_insn (arminsn);
if (mask & R_ARCH_OP_MASK_DISASM) {
const char *cpu = r_str_get_fail (anal->config->cpu, "");
const char *cpu = r_str_get_fail (as->config->cpu, "");
if (!strcmp (cpu, "wd")) {
const char *asmstr = winedbg_arm_insn_asm (arminsn);
if (asmstr) {
@ -433,7 +436,7 @@ static int arm_op32(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int le
} else {
// ut32 oaddr = addr+8+b[0];
// XXX TODO ret = radare_read_at(oaddr, (ut8*)&ptr, 4);
if (anal->config->bits == 32) {
if (as->config->bits == 32) {
b = (ut8 *) &ptr;
op->ptr = b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
// XXX data_xrefs_add(oaddr, op->ptr, 1);
@ -496,13 +499,17 @@ static ut64 getaddr(ut64 addr, const ut8 *d) {
return addr + (4 * (d[0] + (d[1] << 8) + (d[2] << 16)));
}
static int arm_op64(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *d, int len) {
static int arm_op64(RArchSession *as, RAnalOp *op, ut64 addr, const ut8 *d, int len) {
if (d[3] == 0) {
return -1; // invalid
return -1; // invalid
}
int haa = hackyArmAnal (anal, op, d, len);
if (haa > 0) {
return haa;
RAnal *anal = R_UNWRAP4 (as, arch, esil, anal);
if (anal) {
// XXX always nul because hackyarm must be moved to arch
int haa = hackyArmAnal (anal, op, d, len);
if (haa > 0) {
return haa;
}
}
op->size = 4;
op->type = R_ANAL_OP_TYPE_NULL;
@ -545,22 +552,27 @@ static int arm_op64(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *d, int len)
return op->size;
}
static int arm_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len, RAnalOpMask mask) {
static bool arm_op(RArchSession *as, RAnalOp *op, RArchDecodeMask mask) {
// }ut64 addr, const ut8 *data, int len, RAnalOpMask mask)
// static int arm_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len, RAnalOpMask mask)
const ut64 addr = op->addr;
const ut8 *data = op->bytes;
const int len = op->size;
if (mask & R_ARCH_OP_MASK_DISASM) {
const char *cpu = r_str_get_fail (anal->config->cpu, "");
const char *cpu = r_str_get_fail (as->config->cpu, "");
if (strcmp (cpu, "wd")) {
disassemble (anal, op, addr, data, len);
disassemble (as, op, addr, data, len);
}
}
if (anal->config->bits == 64) {
return arm_op64 (anal, op, addr, data, len);
if (as->config->bits == 64) {
return arm_op64 (as, op, addr, data, len);
}
return arm_op32 (anal, op, addr, data, len, mask);
return arm_op32 (as, op, addr, data, len, mask);
}
static bool set_reg_profile(RAnal *anal) {
static char *set_reg_profile(RArchSession *as) {
// TODO: support 64bit profile
const char *p32 =
const char p32[] =
"=PC r15\n"
"=SP r13\n"
"=BP r14\n" // XXX
@ -590,30 +602,28 @@ static bool set_reg_profile(RAnal *anal) {
"gpr r16 .32 64 0\n"
"gpr r17 .32 68 0\n"
"gpr cpsr .32 72 0\n";
return r_reg_set_profile_string (anal->reg, p32);
return strdup (p32);
}
static int archinfo(RAnal *anal, int q) {
static int archinfo(RArchSession *as, ut32 q) {
if (q == R_ANAL_ARCHINFO_ALIGN) {
if (anal && anal->config->bits == 16) {
return 2;
}
return 4;
return (as && as->config->bits == 16)? 2: 4;
}
if (q == R_ANAL_ARCHINFO_MAX_OP_SIZE) {
return 4;
}
if (q == R_ANAL_ARCHINFO_MIN_OP_SIZE) {
if (anal && anal->config->bits == 16) {
return 2;
}
return 4;
return (as && as->config->bits == 16)? 2: 4;
}
return 4; // XXX
}
RAnalPlugin r_anal_plugin_arm_gnu = {
.name = "arm.gnu",
RArchPlugin r_arch_plugin_arm_gnu = {
.meta = {
.name = "arm.gnu",
.license = "LGPL3",
.desc = "ARM code analysis plugin (asm.cpu=wd for winedbg disassembler)",
},
.arch = "arm",
.cpus = "v2,v2a,v3M,v4,v5,v5t,v5te,v5j,XScale,ep9312,iWMMXt,iWMMXt2,wd",
#if 0
@ -624,18 +634,16 @@ RAnalPlugin r_anal_plugin_arm_gnu = {
"mulops,crc,dpvfp,v6m"
#endif
.endian = R_SYS_ENDIAN_LITTLE | R_SYS_ENDIAN_BIG,
.license = "LGPL3",
.bits = 16 | 32 | 64,
.desc = "ARM code analysis plugin (asm.cpu=wd for winedbg disassembler)",
.archinfo = archinfo,
.op = &arm_op,
.set_reg_profile = set_reg_profile,
.bits = R_SYS_BITS_PACK3 (16, 32, 64),
.info = archinfo,
.decode = &arm_op,
.regs = set_reg_profile,
};
#ifndef R2_PLUGIN_INCORE
R_API RLibStruct radare_plugin = {
.type = R_LIB_TYPE_ANAL,
.data = &r_anal_plugin_arm_gnu,
.type = R_LIB_TYPE_ARCH,
.data = &r_arch_plugin_arm_gnu,
.version = R2_VERSION
};
#endif

20
libr/arch/p/arm_gnu.mk Normal file
View File

@ -0,0 +1,20 @@
N=arch_arm_gnu
OBJ_ARM_GNU=p/arm/plugin_gnu.o
OBJ_ARM_GNU+=p/arm/gnu/arm-dis.o
# OBJ_ARM_GNU+=p/arm/gnu/floatformat.o
OBJ_ARM_GNU+=p/arm/winedbg/be_arm.o
#arm64
OBJ_ARM_GNU+=p/arm/aarch64/aarch64-dis.o
OBJ_ARM_GNU+=p/arm/aarch64/aarch64-dis-2.o
OBJ_ARM_GNU+=p/arm/aarch64/aarch64-opc.o
OBJ_ARM_GNU+=p/arm/aarch64/aarch64-opc-2.o
STATIC_OBJ+=${OBJ_ARM_GNU}
TARGET_ARM=$(N).${EXT_SO}
ALL_TARGETS+=${TARGET_ARM}
${TARGET_ARM}: ${OBJ_ARM_GNU}
${CC} $(call libname,$(N)) ${LDFLAGS} ${CFLAGS} \
-o $(TARGET_ARM) $(OBJ_ARM_GNU)

View File

@ -1601,7 +1601,6 @@ R_API bool r_anal_tid_select(RAnal *anal, int tid);
extern RAnalPlugin r_anal_plugin_null;
extern RAnalPlugin r_anal_plugin_8051;
extern RAnalPlugin r_anal_plugin_arm_cs;
extern RAnalPlugin r_anal_plugin_arm_gnu;
extern RAnalPlugin r_anal_plugin_avr;
extern RAnalPlugin r_anal_plugin_dalvik;
extern RAnalPlugin r_anal_plugin_mips_cs;

View File

@ -340,6 +340,7 @@ extern RArchPlugin r_arch_plugin_tms320;
extern RArchPlugin r_arch_plugin_ppc_cs;
extern RArchPlugin r_arch_plugin_i8080;
extern RArchPlugin r_arch_plugin_java;
extern RArchPlugin r_arch_plugin_arm_gnu;
extern RArchPlugin r_arch_plugin_gb;
#ifdef __cplusplus

View File

@ -181,11 +181,9 @@ arch_plugins += [
if no_user_plugins
if use_gpl
anal_plugins += [
'arm_gnu',
]
arch_plugins += [
'loongarch_gnu',
'arm_gnu',
'z80',
'pdp11',
'ppc_gnu',

View File

@ -506,24 +506,32 @@ RUN
NAME=rasm2 -LL
FILE=-
REQUIRE=unix
CMDS=!rasm2 -LL | grep -c arm.gnu
CMDS=<<EOF
!rasm2 -LL | grep -c arm
!rasm2 -L | grep -c arm.gnu
EOF
EXPECT=<<EOF
1
1
EOF
RUN
NAME=rasm2 -LL internal grep
FILE=-
CMDS=!!rasm2 -LL~arm.gnu?
CMDS=<<EOF
!!rasm2 -LL~arm?
!!rasm2 -L~arm.gnu?
EOF
EXPECT=<<EOF
1
1
EOF
RUN
NAME=rasm2 -qLL
FILE=-
REQUIRE=unix
CMDS=!rasm2 -qLL | grep -c arm.gnu
CMDS=!rasm2 -qLL | grep -c arm
EXPECT=<<EOF
1
EOF