* Move buf_hex memcpy in r_asm instead of dupped code in every plugin

- Plugins needs to be synced
* Added x86nasm assembler backend to handle x86-64
  - Not yet working. Pretty buggy :)
* Fix support of building static plugins for r_bp
  - Statically link r_bp_x86 by default
* Implement ugly r_sys_cmd_str() command in r_util
This commit is contained in:
pancake 2009-04-13 22:47:02 +00:00
parent 36771ea713
commit 5f74c8068d
17 changed files with 132 additions and 25 deletions

2
TODO
View File

@ -8,6 +8,8 @@
<{include libr/TODO}>
* Implement rahash2
* Finish and import the spp's getopt owns implementation in r_util
* Add maxrows option for r_print (fix visual problem)

View File

@ -164,8 +164,14 @@ R_API int r_asm_assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, const char
/* find callback if no assembler support in current plugin */
list_for_each_prev(pos, &a->asms) {
struct r_asm_handle_t *h = list_entry(pos, struct r_asm_handle_t, list);
if (h->arch && h->assemble && has_bits(h, a->bits) && !strcmp(a->cur->arch, h->arch))
return h->assemble(a, aop, buf);
if (h->arch && h->assemble && has_bits(h, a->bits) && !strcmp(a->cur->arch, h->arch)) {
int i, ret = h->assemble(a, aop, buf);
if (aop && ret > 0) {
aop->buf_hex[0] = '\0';
for (i=0; i<aop->inst_len; i++)
sprintf(aop->buf_hex, "%s%x", aop->buf_hex, aop->buf[i]);
}
}
}
}
return R_FALSE;

View File

@ -5,7 +5,7 @@ CFLAGS+=-DLIL_ENDIAN=1 -D__UNIX__
foo: all
ALL_TARGETS=
ARCHS=dummy.mk mips.mk sparc.mk java.mk bf.mk arm.mk m68k.mk ppc.mk x86bea.mk x86olly.mk x86.mk csr.mk
ARCHS=dummy.mk mips.mk sparc.mk java.mk bf.mk arm.mk m68k.mk ppc.mk x86bea.mk x86olly.mk x86.mk csr.mk x86nasm.mk
include $(ARCHS)
all: ${ALL_TARGETS}

View File

@ -18,22 +18,13 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
r_hex_bin2str(buf, aop->inst_len, aop->buf_hex);
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)
{
int i;
aop->inst_len = java_assemble(aop->buf, buf);
aop->disasm_obj = NULL;
aop->buf_hex[0] = '\0';
if (aop->inst_len > 0)
for (i=0; i<aop->inst_len; i++)
sprintf(aop->buf_hex, "%s%x", aop->buf_hex, aop->buf[i]);
return aop->inst_len;
}

46
libr/asm/p/asm_x86_nasm.c Normal file
View File

@ -0,0 +1,46 @@
/* radare - LGPL - Copyright 2009 pancake<nopcode.org> */
#include <r_types.h>
#include <r_util.h>
#include <r_lib.h>
#include <r_asm.h>
#if 0
static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len)
{
}
#endif
static int assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, const char *buf)
{
int len = 0;
char cmd[128];
u8 *out;
sprintf(cmd, "nasm /dev/stdin -o /dev/stdout <<__\nBITS 32\n%s\n__", buf);
out = (u8 *)r_sys_cmd_str(cmd, "", &len);
if (out) {
memcpy(aop->buf, out, len);
free(out);
}
aop->inst_len = len;
aop->disasm_obj = NULL;
return len;
}
struct r_asm_handle_t r_asm_plugin_x86_nasm = {
.name = "asm_x86_nasm",
.desc = "X86 nasm assembler plugin",
.arch = "x86",
.bits = (int[]){ 16, 32, 64, 0 },
.init = NULL,
.fini = NULL,
.disassemble = NULL, /*&disassemble,*/
.assemble = &assemble,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_x86_nasm
};
#endif

10
libr/asm/p/x86nasm.mk Normal file
View File

@ -0,0 +1,10 @@
OBJ_X86_NASM=asm_x86_nasm.o
STATIC_OBJ+=${OBJ_X86_NASM}
TARGET_X86_NASM=asm_x86_nasm.so
ALL_TARGETS+=${TARGET_X86_NASM}
${TARGET_X86_NASM}: ${OBJ_X86_NASM}
${CC} ${CFLAGS} -o ${TARGET_X86_NASM} ${OBJ_X86_NASM}
@#strip -s asm_x86.so

View File

