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