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