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 211 212 213 214 215 216
|
/* dlfcn-dyld.c -- provides dlopen() and friends as wrappers around Mach dyld
*
* Author: Ian.Piumarta@INRIA.Fr
*
* Copyright (C) 1996-2006 by Ian Piumarta and other authors/contributors
* listed elsewhere in this file.
* All rights reserved.
*
* This file is part of Unix Squeak.
*
* 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.
*
* Last edited: 2009-08-19 04:17:17 by piumarta on emilia-2.local
*/
#include <stdio.h>
#include <stdarg.h>
#include <mach-o/dyld.h>
#define RTLD_NOW 0
#define RTLD_GLOBAL 0
#define DL_APP_CONTEXT ((void *)-1)
static char dlErrorString[256];
static int dlErrorSet= 0;
static void dlSetError(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(dlErrorString, sizeof(dlErrorString), fmt, ap);
va_end(ap);
dlErrorSet= 1;
}
static const char *dlerror(void)
{
if (dlErrorSet)
{
dlErrorSet= 0;
return (const char *)dlErrorString;
}
return 0;
}
static void dlUndefined(const char *symbol)
{
fprintf(stderr, "dyld: undefined symbol: %s\n", symbol);
}
static NSModule dlMultiple(NSSymbol s, NSModule oldModule, NSModule newModule)
{
fdebugf((stderr, "dyld: %s: %s previously defined in %s, new definition in %s\n",
NSNameOfSymbol(s), NSNameOfModule(oldModule), NSNameOfModule(newModule)));
return newModule;
}
static void dlLinkEdit(NSLinkEditErrors errorClass, int errorNumber,
const char *fileName, const char *errorString)
{
fprintf(stderr, "dyld: %s: %s\n", fileName, errorString);
}
static NSLinkEditErrorHandlers errorHandlers=
{
dlUndefined,
dlMultiple,
dlLinkEdit
};
static void dlinit(void)
{
NSInstallLinkEditErrorHandlers(&errorHandlers);
}
static int dlInitialised= 0;
static void *dlopen(const char *path, int mode)
{
void *handle= 0;
NSObjectFileImage ofi= 0;
if (!dlInitialised)
{
dlinit();
dlInitialised= 1;
}
if (!path)
return DL_APP_CONTEXT;
switch (NSCreateObjectFileImageFromFile(path, &ofi))
{
case NSObjectFileImageSuccess:
handle= NSLinkModule(ofi, path, NSLINKMODULE_OPTION_RETURN_ON_ERROR);
NSDestroyObjectFileImage(ofi);
break;
case NSObjectFileImageInappropriateFile:
handle= (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
break;
default:
handle= 0;
break;
}
if (!handle)
dlSetError("could not load shared object: %s", path);
fdebugf((stderr, "dlopen: %s => %d\n", path, (int)handle));
return handle;
}
static void *dlsym(void *handle, const char *symbol)
{
char _symbol[256];
NSSymbol *nsSymbol= 0;
snprintf(_symbol, sizeof(_symbol), "_%s", symbol);
fdebugf((stderr, "dlsym: looking for %s (%s) in %d\n", symbol, _symbol, (int)handle));
if (!handle)
{
fdebugf((stderr, "dlsym: setting app context for this handle\n"));
handle= DL_APP_CONTEXT;
}
if (DL_APP_CONTEXT == handle)
{
fdebugf((stderr, "dlsym: looking in app context\n"));
if (NSIsSymbolNameDefined(_symbol))
nsSymbol= NSLookupAndBindSymbol(_symbol);
}
else
{
if (( (MH_MAGIC == ((struct mach_header *)handle)->magic)) /* ppc */
|| (MH_CIGAM == ((struct mach_header *)handle)->magic)) /* 386 */
{
if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, _symbol))
{
nsSymbol= NSLookupSymbolInImage
((struct mach_header *)handle,
_symbol,
NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
/*| NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR*/);
fdebugf((stderr, "dlsym: bundle (image) lookup returned %p\n", nsSymbol));
}
else
fdebugf((stderr, "dlsym: bundle (image) symbol not defined\n"));
}
else
{
nsSymbol= NSLookupSymbolInModule(handle, _symbol);
fdebugf((stderr, "dlsym: dylib (module) lookup returned %p\n", nsSymbol));
}
}
if (!nsSymbol)
{
dlSetError("symbol not found: %s", _symbol);
return 0;
}
return NSAddressOfSymbol(nsSymbol);
}
static int dlclose(void *handle)
{
if (( (MH_MAGIC == ((struct mach_header *)handle)->magic)) /* ppc */
|| (MH_CIGAM == ((struct mach_header *)handle)->magic)) /* 386 */
return 0; /* can't unlink, but pretend we did */
if (!NSUnLinkModule(handle, 0))
{
dlSetError("could not unlink shared object: %s", NSNameOfModule(handle));
return -1;
}
return 0;
}
/* autoconf has bugs */
#ifdef HAVE_DLFCN_H
# undef HAVE_DLFCN_H
#endif
#ifndef HAVE_DLOPEN
# define HAVE_DLOPEN 1
#endif
|