blob: 892bb478424b261086363bf56c955426c3f41a52 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001
2/* Support for dynamic loading of extension modules */
3
4#include "Python.h"
5#include "importdl.h"
6
7
8#ifdef WITH_DYLD
9
10#define USE_DYLD
11
12#include <mach-o/dyld.h>
13
14#else /* WITH_DYLD */
15
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.
23*/
24#define HANDLE_OBJC_MODULES 1
25#if HANDLE_OBJC_MODULES
26#include <objc/Object.h>
27#include <objc/objc-load.h>
28#endif
29
30#include <mach-o/rld.h>
31
32#endif /* WITH_DYLD */
33
34
35const struct filedescr _PyImport_DynLoadFiletab[] = {
36 {".so", "rb", C_EXTENSION},
37 {"module.so", "rb", C_EXTENSION},
38 {0, 0}
39};
40
Guido van Rossum96a8fb71999-12-22 14:09:35 +000041dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +000042 const char *pathname, FILE *fp)
43{
44 dl_funcptr p = NULL;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000045 char funcname[258];
46
Jeremy Hylton518ab1c2001-11-28 20:42:20 +000047 PyOS_snprintf(funcname, sizeof(funcname), "_init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000048
49#ifdef USE_RLD
50 {
51 NXStream *errorStream;
52 struct mach_header *new_header;
53 const char *filenames[2];
54 long ret;
55 unsigned long ptr;
56
57 errorStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
58 filenames[0] = pathname;
59 filenames[1] = NULL;
60
61#if HANDLE_OBJC_MODULES
62
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
69*/
70
71 [Object name];
72
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.
80*/
81
82 ret = !objc_loadModules(filenames, errorStream,
83 NULL, &new_header, NULL);
84
85#else /* !HANDLE_OBJC_MODULES */
86
87 ret = rld_load(errorStream, &new_header,
88 filenames, NULL);
89
90#endif /* HANDLE_OBJC_MODULES */
91
92 /* extract the error messages for the exception */
93 if(!ret) {
94 char *streamBuf;
95 int len, maxLen;
96
97 NXPutc(errorStream, (char)0);
98
99 NXGetMemoryBuffer(errorStream,
100 &streamBuf, &len, &maxLen);
101 PyErr_SetString(PyExc_ImportError, streamBuf);
102 }
103
104 if(ret && rld_lookup(errorStream, funcname, &ptr))
105 p = (dl_funcptr) ptr;
106
107 NXCloseMemory(errorStream, NX_FREEBUFFER);
108
109 if(!ret)
110 return NULL;
111 }
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. */
117 {
118 NSObjectFileImageReturnCode rc;
119 NSObjectFileImage image;
120 NSModule newModule;
121 NSSymbol theSym;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000122 const char *errString;
123
Jack Jansenabce4162001-08-11 21:54:11 +0000124 if (NSIsSymbolNameDefined(funcname)) {
125 theSym = NSLookupAndBindSymbol(funcname);
126 p = (dl_funcptr)NSAddressOfSymbol(theSym);
127 return p;
128 }
Guido van Rossum22a1d361999-12-20 21:18:49 +0000129 rc = NSCreateObjectFileImageFromFile(pathname, &image);
130 switch(rc) {
131 default:
132 case NSObjectFileImageFailure:
Jack Jansenabce4162001-08-11 21:54:11 +0000133 case NSObjectFileImageFormat:
Guido van Rossum22a1d361999-12-20 21:18:49 +0000134 /* 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;
Jack Jansenabce4162001-08-11 21:54:11 +0000146 case NSObjectFileImageAccess:
Guido van Rossum22a1d361999-12-20 21:18:49 +0000147 errString = "Can't read object file (no access)";
148 break;
149 }
150 if (errString == NULL) {
Jack Jansene3d76222001-12-06 22:58:56 +0000151 newModule = NSLinkModule(image, pathname,
152 NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000153 if (!newModule)
154 errString = "Failure linking new module";
155 }
156 if (errString != NULL) {
157 PyErr_SetString(PyExc_ImportError, errString);
158 return NULL;
159 }
160 if (!NSIsSymbolNameDefined(funcname)) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000161 /* UnlinkModule() isn't implemented in current versions, but calling it does no harm */
Guido van Rossum22a1d361999-12-20 21:18:49 +0000162 NSUnLinkModule(newModule, FALSE);
Guido van Rossum5db862d2000-04-10 12:46:51 +0000163 PyErr_Format(PyExc_ImportError,
164 "Loaded module does not contain symbol %.200s",
165 funcname);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000166 return NULL;
167 }
168 theSym = NSLookupAndBindSymbol(funcname);
169 p = (dl_funcptr)NSAddressOfSymbol(theSym);
170 }
171#endif /* USE_DYLD */
172
173 return p;
174}