1
/* dlfcn-dyld.c -- provides dlopen() and friends as wrappers around Mach dyld
3
* Author: Ian.Piumarta@INRIA.Fr
5
* Copyright (C) 1996-2006 by Ian Piumarta and other authors/contributors
6
* listed elsewhere in this file.
9
* This file is part of Unix Squeak.
11
* Permission is hereby granted, free of charge, to any person obtaining a copy
12
* of this software and associated documentation files (the "Software"), to deal
13
* in the Software without restriction, including without limitation the rights
14
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
* copies of the Software, and to permit persons to whom the Software is
16
* furnished to do so, subject to the following conditions:
18
* The above copyright notice and this permission notice shall be included in
19
* all copies or substantial portions of the Software.
21
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
* Last edited: 2009-08-19 04:17:17 by piumarta on emilia-2.local
34
#include <mach-o/dyld.h>
39
#define DL_APP_CONTEXT ((void *)-1)
41
static char dlErrorString[256];
42
static int dlErrorSet= 0;
45
static void dlSetError(const char *fmt, ...)
49
vsnprintf(dlErrorString, sizeof(dlErrorString), fmt, ap);
55
static const char *dlerror(void)
60
return (const char *)dlErrorString;
66
static void dlUndefined(const char *symbol)
68
fprintf(stderr, "dyld: undefined symbol: %s\n", symbol);
71
static NSModule dlMultiple(NSSymbol s, NSModule oldModule, NSModule newModule)
73
fdebugf((stderr, "dyld: %s: %s previously defined in %s, new definition in %s\n",
74
NSNameOfSymbol(s), NSNameOfModule(oldModule), NSNameOfModule(newModule)));
78
static void dlLinkEdit(NSLinkEditErrors errorClass, int errorNumber,
79
const char *fileName, const char *errorString)
82
fprintf(stderr, "dyld: %s: %s\n", fileName, errorString);
85
static NSLinkEditErrorHandlers errorHandlers=
92
static void dlinit(void)
94
NSInstallLinkEditErrorHandlers(&errorHandlers);
97
static int dlInitialised= 0;
100
static void *dlopen(const char *path, int mode)
103
NSObjectFileImage ofi= 0;
112
return DL_APP_CONTEXT;
114
switch (NSCreateObjectFileImageFromFile(path, &ofi))
116
case NSObjectFileImageSuccess:
117
handle= NSLinkModule(ofi, path, NSLINKMODULE_OPTION_RETURN_ON_ERROR);
118
NSDestroyObjectFileImage(ofi);
120
case NSObjectFileImageInappropriateFile:
121
handle= (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
129
dlSetError("could not load shared object: %s", path);
131
fdebugf((stderr, "dlopen: %s => %d\n", path, (int)handle));
137
static void *dlsym(void *handle, const char *symbol)
140
NSSymbol *nsSymbol= 0;
142
snprintf(_symbol, sizeof(_symbol), "_%s", symbol);
144
fdebugf((stderr, "dlsym: looking for %s (%s) in %d\n", symbol, _symbol, (int)handle));
148
fdebugf((stderr, "dlsym: setting app context for this handle\n"));
149
handle= DL_APP_CONTEXT;
152
if (DL_APP_CONTEXT == handle)
154
fdebugf((stderr, "dlsym: looking in app context\n"));
155
if (NSIsSymbolNameDefined(_symbol))
156
nsSymbol= NSLookupAndBindSymbol(_symbol);
160
if (( (MH_MAGIC == ((struct mach_header *)handle)->magic)) /* ppc */
161
|| (MH_CIGAM == ((struct mach_header *)handle)->magic)) /* 386 */
163
if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, _symbol))
165
nsSymbol= NSLookupSymbolInImage
166
((struct mach_header *)handle,
168
NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
169
/*| NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR*/);
170
fdebugf((stderr, "dlsym: bundle (image) lookup returned %p\n", nsSymbol));
173
fdebugf((stderr, "dlsym: bundle (image) symbol not defined\n"));
177
nsSymbol= NSLookupSymbolInModule(handle, _symbol);
178
fdebugf((stderr, "dlsym: dylib (module) lookup returned %p\n", nsSymbol));
184
dlSetError("symbol not found: %s", _symbol);
188
return NSAddressOfSymbol(nsSymbol);
192
static int dlclose(void *handle)
194
if (( (MH_MAGIC == ((struct mach_header *)handle)->magic)) /* ppc */
195
|| (MH_CIGAM == ((struct mach_header *)handle)->magic)) /* 386 */
196
return 0; /* can't unlink, but pretend we did */
198
if (!NSUnLinkModule(handle, 0))
200
dlSetError("could not unlink shared object: %s", NSNameOfModule(handle));
208
/* autoconf has bugs */
215
# define HAVE_DLOPEN 1