Skip to content

Commit

Permalink
Unify the asm.parse apis workflow ##asm
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae authored Dec 10, 2024
1 parent 9fd64ee commit 8e8b26a
Show file tree
Hide file tree
Showing 32 changed files with 461 additions and 432 deletions.
1 change: 1 addition & 0 deletions doc/fortunes.fun
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,4 @@ Press the button again.
It's been a long time.
Relic malfunction detected.
You are offline. Some functionality may be unavailable.
Patching instructions should be illegal
2 changes: 1 addition & 1 deletion libr/anal/jmptbl.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2010-2023 - nibble, alvaro, pancake, th3str4ng3r */
/* radare - LGPL - Copyright 2010-2024 - nibble, alvaro, pancake, th3str4ng3r */

#include <r_anal.h>

Expand Down
12 changes: 2 additions & 10 deletions libr/arch/p/arm/pseudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ static char *patch(RAsmPluginSession *s, RAnalOp *aop, const char *op) {
return r_core_hack_arm (s, aop, op);
}

static bool subvar(RAsmPluginSession *s, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) {
static char *subvar(RAsmPluginSession *s, RAnalFunction *f, ut64 addr, int oplen, const char *data) {
R_RETURN_VAL_IF_FAIL (s, false);
RAsm *a = s->rasm;
RParse *p = a->parse;
Expand Down Expand Up @@ -679,15 +679,7 @@ static bool subvar(RAsmPluginSession *s, RAnalFunction *f, ut64 addr, int oplen,
r_list_free (bpargs);
r_list_free (spargs);
}
if (len > strlen (tstr)) {
strcpy (str, tstr);
} else {
// TOO BIG STRING CANNOT REPLACE HERE
free (tstr);
return false;
}
free (tstr);
return true;
return tstr;
}

RAsmPlugin r_asm_plugin_arm = {
Expand Down
14 changes: 3 additions & 11 deletions libr/arch/p/mips/pseudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
return true;
}

static bool subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) {
static char *subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int oplen, const char *data) {
RAsm *a = aps->rasm;
RParse *p = a->parse;
RListIter *iter;
Expand Down Expand Up @@ -350,18 +350,10 @@ static bool subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int ople
r_list_free (bpargs);
r_list_free (spargs);
}
bool ret = true;
if (len > strlen (tstr)) {
strcpy (str, tstr);
} else {
// TOO BIG STRING CANNOT REPLACE HERE
ret = false;
}
free (tstr);
return ret;
return tstr;
}

