2009-02-05 21:08:46 +00:00
|
|
|
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_asm.h>
|
|
|
|
#include <r_util.h>
|
2009-02-18 02:47:40 +00:00
|
|
|
#include <r_lib.h>
|
|
|
|
|
|
|
|
|
|
|
|
static struct r_lib_t l;
|
|
|
|
static struct r_asm_t a;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
static int rasm_show_help()
|
|
|
|
{
|
|
|
|
printf( "rasm2 [-e] [-o offset] [-a arch] [-s syntax] -d \"opcode\"|\"hexpairs\"|-\n"
|
|
|
|
" -d Disassemble from hexpair bytes\n"
|
|
|
|
" -o [offset] Offset where this opcode is suposed to be\n"
|
2009-02-19 13:24:51 +00:00
|
|
|
" -a [arch] Set architecture plugin\n"
|
|
|
|
" -b [bits] Set architecture bits\n"
|
2009-02-19 15:41:51 +00:00
|
|
|
" -s [syntax] Select syntax (intel, att)\n"
|
2009-02-05 21:08:46 +00:00
|
|
|
" -e Use big endian\n"
|
2009-02-18 15:20:14 +00:00
|
|
|
" If the last argument is '-' reads from stdin\n\n"
|
|
|
|
"Available plugins:\n");
|
|
|
|
r_asm_list(&a);
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-02-19 13:24:51 +00:00
|
|
|
static int rasm_disasm(char *buf, u64 offset, int str)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-02-18 02:47:40 +00:00
|
|
|
struct r_asm_aop_t aop;
|
2009-02-05 21:08:46 +00:00
|
|
|
u8 *data;
|
|
|
|
char *ptr = buf;
|
|
|
|
int ret = 0;
|
|
|
|
u64 idx = 0, word = 0, len = 0;
|
|
|
|
|
2009-02-19 13:24:51 +00:00
|
|
|
if (!str) {
|
2009-02-18 12:59:57 +00:00
|
|
|
while(ptr[0]) {
|
|
|
|
if (ptr[0]!= ' ')
|
|
|
|
if (0==(++word%2))len++;
|
|
|
|
ptr += 1;
|
|
|
|
}
|
|
|
|
data = alloca(len);
|
|
|
|
r_hex_str2bin(buf, data);
|
|
|
|
} else {
|
|
|
|
len = strlen(buf);
|
2009-02-18 15:20:14 +00:00
|
|
|
data = (u8*)buf;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (idx < len) {
|
2009-02-19 13:24:51 +00:00
|
|
|
r_asm_set_pc(&a, offset + idx);
|
2009-02-18 02:47:40 +00:00
|
|
|
ret = r_asm_disassemble(&a, &aop, data+idx, len-idx);
|
2009-02-05 21:08:46 +00:00
|
|
|
idx += ret;
|
2009-02-18 02:47:40 +00:00
|
|
|
printf("%s\n", aop.buf_asm);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (int)idx;
|
|
|
|
}
|
|
|
|
|
2009-02-19 13:24:51 +00:00
|
|
|
static int rasm_asm(char *buf, u64 offset)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-02-18 02:47:40 +00:00
|
|
|
struct r_asm_aop_t aop;
|
2009-02-05 21:08:46 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
|
|
/* TODO: Arch, syntax... */
|
2009-02-19 17:13:34 +00:00
|
|
|
if (!r_asm_set(&a, "asm_x86_olly")) {
|
|
|
|
fprintf(stderr, "Error: Cannot find asm_x86 plugin\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
r_asm_set_pc(&a, offset);
|
|
|
|
|
2009-02-18 02:47:40 +00:00
|
|
|
ret = r_asm_assemble(&a, &aop, buf);
|
2009-02-05 21:08:46 +00:00
|
|
|
if (!ret)
|
|
|
|
printf("invalid\n");
|
2009-02-18 02:47:40 +00:00
|
|
|
else printf("%s\n", aop.buf_hex);
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-18 02:47:40 +00:00
|
|
|
/* asm callback */
|
|
|
|
static 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;
|
|
|
|
//printf(" * Added (dis)assembly handler\n");
|
|
|
|
r_asm_add(&a, hand);
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
static int __lib_asm_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; }
|
|
|
|
|
2009-02-05 21:08:46 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2009-02-19 17:13:34 +00:00
|
|
|
char *arch = NULL;
|
2009-02-19 13:24:51 +00:00
|
|
|
u64 offset = 0x8048000;
|
|
|
|
int dis = 0, str = 0, c;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-02-18 02:47:40 +00:00
|
|
|
r_asm_init(&a);
|
|
|
|
r_lib_init(&l, "radare_plugin");
|
|
|
|
r_lib_add_handler(&l, R_LIB_TYPE_ASM, "(dis)assembly plugins",
|
|
|
|
&__lib_asm_cb, &__lib_asm_dt, NULL);
|
|
|
|
r_lib_opendir(&l, getenv("LIBR_PLUGINS"));
|
|
|
|
|
2009-02-18 15:20:14 +00:00
|
|
|
if (argc<2)
|
|
|
|
return rasm_show_help();
|
|
|
|
|
2009-02-19 13:24:51 +00:00
|
|
|
while ((c = getopt(argc, argv, "da:b:s:o:h")) != -1)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
|
|
|
switch( c ) {
|
|
|
|
case 'a':
|
2009-02-19 17:13:34 +00:00
|
|
|
arch = optarg;
|
2009-02-19 13:24:51 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
r_asm_set_bits(&a, r_num_math(NULL, optarg));
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2009-02-19 13:24:51 +00:00
|
|
|
if (!strcmp(optarg, "att"))
|
|
|
|
r_asm_set_syntax(&a, R_ASM_SYN_ATT);
|
|
|
|
else r_asm_set_syntax(&a, R_ASM_SYN_INTEL);
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dis = 1;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
offset = r_num_math(NULL, optarg);
|
|
|
|
break;
|
|
|
|
case 'e':
|
2009-02-19 13:24:51 +00:00
|
|
|
r_asm_set_big_endian(&a, R_TRUE);
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
return rasm_show_help();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-19 17:13:34 +00:00
|
|
|
if (arch) {
|
|
|
|
if (!r_asm_set(&a, arch)) {
|
|
|
|
fprintf(stderr, "Error: Unknown plugin\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcmp(arch, "asm_bf"))
|
|
|
|
str = 1;
|
|
|
|
} else if (!r_asm_set(&a, "asm_x86")) {
|
|
|
|
fprintf(stderr, "Error: Cannot find asm_x86 plugin\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-05 21:08:46 +00:00
|
|
|
if (argv[optind]) {
|
|
|
|
if (!strcmp(argv[optind], "-")) {
|
|
|
|
char buf[1024];
|
|
|
|
for(;;) {
|
|
|
|
fgets(buf, 1024, stdin);
|
|
|
|
if (feof(stdin))
|
|
|
|
break;
|
|
|
|
buf[strlen(buf)-1]='\0';
|
|
|
|
if (dis)
|
2009-02-19 13:24:51 +00:00
|
|
|
offset += rasm_disasm(buf, offset, str);
|
|
|
|
else offset += rasm_asm(buf, offset);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (dis)
|
2009-02-19 13:24:51 +00:00
|
|
|
return rasm_disasm(argv[optind], offset, str);
|
|
|
|
else return rasm_asm(argv[optind], offset);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|