Files for 2.1b1 distribution.
[python/dscho.git] / Python / dynload_next.c
blob9a5c828add938265a08a5074ff4decbcec58f54f
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 sprintf(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 void *symaddr;
123 const char *errString;
125 rc = NSCreateObjectFileImageFromFile(pathname, &image);
126 switch(rc) {
127 default:
128 case NSObjectFileImageFailure:
129 NSObjectFileImageFormat:
130 /* for these a message is printed on stderr by dyld */
131 errString = "Can't create object file image";
132 break;
133 case NSObjectFileImageSuccess:
134 errString = NULL;
135 break;
136 case NSObjectFileImageInappropriateFile:
137 errString = "Inappropriate file type for dynamic loading";
138 break;
139 case NSObjectFileImageArch:
140 errString = "Wrong CPU type in object file";
141 break;
142 NSObjectFileImageAccess:
143 errString = "Can't read object file (no access)";
144 break;
146 if (errString == NULL) {
147 newModule = NSLinkModule(image, pathname, TRUE);
148 if (!newModule)
149 errString = "Failure linking new module";
151 if (errString != NULL) {
152 PyErr_SetString(PyExc_ImportError, errString);
153 return NULL;
155 if (!NSIsSymbolNameDefined(funcname)) {
156 /* UnlinkModule() isn't implemented in current versions, but calling it does no harm */
157 NSUnLinkModule(newModule, FALSE);
158 PyErr_Format(PyExc_ImportError,
159 "Loaded module does not contain symbol %.200s",
160 funcname);
161 return NULL;
163 theSym = NSLookupAndBindSymbol(funcname);
164 p = (dl_funcptr)NSAddressOfSymbol(theSym);
166 #endif /* USE_DYLD */
168 return p;