*/
+#if NS_TARGET_MAJOR >= 4
+#else
/* include these before perl headers */
#include <mach-o/rld.h>
#include <streams/streams.h>
+#endif
#include "EXTERN.h"
#include "perl.h"
static char * dl_last_error = (char *) 0;
static AV *dl_resolve_using = Nullav;
-NXStream *
-OpenError()
+static char *dlerror()
+{
+ return dl_last_error;
+}
+
+int dlclose(handle) /* stub only */
+void *handle;
+{
+ return 0;
+}
+
+#if NS_TARGET_MAJOR >= 4
+#import <mach-o/dyld.h>
+
+enum dyldErrorSource
+{
+ OFImage,
+};
+
+static void TranslateError
+ (const char *path, enum dyldErrorSource type, int number)
+{
+ char errorBuffer[128];
+ unsigned int index;
+ static char *OFIErrorStrings[] =
+ {
+ "%s(%d): Object Image Load Failure\n",
+ "%s(%d): Object Image Load Success\n",
+ "%s(%d): Not an recognisable object file\n",
+ "%s(%d): No valid architecture\n",
+ "%s(%d): Object image has an invalid format\n",
+ "%s(%d): Invalid access (permissions?)\n",
+ "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
+ };
+#define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
+
+ if ( dl_last_error ) {
+ safefree(dl_last_error);
+ }
+ switch (type)
+ {
+ case OFImage:
+ index = number;
+ if (index > NUM_OFI_ERRORS - 1)
+ index = NUM_OFI_ERRORS - 1;
+ sprintf(errorBuffer, OFIErrorStrings[index], path, number);
+ break;
+
+ default:
+ sprintf(errorBuffer, "%s(%d): Totally unknown error type %d\n",
+ path, number, type);
+ break;
+ }
+ dl_last_error = safemalloc(strlen(errorBuffer)+1);
+ strcpy(dl_last_error, errorBuffer);
+}
+
+static char *dlopen(char *path, int mode /* mode is ignored */)
+{
+ int dyld_result;
+ NSObjectFileImage ofile;
+ NSModule handle = NULL;
+
+ dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
+ if (dyld_result != NSObjectFileImageSuccess)
+ TranslateError(path, OFImage, dyld_result);
+ else
+ {
+ // NSLinkModule will cause the run to abort on any link error's
+ // not very friendly but the error recovery functionality is limited.
+ handle = NSLinkModule(ofile, path, TRUE);
+ }
+
+ return handle;
+}
+
+void *
+dlsym(handle, symbol)
+void *handle;
+char *symbol;
+{
+ void *addr;
+
+ if (NSIsSymbolNameDefined(symbol))
+ addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
+ else
+ addr = NULL;
+
+ return addr;
+}
+
+#else /* NS_TARGET_MAJOR <= 3 */
+
+static NXStream *OpenError(void)
{
return NXOpenMemory( (char *) 0, 0, NX_WRITEONLY);
}
-void
-TransferError( s)
-NXStream *s;
+static void TransferError(NXStream *s)
{
char *buffer;
int len, maxlen;
strcpy(dl_last_error, buffer);
}
-void
-CloseError( s)
-NXStream *s;
+static void CloseError(NXStream *s)
{
if ( s ) {
NXCloseMemory( s, NX_FREEBUFFER);
}
}
-char *dlerror()
-{
- return dl_last_error;
-}
-
-char *
-dlopen(path, mode)
-char * path;
-int mode; /* mode is ignored */
+static char *dlopen(char *path, int mode /* mode is ignored */)
{
int rld_success;
NXStream *nxerr;
return result;
}
-int
-dlclose(handle) /* stub only */
-void *handle;
-{
- return 0;
-}
-
void *
dlsym(handle, symbol)
void *handle;
return (void*) symref;
}
+#endif /* NS_TARGET_MAJOR >= 4 */
+
/* ----- code from dl_dlopen.xs below here ----- */
void * libhandle
char * symbolname
CODE:
+#if NS_TARGET_MAJOR >= 4
+ char symbolname_buf[1024];
+ symbolname = dl_add_underscore(symbolname, symbolname_buf);
+#endif
DLDEBUG(2,fprintf(stderr,"dl_find_symbol(handle=%x, symbol=%s)\n",
libhandle, symbolname));
RETVAL = dlsym(libhandle, symbolname);