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