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
|
|
47 #define RTLD_LAZY 1
|
|
48 #endif /* RTLD_LAZY isn't defined under FreeBSD - ick */
|
|
49
|
|
50 #ifndef RTLD_GLOBAL
|
|
51 #define RTLD_GLOBAL 0
|
|
52 #endif
|
|
53
|
|
54 int dll_init(CONST char *arg) {
|
|
55 return(0);
|
|
56 }
|
|
57
|
|
58 dll_handle dll_open(CONST char *fname) {
|
|
59 return((dll_handle)dlopen(fname,RTLD_LAZY|RTLD_GLOBAL));
|
|
60 }
|
|
61
|
|
62 int dll_close(dll_handle h) {
|
|
63 return(dlclose((void *)h));
|
|
64 }
|
|
65
|
|
66 dll_func dll_function(dll_handle h,CONST char *n) {
|
|
67 #ifdef DLSYM_NEEDS_UNDERSCORE
|
|
68 char buf[1024];
|
|
69 *buf = '_';
|
|
70 (void)strcpy(buf + 1, n);
|
|
71 n = buf;
|
|
72 #endif
|
|
73 return((dll_func)dlsym((void *)h,n));
|
|
74 }
|
|
75
|
|
76 dll_var dll_variable(dll_handle h,CONST char *n) {
|
|
77 return((dll_var)dlsym((void *)h,n));
|
|
78 }
|
|
79
|
|
80 CONST char *dll_error(dll_handle h) {
|
|
81 #ifdef HAVE_DLERROR
|
|
82 return((CONST char *)dl_error());
|
|
83 #else
|
|
84 return("Shared library error");
|
|
85 #endif
|
|
86 }
|
|
87
|
|
88 #elif defined(HAVE_SHL_LOAD)
|
|
89 /* This is the HP/UX version */
|
|
90 #include <dl.h>
|
|
91 int dll_init(CONST char *arg) {
|
|
92 return(0);
|
|
93 }
|
|
94
|
|
95 dll_handle dll_open(CONST char *fname) {
|
|
96 return((dll_handle)shl_load(fname,BIND_DEFERRED,0L));
|
|
97 }
|
|
98
|
|
99 int dll_close(dll_handle h) {
|
|
100 return (shl_unload((shl_t)h));
|
|
101 }
|
|
102
|
|
103 dll_func dll_function(dll_handle h,CONST char *n) {
|
|
104 long handle = 0L;
|
|
105
|
|
106 if (shl_findsym(&(shl_t)h,n,TYPE_PROCEDURE,&handle))
|
|
107 return(NULL);
|
|
108
|
|
109 return((dll_func)handle);
|
|
110 }
|
|
111
|
|
112 dll_var dll_variable(dll_handle h,CONST char *n) {
|
|
113 long handle = 0L;
|
|
114
|
|
115 if (shl_findsym(&(shl_t)h,n,TYPE_DATA,&handle))
|
|
116 return(NULL);
|
|
117
|
|
118 return((dll_var)handle);
|
|
119 }
|
|
120
|
|
121 CONST char *dll_error(dll_handle h) {
|
|
122 return("Generic shared library error");
|
|
123 }
|
|
124
|
|
125 #elif defined(HAVE_INIT_DLD)
|
|
126 #include <dld.h>
|
|
127 int dll_init(CONST char *arg) {
|
|
128 char *real_exe = dld_find_executable(arg);
|
|
129 int rc;
|
|
130
|
|
131 rc = dld_init(real_exe);
|
|
132 if (rc) {
|
|
133 dld_perror (exe);
|
|
134 return(-1);
|
|
135 }
|
|
136 return(0);
|
|
137 }
|
|
138
|
|
139 dll_handle dll_open(CONST char *fname) {
|
|
140 rc = dld_link(fname);
|
|
141 if (rc) {
|
|
142 return (NULL);
|
|
143 }
|
|
144 return((dll_handle)1);
|
|
145 }
|
|
146
|
|
147 int dll_close(dll_handle h) {
|
|
148 /* *sigh* DLD is pretty lame and doesn't return a handle that you can use
|
|
149 ** later on to free the file - you have to remember the filename and
|
|
150 ** use that as the unlinker. We should eventually keep a linked list
|
|
151 ** of loaded modules and then use the node pointer as the unique id
|
|
152 ** for the shared library. Wheeee. But not now.
|
|
153 */
|
|
154 return(1);
|
|
155 }
|
|
156
|
|
157 DLL_FUNC dll_function(dll_handle h,CONST char *n) {
|
|
158 return (dld_get_func(n));
|
|
159 }
|
|
160
|
|
161 DLL_FUNC dll_variable(dll_handle h,CONST char *n) {
|
|
162 return (dld_get_symbol(n));
|
|
163 }
|
|
164 #elif defined(_WINDOWS) || defined(WIN32)
|
|
165 int dll_init(CONST char *arg) {
|
|
166 return(0);
|
|
167 }
|
|
168
|
|
169 dll_handle dll_open(CONST char *fname) {
|
|
170 return((dll_handle)LoadLibrary(fname));
|
|
171 }
|
|
172
|
|
173 int dll_close(dll_handle h) {
|
|
174 return(FreeLibrary(h));
|
|
175 }
|
|
176
|
|
177 dll_func dll_function(dll_handle h,CONST char *n) {
|
|
178 return((dll_func)GetProcAddress(h,n));
|
|
179 }
|
|
180
|
|
181 dll_func dll_variable(dll_handle h,CONST char *n) {
|
|
182 return((dll_func)GetProcAddress(h,n));
|
|
183 }
|
|
184
|
|
185 CONST char *dll_error(dll_handle h) {
|
|
186 return("Windows DLL Error");
|
|
187 }
|
|
188 #else
|
|
189 /* Catchall if we don't know about this systems method of dynamic loading */
|
|
190 int dll_init(CONST char *arg) {
|
|
191 return(-1);
|
|
192 }
|
|
193
|
|
194 dll_handle dll_open(CONST char *fname) {
|
|
195 return(NULL);
|
|
196 }
|
|
197
|
|
198 int dll_close(dll_handle h) {
|
|
199 return(0);
|
|
200 }
|
|
201
|
|
202 dll_func dll_function(dll_handle h,CONST char *n) {
|
|
203 return(NULL);
|
|
204 }
|
|
205
|
|
206 dll_func dll_variable(dll_handle h,CONST char *n) {
|
|
207 return(NULL);
|
|
208 }
|
|
209
|
|
210 CONST char *dll_error(dll_handle h) {
|
|
211 return("Shared libraries not implemented on this system.");
|
|
212 }
|
|
213 #endif /* System conditionals */
|
|
214
|
|
215 #endif /* HAVE_SHLIB */
|