annotate src/dlopen.c @ 245:51092a27c943 r20-5b21

Import from CVS: tag r20-5b21
author cvs
date Mon, 13 Aug 2007 10:17:54 +0200
parents
children 677f6a0ee643
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
245
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
1 /* Lisp interface to dynamic loading.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
2 Copyright (C) 1998 Joshua Rowe.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
3 Additional cleanup by Hrvoje Niksic.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
4
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
5 This file is part of XEmacs.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
6
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
7 This program is free software; you can redistribute it and/or modify
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
8 it under the terms of the GNU General Public License as published by
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
9 the Free Software Foundation; either version 2 of the License, or
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
10 (at your option) any later version.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
11
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
12 This program is distributed in the hope that it will be useful,
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
15 GNU General Public License for more details.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
16
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
18 along with this program; see the file COPYING. If not, write to
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
20 Boston, MA 02111-1307, USA. */
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
21
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
22 /* Synched up with: Not in FSF. */
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
23
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
24 /* A shared object may have the following symbols defined:
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
25 syms_of
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
26 vars_of
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
27 complex_vars_of
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
28 They are called in that order. Each takes and returns void
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
29 arguments.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
30
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
31 All of this needs lots and LOTS of work. Some things to work on:
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
32
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
33 1) A good foreign interface. We probably need to get rid of
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
34 syms_of and similar junk, and define a more normal interfacing to
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
35 the outside world, e.g. an init_emacs_module() function. See below
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
36 for more discussion about it. Also, we need a modules/ directory
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
37 with a few nice sample modules, a sample Makefile, etc. so people
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
38 can start hacking.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
39
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
40 2) Compatibility concerns. There are systems without dynamic
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
41 libraries (Ultrix, to name one). OK, scrap them, but not all
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
42 systems out there use the exact dlopen() interface that this file
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
43 uses. Bill Perry said he had ready Makefiles for 14 or so systems
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
44 to do this sort of things portably. Check it out. Or, use Gordon
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
45 Matzigkeit's libtool.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
46
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
47 3) I'm getting coredumps very often -- practically every time I
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
48 compile without USE_MINIMAL_TAGBITS, and even with it sometimes. I
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
49 wasn't able to resolve these.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
50
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
51 4) All of this is sooo simple-minded. As it gets more complex,
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
52 we'll have to look at how others have done similar things
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
53 (e.g. Perl and Zsh 3.1), to avoid botching it up. */
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
54
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
55 #include <config.h>
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
56 #include "lisp.h"
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
57 #include "emacsfns.h"
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
58 #include "buffer.h"
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
59
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
60 #include <stdio.h>
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
61 #include <dlfcn.h>
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
62 #include <errno.h>
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
63
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
64
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
65 DEFUN ("dl-open", Fdl_open, 1, 1, "FShared object: ", /*
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
66 Load LIBRARY as a shared object file.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
67
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
68 After the LIBRARY is dynamically linked with the executable, the
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
69 following functions are called:
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
70
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
71 syms_of(), containing definitions of symbols and subr's;
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
72 vars_of(), containing definitions of variables;
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
73 complex_vars_of(), containing complex definitions of variables.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
74
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
75 After this point, any lisp symbols defined in the shared object are
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
76 available for use.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
77 */
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
78 (library))
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
79 {
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
80 /* This function can GC */
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
81 void *handle;
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
82 char *file;
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
83 void (*function)();
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
84
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
85 CHECK_STRING (library);
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
86 library = Fexpand_file_name (library, Qnil);
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
87
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
88 file = XSTRING_DATA (library);
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
89 /* #### Is this right? */
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
90 GET_C_CHARPTR_EXT_FILENAME_DATA_ALLOCA (file, file);
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
91
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
92 handle = dlopen (file, RTLD_LAZY);
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
93 if (handle == NULL)
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
94 {
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
95 signal_error (Qerror,
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
96 list3 (build_translated_string ("Cannot load shared library"),
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
97 library, build_translated_string (dlerror ())));
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
98 }
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
99
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
100 /* #### This looks unnecessary here, because at this time one
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
101 initialization function is fully sufficient. However, I am not
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
102 removing this support, since we may wish to add mechanisms for
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
103 static linking, which would have invoke these function via normal
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
104 paths.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
105
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
106 #### But then this is not sufficient, because one could as well
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
107 honor specifier_vars_of_foo(), etc. Maybe we should scrap it
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
108 after all.
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
109
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
110 #### What if one of the first two functions signal an error?
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
111 Should we take care to execute the other two? My fingers are
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
112 getting itchy! */
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
113
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
114 function = dlsym (handle, "syms_of");
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
115 if (function)
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
116 {
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
117 function ();
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
118 }
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
119
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
120 function = dlsym (handle, "vars_of");
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
121 if (function)
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
122 {
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
123 function ();
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
124 }
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
125
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
126 function = dlsym (handle, "complex_vars_of");
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
127 if (function)
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
128 {
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
129 function ();
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
130 }
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
131
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
132 return Qnil;
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
133 }
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
134
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
135 void syms_of_dlopen ()
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
136 {
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
137 DEFSUBR (Fdl_open);
51092a27c943 Import from CVS: tag r20-5b21
cvs
parents:
diff changeset
138 }