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)
75 char errorBuffer[128];
77 static char *OFIErrorStrings[] =
79 "%s(%d): Object Image Load Failure\n",
80 "%s(%d): Object Image Load Success\n",
81 "%s(%d): Not an recognisable object file\n",
82 "%s(%d): No valid architecture\n",
83 "%s(%d): Object image has an invalid format\n",
84 "%s(%d): Invalid access (permissions?)\n",
85 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
87 #define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
89 if ( dl_last_error ) {
90 safefree(dl_last_error);
96 if (index > NUM_OFI_ERRORS - 1)
97 index = NUM_OFI_ERRORS - 1;
98 sprintf(errorBuffer, OFIErrorStrings[index], path, number);
102 sprintf(errorBuffer, "%s(%d): Totally unknown error type %d\n",
106 dl_last_error = safemalloc(strlen(errorBuffer)+1);
107 strcpy(dl_last_error, errorBuffer);
110 static char *dlopen(char *path, int mode /* mode is ignored */)
113 NSObjectFileImage ofile;
114 NSModule handle = NULL;
116 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
117 if (dyld_result != NSObjectFileImageSuccess)
118 TranslateError(path, OFImage, dyld_result);
121 // NSLinkModule will cause the run to abort on any link error's
122 // not very friendly but the error recovery functionality is limited.
123 handle = NSLinkModule(ofile, path, TRUE);
130 dlsym(handle, symbol)
136 if (NSIsSymbolNameDefined(symbol))
137 addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
144 #else /* NS_TARGET_MAJOR <= 3 */
146 static NXStream *OpenError(void)
148 return NXOpenMemory( (char *) 0, 0, NX_WRITEONLY);
151 static void TransferError(NXStream *s)
156 if ( dl_last_error ) {
157 safefree(dl_last_error);
159 NXGetMemoryBuffer(s, &buffer, &len, &maxlen);
160 dl_last_error = safemalloc(len);
161 strcpy(dl_last_error, buffer);
164 static void CloseError(NXStream *s)
167 NXCloseMemory( s, NX_FREEBUFFER);
171 static char *dlopen(char *path, int mode /* mode is ignored */)
179 /* Do not load what is already loaded into this process */
180 if (hv_fetch(dl_loaded_files, path, strlen(path), 0))
184 psize = AvFILL(dl_resolve_using) + 3;
185 p = (char **) safemalloc(psize * sizeof(char*));
187 for(i=1; i<psize-1; i++) {
188 p[i] = SvPVx(*av_fetch(dl_resolve_using, i-1, TRUE), na);
191 rld_success = rld_load(nxerr, (struct mach_header **)0, p,
196 /* prevent multiple loads of same file into same process */
197 hv_store(dl_loaded_files, path, strlen(path), &sv_yes, 0);
199 TransferError(nxerr);
207 dlsym(handle, symbol)
211 NXStream *nxerr = OpenError();
213 unsigned long symref = 0;
215 sprintf(symbuf, "_%s", symbol);
216 if (!rld_lookup(nxerr, symbuf, &symref)) {
217 TransferError(nxerr);
220 return (void*) symref;
223 #endif /* NS_TARGET_MAJOR >= 4 */
226 /* ----- code from dl_dlopen.xs below here ----- */
232 (void)dl_generic_private_init();
233 dl_resolve_using = perl_get_av("DynaLoader::dl_resolve_using", 0x4);
236 MODULE = DynaLoader PACKAGE = DynaLoader
239 (void)dl_private_init();
244 dl_load_file(filename, flags=0)
250 DLDEBUG(1,PerlIO_printf(PerlIO_stderr(), "dl_load_file(%s,%x):\n", filename,flags));
252 warn("Can't make loaded symbols global on this platform while loading %s",filename);
253 RETVAL = dlopen(filename, mode) ;
254 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " libref=%x\n", RETVAL));
255 ST(0) = sv_newmortal() ;
257 SaveError("%s",dlerror()) ;
259 sv_setiv( ST(0), (IV)RETVAL);
263 dl_find_symbol(libhandle, symbolname)
267 #if NS_TARGET_MAJOR >= 4
268 char symbolname_buf[1024];
269 symbolname = dl_add_underscore(symbolname, symbolname_buf);
271 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_find_symbol(handle=%x, symbol=%s)\n",
272 libhandle, symbolname));
273 RETVAL = dlsym(libhandle, symbolname);
274 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " symbolref = %x\n", RETVAL));
275 ST(0) = sv_newmortal() ;
277 SaveError("%s",dlerror()) ;
279 sv_setiv( ST(0), (IV)RETVAL);
288 # These functions should not need changing on any platform:
291 dl_install_xsub(perl_name, symref, filename="$Package")
296 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_install_xsub(name=%s, symref=%x)\n",
298 ST(0)=sv_2mortal(newRV((SV*)newXS(perl_name, (void(*)())symref, filename)));