Fix #14655 - Improve asm.pseudo for Dalvik ##disasm (#14694)

class, method and field names are now shorter and easier to read
This commit is contained in:
radare 2019-07-30 04:22:19 +02:00 committed by GitHub
parent 8248039e55
commit a0c6997cc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 21 deletions

View File

@ -42,11 +42,11 @@ static int __getoffset(RBin *bin, int type, int idx) {
return -1;
}
static const char *__getname(RBin *bin, int type, int idx) {
static const char *__getname(RBin *bin, int type, int idx, bool sd) {
RBinFile *a = r_bin_cur (bin);
RBinPlugin *plugin = r_bin_file_cur_plugin (a);
if (plugin && plugin->get_name) {
return plugin->get_name (a, type, idx);
return plugin->get_name (a, type, idx, sd);
}
return NULL;
}

View File

@ -17,6 +17,7 @@ extern struct r_bin_dbginfo_t r_bin_dbginfo_dex;
static bool dexdump = false;
static Sdb *mdb = NULL;
static const char *dexSubsystem = NULL;
static bool simplifiedDemangling = false; // depends on asm.pseudo
static ut64 get_method_flags(ut64 MA) {
ut64 flags = 0;
@ -951,6 +952,15 @@ static char *dex_method_name(RBinDexObj *bin, int idx) {
return getstr (bin, tid);
}
static char *simplify(char *s) {
char *p = (char *)r_str_rchr (s, NULL, '/');
if (p) {
r_str_cpy (s, p + 1);
}
r_str_replace_char (s, '/', '.');
return s;
}
static char *dex_class_name_byid(RBinDexObj *bin, int cid) {
int tid;
if (!bin || !bin->types) {
@ -960,11 +970,22 @@ static char *dex_class_name_byid(RBinDexObj *bin, int cid) {
return NULL;
}
tid = bin->types[cid].descriptor_id;
return getstr (bin, tid);
char *s = getstr (bin, tid);
if (simplifiedDemangling) {
simplify (s);
}
return s;
}
static char *dex_class_name(RBinDexObj *bin, RBinDexClass *c) {
return dex_class_name_byid (bin, c->class_id);
char *s = dex_class_name_byid (bin, c->class_id);
if (simplifiedDemangling) {
simplify (s);
if (*s == 'L') {
r_str_cpy (s, s + 1);
}
}
return s;
}
static char *dex_field_name(RBinDexObj *bin, int fid) {
@ -986,10 +1007,26 @@ static char *dex_field_name(RBinDexObj *bin, int fid) {
const char *a = getstr (bin, bin->types[cid].descriptor_id);
const char *b = getstr (bin, tid);
const char *c = getstr (bin, bin->types[type_id].descriptor_id);
if (a && b && c) {
return r_str_newf ("%s->%s %s", a, b, c);
if (simplifiedDemangling) {
if (a && b && c) {
char *_a = simplify(strdup (a));
char *_b = simplify(strdup (b));
char *_c = simplify(strdup (c));
char *str = r_str_newf ("(%s) %s.%s", _c, _a, _b);
free (_a);
free (_b);
free (_c);
return str;
}
return r_str_newf ("(%d) %d.%d",
bin->types[type_id].descriptor_id,
tid,
bin->types[cid].descriptor_id
);
}
return r_str_newf ("%d->%d %d", bin->types[cid].descriptor_id, tid, bin->types[type_id].descriptor_id);
return (a && b && c)
? r_str_newf ("%s->%s %s", a, b, c)
: r_str_newf ("%d->%d %d", bin->types[cid].descriptor_id, tid, bin->types[type_id].descriptor_id);
}
static char *dex_method_fullname(RBinDexObj *bin, int method_idx) {
@ -1028,6 +1065,19 @@ static char *dex_method_fullname(RBinDexObj *bin, int method_idx) {
free (signature);
}
}
if (flagname && simplifiedDemangling) {
char *p = strchr (flagname, '(');
if (p) {
*p = 0;
char *q = strchr (p + 1, ')');
if (q) {
simplify (q + 1);
r_str_cpy (p, q + 1);
}
simplify (flagname);
}
}
return flagname;
}
@ -1911,7 +1961,8 @@ static int getoffset(RBinFile *bf, int type, int idx) {
return -1;
}
static char *getname(RBinFile *bf, int type, int idx) {
static char *getname(RBinFile *bf, int type, int idx, bool sd) {
simplifiedDemangling = sd; // XXX kill globals
struct r_bin_dex_obj_t *dex = bf->o->bin_obj;
switch (type) {
case 'm': // methods

View File

@ -405,6 +405,13 @@ static bool cb_scrrainbow(void *user, void *data) {
return true;
}
static bool cb_asmpseudo (void *user, void *data) {
RCore *core = (RCore *) user;
RConfigNode *node = (RConfigNode *) data;
core->assembler->pseudo = node->i_value;
return true;
}
static bool cb_asmsecsub(void *user, void *data) {
RCore *core = (RCore *) user;
RConfigNode *node = (RConfigNode *) data;
@ -2973,7 +2980,7 @@ R_API int r_core_config_init(RCore *core) {
SETPREF ("asm.section.name", "true", "Show section name in the disasm");
SETI ("asm.section.col", 20, "Columns width to show asm.section");
SETCB ("asm.section.sub", "false", &cb_asmsecsub, "Show offsets in disasm prefixed with section/map name");
SETPREF ("asm.pseudo", "false", "Enable pseudo syntax");
SETCB ("asm.pseudo", "false", &cb_asmpseudo, "Enable pseudo syntax");
SETPREF ("asm.size", "false", "Show size of opcodes in disassembly (pd)");
SETPREF ("asm.stackptr", "false", "Show stack pointer at disassembly");
SETPREF ("asm.cyclespace", "false", "Indent instructions depending on CPU-cycles");

View File

@ -41,7 +41,7 @@ R_LIB_VERSION_HEADER(r_asm);
#define R_ASM_GET_NAME(x,y,z) \
(x && x->binb.bin && x->binb.get_name)? \
x->binb.get_name (x->binb.bin, y, z): NULL
x->binb.get_name (x->binb.bin, y, z, x->pseudo): NULL
enum {
R_ASM_SYNTAX_NONE = 0,
@ -116,6 +116,7 @@ typedef struct r_asm_t {
bool immdisp; // Display immediates with # symbol (for arm stuff).
HtPP *flags;
int seggrn;
bool pseudo;
} RAsm;
typedef bool (*RAsmModifyCallback)(RAsm *a, ut8 *buf, int field, ut64 val);

View File

@ -445,7 +445,7 @@ typedef struct r_bin_plugin_t {
struct r_bin_dbginfo_t *dbginfo;
struct r_bin_write_t *write;
int (*get_offset)(RBinFile *bf, int type, int idx);
char* (*get_name)(RBinFile *bf, int type, int idx);
char* (*get_name)(RBinFile *bf, int type, int idx, bool simplified);
ut64 (*get_vaddr)(RBinFile *bf, ut64 baddr, ut64 paddr, ut64 vaddr);
RBuffer* (*create)(RBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RBinArchOptions *opt);
char* (*demangle)(const char *str);
@ -613,7 +613,7 @@ typedef struct r_bin_write_t {
// TODO: has_dbg_syms... maybe flags?
typedef int (*RBinGetOffset)(RBin *bin, int type, int idx);
typedef const char *(*RBinGetName)(RBin *bin, int type, int idx);
typedef const char *(*RBinGetName)(RBin *bin, int type, int idx, bool sd);
typedef RList *(*RBinGetSections)(RBin *bin);
typedef RBinSection *(*RBinGetSectionAt)(RBin *bin, ut64 addr);

View File

@ -121,14 +121,14 @@ static int replace(int argc, const char *argv[], char *newstr) {
{ "sput-char", "2[3] = (char) 1"},
{ "iput-int", "2[3] = (int) 1"},
{ "iget", "1 = 2[3]"},
{ "sget-byte", "1 = (byte) 2[3]"},
{ "iget-byte", "1 = (byte) 2[3]"},
{ "iget-char", "1 = (char) 2[3]"},
{ "iget-short", "1 = (short) 2[3]"},
{ "iget-wide", "1 = (wide) 2[3]"},
{ "iget-object", "1 = (object) 2[3]"},
{ "iget-boolean", "1 = (bool) 2[3]"},
{ "+iget-wide-volatile", "1 = (wide-volatile) 2[3]"},
{ "sget-byte", "1 = (byte) 2 [3]"},
{ "iget-byte", "1 = (byte) 2 [3]"},
{ "iget-char", "1 = (char) 2 [3]"},
{ "iget-short", "1 = (short) 2 [3]"},
{ "iget-wide", "1 = (wide) 2 [3]"},
{ "iget-object", "1 = (2) 3"},
{ "iget-boolean", "1 = (bool) 2 [3]"},
{ "+iget-wide-volatile", "1 = (wide-volatile) 2 [3]"},
{ "if-eq", "if (1 == 2) goto 3"},
{ "if-lt", "if (1 < 2) goto 3"},
{ "if-ne", "if (1 != 2) goto 3"},
@ -246,7 +246,7 @@ static int parse(RParse *p, const char *data, char *str) {
if (!(buf = malloc (len + 1))) {
return false;
}
memcpy (buf, data, len+1);
memcpy (buf, data, len + 1);
r_str_trim (buf);