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