From 23afb7eeb38d033ab018204acb0e9de7732499e8 Mon Sep 17 00:00:00 2001 From: Nibble Date: Wed, 18 Feb 2009 01:49:26 +0100 Subject: [PATCH] * r_asm - Used plugin infrastructure - Huge refactoring - Added x86 plugin --HG-- rename : libr/asm/arch/x86/asm.c => libr/asm/p/asm_x86.c --- libr/anal/p/anal_x86.c | 2 +- libr/asm/Makefile | 58 --------------- libr/asm/arch/x86/asm.c | 86 ----------------------- libr/asm/asm.c | 151 ++++++++++++++-------------------------- libr/asm/p/Makefile | 78 +++++++++++++++++++++ libr/asm/p/asm_dummy.c | 26 +++++++ libr/asm/p/asm_x86.c | 101 +++++++++++++++++++++++++++ libr/asm/t/Makefile | 39 ----------- libr/core/cmd.c | 14 ++-- libr/core/core.c | 18 ++++- libr/include/r_anal.h | 15 ---- libr/include/r_asm.h | 91 +++++++++--------------- libr/include/r_core.h | 1 + libr/include/r_lib.h | 1 - 14 files changed, 315 insertions(+), 366 deletions(-) delete mode 100644 libr/asm/arch/x86/asm.c create mode 100644 libr/asm/p/Makefile create mode 100644 libr/asm/p/asm_dummy.c create mode 100644 libr/asm/p/asm_x86.c delete mode 100644 libr/asm/t/Makefile diff --git a/libr/anal/p/anal_x86.c b/libr/anal/p/anal_x86.c index fdd845e87f..c94a132a5d 100644 --- a/libr/anal/p/anal_x86.c +++ b/libr/anal/p/anal_x86.c @@ -25,7 +25,7 @@ // XXX addr should be off_t for 64 love static int aop(struct r_anal_aop_t *aop, void *data) { - struct r_asm_t *asmdata = (struct r_asm_t*)data; + struct r_asm_aop_t *asmdata = (struct r_asm_t*)data; u8 *buf = asmdata->buf; memset(aop, '\0', sizeof(struct r_anal_aop_t)); diff --git a/libr/asm/Makefile b/libr/asm/Makefile index 35ed95531a..39f0b28739 100644 --- a/libr/asm/Makefile +++ b/libr/asm/Makefile @@ -1,62 +1,4 @@ NAME=r_asm OBJ=asm.o -DEPS=r_util - -CFLAGS+=-Iarch/include -# XXX -CFLAGS+=-DLIL_ENDIAN=1 - -# X86 -OBJ+=arch/x86/asm.o -OBJ+=arch/x86/pseudo.o -OBJ+=arch/x86/realloc.o -# udis86 -OBJ+=arch/x86/udis86/syn.o -OBJ+=arch/x86/udis86/input.o -OBJ+=arch/x86/udis86/udis86.o -OBJ+=arch/x86/udis86/decode.o -OBJ+=arch/x86/udis86/itab.o -OBJ+=arch/x86/udis86/syn-intel.o -OBJ+=arch/x86/udis86/syn-att.o -# olly -OBJ+=arch/x86/ollyasm/disasm.o -OBJ+=arch/x86/ollyasm/asmserv.o -OBJ+=arch/x86/ollyasm/assembl.o - -# ARM -OBJ+=arch/arm/asm.o -# gnu arm-dis -OBJ+=arch/arm/gnu/arm-dis.o - -# MIPS -OBJ+=arch/mips/asm.o -# gnu mips-dis -OBJ+=arch/mips/gnu/mips-dis.o -OBJ+=arch/mips/gnu/mips16-opc.o -OBJ+=arch/mips/gnu/mips-opc.o - -# SPARC -OBJ+=arch/sparc/asm.o -# gnu sparc-dis -OBJ+=arch/sparc/gnu/sparc-dis.o -OBJ+=arch/sparc/gnu/sparc-opc.o - -# PPC -OBJ+=arch/ppc/asm.o -# ppc-disasm -OBJ+=arch/ppc/ppc_disasm/ppc_disasm.o - -# BF -OBJ+=arch/bf/asm.o - -# CSR -OBJ+=arch/csr/asm.o -# ppc-disasm -OBJ+=arch/csr/csr_disasm/dis.o - -# M68K -OBJ+=arch/m68k/asm.o -# mk8k-disasm -OBJ+=arch/m68k/m68k_disasm/m68k_disasm.o include ../rules.mk diff --git a/libr/asm/arch/x86/asm.c b/libr/asm/arch/x86/asm.c deleted file mode 100644 index b8e626b995..0000000000 --- a/libr/asm/arch/x86/asm.c +++ /dev/null @@ -1,86 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include -#include - -#include -#include - -#include "udis86/types.h" -#include "udis86/extern.h" -#include "ollyasm/disasm.h" - - -int r_asm_x86_disasm(struct r_asm_t *a, u8 *buf, u64 len) -{ - union { - ud_t ud; - t_disasm olly; - } disasm_obj; - - switch (a->syntax) { - case R_ASM_SYN_INTEL: - case R_ASM_SYN_ATT: - ud_init(&disasm_obj.ud); - if (a->syntax == R_ASM_SYN_INTEL) - ud_set_syntax(&disasm_obj.ud, UD_SYN_INTEL); - else ud_set_syntax(&disasm_obj.ud, UD_SYN_ATT); - ud_set_mode(&disasm_obj.ud, a->bits); - ud_set_pc(&disasm_obj.ud, a->pc); - ud_set_input_buffer(&disasm_obj.ud, buf, len); - ud_disassemble(&disasm_obj.ud); - a->inst_len = ud_insn_len(&disasm_obj.ud); - snprintf(a->buf_asm, 256, "%s", ud_insn_asm(&disasm_obj.ud)); - snprintf(a->buf_hex, 256, "%s", ud_insn_hex(&disasm_obj.ud)); - break; - case R_ASM_SYN_OLLY: - lowercase=1; - a->inst_len = Disasm(buf, len, a->pc, &disasm_obj.olly, DISASM_FILE); - snprintf(a->buf_asm, 256, "%s", disasm_obj.olly.result); - snprintf(a->buf_hex, 256, "%s", disasm_obj.olly.dump); - break; - default: - a->inst_len = 0; - } - - if (a->inst_len > 0) - memcpy(a->buf, buf, a->inst_len); - - return a->inst_len; -} - -int r_asm_x86_asm(struct r_asm_t *a, char *buf) -{ - union { - t_asmmodel olly; - } asm_obj; - int idx; - - switch (a->syntax) { - case R_ASM_SYN_INTEL: - case R_ASM_SYN_ATT: - /* TODO: Use gas for assembling */ - a->inst_len = 0; - break; - case R_ASM_SYN_OLLY: - a->buf_err[0] = '\0'; - /* constsize == 0: Address constants and inmediate data of 16/32b */ - /* attempt == 0: First attempt */ - a->inst_len = Assemble(buf, a->pc, &asm_obj.olly, 0, 0, a->buf_err); - if (a->buf_err[0]) - a->inst_len = 0; - else { - snprintf(a->buf_asm, 256, "%s", buf); - for (idx = 0; idx < a->inst_len; idx++) - sprintf(a->buf_hex+idx*2, "%02x", (u8)asm_obj.olly.code[idx]); - } - break; - default: - a->inst_len = 0; - } - - if (a->inst_len > 0) - memcpy(a->buf, buf, a->inst_len); - - return a->inst_len; -} diff --git a/libr/asm/asm.c b/libr/asm/asm.c index 480d0b4bbb..746ce91aa8 100644 --- a/libr/asm/asm.c +++ b/libr/asm/asm.c @@ -5,18 +5,7 @@ #include #include #include - -int r_asm_init(struct r_asm_t *a) -{ - memset(a, '\0', sizeof(struct r_asm_t)); - r_asm_set_arch(a, R_ASM_ARCH_X86); - r_asm_set_bits(a, 32); - r_asm_set_big_endian(a, 0); - r_asm_set_syntax(a, R_ASM_SYN_INTEL); - r_asm_set_parser(a, R_ASM_PAR_NULL, NULL, NULL); - r_asm_set_pc(a, 0); - return R_TRUE; -} +#include struct r_asm_t *r_asm_new() { @@ -30,50 +19,53 @@ void r_asm_free(struct r_asm_t *a) free(a); } -int r_asm_set_arch(struct r_asm_t *a, int arch) +int r_asm_init(struct r_asm_t *a) { - switch (arch) { - case R_ASM_ARCH_X86: - a->r_asm_disasm = &r_asm_x86_disasm; - a->r_asm_asm = &r_asm_x86_asm; - break; - case R_ASM_ARCH_ARM: - a->r_asm_disasm = &r_asm_arm_disasm; - a->r_asm_asm = NULL; - break; - case R_ASM_ARCH_MIPS: - a->r_asm_disasm = &r_asm_mips_disasm; - a->r_asm_asm = NULL; - break; - case R_ASM_ARCH_SPARC: - a->r_asm_disasm = &r_asm_sparc_disasm; - a->r_asm_asm = NULL; - break; - case R_ASM_ARCH_PPC: - a->r_asm_disasm = &r_asm_ppc_disasm; - a->r_asm_asm = NULL; - break; - case R_ASM_ARCH_BF: - a->r_asm_disasm = &r_asm_bf_disasm; - a->r_asm_asm = NULL; - break; - case R_ASM_ARCH_CSR: - a->r_asm_disasm = &r_asm_csr_disasm; - a->r_asm_asm = NULL; - break; - case R_ASM_ARCH_M68K: - a->r_asm_disasm = &r_asm_m68k_disasm; - a->r_asm_asm = NULL; - break; - default: - a->r_asm_disasm = NULL; - a->r_asm_asm = NULL; - return R_FALSE; - } - a->arch = arch; + a->user = NULL; + INIT_LIST_HEAD(&a->asms); + r_asm_set_bits(a, 32); + r_asm_set_big_endian(a, 0); + r_asm_set_syntax(a, R_ASM_SYN_INTEL); + r_asm_set_pc(a, 0); return R_TRUE; } +void r_asm_set_user_ptr(struct r_asm_t *a, void *user) +{ + a->user = user; +} + +int r_asm_add(struct r_asm_t *a, struct r_asm_handle_t *foo) +{ + if (foo->init) + foo->init(a->user); + list_add_tail(&(foo->list), &(a->asms)); + return R_TRUE; +} + +int r_asm_list(struct r_asm_t *a) +{ + struct list_head *pos; + list_for_each_prev(pos, &a->asms) { + struct r_asm_handle_t *h = list_entry(pos, struct r_asm_handle_t, list); + printf(" %s: %s\n", h->name, h->desc); + } + return R_FALSE; +} + +int r_asm_set(struct r_asm_t *a, const char *name) +{ + struct list_head *pos; + list_for_each_prev(pos, &a->asms) { + struct r_asm_handle_t *h = list_entry(pos, struct r_asm_handle_t, list); + if (!memcmp(h->name, name, strlen(h->name))) { + a->cur = h; + return R_TRUE; + } + } + return R_FALSE; +} + int r_asm_set_bits(struct r_asm_t *a, int bits) { switch (bits) { @@ -106,63 +98,24 @@ int r_asm_set_syntax(struct r_asm_t *a, int syntax) } } -int r_asm_set_parser(struct r_asm_t *a, - int parser, int (*cb)(struct r_asm_t *a), void *aux) -{ - switch (parser) { - case R_ASM_PAR_NULL: - a->r_asm_parse = NULL; - break; - case R_ASM_PAR_PSEUDO: - if (a->arch == R_ASM_ARCH_X86 && a->syntax == R_ASM_SYN_INTEL) { - a->r_asm_parse = &r_asm_x86_pseudo; - break; - } else return R_FALSE; - case R_ASM_PAR_REALLOC: - if (a->arch == R_ASM_ARCH_X86 && a->syntax == R_ASM_SYN_INTEL) { - a->r_asm_parse = &r_asm_x86_realloc; - break; - } else return R_FALSE; - default: - return R_FALSE; - } - a->parser = parser; - a->r_asm_parse_cb = cb; - a->aux = aux; - return R_TRUE; -} - int r_asm_set_pc(struct r_asm_t *a, u64 pc) { a->pc = pc; return R_TRUE; } -int r_asm_disasm(struct r_asm_t *a, u8 *buf, u64 len) +int r_asm_disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) { - if (a->r_asm_disasm != NULL) - return a->r_asm_disasm(a, buf, len); + if (a->cur && a->cur->disassemble) + return a->cur->disassemble(a, aop, buf, len); - return 0; + return R_FALSE; } -int r_asm_asm(struct r_asm_t *a, char *buf) +int r_asm_assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf) { - if (a->r_asm_asm != NULL) - return a->r_asm_asm(a, buf); + if (a->cur && a->cur->assemble) + return a->cur->assemble(a, aop, buf); - return 0; -} - -int r_asm_parse(struct r_asm_t *a) -{ - int ret = 0; - - if (a->r_asm_parse != NULL) - ret = a->r_asm_parse(a); - - if (a->r_asm_parse_cb != NULL) - a->r_asm_parse_cb(a); - - return ret; + return R_FALSE; } diff --git a/libr/asm/p/Makefile b/libr/asm/p/Makefile new file mode 100644 index 0000000000..0567409949 --- /dev/null +++ b/libr/asm/p/Makefile @@ -0,0 +1,78 @@ +CFLAGS=-I../../include -I../arch/ -Wall +BINDEPS= +# XXX +CFLAGS+=-DLIL_ENDIAN=1 + +# X86 +# udis86 +OBJ_X86+=asm_x86.o +OBJ_X86+=../arch/x86/udis86/syn.o +OBJ_X86+=../arch/x86/udis86/input.o +OBJ_X86+=../arch/x86/udis86/udis86.o +OBJ_X86+=../arch/x86/udis86/decode.o +OBJ_X86+=../arch/x86/udis86/itab.o +OBJ_X86+=../arch/x86/udis86/syn-intel.o +OBJ_X86+=../arch/x86/udis86/syn-att.o +# olly +OBJ_X86+=../arch/x86/ollyasm/disasm.o +OBJ_X86+=../arch/x86/ollyasm/asmserv.o +OBJ_X86+=../arch/x86/ollyasm/assembl.o + +all: asm_dummy.so asm_x86.so + @true + +asm_dummy.so: asm_dummy.o + ${CC} ${CFLAGS} -fPIC -shared -o asm_dummy.so asm_dummy.o -Wl,-R.. + @#strip -s asm_dummy.so + +asm_x86.so: ${OBJ_X86} + ${CC} ${CFLAGS} -fPIC -shared -o asm_x86.so ${OBJ_X86} -Wl,-R.. + @#strip -s asm_x86.so + +clean: + -rm -f *.so *.o + +#NAME=r_asm +#OBJ=asm.o +#DEPS=r_util +# +#CFLAGS+=-Iarch/include +# +# +## ARM +#OBJ+=arch/arm/asm.o +## gnu arm-dis +#OBJ+=arch/arm/gnu/arm-dis.o +# +## MIPS +#OBJ+=arch/mips/asm.o +## gnu mips-dis +#OBJ+=arch/mips/gnu/mips-dis.o +#OBJ+=arch/mips/gnu/mips16-opc.o +#OBJ+=arch/mips/gnu/mips-opc.o +# +## SPARC +#OBJ+=arch/sparc/asm.o +## gnu sparc-dis +#OBJ+=arch/sparc/gnu/sparc-dis.o +#OBJ+=arch/sparc/gnu/sparc-opc.o +# +## PPC +#OBJ+=arch/ppc/asm.o +## ppc-disasm +#OBJ+=arch/ppc/ppc_disasm/ppc_disasm.o +# +## BF +#OBJ+=arch/bf/asm.o +# +## CSR +#OBJ+=arch/csr/asm.o +## ppc-disasm +#OBJ+=arch/csr/csr_disasm/dis.o +# +## M68K +#OBJ+=arch/m68k/asm.o +## mk8k-disasm +#OBJ+=arch/m68k/m68k_disasm/m68k_disasm.o +# +#include ../rules.mk diff --git a/libr/asm/p/asm_dummy.c b/libr/asm/p/asm_dummy.c new file mode 100644 index 0000000000..7070ff5455 --- /dev/null +++ b/libr/asm/p/asm_dummy.c @@ -0,0 +1,26 @@ +/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ + +#include +#include +#include + +static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) +{ + printf("Dummy (dis)assembly plugin"); + + return R_FALSE; +} + +static struct r_asm_handle_t r_asm_plugin_dummy = { + .name = "asm_dummy", + .desc = "dummy disassembly plugin", + .init = NULL, + .fini = NULL, + .disassemble = &disassemble, + .assemble = NULL +}; + +struct r_lib_struct_t radare_plugin = { + .type = R_LIB_TYPE_ASM, + .data = &r_asm_plugin_dummy +}; diff --git a/libr/asm/p/asm_x86.c b/libr/asm/p/asm_x86.c new file mode 100644 index 0000000000..c5814210b8 --- /dev/null +++ b/libr/asm/p/asm_x86.c @@ -0,0 +1,101 @@ +/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ + +#include +#include + +#include +#include +#include + +#include "x86/udis86/types.h" +#include "x86/udis86/extern.h" +#include "x86/ollyasm/disasm.h" + + +static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) +{ + union { + ud_t ud; + t_disasm olly; + } disasm_obj; + + switch (a->syntax) { + case R_ASM_SYN_INTEL: + case R_ASM_SYN_ATT: + ud_init(&disasm_obj.ud); + if (a->syntax == R_ASM_SYN_INTEL) + ud_set_syntax(&disasm_obj.ud, UD_SYN_INTEL); + else ud_set_syntax(&disasm_obj.ud, UD_SYN_ATT); + ud_set_mode(&disasm_obj.ud, a->bits); + ud_set_pc(&disasm_obj.ud, a->pc); + ud_set_input_buffer(&disasm_obj.ud, buf, len); + ud_disassemble(&disasm_obj.ud); + aop->inst_len = ud_insn_len(&disasm_obj.ud); + snprintf(aop->buf_asm, 256, "%s", ud_insn_asm(&disasm_obj.ud)); + snprintf(aop->buf_hex, 256, "%s", ud_insn_hex(&disasm_obj.ud)); + break; + case R_ASM_SYN_OLLY: + lowercase=1; + aop->inst_len = Disasm(buf, len, a->pc, &disasm_obj.olly, DISASM_FILE); + snprintf(aop->buf_asm, 256, "%s", disasm_obj.olly.result); + snprintf(aop->buf_hex, 256, "%s", disasm_obj.olly.dump); + break; + default: + aop->inst_len = 0; + } + + if (aop->inst_len > 0) + memcpy(aop->buf, buf, aop->inst_len); + + return aop->inst_len; +} + +static int assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf) +{ + union { + t_asmmodel olly; + } asm_obj; + int idx; + + switch (a->syntax) { + case R_ASM_SYN_INTEL: + case R_ASM_SYN_ATT: + /* TODO: Use gas for assembling */ + aop->inst_len = 0; + break; + case R_ASM_SYN_OLLY: + aop->buf_err[0] = '\0'; + /* constsize == 0: Address constants and inmediate data of 16/32b */ + /* attempt == 0: First attempt */ + aop->inst_len = Assemble(buf, a->pc, &asm_obj.olly, 0, 0, aop->buf_err); + if (aop->buf_err[0]) + aop->inst_len = 0; + else { + snprintf(aop->buf_asm, 256, "%s", buf); + for (idx = 0; idx < aop->inst_len; idx++) + sprintf(aop->buf_hex+idx*2, "%02x", (u8)asm_obj.olly.code[idx]); + } + break; + default: + aop->inst_len = 0; + } + + if (aop->inst_len > 0) + memcpy(aop->buf, buf, aop->inst_len); + + return aop->inst_len; +} + +static struct r_asm_handle_t r_asm_plugin_x86 = { + .name = "asm_x86", + .desc = "X86 disassembly plugin", + .init = NULL, + .fini = NULL, + .disassemble = &disassemble, + .assemble = &assemble, +}; + +struct r_lib_struct_t radare_plugin = { + .type = R_LIB_TYPE_ASM, + .data = &r_asm_plugin_x86 +}; diff --git a/libr/asm/t/Makefile b/libr/asm/t/Makefile deleted file mode 100644 index 8c227f19a1..0000000000 --- a/libr/asm/t/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -LDFLAGS=-L../../util -lr_util -L../ -lr_asm -LDFLAGS_BIN=${LDFLAGS} -lr_bin -L../../bin -all: disasm_x86 disasm_arm disasm_mips disasm_sparc disasm_ppc disasm_csr disasm_m68k disasm_bf asm_x86 rasm2 realloc_x86 - -disasm_x86: - ${CC} -g -I ../../include disasm_x86.c ${LDFLAGS} -o disasm_x86 - -disasm_arm: - ${CC} -g -I ../../include disasm_arm.c ${LDFLAGS} -o disasm_arm - -disasm_mips: - ${CC} -g -I ../../include disasm_mips.c ${LDFLAGS} -o disasm_mips - -disasm_sparc: - ${CC} -g -I ../../include disasm_sparc.c ${LDFLAGS} -o disasm_sparc - -disasm_ppc: - ${CC} -g -I ../../include disasm_ppc.c ${LDFLAGS} -o disasm_ppc - -disasm_csr: - ${CC} -g -I ../../include disasm_csr.c ${LDFLAGS} -o disasm_csr - -disasm_m68k: - ${CC} -g -I ../../include disasm_m68k.c ${LDFLAGS} -o disasm_m68k - -disasm_bf: - ${CC} -g -I ../../include disasm_bf.c ${LDFLAGS} -o disasm_bf - -asm_x86: - ${CC} -g -I ../../include asm_x86.c ${LDFLAGS} -o asm_x86 - -rasm2: - ${CC} -g -I ../../include rasm2.c ${LDFLAGS} -o rasm2 - -realloc_x86: - ${CC} -g -I ../../include realloc_x86.c ${LDFLAGS_BIN} -o realloc_x86 - -clean: - rm -f disasm_x86 disasm_arm disasm_mips disasm_sparc disasm_ppc disasm_csr disasm_m68k disasm_bf asm_x86 rasm2 realloc_x86 diff --git a/libr/core/cmd.c b/libr/core/cmd.c index cfaf0a6fea..53f579f956 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -252,15 +252,15 @@ static int cmd_print(void *data, const char *input) /* XXX hardcoded */ int ret, idx; u8 *buf = core->block; - struct r_asm_t a;// TODO: move to core.h - r_asm_init(&a); - r_asm_set_pc(&a, core->seek); + struct r_asm_aop_t aop; + r_asm_set_pc(&core->assembler, core->seek); + r_asm_set(&core->assembler, "asm_x86"); for(idx=ret=0; idx < len; idx+=ret) { - r_asm_set_pc(&a, a.pc + ret); - ret = r_asm_disasm(&a, buf+idx, len-idx); + r_asm_set_pc(&core->assembler, core->assembler.pc + ret); + ret = r_asm_disassemble(&core->assembler, &aop, buf+idx, len-idx); r_cons_printf("0x%08llx %14s %s\n", - core->seek+idx, a.buf_hex, a.buf_asm); + core->seek+idx, aop.buf_hex, aop.buf_asm); } } break; @@ -368,6 +368,7 @@ static int cmd_anal(void *data, const char *input) r_anal_list(&core->anal); break; case 'o': +#if 0 { /* XXX hardcoded */ int ret, idx; @@ -388,6 +389,7 @@ static int cmd_anal(void *data, const char *input) aop.jump); } } +#endif break; default: fprintf(stderr, "Usage: a[o] [len]\n" diff --git a/libr/core/core.c b/libr/core/core.c index 07ce06e7d8..5ddcc85b70 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -80,6 +80,18 @@ int __lib_anl_cb(struct r_lib_plugin_t *pl, void *user, void *data) int __lib_anl_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; } +/* asm callback */ +int __lib_asm_cb(struct r_lib_plugin_t *pl, void *user, void *data) +{ + struct r_asm_handle_t *hand = (struct r_asm_handle_t *)data; + struct r_core_t *core = (struct r_core_t *)user; + //printf(" * Added (dis)assembly handler\n"); + r_asm_add(&core->assembler, hand); + return R_TRUE; +} + +int __lib_asm_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; } + int r_core_init(struct r_core_t *core) { core->oobi = NULL; @@ -89,8 +101,10 @@ int r_core_init(struct r_core_t *core) r_lang_init(&core->lang); r_lang_set_user_ptr(&core->lang, core); r_anal_init(&core->anal); - r_meta_init(&core->meta); r_anal_set_user_ptr(&core->anal, core); + r_asm_init(&core->assembler); + r_asm_set_user_ptr(&core->assembler, core); + r_meta_init(&core->meta); r_cons_init(); core->search = r_search_new(R_SEARCH_KEYWORD); r_io_init(&core->io); @@ -116,6 +130,8 @@ int r_core_init(struct r_core_t *core) &__lib_lng_cb, &__lib_lng_dt, core); r_lib_add_handler(&core->lib, R_LIB_TYPE_ANAL, "analysis plugins", &__lib_anl_cb, &__lib_anl_dt, core); + r_lib_add_handler(&core->lib, R_LIB_TYPE_ASM, "(dis)assembly plugins", + &__lib_asm_cb, &__lib_asm_dt, core); r_lib_opendir(&core->lib, getenv("LIBR_PLUGINS")); { char *homeplugindir = r_str_home(".radare/plugins"); diff --git a/libr/include/r_anal.h b/libr/include/r_anal.h index d0e3b9c124..249bb08338 100644 --- a/libr/include/r_anal.h +++ b/libr/include/r_anal.h @@ -6,21 +6,6 @@ #include "r_types.h" #include "list.h" -enum { - R_ANAL_ARCH_NULL = 0, - R_ANAL_ARCH_X86, - R_ANAL_ARCH_ARM, - R_ANAL_ARCH_PPC, - R_ANAL_ARCH_M68K, - R_ANAL_ARCH_JAVA, - R_ANAL_ARCH_MIPS, - R_ANAL_ARCH_SPARC, - R_ANAL_ARCH_CSR, - R_ANAL_ARCH_MSIL, - R_ANAL_ARCH_OBJD, - R_ANAL_ARCH_BF -}; - enum { R_ANAL_AOP_TYPE_NULL = 0, R_ANAL_AOP_TYPE_JMP, /* mandatory jump */ diff --git a/libr/include/r_asm.h b/libr/include/r_asm.h index 8676fbd5cd..abe6a03809 100644 --- a/libr/include/r_asm.h +++ b/libr/include/r_asm.h @@ -3,7 +3,8 @@ #ifndef _INCLUDE_R_ASM_H_ #define _INCLUDE_R_ASM_H_ -#include "r_types.h" +#include +#include enum { R_ASM_ARCH_NULL = 0, @@ -20,6 +21,7 @@ enum { R_ASM_ARCH_BF }; + enum { R_ASM_SYN_NULL = 0, R_ASM_SYN_INTEL, @@ -27,77 +29,46 @@ enum { R_ASM_SYN_OLLY }; -enum { - R_ASM_PAR_NULL = 0, - R_ASM_PAR_PSEUDO, - R_ASM_PAR_REALLOC -}; - -struct r_asm_t { - int arch; - int bits; - int big_endian; - int syntax; - int parser; - u64 pc; +struct r_asm_aop_t { int inst_len; u8 buf[256]; char buf_asm[256]; char buf_hex[256]; char buf_err[256]; - void *aux; - int (*r_asm_disasm)(struct r_asm_t *a, u8 *buf, u64 len); - int (*r_asm_asm)(struct r_asm_t *a, char *buf); - int (*r_asm_parse)(struct r_asm_t *a); - int (*r_asm_parse_cb)(struct r_asm_t *a); +}; + +struct r_asm_t { + int bits; + int big_endian; + int syntax; + u64 pc; + void *user; + struct r_asm_handle_t *cur; + struct list_head asms; +}; + +struct r_asm_handle_t { + char *name; + char *desc; + int (*init)(void *user); + int (*fini)(void *user); + int (*disassemble)(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len); + int (*assemble)(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf); + struct list_head list; }; /* asm.c */ -int r_asm_init(struct r_asm_t *a); struct r_asm_t *r_asm_new(); void r_asm_free(struct r_asm_t *a); -int r_asm_set_arch(struct r_asm_t *a, int arch); +int r_asm_init(struct r_asm_t *a); +void r_asm_set_user_ptr(struct r_asm_t *a, void *user); +int r_asm_add(struct r_asm_t *a, struct r_asm_handle_t *foo); +int r_asm_list(struct r_asm_t *a); +int r_asm_set(struct r_asm_t *a, const char *name); int r_asm_set_bits(struct r_asm_t *a, int bits); int r_asm_set_big_endian(struct r_asm_t *a, int boolean); int r_asm_set_syntax(struct r_asm_t *a, int syntax); -int r_asm_set_parser(struct r_asm_t *a, int parser, - int (*cb)(struct r_asm_t *a), void *aux); int r_asm_set_pc(struct r_asm_t *a, u64 pc); -int r_asm_disasm(struct r_asm_t *a, u8 *buf, u64 len); -int r_asm_asm(struct r_asm_t *a, char *buf); -int r_asm_parse(struct r_asm_t *a); - -/* arch/x86/asm.c */ -int r_asm_x86_disasm(struct r_asm_t *a, u8 *buf, u64 len); -int r_asm_x86_asm(struct r_asm_t *a, char *buf); -/* arch/x86/pseudo.c */ -int r_asm_x86_pseudo(struct r_asm_t *a); -/* arch/x86/realloc.c */ -struct r_asm_realloc_t { - u64 offset; - u64 delta; - char str[256]; -}; -int r_asm_x86_realloc(struct r_asm_t *a); - -/* arch/arm/asm.c */ -int r_asm_arm_disasm(struct r_asm_t *a, u8 *buf, u64 len); - -/* arch/mips/asm.c */ -int r_asm_mips_disasm(struct r_asm_t *a, u8 *buf, u64 len); - -/* arch/sparc/asm.c */ -int r_asm_sparc_disasm(struct r_asm_t *a, u8 *buf, u64 len); - -/* arch/ppc/asm.c */ -int r_asm_ppc_disasm(struct r_asm_t *a, u8 *buf, u64 len); - -/* arch/bf/asm.c */ -int r_asm_bf_disasm(struct r_asm_t *a, u8 *buf, u64 len); - -/* arch/csr/asm.c */ -int r_asm_csr_disasm(struct r_asm_t *a, u8 *buf, u64 len); - -/* arch/m68k/asm.c */ -int r_asm_m68k_disasm(struct r_asm_t *a, u8 *buf, u64 len); +int r_asm_disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len); +int r_asm_assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf); #endif diff --git a/libr/include/r_core.h b/libr/include/r_core.h index 26c2b12ffc..9b3a837801 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -45,6 +45,7 @@ struct r_core_t { struct r_lib_t lib; struct r_cmd_t cmd; struct r_anal_t anal; + struct r_asm_t assembler; struct r_meta_t meta; struct r_lang_t lang; struct r_debug_t dbg; diff --git a/libr/include/r_lib.h b/libr/include/r_lib.h index 587ca68f56..dfc5360922 100644 --- a/libr/include/r_lib.h +++ b/libr/include/r_lib.h @@ -40,7 +40,6 @@ enum { R_LIB_TYPE_DBG, /* debugger */ R_LIB_TYPE_LANG, /* language */ R_LIB_TYPE_ASM, /* assembler */ - //R_LIB_TYPE_DIS, /* disassembler */ R_LIB_TYPE_ANAL, /* analysis */ };