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" |
c96d76b8 | 75 | #include "libguile/lang.h" |
a0599745 | 76 | #include "libguile/validate.h" |
408ea28a | 77 | |
07286af9 | 78 | #include "libltdl/ltdl.h" |
4feb69af | 79 | |
c2cbcc57 HWN |
80 | /* |
81 | From the libtool manual: "Note that libltdl is not threadsafe, | |
82 | i.e. a multithreaded application has to use a mutex for libltdl.". | |
83 | ||
84 | Guile does not currently support pre-emptive threads, so there is | |
85 | no mutex. Previously SCM_DEFER_INTS and SCM_ALLOW_INTS were used: | |
86 | they are mentioned here in case somebody is grepping for thread | |
87 | problems ;) | |
732b9327 GH |
88 | */ |
89 | ||
4feb69af | 90 | static void * |
af45e3b0 | 91 | sysdep_dynl_link (const char *fname, const char *subr) |
4feb69af | 92 | { |
7cf1a27e MD |
93 | lt_dlhandle handle; |
94 | handle = lt_dlopenext (fname); | |
4feb69af MV |
95 | if (NULL == handle) |
96 | { | |
01449aa5 DH |
97 | SCM fn; |
98 | SCM msg; | |
99 | ||
01449aa5 DH |
100 | fn = scm_makfrom0str (fname); |
101 | msg = scm_makfrom0str (lt_dlerror ()); | |
1afff620 | 102 | scm_misc_error (subr, "file: ~S, message: ~S", scm_list_2 (fn, msg)); |
4feb69af MV |
103 | } |
104 | return (void *) handle; | |
105 | } | |
106 | ||
107 | static void | |
108 | sysdep_dynl_unlink (void *handle, const char *subr) | |
109 | { | |
110 | if (lt_dlclose ((lt_dlhandle) handle)) | |
111 | { | |
7cf1a27e | 112 | scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL); |
4feb69af MV |
113 | } |
114 | } | |
115 | ||
116 | static void * | |
117 | sysdep_dynl_func (const char *symb, void *handle, const char *subr) | |
118 | { | |
119 | void *fptr; | |
120 | ||
121 | fptr = lt_dlsym ((lt_dlhandle) handle, symb); | |
122 | if (!fptr) | |
123 | { | |
7cf1a27e | 124 | scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL); |
4feb69af MV |
125 | } |
126 | return fptr; | |
127 | } | |
128 | ||
129 | static void | |
130 | sysdep_dynl_init () | |
131 | { | |
132 | lt_dlinit (); | |
133 | } | |
134 | ||
92c2555f | 135 | scm_t_bits scm_tc16_dynamic_obj; |
80bc7890 | 136 | |
b82c6ce0 DH |
137 | #define DYNL_FILENAME(x) (SCM_CELL_OBJECT_1 (x)) |
138 | #define DYNL_HANDLE(x) ((void *) SCM_CELL_WORD_2 (x)) | |
139 | #define SET_DYNL_HANDLE(x, v) (SCM_SET_CELL_WORD_2 ((x), (v))) | |
7cf1a27e | 140 | |
7cf1a27e | 141 | |
80bc7890 | 142 | static SCM |
e841c3e0 | 143 | dynl_obj_mark (SCM ptr) |
80bc7890 | 144 | { |
7cf1a27e | 145 | return DYNL_FILENAME (ptr); |
c487ad44 MV |
146 | } |
147 | ||
e841c3e0 | 148 | |
80bc7890 | 149 | static int |
e841c3e0 | 150 | dynl_obj_print (SCM exp, SCM port, scm_print_state *pstate) |
80bc7890 | 151 | { |
7cf1a27e MD |
152 | scm_puts ("#<dynamic-object ", port); |
153 | scm_iprin1 (DYNL_FILENAME (exp), port, pstate); | |
154 | if (DYNL_HANDLE (exp) == NULL) | |
155 | scm_puts (" (unlinked)", port); | |
156 | scm_putc ('>', port); | |
157 | return 1; | |
80bc7890 MV |
158 | } |
159 | ||
8e3ab003 | 160 | |
af45e3b0 | 161 | SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0, |
1e6808ea | 162 | (SCM filename), |
ee95d597 GH |
163 | "Find the shared object (shared library) denoted by\n" |
164 | "@var{filename} and link it into the running Guile\n" | |
165 | "application. The returned\n" | |
166 | "scheme object is a ``handle'' for the library which can\n" | |
167 | "be passed to @code{dynamic-func}, @code{dynamic-call} etc.\n\n" | |
168 | "Searching for object files is system dependent. Normally,\n" | |
169 | "if @var{filename} does have an explicit directory it will\n" | |
170 | "be searched for in locations\n" | |
171 | "such as @file{/usr/lib} and @file{/usr/local/lib}.") | |
1bbd0b84 | 172 | #define FUNC_NAME s_scm_dynamic_link |
80bc7890 | 173 | { |
7cf1a27e | 174 | void *handle; |
80bc7890 | 175 | |
1e6808ea | 176 | SCM_VALIDATE_STRING (1, filename); |
1e6808ea MG |
177 | handle = sysdep_dynl_link (SCM_STRING_CHARS (filename), FUNC_NAME); |
178 | SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (filename), handle); | |
80bc7890 | 179 | } |
1bbd0b84 | 180 | #undef FUNC_NAME |
80bc7890 | 181 | |
80bc7890 | 182 | |
a1ec6916 | 183 | SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0, |
1bbd0b84 | 184 | (SCM obj), |
ee95d597 GH |
185 | "Return @code{#t} if @var{obj} is a dynamic object handle,\n" |
186 | "or @code{#f} otherwise.") | |
1bbd0b84 | 187 | #define FUNC_NAME s_scm_dynamic_object_p |
80bc7890 | 188 | { |
e841c3e0 | 189 | return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_dynamic_obj, obj)); |
80bc7890 | 190 | } |
1bbd0b84 | 191 | #undef FUNC_NAME |
80bc7890 | 192 | |
b82c6ce0 | 193 | |
a1ec6916 | 194 | SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0, |
1bbd0b84 | 195 | (SCM dobj), |
ee95d597 GH |
196 | "Unlink a dynamic object from the application, if possible. The\n" |
197 | "object must have been linked by @code{dynamic-link}, with \n" | |
198 | "@var{dobj} the corresponding handle. After this procedure\n" | |
199 | "is called, the handle can no longer be used to access the\n" | |
200 | "object.") | |
1bbd0b84 | 201 | #define FUNC_NAME s_scm_dynamic_unlink |
80bc7890 | 202 | { |
7cf1a27e | 203 | /*fixme* GC-problem */ |
b82c6ce0 DH |
204 | SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj); |
205 | if (DYNL_HANDLE (dobj) == NULL) { | |
206 | SCM_MISC_ERROR ("Already unlinked: ~S", dobj); | |
207 | } else { | |
b82c6ce0 DH |
208 | sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME); |
209 | SET_DYNL_HANDLE (dobj, NULL); | |
b82c6ce0 DH |
210 | return SCM_UNSPECIFIED; |
211 | } | |
80bc7890 | 212 | } |
1bbd0b84 | 213 | #undef FUNC_NAME |
80bc7890 | 214 | |
b82c6ce0 | 215 | |
a1ec6916 | 216 | SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0, |
a6d9e5ab | 217 | (SCM name, SCM dobj), |
ee95d597 GH |
218 | "Return a ``handle'' for the function @var{name} in the\n" |
219 | "shared object referred to by @var{dobj}. The handle\n" | |
220 | "can be passed to @code{dynamic-call} to actually\n" | |
221 | "call the function.\n\n" | |
222 | "Regardless whether your C compiler prepends an underscore\n" | |
223 | "@samp{_} to the global names in a program, you should\n" | |
224 | "@strong{not} include this underscore in @var{name}\n" | |
225 | "since it will be added automatically when necessary.") | |
1bbd0b84 | 226 | #define FUNC_NAME s_scm_dynamic_func |
80bc7890 | 227 | { |
a6d9e5ab DH |
228 | /* The returned handle is formed by casting the address of the function to a |
229 | * long value and converting this to a scheme number | |
230 | */ | |
231 | ||
7cf1a27e | 232 | void (*func) (); |
80bc7890 | 233 | |
a6d9e5ab | 234 | SCM_VALIDATE_STRING (1, name); |
7cf1a27e | 235 | /*fixme* GC-problem */ |
b82c6ce0 DH |
236 | SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj); |
237 | if (DYNL_HANDLE (dobj) == NULL) { | |
238 | SCM_MISC_ERROR ("Already unlinked: ~S", dobj); | |
239 | } else { | |
a002f1a2 DH |
240 | char *chars; |
241 | ||
a6d9e5ab | 242 | chars = SCM_STRING_CHARS (name); |
a002f1a2 | 243 | func = (void (*) ()) sysdep_dynl_func (chars, DYNL_HANDLE (dobj), FUNC_NAME); |
b82c6ce0 DH |
244 | return scm_ulong2num ((unsigned long) func); |
245 | } | |
80bc7890 | 246 | } |
1bbd0b84 | 247 | #undef FUNC_NAME |
80bc7890 | 248 | |
b82c6ce0 | 249 | |
a1ec6916 | 250 | SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0, |
1bbd0b84 | 251 | (SCM func, SCM dobj), |
46732b54 GH |
252 | "Call a C function in a dynamic object. Two styles of\n" |
253 | "invocation are supported:\n\n" | |
254 | "@itemize @bullet\n" | |
255 | "@item @var{func} can be a function handle returned by\n" | |
256 | "@code{dynamic-func}. In this case @var{dobj} is\n" | |
257 | "ignored\n" | |
258 | "@item @var{func} can be a string with the name of the\n" | |
259 | "function to call, with @var{dobj} the handle of the\n" | |
260 | "dynamic object in which to find the function.\n" | |
261 | "This is equivalent to\n" | |
262 | "@smallexample\n\n" | |
263 | "(dynamic-call (dynamic-func @var{func} @var{dobj}) #f)\n" | |
264 | "@end smallexample\n" | |
265 | "@end itemize\n\n" | |
266 | "In either case, the function is passed no arguments\n" | |
267 | "and its return value is ignored.") | |
1bbd0b84 | 268 | #define FUNC_NAME s_scm_dynamic_call |
80bc7890 | 269 | { |
7cf1a27e MD |
270 | void (*fptr) (); |
271 | ||
a6d9e5ab | 272 | if (SCM_STRINGP (func)) |
7cf1a27e MD |
273 | func = scm_dynamic_func (func, dobj); |
274 | fptr = (void (*) ()) SCM_NUM2ULONG (1, func); | |
7cf1a27e | 275 | fptr (); |
7cf1a27e | 276 | return SCM_UNSPECIFIED; |
80bc7890 | 277 | } |
1bbd0b84 | 278 | #undef FUNC_NAME |
80bc7890 | 279 | |
2ee08a28 GH |
280 | /* return a newly allocated array of char pointers to each of the strings |
281 | in args, with a terminating NULL pointer. */ | |
282 | /* Note: a similar function is defined in posix.c, but we don't necessarily | |
283 | want to export it. */ | |
284 | static char **allocate_string_pointers (SCM args, int *num_args_return) | |
285 | { | |
286 | char **result; | |
287 | int n_args = scm_ilength (args); | |
288 | int i; | |
289 | ||
290 | SCM_ASSERT (n_args >= 0, args, SCM_ARGn, "allocate_string_pointers"); | |
291 | result = (char **) scm_malloc ((n_args + 1) * sizeof (char *)); | |
292 | result[n_args] = NULL; | |
293 | for (i = 0; i < n_args; i++) | |
294 | { | |
295 | SCM car = SCM_CAR (args); | |
296 | ||
297 | if (!SCM_STRINGP (car)) | |
298 | { | |
299 | free (result); | |
300 | scm_wrong_type_arg ("allocate_string_pointers", SCM_ARGn, car); | |
301 | } | |
302 | result[i] = SCM_STRING_CHARS (SCM_CAR (args)); | |
303 | args = SCM_CDR (args); | |
304 | } | |
305 | *num_args_return = n_args; | |
306 | return result; | |
307 | } | |
308 | ||
a1ec6916 | 309 | SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0, |
1bbd0b84 | 310 | (SCM func, SCM dobj, SCM args), |
1e6808ea MG |
311 | "Call the C function indicated by @var{func} and @var{dobj},\n" |
312 | "just like @code{dynamic-call}, but pass it some arguments and\n" | |
313 | "return its return value. The C function is expected to take\n" | |
314 | "two arguments and return an @code{int}, just like @code{main}:\n" | |
b380b885 MD |
315 | "@smallexample\n" |
316 | "int c_func (int argc, char **argv);\n" | |
317 | "@end smallexample\n\n" | |
1e6808ea MG |
318 | "The parameter @var{args} must be a list of strings and is\n" |
319 | "converted into an array of @code{char *}. The array is passed\n" | |
320 | "in @var{argv} and its size in @var{argc}. The return value is\n" | |
321 | "converted to a Scheme number and returned from the call to\n" | |
322 | "@code{dynamic-args-call}.") | |
1bbd0b84 | 323 | #define FUNC_NAME s_scm_dynamic_args_call |
80bc7890 | 324 | { |
7cf1a27e MD |
325 | int (*fptr) (int argc, char **argv); |
326 | int result, argc; | |
327 | char **argv; | |
328 | ||
a6d9e5ab | 329 | if (SCM_STRINGP (func)) |
7cf1a27e MD |
330 | func = scm_dynamic_func (func, dobj); |
331 | ||
332 | fptr = (int (*) (int, char **)) SCM_NUM2ULONG (1, func); | |
2ee08a28 GH |
333 | argv = allocate_string_pointers (args, &argc); |
334 | /* if the procedure mutates its arguments, the original strings will be | |
335 | changed -- in Guile 1.6 and earlier, this wasn't the case since a | |
336 | new copy of each string was allocated. */ | |
7cf1a27e | 337 | result = (*fptr) (argc, argv); |
2ee08a28 | 338 | free (argv); |
7cf1a27e MD |
339 | |
340 | return SCM_MAKINUM (0L + result); | |
80bc7890 | 341 | } |
1bbd0b84 | 342 | #undef FUNC_NAME |
80bc7890 | 343 | |
1edae076 MV |
344 | void |
345 | scm_init_dynamic_linking () | |
346 | { | |
7cf1a27e | 347 | scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0); |
e841c3e0 KN |
348 | scm_set_smob_mark (scm_tc16_dynamic_obj, dynl_obj_mark); |
349 | scm_set_smob_print (scm_tc16_dynamic_obj, dynl_obj_print); | |
7cf1a27e | 350 | sysdep_dynl_init (); |
a0599745 | 351 | #include "libguile/dynl.x" |
1edae076 | 352 | } |
89e00824 ML |
353 | |
354 | /* | |
355 | Local Variables: | |
356 | c-file-style: "gnu" | |
357 | End: | |
358 | */ |