2014-02-05 23:26:17 +00:00
|
|
|
/* radare - LGPL - Copyright 2008-2014 - pancake */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
#include "r_types.h"
|
2010-06-03 08:57:34 +00:00
|
|
|
#include "r_util.h"
|
2009-02-05 21:08:46 +00:00
|
|
|
#include "r_lib.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2013-06-15 00:56:25 +00:00
|
|
|
R_LIB_VERSION(r_lib);
|
|
|
|
|
2009-02-05 21:08:46 +00:00
|
|
|
/* TODO: support for nested plugins ?? here */
|
|
|
|
|
|
|
|
#if __UNIX__
|
|
|
|
#include <dlfcn.h>
|
2009-03-08 20:41:02 +00:00
|
|
|
#define DLOPEN(x) dlopen(x, RTLD_GLOBAL | RTLD_NOW)
|
2009-02-05 21:08:46 +00:00
|
|
|
#define DLSYM(x,y) dlsym(x,y)
|
|
|
|
#define DLCLOSE(x) dlclose(x)
|
2010-04-12 00:22:52 +00:00
|
|
|
#elif __WINDOWS__
|
2009-02-05 21:08:46 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#define DLOPEN(x) LoadLibrary(x)
|
|
|
|
#define DLSYM(x,y) GetProcAddress(x,y)
|
2010-01-15 12:02:54 +00:00
|
|
|
#define DLCLOSE(x) 0//(x)
|
|
|
|
//CloseLibrary(x)
|
2009-02-05 21:08:46 +00:00
|
|
|
#else
|
|
|
|
#define DLOPEN(x) NULL
|
|
|
|
#define DLSYM(x,y) NULL
|
|
|
|
#define DLCLOSE(x) NULL
|
|
|
|
#endif
|
|
|
|
|
2009-09-17 09:48:36 +00:00
|
|
|
/* XXX : this must be registered in runtime */
|
|
|
|
static const char *r_lib_types[] = {
|
2014-04-25 01:01:42 +00:00
|
|
|
"io", "dbg", "lang", "asm", "anal", "parse", "bin", //"bininfo",
|
2014-03-08 06:41:22 +00:00
|
|
|
"bp", "syscall", "fastcall", "crypto", "cmd", "egg", NULL
|
2009-04-01 00:28:13 +00:00
|
|
|
};
|
|
|
|
|
2013-04-08 07:38:23 +00:00
|
|
|
static int __has_debug = 0;
|
|
|
|
|
|
|
|
#define IFDBG if(__has_debug)
|
2010-06-03 08:57:34 +00:00
|
|
|
|
2009-09-17 09:48:36 +00:00
|
|
|
/* XXX: Rename this helper function */
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API const char *r_lib_types_get(int idx) {
|
2014-04-25 01:14:27 +00:00
|
|
|
if (idx < 0 || idx > R_LIB_TYPE_LAST-1)
|
2009-04-01 01:46:37 +00:00
|
|
|
return "unk";
|
|
|
|
return r_lib_types[idx];
|
2009-04-01 01:40:04 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API void *r_lib_dl_open(const char *libname) {
|
2009-03-31 00:50:02 +00:00
|
|
|
void *ret;
|
2014-06-16 03:58:00 +00:00
|
|
|
if (!libname || !*libname)
|
|
|
|
return NULL;
|
2010-04-12 00:22:52 +00:00
|
|
|
ret = DLOPEN (libname);
|
2013-04-08 07:38:23 +00:00
|
|
|
if (__has_debug && ret == NULL)
|
2010-01-15 12:02:54 +00:00
|
|
|
#if __UNIX__
|
2010-06-03 08:57:34 +00:00
|
|
|
eprintf ("dlerror(%s): %s\n", libname, dlerror ());
|
2010-01-15 12:02:54 +00:00
|
|
|
#else
|
|
|
|
eprintf ("r_lib_dl_open: Cannot open '%s'\n", libname);
|
|
|
|
#endif
|
2009-02-05 21:08:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-26 16:25:35 +00:00
|
|
|
R_API void *r_lib_dl_sym(void *handler, const char *name) {
|
|
|
|
return DLSYM (handler, name);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-05-26 16:25:35 +00:00
|
|
|
R_API int r_lib_dl_close(void *handler) {
|
|
|
|
return DLCLOSE (handler);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ---- */
|
|
|
|
|
2011-08-27 18:25:37 +00:00
|
|
|
R_API char *r_lib_path(const char *libname) {
|
|
|
|
char *next, *path0, libpath[1024];
|
|
|
|
#if __APPLE__
|
|
|
|
char *env = r_sys_getenv ("DYLD_LIBRARY_PATH");
|
|
|
|
const char *ext = ".dylib";
|
|
|
|
env = r_str_concat (env, ":/lib:/usr/lib:/usr/local/lib");
|
|
|
|
#elif __UNIX__
|
|
|
|
char *env = r_sys_getenv ("LD_LIBRARY_PATH");
|
|
|
|
const char *ext = ".so";
|
|
|
|
env = r_str_concat (env, ":/lib:/usr/lib:/usr/local/lib");
|
|
|
|
#else
|
|
|
|
char *env = strdup (".:../../../../../../../windows/system32");
|
|
|
|
const char *ext = ".dll";
|
|
|
|
#endif
|
|
|
|
if (!env) env = strdup (".");
|
|
|
|
path0 = env;
|
|
|
|
do {
|
|
|
|
next = strchr (path0, ':');
|
|
|
|
if (next) *next = 0;
|
|
|
|
snprintf (libpath, sizeof (libpath), "%s/%s%s", path0, libname, ext);
|
|
|
|
//eprintf ("--> %s\n", libpath);
|
2012-09-06 06:59:13 +00:00
|
|
|
if (r_file_exists (libpath)) {
|
2011-08-27 18:25:37 +00:00
|
|
|
free (env);
|
|
|
|
return strdup (libpath);
|
|
|
|
}
|
|
|
|
path0 = next+1;
|
|
|
|
} while (next);
|
|
|
|
free (env);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-20 15:40:58 +00:00
|
|
|
R_API RLib *r_lib_new(const char *symname) {
|
2011-05-13 08:22:28 +00:00
|
|
|
RLib *lib = R_NEW (RLib);
|
2014-03-08 06:41:22 +00:00
|
|
|
char *env_debug;
|
2009-09-17 09:48:36 +00:00
|
|
|
if (lib) {
|
2014-03-08 06:41:22 +00:00
|
|
|
env_debug = r_sys_getenv ("R_DEBUG");
|
|
|
|
__has_debug = env_debug ? R_TRUE : R_FALSE;
|
|
|
|
if (env_debug) {
|
|
|
|
free (env_debug);
|
|
|
|
}
|
2013-11-17 10:25:45 +00:00
|
|
|
lib->handlers = r_list_newf (free);
|
|
|
|
lib->plugins = r_list_newf (free);
|
2010-04-12 00:22:52 +00:00
|
|
|
strncpy (lib->symname, symname, sizeof (lib->symname)-1);
|
2009-09-17 09:48:36 +00:00
|
|
|
}
|
|
|
|
return lib;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API RLib *r_lib_free(RLib *lib) {
|
2012-02-04 22:25:10 +00:00
|
|
|
if (!lib) return NULL;
|
2010-04-12 00:22:52 +00:00
|
|
|
r_lib_close (lib, NULL);
|
2013-11-17 10:25:45 +00:00
|
|
|
r_list_free (lib->handlers);
|
|
|
|
r_list_free (lib->plugins);
|
2009-02-05 21:08:46 +00:00
|
|
|
free (lib);
|
2009-09-17 09:48:36 +00:00
|
|
|
return NULL;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* THIS IS WRONG */
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API int r_lib_dl_check_filename(const char *file) {
|
|
|
|
if (strstr (file, "."R_LIB_EXT))
|
2009-02-05 21:08:46 +00:00
|
|
|
return R_TRUE;
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* high level api */
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API int r_lib_run_handler(RLib *lib, RLibPlugin *plugin, RLibStruct *symbol) {
|
|
|
|
RLibHandler *h = plugin->handler;
|
2014-03-26 00:34:32 +00:00
|
|
|
if (h && h->constructor) {
|
|
|
|
IFDBG eprintf ("PLUGIN HANDLER %p %p\n", h, h->constructor);
|
2011-10-08 21:39:06 +00:00
|
|
|
return h->constructor (plugin, h->user, symbol->data);
|
2014-03-26 00:34:32 +00:00
|
|
|
} else IFDBG eprintf ("Cannot find plugin constructor\n");
|
2009-09-17 09:48:36 +00:00
|
|
|
return R_FAIL;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API RLibHandler *r_lib_get_handler(RLib *lib, int type) {
|
2011-05-13 08:22:28 +00:00
|
|
|
RLibHandler *h;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (lib->handlers, iter, h) {
|
2009-09-17 09:48:36 +00:00
|
|
|
if (h->type == type)
|
2009-02-05 21:08:46 +00:00
|
|
|
return h;
|
|
|
|
}
|
2009-09-17 09:48:36 +00:00
|
|
|
return NULL;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API R_API int r_lib_close(RLib *lib, const char *file) {
|
2011-05-13 08:22:28 +00:00
|
|
|
RLibPlugin *p;
|
|
|
|
RListIter *iter;
|
2012-02-14 17:10:52 +00:00
|
|
|
/* No _safe loop necessary because we return immediately after the delete. */
|
2011-05-13 08:22:28 +00:00
|
|
|
r_list_foreach (lib->plugins, iter, p) {
|
2014-07-29 22:33:54 +00:00
|
|
|
if ((file==NULL || (!strcmp (file, p->file)))) {
|
|
|
|
int ret = 0;
|
|
|
|
if (p->handler && p->handler->constructor) {
|
|
|
|
ret = p->handler->destructor (p,
|
|
|
|
p->handler->user, p->data);
|
|
|
|
}
|
|
|
|
free (p->file);
|
|
|
|
r_list_delete (lib->plugins, iter);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// delete similar plugin name
|
|
|
|
r_list_foreach (lib->plugins, iter, p) {
|
|
|
|
if (strstr (p->file, file)) {
|
|
|
|
int ret = 0;
|
|
|
|
if (p->handler && p->handler->constructor) {
|
|
|
|
ret = p->handler->destructor (p,
|
|
|
|
p->handler->user, p->data);
|
|
|
|
}
|
|
|
|
eprintf ("Unloaded %s\n", p->file);
|
2011-05-13 08:22:28 +00:00
|
|
|
free (p->file);
|
|
|
|
r_list_delete (lib->plugins, iter);
|
2009-02-05 21:08:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-04-01 00:28:13 +00:00
|
|
|
// XXX ugly hack ?
|
2010-04-12 00:22:52 +00:00
|
|
|
static int samefile(const char *a, const char *b) {
|
2011-11-14 21:46:23 +00:00
|
|
|
char *sa = strdup (a);
|
|
|
|
char *sb = strdup (b);
|
2009-04-01 00:28:13 +00:00
|
|
|
char *ptr;
|
2011-11-14 21:46:23 +00:00
|
|
|
int len, ret = R_FALSE;
|
2009-04-01 00:28:13 +00:00
|
|
|
|
2009-04-02 00:07:58 +00:00
|
|
|
if (sa != NULL && sb != NULL) {
|
|
|
|
do {
|
2013-11-28 04:51:09 +00:00
|
|
|
ptr = strstr (sa, "//");
|
2010-11-20 17:35:40 +00:00
|
|
|
if (ptr) {
|
|
|
|
len = strlen (ptr+1) + 1;
|
|
|
|
memmove (ptr, ptr+1, len);
|
|
|
|
}
|
2011-10-08 21:39:06 +00:00
|
|
|
} while (ptr);
|
2009-04-02 00:07:58 +00:00
|
|
|
do {
|
2013-11-28 04:51:09 +00:00
|
|
|
ptr = strstr (sb, "//");
|
2010-11-20 17:35:40 +00:00
|
|
|
if (ptr) {
|
|
|
|
len = strlen (ptr+1) + 1;
|
|
|
|
memmove (ptr, ptr+1, len);
|
|
|
|
}
|
2011-05-13 08:22:28 +00:00
|
|
|
} while (ptr);
|
|
|
|
ret = strcmp (sa,sb)? R_FALSE: R_TRUE;
|
2009-04-02 00:07:58 +00:00
|
|
|
}
|
2009-04-01 00:28:13 +00:00
|
|
|
|
2011-05-13 08:22:28 +00:00
|
|
|
free (sa);
|
|
|
|
free (sb);
|
2009-04-01 00:28:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API int r_lib_open(RLib *lib, const char *file) {
|
|
|
|
RLibPlugin *p;
|
2011-05-13 08:22:28 +00:00
|
|
|
RListIter *iter;
|
2010-04-12 00:22:52 +00:00
|
|
|
RLibStruct *stru;
|
2011-10-08 21:39:06 +00:00
|
|
|
void *handler;
|
2011-10-20 14:36:00 +00:00
|
|
|
int ret = R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
/* ignored by filename */
|
2010-04-12 00:22:52 +00:00
|
|
|
if (!r_lib_dl_check_filename (file)) {
|
|
|
|
eprintf ("Invalid library extension: %s\n", file);
|
2009-09-17 09:48:36 +00:00
|
|
|
return R_FAIL;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
handler = r_lib_dl_open (file);
|
2009-02-05 21:08:46 +00:00
|
|
|
if (handler == NULL) {
|
2010-04-12 00:22:52 +00:00
|
|
|
IFDBG eprintf ("Cannot open library: '%s'\n", file);
|
2009-09-17 09:48:36 +00:00
|
|
|
return R_FAIL;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2014-04-25 01:01:42 +00:00
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
stru = (RLibStruct *) r_lib_dl_sym (handler, lib->symname);
|
2009-02-05 21:08:46 +00:00
|
|
|
if (stru == NULL) {
|
2013-04-08 07:38:23 +00:00
|
|
|
IFDBG eprintf ("Cannot find symbol '%s' in library '%s'\n",
|
|
|
|
lib->symname, file);
|
2014-04-25 01:01:42 +00:00
|
|
|
r_lib_dl_close (handler);
|
2009-09-17 09:48:36 +00:00
|
|
|
return R_FAIL;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2009-09-17 09:48:36 +00:00
|
|
|
|
2014-03-26 00:34:32 +00:00
|
|
|
// TODO: Use Sdb here. just a single line
|
2011-05-13 08:22:28 +00:00
|
|
|
r_list_foreach (lib->plugins, iter, p) {
|
|
|
|
if (samefile (file, p->file)) {
|
2014-03-26 00:34:32 +00:00
|
|
|
IFDBG eprintf ("Dupped\n");
|
2011-05-13 08:22:28 +00:00
|
|
|
r_lib_dl_close (handler);
|
2009-09-17 09:48:36 +00:00
|
|
|
return R_FAIL;
|
2009-04-01 00:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-17 09:48:36 +00:00
|
|
|
|
2014-07-29 22:33:54 +00:00
|
|
|
p = R_NEW0 (RLibPlugin);
|
2009-02-05 21:08:46 +00:00
|
|
|
p->type = stru->type;
|
|
|
|
p->data = stru->data;
|
2011-05-13 08:22:28 +00:00
|
|
|
p->file = strdup (file);
|
2009-02-05 21:08:46 +00:00
|
|
|
p->dl_handler = handler;
|
2011-05-13 08:22:28 +00:00
|
|
|
p->handler = r_lib_get_handler (lib, p->type);
|
2014-04-25 01:01:42 +00:00
|
|
|
|
2011-05-13 08:22:28 +00:00
|
|
|
ret = r_lib_run_handler (lib, p, stru);
|
2009-09-17 09:48:36 +00:00
|
|
|
if (ret == R_FAIL) {
|
2010-06-03 08:57:34 +00:00
|
|
|
IFDBG eprintf ("Library handler has failed for '%s'\n", file);
|
|
|
|
free (p->file);
|
|
|
|
free (p);
|
|
|
|
r_lib_dl_close (handler);
|
2011-05-13 08:22:28 +00:00
|
|
|
} else r_list_append (lib->plugins, p);
|
2009-09-17 09:48:36 +00:00
|
|
|
|
2009-02-05 21:08:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API int r_lib_opendir(RLib *lib, const char *path) {
|
2009-02-05 21:08:46 +00:00
|
|
|
char file[1024];
|
|
|
|
struct dirent *de;
|
2009-03-10 01:49:24 +00:00
|
|
|
DIR *dh;
|
|
|
|
|
2009-03-10 21:58:00 +00:00
|
|
|
#ifdef LIBR_PLUGINS
|
|
|
|
if (path == NULL)
|
|
|
|
path = LIBR_PLUGINS;
|
|
|
|
#endif
|
2009-03-10 01:49:24 +00:00
|
|
|
if (path == NULL)
|
|
|
|
return R_FALSE;
|
2009-04-01 00:28:13 +00:00
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
dh = opendir (path);
|
2009-02-05 21:08:46 +00:00
|
|
|
if (dh == NULL) {
|
2010-04-12 00:22:52 +00:00
|
|
|
IFDBG eprintf ("Cannot open directory '%s'\n", path);
|
2009-03-10 01:49:24 +00:00
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-06-03 08:57:34 +00:00
|
|
|
while ((de = (struct dirent *)readdir (dh))) {
|
2010-04-12 00:22:52 +00:00
|
|
|
snprintf (file, sizeof (file), "%s/%s", path, de->d_name);
|
|
|
|
if (r_lib_dl_check_filename (file))
|
|
|
|
r_lib_open (lib, file);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-04-12 00:22:52 +00:00
|
|
|
closedir (dh);
|
2009-03-10 01:49:24 +00:00
|
|
|
return R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API int r_lib_add_handler(RLib *lib,
|
2009-02-05 21:08:46 +00:00
|
|
|
int type, const char *desc,
|
2010-04-12 00:22:52 +00:00
|
|
|
int (*cb)(RLibPlugin *, void *, void *), /* constructor */
|
|
|
|
int (*dt)(RLibPlugin *, void *, void *), /* destructor */
|
2009-02-05 21:08:46 +00:00
|
|
|
void *user)
|
|
|
|
{
|
2011-05-13 08:22:28 +00:00
|
|
|
RLibHandler *h;
|
|
|
|
RListIter *iter;
|
2010-04-12 00:22:52 +00:00
|
|
|
RLibHandler *handler = NULL;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2011-05-13 08:22:28 +00:00
|
|
|
r_list_foreach (lib->handlers, iter, h) {
|
2009-02-05 21:08:46 +00:00
|
|
|
if (type == h->type) {
|
2010-06-03 08:57:34 +00:00
|
|
|
IFDBG eprintf ("Redefining library handler constructor for %d\n", type);
|
2009-02-05 21:08:46 +00:00
|
|
|
handler = h;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (handler == NULL) {
|
2011-05-13 08:22:28 +00:00
|
|
|
handler = R_NEW (RLibHandler);
|
2009-09-17 09:48:36 +00:00
|
|
|
if (handler == NULL)
|
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
handler->type = type;
|
2011-05-13 08:22:28 +00:00
|
|
|
r_list_append (lib->handlers, handler);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2011-09-18 18:41:36 +00:00
|
|
|
strncpy (handler->desc, desc, sizeof (handler->desc)-1);
|
2009-02-05 21:08:46 +00:00
|
|
|
handler->user = user;
|
|
|
|
handler->constructor = cb;
|
|
|
|
handler->destructor = dt;
|
|
|
|
|
2009-09-17 09:48:36 +00:00
|
|
|
return R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API int r_lib_del_handler(RLib *lib, int type) {
|
2011-05-13 08:22:28 +00:00
|
|
|
RLibHandler *h;
|
|
|
|
RListIter *iter;
|
2009-09-17 09:48:36 +00:00
|
|
|
// TODO: remove all handlers for that type? or only one?
|
2012-02-14 17:10:52 +00:00
|
|
|
/* No _safe loop necessary because we return immediately after the delete. */
|
2011-05-13 08:22:28 +00:00
|
|
|
r_list_foreach (lib->handlers, iter, h) {
|
2009-09-17 09:48:36 +00:00
|
|
|
if (type == h->type) {
|
2011-05-13 08:22:28 +00:00
|
|
|
r_list_delete (lib->handlers, iter);
|
2009-09-17 09:48:36 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
R_API void r_lib_list(RLib *lib) {
|
2011-05-13 08:22:28 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RLibPlugin *p;
|
|
|
|
r_list_foreach (lib->plugins, iter, p) {
|
2011-10-08 21:39:06 +00:00
|
|
|
printf (" %5s %p %s \n", r_lib_types_get (p->type),
|
2014-07-29 22:33:54 +00:00
|
|
|
p->dl_handler, p->file);
|
2009-09-17 09:48:36 +00:00
|
|
|
}
|
|
|
|
}
|