dynl.c 9.6 KB

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