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 "mod2.h" |
---|
21 | #include "../Singular/static.h" |
---|
22 | |
---|
23 | #include "mod_raw.h" |
---|
24 | #include "febase.h" |
---|
25 | |
---|
26 | #ifdef HAVE_STATIC |
---|
27 | #undef HAVE_DL |
---|
28 | #endif |
---|
29 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
30 | #define BYTES_TO_CHECK 7 |
---|
31 | |
---|
32 | lib_types type_of_LIB(char *newlib, char *libnamebuf) |
---|
33 | { |
---|
34 | const char mach_o[]={0xfe,0xed,0xfa,0xce,0}; |
---|
35 | char buf[BYTES_TO_CHECK+1]; /* one extra for terminating '\0' */ |
---|
36 | struct stat sb; |
---|
37 | int nbytes = 0; |
---|
38 | int ret; |
---|
39 | lib_types LT=LT_NONE; |
---|
40 | |
---|
41 | FILE * fp = feFopen( newlib, "r", libnamebuf, FALSE ); |
---|
42 | ret = stat(libnamebuf, &sb); |
---|
43 | |
---|
44 | if (fp==NULL) |
---|
45 | { |
---|
46 | return LT_NOTFOUND; |
---|
47 | } |
---|
48 | if((sb.st_mode & S_IFMT) != S_IFREG) |
---|
49 | { |
---|
50 | goto lib_type_end; |
---|
51 | } |
---|
52 | if ((nbytes = fread((char *)buf, sizeof(char), BYTES_TO_CHECK, fp)) == -1) |
---|
53 | { |
---|
54 | goto lib_type_end; |
---|
55 | /*NOTREACHED*/ |
---|
56 | } |
---|
57 | if (nbytes == 0) |
---|
58 | goto lib_type_end; |
---|
59 | else |
---|
60 | { |
---|
61 | buf[nbytes++] = '\0'; /* null-terminate it */ |
---|
62 | } |
---|
63 | if( (strncmp(buf, "\177ELF", 4)==0)) /* generic ELF */ |
---|
64 | { |
---|
65 | LT = LT_ELF; |
---|
66 | //omFree(newlib); |
---|
67 | //newlib = omStrDup(libnamebuf); |
---|
68 | goto lib_type_end; |
---|
69 | } |
---|
70 | |
---|
71 | if( (strncmp(buf, &mach_o[0], 4)==0)) /* generic Mach-O module */ |
---|
72 | { |
---|
73 | LT = LT_MACH_O; |
---|
74 | //omFree(newlib); |
---|
75 | //newlib = omStrDup(libnamebuf); |
---|
76 | goto lib_type_end; |
---|
77 | } |
---|
78 | if( (strncmp(buf, "\02\020\01\016\05\022@", 7)==0)) |
---|
79 | { |
---|
80 | LT = LT_HPUX; |
---|
81 | //omFree(newlib); |
---|
82 | //newlib = omStrDup(libnamebuf); |
---|
83 | goto lib_type_end; |
---|
84 | } |
---|
85 | if(isprint(buf[0]) || buf[0]=='\n') |
---|
86 | { LT = LT_SINGULAR; goto lib_type_end; } |
---|
87 | |
---|
88 | lib_type_end: |
---|
89 | fclose(fp); |
---|
90 | return LT; |
---|
91 | } |
---|
92 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
93 | #if defined(HAVE_DL) |
---|
94 | |
---|
95 | /***************************************************************************** |
---|
96 | * |
---|
97 | * General section |
---|
98 | * These are just wrappers around the repsective dynl_* calls |
---|
99 | * which look for the binary in the bin_dir of Singular and ommit warnings if |
---|
100 | * somethings goes wrong |
---|
101 | * |
---|
102 | *****************************************************************************/ |
---|
103 | static BOOLEAN warn_handle = FALSE; |
---|
104 | static BOOLEAN warn_proc = FALSE; |
---|
105 | #ifndef DL_TAIL |
---|
106 | #define DL_TAIL "so" |
---|
107 | #endif |
---|
108 | |
---|
109 | void* dynl_open_binary_warn(const char* binary_name, const char* msg) |
---|
110 | { |
---|
111 | void* handle = NULL; |
---|
112 | const char* bin_dir = feGetResource('b'); |
---|
113 | if (bin_dir != NULL) |
---|
114 | { |
---|
115 | char path_name[MAXPATHLEN]; |
---|
116 | sprintf(path_name, "%s%s%s.%s", bin_dir, DIR_SEPP, binary_name, |
---|
117 | DL_TAIL); |
---|
118 | handle = dynl_open(path_name); |
---|
119 | if (handle == NULL && ! warn_handle) |
---|
120 | { |
---|
121 | Warn("Could not open dynamic library: %s", path_name); |
---|
122 | Warn("Error message from system: %s", dynl_error()); |
---|
123 | if (msg != NULL) Warn("%s", msg); |
---|
124 | Warn("See the INSTALL section in the Singular manual for details."); |
---|
125 | warn_handle = TRUE; |
---|
126 | } |
---|
127 | } |
---|
128 | return handle; |
---|
129 | } |
---|
130 | |
---|
131 | void* dynl_sym_warn(void* handle, const char* proc, const char* msg) |
---|
132 | { |
---|
133 | void *proc_ptr = NULL; |
---|
134 | if (handle != NULL) |
---|
135 | { |
---|
136 | proc_ptr = dynl_sym(handle, proc); |
---|
137 | if (proc_ptr == NULL && ! warn_proc) |
---|
138 | { |
---|
139 | Warn("Could load a procedure from a dynamic library"); |
---|
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_proc = TRUE; |
---|
144 | } |
---|
145 | } |
---|
146 | return proc_ptr; |
---|
147 | } |
---|
148 | |
---|
149 | #ifdef __cplusplus |
---|
150 | extern "C" { |
---|
151 | #endif |
---|
152 | |
---|
153 | /***************************************************************************** |
---|
154 | * SECTION generic ELF: ix86-linux / alpha-linux / IA64-linux /x86_64_Linux * |
---|
155 | * SunOS-5 / IRIX-6 / ppcMac-Darwin / FreeeBSD * |
---|
156 | *****************************************************************************/ |
---|
157 | #if defined(ix86_Linux) |
---|
158 | #define HAVE_ELF_SYSTEM |
---|
159 | #endif |
---|
160 | |
---|
161 | #if defined(ix86_Linux_libc5) |
---|
162 | #define HAVE_ELF_SYSTEM |
---|
163 | #endif |
---|
164 | |
---|
165 | #if defined(DecAlpha_Linux) |
---|
166 | #define HAVE_ELF_SYSTEM |
---|
167 | #endif |
---|
168 | |
---|
169 | #if defined(IA64_Linux) |
---|
170 | #define HAVE_ELF_SYSTEM |
---|
171 | #endif |
---|
172 | |
---|
173 | #if defined(x86_64_Linux) |
---|
174 | #define HAVE_ELF_SYSTEM |
---|
175 | #endif |
---|
176 | |
---|
177 | #if defined(ppc_Linux) |
---|
178 | #define HAVE_ELF_SYSTEM |
---|
179 | #endif |
---|
180 | |
---|
181 | #if defined(hppa_Linux) |
---|
182 | #define HAVE_ELF_SYSTEM |
---|
183 | #endif |
---|
184 | |
---|
185 | #if defined(SunOS_5) |
---|
186 | #define HAVE_ELF_SYSTEM |
---|
187 | #endif |
---|
188 | |
---|
189 | #ifdef IRIX_6 |
---|
190 | #define HAVE_ELF_SYSTEM |
---|
191 | #endif |
---|
192 | |
---|
193 | #ifdef ppcMac_darwin |
---|
194 | #define HAVE_ELF_SYSTEM |
---|
195 | #endif |
---|
196 | |
---|
197 | #ifdef ix86Mac_darwin |
---|
198 | #define HAVE_ELF_SYSTEM |
---|
199 | #endif |
---|
200 | |
---|
201 | #ifdef ix86_freebsd |
---|
202 | #define HAVE_ELF_SYSTEM |
---|
203 | #endif |
---|
204 | |
---|
205 | #ifdef sparc64_Linux |
---|
206 | #define HAVE_ELF_SYSTEM |
---|
207 | #endif |
---|
208 | |
---|
209 | #if defined(HAVE_ELF_SYSTEM) |
---|
210 | #include <dlfcn.h> |
---|
211 | |
---|
212 | static void* kernel_handle = NULL; |
---|
213 | void *dynl_open( |
---|
214 | char *filename /* I: filename to load */ |
---|
215 | ) |
---|
216 | { |
---|
217 | return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL)); |
---|
218 | } |
---|
219 | |
---|
220 | void *dynl_sym(void *handle, const char *symbol) |
---|
221 | { |
---|
222 | if (handle == DYNL_KERNEL_HANDLE) |
---|
223 | { |
---|
224 | if (kernel_handle == NULL) |
---|
225 | kernel_handle = dynl_open(NULL); |
---|
226 | handle = kernel_handle; |
---|
227 | } |
---|
228 | return(dlsym(handle, symbol)); |
---|
229 | } |
---|
230 | |
---|
231 | int dynl_close (void *handle) |
---|
232 | { |
---|
233 | return(dlclose (handle)); |
---|
234 | } |
---|
235 | |
---|
236 | const char *dynl_error() |
---|
237 | { |
---|
238 | return(dlerror()); |
---|
239 | } |
---|
240 | #endif /* ELF_SYSTEM */ |
---|
241 | |
---|
242 | /***************************************************************************** |
---|
243 | * SECTION HPUX-9/10 * |
---|
244 | *****************************************************************************/ |
---|
245 | #if defined(HPUX_9) || defined(HPUX_10) |
---|
246 | #include <dl.h> |
---|
247 | |
---|
248 | typedef char *((*func_ptr) ()); |
---|
249 | |
---|
250 | void *dynl_open(char *filename) |
---|
251 | { |
---|
252 | shl_t handle = shl_load(filename, BIND_DEFERRED, 0); |
---|
253 | |
---|
254 | return ((void *) handle); |
---|
255 | } |
---|
256 | |
---|
257 | void *dynl_sym(void *handle, const char *symbol) |
---|
258 | { |
---|
259 | func_ptr f; |
---|
260 | |
---|
261 | if (handle == DYNL_KERNEL_HANDLE) |
---|
262 | handle = PROG_HANDLE; |
---|
263 | |
---|
264 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_PROCEDURE, &f) == -1) |
---|
265 | { |
---|
266 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_UNDEFINED, &f) == -1) |
---|
267 | { |
---|
268 | f = (func_ptr) NULL; |
---|
269 | } |
---|
270 | } |
---|
271 | return ((void *)f); |
---|
272 | } |
---|
273 | |
---|
274 | int dynl_close (void *handle) |
---|
275 | { |
---|
276 | shl_unload((shl_t) handle); |
---|
277 | return(0); |
---|
278 | } |
---|
279 | |
---|
280 | const char *dynl_error() |
---|
281 | { |
---|
282 | static char errmsg[] = "shl_load failed"; |
---|
283 | |
---|
284 | return errmsg; |
---|
285 | } |
---|
286 | #endif /* HPUX_9 or HPUX_10 */ |
---|
287 | |
---|
288 | /***************************************************************************** |
---|
289 | * SECTION AIX-4 * |
---|
290 | *****************************************************************************/ |
---|
291 | #ifdef AIX_4 |
---|
292 | #define DL_NOT_IMPLEMENTED |
---|
293 | #endif |
---|
294 | |
---|
295 | /***************************************************************************** |
---|
296 | * SECTION Sun3OS-4 * |
---|
297 | *****************************************************************************/ |
---|
298 | #ifdef Sun3OS_4 |
---|
299 | #define DL_NOT_IMPLEMENTED |
---|
300 | #endif |
---|
301 | |
---|
302 | /***************************************************************************** |
---|
303 | * SECTION SunOS-4 * |
---|
304 | *****************************************************************************/ |
---|
305 | #if defined(SunOS_4) |
---|
306 | #define DL_NOT_IMPLEMENTED |
---|
307 | #endif |
---|
308 | |
---|
309 | /***************************************************************************** |
---|
310 | * SECTION generic: dynamic madules not available |
---|
311 | *****************************************************************************/ |
---|
312 | #ifdef DL_NOT_IMPLEMEMENTED |
---|
313 | |
---|
314 | void *dynl_open(char *filename) |
---|
315 | { |
---|
316 | return(NULL); |
---|
317 | } |
---|
318 | |
---|
319 | void *dynl_sym(void *handle, const char *symbol) |
---|
320 | { |
---|
321 | return(NULL); |
---|
322 | } |
---|
323 | |
---|
324 | int dynl_close (void *handle) |
---|
325 | { |
---|
326 | return(0); |
---|
327 | } |
---|
328 | |
---|
329 | const char *dynl_error() |
---|
330 | { |
---|
331 | static char errmsg[] = "support for dynamic loading not implemented"; |
---|
332 | return errmsg; |
---|
333 | } |
---|
334 | #endif |
---|
335 | |
---|
336 | #ifdef __cplusplus |
---|
337 | } |
---|
338 | #endif |
---|
339 | |
---|
340 | #endif /* HAVE_DL */ |
---|