Rizin
unix-like reverse engineering framework and cli tools
lib.c File Reference
#include <rz_util.h>
#include <rz_lib.h>
#include <rz_windows.h>

Go to the source code of this file.

Classes

struct  rz_lib_type_name_t
 

Typedefs

typedef struct rz_lib_type_name_t RzLibTypeName
 

Functions

 RZ_LIB_VERSION (rz_lib)
 
static const char * __lib_types_get (int id)
 
RZ_API int rz_lib_types_get_i (const char *str)
 
RZ_API void * rz_lib_dl_open (const char *libname)
 
RZ_API void * rz_lib_dl_sym (void *handler, const char *name)
 
RZ_API int rz_lib_dl_close (void *handler)
 
RZ_API char * rz_lib_path (const char *libname)
 
RZ_API RzLibrz_lib_new (const char *symname, const char *symnamefunc)
 
RZ_API void rz_lib_free (RzLib *lib)
 
static bool __lib_dl_check_filename (const char *file)
 
RZ_API int rz_lib_run_handler (RzLib *lib, RzLibPlugin *plugin, RzLibStruct *symbol)
 
RZ_API RzLibHandlerrz_lib_get_handler (RzLib *lib, int type)
 
RZ_API int rz_lib_close (RzLib *lib, const char *file)
 
static bool __already_loaded (RzLib *lib, const char *file)
 
RZ_API int rz_lib_open (RzLib *lib, const char *file)
 
static char * major_minor (const char *s)
 
RZ_API int rz_lib_open_ptr (RzLib *lib, const char *file, void *handler, RzLibStruct *stru)
 
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. More...
 
RZ_API bool rz_lib_add_handler (RzLib *lib, int type, const char *desc, int(*cb)(RzLibPlugin *, void *, void *), int(*dt)(RzLibPlugin *, void *, void *), void *user)
 
RZ_API bool rz_lib_del_handler (RzLib *lib, int type)
 
RZ_API void rz_lib_list (RzLib *lib)
 

Variables

static RzLibTypeName rz_lib_types []
 

Typedef Documentation

◆ RzLibTypeName

Function Documentation

◆ __already_loaded()

static bool __already_loaded ( RzLib lib,
const char *  file 
)
static

Definition at line 266 of file lib.c.

266  {
267  const char *fileName = rz_str_rstr(file, RZ_SYS_DIR);
268  RzLibPlugin *p;
269  RzListIter *iter;
270  if (fileName) {
271  rz_list_foreach (lib->plugins, iter, p) {
272  const char *pFileName = rz_str_rstr(p->file, RZ_SYS_DIR);
273  if (pFileName && !strcmp(fileName, pFileName)) {
274  return true;
275  }
276  }
277  }
278  return false;
279 }
void * p
Definition: libc.cpp:67
RZ_API const char * rz_str_rstr(const char *base, const char *p)
Definition: str.c:814
#define RZ_SYS_DIR
Definition: rz_types.h:218
Definition: gzappend.c:170
RzList * plugins
Definition: rz_lib.h:95

References test-lz4-speed::fileName, p, rz_lib_t::plugins, rz_str_rstr(), and RZ_SYS_DIR.

Referenced by rz_lib_open().

◆ __lib_dl_check_filename()

static bool __lib_dl_check_filename ( const char *  file)
static

Definition at line 202 of file lib.c.

202  {
203  return rz_str_endswith(file, "." RZ_LIB_EXT);
204 }
#define RZ_LIB_EXT
Definition: rz_lib.h:31
RZ_API bool rz_str_endswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string ends with a specifc sequence of characters (case sensitive)
Definition: str.c:3329

References RZ_LIB_EXT, and rz_str_endswith().

Referenced by rz_lib_open(), and rz_lib_opendir().

◆ __lib_types_get()

static const char* __lib_types_get ( int  id)
static

Definition at line 36 of file lib.c.

36  {
37  for (int i = 0; i < RZ_ARRAY_SIZE(rz_lib_types); ++i) {
38  if (id == rz_lib_types[i].id) {
39  return rz_lib_types[i].name;
40  }
41  }
42  return "unk";
43 }
lzma_index ** i
Definition: index.h:629
static RzLibTypeName rz_lib_types[]
Definition: lib.c:15
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300
const char * name
Definition: lib.c:12

