Talk about kluge at top of srfi13.scm.
[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>
13070bd3
DH
68#include <string.h>
69
a0599745
MD
70#include "libguile/_scm.h"
71#include "libguile/dynl.h"
72#include "libguile/smob.h"
73#include "libguile/keywords.h"
74#include "libguile/ports.h"
75#include "libguile/strings.h"
a0e0793f 76#include "libguile/deprecation.h"
a0599745 77#include "libguile/validate.h"
408ea28a 78
a6d9e5ab
DH
79/* Create a new C argv array from a scheme list of strings. */
80/* Dirk:FIXME:: A quite similar function is implemented in posix.c */
81/* Dirk:FIXME:: In case of assertion errors, we get memory leaks */
82
1edae076
MV
83/* Converting a list of SCM strings into a argv-style array. You must
84 have ints disabled for the whole lifetime of the created argv (from
85 before MAKE_ARGV_FROM_STRINGLIST until after
86 MUST_FREE_ARGV). Atleast this is was the documentation for
87 MAKARGVFROMSTRS says, it isn't really used that way.
88
a6d9e5ab
DH
89 This code probably belongs into strings.c
90 (Dirk: IMO strings.c is not the right place.) */
1edae076 91
1edae076 92static char **
1bbd0b84 93scm_make_argv_from_stringlist (SCM args,int *argcp,const char *subr,int argn)
1edae076 94{
7cf1a27e 95 char **argv;
a6d9e5ab
DH
96 int argc;
97 int i;
7cf1a27e
MD
98
99 argc = scm_ilength (args);
a6d9e5ab
DH
100 SCM_ASSERT (argc >= 0, args, argn, subr);
101 argv = (char **) scm_must_malloc ((argc + 1) * sizeof (char *), subr);
102 for (i = 0; !SCM_NULLP (args); args = SCM_CDR (args), ++i) {
103 SCM arg = SCM_CAR (args);
1be6b49c 104 size_t len;
a6d9e5ab
DH
105 char *dst;
106 char *src;
107
108 SCM_ASSERT (SCM_STRINGP (arg), args, argn, subr);
109 len = SCM_STRING_LENGTH (arg);
34f0f2b8 110 src = SCM_STRING_CHARS (arg);
a6d9e5ab
DH
111 dst = (char *) scm_must_malloc (len + 1, subr);
112 memcpy (dst, src, len);
113 dst[len] = 0;
7cf1a27e
MD
114 argv[i] = dst;
115 }
116
117 if (argcp)
118 *argcp = argc;
119 argv[argc] = 0;
120 return argv;
1edae076
MV
121}
122
1edae076 123static void
1bbd0b84 124scm_must_free_argv(char **argv)
1edae076 125{
7cf1a27e
MD
126 char **av = argv;
127 while (*av)
128 free (*(av++));
129 free (argv);
1edae076
MV
130}
131
46ca6c2e
MV
132#if SCM_DEBUG_DEPRECATED == 0
133
80bc7890
MV
134/* Module registry
135 */
136
137/* We can't use SCM objects here. One should be able to call
138 SCM_REGISTER_MODULE from a C++ constructor for a static
139 object. This happens before main and thus before libguile is
140 initialized. */
141
142struct moddata {
7cf1a27e
MD
143 struct moddata *link;
144 char *module_name;
145 void *init_func;
80bc7890
MV
146};
147
148static struct moddata *registered_mods = NULL;
149
150void
6e8d25a6 151scm_register_module_xxx (char *module_name, void *init_func)
80bc7890 152{
7cf1a27e 153 struct moddata *md;
80bc7890 154
46ca6c2e
MV
155 scm_c_issue_deprecation_warning
156 ("`scm_register_module_xxx' is deprecated. Use extensions instead.");
157
7cf1a27e 158 /* XXX - should we (and can we) DEFER_INTS here? */
80bc7890 159
7cf1a27e
MD
160 for (md = registered_mods; md; md = md->link)
161 if (!strcmp (md->module_name, module_name))
162 {
163 md->init_func = init_func;
80bc7890 164 return;
7cf1a27e
MD
165 }
166
167 md = (struct moddata *) malloc (sizeof (struct moddata));
168 if (md == NULL)
169 {
170 fprintf (stderr,
171 "guile: can't register module (%s): not enough memory",
172 module_name);
173 return;
96599e6a 174 }
80bc7890 175
7cf1a27e
MD
176 md->module_name = module_name;
177 md->init_func = init_func;
178 md->link = registered_mods;
179 registered_mods = md;
80bc7890
MV
180}
181
a1ec6916 182SCM_DEFINE (scm_registered_modules, "c-registered-modules", 0, 0, 0,
1bbd0b84 183 (),
b380b885
MD
184 "Return a list of the object code modules that have been imported into\n"
185 "the current Guile process. Each element of the list is a pair whose\n"
f15a06bf
MV
186 "car is the name of the module, and whose cdr is the function handle\n"
187 "for that module's initializer function. The name is the string that\n"
188 "has been passed to scm_register_module_xxx.")
1bbd0b84 189#define FUNC_NAME s_scm_registered_modules
80bc7890 190{
7cf1a27e
MD
191 SCM res;
192 struct moddata *md;
193
194 res = SCM_EOL;
195 for (md = registered_mods; md; md = md->link)
196 res = scm_cons (scm_cons (scm_makfrom0str (md->module_name),
197 scm_ulong2num ((unsigned long) md->init_func)),
198 res);
199 return res;
80bc7890 200}
1bbd0b84 201#undef FUNC_NAME
80bc7890 202
a1ec6916 203SCM_DEFINE (scm_clear_registered_modules, "c-clear-registered-modules", 0, 0, 0,
1bbd0b84 204 (),
b380b885
MD
205 "Destroy the list of modules registered with the current Guile process.\n"
206 "The return value is unspecified. @strong{Warning:} this function does\n"
207 "not actually unlink or deallocate these modules, but only destroys the\n"
208 "records of which modules have been loaded. It should therefore be used\n"
209 "only by module bookkeeping operations.")
1bbd0b84 210#define FUNC_NAME s_scm_clear_registered_modules
80bc7890 211{
7cf1a27e 212 struct moddata *md1, *md2;
80bc7890 213
7cf1a27e 214 SCM_DEFER_INTS;
80bc7890 215
7cf1a27e
MD
216 for (md1 = registered_mods; md1; md1 = md2)
217 {
218 md2 = md1->link;
219 free (md1);
80bc7890 220 }
7cf1a27e 221 registered_mods = NULL;
80bc7890 222
7cf1a27e
MD
223 SCM_ALLOW_INTS;
224 return SCM_UNSPECIFIED;
80bc7890 225}
1bbd0b84 226#undef FUNC_NAME
80bc7890 227
46ca6c2e
MV
228#endif /* !SCM_DEBUG_DEPRECATED */
229
1edae076 230/* Dispatch to the system dependent files
80bc7890 231 *
419e9e11
MV
232 * They define some static functions. These functions are called with
233 * deferred interrupts. When they want to throw errors, they are
234 * expected to insert a SCM_ALLOW_INTS before doing the throw. It
235 * might work to throw an error while interrupts are deferred (because
236 * they will be unconditionally allowed the next time a SCM_ALLOW_INTS
237 * is executed, SCM_DEFER_INTS and SCM_ALLOW_INTS do not nest).
1edae076
MV
238 */
239
4feb69af
MV
240#ifdef DYNAMIC_LINKING
241
07286af9 242#include "libltdl/ltdl.h"
4feb69af
MV
243
244static void *
af45e3b0 245sysdep_dynl_link (const char *fname, const char *subr)
4feb69af 246{
7cf1a27e
MD
247 lt_dlhandle handle;
248 handle = lt_dlopenext (fname);
4feb69af
MV
249 if (NULL == handle)
250 {
01449aa5
DH
251 SCM fn;
252 SCM msg;
253
4feb69af 254 SCM_ALLOW_INTS;
01449aa5
DH
255 fn = scm_makfrom0str (fname);
256 msg = scm_makfrom0str (lt_dlerror ());
257 scm_misc_error (subr, "file: ~S, message: ~S", SCM_LIST2 (fn, msg));
4feb69af
MV
258 }
259 return (void *) handle;
260}
261
262static void
263sysdep_dynl_unlink (void *handle, const char *subr)
264{
265 if (lt_dlclose ((lt_dlhandle) handle))
266 {
267 SCM_ALLOW_INTS;
7cf1a27e 268 scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
4feb69af
MV
269 }
270}
271
272static void *
273sysdep_dynl_func (const char *symb, void *handle, const char *subr)
274{
275 void *fptr;
276
277 fptr = lt_dlsym ((lt_dlhandle) handle, symb);
278 if (!fptr)
279 {
280 SCM_ALLOW_INTS;
7cf1a27e 281 scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
4feb69af
MV
282 }
283 return fptr;
284}
285
286static void
287sysdep_dynl_init ()
288{
289 lt_dlinit ();
290}
291
1edae076 292#else
96599e6a
MV
293
294/* no dynamic linking available, throw errors. */
295
296static void
1bbd0b84 297sysdep_dynl_init (void)
96599e6a
MV
298{
299}
300
301static void
a80a90e9 302no_dynl_error (const char *subr)
96599e6a 303{
419e9e11
MV
304 SCM_ALLOW_INTS;
305 scm_misc_error (subr, "dynamic linking not available", SCM_EOL);
96599e6a
MV
306}
307
308static void *
af45e3b0 309sysdep_dynl_link (const char *filename, const char *subr)
96599e6a 310{
7cf1a27e
MD
311 no_dynl_error (subr);
312 return NULL;
96599e6a
MV
313}
314
315static void
a80a90e9
JB
316sysdep_dynl_unlink (void *handle,
317 const char *subr)
96599e6a 318{
7cf1a27e 319 no_dynl_error (subr);
96599e6a
MV
320}
321
322static void *
a80a90e9
JB
323sysdep_dynl_func (const char *symbol,
324 void *handle,
325 const char *subr)
96599e6a 326{
7cf1a27e
MD
327 no_dynl_error (subr);
328 return NULL;
96599e6a
MV
329}
330
80bc7890
MV
331#endif
332
e841c3e0 333scm_bits_t scm_tc16_dynamic_obj;
80bc7890 334
b82c6ce0
DH
335#define DYNL_FILENAME(x) (SCM_CELL_OBJECT_1 (x))
336#define DYNL_HANDLE(x) ((void *) SCM_CELL_WORD_2 (x))
337#define SET_DYNL_HANDLE(x, v) (SCM_SET_CELL_WORD_2 ((x), (v)))
7cf1a27e 338
7cf1a27e 339
80bc7890 340static SCM
e841c3e0 341dynl_obj_mark (SCM ptr)
80bc7890 342{
7cf1a27e 343 return DYNL_FILENAME (ptr);
c487ad44
MV
344}
345
e841c3e0 346
80bc7890 347static int
e841c3e0 348dynl_obj_print (SCM exp, SCM port, scm_print_state *pstate)
80bc7890 349{
7cf1a27e
MD
350 scm_puts ("#<dynamic-object ", port);
351 scm_iprin1 (DYNL_FILENAME (exp), port, pstate);
352 if (DYNL_HANDLE (exp) == NULL)
353 scm_puts (" (unlinked)", port);
354 scm_putc ('>', port);
355 return 1;
80bc7890
MV
356}
357
8e3ab003 358
af45e3b0 359SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0,
1e6808ea
MG
360 (SCM filename),
361 "Open the dynamic library called @var{filename}. A library\n"
362 "handle representing the opened library is returned; this handle\n"
363 "should be used as the @var{dobj} argument to the following\n"
364 "functions.")
1bbd0b84 365#define FUNC_NAME s_scm_dynamic_link
80bc7890 366{
7cf1a27e 367 void *handle;
80bc7890 368
1e6808ea
MG
369 SCM_VALIDATE_STRING (1, filename);
370 SCM_STRING_COERCE_0TERMINATION_X (filename);
371 handle = sysdep_dynl_link (SCM_STRING_CHARS (filename), FUNC_NAME);
372 SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (filename), handle);
80bc7890 373}
1bbd0b84 374#undef FUNC_NAME
80bc7890 375
80bc7890 376
a1ec6916 377SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0,
1bbd0b84 378 (SCM obj),
b380b885
MD
379 "Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}\n"
380 "otherwise.")
1bbd0b84 381#define FUNC_NAME s_scm_dynamic_object_p
80bc7890 382{
e841c3e0 383 return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_dynamic_obj, obj));
80bc7890 384}
1bbd0b84 385#undef FUNC_NAME
80bc7890 386
b82c6ce0 387
a1ec6916 388SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0,
1bbd0b84 389 (SCM dobj),
b82c6ce0 390 "Unlink the indicated object file from the application. The\n"
1e6808ea 391 "argument @var{dobj} must have been obtained by a call to\n"
b82c6ce0 392 "@code{dynamic-link}. After @code{dynamic-unlink} has been\n"
1e6808ea 393 "called on @var{dobj}, its content is no longer accessible.")
1bbd0b84 394#define FUNC_NAME s_scm_dynamic_unlink
80bc7890 395{
7cf1a27e 396 /*fixme* GC-problem */
b82c6ce0
DH
397 SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj);
398 if (DYNL_HANDLE (dobj) == NULL) {
399 SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
400 } else {
401 SCM_DEFER_INTS;
402 sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME);
403 SET_DYNL_HANDLE (dobj, NULL);
404 SCM_ALLOW_INTS;
405 return SCM_UNSPECIFIED;
406 }
80bc7890 407}
1bbd0b84 408#undef FUNC_NAME
80bc7890 409
b82c6ce0 410
a1ec6916 411SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0,
a6d9e5ab
DH
412 (SCM name, SCM dobj),
413 "Search the dynamic object @var{dobj} for the C function\n"
414 "indicated by the string @var{name} and return some Scheme\n"
415 "handle that can later be used with @code{dynamic-call} to\n"
416 "actually call the function.\n\n"
b380b885
MD
417 "Regardless whether your C compiler prepends an underscore @samp{_} to\n"
418 "the global names in a program, you should @strong{not} include this\n"
419 "underscore in @var{function}. Guile knows whether the underscore is\n"
872e0c72 420 "needed or not and will add it when necessary.")
1bbd0b84 421#define FUNC_NAME s_scm_dynamic_func
80bc7890 422{
a6d9e5ab
DH
423 /* The returned handle is formed by casting the address of the function to a
424 * long value and converting this to a scheme number
425 */
426
7cf1a27e 427 void (*func) ();
80bc7890 428
a6d9e5ab 429 SCM_VALIDATE_STRING (1, name);
7cf1a27e 430 /*fixme* GC-problem */
b82c6ce0
DH
431 SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj);
432 if (DYNL_HANDLE (dobj) == NULL) {
433 SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
434 } else {
a002f1a2
DH
435 char *chars;
436
b82c6ce0 437 SCM_DEFER_INTS;
a6d9e5ab
DH
438 SCM_STRING_COERCE_0TERMINATION_X (name);
439 chars = SCM_STRING_CHARS (name);
a002f1a2 440 func = (void (*) ()) sysdep_dynl_func (chars, DYNL_HANDLE (dobj), FUNC_NAME);
b82c6ce0
DH
441 SCM_ALLOW_INTS;
442 return scm_ulong2num ((unsigned long) func);
443 }
80bc7890 444}
1bbd0b84 445#undef FUNC_NAME
80bc7890 446
b82c6ce0 447
a1ec6916 448SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0,
1bbd0b84 449 (SCM func, SCM dobj),
1e6808ea
MG
450 "Call the C function indicated by @var{func} and @var{dobj}.\n"
451 "The function is passed no arguments and its return value is\n"
452 "ignored. When @var{function} is something returned by\n"
453 "@code{dynamic-func}, call that function and ignore @var{dobj}.\n"
454 "When @var{func} is a string , look it up in @var{dynobj}; this\n"
455 "is equivalent to\n"
b380b885 456 "@smallexample\n"
1e6808ea 457 "(dynamic-call (dynamic-func @var{func} @var{dobj} #f))\n"
b380b885
MD
458 "@end smallexample\n\n"
459 "Interrupts are deferred while the C function is executing (with\n"
872e0c72 460 "@code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).")
1bbd0b84 461#define FUNC_NAME s_scm_dynamic_call
80bc7890 462{
7cf1a27e
MD
463 void (*fptr) ();
464
a6d9e5ab 465 if (SCM_STRINGP (func))
7cf1a27e
MD
466 func = scm_dynamic_func (func, dobj);
467 fptr = (void (*) ()) SCM_NUM2ULONG (1, func);
468 SCM_DEFER_INTS;
469 fptr ();
470 SCM_ALLOW_INTS;
471 return SCM_UNSPECIFIED;
80bc7890 472}
1bbd0b84 473#undef FUNC_NAME
80bc7890 474
a1ec6916 475SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0,
1bbd0b84 476 (SCM func, SCM dobj, SCM args),
1e6808ea
MG
477 "Call the C function indicated by @var{func} and @var{dobj},\n"
478 "just like @code{dynamic-call}, but pass it some arguments and\n"
479 "return its return value. The C function is expected to take\n"
480 "two arguments and return an @code{int}, just like @code{main}:\n"
b380b885
MD
481 "@smallexample\n"
482 "int c_func (int argc, char **argv);\n"
483 "@end smallexample\n\n"
1e6808ea
MG
484 "The parameter @var{args} must be a list of strings and is\n"
485 "converted into an array of @code{char *}. The array is passed\n"
486 "in @var{argv} and its size in @var{argc}. The return value is\n"
487 "converted to a Scheme number and returned from the call to\n"
488 "@code{dynamic-args-call}.")
1bbd0b84 489#define FUNC_NAME s_scm_dynamic_args_call
80bc7890 490{
7cf1a27e
MD
491 int (*fptr) (int argc, char **argv);
492 int result, argc;
493 char **argv;
494
a6d9e5ab 495 if (SCM_STRINGP (func))
7cf1a27e
MD
496 func = scm_dynamic_func (func, dobj);
497
498 fptr = (int (*) (int, char **)) SCM_NUM2ULONG (1, func);
499 SCM_DEFER_INTS;
500 argv = scm_make_argv_from_stringlist (args, &argc, FUNC_NAME, SCM_ARG3);
501 result = (*fptr) (argc, argv);
502 scm_must_free_argv (argv);
503 SCM_ALLOW_INTS;
504
505 return SCM_MAKINUM (0L + result);
80bc7890 506}
1bbd0b84 507#undef FUNC_NAME
80bc7890 508
1edae076
MV
509void
510scm_init_dynamic_linking ()
511{
7cf1a27e 512 scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0);
e841c3e0
KN
513 scm_set_smob_mark (scm_tc16_dynamic_obj, dynl_obj_mark);
514 scm_set_smob_print (scm_tc16_dynamic_obj, dynl_obj_print);
7cf1a27e 515 sysdep_dynl_init ();
8dc9439f 516#ifndef SCM_MAGIC_SNARFER
a0599745 517#include "libguile/dynl.x"
8dc9439f 518#endif
1edae076 519}
89e00824
ML
520
521/*
522 Local Variables:
523 c-file-style: "gnu"
524 End:
525*/