Implement asm.relsub

This commit is contained in:
pancake 2015-11-19 23:12:08 +01:00
parent 0f94d1c562
commit 6027d6e44e
6 changed files with 27 additions and 19 deletions

View File

@ -1328,6 +1328,7 @@ R_API int r_core_config_init(RCore *core) {
SETPREF("asm.vars", "true", "Show local function variables in disassembly"); SETPREF("asm.vars", "true", "Show local function variables in disassembly");
SETPREF("asm.varxs", "false", "Show accesses of local variables"); SETPREF("asm.varxs", "false", "Show accesses of local variables");
SETPREF("asm.varsub", "true", "Substitute variables in disassembly"); SETPREF("asm.varsub", "true", "Substitute variables in disassembly");
SETPREF("asm.relsub", "false", "Substitute pc relative expressions in disasm");
SETPREF("asm.cmtfold", "false", "Fold comments, toggle with Vz"); SETPREF("asm.cmtfold", "false", "Fold comments, toggle with Vz");
SETPREF("asm.family", "false", "Show family name in disasm"); SETPREF("asm.family", "false", "Show family name in disasm");
SETCB("asm.arch", R_SYS_ARCH, &cb_asmarch, "Set the arch to be used by asm"); SETCB("asm.arch", R_SYS_ARCH, &cb_asmarch, "Set the arch to be used by asm");

View File

@ -290,6 +290,7 @@ static RDisasmState * handle_init_ds (RCore * core) {
ds->filter = r_config_get_i (core->config, "asm.filter"); ds->filter = r_config_get_i (core->config, "asm.filter");
ds->interactive = r_config_get_i (core->config, "scr.interactive"); ds->interactive = r_config_get_i (core->config, "scr.interactive");
ds->varsub = r_config_get_i (core->config, "asm.varsub"); ds->varsub = r_config_get_i (core->config, "asm.varsub");
core->parser->relsub = r_config_get_i (core->config, "asm.relsub");
ds->vars = r_config_get_i (core->config, "asm.vars"); ds->vars = r_config_get_i (core->config, "asm.vars");
ds->varxs = r_config_get_i (core->config, "asm.varxs"); ds->varxs = r_config_get_i (core->config, "asm.varxs");
ds->maxrefs = r_config_get_i (core->config, "asm.maxrefs"); ds->maxrefs = r_config_get_i (core->config, "asm.maxrefs");
@ -488,15 +489,15 @@ static void handle_build_op_str (RCore *core, RDisasmState *ds) {
if (ds->varsub && ds->opstr) { if (ds->varsub && ds->opstr) {
RAnalFunction *f = r_anal_get_fcn_in (core->anal, RAnalFunction *f = r_anal_get_fcn_in (core->anal,
ds->at, R_ANAL_FCN_TYPE_NULL); ds->at, R_ANAL_FCN_TYPE_NULL);
if (f) { //if (f) {
core->parser->varlist = r_anal_var_list; core->parser->varlist = r_anal_var_list;
r_parse_varsub (core->parser, f, r_parse_varsub (core->parser, f, ds->at, ds->analop.size,
ds->opstr, ds->strsub, sizeof (ds->strsub)); ds->opstr, ds->strsub, sizeof (ds->strsub));
if (*ds->strsub) { if (*ds->strsub) {
free (ds->opstr); free (ds->opstr);
ds->opstr = strdup (ds->strsub); ds->opstr = strdup (ds->strsub);
} }
} //}
} }
asm_str = colorize_asm_string (core, ds); asm_str = colorize_asm_string (core, ds);
if (ds->decode) { if (ds->decode) {
@ -750,7 +751,6 @@ static void handle_show_functions (RCore *core, RDisasmState *ds) {
default: default:
fcntype = "loc"; break; fcntype = "loc"; break;
} }
#if SLOW_BUT_OK #if SLOW_BUT_OK
int corner = (f->size <= ds->analop.size) ? RDWN_CORNER : LINE_VERT; int corner = (f->size <= ds->analop.size) ? RDWN_CORNER : LINE_VERT;
corner = LINE_VERT; // 99% of cases corner = LINE_VERT; // 99% of cases
@ -2807,7 +2807,7 @@ R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int nb_byte
f = r_anal_get_fcn_in (core->anal, at, R_ANAL_FCN_TYPE_FCN|R_ANAL_FCN_TYPE_SYM); f = r_anal_get_fcn_in (core->anal, at, R_ANAL_FCN_TYPE_FCN|R_ANAL_FCN_TYPE_SYM);
if (ds->varsub && f) { if (ds->varsub && f) {
core->parser->varlist = r_anal_var_list; core->parser->varlist = r_anal_var_list;
r_parse_varsub (core->parser, f, r_parse_varsub (core->parser, f, at, ds->analop.size,
asmop.buf_asm, asmop.buf_asm, sizeof (asmop.buf_asm)); asmop.buf_asm, asmop.buf_asm, sizeof (asmop.buf_asm));
} }
oplen = r_asm_op_get_size (&asmop); oplen = r_asm_op_get_size (&asmop);

View File

@ -23,6 +23,7 @@ typedef struct r_parse_t {
void *user; void *user;
int flagspace; int flagspace;
int notin_flagspace; int notin_flagspace;
bool relsub; // replace rip relative expressions in instruction
struct r_parse_plugin_t *cur; struct r_parse_plugin_t *cur;
RAnal *anal; // weak anal ref RAnal *anal; // weak anal ref
RAnalHint *hint; // weak anal ref RAnalHint *hint; // weak anal ref
@ -38,7 +39,7 @@ typedef struct r_parse_plugin_t {
int (*parse)(RParse *p, const char *data, char *str); int (*parse)(RParse *p, const char *data, char *str);
int (*assemble)(RParse *p, char *data, char *str); int (*assemble)(RParse *p, char *data, char *str);
int (*filter)(RParse *p, RFlag *f, char *data, char *str, int len); int (*filter)(RParse *p, RFlag *f, char *data, char *str, int len);
int (*varsub)(RParse *p, RAnalFunction *f, char *data, char *str, int len); bool (*varsub)(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len);
int (*replace)(int argc, const char *argv[], char *newstr); int (*replace)(int argc, const char *argv[], char *newstr);
struct list_head list; struct list_head list;
} RParsePlugin; } RParsePlugin;
@ -53,7 +54,7 @@ R_API int r_parse_use(RParse *p, const char *name);
R_API int r_parse_parse(RParse *p, const char *data, char *str); R_API int r_parse_parse(RParse *p, const char *data, char *str);
R_API int r_parse_assemble(RParse *p, char *data, char *str); R_API int r_parse_assemble(RParse *p, char *data, char *str);
R_API int r_parse_filter(RParse *p, RFlag *f, char *data, char *str, int len); R_API int r_parse_filter(RParse *p, RFlag *f, char *data, char *str, int len);
R_API int r_parse_varsub(RParse *p, RAnalFunction *f, char *data, char *str, int len); R_API bool r_parse_varsub(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len);
R_API char *r_parse_c_string(const char *code); R_API char *r_parse_c_string(const char *code);
R_API char *r_parse_c_file(const char *path); R_API char *r_parse_c_file(const char *path);
R_API int r_parse_is_c_file (const char *file); R_API int r_parse_is_c_file (const char *file);

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2012 nibble<.ds@gmail.com> */ /* radare - LGPL - Copyright 2009-2015 nibble */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -33,7 +33,7 @@ static int assemble(RParse *p, char *data, char *str) {
return true; return true;
} }
static int varsub(RParse *p, RAnalFunction *f, char *data, char *str, int len) { static bool varsub(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) {
#if USE_VARSUBS #if USE_VARSUBS
char *ptr, *ptr2; char *ptr, *ptr2;
int i; int i;
@ -55,12 +55,9 @@ static int varsub(RParse *p, RAnalFunction *f, char *data, char *str, int len) {
struct r_parse_plugin_t r_parse_plugin_mreplace = { struct r_parse_plugin_t r_parse_plugin_mreplace = {
.name = "mreplace", .name = "mreplace",
.desc = "mreplace parsing plugin", .desc = "mreplace parsing plugin",
.init = NULL,
.fini = NULL,
.parse = &parse, .parse = &parse,
.assemble = &assemble, .assemble = &assemble,
.varsub = &varsub, .varsub = &varsub,
.filter = NULL,
}; };
#else #else

View File

@ -207,7 +207,7 @@ static inline int issegoff (const char *w) {
} }
#endif #endif
static int varsub(RParse *p, RAnalFunction *f, char *data, char *str, int len) { static bool varsub(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) {
#if USE_VARSUBS #if USE_VARSUBS
int i; int i;
char *ptr, *ptr2; char *ptr, *ptr2;
@ -228,8 +228,20 @@ static int varsub(RParse *p, RAnalFunction *f, char *data, char *str, int len) {
char *tstr = strdup (data); char *tstr = strdup (data);
RList *vars, *args; RList *vars, *args;
if (p->relsub) {
char *rip = strstr (tstr, "[rip");
if (rip) {
char *ripend = strchr (rip+3, ']');
const char *plus = strchr (rip, '+');
const char *neg = strchr (rip, '-');
if (!ripend) ripend = "]";
if (plus) sprintf (rip+1, "0x%llx%s", oplen+addr + r_num_get (NULL, plus+1), ripend);
if (neg) sprintf (rip+1, "0x%llx%s", oplen+addr - r_num_get (NULL, neg+1), ripend);
}
}
if (!p->varlist) { if (!p->varlist) {
free(tstr); free (tstr);
return false; return false;
} }
vars = p->varlist (p->anal, f, 'v'); vars = p->varlist (p->anal, f, 'v');
@ -301,10 +313,7 @@ static int varsub(RParse *p, RAnalFunction *f, char *data, char *str, int len) {
struct r_parse_plugin_t r_parse_plugin_x86_pseudo = { struct r_parse_plugin_t r_parse_plugin_x86_pseudo = {
.name = "x86.pseudo", .name = "x86.pseudo",
.desc = "X86 pseudo syntax", .desc = "X86 pseudo syntax",
.init = NULL,
.fini = NULL,
.parse = &parse, .parse = &parse,
.filter = NULL,
.varsub = &varsub, .varsub = &varsub,
}; };

View File

@ -205,9 +205,9 @@ R_API int r_parse_filter(RParse *p, RFlag *f, char *data, char *str, int len) {
return false; return false;
} }
R_API int r_parse_varsub(RParse *p, RAnalFunction *f, char *data, char *str, int len) { R_API bool r_parse_varsub(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) {
if (p->cur && p->cur->varsub) if (p->cur && p->cur->varsub)
return p->cur->varsub (p, f, data, str, len); return p->cur->varsub (p, f, addr, oplen, data, str, len);
return false; return false;
} }