| 1 | /* dl_macosx.c |
| 2 | * |
| 3 | * Platform: Mac OS X |
| 4 | * Author: Gilles Depeyrot (Gilles.Depeyrot@wanadoo.fr) |
| 5 | * Based on: dl_next.xs by Anno Siegel (siegel@zrz.TU-Berlin.DE) |
| 6 | * Based on: dl_dlopen.xs by Paul Marquess |
| 7 | * Created: Aug 15th, 1994 |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | /* Porting notes: |
| 12 | * dl_macosx.c is itself a port from dl_next.xs by Anno Siegel. |
| 13 | * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess. |
| 14 | * The method used here is just to supply the sun style dlopen etc. |
| 15 | * functions in terms of NeXTs rld_*. |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | |
| 20 | static char dl_last_error[1024]; |
| 21 | |
| 22 | char *dlerror() |
| 23 | { |
| 24 | return dl_last_error; |
| 25 | } |
| 26 | |
| 27 | int dlclose(void *handle) /* stub only */ |
| 28 | { |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | #import <mach-o/dyld.h> |
| 33 | |
| 34 | enum dyldErrorSource |
| 35 | { |
| 36 | OFImage, |
| 37 | }; |
| 38 | |
| 39 | static |
| 40 | void TranslateError(const char *path, enum dyldErrorSource type, int number) |
| 41 | { |
| 42 | unsigned int index; |
| 43 | static char *OFIErrorStrings[] = |
| 44 | { |
| 45 | "%s(%d): Object Image Load Failure\n", |
| 46 | "%s(%d): Object Image Load Success\n", |
| 47 | "%s(%d): Not an recognisable object file\n", |
| 48 | "%s(%d): No valid architecture\n", |
| 49 | "%s(%d): Object image has an invalid format\n", |
| 50 | "%s(%d): Invalid access (permissions?)\n", |
| 51 | "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n", |
| 52 | }; |
| 53 | #define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0])) |
| 54 | |
| 55 | switch (type) |
| 56 | { |
| 57 | case OFImage: |
| 58 | index = number; |
| 59 | if (index > NUM_OFI_ERRORS - 1) |
| 60 | index = NUM_OFI_ERRORS - 1; |
| 61 | sprintf(dl_last_error, OFIErrorStrings[index], path, number); |
| 62 | break; |
| 63 | |
| 64 | default: |
| 65 | sprintf(dl_last_error, "%s(%d): Totally unknown error type %d\n", |
| 66 | path, number, type); |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void *dlopen(const char *path, int mode /* mode is ignored */) |
| 72 | { |
| 73 | int dyld_result; |
| 74 | NSObjectFileImage ofile; |
| 75 | NSModule handle = NULL; |
| 76 | |
| 77 | dyld_result = NSCreateObjectFileImageFromFile(path, &ofile); |
| 78 | if (dyld_result != NSObjectFileImageSuccess) |
| 79 | { |
| 80 | TranslateError(path, OFImage, dyld_result); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | // NSLinkModule will cause the run to abort on any link error's |
| 85 | // not very friendly but the error recovery functionality is limited. |
| 86 | handle = NSLinkModule(ofile, path, TRUE); |
| 87 | } |
| 88 | |
| 89 | return handle; |
| 90 | } |
| 91 | |
| 92 | void *dlsym(void *handle, const char *symbol) |
| 93 | { |
| 94 | void *addr; |
| 95 | |
| 96 | if (NSIsSymbolNameDefined(symbol)) |
| 97 | addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol)); |
| 98 | else |
| 99 | addr = NULL; |
| 100 | |
| 101 | return addr; |
| 102 | } |