This commit was manufactured by cvs2svn to create tag
[python/dscho.git] / Python / dynload_next.c
blob892bb478424b261086363bf56c955426c3f41a52
2 /* Support for dynamic loading of extension modules */
4 #include "Python.h"
5 #include "importdl.h"
8 #ifdef WITH_DYLD
10 #define USE_DYLD
12 #include <mach-o/dyld.h>
14 #else /* WITH_DYLD */
16 #define USE_RLD
17 /* Define this to 1 if you want be able to load ObjC modules as well:
18 it switches between two different way of loading modules on the NeXT,
19 one that directly interfaces with the dynamic loader (rld_load(), which
20 does not correctly load ObjC object files), and another that uses the
21 ObjC runtime (objc_loadModules()) to do the job.
22 You'll have to add ``-ObjC'' to the compiler flags if you set this to 1.
24 #define HANDLE_OBJC_MODULES 1
25 #if HANDLE_OBJC_MODULES
26 #include <objc/Object.h>
27 #include <objc/objc-load.h>
28 #endif
30 #include <mach-o/rld.h>
32 #endif /* WITH_DYLD */
35 const struct filedescr _PyImport_DynLoadFiletab[] = {
36 {".so", "rb", C_EXTENSION},
37 {"module.so", "rb", C_EXTENSION},
38 {0, 0}
41 dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
42 const char *pathname, FILE *fp)
44 dl_funcptr p = NULL;
45 char funcname[258];
47 PyOS_snprintf(funcname, sizeof(funcname), "_init%.200s", shortname);
49 #ifdef USE_RLD
51 NXStream *errorStream;
52 struct mach_header *new_header;
53 const char *filenames[2];
54 long ret;
55 unsigned long ptr;
57 errorStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
58 filenames[0] = pathname;
59 filenames[1] = NULL;
61 #if HANDLE_OBJC_MODULES
63 /* The following very bogus line of code ensures that
64 objc_msgSend, etc are linked into the binary. Without
65 it, dynamic loading of a module that includes objective-c
66 method calls will fail with "undefined symbol _objc_msgSend()".
67 This remains true even in the presence of the -ObjC flag
68 to the compiler
71 [Object name];
73 /* objc_loadModules() dynamically loads the object files
74 indicated by the paths in filenames. If there are any
75 errors generated during loading -- typically due to the
76 inability to find particular symbols -- an error message
77 will be written to errorStream.
78 It returns 0 if the module is successfully loaded, 1
79 otherwise.
82 ret = !objc_loadModules(filenames, errorStream,
83 NULL, &new_header, NULL);
85 #else /* !HANDLE_OBJC_MODULES */
87 ret = rld_load(errorStream, &new_header,
88 filenames, NULL);
90 #endif /* HANDLE_OBJC_MODULES */
92 /* extract the error messages for the exception */
93 if(!ret) {
94 char *streamBuf;
95 int len, maxLen;
97 NXPutc(errorStream, (char)0);
99 NXGetMemoryBuffer(errorStream,
100 &streamBuf, &len, &maxLen);
101 PyErr_SetString(PyExc_ImportError, streamBuf);
104 if(ret && rld_lookup(errorStream, funcname, &ptr))
105 p = (dl_funcptr) ptr;
107 NXCloseMemory(errorStream, NX_FREEBUFFER);
109 if(!ret)
110 return NULL;
112 #endif /* USE_RLD */
113 #ifdef USE_DYLD
114 /* This is also NeXT-specific. However, frameworks (the new style
115 of shared library) and rld() can't be used in the same program;
116 instead, you have to use dyld, which is mostly unimplemented. */
118 NSObjectFileImageReturnCode rc;
119 NSObjectFileImage image;
120 NSModule newModule;
121 NSSymbol theSym;
122 const char *errString;
124 if (NSIsSymbolNameDefined(funcname)) {
125 theSym = NSLookupAndBindSymbol(funcname);
126 p = (dl_funcptr)NSAddressOfSymbol(theSym);
127 return p;
129 rc = NSCreateObjectFileImageFromFile(pathname, &image);
130 switch(rc) {
131 default:
132 case NSObjectFileImageFailure:
133 case NSObjectFileImageFormat:
134 /* for these a message is printed on stderr by dyld */
135 errString = "Can't create object file image";
136 break;
137 case NSObjectFileImageSuccess:
138 errString = NULL;
139 break;
140 case NSObjectFileImageInappropriateFile:
141 errString = "Inappropriate file type for dynamic loading";
142 break;
143 case NSObjectFileImageArch:
144 errString = "Wrong CPU type in object file";
145 break;
146 case NSObjectFileImageAccess:
147 errString = "Can't read object file (no access)";
148 break;
150 if (errString == NULL) {
151 newModule = NSLinkModule(image, pathname,
152 NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR);
153 if (!newModule)
154 errString = "Failure linking new module";
156 if (errString != NULL) {
157 PyErr_SetString(PyExc_ImportError, errString);
158 return NULL;
160 if (!NSIsSymbolNameDefined(funcname)) {
161 /* UnlinkModule() isn't implemented in current versions, but calling it does no harm */
162 NSUnLinkModule(newModule, FALSE);
163 PyErr_Format(PyExc_ImportError,
164 "Loaded module does not contain symbol %.200s",
165 funcname);
166 return NULL;
168 theSym = NSLookupAndBindSymbol(funcname);
169 p = (dl_funcptr)NSAddressOfSymbol(theSym);
171 #endif /* USE_DYLD */
173 return p;