3 * Platform: NeXT NS 3.2
4 * Author: Anno Siegel (siegel@zrz.TU-Berlin.DE)
5 * Based on: dl_dlopen.xs by Paul Marquess
6 * Created: Aug 15th, 1994
11 And Gandalf said: 'Many folk like to know beforehand what is to
12 be set on the table; but those who have laboured to prepare the
13 feast like to keep their secret; for wonder makes the words of
19 dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess. It
20 should not be used as a base for further ports though it may be used
21 as an example for how dl_dlopen.xs can be ported to other platforms.
23 The method used here is just to supply the sun style dlopen etc.
24 functions in terms of NeXTs rld_*. The xs code proper is unchanged
27 The port could use some streamlining. For one, error handling could
34 #if NS_TARGET_MAJOR >= 4
36 /* include these before perl headers */
37 #include <mach-o/rld.h>
38 #include <streams/streams.h>
45 #define DL_LOADONCEONLY
47 #include "dlutils.c" /* SaveError() etc */
50 static char * dl_last_error = (char *) 0;
51 static AV *dl_resolve_using = Nullav;
53 static char *dlerror()
58 int dlclose(handle) /* stub only */
64 #if NS_TARGET_MAJOR >= 4
65 #import <mach-o/dyld.h>
72 static void TranslateError
73 (const char *path, enum dyldErrorSource type, int number)
78 static char *OFIErrorStrings[] =
80 "%s(%d): Object Image Load Failure\n",
81 "%s(%d): Object Image Load Success\n",
82 "%s(%d): Not an recognisable object file\n",
83 "%s(%d): No valid architecture\n",
84 "%s(%d): Object image has an invalid format\n",
85 "%s(%d): Invalid access (permissions?)\n",
86 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
88 #define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
94 if (index > NUM_OFI_ERRORS - 1)
95 index = NUM_OFI_ERRORS - 1;
96 error = form(OFIErrorStrings[index], path, number);
100 error = form("%s(%d): Totally unknown error type %d\n",
104 Safefree(dl_last_error);
105 dl_last_error = savepv(error);
108 static char *dlopen(char *path, int mode /* mode is ignored */)
111 NSObjectFileImage ofile;
112 NSModule handle = NULL;
114 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
115 if (dyld_result != NSObjectFileImageSuccess)
116 TranslateError(path, OFImage, dyld_result);
119 // NSLinkModule will cause the run to abort on any link error's
120 // not very friendly but the error recovery functionality is limited.
121 handle = NSLinkModule(ofile, path, TRUE);
128 dlsym(handle, symbol)
134 if (NSIsSymbolNameDefined(symbol))
135 addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
142 #else /* NS_TARGET_MAJOR <= 3 */
144 static NXStream *OpenError(void)
146 return NXOpenMemory( (char *) 0, 0, NX_WRITEONLY);
149 static void TransferError(NXStream *s)
154 if ( dl_last_error ) {
155 Safefree(dl_last_error);
157 NXGetMemoryBuffer(s, &buffer, &len, &maxlen);
158 New(1097, dl_last_error, len, char);
159 strcpy(dl_last_error, buffer);
162 static void CloseError(NXStream *s)
165 NXCloseMemory( s, NX_FREEBUFFER);
169 static char *dlopen(char *path, int mode /* mode is ignored */)
178 /* Do not load what is already loaded into this process */
179 if (hv_fetch(dl_loaded_files, path, strlen(path), 0))
183 psize = AvFILL(dl_resolve_using) + 3;
184 p = (char **) safemalloc(psize * sizeof(char*));
186 for(i=1; i<psize-1; i++) {
187 p[i] = SvPVx(*av_fetch(dl_resolve_using, i-1, TRUE), n_a);
190 rld_success = rld_load(nxerr, (struct mach_header **)0, p,
195 /* prevent multiple loads of same file into same process */
196 hv_store(dl_loaded_files, path, strlen(path), &PL_sv_yes, 0);
198 TransferError(nxerr);
206 dlsym(handle, symbol)
210 NXStream *nxerr = OpenError();
211 unsigned long symref = 0;
213 if (!rld_lookup(nxerr, form("_%s", symbol), &symref))
214 TransferError(nxerr);
216 return (void*) symref;
219 #endif /* NS_TARGET_MAJOR >= 4 */
222 /* ----- code from dl_dlopen.xs below here ----- */
226 dl_private_init(pTHX)
228 (void)dl_generic_private_init(aTHX);
229 dl_resolve_using = get_av("DynaLoader::dl_resolve_using", 0x4);
232 MODULE = DynaLoader PACKAGE = DynaLoader
235 (void)dl_private_init(aTHX);
240 dl_load_file(filename, flags=0)
246 DLDEBUG(1,PerlIO_printf(PerlIO_stderr(), "dl_load_file(%s,%x):\n", filename,flags));
248 Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
249 RETVAL = dlopen(filename, mode) ;
250 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " libref=%x\n", RETVAL));
251 ST(0) = sv_newmortal() ;
253 SaveError(aTHX_ "%s",dlerror()) ;
255 sv_setiv( ST(0), PTR2IV(RETVAL) );
259 dl_find_symbol(libhandle, symbolname)
263 #if NS_TARGET_MAJOR >= 4
264 symbolname = form("_%s", symbolname);
266 DLDEBUG(2, PerlIO_printf(PerlIO_stderr(),
267 "dl_find_symbol(handle=%lx, symbol=%s)\n",
268 (unsigned long) libhandle, symbolname));
269 RETVAL = dlsym(libhandle, symbolname);
270 DLDEBUG(2, PerlIO_printf(PerlIO_stderr(),
271 " symbolref = %lx\n", (unsigned long) RETVAL));
272 ST(0) = sv_newmortal() ;
274 SaveError(aTHX_ "%s",dlerror()) ;
276 sv_setiv( ST(0), PTR2IV(RETVAL) );
285 # These functions should not need changing on any platform:
288 dl_install_xsub(perl_name, symref, filename="$Package")
293 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_install_xsub(name=%s, symref=%x)\n",
295 ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
296 (void(*)(pTHX_ CV *))symref,