comparison src/sysdll.c @ 1706:9fc738581a9d

[xemacs-hg @ 2003-09-22 03:21:12 by james] Remove GNU DLD support, fix the C++ build, make eval-related functions visible to modules, and fix minor Windows-related problems.
author james
date Mon, 22 Sep 2003 03:21:19 +0000
parents 64eaceca611d
children 3fe1a35b705d
comparison
equal deleted inserted replaced
1705:cdbc76885304 1706:9fc738581a9d
27 #include "lisp.h" 27 #include "lisp.h"
28 #include "sysdll.h" 28 #include "sysdll.h"
29 29
30 #ifdef DLSYM_NEEDS_UNDERSCORE 30 #ifdef DLSYM_NEEDS_UNDERSCORE
31 #define MAYBE_PREPEND_UNDERSCORE(n) do { \ 31 #define MAYBE_PREPEND_UNDERSCORE(n) do { \
32 char *buf = alloca_array (char, strlen (n) + 2); \ 32 CIbyte *buf = alloca_array (CIbyte, strlen (n) + 2); \
33 *buf = '_'; \ 33 *buf = '_'; \
34 strcpy (buf + 1, n); \ 34 strcpy (buf + 1, n); \
35 n = buf; \ 35 n = buf; \
36 } while (0) 36 } while (0)
37 #else 37 #else
61 # define RTLD_NOW 2 61 # define RTLD_NOW 2
62 # endif 62 # endif
63 #endif 63 #endif
64 64
65 int 65 int
66 dll_init (const char *arg) 66 dll_init (const Extbyte *arg)
67 { 67 {
68 return 0; 68 return 0;
69 } 69 }
70 70
71 dll_handle 71 dll_handle
72 dll_open (const char *fname) 72 dll_open (Lisp_Object fname)
73 { 73 {
74 return (dll_handle) dlopen (fname, RTLD_NOW); 74 Extbyte *soname;
75
76 if (NILP (fname))
77 {
78 soname = NULL;
79 }
80 else
81 {
82 LISP_STRING_TO_EXTERNAL (fname, soname, Qdll_filename_encoding);
83 }
84 return (dll_handle) dlopen (soname, RTLD_NOW);
75 } 85 }
76 86
77 int 87 int
78 dll_close (dll_handle h) 88 dll_close (dll_handle h)
79 { 89 {
80 return dlclose ((void *) h); 90 return dlclose ((void *) h);
81 } 91 }
82 92
83 dll_func 93 dll_func
84 dll_function (dll_handle h, const char *n) 94 dll_function (dll_handle h, const CIbyte *n)
85 { 95 {
86 MAYBE_PREPEND_UNDERSCORE (n); 96 MAYBE_PREPEND_UNDERSCORE (n);
87 return (dll_func) dlsym ((void *) h, n); 97 return (dll_func) dlsym ((void *) h, n);
88 } 98 }
89 99
90 dll_var 100 dll_var
91 dll_variable (dll_handle h, const char *n) 101 dll_variable (dll_handle h, const CIbyte *n)
92 { 102 {
93 MAYBE_PREPEND_UNDERSCORE (n); 103 MAYBE_PREPEND_UNDERSCORE (n);
94 return (dll_var)dlsym ((void *)h, n); 104 return (dll_var)dlsym ((void *)h, n);
95 } 105 }
96 106
97 const char * 107 Lisp_Object
98 dll_error (dll_handle h) 108 dll_error (dll_handle h)
99 { 109 {
110 const Extbyte *msg;
100 #if defined(HAVE_DLERROR) || defined(dlerror) 111 #if defined(HAVE_DLERROR) || defined(dlerror)
101 return (const char *) dlerror (); 112 msg = (const Extbyte *) dlerror ();
102 #elif defined(HAVE__DLERROR) 113 #elif defined(HAVE__DLERROR)
103 return (const char *) _dlerror(); 114 msg = (const Extbyte *) _dlerror();
104 #else 115 #else
105 return "Shared library error"; 116 msg = (const Extbyte *) "Shared library error";
106 #endif 117 #endif
118 return build_ext_string (msg, Qnative);
107 } 119 }
108 120
109 #elif defined(HAVE_SHL_LOAD) 121 #elif defined(HAVE_SHL_LOAD)
110 /* This is the HP/UX version */ 122 /* This is the HP/UX version */
111 #include <dl.h> 123 #include <dl.h>
112 int 124 int
113 dll_init (const char *arg) 125 dll_init (const Extbyte *arg)
114 { 126 {
115 return 0; 127 return 0;
116 } 128 }
117 129
118 dll_handle 130 dll_handle
119 dll_open (const char *fname) 131 dll_open (Lisp_Object fname)
120 { 132 {
121 /* shl_load will hang hard if passed a NULL fname. */ 133 Extbyte *soname;
122 if (fname == NULL) return NULL; 134
123 135 if (NILP (fname))
124 return (dll_handle) shl_load (fname, BIND_DEFERRED, 0L); 136 {
137 soname = NULL;
138 }
139 else
140 {
141 LISP_STRING_TO_EXTERNAL (fname, soname, Qdll_filename_encoding);
142 }
143 return (dll_handle) shl_load (soname, BIND_DEFERRED, 0L);
125 } 144 }
126 145
127 int 146 int
128 dll_close (dll_handle h) 147 dll_close (dll_handle h)
129 { 148 {
130 return shl_unload ((shl_t) h); 149 return shl_unload ((shl_t) h);
131 } 150 }
132 151
133 dll_func 152 dll_func
134 dll_function (dll_handle h, const char *n) 153 dll_function (dll_handle h, const CIbyte *n)
135 { 154 {
136 long handle = 0L; 155 long handle = 0L;
137 156
138 if (shl_findsym ((shl_t *) &h, n, TYPE_PROCEDURE, &handle)) 157 if (shl_findsym ((shl_t *) &h, n, TYPE_PROCEDURE, &handle))
139 return NULL; 158 return NULL;
140 159
141 return (dll_func) handle; 160 return (dll_func) handle;
142 } 161 }
143 162
144 dll_var 163 dll_var
145 dll_variable (dll_handle h, const char *n) 164 dll_variable (dll_handle h, const CIbyte *n)
146 { 165 {
147 long handle = 0L; 166 long handle = 0L;
148 167
149 if (shl_findsym ((shl_t *) &h, n, TYPE_DATA, &handle)) 168 if (shl_findsym ((shl_t *) &h, n, TYPE_DATA, &handle))
150 return NULL; 169 return NULL;
151 170
152 return (dll_var) handle; 171 return (dll_var) handle;
153 } 172 }
154 173
155 const char * 174 Lisp_Object
156 dll_error (dll_handle h) 175 dll_error (dll_handle h)
157 { 176 {
158 /* #### WTF?! Shouldn't this at least attempt to get strerror or 177 /* #### WTF?! Shouldn't this at least attempt to get strerror or
159 something? --hniksic */ 178 something? --hniksic */
160 return "Generic shared library error"; 179 return build_string ("Generic shared library error", Qnative);
161 } 180 }
162 181
163 #elif defined(HAVE_DLD_INIT)
164 #include <dld.h>
165 int
166 dll_init (const char *arg)
167 {
168 char *real_exe = dld_find_executable (arg);
169 int rc;
170
171 rc = dld_init (real_exe);
172 if (rc)
173 {
174 dld_perror (exe);
175 return -1;
176 }
177 return 0;
178 }
179
180 dll_handle
181 dll_open (const char *fname)
182 {
183 rc = dld_link (fname);
184 if (rc)
185 return NULL;
186
187 return (dll_handle) 1;
188 }
189
190 int
191 dll_close (dll_handle h)
192 {
193 /* *sigh* DLD is pretty lame and doesn't return a handle that you can use
194 ** later on to free the file - you have to remember the filename and
195 ** use that as the unlinker. We should eventually keep a linked list
196 ** of loaded modules and then use the node pointer as the unique id
197 ** for the shared library. Wheeee. But not now.
198 */
199 return 1;
200 }
201
202 DLL_FUNC
203 dll_function (dll_handle h, const char *n)
204 {
205 return dld_get_func (n);
206 }
207
208 DLL_FUNC
209 dll_variable (dll_handle h, const char *n)
210 {
211 return dld_get_symbol (n);
212 }
213 #elif defined (WIN32_NATIVE) || defined (CYGWIN) 182 #elif defined (WIN32_NATIVE) || defined (CYGWIN)
214 183
215 #include "syswindows.h" 184 #include "syswindows.h"
216 #include "sysfile.h" 185 #include "sysfile.h"
217 186
218 int 187 int
219 dll_init (const char *arg) 188 dll_init (const Extbyte *arg)
220 { 189 {
221 return 0; 190 return 0;
222 } 191 }
223 192
224 dll_handle 193 dll_handle
225 dll_open (const char *fname) 194 dll_open (Lisp_Object fname)
226 { 195 {
227 Ibyte *winfname, *unifname; 196 Extbyte *soname;
228 LOCAL_TO_WIN32_FILE_FORMAT ((char *) fname, winfname); 197
229 C_STRING_TO_TSTR (winfname, unifname); 198 if (NILP (fname))
230 return (dll_handle) qxeLoadLibrary (unifname); 199 {
231 } 200 soname = NULL;
232 201 }
233 int 202 else
234 dll_close (dll_handle h) 203 {
235 { 204 LOCAL_FILE_FORMAT_TO_TSTR (fname, soname);
236 return FreeLibrary (h); 205 }
237 } 206 return (dll_handle) qxeLoadLibrary (soname);
238 207 }
239 dll_func 208
240 dll_function (dll_handle h, const char *n) 209 int
241 { 210 dll_close (dll_handle h)
242 return (dll_func) GetProcAddress (h, n); 211 {
243 } 212 return FreeLibrary ((HMODULE) h);
244 213 }
245 dll_func 214
246 dll_variable (dll_handle h, const char *n) 215 dll_func
247 { 216 dll_function (dll_handle h, const CIbyte *n)
248 return (dll_func) GetProcAddress (h, n); 217 {
249 } 218 return (dll_func) GetProcAddress ((HINSTANCE) h, n);
250 219 }
251 const char * 220
252 dll_error (dll_handle h) 221 dll_func
253 { 222 dll_variable (dll_handle h, const CIbyte *n)
254 /* Since nobody frees the returned string, I have to make this ugly hack. */ 223 {
255 static char err[32] = "Windows DLL Error "; 224 return (dll_func) GetProcAddress ((HINSTANCE) h, n);
256 snprintf (&err[18], 14, "%lu", GetLastError ()); 225 }
257 return err; 226
227 Lisp_Object
228 dll_error (dll_handle h)
229 {
230 CIbyte err[32];
231 snprintf (err, 32, "Windows DLL Error %lu", GetLastError ());
232 return build_string (err);
258 } 233 }
259 #elif defined(HAVE_DYLD) 234 #elif defined(HAVE_DYLD)
260 /* This section supports MacOSX dynamic libraries. Dynamically 235 /* This section supports MacOSX dynamic libraries. Dynamically
261 loadable libraries must be compiled as bundles, not dynamiclibs. 236 loadable libraries must be compiled as bundles, not dynamiclibs.
262 */ 237 */
263 238
264 #include <mach-o/dyld.h> 239 #include <mach-o/dyld.h>
265 240
266 int 241 int
267 dll_init (const char *arg) 242 dll_init (const Extbyte *arg)
268 { 243 {
269 return 0; 244 return 0;
270 } 245 }
271 246
272 dll_handle 247 dll_handle
273 dll_open (const char *fname) 248 dll_open (Lisp_Object fname)
274 { 249 {
250 Extbyte *soname;
275 NSObjectFileImage file; 251 NSObjectFileImage file;
276 NSModule out; 252 NSModule out;
277 NSObjectFileImageReturnCode ret = 253 NSObjectFileImageReturnCode ret;
278 NSCreateObjectFileImageFromFile(fname, &file); 254
255 if (NILP (fname))
256 {
257 soname = NULL;
258 }
259 else
260 {
261 LISP_STRING_TO_EXTERNAL (fname, soname, Qdll_filename_encoding);
262 }
263 ret = NSCreateObjectFileImageFromFile(soname, &file);
279 if (ret != NSObjectFileImageSuccess) { 264 if (ret != NSObjectFileImageSuccess) {
280 return NULL; 265 return NULL;
281 } 266 }
282 out = NSLinkModule(file, fname, 267 out = NSLinkModule(file, soname,
283 NSLINKMODULE_OPTION_BINDNOW | 268 NSLINKMODULE_OPTION_BINDNOW |
284 NSLINKMODULE_OPTION_PRIVATE | 269 NSLINKMODULE_OPTION_PRIVATE |
285 NSLINKMODULE_OPTION_RETURN_ON_ERROR); 270 NSLINKMODULE_OPTION_RETURN_ON_ERROR);
286 return (dll_handle)out; 271 return (dll_handle)out;
287 } 272 }
291 { 276 {
292 return NSUnLinkModule((NSModule)h, NSUNLINKMODULE_OPTION_NONE); 277 return NSUnLinkModule((NSModule)h, NSUNLINKMODULE_OPTION_NONE);
293 } 278 }
294 279
295 dll_func 280 dll_func
296 dll_function (dll_handle h, const char *n) 281 dll_function (dll_handle h, const CIbyte *n)
297 { 282 {
298 NSSymbol sym; 283 NSSymbol sym;
299 MAYBE_PREPEND_UNDERSCORE (n); 284 MAYBE_PREPEND_UNDERSCORE (n);
300 sym = NSLookupSymbolInModule((NSModule)h, n); 285 sym = NSLookupSymbolInModule((NSModule)h, n);
301 if (sym == 0) return 0; 286 if (sym == 0) return 0;
302 return (dll_func)NSAddressOfSymbol(sym); 287 return (dll_func)NSAddressOfSymbol(sym);
303 } 288 }
304 289
305 dll_var 290 dll_var
306 dll_variable (dll_handle h, const char *n) 291 dll_variable (dll_handle h, const CIbyte *n)
307 { 292 {
308 NSSymbol sym; 293 NSSymbol sym;
309 MAYBE_PREPEND_UNDERSCORE (n); 294 MAYBE_PREPEND_UNDERSCORE (n);
310 sym = NSLookupSymbolInModule((NSModule)h, n); 295 sym = NSLookupSymbolInModule((NSModule)h, n);
311 if (sym == 0) return 0; 296 if (sym == 0) return 0;
312 return (dll_var)NSAddressOfSymbol(sym); 297 return (dll_var)NSAddressOfSymbol(sym);
313 } 298 }
314 299
315 const char * 300 Lisp_Object
316 dll_error (dll_handle h) 301 dll_error (dll_handle h)
317 { 302 {
318 NSLinkEditErrors c; 303 NSLinkEditErrors c;
319 int errorNumber; 304 int errorNumber;
320 const char *fileNameWithError, *errorString; 305 const CIbyte *fileNameWithError, *errorString;
321 NSLinkEditError(&c, &errorNumber, &fileNameWithError, &errorString); 306 NSLinkEditError(&c, &errorNumber, &fileNameWithError, &errorString);
322 return errorString; 307 return build_ext_string (errorString, Qnative);
323 } 308 }
324 #else 309 #else
325 /* Catchall if we don't know about this systems method of dynamic loading */ 310 /* Catchall if we don't know about this system's method of dynamic loading */
326 int 311 int
327 dll_init (const char *arg) 312 dll_init (const Extbyte *arg)
328 { 313 {
329 return -1; 314 return -1;
330 } 315 }
331 316
332 dll_handle 317 dll_handle
333 dll_open (const char *fname) 318 dll_open (Lisp_Object fname)
334 { 319 {
335 return NULL; 320 return NULL;
336 } 321 }
337 322
338 int 323 int
340 { 325 {
341 return 0; 326 return 0;
342 } 327 }
343 328
344 dll_func 329 dll_func
345 dll_function (dll_handle h, const char *n) 330 dll_function (dll_handle h, const CIbyte *n)
346 { 331 {
347 return NULL; 332 return NULL;
348 } 333 }
349 334
350 dll_func 335 dll_func
351 dll_variable (dll_handle h, const char *n) 336 dll_variable (dll_handle h, const CIbyte *n)
352 { 337 {
353 return NULL; 338 return NULL;
354 } 339 }
355 340
356 const char * 341 Lisp_Object
357 dll_error (dll_handle h) 342 dll_error (dll_handle h)
358 { 343 {
359 return "Shared libraries not implemented on this system"; 344 return build_string ("Shared libraries not implemented on this system");
360 } 345 }
361 #endif /* System conditionals */ 346 #endif /* System conditionals */
362 347
363 #endif /* HAVE_SHLIB */ 348 #endif /* HAVE_SHLIB */