dynl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* dynl.c - dynamic linking
  2. Copyright 1990-2003,2008-2011,2017-2018,2021
  3. Free Software Foundation, Inc.
  4. This file is part of Guile.
  5. Guile is free software: you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Guile is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with Guile. If not, see
  15. <https://www.gnu.org/licenses/>. */
  16. /* "dynl.c" dynamically link&load object files.
  17. Author: Aubrey Jaffer
  18. Modified for libguile by Marius Vollmer */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #ifdef __MINGW32__
  23. #include "posix-w32.h"
  24. #define dlopen(_nam,_flg) (dlopen_w32((_nam),(_flg)))
  25. #define dlsym(_hndl,_nam) (dlsym_w32((_hndl),(_nam)))
  26. #define dlclose(_hndl) (dlclose_w32((_hndl)))
  27. #define dlerror() (dlerror_w32())
  28. #else
  29. #include <dlfcn.h>
  30. #endif
  31. #include "boolean.h"
  32. #include "deprecation.h"
  33. #include "eval.h"
  34. #include "extensions.h"
  35. #include "foreign.h"
  36. #include "gsubr.h"
  37. #include "list.h"
  38. #include "modules.h"
  39. #include "numbers.h"
  40. #include "strings.h"
  41. #include "threads.h"
  42. #include "variable.h"
  43. #include "version.h"
  44. #include "dynl.h"
  45. static SCM
  46. dlerror_string (const char *fallback)
  47. {
  48. const char *message = dlerror ();
  49. if (message)
  50. return scm_from_locale_string (message);
  51. return scm_from_utf8_string ("Unknown error");
  52. }
  53. SCM_DEFINE_STATIC (scm_dlopen, "dlopen", 2, 0, 0, (SCM name, SCM flags), "")
  54. #define FUNC_NAME s_scm_dlopen
  55. {
  56. void *handle;
  57. int c_flags = scm_to_int (flags);
  58. if (scm_is_false (name))
  59. handle = dlopen (NULL, c_flags);
  60. else
  61. {
  62. char *c_name = scm_to_locale_string (name);
  63. handle = dlopen (c_name, c_flags);
  64. free (c_name);
  65. }
  66. if (!handle) {
  67. SCM message = dlerror_string ("Unknown error while opening module");
  68. SCM_MISC_ERROR ("file ~S, message ~S", scm_list_2 (name, message));
  69. }
  70. return scm_from_pointer (handle, NULL);
  71. }
  72. #undef FUNC_NAME
  73. SCM_DEFINE_STATIC (scm_dlclose, "dlclose", 1, 0, 0, (SCM obj), "")
  74. #define FUNC_NAME s_scm_dlclose
  75. {
  76. void *handle = scm_to_pointer (obj);
  77. if (dlclose (handle) != 0) {
  78. SCM message = dlerror_string ("Unknown error");
  79. SCM_MISC_ERROR ("Error closing module: ~S", scm_list_1 (message));
  80. }
  81. return SCM_UNSPECIFIED;
  82. }
  83. #undef FUNC_NAME
  84. SCM_DEFINE_STATIC (scm_dlsym, "dlsym", 2, 0, 0, (SCM obj, SCM name), "")
  85. #define FUNC_NAME s_scm_dlsym
  86. {
  87. void *handle = scm_to_pointer (obj);
  88. char *c_name = scm_to_utf8_string (name);
  89. void *sym = dlsym (handle, c_name);
  90. free (c_name);
  91. if (!sym) {
  92. SCM message = dlerror_string ("Unknown error");
  93. SCM_MISC_ERROR ("Error resolving ~S: ~S", scm_list_2 (name, message));
  94. }
  95. return scm_from_pointer (sym, NULL);
  96. }
  97. #undef FUNC_NAME
  98. #define DEFINE_LAZY_VAR(c_name, mod_name, sym_name) \
  99. static SCM c_name##_var; \
  100. static void init_##c_name##_var (void) \
  101. { \
  102. c_name##_var = scm_c_public_lookup (mod_name, sym_name); \
  103. } \
  104. static SCM c_name (void) \
  105. { \
  106. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT; \
  107. scm_i_pthread_once (&once, init_##c_name##_var); \
  108. return scm_variable_ref (c_name##_var); \
  109. }
  110. DEFINE_LAZY_VAR (load_foreign_library,
  111. "system foreign-library", "load-foreign-library");
  112. DEFINE_LAZY_VAR (foreign_library_p,
  113. "system foreign-library", "foreign-library?");
  114. DEFINE_LAZY_VAR (foreign_library_pointer,
  115. "system foreign-library", "foreign-library-pointer");
  116. SCM
  117. scm_dynamic_link (SCM filename)
  118. {
  119. return scm_call_1 (load_foreign_library (), filename);
  120. }
  121. SCM
  122. scm_dynamic_object_p (SCM obj)
  123. {
  124. return scm_call_1 (foreign_library_p (), obj);
  125. }
  126. SCM
  127. scm_dynamic_pointer (SCM name, SCM obj)
  128. {
  129. return scm_call_2 (foreign_library_pointer (), obj, name);
  130. }
  131. SCM
  132. scm_dynamic_func (SCM name, SCM obj)
  133. {
  134. return scm_dynamic_pointer (name, obj);
  135. }
  136. SCM
  137. scm_dynamic_call (SCM name, SCM obj)
  138. {
  139. SCM pointer = scm_dynamic_pointer (name, obj);
  140. void (*f)(void) = SCM_POINTER_VALUE (pointer);
  141. f();
  142. return SCM_UNSPECIFIED;
  143. }
  144. static void
  145. scm_init_system_foreign_library (void *unused)
  146. {
  147. scm_c_define ("RTLD_LAZY", scm_from_int (RTLD_LAZY));
  148. scm_c_define ("RTLD_NOW", scm_from_int (RTLD_NOW));
  149. scm_c_define ("RTLD_GLOBAL", scm_from_int (RTLD_GLOBAL));
  150. scm_c_define ("RTLD_LOCAL", scm_from_int (RTLD_LOCAL));
  151. #include "dynl.x"
  152. }
  153. void
  154. scm_init_dynamic_linking ()
  155. {
  156. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  157. "scm_init_system_foreign_library",
  158. scm_init_system_foreign_library,
  159. NULL);
  160. // FIXME: Deprecate all of these, once (system foreign-library) has
  161. // had enough time in the world.
  162. scm_c_define_gsubr
  163. ("dynamic-link", 0, 1, 0, (scm_t_subr) scm_dynamic_link);
  164. scm_c_define_gsubr
  165. ("dynamic-object?", 1, 0, 0, (scm_t_subr) scm_dynamic_object_p);
  166. scm_c_define_gsubr
  167. ("dynamic-func", 2, 0, 0, (scm_t_subr) scm_dynamic_func);
  168. scm_c_define_gsubr
  169. ("dynamic-pointer", 2, 0, 0, (scm_t_subr) scm_dynamic_pointer);
  170. scm_c_define_gsubr
  171. ("dynamic-call", 2, 0, 0, (scm_t_subr) scm_dynamic_call);
  172. }