blob: 9062cf3b8815e024e39ce11c1a3c19fdcd792e53 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001
2/* Support for dynamic loading of extension modules */
3
4#include <windows.h>
5#include <direct.h>
Mark Hammond1f7838b2000-10-05 10:54:45 +00006#include <ctype.h>
Guido van Rossum22a1d361999-12-20 21:18:49 +00007
8#include "Python.h"
9#include "importdl.h"
10
11const struct filedescr _PyImport_DynLoadFiletab[] = {
12#ifdef _DEBUG
13 {"_d.pyd", "rb", C_EXTENSION},
14 {"_d.dll", "rb", C_EXTENSION},
15#else
16 {".pyd", "rb", C_EXTENSION},
17 {".dll", "rb", C_EXTENSION},
18#endif
19 {0, 0}
20};
21
22
Mark Hammond1f7838b2000-10-05 10:54:45 +000023#ifdef MS_WIN32
24
25/* Case insensitive string compare, to avoid any dependencies on particular
26 C RTL implementations */
27
28static int strcasecmp (char *string1, char *string2)
29{
30 int first, second;
31
32 do {
33 first = tolower(*string1);
34 second = tolower(*string2);
35 string1++;
36 string2++;
37 } while (first && first == second);
38
39 return (first - second);
40}
41
42
43/* Function to return the name of the "python" DLL that the supplied module
44 directly imports. Looks through the list of imported modules and
45 returns the first entry that starts with "python" (case sensitive) and
46 is followed by nothing but numbers until the separator (period).
47
48 Returns a pointer to the import name, or NULL if no matching name was
49 located.
50
51 This function parses through the PE header for the module as loaded in
52 memory by the system loader. The PE header is accessed as documented by
53 Microsoft in the MSDN PE and COFF specification (2/99), and handles
54 both PE32 and PE32+. It only worries about the direct import table and
55 not the delay load import table since it's unlikely an extension is
56 going to be delay loading Python (after all, it's already loaded).
57
58 If any magic values are not found (e.g., the PE header or optional
59 header magic), then this function simply returns NULL. */
60
61#define DWORD_AT(mem) (*(DWORD *)(mem))
62#define WORD_AT(mem) (*(WORD *)(mem))
63
64static char *GetPythonImport (HINSTANCE hModule)
65{
66 unsigned char *dllbase, *import_data, *import_name;
67 DWORD pe_offset, opt_offset;
68 WORD opt_magic;
69 int num_dict_off, import_off;
70
71 /* Safety check input */
72 if (hModule == NULL) {
73 return NULL;
74 }
75
76 /* Module instance is also the base load address. First portion of
77 memory is the MS-DOS loader, which holds the offset to the PE
78 header (from the load base) at 0x3C */
79 dllbase = (unsigned char *)hModule;
80 pe_offset = DWORD_AT(dllbase + 0x3C);
81
82 /* The PE signature must be "PE\0\0" */
83 if (memcmp(dllbase+pe_offset,"PE\0\0",4)) {
84 return NULL;
85 }
86
87 /* Following the PE signature is the standard COFF header (20
88 bytes) and then the optional header. The optional header starts
89 with a magic value of 0x10B for PE32 or 0x20B for PE32+ (PE32+
90 uses 64-bits for some fields). It might also be 0x107 for a ROM
91 image, but we don't process that here.
92
93 The optional header ends with a data dictionary that directly
94 points to certain types of data, among them the import entries
95 (in the second table entry). Based on the header type, we
96 determine offsets for the data dictionary count and the entry
97 within the dictionary pointing to the imports. */
98
99 opt_offset = pe_offset + 4 + 20;
100 opt_magic = WORD_AT(dllbase+opt_offset);
101 if (opt_magic == 0x10B) {
102 /* PE32 */
103 num_dict_off = 92;
104 import_off = 104;
105 } else if (opt_magic == 0x20B) {
106 /* PE32+ */
107 num_dict_off = 108;
108 import_off = 120;
109 } else {
110 /* Unsupported */
111 return NULL;
112 }
113
114 /* Now if an import table exists, offset to it and walk the list of
115 imports. The import table is an array (ending when an entry has
116 empty values) of structures (20 bytes each), which contains (at
117 offset 12) a relative address (to the module base) at which a
118 string constant holding the import name is located. */
119
120 if (DWORD_AT(dllbase + opt_offset + num_dict_off) >= 2) {
121 import_data = dllbase + DWORD_AT(dllbase +
122 opt_offset +
123 import_off);
124 while (DWORD_AT(import_data)) {
125 import_name = dllbase + DWORD_AT(import_data+12);
126 if (strlen(import_name) >= 6 &&
127 !strncmp(import_name,"python",6)) {
128 char *pch;
129
130 /* Ensure python prefix is followed only
131 by numbers to the end of the basename */
132 pch = import_name + 6;
133 while (*pch && *pch != '.') {
134 if (*pch >= '0' && *pch <= '9') {
135 pch++;
136 } else {
137 pch = NULL;
138 break;
139 }
140 }
141
142 if (pch) {
143 /* Found it - return the name */
144 return import_name;
145 }
146 }
147 import_data += 20;
148 }
149 }
150
151 return NULL;
152}
153#endif /* MS_WIN32 */
154
155
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000156dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +0000157 const char *pathname, FILE *fp)
158{
159 dl_funcptr p;
Mark Hammond1f7838b2000-10-05 10:54:45 +0000160 char funcname[258], *import_python;
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000161
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000162 PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000163
164#ifdef MS_WIN32
165 {
Mark Hammondfb1f68e2001-05-09 00:50:59 +0000166 HINSTANCE hDLL = NULL;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000167 char pathbuf[260];
Mark Hammondfb1f68e2001-05-09 00:50:59 +0000168 LPTSTR dummy;
169 /* We use LoadLibraryEx so Windows looks for dependent DLLs
170 in directory of pathname first. However, Windows95
171 can sometimes not work correctly unless the absolute
172 path is used. If GetFullPathName() fails, the LoadLibrary
173 will certainly fail too, so use its error code */
174 if (GetFullPathName(pathname,
175 sizeof(pathbuf),
176 pathbuf,
177 &dummy))
178 /* XXX This call doesn't exist in Windows CE */
179 hDLL = LoadLibraryEx(pathname, NULL,
180 LOAD_WITH_ALTERED_SEARCH_PATH);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000181 if (hDLL==NULL){
182 char errBuf[256];
183 unsigned int errorCode;
184
185 /* Get an error string from Win32 error code */
186 char theInfo[256]; /* Pointer to error text
187 from system */
188 int theLength; /* Length of error text */
189
190 errorCode = GetLastError();
191
192 theLength = FormatMessage(
193 FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
194 NULL, /* message source */
195 errorCode, /* the message (error) ID */
196 0, /* default language environment */
197 (LPTSTR) theInfo, /* the buffer */
198 sizeof(theInfo), /* the buffer size */
199 NULL); /* no additional format args. */
200
201 /* Problem: could not get the error message.
202 This should not happen if called correctly. */
203 if (theLength == 0) {
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000204 PyOS_snprintf(errBuf, sizeof(errBuf),
205 "DLL load failed with error code %d",
206 errorCode);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000207 } else {
Guido van Rossum582acec2000-06-28 22:07:35 +0000208 size_t len;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000209 /* For some reason a \r\n
210 is appended to the text */
211 if (theLength >= 2 &&
212 theInfo[theLength-2] == '\r' &&
213 theInfo[theLength-1] == '\n') {
214 theLength -= 2;
215 theInfo[theLength] = '\0';
216 }
217 strcpy(errBuf, "DLL load failed: ");
218 len = strlen(errBuf);
219 strncpy(errBuf+len, theInfo,
220 sizeof(errBuf)-len);
221 errBuf[sizeof(errBuf)-1] = '\0';
222 }
223 PyErr_SetString(PyExc_ImportError, errBuf);
224 return NULL;
Mark Hammond1f7838b2000-10-05 10:54:45 +0000225 } else {
226 char buffer[256];
227
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000228 PyOS_snprintf(buffer, sizeof(buffer), "python%d%d.dll",
Mark Hammond1f7838b2000-10-05 10:54:45 +0000229 PY_MAJOR_VERSION,PY_MINOR_VERSION);
230 import_python = GetPythonImport(hDLL);
231
232 if (import_python &&
233 strcasecmp(buffer,import_python)) {
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000234 PyOS_snprintf(buffer, sizeof(buffer),
235 "Module use of %.150s conflicts "
236 "with this version of Python.",
237 import_python);
Mark Hammond1f7838b2000-10-05 10:54:45 +0000238 PyErr_SetString(PyExc_ImportError,buffer);
239 FreeLibrary(hDLL);
240 return NULL;
241 }
Guido van Rossum22a1d361999-12-20 21:18:49 +0000242 }
243 p = GetProcAddress(hDLL, funcname);
244 }
245#endif /* MS_WIN32 */
246#ifdef MS_WIN16
247 {
248 HINSTANCE hDLL;
249 char pathbuf[16];
250 if (strchr(pathname, '\\') == NULL &&
251 strchr(pathname, '/') == NULL)
252 {
253 /* Prefix bare filename with ".\" */
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000254 PyOS_snprintf(pathbuf, sizeof(pathbuf),
255 ".\\%-.13s", pathname);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000256 pathname = pathbuf;
257 }
258 hDLL = LoadLibrary(pathname);
259 if (hDLL < HINSTANCE_ERROR){
260 char errBuf[256];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000261 PyOS_snprintf(errBuf, sizeof(errBuf),
262 "DLL load failed with error code %d",
263 hDLL);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000264 PyErr_SetString(PyExc_ImportError, errBuf);
265 return NULL;
266 }
267 p = GetProcAddress(hDLL, funcname);
268 }
269#endif /* MS_WIN16 */
270
271 return p;
272}