References i, rz_lib_type_name_t::name, RZ_ARRAY_SIZE, and rz_lib_types.

Referenced by rz_lib_list().

◆ major_minor()

static char* major_minor ( const char *  s)
static

Definition at line 331 of file lib.c.

331  {
332  char *a = strdup(s);
333  char *p = strchr(a, '.');
334  if (p) {
335  p = strchr(p + 1, '.');
336  if (p) {
337  *p = 0;
338  }
339  }
340  return a;
341 }
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
static RzSocket * s
Definition: rtr.c:28
#define a(i)
Definition: sha256.c:41

References a, p, s, and strdup().

Referenced by rz_lib_open_ptr().

◆ rz_lib_add_handler()

RZ_API bool rz_lib_add_handler ( RzLib lib,
int  type,
const char *  desc,
int(*)(RzLibPlugin *, void *, void *)  cb,
int(*)(RzLibPlugin *, void *, void *)  dt,
void *  user 
)

Definition at line 496 of file lib.c.

500  {
501  RzLibHandler *h;
502  RzListIter *iter;
503  RzLibHandler *handler = NULL;
504 
505  rz_list_foreach (lib->handlers, iter, h) {
506  if (type == h->type) {
507  RZ_LOG_INFO("Redefining library handler constructor for %d\n", type);
508  handler = h;
509  break;
510  }
511  }
512  if (!handler) {
513  handler = RZ_NEW(RzLibHandler);
514  if (!handler) {
515  return false;
516  }
517  handler->type = type;
518  rz_list_append(lib->handlers, handler);
519  }
520  strncpy(handler->desc, desc, sizeof(handler->desc) - 1);
521  handler->user = user;
522  handler->constructor = cb;
523  handler->destructor = dt;
524 
525  return true;
526 }
const char * desc
Definition: bin_vsf.c:19
#define NULL
Definition: cris-opc.c:27
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
int type
Definition: mipsasm.c:17
#define RZ_LOG_INFO(fmtstr,...)
Definition: rz_log.h:54
#define RZ_NEW(x)
Definition: rz_types.h:285
#define h(i)
Definition: sha256.c:48
char desc[128]
Definition: rz_lib.h:49
void * user
Definition: rz_lib.h:50
int(* constructor)(RzLibPlugin *, void *user, void *data)
Definition: rz_lib.h:51
int(* destructor)(RzLibPlugin *, void *user, void *data)
Definition: rz_lib.h:52
RzList * handlers
Definition: rz_lib.h:96
static const char * cb[]
Definition: z80_tab.h:176

References cb, rz_lib_handler_t::constructor, desc, rz_lib_handler_t::desc, rz_lib_handler_t::destructor, h, rz_lib_t::handlers, NULL, rz_list_append(), RZ_LOG_INFO, RZ_NEW, type, rz_lib_handler_t::type, and rz_lib_handler_t::user.

◆ rz_lib_close()

RZ_API int rz_lib_close ( RzLib lib,
const char *  file 
)

Definition at line 227 of file lib.c.

