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>
|
428
|
27 #include "sysdll.h"
|
|
28
|
|
29 /* This whole file is conditional upon HAVE_SHLIB */
|
|
30 #ifdef HAVE_SHLIB
|
|
31
|
|
32 /* Thankfully, most systems follow the ELFish dlopen() method.
|
|
33 ** HAVE__DLOPEN is lame, but SCO has their dl* functions as _dl*, and
|
|
34 ** unless you include dlfcn.h you don't get the macros to mask them, and
|
|
35 ** autoconf fails to find them. No longer true as of 5.0.5.
|
|
36 **
|
|
37 ** Anybody who wants to use this on SCO needs to have their configure.in
|
|
38 ** look for _dlopen() as well as dlopen()
|
|
39 */
|
|
40 #if defined(HAVE_DLOPEN) || defined(HAVE__DLOPEN) || defined(HAVE_DLFCN_H)
|
|
41 #include <dlfcn.h>
|
|
42
|
|
43 #ifndef RTLD_LAZY
|
|
44 # define RTLD_LAZY 1
|
|
45 #endif /* RTLD_LAZY isn't defined under FreeBSD - ick */
|
|
46
|
|
47 #ifndef RTLD_GLOBAL
|
|
48 # define RTLD_GLOBAL 0
|
|
49 #endif
|
|
50
|
|
51 int
|
442
|
52 dll_init (const char *arg)
|
428
|
53 {
|
|
54 return 0;
|
|
55 }
|
|
56
|
|
57 dll_handle
|
442
|
58 dll_open (const char *fname)
|
428
|
59 {
|
442
|
60 return (dll_handle) dlopen (fname, RTLD_LAZY | RTLD_GLOBAL);
|
428
|
61 }
|
|
62
|
|
63 int
|
|
64 dll_close (dll_handle h)
|
|
65 {
|
442
|
66 return dlclose ((void *) h);
|
428
|
67 }
|
|
68
|
|
69 dll_func
|
442
|
70 dll_function (dll_handle h, const char *n)
|
428
|
71 {
|
|
72 #ifdef DLSYM_NEEDS_UNDERSCORE
|
|
73 char *buf = alloca_array (char, strlen (n) + 2);
|
|
74 *buf = '_';
|
442
|
75 strcpy (buf + 1, n);
|
428
|
76 n = buf;
|
|
77 #endif
|
442
|
78 return (dll_func) dlsym ((void *) h, n);
|
428
|
79 }
|
|
80
|
|
81 dll_var
|
442
|
82 dll_variable (dll_handle h, const char *n)
|
428
|
83 {
|
|
84 #ifdef DLSYM_NEEDS_UNDERSCORE
|
|
85 char *buf = alloca_array (char, strlen (n) + 2);
|
|
86 *buf = '_';
|
442
|
87 strcpy (buf + 1, n);
|
428
|
88 n = buf;
|
|
89 #endif
|
|
90 return (dll_var)dlsym ((void *)h, n);
|
|
91 }
|
|
92
|
442
|
93 const char *
|
428
|
94 dll_error (dll_handle h)
|
|
95 {
|
|
96 #if defined(HAVE_DLERROR) || defined(dlerror)
|
442
|
97 return (const char *) dlerror ();
|
428
|
98 #elif defined(HAVE__DLERROR)
|
442
|
99 return (const char *) _dlerror();
|
428
|
100 #else
|
|
101 return "Shared library error";
|
|
102 #endif
|
|
103 }
|
|
104
|
|
105 #elif defined(HAVE_SHL_LOAD)
|
|
106 /* This is the HP/UX version */
|
|
107 #include <dl.h>
|
|
108 int
|
442
|
109 dll_init (const char *arg)
|
428
|
110 {
|
|
111 return 0;
|
|
112 }
|
|
113
|
|
114 dll_handle
|
442
|
115 dll_open (const char *fname)
|
428
|
116 {
|
442
|
117 /* shl_load will hang hard if passed a NULL fname. */
|
|
118 if (fname == NULL) return NULL;
|
428
|
119
|
442
|
120 return (dll_handle) shl_load (fname, BIND_DEFERRED,0L);
|
428
|
121 }
|
|
122
|
|
123 int
|
|
124 dll_close (dll_handle h)
|
|
125 {
|
442
|
126 return shl_unload ((shl_t) h);
|
428
|
127 }
|
|
128
|
|
129 dll_func
|
442
|
130 dll_function (dll_handle h, const char *n)
|
428
|
131 {
|
|
132 long handle = 0L;
|
|
133
|
442
|
134 if (shl_findsym ((shl_t *) &h, n, TYPE_PROCEDURE, &handle))
|
428
|
135 return NULL;
|
|
136
|
442
|
137 return (dll_func) handle;
|
428
|
138 }
|
|
139
|
|
140 dll_var
|
442
|
141 dll_variable (dll_handle h, const char *n)
|
428
|
142 {
|
|
143 long handle = 0L;
|
|
144
|
442
|
145 if (shl_findsym ((shl_t *) &h, n, TYPE_DATA, &handle))
|
428
|
146 return NULL;
|
|
147
|
442
|
148 return (dll_var) handle;
|
428
|
149 }
|
|
150
|
442
|
151 const char *
|
428
|
152 dll_error (dll_handle h)
|
|
153 {
|
|
154 /* #### WTF?! Shouldn't this at least attempt to get strerror or
|
|
155 something? --hniksic */
|
|
156 return "Generic shared library error";
|
|
157 }
|
|
158
|
|
159 #elif defined(HAVE_INIT_DLD)
|
|
160 #include <dld.h>
|
|
161 int
|
442
|
162 dll_init (const char *arg)
|
428
|
163 {
|
|
164 char *real_exe = dld_find_executable (arg);
|
|
165 int rc;
|
|
166
|
|
167 rc = dld_init (real_exe);
|
|
168 if (rc)
|
|
169 {
|
|
170 dld_perror (exe);
|
|
171 return -1;
|
|
172 }
|
|
173 return 0;
|
|
174 }
|
|
175
|
|
176 dll_handle
|
442
|
177 dll_open (const char *fname)
|
428
|
178 {
|
|
179 rc = dld_link (fname);
|
|
180 if (rc)
|
|
181 return NULL;
|
|
182
|
442
|
183 return (dll_handle) 1;
|
428
|
184 }
|
|
185
|
|
186 int
|
|
187 dll_close (dll_handle h)
|
|
188 {
|
|
189 /* *sigh* DLD is pretty lame and doesn't return a handle that you can use
|
|
190 ** later on to free the file - you have to remember the filename and
|
|
191 ** use that as the unlinker. We should eventually keep a linked list
|
|
192 ** of loaded modules and then use the node pointer as the unique id
|
|
193 ** for the shared library. Wheeee. But not now.
|
|
194 */
|
|
195 return 1;
|
|
196 }
|
|
197
|
|
198 DLL_FUNC
|
442
|
199 dll_function (dll_handle h, const char *n)
|
428
|
200 {
|
442
|
201 return dld_get_func (n);
|
428
|
202 }
|
|
203
|
|
204 DLL_FUNC
|
442
|
205 dll_variable (dll_handle h, const char *n)
|
428
|
206 {
|
442
|
207 return dld_get_symbol (n);
|
428
|
208 }
|
442
|
209 #elif defined (WIN32_NATIVE)
|
|
210
|
|
211 #define WIN32_LEAN_AND_MEAN
|
|
212 #include <windows.h>
|
|
213 #undef WIN32_LEAN_AND_MEAN
|
|
214
|
428
|
215 int
|
442
|
216 dll_init (const char *arg)
|
428
|
217 {
|
|
218 return 0;
|
|
219 }
|
|
220
|
|
221 dll_handle
|
442
|
222 dll_open (const char *fname)
|
428
|
223 {
|
442
|
224 return (dll_handle) LoadLibrary (fname);
|
428
|
225 }
|
|
226
|
|
227 int
|
|
228 dll_close (dll_handle h)
|
|
229 {
|
|
230 return FreeLibrary (h);
|
|
231 }
|
|
232
|
|
233 dll_func
|
442
|
234 dll_function (dll_handle h, const char *n)
|
428
|
235 {
|
442
|
236 return (dll_func) GetProcAddress (h, n);
|
428
|
237 }
|
|
238
|
|
239 dll_func
|
442
|
240 dll_variable (dll_handle h, const char *n)
|
428
|
241 {
|
442
|
242 return (dll_func) GetProcAddress (h, n);
|
428
|
243 }
|
|
244
|
442
|
245 const char *
|
428
|
246 dll_error (dll_handle h)
|
|
247 {
|
|
248 return "Windows DLL Error";
|
|
249 }
|
|
250 #else
|
|
251 /* Catchall if we don't know about this systems method of dynamic loading */
|
|
252 int
|
442
|
253 dll_init (const char *arg)
|
428
|
254 {
|
|
255 return -1;
|
|
256 }
|
|
257
|
|
258 dll_handle
|
442
|
259 dll_open (const char *fname)
|
428
|
260 {
|
|
261 return NULL;
|
|
262 }
|
|
263
|
|
264 int
|
|
265 dll_close (dll_handle h)
|
|
266 {
|
|
267 return 0;
|
|
268 }
|
|
269
|
|
270 dll_func
|
442
|
271 dll_function (dll_handle h, const char *n)
|
428
|
272 {
|
|
273 return NULL;
|
|
274 }
|
|
275
|
|
276 dll_func
|
442
|
277 dll_variable (dll_handle h, const char *n)
|
428
|
278 {
|
|
279 return NULL;
|
|
280 }
|
|
281
|
442
|
282 const char *
|
428
|
283 dll_error (dll_handle h)
|
|
284 {
|
|
285 return "Shared libraries not implemented on this system";
|
|
286 }
|
|
287 #endif /* System conditionals */
|
|
288
|
|
289 #endif /* HAVE_SHLIB */
|