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 <kernel/mod2.h> |
---|
21 | #include <Singular/static.h> |
---|
22 | |
---|
23 | #include <kernel/mod_raw.h> |
---|
24 | #include <kernel/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 | // relying on gcc to define __ELF__, check with cpp -dM /dev/null |
---|
158 | // Mac OsX is an ELF system, but does not define __ELF__ |
---|
159 | // Solaris is an ELF system, but does not define __ELF__ |
---|
160 | #if defined(__ELF__) |
---|
161 | #define HAVE_ELF_SYSTEM |
---|
162 | #endif |
---|
163 | |
---|
164 | #if defined(ix86Mac_darwin) |
---|
165 | #define HAVE_ELF_SYSTEM |
---|
166 | #endif |
---|
167 | |
---|
168 | #if defined(x86_64Mac_darwin) |
---|
169 | #define HAVE_ELF_SYSTEM |
---|
170 | #endif |
---|
171 | |
---|
172 | #if defined(SunOS_5) |
---|
173 | #define HAVE_ELF_SYSTEM |
---|
174 | #endif |
---|
175 | |
---|
176 | #if defined(HAVE_ELF_SYSTEM) |
---|
177 | #include <dlfcn.h> |
---|
178 | |
---|
179 | static void* kernel_handle = NULL; |
---|
180 | void *dynl_open( |
---|
181 | char *filename /* I: filename to load */ |
---|
182 | ) |
---|
183 | { |
---|
184 | return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL)); |
---|
185 | } |
---|
186 | |
---|
187 | void *dynl_sym(void *handle, const char *symbol) |
---|
188 | { |
---|
189 | if (handle == DYNL_KERNEL_HANDLE) |
---|
190 | { |
---|
191 | if (kernel_handle == NULL) |
---|
192 | kernel_handle = dynl_open(NULL); |
---|
193 | handle = kernel_handle; |
---|
194 | } |
---|
195 | return(dlsym(handle, symbol)); |
---|
196 | } |
---|
197 | |
---|
198 | int dynl_close (void *handle) |
---|
199 | { |
---|
200 | return(dlclose (handle)); |
---|
201 | } |
---|
202 | |
---|
203 | const char *dynl_error() |
---|
204 | { |
---|
205 | return(dlerror()); |
---|
206 | } |
---|
207 | #endif /* ELF_SYSTEM */ |
---|
208 | |
---|
209 | /***************************************************************************** |
---|
210 | * SECTION HPUX-9/10 * |
---|
211 | *****************************************************************************/ |
---|
212 | #if defined(HPUX_9) || defined(HPUX_10) |
---|
213 | #include <dl.h> |
---|
214 | |
---|
215 | typedef char *((*func_ptr) ()); |
---|
216 | |
---|
217 | void *dynl_open(char *filename) |
---|
218 | { |
---|
219 | shl_t handle = shl_load(filename, BIND_DEFERRED, 0); |
---|
220 | |
---|
221 | return ((void *) handle); |
---|
222 | } |
---|
223 | |
---|
224 | void *dynl_sym(void *handle, const char *symbol) |
---|
225 | { |
---|
226 | func_ptr f; |
---|
227 | |
---|
228 | if (handle == DYNL_KERNEL_HANDLE) |
---|
229 | handle = PROG_HANDLE; |
---|
230 | |
---|
231 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_PROCEDURE, &f) == -1) |
---|
232 | { |
---|
233 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_UNDEFINED, &f) == -1) |
---|
234 | { |
---|
235 | f = (func_ptr) NULL; |
---|
236 | } |
---|
237 | } |
---|
238 | return ((void *)f); |
---|
239 | } |
---|
240 | |
---|
241 | int dynl_close (void *handle) |
---|
242 | { |
---|
243 | shl_unload((shl_t) handle); |
---|
244 | return(0); |
---|
245 | } |
---|
246 | |
---|
247 | const char *dynl_error() |
---|
248 | { |
---|
249 | static char errmsg[] = "shl_load failed"; |
---|
250 | |
---|
251 | return errmsg; |
---|
252 | } |
---|
253 | #endif /* HPUX_9 or HPUX_10 */ |
---|
254 | |
---|
255 | /***************************************************************************** |
---|
256 | * SECTION AIX-4 * |
---|
257 | *****************************************************************************/ |
---|
258 | #ifdef AIX_4 |
---|
259 | #define DL_NOT_IMPLEMENTED |
---|
260 | #endif |
---|
261 | |
---|
262 | /***************************************************************************** |
---|
263 | * SECTION Sun3OS-4 * |
---|
264 | *****************************************************************************/ |
---|
265 | #ifdef Sun3OS_4 |
---|
266 | #define DL_NOT_IMPLEMENTED |
---|
267 | #endif |
---|
268 | |
---|
269 | /***************************************************************************** |
---|
270 | * SECTION SunOS-4 * |
---|
271 | *****************************************************************************/ |
---|
272 | #if defined(SunOS_4) |
---|
273 | #define DL_NOT_IMPLEMENTED |
---|
274 | #endif |
---|
275 | |
---|
276 | /***************************************************************************** |
---|
277 | * SECTION generic: dynamic madules not available |
---|
278 | *****************************************************************************/ |
---|
279 | #ifdef DL_NOT_IMPLEMEMENTED |
---|
280 | |
---|
281 | void *dynl_open(char *filename) |
---|
282 | { |
---|
283 | return(NULL); |
---|
284 | } |
---|
285 | |
---|
286 | void *dynl_sym(void *handle, const char *symbol) |
---|
287 | { |
---|
288 | return(NULL); |
---|
289 | } |
---|
290 | |
---|
291 | int dynl_close (void *handle) |
---|
292 | { |
---|
293 | return(0); |
---|
294 | } |
---|
295 | |
---|
296 | const char *dynl_error() |
---|
297 | { |
---|
298 | static char errmsg[] = "support for dynamic loading not implemented"; |
---|
299 | return errmsg; |
---|
300 | } |
---|
301 | #endif |
---|
302 | |
---|
303 | #ifdef __cplusplus |
---|
304 | } |
---|
305 | #endif |
---|
306 | |
---|
307 | #endif /* HAVE_DL */ |
---|