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