dynl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /* dynl.c - dynamic linking
  2. *
  3. * Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2002 Free Software Foundation, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; see the file COPYING. If not, write to
  17. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. * Boston, MA 02111-1307 USA
  19. *
  20. * As a special exception, the Free Software Foundation gives permission
  21. * for additional uses of the text contained in its release of GUILE.
  22. *
  23. * The exception is that, if you link the GUILE library with other files
  24. * to produce an executable, this does not by itself cause the
  25. * resulting executable to be covered by the GNU General Public License.
  26. * Your use of that executable is in no way restricted on account of
  27. * linking the GUILE library code into it.
  28. *
  29. * This exception does not however invalidate any other reasons why
  30. * the executable file might be covered by the GNU General Public License.
  31. *
  32. * This exception applies only to the code released by the
  33. * Free Software Foundation under the name GUILE. If you copy
  34. * code from other Free Software Foundation releases into a copy of
  35. * GUILE, as the General Public License permits, the exception does
  36. * not apply to the code that you add in this way. To avoid misleading
  37. * anyone as to the status of such modified files, you must delete
  38. * this exception notice from them.
  39. *
  40. * If you write modifications of your own for GUILE, it is your choice
  41. * whether to permit this exception to apply to your modifications.
  42. * If you do not wish that, delete this exception notice. */
  43. /* "dynl.c" dynamically link&load object files.
  44. Author: Aubrey Jaffer
  45. Modified for libguile by Marius Vollmer */
  46. #if 0 /* Disabled until we know for sure that it isn't needed */
  47. /* XXX - This is only here to drag in a definition of __eprintf. This
  48. is needed for proper operation of dynamic linking. The real
  49. solution would probably be a shared libgcc. */
  50. #undef NDEBUG
  51. #include <assert.h>
  52. static void
  53. maybe_drag_in_eprintf ()
  54. {
  55. assert (!maybe_drag_in_eprintf);
  56. }
  57. #endif
  58. #include <stdio.h>
  59. #include "libguile/_scm.h"
  60. #include "libguile/dynl.h"
  61. #include "libguile/smob.h"
  62. #include "libguile/keywords.h"
  63. #include "libguile/ports.h"
  64. #include "libguile/strings.h"
  65. #include "libguile/validate.h"
  66. /* Converting a list of SCM strings into a argv-style array. You must
  67. have ints disabled for the whole lifetime of the created argv (from
  68. before MAKE_ARGV_FROM_STRINGLIST until after
  69. MUST_FREE_ARGV). Atleast this is was the documentation for
  70. MAKARGVFROMSTRS says, it isn't really used that way.
  71. This code probably belongs into strings.c */
  72. static char **
  73. scm_make_argv_from_stringlist (SCM args,int *argcp,const char *subr,int argn)
  74. {
  75. char **argv;
  76. int argc, i;
  77. argc = scm_ilength (args);
  78. argv = (char **) scm_must_malloc ((1L + argc) * sizeof (char *), subr);
  79. for (i = 0; SCM_NNULLP (args); args = SCM_CDR (args), i++) {
  80. size_t len;
  81. char *dst, *src;
  82. SCM str = SCM_CAR (args);
  83. SCM_ASSERT (SCM_ROSTRINGP (str), str, argn, subr);
  84. len = 1 + SCM_ROLENGTH (str);
  85. dst = (char *) scm_must_malloc ((long) len, subr);
  86. src = SCM_ROCHARS (str);
  87. while (len--)
  88. dst[len] = src[len];
  89. argv[i] = dst;
  90. }
  91. if (argcp)
  92. *argcp = argc;
  93. argv[argc] = 0;
  94. return argv;
  95. }
  96. static void
  97. scm_must_free_argv(char **argv)
  98. {
  99. char **av = argv;
  100. while (*av)
  101. free (*(av++));
  102. free (argv);
  103. }
  104. /* Coerce an arbitrary readonly-string into a zero-terminated string.
  105. */
  106. static SCM
  107. scm_coerce_rostring (SCM rostr,const char *subr,int argn)
  108. {
  109. SCM_ASSERT (SCM_ROSTRINGP (rostr), rostr, argn, subr);
  110. if (SCM_SUBSTRP (rostr))
  111. rostr = scm_makfromstr (SCM_ROCHARS (rostr), SCM_ROLENGTH (rostr), 0);
  112. return rostr;
  113. }
  114. /* Module registry
  115. */
  116. /* We can't use SCM objects here. One should be able to call
  117. SCM_REGISTER_MODULE from a C++ constructor for a static
  118. object. This happens before main and thus before libguile is
  119. initialized. */
  120. struct moddata {
  121. struct moddata *link;
  122. char *module_name;
  123. void *init_func;
  124. };
  125. static struct moddata *registered_mods = NULL;
  126. void
  127. scm_register_module_xxx (char *module_name, void *init_func)
  128. {
  129. struct moddata *md;
  130. /* XXX - should we (and can we) DEFER_INTS here? */
  131. for (md = registered_mods; md; md = md->link)
  132. if (!strcmp (md->module_name, module_name))
  133. {
  134. md->init_func = init_func;
  135. return;
  136. }
  137. md = (struct moddata *) malloc (sizeof (struct moddata));
  138. if (md == NULL)
  139. {
  140. fprintf (stderr,
  141. "guile: can't register module (%s): not enough memory",
  142. module_name);
  143. return;
  144. }
  145. md->module_name = module_name;
  146. md->init_func = init_func;
  147. md->link = registered_mods;
  148. registered_mods = md;
  149. }
  150. SCM_DEFINE (scm_registered_modules, "c-registered-modules", 0, 0, 0,
  151. (),
  152. "Return a list of the object code modules that have been imported into\n"
  153. "the current Guile process. Each element of the list is a pair whose\n"
  154. "car is the name of the module, and whose cdr is the function handle\n"
  155. "for that module's initializer function. The name is the string that\n"
  156. "has been passed to scm_register_module_xxx.")
  157. #define FUNC_NAME s_scm_registered_modules
  158. {
  159. SCM res;
  160. struct moddata *md;
  161. res = SCM_EOL;
  162. for (md = registered_mods; md; md = md->link)
  163. res = scm_cons (scm_cons (scm_makfrom0str (md->module_name),
  164. scm_ulong2num ((unsigned long) md->init_func)),
  165. res);
  166. return res;
  167. }
  168. #undef FUNC_NAME
  169. SCM_DEFINE (scm_clear_registered_modules, "c-clear-registered-modules", 0, 0, 0,
  170. (),
  171. "Destroy the list of modules registered with the current Guile process.\n"
  172. "The return value is unspecified. @strong{Warning:} this function does\n"
  173. "not actually unlink or deallocate these modules, but only destroys the\n"
  174. "records of which modules have been loaded. It should therefore be used\n"
  175. "only by module bookkeeping operations.")
  176. #define FUNC_NAME s_scm_clear_registered_modules
  177. {
  178. struct moddata *md1, *md2;
  179. SCM_DEFER_INTS;
  180. for (md1 = registered_mods; md1; md1 = md2)
  181. {
  182. md2 = md1->link;
  183. free (md1);
  184. }
  185. registered_mods = NULL;
  186. SCM_ALLOW_INTS;
  187. return SCM_UNSPECIFIED;
  188. }
  189. #undef FUNC_NAME
  190. /* Dispatch to the system dependent files
  191. *
  192. * They define some static functions. These functions are called with
  193. * deferred interrupts. When they want to throw errors, they are
  194. * expected to insert a SCM_ALLOW_INTS before doing the throw. It
  195. * might work to throw an error while interrupts are deferred (because
  196. * they will be unconditionally allowed the next time a SCM_ALLOW_INTS
  197. * is executed, SCM_DEFER_INTS and SCM_ALLOW_INTS do not nest).
  198. */
  199. #ifdef DYNAMIC_LINKING
  200. #include "libltdl/ltdl.h"
  201. static void *
  202. sysdep_dynl_link (const char *fname, const char *subr)
  203. {
  204. lt_dlhandle handle;
  205. handle = lt_dlopenext (fname);
  206. if (NULL == handle)
  207. {
  208. SCM_ALLOW_INTS;
  209. scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
  210. }
  211. return (void *) handle;
  212. }
  213. static void
  214. sysdep_dynl_unlink (void *handle, const char *subr)
  215. {
  216. if (lt_dlclose ((lt_dlhandle) handle))
  217. {
  218. SCM_ALLOW_INTS;
  219. scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
  220. }
  221. }
  222. static void *
  223. sysdep_dynl_func (const char *symb, void *handle, const char *subr)
  224. {
  225. void *fptr;
  226. fptr = lt_dlsym ((lt_dlhandle) handle, symb);
  227. if (!fptr)
  228. {
  229. SCM_ALLOW_INTS;
  230. scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL);
  231. }
  232. return fptr;
  233. }
  234. static void
  235. sysdep_dynl_init ()
  236. {
  237. lt_dlinit ();
  238. }
  239. #else
  240. /* no dynamic linking available, throw errors. */
  241. static void
  242. sysdep_dynl_init (void)
  243. {
  244. }
  245. static void
  246. no_dynl_error (const char *subr)
  247. {
  248. SCM_ALLOW_INTS;
  249. scm_misc_error (subr, "dynamic linking not available", SCM_EOL);
  250. }
  251. static void *
  252. sysdep_dynl_link (const char *filename, const char *subr)
  253. {
  254. no_dynl_error (subr);
  255. return NULL;
  256. }
  257. static void
  258. sysdep_dynl_unlink (void *handle,
  259. const char *subr)
  260. {
  261. no_dynl_error (subr);
  262. }
  263. static void *
  264. sysdep_dynl_func (const char *symbol,
  265. void *handle,
  266. const char *subr)
  267. {
  268. no_dynl_error (subr);
  269. return NULL;
  270. }
  271. #endif
  272. int scm_tc16_dynamic_obj;
  273. #define DYNL_FILENAME(x) (SCM_CELL_OBJECT_1 (x))
  274. #define DYNL_HANDLE(x) ((void *) SCM_CELL_WORD_2 (x))
  275. #define SET_DYNL_HANDLE(x, v) (SCM_SET_CELL_WORD_2 ((x), (v)))
  276. static SCM
  277. mark_dynl_obj (SCM ptr)
  278. {
  279. return DYNL_FILENAME (ptr);
  280. }
  281. static int
  282. print_dynl_obj (SCM exp,SCM port,scm_print_state *pstate)
  283. {
  284. scm_puts ("#<dynamic-object ", port);
  285. scm_iprin1 (DYNL_FILENAME (exp), port, pstate);
  286. if (DYNL_HANDLE (exp) == NULL)
  287. scm_puts (" (unlinked)", port);
  288. scm_putc ('>', port);
  289. return 1;
  290. }
  291. SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0,
  292. (SCM fname),
  293. "Open the dynamic library @var{library-file}. A library handle\n"
  294. "representing the opened library is returned; this handle should be used\n"
  295. "as the @var{lib} argument to the following functions.")
  296. #define FUNC_NAME s_scm_dynamic_link
  297. {
  298. void *handle;
  299. SCM_COERCE_ROSTRING (1, fname);
  300. handle = sysdep_dynl_link (SCM_CHARS (fname), FUNC_NAME);
  301. SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (fname), handle);
  302. }
  303. #undef FUNC_NAME
  304. SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0,
  305. (SCM obj),
  306. "Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}\n"
  307. "otherwise.")
  308. #define FUNC_NAME s_scm_dynamic_object_p
  309. {
  310. return SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_dynamic_obj, obj));
  311. }
  312. #undef FUNC_NAME
  313. SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0,
  314. (SCM dobj),
  315. "Unlink the library represented by @var{library-handle},\n"
  316. "and remove any imported symbols from the address space.\n"
  317. "GJB:FIXME:DOC: 2nd version below:\n"
  318. "Unlink the indicated object file from the application. The\n"
  319. "argument @var{dynobj} must have been obtained by a call to\n"
  320. "@code{dynamic-link}. After @code{dynamic-unlink} has been\n"
  321. "called on @var{dynobj}, its content is no longer accessible.\n")
  322. #define FUNC_NAME s_scm_dynamic_unlink
  323. {
  324. /*fixme* GC-problem */
  325. SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj);
  326. if (DYNL_HANDLE (dobj) == NULL) {
  327. SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
  328. } else {
  329. SCM_DEFER_INTS;
  330. sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME);
  331. SET_DYNL_HANDLE (dobj, NULL);
  332. SCM_ALLOW_INTS;
  333. return SCM_UNSPECIFIED;
  334. }
  335. }
  336. #undef FUNC_NAME
  337. SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0,
  338. (SCM symb, SCM dobj),
  339. "Import the symbol @var{func} from @var{lib} (a dynamic library handle).\n"
  340. "A @dfn{function handle} representing the imported function is returned.\n"
  341. "GJB:FIXME:DOC: 2nd version below\n"
  342. "Search the C function indicated by @var{function} (a string or symbol)\n"
  343. "in @var{dynobj} and return some Scheme object that can later be used\n"
  344. "with @code{dynamic-call} to actually call this function. Right now,\n"
  345. "these Scheme objects are formed by casting the address of the function\n"
  346. "to @code{long} and converting this number to its Scheme representation.\n\n"
  347. "Regardless whether your C compiler prepends an underscore @samp{_} to\n"
  348. "the global names in a program, you should @strong{not} include this\n"
  349. "underscore in @var{function}. Guile knows whether the underscore is\n"
  350. "needed or not and will add it when necessary.\n\n"
  351. "")
  352. #define FUNC_NAME s_scm_dynamic_func
  353. {
  354. void (*func) ();
  355. SCM_COERCE_ROSTRING (1, symb);
  356. /*fixme* GC-problem */
  357. SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj);
  358. if (DYNL_HANDLE (dobj) == NULL) {
  359. SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
  360. } else {
  361. SCM_DEFER_INTS;
  362. func = (void (*) ()) sysdep_dynl_func (SCM_CHARS (symb),
  363. DYNL_HANDLE (dobj),
  364. FUNC_NAME);
  365. SCM_ALLOW_INTS;
  366. return scm_ulong2num ((unsigned long) func);
  367. }
  368. }
  369. #undef FUNC_NAME
  370. SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0,
  371. (SCM func, SCM dobj),
  372. "Call @var{lib-thunk}, a procedure of no arguments. If @var{lib-thunk}\n"
  373. "is a string, it is assumed to be a symbol found in the dynamic library\n"
  374. "@var{lib} and is fetched with @code{dynamic-func}. Otherwise, it should\n"
  375. "be a function handle returned by a previous call to @code{dynamic-func}.\n"
  376. "The return value is unspecified.\n"
  377. "GJB:FIXME:DOC 2nd version below\n"
  378. "Call the C function indicated by @var{function} and @var{dynobj}. The\n"
  379. "function is passed no arguments and its return value is ignored. When\n"
  380. "@var{function} is something returned by @code{dynamic-func}, call that\n"
  381. "function and ignore @var{dynobj}. When @var{function} is a string (or\n"
  382. "symbol, etc.), look it up in @var{dynobj}; this is equivalent to\n\n"
  383. "@smallexample\n"
  384. "(dynamic-call (dynamic-func @var{function} @var{dynobj} #f))\n"
  385. "@end smallexample\n\n"
  386. "Interrupts are deferred while the C function is executing (with\n"
  387. "@code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).\n"
  388. "")
  389. #define FUNC_NAME s_scm_dynamic_call
  390. {
  391. void (*fptr) ();
  392. if (SCM_ROSTRINGP (func))
  393. func = scm_dynamic_func (func, dobj);
  394. fptr = (void (*) ()) SCM_NUM2ULONG (1, func);
  395. SCM_DEFER_INTS;
  396. fptr ();
  397. SCM_ALLOW_INTS;
  398. return SCM_UNSPECIFIED;
  399. }
  400. #undef FUNC_NAME
  401. SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0,
  402. (SCM func, SCM dobj, SCM args),
  403. "Call @var{proc}, a dynamically loaded function, passing it the argument\n"
  404. "list @var{args} (a list of strings). As with @code{dynamic-call},\n"
  405. "@var{proc} should be either a function handle or a string, in which case\n"
  406. "it is first fetched from @var{lib} with @code{dynamic-func}.\n\n"
  407. "@var{proc} is assumed to return an integer, which is used as the return\n"
  408. "value from @code{dynamic-args-call}.\n\n"
  409. "GJB:FIXME:DOC 2nd version below\n"
  410. "Call the C function indicated by @var{function} and @var{dynobj}, just\n"
  411. "like @code{dynamic-call}, but pass it some arguments and return its\n"
  412. "return value. The C function is expected to take two arguments and\n"
  413. "return an @code{int}, just like @code{main}:\n\n"
  414. "@smallexample\n"
  415. "int c_func (int argc, char **argv);\n"
  416. "@end smallexample\n\n"
  417. "The parameter @var{args} must be a list of strings and is converted into\n"
  418. "an array of @code{char *}. The array is passed in @var{argv} and its\n"
  419. "size in @var{argc}. The return value is converted to a Scheme number\n"
  420. "and returned from the call to @code{dynamic-args-call}.\n\n\n"
  421. "")
  422. #define FUNC_NAME s_scm_dynamic_args_call
  423. {
  424. int (*fptr) (int argc, char **argv);
  425. int result, argc;
  426. char **argv;
  427. if (SCM_ROSTRINGP (func))
  428. func = scm_dynamic_func (func, dobj);
  429. fptr = (int (*) (int, char **)) SCM_NUM2ULONG (1, func);
  430. SCM_DEFER_INTS;
  431. argv = scm_make_argv_from_stringlist (args, &argc, FUNC_NAME, SCM_ARG3);
  432. result = (*fptr) (argc, argv);
  433. scm_must_free_argv (argv);
  434. SCM_ALLOW_INTS;
  435. return SCM_MAKINUM (0L + result);
  436. }
  437. #undef FUNC_NAME
  438. void
  439. scm_init_dynamic_linking ()
  440. {
  441. scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0);
  442. scm_set_smob_mark (scm_tc16_dynamic_obj, mark_dynl_obj);
  443. scm_set_smob_print (scm_tc16_dynamic_obj, print_dynl_obj);
  444. sysdep_dynl_init ();
  445. #include "libguile/dynl.x"
  446. }
  447. /*
  448. Local Variables:
  449. c-file-style: "gnu"
  450. End:
  451. */