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