227  {
228  RzLibPlugin *p;
229  RzListIter *iter, *iter2;
230  rz_list_foreach_safe (lib->plugins, iter, iter2, p) {
231  if ((!file || !strcmp(file, p->file))) {
232  int ret = 0;
233  if (p->handler && p->handler->destructor) {
234  ret = p->handler->destructor(p, p->handler->user, p->data);
235  }
236  if (p->free) {
237  p->free(p->data);
238  }
239  free(p->file);
240  rz_list_delete(lib->plugins, iter);
241  if (file != NULL) {
242  return ret;
243  }
244  }
245  }
246  if (!file) {
247  return 0;
248  }
249  // delete similar plugin name
250  rz_list_foreach (lib->plugins, iter, p) {
251  if (strstr(p->file, file)) {
252  int ret = 0;
253  if (p->handler && p->handler->destructor) {
254  ret = p->handler->destructor(p,
255  p->handler->user, p->data);
256  }
257  RZ_LOG_INFO("Unloaded %s\n", p->file);
258  free(p->file);
259  rz_list_delete(lib->plugins, iter);
260  return ret;
261  }
262  }
263  return -1;
264 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_list_delete(RZ_NONNULL RzList *list, RZ_NONNULL RzListIter *iter)
Removes an entry in the list by using the RzListIter pointer.
Definition: list.c:162

References free(), NULL, p, rz_lib_t::plugins, rz_list_delete(), and RZ_LOG_INFO.

Referenced by rz_lib_free(), and rz_plugins_unload_handler().

◆ rz_lib_del_handler()

RZ_API bool rz_lib_del_handler ( RzLib lib,
int  type 
)

Definition at line 528 of file lib.c.

528  {
529  RzLibHandler *h;
530  RzListIter *iter;
531  // TODO: remove all handlers for that type? or only one?
532  /* No _safe loop necessary because we return immediately after the delete. */
533  rz_list_foreach (lib->handlers, iter, h) {
534  if (type == h->type) {
536  return true;
537  }
538  }
539  return false;
540 }

References h, rz_lib_t::handlers, rz_list_delete(), and type.

◆ rz_lib_dl_close()

RZ_API int rz_lib_dl_close ( void *  handler)

Definition at line 104 of file lib.c.

104  {
105 #if __UNIX__
106  return dlclose(handler);
107 #else
108  return handler ? 0 : -1;
109 #endif
110 }

Referenced by _free_source_cb(), lang_lib_file_run(), rz_analysis_esil_load_source(), rz_analysis_esil_release_source(), rz_lib_open(), rz_lib_open_ptr(), rz_main_rz_bin(), and rz_run_start().

◆ rz_lib_dl_open()

RZ_API void* rz_lib_dl_open ( const char *  libname)

Definition at line 54 of file lib.c.

54  {
55  void *ret = NULL;
56 #if WANT_DYLINK
57 #if __UNIX__
58  if (libname) {
59  ret = dlopen(libname, RTLD_GLOBAL | RTLD_LAZY);
60  } else {
61  ret = dlopen(NULL, RTLD_NOW);
62  }
63  if (!ret) {
64  RZ_LOG_ERROR("rz_lib_dl_open: error: %s (%s)\n", libname, dlerror());
65  }
66 #elif __WINDOWS__
67  LPTSTR libname_;
68  if (libname && *libname) {
69  libname_ = rz_sys_conv_utf8_to_win(libname);
70  } else {
71  libname_ = calloc(MAX_PATH, sizeof(TCHAR));
72  if (!libname_) {
73  RZ_LOG_ERROR("lib/rz_lib_dl_open: Failed to allocate memory.\n");
74  return NULL;
75  }
76  if (!GetModuleFileName(NULL, libname_, MAX_PATH)) {
77  libname_[0] = '\0';
78  }
79  }
80  ret = LoadLibrary(libname_);
81  free(libname_);
82  if (!ret) {
83  RZ_LOG_ERROR("rz_lib_dl_open: error: %s\n", libname);
84  }
85 #endif
86 #endif
87  return ret;
88 }
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58

References calloc(), free(), NULL, and RZ_LOG_ERROR.

Referenced by lang_lib_file_run(), rz_analysis_esil_load_source(), rz_lib_open(), rz_main_rz_bin(), rz_run_start(), and rzpipe_open_dl().

◆ rz_lib_dl_sym()

RZ_API void* rz_lib_dl_sym ( void *  handler,
const char *  name 
)

Definition at line 90 of file lib.c.

90  {
91 #if WANT_DYLINK
92 #if __UNIX__
93  return dlsym(handler, name);
94 #elif __WINDOWS__
95  return GetProcAddress(handler, name);
96 #else
97  return NULL;
98 #endif
99 #else
100  return NULL;
101 #endif
102 }
Definition: z80asm.h:102

References NULL.

Referenced by lang_lib_file_run(), rz_analysis_esil_load_interrupts_from_lib(), rz_debug_continue_kill(), rz_lib_open(), rz_run_start(), rz_sys_get_environ(), and rzpipe_open_dl().

◆ rz_lib_free()

RZ_API void rz_lib_free ( RzLib lib)

Definition at line 189 of file lib.c.

189  {
190  if (!lib) {
191  return;
192  }
193  rz_lib_close(lib, NULL);
194  rz_list_free(lib->handlers);
195  rz_list_free(lib->plugins);
196  free(lib->symname);
197  free(lib->symnamefunc);
198  ht_pu_free(lib->opened_dirs);
199  free(lib);
200 }
RZ_API int rz_lib_close(RzLib *lib, const char *file)
Definition: lib.c:227
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
char * symname
Definition: rz_lib.h:93
HtPU * opened_dirs
Hashtable to keep track of already opened directories.
Definition: rz_lib.h:97
char * symnamefunc
Definition: rz_lib.h:94

References free(), rz_lib_t::handlers, NULL, rz_lib_t::opened_dirs, rz_lib_t::plugins, rz_lib_close(), rz_list_free(), rz_lib_t::symname, and rz_lib_t::symnamefunc.

Referenced by __as_free(), hash_load_plugins(), rz_core_fini(), and rz_main_rz_bin().

◆ rz_lib_get_handler()

RZ_API RzLibHandler* rz_lib_get_handler ( RzLib lib,
int  type 
)

Definition at line 216 of file lib.c.

216  {
217  RzLibHandler *h;
218  RzListIter *iter;
219  rz_list_foreach (lib->handlers, iter, h) {
220  if (h->type == type) {
221  return h;
222  }
223  }
224  return NULL;
225 }

References h, rz_lib_t::handlers, NULL, and type.

Referenced by rz_lib_open_ptr().

◆ rz_lib_list()

RZ_API void rz_lib_list ( RzLib lib)

Definition at line 543 of file lib.c.

543  {
544  RzListIter *iter;
545  RzLibPlugin *p;
546  rz_list_foreach (lib->plugins, iter, p) {
547  printf(" %5s %p %s \n", __lib_types_get(p->type), p->dl_handler, p->file);
548  }
549 }
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
static const char * __lib_types_get(int id)
Definition: lib.c:36

References __lib_types_get(), p, rz_lib_t::plugins, and printf().

◆ rz_lib_new()

RZ_API RzLib* rz_lib_new ( const char *  symname,
const char *  symnamefunc 
)

Definition at line 176 of file lib.c.

176  {
177  RzLib *lib = RZ_NEW(RzLib);
178  if (!lib) {
179  return NULL;
180  }
181  lib->handlers = rz_list_newf(free);
182  lib->plugins = rz_list_newf(free);
183  lib->symname = strdup(symname ? symname : RZ_LIB_SYMNAME);
184  lib->symnamefunc = strdup(symnamefunc ? symnamefunc : RZ_LIB_SYMFUNC);
185  lib->opened_dirs = ht_pu_new0();
186  return lib;
187 }
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
#define RZ_LIB_SYMNAME
Definition: rz_lib.h:20
#define RZ_LIB_SYMFUNC
Definition: rz_lib.h:21

References free(), rz_lib_t::handlers, NULL, rz_lib_t::opened_dirs, rz_lib_t::plugins, RZ_LIB_SYMFUNC, RZ_LIB_SYMNAME, rz_list_newf(), RZ_NEW, strdup(), rz_lib_t::symname, and rz_lib_t::symnamefunc.

Referenced by __as_new(), hash_load_plugins(), rz_core_loadlibs_init(), and rz_main_rz_bin().

◆ rz_lib_open()

RZ_API int rz_lib_open ( RzLib lib,
const char *  file 
)

Definition at line 281 of file lib.c.

281  {
282  /* ignored by filename */
284  RZ_LOG_ERROR("Invalid library extension: %s\n", file);
285  return -1;
286  }
287 
288  if (__already_loaded(lib, file)) {
289  RZ_LOG_INFO("Not loading library because it has already been loaded from somewhere else: '%s'\n", file);
290  return -1;
291  }
292 
293  // TODO: remove after 0.4.0 is released
294  if (strstr(file, RZ_HOME_OLD_PLUGINS)) {
295  char *oldhomeplugins = rz_path_home_prefix(RZ_HOME_OLD_PLUGINS);
296  char *homeplugins = rz_path_home_prefix(RZ_PLUGINS);
297  const char *basename = rz_file_basename(file);
298  RZ_LOG_WARN("Loading plugins from '%s' is deprecated, please install plugin '%s' in '%s' instead.\n", oldhomeplugins, basename, homeplugins);
299  free(homeplugins);
300  free(oldhomeplugins);
301  }
302 
303  void *handler = rz_lib_dl_open(file);
304  if (!handler) {
305  RZ_LOG_INFO("Cannot open library: '%s'\n", file);
306  return -1;
307  }
308 
310  RzLibStruct *stru = NULL;
311  if (strf) {
312  stru = strf();
313  }
314  if (!stru) {
315  stru = (RzLibStruct *)rz_lib_dl_sym(handler, lib->symname);
316  }
317  if (!stru) {
318  RZ_LOG_INFO("Cannot find symbol '%s' in library '%s'\n",
319  lib->symname, file);
320  rz_lib_dl_close(handler);
321  return -1;
322  }
323 
324  int res = rz_lib_open_ptr(lib, file, handler, stru);
325  if (strf) {
326  free(stru);
327  }
328  return res;
329 }
#define basename
Definition: libiberty.h:109
RZ_API int rz_lib_open_ptr(RzLib *lib, const char *file, void *handler, RzLibStruct *stru)
Definition: lib.c:343
RZ_API void * rz_lib_dl_sym(void *handler, const char *name)
Definition: lib.c:90
static bool __lib_dl_check_filename(const char *file)
Definition: lib.c:202
RZ_API void * rz_lib_dl_open(const char *libname)
Definition: lib.c:54
static bool __already_loaded(RzLib *lib, const char *file)
Definition: lib.c:266
RZ_API int rz_lib_dl_close(void *handler)
Definition: lib.c:104
RZ_API const char * rz_file_basename(const char *path)
Definition: file.c:83
RzLibStruct *(* RzLibStructFunc)(void)
Definition: rz_lib.h:65
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
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
#define RZ_PLUGINS
Definition: rz_userconf.h:72
#define RZ_HOME_OLD_PLUGINS
Definition: rz_userconf.h:100

