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}, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 16 | #else |
| 17 | {".pyd", "rb", C_EXTENSION}, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 18 | #endif |
| 19 | {0, 0} |
| 20 | }; |
| 21 | |
| 22 | |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 23 | /* Case insensitive string compare, to avoid any dependencies on particular |
| 24 | C RTL implementations */ |
| 25 | |
| 26 | static 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 | |
| 62 | static 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 Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 119 | /* 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 Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 123 | 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 Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 135 | #ifdef _DEBUG |
| 136 | while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') { |
| 137 | #else |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 138 | while (*pch && *pch != '.') { |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 139 | #endif |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 140 | 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 Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 159 | |
| 160 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 161 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 162 | const char *pathname, FILE *fp) |
| 163 | { |
| 164 | dl_funcptr p; |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 165 | char funcname[258], *import_python; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 166 | |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 167 | PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 169 | { |
Mark Hammond | fb1f68e | 2001-05-09 00:50:59 +0000 | [diff] [blame] | 170 | HINSTANCE hDLL = NULL; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 171 | char pathbuf[260]; |
Mark Hammond | fb1f68e | 2001-05-09 00:50:59 +0000 | [diff] [blame] | 172 | 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 Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 185 | 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 Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 208 | PyOS_snprintf(errBuf, sizeof(errBuf), |
| 209 | "DLL load failed with error code %d", |
| 210 | errorCode); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 211 | } else { |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 212 | size_t len; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 213 | /* 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 Drake | e20131e | 2002-08-26 21:20:30 +0000 | [diff] [blame] | 228 | return NULL; |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 229 | } else { |
| 230 | char buffer[256]; |
| 231 | |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 232 | #ifdef _DEBUG |
| 233 | PyOS_snprintf(buffer, sizeof(buffer), "python%d%d_d.dll", |
| 234 | #else |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 235 | PyOS_snprintf(buffer, sizeof(buffer), "python%d%d.dll", |
Thomas Heller | 1df0461 | 2004-07-02 08:53:57 +0000 | [diff] [blame] | 236 | #endif |
Fred Drake | e20131e | 2002-08-26 21:20:30 +0000 | [diff] [blame] | 237 | PY_MAJOR_VERSION,PY_MINOR_VERSION); |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 238 | import_python = GetPythonImport(hDLL); |
| 239 | |
| 240 | if (import_python && |
| 241 | strcasecmp(buffer,import_python)) { |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 242 | PyOS_snprintf(buffer, sizeof(buffer), |
| 243 | "Module use of %.150s conflicts " |
| 244 | "with this version of Python.", |
| 245 | import_python); |
Mark Hammond | 1f7838b | 2000-10-05 10:54:45 +0000 | [diff] [blame] | 246 | PyErr_SetString(PyExc_ImportError,buffer); |
| 247 | FreeLibrary(hDLL); |
| 248 | return NULL; |
| 249 | } |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 250 | } |
| 251 | p = GetProcAddress(hDLL, funcname); |
| 252 | } |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 253 | |
| 254 | return p; |
| 255 | } |