Add rasm2 -w to describe opcodes, fix massemble

Add rasm2 -w to get opcode description
Fix multiopcode/multiline comments parsing
Avoid multiple load when there are no sub-bins
Fix r_str_replace_char
This commit is contained in:
pancake 2013-02-24 21:12:30 +01:00
parent e9c38878de
commit 7fbe9071ce
6 changed files with 44 additions and 23 deletions

View file

@ -28,7 +28,7 @@ static int rasm_show_help() {
printf ("rasm2 [-de] [-o offset] [-a arch] [-s syntax] [-f file ..] \"code\"|hex|-\n"
" -d Disassemble from hexpair bytes\n"
" -D Disassemble showing hexpair and opcode\n"
" -f Read data from file\n"
" -f [file] Read data from file\n"
" -F [in:out] Specify input and/or output filters (att2intel, x86.pseudo, ...)\n"
" -o [offset] Set start address for code (default 0)\n"
" -a [arch] Set assemble/disassemble plugin\n"
@ -40,6 +40,7 @@ static int rasm_show_help() {
" -L List supported asm plugins\n"
" -e Use big endian\n"
" -v Show version information\n"
" -w What's this instruction for? describe opcode\n"
" If '-l' value is greater than output length, output is padded with nops\n"
" If the last argument is '-' reads from stdin\n");
return 0;
@ -153,7 +154,7 @@ int main(int argc, char *argv[]) {
char buf[R_ASM_BUFSIZE];
char *arch = NULL, *file = NULL, *filters = NULL;
ut64 offset = 0;
int dis = 0, ascii = 0, bin = 0, ret = 0, bits = 32, c;
int dis = 0, ascii = 0, bin = 0, ret = 0, bits = 32, c, whatsop = 0;
ut64 len = 0, idx = 0;
a = r_asm_new ();
@ -166,7 +167,7 @@ int main(int argc, char *argv[]) {
return rasm_show_help ();
r_asm_use (a, R_SYS_ARCH);
while ((c = getopt (argc, argv, "DCeva:b:s:do:Bl:hLf:F:")) != -1) {
while ((c = getopt (argc, argv, "DCeva:b:s:do:Bl:hLf:F:w")) != -1) {
switch (c) {
case 'D':
dis = 2;
@ -214,6 +215,9 @@ int main(int argc, char *argv[]) {
return 0;
case 'h':
return rasm_show_help ();
case 'w':
whatsop = R_TRUE;
break;
}
}
@ -232,6 +236,15 @@ int main(int argc, char *argv[]) {
//if (!r_asm_set_bits (a, bits))
// eprintf ("WARNING: cannot set asm backend to %d bits\n", bits);
if (whatsop) {
const char *s = r_asm_describe (a, argv[optind]);
if (s) {
printf ("%s\n", s);
return 0;
}
return 1;
}
if (filters) {
char *p = strchr (filters, ':');
if (p) {
@ -247,13 +260,14 @@ int main(int argc, char *argv[]) {
if (file) {
char *content;
int length;
int length = 0;
if (!strcmp (file, "-")) {
ret = read (0, buf, sizeof (buf)-1);
if (ret == R_ASM_BUFSIZE)
eprintf ("rasm2: Cannot slurp all stdin data\n");
if (ret>=0) // only for text
buf[ret] = '\0';
len = ret;
if (dis) ret = rasm_disasm (buf, offset, len,
a->bits, ascii, bin, dis-1);
else ret = rasm_asm (buf, offset, len, a->bits, bin);

View file

@ -414,8 +414,13 @@ R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf) {
lbuf = strdup (buf);
/* accept ';' as comments when input is multiline */
if (strchr (lbuf, '\n'))
r_str_replace_char (lbuf, ';', '#');
{
char *nl = strchr (lbuf, '\n');
if (nl) {
if (strchr (nl+1, '\n'))
r_str_replace_char (lbuf, ';', '#');
}
}
if (strchr (lbuf, ':'))
labels = 1;
@ -425,8 +430,9 @@ R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf) {
(ptr = strchr (tokens[ctr], ';')) ||
(ptr = strchr (tokens[ctr], '\n')) ||
(ptr = strchr (tokens[ctr], '\r'));
tokens[++ctr] = ptr+1)
tokens[++ctr] = ptr+1) {
*ptr = '\0';
}
/* Stage 0-1: Parse labels*/
/* Stage 2: Assemble */

View file

@ -117,6 +117,7 @@ static int r_bin_init_items(RBin *bin, int dummy) {
RBinArch *a = &bin->cur;
RBinObject *o = a->o;
a->curplugin = NULL;
// DEBUG eprintf ("LOAD\n");
r_list_foreach (bin->plugins, it, plugin) {
if ((dummy && !strncmp (plugin->name, "any", 5)) ||
(!dummy && (plugin->check && plugin->check (&bin->cur)))) {
@ -426,6 +427,7 @@ R_API int r_bin_use_arch(RBin *bin, const char *arch, int bits, const char *name
R_API int r_bin_select(RBin *bin, const char *arch, int bits, const char *name) {
int i;
RBinInfo *info;
//if (bin->narch >1) // fix double load when no multiarch bin is loaded
for (i=0; i<bin->narch; i++) {
r_bin_select_idx (bin, i);
info = bin->cur.o->info;

View file

@ -145,14 +145,16 @@ R_API int r_core_bin_load(RCore *r, const char *file) {
return R_FALSE;
file = r->file->filename;
}
if (r_bin_load (r->bin, file, R_FALSE)) {
/* TODO: fat bins are loaded multiple times, this is a problem that must be fixed . see '-->' marks. */
/* r_bin_select, r_bin_select_idx and r_bin_load end up loading the bin */
if (r_bin_load (r->bin, file, R_FALSE)) { // --->
if (r->bin->narch>1 && r_config_get_i (r->config, "scr.prompt")) {
RBinObject *o = r->bin->cur.o;
eprintf ("NOTE: Fat binary found. Selected sub-bin is: -a %s -b %d\n",
r->assembler->cur->arch, r->assembler->bits);
eprintf ("NOTE: Use -a and -b to select sub binary in fat binary\n");
for (i=0; i<r->bin->narch; i++) {
r_bin_select_idx (r->bin, i);
r_bin_select_idx (r->bin, i); // -->
if (o->info) {
eprintf (" $ r2 -a %s -b %d %s # 0x%08"PFMT64x"\n",
o->info->arch,
@ -161,9 +163,8 @@ R_API int r_core_bin_load(RCore *r, const char *file) {
r->bin->cur.offset);
} else eprintf ("No extract info found.\n");
}
r_bin_select (r->bin, r->assembler->cur->arch, r->assembler->bits, NULL); // -->
}
r_bin_select (r->bin, r->assembler->cur->arch, r->assembler->bits, NULL);//"x86_32");
/* Fix for fat bins */
r_list_foreach (r->io->maps, iter, im) {
if (r->bin->cur.size > 0) {
@ -228,7 +229,6 @@ R_API RCoreFile *r_core_file_open(RCore *r, const char *file, int mode, ut64 loa
fh->size = r_io_size (r->io);
r_list_append (r->files, fh);
// r_core_bin_load (r, fh->filename);
cp = r_config_get (r->config, "cmd.open");
if (cp && *cp)
r_core_cmd (r, cp, 0);
@ -236,10 +236,7 @@ R_API RCoreFile *r_core_file_open(RCore *r, const char *file, int mode, ut64 loa
r_config_set_i (r->config, "zoom.to", loadaddr+fh->size);
fh->map = r_io_map_add (r->io, fh->fd->fd, mode, 0, loadaddr, fh->size);
//r_config_set_i (r->config, "io.va", 0);
r_core_block_read (r, 0);
//r_core_bin_load (r, NULL); // XXX: unnecessary call?
//r_core_block_read (r, 0);
return fh;
}

View file

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2007-2012 - pancake */
/* radare - LGPL - Copyright 2007-2013 - pancake */
#include "r_types.h"
#include "r_util.h"
@ -74,14 +74,14 @@ R_API int r_str_replace_char (char *s, int a, int b) {
char *o = s;
for (; *o; s++, o++) {
if (*o==a) {
ret++;
if (b) {
*s = b;
ret++;
break;
} else {
/* remove char */
s--;
}
o++;
}
*s = *o;
} else *s = *o;
}
*s = 0;
return ret;

View file

@ -1,4 +1,4 @@
.Dd Feb 15, 2013
.Dd Feb 24, 2013
.Dt RASM2 1
.Os
.Sh NAME
@ -6,7 +6,7 @@
.Nd radare2 assembler and disassembler tool
.Sh SYNOPSIS
.Nm rasm2
.Op Fl dDfBCLev
.Op Fl dDfBCLevw
.Op Fl F Ar in:out
.Op Fl o Ar offset
.Op Fl a Ar arch
@ -46,6 +46,8 @@ List supported asm plugins
Use big endian
.It Fl h
Show usage help message.
.It Fl w
Describe opcode (whats op)
.El
.Sh EXAMPLES
.Pp