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