#ifdef SHLIB
#include "shlib.h"
#endif
#import <stdlib.h>
#import <string.h>
#import <stdio.h>
#ifndef __OPENSTEP__
#import <crt_externs.h>
#endif
#import "stuff/bool.h"
#import <mach-o/dyld.h>
#import <mach-o/ldsyms.h>
static enum bool names_match(
char *install_name,
const char *libraryName);
void NSInstallLinkEditErrorHandlers(
NSLinkEditErrorHandlers *handlers)
{
static void (*p)(
void (*undefined)(const char *symbol_name),
NSModule (*multiple)(NSSymbol s, NSModule old, NSModule new),
void (*linkEdit)(NSLinkEditErrors c, int errorNumber,
const char *fileName, const char *errorString)) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_install_handlers", (unsigned long *)&p);
p(handlers->undefined, handlers->multiple, handlers->linkEdit);
}
const char *
NSNameOfModule(
NSModule module)
{
static char * (*p)(NSModule module) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSNameOfModule", (unsigned long *)&p);
return(p(module));
}
const char *
NSLibraryNameForModule(
NSModule module)
{
static char * (*p)(NSModule module) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSLibraryNameForModule",
(unsigned long *)&p);
return(p(module));
}
enum bool
NSIsSymbolNameDefined(
const char *symbolName)
{
static enum bool (*p)(const char *symbolName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSIsSymbolNameDefined",
(unsigned long *)&p);
return(p(symbolName));
}
enum bool
NSIsSymbolNameDefinedWithHint(
const char *symbolName,
const char *libraryNameHint)
{
static enum bool (*p)(const char *symbolName,
const char *libraryNameHint) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSIsSymbolNameDefinedWithHint",
(unsigned long *)&p);
return(p(symbolName, libraryNameHint));
}
enum bool
NSIsSymbolNameDefinedInImage(
const struct mach_header *image,
const char *symbolName)
{
static enum bool (*p)(const struct mach_header *image,
const char *symbolName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSIsSymbolNameDefinedInImage",
(unsigned long *)&p);
return(p(image, symbolName));
}
NSSymbol
NSLookupAndBindSymbol(
const char *symbolName)
{
static NSSymbol (*p)(const char *symbolName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSLookupAndBindSymbol",
(unsigned long *)&p);
return(p(symbolName));
}
NSSymbol
NSLookupAndBindSymbolWithHint(
const char *symbolName,
const char *libraryNameHint)
{
static NSSymbol (*p)(const char *symbolName,
const char *libraryNameHint) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSLookupAndBindSymbolWithHint",
(unsigned long *)&p);
return(p(symbolName, libraryNameHint));
}
NSSymbol
NSLookupSymbolInModule(
NSModule module,
const char *symbolName)
{
static NSSymbol (*p)(NSModule module, const char *symbolName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSLookupSymbolInModule",
(unsigned long *)&p);
return(p(module, symbolName));
}
NSSymbol
NSLookupSymbolInImage(
const struct mach_header *image,
const char *symbolName,
unsigned long options)
{
static NSSymbol (*p)(const struct mach_header *image,
const char *symbolName,
unsigned long options) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSLookupSymbolInImage",
(unsigned long *)&p);
return(p(image, symbolName, options));
}
const char *
NSNameOfSymbol(
NSSymbol symbol)
{
static char * (*p)(NSSymbol symbol) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSNameOfSymbol", (unsigned long *)&p);
return(p(symbol));
}
void *
NSAddressOfSymbol(
NSSymbol symbol)
{
static void * (*p)(NSSymbol symbol) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSAddressOfSymbol", (unsigned long *)&p);
return(p(symbol));
}
NSModule
NSModuleForSymbol(
NSSymbol symbol)
{
static NSModule (*p)(NSSymbol symbol) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSModuleForSymbol", (unsigned long *)&p);
return(p(symbol));
}
enum bool
NSAddLibrary(
const char *pathName)
{
static enum bool (*p)(const char *pathName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSAddLibrary", (unsigned long *)&p);
return(p(pathName));
}
enum bool
NSAddLibraryWithSearching(
const char *pathName)
{
static enum bool (*p)(const char *pathName) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSAddLibraryWithSearching",
(unsigned long *)&p);
return(p(pathName));
}
const struct mach_header *
NSAddImage(
const char *image_name,
unsigned long options)
{
static const struct mach_header * (*p)(const char *image_name,
unsigned long options) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld_NSAddImage",
(unsigned long *)&p);
return(p(image_name, options));
}
long
NSVersionOfLinkTimeLibrary(
const char *libraryName)
{
unsigned long i;
struct load_command *load_commands, *lc;
struct dylib_command *dl;
char *install_name;
#ifndef __OPENSTEP__
static struct mach_header *mh = NULL;
if(mh == NULL)
mh = _NSGetMachExecuteHeader();
#else
#ifdef __DYNAMIC__
static struct mach_header *mh = NULL;
if(mh == NULL)
_dyld_lookup_and_bind("__mh_execute_header",
(unsigned long *)&mh, NULL);
#else
struct mach_header *mh;
mh = (struct mach_header *)&_mh_execute_header;
#endif
#endif
load_commands = (struct load_command *)
((char *)mh + sizeof(struct mach_header));
lc = load_commands;
for(i = 0; i < mh->ncmds; i++){
if(lc->cmd == LC_LOAD_DYLIB){
dl = (struct dylib_command *)lc;
install_name = (char *)dl + dl->dylib.name.offset;
if(names_match(install_name, libraryName) == TRUE)
return(dl->dylib.current_version);
}
lc = (struct load_command *)((char *)lc + lc->cmdsize);
}
return(-1);
}
long
NSVersionOfRunTimeLibrary(
const char *libraryName)
{
unsigned long i, j, n;
char *install_name;
struct load_command *load_commands, *lc;
struct dylib_command *dl;
struct mach_header *mh;
n = _dyld_image_count();
for(i = 0; i < n; i++){
mh = _dyld_get_image_header(i);
if(mh->filetype != MH_DYLIB)
continue;
load_commands = (struct load_command *)
((char *)mh + sizeof(struct mach_header));
lc = load_commands;
for(j = 0; j < mh->ncmds; j++){
if(lc->cmd == LC_ID_DYLIB){
dl = (struct dylib_command *)lc;
install_name = (char *)dl + dl->dylib.name.offset;
if(names_match(install_name, libraryName) == TRUE)
return(dl->dylib.current_version);
}
lc = (struct load_command *)((char *)lc + lc->cmdsize);
}
}
return(-1);
}
static
enum bool
names_match(
char *install_name,
const char *libraryName)
{
char *basename;
unsigned long n;
basename = strrchr(install_name, '/');
if(basename == NULL)
basename = install_name;
else
basename++;
if(strcmp(basename, libraryName) == 0)
return(TRUE);
if(strncmp(basename, "lib", 3) ==0){
n = strlen(libraryName);
if(strncmp(basename+3, libraryName, n) == 0){
if(strncmp(basename+3+n, ".dylib", 6) == 0)
return(TRUE);
if(basename[3+n] == '.' &&
basename[3+n+1] != '\0' &&
strncmp(basename+3+n+2, ".dylib", 6) == 0)
return(TRUE);
}
}
return(FALSE);
}
int
_NSGetExecutablePath(
char *buf,
unsigned long *bufsize)
{
static int (*p)(char *buf, unsigned long *bufsize) = NULL;
if(p == NULL)
_dyld_func_lookup("__dyld__NSGetExecutablePath",
(unsigned long *)&p);
return(p(buf, bufsize));
}