blob: 52629b788ec30deaa49c8995428a960e434a774b [file] [log] [blame]
Alexandre Julliardaca05781994-10-17 18:12:41 +00001/*
2 * Copyright 1994 Eric Youndale & Erik Bos
Alexandre Julliard0c126c71996-02-18 18:44:41 +00003 * Copyright 1995 Martin von Löwis
Alexandre Julliarda11d7b11998-03-01 20:05:02 +00004 * Copyright 1996-98 Marcus Meissner
Alexandre Julliardaca05781994-10-17 18:12:41 +00005 *
6 * based on Eric Youndale's pe-test and:
7 *
8 * ftp.microsoft.com:/pub/developer/MSDN/CD8/PEFILE.ZIP
Alexandre Julliardd4719651995-12-12 18:49:11 +00009 * make that:
10 * ftp.microsoft.com:/developr/MSDN/OctCD/PEFILE.ZIP
Alexandre Julliardaca05781994-10-17 18:12:41 +000011 */
Alexandre Julliarda11d7b11998-03-01 20:05:02 +000012/* 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 Medland65fc1c92001-08-24 21:13:02 +000020 * - In PE_MapImage, right before PE_fixup_imports() all external and internal
Alexandre Julliarda11d7b11998-03-01 20:05:02 +000021 * 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 Julliarda11d7b11998-03-01 20:05:02 +000024 */
Alexandre Julliardaca05781994-10-17 18:12:41 +000025
Patrik Stridvall6e740b52000-09-04 20:21:06 +000026#include "config.h"
27
Alexandre Julliard9c2370b2000-08-30 00:00:48 +000028#include <sys/types.h>
Howard Abrams13277481999-07-10 13:16:29 +000029#ifdef HAVE_SYS_MMAN_H
Alexandre Julliard77b99181997-09-14 17:17:23 +000030#include <sys/mman.h>
Howard Abrams13277481999-07-10 13:16:29 +000031#endif
Jon Griffiths4ab15582001-01-22 02:17:29 +000032#include <string.h>
Alexandre Julliard680919c2000-05-07 18:39:28 +000033#include "wine/winbase16.h"
Ulrich Weigandf6a93611999-02-28 11:19:10 +000034#include "winerror.h"
Alexandre Julliardf90efa91998-06-14 15:24:15 +000035#include "snoop.h"
Alexandre Julliard37e95032001-07-19 00:39:09 +000036#include "wine/server.h"
Alexandre Julliard06c275a1999-05-02 14:32:27 +000037#include "debugtools.h"
Alexandre Julliardaca05781994-10-17 18:12:41 +000038
Alexandre Julliard680919c2000-05-07 18:39:28 +000039DEFAULT_DEBUG_CHANNEL(win32);
40DECLARE_DEBUG_CHANNEL(delayhlp);
41DECLARE_DEBUG_CHANNEL(fixup);
42DECLARE_DEBUG_CHANNEL(module);
43DECLARE_DEBUG_CHANNEL(relay);
44DECLARE_DEBUG_CHANNEL(segment);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000045
Alexandre Julliarddf2673b1997-03-29 17:20:20 +000046
Alexandre Julliard081ee942000-08-07 04:12:41 +000047static 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
57static 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 Julliard01d63461997-01-20 19:43:45 +000068/* convert PE image VirtualAddress to Real Address */
Alexandre Julliardbbeaeeb1999-12-27 00:25:25 +000069#define RVA(x) ((void *)((char *)load_addr+(unsigned int)(x)))
Alexandre Julliardaca05781994-10-17 18:12:41 +000070
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000071#define AdjustPtr(ptr,delta) ((char *)(ptr) + (delta))
72
Alexandre Julliarda3960291999-02-26 11:11:13 +000073void dump_exports( HMODULE hModule )
Alexandre Julliardaca05781994-10-17 18:12:41 +000074{
Alexandre Julliard23946ad1997-06-16 17:43:53 +000075 char *Module;
Alexandre Julliard491502b1997-11-01 19:08:16 +000076 int i, j;
Hidenori Takeshima5b218072000-11-26 22:35:53 +000077 WORD *ordinal;
78 DWORD *function,*functions;
79 BYTE **name;
Alexandre Julliarde658d821997-11-30 17:45:40 +000080 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 Julliardaca05781994-10-17 18:12:41 +000087
Alexandre Julliard01d63461997-01-20 19:43:45 +000088 Module = (char*)RVA(pe_exports->Name);
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +000089 TRACE("*******EXPORT DATA*******\n");
90 TRACE("Module name is %s, %ld functions, %ld names\n",
91 Module, pe_exports->NumberOfFunctions, pe_exports->NumberOfNames);
Alexandre Julliardaca05781994-10-17 18:12:41 +000092
Hidenori Takeshima5b218072000-11-26 22:35:53 +000093 ordinal = RVA(pe_exports->AddressOfNameOrdinals);
94 functions = function = RVA(pe_exports->AddressOfFunctions);
95 name = RVA(pe_exports->AddressOfNames);
Alexandre Julliardaca05781994-10-17 18:12:41 +000096
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +000097 TRACE(" Ord RVA Addr Name\n" );
Alexandre Julliard491502b1997-11-01 19:08:16 +000098 for (i=0;i<pe_exports->NumberOfFunctions;i++, function++)
99 {
100 if (!*function) continue; /* No such function */
Alexandre Julliard15de6151999-08-04 12:22:42 +0000101 if (TRACE_ON(win32))
102 {
Alexandre Julliardbbeaeeb1999-12-27 00:25:25 +0000103 DPRINTF( "%4ld %08lx %p", i + pe_exports->Base, *function, RVA(*function) );
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000104 /* Check if we have a name for it */
105 for (j = 0; j < pe_exports->NumberOfNames; j++)
Alexandre Julliard491502b1997-11-01 19:08:16 +0000106 if (ordinal[j] == i)
Alexandre Julliard15de6151999-08-04 12:22:42 +0000107 {
108 DPRINTF( " %s", (char*)RVA(name[j]) );
109 break;
110 }
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000111 if ((*function >= rva_start) && (*function <= rva_end))
Alexandre Julliard15de6151999-08-04 12:22:42 +0000112 DPRINTF(" (forwarded -> %s)", (char *)RVA(*function));
113 DPRINTF("\n");
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000114 }
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000115 }
Alexandre Julliardaca05781994-10-17 18:12:41 +0000116}
117
Andreas Mohr9abd5532001-05-24 18:43:16 +0000118/* Look up the specified function or ordinal in the export list:
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000119 * If it is a string:
Andreas Mohr9abd5532001-05-24 18:43:16 +0000120 * - look up the name in the name list.
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000121 * - look up the ordinal with that index.
122 * - use the ordinal as offset into the functionlist
Andreas Mohr9abd5532001-05-24 18:43:16 +0000123 * If it is an ordinal:
124 * - use ordinal-pe_export->Base as offset into the function list
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000125 */
Alexandre Julliard081ee942000-08-07 04:12:41 +0000126static FARPROC PE_FindExportedFunction(
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000127 WINE_MODREF *wm, /* [in] WINE modreference */
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000128 LPCSTR funcName, /* [in] function name */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000129 BOOL snoop )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000130{
Hidenori Takeshima5b218072000-11-26 22:35:53 +0000131 WORD * ordinals;
132 DWORD * function;
133 BYTE ** name, *ename = NULL;
Alexandre Julliardbbeaeeb1999-12-27 00:25:25 +0000134 int i, ordinal;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000135 unsigned int load_addr = wm->module;
Hidenori Takeshima5b218072000-11-26 22:35:53 +0000136 DWORD rva_start, rva_end, addr;
Alexandre Julliarde658d821997-11-30 17:45:40 +0000137 char * forward;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000138 IMAGE_EXPORT_DIRECTORY *exports = get_exports(wm->module);
Alexandre Julliard77b99181997-09-14 17:17:23 +0000139
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000140 if (HIWORD(funcName))
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000141 TRACE("(%s)\n",funcName);
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000142 else
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000143 TRACE("(%d)\n",(int)funcName);
Alexandre Julliard77b99181997-09-14 17:17:23 +0000144 if (!exports) {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000145 /* Not a fatal problem, some apps do
146 * GetProcAddress(0,"RegisterPenApp") which triggers this
147 * case.
148 */
Alexandre Julliard081ee942000-08-07 04:12:41 +0000149 WARN("Module %08x(%s)/MODREF %p doesn't have a exports table.\n",wm->module,wm->modname,wm);
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000150 return NULL;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000151 }
Hidenori Takeshima5b218072000-11-26 22:35:53 +0000152 ordinals= RVA(exports->AddressOfNameOrdinals);
153 function= RVA(exports->AddressOfFunctions);
154 name = RVA(exports->AddressOfNames);
Alexandre Julliarde658d821997-11-30 17:45:40 +0000155 forward = NULL;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000156 rva_start = PE_HEADER(wm->module)->OptionalHeader
Alexandre Julliarde658d821997-11-30 17:45:40 +0000157 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000158 rva_end = rva_start + PE_HEADER(wm->module)->OptionalHeader
Alexandre Julliarde658d821997-11-30 17:45:40 +0000159 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000160
Alexandre Julliardbbeaeeb1999-12-27 00:25:25 +0000161 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 Julliarde658d821997-11-30 17:45:40 +0000189 }
Alexandre Julliardbbeaeeb1999-12-27 00:25:25 +0000190 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 Julliarde658d821997-11-30 17:45:40 +0000223 {
Andreas Mohr8c91b082000-12-05 21:11:35 +0000224 WINE_MODREF *wm_fw;
Juergen Schmied6d01aeb2000-06-01 23:23:00 +0000225 FARPROC proc;
Alexandre Julliardbbeaeeb1999-12-27 00:25:25 +0000226 char *forward = RVA(addr);
Alexandre Julliarde658d821997-11-30 17:45:40 +0000227 char module[256];
228 char *end = strchr(forward, '.');
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000229
Alexandre Julliarde658d821997-11-30 17:45:40 +0000230 if (!end) return NULL;
Alexandre Julliardbbeaeeb1999-12-27 00:25:25 +0000231 if (end - forward >= sizeof(module)) return NULL;
232 memcpy( module, forward, end - forward );
Alexandre Julliarde658d821997-11-30 17:45:40 +0000233 module[end-forward] = 0;
Andreas Mohr8c91b082000-12-05 21:11:35 +0000234 if (!(wm_fw = MODULE_FindModule( module )))
Alexandre Julliardb6ecb9a1999-05-12 10:15:41 +0000235 {
Andreas Mohr8c91b082000-12-05 21:11:35 +0000236 ERR("module not found for forward '%s' used by '%s'\n", forward, wm->modname );
Alexandre Julliardb6ecb9a1999-05-12 10:15:41 +0000237 return NULL;
238 }
Andreas Mohr8c91b082000-12-05 21:11:35 +0000239 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 Schmied6d01aeb2000-06-01 23:23:00 +0000241 return proc;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000242 }
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000243}
244
Bill Medland65fc1c92001-08-24 21:13:02 +0000245/****************************************************************
246 * PE_fixup_imports
247 */
248DWORD PE_fixup_imports( WINE_MODREF *wm )
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000249{
Alexandre Julliard77b99181997-09-14 17:17:23 +0000250 IMAGE_IMPORT_DESCRIPTOR *pe_imp;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000251 unsigned int load_addr = wm->module;
Marcus Meissnerf63f8bc1998-11-01 14:48:25 +0000252 int i,characteristics_detection=1;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000253 IMAGE_IMPORT_DESCRIPTOR *imports = get_imports(wm->module);
Alexandre Julliardaca05781994-10-17 18:12:41 +0000254
Alexandre Julliard081ee942000-08-07 04:12:41 +0000255 /* first, count the number of imported non-internal modules */
256 pe_imp = imports;
257 if (!pe_imp) return 0;
258
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000259 /* OK, now dump the import list */
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000260 TRACE("Dumping imports list\n");
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000261
Marcus Meissnerf63f8bc1998-11-01 14:48:25 +0000262 /* We assume that we have at least one import with !0 characteristics and
Juergen Schmied6d01aeb2000-06-01 23:23:00 +0000263 * detect broken imports with all characteristics 0 (notably Borland) and
Marcus Meissnerf63f8bc1998-11-01 14:48:25 +0000264 * 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 Julliard8bbf8181996-09-13 16:50:47 +0000271 i++;
Marcus Meissnerf63f8bc1998-11-01 14:48:25 +0000272 }
Alexandre Julliardb6ecb9a1999-05-12 10:15:41 +0000273 if (!i) return 0; /* no imports */
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000274
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000275 /* Allocate module dependency list */
276 wm->nDeps = i;
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000277 wm->deps = HeapAlloc( GetProcessHeap(), 0, i*sizeof(WINE_MODREF *) );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000278
Alexandre Julliard77b99181997-09-14 17:17:23 +0000279 /* load the imported modules. They are automatically
280 * added to the modref list of the process.
281 */
Alexandre Julliard01d63461997-01-20 19:43:45 +0000282
Alexandre Julliard081ee942000-08-07 04:12:41 +0000283 for (i = 0, pe_imp = imports; pe_imp->Name ; pe_imp++) {
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000284 WINE_MODREF *wmImp;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000285 IMAGE_IMPORT_BY_NAME *pe_name;
Uwe Bonnes73d6c131998-10-16 09:30:33 +0000286 PIMAGE_THUNK_DATA import_list,thunk_list;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000287 char *name = (char *) RVA(pe_imp->Name);
Alexandre Julliard77b99181997-09-14 17:17:23 +0000288
Marcus Meissnerf63f8bc1998-11-01 14:48:25 +0000289 if (characteristics_detection && !pe_imp->u.Characteristics)
290 break;
291
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000292 wmImp = MODULE_LoadLibraryExA( name, 0, 0 );
293 if (!wmImp) {
Andreas Mohrf3598952001-10-02 17:49:20 +0000294 ERR_(module)("Module (file) %s (which is needed by %s) not found\n", name, wm->filename);
Alexandre Julliard642d3131998-07-12 19:29:36 +0000295 return 1;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000296 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000297 wm->deps[i++] = wmImp;
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000298
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000299 /* FIXME: forwarder entries ... */
300
Alexandre Julliard01d63461997-01-20 19:43:45 +0000301 if (pe_imp->u.OriginalFirstThunk != 0) { /* original MS style */
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000302 TRACE("Microsoft style imports used\n");
Uwe Bonnes73d6c131998-10-16 09:30:33 +0000303 import_list =(PIMAGE_THUNK_DATA) RVA(pe_imp->u.OriginalFirstThunk);
304 thunk_list = (PIMAGE_THUNK_DATA) RVA(pe_imp->FirstThunk);
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000305
Alexandre Julliard01d63461997-01-20 19:43:45 +0000306 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. Paundd03cc11999-12-08 03:56:23 +0000310 TRACE("--- Ordinal %s,%d\n", name, ordinal);
François Gouget807b0452000-12-15 20:53:39 +0000311 thunk_list->u1.Function=(PDWORD)MODULE_GetProcAddress(
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000312 wmImp->module, (LPCSTR)ordinal, TRUE
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000313 );
Alexandre Julliard01d63461997-01-20 19:43:45 +0000314 if (!thunk_list->u1.Function) {
Alexandre Julliard3556e1f2001-01-20 02:51:19 +0000315 ERR("No implementation for %s.%d imported from %s, setting to 0xdeadbeef\n",
316 name, ordinal, wm->filename );
François Gouget807b0452000-12-15 20:53:39 +0000317 thunk_list->u1.Function = (PDWORD)0xdeadbeef;
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000318 }
319 } else { /* import by name */
Uwe Bonnes73d6c131998-10-16 09:30:33 +0000320 pe_name = (PIMAGE_IMPORT_BY_NAME)RVA(import_list->u1.AddressOfData);
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000321 TRACE("--- %s %s.%d\n", pe_name->Name, name, pe_name->Hint);
François Gouget807b0452000-12-15 20:53:39 +0000322 thunk_list->u1.Function=(PDWORD)MODULE_GetProcAddress(
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000323 wmImp->module, pe_name->Name, TRUE
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000324 );
Alexandre Julliard01d63461997-01-20 19:43:45 +0000325 if (!thunk_list->u1.Function) {
Alexandre Julliard3556e1f2001-01-20 02:51:19 +0000326 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 Gouget807b0452000-12-15 20:53:39 +0000328 thunk_list->u1.Function = (PDWORD)0xdeadbeef;
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000329 }
330 }
331 import_list++;
332 thunk_list++;
333 }
Alexandre Julliard01d63461997-01-20 19:43:45 +0000334 } else { /* Borland style */
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000335 TRACE("Borland style imports used\n");
Uwe Bonnes73d6c131998-10-16 09:30:33 +0000336 thunk_list = (PIMAGE_THUNK_DATA) RVA(pe_imp->FirstThunk);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000337 while (thunk_list->u1.Ordinal) {
338 if (IMAGE_SNAP_BY_ORDINAL(thunk_list->u1.Ordinal)) {
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000339 /* not sure about this branch, but it seems to work */
Alexandre Julliard01d63461997-01-20 19:43:45 +0000340 int ordinal = IMAGE_ORDINAL(thunk_list->u1.Ordinal);
341
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000342 TRACE("--- Ordinal %s.%d\n",name,ordinal);
François Gouget807b0452000-12-15 20:53:39 +0000343 thunk_list->u1.Function=(PDWORD)MODULE_GetProcAddress(
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000344 wmImp->module, (LPCSTR) ordinal, TRUE
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000345 );
Alexandre Julliard01d63461997-01-20 19:43:45 +0000346 if (!thunk_list->u1.Function) {
Alexandre Julliard3556e1f2001-01-20 02:51:19 +0000347 ERR("No implementation for %s.%d imported from %s, setting to 0xdeadbeef\n",
348 name,ordinal, wm->filename);
François Gouget807b0452000-12-15 20:53:39 +0000349 thunk_list->u1.Function = (PDWORD)0xdeadbeef;
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000350 }
351 } else {
Uwe Bonnes73d6c131998-10-16 09:30:33 +0000352 pe_name=(PIMAGE_IMPORT_BY_NAME) RVA(thunk_list->u1.AddressOfData);
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000353 TRACE("--- %s %s.%d\n",
Alexandre Julliard642d3131998-07-12 19:29:36 +0000354 pe_name->Name,name,pe_name->Hint);
François Gouget807b0452000-12-15 20:53:39 +0000355 thunk_list->u1.Function=(PDWORD)MODULE_GetProcAddress(
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000356 wmImp->module, pe_name->Name, TRUE
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000357 );
Alexandre Julliard01d63461997-01-20 19:43:45 +0000358 if (!thunk_list->u1.Function) {
Alexandre Julliard3556e1f2001-01-20 02:51:19 +0000359 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 Gouget807b0452000-12-15 20:53:39 +0000361 thunk_list->u1.Function = (PDWORD)0xdeadbeef;
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000362 }
363 }
364 thunk_list++;
365 }
366 }
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000367 }
Alexandre Julliard60ce85c1998-02-01 18:33:27 +0000368 return 0;
Alexandre Julliardaca05781994-10-17 18:12:41 +0000369}
370
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000371/***********************************************************************
372 * do_relocations
373 *
374 * Apply the relocations to a mapped PE image
375 */
376static int do_relocations( char *base, const IMAGE_NT_HEADERS *nt, const char *filename )
Alexandre Julliardaca05781994-10-17 18:12:41 +0000377{
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000378 const IMAGE_DATA_DIRECTORY *dir;
379 const IMAGE_BASE_RELOCATION *rel;
380 int delta = base - (char *)nt->OptionalHeader.ImageBase;
Alexandre Julliardaca05781994-10-17 18:12:41 +0000381
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000382 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 Julliardaca05781994-10-17 18:12:41 +0000387 {
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000388 if (nt->OptionalHeader.ImageBase == 0x400000)
Andreas Mohrf3598952001-10-02 17:49:20 +0000389 ERR("Standard load address for a Win32 program (0x00400000) not available - security-patched kernel ?\n");
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000390 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 Julliardaca05781994-10-17 18:12:41 +0000395 }
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000396
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 Julliarda257ba22000-08-16 12:46:09 +0000404 for ( ; ((char *)rel < base + dir->VirtualAddress + dir->Size) && rel->VirtualAddress;
405 rel = (IMAGE_BASE_RELOCATION*)((char*)rel + rel->SizeOfBlock))
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000406 {
407 char *page = base + rel->VirtualAddress;
Dmitry Timoshkov6b6596a2001-11-23 18:44:43 +0000408 WORD *TypeOffset = (WORD *)(rel + 1);
409 int i, count = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(*TypeOffset);
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000410
Alexandre Julliarda257ba22000-08-16 12:46:09 +0000411 if (!count) continue;
412
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000413 /* 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 Timoshkov6b6596a2001-11-23 18:44:43 +0000428 int offset = TypeOffset[i] & 0xFFF;
429 int type = TypeOffset[i] >> 12;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000430 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 Julliardc19e1a72000-08-14 20:20:01 +0000449 }
450 return 1;
Alexandre Julliardaca05781994-10-17 18:12:41 +0000451}
452
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000453
Alexandre Julliardaca05781994-10-17 18:12:41 +0000454/**********************************************************************
Alexandre Julliard234bc241994-12-10 13:02:28 +0000455 * PE_LoadImage
Alexandre Julliard77b99181997-09-14 17:17:23 +0000456 * Load one PE format DLL/EXE into memory
457 *
458 * Unluckily we can't just mmap the sections where we want them, for
Alexandre Julliard491502b1997-11-01 19:08:16 +0000459 * (at least) Linux does only support offsets which are page-aligned.
Alexandre Julliard77b99181997-09-14 17:17:23 +0000460 *
461 * BUT we have to map the whole image anyway, for Win32 programs sometimes
Andreas Mohrda920ee2001-08-09 21:16:55 +0000462 * want to access them. (HMODULE points to the start of it)
Alexandre Julliardaca05781994-10-17 18:12:41 +0000463 */
Alexandre Julliard081ee942000-08-07 04:12:41 +0000464HMODULE PE_LoadImage( HANDLE hFile, LPCSTR filename, DWORD flags )
Alexandre Julliardaca05781994-10-17 18:12:41 +0000465{
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000466 IMAGE_NT_HEADERS *nt;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000467 HMODULE hModule;
468 HANDLE mapping;
469 void *base;
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000470
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000471 TRACE_(module)( "loading %s\n", filename );
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000472
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000473 mapping = CreateFileMappingA( hFile, NULL, SEC_IMAGE, 0, 0, NULL );
Alexandre Julliarde0875082000-11-08 04:33:20 +0000474 if (!mapping) return 0;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000475 base = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
Alexandre Julliard491502b1997-11-01 19:08:16 +0000476 CloseHandle( mapping );
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000477 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 Bonnesc8c800c2000-01-23 02:27:16 +0000483
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000484 nt = PE_HEADER( hModule );
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000485 if (hModule != nt->OptionalHeader.ImageBase)
Alexandre Julliard491502b1997-11-01 19:08:16 +0000486 {
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000487 if (!do_relocations( base, nt, filename ))
Alexandre Julliarde658d821997-11-30 17:45:40 +0000488 {
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000489 UnmapViewOfFile( base );
Alexandre Julliardae719402000-05-03 18:43:11 +0000490 SetLastError( ERROR_BAD_EXE_FORMAT );
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000491 return 0;
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000492 }
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000493 }
494
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000495 /* virus check */
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000496
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000497 if (nt->OptionalHeader.AddressOfEntryPoint)
Alexandre Julliardf93eb3e2000-04-28 20:26:35 +0000498 {
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000499 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 MATE19186012000-08-16 12:54:03 +0000506 if (nt->OptionalHeader.AddressOfEntryPoint < sec->VirtualAddress+sec->SizeOfRawData)
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000507 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 Julliardf93eb3e2000-04-28 20:26:35 +0000513 }
514
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000515 return hModule;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000516}
517
518/**********************************************************************
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000519 * 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 Weigand1d90d691999-02-24 14:27:07 +0000527 *
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 Julliard081ee942000-08-07 04:12:41 +0000530 *
531 * Note: Assumes that the process critical section is held
Alexandre Julliard77b99181997-09-14 17:17:23 +0000532 */
Alexandre Julliard081ee942000-08-07 04:12:41 +0000533WINE_MODREF *PE_CreateModule( HMODULE hModule, LPCSTR filename, DWORD flags,
Alexandre Julliard8081e5a2001-01-05 04:08:07 +0000534 HANDLE hFile, BOOL builtin )
Alexandre Julliarde658d821997-11-30 17:45:40 +0000535{
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000536 DWORD load_addr = (DWORD)hModule; /* for RVA */
537 IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
538 IMAGE_DATA_DIRECTORY *dir;
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000539 IMAGE_EXPORT_DIRECTORY *pe_export = NULL;
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000540 WINE_MODREF *wm;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000541 HMODULE16 hModule16;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000542
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000543 /* Retrieve DataDirectory entries */
Alexandre Julliard77b99181997-09-14 17:17:23 +0000544
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000545 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
546 if (dir->Size)
547 pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(dir->VirtualAddress);
Alexandre Julliardb1bac321996-12-15 19:45:59 +0000548
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000549 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXCEPTION;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000550 if (dir->Size) FIXME("Exception directory ignored\n" );
Marcus Meissner96fc54b1998-12-08 10:06:59 +0000551
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000552 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_SECURITY;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000553 if (dir->Size) FIXME("Security directory ignored\n" );
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000554
555 /* IMAGE_DIRECTORY_ENTRY_BASERELOC handled in PE_LoadImage */
556 /* IMAGE_DIRECTORY_ENTRY_DEBUG handled by debugger */
557
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000558 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_GLOBALPTR;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000559 if (dir->Size) FIXME("Global Pointer (MIPS) ignored\n" );
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000560
561 /* IMAGE_DIRECTORY_ENTRY_TLS handled in PE_TlsInit */
562
563 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000564 if (dir->Size) FIXME("Load Configuration directory ignored\n" );
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000565
566 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000567 if (dir->Size) TRACE("Bound Import directory ignored\n" );
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000568
569 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IAT;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000570 if (dir->Size) TRACE("Import Address Table directory ignored\n" );
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000571
Theodore S. Hetke5d1d7771999-03-14 15:18:08 +0000572 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT;
573 if (dir->Size)
574 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000575 TRACE("Delayed import, stub calls LoadLibrary\n" );
Theodore S. Hetke5d1d7771999-03-14 15:18:08 +0000576 /*
577 * Nothing to do here.
578 */
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000579
Theodore S. Hetke5d1d7771999-03-14 15:18:08 +0000580#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 Julliard06c275a1999-05-02 14:32:27 +0000587 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. Hetke5d1d7771999-03-14 15:18:08 +0000595 }
596#endif /* ImgDelayDescr */
597 }
598
599 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000600 if (dir->Size) FIXME("Unknown directory 14 ignored\n" );
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000601
602 dir = nt->OptionalHeader.DataDirectory+15;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000603 if (dir->Size) FIXME("Unknown directory 15 ignored\n" );
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000604
Alexandre Julliard081ee942000-08-07 04:12:41 +0000605 /* 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 Weigandffa2c6f1998-12-18 15:38:15 +0000612
613 /* Allocate and fill WINE_MODREF */
614
Alexandre Julliard081ee942000-08-07 04:12:41 +0000615 if (!(wm = MODULE_AllocModRef( hModule, filename )))
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000616 {
Alexandre Julliard081ee942000-08-07 04:12:41 +0000617 FreeLibrary16( hModule16 );
618 return NULL;
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000619 }
Andreas Mohrcabee392000-10-25 21:22:27 +0000620 wm->hDummyMod = hModule16;
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000621
Alexandre Julliard081ee942000-08-07 04:12:41 +0000622 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 Julliard671da9f2001-03-21 03:38:44 +0000628 else if ( flags & DONT_RESOLVE_DLL_REFERENCES )
Alexandre Julliard081ee942000-08-07 04:12:41 +0000629 wm->flags |= WINE_MODREF_DONT_RESOLVE_REFS;
630
631 wm->find_export = PE_FindExportedFunction;
Ulrich Weigande469a581999-03-27 16:45:57 +0000632
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000633 /* Dump Exports */
634
635 if ( pe_export )
636 dump_exports( hModule );
637
638 /* Fixup Imports */
639
Bill Medland65fc1c92001-08-24 21:13:02 +0000640 if (!(wm->flags & WINE_MODREF_DONT_RESOLVE_REFS) &&
641 PE_fixup_imports( wm ))
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000642 {
643 /* remove entry from modref chain */
Ulrich Weigande469a581999-03-27 16:45:57 +0000644
645 if ( !wm->prev )
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000646 MODULE_modref_list = wm->next;
Ulrich Weigande469a581999-03-27 16:45:57 +0000647 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 Weigandffa2c6f1998-12-18 15:38:15 +0000653 /* 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 Julliarde658d821997-11-30 17:45:40 +0000659 */
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000660 return NULL;
661 }
Alexandre Julliard77b99181997-09-14 17:17:23 +0000662
Alexandre Julliardca4fa362001-07-12 02:46:55 +0000663 if (!builtin && pe_export)
Andreas Mohr9abd5532001-05-24 18:43:16 +0000664 SNOOP_RegisterDLL( hModule, wm->modname, pe_export->Base, pe_export->NumberOfFunctions );
Alexandre Julliard081ee942000-08-07 04:12:41 +0000665
666 /* Send DLL load event */
Peter Gantenb9f350b2000-08-07 17:10:20 +0000667 /* we don't need to send a dll event for the main exe */
Alexandre Julliard081ee942000-08-07 04:12:41 +0000668
669 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
670 {
Alexandre Julliardac2e4f12001-10-25 19:52:12 +0000671 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 Julliard67a74992001-02-27 02:09:16 +0000677 SERVER_START_REQ( load_dll )
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000678 {
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000679 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 Julliard67a74992001-02-27 02:09:16 +0000684 SERVER_CALL();
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000685 }
686 SERVER_END_REQ;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000687 }
Alexandre Julliard081ee942000-08-07 04:12:41 +0000688
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000689 return wm;
Alexandre Julliardaca05781994-10-17 18:12:41 +0000690}
691
Alexandre Julliard77b99181997-09-14 17:17:23 +0000692/******************************************************************************
693 * The PE Library Loader frontend.
694 * FIXME: handle the flags.
695 */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000696WINE_MODREF *PE_LoadLibraryExA (LPCSTR name, DWORD flags)
Alexandre Julliard60ce85c1998-02-01 18:33:27 +0000697{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000698 HMODULE hModule32;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000699 WINE_MODREF *wm;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000700 HANDLE hFile;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000701
Peter Gantenc7c42462000-08-28 21:33:28 +0000702 hFile = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
François Gougetda2b6a92001-01-06 01:29:18 +0000703 NULL, OPEN_EXISTING, 0, 0 );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000704 if ( hFile == INVALID_HANDLE_VALUE ) return NULL;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000705
706 /* Load PE module */
Peter Gantenc7c42462000-08-28 21:33:28 +0000707 hModule32 = PE_LoadImage( hFile, name, flags );
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000708 if (!hModule32)
709 {
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000710 CloseHandle( hFile );
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000711 return NULL;
712 }
Ulrich Weigandf6a93611999-02-28 11:19:10 +0000713
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000714 /* Create 32-bit MODREF */
Uwe Bonnes38012a52000-10-19 22:27:23 +0000715 if ( !(wm = PE_CreateModule( hModule32, name, flags, hFile, FALSE )) )
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000716 {
Peter Gantenc7c42462000-08-28 21:33:28 +0000717 ERR( "can't load %s\n", name );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000718 CloseHandle( hFile );
719 SetLastError( ERROR_OUTOFMEMORY );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000720 return NULL;
721 }
Ulrich Weigandf6a93611999-02-28 11:19:10 +0000722
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000723 CloseHandle( hFile );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000724 return wm;
725}
Ulrich Weigandf6a93611999-02-28 11:19:10 +0000726
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000727
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000728/* 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 Julliard081ee942000-08-07 04:12:41 +0000733typedef DWORD CALLBACK(*DLLENTRYPROC)(HMODULE,DWORD,LPVOID);
734
735BOOL PE_InitDLL( HMODULE module, DWORD type, LPVOID lpReserved )
Alexandre Julliardaca05781994-10-17 18:12:41 +0000736{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000737 BOOL retv = TRUE;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000738 IMAGE_NT_HEADERS *nt = PE_HEADER(module);
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000739
Alexandre Julliard01d63461997-01-20 19:43:45 +0000740 /* Is this a library? And has it got an entrypoint? */
Alexandre Julliard081ee942000-08-07 04:12:41 +0000741 if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) &&
742 (nt->OptionalHeader.AddressOfEntryPoint))
743 {
744 DLLENTRYPROC entry = (void*)((char*)module + nt->OptionalHeader.AddressOfEntryPoint);
Francois Gougete17d1a32001-05-08 00:13:38 +0000745 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 Julliard081ee942000-08-07 04:12:41 +0000748 retv = entry( module, type, lpReserved );
Francois Gougete17d1a32001-05-08 00:13:38 +0000749 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 Julliard8664b891996-04-05 14:58:24 +0000752 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000753
754 return retv;
Alexandre Julliardaca05781994-10-17 18:12:41 +0000755}
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000756
Marcus Meissner0a45ada1998-11-22 14:11:59 +0000757/************************************************************************
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 Meissner38eae841999-11-07 22:29:18 +0000764static 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 Weigande469a581999-03-27 16:45:57 +0000775void PE_InitTls( void )
Alexandre Julliard21979011997-03-05 08:22:35 +0000776{
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000777 WINE_MODREF *wm;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000778 IMAGE_NT_HEADERS *peh;
Alexandre Julliard60ce85c1998-02-01 18:33:27 +0000779 DWORD size,datasize;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000780 LPVOID mem;
Uwe Bonnes73d6c131998-10-16 09:30:33 +0000781 PIMAGE_TLS_DIRECTORY pdir;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000782 int delta;
783
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000784 for (wm = MODULE_modref_list;wm;wm=wm->next) {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000785 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 Julliard77b99181997-09-14 17:17:23 +0000790 DataDirectory[IMAGE_FILE_THREAD_LOCAL_STORAGE].VirtualAddress);
Alexandre Julliard60ce85c1998-02-01 18:33:27 +0000791
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000792
Alexandre Julliard081ee942000-08-07 04:12:41 +0000793 if ( wm->tlsindex == -1 ) {
Marcus Meissner38eae841999-11-07 22:29:18 +0000794 LPDWORD xaddr;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000795 wm->tlsindex = TlsAlloc();
Marcus Meissner38eae841999-11-07 22:29:18 +0000796 xaddr = _fixup_address(&(peh->OptionalHeader),delta,
797 pdir->AddressOfIndex
798 );
Alexandre Julliard081ee942000-08-07 04:12:41 +0000799 *xaddr=wm->tlsindex;
Alexandre Julliard60ce85c1998-02-01 18:33:27 +0000800 }
Alexandre Julliard77b99181997-09-14 17:17:23 +0000801 datasize= pdir->EndAddressOfRawData-pdir->StartAddressOfRawData;
802 size = datasize + pdir->SizeOfZeroFill;
803 mem=VirtualAlloc(0,size,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);
Marcus Meissner38eae841999-11-07 22:29:18 +0000804 memcpy(mem,_fixup_address(&(peh->OptionalHeader),delta,(LPVOID)pdir->StartAddressOfRawData),datasize);
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000805 if (pdir->AddressOfCallBacks) {
Marcus Meissner38eae841999-11-07 22:29:18 +0000806 PIMAGE_TLS_CALLBACK *cbs;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000807
Marcus Meissner38eae841999-11-07 22:29:18 +0000808 cbs = _fixup_address(&(peh->OptionalHeader),delta,pdir->AddressOfCallBacks);
Marcus Meissner0a45ada1998-11-22 14:11:59 +0000809 if (*cbs)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000810 FIXME("TLS Callbacks aren't going to be called\n");
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000811 }
Ulrich Weigande469a581999-03-27 16:45:57 +0000812
Alexandre Julliard081ee942000-08-07 04:12:41 +0000813 TlsSetValue( wm->tlsindex, mem );
Alexandre Julliard77b99181997-09-14 17:17:23 +0000814 }
815}
Alexandre Julliard21979011997-03-05 08:22:35 +0000816