35 #include <sys/types.h> 39 #include <mach-o/dyld.h> 43 #define ERR_STR_LEN 256 45 static void *
dlsymIntern(
void *handle,
const char *symbol);
47 static const char *
error(
int setget,
const char *str, ...);
51 static const char *
error(
int setget,
const char *str, ...) {
53 static int err_filled = 0;
57 const char *dylderrstr;
67 NSLinkEditError(&ler, &lerno, &file, &dylderrstr);
68 fprintf(stderr,
"dyld: %s\n", dylderrstr);
69 if (dylderrstr && strlen(dylderrstr))
85 extern "C" void *
dlopen(
const char *path,
int mode) {
87 NSObjectFileImage ofi = 0;
88 NSObjectFileImageReturnCode ofirc;
89 static int (*make_private_module_public)(NSModule module) = 0;
90 unsigned int flags = NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE;
98 ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
100 case NSObjectFileImageSuccess:
102 if (!(mode &
RTLD_LAZY)) flags += NSLINKMODULE_OPTION_BINDNOW;
103 module = NSLinkModule(ofi, path, flags);
105 NSDestroyObjectFileImage(ofi);
110 if (!make_private_module_public) {
111 _dyld_func_lookup(
"__dyld_NSMakePrivateModulePublic",
112 (
unsigned long *) &make_private_module_public);
114 make_private_module_public(module);
117 case NSObjectFileImageInappropriateFile:
119 module = (
void *) NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
121 case NSObjectFileImageFailure:
122 error(0,
"Object file setup failure : \"%s\"", path);
124 case NSObjectFileImageArch:
125 error(0,
"No object for this architecture : \"%s\"", path);
127 case NSObjectFileImageFormat:
128 error(0,
"Bad object file format : \"%s\"", path);
130 case NSObjectFileImageAccess:
131 error(0,
"Can't read object file : \"%s\"", path);
135 error(0,
"Can not open \"%s\"", path);
143 if (handle == (
void *) -1) {
145 if (NSIsSymbolNameDefined(symbol)) {
146 nssym = NSLookupAndBindSymbol(symbol);
154 if ((((
struct mach_header *) handle)->magic == MH_MAGIC) ||
155 (((
struct mach_header *) handle)->magic == MH_CIGAM)) {
156 if (NSIsSymbolNameDefinedInImage((
struct mach_header *) handle, symbol)) {
157 nssym = NSLookupSymbolInImage((
struct mach_header *) handle,
159 NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
160 | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
164 nssym = NSLookupSymbolInModule(handle, symbol);
168 error(0,
"Symbol \"%s\" Not found", symbol);
171 return NSAddressOfSymbol(nssym);
179 if ((((
struct mach_header *) handle)->magic == MH_MAGIC) ||
180 (((
struct mach_header *) handle)->magic == MH_CIGAM)) {
181 error(-1,
"Can't remove dynamic libraries on darwin");
184 if (!NSUnLinkModule(handle, 0)) {
185 error(0,
"unable to unlink module %s", NSNameOfModule(handle));
193 extern "C" void *
dlsym(
void *handle,
const char *symbol) {
194 static char undersym[257];
195 int sym_len = strlen(symbol);
197 char *malloc_sym =
NULL;
200 snprintf(undersym, 256,
"_%s", symbol);
203 malloc_sym = malloc(sym_len + 2);
205 sprintf(malloc_sym,
"_%s", symbol);
209 error(-1,
"Unable to allocate memory");
const char * dlerror(void)
sprintf(buf," if (secondorder) {\ " int _i;\" " for(_i=0;_i< %d;++_i) {\" " _p[_slist%d[_i]]+=dt *_p[_dlist%d[_i]];\" " }}\", numeqn, listnum, listnum)
int dlclose(void *handle)
static const char * error(int setget, const char *str,...)
fprintf(stderr, "Don't know the location of params at %p\, pp)
void * dlopen(const char *path, int mode)
static void * dlsymIntern(void *handle, const char *symbol)
static struct @32 flags[]
void * dlsym(void *handle, const char *symbol)