From f08fc91dd9a9876e673abfa430e1fefda0a54033 Mon Sep 17 00:00:00 2001 From: pancake Date: Tue, 29 Nov 2022 10:42:04 +0100 Subject: [PATCH] Initial implementation of the arch.any.as plugin ##arch --- binr/rabin2/rabin2.c | 2 +- configure-plugins | 1 + dist/plugins-cfg/plugins.def.cfg | 1 + libr/arch/p/any_as.mk | 10 ++++++++ libr/arch/p/arch_as.c | 44 ++++++++++++++++++++++++++++++++ libr/arch/p/as/plugin.c | 44 ++++++++++++++++++++++++++++++++ libr/include/r_arch.h | 1 + 7 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 libr/arch/p/any_as.mk create mode 100644 libr/arch/p/arch_as.c create mode 100644 libr/arch/p/as/plugin.c diff --git a/binr/rabin2/rabin2.c b/binr/rabin2/rabin2.c index 72bb71b8bb..bd848d192f 100644 --- a/binr/rabin2/rabin2.c +++ b/binr/rabin2/rabin2.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2021 - pancake */ +/* radare - LGPL - Copyright 2009-2022 - pancake */ #include diff --git a/configure-plugins b/configure-plugins index a450088356..ab8c065a3a 100755 --- a/configure-plugins +++ b/configure-plugins @@ -39,6 +39,7 @@ help () { cfg=./plugins.cfg if [ ! -f "$cfg" ]; then + echo "configure-plugins: Copying dist/plugins-cfg/plugins.def.cfg" cp -f dist/plugins-cfg/plugins.def.cfg plugins.cfg fi diff --git a/dist/plugins-cfg/plugins.def.cfg b/dist/plugins-cfg/plugins.def.cfg index 803ff28bf1..9f440f459a 100644 --- a/dist/plugins-cfg/plugins.def.cfg +++ b/dist/plugins-cfg/plugins.def.cfg @@ -44,6 +44,7 @@ anal.ppc_cs anal.ppc_gnu anal.propeller anal.pyc +arch.any_as arch.riscv anal.riscv_cs anal.s390_cs diff --git a/libr/arch/p/any_as.mk b/libr/arch/p/any_as.mk new file mode 100644 index 0000000000..1837f4e9fd --- /dev/null +++ b/libr/arch/p/any_as.mk @@ -0,0 +1,10 @@ +OBJ_ANYAS=p/as/plugin.o + +STATIC_OBJ+=${OBJ_ANYAS} +TARGET_ANYAS=arch_as.${EXT_SO} + +ALL_TARGETS+=${TARGET_ANYAS} + +${TARGET_ANYAS}: ${OBJ_ANYAS} + ${CC} ${CFLAGS} $(call libname,arch_as) $(CS_CFLAGS) \ + -o arch_as.${EXT_SO} ${OBJ_ANYAS} $(CS_LDFLAGS) diff --git a/libr/arch/p/arch_as.c b/libr/arch/p/arch_as.c new file mode 100644 index 0000000000..2f3df5907f --- /dev/null +++ b/libr/arch/p/arch_as.c @@ -0,0 +1,44 @@ +/* radare2 - BSD - Copyright 2022 - pancake */ + +#include +#include +} + +static bool encode (RArchSession *a, RAnalOp *op, RArchEncodeMask mask) { + int len = 0; + ut8 *out; + char *gas = r_sys_getenv ("RASM2_AS"); + if (!gas) { + gas = strdup ("as"); + } + char *cmd = r_str_newf ( + "%s /dev/stdin -o /dev/stdout <<__\n" + "BITS %i\nORG 0x%"PFMT64x"\n%s\n__", + gas, a->bits, a->pc, buf); + ut8 *out = (ut8 *)r_sys_cmd_str (cmd, "", &len); + if (out) { + r_anal_op_setbytes (op, op->addr, out, len); + free (out); + } + op->size = len; + free (cmd); + return true; +} + +RArchPlugin r_arch_plugin_as = { + .name = "as", + .desc = "GNU/Clang assembler RASM2_AS or `as`", + .license = "LGPL3", + .arch = NULL, + .bits = R_SYS_BITS_PACK3 (16, 32, 64), + .encode = &encode, + .endian = R_SYS_ENDIAN_LITTLE | R_:SYS_ENDIAN_BIG, +}; + +#ifndef R2_PLUGIN_INCORE +R_API RLibStruct radare_plugin = { + .type = R_LIB_TYPE_ARCH, + .data = &r_arch_plugin_as, + .version = R2_VERSION +}; +#endif diff --git a/libr/arch/p/as/plugin.c b/libr/arch/p/as/plugin.c new file mode 100644 index 0000000000..cf62225a22 --- /dev/null +++ b/libr/arch/p/as/plugin.c @@ -0,0 +1,44 @@ +/* Copyright (C) 2008-2022 - pancake */ + +#include + +static bool as_encode(RArchSession *s, RAnalOp *op, RArchEncodeMask mask) { + int len = 0; + char *gas = r_sys_getenv ("RASM2_AS"); + if (!gas) { + // TODO: find in PATH + gas = strdup ("as"); + } + char *cmd = r_str_newf ( + "%s -o a.out /dev/stdin <<__\n%s\n__\n" + "rabin2 -rO 'd/S/*text' a.out; rm -f a.out\n", + gas, op->mnemonic); + ut8 *out = (ut8 *)r_sys_cmd_str (cmd, NULL, &len); + if (out) { + r_anal_op_set_bytes (op, op->addr, out, len); + free (out); + } + op->size = len; + free (cmd); + return len > 0; +} + +RArchPlugin r_arch_plugin_any_as = { + .name = "any.as", + .desc = "Uses system gnu/clang 'as' assembler", + .author = "pancake", + .license = "LGPL3", + // TODO: add the "any" architecture to support any, instead of using null + .arch = "any", // on purpose because that's a multi-arch plugin + .bits = R_SYS_BITS_PACK3 (16, 32, 64), + .endian = R_SYS_ENDIAN_LITTLE, + .encode = &as_encode, +}; + +#ifndef R2_PLUGIN_INCORE +R_API RLibStruct radare_plugin = { + .type = R_LIB_TYPE_ARCH, + .data = &r_arch_plugin_any_as, + .version = R2_VERSION +}; +#endif diff --git a/libr/include/r_arch.h b/libr/include/r_arch.h index 6d8310eab7..ad0f1fcd2c 100644 --- a/libr/include/r_arch.h +++ b/libr/include/r_arch.h @@ -256,6 +256,7 @@ extern RArchPlugin r_arch_plugin_v810; extern RArchPlugin r_arch_plugin_rsp; extern RArchPlugin r_arch_plugin_riscv; extern RArchPlugin r_arch_plugin_x86_nz; +extern RArchPlugin r_arch_plugin_any_as; #ifdef __cplusplus }