Commit | Line | Data |
---|---|---|
1edae076 MV |
1 | /* dynl.c - dynamic linking |
2 | * | |
4d3526d0 KR |
3 | * Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001, 2002, |
4 | * 2003 Free Software Foundation, Inc. | |
1edae076 | 5 | * |
73be1d9e MV |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2.1 of the License, or (at your option) any later version. | |
1edae076 | 10 | * |
73be1d9e MV |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
1edae076 | 15 | * |
73be1d9e MV |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | */ | |
1edae076 | 20 | |
1bbd0b84 GB |
21 | |
22 | ||
1edae076 MV |
23 | /* "dynl.c" dynamically link&load object files. |
24 | Author: Aubrey Jaffer | |
25 | Modified for libguile by Marius Vollmer */ | |
26 | ||
104d4533 | 27 | #if 0 /* Disabled until we know for sure that it isn't needed */ |
80bc7890 MV |
28 | /* XXX - This is only here to drag in a definition of __eprintf. This |
29 | is needed for proper operation of dynamic linking. The real | |
30 | solution would probably be a shared libgcc. */ | |
31 | ||
32 | #undef NDEBUG | |
33 | #include <assert.h> | |
34 | ||
35 | static void | |
36 | maybe_drag_in_eprintf () | |
37 | { | |
38 | assert (!maybe_drag_in_eprintf); | |
39 | } | |
104d4533 | 40 | #endif |
80bc7890 | 41 | |
96599e6a | 42 | #include <stdio.h> |
13070bd3 DH |
43 | #include <string.h> |
44 | ||
a0599745 MD |
45 | #include "libguile/_scm.h" |
46 | #include "libguile/dynl.h" | |
47 | #include "libguile/smob.h" | |
48 | #include "libguile/keywords.h" | |
49 | #include "libguile/ports.h" | |
50 | #include "libguile/strings.h" | |
a0e0793f | 51 | #include "libguile/deprecation.h" |
c96d76b8 | 52 | #include "libguile/lang.h" |
a0599745 | 53 | #include "libguile/validate.h" |
7f9994d9 | 54 | #include "libguile/dynwind.h" |
408ea28a | 55 | |
66d4f5cc | 56 | #include "guile-ltdl.h" |
4feb69af | 57 | |
c2cbcc57 HWN |
58 | /* |
59 | From the libtool manual: "Note that libltdl is not threadsafe, | |
60 | i.e. a multithreaded application has to use a mutex for libltdl.". | |
61 | ||
62 | Guile does not currently support pre-emptive threads, so there is | |
63 | no mutex. Previously SCM_DEFER_INTS and SCM_ALLOW_INTS were used: | |
64 | they are mentioned here in case somebody is grepping for thread | |
65 | problems ;) | |
732b9327 GH |
66 | */ |
67 | ||
4feb69af | 68 | static void * |
af45e3b0 | 69 | sysdep_dynl_link (const char *fname, const char *subr) |
4feb69af | 70 | { |
66d4f5cc | 71 | scm_lt_dlhandle handle; |
8e583c6e | 72 | handle = scm_lt_dlopenext (fname); |
4feb69af MV |
73 | if (NULL == handle) |
74 | { | |
01449aa5 DH |
75 | SCM fn; |
76 | SCM msg; | |
77 | ||
01449aa5 | 78 | fn = scm_makfrom0str (fname); |
8e583c6e | 79 | msg = scm_makfrom0str (scm_lt_dlerror ()); |
1afff620 | 80 | scm_misc_error (subr, "file: ~S, message: ~S", scm_list_2 (fn, msg)); |
4feb69af MV |
81 | } |
82 | return (void *) handle; | |
83 | } | |
84 | ||
85 | static void | |
86 | sysdep_dynl_unlink (void *handle, const char *subr) | |
87 | { | |
66d4f5cc | 88 | if (scm_lt_dlclose ((scm_lt_dlhandle) handle)) |
4feb69af | 89 | { |
8e583c6e | 90 | scm_misc_error (subr, (char *) scm_lt_dlerror (), SCM_EOL); |
4feb69af MV |
91 | } |
92 | } | |
93 | ||
94 | static void * | |
95 | sysdep_dynl_func (const char *symb, void *handle, const char *subr) | |
96 | { | |
97 | void *fptr; | |
98 | ||
66d4f5cc | 99 | fptr = scm_lt_dlsym ((scm_lt_dlhandle) handle, symb); |
4feb69af MV |
100 | if (!fptr) |
101 | { | |
8e583c6e | 102 | scm_misc_error (subr, (char *) scm_lt_dlerror (), SCM_EOL); |
4feb69af MV |
103 | } |
104 | return fptr; | |
105 | } | |
106 | ||
107 | static void | |
108 | sysdep_dynl_init () | |
109 | { | |
8e583c6e | 110 | scm_lt_dlinit (); |
4feb69af MV |
111 | } |
112 | ||
92c2555f | 113 | scm_t_bits scm_tc16_dynamic_obj; |
80bc7890 | 114 | |
f5710d53 MV |
115 | #define DYNL_FILENAME SCM_SMOB_OBJECT |
116 | #define DYNL_HANDLE(x) ((void *) SCM_SMOB_DATA_2 (x)) | |
2ff08405 | 117 | #define SET_DYNL_HANDLE(x, v) (SCM_SET_SMOB_DATA_2 ((x), (scm_t_bits) (v))) |
7cf1a27e | 118 | |
7cf1a27e | 119 | |
80bc7890 | 120 | static SCM |
e841c3e0 | 121 | dynl_obj_mark (SCM ptr) |
80bc7890 | 122 | { |
7cf1a27e | 123 | return DYNL_FILENAME (ptr); |
c487ad44 MV |
124 | } |
125 | ||
e841c3e0 | 126 | |
80bc7890 | 127 | static int |
e841c3e0 | 128 | dynl_obj_print (SCM exp, SCM port, scm_print_state *pstate) |
80bc7890 | 129 | { |
7cf1a27e MD |
130 | scm_puts ("#<dynamic-object ", port); |
131 | scm_iprin1 (DYNL_FILENAME (exp), port, pstate); | |
132 | if (DYNL_HANDLE (exp) == NULL) | |
133 | scm_puts (" (unlinked)", port); | |
134 | scm_putc ('>', port); | |
135 | return 1; | |
80bc7890 MV |
136 | } |
137 | ||
8e3ab003 | 138 | |
af45e3b0 | 139 | SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0, |
1e6808ea | 140 | (SCM filename), |
ee95d597 GH |
141 | "Find the shared object (shared library) denoted by\n" |
142 | "@var{filename} and link it into the running Guile\n" | |
143 | "application. The returned\n" | |
144 | "scheme object is a ``handle'' for the library which can\n" | |
145 | "be passed to @code{dynamic-func}, @code{dynamic-call} etc.\n\n" | |
146 | "Searching for object files is system dependent. Normally,\n" | |
147 | "if @var{filename} does have an explicit directory it will\n" | |
148 | "be searched for in locations\n" | |
149 | "such as @file{/usr/lib} and @file{/usr/local/lib}.") | |
1bbd0b84 | 150 | #define FUNC_NAME s_scm_dynamic_link |
80bc7890 | 151 | { |
7cf1a27e | 152 | void *handle; |
7f9994d9 | 153 | char *file; |
80bc7890 | 154 | |
7f9994d9 MV |
155 | scm_frame_begin (0); |
156 | file = scm_to_locale_string (filename); | |
157 | scm_frame_free (file); | |
158 | handle = sysdep_dynl_link (file, FUNC_NAME); | |
159 | scm_frame_end (); | |
1e6808ea | 160 | SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (filename), handle); |
80bc7890 | 161 | } |
1bbd0b84 | 162 | #undef FUNC_NAME |
80bc7890 | 163 | |
80bc7890 | 164 | |
a1ec6916 | 165 | SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0, |
1bbd0b84 | 166 | (SCM obj), |
ee95d597 GH |
167 | "Return @code{#t} if @var{obj} is a dynamic object handle,\n" |
168 | "or @code{#f} otherwise.") | |
1bbd0b84 | 169 | #define FUNC_NAME s_scm_dynamic_object_p |
80bc7890 | 170 | { |
7888309b | 171 | return scm_from_bool (SCM_TYP16_PREDICATE (scm_tc16_dynamic_obj, obj)); |
80bc7890 | 172 | } |
1bbd0b84 | 173 | #undef FUNC_NAME |
80bc7890 | 174 | |
b82c6ce0 | 175 | |
a1ec6916 | 176 | SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0, |
1bbd0b84 | 177 | (SCM dobj), |
ee95d597 GH |
178 | "Unlink a dynamic object from the application, if possible. The\n" |
179 | "object must have been linked by @code{dynamic-link}, with \n" | |
180 | "@var{dobj} the corresponding handle. After this procedure\n" | |
181 | "is called, the handle can no longer be used to access the\n" | |
182 | "object.") | |
1bbd0b84 | 183 | #define FUNC_NAME s_scm_dynamic_unlink |
80bc7890 | 184 | { |
7cf1a27e | 185 | /*fixme* GC-problem */ |
b82c6ce0 DH |
186 | SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj); |
187 | if (DYNL_HANDLE (dobj) == NULL) { | |
9e375910 | 188 | SCM_MISC_ERROR ("Already unlinked: ~S", scm_list_1 (dobj)); |
b82c6ce0 | 189 | } else { |
b82c6ce0 DH |
190 | sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME); |
191 | SET_DYNL_HANDLE (dobj, NULL); | |
b82c6ce0 DH |
192 | return SCM_UNSPECIFIED; |
193 | } | |
80bc7890 | 194 | } |
1bbd0b84 | 195 | #undef FUNC_NAME |
80bc7890 | 196 | |
b82c6ce0 | 197 | |
a1ec6916 | 198 | SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0, |
a6d9e5ab | 199 | (SCM name, SCM dobj), |
ee95d597 GH |
200 | "Return a ``handle'' for the function @var{name} in the\n" |
201 | "shared object referred to by @var{dobj}. The handle\n" | |
202 | "can be passed to @code{dynamic-call} to actually\n" | |
203 | "call the function.\n\n" | |
204 | "Regardless whether your C compiler prepends an underscore\n" | |
205 | "@samp{_} to the global names in a program, you should\n" | |
206 | "@strong{not} include this underscore in @var{name}\n" | |
207 | "since it will be added automatically when necessary.") | |
1bbd0b84 | 208 | #define FUNC_NAME s_scm_dynamic_func |
80bc7890 | 209 | { |
a6d9e5ab DH |
210 | /* The returned handle is formed by casting the address of the function to a |
211 | * long value and converting this to a scheme number | |
212 | */ | |
213 | ||
7cf1a27e | 214 | void (*func) (); |
80bc7890 | 215 | |
a6d9e5ab | 216 | SCM_VALIDATE_STRING (1, name); |
7cf1a27e | 217 | /*fixme* GC-problem */ |
b82c6ce0 DH |
218 | SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj); |
219 | if (DYNL_HANDLE (dobj) == NULL) { | |
220 | SCM_MISC_ERROR ("Already unlinked: ~S", dobj); | |
221 | } else { | |
a002f1a2 DH |
222 | char *chars; |
223 | ||
7f9994d9 MV |
224 | scm_frame_begin (0); |
225 | chars = scm_to_locale_string (name); | |
226 | scm_frame_free (chars); | |
b9bd8526 MV |
227 | func = (void (*) ()) sysdep_dynl_func (chars, DYNL_HANDLE (dobj), |
228 | FUNC_NAME); | |
7f9994d9 | 229 | scm_frame_end (); |
b9bd8526 | 230 | return scm_from_ulong ((unsigned long) func); |
b82c6ce0 | 231 | } |
80bc7890 | 232 | } |
1bbd0b84 | 233 | #undef FUNC_NAME |
80bc7890 | 234 | |
b82c6ce0 | 235 | |
a1ec6916 | 236 | SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0, |
1bbd0b84 | 237 | (SCM func, SCM dobj), |
46732b54 GH |
238 | "Call a C function in a dynamic object. Two styles of\n" |
239 | "invocation are supported:\n\n" | |
240 | "@itemize @bullet\n" | |
241 | "@item @var{func} can be a function handle returned by\n" | |
242 | "@code{dynamic-func}. In this case @var{dobj} is\n" | |
243 | "ignored\n" | |
244 | "@item @var{func} can be a string with the name of the\n" | |
245 | "function to call, with @var{dobj} the handle of the\n" | |
246 | "dynamic object in which to find the function.\n" | |
247 | "This is equivalent to\n" | |
248 | "@smallexample\n\n" | |
249 | "(dynamic-call (dynamic-func @var{func} @var{dobj}) #f)\n" | |
250 | "@end smallexample\n" | |
251 | "@end itemize\n\n" | |
252 | "In either case, the function is passed no arguments\n" | |
253 | "and its return value is ignored.") | |
1bbd0b84 | 254 | #define FUNC_NAME s_scm_dynamic_call |
80bc7890 | 255 | { |
7cf1a27e MD |
256 | void (*fptr) (); |
257 | ||
7f9994d9 | 258 | if (scm_is_string (func)) |
7cf1a27e | 259 | func = scm_dynamic_func (func, dobj); |
7f9994d9 | 260 | fptr = (void (*) ()) scm_to_ulong (func); |
7cf1a27e | 261 | fptr (); |
7cf1a27e | 262 | return SCM_UNSPECIFIED; |
80bc7890 | 263 | } |
1bbd0b84 | 264 | #undef FUNC_NAME |
80bc7890 | 265 | |
7f9994d9 MV |
266 | static void |
267 | free_string_pointers (void *data) | |
2ee08a28 | 268 | { |
7f9994d9 | 269 | scm_i_free_string_pointers ((char **)data); |
2ee08a28 GH |
270 | } |
271 | ||
a1ec6916 | 272 | SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0, |
1bbd0b84 | 273 | (SCM func, SCM dobj, SCM args), |
1e6808ea MG |
274 | "Call the C function indicated by @var{func} and @var{dobj},\n" |
275 | "just like @code{dynamic-call}, but pass it some arguments and\n" | |
276 | "return its return value. The C function is expected to take\n" | |
277 | "two arguments and return an @code{int}, just like @code{main}:\n" | |
b380b885 MD |
278 | "@smallexample\n" |
279 | "int c_func (int argc, char **argv);\n" | |
280 | "@end smallexample\n\n" | |
1e6808ea MG |
281 | "The parameter @var{args} must be a list of strings and is\n" |
282 | "converted into an array of @code{char *}. The array is passed\n" | |
283 | "in @var{argv} and its size in @var{argc}. The return value is\n" | |
284 | "converted to a Scheme number and returned from the call to\n" | |
285 | "@code{dynamic-args-call}.") | |
1bbd0b84 | 286 | #define FUNC_NAME s_scm_dynamic_args_call |
80bc7890 | 287 | { |
7cf1a27e MD |
288 | int (*fptr) (int argc, char **argv); |
289 | int result, argc; | |
290 | char **argv; | |
291 | ||
7f9994d9 MV |
292 | scm_frame_begin (0); |
293 | ||
294 | if (scm_is_string (func)) | |
7cf1a27e MD |
295 | func = scm_dynamic_func (func, dobj); |
296 | ||
7f9994d9 MV |
297 | fptr = (int (*) (int, char **)) scm_to_ulong (func); |
298 | ||
299 | argv = scm_i_allocate_string_pointers (args); | |
300 | scm_frame_unwind_handler (free_string_pointers, argv, | |
301 | SCM_F_WIND_EXPLICITLY); | |
302 | for (argc = 0; argv[argc]; argc++) | |
303 | ; | |
7cf1a27e | 304 | result = (*fptr) (argc, argv); |
7cf1a27e | 305 | |
7f9994d9 | 306 | scm_frame_end (); |
e11e83f3 | 307 | return scm_from_int (result); |
80bc7890 | 308 | } |
1bbd0b84 | 309 | #undef FUNC_NAME |
80bc7890 | 310 | |
1edae076 MV |
311 | void |
312 | scm_init_dynamic_linking () | |
313 | { | |
7cf1a27e | 314 | scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0); |
e841c3e0 KN |
315 | scm_set_smob_mark (scm_tc16_dynamic_obj, dynl_obj_mark); |
316 | scm_set_smob_print (scm_tc16_dynamic_obj, dynl_obj_print); | |
7cf1a27e | 317 | sysdep_dynl_init (); |
a0599745 | 318 | #include "libguile/dynl.x" |
1edae076 | 319 | } |
89e00824 ML |
320 | |
321 | /* | |
322 | Local Variables: | |
323 | c-file-style: "gnu" | |
324 | End: | |
325 | */ |