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 | |
| 14 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
| 15 | #ifdef _DEBUG |
| 16 | {"_d.pyd", "rb", C_EXTENSION}, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 17 | #else |
| 18 | {".pyd", "rb", C_EXTENSION}, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 19 | #endif |
| 20 | {0, 0} |
| 21 | }; |
| 22 | |
| 23 | |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 24 | /* Case insensitive string compare, to avoid any dependencies on particular |
| 25 | C RTL implementations */ |
| 26 | |
| 27 | static int strcasecmp (char *string1, char *string2) |
| 28 | { |
| 29 | int first, second; |
| 30 | |
| 31 | do { |
| 32 | first = tolower(*string1); |
| 33 | second = tolower(*string2); |
| 34 | string1++; |
| 35 | string2++; |
| 36 | } while (first && first == second); |
| 37 | |
| 38 | return (first - second); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /* Function to return the name of the "python" DLL that the supplied module |
| 43 | directly imports. Looks through the list of imported modules and |
| 44 | returns the first entry that starts with "python" (case sensitive) and |
| 45 | is followed by nothing but numbers until the separator (period). |
| 46 | |
| 47 | Returns a pointer to the import name, or NULL if no matching name was |
| 48 | located. |
| 49 | |
| 50 | This function parses through the PE header for the module as loaded in |
| 51 | memory by the system loader. The PE header is accessed as documented by |
| 52 | Microsoft in the MSDN PE and COFF specification (2/99), and handles |
| 53 | both PE32 and PE32+. It only worries about the direct import table and |
| 54 | not the delay load import table since it's unlikely an extension is |
| 55 | going to be delay loading Python (after all, it's already loaded). |
| 56 | |
| 57 | If any magic values are not found (e.g., the PE header or optional |
| 58 | header magic), then this function simply returns NULL. */ |
| 59 | |
| 60 | #define DWORD_AT(mem) (*(DWORD *)(mem)) |
| 61 | #define WORD_AT(mem) (*(WORD *)(mem)) |
| 62 | |
| 63 | static char *GetPythonImport (HINSTANCE hModule) |
| 64 | { |
| 65 | unsigned char *dllbase, *import_data, *import_name; |
| 66 | DWORD pe_offset, opt_offset; |
| 67 | WORD opt_magic; |
| 68 | int num_dict_off, import_off; |
| 69 | |
| 70 | /* Safety check input */ |
| 71 | if (hModule == NULL) { |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | /* Module instance is also the base load address. First portion of |
| 76 | memory is the MS-DOS loader, which holds the offset to the PE |
| 77 | header (from the load base) at 0x3C */ |
| 78 | dllbase = (unsigned char *)hModule; |
| 79 | pe_offset = DWORD_AT(dllbase + 0x3C); |
| 80 | |
| 81 | /* The PE signature must be "PE\0\0" */ |
| 82 | if (memcmp(dllbase+pe_offset,"PE\0\0",4)) { |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | /* Following the PE signature is the standard COFF header (20 |
| 87 | bytes) and then the optional header. The optional header starts |
| 88 | with a magic value of 0x10B for PE32 or 0x20B for PE32+ (PE32+ |
| 89 | uses 64-bits for some fields). It might also be 0x107 for a ROM |
| 90 | image, but we don't process that here. |
| 91 | |
| 92 | The optional header ends with a data dictionary that directly |
| 93 | points to certain types of data, among them the import entries |
| 94 | (in the second table entry). Based on the header type, we |
| 95 | determine offsets for the data dictionary count and the entry |
| 96 | within the dictionary pointing to the imports. */ |
| 97 | |
| 98 | opt_offset = pe_offset + 4 + 20; |
| 99 | opt_magic = WORD_AT(dllbase+opt_offset); |
| 100 | if (opt_magic == 0x10B) { |
| 101 | /* PE32 */ |
| 102 | num_dict_off = 92; |
| 103 | import_off = 104; |
| 104 | } else if (opt_magic == 0x20B) { |
| 105 | /* PE32+ */ |
| 106 | num_dict_off = 108; |
| 107 | import_off = 120; |
| 108 | } else { |
| 109 | /* Unsupported */ |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | /* Now if an import table exists, offset to it and walk the list of |
| 114 | imports. The import table is an array (ending when an entry has |
| 115 | empty values) of structures (20 bytes each), which contains (at |
| 116 | offset 12) a relative address (to the module base) at which a |
| 117 | string constant holding the import name is located. */ |
| 118 | |
| 119 | if (DWORD_AT(dllbase + opt_offset + num_dict_off) >= 2) { |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 120 | /* We have at least 2 tables - the import table is the second |
| 121 | one. But still it may be that the table size is zero */ |
| 122 | if (0 == DWORD_AT(dllbase + opt_offset + import_off + sizeof(DWORD))) |
| 123 | return NULL; |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 124 | import_data = dllbase + DWORD_AT(dllbase + |
| 125 | opt_offset + |
| 126 | import_off); |
| 127 | while (DWORD_AT(import_data)) { |
| 128 | import_name = dllbase + DWORD_AT(import_data+12); |
| 129 | if (strlen(import_name) >= 6 && |
| 130 | !strncmp(import_name,"python",6)) { |
| 131 | char *pch; |
| 132 | |
| 133 | /* Ensure python prefix is followed only |
| 134 | by numbers to the end of the basename */ |
| 135 | pch = import_name + 6; |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 136 | #ifdef _DEBUG |
| 137 | while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') { |
| 138 | #else |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 139 | while (*pch && *pch != '.') { |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 140 | #endif |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 141 | if (*pch >= '0' && *pch <= '9') { |
| 142 | pch++; |
| 143 | } else { |
| 144 | pch = NULL; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (pch) { |
| 150 | /* Found it - return the name */ |
| 151 | return import_name; |
| 152 | } |
| 153 | } |
| 154 | import_data += 20; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return NULL; |
| 159 | } |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 160 | |
| 161 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 162 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 163 | const char *pathname, FILE *fp) |
| 164 | { |
| 165 | dl_funcptr p; |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 166 | char funcname[258], *import_python; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 167 | |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 168 | PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 170 | { |
Mark Hammond | fb1f68e | 2001-05-09 00:50:59 +0000 | [diff] [blame] | 171 | HINSTANCE hDLL = NULL; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 172 | char pathbuf[260]; |
Mark Hammond | fb1f68e | 2001-05-09 00:50:59 +0000 | [diff] [blame] | 173 | LPTSTR dummy; |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 174 | unsigned int old_mode; |
Mark Hammond | fb1f68e | 2001-05-09 00:50:59 +0000 | [diff] [blame] | 175 | /* We use LoadLibraryEx so Windows looks for dependent DLLs |
| 176 | in directory of pathname first. However, Windows95 |
| 177 | can sometimes not work correctly unless the absolute |
| 178 | path is used. If GetFullPathName() fails, the LoadLibrary |
| 179 | will certainly fail too, so use its error code */ |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 180 | |
| 181 | /* Don't display a message box when Python can't load a DLL */ |
| 182 | old_mode = SetErrorMode(SEM_FAILCRITICALERRORS); |
| 183 | |
Mark Hammond | fb1f68e | 2001-05-09 00:50:59 +0000 | [diff] [blame] | 184 | if (GetFullPathName(pathname, |
| 185 | sizeof(pathbuf), |
| 186 | pathbuf, |
| 187 | &dummy)) |
| 188 | /* XXX This call doesn't exist in Windows CE */ |
| 189 | hDLL = LoadLibraryEx(pathname, NULL, |
| 190 | LOAD_WITH_ALTERED_SEARCH_PATH); |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 191 | |
| 192 | /* restore old error mode settings */ |
| 193 | SetErrorMode(old_mode); |
| 194 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 195 | if (hDLL==NULL){ |
Amaury Forgeot d'Arc | d6179e1 | 2008-01-03 23:42:13 +0000 | [diff] [blame] | 196 | PyObject *message; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 197 | unsigned int errorCode; |
| 198 | |
| 199 | /* Get an error string from Win32 error code */ |
Amaury Forgeot d'Arc | d6179e1 | 2008-01-03 23:42:13 +0000 | [diff] [blame] | 200 | wchar_t theInfo[256]; /* Pointer to error text |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 201 | from system */ |
| 202 | int theLength; /* Length of error text */ |
| 203 | |
| 204 | errorCode = GetLastError(); |
| 205 | |
Amaury Forgeot d'Arc | d6179e1 | 2008-01-03 23:42:13 +0000 | [diff] [blame] | 206 | theLength = FormatMessageW( |
| 207 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 208 | FORMAT_MESSAGE_IGNORE_INSERTS, /* flags */ |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 209 | NULL, /* message source */ |
| 210 | errorCode, /* the message (error) ID */ |
Amaury Forgeot d'Arc | d6179e1 | 2008-01-03 23:42:13 +0000 | [diff] [blame] | 211 | MAKELANGID(LANG_NEUTRAL, |
| 212 | SUBLANG_DEFAULT), |
| 213 | /* Default language */ |
| 214 | theInfo, /* the buffer */ |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 215 | sizeof(theInfo), /* the buffer size */ |
| 216 | NULL); /* no additional format args. */ |
| 217 | |
| 218 | /* Problem: could not get the error message. |
| 219 | This should not happen if called correctly. */ |
| 220 | if (theLength == 0) { |
Amaury Forgeot d'Arc | d6179e1 | 2008-01-03 23:42:13 +0000 | [diff] [blame] | 221 | message = PyUnicode_FromFormat( |
| 222 | "DLL load failed with error code %d", |
| 223 | errorCode); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 224 | } else { |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 225 | /* For some reason a \r\n |
| 226 | is appended to the text */ |
| 227 | if (theLength >= 2 && |
| 228 | theInfo[theLength-2] == '\r' && |
| 229 | theInfo[theLength-1] == '\n') { |
| 230 | theLength -= 2; |
| 231 | theInfo[theLength] = '\0'; |
| 232 | } |
Amaury Forgeot d'Arc | d6179e1 | 2008-01-03 23:42:13 +0000 | [diff] [blame] | 233 | message = PyUnicode_FromString( |
| 234 | "DLL load failed: "); |
| 235 | |
| 236 | PyUnicode_AppendAndDel(&message, |
| 237 | PyUnicode_FromUnicode( |
| 238 | theInfo, |
| 239 | theLength)); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 240 | } |
Amaury Forgeot d'Arc | d6179e1 | 2008-01-03 23:42:13 +0000 | [diff] [blame] | 241 | PyErr_SetObject(PyExc_ImportError, message); |
| 242 | Py_XDECREF(message); |
Fred Drake | e20131e | 2002-08-26 21:20:30 +0000 | [diff] [blame] | 243 | return NULL; |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 244 | } else { |
| 245 | char buffer[256]; |
| 246 | |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 247 | #ifdef _DEBUG |
| 248 | PyOS_snprintf(buffer, sizeof(buffer), "python%d%d_d.dll", |
| 249 | #else |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 250 | PyOS_snprintf(buffer, sizeof(buffer), "python%d%d.dll", |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 251 | #endif |
Fred Drake | e20131e | 2002-08-26 21:20:30 +0000 | [diff] [blame] | 252 | PY_MAJOR_VERSION,PY_MINOR_VERSION); |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 253 | import_python = GetPythonImport(hDLL); |
| 254 | |
| 255 | if (import_python && |
| 256 | strcasecmp(buffer,import_python)) { |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 257 | PyOS_snprintf(buffer, sizeof(buffer), |
| 258 | "Module use of %.150s conflicts " |
| 259 | "with this version of Python.", |
| 260 | import_python); |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 261 | PyErr_SetString(PyExc_ImportError,buffer); |
| 262 | FreeLibrary(hDLL); |
| 263 | return NULL; |
| 264 | } |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 265 | } |
| 266 | p = GetProcAddress(hDLL, funcname); |
| 267 | } |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 268 | |
| 269 | return p; |
| 270 | } |