From f3667b096308d3f199e755d9d675eeaa827d2885 Mon Sep 17 00:00:00 2001 From: Gleb Popov <6yearold@gmail.com> Date: Fri, 21 Jul 2023 21:47:35 +0300 Subject: [PATCH] Introduce rz_core_analysis_cc_init_by_path() that allows specifying a path to cc data. --- librz/core/canalysis.c | 27 ++++++++++++++++----------- librz/include/rz_core.h | 1 + test/unit/test_analysis_var.c | 2 +- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/librz/core/canalysis.c b/librz/core/canalysis.c index 8fa2dc6cbb5..1d59e3c19ea 100644 --- a/librz/core/canalysis.c +++ b/librz/core/canalysis.c @@ -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")) { @@ -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))) { @@ -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)) { @@ -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); } /** diff --git a/librz/include/rz_core.h b/librz/include/rz_core.h index db373f60465..20ed5400739 100644 --- a/librz/include/rz_core.h +++ b/librz/include/rz_core.h @@ -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); diff --git a/test/unit/test_analysis_var.c b/test/unit/test_analysis_var.c index 37559c09e9c..1c89cc8c2ae 100644 --- a/test/unit/test_analysis_var.c +++ b/test/unit/test_analysis_var.c @@ -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);