1 | n/a | /* |
---|
2 | n/a | Copyright (c) 2002 Peter O'Gorman <ogorman@users.sourceforge.net> |
---|
3 | n/a | |
---|
4 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
---|
5 | n/a | a copy of this software and associated documentation files (the |
---|
6 | n/a | "Software"), to deal in the Software without restriction, including |
---|
7 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
---|
8 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
---|
9 | n/a | permit persons to whom the Software is furnished to do so, subject to |
---|
10 | n/a | the following conditions: |
---|
11 | n/a | |
---|
12 | n/a | The above copyright notice and this permission notice shall be |
---|
13 | n/a | included in all copies or substantial portions of the Software. |
---|
14 | n/a | |
---|
15 | n/a | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
---|
16 | n/a | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
17 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
---|
18 | n/a | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
---|
19 | n/a | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
---|
20 | n/a | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
---|
21 | n/a | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
22 | n/a | */ |
---|
23 | n/a | |
---|
24 | n/a | |
---|
25 | n/a | /* Just to prove that it isn't that hard to add Mac calls to your code :) |
---|
26 | n/a | This works with pretty much everything, including kde3 xemacs and the gimp, |
---|
27 | n/a | I'd guess that it'd work in at least 95% of cases, use this as your starting |
---|
28 | n/a | point, rather than the mess that is dlfcn.c, assuming that your code does not |
---|
29 | n/a | require ref counting or symbol lookups in dependent libraries |
---|
30 | n/a | */ |
---|
31 | n/a | |
---|
32 | n/a | #include <stdio.h> |
---|
33 | n/a | #include <stdlib.h> |
---|
34 | n/a | #include <string.h> |
---|
35 | n/a | #include <sys/types.h> |
---|
36 | n/a | #include <sys/stat.h> |
---|
37 | n/a | #include <stdarg.h> |
---|
38 | n/a | #include <limits.h> |
---|
39 | n/a | #include <mach-o/dyld.h> |
---|
40 | n/a | #include <AvailabilityMacros.h> |
---|
41 | n/a | #include "dlfcn.h" |
---|
42 | n/a | |
---|
43 | n/a | #ifdef CTYPES_DARWIN_DLFCN |
---|
44 | n/a | |
---|
45 | n/a | #define ERR_STR_LEN 256 |
---|
46 | n/a | |
---|
47 | n/a | #ifndef MAC_OS_X_VERSION_10_3 |
---|
48 | n/a | #define MAC_OS_X_VERSION_10_3 1030 |
---|
49 | n/a | #endif |
---|
50 | n/a | |
---|
51 | n/a | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 |
---|
52 | n/a | #define DARWIN_HAS_DLOPEN |
---|
53 | n/a | extern void * dlopen(const char *path, int mode) __attribute__((weak_import)); |
---|
54 | n/a | extern void * dlsym(void * handle, const char *symbol) __attribute__((weak_import)); |
---|
55 | n/a | extern const char * dlerror(void) __attribute__((weak_import)); |
---|
56 | n/a | extern int dlclose(void * handle) __attribute__((weak_import)); |
---|
57 | n/a | extern int dladdr(const void *, Dl_info *) __attribute__((weak_import)); |
---|
58 | n/a | #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 */ |
---|
59 | n/a | |
---|
60 | n/a | #ifndef DARWIN_HAS_DLOPEN |
---|
61 | n/a | #define dlopen darwin_dlopen |
---|
62 | n/a | #define dlsym darwin_dlsym |
---|
63 | n/a | #define dlerror darwin_dlerror |
---|
64 | n/a | #define dlclose darwin_dlclose |
---|
65 | n/a | #define dladdr darwin_dladdr |
---|
66 | n/a | #endif |
---|
67 | n/a | |
---|
68 | n/a | void * (*ctypes_dlopen)(const char *path, int mode); |
---|
69 | n/a | void * (*ctypes_dlsym)(void * handle, const char *symbol); |
---|
70 | n/a | const char * (*ctypes_dlerror)(void); |
---|
71 | n/a | int (*ctypes_dlclose)(void * handle); |
---|
72 | n/a | int (*ctypes_dladdr)(const void *, Dl_info *); |
---|
73 | n/a | |
---|
74 | n/a | #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3 |
---|
75 | n/a | /* Mac OS X 10.3+ has dlopen, so strip all this dead code to avoid warnings */ |
---|
76 | n/a | |
---|
77 | n/a | static void *dlsymIntern(void *handle, const char *symbol); |
---|
78 | n/a | |
---|
79 | n/a | static const char *error(int setget, const char *str, ...); |
---|
80 | n/a | |
---|
81 | n/a | /* Set and get the error string for use by dlerror */ |
---|
82 | n/a | static const char *error(int setget, const char *str, ...) |
---|
83 | n/a | { |
---|
84 | n/a | static char errstr[ERR_STR_LEN]; |
---|
85 | n/a | static int err_filled = 0; |
---|
86 | n/a | const char *retval; |
---|
87 | n/a | va_list arg; |
---|
88 | n/a | if (setget == 0) |
---|
89 | n/a | { |
---|
90 | n/a | va_start(arg, str); |
---|
91 | n/a | strncpy(errstr, "dlcompat: ", ERR_STR_LEN); |
---|
92 | n/a | vsnprintf(errstr + 10, ERR_STR_LEN - 10, str, arg); |
---|
93 | n/a | va_end(arg); |
---|
94 | n/a | err_filled = 1; |
---|
95 | n/a | retval = NULL; |
---|
96 | n/a | } |
---|
97 | n/a | else |
---|
98 | n/a | { |
---|
99 | n/a | if (!err_filled) |
---|
100 | n/a | retval = NULL; |
---|
101 | n/a | else |
---|
102 | n/a | retval = errstr; |
---|
103 | n/a | err_filled = 0; |
---|
104 | n/a | } |
---|
105 | n/a | return retval; |
---|
106 | n/a | } |
---|
107 | n/a | |
---|
108 | n/a | /* darwin_dlopen */ |
---|
109 | n/a | static void *darwin_dlopen(const char *path, int mode) |
---|
110 | n/a | { |
---|
111 | n/a | void *module = 0; |
---|
112 | n/a | NSObjectFileImage ofi = 0; |
---|
113 | n/a | NSObjectFileImageReturnCode ofirc; |
---|
114 | n/a | |
---|
115 | n/a | /* If we got no path, the app wants the global namespace, use -1 as the marker |
---|
116 | n/a | in this case */ |
---|
117 | n/a | if (!path) |
---|
118 | n/a | return (void *)-1; |
---|
119 | n/a | |
---|
120 | n/a | /* Create the object file image, works for things linked with the -bundle arg to ld */ |
---|
121 | n/a | ofirc = NSCreateObjectFileImageFromFile(path, &ofi); |
---|
122 | n/a | switch (ofirc) |
---|
123 | n/a | { |
---|
124 | n/a | case NSObjectFileImageSuccess: |
---|
125 | n/a | /* It was okay, so use NSLinkModule to link in the image */ |
---|
126 | n/a | module = NSLinkModule(ofi, path, |
---|
127 | n/a | NSLINKMODULE_OPTION_RETURN_ON_ERROR |
---|
128 | n/a | | (mode & RTLD_GLOBAL) ? 0 : NSLINKMODULE_OPTION_PRIVATE |
---|
129 | n/a | | (mode & RTLD_LAZY) ? 0 : NSLINKMODULE_OPTION_BINDNOW); |
---|
130 | n/a | NSDestroyObjectFileImage(ofi); |
---|
131 | n/a | break; |
---|
132 | n/a | case NSObjectFileImageInappropriateFile: |
---|
133 | n/a | /* It may have been a dynamic library rather than a bundle, try to load it */ |
---|
134 | n/a | module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR); |
---|
135 | n/a | break; |
---|
136 | n/a | default: |
---|
137 | n/a | /* God knows what we got */ |
---|
138 | n/a | error(0, "Can not open \"%s\"", path); |
---|
139 | n/a | return 0; |
---|
140 | n/a | } |
---|
141 | n/a | if (!module) |
---|
142 | n/a | error(0, "Can not open \"%s\"", path); |
---|
143 | n/a | return module; |
---|
144 | n/a | |
---|
145 | n/a | } |
---|
146 | n/a | |
---|
147 | n/a | /* dlsymIntern is used by dlsym to find the symbol */ |
---|
148 | n/a | static void *dlsymIntern(void *handle, const char *symbol) |
---|
149 | n/a | { |
---|
150 | n/a | NSSymbol nssym = 0; |
---|
151 | n/a | /* If the handle is -1, if is the app global context */ |
---|
152 | n/a | if (handle == (void *)-1) |
---|
153 | n/a | { |
---|
154 | n/a | /* Global context, use NSLookupAndBindSymbol */ |
---|
155 | n/a | if (NSIsSymbolNameDefined(symbol)) |
---|
156 | n/a | { |
---|
157 | n/a | nssym = NSLookupAndBindSymbol(symbol); |
---|
158 | n/a | } |
---|
159 | n/a | |
---|
160 | n/a | } |
---|
161 | n/a | /* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image |
---|
162 | n/a | for libraries, and NSLookupSymbolInModule for bundles */ |
---|
163 | n/a | else |
---|
164 | n/a | { |
---|
165 | n/a | /* Check for both possible magic numbers depending on x86/ppc byte order */ |
---|
166 | n/a | if ((((struct mach_header *)handle)->magic == MH_MAGIC) || |
---|
167 | n/a | (((struct mach_header *)handle)->magic == MH_CIGAM)) |
---|
168 | n/a | { |
---|
169 | n/a | if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol)) |
---|
170 | n/a | { |
---|
171 | n/a | nssym = NSLookupSymbolInImage((struct mach_header *)handle, |
---|
172 | n/a | symbol, |
---|
173 | n/a | NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
---|
174 | n/a | | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); |
---|
175 | n/a | } |
---|
176 | n/a | |
---|
177 | n/a | } |
---|
178 | n/a | else |
---|
179 | n/a | { |
---|
180 | n/a | nssym = NSLookupSymbolInModule(handle, symbol); |
---|
181 | n/a | } |
---|
182 | n/a | } |
---|
183 | n/a | if (!nssym) |
---|
184 | n/a | { |
---|
185 | n/a | error(0, "Symbol \"%s\" Not found", symbol); |
---|
186 | n/a | return NULL; |
---|
187 | n/a | } |
---|
188 | n/a | return NSAddressOfSymbol(nssym); |
---|
189 | n/a | } |
---|
190 | n/a | |
---|
191 | n/a | static const char *darwin_dlerror(void) |
---|
192 | n/a | { |
---|
193 | n/a | return error(1, (char *)NULL); |
---|
194 | n/a | } |
---|
195 | n/a | |
---|
196 | n/a | static int darwin_dlclose(void *handle) |
---|
197 | n/a | { |
---|
198 | n/a | if ((((struct mach_header *)handle)->magic == MH_MAGIC) || |
---|
199 | n/a | (((struct mach_header *)handle)->magic == MH_CIGAM)) |
---|
200 | n/a | { |
---|
201 | n/a | error(0, "Can't remove dynamic libraries on darwin"); |
---|
202 | n/a | return 0; |
---|
203 | n/a | } |
---|
204 | n/a | if (!NSUnLinkModule(handle, 0)) |
---|
205 | n/a | { |
---|
206 | n/a | error(0, "unable to unlink module %s", NSNameOfModule(handle)); |
---|
207 | n/a | return 1; |
---|
208 | n/a | } |
---|
209 | n/a | return 0; |
---|
210 | n/a | } |
---|
211 | n/a | |
---|
212 | n/a | |
---|
213 | n/a | /* dlsym, prepend the underscore and call dlsymIntern */ |
---|
214 | n/a | static void *darwin_dlsym(void *handle, const char *symbol) |
---|
215 | n/a | { |
---|
216 | n/a | static char undersym[257]; /* Saves calls to malloc(3) */ |
---|
217 | n/a | int sym_len = strlen(symbol); |
---|
218 | n/a | void *value = NULL; |
---|
219 | n/a | char *malloc_sym = NULL; |
---|
220 | n/a | |
---|
221 | n/a | if (sym_len < 256) |
---|
222 | n/a | { |
---|
223 | n/a | snprintf(undersym, 256, "_%s", symbol); |
---|
224 | n/a | value = dlsymIntern(handle, undersym); |
---|
225 | n/a | } |
---|
226 | n/a | else |
---|
227 | n/a | { |
---|
228 | n/a | malloc_sym = malloc(sym_len + 2); |
---|
229 | n/a | if (malloc_sym) |
---|
230 | n/a | { |
---|
231 | n/a | sprintf(malloc_sym, "_%s", symbol); |
---|
232 | n/a | value = dlsymIntern(handle, malloc_sym); |
---|
233 | n/a | free(malloc_sym); |
---|
234 | n/a | } |
---|
235 | n/a | else |
---|
236 | n/a | { |
---|
237 | n/a | error(0, "Unable to allocate memory"); |
---|
238 | n/a | } |
---|
239 | n/a | } |
---|
240 | n/a | return value; |
---|
241 | n/a | } |
---|
242 | n/a | |
---|
243 | n/a | static int darwin_dladdr(const void *handle, Dl_info *info) { |
---|
244 | n/a | return 0; |
---|
245 | n/a | } |
---|
246 | n/a | #endif /* MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3 */ |
---|
247 | n/a | |
---|
248 | n/a | #if __GNUC__ < 4 |
---|
249 | n/a | #pragma CALL_ON_LOAD ctypes_dlfcn_init |
---|
250 | n/a | #else |
---|
251 | n/a | static void __attribute__ ((constructor)) ctypes_dlfcn_init(void); |
---|
252 | n/a | static |
---|
253 | n/a | #endif |
---|
254 | n/a | void ctypes_dlfcn_init(void) { |
---|
255 | n/a | if (dlopen != NULL) { |
---|
256 | n/a | ctypes_dlsym = dlsym; |
---|
257 | n/a | ctypes_dlopen = dlopen; |
---|
258 | n/a | ctypes_dlerror = dlerror; |
---|
259 | n/a | ctypes_dlclose = dlclose; |
---|
260 | n/a | ctypes_dladdr = dladdr; |
---|
261 | n/a | } else { |
---|
262 | n/a | #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3 |
---|
263 | n/a | ctypes_dlsym = darwin_dlsym; |
---|
264 | n/a | ctypes_dlopen = darwin_dlopen; |
---|
265 | n/a | ctypes_dlerror = darwin_dlerror; |
---|
266 | n/a | ctypes_dlclose = darwin_dlclose; |
---|
267 | n/a | ctypes_dladdr = darwin_dladdr; |
---|
268 | n/a | #endif /* MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3 */ |
---|
269 | n/a | } |
---|
270 | n/a | } |
---|
271 | n/a | |
---|
272 | n/a | #endif /* CTYPES_DARWIN_DLFCN */ |
---|