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