| 1 | /* loader-shl_load.c -- dynamic linking with shl_load (HP-UX) |
| 2 | |
| 3 | Copyright (C) 1998, 1999, 2000, 2004, 2006, |
| 4 | 2007, 2008 Free Software Foundation, Inc. |
| 5 | Written by Thomas Tanner, 1998 |
| 6 | |
| 7 | NOTE: The canonical source of this file is maintained with the |
| 8 | GNU Libtool package. Report bugs to bug-libtool@gnu.org. |
| 9 | |
| 10 | GNU Libltdl is free software; you can redistribute it and/or |
| 11 | modify it under the terms of the GNU Lesser General Public |
| 12 | License as published by the Free Software Foundation; either |
| 13 | version 2 of the License, or (at your option) any later version. |
| 14 | |
| 15 | As a special exception to the GNU Lesser General Public License, |
| 16 | if you distribute this file as part of a program or library that |
| 17 | is built using GNU Libtool, you may include this file under the |
| 18 | same distribution terms that you use for the rest of that program. |
| 19 | |
| 20 | GNU Libltdl is distributed in the hope that it will be useful, |
| 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | GNU Lesser General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU Lesser General Public |
| 26 | License along with GNU Libltdl; see the file COPYING.LIB. If not, a |
| 27 | copy can be downloaded from http://www.gnu.org/licenses/lgpl.html, |
| 28 | or obtained by writing to the Free Software Foundation, Inc., |
| 29 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 30 | */ |
| 31 | |
| 32 | #include "lt__private.h" |
| 33 | #include "lt_dlloader.h" |
| 34 | |
| 35 | /* Use the preprocessor to rename non-static symbols to avoid namespace |
| 36 | collisions when the loader code is statically linked into libltdl. |
| 37 | Use the "<module_name>_LTX_" prefix so that the symbol addresses can |
| 38 | be fetched from the preloaded symbol list by lt_dlsym(): */ |
| 39 | #define get_vtable shl_load_LTX_get_vtable |
| 40 | |
| 41 | LT_BEGIN_C_DECLS |
| 42 | LT_SCOPE lt_dlvtable *get_vtable (lt_user_data loader_data); |
| 43 | LT_END_C_DECLS |
| 44 | |
| 45 | |
| 46 | /* Boilerplate code to set up the vtable for hooking this loader into |
| 47 | libltdl's loader list: */ |
| 48 | static int vl_exit (lt_user_data loader_data); |
| 49 | static lt_module vm_open (lt_user_data loader_data, const char *filename, |
| 50 | lt_dladvise advise); |
| 51 | static int vm_close (lt_user_data loader_data, lt_module module); |
| 52 | static void * vm_sym (lt_user_data loader_data, lt_module module, |
| 53 | const char *symbolname); |
| 54 | |
| 55 | static lt_dlvtable *vtable = 0; |
| 56 | |
| 57 | /* Return the vtable for this loader, only the name and sym_prefix |
| 58 | attributes (plus the virtual function implementations, obviously) |
| 59 | change between loaders. */ |
| 60 | lt_dlvtable * |
| 61 | get_vtable (lt_user_data loader_data) |
| 62 | { |
| 63 | if (!vtable) |
| 64 | { |
| 65 | vtable = lt__zalloc (sizeof *vtable); |
| 66 | } |
| 67 | |
| 68 | if (vtable && !vtable->name) |
| 69 | { |
| 70 | vtable->name = "lt_shl_load"; |
| 71 | vtable->module_open = vm_open; |
| 72 | vtable->module_close = vm_close; |
| 73 | vtable->find_sym = vm_sym; |
| 74 | vtable->dlloader_exit = vl_exit; |
| 75 | vtable->dlloader_data = loader_data; |
| 76 | vtable->priority = LT_DLLOADER_APPEND; |
| 77 | } |
| 78 | |
| 79 | if (vtable && (vtable->dlloader_data != loader_data)) |
| 80 | { |
| 81 | LT__SETERROR (INIT_LOADER); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | return vtable; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | /* --- IMPLEMENTATION --- */ |
| 91 | |
| 92 | |
| 93 | #if defined(HAVE_DL_H) |
| 94 | # include <dl.h> |
| 95 | #endif |
| 96 | |
| 97 | /* some flags are missing on some systems, so we provide |
| 98 | * harmless defaults. |
| 99 | * |
| 100 | * Mandatory: |
| 101 | * BIND_IMMEDIATE - Resolve symbol references when the library is loaded. |
| 102 | * BIND_DEFERRED - Delay code symbol resolution until actual reference. |
| 103 | * |
| 104 | * Optionally: |
| 105 | * BIND_FIRST - Place the library at the head of the symbol search |
| 106 | * order. |
| 107 | * BIND_NONFATAL - The default BIND_IMMEDIATE behavior is to treat all |
| 108 | * unsatisfied symbols as fatal. This flag allows |
| 109 | * binding of unsatisfied code symbols to be deferred |
| 110 | * until use. |
| 111 | * [Perl: For certain libraries, like DCE, deferred |
| 112 | * binding often causes run time problems. Adding |
| 113 | * BIND_NONFATAL to BIND_IMMEDIATE still allows |
| 114 | * unresolved references in situations like this.] |
| 115 | * BIND_NOSTART - Do not call the initializer for the shared library |
| 116 | * when the library is loaded, nor on a future call to |
| 117 | * shl_unload(). |
| 118 | * BIND_VERBOSE - Print verbose messages concerning possible |
| 119 | * unsatisfied symbols. |
| 120 | * |
| 121 | * hp9000s700/hp9000s800: |
| 122 | * BIND_RESTRICTED - Restrict symbols visible by the library to those |
| 123 | * present at library load time. |
| 124 | * DYNAMIC_PATH - Allow the loader to dynamically search for the |
| 125 | * library specified by the path argument. |
| 126 | */ |
| 127 | |
| 128 | #if !defined(DYNAMIC_PATH) |
| 129 | # define DYNAMIC_PATH 0 |
| 130 | #endif |
| 131 | #if !defined(BIND_RESTRICTED) |
| 132 | # define BIND_RESTRICTED 0 |
| 133 | #endif |
| 134 | |
| 135 | #define LT_BIND_FLAGS (BIND_IMMEDIATE | BIND_NONFATAL | DYNAMIC_PATH) |
| 136 | |
| 137 | |
| 138 | /* A function called through the vtable when this loader is no |
| 139 | longer needed by the application. */ |
| 140 | static int |
| 141 | vl_exit (lt_user_data LT__UNUSED loader_data) |
| 142 | { |
| 143 | vtable = NULL; |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* A function called through the vtable to open a module with this |
| 148 | loader. Returns an opaque representation of the newly opened |
| 149 | module for processing with this loader's other vtable functions. */ |
| 150 | static lt_module |
| 151 | vm_open (lt_user_data LT__UNUSED loader_data, const char *filename, |
| 152 | lt_dladvise LT__UNUSED advise) |
| 153 | { |
| 154 | static shl_t self = (shl_t) 0; |
| 155 | lt_module module = shl_load (filename, LT_BIND_FLAGS, 0L); |
| 156 | |
| 157 | /* Since searching for a symbol against a NULL module handle will also |
| 158 | look in everything else that was already loaded and exported with |
| 159 | the -E compiler flag, we always cache a handle saved before any |
| 160 | modules are loaded. */ |
| 161 | if (!self) |
| 162 | { |
| 163 | void *address; |
| 164 | shl_findsym (&self, "main", TYPE_UNDEFINED, &address); |
| 165 | } |
| 166 | |
| 167 | if (!filename) |
| 168 | { |
| 169 | module = self; |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | module = shl_load (filename, LT_BIND_FLAGS, 0L); |
| 174 | |
| 175 | if (!module) |
| 176 | { |
| 177 | LT__SETERROR (CANNOT_OPEN); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return module; |
| 182 | } |
| 183 | |
| 184 | /* A function called through the vtable when a particular module |
| 185 | should be unloaded. */ |
| 186 | static int |
| 187 | vm_close (lt_user_data LT__UNUSED loader_data, lt_module module) |
| 188 | { |
| 189 | int errors = 0; |
| 190 | |
| 191 | if (module && (shl_unload ((shl_t) (module)) != 0)) |
| 192 | { |
| 193 | LT__SETERROR (CANNOT_CLOSE); |
| 194 | ++errors; |
| 195 | } |
| 196 | |
| 197 | return errors; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /* A function called through the vtable to get the address of |
| 202 | a symbol loaded from a particular module. */ |
| 203 | static void * |
| 204 | vm_sym (lt_user_data LT__UNUSED loader_data, lt_module module, const char *name) |
| 205 | { |
| 206 | void *address = 0; |
| 207 | |
| 208 | /* sys_shl_open should never return a NULL module handle */ |
| 209 | if (module == (lt_module) 0) |
| 210 | { |
| 211 | LT__SETERROR (INVALID_HANDLE); |
| 212 | } |
| 213 | else if (!shl_findsym((shl_t*) &module, name, TYPE_UNDEFINED, &address)) |
| 214 | { |
| 215 | if (!address) |
| 216 | { |
| 217 | LT__SETERROR (SYMBOL_NOT_FOUND); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return address; |
| 222 | } |