References __already_loaded(), __lib_dl_check_filename(), basename, free(), NULL, rz_file_basename(), RZ_HOME_OLD_PLUGINS, rz_lib_dl_close(), rz_lib_dl_open(), rz_lib_dl_sym(), rz_lib_open_ptr(), RZ_LOG_ERROR, RZ_LOG_INFO, RZ_LOG_WARN, rz_path_home_prefix(), RZ_PLUGINS, rz_lib_t::symname, and rz_lib_t::symnamefunc.

Referenced by rz_lib_opendir(), rz_main_rizin(), rz_plugins_io_print_handler(), and rz_plugins_load_handler().

◆ rz_lib_open_ptr()

RZ_API int rz_lib_open_ptr ( RzLib lib,
const char *  file,
void *  handler,
RzLibStruct stru 
)

Definition at line 343 of file lib.c.

343  {
344  rz_return_val_if_fail(lib && file && stru, -1);
345  if (stru->version) {
346  char *mm0 = major_minor(stru->version);
347  char *mm1 = major_minor(RZ_VERSION);
348  bool mismatch = strcmp(mm0, mm1);
349  free(mm0);
350  free(mm1);
351  if (mismatch) {
352  RZ_LOG_WARN("Module version mismatch %s (%s) vs (%s)\n",
353  file, stru->version, RZ_VERSION);
354  if (stru->pkgname) {
355  const char *dot = strchr(stru->version, '.');
356  int major = atoi(stru->version);
357  int minor = dot ? atoi(dot + 1) : 0;
358  // The pkgname member was introduced in 4.2.0
359  if (major > 4 || (major == 4 && minor >= 2)) {
360  printf("rz-pm -ci %s\n", stru->pkgname);
361  }
362  }
363  return -1;
364  }
365  }
366 
367  RzLibHandler *lib_handler = rz_lib_get_handler(lib, stru->type);
368  if (!lib_handler) {
369  // the handler was not assigned for this type therefore
370  // we skip this library (this happens when not loading
371  // all the plugins types, like rz-bin does).
372  RZ_LOG_DEBUG("rz_lib_open_ptr: no handler was defined for %s with type %d\n", file, stru->type);
373  rz_lib_dl_close(handler);
374  return -1;
375  }
376 
378  if (!p) {
379  RZ_LOG_ERROR("rz_lib_open_ptr: Cannot allocate RzLibPlugin\n");
380  rz_lib_dl_close(handler);
381  return -1;
382  }
383 
384  p->type = stru->type;
385  p->data = stru->data;
386  p->file = strdup(file);
387  p->dl_handler = handler;
388  p->handler = lib_handler;
389  p->free = stru->free;
390 
391  int ret = rz_lib_run_handler(lib, p, stru);
392  if (ret == -1) {
393  RZ_LOG_INFO("Library handler has failed for '%s'\n", file);
394  free(p->file);
395  free(p);
396  rz_lib_dl_close(handler);
397  } else {
398  rz_list_append(lib->plugins, p);
399  }
400 
401  return ret;
402 }
mm1
Definition: 3DNow.s.cs:2
#define minor(dev)
Definition: fsmagic.c:57
#define major(dev)
Definition: fsmagic.c:56
RZ_API int rz_lib_run_handler(RzLib *lib, RzLibPlugin *plugin, RzLibStruct *symbol)
Definition: lib.c:206
RZ_API RzLibHandler * rz_lib_get_handler(RzLib *lib, int type)
Definition: lib.c:216
static char * major_minor(const char *s)
Definition: lib.c:331
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define RZ_LOG_DEBUG(fmtstr,...)
Definition: rz_log.h:49
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_lib.h:60
const char * pkgname
Definition: rz_lib.h:62
void * data
Definition: rz_lib.h:59
void(* free)(void *data)
Definition: rz_lib.h:61

