From 19fa55ba334c2ecdb48911fd2be7557519cccf45 Mon Sep 17 00:00:00 2001 From: pancake Date: Tue, 10 Mar 2009 01:49:24 +0000 Subject: [PATCH] * Add random documentation for dynamic language plugins * Properly installation of vapi files - Lot of fixtures to build a external hello world in gtk+vala+r_asm - default staticplugins for r_asm are: mips, java and x86_olly by default - Add r_util.vapi and make .pc file point to -lr_util - Use .deps to generate dependencies between vapi files - Fix r_asm.vapi (Added Asm.Aop struct) - add set(string) method - asm/disasm -> assemble/disassemble * Fix warning + build in sign.c - Included in default build * Fix segfault in r_lib with NULL in opendir() * More fixtures for r_asm_plugin_* - use #ifndef CORELIB - drop 'static' * Update README --- doc/lang-plugins | 34 ++++++++++++++++++++++++++++++++++ libr/Makefile | 5 ++++- libr/README | 10 ++++++++++ libr/asm/asm.c | 1 + libr/asm/p/asm_arm.c | 4 +++- libr/asm/p/asm_csr.c | 4 +++- libr/asm/p/asm_m68k.c | 4 +++- libr/asm/p/asm_ppc.c | 2 +- libr/asm/p/asm_sparc.c | 2 +- libr/asm/p/asm_x86.c | 4 +++- libr/asm/p/asm_x86_bea.c | 4 +++- libr/asm/p/asm_x86_olly.c | 5 +++-- libr/config.h | 5 ++++- libr/config.mk | 2 +- libr/include/r_asm.h | 7 +++++++ libr/lib/lib.c | 10 +++++++--- libr/libr.pc.acr | 2 +- libr/sign/sign.c | 6 +++--- libr/syscall/TODO | 1 + libr/vapi/libr.deps | 2 ++ libr/vapi/libr.vapi | 1 + libr/vapi/r_asm.vapi | 23 ++++++++++++++++++----- libr/vapi/r_util.vapi | 11 +++++++++++ 23 files changed, 125 insertions(+), 24 deletions(-) create mode 100644 doc/lang-plugins create mode 100644 libr/vapi/libr.deps create mode 100644 libr/vapi/libr.vapi create mode 100644 libr/vapi/r_util.vapi diff --git a/doc/lang-plugins b/doc/lang-plugins new file mode 100644 index 0000000000..bba44505c1 --- /dev/null +++ b/doc/lang-plugins @@ -0,0 +1,34 @@ +There is a way to implement dynamic language plugins for radare... + +A stub generator will be appreciated to create all the necessary +boilerplate to write a single .c file calling the perl/python/ruby +interface from each method hook. + +We should define a common and simple way to design those inter- +language communications. + +With this interface properly defined we can get some more fun and +write the language bindings using the same lang-C api. + +A simple perl script can be used to parse function signatures and +enums to fill the structures in the proper way (SWIG replacement) + +help is welcome! + +------------------8<------------------------------ + +some random food: + +#include + +static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) +{ + LANG_PERL_SET_U8P("$buf", buf); + LANG_PERL_SET_U64("$len", len); + LANG_PERL( + "print \"Hello World $len\"; + $ret = 1;") + return LANG_PERL_GET($ret); +} + +PLUGIN_ASM("asm_ppc", "PPC disassembly plugin", NULL, NULL, &disasm, NULL); diff --git a/libr/Makefile b/libr/Makefile index 6d3d41fd86..e6a893fe30 100644 --- a/libr/Makefile +++ b/libr/Makefile @@ -4,7 +4,7 @@ PFX=${DESTDIR}${PREFIX} #PREFIX=${PFX} # Libraries -LIBLIST=io util lib meta lang flags bin macro hash line cons line print config syscall range socket cmd asm anal parse search diff debug reg core var +LIBLIST=io util lib meta lang flags bin macro hash line cons line print config syscall range socket cmd asm anal parse search diff debug reg core var sign # Under development #LIBLIST+=print @@ -39,6 +39,8 @@ install: @mkdir -p ${PREFIX}/lib/pkgconfig @cat libr.pc.acr | sed -e 's,@PREFIX@,${PREFIX},g' > libr.pc cp libr.pc ${PREFIX}/lib/pkgconfig + @mkdir -p ${PREFIX}/share/vala/vapi + cp vapi/*.vapi vapi/*.deps ${PREFIX}/share/vala/vapi @mkdir -p ${PREFIX}/lib/radare2 @for a in `find */p -perm /u+x -type f`; \ do echo " $$a"; cp $$a ${PREFIX}/lib/radare2 ; done @@ -59,6 +61,7 @@ uninstall: echo ${PREFIX}/lib/$$a ; rm -f ${PREFIX}/lib/$$a ; done ### includes -(cd include && for a in * ; do rm -f ${PREFIX}/libr/$$a ; done) + cd vapi/ ; for a in *.vapi *.deps ; do rm ${PREFIX}/share/vala/vapi/$$a ; done ### programs -@for a in `find */t -perm /u+x -type f | grep 2`; do \ a=`echo $$a|awk -F / '{ print $$NF; }'`; \ diff --git a/libr/README b/libr/README index 3ddbb53a2d..c932352a08 100644 --- a/libr/README +++ b/libr/README @@ -14,6 +14,16 @@ The 'R' is for refactoring, reversing, radare, ... Each module has its own test directory (t/) which contains simple applications using the upper directory code and linking against the direct dependencies. +Each mode can own a plugin directory (p/) that contains extensions for the +parent library module (libr/asm/p/asm_x86). + +There's also the possibility to define a list of static plugins to be built +inside the parent module. This way you can avoid some flexibility to ease +portability and build single binaries with some embedded features. + +The library can be used from 'pkg-config', this way you can easy build an +application or plugin that links against libr or just few elements of it. + This enables a fine-grained test level for checking all functionalities in one shot and give us the opportunity to write radare2 as test programs. diff --git a/libr/asm/asm.c b/libr/asm/asm.c index 08f037b1e9..8c51e958e8 100644 --- a/libr/asm/asm.c +++ b/libr/asm/asm.c @@ -27,6 +27,7 @@ int r_asm_init(struct r_asm_t *a) { int i; a->user = NULL; + a->cur = NULL; INIT_LIST_HEAD(&a->asms); r_asm_set_bits(a, 32); r_asm_set_big_endian(a, 0); diff --git a/libr/asm/p/asm_arm.c b/libr/asm/p/asm_arm.c index 5bb4de9dca..3284d5cc8c 100644 --- a/libr/asm/p/asm_arm.c +++ b/libr/asm/p/asm_arm.c @@ -91,7 +91,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 return aop->inst_len; } -static struct r_asm_handle_t r_asm_plugin_arm = { +struct r_asm_handle_t r_asm_plugin_arm = { .name = "asm_arm", .desc = "ARM disassembly plugin", .init = NULL, @@ -100,7 +100,9 @@ static struct r_asm_handle_t r_asm_plugin_arm = { .assemble = NULL }; +#ifndef CORELIB struct r_lib_struct_t radare_plugin = { .type = R_LIB_TYPE_ASM, .data = &r_asm_plugin_arm }; +#endif diff --git a/libr/asm/p/asm_csr.c b/libr/asm/p/asm_csr.c index a4dc5404ba..25cda54dde 100644 --- a/libr/asm/p/asm_csr.c +++ b/libr/asm/p/asm_csr.c @@ -21,7 +21,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 return aop->inst_len; } -static struct r_asm_handle_t r_asm_plugin_csr = { +struct r_asm_handle_t r_asm_plugin_csr = { .name = "asm_csr", .desc = "CSR disassembly plugin", .init = NULL, @@ -30,7 +30,9 @@ static struct r_asm_handle_t r_asm_plugin_csr = { .assemble = NULL }; +#ifndef CORELIB struct r_lib_struct_t radare_plugin = { .type = R_LIB_TYPE_ASM, .data = &r_asm_plugin_csr }; +#endif diff --git a/libr/asm/p/asm_m68k.c b/libr/asm/p/asm_m68k.c index bf522db5f5..fdf3e10678 100644 --- a/libr/asm/p/asm_m68k.c +++ b/libr/asm/p/asm_m68k.c @@ -35,7 +35,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 return aop->inst_len; } -static struct r_asm_handle_t r_asm_plugin_m68k = { +struct r_asm_handle_t r_asm_plugin_m68k = { .name = "asm_m68k", .desc = "M68K disassembly plugin", .init = NULL, @@ -44,7 +44,9 @@ static struct r_asm_handle_t r_asm_plugin_m68k = { .assemble = NULL }; +#ifndef CORELIB struct r_lib_struct_t radare_plugin = { .type = R_LIB_TYPE_ASM, .data = &r_asm_plugin_m68k }; +#endif diff --git a/libr/asm/p/asm_ppc.c b/libr/asm/p/asm_ppc.c index 40b424aab3..4e2690aab8 100644 --- a/libr/asm/p/asm_ppc.c +++ b/libr/asm/p/asm_ppc.c @@ -35,7 +35,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 return aop->inst_len; } -static struct r_asm_handle_t r_asm_plugin_ppc = { +struct r_asm_handle_t r_asm_plugin_ppc = { .name = "asm_ppc", .desc = "PPC disassembly plugin", .init = NULL, diff --git a/libr/asm/p/asm_sparc.c b/libr/asm/p/asm_sparc.c index 2e1c3f6862..e395d1dc88 100644 --- a/libr/asm/p/asm_sparc.c +++ b/libr/asm/p/asm_sparc.c @@ -90,7 +90,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 return aop->inst_len; } -static struct r_asm_handle_t r_asm_plugin_sparc = { +struct r_asm_handle_t r_asm_plugin_sparc = { .name = "asm_sparc", .desc = "SPARC disassembly plugin", .init = NULL, diff --git a/libr/asm/p/asm_x86.c b/libr/asm/p/asm_x86.c index 8fabd0d633..c195bbc908 100644 --- a/libr/asm/p/asm_x86.c +++ b/libr/asm/p/asm_x86.c @@ -35,7 +35,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 return aop->inst_len; } -static struct r_asm_handle_t r_asm_plugin_x86 = { +struct r_asm_handle_t r_asm_plugin_x86 = { .name = "asm_x86", .desc = "X86 disassembly plugin", .init = NULL, @@ -44,7 +44,9 @@ static struct r_asm_handle_t r_asm_plugin_x86 = { .assemble = NULL }; +#ifndef CORELIB struct r_lib_struct_t radare_plugin = { .type = R_LIB_TYPE_ASM, .data = &r_asm_plugin_x86 }; +#endif diff --git a/libr/asm/p/asm_x86_bea.c b/libr/asm/p/asm_x86_bea.c index 60fed11fab..d97afe0a13 100644 --- a/libr/asm/p/asm_x86_bea.c +++ b/libr/asm/p/asm_x86_bea.c @@ -38,7 +38,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 return aop->inst_len; } -static struct r_asm_handle_t r_asm_plugin_x86_bea = { +struct r_asm_handle_t r_asm_plugin_x86_bea = { .name = "asm_x86_bea", .desc = "X86 disassembly plugin (bea engine)", .init = NULL, @@ -47,7 +47,9 @@ static struct r_asm_handle_t r_asm_plugin_x86_bea = { .assemble = NULL }; +#ifndef CORELIB struct r_lib_struct_t radare_plugin = { .type = R_LIB_TYPE_ASM, .data = &r_asm_plugin_x86_bea }; +#endif diff --git a/libr/asm/p/asm_x86_olly.c b/libr/asm/p/asm_x86_olly.c index b595890a76..845fe49fa1 100644 --- a/libr/asm/p/asm_x86_olly.c +++ b/libr/asm/p/asm_x86_olly.c @@ -49,7 +49,7 @@ static int assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf) return aop->inst_len; } -static struct r_asm_handle_t r_asm_plugin_x86_olly = { +struct r_asm_handle_t r_asm_plugin_x86_olly = { .name = "asm_x86_olly", .desc = "X86 disassembly plugin (olly engine)", .init = NULL, @@ -58,8 +58,9 @@ static struct r_asm_handle_t r_asm_plugin_x86_olly = { .assemble = &assemble }; +#ifndef CORELIB struct r_lib_struct_t radare_plugin = { .type = R_LIB_TYPE_ASM, .data = &r_asm_plugin_x86_olly }; - +#endif diff --git a/libr/config.h b/libr/config.h index e3ed68fdfe..fae3899700 100644 --- a/libr/config.h +++ b/libr/config.h @@ -2,7 +2,10 @@ #define _INCLUDE_CONFIG_H_ #define R_ASM_STATIC_PLUGINS \ - &r_asm_plugin_java, &r_asm_plugin_mips, 0 + &r_asm_plugin_java, \ + &r_asm_plugin_x86_olly, \ + &r_asm_plugin_mips, 0 + #define R_BIN_STATIC_PLUGINS \ 0 diff --git a/libr/config.mk b/libr/config.mk index 0c7b4d1c7d..389709a261 100644 --- a/libr/config.mk +++ b/libr/config.mk @@ -6,5 +6,5 @@ WINDOWS=0 USE_RIO=1 # static plugins -STATIC_ASM_PLUGINS=p/java.mk p/mips.mk +STATIC_ASM_PLUGINS=p/x86olly.mk p/mips.mk p/java.mk STATIC_BIN_PLUGINS= diff --git a/libr/include/r_asm.h b/libr/include/r_asm.h index 79fe448b0d..b9c0a16f3b 100644 --- a/libr/include/r_asm.h +++ b/libr/include/r_asm.h @@ -61,4 +61,11 @@ int r_asm_assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf); extern struct r_asm_handle_t r_asm_plugin_bf; extern struct r_asm_handle_t r_asm_plugin_java; extern struct r_asm_handle_t r_asm_plugin_mips; +extern struct r_asm_handle_t r_asm_plugin_x86; +extern struct r_asm_handle_t r_asm_plugin_arm; +extern struct r_asm_handle_t r_asm_plugin_csr; +extern struct r_asm_handle_t r_asm_plugin_m68k; +extern struct r_asm_handle_t r_asm_plugin_x86_bea; +extern struct r_asm_handle_t r_asm_plugin_x86_olly; + #endif diff --git a/libr/lib/lib.c b/libr/lib/lib.c index f052a4dad6..c996461f54 100644 --- a/libr/lib/lib.c +++ b/libr/lib/lib.c @@ -169,17 +169,21 @@ int r_lib_opendir(struct r_lib_t *lib, const char *path) { char file[1024]; struct dirent *de; - DIR *dh = opendir(path); + DIR *dh; + + if (path == NULL) + return R_FALSE; + dh = opendir(path); if (dh == NULL) { IFDBG fprintf(stderr, "Cannot open directory '%s'\n", path); - return -1; + return R_FALSE; } while((de = (struct dirent *)readdir(dh))) { snprintf(file, 1023, "%s/%s", path, de->d_name); r_lib_open(lib, file); } closedir(dh); - return 0; + return R_TRUE; } int r_lib_list(struct r_lib_t *lib) diff --git a/libr/libr.pc.acr b/libr/libr.pc.acr index 6f6c2906af..129f626ad2 100644 --- a/libr/libr.pc.acr +++ b/libr/libr.pc.acr @@ -7,5 +7,5 @@ Name: libr Description: radare framework libraries Version: 0.1 Requires: -Libs: -L${libdir} -lr_core -lr_lang -lr_search -lr_cmd -lr_meta -lr_asm +Libs: -L${libdir} -lr_core -lr_lang -lr_search -lr_cmd -lr_meta -lr_asm -lr_util Cflags: -I${includedir}/libr diff --git a/libr/sign/sign.c b/libr/sign/sign.c index eee939f403..36ae7185ec 100644 --- a/libr/sign/sign.c +++ b/libr/sign/sign.c @@ -18,7 +18,7 @@ int r_sign_set(struct r_sign_item_t *sig, const char *key, const char *value) sig->size = atoi(value); } else if (!strcmp(key, "cksum")) { - sscanf(value, "%x", &sig->csum); + sscanf(value, "%lx", &sig->csum); } return R_TRUE; // fprintf(stderr, "%s:%s\n", key, value); @@ -84,8 +84,8 @@ int r_sign_info(struct r_sign_t *sig) struct r_sign_t *r_sign_free(struct r_sign_t *sig) { free (sig); - struct list_head *pos; - list_for_each_safe(pos, n, head) { + struct list_head *pos, *n; + list_for_each_safe(pos, n, &sig->items) { struct r_sign_item_t *i = list_entry(pos, struct r_sign_item_t, list); free(i->bytes); free(i); diff --git a/libr/syscall/TODO b/libr/syscall/TODO index 5931388f1b..45a7f0e89b 100644 --- a/libr/syscall/TODO +++ b/libr/syscall/TODO @@ -1,3 +1,4 @@ +* Pluggify! * Add w32 0x2e syscall support * include syscalls from file * Added documentation string per-syscall in r_syscall_list_t diff --git a/libr/vapi/libr.deps b/libr/vapi/libr.deps new file mode 100644 index 0000000000..b4da35b7cb --- /dev/null +++ b/libr/vapi/libr.deps @@ -0,0 +1,2 @@ +r_asm +r_util diff --git a/libr/vapi/libr.vapi b/libr/vapi/libr.vapi new file mode 100644 index 0000000000..40a8c178f1 --- /dev/null +++ b/libr/vapi/libr.vapi @@ -0,0 +1 @@ +/* empty */ diff --git a/libr/vapi/r_asm.vapi b/libr/vapi/r_asm.vapi index 3d66f4337e..d3e3a6933e 100644 --- a/libr/vapi/r_asm.vapi +++ b/libr/vapi/r_asm.vapi @@ -27,6 +27,7 @@ namespace Radare { ATT = 2, OLLY = 3 } + [CCode (cprefix="R_ASM_PAR_")] public enum Parser { @@ -35,6 +36,16 @@ namespace Radare { REALLOC = 2 } + [CCode (cname="struct r_asm_aop_t")] + public struct Aop { + public int inst_len; + public uint8 *buf; + public string buf_asm; + public string buf_hex; + public string buf_err; + //pointer + } + public int arch; public int bits; public bool big_endian; @@ -49,14 +60,16 @@ namespace Radare { public Asm(); public int init(); - public bool set_arch(Arch arch); + public int list(); + public bool set(string name); + public bool set_arch(Asm.Arch arch); public bool set_bits(int bits); - public bool set_syntax(Syntax syntax); + public bool set_syntax(Asm.Syntax syntax); public bool set_pc(uint64 addr); public bool set_big_endian(bool big); - public bool set_parser(Parser parser, parse_cb cb, void *aux); - public int disasm(uint8 *buf, uint64 length); - public int asm(string buf); + public bool set_parser(Asm.Parser parser, parse_cb cb, void *aux); + public int disassemble(Aop aop, uint8 *buf, uint64 length); + public int assemble(Aop aop, string buf); public int parse(); } diff --git a/libr/vapi/r_util.vapi b/libr/vapi/r_util.vapi new file mode 100644 index 0000000000..0dd7589551 --- /dev/null +++ b/libr/vapi/r_util.vapi @@ -0,0 +1,11 @@ +/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ + +[CCode (cheader_filename="r_util.h", cprefix="r_util_", lower_case_cprefix="r_util_")] +namespace Radare { + [Compact] + [CCode (cprefix="r_")] + public static class Util { + public static int hex_str2bin(string in, uint8 *buf); + } +} +