vlist: Remove Texinfo markup from docstrings.
[bpt/guile.git] / libguile / dynl.c
CommitLineData
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,
e66ff09a 4 * 2003, 2008, 2009, 2010, 2011 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
e66ff09a
LC
28#include <alloca.h>
29
1edae076
MV
30/* "dynl.c" dynamically link&load object files.
31 Author: Aubrey Jaffer
32 Modified for libguile by Marius Vollmer */
33
104d4533 34#if 0 /* Disabled until we know for sure that it isn't needed */
80bc7890
MV
35/* XXX - This is only here to drag in a definition of __eprintf. This
36 is needed for proper operation of dynamic linking. The real
37 solution would probably be a shared libgcc. */
38
39#undef NDEBUG
40#include <assert.h>
41
42static void
43maybe_drag_in_eprintf ()
44{
45 assert (!maybe_drag_in_eprintf);
46}
104d4533 47#endif
80bc7890 48
e66ff09a 49#include <stdlib.h>
96599e6a 50#include <stdio.h>
13070bd3
DH
51#include <string.h>
52
a0599745 53#include "libguile/_scm.h"
eb350124 54#include "libguile/libpath.h"
a0599745
MD
55#include "libguile/dynl.h"
56#include "libguile/smob.h"
57#include "libguile/keywords.h"
58#include "libguile/ports.h"
59#include "libguile/strings.h"
a0e0793f 60#include "libguile/deprecation.h"
a0599745 61#include "libguile/validate.h"
7f9994d9 62#include "libguile/dynwind.h"
e773b1e6 63#include "libguile/foreign.h"
408ea28a 64
a8255dca 65#include <ltdl.h>
4feb69af 66
c2cbcc57
HWN
67/*
68 From the libtool manual: "Note that libltdl is not threadsafe,
69 i.e. a multithreaded application has to use a mutex for libltdl.".
70
9de87eea
MV
71 Guile does not currently support pre-emptive threads, so there is no
72 mutex. Previously SCM_CRITICAL_SECTION_START and
73 SCM_CRITICAL_SECTION_END were used: they are mentioned here in case
74 somebody is grepping for thread problems ;)
732b9327 75*/
e45947bf 76/* njrev: not threadsafe, protection needed as described above */
732b9327 77
4feb69af 78static void *
af45e3b0 79sysdep_dynl_link (const char *fname, const char *subr)
4feb69af 80{
a8255dca 81 lt_dlhandle handle;
d12f974b
LC
82
83 if (fname != NULL)
84 handle = lt_dlopenext (fname);
85 else
86 /* Return a handle for the program as a whole. */
87 handle = lt_dlopen (NULL);
88
4feb69af
MV
89 if (NULL == handle)
90 {
01449aa5
DH
91 SCM fn;
92 SCM msg;
93
d12f974b 94 fn = fname != NULL ? scm_from_locale_string (fname) : SCM_BOOL_F;
a8255dca 95 msg = scm_from_locale_string (lt_dlerror ());
1afff620 96 scm_misc_error (subr, "file: ~S, message: ~S", scm_list_2 (fn, msg));
4feb69af 97 }
d12f974b 98
4feb69af
MV
99 return (void *) handle;
100}
101
102static void
103sysdep_dynl_unlink (void *handle, const char *subr)
104{
a8255dca 105 if (lt_dlclose ((lt_dlhandle) handle))
4feb69af 106 {
a8255dca 107 scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
4feb69af
MV
108 }
109}
110
111static void *
52fd9639 112sysdep_dynl_value (const char *symb, void *handle, const char *subr)
4feb69af
MV
113{
114 void *fptr;
115
a8255dca 116 fptr = lt_dlsym ((lt_dlhandle) handle, symb);
4feb69af 117 if (!fptr)
0f1fd214
MG
118 scm_misc_error (subr, "Symbol not found: ~a",
119 scm_list_1 (scm_from_locale_string (symb)));
4feb69af
MV
120 return fptr;
121}
122
e66ff09a
LC
123/* Augment environment variable VARIABLE with VALUE, assuming VARIABLE
124 is a path kind of variable. */
125static void
126augment_env (const char *variable, const char *value)
127{
128 const char *env;
129
130 env = getenv (variable);
131 if (env != NULL)
132 {
133 char *new_value;
134 static const char path_sep[] = { LT_PATHSEP_CHAR, 0 };
135
136 new_value = alloca (strlen (env) + strlen (value) + 2);
137 strcpy (new_value, env);
138 strcat (new_value, path_sep);
139 strcat (new_value, value);
140
141 setenv (variable, new_value, 1);
142 }
143 else
144 setenv (variable, value, 1);
145}
146
4feb69af
MV
147static void
148sysdep_dynl_init ()
149{
eb350124
AW
150 char *env;
151
a8255dca 152 lt_dlinit ();
eb350124 153
28af5ee5 154 env = getenv ("GUILE_SYSTEM_EXTENSIONS_PATH");
eb350124
AW
155 if (env && strcmp (env, "") == 0)
156 /* special-case interpret system-ltdl-path=="" as meaning no system path,
157 which is the case during the build */
e66ff09a 158 ;
eb350124 159 else if (env)
28af5ee5
AW
160 /* FIXME: should this be a colon-separated path? Or is the only point to
161 allow the build system to turn off the installed extensions path? */
eb350124 162 lt_dladdsearchdir (env);
eb350124 163 else
28af5ee5 164 {
e66ff09a
LC
165 /* Add SCM_LIB_DIR and SCM_EXTENSIONS_DIR to the loader's search
166 path. `lt_dladdsearchdir' and $LTDL_LIBRARY_PATH can't be used
167 for that because they are searched before the system-dependent
168 search path, which is the one `libtool --mode=execute -dlopen'
169 fiddles with (info "(libtool) Libltdl Interface"). See
170 <http://lists.gnu.org/archive/html/guile-devel/2010-11/msg00095.html>
171 for details. */
172 augment_env (SHARED_LIBRARY_PATH_VARIABLE, SCM_LIB_DIR);
173 augment_env (SHARED_LIBRARY_PATH_VARIABLE, SCM_EXTENSIONS_DIR);
28af5ee5 174 }
4feb69af
MV
175}
176
92c2555f 177scm_t_bits scm_tc16_dynamic_obj;
80bc7890 178
f5710d53
MV
179#define DYNL_FILENAME SCM_SMOB_OBJECT
180#define DYNL_HANDLE(x) ((void *) SCM_SMOB_DATA_2 (x))
2ff08405 181#define SET_DYNL_HANDLE(x, v) (SCM_SET_SMOB_DATA_2 ((x), (scm_t_bits) (v)))
7cf1a27e 182
7cf1a27e 183
e841c3e0 184
80bc7890 185static int
e841c3e0 186dynl_obj_print (SCM exp, SCM port, scm_print_state *pstate)
80bc7890 187{
7cf1a27e
MD
188 scm_puts ("#<dynamic-object ", port);
189 scm_iprin1 (DYNL_FILENAME (exp), port, pstate);
190 if (DYNL_HANDLE (exp) == NULL)
191 scm_puts (" (unlinked)", port);
192 scm_putc ('>', port);
193 return 1;
80bc7890
MV
194}
195
8e3ab003 196
d12f974b 197SCM_DEFINE (scm_dynamic_link, "dynamic-link", 0, 1, 0,
1e6808ea 198 (SCM filename),
ee95d597
GH
199 "Find the shared object (shared library) denoted by\n"
200 "@var{filename} and link it into the running Guile\n"
201 "application. The returned\n"
202 "scheme object is a ``handle'' for the library which can\n"
203 "be passed to @code{dynamic-func}, @code{dynamic-call} etc.\n\n"
204 "Searching for object files is system dependent. Normally,\n"
205 "if @var{filename} does have an explicit directory it will\n"
206 "be searched for in locations\n"
d12f974b
LC
207 "such as @file{/usr/lib} and @file{/usr/local/lib}.\n\n"
208 "When @var{filename} is omitted, a @dfn{global symbol handle} is\n"
209 "returned. This handle provides access to the symbols\n"
210 "available to the program at run-time, including those exported\n"
211 "by the program itself and the shared libraries already loaded.\n")
1bbd0b84 212#define FUNC_NAME s_scm_dynamic_link
80bc7890 213{
7cf1a27e 214 void *handle;
7f9994d9 215 char *file;
80bc7890 216
661ae7ab 217 scm_dynwind_begin (0);
d12f974b
LC
218
219 if (SCM_UNBNDP (filename))
220 file = NULL;
221 else
222 {
223 file = scm_to_locale_string (filename);
224 scm_dynwind_free (file);
225 }
226
7f9994d9 227 handle = sysdep_dynl_link (file, FUNC_NAME);
661ae7ab 228 scm_dynwind_end ();
d12f974b
LC
229
230 SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj,
231 SCM_UNBNDP (filename)
232 ? SCM_UNPACK (SCM_BOOL_F) : SCM_UNPACK (filename),
233 handle);
80bc7890 234}
1bbd0b84 235#undef FUNC_NAME
80bc7890 236
80bc7890 237
a1ec6916 238SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0,
1bbd0b84 239 (SCM obj),
ee95d597
GH
240 "Return @code{#t} if @var{obj} is a dynamic object handle,\n"
241 "or @code{#f} otherwise.")
1bbd0b84 242#define FUNC_NAME s_scm_dynamic_object_p
80bc7890 243{
7888309b 244 return scm_from_bool (SCM_TYP16_PREDICATE (scm_tc16_dynamic_obj, obj));
80bc7890 245}
1bbd0b84 246#undef FUNC_NAME
80bc7890 247
b82c6ce0 248
a1ec6916 249SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0,
1bbd0b84 250 (SCM dobj),
ee95d597
GH
251 "Unlink a dynamic object from the application, if possible. The\n"
252 "object must have been linked by @code{dynamic-link}, with \n"
253 "@var{dobj} the corresponding handle. After this procedure\n"
254 "is called, the handle can no longer be used to access the\n"
255 "object.")
1bbd0b84 256#define FUNC_NAME s_scm_dynamic_unlink
80bc7890 257{
7cf1a27e 258 /*fixme* GC-problem */
b82c6ce0
DH
259 SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj);
260 if (DYNL_HANDLE (dobj) == NULL) {
9e375910 261 SCM_MISC_ERROR ("Already unlinked: ~S", scm_list_1 (dobj));
b82c6ce0 262 } else {
b82c6ce0
DH
263 sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME);
264 SET_DYNL_HANDLE (dobj, NULL);
b82c6ce0
DH
265 return SCM_UNSPECIFIED;
266 }
80bc7890 267}
1bbd0b84 268#undef FUNC_NAME
80bc7890 269
b82c6ce0 270
d4149a51
LC
271SCM_DEFINE (scm_dynamic_pointer, "dynamic-pointer", 2, 0, 0,
272 (SCM name, SCM dobj),
183a2a22
LC
273 "Return a ``wrapped pointer'' to the symbol @var{name}\n"
274 "in the shared object referred to by @var{dobj}. The returned\n"
275 "pointer points to a C object.\n\n"
ee95d597
GH
276 "Regardless whether your C compiler prepends an underscore\n"
277 "@samp{_} to the global names in a program, you should\n"
278 "@strong{not} include this underscore in @var{name}\n"
279 "since it will be added automatically when necessary.")
52fd9639 280#define FUNC_NAME s_scm_dynamic_pointer
80bc7890 281{
52fd9639 282 void *val;
80bc7890 283
a6d9e5ab 284 SCM_VALIDATE_STRING (1, name);
d4149a51 285 SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj);
3023e7b0
LC
286
287 if (DYNL_HANDLE (dobj) == NULL)
b82c6ce0 288 SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
3023e7b0
LC
289 else
290 {
291 char *chars;
292
293 scm_dynwind_begin (0);
294 chars = scm_to_locale_string (name);
295 scm_dynwind_free (chars);
296 val = sysdep_dynl_value (chars, DYNL_HANDLE (dobj), FUNC_NAME);
297 scm_dynwind_end ();
298
5b46a8c2 299 return scm_from_pointer (val, NULL);
3023e7b0 300 }
80bc7890 301}
1bbd0b84 302#undef FUNC_NAME
80bc7890 303
b82c6ce0 304
52fd9639
AW
305SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0,
306 (SCM name, SCM dobj),
307 "Return a ``handle'' for the function @var{name} in the\n"
308 "shared object referred to by @var{dobj}. The handle\n"
309 "can be passed to @code{dynamic-call} to actually\n"
310 "call the function.\n\n"
311 "Regardless whether your C compiler prepends an underscore\n"
312 "@samp{_} to the global names in a program, you should\n"
313 "@strong{not} include this underscore in @var{name}\n"
314 "since it will be added automatically when necessary.")
315#define FUNC_NAME s_scm_dynamic_func
316{
d4149a51 317 return scm_dynamic_pointer (name, dobj);
52fd9639
AW
318}
319#undef FUNC_NAME
320
321
a1ec6916 322SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0,
1bbd0b84 323 (SCM func, SCM dobj),
46732b54
GH
324 "Call a C function in a dynamic object. Two styles of\n"
325 "invocation are supported:\n\n"
326 "@itemize @bullet\n"
327 "@item @var{func} can be a function handle returned by\n"
328 "@code{dynamic-func}. In this case @var{dobj} is\n"
329 "ignored\n"
330 "@item @var{func} can be a string with the name of the\n"
331 "function to call, with @var{dobj} the handle of the\n"
332 "dynamic object in which to find the function.\n"
333 "This is equivalent to\n"
334 "@smallexample\n\n"
335 "(dynamic-call (dynamic-func @var{func} @var{dobj}) #f)\n"
336 "@end smallexample\n"
337 "@end itemize\n\n"
338 "In either case, the function is passed no arguments\n"
339 "and its return value is ignored.")
1bbd0b84 340#define FUNC_NAME s_scm_dynamic_call
80bc7890 341{
5b46a8c2
LC
342 void (*fptr) (void);
343
7f9994d9 344 if (scm_is_string (func))
7cf1a27e 345 func = scm_dynamic_func (func, dobj);
5b46a8c2 346 SCM_VALIDATE_POINTER (SCM_ARG1, func);
e773b1e6 347
5b46a8c2 348 fptr = SCM_POINTER_VALUE (func);
7cf1a27e 349 fptr ();
7cf1a27e 350 return SCM_UNSPECIFIED;
80bc7890 351}
1bbd0b84 352#undef FUNC_NAME
80bc7890 353
1edae076
MV
354void
355scm_init_dynamic_linking ()
356{
7cf1a27e 357 scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0);
e841c3e0 358 scm_set_smob_print (scm_tc16_dynamic_obj, dynl_obj_print);
7cf1a27e 359 sysdep_dynl_init ();
a0599745 360#include "libguile/dynl.x"
1edae076 361}
89e00824
ML
362
363/*
364 Local Variables:
365 c-file-style: "gnu"
366 End:
367*/