blob: 2b9e0bee3765c922e425fbc6ae76d3ef497286b9 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001
2/* Support for dynamic loading of extension modules */
3
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004#ifdef HAVE_DIRECT_H
Guido van Rossum22a1d361999-12-20 21:18:49 +00005#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006#endif
Mark Hammond1f7838b2000-10-05 10:54:45 +00007#include <ctype.h>
Guido van Rossum22a1d361999-12-20 21:18:49 +00008
9#include "Python.h"
10#include "importdl.h"
Guido van Rossume7ba4952007-06-06 23:52:48 +000011#include <windows.h>
Guido van Rossum22a1d361999-12-20 21:18:49 +000012
13const struct filedescr _PyImport_DynLoadFiletab[] = {
14#ifdef _DEBUG
15 {"_d.pyd", "rb", C_EXTENSION},
Guido van Rossum22a1d361999-12-20 21:18:49 +000016#else
17 {".pyd", "rb", C_EXTENSION},
Guido van Rossum22a1d361999-12-20 21:18:49 +000018#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) {
Thomas Heller1df04612004-07-02 08:53:57 +0000119 /* We have at least 2 tables - the import table is the second
120 one. But still it may be that the table size is zero */
121 if (0 == DWORD_AT(dllbase + opt_offset + import_off + sizeof(DWORD)))
122 return NULL;
Mark Hammond1f7838b2000-10-05 10:54:45 +0000123 import_data = dllbase + DWORD_AT(dllbase +
124 opt_offset +
125 import_off);
126 while (DWORD_AT(import_data)) {
127 import_name = dllbase + DWORD_AT(import_data+12);
128 if (strlen(import_name) >= 6 &&
129 !strncmp(import_name,"python",6)) {
130 char *pch;
131
132 /* Ensure python prefix is followed only
133 by numbers to the end of the basename */
134 pch = import_name + 6;
Thomas Heller1df04612004-07-02 08:53:57 +0000135#ifdef _DEBUG
136 while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') {
137#else
Mark Hammond1f7838b2000-10-05 10:54:45 +0000138 while (*pch && *pch != '.') {
Thomas Heller1df04612004-07-02 08:53:57 +0000139#endif
Mark Hammond1f7838b2000-10-05 10:54:45 +0000140 if (*pch >= '0' && *pch <= '9') {
141 pch++;
142 } else {
143 pch = NULL;
144 break;
145 }
146 }
147
148 if (pch) {
149 /* Found it - return the name */
150 return import_name;
151 }
152 }
153 import_data += 20;
154 }
155 }
156
157 return NULL;
158}
Mark Hammond1f7838b2000-10-05 10:54:45 +0000159
160
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000161dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +0000162 const char *pathname, FILE *fp)
163{
164 dl_funcptr p;
Mark Hammond1f7838b2000-10-05 10:54:45 +0000165 char funcname[258], *import_python;
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000166
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000167 PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000168
Guido van Rossum22a1d361999-12-20 21:18:49 +0000169 {
Mark Hammondfb1f68e2001-05-09 00:50:59 +0000170 HINSTANCE hDLL = NULL;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000171 char pathbuf[260];
Mark Hammondfb1f68e2001-05-09 00:50:59 +0000172 LPTSTR dummy;
173 /* We use LoadLibraryEx so Windows looks for dependent DLLs
174 in directory of pathname first. However, Windows95
175 can sometimes not work correctly unless the absolute
176 path is used. If GetFullPathName() fails, the LoadLibrary
177 will certainly fail too, so use its error code */
178 if (GetFullPathName(pathname,
179 sizeof(pathbuf),
180 pathbuf,
181 &dummy))
182 /* XXX This call doesn't exist in Windows CE */
183 hDLL = LoadLibraryEx(pathname, NULL,
184 LOAD_WITH_ALTERED_SEARCH_PATH);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000185 if (hDLL==NULL){
Amaury Forgeot d'Arcd6179e12008-01-03 23:42:13 +0000186 PyObject *message;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000187 unsigned int errorCode;
188
189 /* Get an error string from Win32 error code */
Amaury Forgeot d'Arcd6179e12008-01-03 23:42:13 +0000190 wchar_t theInfo[256]; /* Pointer to error text
Guido van Rossum22a1d361999-12-20 21:18:49 +0000191 from system */
192 int theLength; /* Length of error text */
193
194 errorCode = GetLastError();
195
Amaury Forgeot d'Arcd6179e12008-01-03 23:42:13 +0000196 theLength = FormatMessageW(
197 FORMAT_MESSAGE_FROM_SYSTEM |
198 FORMAT_MESSAGE_IGNORE_INSERTS, /* flags */
Guido van Rossum22a1d361999-12-20 21:18:49 +0000199 NULL, /* message source */
200 errorCode, /* the message (error) ID */
Amaury Forgeot d'Arcd6179e12008-01-03 23:42:13 +0000201 MAKELANGID(LANG_NEUTRAL,
202 SUBLANG_DEFAULT),
203 /* Default language */
204 theInfo, /* the buffer */
Guido van Rossum22a1d361999-12-20 21:18:49 +0000205 sizeof(theInfo), /* the buffer size */
206 NULL); /* no additional format args. */
207
208 /* Problem: could not get the error message.
209 This should not happen if called correctly. */
210 if (theLength == 0) {
Amaury Forgeot d'Arcd6179e12008-01-03 23:42:13 +0000211 message = PyUnicode_FromFormat(
212 "DLL load failed with error code %d",
213 errorCode);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000214 } else {
Guido van Rossum22a1d361999-12-20 21:18:49 +0000215 /* For some reason a \r\n
216 is appended to the text */
217 if (theLength >= 2 &&
218 theInfo[theLength-2] == '\r' &&
219 theInfo[theLength-1] == '\n') {
220 theLength -= 2;
221 theInfo[theLength] = '\0';
222 }
Amaury Forgeot d'Arcd6179e12008-01-03 23:42:13 +0000223 message = PyUnicode_FromString(
224 "DLL load failed: ");
225
226 PyUnicode_AppendAndDel(&message,
227 PyUnicode_FromUnicode(
228 theInfo,
229 theLength));
Guido van Rossum22a1d361999-12-20 21:18:49 +0000230 }
Amaury Forgeot d'Arcd6179e12008-01-03 23:42:13 +0000231 PyErr_SetObject(PyExc_ImportError, message);
232 Py_XDECREF(message);
Fred Drakee20131e2002-08-26 21:20:30 +0000233 return NULL;
Mark Hammond1f7838b2000-10-05 10:54:45 +0000234 } else {
235 char buffer[256];
236
Thomas Heller1df04612004-07-02 08:53:57 +0000237#ifdef _DEBUG
238 PyOS_snprintf(buffer, sizeof(buffer), "python%d%d_d.dll",
239#else
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000240 PyOS_snprintf(buffer, sizeof(buffer), "python%d%d.dll",
Thomas Heller1df04612004-07-02 08:53:57 +0000241#endif
Fred Drakee20131e2002-08-26 21:20:30 +0000242 PY_MAJOR_VERSION,PY_MINOR_VERSION);
Mark Hammond1f7838b2000-10-05 10:54:45 +0000243 import_python = GetPythonImport(hDLL);
244
245 if (import_python &&
246 strcasecmp(buffer,import_python)) {
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000247 PyOS_snprintf(buffer, sizeof(buffer),
248 "Module use of %.150s conflicts "
249 "with this version of Python.",
250 import_python);
Mark Hammond1f7838b2000-10-05 10:54:45 +0000251 PyErr_SetString(PyExc_ImportError,buffer);
252 FreeLibrary(hDLL);
253 return NULL;
254 }
Guido van Rossum22a1d361999-12-20 21:18:49 +0000255 }
256 p = GetProcAddress(hDLL, funcname);
257 }
Guido van Rossum22a1d361999-12-20 21:18:49 +0000258
259 return p;
260}