- Fixed initialization
  - Fixed assembly fallbacks
  - Removed dupped memcpy's in disassembly functions
  - Added the directive ORG to asm_x86_nasm
  - Refactoring
* rabin2
  - Added -L to list supported plugins
This commit is contained in:
Nibble 2009-04-14 15:21:19 +02:00
parent 5f74c8068d
commit 724d900e62
16 changed files with 40 additions and 79 deletions

View file

@ -40,10 +40,10 @@ R_API int r_asm_init(struct r_asm_t *a)
a->user = NULL;
a->cur = 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);
a->bits = 32;
a->big_endian = 0;
a->syntax = R_ASM_SYN_INTEL;
a->pc = 0;
for(i=0;asm_static_plugins[i];i++)
r_asm_add(a, asm_static_plugins[i]);
r_cmd_init(&a->cmd);
@ -117,7 +117,7 @@ static int has_bits(struct r_asm_handle_t *h, int bits)
R_API int r_asm_set_bits(struct r_asm_t *a, int bits)
{
if ( has_bits(a->cur, bits) ) {
if (has_bits(a->cur, bits)) {
a->bits = bits;
return R_TRUE;
}
@ -150,31 +150,36 @@ R_API int r_asm_set_pc(struct r_asm_t *a, u64 pc)
R_API int r_asm_disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len)
{
int ret = 0;
if (a->cur && a->cur->disassemble)
return a->cur->disassemble(a, aop, buf, len);
return R_FALSE;
ret = a->cur->disassemble(a, aop, buf, len);
if (ret > 0) {
memcpy(aop->buf, buf, ret);
r_hex_bin2str(buf, ret, aop->buf_hex);
}
return ret;
}
R_API int r_asm_assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, const char *buf)
{
int ret = 0;
struct list_head *pos;
if (a->cur) {
if (a->cur->assemble)
return a->cur->assemble(a, aop, buf);
/* 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)) {
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]);
ret = a->cur->assemble(a, aop, buf);
else /* 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)) {
printf("NAME %s\n", h->name);
ret = h->assemble(a, aop, buf);
break;
}
}
}
}
return R_FALSE;
if (aop && ret > 0)
r_hex_bin2str(aop->buf, ret, aop->buf_hex);
return ret;
}
R_API int r_asm_massemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf)

View file

@ -63,7 +63,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
buf_global = aop->buf_asm;
Offset = a->pc;
memcpy(bytes, buf, 4); // TODO handle thumb
r_hex_bin2str(bytes, 4, aop->buf_hex);
/* prepare disassembler */
memset(&disasm_obj,'\0', sizeof(struct disassemble_info));
@ -85,9 +84,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
if (aop->inst_len == -1)
strcpy(aop->buf_asm, " (data)");
if (aop->inst_len > 0)
memcpy(aop->buf, buf, aop->inst_len);
return aop->inst_len;
}

View file

@ -19,8 +19,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
for(i=0;b[0] == b[1] && i<len; b=b+1,i++); b[1] = '\0';
strncpy(aop->buf_hex, buf_cp, 256);
switch(buf[0]) {
case '[':
strcpy(aop->buf_asm, "[ loop {");

View file

@ -12,9 +12,7 @@
static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len)
{
r_hex_bin2str((u8*)buf, 2, aop->buf_hex);
arch_csr_disasm(aop->buf_asm, buf, a->pc);
memcpy(aop->buf, buf, 2);
aop->inst_len = 2;
aop->disasm_obj = NULL;

View file

@ -14,10 +14,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
aop->inst_len = java_disasm(buf, aop->buf_asm);
aop->disasm_obj = NULL;
if (aop->inst_len > 0) {
r_hex_bin2str(buf, aop->inst_len, aop->buf_hex);
memcpy(aop->buf, buf, aop->inst_len);
}
return aop->inst_len;
}

View file

@ -28,8 +28,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
M68k_Disassemble(&dp);
aop->disasm_obj = &dp;
sprintf(aop->buf_asm, "%s %s", opcode, operands);
r_hex_bin2str((u8*)bof, 4, aop->buf_hex);
memcpy(aop->buf, bof, 4);
aop->inst_len = 4;
return aop->inst_len;

View file

@ -63,7 +63,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
buf_global = aop->buf_asm;
Offset = a->pc;
memcpy(bytes, buf, 4); // TODO handle thumb
r_hex_bin2str(bytes, 4, aop->buf_hex);
/* prepare disassembler */
memset(&disasm_obj,'\0', sizeof(struct disassemble_info));
@ -89,9 +88,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
if (aop->inst_len == -1)
strcpy(aop->buf_asm, " (data)");
if (aop->inst_len > 0)
memcpy(aop->buf, buf, aop->inst_len);
return aop->inst_len;
}

View file

@ -27,9 +27,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
dp.instr = bof;
PPC_Disassemble(&dp, a->big_endian);
aop->disasm_obj = &dp;
r_hex_bin2str((u8*)bof, 4, aop->buf_hex);
sprintf(aop->buf_asm, "%s %s", opcode, operands);
memcpy(aop->buf, bof, 4);
aop->inst_len = 4;
return aop->inst_len;

View file

