2011-07-25 10:22:55 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2011 nibble<.ds@gmail.com> */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
#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>
|
|
|
|
|
2010-05-20 15:40:58 +00:00
|
|
|
static struct r_lib_t *l;
|
|
|
|
static struct r_asm_t *a;
|
2010-02-05 11:21:37 +00:00
|
|
|
static int coutput = R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-05-25 22:59:10 +00:00
|
|
|
static void r_asm_list(RAsm *a) {
|
2010-05-25 23:42:22 +00:00
|
|
|
RAsmPlugin *h;
|
2010-05-25 22:59:10 +00:00
|
|
|
RListIter *iter;
|
2010-05-26 00:55:50 +00:00
|
|
|
r_list_foreach (a->plugins, iter, h)
|
2010-08-24 20:16:03 +00:00
|
|
|
printf ("asm %-10s\t %s\n", h->name, h->desc);
|
2010-05-25 22:59:10 +00:00
|
|
|
}
|
|
|
|
|
2009-09-25 02:04:51 +00:00
|
|
|
static int rasm_show_help() {
|
2010-02-12 12:45:03 +00:00
|
|
|
printf ("rasm2 [-e] [-o offset] [-a arch] [-s syntax] -d \"opcode\"|\"hexpairs\"|- [-f file ..]\n"
|
2009-03-08 23:49:15 +00:00
|
|
|
" -d Disassemble from hexpair bytes\n"
|
2010-02-12 12:45:03 +00:00
|
|
|
" -f Read data from file\n"
|
2011-10-05 00:38:37 +00:00
|
|
|
" -F [in:out] Specify input and/or output filters (att2intel, x86.pseudo, ...)\n"
|
2009-03-08 23:49:15 +00:00
|
|
|
" -o [offset] Offset where this opcode is suposed to be\n"
|
|
|
|
" -a [arch] Set architecture plugin\n"
|
|
|
|
" -b [bits] Set architecture bits\n"
|
|
|
|
" -s [syntax] Select syntax (intel, att)\n"
|
2009-04-05 14:02:17 +00:00
|
|
|
" -B Binary input/output (-l is mandatory for binary input)\n"
|
2009-04-14 13:21:19 +00:00
|
|
|
" -l [int] Input/Output length\n"
|
2010-02-05 11:21:37 +00:00
|
|
|
" -C Output in C format\n"
|
2009-04-14 13:21:19 +00:00
|
|
|
" -L List supported asm plugins\n"
|
2009-03-08 23:49:15 +00:00
|
|
|
" -e Use big endian\n"
|
2011-07-25 10:22:55 +00:00
|
|
|
" -v Show version information\n"
|
2009-04-05 14:02:17 +00:00
|
|
|
" If '-l' value is greater than output length, output is padded with nops\n"
|
2009-04-12 23:10:22 +00:00
|
|
|
" If the last argument is '-' reads from stdin\n");
|
2009-04-14 23:19:42 +00:00
|
|
|
return 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-02-12 12:45:03 +00:00
|
|
|
static int rasm_disasm(char *buf, ut64 offset, ut64 len, int ascii, int bin) {
|
2010-01-08 17:25:25 +00:00
|
|
|
struct r_asm_code_t *acode;
|
2009-07-08 11:49:55 +00:00
|
|
|
ut8 *data;
|
2009-02-05 21:08:46 +00:00
|
|
|
char *ptr = buf;
|
|
|
|
int ret = 0;
|
2009-07-08 11:49:55 +00:00
|
|
|
ut64 word = 0, clen = 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-04-05 13:23:36 +00:00
|
|
|
if (bin) {
|
2010-09-15 08:50:43 +00:00
|
|
|
clen = len; // XXX
|
2009-07-08 11:49:55 +00:00
|
|
|
data = (ut8*)buf;
|
2009-04-05 13:23:36 +00:00
|
|
|
} else if (ascii) {
|
2010-02-12 12:45:03 +00:00
|
|
|
clen = strlen (buf);
|
2009-07-08 11:49:55 +00:00
|
|
|
data = (ut8*)buf;
|
2009-04-05 13:23:36 +00:00
|
|
|
} else {
|
2010-09-15 08:50:43 +00:00
|
|
|
for (; *ptr; ptr++)
|
2011-08-10 09:24:15 +00:00
|
|
|
if (*ptr!=' ' && *ptr!='\n' && *ptr!='\r')
|
2010-09-15 08:50:43 +00:00
|
|
|
if (!(++word%2)) clen++;
|
2010-02-12 12:45:03 +00:00
|
|
|
data = alloca (clen);
|
2010-09-10 08:50:53 +00:00
|
|
|
if (r_hex_str2bin (buf, data)==-1)
|
|
|
|
return 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2009-04-05 13:23:36 +00:00
|
|
|
if (!len || clen <= len)
|
|
|
|
len = clen;
|
|
|
|
|
2010-05-20 15:40:58 +00:00
|
|
|
r_asm_set_pc (a, offset);
|
|
|
|
if (!(acode = r_asm_mdisassemble (a, data, len)))
|
2009-04-26 22:34:54 +00:00
|
|
|
return 0;
|
2010-09-10 08:50:53 +00:00
|
|
|
|
2010-02-12 12:45:03 +00:00
|
|
|
printf ("%s\n", acode->buf_asm);
|
2010-01-08 17:25:25 +00:00
|
|
|
ret = acode->len;
|
2010-02-12 12:45:03 +00:00
|
|
|
r_asm_code_free (acode);
|
2009-04-26 22:34:54 +00:00
|
|
|
return ret;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-02-05 11:21:37 +00:00
|
|
|
static void print_buf(char *str) {
|
|
|
|
int i;
|
|
|
|
if (coutput) {
|
|
|
|
printf ("\"");
|
|
|
|
for (i=1; *str; str+=2, i+=2) {
|
|
|
|
if (!(i%41)) {
|
|
|
|
printf ("\" \\\n\"");
|
|
|
|
i=1;
|
|
|
|
}
|
|
|
|
printf ("\\x%c%c", *str, str[1]);
|
|
|
|
}
|
|
|
|
printf ("\"\n");
|
|
|
|
} else printf ("%s\n", str);
|
|
|
|
}
|
|
|
|
|
2010-02-12 12:45:03 +00:00
|
|
|
static int rasm_asm(char *buf, ut64 offset, ut64 len, int bin) {
|
2010-01-08 17:25:25 +00:00
|
|
|
struct r_asm_code_t *acode;
|
2011-02-24 15:50:29 +00:00
|
|
|
struct r_asm_op_t op;
|
2009-04-08 23:03:49 +00:00
|
|
|
int ret, idx, i;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-05-20 15:40:58 +00:00
|
|
|
r_asm_set_pc (a, offset);
|
|
|
|
if (!(acode = r_asm_massemble (a, buf)))
|
2009-04-14 23:19:42 +00:00
|
|
|
return 0;
|
2009-04-08 23:03:49 +00:00
|
|
|
if (bin)
|
2010-01-08 17:25:25 +00:00
|
|
|
for (i = 0; i < acode->len; i++)
|
2010-02-05 11:21:37 +00:00
|
|
|
printf ("%c", acode->buf[i]);
|
|
|
|
else print_buf (acode->buf_hex);
|
2010-01-08 17:25:25 +00:00
|
|
|
for (ret = 0, idx = acode->len; idx < len; idx+=ret) {
|
2011-02-24 15:50:29 +00:00
|
|
|
if (!(ret = r_asm_assemble (a, &op, "nop")))
|
2009-04-05 14:02:17 +00:00
|
|
|
return 0;
|
2009-04-14 23:19:42 +00:00
|
|
|
if (bin)
|
|
|
|
for (i = 0; i < ret; i++)
|
2011-02-24 15:50:29 +00:00
|
|
|
printf ("%c", op.buf[i]);
|
|
|
|
else print_buf (op.buf_hex);
|
2009-04-05 14:02:17 +00:00
|
|
|
}
|
2010-02-12 12:45:03 +00:00
|
|
|
if (!bin && len && idx == len) printf ("\n");
|
|
|
|
r_asm_code_free (acode);
|
2009-04-05 13:23:36 +00:00
|
|
|
return idx;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2009-02-18 02:47:40 +00:00
|
|
|
/* asm callback */
|
2010-09-15 08:50:43 +00:00
|
|
|
static int __lib_asm_cb(struct r_lib_plugin_t *pl, void *user, void *data) {
|
2010-05-25 23:42:22 +00:00
|
|
|
RAsmPlugin *hand = (struct r_asm_plugin_t *)data;
|
2010-05-20 15:40:58 +00:00
|
|
|
r_asm_add (a, hand);
|
2009-02-18 02:47:40 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
static int __lib_asm_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; }
|
|
|
|
|
2010-09-15 08:50:43 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2011-10-05 00:38:37 +00:00
|
|
|
char *arch = NULL, *file = NULL, *filters = NULL;
|
2009-07-08 11:49:55 +00:00
|
|
|
ut64 offset = 0x8048000;
|
2009-04-14 13:21:19 +00:00
|
|
|
int dis = 0, ascii = 0, bin = 0, ret = 0, bits = 32, c;
|
2010-02-12 17:40:05 +00:00
|
|
|
ut64 len = 0, idx = 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-05-20 15:40:58 +00:00
|
|
|
a = r_asm_new ();
|
|
|
|
l = r_lib_new ("radare_plugin");
|
|
|
|
r_lib_add_handler (l, R_LIB_TYPE_ASM, "(dis)assembly plugins",
|
2009-02-18 02:47:40 +00:00
|
|
|
&__lib_asm_cb, &__lib_asm_dt, NULL);
|
2010-05-20 15:40:58 +00:00
|
|
|
r_lib_opendir (l, r_sys_getenv ("LIBR_PLUGINS"));
|
2009-02-18 02:47:40 +00:00
|
|
|
|
2009-02-18 15:20:14 +00:00
|
|
|
if (argc<2)
|
2010-02-05 11:21:37 +00:00
|
|
|
return rasm_show_help ();
|
2009-02-18 15:20:14 +00:00
|
|
|
|
2010-05-20 15:40:58 +00:00
|
|
|
r_asm_use (a, "x86"); // XXX: do not harcode default arch
|
2011-10-05 00:38:37 +00:00
|
|
|
while ((c = getopt (argc, argv, "Ceva:b:s:do:Bl:hLf:F:")) != -1) {
|
2009-09-25 02:04:51 +00:00
|
|
|
switch (c) {
|
2010-02-12 12:45:03 +00:00
|
|
|
case 'f':
|
2010-02-12 17:40:05 +00:00
|
|
|
file = optarg;
|
2010-02-12 12:45:03 +00:00
|
|
|
break;
|
2011-10-05 00:38:37 +00:00
|
|
|
case 'F':
|
|
|
|
filters = optarg;
|
|
|
|
break;
|
2010-02-05 11:21:37 +00:00
|
|
|
case 'C':
|
|
|
|
coutput = R_TRUE;
|
|
|
|
break;
|
2009-02-05 21:08:46 +00:00
|
|
|
case 'a':
|
2009-02-19 17:13:34 +00:00
|
|
|
arch = optarg;
|
2009-02-19 13:24:51 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
2010-02-05 11:21:37 +00:00
|
|
|
bits = r_num_math (NULL, optarg);
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2010-02-05 11:21:37 +00:00
|
|
|
if (!strcmp (optarg, "att"))
|
2010-05-20 15:40:58 +00:00
|
|
|
r_asm_set_syntax (a, R_ASM_SYNTAX_ATT);
|
|
|
|
else r_asm_set_syntax (a, R_ASM_SYNTAX_INTEL);
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dis = 1;
|
|
|
|
break;
|
|
|
|
case 'o':
|
2010-02-12 12:45:03 +00:00
|
|
|
offset = r_num_math (NULL, optarg);
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
2009-04-05 13:23:36 +00:00
|
|
|
case 'B':
|
|
|
|
bin = 1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
2010-02-05 11:21:37 +00:00
|
|
|
len = r_num_math (NULL, optarg);
|
2009-04-05 13:23:36 +00:00
|
|
|
break;
|
2009-04-11 21:24:37 +00:00
|
|
|
case 'L':
|
2010-05-20 15:40:58 +00:00
|
|
|
r_asm_list (a);
|
2010-02-12 12:45:03 +00:00
|
|
|
exit (1);
|
2009-02-05 21:08:46 +00:00
|
|
|
case 'e':
|
2010-05-20 15:40:58 +00:00
|
|
|
r_asm_set_big_endian (a, R_TRUE);
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
2011-07-25 10:22:55 +00:00
|
|
|
case 'v':
|
2010-07-15 11:34:53 +00:00
|
|
|
printf ("rasm2 v"R2_VERSION"\n");
|
2009-09-25 02:04:51 +00:00
|
|
|
return 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
case 'h':
|
2010-02-05 11:21:37 +00:00
|
|
|
return rasm_show_help ();
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-19 17:13:34 +00:00
|
|
|
if (arch) {
|
2010-05-20 15:40:58 +00:00
|
|
|
if (!r_asm_use (a, arch)) {
|
2010-02-12 12:45:03 +00:00
|
|
|
eprintf ("Error: Unknown asm plugin '%s'\n", arch);
|
2009-04-14 23:19:42 +00:00
|
|
|
return 0;
|
2009-02-19 17:13:34 +00:00
|
|
|
}
|
2010-02-12 12:45:03 +00:00
|
|
|
if (!strcmp (arch, "bf"))
|
2010-01-08 17:25:25 +00:00
|
|
|
ascii = 1;
|
2010-05-20 15:40:58 +00:00
|
|
|
} else if (!r_asm_use (a, "x86")) {
|
2010-02-05 11:21:37 +00:00
|
|
|
eprintf ("Error: Cannot find asm.x86 plugin\n");
|
2009-04-14 23:19:42 +00:00
|
|
|
return 0;
|
2009-02-19 17:13:34 +00:00
|
|
|
}
|
2010-05-20 15:40:58 +00:00
|
|
|
if (!r_asm_set_bits (a, bits))
|
2010-09-08 17:49:34 +00:00
|
|
|
eprintf ("WARNING: cannot set asm backend to %d bits\n", bits);
|
2009-02-19 17:13:34 +00:00
|
|
|
|
2011-10-05 00:38:37 +00:00
|
|
|
if (filters) {
|
|
|
|
char *p = strchr (filters, ':');
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
if (*filters) r_asm_filter_input (a, filters);
|
|
|
|
if (p[1]) r_asm_filter_output (a, p+1);
|
|
|
|
*p = ':';
|
|
|
|
} else {
|
|
|
|
if (dis) r_asm_filter_output (a, filters);
|
|
|
|
else r_asm_filter_input (a, filters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-12 17:40:05 +00:00
|
|
|
if (file) {
|
|
|
|
char *content;
|
|
|
|
int length;
|
2010-02-15 21:59:26 +00:00
|
|
|
if (!strcmp (file, "-")) {
|
|
|
|
char buf[R_ASM_BUFSIZE]; // TODO: Fix this limitation
|
2011-08-10 09:24:15 +00:00
|
|
|
ret = fread (buf, 1, sizeof (buf)-1, stdin);
|
2010-02-15 21:59:26 +00:00
|
|
|
if (ret == R_ASM_BUFSIZE)
|
|
|
|
eprintf ("WARNING: Cannot slurp more from stdin\n");
|
|
|
|
if (ret>=0)
|
|
|
|
buf[ret] = '\0';
|
|
|
|
if (dis) ret = rasm_disasm (buf, offset, len, ascii, bin);
|
|
|
|
else ret = rasm_asm (buf, offset, len, bin);
|
|
|
|
} else {
|
|
|
|
content = r_file_slurp (file, &length);
|
|
|
|
if (content) {
|
|
|
|
content[length] = '\0';
|
|
|
|
if (dis) ret = rasm_disasm (content, offset, len, ascii, bin);
|
|
|
|
else ret = rasm_asm (content, offset, len, bin);
|
|
|
|
free (content);
|
|
|
|
} else eprintf ("Cannot open file %s\n", file);
|
|
|
|
}
|
2010-02-12 17:40:05 +00:00
|
|
|
} else if (argv[optind]) {
|
|
|
|
if (!strcmp (argv[optind], "-")) {
|
|
|
|
char buf[R_ASM_BUFSIZE];
|
|
|
|
for (;;) {
|
2011-08-10 09:24:15 +00:00
|
|
|
fgets (buf, sizeof (buf)-1, stdin);
|
2010-02-12 17:40:05 +00:00
|
|
|
if ((!bin || !dis) && feof (stdin))
|
|
|
|
break;
|
2011-08-10 09:24:15 +00:00
|
|
|
if (!bin || !dis) buf[strlen (buf)-1]='\0';
|
2010-02-12 17:40:05 +00:00
|
|
|
if (dis) ret = rasm_disasm (buf, offset, len, ascii, bin);
|
|
|
|
else ret = rasm_asm (buf, offset, len, bin);
|
|
|
|
idx += ret;
|
|
|
|
offset += ret;
|
|
|
|
if (!ret) {
|
|
|
|
eprintf ("invalid\n");
|
|
|
|
return 0;
|
2009-04-14 23:19:42 +00:00
|
|
|
}
|
2010-02-12 17:40:05 +00:00
|
|
|
if (len && idx >= len)
|
|
|
|
break;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-02-12 17:40:05 +00:00
|
|
|
return idx;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-02-12 17:40:05 +00:00
|
|
|
if (dis) ret = rasm_disasm (argv[optind], offset, len, ascii, bin);
|
|
|
|
else ret = rasm_asm (argv[optind], offset, len, bin);
|
2010-09-15 08:50:43 +00:00
|
|
|
if (!ret) eprintf ("invalid\n");
|
2009-04-14 23:19:42 +00:00
|
|
|
return ret;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|