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