References rz_lib_struct_t::data, free(), rz_lib_struct_t::free, major, major_minor(), minor, mm1, p, rz_lib_struct_t::pkgname, rz_lib_t::plugins, printf(), rz_lib_dl_close(), rz_lib_get_handler(), rz_lib_run_handler(), rz_list_append(), RZ_LOG_DEBUG, RZ_LOG_ERROR, RZ_LOG_INFO, RZ_LOG_WARN, RZ_NEW0, rz_return_val_if_fail, RZ_VERSION, strdup(), rz_lib_struct_t::type, and rz_lib_struct_t::version.

Referenced by rz_lib_open().

◆ rz_lib_opendir()

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.

Opens all the files ending with the right library extension (e.g. ".so") present in the directory pointed by path . If path was already opened, it is not opened again unless force is set to true.

Parameters
libReference to RzLib
pathDirectory to open
forceWhen true, a directory is re-scanned even if it was already opened
Returns
True when the directory is scanned for libs, false otherwise

Definition at line 417 of file lib.c.

417  {
418  rz_return_val_if_fail(lib && path, false);
419 
420  if (!force && ht_pu_find(lib->opened_dirs, path, NULL)) {
421  return false;
422  }
423 #if WANT_DYLINK
424  rz_return_val_if_fail(lib && path, false);
425 #if __WINDOWS__
426  wchar_t file[1024];
427  WIN32_FIND_DATAW dir;
428  HANDLE fh;
429  wchar_t directory[MAX_PATH];
430  wchar_t *wcpath;
431  char *wctocbuff;
432 #else
433  char file[1024];
434  struct dirent *de;
435  DIR *dh;
436 #endif
437 #ifdef RZ_LIBR_PLUGINS
438  if (!path) {
439  path = RZ_LIBR_PLUGINS;
440  }
441 #endif
442  if (!path) {
443  return false;
444  }
445 #if __WINDOWS__
446  wcpath = rz_utf8_to_utf16(path);
447  if (!wcpath) {
448  return false;
449  }
450  swprintf(directory, _countof(directory), L"%ls\\*.*", wcpath);
451  fh = FindFirstFileW(directory, &dir);
452  if (fh == INVALID_HANDLE_VALUE) {
453  RZ_LOG_INFO("Cannot open directory %ls\n", wcpath);
454  free(wcpath);
455  return false;
456  }
457  do {
458  swprintf(file, _countof(file), L"%ls/%ls", wcpath, dir.cFileName);
459  wctocbuff = rz_utf16_to_utf8(file);
460  if (wctocbuff) {
461  if (__lib_dl_check_filename(wctocbuff)) {
462  rz_lib_open(lib, wctocbuff);
463  } else {
464  RZ_LOG_INFO("Cannot open %ls\n", dir.cFileName);
465  }
466  free(wctocbuff);
467  }
468  } while (FindNextFileW(fh, &dir));
469  FindClose(fh);
470  free(wcpath);
471 #else
472  dh = opendir(path);
473  if (!dh) {
474  RZ_LOG_INFO("Cannot open directory '%s'\n", path);
475  return false;
476  }
477  while ((de = (struct dirent *)readdir(dh))) {
478  if (de->d_name[0] == '.' || strstr(de->d_name, ".dSYM")) {
479  continue;
480  }
481  snprintf(file, sizeof(file), "%s/%s", path, de->d_name);
483  RZ_LOG_INFO("Loading %s\n", file);
484  rz_lib_open(lib, file);
485  } else {
486  RZ_LOG_INFO("Cannot open %s\n", file);
487  }
488  }
489  closedir(dh);
490 #endif
491 #endif
492  ht_pu_insert(lib->opened_dirs, path, 1);
493  return true;
494 }
FILE * fh
Definition: cabinfo.c:52
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21
snprintf
Definition: kernel.h:364
RZ_API int rz_lib_open(RzLib *lib, const char *file)
Definition: lib.c:281
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid const char static path static newfd static getpgrp static euid const sigset_t static mask const char static len const gid_t static list const char const char static newpath const char static library readdir
Definition: sflib.h:120
directory
Definition: regress.py:17
Definition: sftypes.h:48
char d_name[256]
Definition: sftypes.h:52
DWORD * HANDLE
#define L
Definition: zip_err_str.c:7

