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