Skip to content

Commit

Permalink
Pull Applying Bin Config and Main out of rz_core_bin_info() (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
thestr4ng3r authored Mar 6, 2021
1 parent 8269a24 commit fbeb51b
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 97 deletions.
6 changes: 1 addition & 5 deletions librz/bin/bfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ static RzBinString *__stringAt(RzBinFile *bf, RzList *ret, ut64 addr) {
return NULL;
}

static ut64 binobj_a2b(RzBinObject *o, ut64 addr) {
return o ? addr + o->baddr_shift : addr;
}

static void print_string(RzBinFile *bf, RzBinString *string, int raw, PJ *pj) {
rz_return_if_fail(bf && string);

Expand Down Expand Up @@ -1034,7 +1030,7 @@ RZ_API RzBinField *rz_bin_file_add_field(RzBinFile *binfile, const char *classna
RZ_API ut64 rz_bin_file_get_vaddr(RzBinFile *bf, ut64 paddr, ut64 vaddr) {
rz_return_val_if_fail(bf && bf->o, paddr);
if (bf->o->info && bf->o->info->has_va) {
return binobj_a2b(bf->o, vaddr);
return rz_bin_object_addr_with_base(bf->o, vaddr);
}
return paddr;
}
Expand Down
12 changes: 1 addition & 11 deletions librz/bin/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ static const char *__getname(RzBin *bin, int type, int idx, bool sd) {
return NULL;
}

static ut64 binobj_a2b(RzBinObject *o, ut64 addr) {
return o ? addr + o->baddr_shift : addr;
}

// TODO: move these two function do a different file
RZ_API RzBinXtrData *rz_bin_xtrdata_new(RzBuffer *buf, ut64 offset, ut64 size, ut32 file_count, RzBinXtrMetadata *metadata) {
RzBinXtrData *data = RZ_NEW0(RzBinXtrData);
Expand Down Expand Up @@ -756,7 +752,7 @@ RZ_API RzBinSection *rz_bin_get_section_at(RzBinObject *o, ut64 off, int va) {
if (section->is_segment) {
continue;
}
from = va ? binobj_a2b(o, section->vaddr) : section->paddr;
from = va ? rz_bin_object_addr_with_base(o, section->vaddr) : section->paddr;
to = from + (va ? section->vsize : section->size);
if (off >= from && off < to) {
return section;
Expand Down Expand Up @@ -1293,12 +1289,6 @@ RZ_API ut64 rz_bin_get_vaddr(RzBin *bin, ut64 paddr, ut64 vaddr) {
return rz_bin_file_get_vaddr(bin->cur, paddr, vaddr);
}

RZ_API ut64 rz_bin_a2b(RzBin *bin, ut64 addr) {
rz_return_val_if_fail(bin, UT64_MAX);
RzBinObject *o = rz_bin_cur_object(bin);
return binobj_a2b(o, addr);
}

RZ_API ut64 rz_bin_get_size(RzBin *bin) {
rz_return_val_if_fail(bin, UT64_MAX);
RzBinObject *o = rz_bin_cur_object(bin);
Expand Down
4 changes: 4 additions & 0 deletions librz/bin/bobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,7 @@ RZ_IPI void rz_bin_object_filter_strings(RzBinObject *bo) {
}
}
}

RZ_API ut64 rz_bin_object_addr_with_base(RzBinObject *o, ut64 addr) {
return o ? addr + o->baddr_shift : addr;
}
157 changes: 94 additions & 63 deletions librz/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
static RZ_NULLABLE RZ_BORROW const RzList *core_bin_strings(RzCore *r, RzBinFile *file);
static void _print_strings(RzCore *r, const RzList *list, PJ *pj, int mode, int va);
static bool bin_raw_strings(RzCore *r, PJ *pj, int mode, int va);
static int bin_info(RzCore *r, PJ *pj, int mode, ut64 laddr);
static int bin_main(RzCore *r, PJ *pj, int mode, int va);
static int bin_dwarf(RzCore *core, PJ *pj, int mode);
static int bin_source(RzCore *r, PJ *pj, int mode);
static int bin_entry(RzCore *r, PJ *pj, int mode, ut64 laddr, int va, bool inifin);
Expand Down Expand Up @@ -379,8 +377,8 @@ RZ_API int rz_core_bin_apply_all_info(RzCore *r, RzBinFile *binfile) {
va = va ? VA_TRUE : VA_FALSE;

rz_core_bin_apply_strings(r, binfile);
bin_info(r, NULL, RZ_MODE_SET, loadaddr);
bin_main(r, NULL, RZ_MODE_SET, va);
rz_core_bin_apply_config(r, binfile);
rz_core_bin_apply_main(r, binfile, va);
bin_dwarf(r, NULL, RZ_MODE_SET);
bin_entry(r, NULL, RZ_MODE_SET, loadaddr, va, false);
bin_sections(r, NULL, RZ_MODE_SET, loadaddr, va, UT64_MAX, NULL, NULL, false);
Expand Down Expand Up @@ -436,6 +434,85 @@ RZ_API bool rz_core_bin_apply_strings(RzCore *r, RzBinFile *binfile) {
return true;
}

static void sdb_concat_by_path(Sdb *s, const char *path) {
Sdb *db = sdb_new(0, path, 0);
sdb_merge(s, db);
sdb_close(db);
sdb_free(db);
}

RZ_API bool rz_core_bin_apply_config(RzCore *r, RzBinFile *binfile) {
rz_return_val_if_fail(r && binfile, false);
int v;
char str[RZ_FLAG_NAME_SIZE];
RzBinObject *obj = binfile->o;
if (!obj) {
return false;
}
RzBinInfo *info = obj->info;
if (!info) {
return false;
}
rz_config_set(r->config, "file.type", info->rclass);
rz_config_set(r->config, "cfg.bigendian",
info->big_endian ? "true" : "false");
if (!info->rclass || strcmp(info->rclass, "fs")) {
if (info->lang) {
rz_config_set(r->config, "bin.lang", info->lang);
}
rz_config_set(r->config, "asm.os", info->os);
if (info->rclass && !strcmp(info->rclass, "pe")) {
rz_config_set(r->config, "analysis.cpp.abi", "msvc");
} else {
rz_config_set(r->config, "analysis.cpp.abi", "itanium");
}
rz_config_set(r->config, "asm.arch", info->arch);
if (info->cpu && *info->cpu) {
rz_config_set(r->config, "asm.cpu", info->cpu);
}
if (info->features && *info->features) {
rz_config_set(r->config, "asm.features", info->features);
}
rz_config_set(r->config, "analysis.arch", info->arch);
snprintf(str, RZ_FLAG_NAME_SIZE, "%i", info->bits);
rz_config_set(r->config, "asm.bits", str);
rz_config_set(r->config, "asm.dwarf",
(RZ_BIN_DBG_STRIPPED & info->dbg_info) ? "false" : "true");
v = rz_analysis_archinfo(r->analysis, RZ_ANALYSIS_ARCHINFO_ALIGN);
if (v != -1) {
rz_config_set_i(r->config, "asm.pcalign", v);
}
}
rz_core_analysis_type_init(r);
rz_core_analysis_cc_init(r);
if (info->default_cc && rz_analysis_cc_exist(r->analysis, info->default_cc)) {
rz_config_set(r->config, "analysis.cc", info->default_cc);
}
const char *dir_prefix = rz_config_get(r->config, "dir.prefix");
char *spath = rz_str_newf("%s/" RZ_SDB_FCNSIGN "/spec.sdb", dir_prefix);
if (spath && rz_file_exists(spath)) {
sdb_concat_by_path(r->analysis->sdb_fmts, spath);
}
free(spath);
return true;
}

RZ_API bool rz_core_bin_apply_main(RzCore *r, RzBinFile *binfile, bool va) {
rz_return_val_if_fail(r && binfile, false);
RzBinObject *o = binfile->o;
if (!o) {
return false;
}
RzBinAddr *binmain = o->binsym[RZ_BIN_SYM_MAIN];
if (!binmain) {
return false;
}
ut64 addr = va ? rz_bin_object_addr_with_base(o, binmain->vaddr) : binmain->paddr;
rz_flag_space_set(r->flags, RZ_FLAGS_FS_SYMBOLS);
rz_flag_set(r->flags, "main", addr, r->blocksize);
return true;
}

RZ_API int rz_core_bin_set_cur(RzCore *core, RzBinFile *binfile) {
if (!core->bin) {
return false;
Expand Down Expand Up @@ -717,13 +794,6 @@ static bool is_executable(RzBinObject *obj) {
return false;
}

static void sdb_concat_by_path(Sdb *s, const char *path) {
Sdb *db = sdb_new(0, path, 0);
sdb_merge(s, db);
sdb_close(db);
sdb_free(db);
}

RZ_API void rz_core_analysis_type_init(RzCore *core) {
rz_return_if_fail(core && core->analysis);
const char *dir_prefix = rz_config_get(core->config, "dir.prefix");
Expand Down Expand Up @@ -826,7 +896,6 @@ RZ_API void rz_core_analysis_cc_init(RzCore *core) {

static int bin_info(RzCore *r, PJ *pj, int mode, ut64 laddr) {
int i, j, v;
char str[RZ_FLAG_NAME_SIZE];
RzBinInfo *info = rz_bin_get_info(r->bin);
RzBinFile *bf = rz_bin_cur(r->bin);
if (!bf) {
Expand All @@ -851,43 +920,7 @@ static int bin_info(RzCore *r, PJ *pj, int mode, ut64 laddr) {
havecode = is_executable(obj) | (obj->entries != NULL);
compiled = get_compile_time(bf->sdb);

if (IS_MODE_SET(mode)) {
rz_config_set(r->config, "file.type", info->rclass);
rz_config_set(r->config, "cfg.bigendian",
info->big_endian ? "true" : "false");
if (!info->rclass || strcmp(info->rclass, "fs")) {
if (info->lang) {
rz_config_set(r->config, "bin.lang", info->lang);
}
rz_config_set(r->config, "asm.os", info->os);
if (info->rclass && !strcmp(info->rclass, "pe")) {
rz_config_set(r->config, "analysis.cpp.abi", "msvc");
} else {
rz_config_set(r->config, "analysis.cpp.abi", "itanium");
}
rz_config_set(r->config, "asm.arch", info->arch);
if (info->cpu && *info->cpu) {
rz_config_set(r->config, "asm.cpu", info->cpu);
}
if (info->features && *info->features) {
rz_config_set(r->config, "asm.features", info->features);
}
rz_config_set(r->config, "analysis.arch", info->arch);
snprintf(str, RZ_FLAG_NAME_SIZE, "%i", info->bits);
rz_config_set(r->config, "asm.bits", str);
rz_config_set(r->config, "asm.dwarf",
(RZ_BIN_DBG_STRIPPED & info->dbg_info) ? "false" : "true");
v = rz_analysis_archinfo(r->analysis, RZ_ANALYSIS_ARCHINFO_ALIGN);
if (v != -1) {
rz_config_set_i(r->config, "asm.pcalign", v);
}
}
rz_core_analysis_type_init(r);
rz_core_analysis_cc_init(r);
if (info->default_cc && rz_analysis_cc_exist(r->analysis, info->default_cc)) {
rz_config_set(r->config, "analysis.cc", info->default_cc);
}
} else if (IS_MODE_SIMPLE(mode)) {
if (IS_MODE_SIMPLE(mode)) {
rz_cons_printf("arch %s\n", info->arch);
if (info->cpu && *info->cpu) {
rz_cons_printf("cpu %s\n", info->cpu);
Expand Down Expand Up @@ -1074,11 +1107,6 @@ static int bin_info(RzCore *r, PJ *pj, int mode, ut64 laddr) {
pj_end(pj);
}
}
const char *dir_prefix = rz_config_get(r->config, "dir.prefix");
char *spath = sdb_fmt("%s/" RZ_SDB_FCNSIGN "/spec.sdb", dir_prefix);
if (rz_file_exists(spath)) {
sdb_concat_by_path(r->analysis->sdb_fmts, spath);
}
return true;
}

Expand Down Expand Up @@ -1355,18 +1383,21 @@ static int bin_source(RzCore *r, PJ *pj, int mode) {
return true;
}

static int bin_main(RzCore *r, PJ *pj, int mode, int va) {
RzBinAddr *binmain = rz_bin_get_sym(r->bin, RZ_BIN_SYM_MAIN);
ut64 addr;
static int bin_main(RzCore *r, RzBinFile *binfile, PJ *pj, int mode, int va) {
if (!binfile) {
return false;
}
RzBinObject *o = binfile->o;
if (!o) {
return false;
}
RzBinAddr *binmain = o->binsym[RZ_BIN_SYM_MAIN];
if (!binmain) {
return false;
}
addr = va ? rz_bin_a2b(r->bin, binmain->vaddr) : binmain->paddr;
ut64 addr = va ? rz_bin_object_addr_with_base(o, binmain->vaddr) : binmain->paddr;

if (IS_MODE_SET(mode)) {
rz_flag_space_set(r->flags, RZ_FLAGS_FS_SYMBOLS);
rz_flag_set(r->flags, "main", addr, r->blocksize);
} else if (IS_MODE_SIMPLE(mode)) {
if (IS_MODE_SIMPLE(mode)) {
rz_cons_printf("%" PFMT64d, addr);
} else if (IS_MODE_RZCMD(mode)) {
rz_cons_printf("fs symbols\n");
Expand Down Expand Up @@ -4196,7 +4227,7 @@ RZ_API int rz_core_bin_info(RzCore *core, int action, PJ *pj, int mode, int va,
ret &= bin_info(core, pj, mode, loadaddr);
}
if ((action & RZ_CORE_BIN_ACC_MAIN)) {
ret &= bin_main(core, pj, mode, va);
ret &= bin_main(core, binfile, pj, mode, va);
}
if ((action & RZ_CORE_BIN_ACC_DWARF)) {
ret &= bin_dwarf(core, pj, mode);
Expand Down
36 changes: 18 additions & 18 deletions librz/include/rz_bin.h
Original file line number Diff line number Diff line change
Expand Up @@ -705,26 +705,25 @@ RZ_API RzList *rz_bin_raw_strings(RzBinFile *a, int min);
RZ_API RzList *rz_bin_dump_strings(RzBinFile *a, int min, int raw);

// use RzBinFile instead
RZ_API RzList *rz_bin_get_entries(RzBin *bin);
RZ_API RzList *rz_bin_get_fields(RzBin *bin);
RZ_API RzList *rz_bin_get_imports(RzBin *bin);
RZ_API RzList *rz_bin_get_libs(RzBin *bin);
RZ_API RBNode *rz_bin_patch_relocs(RzBin *bin);
RZ_API RzList *rz_bin_patch_relocs_list(RzBin *bin);
RZ_API RBNode *rz_bin_get_relocs(RzBin *bin);
RZ_API RzList *rz_bin_get_relocs_list(RzBin *bin);
RZ_API RzList *rz_bin_get_sections(RzBin *bin);
RZ_API RzList *rz_bin_get_classes(RzBin *bin);
RZ_API RzList *rz_bin_get_strings(RzBin *bin);
RZ_API RZ_DEPRECATE RzList *rz_bin_get_entries(RzBin *bin);
RZ_API RZ_DEPRECATE RzList *rz_bin_get_fields(RzBin *bin);
RZ_API RZ_DEPRECATE RzList *rz_bin_get_imports(RzBin *bin);
RZ_API RZ_DEPRECATE RzList *rz_bin_get_libs(RzBin *bin);
RZ_API RZ_DEPRECATE RBNode *rz_bin_patch_relocs(RzBin *bin);
RZ_API RZ_DEPRECATE RzList *rz_bin_patch_relocs_list(RzBin *bin);
RZ_API RZ_DEPRECATE RBNode *rz_bin_get_relocs(RzBin *bin);
RZ_API RZ_DEPRECATE RzList *rz_bin_get_relocs_list(RzBin *bin);
RZ_API RZ_DEPRECATE RzList *rz_bin_get_sections(RzBin *bin);
RZ_API RZ_DEPRECATE RzList *rz_bin_get_classes(RzBin *bin);
RZ_API RZ_DEPRECATE RzList *rz_bin_get_strings(RzBin *bin);
RZ_API RzList *rz_bin_file_get_trycatch(RzBinFile *bf);
RZ_API RzList *rz_bin_get_symbols(RzBin *bin);
RZ_API RzList *rz_bin_reset_strings(RzBin *bin);
RZ_API int rz_bin_is_string(RzBin *bin, ut64 va);
RZ_API int rz_bin_is_big_endian(RzBin *bin);
RZ_API int rz_bin_is_static(RzBin *bin);
RZ_API ut64 rz_bin_get_vaddr(RzBin *bin, ut64 paddr, ut64 vaddr);
RZ_API RZ_DEPRECATE RzList *rz_bin_get_symbols(RzBin *bin);
RZ_API RZ_DEPRECATE RzList *rz_bin_reset_strings(RzBin *bin);
RZ_API RZ_DEPRECATE int rz_bin_is_string(RzBin *bin, ut64 va);
RZ_API RZ_DEPRECATE int rz_bin_is_big_endian(RzBin *bin);
RZ_API RZ_DEPRECATE int rz_bin_is_static(RzBin *bin);
RZ_API RZ_DEPRECATE ut64 rz_bin_get_vaddr(RzBin *bin, ut64 paddr, ut64 vaddr);
RZ_API ut64 rz_bin_file_get_vaddr(RzBinFile *bf, ut64 paddr, ut64 vaddr);
RZ_API ut64 rz_bin_a2b(RzBin *bin, ut64 addr);

RZ_API int rz_bin_load_languages(RzBinFile *binfile);
RZ_API RzBinFile *rz_bin_cur(RzBin *bin);
Expand Down Expand Up @@ -779,6 +778,7 @@ RZ_API void rz_bin_file_hash_free(RzBinFileHash *fhash);
// binobject functions
RZ_API int rz_bin_object_set_items(RzBinFile *binfile, RzBinObject *o);
RZ_API bool rz_bin_object_delete(RzBin *bin, ut32 binfile_id);
RZ_API ut64 rz_bin_object_addr_with_base(RzBinObject *o, ut64 addr);
RZ_API void rz_bin_mem_free(void *data);

// demangle functions
Expand Down
2 changes: 2 additions & 0 deletions librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ RZ_API int rz_core_bb_starts_in_middle(RzCore *core, ut64 at, int oplen);
RZ_API bool rz_core_bin_raise(RzCore *core, ut32 bfid);

RZ_API bool rz_core_bin_apply_strings(RzCore *r, RzBinFile *binfile);
RZ_API bool rz_core_bin_apply_config(RzCore *r, RzBinFile *binfile);
RZ_API bool rz_core_bin_apply_main(RzCore *r, RzBinFile *binfile, bool va);
RZ_API int rz_core_bin_apply_all_info(RzCore *r, RzBinFile *binfile);
RZ_API int rz_core_bin_set_by_fd(RzCore *core, ut64 bin_fd);
RZ_API int rz_core_bin_set_by_name(RzCore *core, const char *name);
Expand Down

0 comments on commit fbeb51b

Please sign in to comment.