| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* libraries.d - shared library and bundle opening / copying / closing |
| 6 |
* |
| 7 |
* Copyright (c) 1990 Giuseppe Attardi |
| 8 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 9 |
* |
| 10 |
* See file 'LICENSE' for the copyright details. |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <ecl/ecl.h> |
| 15 |
/* |
| 16 |
* Choosing the interface for loading binary files. Currently we recognize |
| 17 |
* three different methods: |
| 18 |
* - Windows API, provided by ECL_MS_WINDOWS_HOST |
| 19 |
* - dlopen, provided HAVE_DLFCN_H is defined |
| 20 |
* - NSLinkModule, provided HAVE_MACH_O_DYLD_H is defined |
| 21 |
* They are chosen in this precise order. In order to make the code for these |
| 22 |
* methods mutually exclusive, when one method is present, the other macros |
| 23 |
* get undefined. Handling of dynamically loaded libraries is constrained to |
| 24 |
* this file and thus the changes can be limited to this file. |
| 25 |
*/ |
| 26 |
#ifdef ECL_MS_WINDOWS_HOST |
| 27 |
# ifdef HAVE_DLFCN_H |
| 28 |
# undef HAVE_DLFCN_H |
| 29 |
# endif |
| 30 |
# ifdef HAVE_MACH_O_DYLD_H |
| 31 |
# undef HAVE_MACH_O_DYLD_H |
| 32 |
# endif |
| 33 |
#endif |
| 34 |
#ifdef HAVE_DLFCN_H |
| 35 |
# ifdef HAVE_MACH_O_DYLD_H |
| 36 |
# undef HAVE_MACH_O_DYLD_H |
| 37 |
# endif |
| 38 |
#endif |
| 39 |
#ifdef ENABLE_DLOPEN |
| 40 |
# ifdef cygwin |
| 41 |
# include <w32api/windows.h> |
| 42 |
# include <sys/stat.h> |
| 43 |
# endif |
| 44 |
# ifdef HAVE_DLFCN_H |
| 45 |
# include <dlfcn.h> |
| 46 |
# define INIT_PREFIX "init_fas_" |
| 47 |
# endif |
| 48 |
# ifdef HAVE_MACH_O_DYLD_H |
| 49 |
# include <mach-o/dyld.h> |
| 50 |
# define INIT_PREFIX "_init_fas_" |
| 51 |
# endif |
| 52 |
# ifdef HAVE_LINK_H |
| 53 |
# include <link.h> |
| 54 |
# endif |
| 55 |
# if defined(ECL_MS_WINDOWS_HOST) |
| 56 |
# include <windows.h> |
| 57 |
# include <windef.h> |
| 58 |
# include <winbase.h> |
| 59 |
# include <tlhelp32.h> |
| 60 |
# define INIT_PREFIX "init_fas_" |
| 61 |
# else |
| 62 |
# include <unistd.h> |
| 63 |
# endif |
| 64 |
#endif /* ENABLE_DLOPEN */ |
| 65 |
#include <ecl/ecl-inl.h> |
| 66 |
#include <ecl/internal.h> |
| 67 |
|
| 68 |
#define GC_call_with_alloc_lock(f,arg) f(arg) |
| 69 |
|
| 70 |
cl_object |
| 71 |
ecl_make_codeblock() |
| 72 |
{ |
| 73 |
cl_object block = ecl_alloc_object(t_codeblock); |
| 74 |
block->cblock.self_destruct = 0; |
| 75 |
block->cblock.locked = 0; |
| 76 |
block->cblock.handle = NULL; |
| 77 |
block->cblock.data = NULL; |
| 78 |
block->cblock.data_size = 0; |
| 79 |
block->cblock.temp_data = NULL; |
| 80 |
block->cblock.temp_data_size = 0; |
| 81 |
block->cblock.data_text = NULL; |
| 82 |
block->cblock.next = ECL_NIL; |
| 83 |
block->cblock.name = ECL_NIL; |
| 84 |
block->cblock.links = ECL_NIL; |
| 85 |
block->cblock.cfuns_size = 0; |
| 86 |
block->cblock.cfuns = NULL; |
| 87 |
block->cblock.source = ECL_NIL; |
| 88 |
block->cblock.error = ECL_NIL; |
| 89 |
block->cblock.refs = ecl_make_fixnum(0); |
| 90 |
si_set_finalizer(block, ECL_T); |
| 91 |
return block; |
| 92 |
} |
| 93 |
|
| 94 |
static cl_object |
| 95 |
copy_object_file(cl_object original) |
| 96 |
{ |
| 97 |
int err; |
| 98 |
cl_object copy = @"TMP:ECL"; |
| 99 |
copy = si_coerce_to_filename(si_mkstemp(copy)); |
| 100 |
/* |
| 101 |
* We either have to make a full copy to convince the loader to load this object |
| 102 |
* file again, or we want to retain the possibility of overwriting the object |
| 103 |
* file we load later on (case of Windows, which locks files that are loaded). |
| 104 |
* The symlinks do not seem to work in latest versions of Linux. |
| 105 |
*/ |
| 106 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 107 |
ecl_disable_interrupts(); |
| 108 |
err = !ecl_CopyFile(ecl_filename_self(original), ecl_filename_self(copy), 0); |
| 109 |
ecl_enable_interrupts(); |
| 110 |
if (err) { |
| 111 |
FEwin32_error("Error when copying file from~&~3T~A~&to~&~3T~A", |
| 112 |
2, original, copy); |
| 113 |
} |
| 114 |
#else |
| 115 |
err = Null(si_copy_file(ecl_decode_filename(original,ECL_NIL), copy)); |
| 116 |
if (err) { |
| 117 |
FEerror("Error when copying file from~&~3T~A~&to~&~3T~A", |
| 118 |
2, original, copy); |
| 119 |
} |
| 120 |
#endif |
| 121 |
#ifdef cygwin |
| 122 |
{ |
| 123 |
cl_object new_copy = @".dll"; |
| 124 |
new_copy = ecl_concatenate_filename(copy, new_copy); |
| 125 |
cl_rename_file(2, ecl_decode_filename(copy,ECL_NIL), ecl_decode_filename(new_copy,ECL_NIL)); |
| 126 |
copy = new_copy; |
| 127 |
} |
| 128 |
ecl_disable_interrupts(); |
| 129 |
err = ecl_chmod(ecl_filename_self(copy), S_IRWXU) < 0; |
| 130 |
ecl_enable_interrupts(); |
| 131 |
if (err) { |
| 132 |
FElibc_error("Unable to give executable permissions to ~A", |
| 133 |
1, copy); |
| 134 |
} |
| 135 |
#endif |
| 136 |
return copy; |
| 137 |
} |
| 138 |
|
| 139 |
#ifdef ENABLE_DLOPEN |
| 140 |
|
| 141 |
static void |
| 142 |
set_library_error(cl_object block) { |
| 143 |
cl_object output; |
| 144 |
ecl_disable_interrupts(); |
| 145 |
#ifdef HAVE_DLFCN_H |
| 146 |
output = ecl_cstring_to_base_string_or_nil(dlerror()); |
| 147 |
#endif |
| 148 |
#ifdef HAVE_MACH_O_DYLD_H |
| 149 |
{ |
| 150 |
NSLinkEditErrors c; |
| 151 |
int number; |
| 152 |
const char *filename; |
| 153 |
NSLinkEditError(&c, &number, &filename, &message); |
| 154 |
output = ecl_make_simple_base_string(message,-1); |
| 155 |
} |
| 156 |
#endif |
| 157 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 158 |
{ |
| 159 |
const char *message; |
| 160 |
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | |
| 161 |
FORMAT_MESSAGE_ALLOCATE_BUFFER, |
| 162 |
0, GetLastError(), 0, (void*)&message, 0, NULL); |
| 163 |
output = ecl_make_simple_base_string(message,-1); |
| 164 |
LocalFree(message); |
| 165 |
} |
| 166 |
#endif |
| 167 |
ecl_enable_interrupts(); |
| 168 |
block->cblock.error = output; |
| 169 |
} |
| 170 |
|
| 171 |
static void |
| 172 |
dlopen_wrapper(cl_object block) |
| 173 |
{ |
| 174 |
cl_object filename = block->cblock.name; |
| 175 |
ecl_filename_char *filename_string = ecl_filename_self(filename); |
| 176 |
#ifdef HAVE_DLFCN_H |
| 177 |
block->cblock.handle = dlopen(filename_string, RTLD_NOW|RTLD_GLOBAL); |
| 178 |
#endif |
| 179 |
#ifdef HAVE_MACH_O_DYLD_H |
| 180 |
{ |
| 181 |
NSObjectFileImage file; |
| 182 |
static NSObjectFileImageReturnCode code; |
| 183 |
code = NSCreateObjectFileImageFromFile(filename_string, &file); |
| 184 |
if (code != NSObjectFileImageSuccess) { |
| 185 |
block->cblock.handle = NULL; |
| 186 |
} else { |
| 187 |
NSModule out = NSLinkModule(file, filename_string, |
| 188 |
NSLINKMODULE_OPTION_PRIVATE| |
| 189 |
NSLINKMODULE_OPTION_BINDNOW| |
| 190 |
NSLINKMODULE_OPTION_RETURN_ON_ERROR); |
| 191 |
block->cblock.handle = out; |
| 192 |
}} |
| 193 |
#endif |
| 194 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 195 |
block->cblock.handle = ecl_LoadLibrary(filename_string); |
| 196 |
#endif |
| 197 |
if (block->cblock.handle == NULL) |
| 198 |
set_library_error(block); |
| 199 |
} |
| 200 |
|
| 201 |
static int |
| 202 |
dlclose_wrapper(cl_object block) |
| 203 |
{ |
| 204 |
if (block->cblock.handle != NULL) { |
| 205 |
#ifdef HAVE_DLFCN_H |
| 206 |
dlclose(block->cblock.handle); |
| 207 |
#endif |
| 208 |
#ifdef HAVE_MACH_O_DYLD_H |
| 209 |
NSUnLinkModule(block->cblock.handle, NSUNLINKMODULE_OPTION_NONE); |
| 210 |
#endif |
| 211 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 212 |
FreeLibrary(block->cblock.handle); |
| 213 |
#endif |
| 214 |
block->cblock.handle = NULL; |
| 215 |
return TRUE; |
| 216 |
} |
| 217 |
return FALSE; |
| 218 |
} |
| 219 |
|
| 220 |
static cl_object |
| 221 |
ecl_library_find_by_name(cl_object filename) |
| 222 |
{ |
| 223 |
cl_object l; |
| 224 |
for (l = cl_core.libraries; l != ECL_NIL; l = ECL_CONS_CDR(l)) { |
| 225 |
cl_object other = ECL_CONS_CAR(l); |
| 226 |
cl_object name = other->cblock.name; |
| 227 |
if (!Null(name) && ecl_string_eq(name, filename)) { |
| 228 |
return other; |
| 229 |
} |
| 230 |
} |
| 231 |
return ECL_NIL; |
| 232 |
} |
| 233 |
|
| 234 |
static cl_object |
| 235 |
ecl_library_find_by_handle(void *handle) |
| 236 |
{ |
| 237 |
cl_object l; |
| 238 |
for (l = cl_core.libraries; l != ECL_NIL; l = ECL_CONS_CDR(l)) { |
| 239 |
cl_object other = ECL_CONS_CAR(l); |
| 240 |
if (handle == other->cblock.handle) { |
| 241 |
return other; |
| 242 |
} |
| 243 |
} |
| 244 |
return ECL_NIL; |
| 245 |
} |
| 246 |
|
| 247 |
static cl_object |
| 248 |
ecl_library_open_inner(cl_object filename, bool self_destruct) |
| 249 |
{ |
| 250 |
const cl_env_ptr the_env = ecl_process_env(); |
| 251 |
cl_object block = ecl_make_codeblock(); |
| 252 |
block->cblock.self_destruct = self_destruct; |
| 253 |
block->cblock.name = filename; |
| 254 |
block->cblock.refs = ecl_make_fixnum(1); |
| 255 |
|
| 256 |
ECL_WITH_GLOBAL_LOCK_BEGIN(the_env) { |
| 257 |
ecl_disable_interrupts(); |
| 258 |
GC_call_with_alloc_lock(dlopen_wrapper, block); |
| 259 |
if (block->cblock.handle != NULL) { |
| 260 |
/* Have we already loaded this library? If so, then unload this |
| 261 |
* copy and increase the reference counter so that we can keep |
| 262 |
* track (in lisp) of how many copies we use. |
| 263 |
*/ |
| 264 |
cl_object other = ecl_library_find_by_handle(block->cblock.handle); |
| 265 |
if (other != ECL_NIL) { |
| 266 |
GC_call_with_alloc_lock(dlclose_wrapper, block); |
| 267 |
block = other; |
| 268 |
block->cblock.refs = ecl_one_plus(block->cblock.refs); |
| 269 |
} else { |
| 270 |
si_set_finalizer(block, ECL_T); |
| 271 |
cl_core.libraries = CONS(block, cl_core.libraries); |
| 272 |
} |
| 273 |
} |
| 274 |
ecl_enable_interrupts(); |
| 275 |
} ECL_WITH_GLOBAL_LOCK_END; |
| 276 |
return block; |
| 277 |
} |
| 278 |
|
| 279 |
cl_object |
| 280 |
ecl_library_open(cl_object filename, bool force_reload) { |
| 281 |
cl_object block; |
| 282 |
bool self_destruct = 0; |
| 283 |
|
| 284 |
/* Coerces to a file name but does not merge with cwd */ |
| 285 |
filename = si_coerce_to_physical_pathname(filename); |
| 286 |
filename = ecl_namestring(filename, |
| 287 |
ECL_NAMESTRING_TRUNCATE_IF_ERROR | |
| 288 |
ECL_NAMESTRING_FORCE_BASE_STRING); |
| 289 |
|
| 290 |
if (!force_reload) { |
| 291 |
/* When loading a foreign library, such as a dll or a |
| 292 |
* so, it cannot contain any executable top level |
| 293 |
* code. In that case force_reload=0 and there is no |
| 294 |
* need to reload it if it has already been loaded. */ |
| 295 |
block = ecl_library_find_by_name(filename); |
| 296 |
if (!Null(block)) { |
| 297 |
return block; |
| 298 |
} |
| 299 |
} else { |
| 300 |
/* We are using shared libraries as modules and |
| 301 |
* force_reload=1. Here we have to face the problem |
| 302 |
* that many operating systems do not allow to load a |
| 303 |
* shared library twice, even if it has changed. Hence |
| 304 |
* we have to make a unique copy to be able to load |
| 305 |
* the same FASL twice. In Windows this copy is |
| 306 |
* _always_ made because otherwise it cannot be |
| 307 |
* overwritten. In Unix we need only do that when the |
| 308 |
* file has been previously loaded. */ |
| 309 |
#if defined(ECL_MS_WINDOWS_HOST) || defined(cygwin) |
| 310 |
filename = copy_object_file(filename); |
| 311 |
self_destruct = 1; |
| 312 |
#else |
| 313 |
block = ecl_library_find_by_name(filename); |
| 314 |
if (!Null(block)) { |
| 315 |
filename = copy_object_file(filename); |
| 316 |
self_destruct = 1; |
| 317 |
} |
| 318 |
#endif |
| 319 |
} |
| 320 |
DO_LOAD: |
| 321 |
block = ecl_library_open_inner(filename, self_destruct); |
| 322 |
/* |
| 323 |
* A second pass to ensure that the dlopen routine has not |
| 324 |
* returned a library that we had already loaded. If this is |
| 325 |
* the case, we close the new copy to ensure we do refcounting |
| 326 |
* right. |
| 327 |
*/ |
| 328 |
if (block->cblock.refs != ecl_make_fixnum(1)) { |
| 329 |
if (force_reload) { |
| 330 |
ecl_library_close(block); |
| 331 |
filename = copy_object_file(filename); |
| 332 |
self_destruct = 1; |
| 333 |
goto DO_LOAD; |
| 334 |
} |
| 335 |
} |
| 336 |
return block; |
| 337 |
} |
| 338 |
|
| 339 |
void * |
| 340 |
ecl_library_symbol(cl_object block, const char *symbol, bool lock) { |
| 341 |
void *p; |
| 342 |
if (block == @':default') { |
| 343 |
cl_object l; |
| 344 |
for (l = cl_core.libraries; l != ECL_NIL; l = ECL_CONS_CDR(l)) { |
| 345 |
cl_object block = ECL_CONS_CAR(l); |
| 346 |
p = ecl_library_symbol(block, symbol, lock); |
| 347 |
if (p) return p; |
| 348 |
} |
| 349 |
ecl_disable_interrupts(); |
| 350 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 351 |
{ |
| 352 |
HANDLE hndSnap = NULL; |
| 353 |
HANDLE hnd = NULL; |
| 354 |
hndSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId()); |
| 355 |
if (hndSnap != INVALID_HANDLE_VALUE) |
| 356 |
{ |
| 357 |
MODULEENTRY32 me32; |
| 358 |
me32.dwSize = sizeof(MODULEENTRY32); |
| 359 |
if (Module32First(hndSnap, &me32)) |
| 360 |
{ |
| 361 |
do |
| 362 |
hnd = GetProcAddress(me32.hModule, symbol); |
| 363 |
while (hnd == NULL && Module32Next(hndSnap, &me32)); |
| 364 |
} |
| 365 |
CloseHandle(hndSnap); |
| 366 |
} |
| 367 |
p = (void*)hnd; |
| 368 |
} |
| 369 |
#endif |
| 370 |
#ifdef HAVE_DLFCN_H |
| 371 |
p = dlsym(0, symbol); |
| 372 |
#endif |
| 373 |
#if !defined(ECL_MS_WINDOWS_HOST) && !defined(HAVE_DLFCN_H) |
| 374 |
p = 0; |
| 375 |
#endif |
| 376 |
ecl_enable_interrupts(); |
| 377 |
} else { |
| 378 |
ecl_disable_interrupts(); |
| 379 |
#ifdef HAVE_DLFCN_H |
| 380 |
p = dlsym(block->cblock.handle, symbol); |
| 381 |
#endif |
| 382 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 383 |
{ |
| 384 |
HMODULE h = (HMODULE)(block->cblock.handle); |
| 385 |
p = GetProcAddress(h, symbol); |
| 386 |
} |
| 387 |
#endif |
| 388 |
#ifdef HAVE_MACH_O_DYLD_H |
| 389 |
NSSymbol sym; |
| 390 |
sym = NSLookupSymbolInModule((NSModule)(block->cblock.handle), |
| 391 |
symbol); |
| 392 |
if (sym == 0) { |
| 393 |
p = 0; |
| 394 |
} else { |
| 395 |
p = NSAddressOfSymbol(sym); |
| 396 |
} |
| 397 |
#endif |
| 398 |
ecl_enable_interrupts(); |
| 399 |
/* Libraries whose symbols are being referenced by the FFI should not |
| 400 |
* get garbage collected. Until we find a better solution we simply lock |
| 401 |
* them for the rest of the runtime */ |
| 402 |
if (p) { |
| 403 |
block->cblock.locked |= lock; |
| 404 |
} |
| 405 |
} |
| 406 |
if (!p) |
| 407 |
set_library_error(block); |
| 408 |
return p; |
| 409 |
} |
| 410 |
|
| 411 |
cl_object |
| 412 |
ecl_library_error(cl_object block) { |
| 413 |
return block->cblock.error; |
| 414 |
} |
| 415 |
|
| 416 |
bool |
| 417 |
ecl_library_close(cl_object block) { |
| 418 |
const cl_env_ptr the_env = ecl_process_env(); |
| 419 |
bool success = TRUE; |
| 420 |
ECL_WITH_GLOBAL_LOCK_BEGIN(the_env) { |
| 421 |
ecl_disable_interrupts(); |
| 422 |
/* is it ever a case? no matter how many times i call |
| 423 |
load-foreign-module it seems that block->cblock.refs = 1 */ |
| 424 |
if (block->cblock.refs > ecl_make_fixnum(1)) { |
| 425 |
block->cblock.refs = ecl_one_minus(block->cblock.refs); |
| 426 |
block = ECL_NIL; |
| 427 |
} else if (block->cblock.handle != NULL) { |
| 428 |
success = GC_call_with_alloc_lock(dlclose_wrapper, block); |
| 429 |
cl_core.libraries = ecl_remove_eq(block, cl_core.libraries); |
| 430 |
} else { /* block not loaded */ |
| 431 |
success = FALSE; |
| 432 |
} |
| 433 |
ecl_enable_interrupts(); |
| 434 |
} ECL_WITH_GLOBAL_LOCK_END; |
| 435 |
if (block != ECL_NIL && block->cblock.self_destruct) { |
| 436 |
if (!Null(block->cblock.name)) { |
| 437 |
ecl_unlink(ecl_filename_self(block->cblock.name)); |
| 438 |
} |
| 439 |
} |
| 440 |
return success; |
| 441 |
} |
| 442 |
|
| 443 |
void |
| 444 |
ecl_library_close_all(void) |
| 445 |
{ |
| 446 |
while (cl_core.libraries != ECL_NIL) { |
| 447 |
ecl_library_close(ECL_CONS_CAR(cl_core.libraries)); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
ecl_def_ct_base_string(init_prefix, INIT_PREFIX, sizeof(INIT_PREFIX)-1, static, const); |
| 452 |
|
| 453 |
cl_object |
| 454 |
_ecl_library_init_prefix(void) |
| 455 |
{ |
| 456 |
return init_prefix; |
| 457 |
} |
| 458 |
|
| 459 |
ecl_def_ct_base_string(default_entry, INIT_PREFIX "CODE", sizeof(INIT_PREFIX "CODE")-1, |
| 460 |
static, const); |
| 461 |
|
| 462 |
cl_object |
| 463 |
_ecl_library_default_entry(void) |
| 464 |
{ |
| 465 |
return default_entry; |
| 466 |
} |
| 467 |
#endif /* ENABLE_DLOPEN */ |