Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 1 | |
Jack Jansen | d4c76bf | 2002-02-01 16:01:05 +0000 | [diff] [blame] | 2 | /* Support for dynamic loading of extension modules on Mac OS X |
| 3 | ** All references to "NeXT" are for historical reasons. |
| 4 | */ |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 5 | |
| 6 | #include "Python.h" |
| 7 | #include "importdl.h" |
| 8 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 9 | #include <mach-o/dyld.h> |
| 10 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 11 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
| 12 | {".so", "rb", C_EXTENSION}, |
| 13 | {"module.so", "rb", C_EXTENSION}, |
| 14 | {0, 0} |
| 15 | }; |
| 16 | |
Jack Jansen | d4c76bf | 2002-02-01 16:01:05 +0000 | [diff] [blame] | 17 | /* |
| 18 | ** Python modules are Mach-O MH_BUNDLE files. The best way to load these |
| 19 | ** is each in a private namespace, so you can load, say, a module bar and a |
| 20 | ** module foo.bar. If we load everything in the global namespace the two |
| 21 | ** initbar() symbols will conflict. |
| 22 | ** However, it seems some extension packages depend upon being able to access |
| 23 | ** each others' global symbols. There seems to be no way to eat our cake and |
| 24 | ** have it, so the USE_DYLD_GLOBAL_NAMESPACE define determines which behaviour |
| 25 | ** you get. |
| 26 | */ |
| 27 | |
| 28 | #ifdef USE_DYLD_GLOBAL_NAMESPACE |
| 29 | #define LINKOPTIONS NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR |
| 30 | #else |
| 31 | #define LINKOPTIONS NSLINKMODULE_OPTION_BINDNOW| \ |
| 32 | NSLINKMODULE_OPTION_RETURN_ON_ERROR|NSLINKMODULE_OPTION_PRIVATE |
| 33 | #endif |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 34 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Jack Jansen | d4c76bf | 2002-02-01 16:01:05 +0000 | [diff] [blame] | 35 | const char *pathname, FILE *fp) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 36 | { |
| 37 | dl_funcptr p = NULL; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 38 | char funcname[258]; |
Jack Jansen | a762f4c | 2002-02-01 22:24:56 +0000 | [diff] [blame] | 39 | NSObjectFileImageReturnCode rc; |
| 40 | NSObjectFileImage image; |
| 41 | NSModule newModule; |
| 42 | NSSymbol theSym; |
| 43 | const char *errString; |
| 44 | char errBuf[512]; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 45 | |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 46 | PyOS_snprintf(funcname, sizeof(funcname), "_init%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 47 | |
Jack Jansen | d4c76bf | 2002-02-01 16:01:05 +0000 | [diff] [blame] | 48 | #ifdef USE_DYLD_GLOBAL_NAMESPACE |
Jack Jansen | a762f4c | 2002-02-01 22:24:56 +0000 | [diff] [blame] | 49 | if (NSIsSymbolNameDefined(funcname)) { |
| 50 | theSym = NSLookupAndBindSymbol(funcname); |
| 51 | p = (dl_funcptr)NSAddressOfSymbol(theSym); |
| 52 | return p; |
| 53 | } |
Jack Jansen | d4c76bf | 2002-02-01 16:01:05 +0000 | [diff] [blame] | 54 | #endif |
Jack Jansen | a762f4c | 2002-02-01 22:24:56 +0000 | [diff] [blame] | 55 | rc = NSCreateObjectFileImageFromFile(pathname, &image); |
| 56 | switch(rc) { |
| 57 | default: |
| 58 | case NSObjectFileImageFailure: |
| 59 | case NSObjectFileImageFormat: |
Jack Jansen | d4c76bf | 2002-02-01 16:01:05 +0000 | [diff] [blame] | 60 | /* for these a message is printed on stderr by dyld */ |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 61 | errString = "Can't create object file image"; |
Jack Jansen | a762f4c | 2002-02-01 22:24:56 +0000 | [diff] [blame] | 62 | break; |
| 63 | case NSObjectFileImageSuccess: |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 64 | errString = NULL; |
| 65 | break; |
Jack Jansen | a762f4c | 2002-02-01 22:24:56 +0000 | [diff] [blame] | 66 | case NSObjectFileImageInappropriateFile: |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 67 | errString = "Inappropriate file type for dynamic loading"; |
| 68 | break; |
Jack Jansen | a762f4c | 2002-02-01 22:24:56 +0000 | [diff] [blame] | 69 | case NSObjectFileImageArch: |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 70 | errString = "Wrong CPU type in object file"; |
| 71 | break; |
Jack Jansen | a762f4c | 2002-02-01 22:24:56 +0000 | [diff] [blame] | 72 | case NSObjectFileImageAccess: |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 73 | errString = "Can't read object file (no access)"; |
| 74 | break; |
Jack Jansen | d4c76bf | 2002-02-01 16:01:05 +0000 | [diff] [blame] | 75 | } |
Jack Jansen | a762f4c | 2002-02-01 22:24:56 +0000 | [diff] [blame] | 76 | if (errString == NULL) { |
| 77 | newModule = NSLinkModule(image, pathname, LINKOPTIONS); |
| 78 | if (newModule == NULL) { |
| 79 | int errNo; |
| 80 | char *fileName, *moreErrorStr; |
| 81 | NSLinkEditErrors c; |
| 82 | NSLinkEditError( &c, &errNo, &fileName, &moreErrorStr ); |
| 83 | PyOS_snprintf(errBuf, 512, "Failure linking new module: %s: %s", |
| 84 | fileName, moreErrorStr); |
| 85 | errString = errBuf; |
| 86 | } |
| 87 | } |
| 88 | if (errString != NULL) { |
| 89 | PyErr_SetString(PyExc_ImportError, errString); |
| 90 | return NULL; |
| 91 | } |
| 92 | #ifdef USE_DYLD_GLOBAL_NAMESPACE |
| 93 | if (!NSIsSymbolNameDefined(funcname)) { |
| 94 | /* UnlinkModule() isn't implemented in current versions, but calling it does no harm */ |
| 95 | NSUnLinkModule(newModule, FALSE); |
| 96 | PyErr_Format(PyExc_ImportError, |
| 97 | "Loaded module does not contain symbol %.200s", |
| 98 | funcname); |
| 99 | return NULL; |
| 100 | } |
| 101 | theSym = NSLookupAndBindSymbol(funcname); |
| 102 | #else |
| 103 | theSym = NSLookupSymbolInModule(newModule, funcname); |
| 104 | if ( theSym == NULL ) { |
| 105 | NSUnLinkModule(newModule, FALSE); |
| 106 | PyErr_Format(PyExc_ImportError, |
| 107 | "Loaded module does not contain symbol %.200s", |
| 108 | funcname); |
| 109 | return NULL; |
| 110 | } |
| 111 | #endif |
| 112 | p = (dl_funcptr)NSAddressOfSymbol(theSym); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 113 | return p; |
| 114 | } |