1 /* dynl.c - dynamic linking
3 * Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001, 2002,
4 * 2003 Free Software Foundation, Inc.
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.
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.
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
23 /* "dynl.c" dynamically link&load object files.
25 Modified for libguile by Marius Vollmer */
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. */
36 maybe_drag_in_eprintf ()
38 assert (!maybe_drag_in_eprintf
);
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"
59 From the libtool manual: "Note that libltdl is not threadsafe,
60 i.e. a multithreaded application has to use a mutex for libltdl.".
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 ;)
67 /* njrev: not threadsafe, protection needed as described above */
70 sysdep_dynl_link (const char *fname
, const char *subr
)
73 handle
= lt_dlopenext (fname
);
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
));
83 return (void *) handle
;
87 sysdep_dynl_unlink (void *handle
, const char *subr
)
89 if (lt_dlclose ((lt_dlhandle
) handle
))
91 scm_misc_error (subr
, (char *) lt_dlerror (), SCM_EOL
);
96 sysdep_dynl_func (const char *symb
, void *handle
, const char *subr
)
100 fptr
= lt_dlsym ((lt_dlhandle
) handle
, symb
);
103 scm_misc_error (subr
, (char *) lt_dlerror (), SCM_EOL
);
114 scm_t_bits scm_tc16_dynamic_obj
;
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)))
122 dynl_obj_mark (SCM ptr
)
124 return DYNL_FILENAME (ptr
);
129 dynl_obj_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
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
);
140 SCM_DEFINE (scm_dynamic_link
, "dynamic-link", 1, 0, 0,
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}.")
151 #define FUNC_NAME s_scm_dynamic_link
156 scm_dynwind_begin (0);
157 file
= scm_to_locale_string (filename
);
158 scm_dynwind_free (file
);
159 handle
= sysdep_dynl_link (file
, FUNC_NAME
);
161 SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj
, SCM_UNPACK (filename
), handle
);
166 SCM_DEFINE (scm_dynamic_object_p
, "dynamic-object?", 1, 0, 0,
168 "Return @code{#t} if @var{obj} is a dynamic object handle,\n"
169 "or @code{#f} otherwise.")
170 #define FUNC_NAME s_scm_dynamic_object_p
172 return scm_from_bool (SCM_TYP16_PREDICATE (scm_tc16_dynamic_obj
, obj
));
177 SCM_DEFINE (scm_dynamic_unlink
, "dynamic-unlink", 1, 0, 0,
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"
184 #define FUNC_NAME s_scm_dynamic_unlink
186 /*fixme* GC-problem */
187 SCM_VALIDATE_SMOB (SCM_ARG1
, dobj
, dynamic_obj
);
188 if (DYNL_HANDLE (dobj
) == NULL
) {
189 SCM_MISC_ERROR ("Already unlinked: ~S", scm_list_1 (dobj
));
191 sysdep_dynl_unlink (DYNL_HANDLE (dobj
), FUNC_NAME
);
192 SET_DYNL_HANDLE (dobj
, NULL
);
193 return SCM_UNSPECIFIED
;
199 SCM_DEFINE (scm_dynamic_func
, "dynamic-func", 2, 0, 0,
200 (SCM name
, SCM dobj
),
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.")
209 #define FUNC_NAME s_scm_dynamic_func
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
217 SCM_VALIDATE_STRING (1, name
);
218 /*fixme* GC-problem */
219 SCM_VALIDATE_SMOB (SCM_ARG2
, dobj
, dynamic_obj
);
220 if (DYNL_HANDLE (dobj
) == NULL
) {
221 SCM_MISC_ERROR ("Already unlinked: ~S", dobj
);
225 scm_dynwind_begin (0);
226 chars
= scm_to_locale_string (name
);
227 scm_dynwind_free (chars
);
228 func
= (void (*) ()) sysdep_dynl_func (chars
, DYNL_HANDLE (dobj
),
231 return scm_from_ulong ((unsigned long) func
);
237 SCM_DEFINE (scm_dynamic_call
, "dynamic-call", 2, 0, 0,
238 (SCM func
, SCM dobj
),
239 "Call a C function in a dynamic object. Two styles of\n"
240 "invocation are supported:\n\n"
242 "@item @var{func} can be a function handle returned by\n"
243 "@code{dynamic-func}. In this case @var{dobj} is\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"
250 "(dynamic-call (dynamic-func @var{func} @var{dobj}) #f)\n"
251 "@end smallexample\n"
253 "In either case, the function is passed no arguments\n"
254 "and its return value is ignored.")
255 #define FUNC_NAME s_scm_dynamic_call
259 if (scm_is_string (func
))
260 func
= scm_dynamic_func (func
, dobj
);
261 fptr
= (void (*) ()) scm_to_ulong (func
);
263 return SCM_UNSPECIFIED
;
268 free_string_pointers (void *data
)
270 scm_i_free_string_pointers ((char **)data
);
273 SCM_DEFINE (scm_dynamic_args_call
, "dynamic-args-call", 3, 0, 0,
274 (SCM func
, SCM dobj
, SCM args
),
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"
280 "int c_func (int argc, char **argv);\n"
281 "@end smallexample\n\n"
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}.")
287 #define FUNC_NAME s_scm_dynamic_args_call
289 int (*fptr
) (int argc
, char **argv
);
293 scm_dynwind_begin (0);
295 if (scm_is_string (func
))
296 func
= scm_dynamic_func (func
, dobj
);
298 fptr
= (int (*) (int, char **)) scm_to_ulong (func
);
300 argv
= scm_i_allocate_string_pointers (args
);
301 scm_dynwind_unwind_handler (free_string_pointers
, argv
,
302 SCM_F_WIND_EXPLICITLY
);
303 for (argc
= 0; argv
[argc
]; argc
++)
305 result
= (*fptr
) (argc
, argv
);
308 return scm_from_int (result
);
313 scm_init_dynamic_linking ()
315 scm_tc16_dynamic_obj
= scm_make_smob_type ("dynamic-object", 0);
316 scm_set_smob_mark (scm_tc16_dynamic_obj
, dynl_obj_mark
);
317 scm_set_smob_print (scm_tc16_dynamic_obj
, dynl_obj_print
);
319 #include "libguile/dynl.x"