Honor endianness without messing with capstone initialization in ppc.cs ##arch

This commit is contained in:
pancake 2023-05-16 13:02:26 +02:00 committed by pancake
parent 41fe21e98c
commit c1ceebd878
7 changed files with 109 additions and 87 deletions

View File

@ -28,8 +28,8 @@ anal.i8080
anal.java
anal.m68k
anal.mips_cs
anal.ppc
arch.ppc_cs
arch.ppc_gnu
anal.sparc
arch.tms320
anal.x86_cs

View File

@ -9,7 +9,7 @@ anal.i8080
anal.java
anal.mips_cs
anal.null
anal.ppc_cs
arch.ppc_cs
anal.x86_cs
arch.6502
arch.6502_cs

View File

@ -9,7 +9,6 @@ anal.i8080
anal.java
anal.mips_cs
anal.null
anal.ppc_cs
anal.x86_cs
arch.6502
arch.6502_cs

View File

@ -1,88 +1,89 @@
STATIC="
arch.arm
arch.x86_nz
anal.avr
anal.mips_cs
anal.x86_cs
anal.arm_cs
anal.dalvik
arch.null
arch.i4004
arch.wasm
esil.dummy
fs.posix
bin.any
bin.elf
bin.elf64
bin.dex
bin.pe
bin.mz
bin.pe64
bin.mach0
bin.mach064
bin.wasm
bin_xtr.xtr_fatmach0
bin_xtr.xtr_sep64
core.a2f
bp.arm
bp.x86
bp.mips
cmd.dummy
egg.exec
egg.xor
io.malloc
io.sparse
io.default
parse.att2intel
parse.mips_pseudo
parse.dalvik_pseudo
parse.x86_pseudo
"
SHARED="
parse.6502_pseudo
parse.tms320_pseudo
arch.ppc_cs
io.mach
io.debug
io.mmap
io.w32
io.w32dbg
io.ihex
crypto.aes
io.rap
io.ewf
io.http
io.bfdbg
io.gdb
io.bochs
io.haret
bin_xtr.dyldcache
io.ptrace
io.procpid
io.shm
io.zip
anal.ppc
anal.sparc
anal.8051
anal.arm_cs
anal.avr
anal.dalvik
anal.i8080
arch.arc
arch.bf
arch.z80
arch.xap
arch.tms320
anal.m68k
arch.sh
anal.mips_cs
anal.sparc
anal.x86_cs
anal.x86_im
anal.x86_simple
arch.arc
arch.arm
arch.bf
arch.i4004
arch.null
arch.ppc_gnu
arch.ppc_cs
arch.sh
arch.tms320
arch.wasm
arch.x86_nz
arch.xap
arch.z80
bin.any
bin.dex
bin.elf
bin.elf64
bin.fs
bin.mach0
bin.mach064
bin.mz
bin.p9
bin.pe
bin.pe64
bin.rar
bin.te
bin.wasm
bin_xtr.dyldcache
bin_xtr.xtr_fatmach0
bin_xtr.xtr_sep64
bp.arm
bp.bf
bp.mips
bp.ppc
debug.native
bp.x86
cmd.dummy
core.a2f
crypto.aes
debug.bf
debug.esil
debug.gdb
debug.native
debug.rap
debug.bf
io.shm"
egg.exec
egg.xor
esil.dummy
fs.posix
io.bfdbg
io.bochs
io.debug
io.default
io.ewf
io.gdb
io.haret
io.http
io.ihex
io.mach
io.malloc
io.mmap
io.procpid
io.ptrace
io.rap
io.shm
io.sparse
io.w32
io.w32dbg
io.shm
io.zip
parse.6502_pseudo
parse.att2intel
parse.dalvik_pseudo
parse.mips_pseudo
parse.tms320_pseudo
parse.x86_pseudo
"

View File

