Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 1 | |
| 2 | /* Support for dynamic loading of extension modules */ |
| 3 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 4 | #include "Python.h" |
| 5 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 7 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8 | #endif |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 9 | #include <ctype.h> |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 11 | #include "importdl.h" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 12 | #include <windows.h> |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 13 | |
Mark Hammond | 9844a1f | 2009-01-27 23:46:57 +0000 | [diff] [blame] | 14 | // "activation context" magic - see dl_nt.c... |
| 15 | extern ULONG_PTR _Py_ActivateActCtx(); |
| 16 | void _Py_DeactivateActCtx(ULONG_PTR cookie); |
| 17 | |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 18 | const char *_PyImport_DynLoadFiletab[] = { |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 19 | #ifdef _DEBUG |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 20 | "_d.pyd", |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 21 | #else |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 22 | ".pyd", |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 23 | #endif |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 24 | NULL |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 28 | /* Case insensitive string compare, to avoid any dependencies on particular |
| 29 | C RTL implementations */ |
| 30 | |
| 31 | static int strcasecmp (char *string1, char *string2) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 32 | { |
| 33 | int first, second; |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 34 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | do { |
| 36 | first = tolower(*string1); |
| 37 | second = tolower(*string2); |
| 38 | string1++; |
| 39 | string2++; |
| 40 | } while (first && first == second); |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 41 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | return (first - second); |
| 43 | } |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 44 | |
| 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 | |
| 67 | static char *GetPythonImport (HINSTANCE hModule) |
| 68 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | 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 Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 73 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | /* Safety check input */ |
| 75 | if (hModule == NULL) { |
| 76 | return NULL; |
| 77 | } |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 78 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | /* 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 Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 84 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | /* The PE signature must be "PE\0\0" */ |
| 86 | if (memcmp(dllbase+pe_offset,"PE\0\0",4)) { |
| 87 | return NULL; |
| 88 | } |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 89 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | /* 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 Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | 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 Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | 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 Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | /* 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 Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | 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 Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 136 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 137 | #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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | /* Ensure python prefix is followed only |
| 147 | by numbers to the end of the basename */ |
| 148 | pch = import_name + 6; |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 149 | #ifdef _DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') { |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 151 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | while (*pch && *pch != '.') { |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 153 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | if (*pch >= '0' && *pch <= '9') { |
| 155 | pch++; |
| 156 | } else { |
| 157 | pch = NULL; |
| 158 | break; |
| 159 | } |
| 160 | } |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | 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 Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 172 | } |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 173 | |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 174 | dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname, |
| 175 | PyObject *pathname, FILE *fp) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 176 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | dl_funcptr p; |
| 178 | char funcname[258], *import_python; |
Victor Stinner | 3335447 | 2011-11-21 02:01:41 +0100 | [diff] [blame] | 179 | wchar_t *wpathname; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 180 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 181 | #ifndef _DEBUG |
| 182 | _Py_CheckPython3(); |
| 183 | #endif |
| 184 | |
Victor Stinner | 3335447 | 2011-11-21 02:01:41 +0100 | [diff] [blame] | 185 | wpathname = PyUnicode_AsUnicode(pathname); |
| 186 | if (wpathname == NULL) |
| 187 | return NULL; |
| 188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | PyOS_snprintf(funcname, sizeof(funcname), "PyInit_%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | { |
| 192 | HINSTANCE hDLL = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | unsigned int old_mode; |
| 194 | ULONG_PTR cookie = 0; |
Victor Stinner | 67002af | 2011-10-02 20:35:10 +0200 | [diff] [blame] | 195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | /* Don't display a message box when Python can't load a DLL */ |
| 197 | old_mode = SetErrorMode(SEM_FAILCRITICALERRORS); |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 198 | |
Brian Curtin | 589f89e | 2011-06-09 17:55:54 -0500 | [diff] [blame] | 199 | cookie = _Py_ActivateActCtx(); |
| 200 | /* We use LoadLibraryEx so Windows looks for dependent DLLs |
| 201 | in directory of pathname first. */ |
| 202 | /* XXX This call doesn't exist in Windows CE */ |
Victor Stinner | 3335447 | 2011-11-21 02:01:41 +0100 | [diff] [blame] | 203 | hDLL = LoadLibraryExW(wpathname, NULL, |
Brian Curtin | 589f89e | 2011-06-09 17:55:54 -0500 | [diff] [blame] | 204 | LOAD_WITH_ALTERED_SEARCH_PATH); |
| 205 | _Py_DeactivateActCtx(cookie); |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | /* restore old error mode settings */ |
| 208 | SetErrorMode(old_mode); |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | if (hDLL==NULL){ |
| 211 | PyObject *message; |
| 212 | unsigned int errorCode; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | /* Get an error string from Win32 error code */ |
| 215 | wchar_t theInfo[256]; /* Pointer to error text |
| 216 | from system */ |
| 217 | int theLength; /* Length of error text */ |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | errorCode = GetLastError(); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | theLength = FormatMessageW( |
| 222 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 223 | FORMAT_MESSAGE_IGNORE_INSERTS, /* flags */ |
| 224 | NULL, /* message source */ |
| 225 | errorCode, /* the message (error) ID */ |
| 226 | MAKELANGID(LANG_NEUTRAL, |
| 227 | SUBLANG_DEFAULT), |
| 228 | /* Default language */ |
| 229 | theInfo, /* the buffer */ |
| 230 | sizeof(theInfo), /* the buffer size */ |
| 231 | NULL); /* no additional format args. */ |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | /* Problem: could not get the error message. |
| 234 | This should not happen if called correctly. */ |
| 235 | if (theLength == 0) { |
| 236 | message = PyUnicode_FromFormat( |
| 237 | "DLL load failed with error code %d", |
| 238 | errorCode); |
| 239 | } else { |
| 240 | /* For some reason a \r\n |
| 241 | is appended to the text */ |
| 242 | if (theLength >= 2 && |
| 243 | theInfo[theLength-2] == '\r' && |
| 244 | theInfo[theLength-1] == '\n') { |
| 245 | theLength -= 2; |
| 246 | theInfo[theLength] = '\0'; |
| 247 | } |
| 248 | message = PyUnicode_FromString( |
| 249 | "DLL load failed: "); |
Amaury Forgeot d'Arc | d6179e1 | 2008-01-03 23:42:13 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | PyUnicode_AppendAndDel(&message, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 252 | PyUnicode_FromWideChar( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | theInfo, |
| 254 | theLength)); |
| 255 | } |
Victor Stinner | 67002af | 2011-10-02 20:35:10 +0200 | [diff] [blame] | 256 | if (message != NULL) { |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 257 | PyErr_SetImportError(message, PyUnicode_FromString(shortname), |
| 258 | pathname); |
| 259 | Py_DECREF(message); |
Victor Stinner | 67002af | 2011-10-02 20:35:10 +0200 | [diff] [blame] | 260 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | return NULL; |
| 262 | } else { |
| 263 | char buffer[256]; |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 264 | |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 265 | PyOS_snprintf(buffer, sizeof(buffer), |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 266 | #ifdef _DEBUG |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 267 | "python%d%d_d.dll", |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 268 | #else |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 269 | "python%d%d.dll", |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 270 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | PY_MAJOR_VERSION,PY_MINOR_VERSION); |
| 272 | import_python = GetPythonImport(hDLL); |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | if (import_python && |
| 275 | strcasecmp(buffer,import_python)) { |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 276 | PyErr_Format(PyExc_ImportError, |
| 277 | "Module use of %.150s conflicts " |
| 278 | "with this version of Python.", |
| 279 | import_python); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | FreeLibrary(hDLL); |
| 281 | return NULL; |
| 282 | } |
| 283 | } |
| 284 | p = GetProcAddress(hDLL, funcname); |
| 285 | } |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | return p; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 288 | } |