@ -63,7 +63,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
buf_global = aop->buf_asm;
Offset = a->pc;
memcpy(bytes, buf, 4); // TODO handle thumb
r_hex_bin2str(bytes, 4, aop->buf_hex);
/* prepare disassembler */
memset(&disasm_obj,'\0', sizeof(struct disassemble_info));
@ -84,9 +83,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
if (aop->inst_len == -1)
strcpy(aop->buf_asm, " (data)");
if (aop->inst_len > 0)
memcpy(aop->buf, buf, aop->inst_len);
return aop->inst_len;
}

View file

@ -27,10 +27,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
aop->disasm_obj = &disasm_obj;
aop->inst_len = ud_insn_len(&disasm_obj);
snprintf(aop->buf_asm, 256, "%s", ud_insn_asm(&disasm_obj));
snprintf(aop->buf_hex, 256, "%s", ud_insn_hex(&disasm_obj));
if (aop->inst_len > 0)
memcpy(aop->buf, buf, aop->inst_len);
return aop->inst_len;
}

View file

@ -30,11 +30,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
snprintf(aop->buf_asm, 256, disasm_obj.CompleteInstr);
if (aop->inst_len > 0) {
r_hex_bin2str(buf, aop->inst_len, aop->buf_hex);
memcpy(aop->buf, buf, aop->inst_len);
}
return aop->inst_len;
}

View file

@ -16,7 +16,7 @@ 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);
sprintf(cmd, "nasm /dev/stdin -o /dev/stdout <<__\nBITS %i\nORG 0x%llx\n%s\n__", a->bits, a->pc, buf);
out = (u8 *)r_sys_cmd_str(cmd, "", &len);
if (out) {
memcpy(aop->buf, out, len);

View file

@ -17,10 +17,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
lowercase=1;
aop->inst_len = Disasm_olly(buf, len, a->pc, &disasm_obj, DISASM_FILE);
snprintf(aop->buf_asm, 256, "%s", disasm_obj.result);
snprintf(aop->buf_hex, 256, "%s", disasm_obj.dump);
if (aop->inst_len > 0)
memcpy(aop->buf, buf, aop->inst_len);
return aop->inst_len;
}
@ -31,16 +27,14 @@ static int assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, const char *buf)
int idx;
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, 0, 0, aop->buf_err);
/* constsize == 0: Address constants and inmediate data of 16/32b */
aop->inst_len = Assemble((char*)buf, a->pc, &asm_obj, 0, 0, aop->buf_err);
aop->disasm_obj = &asm_obj;
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.code[idx]);
snprintf(aop->buf_asm, 1024, "%s", buf);
}
if (aop->inst_len > 0)

View file

@ -22,12 +22,11 @@ static int rasm_show_help()
" -b [bits] Set architecture bits\n"
" -s [syntax] Select syntax (intel, att)\n"
" -B Binary input/output (-l is mandatory for binary input)\n"
" -l [int] input/output length\n"
" -L list supported asm plugins\n"
" -l [int] Input/Output length\n"
" -L List supported asm plugins\n"
" -e Use big endian\n"
" If '-l' value is greater than output length, output is padded with nops\n"
" If the last argument is '-' reads from stdin\n");
//r_asm_list(&a);
return R_TRUE;
}
@ -119,7 +118,7 @@ int main(int argc, char *argv[])
{
char *arch = NULL;
u64 offset = 0x8048000;
int dis = 0, ascii = 0, bin = 0, ret = 0, c;
int dis = 0, ascii = 0, bin = 0, ret = 0, bits = 32, c;
u64 len = 0, idx = 0;
r_asm_init(&a);
@ -139,8 +138,7 @@ int main(int argc, char *argv[])
arch = optarg;
break;
case 'b':
ret = r_asm_set_bits(&a, r_num_math(NULL, optarg));
if (!ret) fprintf(stderr, "cannot set bits\n");
bits = r_num_math(NULL, optarg);
break;
case 's':
if (!strcmp(optarg, "att"))
@ -181,15 +179,12 @@ int main(int argc, char *argv[])
free (str);
if (!strcmp(arch, "bf"))
ascii = 1;
} else if (!dis) {
if (!r_asm_set(&a, "asm_x86_olly")) {
fprintf(stderr, "Error: Cannot find asm_x86_olly plugin\n");
return 1;
}
} else if (!r_asm_set(&a, "asm_x86")) {
fprintf(stderr, "Error: Cannot find asm_x86 plugin\n");
return 1;
}
if (!r_asm_set_bits(&a, bits))
fprintf(stderr, "cannot set bits (triying with 32)\n");
if (argv[optind]) {
if (!strcmp(argv[optind], "-")) {

View file

@ -44,9 +44,8 @@ static int rabin_show_help()
" -o [str] Operation action (str=help for help)\n"
" -f [format] Override file format autodetection\n"
" -r Radare output\n"
" -h This help\n"
"Available plugins:\n");
r_bin_list(&bin);
" -L List supported bin plugins\n"
" -h This help\n");
return R_TRUE;
}
@ -351,7 +350,7 @@ int main(int argc, char **argv)
r_lib_opendir(&l, LIBDIR"/radare2/");
}
while ((c = getopt(argc, argv, "isSzIeo:f:rvh")) != -1)
while ((c = getopt(argc, argv, "isSzIeo:f:rvLh")) != -1)
{
switch( c ) {
case 'i':
@ -386,6 +385,9 @@ int main(int argc, char **argv)
case 'v':
verbose++;
break;
case 'L':
r_bin_list(&bin);
exit(1);
case 'h':
default:
action |= ACTION_HELP;

View file

@ -49,9 +49,7 @@ 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;