Skip to content

Commit

Permalink
Use macros for common flagspaces names instead of hardcoding them (ra…
Browse files Browse the repository at this point in the history
  • Loading branch information
ret2libc authored and radare committed Feb 23, 2019
1 parent 65360dd commit eae1913
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 43 deletions.
26 changes: 9 additions & 17 deletions libr/anal/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
#include <r_vector.h>
#include "../include/r_anal.h"

#define CLASSES_FLAGSPACE "classes"


static void r_anal_class_base_delete_class(RAnal *anal, const char *class_name);
static void r_anal_class_method_delete_class(RAnal *anal, const char *class_name);
static void r_anal_class_vtable_delete_class(RAnal *anal, const char *class_name);
Expand Down Expand Up @@ -422,37 +419,32 @@ static char *flagname_attr(const char *attr_type, const char *class_name, const
}

static void r_anal_class_set_flag(RAnal *anal, const char *name, ut64 addr) {
if (!name || !anal->flb.set || !anal->flb.push_fs || !anal->flb.pop_fs) {
if (!name || !anal->flg_class_set) {
return;
}
anal->flb.push_fs (anal->flb.f, CLASSES_FLAGSPACE);
anal->flb.set (anal->flb.f, name, addr, 0);
anal->flb.pop_fs (anal->flb.f);
anal->flg_class_set (anal->flb.f, name, addr, 0);
}

static void r_anal_class_unset_flag(RAnal *anal, const char *name) {
if (!name || !anal->flb.unset_name || !anal->flb.push_fs || !anal->flb.pop_fs) {
if (!name || !anal->flb.unset_name || !anal->flg_class_get) {
return;
}
anal->flb.push_fs (anal->flb.f, CLASSES_FLAGSPACE);
anal->flb.unset_name (anal->flb.f, name);
anal->flb.pop_fs (anal->flb.f);
if (anal->flg_class_get (anal->flb.f, name)) {
anal->flb.unset_name (anal->flb.f, name);
}
}

static void r_anal_class_rename_flag(RAnal *anal, const char *old_name, const char *new_name) {
if (!old_name || !new_name || !anal->flb.set || !anal->flb.get || !anal->flb.push_fs || !anal->flb.pop_fs || !anal->flb.unset_name) {
if (!old_name || !new_name || !anal->flb.unset || !anal->flg_class_get || !anal->flg_class_set) {
return;
}
anal->flb.push_fs (anal->flb.f, CLASSES_FLAGSPACE);
RFlagItem *flag = anal->flb.get (anal->flb.f, old_name);
RFlagItem *flag = anal->flg_class_get (anal->flb.f, old_name);
if (!flag) {
anal->flb.pop_fs (anal->flb.f);
return;
}
ut64 addr = flag->offset;
anal->flb.unset (anal->flb.f, flag);
anal->flb.set (anal->flb.f, new_name, addr, 0);
anal->flb.pop_fs (anal->flb.f);
anal->flg_class_set (anal->flb.f, new_name, addr, 0);
}

static RAnalClassErr r_anal_class_add_attr_unique_raw(RAnal *anal, const char *class_name, RAnalClassAttrType attr_type, const char *content, char *attr_id_out, size_t attr_id_out_size) {
Expand Down
6 changes: 2 additions & 4 deletions libr/anal/fcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1707,10 +1707,8 @@ R_API int r_anal_fcn_insert(RAnal *anal, RAnalFunction *fcn) {
if (anal->cb.on_fcn_new) {
anal->cb.on_fcn_new (anal, anal->user, fcn);
}
if (anal->flb.set && anal->flb.push_fs && anal->flb.pop_fs) {
anal->flb.push_fs (anal->flb.f, "functions");
anal->flb.set (anal->flb.f, fcn->name, fcn->addr, r_anal_fcn_size (fcn));
anal->flb.pop_fs (anal->flb.f);
if (anal->flg_fcn_set) {
anal->flg_fcn_set (anal->flb.f, fcn->name, fcn->addr, r_anal_fcn_size (fcn));
}
return true;
}
Expand Down
14 changes: 7 additions & 7 deletions libr/core/canal.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ R_API void r_core_anal_autoname_all_golang_fcns(RCore *core) {
ut32 size = r_read_le32 (temp_size);
int num_syms = 0;
//r_cons_print ("[x] Reading .gopclntab...\n");
r_flag_space_push (core->flags, "symbols");
r_flag_space_push (core->flags, R_FLAGS_FS_SYMBOLS);
while (offset < gopclntab + size) {
ut8 temp_delta[4] = {0};
ut8 temp_func_addr[4] = {0};
Expand Down Expand Up @@ -786,7 +786,7 @@ static int core_anal_fcn(RCore *core, ut64 at, ut64 from, int reftype, int depth
autoname_imp_trampoline (core, fcn);
}
/* Add flag */
r_flag_space_push (core->flags, "functions");
r_flag_space_push (core->flags, R_FLAGS_FS_FUNCTIONS);
r_flag_set (core->flags, fcn->name, fcn->addr, r_anal_fcn_size (fcn));
r_flag_space_pop (core->flags);
}
Expand Down Expand Up @@ -878,7 +878,7 @@ static int core_anal_fcn(RCore *core, ut64 at, ut64 from, int reftype, int depth
r_anal_fcn_type_tostring (fcn->type),
at);
/* Add flag */
r_flag_space_push (core->flags, "functions");
r_flag_space_push (core->flags, R_FLAGS_FS_FUNCTIONS);
r_flag_set (core->flags, fcn->name, at, r_anal_fcn_size (fcn));
r_flag_space_pop (core->flags);
}
Expand Down Expand Up @@ -1626,7 +1626,7 @@ R_API int r_core_anal_fcn(RCore *core, ut64 at, ut64 from, int reftype, int dept
int result = R_ANAL_RET_ERROR;
result = core->anal->cur->analyze_fns (core->anal, at, from, reftype, depth);
/* update the flags after running the analysis function of the plugin */
r_flag_space_push (core->flags, "functions");
r_flag_space_push (core->flags, R_FLAGS_FS_FUNCTIONS);
r_list_foreach (core->anal->fcns, iter, fcn) {
r_flag_set (core->flags, fcn->name, fcn->addr, r_anal_fcn_size (fcn));
}
Expand Down Expand Up @@ -3380,7 +3380,7 @@ static void found_xref(RCore *core, ut64 at, ut64 xref_to, RAnalRefType type, in
if (str_string) {
r_name_filter (str_string, -1);
char *str_flagname = r_str_newf ("str.%s", str_string);
r_flag_space_push (core->flags, "strings");
r_flag_space_push (core->flags, R_FLAGS_FS_STRINGS);
(void)r_flag_set (core->flags, str_flagname, xref_to, 1);
r_flag_space_pop (core->flags);
free (str_flagname);
Expand Down Expand Up @@ -4128,7 +4128,7 @@ static void add_string_ref(RCore *core, ut64 xref_to) {
r_anal_xrefs_set (core->anal, core->anal->esil->address, xref_to, R_ANAL_REF_TYPE_DATA);
r_name_filter (str_flagname, -1);
char *flagname = sdb_fmt ("str.%s", str_flagname);
r_flag_space_push (core->flags, "strings");
r_flag_space_push (core->flags, R_FLAGS_FS_STRINGS);
r_flag_set (core->flags, flagname, xref_to, len);
r_flag_space_pop (core->flags);
r_meta_add (core->anal, 's', xref_to, xref_to + len, str_flagname);
Expand Down Expand Up @@ -4460,7 +4460,7 @@ R_API void r_core_anal_esil(RCore *core, const char *str, const char *target) {
}
}
if (op.type == R_ANAL_OP_TYPE_SWI) {
r_flag_space_set (core->flags, "syscalls");
r_flag_space_set (core->flags, R_FLAGS_FS_SYSCALLS);
int snv = canal_isThumb (core)? op.val: (int)r_reg_getv (core->anal->reg, sn);
RSyscallItem *si = r_syscall_get (core->anal->syscall, snv, -1);
if (si) {
Expand Down
20 changes: 10 additions & 10 deletions libr/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ static void _print_strings(RCore *r, RList *list, int mode, int va) {
r_cons_printf ("fs strings");
}
if (IS_MODE_SET (mode) && r_config_get_i (r->config, "bin.strings")) {
r_flag_space_set (r->flags, "strings");
r_flag_space_set (r->flags, R_FLAGS_FS_STRINGS);
r_cons_break_push (NULL, NULL);
}
if (IS_MODE_NORMAL (mode)) {
Expand Down Expand Up @@ -1055,7 +1055,7 @@ static int bin_main(RCore *r, int mode, int va) {
addr = va ? r_bin_a2b (r->bin, binmain->vaddr) : binmain->paddr;

if (IS_MODE_SET (mode)) {
r_flag_space_set (r->flags, "symbols");
r_flag_space_set (r->flags, R_FLAGS_FS_SYMBOLS);
r_flag_set (r->flags, "main", addr, r->blocksize);
} else if (IS_MODE_SIMPLE (mode)) {
r_cons_printf ("%"PFMT64d, addr);
Expand Down Expand Up @@ -1134,7 +1134,7 @@ static int bin_entry(RCore *r, int mode, ut64 laddr, int va, bool inifin) {
const char *hpaddr_key = (entry->type == R_BIN_ENTRY_TYPE_PROGRAM)
? "haddr" : "hpaddr";
if (IS_MODE_SET (mode)) {
r_flag_space_set (r->flags, "symbols");
r_flag_space_set (r->flags, R_FLAGS_FS_SYMBOLS);
if (entry->type == R_BIN_ENTRY_TYPE_INIT) {
snprintf (str, R_FLAG_NAME_SIZE, "entry.init%i", init_i);
} else if (entry->type == R_BIN_ENTRY_TYPE_FINI) {
Expand Down Expand Up @@ -1451,7 +1451,7 @@ static int bin_relocs(RCore *r, int mode, int va) {
pj_a (pj);
}
} else if (IS_MODE_SET (mode)) {
r_flag_space_set (r->flags, "relocs");
r_flag_space_set (r->flags, R_FLAGS_FS_RELOCS);
}

r_rbtree_foreach (relocs, iter, reloc, RBinReloc, vrb) {
Expand Down Expand Up @@ -1900,7 +1900,7 @@ static int bin_symbols(RCore *r, int mode, ut64 laddr, int va, ut64 at, const ch
if (IS_MODE_JSON (mode) && !printHere) {
r_cons_printf ("[");
} else if (IS_MODE_SET (mode)) {
r_flag_space_set (r->flags, "symbols");
r_flag_space_set (r->flags, R_FLAGS_FS_SYMBOLS);
} else if (!at && exponly) {
if (IS_MODE_RAD (mode)) {
r_cons_printf ("fs exports\n");
Expand Down Expand Up @@ -1967,12 +1967,12 @@ static int bin_symbols(RCore *r, int mode, ut64 laddr, int va, ut64 at, const ch
}
if (!strncmp (r_symbol_name, "imp.", 4)) {
if (lastfs != 'i') {
r_flag_space_set (r->flags, "imports");
r_flag_space_set (r->flags, R_FLAGS_FS_IMPORTS);
}
lastfs = 'i';
} else {
if (lastfs != 's') {
r_flag_space_set (r->flags, "symbols");
r_flag_space_set (r->flags, R_FLAGS_FS_SYMBOLS);
}
lastfs = 's';
}
Expand Down Expand Up @@ -2430,7 +2430,7 @@ static int bin_sections(RCore *r, int mode, ut64 laddr, int va, ut64 at, const c
r_cons_printf ("Current section\n");
} else if (IS_MODE_SET (mode)) {
fd = r_core_file_cur_fd (r);
r_flag_space_set (r->flags, print_segments ? "segments" : "sections");
r_flag_space_set (r->flags, print_segments? R_FLAGS_FS_SEGMENTS: R_FLAGS_FS_SECTIONS);
}
if (IS_MODE_NORMAL (mode)) {
r_cons_printf ("Nm Paddr Size Vaddr Memsz Perms %sName\n",
Expand Down Expand Up @@ -2827,7 +2827,7 @@ static int bin_classes(RCore *r, int mode) {
if (!r_config_get_i (r->config, "bin.classes")) {
return false;
}
r_flag_space_set (r->flags, "classes");
r_flag_space_set (r->flags, R_FLAGS_FS_CLASSES);
} else if (IS_MODE_RAD (mode)) {
r_cons_println ("fs classes");
}
Expand Down Expand Up @@ -3380,7 +3380,7 @@ static void bin_pe_resources(RCore *r, int mode) {
return;
}
if (IS_MODE_SET (mode)) {
r_flag_space_set (r->flags, "resources");
r_flag_space_set (r->flags, R_FLAGS_FS_RESOURCES);
} else if (IS_MODE_RAD (mode)) {
r_cons_printf ("fs resources\n");
} else if (IS_MODE_JSON (mode)) {
Expand Down
2 changes: 1 addition & 1 deletion libr/core/cmd_anal.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ static void find_refs(RCore *core, const char *glob) {
static void flag_every_function(RCore *core) {
RListIter *iter;
RAnalFunction *fcn;
r_flag_space_push (core->flags, "functions");
r_flag_space_push (core->flags, R_FLAGS_FS_FUNCTIONS);
r_list_foreach (core->anal->fcns, iter, fcn) {
r_flag_set (core->flags, fcn->name,
fcn->addr, r_anal_fcn_size (fcn));
Expand Down
2 changes: 1 addition & 1 deletion libr/core/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,7 @@ static int printzoomcallback(void *user, int mode, ut64 addr, ut8 *bufz, ut64 si
}
break;
case 's': // "pzs"
u.flagspace = r_flag_space_get (core->flags, "strings");
u.flagspace = r_flag_space_get (core->flags, R_FLAGS_FS_STRINGS);
u.addr = addr;
u.size = size;
u.ret = &ret;
Expand Down
24 changes: 24 additions & 0 deletions libr/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,27 @@ static void cb_event_handler(REvent *ev, int event_type, void *user, void *data)
free (str);
}

static RFlagItem *core_flg_class_set(RFlag *f, const char *name, ut64 addr, ut32 size) {
r_flag_space_push (f, R_FLAGS_FS_CLASSES);
RFlagItem *res = r_flag_set (f, name, addr, size);
r_flag_space_pop (f);
return res;
}

static RFlagItem *core_flg_class_get(RFlag *f, const char *name) {
r_flag_space_push (f, R_FLAGS_FS_CLASSES);
RFlagItem *res = r_flag_get (f, name);
r_flag_space_pop (f);
return res;
}

static RFlagItem *core_flg_fcn_set (RFlag *f, const char *name, ut64 addr, ut32 size) {
r_flag_space_push (f, R_FLAGS_FS_FUNCTIONS);
RFlagItem *res = r_flag_set (f, name, addr, size);
r_flag_space_pop (f);
return res;
}

R_API bool r_core_init(RCore *core) {
core->blocksize = R_CORE_BLOCKSIZE;
core->block = (ut8 *)calloc (R_CORE_BLOCKSIZE + 1, 1);
Expand Down Expand Up @@ -2443,6 +2464,9 @@ R_API bool r_core_init(RCore *core) {
r_core_bind (core, &(core->fs->cob));
r_io_bind (core->io, &(core->bin->iob));
r_flag_bind (core->flags, &(core->anal->flb));
core->anal->flg_class_set = core_flg_class_set;
core->anal->flg_class_get = core_flg_class_get;
core->anal->flg_fcn_set = core_flg_fcn_set;
r_anal_bind (core->anal, &(core->parser->analb));

r_core_bind (core, &(core->anal->coreb));
Expand Down
6 changes: 3 additions & 3 deletions libr/core/visual.c
Original file line number Diff line number Diff line change
Expand Up @@ -3533,9 +3533,9 @@ R_API void r_core_visual_title(RCore *core, int color) {
ut64 addr = core->offset + (core->print->cur_enabled? core->print->cur: 0);
/* TODO: we need a helper into r_flags to do that */
RFlagItem *f = NULL;
if (core->anal->flb.push_fs (core->flags, "symbols")) {
f = core->anal->flb.get_at (core->flags, addr, showDelta);
core->anal->flb.pop_fs (core->flags);
if (r_flag_space_push (core->flags, R_FLAGS_FS_SYMBOLS)) {
f = r_flag_get_at (core->flags, addr, showDelta);
r_flag_space_pop (core->flags);
}
if (!f) {
f = r_flag_get_at (core->flags, addr, showDelta);
Expand Down
3 changes: 3 additions & 0 deletions libr/include/r_anal.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,9 @@ typedef struct r_anal_t {
double diff_thfcn;
RIOBind iob;
RFlagBind flb;
RFlagSet flg_class_set;
RFlagGet flg_class_get;
RFlagSet flg_fcn_set;
RBinBind binb; // Set only from core when an analysis plugin is called.
RCoreBind coreb;
int decode;
Expand Down
11 changes: 11 additions & 0 deletions libr/include/r_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ R_LIB_VERSION_HEADER(r_core);
#define R_CORE_ANAL_KEYVALUE 16
#define R_CORE_ANAL_JSON_FORMAT_DISASM 32

#define R_FLAGS_FS_CLASSES "classes"
#define R_FLAGS_FS_FUNCTIONS "functions"
#define R_FLAGS_FS_IMPORTS "imports"
#define R_FLAGS_FS_RELOCS "relocs"
#define R_FLAGS_FS_RESOURCES "resources"
#define R_FLAGS_FS_SECTIONS "sections"
#define R_FLAGS_FS_SEGMENTS "segments"
#define R_FLAGS_FS_STRINGS "strings"
#define R_FLAGS_FS_SYMBOLS "symbols"
#define R_FLAGS_FS_SYSCALLS "syscalls"

#define R_GRAPH_FORMAT_NO 0
#define R_GRAPH_FORMAT_GMLFCN 1
#define R_GRAPH_FORMAT_JSON 2
Expand Down

0 comments on commit eae1913

Please sign in to comment.