Skip to content

Commit

Permalink
Introduce rz_core_analysis_cc_init_by_path() that allows specifying a…
Browse files Browse the repository at this point in the history
… path to cc data.
  • Loading branch information
arrowd authored and XVilka committed Aug 3, 2023
1 parent 30ba928 commit f3667b0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
27 changes: 16 additions & 11 deletions librz/core/canalysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -5580,7 +5580,7 @@ static void sdb_concat_by_path(Sdb *s, const char *path) {
sdb_free(db);
}

RZ_API void rz_core_analysis_cc_init(RzCore *core) {
RZ_API void rz_core_analysis_cc_init_by_path(RzCore *core, RZ_NULLABLE const char *path, RZ_NULLABLE const char *homepath) {
const char *analysis_arch = rz_config_get(core->config, "analysis.arch");
Sdb *cc = core->analysis->sdb_cc;
if (!strcmp(analysis_arch, "null")) {
Expand All @@ -5589,14 +5589,10 @@ RZ_API void rz_core_analysis_cc_init(RzCore *core) {
return;
}

int bits = core->analysis->bits;
char *types_dir = rz_path_system(RZ_SDB_TYPES);
char *home_types_dir = rz_path_home_prefix(RZ_SDB_TYPES);
char buf[40];
char *dbpath = rz_file_path_join(types_dir, rz_strf(buf, "cc-%s-%d.sdb", analysis_arch, bits));
char *dbhomepath = rz_file_path_join(home_types_dir, rz_strf(buf, "cc-%s-%d.sdb", analysis_arch, bits));
free(types_dir);
free(home_types_dir);
int bits = core->analysis->bits;
char *dbpath = rz_file_path_join(path ? path : "", rz_strf(buf, "cc-%s-%d.sdb", analysis_arch, bits));
char *dbhomepath = rz_file_path_join(homepath ? homepath : "", rz_strf(buf, "cc-%s-%d.sdb", analysis_arch, bits));

// Avoid sdb reloading
if (cc->path && (!strcmp(cc->path, dbpath) || !strcmp(cc->path, dbhomepath))) {
Expand All @@ -5615,6 +5611,9 @@ RZ_API void rz_core_analysis_cc_init(RzCore *core) {
free(cc->path);
cc->path = strdup(dbhomepath);
}
free(dbpath);
free(dbhomepath);

// same as "tcc `arcc`"
char *s = rz_reg_profile_to_cc(core->analysis->reg);
if (s && !rz_analysis_cc_set(core->analysis, s)) {
Expand All @@ -5623,11 +5622,17 @@ RZ_API void rz_core_analysis_cc_init(RzCore *core) {
RZ_LOG_ERROR("core: cannot derive CC from reg profile.\n");
}
free(s);
if (sdb_isempty(core->analysis->sdb_cc)) {
if (sdb_isempty(cc)) {
RZ_LOG_WARN("core: missing calling conventions for '%s'. Deriving it from the regprofile.\n", analysis_arch);
}
free(dbpath);
free(dbhomepath);
}

RZ_API void rz_core_analysis_cc_init(RzCore *core) {
char *types_dir = rz_path_system(RZ_SDB_TYPES);
char *home_types_dir = rz_path_home_prefix(RZ_SDB_TYPES);
rz_core_analysis_cc_init_by_path(core, types_dir, home_types_dir);
free(types_dir);
free(home_types_dir);
}

/**
Expand Down
1 change: 1 addition & 0 deletions librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ RZ_API char *rz_core_analysis_hasrefs_to_depth(RzCore *core, ut64 value, PJ *pj,
RZ_API void rz_core_global_vars_propagate_types(RzCore *core, RzAnalysisFunction *fcn);
RZ_API bool rz_core_analysis_objc_refs(RzCore *core, bool auto_analysis);
RZ_API void rz_core_analysis_objc_stubs(RzCore *core);
RZ_API void rz_core_analysis_cc_init_by_path(RzCore *core, RZ_NULLABLE const char *path, RZ_NULLABLE const char *homepath);
RZ_API void rz_core_analysis_cc_init(RzCore *core);
RZ_API void rz_core_analysis_paths(RzCore *core, ut64 from, ut64 to, bool followCalls, int followDepth, bool is_json);
RZ_API RZ_OWN char *rz_core_types_as_c(RZ_NONNULL RzCore *core, RZ_NONNULL const char *name, bool multiline);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_analysis_var.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ bool test_rz_analysis_var_is_arg() {
RzAnalysis *analysis = core->analysis;
rz_config_set(core->config, "analysis.arch", "x86");
rz_analysis_set_bits(core->analysis, 64);
rz_core_analysis_cc_init(core);
rz_core_analysis_cc_init_by_path(core, TEST_BUILD_TYPES_DIR, NULL);

RzAnalysisFunction *fcn = rz_analysis_create_function(analysis, "fcn", 0x100, RZ_ANALYSIS_FCN_TYPE_FCN);
assert_sane(core->analysis);
Expand Down

0 comments on commit f3667b0

Please sign in to comment.