428
|
1 /* sysdll.c --- system dependent support for dynamic linked libraries
|
|
2 Copyright (C) 1998 Free Software Foundation, Inc.
|
|
3 Author: William Perry <wmperry@aventail.com>
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to the Free
|
|
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
20 02111-1307, USA. */
|
|
21
|
|
22 #ifdef HAVE_CONFIG_H
|
|
23 #include <config.h>
|
|
24 #endif
|
|
25
|
430
|
26 #include <stdlib.h>
|
1272
|
27 #include "lisp.h"
|
428
|
28 #include "sysdll.h"
|
|
29
|
1383
|
30 #ifdef DLSYM_NEEDS_UNDERSCORE
|
|
31 #define MAYBE_PREPEND_UNDERSCORE(n) do { \
|
1706
|
32 CIbyte *buf = alloca_array (CIbyte, strlen (n) + 2); \
|
1383
|
33 *buf = '_'; \
|
|
34 strcpy (buf + 1, n); \
|
|
35 n = buf; \
|
|
36 } while (0)
|
|
37 #else
|
|
38 #define MAYBE_PREPEND_UNDERSCORE(n)
|
|
39 #endif
|
|
40
|
428
|
41 /* This whole file is conditional upon HAVE_SHLIB */
|
|
42 #ifdef HAVE_SHLIB
|
|
43
|
|
44 /* Thankfully, most systems follow the ELFish dlopen() method.
|
|
45 */
|
452
|
46 #if defined(HAVE_DLOPEN)
|
428
|
47 #include <dlfcn.h>
|
|
48
|
|
49 #ifndef RTLD_LAZY
|
1383
|
50 # ifdef DL_LAZY
|
|
51 # define RTLD_LAZY DL_LAZY
|
|
52 # else
|
|
53 # define RTLD_LAZY 1
|
|
54 # endif
|
428
|
55 #endif /* RTLD_LAZY isn't defined under FreeBSD - ick */
|
|
56
|
863
|
57 #ifndef RTLD_NOW
|
1383
|
58 # ifdef DL_NOW
|
|
59 # define RTLD_NOW DL_NOW
|
|
60 # else
|
|
61 # define RTLD_NOW 2
|
|
62 # endif
|
863
|
63 #endif
|
|
64
|
428
|
65 int
|
1706
|
66 dll_init (const Extbyte *arg)
|
428
|
67 {
|
|
68 return 0;
|
|
69 }
|
|
70
|
|
71 dll_handle
|
1706
|
72 dll_open (Lisp_Object fname)
|
428
|
73 {
|
1706
|
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);
|
428
|
85 }
|
|
86
|
|
87 int
|
|
88 dll_close (dll_handle h)
|
|
89 {
|
442
|
90 return dlclose ((void *) h);
|
428
|
91 }
|
|
92
|
|
93 dll_func
|
1706
|
94 dll_function (dll_handle h, const CIbyte *n)
|
428
|
95 {
|
1383
|
96 MAYBE_PREPEND_UNDERSCORE (n);
|
442
|
97 return (dll_func) dlsym ((void *) h, n);
|
428
|
98 }
|
|
99
|
|
100 dll_var
|
1706
|
101 dll_variable (dll_handle h, const CIbyte *n)
|
428
|
102 {
|
1383
|
103 MAYBE_PREPEND_UNDERSCORE (n);
|
428
|
104 return (dll_var)dlsym ((void *)h, n);
|
|
105 }
|
|
106
|
1706
|
107 Lisp_Object
|
428
|
108 dll_error (dll_handle h)
|
|
109 {
|
1706
|
110 const Extbyte *msg;
|
428
|
111 #if defined(HAVE_DLERROR) || defined(dlerror)
|
1706
|
112 msg = (const Extbyte *) dlerror ();
|
428
|
113 #elif defined(HAVE__DLERROR)
|
1706
|
114 msg = (const Extbyte *) _dlerror();
|
428
|
115 #else
|
1706
|
116 msg = (const Extbyte *) "Shared library error";
|
428
|
117 #endif
|
1706
|
118 return build_ext_string (msg, Qnative);
|
428
|
119 }
|
|
120
|
|
121 #elif defined(HAVE_SHL_LOAD)
|
|
122 /* This is the HP/UX version */
|
|
123 #include <dl.h>
|
|
124 int
|
1706
|
125 dll_init (const Extbyte *arg)
|
428
|
126 {
|
|
127 return 0;
|
|
128 }
|
|
129
|
|
130 dll_handle
|
1706
|
131 dll_open (Lisp_Object fname)
|
428
|
132 {
|
1706
|
133 Extbyte *soname;
|
428
|
134
|
1706
|
135 if (NILP (fname))
|
|
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);
|
428
|
144 }
|
|
145
|
|
146 int
|
|
147 dll_close (dll_handle h)
|
|
148 {
|
442
|
149 return shl_unload ((shl_t) h);
|
428
|
150 }
|
|
151
|
|
152 dll_func
|
1706
|
153 dll_function (dll_handle h, const CIbyte *n)
|
428
|
154 {
|
|
155 long handle = 0L;
|
|
156
|
442
|
157 if (shl_findsym ((shl_t *) &h, n, TYPE_PROCEDURE, &handle))
|
428
|
158 return NULL;
|
|
159
|
442
|
160 return (dll_func) handle;
|
428
|
161 }
|
|
162
|
|
163 dll_var
|
1706
|
164 dll_variable (dll_handle h, const CIbyte *n)
|
428
|
165 {
|
|
166 long handle = 0L;
|
|
167
|
442
|
168 if (shl_findsym ((shl_t *) &h, n, TYPE_DATA, &handle))
|
428
|
169 return NULL;
|
|
170
|
442
|
171 return (dll_var) handle;
|
428
|
172 }
|
|
173
|
1706
|
174 Lisp_Object
|
428
|
175 dll_error (dll_handle h)
|
|
176 {
|
|
177 /* #### WTF?! Shouldn't this at least attempt to get strerror or
|
|
178 something? --hniksic */
|
1706
|
179 return build_string ("Generic shared library error", Qnative);
|
428
|
180 }
|
|
181
|
1632
|
182 #elif defined (WIN32_NATIVE) || defined (CYGWIN)
|
442
|
183
|
1632
|
184 #include "syswindows.h"
|
|
185 #include "sysfile.h"
|
442
|
186
|
428
|
187 int
|
1706
|
188 dll_init (const Extbyte *arg)
|
428
|
189 {
|
|
190 return 0;
|
|
191 }
|
|
192
|
|
193 dll_handle
|
1706
|
194 dll_open (Lisp_Object fname)
|
428
|
195 {
|
1706
|
196 Extbyte *soname;
|
|
197
|
|
198 if (NILP (fname))
|
|
199 {
|
|
200 soname = NULL;
|
|
201 }
|
|
202 else
|
|
203 {
|
|
204 LOCAL_FILE_FORMAT_TO_TSTR (fname, soname);
|
|
205 }
|
|
206 return (dll_handle) qxeLoadLibrary (soname);
|
428
|
207 }
|
|
208
|
|
209 int
|
|
210 dll_close (dll_handle h)
|
|
211 {
|
1706
|
212 return FreeLibrary ((HMODULE) h);
|
428
|
213 }
|
|
214
|
|
215 dll_func
|
1706
|
216 dll_function (dll_handle h, const CIbyte *n)
|
428
|
217 {
|
1706
|
218 return (dll_func) GetProcAddress ((HINSTANCE) h, n);
|
428
|
219 }
|
|
220
|
|
221 dll_func
|
1706
|
222 dll_variable (dll_handle h, const CIbyte *n)
|
428
|
223 {
|
1706
|
224 return (dll_func) GetProcAddress ((HINSTANCE) h, n);
|
428
|
225 }
|
|
226
|
1706
|
227 Lisp_Object
|
428
|
228 dll_error (dll_handle h)
|
|
229 {
|
1706
|
230 CIbyte err[32];
|
|
231 snprintf (err, 32, "Windows DLL Error %lu", GetLastError ());
|
|
232 return build_string (err);
|
428
|
233 }
|
1383
|
234 #elif defined(HAVE_DYLD)
|
|
235 /* This section supports MacOSX dynamic libraries. Dynamically
|
|
236 loadable libraries must be compiled as bundles, not dynamiclibs.
|
|
237 */
|
|
238
|
|
239 #include <mach-o/dyld.h>
|
|
240
|
|
241 int
|
1706
|
242 dll_init (const Extbyte *arg)
|
1383
|
243 {
|
|
244 return 0;
|
|
245 }
|
|
246
|
|
247 dll_handle
|
1706
|
248 dll_open (Lisp_Object fname)
|
1383
|
249 {
|
1706
|
250 Extbyte *soname;
|
1383
|
251 NSObjectFileImage file;
|
1418
|
252 NSModule out;
|
1706
|
253 NSObjectFileImageReturnCode ret;
|
|
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);
|
1383
|
264 if (ret != NSObjectFileImageSuccess) {
|
|
265 return NULL;
|
|
266 }
|
1706
|
267 out = NSLinkModule(file, soname,
|
1418
|
268 NSLINKMODULE_OPTION_BINDNOW |
|
|
269 NSLINKMODULE_OPTION_PRIVATE |
|
|
270 NSLINKMODULE_OPTION_RETURN_ON_ERROR);
|
1383
|
271 return (dll_handle)out;
|
|
272 }
|
|
273
|
|
274 int
|
|
275 dll_close (dll_handle h)
|
|
276 {
|
|
277 return NSUnLinkModule((NSModule)h, NSUNLINKMODULE_OPTION_NONE);
|
|
278 }
|
|
279
|
|
280 dll_func
|
1706
|
281 dll_function (dll_handle h, const CIbyte *n)
|
1383
|
282 {
|
|
283 NSSymbol sym;
|
|
284 MAYBE_PREPEND_UNDERSCORE (n);
|
|
285 sym = NSLookupSymbolInModule((NSModule)h, n);
|
|
286 if (sym == 0) return 0;
|
|
287 return (dll_func)NSAddressOfSymbol(sym);
|
|
288 }
|
|
289
|
|
290 dll_var
|
1706
|
291 dll_variable (dll_handle h, const CIbyte *n)
|
1383
|
292 {
|
|
293 NSSymbol sym;
|
|
294 MAYBE_PREPEND_UNDERSCORE (n);
|
|
295 sym = NSLookupSymbolInModule((NSModule)h, n);
|
|
296 if (sym == 0) return 0;
|
|
297 return (dll_var)NSAddressOfSymbol(sym);
|
|
298 }
|
|
299
|
1706
|
300 Lisp_Object
|
1383
|
301 dll_error (dll_handle h)
|
|
302 {
|
|
303 NSLinkEditErrors c;
|
|
304 int errorNumber;
|
1706
|
305 const CIbyte *fileNameWithError, *errorString;
|
1383
|
306 NSLinkEditError(&c, &errorNumber, &fileNameWithError, &errorString);
|
1706
|
307 return build_ext_string (errorString, Qnative);
|
1383
|
308 }
|
428
|
309 #else
|
1706
|
310 /* Catchall if we don't know about this system's method of dynamic loading */
|
428
|
311 int
|
1706
|
312 dll_init (const Extbyte *arg)
|
428
|
313 {
|
|
314 return -1;
|
|
315 }
|
|
316
|
|
317 dll_handle
|
1706
|
318 dll_open (Lisp_Object fname)
|
428
|
319 {
|
|
320 return NULL;
|
|
321 }
|
|
322
|
|
323 int
|
|
324 dll_close (dll_handle h)
|
|
325 {
|
|
326 return 0;
|
|
327 }
|
|
328
|
|
329 dll_func
|
1706
|
330 dll_function (dll_handle h, const CIbyte *n)
|
428
|
331 {
|
|
332 return NULL;
|
|
333 }
|
|
334
|
|
335 dll_func
|
1706
|
336 dll_variable (dll_handle h, const CIbyte *n)
|
428
|
337 {
|
|
338 return NULL;
|
|
339 }
|
|
340
|
1706
|
341 Lisp_Object
|
428
|
342 dll_error (dll_handle h)
|
|
343 {
|
1706
|
344 return build_string ("Shared libraries not implemented on this system");
|
428
|
345 }
|
|
346 #endif /* System conditionals */
|
|
347
|
|
348 #endif /* HAVE_SHLIB */
|