1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: mod_raw.cc,v 1.4 2005-05-09 16:42:02 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 | |
---|
16 | #include "mod2.h" |
---|
17 | #include "static.h" |
---|
18 | #ifdef HAVE_STATIC |
---|
19 | #undef HAVE_DL |
---|
20 | #endif |
---|
21 | |
---|
22 | #if defined(HAVE_DL) |
---|
23 | #include "mod_raw.h" |
---|
24 | #include "febase.h" |
---|
25 | |
---|
26 | /***************************************************************************** |
---|
27 | * |
---|
28 | * General section |
---|
29 | * These are just wrappers around the repsective dynl_* calls |
---|
30 | * which look for the binary in the bin_dir of Singular and ommit warnings if |
---|
31 | * somethings goes wrong |
---|
32 | * |
---|
33 | *****************************************************************************/ |
---|
34 | static BOOLEAN warn_handle = FALSE; |
---|
35 | static BOOLEAN warn_proc = FALSE; |
---|
36 | #ifndef DL_TAIL |
---|
37 | #define DL_TAIL "so" |
---|
38 | #endif |
---|
39 | |
---|
40 | void* dynl_open_binary_warn(char* binary_name, const char* msg) |
---|
41 | { |
---|
42 | void* handle = NULL; |
---|
43 | const char* bin_dir = feGetResource('b'); |
---|
44 | if (bin_dir != NULL) |
---|
45 | { |
---|
46 | char path_name[MAXPATHLEN]; |
---|
47 | sprintf(path_name, "%s%s%s.%s", bin_dir, DIR_SEPP, binary_name, |
---|
48 | DL_TAIL); |
---|
49 | handle = dynl_open(path_name); |
---|
50 | if (handle == NULL && ! warn_handle) |
---|
51 | { |
---|
52 | Warn("Could not open dynamic library: %s", path_name); |
---|
53 | Warn("Error message from system: %s", dynl_error()); |
---|
54 | if (msg != NULL) Warn("%s", msg); |
---|
55 | Warn("See the INSTALL section in the Singular manual for details."); |
---|
56 | warn_handle = TRUE; |
---|
57 | } |
---|
58 | } |
---|
59 | return handle; |
---|
60 | } |
---|
61 | |
---|
62 | void* dynl_sym_warn(void* handle, char* proc, const char* msg) |
---|
63 | { |
---|
64 | void *proc_ptr = NULL; |
---|
65 | if (handle != NULL) |
---|
66 | { |
---|
67 | proc_ptr = dynl_sym(handle, proc); |
---|
68 | if (proc_ptr == NULL && ! warn_proc) |
---|
69 | { |
---|
70 | Warn("Could load a procedure from a dynamic library"); |
---|
71 | Warn("Error message from system: %s", dynl_error()); |
---|
72 | if (msg != NULL) Warn("%s", msg); |
---|
73 | Warn("See the INSTALL section in the Singular manual for details."); |
---|
74 | warn_proc = TRUE; |
---|
75 | } |
---|
76 | } |
---|
77 | return proc_ptr; |
---|
78 | } |
---|
79 | |
---|
80 | #ifdef __cplusplus |
---|
81 | extern "C" { |
---|
82 | #endif |
---|
83 | |
---|
84 | /***************************************************************************** |
---|
85 | * SECTION generic ELF: ix86-linux / alpha-linux / IA64-linux /x86_64_Linux * |
---|
86 | * SunOS-5 / IRIX-6 / ppcMac-Darwin |
---|
87 | *****************************************************************************/ |
---|
88 | #if defined(ix86_Linux) |
---|
89 | #define HAVE_ELF_SYSTEM |
---|
90 | #endif |
---|
91 | |
---|
92 | #if defined(ix86_Linux_libc5) |
---|
93 | #define HAVE_ELF_SYSTEM |
---|
94 | #endif |
---|
95 | |
---|
96 | #if defined(DecAlpha_Linux) |
---|
97 | #define HAVE_ELF_SYSTEM |
---|
98 | #endif |
---|
99 | |
---|
100 | #if defined(IA64_Linux) |
---|
101 | #define HAVE_ELF_SYSTEM |
---|
102 | #endif |
---|
103 | |
---|
104 | #if defined(x86_64_Linux) |
---|
105 | #define HAVE_ELF_SYSTEM |
---|
106 | #endif |
---|
107 | |
---|
108 | #if defined(ppc_Linux) |
---|
109 | #define HAVE_ELF_SYSTEM |
---|
110 | #endif |
---|
111 | |
---|
112 | #if defined(SunOS_5) |
---|
113 | #define HAVE_ELF_SYSTEM |
---|
114 | #endif |
---|
115 | |
---|
116 | #ifdef IRIX_6 |
---|
117 | #define HAVE_ELF_SYSTEM |
---|
118 | #endif |
---|
119 | |
---|
120 | #if defined(HAVE_ELF_SYSTEM) |
---|
121 | #include <dlfcn.h> |
---|
122 | |
---|
123 | static void* kernel_handle = NULL; |
---|
124 | void *dynl_open( |
---|
125 | char *filename /* I: filename to load */ |
---|
126 | ) |
---|
127 | { |
---|
128 | return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL)); |
---|
129 | } |
---|
130 | |
---|
131 | void *dynl_sym(void *handle, char *symbol) |
---|
132 | { |
---|
133 | if (handle == DYNL_KERNEL_HANDLE) |
---|
134 | { |
---|
135 | if (kernel_handle == NULL) |
---|
136 | kernel_handle = dynl_open(NULL); |
---|
137 | handle = kernel_handle; |
---|
138 | } |
---|
139 | return(dlsym(handle, symbol)); |
---|
140 | } |
---|
141 | |
---|
142 | int dynl_close (void *handle) |
---|
143 | { |
---|
144 | return(dlclose (handle)); |
---|
145 | } |
---|
146 | |
---|
147 | const char *dynl_error() |
---|
148 | { |
---|
149 | return(dlerror()); |
---|
150 | } |
---|
151 | #endif /* ELF_SYSTEM */ |
---|
152 | |
---|
153 | /***************************************************************************** |
---|
154 | * SECTION HPUX-9/10 * |
---|
155 | *****************************************************************************/ |
---|
156 | #if defined(HPUX_9) || defined(HPUX_10) |
---|
157 | #include <dl.h> |
---|
158 | |
---|
159 | typedef char *((*func_ptr) ()); |
---|
160 | |
---|
161 | void *dynl_open(char *filename) |
---|
162 | { |
---|
163 | shl_t handle = shl_load(filename, BIND_DEFERRED, 0); |
---|
164 | |
---|
165 | return ((void *) handle); |
---|
166 | } |
---|
167 | |
---|
168 | void *dynl_sym(void *handle, char *symbol) |
---|
169 | { |
---|
170 | func_ptr f; |
---|
171 | |
---|
172 | if (handle == DYNL_KERNEL_HANDLE) |
---|
173 | handle = PROG_HANDLE; |
---|
174 | |
---|
175 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_PROCEDURE, &f) == -1) |
---|
176 | { |
---|
177 | if (shl_findsym((shl_t *) & handle, symbol, TYPE_UNDEFINED, &f) == -1) |
---|
178 | { |
---|
179 | f = (func_ptr) NULL; |
---|
180 | } |
---|
181 | } |
---|
182 | return ((void *)f); |
---|
183 | } |
---|
184 | |
---|
185 | int dynl_close (void *handle) |
---|
186 | { |
---|
187 | shl_unload((shl_t) handle); |
---|
188 | return(0); |
---|
189 | } |
---|
190 | |
---|
191 | const char *dynl_error() |
---|
192 | { |
---|
193 | static char errmsg[] = "shl_load failed"; |
---|
194 | |
---|
195 | return errmsg; |
---|
196 | } |
---|
197 | #endif /* HPUX_9 or HPUX_10 */ |
---|
198 | |
---|
199 | /***************************************************************************** |
---|
200 | * SECTION AIX-4 * |
---|
201 | *****************************************************************************/ |
---|
202 | #ifdef AIX_4 |
---|
203 | #define DL_NOT_IMPLEMENTED |
---|
204 | #endif |
---|
205 | |
---|
206 | /***************************************************************************** |
---|
207 | * SECTION Sun3OS-4 * |
---|
208 | *****************************************************************************/ |
---|
209 | #ifdef Sun3OS_4 |
---|
210 | #define DL_NOT_IMPLEMENTED |
---|
211 | #endif |
---|
212 | |
---|
213 | /***************************************************************************** |
---|
214 | * SECTION SunOS-4 * |
---|
215 | *****************************************************************************/ |
---|
216 | #if defined(SunOS_4) |
---|
217 | #define DL_NOT_IMPLEMENTED |
---|
218 | #endif |
---|
219 | |
---|
220 | /***************************************************************************** |
---|
221 | * SECTION generic: dynamic madules not available |
---|
222 | *****************************************************************************/ |
---|
223 | #ifdef DL_NOT_IMPLEMEMENTED |
---|
224 | |
---|
225 | void *dynl_open(char *filename) |
---|
226 | { |
---|
227 | return(NULL); |
---|
228 | } |
---|
229 | |
---|
230 | void *dynl_sym(void *handle, char *symbol) |
---|
231 | { |
---|
232 | return(NULL); |
---|
233 | } |
---|
234 | |
---|
235 | int dynl_close (void *handle) |
---|
236 | { |
---|
237 | return(0); |
---|
238 | } |
---|
239 | |
---|
240 | const char *dynl_error() |
---|
241 | { |
---|
242 | static char errmsg[] = "support for dynamic loading not implemented"; |
---|
243 | return errmsg; |
---|
244 | } |
---|
245 | #endif |
---|
246 | |
---|
247 | #ifdef __cplusplus |
---|
248 | } |
---|
249 | #endif |
---|
250 | |
---|
251 | #endif /* HAVE_DL */ |
---|