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