References __lib_dl_check_filename(), dirent::d_name, regress::directory, fh, free(), HANDLE, INVALID_HANDLE_VALUE, L, NULL, rz_lib_t::opened_dirs, path, readdir, rz_lib_open(), RZ_LOG_INFO, rz_return_val_if_fail, and snprintf.

Referenced by __load_plugins(), hash_load_plugins(), loadSystemPlugins(), and rz_main_rz_bin().

◆ rz_lib_path()

RZ_API char* rz_lib_path ( const char *  libname)

Definition at line 112 of file lib.c.

112  {
113 #if __WINDOWS__
114  char *tmp = rz_str_newf("%s." RZ_LIB_EXT, libname);
115  if (!tmp) {
116  return NULL;
117  }
118  WCHAR *name = rz_utf8_to_utf16(tmp);
119  free(tmp);
120  WCHAR *path = NULL;
121  if (!name) {
122  goto err;
123  }
124 
125  int count;
126  if (!(count = SearchPathW(NULL, name, NULL, 0, NULL, NULL))) {
127  rz_sys_perror("SearchPath");
128  goto err;
129  }
130  path = malloc(count * sizeof(WCHAR));
131  if (!path) {
132  goto err;
133  }
134  if (!(count = SearchPathW(NULL, name, NULL, count, path, NULL))) {
135  RZ_FREE(path);
136  rz_sys_perror("SearchPath");
137  goto err;
138  }
139  tmp = rz_utf16_to_utf8(path);
140  free(name);
141  free(path);
142  return tmp;
143 err:
144  free(name);
145  return NULL;
146 #else
147 #if __APPLE__
148  char *env = rz_sys_getenv("DYLD_LIBRARY_PATH");
149  env = rz_str_append(env, ":/lib:/usr/lib:/usr/local/lib");
150 #elif __UNIX__
151  char *env = rz_sys_getenv("LD_LIBRARY_PATH");
152  env = rz_str_append(env, ":/lib:/usr/lib:/usr/local/lib");
153 #endif
154  if (!env) {
155  env = strdup(".");
156  }
157  char *next, *path0 = env;
158  do {
159  next = strchr(path0, ':');
160  if (next) {
161  *next = 0;
162  }
163  char *libpath = rz_str_newf("%s/%s." RZ_LIB_EXT, path0, libname);
164  if (rz_file_exists(libpath)) {
165  free(env);
166  return libpath;
167  }
168  free(libpath);
169  path0 = next + 1;
170  } while (next);
171  free(env);
172  return NULL;
173 #endif
174 }
static bool err
Definition: armass.c:435
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
void * malloc(size_t size)
Definition: malloc.c:123
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API char * rz_str_append(char *ptr, const char *string)
Definition: str.c:1063
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
#define rz_sys_perror(x)
Definition: rz_types.h:336
#define RZ_FREE(x)
Definition: rz_types.h:369
static char ** env
Definition: sys.c:32

