comparison src/sysdll.c @ 1383:517919955e3f

[xemacs-hg @ 2003-03-26 04:22:47 by james] Add Andrew Begel's support for MacOS X dynamic bundles. Also some minor tweaks to sysdll.c to enhance compatibility and clarity.
author james
date Wed, 26 Mar 2003 04:22:49 +0000
parents df61d2b1d4c3
children c7045d239c2b
comparison
equal deleted inserted replaced
1382:134a4093b1e7 1383:517919955e3f
25 25
26 #include <stdlib.h> 26 #include <stdlib.h>
27 #include "lisp.h" 27 #include "lisp.h"
28 #include "sysdll.h" 28 #include "sysdll.h"
29 29
30 #ifdef DLSYM_NEEDS_UNDERSCORE
31 #define MAYBE_PREPEND_UNDERSCORE(n) do { \
32 char *buf = alloca_array (char, strlen (n) + 2); \
33 *buf = '_'; \
34 strcpy (buf + 1, n); \
35 n = buf; \
36 } while (0)
37 #else
38 #define MAYBE_PREPEND_UNDERSCORE(n)
39 #endif
40
30 /* This whole file is conditional upon HAVE_SHLIB */ 41 /* This whole file is conditional upon HAVE_SHLIB */
31 #ifdef HAVE_SHLIB 42 #ifdef HAVE_SHLIB
32 43
33 /* Thankfully, most systems follow the ELFish dlopen() method. 44 /* Thankfully, most systems follow the ELFish dlopen() method.
34 */ 45 */
35 #if defined(HAVE_DLOPEN) 46 #if defined(HAVE_DLOPEN)
36 #include <dlfcn.h> 47 #include <dlfcn.h>
37 48
38 #ifndef RTLD_LAZY 49 #ifndef RTLD_LAZY
39 # define RTLD_LAZY 1 50 # ifdef DL_LAZY
51 # define RTLD_LAZY DL_LAZY
52 # else
53 # define RTLD_LAZY 1
54 # endif
40 #endif /* RTLD_LAZY isn't defined under FreeBSD - ick */ 55 #endif /* RTLD_LAZY isn't defined under FreeBSD - ick */
41 56
42 #ifndef RTLD_NOW 57 #ifndef RTLD_NOW
43 # define RTLD_NOW 2 58 # ifdef DL_NOW
59 # define RTLD_NOW DL_NOW
60 # else
61 # define RTLD_NOW 2
62 # endif
44 #endif 63 #endif
45 64
46 int 65 int
47 dll_init (const char *arg) 66 dll_init (const char *arg)
48 { 67 {
62 } 81 }
63 82
64 dll_func 83 dll_func
65 dll_function (dll_handle h, const char *n) 84 dll_function (dll_handle h, const char *n)
66 { 85 {
67 #ifdef DLSYM_NEEDS_UNDERSCORE 86 MAYBE_PREPEND_UNDERSCORE (n);
68 char *buf = alloca_array (char, strlen (n) + 2);
69 *buf = '_';
70 strcpy (buf + 1, n);
71 n = buf;
72 #endif
73 return (dll_func) dlsym ((void *) h, n); 87 return (dll_func) dlsym ((void *) h, n);
74 } 88 }
75 89
76 dll_var 90 dll_var
77 dll_variable (dll_handle h, const char *n) 91 dll_variable (dll_handle h, const char *n)
78 { 92 {
79 #ifdef DLSYM_NEEDS_UNDERSCORE 93 MAYBE_PREPEND_UNDERSCORE (n);
80 char *buf = alloca_array (char, strlen (n) + 2);
81 *buf = '_';
82 strcpy (buf + 1, n);
83 n = buf;
84 #endif
85 return (dll_var)dlsym ((void *)h, n); 94 return (dll_var)dlsym ((void *)h, n);
86 } 95 }
87 96
88 const char * 97 const char *
89 dll_error (dll_handle h) 98 dll_error (dll_handle h)
110 dll_open (const char *fname) 119 dll_open (const char *fname)
111 { 120 {
112 /* shl_load will hang hard if passed a NULL fname. */ 121 /* shl_load will hang hard if passed a NULL fname. */
113 if (fname == NULL) return NULL; 122 if (fname == NULL) return NULL;
114 123
115 return (dll_handle) shl_load (fname, BIND_DEFERRED,0L); 124 return (dll_handle) shl_load (fname, BIND_DEFERRED, 0L);
116 } 125 }
117 126
118 int 127 int
119 dll_close (dll_handle h) 128 dll_close (dll_handle h)
120 { 129 {
240 const char * 249 const char *
241 dll_error (dll_handle h) 250 dll_error (dll_handle h)
242 { 251 {
243 return "Windows DLL Error"; 252 return "Windows DLL Error";
244 } 253 }
254 #elif defined(HAVE_DYLD)
255 /* This section supports MacOSX dynamic libraries. Dynamically
256 loadable libraries must be compiled as bundles, not dynamiclibs.
257 */
258
259 #include <mach-o/dyld.h>
260
261 int
262 dll_init (const char *arg)
263 {
264 return 0;
265 }
266
267 dll_handle
268 dll_open (const char *fname)
269 {
270 NSObjectFileImage file;
271 NSObjectFileImageReturnCode ret =
272 NSCreateObjectFileImageFromFile(fname, &file);
273 if (ret != NSObjectFileImageSuccess) {
274 return NULL;
275 }
276 NSModule out = NSLinkModule(file, fname,
277 NSLINKMODULE_OPTION_BINDNOW |
278 NSLINKMODULE_OPTION_PRIVATE |
279 NSLINKMODULE_OPTION_RETURN_ON_ERROR);
280 return (dll_handle)out;
281 }
282
283 int
284 dll_close (dll_handle h)
285 {
286 return NSUnLinkModule((NSModule)h, NSUNLINKMODULE_OPTION_NONE);
287 }
288
289 dll_func
290 dll_function (dll_handle h, const char *n)
291 {
292 NSSymbol sym;
293 MAYBE_PREPEND_UNDERSCORE (n);
294 sym = NSLookupSymbolInModule((NSModule)h, n);
295 if (sym == 0) return 0;
296 return (dll_func)NSAddressOfSymbol(sym);
297 }
298
299 dll_var
300 dll_variable (dll_handle h, const char *n)
301 {
302 NSSymbol sym;
303 MAYBE_PREPEND_UNDERSCORE (n);
304 sym = NSLookupSymbolInModule((NSModule)h, n);
305 if (sym == 0) return 0;
306 return (dll_var)NSAddressOfSymbol(sym);
307 }
308
309 const char *
310 dll_error (dll_handle h)
311 {
312 NSLinkEditErrors c;
313 int errorNumber;
314 const char *fileNameWithError, *errorString;
315 NSLinkEditError(&c, &errorNumber, &fileNameWithError, &errorString);
316 return errorString;
317 }
245 #else 318 #else
246 /* Catchall if we don't know about this systems method of dynamic loading */ 319 /* Catchall if we don't know about this systems method of dynamic loading */
247 int 320 int
248 dll_init (const char *arg) 321 dll_init (const char *arg)
249 { 322 {