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