dlopen.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* loader-dlopen.c -- dynamic linking with dlopen/dlsym
  2. Copyright (C) 1998-2000, 2004, 2006-2008, 2011-2015 Free Software
  3. Foundation, Inc.
  4. Written by Thomas Tanner, 1998
  5. NOTE: The canonical source of this file is maintained with the
  6. GNU Libtool package. Report bugs to bug-libtool@gnu.org.
  7. GNU Libltdl is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2 of the License, or (at your option) any later version.
  11. As a special exception to the GNU Lesser General Public License,
  12. if you distribute this file as part of a program or library that
  13. is built using GNU Libtool, you may include this file under the
  14. same distribution terms that you use for the rest of that program.
  15. GNU Libltdl is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU Lesser General Public License for more details.
  19. You should have received a copy of the GNU Lesser General Public
  20. License along with GNU Libltdl; see the file COPYING.LIB. If not, a
  21. copy can be downloaded from http://www.gnu.org/licenses/lgpl.html,
  22. or obtained by writing to the Free Software Foundation, Inc.,
  23. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. #include "lt__private.h"
  26. #include "lt_dlloader.h"
  27. /* Use the preprocessor to rename non-static symbols to avoid namespace
  28. collisions when the loader code is statically linked into libltdl.
  29. Use the "<module_name>_LTX_" prefix so that the symbol addresses can
  30. be fetched from the preloaded symbol list by lt_dlsym(): */
  31. #define get_vtable dlopen_LTX_get_vtable
  32. LT_BEGIN_C_DECLS
  33. LT_SCOPE lt_dlvtable *get_vtable (lt_user_data loader_data);
  34. LT_END_C_DECLS
  35. /* Boilerplate code to set up the vtable for hooking this loader into
  36. libltdl's loader list: */
  37. static int vl_exit (lt_user_data loader_data);
  38. static lt_module vm_open (lt_user_data loader_data, const char *filename,
  39. lt_dladvise advise);
  40. static int vm_close (lt_user_data loader_data, lt_module module);
  41. static void * vm_sym (lt_user_data loader_data, lt_module module,
  42. const char *symbolname);
  43. static lt_dlvtable *vtable = 0;
  44. /* Return the vtable for this loader, only the name and sym_prefix
  45. attributes (plus the virtual function implementations, obviously)
  46. change between loaders. */
  47. lt_dlvtable *
  48. get_vtable (lt_user_data loader_data)
  49. {
  50. if (!vtable)
  51. {
  52. vtable = (lt_dlvtable *) lt__zalloc (sizeof *vtable);
  53. }
  54. if (vtable && !vtable->name)
  55. {
  56. vtable->name = "lt_dlopen";
  57. #if defined DLSYM_USCORE
  58. vtable->sym_prefix = "_";
  59. #endif
  60. vtable->module_open = vm_open;
  61. vtable->module_close = vm_close;
  62. vtable->find_sym = vm_sym;
  63. vtable->dlloader_exit = vl_exit;
  64. vtable->dlloader_data = loader_data;
  65. vtable->priority = LT_DLLOADER_PREPEND;
  66. }
  67. if (vtable && (vtable->dlloader_data != loader_data))
  68. {
  69. LT__SETERROR (INIT_LOADER);
  70. return 0;
  71. }
  72. return vtable;
  73. }
  74. /* --- IMPLEMENTATION --- */
  75. #if defined HAVE_DLFCN_H
  76. # include <dlfcn.h>
  77. #endif
  78. #if defined HAVE_SYS_DL_H
  79. # include <sys/dl.h>
  80. #endif
  81. /* We may have to define LT_LAZY_OR_NOW in the command line if we
  82. find out it does not work in some platform. */
  83. #if !defined LT_LAZY_OR_NOW
  84. # if defined RTLD_LAZY
  85. # define LT_LAZY_OR_NOW RTLD_LAZY
  86. # else
  87. # if defined DL_LAZY
  88. # define LT_LAZY_OR_NOW DL_LAZY
  89. # endif
  90. # endif /* !RTLD_LAZY */
  91. #endif
  92. #if !defined LT_LAZY_OR_NOW
  93. # if defined RTLD_NOW
  94. # define LT_LAZY_OR_NOW RTLD_NOW
  95. # else
  96. # if defined DL_NOW
  97. # define LT_LAZY_OR_NOW DL_NOW
  98. # endif
  99. # endif /* !RTLD_NOW */
  100. #endif
  101. #if !defined LT_LAZY_OR_NOW
  102. # define LT_LAZY_OR_NOW 0
  103. #endif /* !LT_LAZY_OR_NOW */
  104. /* We only support local and global symbols from modules for loaders
  105. that provide such a thing, otherwise the system default is used. */
  106. #if !defined RTLD_GLOBAL
  107. # if defined DL_GLOBAL
  108. # define RTLD_GLOBAL DL_GLOBAL
  109. # endif
  110. #endif /* !RTLD_GLOBAL */
  111. #if !defined RTLD_LOCAL
  112. # if defined DL_LOCAL
  113. # define RTLD_LOCAL DL_LOCAL
  114. # endif
  115. #endif /* !RTLD_LOCAL */
  116. #if defined HAVE_DLERROR
  117. # define DLERROR(arg) dlerror ()
  118. #else
  119. # define DLERROR(arg) LT__STRERROR (arg)
  120. #endif
  121. #define DL__SETERROR(errorcode) \
  122. LT__SETERRORSTR (DLERROR (errorcode))
  123. /* A function called through the vtable when this loader is no
  124. longer needed by the application. */
  125. static int
  126. vl_exit (lt_user_data loader_data LT__UNUSED)
  127. {
  128. vtable = NULL;
  129. return 0;
  130. }
  131. /* A function called through the vtable to open a module with this
  132. loader. Returns an opaque representation of the newly opened
  133. module for processing with this loader's other vtable functions. */
  134. static lt_module
  135. vm_open (lt_user_data loader_data LT__UNUSED, const char *filename,
  136. lt_dladvise advise)
  137. {
  138. int module_flags = LT_LAZY_OR_NOW;
  139. lt_module module;
  140. #ifdef RTLD_MEMBER
  141. int len = LT_STRLEN (filename);
  142. #endif
  143. if (advise)
  144. {
  145. #ifdef RTLD_GLOBAL
  146. /* If there is some means of asking for global symbol resolution,
  147. do so. */
  148. if (advise->is_symglobal)
  149. module_flags |= RTLD_GLOBAL;
  150. #else
  151. /* Otherwise, reset that bit so the caller can tell it wasn't
  152. acted on. */
  153. advise->is_symglobal = 0;
  154. #endif
  155. /* And similarly for local only symbol resolution. */
  156. #ifdef RTLD_LOCAL
  157. if (advise->is_symlocal)
  158. module_flags |= RTLD_LOCAL;
  159. #else
  160. advise->is_symlocal = 0;
  161. #endif
  162. }
  163. #ifdef RTLD_MEMBER /* AIX */
  164. if (len >= 4) /* at least "l(m)" */
  165. {
  166. /* Advise loading an archive member only if the filename really
  167. contains both the opening and closing parent, and a member. */
  168. if (filename[len-1] == ')')
  169. {
  170. const char *opening = strrchr(filename, '(');
  171. if (opening && opening < (filename+len-2) && strchr(opening+1, '/') == NULL)
  172. module_flags |= RTLD_MEMBER;
  173. }
  174. }
  175. #endif
  176. module = dlopen (filename, module_flags);
  177. #if defined RTLD_MEMBER && defined LT_SHARED_LIB_MEMBER
  178. if (!module && len && !(module_flags & RTLD_MEMBER) && errno == ENOEXEC)
  179. {
  180. /* Loading without a member specified failed with "Exec format error".
  181. So the file is there, but either has wrong bitwidth, or is an
  182. archive eventually containing the default shared archive member.
  183. Retry with default member, getting same error in worst case. */
  184. const char *member = LT_SHARED_LIB_MEMBER;
  185. char *attempt = MALLOC (char, len + strlen (member) + 1);
  186. if (!attempt)
  187. {
  188. LT__SETERROR (NO_MEMORY);
  189. return module;
  190. }
  191. sprintf (attempt, "%s%s", filename, member);
  192. module = vm_open (loader_data, attempt, advise);
  193. FREE (attempt);
  194. return module;
  195. }
  196. #endif
  197. if (!module)
  198. {
  199. DL__SETERROR (CANNOT_OPEN);
  200. }
  201. return module;
  202. }
  203. /* A function called through the vtable when a particular module
  204. should be unloaded. */
  205. static int
  206. vm_close (lt_user_data loader_data LT__UNUSED, lt_module module)
  207. {
  208. int errors = 0;
  209. if (dlclose (module) != 0)
  210. {
  211. DL__SETERROR (CANNOT_CLOSE);
  212. ++errors;
  213. }
  214. return errors;
  215. }
  216. /* A function called through the vtable to get the address of
  217. a symbol loaded from a particular module. */
  218. static void *
  219. vm_sym (lt_user_data loader_data LT__UNUSED, lt_module module, const char *name)
  220. {
  221. void *address = dlsym (module, name);
  222. if (!address)
  223. {
  224. DL__SETERROR (SYMBOL_NOT_FOUND);
  225. }
  226. return address;
  227. }