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