Rizin
unix-like reverse engineering framework and cli tools
libs.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2021 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_core.h>
5 #include <rz_demangler.h>
6 #include "config.h"
7 
8 #define CB(x, y) \
9  static int __lib_##x##_cb(RzLibPlugin *pl, void *user, void *data) { \
10  struct rz_##x##_plugin_t *hand = (struct rz_##x##_plugin_t *)data; \
11  RzCore *core = (RzCore *)user; \
12  pl->free = NULL; \
13  rz_##x##_add(core->y, hand); \
14  return true; \
15  } \
16  static int __lib_##x##_dt(RzLibPlugin *pl, void *p, void *u) { \
17  return true; \
18  }
19 
20 static int __lib_demangler_cb(RzLibPlugin *pl, void *user, void *data) {
21  RzCore *core = (RzCore *)user;
23  return true;
24 }
25 
26 static int __lib_demangler_dt(RzLibPlugin *pl, void *p, void *u) {
27  return true;
28 }
29 
30 static int __lib_core_cb(RzLibPlugin *pl, void *user, void *data) {
31  struct rz_core_plugin_t *hand = (struct rz_core_plugin_t *)data;
32  RzCore *core = (RzCore *)user;
33  pl->free = NULL;
34  rz_core_plugin_add(core, hand);
35  return true;
36 }
37 
38 static int __lib_core_dt(RzLibPlugin *pl, void *p, void *u) {
39  return true;
40 }
41 
42 #define rz_io_add rz_io_plugin_add
43 CB(io, io)
44 #define rz_debug_add rz_debug_plugin_add
45 CB(debug, dbg)
46 #define rz_bp_add rz_bp_plugin_add
47 CB(bp, dbg->bp)
48 CB(lang, lang)
49 CB(analysis, analysis)
50 CB(asm, rasm)
51 CB(parse, parser)
52 #define rz_bin_add rz_bin_plugin_add
53 CB(bin, bin)
54 CB(egg, egg)
55 #define rz_hash_add rz_hash_plugin_add
56 CB(hash, hash)
57 
58 static void loadSystemPlugins(RzCore *core, int where) {
59 #if RZ_LOADLIBS
60  const char *dir_plugins = rz_config_get(core->config, "dir.plugins");
61  if (where & RZ_CORE_LOADLIBS_CONFIG) {
62  rz_lib_opendir(core->lib, dir_plugins, false);
63  }
64  if (where & RZ_CORE_LOADLIBS_ENV) {
65  char *p = rz_sys_getenv(RZ_LIB_ENV);
66  if (p && *p) {
67  rz_lib_opendir(core->lib, p, false);
68  }
69  free(p);
70  }
71  if (where & RZ_CORE_LOADLIBS_HOME) {
72  char *hpd = rz_path_home_prefix(RZ_PLUGINS);
73  rz_lib_opendir(core->lib, hpd, false);
74  free(hpd);
75 
76  // TODO: remove after 0.4.0 is released
78  rz_lib_opendir(core->lib, hpd, false);
79  free(hpd);
80  }
81  if (where & RZ_CORE_LOADLIBS_SYSTEM) {
82  char *spd = rz_path_system(RZ_PLUGINS);
83  rz_lib_opendir(core->lib, spd, false);
84  free(spd);
85  }
86 #endif
87 }
88 
90  ut64 prev = rz_time_now_mono();
91 #define DF(x, y, z) rz_lib_add_handler(core->lib, RZ_LIB_TYPE_##x, y, &__lib_##z##_cb, &__lib_##z##_dt, core);
92  core->lib = rz_lib_new(NULL, NULL);
93  DF(DEMANGLER, "demangler plugins", demangler);
94  DF(IO, "io plugins", io);
95  DF(CORE, "core plugins", core);
96  DF(DBG, "debugger plugins", debug);
97  DF(BP, "debugger breakpoint plugins", bp);
98  DF(LANG, "language plugins", lang);
99  DF(ANALYSIS, "analysis plugins", analysis);
100  DF(ASM, "(dis)assembler plugins", asm);
101  DF(PARSE, "parsing plugins", parse);
102  DF(BIN, "bin plugins", bin);
103  DF(EGG, "egg plugins", egg);
104  DF(HASH, "hash plugins", hash);
105  core->times->loadlibs_init_time = rz_time_now_mono() - prev;
106 }
107 
108 static bool __isScriptFilename(const char *name) {
109  const char *ext = rz_str_lchr(name, '.');
110  if (ext) {
111  ext++;
112  if (!strcmp(ext, "py") || !strcmp(ext, "js") || !strcmp(ext, "lua")) {
113  return true;
114  }
115  }
116  return false;
117 }
118 
119 RZ_API int rz_core_loadlibs(RzCore *core, int where) {
120  ut64 prev = rz_time_now_mono();
121  loadSystemPlugins(core, where);
122  /* TODO: all those default plugin paths should be defined in rz_lib */
123  if (!rz_config_get_i(core->config, "cfg.plugins")) {
124  core->times->loadlibs_time = 0;
125  return false;
126  }
127  // load script plugins
128  char *homeplugindir = rz_path_home_prefix(RZ_PLUGINS);
129  RzList *files = rz_sys_dir(homeplugindir);
130  RzListIter *iter;
131  char *file;
132  rz_list_foreach (files, iter, file) {
133  if (__isScriptFilename(file)) {
134  char *script_file = rz_str_newf("%s/%s", homeplugindir, file);
135  if (!rz_core_run_script(core, script_file)) {
136  eprintf("Cannot find script '%s'\n", script_file);
137  }
138  free(script_file);
139  }
140  }
141 
142  free(homeplugindir);
143  core->times->loadlibs_time = rz_time_now_mono() - prev;
145  return true;
146 }
#define CORE
Definition: aarch64-tbl.h:2166
static const char ext[]
Definition: apprentice.c:1981
RZ_API bool rz_core_run_script(RzCore *core, RZ_NONNULL const char *file)
Definition: cmd.c:457
RZ_API ut64 rz_config_get_i(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:119
RZ_API RZ_BORROW const char * rz_config_get(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:75
#define RZ_API
RZ_API bool rz_core_plugin_add(RzCore *core, RzCorePlugin *plugin)
Definition: cplugin.c:27
#define NULL
Definition: cris-opc.c:27
RzDebug * dbg
Definition: desil.c:30
checking print the parsed form of the magic use in n conjunction with m to debug a new magic file n before installing it n output MIME type special files
Definition: file_opts.h:46
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * p
Definition: libc.cpp:67
RZ_API bool rz_demangler_plugin_add(RZ_NONNULL RzDemangler *dem, RZ_NONNULL RzDemanglerPlugin *plugin)
Adds a new demangler plugin to the plugin list.
Definition: demangler.c:144
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
static int __lib_demangler_cb(RzLibPlugin *pl, void *user, void *data)
Definition: libs.c:20
static int __lib_core_cb(RzLibPlugin *pl, void *user, void *data)
Definition: libs.c:30
RZ_API void rz_core_loadlibs_init(RzCore *core)
Definition: libs.c:89
static int __lib_core_dt(RzLibPlugin *pl, void *p, void *u)
Definition: libs.c:38
static int __lib_demangler_dt(RzLibPlugin *pl, void *p, void *u)
Definition: libs.c:26
#define DF(x, y, z)
static void loadSystemPlugins(RzCore *core, int where)
Definition: libs.c:58
static bool __isScriptFilename(const char *name)
Definition: libs.c:108
#define CB(x, y)
Definition: libs.c:8
RZ_API int rz_core_loadlibs(RzCore *core, int where)
Definition: libs.c:119
#define eprintf(x, y...)
Definition: rlcc.c:7
#define RZ_LIB_ENV
Definition: rz_lib.h:23
RZ_API RzLib * rz_lib_new(const char *symname, const char *symnamefunc)
Definition: lib.c:176
RZ_API bool rz_lib_opendir(RzLib *lib, const char *path, bool force)
Open all the libraries in the given directory, if it wasn't already opened.
Definition: lib.c:417
RZ_API RZ_OWN char * rz_path_home_prefix(RZ_NULLABLE const char *path)
Return path prefixed by the home prefix.
Definition: path.c:182
RZ_API RZ_OWN char * rz_path_system(RZ_NULLABLE const char *path)
Return the full system path of the given subpath path.
Definition: path.c:162
RZ_API const char * rz_str_lchr(const char *str, char chr)
Definition: str.c:669
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API char * rz_sys_getenv(const char *key)
Get the value of an environment variable named key or NULL if none exists.
Definition: sys.c:483
RZ_API RzList * rz_sys_dir(const char *path)
Definition: sys.c:216
RZ_API ut64 rz_time_now_mono(void)
Returns the current time in microseconds, using the monotonic clock.
Definition: time.c:102
#define RZ_PLUGINS
Definition: rz_userconf.h:72
#define RZ_HOME_OLD_PLUGINS
Definition: rz_userconf.h:100
Definition: malloc.c:26
Definition: gzappend.c:170
Definition: z80asm.h:102
Definition: regcomp.c:57
RzDemangler * demangler
Definition: rz_bin.h:364
RzBin * bin
Definition: rz_core.h:298
RzLib * lib
Definition: rz_core.h:318
RzConfig * config
Definition: rz_core.h:300
RzCoreTimes * times
Definition: rz_core.h:325
ut64 loadlibs_init_time
Definition: rz_core.h:178
ut64 loadlibs_time
Definition: rz_core.h:179
RzBreakpoint * bp
Definition: rz_debug.h:288
static int debug
Definition: visual.c:21
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int file
Definition: z80asm.c:58