References count, env, err, free(), malloc(), NULL, path, rz_file_exists(), RZ_FREE, RZ_LIB_EXT, rz_str_append(), rz_str_newf(), rz_sys_getenv(), rz_sys_perror, strdup(), and autogen_x86imm::tmp.

◆ rz_lib_run_handler()

RZ_API int rz_lib_run_handler ( RzLib lib,
RzLibPlugin plugin,
RzLibStruct symbol 
)

Definition at line 206 of file lib.c.

206  {
207  RzLibHandler *h = plugin->handler;
208  if (h && h->constructor) {
209  RZ_LOG_DEBUG("PLUGIN LOADED %p fcn %p\n", h, h->constructor);
210  return h->constructor(plugin, h->user, symbol->data);
211  }
212  RZ_LOG_WARN("Cannot find plugin constructor\n");
213  return -1;
214 }
struct rz_lib_handler_t * handler
Definition: rz_lib.h:39

References rz_lib_struct_t::data, h, rz_lib_plugin_t::handler, RZ_LOG_DEBUG, and RZ_LOG_WARN.

Referenced by rz_lib_open_ptr().

◆ rz_lib_types_get_i()

RZ_API int rz_lib_types_get_i ( const char *  str)

Definition at line 45 of file lib.c.

45  {
46  for (int i = 0; i < RZ_ARRAY_SIZE(rz_lib_types); ++i) {
47  if (!strcmp(str, rz_lib_types[i].name)) {
48  return rz_lib_types[i].id;
49  }
50  }
51  return RZ_LIB_TYPE_UNKNOWN;
52 }
@ RZ_LIB_TYPE_UNKNOWN
Definition: rz_lib.h:86
RzLibType id
Definition: lib.c:11

