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
|
|
231 if (NILP (fname))
|
|
232 {
|
|
233 soname = NULL;
|
|
234 }
|
|
235 else
|
|
236 {
|
|
237 LISP_STRING_TO_EXTERNAL (fname, soname, Qdll_filename_encoding);
|
|
238 }
|
|
239 ret = NSCreateObjectFileImageFromFile(soname, &file);
|
1383
|
240 if (ret != NSObjectFileImageSuccess) {
|
|
241 return NULL;
|
|
242 }
|
1706
|
243 out = NSLinkModule(file, soname,
|
1418
|
244 NSLINKMODULE_OPTION_BINDNOW |
|
|
245 NSLINKMODULE_OPTION_PRIVATE |
|
|
246 NSLINKMODULE_OPTION_RETURN_ON_ERROR);
|
1383
|
247 return (dll_handle)out;
|
|
248 }
|
|
249
|
|
250 int
|
|
251 dll_close (dll_handle h)
|
|
252 {
|
|
253 return NSUnLinkModule((NSModule)h, NSUNLINKMODULE_OPTION_NONE);
|
|
254 }
|
|
255
|
1851
|
256 /* Given an address, return the mach_header for the image containing it
|
|
257 * or zero if the given address is not contained in any loaded images.
|
|
258 *
|
|
259 * Note: image_for_address(), my_find_image() and search_linked_libs() are
|
|
260 * based on code from the dlcompat library
|
|
261 * (http://www.opendarwin.org/projects/dlcompat).
|
|
262 */
|
|
263
|
|
264 static struct mach_header*
|
|
265 image_for_address(void *address)
|
|
266 {
|
|
267 unsigned long i;
|
|
268 unsigned long count = _dyld_image_count();
|
|
269 struct mach_header *mh = 0;
|
|
270
|
|
271 for (i = 0; i < count; i++)
|
|
272 {
|
|
273 unsigned long addr = (unsigned long)address -
|
|
274 _dyld_get_image_vmaddr_slide(i);
|
|
275 mh = _dyld_get_image_header(i);
|
|
276
|
|
277 if (mh)
|
|
278 {
|
|
279 struct load_command *lc =
|
|
280 (struct load_command *)((char *)mh + sizeof(struct mach_header));
|
|
281 unsigned long j;
|
|
282
|
|
283 for (j = 0; j < mh->ncmds;
|
|
284 j++, lc = (struct load_command *)((char *)lc + lc->cmdsize))
|
|
285 {
|
|
286 if (LC_SEGMENT == lc->cmd &&
|
|
287 addr >= ((struct segment_command *)lc)->vmaddr &&
|
|
288 addr <
|
|
289 ((struct segment_command *)lc)->vmaddr +
|
|
290 ((struct segment_command *)lc)->vmsize)
|
|
291 {
|
|
292 goto image_found;
|
|
293 }
|
|
294 }
|
|
295 }
|
|
296
|
|
297 mh = 0;
|
|
298 }
|
|
299
|
|
300 image_found:
|
|
301 return mh;
|
|
302 }
|
|
303
|
|
304 static const struct mach_header*
|
|
305 my_find_image(const char *name)
|
|
306 {
|
|
307 const struct mach_header *mh = (struct mach_header *)
|
|
308 NSAddImage(name, NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED |
|
|
309 NSADDIMAGE_OPTION_RETURN_ON_ERROR);
|
|
310
|
|
311 if (!mh)
|
|
312 {
|
|
313 int count = _dyld_image_count();
|
|
314 int j;
|
|
315
|
|
316 for (j = 0; j < count; j++)
|
|
317 {
|
|
318 const char *id = _dyld_get_image_name(j);
|
|
319
|
|
320 if (!strcmp(id, name))
|
|
321 {
|
|
322 mh = _dyld_get_image_header(j);
|
|
323 break;
|
|
324 }
|
|
325 }
|
|
326 }
|
|
327
|
|
328 return mh;
|
|
329 }
|
|
330
|
|
331 /*
|
|
332 * dyld adds libraries by first adding the directly dependant libraries in
|
|
333 * link order, and then adding the dependencies for those libraries, so we
|
|
334 * should do the same... but we don't bother adding the extra dependencies, if
|
|
335 * the symbols are neither in the loaded image nor any of it's direct
|
|
336 * dependencies, then it probably isn't there.
|
|
337 */
|
|
338 static NSSymbol
|
|
339 search_linked_libs(const struct mach_header * mh, const char *symbol)
|
|
340 {
|
2054
|
341 unsigned long n;
|
1851
|
342 NSSymbol nssym = 0;
|
|
343
|
|
344 struct load_command *lc =
|
|
345 (struct load_command *)((char *)mh + sizeof(struct mach_header));
|
|
346
|
|
347 for (n = 0; n < mh->ncmds;
|
|
348 n++, lc = (struct load_command *)((char *)lc + lc->cmdsize))
|
|
349 {
|
|
350 if ((LC_LOAD_DYLIB == lc->cmd) || (LC_LOAD_WEAK_DYLIB == lc->cmd))
|
|
351 {
|
|
352 struct mach_header *wh;
|
|
353
|
|
354 if ((wh = (struct mach_header *)
|
|
355 my_find_image((char *)(((struct dylib_command *)lc)->dylib.name.offset +
|
|
356 (char *)lc))))
|
|
357 {
|
|
358 if (NSIsSymbolNameDefinedInImage(wh, symbol))
|
|
359 {
|
|
360 nssym =
|
|
361 NSLookupSymbolInImage(wh,
|
|
362 symbol,
|
|
363 NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
|
|
364 NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
|
|
365 break;
|
|
366 }
|
|
367 }
|
|
368 }
|
|
369 }
|
|
370
|
|
371 return nssym;
|
|
372 }
|
|
373
|
1383
|
374 dll_func
|
1706
|
375 dll_function (dll_handle h, const CIbyte *n)
|
1383
|
376 {
|
1851
|
377 NSSymbol sym = 0;
|
1383
|
378 MAYBE_PREPEND_UNDERSCORE (n);
|
1851
|
379
|
|
380 /* NULL means the program image and shared libraries, not bundles. */
|
|
381
|
|
382 if (h == NULL)
|
|
383 {
|
|
384 /* NOTE: This assumes that this function is included in the main program
|
|
385 and not in a shared library. */
|
2054
|
386 const struct mach_header* my_mh = image_for_address((void*) &dll_function);
|
1851
|
387
|
|
388 if (NSIsSymbolNameDefinedInImage(my_mh, n))
|
|
389 {
|
|
390 sym =
|
|
391 NSLookupSymbolInImage(my_mh,
|
|
392 n,
|
|
393 NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
|
|
394 NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
|
|
395 }
|
|
396
|
|
397 if (!sym)
|
|
398 {
|
|
399 sym = search_linked_libs(my_mh, n);
|
|
400 }
|
|
401 }
|
|
402 else
|
|
403 {
|
|
404 sym = NSLookupSymbolInModule((NSModule)h, n);
|
|
405 }
|
|
406
|
|
407 if (sym == 0) return 0;
|
|
408 return (dll_func)NSAddressOfSymbol(sym);
|
|
409 }
|
1383
|
410
|
|
411 dll_var
|
1706
|
412 dll_variable (dll_handle h, const CIbyte *n)
|
1383
|
413 {
|
|
414 NSSymbol sym;
|
|
415 MAYBE_PREPEND_UNDERSCORE (n);
|
|
416 sym = NSLookupSymbolInModule((NSModule)h, n);
|
|
417 if (sym == 0) return 0;
|
|
418 return (dll_var)NSAddressOfSymbol(sym);
|
|
419 }
|
|
420
|
1706
|
421 Lisp_Object
|
1811
|
422 dll_error ()
|
1383
|
423 {
|
|
424 NSLinkEditErrors c;
|
|
425 int errorNumber;
|
1706
|
426 const CIbyte *fileNameWithError, *errorString;
|
1383
|
427 NSLinkEditError(&c, &errorNumber, &fileNameWithError, &errorString);
|
1706
|
428 return build_ext_string (errorString, Qnative);
|
1383
|
429 }
|
2078
|
430 #elif HAVE_LTDL
|
|
431 /* Libtool's libltdl */
|
|
432 #include <ltdl.h>
|
|
433
|
|
434 dll_handle
|
|
435 dll_open (Lisp_Object fname)
|
|
436 {
|
|
437 Extbyte *soname;
|
|
438
|
|
439 if (NILP (fname))
|
|
440 {
|
|
441 soname = NULL;
|
|
442 }
|
|
443 else
|
|
444 {
|
|
445 LISP_STRING_TO_EXTERNAL (fname, soname, Qdll_filename_encoding);
|
|
446 }
|
|
447 return (dll_handle) lt_dlopen (soname);
|
|
448 }
|
|
449
|
|
450 int
|
|
451 dll_close (dll_handle h)
|
|
452 {
|
|
453 return lt_dlclose ((lt_dlhandle) h);
|
|
454 }
|
|
455
|
|
456 dll_func
|
|
457 dll_function (dll_handle h, const CIbyte *n)
|
|
458 {
|
|
459 MAYBE_PREPEND_UNDERSCORE (n);
|
|
460 return (dll_func) lt_dlsym ((lt_dlhandle) h, n);
|
|
461 }
|
|
462
|
|
463 dll_var
|
|
464 dll_variable (dll_handle h, const CIbyte *n)
|
|
465 {
|
|
466 MAYBE_PREPEND_UNDERSCORE (n);
|
|
467 return (dll_var) lt_dlsym ((lt_dlhandle) h, n);
|
|
468 }
|
|
469
|
|
470 Lisp_Object
|
|
471 dll_error ()
|
|
472 {
|
|
473 return build_ext_string (lt_dlerror (), Qnative);
|
|
474 }
|
428
|
475 #else
|
1706
|
476 /* Catchall if we don't know about this system's method of dynamic loading */
|
428
|
477 dll_handle
|
1706
|
478 dll_open (Lisp_Object fname)
|
428
|
479 {
|
|
480 return NULL;
|
|
481 }
|
|
482
|
|
483 int
|
|
484 dll_close (dll_handle h)
|
|
485 {
|
|
486 return 0;
|
|
487 }
|
|
488
|
|
489 dll_func
|
1706
|
490 dll_function (dll_handle h, const CIbyte *n)
|
428
|
491 {
|
|
492 return NULL;
|
|
493 }
|
|
494
|
|
495 dll_func
|
1706
|
496 dll_variable (dll_handle h, const CIbyte *n)
|
428
|
497 {
|
|
498 return NULL;
|
|
499 }
|
|
500
|
1706
|
501 Lisp_Object
|
1811
|
502 dll_error ()
|
428
|
503 {
|
1706
|
504 return build_string ("Shared libraries not implemented on this system");
|
428
|
505 }
|
|
506 #endif /* System conditionals */
|
|
507
|
|
508 #endif /* HAVE_SHLIB */
|