*** empty log message ***
[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
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
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"
408ea28a 54
66d4f5cc 55#include "guile-ltdl.h"
4feb69af 56
c2cbcc57
HWN
57/*
58 From the libtool manual: "Note that libltdl is not threadsafe,
59 i.e. a multithreaded application has to use a mutex for libltdl.".
60
61 Guile does not currently support pre-emptive threads, so there is
62 no mutex. Previously SCM_DEFER_INTS and SCM_ALLOW_INTS were used:
63 they are mentioned here in case somebody is grepping for thread
64 problems ;)
732b9327
GH
65*/
66
4feb69af 67static void *
af45e3b0 68sysdep_dynl_link (const char *fname, const char *subr)
4feb69af 69{
66d4f5cc 70 scm_lt_dlhandle handle;
8e583c6e 71 handle = scm_lt_dlopenext (fname);
4feb69af
MV
72 if (NULL == handle)
73 {
01449aa5
DH
74 SCM fn;
75 SCM msg;
76
01449aa5 77 fn = scm_makfrom0str (fname);
8e583c6e 78 msg = scm_makfrom0str (scm_lt_dlerror ());
1afff620 79 scm_misc_error (subr, "file: ~S, message: ~S", scm_list_2 (fn, msg));
4feb69af
MV
80 }
81 return (void *) handle;
82}
83
84static void
85sysdep_dynl_unlink (void *handle, const char *subr)
86{
66d4f5cc 87 if (scm_lt_dlclose ((scm_lt_dlhandle) handle))
4feb69af 88 {
8e583c6e 89 scm_misc_error (subr, (char *) scm_lt_dlerror (), SCM_EOL);
4feb69af
MV
90 }
91}
92
93static void *
94sysdep_dynl_func (const char *symb, void *handle, const char *subr)
95{
96 void *fptr;
97
66d4f5cc 98 fptr = scm_lt_dlsym ((scm_lt_dlhandle) handle, symb);
4feb69af
MV
99 if (!fptr)
100 {
8e583c6e 101 scm_misc_error (subr, (char *) scm_lt_dlerror (), SCM_EOL);
4feb69af
MV
102 }
103 return fptr;
104}
105
106static void
107sysdep_dynl_init ()
108{
8e583c6e 109 scm_lt_dlinit ();
4feb69af
MV
110}
111
92c2555f 112scm_t_bits scm_tc16_dynamic_obj;
80bc7890 113
f5710d53
MV
114#define DYNL_FILENAME SCM_SMOB_OBJECT
115#define DYNL_HANDLE(x) ((void *) SCM_SMOB_DATA_2 (x))
2ff08405 116#define SET_DYNL_HANDLE(x, v) (SCM_SET_SMOB_DATA_2 ((x), (scm_t_bits) (v)))
7cf1a27e 117
7cf1a27e 118
80bc7890 119static SCM
e841c3e0 120dynl_obj_mark (SCM ptr)
80bc7890 121{
7cf1a27e 122 return DYNL_FILENAME (ptr);
c487ad44
MV
123}
124
e841c3e0 125
80bc7890 126static int
e841c3e0 127dynl_obj_print (SCM exp, SCM port, scm_print_state *pstate)
80bc7890 128{
7cf1a27e
MD
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;
80bc7890
MV
135}
136
8e3ab003 137
af45e3b0 138SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0,
1e6808ea 139 (SCM filename),
ee95d597
GH
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}.")
1bbd0b84 149#define FUNC_NAME s_scm_dynamic_link
80bc7890 150{
7cf1a27e 151 void *handle;
80bc7890 152
1e6808ea 153 SCM_VALIDATE_STRING (1, filename);
1e6808ea
MG
154 handle = sysdep_dynl_link (SCM_STRING_CHARS (filename), FUNC_NAME);
155 SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (filename), handle);
80bc7890 156}
1bbd0b84 157#undef FUNC_NAME
80bc7890 158
80bc7890 159
a1ec6916 160SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0,
1bbd0b84 161 (SCM obj),
ee95d597
GH
162 "Return @code{#t} if @var{obj} is a dynamic object handle,\n"
163 "or @code{#f} otherwise.")
1bbd0b84 164#define FUNC_NAME s_scm_dynamic_object_p
80bc7890 165{
7888309b 166 return scm_from_bool (SCM_TYP16_PREDICATE (scm_tc16_dynamic_obj, obj));
80bc7890 167}
1bbd0b84 168#undef FUNC_NAME
80bc7890 169
b82c6ce0 170
a1ec6916 171SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0,
1bbd0b84 172 (SCM dobj),
ee95d597
GH
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.")
1bbd0b84 178#define FUNC_NAME s_scm_dynamic_unlink
80bc7890 179{
7cf1a27e 180 /*fixme* GC-problem */
b82c6ce0
DH
181 SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj);
182 if (DYNL_HANDLE (dobj) == NULL) {
9e375910 183 SCM_MISC_ERROR ("Already unlinked: ~S", scm_list_1 (dobj));
b82c6ce0 184 } else {
b82c6ce0
DH
185 sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME);
186 SET_DYNL_HANDLE (dobj, NULL);
b82c6ce0
DH
187 return SCM_UNSPECIFIED;
188 }
80bc7890 189}
1bbd0b84 190#undef FUNC_NAME
80bc7890 191
b82c6ce0 192
a1ec6916 193SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0,
a6d9e5ab 194 (SCM name, SCM dobj),
ee95d597
GH
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.")
1bbd0b84 203#define FUNC_NAME s_scm_dynamic_func
80bc7890 204{
a6d9e5ab
DH
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
7cf1a27e 209 void (*func) ();
80bc7890 210
a6d9e5ab 211 SCM_VALIDATE_STRING (1, name);
7cf1a27e 212 /*fixme* GC-problem */
b82c6ce0
DH
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 {
a002f1a2
DH
217 char *chars;
218
a6d9e5ab 219 chars = SCM_STRING_CHARS (name);
b9bd8526
MV
220 func = (void (*) ()) sysdep_dynl_func (chars, DYNL_HANDLE (dobj),
221 FUNC_NAME);
222 return scm_from_ulong ((unsigned long) func);
b82c6ce0 223 }
80bc7890 224}
1bbd0b84 225#undef FUNC_NAME
80bc7890 226
b82c6ce0 227
a1ec6916 228SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0,
1bbd0b84 229 (SCM func, SCM dobj),
46732b54
GH
230 "Call a C function in a dynamic object. Two styles of\n"
231 "invocation are supported:\n\n"
232 "@itemize @bullet\n"
233 "@item @var{func} can be a function handle returned by\n"
234 "@code{dynamic-func}. In this case @var{dobj} is\n"
235 "ignored\n"
236 "@item @var{func} can be a string with the name of the\n"
237 "function to call, with @var{dobj} the handle of the\n"
238 "dynamic object in which to find the function.\n"
239 "This is equivalent to\n"
240 "@smallexample\n\n"
241 "(dynamic-call (dynamic-func @var{func} @var{dobj}) #f)\n"
242 "@end smallexample\n"
243 "@end itemize\n\n"
244 "In either case, the function is passed no arguments\n"
245 "and its return value is ignored.")
1bbd0b84 246#define FUNC_NAME s_scm_dynamic_call
80bc7890 247{
7cf1a27e
MD
248 void (*fptr) ();
249
a6d9e5ab 250 if (SCM_STRINGP (func))
7cf1a27e
MD
251 func = scm_dynamic_func (func, dobj);
252 fptr = (void (*) ()) SCM_NUM2ULONG (1, func);
7cf1a27e 253 fptr ();
7cf1a27e 254 return SCM_UNSPECIFIED;
80bc7890 255}
1bbd0b84 256#undef FUNC_NAME
80bc7890 257
2ee08a28
GH
258/* return a newly allocated array of char pointers to each of the strings
259 in args, with a terminating NULL pointer. */
260/* Note: a similar function is defined in posix.c, but we don't necessarily
261 want to export it. */
262static char **allocate_string_pointers (SCM args, int *num_args_return)
263{
264 char **result;
265 int n_args = scm_ilength (args);
266 int i;
267
268 SCM_ASSERT (n_args >= 0, args, SCM_ARGn, "allocate_string_pointers");
269 result = (char **) scm_malloc ((n_args + 1) * sizeof (char *));
270 result[n_args] = NULL;
271 for (i = 0; i < n_args; i++)
272 {
273 SCM car = SCM_CAR (args);
274
275 if (!SCM_STRINGP (car))
276 {
277 free (result);
278 scm_wrong_type_arg ("allocate_string_pointers", SCM_ARGn, car);
279 }
280 result[i] = SCM_STRING_CHARS (SCM_CAR (args));
281 args = SCM_CDR (args);
282 }
283 *num_args_return = n_args;
284 return result;
285}
286
a1ec6916 287SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0,
1bbd0b84 288 (SCM func, SCM dobj, SCM args),
1e6808ea
MG
289 "Call the C function indicated by @var{func} and @var{dobj},\n"
290 "just like @code{dynamic-call}, but pass it some arguments and\n"
291 "return its return value. The C function is expected to take\n"
292 "two arguments and return an @code{int}, just like @code{main}:\n"
b380b885
MD
293 "@smallexample\n"
294 "int c_func (int argc, char **argv);\n"
295 "@end smallexample\n\n"
1e6808ea
MG
296 "The parameter @var{args} must be a list of strings and is\n"
297 "converted into an array of @code{char *}. The array is passed\n"
298 "in @var{argv} and its size in @var{argc}. The return value is\n"
299 "converted to a Scheme number and returned from the call to\n"
300 "@code{dynamic-args-call}.")
1bbd0b84 301#define FUNC_NAME s_scm_dynamic_args_call
80bc7890 302{
7cf1a27e
MD
303 int (*fptr) (int argc, char **argv);
304 int result, argc;
305 char **argv;
306
a6d9e5ab 307 if (SCM_STRINGP (func))
7cf1a27e
MD
308 func = scm_dynamic_func (func, dobj);
309
310 fptr = (int (*) (int, char **)) SCM_NUM2ULONG (1, func);
2ee08a28
GH
311 argv = allocate_string_pointers (args, &argc);
312 /* if the procedure mutates its arguments, the original strings will be
313 changed -- in Guile 1.6 and earlier, this wasn't the case since a
314 new copy of each string was allocated. */
7cf1a27e 315 result = (*fptr) (argc, argv);
2ee08a28 316 free (argv);
7cf1a27e 317
e11e83f3 318 return scm_from_int (result);
80bc7890 319}
1bbd0b84 320#undef FUNC_NAME
80bc7890 321
1edae076
MV
322void
323scm_init_dynamic_linking ()
324{
7cf1a27e 325 scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0);
e841c3e0
KN
326 scm_set_smob_mark (scm_tc16_dynamic_obj, dynl_obj_mark);
327 scm_set_smob_print (scm_tc16_dynamic_obj, dynl_obj_print);
7cf1a27e 328 sysdep_dynl_init ();
a0599745 329#include "libguile/dynl.x"
1edae076 330}
89e00824
ML
331
332/*
333 Local Variables:
334 c-file-style: "gnu"
335 End:
336*/