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