1/*
2Copyright (c) 2002 Peter O'Gorman <ogorman@users.sourceforge.net>
3
4Permission is hereby granted, free of charge, to any person obtaining
5a copy of this software and associated documentation files (the
6"Software"), to deal in the Software without restriction, including
7without limitation the rights to use, copy, modify, merge, publish,
8distribute, sublicense, and/or sell copies of the Software, and to
9permit persons to whom the Software is furnished to do so, subject to
10the following conditions:
11
12The above copyright notice and this permission notice shall be
13included in all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*/
23
24
25/* Just to prove that it isn't that hard to add Mac calls to your code :)
26   This works with pretty much everything, including kde3 xemacs and the gimp,
27   I'd guess that it'd work in at least 95% of cases, use this as your starting
28   point, rather than the mess that is dlfcn.c, assuming that your code does not
29   require ref counting or symbol lookups in dependent libraries
30*/
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <stdarg.h>
38#include <limits.h>
39#include <mach-o/dyld.h>
40#include "dlfcn_simple.h"
41
42#define ERR_STR_LEN 256
43
44static void *dlsymIntern(void *handle, const char *symbol);
45
46static const char *error(int setget, const char *str, ...);
47
48
49
50/* Set and get the error string for use by dlerror */
51static const char *error(int setget, const char *str, ...)
52{
53	static char errstr[ERR_STR_LEN];
54	static int err_filled = 0;
55	const char *retval;
56	va_list arg;
57	if (setget == 0)
58	{
59		va_start(arg, str);
60		strncpy(errstr, "dlcompat: ", ERR_STR_LEN);
61		vsnprintf(errstr + 10, ERR_STR_LEN - 10, str, arg);
62		va_end(arg);
63		err_filled = 1;
64		retval = NULL;
65	}
66	else
67	{
68		if (!err_filled)
69			retval = NULL;
70		else
71			retval = errstr;
72		err_filled = 0;
73	}
74	return retval;
75}
76
77/* dlopen */
78void *dlopen(const char *path, int mode)
79{
80	void *module = 0;
81	NSObjectFileImage ofi = 0;
82	NSObjectFileImageReturnCode ofirc;
83
84	/* If we got no path, the app wants the global namespace, use -1 as the marker
85	   in this case */
86	if (!path)
87		return (void *)-1;
88
89	/* Create the object file image, works for things linked with the -bundle arg to ld */
90	ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
91	switch (ofirc)
92	{
93		case NSObjectFileImageSuccess:
94			/* It was okay, so use NSLinkModule to link in the image */
95			module = NSLinkModule(ofi, path,
96								  NSLINKMODULE_OPTION_RETURN_ON_ERROR
97								  | (mode & RTLD_GLOBAL) ? 0 : NSLINKMODULE_OPTION_PRIVATE
98								  | (mode & RTLD_LAZY) ? 0 : NSLINKMODULE_OPTION_BINDNOW);
99			NSDestroyObjectFileImage(ofi);
100			break;
101		case NSObjectFileImageInappropriateFile:
102			/* It may have been a dynamic library rather than a bundle, try to load it */
103			module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
104			break;
105		default:
106			/* God knows what we got */
107			error(0, "Can not open \"%s\"", path);
108			return 0;
109	}
110	if (!module)
111		error(0, "Can not open \"%s\"", path);
112	return module;
113
114}
115
116/* dlsymIntern is used by dlsym to find the symbol */
117void *dlsymIntern(void *handle, const char *symbol)
118{
119	NSSymbol *nssym = 0;
120	/* If the handle is -1, if is the app global context */
121	if (handle == (void *)-1)
122	{
123		/* Global context, use NSLookupAndBindSymbol */
124		if (NSIsSymbolNameDefined(symbol))
125		{
126			nssym = NSLookupAndBindSymbol(symbol);
127		}
128
129	}
130	/* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image
131	   for libraries, and NSLookupSymbolInModule for bundles */
132	else
133	{
134		/* Check for both possible magic numbers depending on x86/ppc byte order */
135		if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
136			(((struct mach_header *)handle)->magic == MH_CIGAM))
137		{
138			if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol))
139			{
140				nssym = NSLookupSymbolInImage((struct mach_header *)handle,
141											  symbol,
142											  NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
143											  | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
144			}
145
146		}
147		else
148		{
149			nssym = NSLookupSymbolInModule(handle, symbol);
150		}
151	}
152	if (!nssym)
153	{
154		error(0, "Symbol \"%s\" Not found", symbol);
155		return NULL;
156	}
157	return NSAddressOfSymbol(nssym);
158}
159
160const char *dlerror(void)
161{
162	return error(1, (char *)NULL);
163}
164
165int dlclose(void *handle)
166{
167	if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
168		(((struct mach_header *)handle)->magic == MH_CIGAM))
169	{
170		error(0, "Can't remove dynamic libraries on darwin");
171		return 0;
172	}
173	if (!NSUnLinkModule(handle, 0))
174	{
175		error(0, "unable to unlink module %s", NSNameOfModule(handle));
176		return 1;
177	}
178	return 0;
179}
180
181
182/* dlsym, prepend the underscore and call dlsymIntern */
183void *dlsym(void *handle, const char *symbol)
184{
185	static char undersym[257];	/* Saves calls to malloc(3) */
186	int sym_len = strlen(symbol);
187	void *value = NULL;
188	char *malloc_sym = NULL;
189
190	if (sym_len < 256)
191	{
192		snprintf(undersym, 256, "_%s", symbol);
193		value = dlsymIntern(handle, undersym);
194	}
195	else
196	{
197		malloc_sym = malloc(sym_len + 2);
198		if (malloc_sym)
199		{
200			sprintf(malloc_sym, "_%s", symbol);
201			value = dlsymIntern(handle, malloc_sym);
202			free(malloc_sym);
203		}
204		else
205		{
206			error(0, "Unable to allocate memory");
207		}
208	}
209	return value;
210}
211