@ -32,6 +32,12 @@ static void initcs(csh *ud) {
static bool r_arch_cs_init(RArchSession *as, csh *cs_handle) {
int mode = CSINC_MODE;
initcs (cs_handle);
#if 0
const bool be = R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config);
if (be) {
mode = CS_MODE_BIG_ENDIAN;
}
#endif
#if 0
if (mode != a->cs_omode || a->config->bits != a->cs_obits) {
if (a->cs_handle != 0) {

View File

@ -1,4 +1,4 @@
/* radare2 - LGPL - Copyright 2013-2022 - pancake */
/* radare2 - LGPL - Copyright 2013-2023 - pancake */
#include <r_arch.h>
#include <r_esil.h>
@ -59,14 +59,8 @@ static const char* cmask64(char *cmaskbuf, const char *mb_c, const char *me_c) {
}
static const char* cmask32(char *cmaskbuf, const char *mb_c, const char *me_c) {
ut32 mb = 0;
ut32 me = 0;
if (mb_c) {
mb = strtol (mb_c, NULL, 16);
}
if (me_c) {
me = strtol (me_c, NULL, 16);
}
ut32 mb = mb_c? strtol (mb_c, NULL, 16): 0;
ut32 me = me_c? strtol (me_c, NULL, 16): 0;
snprintf (cmaskbuf, cmaskbuf_SIZEOF, "0x%"PFMT32x, mask32 (mb, me));
return cmaskbuf;
}
@ -541,9 +535,14 @@ static char *shrink(char *op) {
}
#define CSINC PPC
#if 0
#define CSINC_MODE \
((as->config->bits == 64) ? CS_MODE_64 : (as->config->bits == 32) ? CS_MODE_32 : 0) \
| (R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config)? CS_MODE_BIG_ENDIAN: CS_MODE_LITTLE_ENDIAN)
#else
#define CSINC_MODE \
((as->config->bits == 64) ? CS_MODE_64 : (as->config->bits == 32) ? CS_MODE_32 : 0)
#endif
#include "../capstone.inc"
typedef struct plugin_data_t {
@ -659,9 +658,18 @@ static csh cs_handle_for_session(RArchSession *as) {
return pd->cs_handle;
}
static void swap4(ut8 *buf) {
ut8 swap = buf[0];
buf[0] = buf[3];
buf[3] = swap;
swap = buf[1];
buf[1] = buf[2];
buf[2] = swap;
}
static bool decode(RArchSession *as, RAnalOp *op, RAnalOpMask mask) {
const ut64 addr = op->addr;
const ut8 *buf = op->bytes;
ut8 *buf = op->bytes;
const int len = op->size;
char cmaskbuf[cmaskbuf_SIZEOF] = {0};
csh handle = cs_handle_for_session (as);
@ -675,19 +683,26 @@ static bool decode(RArchSession *as, RAnalOp *op, RAnalOpMask mask) {
PluginData *pd = as->data;
const char *cpu = as->config->cpu;
const bool be = R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config);
if (be) {
swap4 (buf);
}
// capstone-next
int n = cs_disasm (handle, (const ut8*)buf, len, addr, 1, &insn);
if (be) {
swap4 (buf);
}
if (mask & R_ARCH_OP_MASK_DISASM) {
ret = -1;
if (cpu && !strcmp (cpu, "vle")) {
if (!R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config)) {
if (!be) {
return false;
}
// vle is big-endian only
ret = decompile_vle (as, op, addr, buf, len);
} else if (cpu && !strcmp (cpu, "ps")) {
// libps is big-endian only
if (!R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config)) {
if (!be) {
return false;
}
ret = decompile_ps (as, op, addr, buf, len);
@ -705,7 +720,7 @@ static bool decode(RArchSession *as, RAnalOp *op, RAnalOpMask mask) {
}
if (cpu && !strcmp (cpu, "vle")) {
// vle is big-endian only
if (!R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config)) {
if (!be) {
return false;
}
ret = analop_vle (as, op, addr, buf, len);

View File

@ -19,6 +19,7 @@ else
R_IO_SHM_LINKFLAGS+=-L../../cons -lr_cons
R_IO_SHM_LINKFLAGS+=-L../../util -lr_util
R_IO_SHM_LINKFLAGS+=-L../../socket -lr_socket
R_IO_SHM_LINKFLAGS+=-L../../crypto -lr_crypto
R_IO_SHM_LINKFLAGS+=-L.. -lr_io
endif