1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: machine depend code for dynamic modules |
---|
6 | * |
---|
7 | * Provides: dynl_check_opened() |
---|
8 | * 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 | #include <errno.h> |
---|
19 | #include <unistd.h> |
---|
20 | |
---|
21 | #include "misc/auxiliary.h" |
---|
22 | #include "reporter/reporter.h" |
---|
23 | #include "resources/feResource.h" |
---|
24 | #include "omalloc/omalloc.h" |
---|
25 | #include "mod_raw.h" |
---|
26 | |
---|
27 | #ifdef HAVE_STATIC |
---|
28 | #undef HAVE_DL |
---|
29 | #endif |
---|
30 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
31 | #if defined(HAVE_DL) |
---|
32 | |
---|
33 | /***************************************************************************** |
---|
34 | * |
---|
35 | * General section |
---|
36 | * These are just wrappers around the repsective dynl_* calls |
---|
37 | * which look for the binary in the bin_dir of Singular and ommit warnings if |
---|
38 | * somethings goes wrong |
---|
39 | * |
---|
40 | *****************************************************************************/ |
---|
41 | static BOOLEAN warn_handle = FALSE; |
---|
42 | static BOOLEAN warn_proc = FALSE; |
---|
43 | #ifndef DL_TAIL |
---|
44 | #define DL_TAIL ".so" |
---|
45 | //#define DL_TAIL "_g.so" |
---|
46 | #endif |
---|
47 | |
---|
48 | void* dynl_open_binary_warn(const char* binary_name, const char* msg) |
---|
49 | { |
---|
50 | void* handle = NULL; |
---|
51 | char* binary_name_so=NULL; |
---|
52 | BOOLEAN found=FALSE; |
---|
53 | |
---|
54 | // try P_PROCS_DIR (%P) |
---|
55 | char* proc_path = feGetResource('P'); |
---|
56 | if (proc_path != NULL) |
---|
57 | { |
---|
58 | char *p; |
---|
59 | char *q; |
---|
60 | p=proc_path; |
---|
61 | int binary_name_so_length = 3 + strlen(DL_TAIL) + strlen(binary_name) + strlen(DIR_SEPP) + strlen(proc_path); |
---|
62 | binary_name_so = (char *)omAlloc0( binary_name_so_length * sizeof(char) ); |
---|
63 | while((p!=NULL)&&(*p!='\0')) |
---|
64 | { |
---|
65 | q=strchr(p,fePathSep); |
---|
66 | if (q!=NULL) *q='\0'; |
---|
67 | strcpy(binary_name_so,p); |
---|
68 | if (q!=NULL) *q=fePathSep; |
---|
69 | strcat(binary_name_so,DIR_SEPP); |
---|
70 | strcat(binary_name_so,binary_name); |
---|
71 | strcat(binary_name_so,DL_TAIL); |
---|
72 | if(!access(binary_name_so, R_OK)) { found=TRUE; break; } |
---|
73 | if (q!=NULL) p=q+1; else p=NULL; |
---|
74 | } |
---|
75 | if (found) handle = dynl_open(binary_name_so); |
---|
76 | } |
---|
77 | |
---|
78 | if (handle == NULL && ! warn_handle) |
---|
79 | { |
---|
80 | Warn("Could not find dynamic library: %s%s (path %s)", |
---|
81 | binary_name, DL_TAIL,proc_path); |
---|
82 | if (found) Warn("Error message from system: %s", dynl_error()); |
---|
83 | if (msg != NULL) Warn("%s", msg); |
---|
84 | WarnS("See the INSTALL section in the Singular manual for details."); |
---|
85 | warn_handle = TRUE; |
---|
86 | } |
---|
87 | omfree((ADDRESS)binary_name_so ); |
---|
88 | |
---|
89 | return handle; |
---|
90 | } |
---|
91 | |
---|
92 | void* dynl_sym_warn(void* handle, const char* proc, const char* msg) |
---|
93 | { |
---|
94 | void *proc_ptr = NULL; |
---|
95 | if (handle != NULL) |
---|
96 | { |
---|
97 | proc_ptr = dynl_sym(handle, proc); |
---|
98 | if (proc_ptr == NULL && ! warn_proc) |
---|
99 | { |
---|
100 | WarnS("Could load a procedure from a dynamic library"); |
---|
101 | Warn("Error message from system: %s", dynl_error()); |
---|
102 | if (msg != NULL) Warn("%s", msg); |
---|
103 | WarnS("See the INSTALL section in the Singular manual for details."); |
---|
104 | warn_proc = TRUE; |
---|
105 | } |
---|
106 | } |
---|
107 | return proc_ptr; |
---|
108 | } |
---|
109 | |
---|
110 | #ifdef __cplusplus |
---|
111 | extern "C" { |
---|
112 | #endif |
---|
113 | |
---|
114 | /***************************************************************************** |
---|
115 | * SECTION generic ELF: ix86-linux / alpha-linux / IA64-linux /x86_64_Linux * |
---|
116 | * SunOS-5 / IRIX-6 / ppcMac-Darwin / FreeeBSD * |
---|
117 | *****************************************************************************/ |
---|
118 | // relying on gcc to define __ELF__, check with cpp -dM /dev/null |
---|
119 | #if defined(__ELF__) |
---|
120 | #define HAVE_ELF_SYSTEM |
---|
121 | #endif |
---|
122 | |
---|
123 | // Mac OsX is an ELF system, but does not define __ELF__ |
---|
124 | #if (defined(__APPLE__) && defined(__MACH__)) && (!defined(HAVE_ELF_SYSTEM)) |
---|
125 | #define HAVE_ELF_SYSTEM |
---|
126 | #endif |
---|
127 | |
---|
128 | // Solaris is an ELF system, but does not define __ELF__ |
---|
129 | #if defined(__sun) && defined(__SVR4) |
---|
130 | #define HAVE_ELF_SYSTEM |
---|
131 | #endif |
---|
132 | |
---|
133 | #if defined(HAVE_ELF_SYSTEM) |
---|
134 | #include <dlfcn.h> |
---|
135 | #define DL_IMPLEMENTED |
---|
136 | |
---|
137 | static void* kernel_handle = NULL; |
---|
138 | int dynl_check_opened( |
---|
139 | char *filename /* I: filename to check */ |
---|
140 | ) |
---|
141 | { |
---|
142 | return dlopen(filename,RTLD_NOW|RTLD_NOLOAD) != NULL; |
---|
143 | } |
---|
144 | |
---|
145 | void *dynl_open( |
---|
146 | char *filename /* I: filename to load */ |
---|
147 | ) |
---|
148 | { |
---|
149 | // glibc 2.2: |
---|
150 | if ((filename==NULL) || (dlopen(filename,RTLD_NOW|RTLD_NOLOAD)==NULL)) |
---|
151 | return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL)); |
---|
152 | else |
---|
153 | Werror("module %s already loaded",filename); |
---|
154 | return NULL; |
---|
155 | // alternative |
---|
156 | // return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL)); |
---|
157 | } |
---|
158 | |
---|
159 | void *dynl_sym(void *handle, const char *symbol) |
---|
160 | { |
---|
161 | if (handle == DYNL_KERNEL_HANDLE) |
---|
162 | { |
---|
163 | if (kernel_handle == NULL) |
---|
164 | kernel_handle = dynl_open(NULL); |
---|
165 | handle = kernel_handle; |
---|
166 | } |
---|
167 | return(dlsym(handle, symbol)); |
---|
168 | } |
---|
169 | |
---|
170 | int dynl_close (void *handle) |
---|
171 | { |
---|
172 | return(dlclose (handle)); |
---|
173 | } |
---|
174 | |
---|
175 | const char *dynl_error() |
---|
176 | { |
---|
177 | return(dlerror()); |
---|
178 | } |
---|
179 | #endif /* ELF_SYSTEM */ |
---|
180 | |
---|
181 | /***************************************************************************** |
---|
182 | * SECTION HPUX-9/10 * |
---|
183 | *****************************************************************************/ |
---|
184 | #if defined(HPUX_9) || defined(HPUX_10) |
---|
185 | #define DL_IMPLEMENTED |
---|
186 | #include <dl.h> |
---|
187 | |
---|
188 | typedef char *((*func_ptr) ()); |
---|
189 | |
---|
190 | int dynl_check_opened( /* NOTE: untested */ |
---|
191 | char *filename /* I: filename to check */ |
---|
192 | ) |
---|
193 | { |
---|
194 | struct shl_descriptor *desc; |
---|
195 | for (int idx = 0; shl_get(idx, &desc) != -1; ++idx) |
---|
196 | { |
---|
197 | if (strcmp(filename, desc->filename) == 0) return TRUE; |
---|
198 | } |
---|
199 | return FALSE; |
---|
200 | } |
---|
201 | |
---|
202 | void *dynl_open(char *filename) |
---|
203 | { |
---|
204 | shl_t handle = shl_load(filename, BIND_DEFERRED, 0); |
---|
205 | |
---|
206 | return ((void *) handle); |
---|
207 | } |
---|
208 | |
---|
209 | void *dynl_sym(void *handle, const char *symbol) |
---|
210 | { |
---|
211 | func_ptr f; |
---|
212 | |
---|
213 | if (handle == DYNL_KERNEL_HANDLE) |
---|
214 | handle = PROG_HANDLE; |
---|
215 | |
---|
216 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_PROCEDURE, &f) == -1) |
---|
217 | { |
---|
218 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_UNDEFINED, &f) == -1) |
---|
219 | { |
---|
220 | f = (func_ptr) NULL; |
---|
221 | } |
---|
222 | } |
---|
223 | return ((void *)f); |
---|
224 | } |
---|
225 | |
---|
226 | int dynl_close (void *handle) |
---|
227 | { |
---|
228 | shl_unload((shl_t) handle); |
---|
229 | return(0); |
---|
230 | } |
---|
231 | |
---|
232 | const char *dynl_error() |
---|
233 | { |
---|
234 | static char errmsg[] = "shl_load failed"; |
---|
235 | |
---|
236 | return errmsg; |
---|
237 | } |
---|
238 | #endif /* HPUX_9 or HPUX_10 */ |
---|
239 | |
---|
240 | /***************************************************************************** |
---|
241 | * SECTION generic: dynamic madules not available |
---|
242 | *****************************************************************************/ |
---|
243 | #ifndef DL_IMPLEMENTED |
---|
244 | |
---|
245 | int dynl_check_opened(char *filename) |
---|
246 | { |
---|
247 | return FALSE; |
---|
248 | } |
---|
249 | |
---|
250 | void *dynl_open(char *filename) |
---|
251 | { |
---|
252 | return(NULL); |
---|
253 | } |
---|
254 | |
---|
255 | void *dynl_sym(void *handle, const char *symbol) |
---|
256 | { |
---|
257 | return(NULL); |
---|
258 | } |
---|
259 | |
---|
260 | int dynl_close (void *handle) |
---|
261 | { |
---|
262 | return(0); |
---|
263 | } |
---|
264 | |
---|
265 | const char *dynl_error() |
---|
266 | { |
---|
267 | static char errmsg[] = "support for dynamic loading not implemented"; |
---|
268 | return errmsg; |
---|
269 | } |
---|
270 | #endif |
---|
271 | |
---|
272 | #ifdef __cplusplus |
---|
273 | } |
---|
274 | #endif |
---|
275 | |
---|
276 | #endif /* HAVE_DL */ |
---|