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 dll_handle
|
1706
|
66 dll_open (Lisp_Object fname)
|
428
|
67 {
|
1706
|
68 Extbyte *soname;
|
|
69
|
|
70 if (NILP (fname))
|
|
71 {
|
|
72 soname = NULL;
|
|
73 }
|
|
74 else
|
|
75 {
|
|
76 LISP_STRING_TO_EXTERNAL (fname, soname, Qdll_filename_encoding);
|
|
77 }
|
|
78 return (dll_handle) dlopen (soname, RTLD_NOW);
|
428
|
79 }
|
|
80
|
|
81 int
|
|
82 dll_close (dll_handle h)
|
|
83 {
|
442
|
84 return dlclose ((void *) h);
|
428
|
85 }
|
|
86
|
|
87 dll_func
|
1706
|
88 dll_function (dll_handle h, const CIbyte *n)
|
428
|
89 {
|
1383
|
90 MAYBE_PREPEND_UNDERSCORE (n);
|
442
|
91 return (dll_func) dlsym ((void *) h, n);
|
428
|
92 }
|
|
93
|
|
94 dll_var
|
1706
|
95 dll_variable (dll_handle h, const CIbyte *n)
|
428
|
96 {
|
1383
|
97 MAYBE_PREPEND_UNDERSCORE (n);
|
428
|
98 return (dll_var)dlsym ((void *)h, n);
|
|
99 }
|
|
100
|
1706
|
101 Lisp_Object
|
1811
|
102 dll_error ()
|
428
|
103 {
|
1706
|
104 const Extbyte *msg;
|
428
|
105 #if defined(HAVE_DLERROR) || defined(dlerror)
|
1706
|
106 msg = (const Extbyte *) dlerror ();
|
428
|
107 #elif defined(HAVE__DLERROR)
|
1706
|
108 msg = (const Extbyte *) _dlerror();
|
428
|
109 #else
|
1706
|
110 msg = (const Extbyte *) "Shared library error";
|
428
|
111 #endif
|
1706
|
112 return build_ext_string (msg, Qnative);
|
428
|
113 }
|
|
114
|
|
115 #elif defined(HAVE_SHL_LOAD)
|
|
116 /* This is the HP/UX version */
|
|
117 #include <dl.h>
|
|
118 dll_handle
|
1706
|
119 dll_open (Lisp_Object fname)
|
428
|
120 {
|
1706
|
121 Extbyte *soname;
|
428
|
122
|
1706
|
123 if (NILP (fname))
|
|
124 {
|
|
125 soname = NULL;
|
|
126 }
|
|
127 else
|
|
128 {
|
|
129 LISP_STRING_TO_EXTERNAL (fname, soname, Qdll_filename_encoding);
|
|
130 }
|
|
131 return (dll_handle) shl_load (soname, BIND_DEFERRED, 0L);
|
428
|
132 }
|
|
133
|
|
134 int
|
|
135 dll_close (dll_handle h)
|
|
136 {
|
442
|
137 return shl_unload ((shl_t) h);
|
428
|
138 }
|
|
139
|
|
140 dll_func
|
1706
|
141 dll_function (dll_handle h, const CIbyte *n)
|
428
|
142 {
|
|
143 long handle = 0L;
|
|
144
|
442
|
145 if (shl_findsym ((shl_t *) &h, n, TYPE_PROCEDURE, &handle))
|
428
|
146 return NULL;
|
|
147
|
442
|
148 return (dll_func) handle;
|
428
|
149 }
|
|
150
|
|
151 dll_var
|
1706
|
152 dll_variable (dll_handle h, const CIbyte *n)
|
428
|
153 {
|
|
154 long handle = 0L;
|
|
155
|
442
|
156 if (shl_findsym ((shl_t *) &h, n, TYPE_DATA, &handle))
|
428
|
157 return NULL;
|
|
158
|
442
|
159 return (dll_var) handle;
|
428
|
160 }
|
|
161
|
1706
|
162 Lisp_Object
|
1811
|
163 dll_error ()
|
428
|
164 {
|
|
165 /* #### WTF?! Shouldn't this at least attempt to get strerror or
|
|
166 something? --hniksic */
|
1706
|
167 return build_string ("Generic shared library error", Qnative);
|
428
|
168 }
|
|
169
|
1632
|
170 #elif defined (WIN32_NATIVE) || defined (CYGWIN)
|
442
|
171
|
1632
|
172 #include "syswindows.h"
|
|
173 #include "sysfile.h"
|
442
|
174
|
428
|
175 dll_handle
|
1706
|
176 dll_open (Lisp_Object fname)
|
428
|
177 {
|
1706
|
178 Extbyte *soname;
|
|
179
|
|
180 if (NILP (fname))
|
|
181 {
|
|
182 soname = NULL;
|
|
183 }
|
|
184 else
|
|
185 {
|
|
186 LOCAL_FILE_FORMAT_TO_TSTR (fname, soname);
|
|
187 }
|
|
188 return (dll_handle) qxeLoadLibrary (soname);
|
428
|
189 }
|
|
190
|
|
191 int
|
|
192 dll_close (dll_handle h)
|
|
193 {
|
1706
|
194 return FreeLibrary ((HMODULE) h);
|
428
|
195 }
|
|
196
|
|
197 dll_func
|
1706
|
198 dll_function (dll_handle h, const CIbyte *n)
|
428
|
199 {
|
1706
|
200 return (dll_func) GetProcAddress ((HINSTANCE) h, n);
|
428
|
201 }
|
|
202
|
|
203 dll_func
|
1706
|
204 dll_variable (dll_handle h, const CIbyte *n)
|
428
|
205 {
|
1706
|
206 return (dll_func) GetProcAddress ((HINSTANCE) h, n);
|
428
|
207 }
|
|
208
|
1706
|
209 Lisp_Object
|
1811
|
210 dll_error ()
|
428
|
211 {
|
1706
|
212 CIbyte err[32];
|
|
213 snprintf (err, 32, "Windows DLL Error %lu", GetLastError ());
|
|
214 return build_string (err);
|
428
|
215 }
|
1383
|
216 #elif defined(HAVE_DYLD)
|
|
217 /* This section supports MacOSX dynamic libraries. Dynamically
|
|
218 loadable libraries must be compiled as bundles, not dynamiclibs.
|
|
219 */
|
|
220
|
|
221 #include <mach-o/dyld.h>
|
|
222
|
|
223 dll_handle
|
1706
|
224 dll_open (Lisp_Object fname)
|
1383
|
225 {
|
1706
|
226 Extbyte *soname;
|
1383
|
227 NSObjectFileImage file;
|
1418
|
228 NSModule out;
|
1706
|
229 NSObjectFileImageReturnCode ret;
|
|
230
|
2855
|
231 /*
|
|
232 * MacOS X dll support is for bundles, not the current executable, so return
|
|
233 * NULL is this case. However, dll_function() uses a special hack where a
|
|
234 * NULL handle can be used to find executable symbols. This satisfies the
|
|
235 * needs of ui-gtk.c but is not a general solution.
|
|
236 */
|
1706
|
237 if (NILP (fname))
|
|
238 {
|
2855
|
239 return NULL;
|
1706
|
240 }
|
|
241 else
|
|
242 {
|
|
243 LISP_STRING_TO_EXTERNAL (fname, soname, Qdll_filename_encoding);
|
|
244 }
|
|
245 ret = NSCreateObjectFileImageFromFile(soname, &file);
|
1383
|
246 if (ret != NSObjectFileImageSuccess) {
|
|
247 return NULL;
|
|
248 }
|
1706
|
249 out = NSLinkModule(file, soname,
|
1418
|
250 NSLINKMODULE_OPTION_BINDNOW |
|
|
251 NSLINKMODULE_OPTION_PRIVATE |
|
|
252 NSLINKMODULE_OPTION_RETURN_ON_ERROR);
|
1383
|
253 return (dll_handle)out;
|
|
254 }
|
|
255
|
|
256 int
|
|
257 dll_close (dll_handle h)
|
|
258 {
|
|
259 return NSUnLinkModule((NSModule)h, NSUNLINKMODULE_OPTION_NONE);
|
|
260 }
|
|
261
|
1851
|
262 /* Given an address, return the mach_header for the image containing it
|
|
263 * or zero if the given address is not contained in any loaded images.
|
|
264 *
|
|
265 * Note: image_for_address(), my_find_image() and search_linked_libs() are
|
|
266 * based on code from the dlcompat library
|
|
267 * (http://www.opendarwin.org/projects/dlcompat).
|
|
268 */
|
|
269
|
3841
|
270 static const struct mach_header*
|
1851
|
271 image_for_address(void *address)
|
|
272 {
|
|
273 unsigned long i;
|
|
274 unsigned long count = _dyld_image_count();
|
3841
|
275 const struct mach_header *mh = 0;
|
1851
|
276
|
|
277 for (i = 0; i < count; i++)
|
|
278 {
|
|
279 unsigned long addr = (unsigned long)address -
|
|
280 _dyld_get_image_vmaddr_slide(i);
|
|
281 mh = _dyld_get_image_header(i);
|
|
282
|
|
283 if (mh)
|
|
284 {
|
|
285 struct load_command *lc =
|
|
286 (struct load_command *)((char *)mh + sizeof(struct mach_header));
|
|
287 unsigned long j;
|
|
288
|
|
289 for (j = 0; j < mh->ncmds;
|
|
290 j++, lc = (struct load_command *)((char *)lc + lc->cmdsize))
|
|
291 {
|
|
292 if (LC_SEGMENT == lc->cmd &&
|
|
293 addr >= ((struct segment_command *)lc)->vmaddr &&
|
|
294 addr <
|
|
295 ((struct segment_command *)lc)->vmaddr +
|
|
296 ((struct segment_command *)lc)->vmsize)
|
|
297 {
|
|
298 goto image_found;
|
|
299 }
|
|
300 }
|
|
301 }
|
|
302
|
|
303 mh = 0;
|
|
304 }
|
|
305
|
|
306 image_found:
|
|
307 return mh;
|
|
308 }
|
|
309
|
|
310 static const struct mach_header*
|
|
311 my_find_image(const char *name)
|
|
312 {
|
|
313 const struct mach_header *mh = (struct mach_header *)
|
|
314 NSAddImage(name, NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED |
|
|
315 NSADDIMAGE_OPTION_RETURN_ON_ERROR);
|
|
316
|
|
317 if (!mh)
|
|
318 {
|
|
319 int count = _dyld_image_count();
|
|
320 int j;
|
|
321
|
|
322 for (j = 0; j < count; j++)
|
|
323 {
|
|
324 const char *id = _dyld_get_image_name(j);
|
|
325
|
|
326 if (!strcmp(id, name))
|
|
327 {
|
|
328 mh = _dyld_get_image_header(j);
|
|
329 break;
|
|
330 }
|
|
331 }
|
|
332 }
|
|
333
|
|
334 return mh;
|
|
335 }
|
|
336
|
|
337 /*
|
|
338 * dyld adds libraries by first adding the directly dependant libraries in
|
|
339 * link order, and then adding the dependencies for those libraries, so we
|
|
340 * should do the same... but we don't bother adding the extra dependencies, if
|
|
341 * the symbols are neither in the loaded image nor any of it's direct
|
|
342 * dependencies, then it probably isn't there.
|
|
343 */
|
|
344 static NSSymbol
|
|
345 search_linked_libs(const struct mach_header * mh, const char *symbol)
|
|
346 {
|
2054
|
347 unsigned long n;
|
1851
|
348 NSSymbol nssym = 0;
|
|
349
|
|
350 struct load_command *lc =
|
|
351 (struct load_command *)((char *)mh + sizeof(struct mach_header));
|
|
352
|
|
353 for (n = 0; n < mh->ncmds;
|
|
354 n++, lc = (struct load_command *)((char *)lc + lc->cmdsize))
|
|
355 {
|
|
356 if ((LC_LOAD_DYLIB == lc->cmd) || (LC_LOAD_WEAK_DYLIB == lc->cmd))
|
|
357 {
|
|
358 struct mach_header *wh;
|
|
359
|
|
360 if ((wh = (struct mach_header *)
|
|
361 my_find_image((char *)(((struct dylib_command *)lc)->dylib.name.offset +
|
|
362 (char *)lc))))
|
|
363 {
|
|
364 if (NSIsSymbolNameDefinedInImage(wh, symbol))
|
|
365 {
|
|
366 nssym =
|
|
367 NSLookupSymbolInImage(wh,
|
|
368 symbol,
|
|
369 NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
|
|
370 NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
|
|
371 break;
|
|
372 }
|
|
373 }
|
|
374 }
|
|
375 }
|
|
376
|
|
377 return nssym;
|
|
378 }
|
|
379
|
1383
|
380 dll_func
|
1706
|
381 dll_function (dll_handle h, const CIbyte *n)
|
1383
|
382 {
|
1851
|
383 NSSymbol sym = 0;
|
1383
|
384 MAYBE_PREPEND_UNDERSCORE (n);
|
1851
|
385
|
|
386 /* NULL means the program image and shared libraries, not bundles. */
|
|
387
|
|
388 if (h == NULL)
|
|
389 {
|
|
390 /* NOTE: This assumes that this function is included in the main program
|
|
391 and not in a shared library. */
|
2054
|
392 const struct mach_header* my_mh = image_for_address((void*) &dll_function);
|
1851
|
393
|
|
394 if (NSIsSymbolNameDefinedInImage(my_mh, n))
|
|
395 {
|
|
396 sym =
|
|
397 NSLookupSymbolInImage(my_mh,
|
|
398 n,
|
|
399 NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
|
|
400 NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
|
|
401 }
|
|
402
|
|
403 if (!sym)
|
|
404 {
|
|
405 sym = search_linked_libs(my_mh, n);
|
|
406 }
|
|
407 }
|
|
408 else
|
|
409 {
|
|
410 sym = NSLookupSymbolInModule((NSModule)h, n);
|
|
411 }
|
|
412
|
|
413 if (sym == 0) return 0;
|
|
414 return (dll_func)NSAddressOfSymbol(sym);
|
|
415 }
|
1383
|
416
|
|
417 dll_var
|
1706
|
418 dll_variable (dll_handle h, const CIbyte *n)
|
1383
|
419 {
|
|
420 NSSymbol sym;
|
|
421 MAYBE_PREPEND_UNDERSCORE (n);
|
|
422 sym = NSLookupSymbolInModule((NSModule)h, n);
|
|
423 if (sym == 0) return 0;
|
|
424 return (dll_var)NSAddressOfSymbol(sym);
|
|
425 }
|
|
426
|
1706
|
427 Lisp_Object
|
1811
|
428 dll_error ()
|
1383
|
429 {
|
|
430 NSLinkEditErrors c;
|
|
431 int errorNumber;
|
1706
|
432 const CIbyte *fileNameWithError, *errorString;
|
1383
|
433 NSLinkEditError(&c, &errorNumber, &fileNameWithError, &errorString);
|
1706
|
434 return build_ext_string (errorString, Qnative);
|
1383
|
435 }
|
2078
|
436 #elif HAVE_LTDL
|
|
437 /* Libtool's libltdl */
|
|
438 #include <ltdl.h>
|
|
439
|
|
440 dll_handle
|
|
441 dll_open (Lisp_Object fname)
|
|
442 {
|
|
443 Extbyte *soname;
|
|
444
|
|
445 if (NILP (fname))
|
|
446 {
|
|
447 soname = NULL;
|
|
448 }
|
|
449 else
|
|
450 {
|
|
451 LISP_STRING_TO_EXTERNAL (fname, soname, Qdll_filename_encoding);
|
|
452 }
|
|
453 return (dll_handle) lt_dlopen (soname);
|
|
454 }
|
|
455
|
|
456 int
|
|
457 dll_close (dll_handle h)
|
|
458 {
|
|
459 return lt_dlclose ((lt_dlhandle) h);
|
|
460 }
|
|
461
|
|
462 dll_func
|
|
463 dll_function (dll_handle h, const CIbyte *n)
|
|
464 {
|
|
465 MAYBE_PREPEND_UNDERSCORE (n);
|
|
466 return (dll_func) lt_dlsym ((lt_dlhandle) h, n);
|
|
467 }
|
|
468
|
|
469 dll_var
|
|
470 dll_variable (dll_handle h, const CIbyte *n)
|
|
471 {
|
|
472 MAYBE_PREPEND_UNDERSCORE (n);
|
|
473 return (dll_var) lt_dlsym ((lt_dlhandle) h, n);
|
|
474 }
|
|
475
|
|
476 Lisp_Object
|
|
477 dll_error ()
|
|
478 {
|
|
479 return build_ext_string (lt_dlerror (), Qnative);
|
|
480 }
|
428
|
481 #else
|
1706
|
482 /* Catchall if we don't know about this system's method of dynamic loading */
|
428
|
483 dll_handle
|
1706
|
484 dll_open (Lisp_Object fname)
|
428
|
485 {
|
|
486 return NULL;
|
|
487 }
|
|
488
|
|
489 int
|
|
490 dll_close (dll_handle h)
|
|
491 {
|
|
492 return 0;
|
|
493 }
|
|
494
|
|
495 dll_func
|
1706
|
496 dll_function (dll_handle h, const CIbyte *n)
|
428
|
497 {
|
|
498 return NULL;
|
|
499 }
|
|
500
|
|
501 dll_func
|
1706
|
502 dll_variable (dll_handle h, const CIbyte *n)
|
428
|
503 {
|
|
504 return NULL;
|
|
505 }
|
|
506
|
1706
|
507 Lisp_Object
|
1811
|
508 dll_error ()
|
428
|
509 {
|
1706
|
510 return build_string ("Shared libraries not implemented on this system");
|
428
|
511 }
|
|
512 #endif /* System conditionals */
|
|
513
|
|
514 #endif /* HAVE_SHLIB */
|