2
Copyright (c) 2002 Peter O'Gorman <ogorman@users.sourceforge.net>
4
Permission is hereby granted, free of charge, to any person obtaining
5
a copy of this software and associated documentation files (the
6
"Software"), to deal in the Software without restriction, including
7
without limitation the rights to use, copy, modify, merge, publish,
8
distribute, sublicense, and/or sell copies of the Software, and to
9
permit persons to whom the Software is furnished to do so, subject to
10
the following conditions:
12
The above copyright notice and this permission notice shall be
13
included in all copies or substantial portions of the Software.
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
/* Just to prove that it isn't that hard to add Mac calls to your code :)
26
This works with pretty much everything, including kde3 xemacs and the gimp,
27
I'd guess that it'd work in at least 95% of cases, use this as your starting
28
point, rather than the mess that is dlfcn.c, assuming that your code does not
29
require ref counting or symbol lookups in dependent libraries
35
#include <sys/types.h>
39
#include <mach-o/dyld.h>
42
#define ERR_STR_LEN 256
43
static void *dlsymIntern(void *handle, const char *symbol);
44
static const char *error(int setget, const char *str, ...);
48
/* Set and get the error string for use by dlerror */
49
static const char *error(int setget, const char *str, ...)
51
static char errstr[ERR_STR_LEN];
52
static int err_filled = 0;
56
const char *dylderrstr;
62
strncpy(errstr, "dlsimple: ", ERR_STR_LEN);
63
vsnprintf(errstr + 10, ERR_STR_LEN - 10, str, arg);
65
/* We prefer to use the dyld error string if getset is 1*/
67
NSLinkEditError(&ler, &lerno, &file, &dylderrstr);
68
fprintf(stderr,"dyld: %s\n",dylderrstr);
69
if (dylderrstr && strlen(dylderrstr))
70
strncpy(errstr,dylderrstr,ERR_STR_LEN);
87
void *dlopen(const char *path, int mode)
90
NSObjectFileImage ofi = 0;
91
NSObjectFileImageReturnCode ofirc;
92
static int (*make_private_module_public) (NSModule module) = 0;
93
unsigned int flags = NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE;
95
/* If we got no path, the app wants the global namespace, use -1 as the marker
100
/* Create the object file image, works for things linked with the -bundle arg to ld */
101
ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
104
case NSObjectFileImageSuccess:
105
/* It was okay, so use NSLinkModule to link in the image */
106
if (!(mode & RTLD_LAZY)) flags += NSLINKMODULE_OPTION_BINDNOW;
107
module = NSLinkModule(ofi, path,flags);
108
/* Don't forget to destroy the object file image, unless you like leaks */
109
NSDestroyObjectFileImage(ofi);
110
/* If the mode was global, then change the module, this avoids
111
multiply defined symbol errors to first load private then make
112
global. Silly, isn't it. */
113
if ((mode & RTLD_GLOBAL))
115
if (!make_private_module_public)
117
_dyld_func_lookup("__dyld_NSMakePrivateModulePublic",
118
(unsigned long *)&make_private_module_public);
120
make_private_module_public(module);
123
case NSObjectFileImageInappropriateFile:
124
/* It may have been a dynamic library rather than a bundle, try to load it */
125
module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
127
case NSObjectFileImageFailure:
128
error(0,"Object file setup failure : \"%s\"", path);
130
case NSObjectFileImageArch:
131
error(0,"No object for this architecture : \"%s\"", path);
133
case NSObjectFileImageFormat:
134
error(0,"Bad object file format : \"%s\"", path);
136
case NSObjectFileImageAccess:
137
error(0,"Can't read object file : \"%s\"", path);
141
error(0, "Can not open \"%s\"", path);
145
/* dlsymIntern is used by dlsym to find the symbol */
146
void *dlsymIntern(void *handle, const char *symbol)
149
/* If the handle is -1, if is the app global context */
150
if (handle == (void *)-1)
152
/* Global context, use NSLookupAndBindSymbol */
153
if (NSIsSymbolNameDefined(symbol))
155
nssym = NSLookupAndBindSymbol(symbol);
159
/* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image
160
for libraries, and NSLookupSymbolInModule for bundles */
163
/* Check for both possible magic numbers depending on x86/ppc byte order */
164
if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
165
(((struct mach_header *)handle)->magic == MH_CIGAM))
167
if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol))
169
nssym = NSLookupSymbolInImage((struct mach_header *)handle,
171
NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
172
| NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
178
nssym = NSLookupSymbolInModule(handle, symbol);
183
error(0, "Symbol \"%s\" Not found", symbol);
186
return NSAddressOfSymbol(nssym);
189
const char *dlerror(void)
191
return error(1, (char *)NULL);
194
int dlclose(void *handle)
196
if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
197
(((struct mach_header *)handle)->magic == MH_CIGAM))
199
error(-1, "Can't remove dynamic libraries on darwin");
202
if (!NSUnLinkModule(handle, 0))
204
error(0, "unable to unlink module %s", NSNameOfModule(handle));
211
/* dlsym, prepend the underscore and call dlsymIntern */
212
void *dlsym(void *handle, const char *symbol)
214
static char undersym[257]; /* Saves calls to malloc(3) */
215
int sym_len = strlen(symbol);
217
char *malloc_sym = NULL;
221
snprintf(undersym, 256, "_%s", symbol);
222
value = dlsymIntern(handle, undersym);
226
malloc_sym = malloc(sym_len + 2);
229
sprintf(malloc_sym, "_%s", symbol);
230
value = dlsymIntern(handle, malloc_sym);
235
error(-1, "Unable to allocate memory");