Add rasm -r option honor flags and seeks

This commit is contained in:
Sven Steinbauer 2017-05-30 12:19:21 +01:00 committed by radare
parent 1be1050160
commit 4dd740ebf2
3 changed files with 49 additions and 8 deletions

View File

@ -326,6 +326,11 @@ static void print_buf(char *str) {
} else printf ("%s\n", str);
}
static bool print_label(void *user, const char *k, void *v) {
printf ("f label.%s = %s\n", k, v);
return true;
}
static int rasm_asm(const char *buf, ut64 offset, ut64 len, int bits, int bin, bool use_spp) {
RAsmCode *acode;
int i, j, ret = 0;
@ -378,6 +383,26 @@ static int __lib_anal_dt(RLibPlugin *pl, void *p, void *u) {
return true;
}
static int print_assembly_output(const char *buf, ut64 offset, ut64 len, int bits,
int bin, bool use_spp, bool rad, char *arch) {
int ret = 0;
if (rad) {
printf ("e asm.arch=%s\n", arch? arch: R_SYS_ARCH);
printf ("e asm.bits=%d\n", bits);
if (offset) {
printf ("s 0x%"PFMT64x"\n", offset);
}
printf ("wx ");
}
ret = rasm_asm ((char *)buf, offset, len, a->bits, bin, use_spp);
if (rad) {
printf ("f entry = $$\n");
printf ("f label.main = $$ + 1\n");
ht_foreach (a->flags, print_label, NULL);
}
return ret;
}
int main (int argc, char *argv[]) {
const char *path;
const char *env_arch = r_sys_getenv ("RASM2_ARCH");
@ -632,7 +657,8 @@ int main (int argc, char *argv[]) {
} else if (analinfo) {
ret = show_analinfo ((const char *)buf, offset);
} else {
ret = rasm_asm ((char *)buf, offset, len, a->bits, bin, use_spp);
ret = print_assembly_output ((char *)buf, offset, len,
a->bits, bin, use_spp, rad, arch);
}
} else {
content = r_file_slurp (file, &length);
@ -653,7 +679,8 @@ int main (int argc, char *argv[]) {
} else if (analinfo) {
ret = show_analinfo ((const char *)buf, offset);
} else {
ret = rasm_asm (content, offset, length, a->bits, bin, use_spp);
ret = print_assembly_output (content, offset, length,
a->bits, bin, use_spp, rad, arch);
}
ret = !ret;
free (content);
@ -726,12 +753,8 @@ int main (int argc, char *argv[]) {
} else if (analinfo) {
ret = show_analinfo ((const char *)argv[optind], offset);
} else {
if (rad) {
printf ("e asm.arch=%s\n", arch? arch: R_SYS_ARCH);
printf ("e asm.bits=%d\n", bits);
printf ("wx ");
}
ret = rasm_asm (argv[optind], offset, len, a->bits, bin, use_spp);
ret = print_assembly_output (argv[optind], offset, len, a->bits,
bin, use_spp, rad, arch);
}
if (!ret) {
eprintf ("invalid\n");

View File

@ -199,6 +199,7 @@ R_API RAsm *r_asm_free(RAsm *a) {
}
free (a->cpu);
sdb_free (a->pair);
ht_free (a->flags);
a->pair = NULL;
free (a);
}
@ -613,6 +614,17 @@ R_API RAsmCode* r_asm_assemble_file(RAsm *a, const char *file) {
return ac;
}
static void flag_free_kv(HtKv *kv) {
free (kv->key);
free (kv->value);
free (kv);
}
static char* dup_val(void *v) {
char *str = strdup ((char *)v);
return str;
}
R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf) {
int labels = 0, num, stage, ret, idx, ctr, i, j, linenum = 0;
char *lbuf = NULL, *ptr2, *ptr = NULL, *ptr_start = NULL;
@ -623,6 +635,9 @@ R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf) {
if (!buf) {
return NULL;
}
if (!(a->flags = ht_new (dup_val, flag_free_kv, NULL))) {
return NULL;
}
if (!(acode = r_asm_code_new ())) {
return NULL;
}
@ -759,6 +774,8 @@ R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf) {
off += (acode->code_align - (off % acode->code_align));
}
snprintf (food, sizeof (food), "0x%"PFMT64x"", off);
ht_insert (a->flags, ptr_start, food);
// TODO: warning when redefined
r_asm_code_set_equ (acode, ptr_start, food);
}

View File

@ -112,6 +112,7 @@ typedef struct r_asm_t {
int invhex; // invalid instructions displayed in hex
int pcalign;
int dataalign;
SdbHash *flags;
} RAsm;
typedef int (*RAsmModifyCallback)(RAsm *a, ut8 *buf, int field, ut64 val);