2006-02-01 Ludovic Courtès <ludovic.courtes@laas.fr>
[bpt/guile.git] / libguile / dynl.c
CommitLineData
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
92205699 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 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
35static void
36maybe_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
a8255dca 56#include <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
9de87eea
MV
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 ;)
732b9327 66*/
e45947bf 67/* njrev: not threadsafe, protection needed as described above */
732b9327 68
4feb69af 69static void *
af45e3b0 70sysdep_dynl_link (const char *fname, const char *subr)
4feb69af 71{
a8255dca
MV
72 lt_dlhandle handle;
73 handle = lt_dlopenext (fname);
4feb69af
MV
74 if (NULL == handle)
75 {
01449aa5
DH
76 SCM fn;
77 SCM msg;
78
cc95e00a 79 fn = scm_from_locale_string (fname);
a8255dca 80 msg = scm_from_locale_string (lt_dlerror ());
1afff620 81 scm_misc_error (subr, "file: ~S, message: ~S", scm_list_2 (fn, msg));
4feb69af
MV
82 }
83 return (void *) handle;
84}
85
86static void
87sysdep_dynl_unlink (void *handle, const char *subr)
88{
a8255dca 89 if (lt_dlclose ((lt_dlhandle) handle))
4feb69af 90 {
a8255dca 91 scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
4feb69af
MV
92 }
93}
94
95static void *
96sysdep_dynl_func (const char *symb, void *handle, const char *subr)
97{
98 void *fptr;
99
a8255dca 100 fptr = lt_dlsym ((lt_dlhandle) handle, symb);
4feb69af
MV
101 if (!fptr)
102 {
a8255dca 103 scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
4feb69af
MV
104 }
105 return fptr;
106}
107
108static void
109sysdep_dynl_init ()
110{
a8255dca 111 lt_dlinit ();
4feb69af
MV
112}
113
92c2555f 114scm_t_bits scm_tc16_dynamic_obj;
80bc7890 115
f5710d53
MV
116#define DYNL_FILENAME SCM_SMOB_OBJECT
117#define DYNL_HANDLE(x) ((void *) SCM_SMOB_DATA_2 (x))
2ff08405 118#define SET_DYNL_HANDLE(x, v) (SCM_SET_SMOB_DATA_2 ((x), (scm_t_bits) (v)))
7cf1a27e 119
7cf1a27e 120
80bc7890 121static SCM
e841c3e0 122dynl_obj_mark (SCM ptr)
80bc7890 123{
7cf1a27e 124 return DYNL_FILENAME (ptr);
c487ad44
MV
125}
126
e841c3e0 127
80bc7890 128static int
e841c3e0 129dynl_obj_print (SCM exp, SCM port, scm_print_state *pstate)
80bc7890 130{
7cf1a27e
MD
131 scm_puts ("#<dynamic-object ", port);
132 scm_iprin1 (DYNL_FILENAME (exp), port, pstate);
133 if (DYNL_HANDLE (exp) == NULL)
134 scm_puts (" (unlinked)", port);
135 scm_putc ('>', port);
136 return 1;
80bc7890
MV
137}
138
8e3ab003 139
af45e3b0 140SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0,
1e6808ea 141 (SCM filename),
ee95d597
GH
142 "Find the shared object (shared library) denoted by\n"
143 "@var{filename} and link it into the running Guile\n"
144 "application. The returned\n"
145 "scheme object is a ``handle'' for the library which can\n"
146 "be passed to @code{dynamic-func}, @code{dynamic-call} etc.\n\n"
147 "Searching for object files is system dependent. Normally,\n"
148 "if @var{filename} does have an explicit directory it will\n"
149 "be searched for in locations\n"
150 "such as @file{/usr/lib} and @file{/usr/local/lib}.")
1bbd0b84 151#define FUNC_NAME s_scm_dynamic_link
80bc7890 152{
7cf1a27e 153 void *handle;
7f9994d9 154 char *file;
80bc7890 155
661ae7ab 156 scm_dynwind_begin (0);
7f9994d9 157 file = scm_to_locale_string (filename);
661ae7ab 158 scm_dynwind_free (file);
7f9994d9 159 handle = sysdep_dynl_link (file, FUNC_NAME);
661ae7ab 160 scm_dynwind_end ();
1e6808ea 161 SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (filename), handle);
80bc7890 162}
1bbd0b84 163#undef FUNC_NAME
80bc7890 164
80bc7890 165
a1ec6916 166SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0,
1bbd0b84 167 (SCM obj),
ee95d597
GH
168 "Return @code{#t} if @var{obj} is a dynamic object handle,\n"
169 "or @code{#f} otherwise.")
1bbd0b84 170#define FUNC_NAME s_scm_dynamic_object_p
80bc7890 171{
7888309b 172 return scm_from_bool (SCM_TYP16_PREDICATE (scm_tc16_dynamic_obj, obj));
80bc7890 173}
1bbd0b84 174#undef FUNC_NAME
80bc7890 175
b82c6ce0 176
a1ec6916 177SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0,
1bbd0b84 178 (SCM dobj),
ee95d597
GH
179 "Unlink a dynamic object from the application, if possible. The\n"
180 "object must have been linked by @code{dynamic-link}, with \n"
181 "@var{dobj} the corresponding handle. After this procedure\n"
182 "is called, the handle can no longer be used to access the\n"
183 "object.")
1bbd0b84 184#define FUNC_NAME s_scm_dynamic_unlink
80bc7890 185{
7cf1a27e 186 /*fixme* GC-problem */
b82c6ce0
DH
187 SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj);
188 if (DYNL_HANDLE (dobj) == NULL) {
9e375910 189 SCM_MISC_ERROR ("Already unlinked: ~S", scm_list_1 (dobj));
b82c6ce0 190 } else {
b82c6ce0
DH
191 sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME);
192 SET_DYNL_HANDLE (dobj, NULL);
b82c6ce0
DH
193 return SCM_UNSPECIFIED;
194 }
80bc7890 195}
1bbd0b84 196#undef FUNC_NAME
80bc7890 197
b82c6ce0 198
a1ec6916 199SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0,
a6d9e5ab 200 (SCM name, SCM dobj),
ee95d597
GH
201 "Return a ``handle'' for the function @var{name} in the\n"
202 "shared object referred to by @var{dobj}. The handle\n"
203 "can be passed to @code{dynamic-call} to actually\n"
204 "call the function.\n\n"
205 "Regardless whether your C compiler prepends an underscore\n"
206 "@samp{_} to the global names in a program, you should\n"
207 "@strong{not} include this underscore in @var{name}\n"
208 "since it will be added automatically when necessary.")
1bbd0b84 209#define FUNC_NAME s_scm_dynamic_func
80bc7890 210{
a6d9e5ab
DH
211 /* The returned handle is formed by casting the address of the function to a
212 * long value and converting this to a scheme number
213 */
214
7cf1a27e 215 void (*func) ();
80bc7890 216
a6d9e5ab 217 SCM_VALIDATE_STRING (1, name);
7cf1a27e 218 /*fixme* GC-problem */
b82c6ce0
DH
219 SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj);
220 if (DYNL_HANDLE (dobj) == NULL) {
221 SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
222 } else {
a002f1a2
DH
223 char *chars;
224
661ae7ab 225 scm_dynwind_begin (0);
7f9994d9 226 chars = scm_to_locale_string (name);
661ae7ab 227 scm_dynwind_free (chars);
b9bd8526
MV
228 func = (void (*) ()) sysdep_dynl_func (chars, DYNL_HANDLE (dobj),
229 FUNC_NAME);
661ae7ab 230 scm_dynwind_end ();
b9bd8526 231 return scm_from_ulong ((unsigned long) func);
b82c6ce0 232 }
80bc7890 233}
1bbd0b84 234#undef FUNC_NAME
80bc7890 235
b82c6ce0 236
a1ec6916 237SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0,
1bbd0b84 238 (SCM func, SCM dobj),
46732b54
GH
239 "Call a C function in a dynamic object. Two styles of\n"
240 "invocation are supported:\n\n"
241 "@itemize @bullet\n"
242 "@item @var{func} can be a function handle returned by\n"
243 "@code{dynamic-func}. In this case @var{dobj} is\n"
244 "ignored\n"
245 "@item @var{func} can be a string with the name of the\n"
246 "function to call, with @var{dobj} the handle of the\n"
247 "dynamic object in which to find the function.\n"
248 "This is equivalent to\n"
249 "@smallexample\n\n"
250 "(dynamic-call (dynamic-func @var{func} @var{dobj}) #f)\n"
251 "@end smallexample\n"
252 "@end itemize\n\n"
253 "In either case, the function is passed no arguments\n"
254 "and its return value is ignored.")
1bbd0b84 255#define FUNC_NAME s_scm_dynamic_call
80bc7890 256{
7cf1a27e
MD
257 void (*fptr) ();
258
7f9994d9 259 if (scm_is_string (func))
7cf1a27e 260 func = scm_dynamic_func (func, dobj);
7f9994d9 261 fptr = (void (*) ()) scm_to_ulong (func);
7cf1a27e 262 fptr ();
7cf1a27e 263 return SCM_UNSPECIFIED;
80bc7890 264}
1bbd0b84 265#undef FUNC_NAME
80bc7890 266
7f9994d9
MV
267static void
268free_string_pointers (void *data)
2ee08a28 269{
7f9994d9 270 scm_i_free_string_pointers ((char **)data);
2ee08a28
GH
271}
272
a1ec6916 273SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0,
1bbd0b84 274 (SCM func, SCM dobj, SCM args),
1e6808ea
MG
275 "Call the C function indicated by @var{func} and @var{dobj},\n"
276 "just like @code{dynamic-call}, but pass it some arguments and\n"
277 "return its return value. The C function is expected to take\n"
278 "two arguments and return an @code{int}, just like @code{main}:\n"
b380b885
MD
279 "@smallexample\n"
280 "int c_func (int argc, char **argv);\n"
281 "@end smallexample\n\n"
1e6808ea
MG
282 "The parameter @var{args} must be a list of strings and is\n"
283 "converted into an array of @code{char *}. The array is passed\n"
284 "in @var{argv} and its size in @var{argc}. The return value is\n"
285 "converted to a Scheme number and returned from the call to\n"
286 "@code{dynamic-args-call}.")
1bbd0b84 287#define FUNC_NAME s_scm_dynamic_args_call
80bc7890 288{
7cf1a27e
MD
289 int (*fptr) (int argc, char **argv);
290 int result, argc;
291 char **argv;
292
661ae7ab 293 scm_dynwind_begin (0);
7f9994d9
MV
294
295 if (scm_is_string (func))
7cf1a27e
MD
296 func = scm_dynamic_func (func, dobj);
297
7f9994d9
MV
298 fptr = (int (*) (int, char **)) scm_to_ulong (func);
299
300 argv = scm_i_allocate_string_pointers (args);
661ae7ab
MV
301 scm_dynwind_unwind_handler (free_string_pointers, argv,
302 SCM_F_WIND_EXPLICITLY);
7f9994d9
MV
303 for (argc = 0; argv[argc]; argc++)
304 ;
7cf1a27e 305 result = (*fptr) (argc, argv);
7cf1a27e 306
661ae7ab 307 scm_dynwind_end ();
e11e83f3 308 return scm_from_int (result);
80bc7890 309}
1bbd0b84 310#undef FUNC_NAME
80bc7890 311
1edae076
MV
312void
313scm_init_dynamic_linking ()
314{
7cf1a27e 315 scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0);
e841c3e0
KN
316 scm_set_smob_mark (scm_tc16_dynamic_obj, dynl_obj_mark);
317 scm_set_smob_print (scm_tc16_dynamic_obj, dynl_obj_print);
7cf1a27e 318 sysdep_dynl_init ();
a0599745 319#include "libguile/dynl.x"
1edae076 320}
89e00824
ML
321
322/*
323 Local Variables:
324 c-file-style: "gnu"
325 End:
326*/