Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1994 Eric Youndale & Erik Bos |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 3 | * Copyright 1995 Martin von Löwis |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 4 | * Copyright 1996-98 Marcus Meissner |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 5 | * |
| 6 | * based on Eric Youndale's pe-test and: |
| 7 | * |
| 8 | * ftp.microsoft.com:/pub/developer/MSDN/CD8/PEFILE.ZIP |
Alexandre Julliard | d471965 | 1995-12-12 18:49:11 +0000 | [diff] [blame] | 9 | * make that: |
| 10 | * ftp.microsoft.com:/developr/MSDN/OctCD/PEFILE.ZIP |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 11 | */ |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 12 | /* Notes: |
| 13 | * Before you start changing something in this file be aware of the following: |
| 14 | * |
| 15 | * - There are several functions called recursively. In a very subtle and |
| 16 | * obscure way. DLLs can reference each other recursively etc. |
| 17 | * - If you want to enhance, speed up or clean up something in here, think |
| 18 | * twice WHY it is implemented in that strange way. There is usually a reason. |
| 19 | * Though sometimes it might just be lazyness ;) |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 20 | * - In PE_MapImage, right before PE_fixup_imports() all external and internal |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 21 | * state MUST be correct since this function can be called with the SAME image |
| 22 | * AGAIN. (Thats recursion for you.) That means MODREF.module and |
| 23 | * NE_MODULE.module32. |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 24 | */ |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 25 | |
Patrik Stridvall | 6e740b5 | 2000-09-04 20:21:06 +0000 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 28 | #include <sys/types.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 29 | #ifdef HAVE_SYS_MMAN_H |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 30 | #include <sys/mman.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 31 | #endif |
Jon Griffiths | 4ab1558 | 2001-01-22 02:17:29 +0000 | [diff] [blame] | 32 | #include <string.h> |
Alexandre Julliard | 680919c | 2000-05-07 18:39:28 +0000 | [diff] [blame] | 33 | #include "wine/winbase16.h" |
Ulrich Weigand | f6a9361 | 1999-02-28 11:19:10 +0000 | [diff] [blame] | 34 | #include "winerror.h" |
Alexandre Julliard | f90efa9 | 1998-06-14 15:24:15 +0000 | [diff] [blame] | 35 | #include "snoop.h" |
Alexandre Julliard | 37e9503 | 2001-07-19 00:39:09 +0000 | [diff] [blame] | 36 | #include "wine/server.h" |
Alexandre Julliard | 06c275a | 1999-05-02 14:32:27 +0000 | [diff] [blame] | 37 | #include "debugtools.h" |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 38 | |
Alexandre Julliard | 680919c | 2000-05-07 18:39:28 +0000 | [diff] [blame] | 39 | DEFAULT_DEBUG_CHANNEL(win32); |
| 40 | DECLARE_DEBUG_CHANNEL(delayhlp); |
| 41 | DECLARE_DEBUG_CHANNEL(fixup); |
| 42 | DECLARE_DEBUG_CHANNEL(module); |
| 43 | DECLARE_DEBUG_CHANNEL(relay); |
| 44 | DECLARE_DEBUG_CHANNEL(segment); |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 45 | |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 46 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 47 | static IMAGE_EXPORT_DIRECTORY *get_exports( HMODULE hmod ) |
| 48 | { |
| 49 | IMAGE_EXPORT_DIRECTORY *ret = NULL; |
| 50 | IMAGE_DATA_DIRECTORY *dir = PE_HEADER(hmod)->OptionalHeader.DataDirectory |
| 51 | + IMAGE_DIRECTORY_ENTRY_EXPORT; |
| 52 | if (dir->Size && dir->VirtualAddress) |
| 53 | ret = (IMAGE_EXPORT_DIRECTORY *)((char *)hmod + dir->VirtualAddress); |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | static IMAGE_IMPORT_DESCRIPTOR *get_imports( HMODULE hmod ) |
| 58 | { |
| 59 | IMAGE_IMPORT_DESCRIPTOR *ret = NULL; |
| 60 | IMAGE_DATA_DIRECTORY *dir = PE_HEADER(hmod)->OptionalHeader.DataDirectory |
| 61 | + IMAGE_DIRECTORY_ENTRY_IMPORT; |
| 62 | if (dir->Size && dir->VirtualAddress) |
| 63 | ret = (IMAGE_IMPORT_DESCRIPTOR *)((char *)hmod + dir->VirtualAddress); |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 68 | /* convert PE image VirtualAddress to Real Address */ |
Alexandre Julliard | bbeaeeb | 1999-12-27 00:25:25 +0000 | [diff] [blame] | 69 | #define RVA(x) ((void *)((char *)load_addr+(unsigned int)(x))) |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 70 | |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 71 | #define AdjustPtr(ptr,delta) ((char *)(ptr) + (delta)) |
| 72 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 73 | void dump_exports( HMODULE hModule ) |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 74 | { |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 75 | char *Module; |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 76 | int i, j; |
Hidenori Takeshima | 5b21807 | 2000-11-26 22:35:53 +0000 | [diff] [blame] | 77 | WORD *ordinal; |
| 78 | DWORD *function,*functions; |
| 79 | BYTE **name; |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 80 | unsigned int load_addr = hModule; |
| 81 | |
| 82 | DWORD rva_start = PE_HEADER(hModule)->OptionalHeader |
| 83 | .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; |
| 84 | DWORD rva_end = rva_start + PE_HEADER(hModule)->OptionalHeader |
| 85 | .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size; |
| 86 | IMAGE_EXPORT_DIRECTORY *pe_exports = (IMAGE_EXPORT_DIRECTORY*)RVA(rva_start); |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 87 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 88 | Module = (char*)RVA(pe_exports->Name); |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 89 | TRACE("*******EXPORT DATA*******\n"); |
| 90 | TRACE("Module name is %s, %ld functions, %ld names\n", |
| 91 | Module, pe_exports->NumberOfFunctions, pe_exports->NumberOfNames); |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 92 | |
Hidenori Takeshima | 5b21807 | 2000-11-26 22:35:53 +0000 | [diff] [blame] | 93 | ordinal = RVA(pe_exports->AddressOfNameOrdinals); |
| 94 | functions = function = RVA(pe_exports->AddressOfFunctions); |
| 95 | name = RVA(pe_exports->AddressOfNames); |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 96 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 97 | TRACE(" Ord RVA Addr Name\n" ); |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 98 | for (i=0;i<pe_exports->NumberOfFunctions;i++, function++) |
| 99 | { |
| 100 | if (!*function) continue; /* No such function */ |
Alexandre Julliard | 15de615 | 1999-08-04 12:22:42 +0000 | [diff] [blame] | 101 | if (TRACE_ON(win32)) |
| 102 | { |
Alexandre Julliard | bbeaeeb | 1999-12-27 00:25:25 +0000 | [diff] [blame] | 103 | DPRINTF( "%4ld %08lx %p", i + pe_exports->Base, *function, RVA(*function) ); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 104 | /* Check if we have a name for it */ |
| 105 | for (j = 0; j < pe_exports->NumberOfNames; j++) |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 106 | if (ordinal[j] == i) |
Alexandre Julliard | 15de615 | 1999-08-04 12:22:42 +0000 | [diff] [blame] | 107 | { |
| 108 | DPRINTF( " %s", (char*)RVA(name[j]) ); |
| 109 | break; |
| 110 | } |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 111 | if ((*function >= rva_start) && (*function <= rva_end)) |
Alexandre Julliard | 15de615 | 1999-08-04 12:22:42 +0000 | [diff] [blame] | 112 | DPRINTF(" (forwarded -> %s)", (char *)RVA(*function)); |
| 113 | DPRINTF("\n"); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 114 | } |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 115 | } |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Andreas Mohr | 9abd553 | 2001-05-24 18:43:16 +0000 | [diff] [blame] | 118 | /* Look up the specified function or ordinal in the export list: |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 119 | * If it is a string: |
Andreas Mohr | 9abd553 | 2001-05-24 18:43:16 +0000 | [diff] [blame] | 120 | * - look up the name in the name list. |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 121 | * - look up the ordinal with that index. |
| 122 | * - use the ordinal as offset into the functionlist |
Andreas Mohr | 9abd553 | 2001-05-24 18:43:16 +0000 | [diff] [blame] | 123 | * If it is an ordinal: |
| 124 | * - use ordinal-pe_export->Base as offset into the function list |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 125 | */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 126 | static FARPROC PE_FindExportedFunction( |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 127 | WINE_MODREF *wm, /* [in] WINE modreference */ |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 128 | LPCSTR funcName, /* [in] function name */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 129 | BOOL snoop ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 130 | { |
Hidenori Takeshima | 5b21807 | 2000-11-26 22:35:53 +0000 | [diff] [blame] | 131 | WORD * ordinals; |
| 132 | DWORD * function; |
| 133 | BYTE ** name, *ename = NULL; |
Alexandre Julliard | bbeaeeb | 1999-12-27 00:25:25 +0000 | [diff] [blame] | 134 | int i, ordinal; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 135 | unsigned int load_addr = wm->module; |
Hidenori Takeshima | 5b21807 | 2000-11-26 22:35:53 +0000 | [diff] [blame] | 136 | DWORD rva_start, rva_end, addr; |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 137 | char * forward; |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 138 | IMAGE_EXPORT_DIRECTORY *exports = get_exports(wm->module); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 139 | |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 140 | if (HIWORD(funcName)) |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 141 | TRACE("(%s)\n",funcName); |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 142 | else |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 143 | TRACE("(%d)\n",(int)funcName); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 144 | if (!exports) { |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 145 | /* Not a fatal problem, some apps do |
| 146 | * GetProcAddress(0,"RegisterPenApp") which triggers this |
| 147 | * case. |
| 148 | */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 149 | WARN("Module %08x(%s)/MODREF %p doesn't have a exports table.\n",wm->module,wm->modname,wm); |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 150 | return NULL; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 151 | } |
Hidenori Takeshima | 5b21807 | 2000-11-26 22:35:53 +0000 | [diff] [blame] | 152 | ordinals= RVA(exports->AddressOfNameOrdinals); |
| 153 | function= RVA(exports->AddressOfFunctions); |
| 154 | name = RVA(exports->AddressOfNames); |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 155 | forward = NULL; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 156 | rva_start = PE_HEADER(wm->module)->OptionalHeader |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 157 | .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 158 | rva_end = rva_start + PE_HEADER(wm->module)->OptionalHeader |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 159 | .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 160 | |
Alexandre Julliard | bbeaeeb | 1999-12-27 00:25:25 +0000 | [diff] [blame] | 161 | if (HIWORD(funcName)) |
| 162 | { |
| 163 | /* first try a binary search */ |
| 164 | int min = 0, max = exports->NumberOfNames - 1; |
| 165 | while (min <= max) |
| 166 | { |
| 167 | int res, pos = (min + max) / 2; |
| 168 | ename = RVA(name[pos]); |
| 169 | if (!(res = strcmp( ename, funcName ))) |
| 170 | { |
| 171 | ordinal = ordinals[pos]; |
| 172 | goto found; |
| 173 | } |
| 174 | if (res > 0) max = pos - 1; |
| 175 | else min = pos + 1; |
| 176 | } |
| 177 | /* now try a linear search in case the names aren't sorted properly */ |
| 178 | for (i = 0; i < exports->NumberOfNames; i++) |
| 179 | { |
| 180 | ename = RVA(name[i]); |
| 181 | if (!strcmp( ename, funcName )) |
| 182 | { |
| 183 | ERR( "%s.%s required a linear search\n", wm->modname, funcName ); |
| 184 | ordinal = ordinals[i]; |
| 185 | goto found; |
| 186 | } |
| 187 | } |
| 188 | return NULL; |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 189 | } |
Alexandre Julliard | bbeaeeb | 1999-12-27 00:25:25 +0000 | [diff] [blame] | 190 | else /* find by ordinal */ |
| 191 | { |
| 192 | ordinal = LOWORD(funcName) - exports->Base; |
| 193 | if (snoop && name) /* need to find a name for it */ |
| 194 | { |
| 195 | for (i = 0; i < exports->NumberOfNames; i++) |
| 196 | if (ordinals[i] == ordinal) |
| 197 | { |
| 198 | ename = RVA(name[i]); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | found: |
| 205 | if (ordinal >= exports->NumberOfFunctions) |
| 206 | { |
| 207 | TRACE(" ordinal %ld out of range!\n", ordinal + exports->Base ); |
| 208 | return NULL; |
| 209 | } |
| 210 | addr = function[ordinal]; |
| 211 | if (!addr) return NULL; |
| 212 | if ((addr < rva_start) || (addr >= rva_end)) |
| 213 | { |
| 214 | FARPROC proc = RVA(addr); |
| 215 | if (snoop) |
| 216 | { |
| 217 | if (!ename) ename = "@"; |
| 218 | proc = SNOOP_GetProcAddress(wm->module,ename,ordinal,proc); |
| 219 | } |
| 220 | return proc; |
| 221 | } |
| 222 | else /* forward entry point */ |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 223 | { |
Andreas Mohr | 8c91b08 | 2000-12-05 21:11:35 +0000 | [diff] [blame] | 224 | WINE_MODREF *wm_fw; |
Juergen Schmied | 6d01aeb | 2000-06-01 23:23:00 +0000 | [diff] [blame] | 225 | FARPROC proc; |
Alexandre Julliard | bbeaeeb | 1999-12-27 00:25:25 +0000 | [diff] [blame] | 226 | char *forward = RVA(addr); |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 227 | char module[256]; |
| 228 | char *end = strchr(forward, '.'); |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 229 | |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 230 | if (!end) return NULL; |
Alexandre Julliard | bbeaeeb | 1999-12-27 00:25:25 +0000 | [diff] [blame] | 231 | if (end - forward >= sizeof(module)) return NULL; |
| 232 | memcpy( module, forward, end - forward ); |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 233 | module[end-forward] = 0; |
Andreas Mohr | 8c91b08 | 2000-12-05 21:11:35 +0000 | [diff] [blame] | 234 | if (!(wm_fw = MODULE_FindModule( module ))) |
Alexandre Julliard | b6ecb9a | 1999-05-12 10:15:41 +0000 | [diff] [blame] | 235 | { |
Andreas Mohr | 8c91b08 | 2000-12-05 21:11:35 +0000 | [diff] [blame] | 236 | ERR("module not found for forward '%s' used by '%s'\n", forward, wm->modname ); |
Alexandre Julliard | b6ecb9a | 1999-05-12 10:15:41 +0000 | [diff] [blame] | 237 | return NULL; |
| 238 | } |
Andreas Mohr | 8c91b08 | 2000-12-05 21:11:35 +0000 | [diff] [blame] | 239 | if (!(proc = MODULE_GetProcAddress( wm_fw->module, end + 1, snoop ))) |
| 240 | ERR("function not found for forward '%s' used by '%s'. If you are using builtin '%s', try using the native one instead.\n", forward, wm->modname, wm->modname ); |
Juergen Schmied | 6d01aeb | 2000-06-01 23:23:00 +0000 | [diff] [blame] | 241 | return proc; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 242 | } |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 245 | /**************************************************************** |
| 246 | * PE_fixup_imports |
| 247 | */ |
| 248 | DWORD PE_fixup_imports( WINE_MODREF *wm ) |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 249 | { |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 250 | IMAGE_IMPORT_DESCRIPTOR *pe_imp; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 251 | unsigned int load_addr = wm->module; |
Marcus Meissner | f63f8bc | 1998-11-01 14:48:25 +0000 | [diff] [blame] | 252 | int i,characteristics_detection=1; |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 253 | IMAGE_IMPORT_DESCRIPTOR *imports = get_imports(wm->module); |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 254 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 255 | /* first, count the number of imported non-internal modules */ |
| 256 | pe_imp = imports; |
| 257 | if (!pe_imp) return 0; |
| 258 | |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 259 | /* OK, now dump the import list */ |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 260 | TRACE("Dumping imports list\n"); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 261 | |
Marcus Meissner | f63f8bc | 1998-11-01 14:48:25 +0000 | [diff] [blame] | 262 | /* We assume that we have at least one import with !0 characteristics and |
Juergen Schmied | 6d01aeb | 2000-06-01 23:23:00 +0000 | [diff] [blame] | 263 | * detect broken imports with all characteristics 0 (notably Borland) and |
Marcus Meissner | f63f8bc | 1998-11-01 14:48:25 +0000 | [diff] [blame] | 264 | * switch the detection off for them. |
| 265 | */ |
| 266 | for (i = 0; pe_imp->Name ; pe_imp++) { |
| 267 | if (!i && !pe_imp->u.Characteristics) |
| 268 | characteristics_detection = 0; |
| 269 | if (characteristics_detection && !pe_imp->u.Characteristics) |
| 270 | break; |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 271 | i++; |
Marcus Meissner | f63f8bc | 1998-11-01 14:48:25 +0000 | [diff] [blame] | 272 | } |
Alexandre Julliard | b6ecb9a | 1999-05-12 10:15:41 +0000 | [diff] [blame] | 273 | if (!i) return 0; /* no imports */ |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 274 | |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 275 | /* Allocate module dependency list */ |
| 276 | wm->nDeps = i; |
Ulrich Weigand | 1d90d69 | 1999-02-24 14:27:07 +0000 | [diff] [blame] | 277 | wm->deps = HeapAlloc( GetProcessHeap(), 0, i*sizeof(WINE_MODREF *) ); |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 278 | |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 279 | /* load the imported modules. They are automatically |
| 280 | * added to the modref list of the process. |
| 281 | */ |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 282 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 283 | for (i = 0, pe_imp = imports; pe_imp->Name ; pe_imp++) { |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 284 | WINE_MODREF *wmImp; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 285 | IMAGE_IMPORT_BY_NAME *pe_name; |
Uwe Bonnes | 73d6c13 | 1998-10-16 09:30:33 +0000 | [diff] [blame] | 286 | PIMAGE_THUNK_DATA import_list,thunk_list; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 287 | char *name = (char *) RVA(pe_imp->Name); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 288 | |
Marcus Meissner | f63f8bc | 1998-11-01 14:48:25 +0000 | [diff] [blame] | 289 | if (characteristics_detection && !pe_imp->u.Characteristics) |
| 290 | break; |
| 291 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 292 | wmImp = MODULE_LoadLibraryExA( name, 0, 0 ); |
| 293 | if (!wmImp) { |
Andreas Mohr | f359895 | 2001-10-02 17:49:20 +0000 | [diff] [blame] | 294 | ERR_(module)("Module (file) %s (which is needed by %s) not found\n", name, wm->filename); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 295 | return 1; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 296 | } |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 297 | wm->deps[i++] = wmImp; |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 298 | |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 299 | /* FIXME: forwarder entries ... */ |
| 300 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 301 | if (pe_imp->u.OriginalFirstThunk != 0) { /* original MS style */ |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 302 | TRACE("Microsoft style imports used\n"); |
Uwe Bonnes | 73d6c13 | 1998-10-16 09:30:33 +0000 | [diff] [blame] | 303 | import_list =(PIMAGE_THUNK_DATA) RVA(pe_imp->u.OriginalFirstThunk); |
| 304 | thunk_list = (PIMAGE_THUNK_DATA) RVA(pe_imp->FirstThunk); |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 305 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 306 | while (import_list->u1.Ordinal) { |
| 307 | if (IMAGE_SNAP_BY_ORDINAL(import_list->u1.Ordinal)) { |
| 308 | int ordinal = IMAGE_ORDINAL(import_list->u1.Ordinal); |
| 309 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 310 | TRACE("--- Ordinal %s,%d\n", name, ordinal); |
François Gouget | 807b045 | 2000-12-15 20:53:39 +0000 | [diff] [blame] | 311 | thunk_list->u1.Function=(PDWORD)MODULE_GetProcAddress( |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 312 | wmImp->module, (LPCSTR)ordinal, TRUE |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 313 | ); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 314 | if (!thunk_list->u1.Function) { |
Alexandre Julliard | 3556e1f | 2001-01-20 02:51:19 +0000 | [diff] [blame] | 315 | ERR("No implementation for %s.%d imported from %s, setting to 0xdeadbeef\n", |
| 316 | name, ordinal, wm->filename ); |
François Gouget | 807b045 | 2000-12-15 20:53:39 +0000 | [diff] [blame] | 317 | thunk_list->u1.Function = (PDWORD)0xdeadbeef; |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 318 | } |
| 319 | } else { /* import by name */ |
Uwe Bonnes | 73d6c13 | 1998-10-16 09:30:33 +0000 | [diff] [blame] | 320 | pe_name = (PIMAGE_IMPORT_BY_NAME)RVA(import_list->u1.AddressOfData); |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 321 | TRACE("--- %s %s.%d\n", pe_name->Name, name, pe_name->Hint); |
François Gouget | 807b045 | 2000-12-15 20:53:39 +0000 | [diff] [blame] | 322 | thunk_list->u1.Function=(PDWORD)MODULE_GetProcAddress( |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 323 | wmImp->module, pe_name->Name, TRUE |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 324 | ); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 325 | if (!thunk_list->u1.Function) { |
Alexandre Julliard | 3556e1f | 2001-01-20 02:51:19 +0000 | [diff] [blame] | 326 | ERR("No implementation for %s.%d(%s) imported from %s, setting to 0xdeadbeef\n", |
| 327 | name,pe_name->Hint,pe_name->Name,wm->filename); |
François Gouget | 807b045 | 2000-12-15 20:53:39 +0000 | [diff] [blame] | 328 | thunk_list->u1.Function = (PDWORD)0xdeadbeef; |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | import_list++; |
| 332 | thunk_list++; |
| 333 | } |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 334 | } else { /* Borland style */ |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 335 | TRACE("Borland style imports used\n"); |
Uwe Bonnes | 73d6c13 | 1998-10-16 09:30:33 +0000 | [diff] [blame] | 336 | thunk_list = (PIMAGE_THUNK_DATA) RVA(pe_imp->FirstThunk); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 337 | while (thunk_list->u1.Ordinal) { |
| 338 | if (IMAGE_SNAP_BY_ORDINAL(thunk_list->u1.Ordinal)) { |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 339 | /* not sure about this branch, but it seems to work */ |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 340 | int ordinal = IMAGE_ORDINAL(thunk_list->u1.Ordinal); |
| 341 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 342 | TRACE("--- Ordinal %s.%d\n",name,ordinal); |
François Gouget | 807b045 | 2000-12-15 20:53:39 +0000 | [diff] [blame] | 343 | thunk_list->u1.Function=(PDWORD)MODULE_GetProcAddress( |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 344 | wmImp->module, (LPCSTR) ordinal, TRUE |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 345 | ); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 346 | if (!thunk_list->u1.Function) { |
Alexandre Julliard | 3556e1f | 2001-01-20 02:51:19 +0000 | [diff] [blame] | 347 | ERR("No implementation for %s.%d imported from %s, setting to 0xdeadbeef\n", |
| 348 | name,ordinal, wm->filename); |
François Gouget | 807b045 | 2000-12-15 20:53:39 +0000 | [diff] [blame] | 349 | thunk_list->u1.Function = (PDWORD)0xdeadbeef; |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 350 | } |
| 351 | } else { |
Uwe Bonnes | 73d6c13 | 1998-10-16 09:30:33 +0000 | [diff] [blame] | 352 | pe_name=(PIMAGE_IMPORT_BY_NAME) RVA(thunk_list->u1.AddressOfData); |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 353 | TRACE("--- %s %s.%d\n", |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 354 | pe_name->Name,name,pe_name->Hint); |
François Gouget | 807b045 | 2000-12-15 20:53:39 +0000 | [diff] [blame] | 355 | thunk_list->u1.Function=(PDWORD)MODULE_GetProcAddress( |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 356 | wmImp->module, pe_name->Name, TRUE |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 357 | ); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 358 | if (!thunk_list->u1.Function) { |
Alexandre Julliard | 3556e1f | 2001-01-20 02:51:19 +0000 | [diff] [blame] | 359 | ERR("No implementation for %s.%d(%s) imported from %s, setting to 0xdeadbeef\n", |
| 360 | name, pe_name->Hint, pe_name->Name, wm->filename); |
François Gouget | 807b045 | 2000-12-15 20:53:39 +0000 | [diff] [blame] | 361 | thunk_list->u1.Function = (PDWORD)0xdeadbeef; |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | thunk_list++; |
| 365 | } |
| 366 | } |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 367 | } |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 368 | return 0; |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 371 | /*********************************************************************** |
| 372 | * do_relocations |
| 373 | * |
| 374 | * Apply the relocations to a mapped PE image |
| 375 | */ |
| 376 | static int do_relocations( char *base, const IMAGE_NT_HEADERS *nt, const char *filename ) |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 377 | { |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 378 | const IMAGE_DATA_DIRECTORY *dir; |
| 379 | const IMAGE_BASE_RELOCATION *rel; |
| 380 | int delta = base - (char *)nt->OptionalHeader.ImageBase; |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 381 | |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 382 | dir = &nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; |
| 383 | rel = (IMAGE_BASE_RELOCATION *)(base + dir->VirtualAddress); |
| 384 | |
| 385 | WARN("Info: base relocations needed for %s\n", filename); |
| 386 | if (!dir->VirtualAddress || !dir->Size) |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 387 | { |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 388 | if (nt->OptionalHeader.ImageBase == 0x400000) |
Andreas Mohr | f359895 | 2001-10-02 17:49:20 +0000 | [diff] [blame] | 389 | ERR("Standard load address for a Win32 program (0x00400000) not available - security-patched kernel ?\n"); |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 390 | ERR( "FATAL: Need to relocate %s, but no relocation records present (%s). Try to run that file directly !\n", |
| 391 | filename, |
| 392 | (nt->FileHeader.Characteristics&IMAGE_FILE_RELOCS_STRIPPED)? |
| 393 | "stripped during link" : "unknown reason" ); |
| 394 | return 0; |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 395 | } |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 396 | |
| 397 | /* FIXME: If we need to relocate a system DLL (base > 2GB) we should |
| 398 | * really make sure that the *new* base address is also > 2GB. |
| 399 | * Some DLLs really check the MSB of the module handle :-/ |
| 400 | */ |
| 401 | if ((nt->OptionalHeader.ImageBase & 0x80000000) && !((DWORD)base & 0x80000000)) |
| 402 | ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" ); |
| 403 | |
Alexandre Julliard | a257ba2 | 2000-08-16 12:46:09 +0000 | [diff] [blame] | 404 | for ( ; ((char *)rel < base + dir->VirtualAddress + dir->Size) && rel->VirtualAddress; |
| 405 | rel = (IMAGE_BASE_RELOCATION*)((char*)rel + rel->SizeOfBlock)) |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 406 | { |
| 407 | char *page = base + rel->VirtualAddress; |
Dmitry Timoshkov | 6b6596a | 2001-11-23 18:44:43 +0000 | [diff] [blame] | 408 | WORD *TypeOffset = (WORD *)(rel + 1); |
| 409 | int i, count = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(*TypeOffset); |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 410 | |
Alexandre Julliard | a257ba2 | 2000-08-16 12:46:09 +0000 | [diff] [blame] | 411 | if (!count) continue; |
| 412 | |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 413 | /* sanity checks */ |
| 414 | if ((char *)rel + rel->SizeOfBlock > base + dir->VirtualAddress + dir->Size || |
| 415 | page > base + nt->OptionalHeader.SizeOfImage) |
| 416 | { |
| 417 | ERR_(module)("invalid relocation %p,%lx,%ld at %p,%lx,%lx\n", |
| 418 | rel, rel->VirtualAddress, rel->SizeOfBlock, |
| 419 | base, dir->VirtualAddress, dir->Size ); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | TRACE_(module)("%ld relocations for page %lx\n", rel->SizeOfBlock, rel->VirtualAddress); |
| 424 | |
| 425 | /* patching in reverse order */ |
| 426 | for (i = 0 ; i < count; i++) |
| 427 | { |
Dmitry Timoshkov | 6b6596a | 2001-11-23 18:44:43 +0000 | [diff] [blame] | 428 | int offset = TypeOffset[i] & 0xFFF; |
| 429 | int type = TypeOffset[i] >> 12; |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 430 | switch(type) |
| 431 | { |
| 432 | case IMAGE_REL_BASED_ABSOLUTE: |
| 433 | break; |
| 434 | case IMAGE_REL_BASED_HIGH: |
| 435 | *(short*)(page+offset) += HIWORD(delta); |
| 436 | break; |
| 437 | case IMAGE_REL_BASED_LOW: |
| 438 | *(short*)(page+offset) += LOWORD(delta); |
| 439 | break; |
| 440 | case IMAGE_REL_BASED_HIGHLOW: |
| 441 | *(int*)(page+offset) += delta; |
| 442 | /* FIXME: if this is an exported address, fire up enhanced logic */ |
| 443 | break; |
| 444 | default: |
| 445 | FIXME_(module)("Unknown/unsupported fixup type %d.\n", type); |
| 446 | break; |
| 447 | } |
| 448 | } |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 449 | } |
| 450 | return 1; |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 453 | |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 454 | /********************************************************************** |
Alexandre Julliard | 234bc24 | 1994-12-10 13:02:28 +0000 | [diff] [blame] | 455 | * PE_LoadImage |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 456 | * Load one PE format DLL/EXE into memory |
| 457 | * |
| 458 | * Unluckily we can't just mmap the sections where we want them, for |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 459 | * (at least) Linux does only support offsets which are page-aligned. |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 460 | * |
| 461 | * BUT we have to map the whole image anyway, for Win32 programs sometimes |
Andreas Mohr | da920ee | 2001-08-09 21:16:55 +0000 | [diff] [blame] | 462 | * want to access them. (HMODULE points to the start of it) |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 463 | */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 464 | HMODULE PE_LoadImage( HANDLE hFile, LPCSTR filename, DWORD flags ) |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 465 | { |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 466 | IMAGE_NT_HEADERS *nt; |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 467 | HMODULE hModule; |
| 468 | HANDLE mapping; |
| 469 | void *base; |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 470 | |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 471 | TRACE_(module)( "loading %s\n", filename ); |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 472 | |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 473 | mapping = CreateFileMappingA( hFile, NULL, SEC_IMAGE, 0, 0, NULL ); |
Alexandre Julliard | e087508 | 2000-11-08 04:33:20 +0000 | [diff] [blame] | 474 | if (!mapping) return 0; |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 475 | base = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 ); |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 476 | CloseHandle( mapping ); |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 477 | if (!base) return 0; |
| 478 | |
| 479 | hModule = (HMODULE)base; |
| 480 | if (flags & LOAD_LIBRARY_AS_DATAFILE) return hModule; /* nothing else to do */ |
| 481 | |
| 482 | /* perform base relocation, if necessary */ |
Uwe Bonnes | c8c800c | 2000-01-23 02:27:16 +0000 | [diff] [blame] | 483 | |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 484 | nt = PE_HEADER( hModule ); |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 485 | if (hModule != nt->OptionalHeader.ImageBase) |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 486 | { |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 487 | if (!do_relocations( base, nt, filename )) |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 488 | { |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 489 | UnmapViewOfFile( base ); |
Alexandre Julliard | ae71940 | 2000-05-03 18:43:11 +0000 | [diff] [blame] | 490 | SetLastError( ERROR_BAD_EXE_FORMAT ); |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 491 | return 0; |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 492 | } |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 495 | /* virus check */ |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 496 | |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 497 | if (nt->OptionalHeader.AddressOfEntryPoint) |
Alexandre Julliard | f93eb3e | 2000-04-28 20:26:35 +0000 | [diff] [blame] | 498 | { |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 499 | int i; |
| 500 | IMAGE_SECTION_HEADER *sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader + |
| 501 | nt->FileHeader.SizeOfOptionalHeader); |
| 502 | for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++) |
| 503 | { |
| 504 | if (nt->OptionalHeader.AddressOfEntryPoint < sec->VirtualAddress) |
| 505 | continue; |
Niclas Karlsson MATE | 1918601 | 2000-08-16 12:54:03 +0000 | [diff] [blame] | 506 | if (nt->OptionalHeader.AddressOfEntryPoint < sec->VirtualAddress+sec->SizeOfRawData) |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 507 | break; |
| 508 | } |
| 509 | if (i == nt->FileHeader.NumberOfSections) |
| 510 | MESSAGE("VIRUS WARNING: PE module has an invalid entrypoint (0x%08lx) " |
| 511 | "outside all sections (possibly infected by Tchernobyl/SpaceFiller virus)!\n", |
| 512 | nt->OptionalHeader.AddressOfEntryPoint ); |
Alexandre Julliard | f93eb3e | 2000-04-28 20:26:35 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Alexandre Julliard | c19e1a7 | 2000-08-14 20:20:01 +0000 | [diff] [blame] | 515 | return hModule; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /********************************************************************** |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 519 | * PE_CreateModule |
| 520 | * |
| 521 | * Create WINE_MODREF structure for loaded HMODULE32, link it into |
| 522 | * process modref_list, and fixup all imports. |
| 523 | * |
| 524 | * Note: hModule must point to a correctly allocated PE image, |
| 525 | * with base relocations applied; the 16-bit dummy module |
| 526 | * associated to hModule must already exist. |
Ulrich Weigand | 1d90d69 | 1999-02-24 14:27:07 +0000 | [diff] [blame] | 527 | * |
| 528 | * Note: This routine must always be called in the context of the |
| 529 | * process that is to own the module to be created. |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 530 | * |
| 531 | * Note: Assumes that the process critical section is held |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 532 | */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 533 | WINE_MODREF *PE_CreateModule( HMODULE hModule, LPCSTR filename, DWORD flags, |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 534 | HANDLE hFile, BOOL builtin ) |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 535 | { |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 536 | DWORD load_addr = (DWORD)hModule; /* for RVA */ |
| 537 | IMAGE_NT_HEADERS *nt = PE_HEADER(hModule); |
| 538 | IMAGE_DATA_DIRECTORY *dir; |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 539 | IMAGE_EXPORT_DIRECTORY *pe_export = NULL; |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 540 | WINE_MODREF *wm; |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 541 | HMODULE16 hModule16; |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 542 | |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 543 | /* Retrieve DataDirectory entries */ |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 544 | |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 545 | dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT; |
| 546 | if (dir->Size) |
| 547 | pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(dir->VirtualAddress); |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 548 | |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 549 | dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXCEPTION; |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 550 | if (dir->Size) FIXME("Exception directory ignored\n" ); |
Marcus Meissner | 96fc54b | 1998-12-08 10:06:59 +0000 | [diff] [blame] | 551 | |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 552 | dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_SECURITY; |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 553 | if (dir->Size) FIXME("Security directory ignored\n" ); |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 554 | |
| 555 | /* IMAGE_DIRECTORY_ENTRY_BASERELOC handled in PE_LoadImage */ |
| 556 | /* IMAGE_DIRECTORY_ENTRY_DEBUG handled by debugger */ |
| 557 | |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 558 | dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_GLOBALPTR; |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 559 | if (dir->Size) FIXME("Global Pointer (MIPS) ignored\n" ); |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 560 | |
| 561 | /* IMAGE_DIRECTORY_ENTRY_TLS handled in PE_TlsInit */ |
| 562 | |
| 563 | dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG; |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 564 | if (dir->Size) FIXME("Load Configuration directory ignored\n" ); |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 565 | |
| 566 | dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT; |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 567 | if (dir->Size) TRACE("Bound Import directory ignored\n" ); |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 568 | |
| 569 | dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IAT; |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 570 | if (dir->Size) TRACE("Import Address Table directory ignored\n" ); |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 571 | |
Theodore S. Hetke | 5d1d777 | 1999-03-14 15:18:08 +0000 | [diff] [blame] | 572 | dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT; |
| 573 | if (dir->Size) |
| 574 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 575 | TRACE("Delayed import, stub calls LoadLibrary\n" ); |
Theodore S. Hetke | 5d1d777 | 1999-03-14 15:18:08 +0000 | [diff] [blame] | 576 | /* |
| 577 | * Nothing to do here. |
| 578 | */ |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 579 | |
Theodore S. Hetke | 5d1d777 | 1999-03-14 15:18:08 +0000 | [diff] [blame] | 580 | #ifdef ImgDelayDescr |
| 581 | /* |
| 582 | * This code is useful to observe what the heck is going on. |
| 583 | */ |
| 584 | { |
| 585 | ImgDelayDescr *pe_delay = NULL; |
| 586 | pe_delay = (PImgDelayDescr)RVA(dir->VirtualAddress); |
Alexandre Julliard | 06c275a | 1999-05-02 14:32:27 +0000 | [diff] [blame] | 587 | TRACE_(delayhlp)("pe_delay->grAttrs = %08x\n", pe_delay->grAttrs); |
| 588 | TRACE_(delayhlp)("pe_delay->szName = %s\n", pe_delay->szName); |
| 589 | TRACE_(delayhlp)("pe_delay->phmod = %08x\n", pe_delay->phmod); |
| 590 | TRACE_(delayhlp)("pe_delay->pIAT = %08x\n", pe_delay->pIAT); |
| 591 | TRACE_(delayhlp)("pe_delay->pINT = %08x\n", pe_delay->pINT); |
| 592 | TRACE_(delayhlp)("pe_delay->pBoundIAT = %08x\n", pe_delay->pBoundIAT); |
| 593 | TRACE_(delayhlp)("pe_delay->pUnloadIAT = %08x\n", pe_delay->pUnloadIAT); |
| 594 | TRACE_(delayhlp)("pe_delay->dwTimeStamp = %08x\n", pe_delay->dwTimeStamp); |
Theodore S. Hetke | 5d1d777 | 1999-03-14 15:18:08 +0000 | [diff] [blame] | 595 | } |
| 596 | #endif /* ImgDelayDescr */ |
| 597 | } |
| 598 | |
| 599 | dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR; |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 600 | if (dir->Size) FIXME("Unknown directory 14 ignored\n" ); |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 601 | |
| 602 | dir = nt->OptionalHeader.DataDirectory+15; |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 603 | if (dir->Size) FIXME("Unknown directory 15 ignored\n" ); |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 604 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 605 | /* Create 16-bit dummy module */ |
| 606 | |
| 607 | if ((hModule16 = MODULE_CreateDummyModule( filename, hModule )) < 32) |
| 608 | { |
| 609 | SetLastError( (DWORD)hModule16 ); /* This should give the correct error */ |
| 610 | return NULL; |
| 611 | } |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 612 | |
| 613 | /* Allocate and fill WINE_MODREF */ |
| 614 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 615 | if (!(wm = MODULE_AllocModRef( hModule, filename ))) |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 616 | { |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 617 | FreeLibrary16( hModule16 ); |
| 618 | return NULL; |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 619 | } |
Andreas Mohr | cabee39 | 2000-10-25 21:22:27 +0000 | [diff] [blame] | 620 | wm->hDummyMod = hModule16; |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 621 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 622 | if ( builtin ) |
| 623 | { |
| 624 | NE_MODULE *pModule = (NE_MODULE *)GlobalLock16( hModule16 ); |
| 625 | pModule->flags |= NE_FFLAGS_BUILTIN; |
| 626 | wm->flags |= WINE_MODREF_INTERNAL; |
| 627 | } |
Alexandre Julliard | 671da9f | 2001-03-21 03:38:44 +0000 | [diff] [blame] | 628 | else if ( flags & DONT_RESOLVE_DLL_REFERENCES ) |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 629 | wm->flags |= WINE_MODREF_DONT_RESOLVE_REFS; |
| 630 | |
| 631 | wm->find_export = PE_FindExportedFunction; |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 632 | |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 633 | /* Dump Exports */ |
| 634 | |
| 635 | if ( pe_export ) |
| 636 | dump_exports( hModule ); |
| 637 | |
| 638 | /* Fixup Imports */ |
| 639 | |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 640 | if (!(wm->flags & WINE_MODREF_DONT_RESOLVE_REFS) && |
| 641 | PE_fixup_imports( wm )) |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 642 | { |
| 643 | /* remove entry from modref chain */ |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 644 | |
| 645 | if ( !wm->prev ) |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 646 | MODULE_modref_list = wm->next; |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 647 | else |
| 648 | wm->prev->next = wm->next; |
| 649 | |
| 650 | if ( wm->next ) wm->next->prev = wm->prev; |
| 651 | wm->next = wm->prev = NULL; |
| 652 | |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 653 | /* FIXME: there are several more dangling references |
| 654 | * left. Including dlls loaded by this dll before the |
| 655 | * failed one. Unrolling is rather difficult with the |
| 656 | * current structure and we can leave it them lying |
| 657 | * around with no problems, so we don't care. |
| 658 | * As these might reference our wm, we don't free it. |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 659 | */ |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 660 | return NULL; |
| 661 | } |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 662 | |
Alexandre Julliard | ca4fa36 | 2001-07-12 02:46:55 +0000 | [diff] [blame] | 663 | if (!builtin && pe_export) |
Andreas Mohr | 9abd553 | 2001-05-24 18:43:16 +0000 | [diff] [blame] | 664 | SNOOP_RegisterDLL( hModule, wm->modname, pe_export->Base, pe_export->NumberOfFunctions ); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 665 | |
| 666 | /* Send DLL load event */ |
Peter Ganten | b9f350b | 2000-08-07 17:10:20 +0000 | [diff] [blame] | 667 | /* we don't need to send a dll event for the main exe */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 668 | |
| 669 | if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL) |
| 670 | { |
Alexandre Julliard | ac2e4f1 | 2001-10-25 19:52:12 +0000 | [diff] [blame] | 671 | if (hFile) |
| 672 | { |
| 673 | UINT drive_type = GetDriveTypeA( wm->short_filename ); |
| 674 | /* don't keep the file handle open on removable media */ |
| 675 | if (drive_type == DRIVE_REMOVABLE || drive_type == DRIVE_CDROM) hFile = 0; |
| 676 | } |
Alexandre Julliard | 67a7499 | 2001-02-27 02:09:16 +0000 | [diff] [blame] | 677 | SERVER_START_REQ( load_dll ) |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 678 | { |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 679 | req->handle = hFile; |
| 680 | req->base = (void *)hModule; |
| 681 | req->dbg_offset = nt->FileHeader.PointerToSymbolTable; |
| 682 | req->dbg_size = nt->FileHeader.NumberOfSymbols; |
| 683 | req->name = &wm->filename; |
Alexandre Julliard | 67a7499 | 2001-02-27 02:09:16 +0000 | [diff] [blame] | 684 | SERVER_CALL(); |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 685 | } |
| 686 | SERVER_END_REQ; |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 687 | } |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 688 | |
Ulrich Weigand | ffa2c6f | 1998-12-18 15:38:15 +0000 | [diff] [blame] | 689 | return wm; |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 692 | /****************************************************************************** |
| 693 | * The PE Library Loader frontend. |
| 694 | * FIXME: handle the flags. |
| 695 | */ |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 696 | WINE_MODREF *PE_LoadLibraryExA (LPCSTR name, DWORD flags) |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 697 | { |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 698 | HMODULE hModule32; |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 699 | WINE_MODREF *wm; |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 700 | HANDLE hFile; |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 701 | |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 702 | hFile = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ, |
François Gouget | da2b6a9 | 2001-01-06 01:29:18 +0000 | [diff] [blame] | 703 | NULL, OPEN_EXISTING, 0, 0 ); |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 704 | if ( hFile == INVALID_HANDLE_VALUE ) return NULL; |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 705 | |
| 706 | /* Load PE module */ |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 707 | hModule32 = PE_LoadImage( hFile, name, flags ); |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 708 | if (!hModule32) |
| 709 | { |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 710 | CloseHandle( hFile ); |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 711 | return NULL; |
| 712 | } |
Ulrich Weigand | f6a9361 | 1999-02-28 11:19:10 +0000 | [diff] [blame] | 713 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 714 | /* Create 32-bit MODREF */ |
Uwe Bonnes | 38012a5 | 2000-10-19 22:27:23 +0000 | [diff] [blame] | 715 | if ( !(wm = PE_CreateModule( hModule32, name, flags, hFile, FALSE )) ) |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 716 | { |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 717 | ERR( "can't load %s\n", name ); |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 718 | CloseHandle( hFile ); |
| 719 | SetLastError( ERROR_OUTOFMEMORY ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 720 | return NULL; |
| 721 | } |
Ulrich Weigand | f6a9361 | 1999-02-28 11:19:10 +0000 | [diff] [blame] | 722 | |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 723 | CloseHandle( hFile ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 724 | return wm; |
| 725 | } |
Ulrich Weigand | f6a9361 | 1999-02-28 11:19:10 +0000 | [diff] [blame] | 726 | |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 727 | |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 728 | /* Called if the library is loaded or freed. |
| 729 | * NOTE: if a thread attaches a DLL, the current thread will only do |
| 730 | * DLL_PROCESS_ATTACH. Only new created threads do DLL_THREAD_ATTACH |
| 731 | * (SDK) |
| 732 | */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 733 | typedef DWORD CALLBACK(*DLLENTRYPROC)(HMODULE,DWORD,LPVOID); |
| 734 | |
| 735 | BOOL PE_InitDLL( HMODULE module, DWORD type, LPVOID lpReserved ) |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 736 | { |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 737 | BOOL retv = TRUE; |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 738 | IMAGE_NT_HEADERS *nt = PE_HEADER(module); |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 739 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 740 | /* Is this a library? And has it got an entrypoint? */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 741 | if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && |
| 742 | (nt->OptionalHeader.AddressOfEntryPoint)) |
| 743 | { |
| 744 | DLLENTRYPROC entry = (void*)((char*)module + nt->OptionalHeader.AddressOfEntryPoint); |
Francois Gouget | e17d1a3 | 2001-05-08 00:13:38 +0000 | [diff] [blame] | 745 | if (TRACE_ON(relay)) |
| 746 | DPRINTF("%08lx:Call PE DLL (proc=%p,module=%08x,type=%ld,res=%p)\n", |
| 747 | GetCurrentThreadId(), entry, module, type, lpReserved ); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 748 | retv = entry( module, type, lpReserved ); |
Francois Gouget | e17d1a3 | 2001-05-08 00:13:38 +0000 | [diff] [blame] | 749 | if (TRACE_ON(relay)) |
| 750 | DPRINTF("%08lx:Ret PE DLL (proc=%p,module=%08x,type=%ld,res=%p) retval=%x\n", |
| 751 | GetCurrentThreadId(), entry, module, type, lpReserved, retv ); |
Alexandre Julliard | 8664b89 | 1996-04-05 14:58:24 +0000 | [diff] [blame] | 752 | } |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 753 | |
| 754 | return retv; |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 755 | } |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 756 | |
Marcus Meissner | 0a45ada | 1998-11-22 14:11:59 +0000 | [diff] [blame] | 757 | /************************************************************************ |
| 758 | * PE_InitTls (internal) |
| 759 | * |
| 760 | * If included, initialises the thread local storages of modules. |
| 761 | * Pointers in those structs are not RVAs but real pointers which have been |
| 762 | * relocated by do_relocations() already. |
| 763 | */ |
Marcus Meissner | 38eae84 | 1999-11-07 22:29:18 +0000 | [diff] [blame] | 764 | static LPVOID |
| 765 | _fixup_address(PIMAGE_OPTIONAL_HEADER opt,int delta,LPVOID addr) { |
| 766 | if ( ((DWORD)addr>opt->ImageBase) && |
| 767 | ((DWORD)addr<opt->ImageBase+opt->SizeOfImage) |
| 768 | ) |
| 769 | /* the address has not been relocated! */ |
| 770 | return (LPVOID)(((DWORD)addr)+delta); |
| 771 | else |
| 772 | /* the address has been relocated already */ |
| 773 | return addr; |
| 774 | } |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 775 | void PE_InitTls( void ) |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 776 | { |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 777 | WINE_MODREF *wm; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 778 | IMAGE_NT_HEADERS *peh; |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 779 | DWORD size,datasize; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 780 | LPVOID mem; |
Uwe Bonnes | 73d6c13 | 1998-10-16 09:30:33 +0000 | [diff] [blame] | 781 | PIMAGE_TLS_DIRECTORY pdir; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 782 | int delta; |
| 783 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 784 | for (wm = MODULE_modref_list;wm;wm=wm->next) { |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 785 | peh = PE_HEADER(wm->module); |
| 786 | delta = wm->module - peh->OptionalHeader.ImageBase; |
| 787 | if (!peh->OptionalHeader.DataDirectory[IMAGE_FILE_THREAD_LOCAL_STORAGE].VirtualAddress) |
| 788 | continue; |
| 789 | pdir = (LPVOID)(wm->module + peh->OptionalHeader. |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 790 | DataDirectory[IMAGE_FILE_THREAD_LOCAL_STORAGE].VirtualAddress); |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 791 | |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 792 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 793 | if ( wm->tlsindex == -1 ) { |
Marcus Meissner | 38eae84 | 1999-11-07 22:29:18 +0000 | [diff] [blame] | 794 | LPDWORD xaddr; |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 795 | wm->tlsindex = TlsAlloc(); |
Marcus Meissner | 38eae84 | 1999-11-07 22:29:18 +0000 | [diff] [blame] | 796 | xaddr = _fixup_address(&(peh->OptionalHeader),delta, |
| 797 | pdir->AddressOfIndex |
| 798 | ); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 799 | *xaddr=wm->tlsindex; |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 800 | } |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 801 | datasize= pdir->EndAddressOfRawData-pdir->StartAddressOfRawData; |
| 802 | size = datasize + pdir->SizeOfZeroFill; |
| 803 | mem=VirtualAlloc(0,size,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE); |
Marcus Meissner | 38eae84 | 1999-11-07 22:29:18 +0000 | [diff] [blame] | 804 | memcpy(mem,_fixup_address(&(peh->OptionalHeader),delta,(LPVOID)pdir->StartAddressOfRawData),datasize); |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 805 | if (pdir->AddressOfCallBacks) { |
Marcus Meissner | 38eae84 | 1999-11-07 22:29:18 +0000 | [diff] [blame] | 806 | PIMAGE_TLS_CALLBACK *cbs; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 807 | |
Marcus Meissner | 38eae84 | 1999-11-07 22:29:18 +0000 | [diff] [blame] | 808 | cbs = _fixup_address(&(peh->OptionalHeader),delta,pdir->AddressOfCallBacks); |
Marcus Meissner | 0a45ada | 1998-11-22 14:11:59 +0000 | [diff] [blame] | 809 | if (*cbs) |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 810 | FIXME("TLS Callbacks aren't going to be called\n"); |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 811 | } |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 812 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 813 | TlsSetValue( wm->tlsindex, mem ); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 814 | } |
| 815 | } |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 816 | |