@ -81,7 +81,6 @@ static int rasm_asm(char *buf, u64 offset, u64 len, int bin)
return 1;
}
#endif
r_asm_set_pc(&a, offset);
idx = r_asm_massemble(&a, &aop, buf);
if (bin)
@ -132,6 +131,7 @@ int main(int argc, char *argv[])
if (argc<2)
return rasm_show_help();
r_asm_set(&a, "asm_x86");
while ((c = getopt(argc, argv, "a:b:s:do:Bl:hL")) != -1)
{
switch( c ) {
@ -139,7 +139,8 @@ int main(int argc, char *argv[])
arch = optarg;
break;
case 'b':
r_asm_set_bits(&a, r_num_math(NULL, optarg));
ret = r_asm_set_bits(&a, r_num_math(NULL, optarg));
if (!ret) fprintf(stderr, "cannot set bits\n");
break;
case 's':
if (!strcmp(optarg, "att"))
@ -189,7 +190,6 @@ int main(int argc, char *argv[])
fprintf(stderr, "Error: Cannot find asm_x86 plugin\n");
return 1;
}
if (argv[optind]) {
if (!strcmp(argv[optind], "-")) {

View File

@ -1,4 +1,17 @@
NAME=r_bp
OBJ=bp.o
foo: pre libr_bp.so plugins
CFLAGS+=-DCORELIB
include ../config.mk
include ${STATIC_BP_PLUGINS}
STATIC_OBJS=$(subst ..,p/..,$(subst bp_,p/bp_,$(STATIC_OBJ)))
OBJ=bp.o ${STATIC_OBJS}
pre:
if [ ! -e libr_bp.so ]; then rm -f ${STATIC_OBJS} ; fi
plugins:
cd p && ${MAKE} all
include ../rules.mk

View File

@ -1,13 +1,20 @@
/* radare - LGPL - Copyright 2009 pancake<nopcode.org> */
#include <r_bp.h>
#include "../config.h"
static struct r_bp_handle_t *bp_static_plugins[] =
{ R_BP_STATIC_PLUGINS };
R_API int r_bp_init(struct r_bp_t *bp)
{
int i;
bp->nbps = 0;
printf("INIT!!\n");
fprintf(stderr, "bp.init()\n");
bp->cur = NULL;
INIT_LIST_HEAD(&bp->bps);
for(i=0;bp_static_plugins[i];i++)
r_bp_handle_add(bp, bp_static_plugins[i]);
return R_TRUE;
}

View File

@ -16,7 +16,7 @@ static struct r_bp_arch_t r_bp_plugin_arm_bps[] = {
{ 2, 1, (const u8*)"\xdf\xfe" } // arm-thumb-be
};
static struct r_bp_handle_t r_bp_plugin_arm = {
struct r_bp_handle_t r_bp_plugin_arm = {
.name = "bp_arm",
.arch = "arm",
.nbps = 2,

View File

@ -9,7 +9,7 @@ static struct r_bp_arch_t r_bp_plugin_x86_bps[] = {
{ 0, 0, NULL },
};
static struct r_bp_handle_t r_bp_plugin_x86 = {
struct r_bp_handle_t r_bp_plugin_x86 = {
.name = "bp_x86",
.arch = "x86",
.nbps = 2,

View File

@ -23,4 +23,8 @@
&r_bininfo_plugin_addr2line, \
0
#define R_BP_STATIC_PLUGINS \
&r_bp_plugin_x86, \
0
#endif

View File

@ -11,6 +11,7 @@ STATIC_DEBUG=0
RUNTIME_DEBUG=1
STATIC_ASM_PLUGINS=p/x86olly.mk p/mips.mk p/java.mk
STATIC_BIN_PLUGINS=p/elf.mk
STATIC_BP_PLUGINS=p/x86.mk
STATIC_BININFO_PLUGINS=p/addr2line.mk
ifneq (${BINDEPS},)

6
libr/hash/t/rahash2.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main(int argc, char **argv)
{
fprintf(stderr, "TODO\n");
}

View File

@ -64,4 +64,11 @@ R_API int r_bp_getbytes(struct r_bp_t *bp, u8 *buf, int len, int endian, int idx
R_API int r_bp_set_trace(struct r_bp_t *bp, u64 addr, int set);
R_API int r_bp_set_trace_bp(struct r_bp_t *bp, u64 addr, int set);
/* plugin pointers */
extern struct r_bp_handle_t r_bp_plugin_x86;
extern struct r_bp_handle_t r_bp_plugin_arm;
extern struct r_bp_handle_t r_bp_plugin_powerpc;
extern struct r_bp_handle_t r_bp_plugin_mips;
extern struct r_bp_handle_t r_bp_plugin_sparc;
#endif

View File

@ -116,6 +116,6 @@ int r_sys_sleep(int secs);
int r_sys_usleep(int usecs);
R_API const char *r_sys_getenv(const char *key);
R_API int r_sys_setenv(const char *key, const char *value, int ow);
R_API char *r_sys_cmd_str(const char *cmd, const char *input, int *len);
#endif

View File

@ -3,11 +3,6 @@
#include <r_types.h>
/* TODO: import stuff fron bininfo/p/bininfo_addr2line */
R_API char *r_sys_cmd_str(const char *cmd)
{
return NULL;
}
R_API char *r_sys_cmd_strf(const char *cmd, ...)
{
return NULL;
@ -49,3 +44,22 @@ R_API const char *r_sys_getenv(const char *key)
#warning TODO: r_sys_getenv
#endif
}
R_API char *r_sys_cmd_str(const char *cmd, const char *input, int *len)
{
#if __UNIX__
char *b = malloc(1024);
write(1, "((",2);
FILE *fd = popen(cmd, "r");
write(1, "))",2);
*len = 0;
if (fd == NULL)
return NULL;
*len = fread(b, 1, 1024, fd);
pclose(fd);
return b;
#else
#warning NO r_sys_cmd_str support for this platform
return NULL;
#endif
}