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