#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, ...);
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, "dlsimple: ", 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;
}
void *dlopen(const char *path, int mode)
{
void *module = 0;
NSObjectFileImage ofi = 0;
NSObjectFileImageReturnCode ofirc;
static int (*make_private_module_public) (NSModule module) = 0;
unsigned int flags = NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE;
if (!path)
return (void *)-1;
ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
switch (ofirc)
{
case NSObjectFileImageSuccess:
if (!(mode & RTLD_LAZY)) flags += NSLINKMODULE_OPTION_BINDNOW;
module = NSLinkModule(ofi, path,flags);
NSDestroyObjectFileImage(ofi);
if ((mode & RTLD_GLOBAL))
{
if (!make_private_module_public)
{
_dyld_func_lookup("__dyld_NSMakePrivateModulePublic",
(unsigned long *)&make_private_module_public);
}
make_private_module_public(module);
}
break;
case NSObjectFileImageInappropriateFile:
module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
break;
case NSObjectFileImageFailure:
error(0,"Object file setup failure : \"%s\"", path);
return 0;
case NSObjectFileImageArch:
error(0,"No object for this architecture : \"%s\"", path);
return 0;
case NSObjectFileImageFormat:
error(0,"Bad object file format : \"%s\"", path);
return 0;
case NSObjectFileImageAccess:
error(0,"Can't read object file : \"%s\"", path);
return 0;
}
if (!module)
error(0, "Can not open \"%s\"", path);
return module;
}
void *dlsymIntern(void *handle, const char *symbol)
{
NSSymbol *nssym = 0;
if (handle == (void *)-1)
{
if (NSIsSymbolNameDefined(symbol))
{
nssym = NSLookupAndBindSymbol(symbol);
}
}
else
{
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;
}
void *dlsym(void *handle, const char *symbol)
{
static char undersym[257];
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;
}