1 | n/a | |
---|
2 | n/a | /* Support for dynamic loading of extension modules */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | |
---|
6 | n/a | #ifdef HAVE_DIRECT_H |
---|
7 | n/a | #include <direct.h> |
---|
8 | n/a | #endif |
---|
9 | n/a | #include <ctype.h> |
---|
10 | n/a | |
---|
11 | n/a | #include "importdl.h" |
---|
12 | n/a | #include "patchlevel.h" |
---|
13 | n/a | #include <windows.h> |
---|
14 | n/a | |
---|
15 | n/a | // "activation context" magic - see dl_nt.c... |
---|
16 | n/a | #if HAVE_SXS |
---|
17 | n/a | extern ULONG_PTR _Py_ActivateActCtx(); |
---|
18 | n/a | void _Py_DeactivateActCtx(ULONG_PTR cookie); |
---|
19 | n/a | #endif |
---|
20 | n/a | |
---|
21 | n/a | #ifdef _DEBUG |
---|
22 | n/a | #define PYD_DEBUG_SUFFIX "_d" |
---|
23 | n/a | #else |
---|
24 | n/a | #define PYD_DEBUG_SUFFIX "" |
---|
25 | n/a | #endif |
---|
26 | n/a | |
---|
27 | n/a | #ifdef PYD_PLATFORM_TAG |
---|
28 | n/a | #define PYD_TAGGED_SUFFIX PYD_DEBUG_SUFFIX ".cp" Py_STRINGIFY(PY_MAJOR_VERSION) Py_STRINGIFY(PY_MINOR_VERSION) "-" PYD_PLATFORM_TAG ".pyd" |
---|
29 | n/a | #else |
---|
30 | n/a | #define PYD_TAGGED_SUFFIX PYD_DEBUG_SUFFIX ".cp" Py_STRINGIFY(PY_MAJOR_VERSION) Py_STRINGIFY(PY_MINOR_VERSION) ".pyd" |
---|
31 | n/a | #endif |
---|
32 | n/a | |
---|
33 | n/a | #define PYD_UNTAGGED_SUFFIX PYD_DEBUG_SUFFIX ".pyd" |
---|
34 | n/a | |
---|
35 | n/a | const char *_PyImport_DynLoadFiletab[] = { |
---|
36 | n/a | PYD_TAGGED_SUFFIX, |
---|
37 | n/a | PYD_UNTAGGED_SUFFIX, |
---|
38 | n/a | NULL |
---|
39 | n/a | }; |
---|
40 | n/a | |
---|
41 | n/a | /* Case insensitive string compare, to avoid any dependencies on particular |
---|
42 | n/a | C RTL implementations */ |
---|
43 | n/a | |
---|
44 | n/a | static int strcasecmp (const char *string1, const char *string2) |
---|
45 | n/a | { |
---|
46 | n/a | int first, second; |
---|
47 | n/a | |
---|
48 | n/a | do { |
---|
49 | n/a | first = tolower(*string1); |
---|
50 | n/a | second = tolower(*string2); |
---|
51 | n/a | string1++; |
---|
52 | n/a | string2++; |
---|
53 | n/a | } while (first && first == second); |
---|
54 | n/a | |
---|
55 | n/a | return (first - second); |
---|
56 | n/a | } |
---|
57 | n/a | |
---|
58 | n/a | |
---|
59 | n/a | /* Function to return the name of the "python" DLL that the supplied module |
---|
60 | n/a | directly imports. Looks through the list of imported modules and |
---|
61 | n/a | returns the first entry that starts with "python" (case sensitive) and |
---|
62 | n/a | is followed by nothing but numbers until the separator (period). |
---|
63 | n/a | |
---|
64 | n/a | Returns a pointer to the import name, or NULL if no matching name was |
---|
65 | n/a | located. |
---|
66 | n/a | |
---|
67 | n/a | This function parses through the PE header for the module as loaded in |
---|
68 | n/a | memory by the system loader. The PE header is accessed as documented by |
---|
69 | n/a | Microsoft in the MSDN PE and COFF specification (2/99), and handles |
---|
70 | n/a | both PE32 and PE32+. It only worries about the direct import table and |
---|
71 | n/a | not the delay load import table since it's unlikely an extension is |
---|
72 | n/a | going to be delay loading Python (after all, it's already loaded). |
---|
73 | n/a | |
---|
74 | n/a | If any magic values are not found (e.g., the PE header or optional |
---|
75 | n/a | header magic), then this function simply returns NULL. */ |
---|
76 | n/a | |
---|
77 | n/a | #define DWORD_AT(mem) (*(DWORD *)(mem)) |
---|
78 | n/a | #define WORD_AT(mem) (*(WORD *)(mem)) |
---|
79 | n/a | |
---|
80 | n/a | static char *GetPythonImport (HINSTANCE hModule) |
---|
81 | n/a | { |
---|
82 | n/a | unsigned char *dllbase, *import_data, *import_name; |
---|
83 | n/a | DWORD pe_offset, opt_offset; |
---|
84 | n/a | WORD opt_magic; |
---|
85 | n/a | int num_dict_off, import_off; |
---|
86 | n/a | |
---|
87 | n/a | /* Safety check input */ |
---|
88 | n/a | if (hModule == NULL) { |
---|
89 | n/a | return NULL; |
---|
90 | n/a | } |
---|
91 | n/a | |
---|
92 | n/a | /* Module instance is also the base load address. First portion of |
---|
93 | n/a | memory is the MS-DOS loader, which holds the offset to the PE |
---|
94 | n/a | header (from the load base) at 0x3C */ |
---|
95 | n/a | dllbase = (unsigned char *)hModule; |
---|
96 | n/a | pe_offset = DWORD_AT(dllbase + 0x3C); |
---|
97 | n/a | |
---|
98 | n/a | /* The PE signature must be "PE\0\0" */ |
---|
99 | n/a | if (memcmp(dllbase+pe_offset,"PE\0\0",4)) { |
---|
100 | n/a | return NULL; |
---|
101 | n/a | } |
---|
102 | n/a | |
---|
103 | n/a | /* Following the PE signature is the standard COFF header (20 |
---|
104 | n/a | bytes) and then the optional header. The optional header starts |
---|
105 | n/a | with a magic value of 0x10B for PE32 or 0x20B for PE32+ (PE32+ |
---|
106 | n/a | uses 64-bits for some fields). It might also be 0x107 for a ROM |
---|
107 | n/a | image, but we don't process that here. |
---|
108 | n/a | |
---|
109 | n/a | The optional header ends with a data dictionary that directly |
---|
110 | n/a | points to certain types of data, among them the import entries |
---|
111 | n/a | (in the second table entry). Based on the header type, we |
---|
112 | n/a | determine offsets for the data dictionary count and the entry |
---|
113 | n/a | within the dictionary pointing to the imports. */ |
---|
114 | n/a | |
---|
115 | n/a | opt_offset = pe_offset + 4 + 20; |
---|
116 | n/a | opt_magic = WORD_AT(dllbase+opt_offset); |
---|
117 | n/a | if (opt_magic == 0x10B) { |
---|
118 | n/a | /* PE32 */ |
---|
119 | n/a | num_dict_off = 92; |
---|
120 | n/a | import_off = 104; |
---|
121 | n/a | } else if (opt_magic == 0x20B) { |
---|
122 | n/a | /* PE32+ */ |
---|
123 | n/a | num_dict_off = 108; |
---|
124 | n/a | import_off = 120; |
---|
125 | n/a | } else { |
---|
126 | n/a | /* Unsupported */ |
---|
127 | n/a | return NULL; |
---|
128 | n/a | } |
---|
129 | n/a | |
---|
130 | n/a | /* Now if an import table exists, offset to it and walk the list of |
---|
131 | n/a | imports. The import table is an array (ending when an entry has |
---|
132 | n/a | empty values) of structures (20 bytes each), which contains (at |
---|
133 | n/a | offset 12) a relative address (to the module base) at which a |
---|
134 | n/a | string constant holding the import name is located. */ |
---|
135 | n/a | |
---|
136 | n/a | if (DWORD_AT(dllbase + opt_offset + num_dict_off) >= 2) { |
---|
137 | n/a | /* We have at least 2 tables - the import table is the second |
---|
138 | n/a | one. But still it may be that the table size is zero */ |
---|
139 | n/a | if (0 == DWORD_AT(dllbase + opt_offset + import_off + sizeof(DWORD))) |
---|
140 | n/a | return NULL; |
---|
141 | n/a | import_data = dllbase + DWORD_AT(dllbase + |
---|
142 | n/a | opt_offset + |
---|
143 | n/a | import_off); |
---|
144 | n/a | while (DWORD_AT(import_data)) { |
---|
145 | n/a | import_name = dllbase + DWORD_AT(import_data+12); |
---|
146 | n/a | if (strlen(import_name) >= 6 && |
---|
147 | n/a | !strncmp(import_name,"python",6)) { |
---|
148 | n/a | char *pch; |
---|
149 | n/a | |
---|
150 | n/a | #ifndef _DEBUG |
---|
151 | n/a | /* In a release version, don't claim that python3.dll is |
---|
152 | n/a | a Python DLL. */ |
---|
153 | n/a | if (strcmp(import_name, "python3.dll") == 0) { |
---|
154 | n/a | import_data += 20; |
---|
155 | n/a | continue; |
---|
156 | n/a | } |
---|
157 | n/a | #endif |
---|
158 | n/a | |
---|
159 | n/a | /* Ensure python prefix is followed only |
---|
160 | n/a | by numbers to the end of the basename */ |
---|
161 | n/a | pch = import_name + 6; |
---|
162 | n/a | #ifdef _DEBUG |
---|
163 | n/a | while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') { |
---|
164 | n/a | #else |
---|
165 | n/a | while (*pch && *pch != '.') { |
---|
166 | n/a | #endif |
---|
167 | n/a | if (*pch >= '0' && *pch <= '9') { |
---|
168 | n/a | pch++; |
---|
169 | n/a | } else { |
---|
170 | n/a | pch = NULL; |
---|
171 | n/a | break; |
---|
172 | n/a | } |
---|
173 | n/a | } |
---|
174 | n/a | |
---|
175 | n/a | if (pch) { |
---|
176 | n/a | /* Found it - return the name */ |
---|
177 | n/a | return import_name; |
---|
178 | n/a | } |
---|
179 | n/a | } |
---|
180 | n/a | import_data += 20; |
---|
181 | n/a | } |
---|
182 | n/a | } |
---|
183 | n/a | |
---|
184 | n/a | return NULL; |
---|
185 | n/a | } |
---|
186 | n/a | |
---|
187 | n/a | dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix, |
---|
188 | n/a | const char *shortname, |
---|
189 | n/a | PyObject *pathname, FILE *fp) |
---|
190 | n/a | { |
---|
191 | n/a | dl_funcptr p; |
---|
192 | n/a | char funcname[258], *import_python; |
---|
193 | n/a | wchar_t *wpathname; |
---|
194 | n/a | |
---|
195 | n/a | #ifndef _DEBUG |
---|
196 | n/a | _Py_CheckPython3(); |
---|
197 | n/a | #endif |
---|
198 | n/a | |
---|
199 | n/a | wpathname = PyUnicode_AsUnicode(pathname); |
---|
200 | n/a | if (wpathname == NULL) |
---|
201 | n/a | return NULL; |
---|
202 | n/a | |
---|
203 | n/a | PyOS_snprintf(funcname, sizeof(funcname), "%.20s_%.200s", prefix, shortname); |
---|
204 | n/a | |
---|
205 | n/a | { |
---|
206 | n/a | HINSTANCE hDLL = NULL; |
---|
207 | n/a | unsigned int old_mode; |
---|
208 | n/a | #if HAVE_SXS |
---|
209 | n/a | ULONG_PTR cookie = 0; |
---|
210 | n/a | #endif |
---|
211 | n/a | |
---|
212 | n/a | /* Don't display a message box when Python can't load a DLL */ |
---|
213 | n/a | old_mode = SetErrorMode(SEM_FAILCRITICALERRORS); |
---|
214 | n/a | |
---|
215 | n/a | #if HAVE_SXS |
---|
216 | n/a | cookie = _Py_ActivateActCtx(); |
---|
217 | n/a | #endif |
---|
218 | n/a | /* We use LoadLibraryEx so Windows looks for dependent DLLs |
---|
219 | n/a | in directory of pathname first. */ |
---|
220 | n/a | /* XXX This call doesn't exist in Windows CE */ |
---|
221 | n/a | hDLL = LoadLibraryExW(wpathname, NULL, |
---|
222 | n/a | LOAD_WITH_ALTERED_SEARCH_PATH); |
---|
223 | n/a | #if HAVE_SXS |
---|
224 | n/a | _Py_DeactivateActCtx(cookie); |
---|
225 | n/a | #endif |
---|
226 | n/a | |
---|
227 | n/a | /* restore old error mode settings */ |
---|
228 | n/a | SetErrorMode(old_mode); |
---|
229 | n/a | |
---|
230 | n/a | if (hDLL==NULL){ |
---|
231 | n/a | PyObject *message; |
---|
232 | n/a | unsigned int errorCode; |
---|
233 | n/a | |
---|
234 | n/a | /* Get an error string from Win32 error code */ |
---|
235 | n/a | wchar_t theInfo[256]; /* Pointer to error text |
---|
236 | n/a | from system */ |
---|
237 | n/a | int theLength; /* Length of error text */ |
---|
238 | n/a | |
---|
239 | n/a | errorCode = GetLastError(); |
---|
240 | n/a | |
---|
241 | n/a | theLength = FormatMessageW( |
---|
242 | n/a | FORMAT_MESSAGE_FROM_SYSTEM | |
---|
243 | n/a | FORMAT_MESSAGE_IGNORE_INSERTS, /* flags */ |
---|
244 | n/a | NULL, /* message source */ |
---|
245 | n/a | errorCode, /* the message (error) ID */ |
---|
246 | n/a | MAKELANGID(LANG_NEUTRAL, |
---|
247 | n/a | SUBLANG_DEFAULT), |
---|
248 | n/a | /* Default language */ |
---|
249 | n/a | theInfo, /* the buffer */ |
---|
250 | n/a | sizeof(theInfo) / sizeof(wchar_t), /* size in wchars */ |
---|
251 | n/a | NULL); /* no additional format args. */ |
---|
252 | n/a | |
---|
253 | n/a | /* Problem: could not get the error message. |
---|
254 | n/a | This should not happen if called correctly. */ |
---|
255 | n/a | if (theLength == 0) { |
---|
256 | n/a | message = PyUnicode_FromFormat( |
---|
257 | n/a | "DLL load failed with error code %d", |
---|
258 | n/a | errorCode); |
---|
259 | n/a | } else { |
---|
260 | n/a | /* For some reason a \r\n |
---|
261 | n/a | is appended to the text */ |
---|
262 | n/a | if (theLength >= 2 && |
---|
263 | n/a | theInfo[theLength-2] == '\r' && |
---|
264 | n/a | theInfo[theLength-1] == '\n') { |
---|
265 | n/a | theLength -= 2; |
---|
266 | n/a | theInfo[theLength] = '\0'; |
---|
267 | n/a | } |
---|
268 | n/a | message = PyUnicode_FromString( |
---|
269 | n/a | "DLL load failed: "); |
---|
270 | n/a | |
---|
271 | n/a | PyUnicode_AppendAndDel(&message, |
---|
272 | n/a | PyUnicode_FromWideChar( |
---|
273 | n/a | theInfo, |
---|
274 | n/a | theLength)); |
---|
275 | n/a | } |
---|
276 | n/a | if (message != NULL) { |
---|
277 | n/a | PyObject *shortname_obj = PyUnicode_FromString(shortname); |
---|
278 | n/a | PyErr_SetImportError(message, shortname_obj, pathname); |
---|
279 | n/a | Py_XDECREF(shortname_obj); |
---|
280 | n/a | Py_DECREF(message); |
---|
281 | n/a | } |
---|
282 | n/a | return NULL; |
---|
283 | n/a | } else { |
---|
284 | n/a | char buffer[256]; |
---|
285 | n/a | |
---|
286 | n/a | PyOS_snprintf(buffer, sizeof(buffer), |
---|
287 | n/a | #ifdef _DEBUG |
---|
288 | n/a | "python%d%d_d.dll", |
---|
289 | n/a | #else |
---|
290 | n/a | "python%d%d.dll", |
---|
291 | n/a | #endif |
---|
292 | n/a | PY_MAJOR_VERSION,PY_MINOR_VERSION); |
---|
293 | n/a | import_python = GetPythonImport(hDLL); |
---|
294 | n/a | |
---|
295 | n/a | if (import_python && |
---|
296 | n/a | strcasecmp(buffer,import_python)) { |
---|
297 | n/a | PyErr_Format(PyExc_ImportError, |
---|
298 | n/a | "Module use of %.150s conflicts " |
---|
299 | n/a | "with this version of Python.", |
---|
300 | n/a | import_python); |
---|
301 | n/a | FreeLibrary(hDLL); |
---|
302 | n/a | return NULL; |
---|
303 | n/a | } |
---|
304 | n/a | } |
---|
305 | n/a | p = GetProcAddress(hDLL, funcname); |
---|
306 | n/a | } |
---|
307 | n/a | |
---|
308 | n/a | return p; |
---|
309 | n/a | } |
---|