dynl.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* dynl.c - dynamic linking
  2. *
  3. * Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001, 2002,
  4. * 2003, 2008 Free Software Foundation, Inc.
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. /* "dynl.c" dynamically link&load object files.
  24. Author: Aubrey Jaffer
  25. Modified for libguile by Marius Vollmer */
  26. #if 0 /* Disabled until we know for sure that it isn't needed */
  27. /* XXX - This is only here to drag in a definition of __eprintf. This
  28. is needed for proper operation of dynamic linking. The real
  29. solution would probably be a shared libgcc. */
  30. #undef NDEBUG
  31. #include <assert.h>
  32. static void
  33. maybe_drag_in_eprintf ()
  34. {
  35. assert (!maybe_drag_in_eprintf);
  36. }
  37. #endif
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include "libguile/_scm.h"
  41. #include "libguile/dynl.h"
  42. #include "libguile/smob.h"
  43. #include "libguile/keywords.h"
  44. #include "libguile/ports.h"
  45. #include "libguile/strings.h"
  46. #include "libguile/deprecation.h"
  47. #include "libguile/lang.h"
  48. #include "libguile/validate.h"
  49. #include "libguile/dynwind.h"
  50. #include <ltdl.h>
  51. /*
  52. From the libtool manual: "Note that libltdl is not threadsafe,
  53. i.e. a multithreaded application has to use a mutex for libltdl.".
  54. Guile does not currently support pre-emptive threads, so there is no
  55. mutex. Previously SCM_CRITICAL_SECTION_START and
  56. SCM_CRITICAL_SECTION_END were used: they are mentioned here in case
  57. somebody is grepping for thread problems ;)
  58. */
  59. /* njrev: not threadsafe, protection needed as described above */
  60. static void *
  61. sysdep_dynl_link (const char *fname, const char *subr)
  62. {
  63. lt_dlhandle handle;
  64. handle = lt_dlopenext (fname);
  65. if (NULL == handle)
  66. {
  67. SCM fn;
  68. SCM msg;
  69. fn = scm_from_locale_string (fname);
  70. msg = scm_from_locale_string (lt_dlerror ());
  71. scm_misc_error (subr, "file: ~S, message: ~S", scm_list_2 (fn, msg));
  72. }
  73. return (void *) handle;
  74. }
  75. static void
  76. sysdep_dynl_unlink (void *handle, const char *subr)
  77. {
  78. if (lt_dlclose ((lt_dlhandle) handle))
  79. {
  80. scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
  81. }
  82. }
  83. static void *
  84. sysdep_dynl_func (const char *symb, void *handle, const char *subr)
  85. {
  86. void *fptr;
  87. fptr = lt_dlsym ((lt_dlhandle) handle, symb);
  88. if (!fptr)
  89. {
  90. scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
  91. }
  92. return fptr;
  93. }
  94. static void
  95. sysdep_dynl_init ()
  96. {
  97. lt_dlinit ();
  98. }
  99. scm_t_bits scm_tc16_dynamic_obj;
  100. #define DYNL_FILENAME SCM_SMOB_OBJECT
  101. #define DYNL_HANDLE(x) ((void *) SCM_SMOB_DATA_2 (x))
  102. #define SET_DYNL_HANDLE(x, v) (SCM_SET_SMOB_DATA_2 ((x), (scm_t_bits) (v)))
  103. static SCM
  104. dynl_obj_mark (SCM ptr)
  105. {
  106. return DYNL_FILENAME (ptr);
  107. }
  108. static int
  109. dynl_obj_print (SCM exp, SCM port, scm_print_state *pstate)
  110. {
  111. scm_puts ("#<dynamic-object ", port);
  112. scm_iprin1 (DYNL_FILENAME (exp), port, pstate);
  113. if (DYNL_HANDLE (exp) == NULL)
  114. scm_puts (" (unlinked)", port);
  115. scm_putc ('>', port);
  116. return 1;
  117. }
  118. SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0,
  119. (SCM filename),
  120. "Find the shared object (shared library) denoted by\n"
  121. "@var{filename} and link it into the running Guile\n"
  122. "application. The returned\n"
  123. "scheme object is a ``handle'' for the library which can\n"
  124. "be passed to @code{dynamic-func}, @code{dynamic-call} etc.\n\n"
  125. "Searching for object files is system dependent. Normally,\n"
  126. "if @var{filename} does have an explicit directory it will\n"
  127. "be searched for in locations\n"
  128. "such as @file{/usr/lib} and @file{/usr/local/lib}.")
  129. #define FUNC_NAME s_scm_dynamic_link
  130. {
  131. void *handle;
  132. char *file;
  133. scm_dynwind_begin (0);
  134. file = scm_to_locale_string (filename);
  135. scm_dynwind_free (file);
  136. handle = sysdep_dynl_link (file, FUNC_NAME);
  137. scm_dynwind_end ();
  138. SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (filename), handle);
  139. }
  140. #undef FUNC_NAME
  141. SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0,
  142. (SCM obj),
  143. "Return @code{#t} if @var{obj} is a dynamic object handle,\n"
  144. "or @code{#f} otherwise.")
  145. #define FUNC_NAME s_scm_dynamic_object_p
  146. {
  147. return scm_from_bool (SCM_TYP16_PREDICATE (scm_tc16_dynamic_obj, obj));
  148. }
  149. #undef FUNC_NAME
  150. SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0,
  151. (SCM dobj),
  152. "Unlink a dynamic object from the application, if possible. The\n"
  153. "object must have been linked by @code{dynamic-link}, with \n"
  154. "@var{dobj} the corresponding handle. After this procedure\n"
  155. "is called, the handle can no longer be used to access the\n"
  156. "object.")
  157. #define FUNC_NAME s_scm_dynamic_unlink
  158. {
  159. /*fixme* GC-problem */
  160. SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj);
  161. if (DYNL_HANDLE (dobj) == NULL) {
  162. SCM_MISC_ERROR ("Already unlinked: ~S", scm_list_1 (dobj));
  163. } else {
  164. sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME);
  165. SET_DYNL_HANDLE (dobj, NULL);
  166. return SCM_UNSPECIFIED;
  167. }
  168. }
  169. #undef FUNC_NAME
  170. SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0,
  171. (SCM name, SCM dobj),
  172. "Return a ``handle'' for the function @var{name} in the\n"
  173. "shared object referred to by @var{dobj}. The handle\n"
  174. "can be passed to @code{dynamic-call} to actually\n"
  175. "call the function.\n\n"
  176. "Regardless whether your C compiler prepends an underscore\n"
  177. "@samp{_} to the global names in a program, you should\n"
  178. "@strong{not} include this underscore in @var{name}\n"
  179. "since it will be added automatically when necessary.")
  180. #define FUNC_NAME s_scm_dynamic_func
  181. {
  182. /* The returned handle is formed by casting the address of the function to a
  183. * long value and converting this to a scheme number
  184. */
  185. void (*func) ();
  186. SCM_VALIDATE_STRING (1, name);
  187. /*fixme* GC-problem */
  188. SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj);
  189. if (DYNL_HANDLE (dobj) == NULL) {
  190. SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
  191. } else {
  192. char *chars;
  193. scm_dynwind_begin (0);
  194. chars = scm_to_locale_string (name);
  195. scm_dynwind_free (chars);
  196. func = (void (*) ()) sysdep_dynl_func (chars, DYNL_HANDLE (dobj),
  197. FUNC_NAME);
  198. scm_dynwind_end ();
  199. return scm_from_ulong ((unsigned long) func);
  200. }
  201. }
  202. #undef FUNC_NAME
  203. SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0,
  204. (SCM func, SCM dobj),
  205. "Call a C function in a dynamic object. Two styles of\n"
  206. "invocation are supported:\n\n"
  207. "@itemize @bullet\n"
  208. "@item @var{func} can be a function handle returned by\n"
  209. "@code{dynamic-func}. In this case @var{dobj} is\n"
  210. "ignored\n"
  211. "@item @var{func} can be a string with the name of the\n"
  212. "function to call, with @var{dobj} the handle of the\n"
  213. "dynamic object in which to find the function.\n"
  214. "This is equivalent to\n"
  215. "@smallexample\n\n"
  216. "(dynamic-call (dynamic-func @var{func} @var{dobj}) #f)\n"
  217. "@end smallexample\n"
  218. "@end itemize\n\n"
  219. "In either case, the function is passed no arguments\n"
  220. "and its return value is ignored.")
  221. #define FUNC_NAME s_scm_dynamic_call
  222. {
  223. void (*fptr) ();
  224. if (scm_is_string (func))
  225. func = scm_dynamic_func (func, dobj);
  226. fptr = (void (*) ()) scm_to_ulong (func);
  227. fptr ();
  228. return SCM_UNSPECIFIED;
  229. }
  230. #undef FUNC_NAME
  231. static void
  232. free_string_pointers (void *data)
  233. {
  234. scm_i_free_string_pointers ((char **)data);
  235. }
  236. SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0,
  237. (SCM func, SCM dobj, SCM args),
  238. "Call the C function indicated by @var{func} and @var{dobj},\n"
  239. "just like @code{dynamic-call}, but pass it some arguments and\n"
  240. "return its return value. The C function is expected to take\n"
  241. "two arguments and return an @code{int}, just like @code{main}:\n"
  242. "@smallexample\n"
  243. "int c_func (int argc, char **argv);\n"
  244. "@end smallexample\n\n"
  245. "The parameter @var{args} must be a list of strings and is\n"
  246. "converted into an array of @code{char *}. The array is passed\n"
  247. "in @var{argv} and its size in @var{argc}. The return value is\n"
  248. "converted to a Scheme number and returned from the call to\n"
  249. "@code{dynamic-args-call}.")
  250. #define FUNC_NAME s_scm_dynamic_args_call
  251. {
  252. int (*fptr) (int argc, char **argv);
  253. int result, argc;
  254. char **argv;
  255. scm_dynwind_begin (0);
  256. if (scm_is_string (func))
  257. func = scm_dynamic_func (func, dobj);
  258. fptr = (int (*) (int, char **)) scm_to_ulong (func);
  259. argv = scm_i_allocate_string_pointers (args);
  260. scm_dynwind_unwind_handler (free_string_pointers, argv,
  261. SCM_F_WIND_EXPLICITLY);
  262. for (argc = 0; argv[argc]; argc++)
  263. ;
  264. result = (*fptr) (argc, argv);
  265. scm_dynwind_end ();
  266. return scm_from_int (result);
  267. }
  268. #undef FUNC_NAME
  269. void
  270. scm_init_dynamic_linking ()
  271. {
  272. scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0);
  273. scm_set_smob_mark (scm_tc16_dynamic_obj, dynl_obj_mark);
  274. scm_set_smob_print (scm_tc16_dynamic_obj, dynl_obj_print);
  275. sysdep_dynl_init ();
  276. #include "libguile/dynl.x"
  277. }
  278. /*
  279. Local Variables:
  280. c-file-style: "gnu"
  281. End:
  282. */