[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* |
---|
| 5 | * ABSTRACT: machine depend code for dynamic modules |
---|
| 6 | * |
---|
| 7 | * Provides: dynl_open() |
---|
| 8 | * dynl_sym() |
---|
| 9 | * dynl_error() |
---|
[4c7d73] | 10 | * dynl_close() |
---|
[35aab3] | 11 | */ |
---|
| 12 | |
---|
| 13 | #include <stdio.h> |
---|
[9ed245] | 14 | #include <string.h> |
---|
| 15 | #include <ctype.h> |
---|
| 16 | #include <sys/stat.h> |
---|
| 17 | |
---|
[35aab3] | 18 | |
---|
[8c9912] | 19 | #include "config.h" |
---|
| 20 | #include <misc/auxiliary.h> |
---|
[35aab3] | 21 | |
---|
[f9dc520] | 22 | #include <omalloc/omalloc.h> |
---|
[8c9912] | 23 | |
---|
| 24 | #include <reporter/reporter.h> |
---|
| 25 | |
---|
[f323dd1] | 26 | #include <findexec/feResource.h> |
---|
[8c9912] | 27 | |
---|
[fad7c58] | 28 | #include "mod_raw.h" |
---|
[35aab3] | 29 | |
---|
[abaf13] | 30 | #ifdef HAVE_STATIC |
---|
| 31 | #undef HAVE_DL |
---|
| 32 | #endif |
---|
[9ed245] | 33 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
| 34 | #define BYTES_TO_CHECK 7 |
---|
| 35 | |
---|
| 36 | lib_types type_of_LIB(char *newlib, char *libnamebuf) |
---|
| 37 | { |
---|
[7dce2d7] | 38 | const unsigned char mach_o[]={0xfe,0xed,0xfa,0xce,0}; |
---|
[05b917] | 39 | const unsigned char mach_O[]={0xce,0xfa,0xed,0xfe,0}; |
---|
[6e81fc] | 40 | |
---|
| 41 | const unsigned char mach_o64[]={0xfe,0xed,0xfa,0xcf,0}; |
---|
| 42 | const unsigned char mach_O64[]={0xcf,0xfa,0xed,0xfe,0}; |
---|
| 43 | |
---|
[9ed245] | 44 | char buf[BYTES_TO_CHECK+1]; /* one extra for terminating '\0' */ |
---|
| 45 | struct stat sb; |
---|
| 46 | int nbytes = 0; |
---|
| 47 | int ret; |
---|
| 48 | lib_types LT=LT_NONE; |
---|
| 49 | |
---|
| 50 | FILE * fp = feFopen( newlib, "r", libnamebuf, FALSE ); |
---|
| 51 | ret = stat(libnamebuf, &sb); |
---|
| 52 | |
---|
| 53 | if (fp==NULL) |
---|
| 54 | { |
---|
| 55 | return LT_NOTFOUND; |
---|
| 56 | } |
---|
| 57 | if((sb.st_mode & S_IFMT) != S_IFREG) |
---|
| 58 | { |
---|
| 59 | goto lib_type_end; |
---|
| 60 | } |
---|
| 61 | if ((nbytes = fread((char *)buf, sizeof(char), BYTES_TO_CHECK, fp)) == -1) |
---|
| 62 | { |
---|
| 63 | goto lib_type_end; |
---|
| 64 | /*NOTREACHED*/ |
---|
| 65 | } |
---|
| 66 | if (nbytes == 0) |
---|
| 67 | goto lib_type_end; |
---|
| 68 | else |
---|
| 69 | { |
---|
| 70 | buf[nbytes++] = '\0'; /* null-terminate it */ |
---|
| 71 | } |
---|
| 72 | if( (strncmp(buf, "\177ELF", 4)==0)) /* generic ELF */ |
---|
| 73 | { |
---|
| 74 | LT = LT_ELF; |
---|
| 75 | //omFree(newlib); |
---|
| 76 | //newlib = omStrDup(libnamebuf); |
---|
| 77 | goto lib_type_end; |
---|
| 78 | } |
---|
[1ea9a7] | 79 | |
---|
[05b917] | 80 | if( (strncmp(buf, (const char *)mach_o, 4)==0) || (strncmp(buf, (const char *)mach_O, 4)==0)) /* generic Mach-O module */ |
---|
[1ea9a7] | 81 | { |
---|
| 82 | LT = LT_MACH_O; |
---|
| 83 | //omFree(newlib); |
---|
| 84 | //newlib = omStrDup(libnamebuf); |
---|
| 85 | goto lib_type_end; |
---|
| 86 | } |
---|
[27fc50] | 87 | |
---|
[6e81fc] | 88 | if( (strncmp(buf, (const char *)mach_o64, 4)==0) || (strncmp(buf, (const char *)mach_O64, 4)==0)) /* generic Mach-O 64-bit module */ |
---|
[27fc50] | 89 | { |
---|
| 90 | LT = LT_MACH_O; |
---|
| 91 | //omFree(newlib); |
---|
| 92 | //newlib = omStrDup(libnamebuf); |
---|
| 93 | goto lib_type_end; |
---|
| 94 | } |
---|
[6e81fc] | 95 | |
---|
[9ed245] | 96 | if( (strncmp(buf, "\02\020\01\016\05\022@", 7)==0)) |
---|
| 97 | { |
---|
| 98 | LT = LT_HPUX; |
---|
| 99 | //omFree(newlib); |
---|
| 100 | //newlib = omStrDup(libnamebuf); |
---|
| 101 | goto lib_type_end; |
---|
| 102 | } |
---|
| 103 | if(isprint(buf[0]) || buf[0]=='\n') |
---|
| 104 | { LT = LT_SINGULAR; goto lib_type_end; } |
---|
| 105 | |
---|
| 106 | lib_type_end: |
---|
| 107 | fclose(fp); |
---|
| 108 | return LT; |
---|
| 109 | } |
---|
| 110 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
| 111 | #if defined(HAVE_DL) |
---|
| 112 | |
---|
[35aab3] | 113 | /***************************************************************************** |
---|
| 114 | * |
---|
| 115 | * General section |
---|
[abaf13] | 116 | * These are just wrappers around the repsective dynl_* calls |
---|
[35aab3] | 117 | * which look for the binary in the bin_dir of Singular and ommit warnings if |
---|
| 118 | * somethings goes wrong |
---|
| 119 | * |
---|
| 120 | *****************************************************************************/ |
---|
| 121 | static BOOLEAN warn_handle = FALSE; |
---|
| 122 | static BOOLEAN warn_proc = FALSE; |
---|
| 123 | #ifndef DL_TAIL |
---|
[e71b67] | 124 | #ifdef NDEBUG |
---|
| 125 | #define DL_TAIL ".so" |
---|
| 126 | #else |
---|
| 127 | #define DL_TAIL "_g.so" |
---|
| 128 | #endif |
---|
[35aab3] | 129 | #endif |
---|
| 130 | |
---|
[9ef9d0] | 131 | void* dynl_open_binary_warn(const char* binary_name, const char* msg) |
---|
[35aab3] | 132 | { |
---|
| 133 | void* handle = NULL; |
---|
| 134 | const char* bin_dir = feGetResource('b'); |
---|
| 135 | if (bin_dir != NULL) |
---|
| 136 | { |
---|
[f9dc520] | 137 | const int binary_name_so_length = 3 + strlen(DL_TAIL) + strlen(binary_name) + strlen(DIR_SEPP) + strlen(bin_dir); |
---|
| 138 | char* binary_name_so = (char *)omAlloc0( binary_name_so_length * sizeof(char) ); |
---|
[e71b67] | 139 | snprintf(binary_name_so, binary_name_so_length, "%s%s%s%s", bin_dir, DIR_SEPP, binary_name, DL_TAIL); |
---|
[f9dc520] | 140 | handle = dynl_open(binary_name_so); |
---|
| 141 | omFreeSize((ADDRESS)binary_name_so, binary_name_so_length * sizeof(char) ); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | if (handle == NULL ) |
---|
| 145 | { |
---|
| 146 | const int binary_name_so_length = 3 + strlen(DL_TAIL) + strlen(binary_name); |
---|
| 147 | char* binary_name_so = (char *)omAlloc0( binary_name_so_length * sizeof(char) ); |
---|
[e71b67] | 148 | snprintf(binary_name_so, binary_name_so_length, "%s%s", binary_name, DL_TAIL); |
---|
[f9dc520] | 149 | |
---|
| 150 | char* pp = (char *)omAlloc0( MAXPATHLEN * sizeof(char) ); |
---|
| 151 | lib_types type = type_of_LIB( binary_name_so, pp ); |
---|
| 152 | omFreeSize((ADDRESS)binary_name_so, binary_name_so_length * sizeof(char) ); |
---|
| 153 | |
---|
[05b917] | 154 | if( type != LT_SINGULAR && type != LT_NONE && type != LT_NOTFOUND ) |
---|
[f9dc520] | 155 | handle = dynl_open(pp); |
---|
| 156 | |
---|
| 157 | omFreeSize((ADDRESS)pp, MAXPATHLEN * sizeof(char) ); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | if (handle == NULL && ! warn_handle) |
---|
| 161 | { |
---|
[e71b67] | 162 | Warn("Could not find dynamic library: %s%s", binary_name, DL_TAIL); |
---|
[35aab3] | 163 | Warn("Error message from system: %s", dynl_error()); |
---|
| 164 | if (msg != NULL) Warn("%s", msg); |
---|
| 165 | Warn("See the INSTALL section in the Singular manual for details."); |
---|
| 166 | warn_handle = TRUE; |
---|
| 167 | } |
---|
[f9dc520] | 168 | |
---|
[35aab3] | 169 | return handle; |
---|
| 170 | } |
---|
| 171 | |
---|
[85e68dd] | 172 | void* dynl_sym_warn(void* handle, const char* proc, const char* msg) |
---|
[35aab3] | 173 | { |
---|
| 174 | void *proc_ptr = NULL; |
---|
| 175 | if (handle != NULL) |
---|
| 176 | { |
---|
| 177 | proc_ptr = dynl_sym(handle, proc); |
---|
| 178 | if (proc_ptr == NULL && ! warn_proc) |
---|
| 179 | { |
---|
| 180 | Warn("Could load a procedure from a dynamic library"); |
---|
| 181 | Warn("Error message from system: %s", dynl_error()); |
---|
| 182 | if (msg != NULL) Warn("%s", msg); |
---|
| 183 | Warn("See the INSTALL section in the Singular manual for details."); |
---|
| 184 | warn_proc = TRUE; |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | return proc_ptr; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | #ifdef __cplusplus |
---|
| 191 | extern "C" { |
---|
| 192 | #endif |
---|
| 193 | |
---|
| 194 | /***************************************************************************** |
---|
[fc300f] | 195 | * SECTION generic ELF: ix86-linux / alpha-linux / IA64-linux /x86_64_Linux * |
---|
[4287a0] | 196 | * SunOS-5 / IRIX-6 / ppcMac-Darwin / FreeeBSD * |
---|
[35aab3] | 197 | *****************************************************************************/ |
---|
[f02645b] | 198 | // relying on gcc to define __ELF__, check with cpp -dM /dev/null |
---|
[9b3de5] | 199 | // Mac OsX is an ELF system, but does not define __ELF__ |
---|
[c3ab214] | 200 | // Solaris is an ELF system, but does not define __ELF__ |
---|
[d44ced] | 201 | #if defined(__ELF__) |
---|
[fde597] | 202 | #define HAVE_ELF_SYSTEM |
---|
[9b3de5] | 203 | #endif |
---|
| 204 | |
---|
[0fd6d8] | 205 | #if defined(ppcMac_darwin) |
---|
| 206 | #define HAVE_ELF_SYSTEM |
---|
| 207 | #endif |
---|
| 208 | |
---|
[d142260] | 209 | #if defined(__APPLE__) && defined(__MACH__) |
---|
| 210 | #define HAVE_ELF_SYSTEM |
---|
| 211 | #endif |
---|
[01d9463] | 212 | |
---|
[c3ab214] | 213 | #if defined(SunOS_5) |
---|
| 214 | #define HAVE_ELF_SYSTEM |
---|
[fde597] | 215 | #endif |
---|
| 216 | |
---|
[35aab3] | 217 | #if defined(HAVE_ELF_SYSTEM) |
---|
| 218 | #include <dlfcn.h> |
---|
[3dbe0bf] | 219 | #define DL_IMPLEMENTED |
---|
[35aab3] | 220 | |
---|
| 221 | static void* kernel_handle = NULL; |
---|
| 222 | void *dynl_open( |
---|
| 223 | char *filename /* I: filename to load */ |
---|
| 224 | ) |
---|
| 225 | { |
---|
[a513d10] | 226 | // glibc 2.2: |
---|
[a11e71] | 227 | if ((filename==NULL) || (dlopen(filename,RTLD_NOW|RTLD_NOLOAD)==NULL)) |
---|
[a513d10] | 228 | return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL)); |
---|
| 229 | else |
---|
| 230 | Werror("module %s already loaded",filename); |
---|
| 231 | return NULL; |
---|
[3dbe0bf] | 232 | // alternative |
---|
[a513d10] | 233 | // return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL)); |
---|
[35aab3] | 234 | } |
---|
| 235 | |
---|
[85e68dd] | 236 | void *dynl_sym(void *handle, const char *symbol) |
---|
[35aab3] | 237 | { |
---|
| 238 | if (handle == DYNL_KERNEL_HANDLE) |
---|
| 239 | { |
---|
| 240 | if (kernel_handle == NULL) |
---|
| 241 | kernel_handle = dynl_open(NULL); |
---|
| 242 | handle = kernel_handle; |
---|
| 243 | } |
---|
| 244 | return(dlsym(handle, symbol)); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | int dynl_close (void *handle) |
---|
| 248 | { |
---|
| 249 | return(dlclose (handle)); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | const char *dynl_error() |
---|
| 253 | { |
---|
| 254 | return(dlerror()); |
---|
| 255 | } |
---|
[fc300f] | 256 | #endif /* ELF_SYSTEM */ |
---|
[35aab3] | 257 | |
---|
| 258 | /***************************************************************************** |
---|
| 259 | * SECTION HPUX-9/10 * |
---|
| 260 | *****************************************************************************/ |
---|
| 261 | #if defined(HPUX_9) || defined(HPUX_10) |
---|
[3dbe0bf] | 262 | #define DL_IMPLEMENTED |
---|
[35aab3] | 263 | #include <dl.h> |
---|
| 264 | |
---|
| 265 | typedef char *((*func_ptr) ()); |
---|
| 266 | |
---|
| 267 | void *dynl_open(char *filename) |
---|
| 268 | { |
---|
| 269 | shl_t handle = shl_load(filename, BIND_DEFERRED, 0); |
---|
| 270 | |
---|
| 271 | return ((void *) handle); |
---|
| 272 | } |
---|
| 273 | |
---|
[85e68dd] | 274 | void *dynl_sym(void *handle, const char *symbol) |
---|
[35aab3] | 275 | { |
---|
| 276 | func_ptr f; |
---|
[abaf13] | 277 | |
---|
[35aab3] | 278 | if (handle == DYNL_KERNEL_HANDLE) |
---|
| 279 | handle = PROG_HANDLE; |
---|
[abaf13] | 280 | |
---|
[35aab3] | 281 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_PROCEDURE, &f) == -1) |
---|
| 282 | { |
---|
| 283 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_UNDEFINED, &f) == -1) |
---|
| 284 | { |
---|
| 285 | f = (func_ptr) NULL; |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | return ((void *)f); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | int dynl_close (void *handle) |
---|
| 292 | { |
---|
| 293 | shl_unload((shl_t) handle); |
---|
| 294 | return(0); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | const char *dynl_error() |
---|
| 298 | { |
---|
| 299 | static char errmsg[] = "shl_load failed"; |
---|
| 300 | |
---|
| 301 | return errmsg; |
---|
| 302 | } |
---|
| 303 | #endif /* HPUX_9 or HPUX_10 */ |
---|
| 304 | |
---|
| 305 | /***************************************************************************** |
---|
[fc300f] | 306 | * SECTION generic: dynamic madules not available |
---|
[35aab3] | 307 | *****************************************************************************/ |
---|
[3dbe0bf] | 308 | #ifndef DL_IMPLEMENTED |
---|
[35aab3] | 309 | |
---|
| 310 | void *dynl_open(char *filename) |
---|
| 311 | { |
---|
| 312 | return(NULL); |
---|
| 313 | } |
---|
| 314 | |
---|
[85e68dd] | 315 | void *dynl_sym(void *handle, const char *symbol) |
---|
[35aab3] | 316 | { |
---|
| 317 | return(NULL); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | int dynl_close (void *handle) |
---|
| 321 | { |
---|
| 322 | return(0); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | const char *dynl_error() |
---|
| 326 | { |
---|
| 327 | static char errmsg[] = "support for dynamic loading not implemented"; |
---|
| 328 | return errmsg; |
---|
| 329 | } |
---|
[fc300f] | 330 | #endif |
---|
[35aab3] | 331 | |
---|
| 332 | #ifdef __cplusplus |
---|
| 333 | } |
---|
| 334 | #endif |
---|
| 335 | |
---|
| 336 | #endif /* HAVE_DL */ |
---|