1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/* radare - LGPL - Copyright 2008-2013 - pancake */
#include "r_types.h"
#include "r_util.h"
#include "r_lib.h"
#include <stdio.h>
#include <dirent.h>
R_LIB_VERSION(r_lib);
/* TODO: support for nested plugins ?? here */
#if __UNIX__
#include <dlfcn.h>
#define DLOPEN(x) dlopen(x, RTLD_GLOBAL | RTLD_NOW)
#define DLSYM(x,y) dlsym(x,y)
#define DLCLOSE(x) dlclose(x)
#elif __WINDOWS__
#include <windows.h>
#define DLOPEN(x) LoadLibrary(x)
#define DLSYM(x,y) GetProcAddress(x,y)
#define DLCLOSE(x) 0//(x)
//CloseLibrary(x)
#else
#define DLOPEN(x) NULL
#define DLSYM(x,y) NULL
#define DLCLOSE(x) NULL
#endif
/* XXX : this must be registered in runtime */
static const char *r_lib_types[] = {
"io", "dbg", "lang", "asm", "anal", "parse", "bin", //"bininfo",
"bp", "syscall", "fastcall", "crypto", "cmd", NULL
};
static int __has_debug = 0;
#define IFDBG if(__has_debug)
/* XXX: Rename this helper function */
R_API const char *r_lib_types_get(int idx) {
if (idx<0||idx>R_LIB_TYPE_LAST)
return "unk";
return r_lib_types[idx];
}
R_API void *r_lib_dl_open(const char *libname) {
void *ret;
ret = DLOPEN (libname);
if (__has_debug && ret == NULL)
#if __UNIX__
eprintf ("dlerror(%s): %s\n", libname, dlerror ());
#else
eprintf ("r_lib_dl_open: Cannot open '%s'\n", libname);
#endif
return ret;
}
R_API void *r_lib_dl_sym(void *handler, const char *name) {
return DLSYM (handler, name);
}
R_API int r_lib_dl_close(void *handler) {
return DLCLOSE (handler);
}
/* ---- */
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);
if (r_file_exists (libpath)) {
free (env);
return strdup (libpath);
}
path0 = next+1;
} while (next);
free (env);
return NULL;
}
R_API RLib *r_lib_new(const char *symname) {
RLib *lib = R_NEW (RLib);
if (lib) {
__has_debug = r_sys_getenv ("R_DEBUG")?R_TRUE:R_FALSE;
lib->handlers = r_list_new ();
lib->plugins = r_list_new ();
strncpy (lib->symname, symname, sizeof (lib->symname)-1);
}
return lib;
}
R_API RLib *r_lib_free(RLib *lib) {
if (!lib) return NULL;
/* TODO: iterate over libraries and free them all */
/* TODO: iterate over handlers and free them all */
r_lib_close (lib, NULL);
free (lib);
return NULL;
}
/* THIS IS WRONG */
R_API int r_lib_dl_check_filename(const char *file) {
if (strstr (file, "."R_LIB_EXT))
return R_TRUE;
return R_FALSE;
}
/* high level api */
R_API int r_lib_run_handler(RLib *lib, RLibPlugin *plugin, RLibStruct *symbol) {
RLibHandler *h = plugin->handler;
if (h && h->constructor)
return h->constructor (plugin, h->user, symbol->data);
return R_FAIL;
}
R_API RLibHandler *r_lib_get_handler(RLib *lib, int type) {
RLibHandler *h;
RListIter *iter;
r_list_foreach (lib->handlers, iter, h) {
if (h->type == type)
return h;
}
return NULL;
}
R_API R_API int r_lib_close(RLib *lib, const char *file) {
RLibPlugin *p;
RListIter *iter;
/* No _safe loop necessary because we return immediately after the delete. */
r_list_foreach (lib->plugins, iter, p) {
if ((file==NULL || (!strcmp(file, p->file))) && p->handler->destructor != NULL) {
int ret = p->handler->destructor (p, p->handler->user, p->data);
free (p->file);
r_list_delete (lib->plugins, iter);
free (p);
return ret;
}
}
return -1;
}
// XXX ugly hack ?
static int samefile(const char *a, const char *b) {
char *sa = strdup (a);
char *sb = strdup (b);
char *ptr;
int len, ret = R_FALSE;
if (sa != NULL && sb != NULL) {
do {
ptr = strstr(sa, "//");
if (ptr) {
len = strlen (ptr+1) + 1;
memmove (ptr, ptr+1, len);
}
} while (ptr);
do {
ptr = strstr(sb, "//");
if (ptr) {
len = strlen (ptr+1) + 1;
memmove (ptr, ptr+1, len);
}
} while (ptr);
ret = strcmp (sa,sb)? R_FALSE: R_TRUE;
}
free (sa);
free (sb);
return ret;
}
R_API int r_lib_open(RLib *lib, const char *file) {
RLibPlugin *p;
RListIter *iter;
RLibStruct *stru;
void *handler;
int ret = R_FALSE;
/* ignored by filename */
if (!r_lib_dl_check_filename (file)) {
eprintf ("Invalid library extension: %s\n", file);
return R_FAIL;
}
handler = r_lib_dl_open (file);
if (handler == NULL) {
IFDBG eprintf ("Cannot open library: '%s'\n", file);
return R_FAIL;
}
stru = (RLibStruct *) r_lib_dl_sym (handler, lib->symname);
if (stru == NULL) {
IFDBG eprintf ("Cannot find symbol '%s' in library '%s'\n",
lib->symname, file);
return R_FAIL;
}
r_list_foreach (lib->plugins, iter, p) {
if (samefile (file, p->file)) {
r_lib_dl_close (handler);
return R_FAIL;
}
}
p = R_NEW (RLibPlugin);
p->type = stru->type;
p->data = stru->data;
p->file = strdup (file);
p->dl_handler = handler;
p->handler = r_lib_get_handler (lib, p->type);
ret = r_lib_run_handler (lib, p, stru);
if (ret == R_FAIL) {
IFDBG eprintf ("Library handler has failed for '%s'\n", file);
free (p->file);
free (p);
r_lib_dl_close (handler);
} else r_list_append (lib->plugins, p);
return ret;
}
R_API int r_lib_opendir(RLib *lib, const char *path) {
char file[1024];
struct dirent *de;
DIR *dh;
#ifdef LIBR_PLUGINS
if (path == NULL)
path = LIBR_PLUGINS;
#endif
if (path == NULL)
return R_FALSE;
dh = opendir (path);
if (dh == NULL) {
IFDBG eprintf ("Cannot open directory '%s'\n", path);
return R_FALSE;
}
while ((de = (struct dirent *)readdir (dh))) {
snprintf (file, sizeof (file), "%s/%s", path, de->d_name);
if (r_lib_dl_check_filename (file))
r_lib_open (lib, file);
}
closedir (dh);
return R_TRUE;
}
R_API int r_lib_add_handler(RLib *lib,
int type, const char *desc,
int (*cb)(RLibPlugin *, void *, void *), /* constructor */
int (*dt)(RLibPlugin *, void *, void *), /* destructor */
void *user)
{
RLibHandler *h;
RListIter *iter;
RLibHandler *handler = NULL;
r_list_foreach (lib->handlers, iter, h) {
if (type == h->type) {
IFDBG eprintf ("Redefining library handler constructor for %d\n", type);
handler = h;
break;
}
}
if (handler == NULL) {
handler = R_NEW (RLibHandler);
if (handler == NULL)
return R_FALSE;
handler->type = type;
r_list_append (lib->handlers, handler);
}
strncpy (handler->desc, desc, sizeof (handler->desc)-1);
handler->user = user;
handler->constructor = cb;
handler->destructor = dt;
return R_TRUE;
}
R_API int r_lib_del_handler(RLib *lib, int type) {
RLibHandler *h;
RListIter *iter;
// TODO: remove all handlers for that type? or only one?
/* No _safe loop necessary because we return immediately after the delete. */
r_list_foreach (lib->handlers, iter, h) {
if (type == h->type) {
r_list_delete (lib->handlers, iter);
return R_TRUE;
}
}
return R_FALSE;
}
/* XXX _list methods must be deprecated before r2-1.0 */
R_API void r_lib_list(RLib *lib) {
RListIter *iter;
RLibPlugin *p;
#if 0
printf("Plugin Plugins:\n");
list_for_each_prev(pos, &lib->handlers) {
RLibHandler *h = list_entry(pos, RLibHandler, list);
printf(" - %d: %s\n", h->type, h->desc);
}
#endif
//printf("Loaded plugins:\n");
r_list_foreach (lib->plugins, iter, p) {
printf (" %5s %p %s \n", r_lib_types_get (p->type),
p->handler->destructor, p->file);
}
}
|