* Use scm_mem2symbol or scm_str2symbol to create symbol objects.
[bpt/guile.git] / libguile / dynl.c
CommitLineData
1edae076
MV
1/* dynl.c - dynamic linking
2 *
7cf1a27e 3 * Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
1edae076
MV
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
82892bed
JB
17 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA
1edae076
MV
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.
82892bed 42 * If you do not wish that, delete this exception notice. */
1edae076 43
1bbd0b84
GB
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
1edae076
MV
48/* "dynl.c" dynamically link&load object files.
49 Author: Aubrey Jaffer
50 Modified for libguile by Marius Vollmer */
51
104d4533 52#if 0 /* Disabled until we know for sure that it isn't needed */
80bc7890
MV
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
60static void
61maybe_drag_in_eprintf ()
62{
63 assert (!maybe_drag_in_eprintf);
64}
104d4533 65#endif
80bc7890 66
96599e6a 67#include <stdio.h>
a0599745
MD
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"
80bc7890 74
a0599745 75#include "libguile/validate.h"
408ea28a 76
a6d9e5ab
DH
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
1edae076
MV
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
a6d9e5ab
DH
87 This code probably belongs into strings.c
88 (Dirk: IMO strings.c is not the right place.) */
1edae076 89
1edae076 90static char **
1bbd0b84 91scm_make_argv_from_stringlist (SCM args,int *argcp,const char *subr,int argn)
1edae076 92{
7cf1a27e 93 char **argv;
a6d9e5ab
DH
94 int argc;
95 int i;
7cf1a27e
MD
96
97 argc = scm_ilength (args);
a6d9e5ab
DH
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);
34f0f2b8 108 src = SCM_STRING_CHARS (arg);
a6d9e5ab
DH
109 dst = (char *) scm_must_malloc (len + 1, subr);
110 memcpy (dst, src, len);
111 dst[len] = 0;
7cf1a27e
MD
112 argv[i] = dst;
113 }
114
115 if (argcp)
116 *argcp = argc;
117 argv[argc] = 0;
118 return argv;
1edae076
MV
119}
120
1edae076 121static void
1bbd0b84 122scm_must_free_argv(char **argv)
1edae076 123{
7cf1a27e
MD
124 char **av = argv;
125 while (*av)
126 free (*(av++));
127 free (argv);
1edae076
MV
128}
129
80bc7890
MV
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
138struct moddata {
7cf1a27e
MD
139 struct moddata *link;
140 char *module_name;
141 void *init_func;
80bc7890
MV
142};
143
144static struct moddata *registered_mods = NULL;
145
146void
6e8d25a6 147scm_register_module_xxx (char *module_name, void *init_func)
80bc7890 148{
7cf1a27e 149 struct moddata *md;
80bc7890 150
7cf1a27e 151 /* XXX - should we (and can we) DEFER_INTS here? */
80bc7890 152
7cf1a27e
MD
153 for (md = registered_mods; md; md = md->link)
154 if (!strcmp (md->module_name, module_name))
155 {
156 md->init_func = init_func;
80bc7890 157 return;
7cf1a27e
MD
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;
96599e6a 167 }
80bc7890 168
7cf1a27e
MD
169 md->module_name = module_name;
170 md->init_func = init_func;
171 md->link = registered_mods;
172 registered_mods = md;
80bc7890
MV
173}
174
a1ec6916 175SCM_DEFINE (scm_registered_modules, "c-registered-modules", 0, 0, 0,
1bbd0b84 176 (),
b380b885
MD
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"
f15a06bf
MV
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.")
1bbd0b84 182#define FUNC_NAME s_scm_registered_modules
80bc7890 183{
7cf1a27e
MD
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;
80bc7890 193}
1bbd0b84 194#undef FUNC_NAME
80bc7890 195
a1ec6916 196SCM_DEFINE (scm_clear_registered_modules, "c-clear-registered-modules", 0, 0, 0,
1bbd0b84 197 (),
b380b885
MD
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.")
1bbd0b84 203#define FUNC_NAME s_scm_clear_registered_modules
80bc7890 204{
7cf1a27e 205 struct moddata *md1, *md2;
80bc7890 206
7cf1a27e 207 SCM_DEFER_INTS;
80bc7890 208
7cf1a27e
MD
209 for (md1 = registered_mods; md1; md1 = md2)
210 {
211 md2 = md1->link;
212 free (md1);
80bc7890 213 }
7cf1a27e 214 registered_mods = NULL;
80bc7890 215
7cf1a27e
MD
216 SCM_ALLOW_INTS;
217 return SCM_UNSPECIFIED;
80bc7890 218}
1bbd0b84 219#undef FUNC_NAME
80bc7890 220
1edae076 221/* Dispatch to the system dependent files
80bc7890 222 *
419e9e11
MV
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).
1edae076
MV
229 */
230
4feb69af
MV
231#ifdef DYNAMIC_LINKING
232
07286af9 233#include "libltdl/ltdl.h"
4feb69af
MV
234
235static void *
af45e3b0 236sysdep_dynl_link (const char *fname, const char *subr)
4feb69af 237{
7cf1a27e
MD
238 lt_dlhandle handle;
239 handle = lt_dlopenext (fname);
4feb69af
MV
240 if (NULL == handle)
241 {
01449aa5
DH
242 SCM fn;
243 SCM msg;
244
4feb69af 245 SCM_ALLOW_INTS;
01449aa5
DH
246 fn = scm_makfrom0str (fname);
247 msg = scm_makfrom0str (lt_dlerror ());
248 scm_misc_error (subr, "file: ~S, message: ~S", SCM_LIST2 (fn, msg));
4feb69af
MV
249 }
250 return (void *) handle;
251}
252
253static void
254sysdep_dynl_unlink (void *handle, const char *subr)
255{
256 if (lt_dlclose ((lt_dlhandle) handle))
257 {
258 SCM_ALLOW_INTS;
7cf1a27e 259 scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
4feb69af
MV
260 }
261}
262
263static void *
264sysdep_dynl_func (const char *symb, void *handle, const char *subr)
265{
266 void *fptr;
267
268 fptr = lt_dlsym ((lt_dlhandle) handle, symb);
269 if (!fptr)
270 {
271 SCM_ALLOW_INTS;
7cf1a27e 272 scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
4feb69af
MV
273 }
274 return fptr;
275}
276
277static void
278sysdep_dynl_init ()
279{
280 lt_dlinit ();
281}
282
1edae076 283#else
96599e6a
MV
284
285/* no dynamic linking available, throw errors. */
286
287static void
1bbd0b84 288sysdep_dynl_init (void)
96599e6a
MV
289{
290}
291
292static void
a80a90e9 293no_dynl_error (const char *subr)
96599e6a 294{
419e9e11
MV
295 SCM_ALLOW_INTS;
296 scm_misc_error (subr, "dynamic linking not available", SCM_EOL);
96599e6a
MV
297}
298
299static void *
af45e3b0 300sysdep_dynl_link (const char *filename, const char *subr)
96599e6a 301{
7cf1a27e
MD
302 no_dynl_error (subr);
303 return NULL;
96599e6a
MV
304}
305
306static void
a80a90e9
JB
307sysdep_dynl_unlink (void *handle,
308 const char *subr)
96599e6a 309{
7cf1a27e 310 no_dynl_error (subr);
96599e6a
MV
311}
312
313static void *
a80a90e9
JB
314sysdep_dynl_func (const char *symbol,
315 void *handle,
316 const char *subr)
96599e6a 317{
7cf1a27e
MD
318 no_dynl_error (subr);
319 return NULL;
96599e6a
MV
320}
321
80bc7890
MV
322#endif
323
324int scm_tc16_dynamic_obj;
325
b82c6ce0
DH
326#define DYNL_FILENAME(x) (SCM_CELL_OBJECT_1 (x))
327#define DYNL_HANDLE(x) ((void *) SCM_CELL_WORD_2 (x))
328#define SET_DYNL_HANDLE(x, v) (SCM_SET_CELL_WORD_2 ((x), (v)))
7cf1a27e 329
7cf1a27e 330
80bc7890 331static SCM
1bbd0b84 332mark_dynl_obj (SCM ptr)
80bc7890 333{
7cf1a27e 334 return DYNL_FILENAME (ptr);
c487ad44
MV
335}
336
80bc7890 337static int
1bbd0b84 338print_dynl_obj (SCM exp,SCM port,scm_print_state *pstate)
80bc7890 339{
7cf1a27e
MD
340 scm_puts ("#<dynamic-object ", port);
341 scm_iprin1 (DYNL_FILENAME (exp), port, pstate);
342 if (DYNL_HANDLE (exp) == NULL)
343 scm_puts (" (unlinked)", port);
344 scm_putc ('>', port);
345 return 1;
80bc7890
MV
346}
347
8e3ab003 348
af45e3b0
DH
349SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0,
350 (SCM fname),
b380b885
MD
351 "Open the dynamic library @var{library-file}. A library handle\n"
352 "representing the opened library is returned; this handle should be used\n"
353 "as the @var{lib} argument to the following functions.")
1bbd0b84 354#define FUNC_NAME s_scm_dynamic_link
80bc7890 355{
7cf1a27e 356 void *handle;
80bc7890 357
a6d9e5ab
DH
358 SCM_VALIDATE_STRING (1, fname);
359 SCM_STRING_COERCE_0TERMINATION_X (fname);
360 handle = sysdep_dynl_link (SCM_STRING_CHARS (fname), FUNC_NAME);
cffcab30 361 SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (fname), handle);
80bc7890 362}
1bbd0b84 363#undef FUNC_NAME
80bc7890 364
80bc7890 365
a1ec6916 366SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0,
1bbd0b84 367 (SCM obj),
b380b885
MD
368 "Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}\n"
369 "otherwise.")
1bbd0b84 370#define FUNC_NAME s_scm_dynamic_object_p
80bc7890 371{
b82c6ce0 372 return SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_dynamic_obj, obj));
80bc7890 373}
1bbd0b84 374#undef FUNC_NAME
80bc7890 375
b82c6ce0 376
a1ec6916 377SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0,
1bbd0b84 378 (SCM dobj),
b82c6ce0
DH
379 "Unlink the library represented by @var{library-handle},\n"
380 "and remove any imported symbols from the address space.\n"
b380b885 381 "GJB:FIXME:DOC: 2nd version below:\n"
b82c6ce0
DH
382 "Unlink the indicated object file from the application. The\n"
383 "argument @var{dynobj} must have been obtained by a call to\n"
384 "@code{dynamic-link}. After @code{dynamic-unlink} has been\n"
872e0c72 385 "called on @var{dynobj}, its content is no longer accessible.")
1bbd0b84 386#define FUNC_NAME s_scm_dynamic_unlink
80bc7890 387{
7cf1a27e 388 /*fixme* GC-problem */
b82c6ce0
DH
389 SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj);
390 if (DYNL_HANDLE (dobj) == NULL) {
391 SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
392 } else {
393 SCM_DEFER_INTS;
394 sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME);
395 SET_DYNL_HANDLE (dobj, NULL);
396 SCM_ALLOW_INTS;
397 return SCM_UNSPECIFIED;
398 }
80bc7890 399}
1bbd0b84 400#undef FUNC_NAME
80bc7890 401
b82c6ce0 402
a1ec6916 403SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0,
a6d9e5ab
DH
404 (SCM name, SCM dobj),
405 "Search the dynamic object @var{dobj} for the C function\n"
406 "indicated by the string @var{name} and return some Scheme\n"
407 "handle that can later be used with @code{dynamic-call} to\n"
408 "actually call the function.\n\n"
b380b885
MD
409 "Regardless whether your C compiler prepends an underscore @samp{_} to\n"
410 "the global names in a program, you should @strong{not} include this\n"
411 "underscore in @var{function}. Guile knows whether the underscore is\n"
872e0c72 412 "needed or not and will add it when necessary.")
1bbd0b84 413#define FUNC_NAME s_scm_dynamic_func
80bc7890 414{
a6d9e5ab
DH
415 /* The returned handle is formed by casting the address of the function to a
416 * long value and converting this to a scheme number
417 */
418
7cf1a27e 419 void (*func) ();
80bc7890 420
a6d9e5ab 421 SCM_VALIDATE_STRING (1, name);
7cf1a27e 422 /*fixme* GC-problem */
b82c6ce0
DH
423 SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj);
424 if (DYNL_HANDLE (dobj) == NULL) {
425 SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
426 } else {
a002f1a2
DH
427 char *chars;
428
b82c6ce0 429 SCM_DEFER_INTS;
a6d9e5ab
DH
430 SCM_STRING_COERCE_0TERMINATION_X (name);
431 chars = SCM_STRING_CHARS (name);
a002f1a2 432 func = (void (*) ()) sysdep_dynl_func (chars, DYNL_HANDLE (dobj), FUNC_NAME);
b82c6ce0
DH
433 SCM_ALLOW_INTS;
434 return scm_ulong2num ((unsigned long) func);
435 }
80bc7890 436}
1bbd0b84 437#undef FUNC_NAME
80bc7890 438
b82c6ce0 439
a1ec6916 440SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0,
1bbd0b84 441 (SCM func, SCM dobj),
b380b885
MD
442 "Call @var{lib-thunk}, a procedure of no arguments. If @var{lib-thunk}\n"
443 "is a string, it is assumed to be a symbol found in the dynamic library\n"
444 "@var{lib} and is fetched with @code{dynamic-func}. Otherwise, it should\n"
445 "be a function handle returned by a previous call to @code{dynamic-func}.\n"
446 "The return value is unspecified.\n"
447 "GJB:FIXME:DOC 2nd version below\n"
448 "Call the C function indicated by @var{function} and @var{dynobj}. The\n"
449 "function is passed no arguments and its return value is ignored. When\n"
450 "@var{function} is something returned by @code{dynamic-func}, call that\n"
451 "function and ignore @var{dynobj}. When @var{function} is a string (or\n"
452 "symbol, etc.), look it up in @var{dynobj}; this is equivalent to\n\n"
453 "@smallexample\n"
454 "(dynamic-call (dynamic-func @var{function} @var{dynobj} #f))\n"
455 "@end smallexample\n\n"
456 "Interrupts are deferred while the C function is executing (with\n"
872e0c72 457 "@code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).")
1bbd0b84 458#define FUNC_NAME s_scm_dynamic_call
80bc7890 459{
7cf1a27e
MD
460 void (*fptr) ();
461
a6d9e5ab 462 if (SCM_STRINGP (func))
7cf1a27e
MD
463 func = scm_dynamic_func (func, dobj);
464 fptr = (void (*) ()) SCM_NUM2ULONG (1, func);
465 SCM_DEFER_INTS;
466 fptr ();
467 SCM_ALLOW_INTS;
468 return SCM_UNSPECIFIED;
80bc7890 469}
1bbd0b84 470#undef FUNC_NAME
80bc7890 471
a1ec6916 472SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0,
1bbd0b84 473 (SCM func, SCM dobj, SCM args),
b380b885
MD
474 "Call @var{proc}, a dynamically loaded function, passing it the argument\n"
475 "list @var{args} (a list of strings). As with @code{dynamic-call},\n"
476 "@var{proc} should be either a function handle or a string, in which case\n"
477 "it is first fetched from @var{lib} with @code{dynamic-func}.\n\n"
478 "@var{proc} is assumed to return an integer, which is used as the return\n"
479 "value from @code{dynamic-args-call}.\n\n"
480 "GJB:FIXME:DOC 2nd version below\n"
481 "Call the C function indicated by @var{function} and @var{dynobj}, just\n"
482 "like @code{dynamic-call}, but pass it some arguments and return its\n"
483 "return value. The C function is expected to take two arguments and\n"
484 "return an @code{int}, just like @code{main}:\n\n"
485 "@smallexample\n"
486 "int c_func (int argc, char **argv);\n"
487 "@end smallexample\n\n"
488 "The parameter @var{args} must be a list of strings and is converted into\n"
489 "an array of @code{char *}. The array is passed in @var{argv} and its\n"
490 "size in @var{argc}. The return value is converted to a Scheme number\n"
872e0c72 491 "and returned from the call to @code{dynamic-args-call}.")
1bbd0b84 492#define FUNC_NAME s_scm_dynamic_args_call
80bc7890 493{
7cf1a27e
MD
494 int (*fptr) (int argc, char **argv);
495 int result, argc;
496 char **argv;
497
a6d9e5ab 498 if (SCM_STRINGP (func))
7cf1a27e
MD
499 func = scm_dynamic_func (func, dobj);
500
501 fptr = (int (*) (int, char **)) SCM_NUM2ULONG (1, func);
502 SCM_DEFER_INTS;
503 argv = scm_make_argv_from_stringlist (args, &argc, FUNC_NAME, SCM_ARG3);
504 result = (*fptr) (argc, argv);
505 scm_must_free_argv (argv);
506 SCM_ALLOW_INTS;
507
508 return SCM_MAKINUM (0L + result);
80bc7890 509}
1bbd0b84 510#undef FUNC_NAME
80bc7890 511
1edae076
MV
512void
513scm_init_dynamic_linking ()
514{
7cf1a27e
MD
515 scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0);
516 scm_set_smob_mark (scm_tc16_dynamic_obj, mark_dynl_obj);
517 scm_set_smob_print (scm_tc16_dynamic_obj, print_dynl_obj);
518 sysdep_dynl_init ();
8dc9439f 519#ifndef SCM_MAGIC_SNARFER
a0599745 520#include "libguile/dynl.x"
8dc9439f 521#endif
1edae076 522}
89e00824
ML
523
524/*
525 Local Variables:
526 c-file-style: "gnu"
527 End:
528*/