[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* |
---|
| 5 | * ABSTRACT: machine depend code for dynamic modules |
---|
| 6 | * |
---|
[f4399e] | 7 | * Provides: dynl_check_opened() |
---|
| 8 | * dynl_open() |
---|
[35aab3] | 9 | * dynl_sym() |
---|
| 10 | * dynl_error() |
---|
[4c7d73] | 11 | * dynl_close() |
---|
[35aab3] | 12 | */ |
---|
| 13 | |
---|
| 14 | #include <stdio.h> |
---|
[9ed245] | 15 | #include <string.h> |
---|
| 16 | #include <ctype.h> |
---|
| 17 | #include <sys/stat.h> |
---|
[bdda8c2] | 18 | #include <errno.h> |
---|
[394d507] | 19 | #include <unistd.h> |
---|
[9ed245] | 20 | |
---|
[35aab3] | 21 | |
---|
[9f7665] | 22 | |
---|
| 23 | |
---|
| 24 | |
---|
[aadd638] | 25 | #include "misc/auxiliary.h" |
---|
[35aab3] | 26 | |
---|
[aadd638] | 27 | #include "omalloc/omalloc.h" |
---|
[8c9912] | 28 | |
---|
[aadd638] | 29 | #include "reporter/reporter.h" |
---|
[8c9912] | 30 | |
---|
[aadd638] | 31 | #include "resources/feResource.h" |
---|
[8c9912] | 32 | |
---|
[fad7c58] | 33 | #include "mod_raw.h" |
---|
[35aab3] | 34 | |
---|
[abaf13] | 35 | #ifdef HAVE_STATIC |
---|
| 36 | #undef HAVE_DL |
---|
| 37 | #endif |
---|
[9ed245] | 38 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
| 39 | #if defined(HAVE_DL) |
---|
| 40 | |
---|
[35aab3] | 41 | /***************************************************************************** |
---|
| 42 | * |
---|
| 43 | * General section |
---|
[abaf13] | 44 | * These are just wrappers around the repsective dynl_* calls |
---|
[35aab3] | 45 | * which look for the binary in the bin_dir of Singular and ommit warnings if |
---|
| 46 | * somethings goes wrong |
---|
| 47 | * |
---|
| 48 | *****************************************************************************/ |
---|
| 49 | static BOOLEAN warn_handle = FALSE; |
---|
| 50 | static BOOLEAN warn_proc = FALSE; |
---|
| 51 | #ifndef DL_TAIL |
---|
[b5d6f0] | 52 | #define DL_TAIL ".so" |
---|
| 53 | //#define DL_TAIL "_g.so" |
---|
[e71b67] | 54 | #endif |
---|
[35aab3] | 55 | |
---|
[9ef9d0] | 56 | void* dynl_open_binary_warn(const char* binary_name, const char* msg) |
---|
[35aab3] | 57 | { |
---|
| 58 | void* handle = NULL; |
---|
[96c5cb] | 59 | char* binary_name_so=NULL; |
---|
[394d507] | 60 | BOOLEAN found=FALSE; |
---|
[f9dc520] | 61 | |
---|
[3fa147] | 62 | // try P_PROCS_DIR (%P) |
---|
[394d507] | 63 | char* proc_path = feGetResource('P'); |
---|
| 64 | if (proc_path != NULL) |
---|
[3fa147] | 65 | { |
---|
[394d507] | 66 | char *p; |
---|
| 67 | char *q; |
---|
| 68 | p=proc_path; |
---|
| 69 | int binary_name_so_length = 3 + strlen(DL_TAIL) + strlen(binary_name) + strlen(DIR_SEPP) + strlen(proc_path); |
---|
| 70 | binary_name_so = (char *)omAlloc0( binary_name_so_length * sizeof(char) ); |
---|
| 71 | while((p!=NULL)&&(*p!='\0')) |
---|
[3fa147] | 72 | { |
---|
[394d507] | 73 | q=strchr(p,fePathSep); |
---|
| 74 | if (q!=NULL) *q='\0'; |
---|
| 75 | strcpy(binary_name_so,p); |
---|
| 76 | if (q!=NULL) *q=fePathSep; |
---|
| 77 | strcat(binary_name_so,DIR_SEPP); |
---|
| 78 | strcat(binary_name_so,binary_name); |
---|
| 79 | strcat(binary_name_so,DL_TAIL); |
---|
| 80 | if(!access(binary_name_so, R_OK)) { found=TRUE; break; } |
---|
| 81 | if (q!=NULL) p=q+1; else p=NULL; |
---|
[3fa147] | 82 | } |
---|
[394d507] | 83 | if (found) handle = dynl_open(binary_name_so); |
---|
[3fa147] | 84 | } |
---|
| 85 | |
---|
[f9dc520] | 86 | if (handle == NULL && ! warn_handle) |
---|
| 87 | { |
---|
[394d507] | 88 | Warn("Could not find dynamic library: %s%s (path %s)", |
---|
| 89 | binary_name, DL_TAIL,proc_path); |
---|
| 90 | if (found) Warn("Error message from system: %s", dynl_error()); |
---|
| 91 | if (msg != NULL) Warn("%s", msg); |
---|
| 92 | Warn("See the INSTALL section in the Singular manual for details."); |
---|
| 93 | warn_handle = TRUE; |
---|
[35aab3] | 94 | } |
---|
[96c5cb] | 95 | omfree((ADDRESS)binary_name_so ); |
---|
[f9dc520] | 96 | |
---|
[35aab3] | 97 | return handle; |
---|
| 98 | } |
---|
| 99 | |
---|
[85e68dd] | 100 | void* dynl_sym_warn(void* handle, const char* proc, const char* msg) |
---|
[35aab3] | 101 | { |
---|
| 102 | void *proc_ptr = NULL; |
---|
| 103 | if (handle != NULL) |
---|
| 104 | { |
---|
| 105 | proc_ptr = dynl_sym(handle, proc); |
---|
| 106 | if (proc_ptr == NULL && ! warn_proc) |
---|
| 107 | { |
---|
| 108 | Warn("Could load a procedure from a dynamic library"); |
---|
| 109 | Warn("Error message from system: %s", dynl_error()); |
---|
| 110 | if (msg != NULL) Warn("%s", msg); |
---|
| 111 | Warn("See the INSTALL section in the Singular manual for details."); |
---|
| 112 | warn_proc = TRUE; |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | return proc_ptr; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | #ifdef __cplusplus |
---|
| 119 | extern "C" { |
---|
| 120 | #endif |
---|
| 121 | |
---|
| 122 | /***************************************************************************** |
---|
[fc300f] | 123 | * SECTION generic ELF: ix86-linux / alpha-linux / IA64-linux /x86_64_Linux * |
---|
[4287a0] | 124 | * SunOS-5 / IRIX-6 / ppcMac-Darwin / FreeeBSD * |
---|
[35aab3] | 125 | *****************************************************************************/ |
---|
[f02645b] | 126 | // relying on gcc to define __ELF__, check with cpp -dM /dev/null |
---|
[d44ced] | 127 | #if defined(__ELF__) |
---|
[fde597] | 128 | #define HAVE_ELF_SYSTEM |
---|
[9b3de5] | 129 | #endif |
---|
| 130 | |
---|
[319eab] | 131 | // Mac OsX is an ELF system, but does not define __ELF__ |
---|
[c899161] | 132 | #if (defined(__APPLE__) && defined(__MACH__)) && (!defined(HAVE_ELF_SYSTEM)) |
---|
| 133 | #define HAVE_ELF_SYSTEM |
---|
| 134 | #endif |
---|
| 135 | |
---|
[319eab] | 136 | // Solaris is an ELF system, but does not define __ELF__ |
---|
| 137 | #if defined(__sun) && defined(__SVR4) |
---|
[c3ab214] | 138 | #define HAVE_ELF_SYSTEM |
---|
[fde597] | 139 | #endif |
---|
| 140 | |
---|
[35aab3] | 141 | #if defined(HAVE_ELF_SYSTEM) |
---|
| 142 | #include <dlfcn.h> |
---|
[3dbe0bf] | 143 | #define DL_IMPLEMENTED |
---|
[35aab3] | 144 | |
---|
| 145 | static void* kernel_handle = NULL; |
---|
[f4399e] | 146 | int dynl_check_opened( |
---|
| 147 | char *filename /* I: filename to check */ |
---|
| 148 | ) |
---|
| 149 | { |
---|
| 150 | return dlopen(filename,RTLD_NOW|RTLD_NOLOAD) != NULL; |
---|
| 151 | } |
---|
| 152 | |
---|
[35aab3] | 153 | void *dynl_open( |
---|
| 154 | char *filename /* I: filename to load */ |
---|
| 155 | ) |
---|
| 156 | { |
---|
[a513d10] | 157 | // glibc 2.2: |
---|
[a11e71] | 158 | if ((filename==NULL) || (dlopen(filename,RTLD_NOW|RTLD_NOLOAD)==NULL)) |
---|
[a513d10] | 159 | return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL)); |
---|
| 160 | else |
---|
| 161 | Werror("module %s already loaded",filename); |
---|
| 162 | return NULL; |
---|
[3dbe0bf] | 163 | // alternative |
---|
[a513d10] | 164 | // return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL)); |
---|
[35aab3] | 165 | } |
---|
| 166 | |
---|
[85e68dd] | 167 | void *dynl_sym(void *handle, const char *symbol) |
---|
[35aab3] | 168 | { |
---|
| 169 | if (handle == DYNL_KERNEL_HANDLE) |
---|
| 170 | { |
---|
| 171 | if (kernel_handle == NULL) |
---|
| 172 | kernel_handle = dynl_open(NULL); |
---|
| 173 | handle = kernel_handle; |
---|
| 174 | } |
---|
| 175 | return(dlsym(handle, symbol)); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | int dynl_close (void *handle) |
---|
| 179 | { |
---|
| 180 | return(dlclose (handle)); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | const char *dynl_error() |
---|
| 184 | { |
---|
| 185 | return(dlerror()); |
---|
| 186 | } |
---|
[fc300f] | 187 | #endif /* ELF_SYSTEM */ |
---|
[35aab3] | 188 | |
---|
| 189 | /***************************************************************************** |
---|
| 190 | * SECTION HPUX-9/10 * |
---|
| 191 | *****************************************************************************/ |
---|
| 192 | #if defined(HPUX_9) || defined(HPUX_10) |
---|
[3dbe0bf] | 193 | #define DL_IMPLEMENTED |
---|
[35aab3] | 194 | #include <dl.h> |
---|
| 195 | |
---|
| 196 | typedef char *((*func_ptr) ()); |
---|
| 197 | |
---|
[f4399e] | 198 | int dynl_check_opened( /* NOTE: untested */ |
---|
| 199 | char *filename /* I: filename to check */ |
---|
| 200 | ) |
---|
| 201 | { |
---|
| 202 | struct shl_descriptor *desc; |
---|
| 203 | for (int idx = 0; shl_get(idx, &desc) != -1; ++idx) |
---|
| 204 | { |
---|
| 205 | if (strcmp(filename, desc->filename) == 0) return TRUE; |
---|
| 206 | } |
---|
| 207 | return FALSE; |
---|
| 208 | } |
---|
| 209 | |
---|
[35aab3] | 210 | void *dynl_open(char *filename) |
---|
| 211 | { |
---|
| 212 | shl_t handle = shl_load(filename, BIND_DEFERRED, 0); |
---|
| 213 | |
---|
| 214 | return ((void *) handle); |
---|
| 215 | } |
---|
| 216 | |
---|
[85e68dd] | 217 | void *dynl_sym(void *handle, const char *symbol) |
---|
[35aab3] | 218 | { |
---|
| 219 | func_ptr f; |
---|
[abaf13] | 220 | |
---|
[35aab3] | 221 | if (handle == DYNL_KERNEL_HANDLE) |
---|
| 222 | handle = PROG_HANDLE; |
---|
[abaf13] | 223 | |
---|
[35aab3] | 224 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_PROCEDURE, &f) == -1) |
---|
| 225 | { |
---|
| 226 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_UNDEFINED, &f) == -1) |
---|
| 227 | { |
---|
| 228 | f = (func_ptr) NULL; |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | return ((void *)f); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | int dynl_close (void *handle) |
---|
| 235 | { |
---|
| 236 | shl_unload((shl_t) handle); |
---|
| 237 | return(0); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | const char *dynl_error() |
---|
| 241 | { |
---|
| 242 | static char errmsg[] = "shl_load failed"; |
---|
| 243 | |
---|
| 244 | return errmsg; |
---|
| 245 | } |
---|
| 246 | #endif /* HPUX_9 or HPUX_10 */ |
---|
| 247 | |
---|
| 248 | /***************************************************************************** |
---|
[fc300f] | 249 | * SECTION generic: dynamic madules not available |
---|
[35aab3] | 250 | *****************************************************************************/ |
---|
[3dbe0bf] | 251 | #ifndef DL_IMPLEMENTED |
---|
[35aab3] | 252 | |
---|
[f4399e] | 253 | int dynl_check_opened(char *filename) |
---|
| 254 | { |
---|
| 255 | return FALSE; |
---|
| 256 | } |
---|
| 257 | |
---|
[35aab3] | 258 | void *dynl_open(char *filename) |
---|
| 259 | { |
---|
| 260 | return(NULL); |
---|
| 261 | } |
---|
| 262 | |
---|
[85e68dd] | 263 | void *dynl_sym(void *handle, const char *symbol) |
---|
[35aab3] | 264 | { |
---|
| 265 | return(NULL); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | int dynl_close (void *handle) |
---|
| 269 | { |
---|
| 270 | return(0); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | const char *dynl_error() |
---|
| 274 | { |
---|
| 275 | static char errmsg[] = "support for dynamic loading not implemented"; |
---|
| 276 | return errmsg; |
---|
| 277 | } |
---|
[fc300f] | 278 | #endif |
---|
[35aab3] | 279 | |
---|
| 280 | #ifdef __cplusplus |
---|
| 281 | } |
---|
| 282 | #endif |
---|
| 283 | |
---|
| 284 | #endif /* HAVE_DL */ |
---|