Lines 65-71
Link Here
|
65 |
#include <errno.h> |
65 |
#include <errno.h> |
66 |
#include <unistd.h> |
66 |
#include <unistd.h> |
67 |
#ifdef MACOSX |
67 |
#ifdef MACOSX |
68 |
#include <CoreFoundation/CoreFoundation.h> |
68 |
#include <mach-o/dyld.h> |
69 |
#else |
69 |
#else |
70 |
#include <dlfcn.h> |
70 |
#include <dlfcn.h> |
71 |
#endif |
71 |
#endif |
Lines 90-99
Link Here
|
90 |
{ |
90 |
{ |
91 |
int rc; |
91 |
int rc; |
92 |
#ifdef MACOSX |
92 |
#ifdef MACOSX |
93 |
CFStringRef bundlePath; |
93 |
struct mach_header *pLib; |
94 |
CFURLRef bundleURL; |
|
|
95 |
CFBundleRef bundle; |
96 |
CFStringRef symbolName; |
97 |
#else |
94 |
#else |
98 |
void *phandle; |
95 |
void *phandle; |
99 |
#endif |
96 |
#endif |
Lines 115-159
Link Here
|
115 |
|
112 |
|
116 |
#ifdef MACOSX |
113 |
#ifdef MACOSX |
117 |
|
114 |
|
118 |
/* Convert char pointers to CFStringRefs */ |
115 |
// Check if library is already loaded |
119 |
bundlePath = CFStringCreateWithCStringNoCopy(NULL, argv[1], |
116 |
pLib = NSAddImage(argv[1], NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED); |
120 |
CFStringGetSystemEncoding(), kCFAllocatorNull); |
117 |
if (!pLib) { |
121 |
symbolName = CFStringCreateWithCStringNoCopy(NULL, psymbol, |
118 |
// Check DYLD_LIBRARY_PATH |
122 |
CFStringGetSystemEncoding(), kCFAllocatorNull); |
119 |
pLib = NSAddImage(argv[1], NSADDIMAGE_OPTION_WITH_SEARCHING); |
123 |
|
120 |
} |
124 |
/* Get the framework's URL using its path */ |
121 |
if (pLib) { |
125 |
if ((bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, |
122 |
// Prefix symbol name with '_' |
126 |
bundlePath, kCFURLPOSIXPathStyle, true)) != NULL) { |
123 |
char *name = malloc(1+strlen(psymbol)+1); |
127 |
/* Load the framework */ |
124 |
NSSymbol *symbol; |
128 |
if ((bundle = CFBundleCreate( kCFAllocatorDefault, |
125 |
void *address = NULL; |
129 |
bundleURL)) != NULL) { |
126 |
strcpy(name, "_"); |
130 |
/* Load the shared library */ |
127 |
strcat(name, psymbol); |
131 |
if (CFBundleLoadExecutable(bundle)) { |
128 |
symbol = NSLookupSymbolInImage(pLib, name, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND); |
132 |
if ((pfun = CFBundleGetFunctionPointerForName(bundle, |
129 |
free(name); |
133 |
symbolName)) != NULL) { |
130 |
if (symbol) address = NSAddressOfSymbol(symbol); |
|
|
131 |
if (address != NULL) { |
134 |
printf(": ok\n"); |
132 |
printf(": ok\n"); |
135 |
CFRelease(bundlePath); |
133 |
#ifdef NO_UNLOAD_CHECK |
136 |
CFRelease(bundleURL); |
134 |
_exit(0); |
137 |
CFRelease(bundle); |
135 |
#else |
138 |
CFRelease(symbolName); |
136 |
// Mac OS X can't unload dylibs |
|
|
137 |
#endif |
139 |
return 0; |
138 |
return 0; |
140 |
} |
139 |
} else { |
141 |
else |
|
|
142 |
printf(": ERROR: symbol %s not found\n", psymbol); |
140 |
printf(": ERROR: symbol %s not found\n", psymbol); |
143 |
} |
141 |
} |
144 |
/* No message printed since CFLog prints its own message */ |
142 |
} else { |
|
|
143 |
printf(": ERROR: %s is not a valid dylib name\n", argv[1]); |
145 |
} |
144 |
} |
146 |
else |
|
|
147 |
printf(": ERROR: %s is not a bundle\n", argv[1]); |
148 |
} |
149 |
else |
150 |
printf(": ERROR: %s is not a valid bundle name\n", argv[1]); |
151 |
|
152 |
CFRelease(bundlePath); |
153 |
if (bundleURL != NULL) CFRelease(bundleURL); |
154 |
if (bundle != NULL) CFRelease(bundle); |
155 |
CFRelease(symbolName); |
156 |
return 3; |
145 |
return 3; |
|
|
146 |
|
147 |
// fixme use NSLinkEditError() for better error messages |
157 |
|
148 |
|
158 |
#else /* MACOSX */ |
149 |
#else /* MACOSX */ |
159 |
|
150 |
|