Commit | Line | Data |
---|---|---|
1edae076 MV |
1 | /* dynl.c - dynamic linking |
2 | * | |
58ade102 | 3 | * Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001 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 | |
45 | ||
1edae076 MV |
46 | /* "dynl.c" dynamically link&load object files. |
47 | Author: Aubrey Jaffer | |
48 | Modified for libguile by Marius Vollmer */ | |
49 | ||
104d4533 | 50 | #if 0 /* Disabled until we know for sure that it isn't needed */ |
80bc7890 MV |
51 | /* XXX - This is only here to drag in a definition of __eprintf. This |
52 | is needed for proper operation of dynamic linking. The real | |
53 | solution would probably be a shared libgcc. */ | |
54 | ||
55 | #undef NDEBUG | |
56 | #include <assert.h> | |
57 | ||
58 | static void | |
59 | maybe_drag_in_eprintf () | |
60 | { | |
61 | assert (!maybe_drag_in_eprintf); | |
62 | } | |
104d4533 | 63 | #endif |
80bc7890 | 64 | |
96599e6a | 65 | #include <stdio.h> |
13070bd3 DH |
66 | #include <string.h> |
67 | ||
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" | |
a0e0793f | 74 | #include "libguile/deprecation.h" |
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 | 90 | static char ** |
1bbd0b84 | 91 | scm_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); | |
1be6b49c | 102 | size_t len; |
a6d9e5ab DH |
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 | 121 | static void |
1bbd0b84 | 122 | scm_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 | ||
1edae076 | 130 | /* Dispatch to the system dependent files |
80bc7890 | 131 | * |
419e9e11 MV |
132 | * They define some static functions. These functions are called with |
133 | * deferred interrupts. When they want to throw errors, they are | |
134 | * expected to insert a SCM_ALLOW_INTS before doing the throw. It | |
135 | * might work to throw an error while interrupts are deferred (because | |
136 | * they will be unconditionally allowed the next time a SCM_ALLOW_INTS | |
137 | * is executed, SCM_DEFER_INTS and SCM_ALLOW_INTS do not nest). | |
1edae076 MV |
138 | */ |
139 | ||
4feb69af MV |
140 | #ifdef DYNAMIC_LINKING |
141 | ||
07286af9 | 142 | #include "libltdl/ltdl.h" |
4feb69af MV |
143 | |
144 | static void * | |
af45e3b0 | 145 | sysdep_dynl_link (const char *fname, const char *subr) |
4feb69af | 146 | { |
7cf1a27e MD |
147 | lt_dlhandle handle; |
148 | handle = lt_dlopenext (fname); | |
4feb69af MV |
149 | if (NULL == handle) |
150 | { | |
01449aa5 DH |
151 | SCM fn; |
152 | SCM msg; | |
153 | ||
4feb69af | 154 | SCM_ALLOW_INTS; |
01449aa5 DH |
155 | fn = scm_makfrom0str (fname); |
156 | msg = scm_makfrom0str (lt_dlerror ()); | |
1afff620 | 157 | scm_misc_error (subr, "file: ~S, message: ~S", scm_list_2 (fn, msg)); |
4feb69af MV |
158 | } |
159 | return (void *) handle; | |
160 | } | |
161 | ||
162 | static void | |
163 | sysdep_dynl_unlink (void *handle, const char *subr) | |
164 | { | |
165 | if (lt_dlclose ((lt_dlhandle) handle)) | |
166 | { | |
167 | SCM_ALLOW_INTS; | |
7cf1a27e | 168 | scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL); |
4feb69af MV |
169 | } |
170 | } | |
171 | ||
172 | static void * | |
173 | sysdep_dynl_func (const char *symb, void *handle, const char *subr) | |
174 | { | |
175 | void *fptr; | |
176 | ||
177 | fptr = lt_dlsym ((lt_dlhandle) handle, symb); | |
178 | if (!fptr) | |
179 | { | |
180 | SCM_ALLOW_INTS; | |
7cf1a27e | 181 | scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL); |
4feb69af MV |
182 | } |
183 | return fptr; | |
184 | } | |
185 | ||
186 | static void | |
187 | sysdep_dynl_init () | |
188 | { | |
189 | lt_dlinit (); | |
190 | } | |
191 | ||
1edae076 | 192 | #else |
96599e6a MV |
193 | |
194 | /* no dynamic linking available, throw errors. */ | |
195 | ||
196 | static void | |
1bbd0b84 | 197 | sysdep_dynl_init (void) |
96599e6a MV |
198 | { |
199 | } | |
200 | ||
201 | static void | |
a80a90e9 | 202 | no_dynl_error (const char *subr) |
96599e6a | 203 | { |
419e9e11 MV |
204 | SCM_ALLOW_INTS; |
205 | scm_misc_error (subr, "dynamic linking not available", SCM_EOL); | |
96599e6a MV |
206 | } |
207 | ||
208 | static void * | |
af45e3b0 | 209 | sysdep_dynl_link (const char *filename, const char *subr) |
96599e6a | 210 | { |
7cf1a27e MD |
211 | no_dynl_error (subr); |
212 | return NULL; | |
96599e6a MV |
213 | } |
214 | ||
215 | static void | |
a80a90e9 JB |
216 | sysdep_dynl_unlink (void *handle, |
217 | const char *subr) | |
96599e6a | 218 | { |
7cf1a27e | 219 | no_dynl_error (subr); |
96599e6a MV |
220 | } |
221 | ||
222 | static void * | |
a80a90e9 JB |
223 | sysdep_dynl_func (const char *symbol, |
224 | void *handle, | |
225 | const char *subr) | |
96599e6a | 226 | { |
7cf1a27e MD |
227 | no_dynl_error (subr); |
228 | return NULL; | |
96599e6a MV |
229 | } |
230 | ||
80bc7890 MV |
231 | #endif |
232 | ||
92c2555f | 233 | scm_t_bits scm_tc16_dynamic_obj; |
80bc7890 | 234 | |
b82c6ce0 DH |
235 | #define DYNL_FILENAME(x) (SCM_CELL_OBJECT_1 (x)) |
236 | #define DYNL_HANDLE(x) ((void *) SCM_CELL_WORD_2 (x)) | |
237 | #define SET_DYNL_HANDLE(x, v) (SCM_SET_CELL_WORD_2 ((x), (v))) | |
7cf1a27e | 238 | |
7cf1a27e | 239 | |
80bc7890 | 240 | static SCM |
e841c3e0 | 241 | dynl_obj_mark (SCM ptr) |
80bc7890 | 242 | { |
7cf1a27e | 243 | return DYNL_FILENAME (ptr); |
c487ad44 MV |
244 | } |
245 | ||
e841c3e0 | 246 | |
80bc7890 | 247 | static int |
e841c3e0 | 248 | dynl_obj_print (SCM exp, SCM port, scm_print_state *pstate) |
80bc7890 | 249 | { |
7cf1a27e MD |
250 | scm_puts ("#<dynamic-object ", port); |
251 | scm_iprin1 (DYNL_FILENAME (exp), port, pstate); | |
252 | if (DYNL_HANDLE (exp) == NULL) | |
253 | scm_puts (" (unlinked)", port); | |
254 | scm_putc ('>', port); | |
255 | return 1; | |
80bc7890 MV |
256 | } |
257 | ||
8e3ab003 | 258 | |
af45e3b0 | 259 | SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0, |
1e6808ea MG |
260 | (SCM filename), |
261 | "Open the dynamic library called @var{filename}. A library\n" | |
262 | "handle representing the opened library is returned; this handle\n" | |
263 | "should be used as the @var{dobj} argument to the following\n" | |
264 | "functions.") | |
1bbd0b84 | 265 | #define FUNC_NAME s_scm_dynamic_link |
80bc7890 | 266 | { |
7cf1a27e | 267 | void *handle; |
80bc7890 | 268 | |
1e6808ea | 269 | SCM_VALIDATE_STRING (1, filename); |
1e6808ea MG |
270 | handle = sysdep_dynl_link (SCM_STRING_CHARS (filename), FUNC_NAME); |
271 | SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (filename), handle); | |
80bc7890 | 272 | } |
1bbd0b84 | 273 | #undef FUNC_NAME |
80bc7890 | 274 | |
80bc7890 | 275 | |
a1ec6916 | 276 | SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0, |
1bbd0b84 | 277 | (SCM obj), |
b380b885 MD |
278 | "Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}\n" |
279 | "otherwise.") | |
1bbd0b84 | 280 | #define FUNC_NAME s_scm_dynamic_object_p |
80bc7890 | 281 | { |
e841c3e0 | 282 | return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_dynamic_obj, obj)); |
80bc7890 | 283 | } |
1bbd0b84 | 284 | #undef FUNC_NAME |
80bc7890 | 285 | |
b82c6ce0 | 286 | |
a1ec6916 | 287 | SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0, |
1bbd0b84 | 288 | (SCM dobj), |
b82c6ce0 | 289 | "Unlink the indicated object file from the application. The\n" |
1e6808ea | 290 | "argument @var{dobj} must have been obtained by a call to\n" |
b82c6ce0 | 291 | "@code{dynamic-link}. After @code{dynamic-unlink} has been\n" |
1e6808ea | 292 | "called on @var{dobj}, its content is no longer accessible.") |
1bbd0b84 | 293 | #define FUNC_NAME s_scm_dynamic_unlink |
80bc7890 | 294 | { |
7cf1a27e | 295 | /*fixme* GC-problem */ |
b82c6ce0 DH |
296 | SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj); |
297 | if (DYNL_HANDLE (dobj) == NULL) { | |
298 | SCM_MISC_ERROR ("Already unlinked: ~S", dobj); | |
299 | } else { | |
300 | SCM_DEFER_INTS; | |
301 | sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME); | |
302 | SET_DYNL_HANDLE (dobj, NULL); | |
303 | SCM_ALLOW_INTS; | |
304 | return SCM_UNSPECIFIED; | |
305 | } | |
80bc7890 | 306 | } |
1bbd0b84 | 307 | #undef FUNC_NAME |
80bc7890 | 308 | |
b82c6ce0 | 309 | |
a1ec6916 | 310 | SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0, |
a6d9e5ab DH |
311 | (SCM name, SCM dobj), |
312 | "Search the dynamic object @var{dobj} for the C function\n" | |
313 | "indicated by the string @var{name} and return some Scheme\n" | |
314 | "handle that can later be used with @code{dynamic-call} to\n" | |
315 | "actually call the function.\n\n" | |
b380b885 MD |
316 | "Regardless whether your C compiler prepends an underscore @samp{_} to\n" |
317 | "the global names in a program, you should @strong{not} include this\n" | |
318 | "underscore in @var{function}. Guile knows whether the underscore is\n" | |
872e0c72 | 319 | "needed or not and will add it when necessary.") |
1bbd0b84 | 320 | #define FUNC_NAME s_scm_dynamic_func |
80bc7890 | 321 | { |
a6d9e5ab DH |
322 | /* The returned handle is formed by casting the address of the function to a |
323 | * long value and converting this to a scheme number | |
324 | */ | |
325 | ||
7cf1a27e | 326 | void (*func) (); |
80bc7890 | 327 | |
a6d9e5ab | 328 | SCM_VALIDATE_STRING (1, name); |
7cf1a27e | 329 | /*fixme* GC-problem */ |
b82c6ce0 DH |
330 | SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj); |
331 | if (DYNL_HANDLE (dobj) == NULL) { | |
332 | SCM_MISC_ERROR ("Already unlinked: ~S", dobj); | |
333 | } else { | |
a002f1a2 DH |
334 | char *chars; |
335 | ||
b82c6ce0 | 336 | SCM_DEFER_INTS; |
a6d9e5ab | 337 | chars = SCM_STRING_CHARS (name); |
a002f1a2 | 338 | func = (void (*) ()) sysdep_dynl_func (chars, DYNL_HANDLE (dobj), FUNC_NAME); |
b82c6ce0 DH |
339 | SCM_ALLOW_INTS; |
340 | return scm_ulong2num ((unsigned long) func); | |
341 | } | |
80bc7890 | 342 | } |
1bbd0b84 | 343 | #undef FUNC_NAME |
80bc7890 | 344 | |
b82c6ce0 | 345 | |
a1ec6916 | 346 | SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0, |
1bbd0b84 | 347 | (SCM func, SCM dobj), |
1e6808ea MG |
348 | "Call the C function indicated by @var{func} and @var{dobj}.\n" |
349 | "The function is passed no arguments and its return value is\n" | |
350 | "ignored. When @var{function} is something returned by\n" | |
351 | "@code{dynamic-func}, call that function and ignore @var{dobj}.\n" | |
352 | "When @var{func} is a string , look it up in @var{dynobj}; this\n" | |
353 | "is equivalent to\n" | |
b380b885 | 354 | "@smallexample\n" |
1e6808ea | 355 | "(dynamic-call (dynamic-func @var{func} @var{dobj} #f))\n" |
b380b885 MD |
356 | "@end smallexample\n\n" |
357 | "Interrupts are deferred while the C function is executing (with\n" | |
872e0c72 | 358 | "@code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).") |
1bbd0b84 | 359 | #define FUNC_NAME s_scm_dynamic_call |
80bc7890 | 360 | { |
7cf1a27e MD |
361 | void (*fptr) (); |
362 | ||
a6d9e5ab | 363 | if (SCM_STRINGP (func)) |
7cf1a27e MD |
364 | func = scm_dynamic_func (func, dobj); |
365 | fptr = (void (*) ()) SCM_NUM2ULONG (1, func); | |
366 | SCM_DEFER_INTS; | |
367 | fptr (); | |
368 | SCM_ALLOW_INTS; | |
369 | return SCM_UNSPECIFIED; | |
80bc7890 | 370 | } |
1bbd0b84 | 371 | #undef FUNC_NAME |
80bc7890 | 372 | |
a1ec6916 | 373 | SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0, |
1bbd0b84 | 374 | (SCM func, SCM dobj, SCM args), |
1e6808ea MG |
375 | "Call the C function indicated by @var{func} and @var{dobj},\n" |
376 | "just like @code{dynamic-call}, but pass it some arguments and\n" | |
377 | "return its return value. The C function is expected to take\n" | |
378 | "two arguments and return an @code{int}, just like @code{main}:\n" | |
b380b885 MD |
379 | "@smallexample\n" |
380 | "int c_func (int argc, char **argv);\n" | |
381 | "@end smallexample\n\n" | |
1e6808ea MG |
382 | "The parameter @var{args} must be a list of strings and is\n" |
383 | "converted into an array of @code{char *}. The array is passed\n" | |
384 | "in @var{argv} and its size in @var{argc}. The return value is\n" | |
385 | "converted to a Scheme number and returned from the call to\n" | |
386 | "@code{dynamic-args-call}.") | |
1bbd0b84 | 387 | #define FUNC_NAME s_scm_dynamic_args_call |
80bc7890 | 388 | { |
7cf1a27e MD |
389 | int (*fptr) (int argc, char **argv); |
390 | int result, argc; | |
391 | char **argv; | |
392 | ||
a6d9e5ab | 393 | if (SCM_STRINGP (func)) |
7cf1a27e MD |
394 | func = scm_dynamic_func (func, dobj); |
395 | ||
396 | fptr = (int (*) (int, char **)) SCM_NUM2ULONG (1, func); | |
397 | SCM_DEFER_INTS; | |
398 | argv = scm_make_argv_from_stringlist (args, &argc, FUNC_NAME, SCM_ARG3); | |
399 | result = (*fptr) (argc, argv); | |
400 | scm_must_free_argv (argv); | |
401 | SCM_ALLOW_INTS; | |
402 | ||
403 | return SCM_MAKINUM (0L + result); | |
80bc7890 | 404 | } |
1bbd0b84 | 405 | #undef FUNC_NAME |
80bc7890 | 406 | |
1edae076 MV |
407 | void |
408 | scm_init_dynamic_linking () | |
409 | { | |
7cf1a27e | 410 | scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0); |
e841c3e0 KN |
411 | scm_set_smob_mark (scm_tc16_dynamic_obj, dynl_obj_mark); |
412 | scm_set_smob_print (scm_tc16_dynamic_obj, dynl_obj_print); | |
7cf1a27e | 413 | sysdep_dynl_init (); |
8dc9439f | 414 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 415 | #include "libguile/dynl.x" |
8dc9439f | 416 | #endif |
1edae076 | 417 | } |
89e00824 ML |
418 | |
419 | /* | |
420 | Local Variables: | |
421 | c-file-style: "gnu" | |
422 | End: | |
423 | */ |