Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull Applying Bin Config and Main out of rz_core_bin_info() #775

Merged
merged 4 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 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_a2b(o, section->vaddr) : section->paddr;
to = from + (va ? section->vsize : section->size);
if (off >= from && off < to) {
return section;
Expand Down Expand Up @@ -1296,7 +1292,7 @@ RZ_API ut64 rz_bin_get_vaddr(RzBin *bin, ut64 paddr, ut64 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);
return rz_bin_object_a2b(o, addr);
}

RZ_API ut64 rz_bin_get_size(RzBin *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_a2b(RzBinObject *o, ut64 addr) {
thestr4ng3r marked this conversation as resolved.
Show resolved Hide resolved
return o ? addr + o->baddr_shift : addr;
}
141 changes: 83 additions & 58 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_a2b(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 @@ -1363,10 +1391,7 @@ static int bin_main(RzCore *r, PJ *pj, int mode, int va) {
}
addr = va ? rz_bin_a2b(r->bin, 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
37 changes: 19 additions & 18 deletions librz/include/rz_bin.h
Original file line number Diff line number Diff line change
Expand Up @@ -705,26 +705,26 @@ 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 RZ_DEPRECATE 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 +779,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_a2b(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