* alist.c, arbiters.c, async.c, backtrace.c, boolean.c, chars.c,
[bpt/guile.git] / libguile / dynl.c
1 /* dynl.c - dynamic linking
2 *
3 * Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA
19 *
20 * As a special exception, the Free Software Foundation gives permission
21 * for additional uses of the text contained in its release of GUILE.
22 *
23 * The exception is that, if you link the GUILE library with other files
24 * to produce an executable, this does not by itself cause the
25 * resulting executable to be covered by the GNU General Public License.
26 * Your use of that executable is in no way restricted on account of
27 * linking the GUILE library code into it.
28 *
29 * This exception does not however invalidate any other reasons why
30 * the executable file might be covered by the GNU General Public License.
31 *
32 * This exception applies only to the code released by the
33 * Free Software Foundation under the name GUILE. If you copy
34 * code from other Free Software Foundation releases into a copy of
35 * GUILE, as the General Public License permits, the exception does
36 * not apply to the code that you add in this way. To avoid misleading
37 * anyone as to the status of such modified files, you must delete
38 * this exception notice from them.
39 *
40 * If you write modifications of your own for GUILE, it is your choice
41 * whether to permit this exception to apply to your modifications.
42 * If you do not wish that, delete this exception notice. */
43
44 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
45 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
46
47
48 /* "dynl.c" dynamically link&load object files.
49 Author: Aubrey Jaffer
50 Modified for libguile by Marius Vollmer */
51
52 #if 0 /* Disabled until we know for sure that it isn't needed */
53 /* XXX - This is only here to drag in a definition of __eprintf. This
54 is needed for proper operation of dynamic linking. The real
55 solution would probably be a shared libgcc. */
56
57 #undef NDEBUG
58 #include <assert.h>
59
60 static void
61 maybe_drag_in_eprintf ()
62 {
63 assert (!maybe_drag_in_eprintf);
64 }
65 #endif
66
67 #include <stdio.h>
68 #include "libguile/_scm.h"
69 #include "libguile/dynl.h"
70 #include "libguile/smob.h"
71 #include "libguile/keywords.h"
72 #include "libguile/ports.h"
73 #include "libguile/strings.h"
74
75 #include "libguile/validate.h"
76
77 /* Create a new C argv array from a scheme list of strings. */
78 /* Dirk:FIXME:: A quite similar function is implemented in posix.c */
79 /* Dirk:FIXME:: In case of assertion errors, we get memory leaks */
80
81 /* Converting a list of SCM strings into a argv-style array. You must
82 have ints disabled for the whole lifetime of the created argv (from
83 before MAKE_ARGV_FROM_STRINGLIST until after
84 MUST_FREE_ARGV). Atleast this is was the documentation for
85 MAKARGVFROMSTRS says, it isn't really used that way.
86
87 This code probably belongs into strings.c
88 (Dirk: IMO strings.c is not the right place.) */
89
90 static char **
91 scm_make_argv_from_stringlist (SCM args,int *argcp,const char *subr,int argn)
92 {
93 char **argv;
94 int argc;
95 int i;
96
97 argc = scm_ilength (args);
98 SCM_ASSERT (argc >= 0, args, argn, subr);
99 argv = (char **) scm_must_malloc ((argc + 1) * sizeof (char *), subr);
100 for (i = 0; !SCM_NULLP (args); args = SCM_CDR (args), ++i) {
101 SCM arg = SCM_CAR (args);
102 scm_sizet len;
103 char *dst;
104 char *src;
105
106 SCM_ASSERT (SCM_STRINGP (arg), args, argn, subr);
107 len = SCM_STRING_LENGTH (arg);
108 src = SCM_ROCHARS (arg);
109 dst = (char *) scm_must_malloc (len + 1, subr);
110 memcpy (dst, src, len);
111 dst[len] = 0;
112 argv[i] = dst;
113 }
114
115 if (argcp)
116 *argcp = argc;
117 argv[argc] = 0;
118 return argv;
119 }
120
121 static void
122 scm_must_free_argv(char **argv)
123 {
124 char **av = argv;
125 while (*av)
126 free (*(av++));
127 free (argv);
128 }
129
130 /* Module registry
131 */
132
133 /* We can't use SCM objects here. One should be able to call
134 SCM_REGISTER_MODULE from a C++ constructor for a static
135 object. This happens before main and thus before libguile is
136 initialized. */
137
138 struct moddata {
139 struct moddata *link;
140 char *module_name;
141 void *init_func;
142 };
143
144 static struct moddata *registered_mods = NULL;
145
146 void
147 scm_register_module_xxx (char *module_name, void *init_func)
148 {
149 struct moddata *md;
150
151 /* XXX - should we (and can we) DEFER_INTS here? */
152
153 for (md = registered_mods; md; md = md->link)
154 if (!strcmp (md->module_name, module_name))
155 {
156 md->init_func = init_func;
157 return;
158 }
159
160 md = (struct moddata *) malloc (sizeof (struct moddata));
161 if (md == NULL)
162 {
163 fprintf (stderr,
164 "guile: can't register module (%s): not enough memory",
165 module_name);
166 return;
167 }
168
169 md->module_name = module_name;
170 md->init_func = init_func;
171 md->link = registered_mods;
172 registered_mods = md;
173 }
174
175 SCM_DEFINE (scm_registered_modules, "c-registered-modules", 0, 0, 0,
176 (),
177 "Return a list of the object code modules that have been imported into\n"
178 "the current Guile process. Each element of the list is a pair whose\n"
179 "car is the name of the module, and whose cdr is the function handle\n"
180 "for that module's initializer function. The name is the string that\n"
181 "has been passed to scm_register_module_xxx.")
182 #define FUNC_NAME s_scm_registered_modules
183 {
184 SCM res;
185 struct moddata *md;
186
187 res = SCM_EOL;
188 for (md = registered_mods; md; md = md->link)
189 res = scm_cons (scm_cons (scm_makfrom0str (md->module_name),
190 scm_ulong2num ((unsigned long) md->init_func)),
191 res);
192 return res;
193 }
194 #undef FUNC_NAME
195
196 SCM_DEFINE (scm_clear_registered_modules, "c-clear-registered-modules", 0, 0, 0,
197 (),
198 "Destroy the list of modules registered with the current Guile process.\n"
199 "The return value is unspecified. @strong{Warning:} this function does\n"
200 "not actually unlink or deallocate these modules, but only destroys the\n"
201 "records of which modules have been loaded. It should therefore be used\n"
202 "only by module bookkeeping operations.")
203 #define FUNC_NAME s_scm_clear_registered_modules
204 {
205 struct moddata *md1, *md2;
206
207 SCM_DEFER_INTS;
208
209 for (md1 = registered_mods; md1; md1 = md2)
210 {
211 md2 = md1->link;
212 free (md1);
213 }
214 registered_mods = NULL;
215
216 SCM_ALLOW_INTS;
217 return SCM_UNSPECIFIED;
218 }
219 #undef FUNC_NAME
220
221 /* Dispatch to the system dependent files
222 *
223 * They define some static functions. These functions are called with
224 * deferred interrupts. When they want to throw errors, they are
225 * expected to insert a SCM_ALLOW_INTS before doing the throw. It
226 * might work to throw an error while interrupts are deferred (because
227 * they will be unconditionally allowed the next time a SCM_ALLOW_INTS
228 * is executed, SCM_DEFER_INTS and SCM_ALLOW_INTS do not nest).
229 */
230
231 #ifdef DYNAMIC_LINKING
232
233 #include "libltdl/ltdl.h"
234
235 static void *
236 sysdep_dynl_link (const char *fname, const char *subr)
237 {
238 lt_dlhandle handle;
239 handle = lt_dlopenext (fname);
240 if (NULL == handle)
241 {
242 SCM_ALLOW_INTS;
243 scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
244 }
245 return (void *) handle;
246 }
247
248 static void
249 sysdep_dynl_unlink (void *handle, const char *subr)
250 {
251 if (lt_dlclose ((lt_dlhandle) handle))
252 {
253 SCM_ALLOW_INTS;
254 scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
255 }
256 }
257
258 static void *
259 sysdep_dynl_func (const char *symb, void *handle, const char *subr)
260 {
261 void *fptr;
262
263 fptr = lt_dlsym ((lt_dlhandle) handle, symb);
264 if (!fptr)
265 {
266 SCM_ALLOW_INTS;
267 scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
268 }
269 return fptr;
270 }
271
272 static void
273 sysdep_dynl_init ()
274 {
275 lt_dlinit ();
276 }
277
278 #else
279
280 /* no dynamic linking available, throw errors. */
281
282 static void
283 sysdep_dynl_init (void)
284 {
285 }
286
287 static void
288 no_dynl_error (const char *subr)
289 {
290 SCM_ALLOW_INTS;
291 scm_misc_error (subr, "dynamic linking not available", SCM_EOL);
292 }
293
294 static void *
295 sysdep_dynl_link (const char *filename, const char *subr)
296 {
297 no_dynl_error (subr);
298 return NULL;
299 }
300
301 static void
302 sysdep_dynl_unlink (void *handle,
303 const char *subr)
304 {
305 no_dynl_error (subr);
306 }
307
308 static void *
309 sysdep_dynl_func (const char *symbol,
310 void *handle,
311 const char *subr)
312 {
313 no_dynl_error (subr);
314 return NULL;
315 }
316
317 #endif
318
319 int scm_tc16_dynamic_obj;
320
321 #define DYNL_FILENAME(x) (SCM_CELL_OBJECT_1 (x))
322 #define DYNL_HANDLE(x) ((void *) SCM_CELL_WORD_2 (x))
323 #define SET_DYNL_HANDLE(x, v) (SCM_SET_CELL_WORD_2 ((x), (v)))
324
325
326 static SCM
327 mark_dynl_obj (SCM ptr)
328 {
329 return DYNL_FILENAME (ptr);
330 }
331
332 static int
333 print_dynl_obj (SCM exp,SCM port,scm_print_state *pstate)
334 {
335 scm_puts ("#<dynamic-object ", port);
336 scm_iprin1 (DYNL_FILENAME (exp), port, pstate);
337 if (DYNL_HANDLE (exp) == NULL)
338 scm_puts (" (unlinked)", port);
339 scm_putc ('>', port);
340 return 1;
341 }
342
343
344 SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0,
345 (SCM fname),
346 "Open the dynamic library @var{library-file}. A library handle\n"
347 "representing the opened library is returned; this handle should be used\n"
348 "as the @var{lib} argument to the following functions.")
349 #define FUNC_NAME s_scm_dynamic_link
350 {
351 void *handle;
352
353 SCM_VALIDATE_STRING (1, fname);
354 SCM_STRING_COERCE_0TERMINATION_X (fname);
355 handle = sysdep_dynl_link (SCM_STRING_CHARS (fname), FUNC_NAME);
356 SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (fname), handle);
357 }
358 #undef FUNC_NAME
359
360
361 SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0,
362 (SCM obj),
363 "Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}\n"
364 "otherwise.")
365 #define FUNC_NAME s_scm_dynamic_object_p
366 {
367 return SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_dynamic_obj, obj));
368 }
369 #undef FUNC_NAME
370
371
372 SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0,
373 (SCM dobj),
374 "Unlink the library represented by @var{library-handle},\n"
375 "and remove any imported symbols from the address space.\n"
376 "GJB:FIXME:DOC: 2nd version below:\n"
377 "Unlink the indicated object file from the application. The\n"
378 "argument @var{dynobj} must have been obtained by a call to\n"
379 "@code{dynamic-link}. After @code{dynamic-unlink} has been\n"
380 "called on @var{dynobj}, its content is no longer accessible.")
381 #define FUNC_NAME s_scm_dynamic_unlink
382 {
383 /*fixme* GC-problem */
384 SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj);
385 if (DYNL_HANDLE (dobj) == NULL) {
386 SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
387 } else {
388 SCM_DEFER_INTS;
389 sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME);
390 SET_DYNL_HANDLE (dobj, NULL);
391 SCM_ALLOW_INTS;
392 return SCM_UNSPECIFIED;
393 }
394 }
395 #undef FUNC_NAME
396
397
398 SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0,
399 (SCM name, SCM dobj),
400 "Search the dynamic object @var{dobj} for the C function\n"
401 "indicated by the string @var{name} and return some Scheme\n"
402 "handle that can later be used with @code{dynamic-call} to\n"
403 "actually call the function.\n\n"
404 "Regardless whether your C compiler prepends an underscore @samp{_} to\n"
405 "the global names in a program, you should @strong{not} include this\n"
406 "underscore in @var{function}. Guile knows whether the underscore is\n"
407 "needed or not and will add it when necessary.")
408 #define FUNC_NAME s_scm_dynamic_func
409 {
410 /* The returned handle is formed by casting the address of the function to a
411 * long value and converting this to a scheme number
412 */
413
414 void (*func) ();
415
416 SCM_VALIDATE_STRING (1, name);
417 /*fixme* GC-problem */
418 SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj);
419 if (DYNL_HANDLE (dobj) == NULL) {
420 SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
421 } else {
422 char *chars;
423
424 SCM_DEFER_INTS;
425 SCM_STRING_COERCE_0TERMINATION_X (name);
426 chars = SCM_STRING_CHARS (name);
427 func = (void (*) ()) sysdep_dynl_func (chars, DYNL_HANDLE (dobj), FUNC_NAME);
428 SCM_ALLOW_INTS;
429 return scm_ulong2num ((unsigned long) func);
430 }
431 }
432 #undef FUNC_NAME
433
434
435 SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0,
436 (SCM func, SCM dobj),
437 "Call @var{lib-thunk}, a procedure of no arguments. If @var{lib-thunk}\n"
438 "is a string, it is assumed to be a symbol found in the dynamic library\n"
439 "@var{lib} and is fetched with @code{dynamic-func}. Otherwise, it should\n"
440 "be a function handle returned by a previous call to @code{dynamic-func}.\n"
441 "The return value is unspecified.\n"
442 "GJB:FIXME:DOC 2nd version below\n"
443 "Call the C function indicated by @var{function} and @var{dynobj}. The\n"
444 "function is passed no arguments and its return value is ignored. When\n"
445 "@var{function} is something returned by @code{dynamic-func}, call that\n"
446 "function and ignore @var{dynobj}. When @var{function} is a string (or\n"
447 "symbol, etc.), look it up in @var{dynobj}; this is equivalent to\n\n"
448 "@smallexample\n"
449 "(dynamic-call (dynamic-func @var{function} @var{dynobj} #f))\n"
450 "@end smallexample\n\n"
451 "Interrupts are deferred while the C function is executing (with\n"
452 "@code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).")
453 #define FUNC_NAME s_scm_dynamic_call
454 {
455 void (*fptr) ();
456
457 if (SCM_STRINGP (func))
458 func = scm_dynamic_func (func, dobj);
459 fptr = (void (*) ()) SCM_NUM2ULONG (1, func);
460 SCM_DEFER_INTS;
461 fptr ();
462 SCM_ALLOW_INTS;
463 return SCM_UNSPECIFIED;
464 }
465 #undef FUNC_NAME
466
467 SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0,
468 (SCM func, SCM dobj, SCM args),
469 "Call @var{proc}, a dynamically loaded function, passing it the argument\n"
470 "list @var{args} (a list of strings). As with @code{dynamic-call},\n"
471 "@var{proc} should be either a function handle or a string, in which case\n"
472 "it is first fetched from @var{lib} with @code{dynamic-func}.\n\n"
473 "@var{proc} is assumed to return an integer, which is used as the return\n"
474 "value from @code{dynamic-args-call}.\n\n"
475 "GJB:FIXME:DOC 2nd version below\n"
476 "Call the C function indicated by @var{function} and @var{dynobj}, just\n"
477 "like @code{dynamic-call}, but pass it some arguments and return its\n"
478 "return value. The C function is expected to take two arguments and\n"
479 "return an @code{int}, just like @code{main}:\n\n"
480 "@smallexample\n"
481 "int c_func (int argc, char **argv);\n"
482 "@end smallexample\n\n"
483 "The parameter @var{args} must be a list of strings and is converted into\n"
484 "an array of @code{char *}. The array is passed in @var{argv} and its\n"
485 "size in @var{argc}. The return value is converted to a Scheme number\n"
486 "and returned from the call to @code{dynamic-args-call}.")
487 #define FUNC_NAME s_scm_dynamic_args_call
488 {
489 int (*fptr) (int argc, char **argv);
490 int result, argc;
491 char **argv;
492
493 if (SCM_STRINGP (func))
494 func = scm_dynamic_func (func, dobj);
495
496 fptr = (int (*) (int, char **)) SCM_NUM2ULONG (1, func);
497 SCM_DEFER_INTS;
498 argv = scm_make_argv_from_stringlist (args, &argc, FUNC_NAME, SCM_ARG3);
499 result = (*fptr) (argc, argv);
500 scm_must_free_argv (argv);
501 SCM_ALLOW_INTS;
502
503 return SCM_MAKINUM (0L + result);
504 }
505 #undef FUNC_NAME
506
507 void
508 scm_init_dynamic_linking ()
509 {
510 scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0);
511 scm_set_smob_mark (scm_tc16_dynamic_obj, mark_dynl_obj);
512 scm_set_smob_print (scm_tc16_dynamic_obj, print_dynl_obj);
513 sysdep_dynl_init ();
514 #ifndef SCM_MAGIC_SNARFER
515 #include "libguile/dynl.x"
516 #endif
517 }
518
519 /*
520 Local Variables:
521 c-file-style: "gnu"
522 End:
523 */