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