blob: a352edcf18e3955122c2c24a181c245194f1eb17 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum22a1d361999-12-20 21:18:49 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum22a1d361999-12-20 21:18:49 +00009******************************************************************/
10
11/* Support for dynamic loading of extension modules */
12
13#include "Python.h"
14#include "importdl.h"
15
16
17#ifdef WITH_DYLD
18
19#define USE_DYLD
20
21#include <mach-o/dyld.h>
22
23#else /* WITH_DYLD */
24
25#define USE_RLD
26/* Define this to 1 if you want be able to load ObjC modules as well:
27 it switches between two different way of loading modules on the NeXT,
28 one that directly interfaces with the dynamic loader (rld_load(), which
29 does not correctly load ObjC object files), and another that uses the
30 ObjC runtime (objc_loadModules()) to do the job.
31 You'll have to add ``-ObjC'' to the compiler flags if you set this to 1.
32*/
33#define HANDLE_OBJC_MODULES 1
34#if HANDLE_OBJC_MODULES
35#include <objc/Object.h>
36#include <objc/objc-load.h>
37#endif
38
39#include <mach-o/rld.h>
40
41#endif /* WITH_DYLD */
42
43
44const struct filedescr _PyImport_DynLoadFiletab[] = {
45 {".so", "rb", C_EXTENSION},
46 {"module.so", "rb", C_EXTENSION},
47 {0, 0}
48};
49
Guido van Rossum96a8fb71999-12-22 14:09:35 +000050dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +000051 const char *pathname, FILE *fp)
52{
53 dl_funcptr p = NULL;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000054 char funcname[258];
55
56 sprintf(funcname, "_init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000057
58#ifdef USE_RLD
59 {
60 NXStream *errorStream;
61 struct mach_header *new_header;
62 const char *filenames[2];
63 long ret;
64 unsigned long ptr;
65
66 errorStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
67 filenames[0] = pathname;
68 filenames[1] = NULL;
69
70#if HANDLE_OBJC_MODULES
71
72/* The following very bogus line of code ensures that
73 objc_msgSend, etc are linked into the binary. Without
74 it, dynamic loading of a module that includes objective-c
75 method calls will fail with "undefined symbol _objc_msgSend()".
76 This remains true even in the presence of the -ObjC flag
77 to the compiler
78*/
79
80 [Object name];
81
82/* objc_loadModules() dynamically loads the object files
83 indicated by the paths in filenames. If there are any
84 errors generated during loading -- typically due to the
85 inability to find particular symbols -- an error message
86 will be written to errorStream.
87 It returns 0 if the module is successfully loaded, 1
88 otherwise.
89*/
90
91 ret = !objc_loadModules(filenames, errorStream,
92 NULL, &new_header, NULL);
93
94#else /* !HANDLE_OBJC_MODULES */
95
96 ret = rld_load(errorStream, &new_header,
97 filenames, NULL);
98
99#endif /* HANDLE_OBJC_MODULES */
100
101 /* extract the error messages for the exception */
102 if(!ret) {
103 char *streamBuf;
104 int len, maxLen;
105
106 NXPutc(errorStream, (char)0);
107
108 NXGetMemoryBuffer(errorStream,
109 &streamBuf, &len, &maxLen);
110 PyErr_SetString(PyExc_ImportError, streamBuf);
111 }
112
113 if(ret && rld_lookup(errorStream, funcname, &ptr))
114 p = (dl_funcptr) ptr;
115
116 NXCloseMemory(errorStream, NX_FREEBUFFER);
117
118 if(!ret)
119 return NULL;
120 }
121#endif /* USE_RLD */
122#ifdef USE_DYLD
123 /* This is also NeXT-specific. However, frameworks (the new style
124 of shared library) and rld() can't be used in the same program;
125 instead, you have to use dyld, which is mostly unimplemented. */
126 {
127 NSObjectFileImageReturnCode rc;
128 NSObjectFileImage image;
129 NSModule newModule;
130 NSSymbol theSym;
131 void *symaddr;
132 const char *errString;
133
134 rc = NSCreateObjectFileImageFromFile(pathname, &image);
135 switch(rc) {
136 default:
137 case NSObjectFileImageFailure:
138 NSObjectFileImageFormat:
139 /* for these a message is printed on stderr by dyld */
140 errString = "Can't create object file image";
141 break;
142 case NSObjectFileImageSuccess:
143 errString = NULL;
144 break;
145 case NSObjectFileImageInappropriateFile:
146 errString = "Inappropriate file type for dynamic loading";
147 break;
148 case NSObjectFileImageArch:
149 errString = "Wrong CPU type in object file";
150 break;
151 NSObjectFileImageAccess:
152 errString = "Can't read object file (no access)";
153 break;
154 }
155 if (errString == NULL) {
156 newModule = NSLinkModule(image, pathname, TRUE);
157 if (!newModule)
158 errString = "Failure linking new module";
159 }
160 if (errString != NULL) {
161 PyErr_SetString(PyExc_ImportError, errString);
162 return NULL;
163 }
164 if (!NSIsSymbolNameDefined(funcname)) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000165 /* UnlinkModule() isn't implemented in current versions, but calling it does no harm */
Guido van Rossum22a1d361999-12-20 21:18:49 +0000166 NSUnLinkModule(newModule, FALSE);
Guido van Rossum5db862d2000-04-10 12:46:51 +0000167 PyErr_Format(PyExc_ImportError,
168 "Loaded module does not contain symbol %.200s",
169 funcname);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000170 return NULL;
171 }
172 theSym = NSLookupAndBindSymbol(funcname);
173 p = (dl_funcptr)NSAddressOfSymbol(theSym);
174 }
175#endif /* USE_DYLD */
176
177 return p;
178}