blob: 9869f6ae8b94354868cd03ba6aed5a202232d0f2 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001
2/* Support for dynamic loading of extension modules */
3
Christian Heimesfaf2f632008-01-06 16:59:19 +00004#include "Python.h"
5
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006#ifdef HAVE_DIRECT_H
Guido van Rossum22a1d361999-12-20 21:18:49 +00007#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008#endif
Mark Hammond1f7838b2000-10-05 10:54:45 +00009#include <ctype.h>
Guido van Rossum22a1d361999-12-20 21:18:49 +000010
Guido van Rossum22a1d361999-12-20 21:18:49 +000011#include "importdl.h"
Guido van Rossume7ba4952007-06-06 23:52:48 +000012#include <windows.h>
Guido van Rossum22a1d361999-12-20 21:18:49 +000013
Mark Hammond9844a1f2009-01-27 23:46:57 +000014// "activation context" magic - see dl_nt.c...
15extern ULONG_PTR _Py_ActivateActCtx();
16void _Py_DeactivateActCtx(ULONG_PTR cookie);
17
Guido van Rossum22a1d361999-12-20 21:18:49 +000018const struct filedescr _PyImport_DynLoadFiletab[] = {
19#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 {"_d.pyd", "rb", C_EXTENSION},
Guido van Rossum22a1d361999-12-20 21:18:49 +000021#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 {".pyd", "rb", C_EXTENSION},
Guido van Rossum22a1d361999-12-20 21:18:49 +000023#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 {0, 0}
Guido van Rossum22a1d361999-12-20 21:18:49 +000025};
26
27
Mark Hammond1f7838b2000-10-05 10:54:45 +000028/* Case insensitive string compare, to avoid any dependencies on particular
29 C RTL implementations */
30
31static int strcasecmp (char *string1, char *string2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032{
33 int first, second;
Mark Hammond1f7838b2000-10-05 10:54:45 +000034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 do {
36 first = tolower(*string1);
37 second = tolower(*string2);
38 string1++;
39 string2++;
40 } while (first && first == second);
Mark Hammond1f7838b2000-10-05 10:54:45 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 return (first - second);
43}
Mark Hammond1f7838b2000-10-05 10:54:45 +000044
45
46/* Function to return the name of the "python" DLL that the supplied module
47 directly imports. Looks through the list of imported modules and
48 returns the first entry that starts with "python" (case sensitive) and
49 is followed by nothing but numbers until the separator (period).
50
51 Returns a pointer to the import name, or NULL if no matching name was
52 located.
53
54 This function parses through the PE header for the module as loaded in
55 memory by the system loader. The PE header is accessed as documented by
56 Microsoft in the MSDN PE and COFF specification (2/99), and handles
57 both PE32 and PE32+. It only worries about the direct import table and
58 not the delay load import table since it's unlikely an extension is
59 going to be delay loading Python (after all, it's already loaded).
60
61 If any magic values are not found (e.g., the PE header or optional
62 header magic), then this function simply returns NULL. */
63
64#define DWORD_AT(mem) (*(DWORD *)(mem))
65#define WORD_AT(mem) (*(WORD *)(mem))
66
67static char *GetPythonImport (HINSTANCE hModule)
68{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 unsigned char *dllbase, *import_data, *import_name;
70 DWORD pe_offset, opt_offset;
71 WORD opt_magic;
72 int num_dict_off, import_off;
Mark Hammond1f7838b2000-10-05 10:54:45 +000073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 /* Safety check input */
75 if (hModule == NULL) {
76 return NULL;
77 }
Mark Hammond1f7838b2000-10-05 10:54:45 +000078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 /* Module instance is also the base load address. First portion of
80 memory is the MS-DOS loader, which holds the offset to the PE
81 header (from the load base) at 0x3C */
82 dllbase = (unsigned char *)hModule;
83 pe_offset = DWORD_AT(dllbase + 0x3C);
Mark Hammond1f7838b2000-10-05 10:54:45 +000084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 /* The PE signature must be "PE\0\0" */
86 if (memcmp(dllbase+pe_offset,"PE\0\0",4)) {
87 return NULL;
88 }
Mark Hammond1f7838b2000-10-05 10:54:45 +000089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 /* Following the PE signature is the standard COFF header (20
91 bytes) and then the optional header. The optional header starts
92 with a magic value of 0x10B for PE32 or 0x20B for PE32+ (PE32+
93 uses 64-bits for some fields). It might also be 0x107 for a ROM
94 image, but we don't process that here.
Mark Hammond1f7838b2000-10-05 10:54:45 +000095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 The optional header ends with a data dictionary that directly
97 points to certain types of data, among them the import entries
98 (in the second table entry). Based on the header type, we
99 determine offsets for the data dictionary count and the entry
100 within the dictionary pointing to the imports. */
Mark Hammond1f7838b2000-10-05 10:54:45 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 opt_offset = pe_offset + 4 + 20;
103 opt_magic = WORD_AT(dllbase+opt_offset);
104 if (opt_magic == 0x10B) {
105 /* PE32 */
106 num_dict_off = 92;
107 import_off = 104;
108 } else if (opt_magic == 0x20B) {
109 /* PE32+ */
110 num_dict_off = 108;
111 import_off = 120;
112 } else {
113 /* Unsupported */
114 return NULL;
115 }
Mark Hammond1f7838b2000-10-05 10:54:45 +0000116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 /* Now if an import table exists, offset to it and walk the list of
118 imports. The import table is an array (ending when an entry has
119 empty values) of structures (20 bytes each), which contains (at
120 offset 12) a relative address (to the module base) at which a
121 string constant holding the import name is located. */
Mark Hammond1f7838b2000-10-05 10:54:45 +0000122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 if (DWORD_AT(dllbase + opt_offset + num_dict_off) >= 2) {
124 /* We have at least 2 tables - the import table is the second
125 one. But still it may be that the table size is zero */
126 if (0 == DWORD_AT(dllbase + opt_offset + import_off + sizeof(DWORD)))
127 return NULL;
128 import_data = dllbase + DWORD_AT(dllbase +
129 opt_offset +
130 import_off);
131 while (DWORD_AT(import_data)) {
132 import_name = dllbase + DWORD_AT(import_data+12);
133 if (strlen(import_name) >= 6 &&
134 !strncmp(import_name,"python",6)) {
135 char *pch;
Mark Hammond1f7838b2000-10-05 10:54:45 +0000136
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000137#ifndef _DEBUG
138 /* In a release version, don't claim that python3.dll is
139 a Python DLL. */
140 if (strcmp(import_name, "python3.dll") == 0) {
141 import_data += 20;
142 continue;
143 }
144#endif
145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 /* Ensure python prefix is followed only
147 by numbers to the end of the basename */
148 pch = import_name + 6;
Thomas Heller1df04612004-07-02 08:53:57 +0000149#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') {
Thomas Heller1df04612004-07-02 08:53:57 +0000151#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 while (*pch && *pch != '.') {
Thomas Heller1df04612004-07-02 08:53:57 +0000153#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 if (*pch >= '0' && *pch <= '9') {
155 pch++;
156 } else {
157 pch = NULL;
158 break;
159 }
160 }
Mark Hammond1f7838b2000-10-05 10:54:45 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 if (pch) {
163 /* Found it - return the name */
164 return import_name;
165 }
166 }
167 import_data += 20;
168 }
169 }
170
171 return NULL;
Mark Hammond1f7838b2000-10-05 10:54:45 +0000172}
Mark Hammond1f7838b2000-10-05 10:54:45 +0000173
Victor Stinner2d322272011-04-04 23:05:53 +0200174dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
175 PyObject *pathname, FILE *fp)
Guido van Rossum22a1d361999-12-20 21:18:49 +0000176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 dl_funcptr p;
178 char funcname[258], *import_python;
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000179
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000180#ifndef _DEBUG
181 _Py_CheckPython3();
182#endif
183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 PyOS_snprintf(funcname, sizeof(funcname), "PyInit_%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 {
187 HINSTANCE hDLL = NULL;
Victor Stinner2d322272011-04-04 23:05:53 +0200188 wchar_t pathbuf[260];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 unsigned int old_mode;
190 ULONG_PTR cookie = 0;
191 /* We use LoadLibraryEx so Windows looks for dependent DLLs
192 in directory of pathname first. However, Windows95
193 can sometimes not work correctly unless the absolute
194 path is used. If GetFullPathName() fails, the LoadLibrary
195 will certainly fail too, so use its error code */
Christian Heimesbbffeb62008-01-24 09:42:52 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 /* Don't display a message box when Python can't load a DLL */
198 old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
Christian Heimesbbffeb62008-01-24 09:42:52 +0000199
Victor Stinner2d322272011-04-04 23:05:53 +0200200 if (GetFullPathNameW(PyUnicode_AS_UNICODE(pathname),
201 sizeof(pathbuf) / sizeof(pathbuf[0]),
202 pathbuf,
203 NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 ULONG_PTR cookie = _Py_ActivateActCtx();
205 /* XXX This call doesn't exist in Windows CE */
Victor Stinner2d322272011-04-04 23:05:53 +0200206 hDLL = LoadLibraryExW(PyUnicode_AS_UNICODE(pathname), NULL,
207 LOAD_WITH_ALTERED_SEARCH_PATH);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 _Py_DeactivateActCtx(cookie);
209 }
Christian Heimesbbffeb62008-01-24 09:42:52 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 /* restore old error mode settings */
212 SetErrorMode(old_mode);
Christian Heimesbbffeb62008-01-24 09:42:52 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (hDLL==NULL){
215 PyObject *message;
216 unsigned int errorCode;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 /* Get an error string from Win32 error code */
219 wchar_t theInfo[256]; /* Pointer to error text
220 from system */
221 int theLength; /* Length of error text */
Guido van Rossum22a1d361999-12-20 21:18:49 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 errorCode = GetLastError();
Guido van Rossum22a1d361999-12-20 21:18:49 +0000224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 theLength = FormatMessageW(
226 FORMAT_MESSAGE_FROM_SYSTEM |
227 FORMAT_MESSAGE_IGNORE_INSERTS, /* flags */
228 NULL, /* message source */
229 errorCode, /* the message (error) ID */
230 MAKELANGID(LANG_NEUTRAL,
231 SUBLANG_DEFAULT),
232 /* Default language */
233 theInfo, /* the buffer */
234 sizeof(theInfo), /* the buffer size */
235 NULL); /* no additional format args. */
Guido van Rossum22a1d361999-12-20 21:18:49 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 /* Problem: could not get the error message.
238 This should not happen if called correctly. */
239 if (theLength == 0) {
240 message = PyUnicode_FromFormat(
241 "DLL load failed with error code %d",
242 errorCode);
243 } else {
244 /* For some reason a \r\n
245 is appended to the text */
246 if (theLength >= 2 &&
247 theInfo[theLength-2] == '\r' &&
248 theInfo[theLength-1] == '\n') {
249 theLength -= 2;
250 theInfo[theLength] = '\0';
251 }
252 message = PyUnicode_FromString(
253 "DLL load failed: ");
Amaury Forgeot d'Arcd6179e12008-01-03 23:42:13 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 PyUnicode_AppendAndDel(&message,
256 PyUnicode_FromUnicode(
257 theInfo,
258 theLength));
259 }
260 PyErr_SetObject(PyExc_ImportError, message);
261 Py_XDECREF(message);
262 return NULL;
263 } else {
264 char buffer[256];
Mark Hammond1f7838b2000-10-05 10:54:45 +0000265
Victor Stinner2d322272011-04-04 23:05:53 +0200266 PyOS_snprintf(buffer, sizeof(buffer),
Thomas Heller1df04612004-07-02 08:53:57 +0000267#ifdef _DEBUG
Victor Stinner2d322272011-04-04 23:05:53 +0200268 "python%d%d_d.dll",
Thomas Heller1df04612004-07-02 08:53:57 +0000269#else
Victor Stinner2d322272011-04-04 23:05:53 +0200270 "python%d%d.dll",
Thomas Heller1df04612004-07-02 08:53:57 +0000271#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 PY_MAJOR_VERSION,PY_MINOR_VERSION);
273 import_python = GetPythonImport(hDLL);
Mark Hammond1f7838b2000-10-05 10:54:45 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 if (import_python &&
276 strcasecmp(buffer,import_python)) {
Victor Stinner2d322272011-04-04 23:05:53 +0200277 PyErr_Format(PyExc_ImportError,
278 "Module use of %.150s conflicts "
279 "with this version of Python.",
280 import_python);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 FreeLibrary(hDLL);
282 return NULL;
283 }
284 }
285 p = GetProcAddress(hDLL, funcname);
286 }
Guido van Rossum22a1d361999-12-20 21:18:49 +0000287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 return p;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000289}