References i, rz_lib_type_name_t::id, RZ_ARRAY_SIZE, RZ_LIB_TYPE_UNKNOWN, rz_lib_types, and cmd_descs_generate::str.

◆ RZ_LIB_VERSION()

RZ_LIB_VERSION ( rz_lib  )

Variable Documentation

◆ rz_lib_types

RzLibTypeName rz_lib_types[]
static
Initial value:
= {
{ RZ_LIB_TYPE_IO, "io" },
{ RZ_LIB_TYPE_DBG, "dbg" },
{ RZ_LIB_TYPE_LANG, "lang" },
{ RZ_LIB_TYPE_ASM, "asm" },
{ RZ_LIB_TYPE_ANALYSIS, "analysis" },
{ RZ_LIB_TYPE_PARSE, "parse" },
{ RZ_LIB_TYPE_BIN, "bin" },
{ RZ_LIB_TYPE_BIN_XTR, "bin_xtr" },
{ RZ_LIB_TYPE_BIN_LDR, "bin_ldr" },
{ RZ_LIB_TYPE_BP, "bp" },
{ RZ_LIB_TYPE_SYSCALL, "syscall" },
{ RZ_LIB_TYPE_FASTCALL, "fastcall" },
{ RZ_LIB_TYPE_CRYPTO, "crypto" },
{ RZ_LIB_TYPE_HASH, "msgdigest" },
{ RZ_LIB_TYPE_CORE, "core" },
{ RZ_LIB_TYPE_EGG, "egg" },
{ RZ_LIB_TYPE_DEMANGLER, "demangler" },
{ RZ_LIB_TYPE_UNKNOWN, "unknown" },
}
@ RZ_LIB_TYPE_SYSCALL
Definition: rz_lib.h:79
@ RZ_LIB_TYPE_EGG
Definition: rz_lib.h:84
@ RZ_LIB_TYPE_FASTCALL
Definition: rz_lib.h:80
@ RZ_LIB_TYPE_HASH
Definition: rz_lib.h:82
@ RZ_LIB_TYPE_BIN_LDR
Definition: rz_lib.h:77
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
@ RZ_LIB_TYPE_CRYPTO
Definition: rz_lib.h:81
@ RZ_LIB_TYPE_ANALYSIS
Definition: rz_lib.h:73
@ RZ_LIB_TYPE_DBG
Definition: rz_lib.h:70
@ RZ_LIB_TYPE_IO
Definition: rz_lib.h:69
@ RZ_LIB_TYPE_PARSE
Definition: rz_lib.h:74
@ RZ_LIB_TYPE_BIN_XTR
Definition: rz_lib.h:76
@ RZ_LIB_TYPE_LANG
Definition: rz_lib.h:71
@ RZ_LIB_TYPE_BP
Definition: rz_lib.h:78
@ RZ_LIB_TYPE_CORE
Definition: rz_lib.h:83
@ RZ_LIB_TYPE_DEMANGLER
Definition: rz_lib.h:85

Definition at line 15 of file lib.c.

Referenced by __lib_types_get(), and rz_lib_types_get_i().