RAsmPlugin r_asm_plugin_mips= {
RAsmPlugin r_asm_plugin_mips = {
.meta = {
.name = "mips",
.desc = "MIPS pseudo syntax",
Expand Down
18 changes: 5 additions & 13 deletions libr/arch/p/tricore/pseudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ static void mk_reg_str(const char *regname, int delta, bool sign, bool att, char
r_strbuf_free (sb);
}

static bool subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) {
static char *subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int oplen, const char *data) {
RAsm *a = aps->rasm;
RParse *p = a->parse;
RAnal *anal = a->analb.anal;
Expand Down Expand Up @@ -435,27 +435,19 @@ static bool subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int ople
r_list_free (spargs);
r_list_free (bpargs);
}

#if 0
char bp[32];
if (anal->reg->name[R_REG_NAME_BP]) {
strncpy (bp, anal->reg->name[R_REG_NAME_BP], sizeof (bp) - 1);
if (isupper ((ut8)*str)) {
if (isupper ((ut8)tstr[0])) {
r_str_case (bp, true);
}
bp[sizeof (bp) - 1] = 0;
} else {
bp[0] = 0;
}

bool ret = true;
if (len > strlen (tstr)) {
strcpy (str, tstr);
} else {
// TOO BIG STRING CANNOT REPLACE HERE
ret = false;
}
free (tstr);
return ret;
#endif
return tstr;
}

static void fini(RAsmPluginSession *aps) {
Expand Down
13 changes: 4 additions & 9 deletions libr/arch/p/v850/pseudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ static int replace(int argc, const char *argv[], char *newstr) {
return false;
}

// UNSAFE
static char *reorder(char *buf) {
char *arr = strstr (buf, "-0x");
if (!arr) {
Expand Down Expand Up @@ -198,7 +199,7 @@ static bool parse(RAsmPluginSession *p, const char *data, char *str) {
return true;
}

static bool subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) {
static char *subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int oplen, const char *data) {
char *r0 = strstr (data, "[r0]");
if (r0) {
char *neg = strstr (data, " -");
Expand All @@ -208,16 +209,10 @@ static bool subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int ople
free (n);
*neg = 0;
ut64 addr = UT32_MAX + negdelta + 1;
char *res = r_str_newf ("%s 0x%"PFMT64x"%s", data, addr, r0 + 4);
strcpy (str, res);
free (res);
return true;
return r_str_newf ("%s 0x%"PFMT64x"%s", data, addr, r0 + 4);
}
}
if (str != data) {
r_str_cpy (str, data);
}
return false;
return NULL;
}

RAsmPlugin r_asm_plugin_v850 = {
Expand Down
13 changes: 5 additions & 8 deletions libr/arch/p/wasm/pseudo.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
/* radare - LGPL - Copyright 2019-2024 - deroad */

#include <r_lib.h>
#include <r_flag.h>
#include <r_anal.h>
#include <r_asm.h>

static char* get_fcn_name(RAnal *anal, ut32 fcn_id) {
const char *s = anal->binb.get_name (anal->binb.bin, 'f', fcn_id, false);
return s? strdup (s): NULL;
}

static bool subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) {
static char *subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int oplen, const char *data) {
char *fcn_name = NULL;
str[0] = 0;
if (!strncmp (data, "call ", 5)) {
if (r_str_startswith (data, "call ")) {
ut32 fcn_id = (ut32) r_num_get (NULL, data + 5);
if (!(fcn_name = get_fcn_name (aps->rasm->analb.anal, fcn_id))) {
return false;
}
snprintf (str, len, "call sym.%s", fcn_name);
char *res = r_str_newf ("call sym.%s", fcn_name);
free (fcn_name);
return true;
return res;
}
return false;
return NULL;
}

RAsmPlugin r_asm_plugin_wasm= {
Expand Down
17 changes: 4 additions & 13 deletions libr/arch/p/x86_nz/pseudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,15 @@ static char *patch(RAsmPluginSession *aps, RAnalOp *aop, const char *op) {
return NULL;
}

static bool subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) {
static char *subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int oplen, const char *data) {
RAsm *a = aps->rasm;
RParse *p = a->parse;
RAnal *anal = a->analb.anal;
RListIter *bpargiter, *spiter;
char oldstr[64], newstr[64];
char *tstr = strdup (data);
if (!tstr) {
return false;
return NULL;
}

bool att = strchr (data, '%');
Expand Down Expand Up @@ -625,23 +625,14 @@ static bool subvar(RAsmPluginSession *aps, RAnalFunction *f, ut64 addr, int ople
char bp[32];
if (anal->reg->name[R_REG_NAME_BP]) {
strncpy (bp, anal->reg->name[R_REG_NAME_BP], sizeof (bp) - 1);
if (isupper ((ut8)*str)) {
if (isupper ((ut8)*tstr)) {
r_str_case (bp, true);
}
bp[sizeof (bp) - 1] = 0;
} else {
bp[0] = 0;
}

bool ret = true;
if (len > strlen (tstr)) {
strcpy (str, tstr);
} else {
// TOO BIG STRING CANNOT REPLACE HERE
ret = false;
}
free (tstr);
return ret;
return tstr;
}

static void fini(RAsmPluginSession *aps) {
Expand Down
1 change: 1 addition & 0 deletions libr/asm/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ R_API int r_asm_disassemble(RAsm *a, RAnalOp *op, const ut8 *buf, int len) {
char *newtext = r_asm_parse_pseudo (a, op->mnemonic);
if (newtext) {
r_anal_op_set_mnemonic (op, op->addr, newtext);
free (newtext);
}
}
int opsz = (op->size > 0)? R_MAX (0, R_MIN (len, op->size)): 1;
Expand Down
Loading

0 comments on commit 8e8b26a

Please sign in to comment.