shl_load.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* loader-shl_load.c -- dynamic linking with shl_load (HP-UX)
  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 shl_load_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__zalloc (sizeof *vtable);
  53. }
  54. if (vtable && !vtable->name)
  55. {
  56. vtable->name = "lt_shl_load";
  57. vtable->module_open = vm_open;
  58. vtable->module_close = vm_close;
  59. vtable->find_sym = vm_sym;
  60. vtable->dlloader_exit = vl_exit;
  61. vtable->dlloader_data = loader_data;
  62. vtable->priority = LT_DLLOADER_APPEND;
  63. }
  64. if (vtable && (vtable->dlloader_data != loader_data))
  65. {
  66. LT__SETERROR (INIT_LOADER);
  67. return 0;
  68. }
  69. return vtable;
  70. }
  71. /* --- IMPLEMENTATION --- */
  72. #if defined HAVE_DL_H
  73. # include <dl.h>
  74. #endif
  75. /* some flags are missing on some systems, so we provide
  76. * harmless defaults.
  77. *
  78. * Mandatory:
  79. * BIND_IMMEDIATE - Resolve symbol references when the library is loaded.
  80. * BIND_DEFERRED - Delay code symbol resolution until actual reference.
  81. *
  82. * Optionally:
  83. * BIND_FIRST - Place the library at the head of the symbol search
  84. * order.
  85. * BIND_NONFATAL - The default BIND_IMMEDIATE behavior is to treat all
  86. * unsatisfied symbols as fatal. This flag allows
  87. * binding of unsatisfied code symbols to be deferred
  88. * until use.
  89. * [Perl: For certain libraries, like DCE, deferred
  90. * binding often causes run time problems. Adding
  91. * BIND_NONFATAL to BIND_IMMEDIATE still allows
  92. * unresolved references in situations like this.]
  93. * BIND_NOSTART - Do not call the initializer for the shared library
  94. * when the library is loaded, nor on a future call to
  95. * shl_unload().
  96. * BIND_VERBOSE - Print verbose messages concerning possible
  97. * unsatisfied symbols.
  98. *
  99. * hp9000s700/hp9000s800:
  100. * BIND_RESTRICTED - Restrict symbols visible by the library to those
  101. * present at library load time.
  102. * DYNAMIC_PATH - Allow the loader to dynamically search for the
  103. * library specified by the path argument.
  104. */
  105. #if !defined DYNAMIC_PATH
  106. # define DYNAMIC_PATH 0
  107. #endif
  108. #if !defined BIND_RESTRICTED
  109. # define BIND_RESTRICTED 0
  110. #endif
  111. #define LT_BIND_FLAGS (BIND_IMMEDIATE | BIND_NONFATAL | DYNAMIC_PATH)
  112. /* A function called through the vtable when this loader is no
  113. longer needed by the application. */
  114. static int
  115. vl_exit (lt_user_data loader_data LT__UNUSED)
  116. {
  117. vtable = NULL;
  118. return 0;
  119. }
  120. /* A function called through the vtable to open a module with this
  121. loader. Returns an opaque representation of the newly opened
  122. module for processing with this loader's other vtable functions. */
  123. static lt_module
  124. vm_open (lt_user_data loader_data LT__UNUSED, const char *filename,
  125. lt_dladvise advise LT__UNUSED)
  126. {
  127. static shl_t self = (shl_t) 0;
  128. lt_module module = shl_load (filename, LT_BIND_FLAGS, 0L);
  129. /* Since searching for a symbol against a NULL module handle will also
  130. look in everything else that was already loaded and exported with
  131. the -E compiler flag, we always cache a handle saved before any
  132. modules are loaded. */
  133. if (!self)
  134. {
  135. void *address;
  136. shl_findsym (&self, "main", TYPE_UNDEFINED, &address);
  137. }
  138. if (!filename)
  139. {
  140. module = self;
  141. }
  142. else
  143. {
  144. module = shl_load (filename, LT_BIND_FLAGS, 0L);
  145. if (!module)
  146. {
  147. LT__SETERROR (CANNOT_OPEN);
  148. }
  149. }
  150. return module;
  151. }
  152. /* A function called through the vtable when a particular module
  153. should be unloaded. */
  154. static int
  155. vm_close (lt_user_data loader_data LT__UNUSED, lt_module module)
  156. {
  157. int errors = 0;
  158. if (module && (shl_unload ((shl_t) (module)) != 0))
  159. {
  160. LT__SETERROR (CANNOT_CLOSE);
  161. ++errors;
  162. }
  163. return errors;
  164. }
  165. /* A function called through the vtable to get the address of
  166. a symbol loaded from a particular module. */
  167. static void *
  168. vm_sym (lt_user_data loader_data LT__UNUSED, lt_module module, const char *name)
  169. {
  170. void *address = 0;
  171. /* sys_shl_open should never return a NULL module handle */
  172. if (module == (lt_module) 0)
  173. {
  174. LT__SETERROR (INVALID_HANDLE);
  175. }
  176. else if (!shl_findsym((shl_t*) &module, name, TYPE_UNDEFINED, &address))
  177. {
  178. if (!address)
  179. {
  180. LT__SETERROR (SYMBOL_NOT_FOUND);
  181. }
  182. }
  183. return address;
  184. }