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