blob: fc641b9ff9e2d034186cc5097ee74f7c4f60aecc [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){
186 char errBuf[256];
187 unsigned int errorCode;
188
189 /* Get an error string from Win32 error code */
190 char theInfo[256]; /* Pointer to error text
191 from system */
192 int theLength; /* Length of error text */
193
194 errorCode = GetLastError();
195
196 theLength = FormatMessage(
197 FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
198 NULL, /* message source */
199 errorCode, /* the message (error) ID */
200 0, /* default language environment */
201 (LPTSTR) theInfo, /* the buffer */
202 sizeof(theInfo), /* the buffer size */
203 NULL); /* no additional format args. */
204
205 /* Problem: could not get the error message.
206 This should not happen if called correctly. */
207 if (theLength == 0) {
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000208 PyOS_snprintf(errBuf, sizeof(errBuf),
209 "DLL load failed with error code %d",
210 errorCode);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000211 } else {
Guido van Rossum582acec2000-06-28 22:07:35 +0000212 size_t len;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000213 /* For some reason a \r\n
214 is appended to the text */
215 if (theLength >= 2 &&
216 theInfo[theLength-2] == '\r' &&
217 theInfo[theLength-1] == '\n') {
218 theLength -= 2;
219 theInfo[theLength] = '\0';
220 }
221 strcpy(errBuf, "DLL load failed: ");
222 len = strlen(errBuf);
223 strncpy(errBuf+len, theInfo,
224 sizeof(errBuf)-len);
225 errBuf[sizeof(errBuf)-1] = '\0';
226 }
227 PyErr_SetString(PyExc_ImportError, errBuf);
Fred Drakee20131e2002-08-26 21:20:30 +0000228 return NULL;
Mark Hammond1f7838b2000-10-05 10:54:45 +0000229 } else {
230 char buffer[256];
231
Thomas Heller1df04612004-07-02 08:53:57 +0000232#ifdef _DEBUG
233 PyOS_snprintf(buffer, sizeof(buffer), "python%d%d_d.dll",
234#else
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000235 PyOS_snprintf(buffer, sizeof(buffer), "python%d%d.dll",
Thomas Heller1df04612004-07-02 08:53:57 +0000236#endif
Fred Drakee20131e2002-08-26 21:20:30 +0000237 PY_MAJOR_VERSION,PY_MINOR_VERSION);
Mark Hammond1f7838b2000-10-05 10:54:45 +0000238 import_python = GetPythonImport(hDLL);
239
240 if (import_python &&
241 strcasecmp(buffer,import_python)) {
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000242 PyOS_snprintf(buffer, sizeof(buffer),
243 "Module use of %.150s conflicts "
244 "with this version of Python.",
245 import_python);
Mark Hammond1f7838b2000-10-05 10:54:45 +0000246 PyErr_SetString(PyExc_ImportError,buffer);
247 FreeLibrary(hDLL);
248 return NULL;
249 }
Guido van Rossum22a1d361999-12-20 21:18:49 +0000250 }
251 p = GetProcAddress(hDLL, funcname);
252 }
Guido van Rossum22a1d361999-12-20 21:18:49 +0000253
254 return p;
255}