1
|
1 /*
|
|
2 * Copyright 1994 Eric Youndale & Erik Bos
|
|
3 * Copyright 1995 Martin von Löwis
|
|
4 * Copyright 1996-98 Marcus Meissner
|
|
5 *
|
|
6 * based on Eric Youndale's pe-test and:
|
|
7 *
|
|
8 * ftp.microsoft.com:/pub/developer/MSDN/CD8/PEFILE.ZIP
|
|
9 * make that:
|
|
10 * ftp.microsoft.com:/developr/MSDN/OctCD/PEFILE.ZIP
|
|
11 */
|
|
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 ;)
|
|
20 * - In PE_MapImage, right before fixup_imports() all external and internal
|
|
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.
|
|
24 * - Sometimes, we can't use Linux mmap() to mmap() the images directly.
|
|
25 *
|
|
26 * The problem is, that there is not direct 1:1 mapping from a diskimage and
|
|
27 * a memoryimage. The headers at the start are mapped linear, but the sections
|
|
28 * are not. Older x86 pe binaries are 512 byte aligned in file and 4096 byte
|
|
29 * aligned in memory. Linux likes them 4096 byte aligned in memory (due to
|
|
30 * x86 pagesize, this cannot be fixed without a rather large kernel rewrite)
|
|
31 * and 'blocksize' file-aligned (offsets). Since we have 512/1024/2048 (CDROM)
|
|
32 * and other byte blocksizes, we can't always do this. We *can* do this for
|
|
33 * newer pe binaries produced by MSVC 5 and later, since they are also aligned
|
|
34 * to 4096 byte boundaries on disk.
|
|
35 */
|
|
36 #include <config.h>
|
|
37 #include <wine/config.h>
|
|
38
|
|
39 #include <errno.h>
|
|
40 #include <assert.h>
|
|
41 #include <stdlib.h>
|
|
42 #include <string.h>
|
|
43 #include <unistd.h>
|
|
44 #include <sys/types.h>
|
|
45 #include <sys/stat.h>
|
|
46 #include <fcntl.h>
|
|
47 #ifdef HAVE_SYS_MMAN_H
|
|
48 #include <sys/mman.h>
|
|
49 #endif
|
|
50 #include <wine/windef.h>
|
|
51 #include <wine/winbase.h>
|
|
52 #include <wine/winerror.h>
|
|
53 #include <wine/heap.h>
|
|
54 #include <wine/pe_image.h>
|
|
55 #include <wine/module.h>
|
|
56 #include <wine/debugtools.h>
|
|
57
|
|
58 #include "win32.h"
|
|
59
|
|
60 #define RVA(x) ((void *)((char *)load_addr+(unsigned int)(x)))
|
|
61
|
|
62 #define AdjustPtr(ptr,delta) ((char *)(ptr) + (delta))
|
|
63
|
|
64 extern void* LookupExternal(const char* library, int ordinal);
|
|
65 extern void* LookupExternalByName(const char* library, const char* name);
|
|
66
|
|
67 void dump_exports( HMODULE hModule )
|
|
68 {
|
|
69 char *Module;
|
|
70 int i, j;
|
|
71 u_short *ordinal;
|
|
72 u_long *function,*functions;
|
|
73 u_char **name;
|
|
74 unsigned int load_addr = hModule;
|
|
75
|
|
76 DWORD rva_start = PE_HEADER(hModule)->OptionalHeader
|
|
77 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
|
|
78 DWORD rva_end = rva_start + PE_HEADER(hModule)->OptionalHeader
|
|
79 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
|
|
80 IMAGE_EXPORT_DIRECTORY *pe_exports = (IMAGE_EXPORT_DIRECTORY*)RVA(rva_start);
|
|
81
|
|
82 Module = (char*)RVA(pe_exports->Name);
|
|
83 TRACE("*******EXPORT DATA*******\n");
|
|
84 TRACE("Module name is %s, %ld functions, %ld names\n",
|
|
85 Module, pe_exports->NumberOfFunctions, pe_exports->NumberOfNames);
|
|
86
|
|
87 ordinal=(u_short*) RVA(pe_exports->AddressOfNameOrdinals);
|
|
88 functions=function=(u_long*) RVA(pe_exports->AddressOfFunctions);
|
|
89 name=(u_char**) RVA(pe_exports->AddressOfNames);
|
|
90
|
|
91 TRACE(" Ord RVA Addr Name\n" );
|
|
92 for (i=0;i<pe_exports->NumberOfFunctions;i++, function++)
|
|
93 {
|
|
94 if (!*function) continue;
|
|
95 if (TRACE_ON(win32))
|
|
96 {
|
|
97 DPRINTF( "%4ld %08lx %p", i + pe_exports->Base, *function, RVA(*function) );
|
|
98
|
|
99 for (j = 0; j < pe_exports->NumberOfNames; j++)
|
|
100 if (ordinal[j] == i)
|
|
101 {
|
|
102 DPRINTF( " %s", (char*)RVA(name[j]) );
|
|
103 break;
|
|
104 }
|
|
105 if ((*function >= rva_start) && (*function <= rva_end))
|
|
106 DPRINTF(" (forwarded -> %s)", (char *)RVA(*function));
|
|
107 DPRINTF("\n");
|
|
108 }
|
|
109 }
|
|
110 }
|
|
111
|
|
112 /* Look up the specified function or ordinal in the exportlist:
|
|
113 * If it is a string:
|
|
114 * - look up the name in the Name list.
|
|
115 * - look up the ordinal with that index.
|
|
116 * - use the ordinal as offset into the functionlist
|
|
117 * If it is a ordinal:
|
|
118 * - use ordinal-pe_export->Base as offset into the functionlist
|
|
119 */
|
|
120 FARPROC PE_FindExportedFunction(
|
|
121 WINE_MODREF *wm,
|
|
122 LPCSTR funcName,
|
|
123 WIN_BOOL snoop )
|
|
124 {
|
|
125 u_short * ordinals;
|
|
126 u_long * function;
|
|
127 u_char ** name, *ename = NULL;
|
|
128 int i, ordinal;
|
|
129 PE_MODREF *pem = &(wm->binfmt.pe);
|
|
130 IMAGE_EXPORT_DIRECTORY *exports = pem->pe_export;
|
|
131 unsigned int load_addr = wm->module;
|
|
132 u_long rva_start, rva_end, addr;
|
|
133 char * forward;
|
|
134
|
|
135 if (HIWORD(funcName))
|
|
136 TRACE("(%s)\n",funcName);
|
|
137 else
|
|
138 TRACE("(%d)\n",(int)funcName);
|
|
139 if (!exports) {
|
|
140 /* Not a fatal problem, some apps do
|
|
141 * GetProcAddress(0,"RegisterPenApp") which triggers this
|
|
142 * case.
|
|
143 */
|
|
144 WARN("Module %08x(%s)/MODREF %p doesn't have a exports table.\n",wm->module,wm->modname,pem);
|
|
145 return NULL;
|
|
146 }
|
|
147 ordinals= (u_short*) RVA(exports->AddressOfNameOrdinals);
|
|
148 function= (u_long*) RVA(exports->AddressOfFunctions);
|
|
149 name = (u_char **) RVA(exports->AddressOfNames);
|
|
150 forward = NULL;
|
|
151 rva_start = PE_HEADER(wm->module)->OptionalHeader
|
|
152 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
|
|
153 rva_end = rva_start + PE_HEADER(wm->module)->OptionalHeader
|
|
154 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
|
|
155
|
|
156 if (HIWORD(funcName))
|
|
157 {
|
|
158
|
|
159 int min = 0, max = exports->NumberOfNames - 1;
|
|
160 while (min <= max)
|
|
161 {
|
|
162 int res, pos = (min + max) / 2;
|
|
163 ename = RVA(name[pos]);
|
|
164 if (!(res = strcmp( ename, funcName )))
|
|
165 {
|
|
166 ordinal = ordinals[pos];
|
|
167 goto found;
|
|
168 }
|
|
169 if (res > 0) max = pos - 1;
|
|
170 else min = pos + 1;
|
|
171 }
|
|
172
|
|
173 for (i = 0; i < exports->NumberOfNames; i++)
|
|
174 {
|
|
175 ename = RVA(name[i]);
|
|
176 if (!strcmp( ename, funcName ))
|
|
177 {
|
|
178 ERR( "%s.%s required a linear search\n", wm->modname, funcName );
|
|
179 ordinal = ordinals[i];
|
|
180 goto found;
|
|
181 }
|
|
182 }
|
|
183 return NULL;
|
|
184 }
|
|
185 else
|
|
186 {
|
|
187 ordinal = LOWORD(funcName) - exports->Base;
|
|
188 if (snoop && name)
|
|
189 {
|
|
190 for (i = 0; i < exports->NumberOfNames; i++)
|
|
191 if (ordinals[i] == ordinal)
|
|
192 {
|
|
193 ename = RVA(name[i]);
|
|
194 break;
|
|
195 }
|
|
196 }
|
|
197 }
|
|
198
|
|
199 found:
|
|
200 if (ordinal >= exports->NumberOfFunctions)
|
|
201 {
|
|
202 TRACE(" ordinal %ld out of range!\n", ordinal + exports->Base );
|
|
203 return NULL;
|
|
204 }
|
|
205 addr = function[ordinal];
|
|
206 if (!addr) return NULL;
|
|
207 if ((addr < rva_start) || (addr >= rva_end))
|
|
208 {
|
|
209 FARPROC proc = RVA(addr);
|
|
210 if (snoop)
|
|
211 {
|
|
212 if (!ename) ename = "@";
|
|
213 // proc = SNOOP_GetProcAddress(wm->module,ename,ordinal,proc);
|
|
214 TRACE("SNOOP_GetProcAddress n/a\n");
|
|
215
|
|
216 }
|
|
217 return proc;
|
|
218 }
|
|
219 else
|
|
220 {
|
|
221 WINE_MODREF *wm;
|
|
222 char *forward = RVA(addr);
|
|
223 char module[256];
|
|
224 char *end = strchr(forward, '.');
|
|
225
|
|
226 if (!end) return NULL;
|
|
227 if (end - forward >= sizeof(module)) return NULL;
|
|
228 memcpy( module, forward, end - forward );
|
|
229 module[end-forward] = 0;
|
|
230 if (!(wm = MODULE_FindModule( module )))
|
|
231 {
|
|
232 ERR("module not found for forward '%s'\n", forward );
|
|
233 return NULL;
|
|
234 }
|
|
235 return MODULE_GetProcAddress( wm->module, end + 1, snoop );
|
|
236 }
|
|
237 }
|
|
238
|
|
239 DWORD fixup_imports( WINE_MODREF *wm )
|
|
240 {
|
|
241 IMAGE_IMPORT_DESCRIPTOR *pe_imp;
|
|
242 PE_MODREF *pem;
|
|
243 unsigned int load_addr = wm->module;
|
|
244 int i,characteristics_detection=1;
|
|
245 char *modname;
|
|
246
|
|
247 assert(wm->type==MODULE32_PE);
|
|
248 pem = &(wm->binfmt.pe);
|
|
249 if (pem->pe_export)
|
|
250 modname = (char*) RVA(pem->pe_export->Name);
|
|
251 else
|
|
252 modname = "<unknown>";
|
|
253
|
|
254
|
|
255 TRACE("Dumping imports list\n");
|
|
256
|
|
257
|
|
258 pe_imp = pem->pe_import;
|
|
259 if (!pe_imp) return 0;
|
|
260
|
|
261 /* We assume that we have at least one import with !0 characteristics and
|
|
262 * detect broken imports with all characteristsics 0 (notably Borland) and
|
|
263 * switch the detection off for them.
|
|
264 */
|
|
265 for (i = 0; pe_imp->Name ; pe_imp++) {
|
|
266 if (!i && !pe_imp->u.Characteristics)
|
|
267 characteristics_detection = 0;
|
|
268 if (characteristics_detection && !pe_imp->u.Characteristics)
|
|
269 break;
|
|
270 i++;
|
|
271 }
|
|
272 if (!i) return 0;
|
|
273
|
|
274
|
|
275 wm->nDeps = i;
|
|
276 wm->deps = HeapAlloc( GetProcessHeap(), 0, i*sizeof(WINE_MODREF *) );
|
|
277
|
|
278 /* load the imported modules. They are automatically
|
|
279 * added to the modref list of the process.
|
|
280 */
|
|
281
|
|
282 for (i = 0, pe_imp = pem->pe_import; pe_imp->Name ; pe_imp++) {
|
|
283 WINE_MODREF *wmImp;
|
|
284 IMAGE_IMPORT_BY_NAME *pe_name;
|
|
285 PIMAGE_THUNK_DATA import_list,thunk_list;
|
|
286 char *name = (char *) RVA(pe_imp->Name);
|
|
287
|
|
288 if (characteristics_detection && !pe_imp->u.Characteristics)
|
|
289 break;
|
|
290
|
|
291 //#warning FIXME: here we should fill imports
|
|
292 TRACE("Loading imports for %s.dll\n", name);
|
|
293
|
|
294 if (pe_imp->u.OriginalFirstThunk != 0) {
|
|
295 TRACE("Microsoft style imports used\n");
|
|
296 import_list =(PIMAGE_THUNK_DATA) RVA(pe_imp->u.OriginalFirstThunk);
|
|
297 thunk_list = (PIMAGE_THUNK_DATA) RVA(pe_imp->FirstThunk);
|
|
298
|
|
299 while (import_list->u1.Ordinal) {
|
|
300 if (IMAGE_SNAP_BY_ORDINAL(import_list->u1.Ordinal)) {
|
|
301 int ordinal = IMAGE_ORDINAL(import_list->u1.Ordinal);
|
|
302
|
|
303 // TRACE("--- Ordinal %s,%d\n", name, ordinal);
|
|
304
|
|
305 thunk_list->u1.Function=LookupExternal(
|
|
306 name, ordinal);
|
|
307 } else {
|
|
308 pe_name = (PIMAGE_IMPORT_BY_NAME)RVA(import_list->u1.AddressOfData);
|
|
309 // TRACE("--- %s %s.%d\n", pe_name->Name, name, pe_name->Hint);
|
|
310 thunk_list->u1.Function=LookupExternalByName(
|
|
311 name, pe_name->Name);
|
|
312 }
|
|
313 import_list++;
|
|
314 thunk_list++;
|
|
315 }
|
|
316 } else {
|
|
317 TRACE("Borland style imports used\n");
|
|
318 thunk_list = (PIMAGE_THUNK_DATA) RVA(pe_imp->FirstThunk);
|
|
319 while (thunk_list->u1.Ordinal) {
|
|
320 if (IMAGE_SNAP_BY_ORDINAL(thunk_list->u1.Ordinal)) {
|
|
321
|
|
322 int ordinal = IMAGE_ORDINAL(thunk_list->u1.Ordinal);
|
|
323
|
|
324 TRACE("--- Ordinal %s.%d\n",name,ordinal);
|
|
325 thunk_list->u1.Function=LookupExternal(
|
|
326 name, ordinal);
|
|
327 } else {
|
|
328 pe_name=(PIMAGE_IMPORT_BY_NAME) RVA(thunk_list->u1.AddressOfData);
|
|
329 TRACE("--- %s %s.%d\n",
|
|
330 pe_name->Name,name,pe_name->Hint);
|
|
331 thunk_list->u1.Function=LookupExternalByName(
|
|
332 name, pe_name->Name);
|
|
333 }
|
|
334 thunk_list++;
|
|
335 }
|
|
336 }
|
|
337
|
|
338
|
|
339 }
|
|
340 return 0;
|
|
341 }
|
|
342
|
|
343 static int calc_vma_size( HMODULE hModule )
|
|
344 {
|
|
345 int i,vma_size = 0;
|
|
346 IMAGE_SECTION_HEADER *pe_seg = PE_SECTIONS(hModule);
|
|
347
|
|
348 TRACE("Dump of segment table\n");
|
|
349 TRACE(" Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
|
|
350 for (i = 0; i< PE_HEADER(hModule)->FileHeader.NumberOfSections; i++)
|
|
351 {
|
|
352 TRACE("%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
|
|
353 pe_seg->Name,
|
|
354 pe_seg->Misc.VirtualSize,
|
|
355 pe_seg->VirtualAddress,
|
|
356 pe_seg->SizeOfRawData,
|
|
357 pe_seg->PointerToRawData,
|
|
358 pe_seg->PointerToRelocations,
|
|
359 pe_seg->PointerToLinenumbers,
|
|
360 pe_seg->NumberOfRelocations,
|
|
361 pe_seg->NumberOfLinenumbers,
|
|
362 pe_seg->Characteristics);
|
|
363 vma_size=max(vma_size, pe_seg->VirtualAddress+pe_seg->SizeOfRawData);
|
|
364 vma_size=max(vma_size, pe_seg->VirtualAddress+pe_seg->Misc.VirtualSize);
|
|
365 pe_seg++;
|
|
366 }
|
|
367 return vma_size;
|
|
368 }
|
|
369
|
|
370 static void do_relocations( unsigned int load_addr, IMAGE_BASE_RELOCATION *r )
|
|
371 {
|
|
372 int delta = load_addr - PE_HEADER(load_addr)->OptionalHeader.ImageBase;
|
|
373 int hdelta = (delta >> 16) & 0xFFFF;
|
|
374 int ldelta = delta & 0xFFFF;
|
|
375
|
|
376 if(delta == 0)
|
|
377
|
|
378 return;
|
|
379 while(r->VirtualAddress)
|
|
380 {
|
|
381 char *page = (char*) RVA(r->VirtualAddress);
|
|
382 int count = (r->SizeOfBlock - 8)/2;
|
|
383 int i;
|
|
384 TRACE_(fixup)("%x relocations for page %lx\n",
|
|
385 count, r->VirtualAddress);
|
|
386
|
|
387 for(i=0;i<count;i++)
|
|
388 {
|
|
389 int offset = r->TypeOffset[i] & 0xFFF;
|
|
390 int type = r->TypeOffset[i] >> 12;
|
|
391 // TRACE_(fixup)("patching %x type %x\n", offset, type);
|
|
392 switch(type)
|
|
393 {
|
|
394 case IMAGE_REL_BASED_ABSOLUTE: break;
|
|
395 case IMAGE_REL_BASED_HIGH:
|
|
396 *(short*)(page+offset) += hdelta;
|
|
397 break;
|
|
398 case IMAGE_REL_BASED_LOW:
|
|
399 *(short*)(page+offset) += ldelta;
|
|
400 break;
|
|
401 case IMAGE_REL_BASED_HIGHLOW:
|
|
402 *(int*)(page+offset) += delta;
|
|
403
|
|
404 break;
|
|
405 case IMAGE_REL_BASED_HIGHADJ:
|
|
406 FIXME("Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
|
|
407 break;
|
|
408 case IMAGE_REL_BASED_MIPS_JMPADDR:
|
|
409 FIXME("Is this a MIPS machine ???\n");
|
|
410 break;
|
|
411 default:
|
|
412 FIXME("Unknown fixup type\n");
|
|
413 break;
|
|
414 }
|
|
415 }
|
|
416 r = (IMAGE_BASE_RELOCATION*)((char*)r + r->SizeOfBlock);
|
|
417 }
|
|
418 }
|
|
419
|
|
420
|
|
421
|
|
422
|
|
423
|
|
424 /**********************************************************************
|
|
425 * PE_LoadImage
|
|
426 * Load one PE format DLL/EXE into memory
|
|
427 *
|
|
428 * Unluckily we can't just mmap the sections where we want them, for
|
|
429 * (at least) Linux does only support offsets which are page-aligned.
|
|
430 *
|
|
431 * BUT we have to map the whole image anyway, for Win32 programs sometimes
|
|
432 * want to access them. (HMODULE32 point to the start of it)
|
|
433 */
|
|
434 HMODULE PE_LoadImage( int handle, LPCSTR filename, WORD *version )
|
|
435 {
|
|
436 HMODULE hModule;
|
|
437 HANDLE mapping;
|
|
438
|
|
439 IMAGE_NT_HEADERS *nt;
|
|
440 IMAGE_SECTION_HEADER *pe_sec;
|
|
441 IMAGE_DATA_DIRECTORY *dir;
|
|
442 BY_HANDLE_FILE_INFORMATION bhfi;
|
|
443 int i, rawsize, lowest_va, vma_size, file_size = 0;
|
|
444 DWORD load_addr = 0, aoep, reloc = 0;
|
|
445 // struct get_read_fd_request *req = get_req_buffer();
|
|
446 int unix_handle = handle;
|
|
447 int page_size = getpagesize();
|
|
448
|
|
449
|
|
450 // if ( GetFileInformationByHandle( hFile, &bhfi ) )
|
|
451 // file_size = bhfi.nFileSizeLow;
|
|
452 file_size=lseek(handle, 0, SEEK_END);
|
|
453 lseek(handle, 0, SEEK_SET);
|
|
454
|
|
455 //#warning fix CreateFileMappingA
|
|
456 mapping = CreateFileMappingA( handle, NULL, PAGE_READONLY | SEC_COMMIT,
|
|
457 0, 0, NULL );
|
|
458 if (!mapping)
|
|
459 {
|
|
460 WARN("CreateFileMapping error %ld\n", GetLastError() );
|
|
461 return 0;
|
|
462 }
|
|
463 // hModule = (HMODULE)MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
|
|
464 hModule=(HMODULE)mapping;
|
|
465 // CloseHandle( mapping );
|
|
466 if (!hModule)
|
|
467 {
|
|
468 WARN("MapViewOfFile error %ld\n", GetLastError() );
|
|
469 return 0;
|
|
470 }
|
|
471 if ( *(WORD*)hModule !=IMAGE_DOS_SIGNATURE)
|
|
472 {
|
|
473 WARN("%s image doesn't have DOS signature, but 0x%04x\n", filename,*(WORD*)hModule);
|
|
474 goto error;
|
|
475 }
|
|
476
|
|
477 nt = PE_HEADER( hModule );
|
|
478
|
|
479
|
|
480 if ( nt->Signature != IMAGE_NT_SIGNATURE )
|
|
481 {
|
|
482 WARN("%s image doesn't have PE signature, but 0x%08lx\n", filename, nt->Signature );
|
|
483 goto error;
|
|
484 }
|
|
485
|
|
486
|
|
487 if ( nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386 )
|
|
488 {
|
|
489 MESSAGE("Trying to load PE image for unsupported architecture (");
|
|
490 switch (nt->FileHeader.Machine)
|
|
491 {
|
|
492 case IMAGE_FILE_MACHINE_UNKNOWN: MESSAGE("Unknown"); break;
|
|
493 case IMAGE_FILE_MACHINE_I860: MESSAGE("I860"); break;
|
|
494 case IMAGE_FILE_MACHINE_R3000: MESSAGE("R3000"); break;
|
|
495 case IMAGE_FILE_MACHINE_R4000: MESSAGE("R4000"); break;
|
|
496 case IMAGE_FILE_MACHINE_R10000: MESSAGE("R10000"); break;
|
|
497 case IMAGE_FILE_MACHINE_ALPHA: MESSAGE("Alpha"); break;
|
|
498 case IMAGE_FILE_MACHINE_POWERPC: MESSAGE("PowerPC"); break;
|
|
499 default: MESSAGE("Unknown-%04x", nt->FileHeader.Machine); break;
|
|
500 }
|
|
501 MESSAGE(")\n");
|
|
502 goto error;
|
|
503 }
|
|
504
|
|
505
|
|
506 pe_sec = PE_SECTIONS( hModule );
|
|
507 rawsize = 0; lowest_va = 0x10000;
|
|
508 for (i = 0; i < nt->FileHeader.NumberOfSections; i++)
|
|
509 {
|
|
510 if (lowest_va > pe_sec[i].VirtualAddress)
|
|
511 lowest_va = pe_sec[i].VirtualAddress;
|
|
512 if (pe_sec[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
|
|
513 continue;
|
|
514 if (pe_sec[i].PointerToRawData+pe_sec[i].SizeOfRawData > rawsize)
|
|
515 rawsize = pe_sec[i].PointerToRawData+pe_sec[i].SizeOfRawData;
|
|
516 }
|
|
517
|
|
518
|
|
519 if ( file_size && file_size < rawsize )
|
|
520 {
|
|
521 ERR("PE module is too small (header: %d, filesize: %d), "
|
|
522 "probably truncated download?\n",
|
|
523 rawsize, file_size );
|
|
524 goto error;
|
|
525 }
|
|
526
|
|
527
|
|
528 aoep = nt->OptionalHeader.AddressOfEntryPoint;
|
|
529 if (aoep && (aoep < lowest_va))
|
|
530 FIXME("VIRUS WARNING: '%s' has an invalid entrypoint (0x%08lx) "
|
|
531 "below the first virtual address (0x%08x) "
|
|
532 "(possibly infected by Tchernobyl/SpaceFiller virus)!\n",
|
|
533 filename, aoep, lowest_va );
|
|
534
|
|
535
|
|
536 /* FIXME: Hack! While we don't really support shared sections yet,
|
|
537 * this checks for those special cases where the whole DLL
|
|
538 * consists only of shared sections and is mapped into the
|
|
539 * shared address space > 2GB. In this case, we assume that
|
|
540 * the module got mapped at its base address. Thus we simply
|
|
541 * check whether the module has actually been mapped there
|
|
542 * and use it, if so. This is needed to get Win95 USER32.DLL
|
|
543 * to work (until we support shared sections properly).
|
|
544 */
|
|
545
|
|
546 if ( nt->OptionalHeader.ImageBase & 0x80000000 )
|
|
547 {
|
|
548 HMODULE sharedMod = (HMODULE)nt->OptionalHeader.ImageBase;
|
|
549 IMAGE_NT_HEADERS *sharedNt = (PIMAGE_NT_HEADERS)
|
|
550 ( (LPBYTE)sharedMod + ((LPBYTE)nt - (LPBYTE)hModule) );
|
|
551
|
|
552 /* Well, this check is not really comprehensive,
|
|
553 but should be good enough for now ... */
|
|
554 if ( !IsBadReadPtr( (LPBYTE)sharedMod, sizeof(IMAGE_DOS_HEADER) )
|
|
555 && memcmp( (LPBYTE)sharedMod, (LPBYTE)hModule, sizeof(IMAGE_DOS_HEADER) ) == 0
|
|
556 && !IsBadReadPtr( sharedNt, sizeof(IMAGE_NT_HEADERS) )
|
|
557 && memcmp( sharedNt, nt, sizeof(IMAGE_NT_HEADERS) ) == 0 )
|
|
558 {
|
|
559 UnmapViewOfFile( (LPVOID)hModule );
|
|
560 return sharedMod;
|
|
561 }
|
|
562 }
|
|
563
|
|
564
|
|
565
|
|
566 load_addr = nt->OptionalHeader.ImageBase;
|
|
567 vma_size = calc_vma_size( hModule );
|
|
568
|
|
569 load_addr = (DWORD)VirtualAlloc( (void*)load_addr, vma_size,
|
|
570 MEM_RESERVE | MEM_COMMIT,
|
|
571 PAGE_EXECUTE_READWRITE );
|
|
572 if (load_addr == 0)
|
|
573 {
|
|
574
|
|
575 FIXME("We need to perform base relocations for %s\n", filename);
|
|
576 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_BASERELOC;
|
|
577 if (dir->Size)
|
|
578 reloc = dir->VirtualAddress;
|
|
579 else
|
|
580 {
|
|
581 FIXME( "FATAL: Need to relocate %s, but no relocation records present (%s). Try to run that file directly !\n",
|
|
582 filename,
|
|
583 (nt->FileHeader.Characteristics&IMAGE_FILE_RELOCS_STRIPPED)?
|
|
584 "stripped during link" : "unknown reason" );
|
|
585 goto error;
|
|
586 }
|
|
587
|
|
588 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
|
|
589 * really make sure that the *new* base address is also > 2GB.
|
|
590 * Some DLLs really check the MSB of the module handle :-/
|
|
591 */
|
|
592 if ( nt->OptionalHeader.ImageBase & 0x80000000 )
|
|
593 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
|
|
594
|
|
595 load_addr = (DWORD)VirtualAlloc( NULL, vma_size,
|
|
596 MEM_RESERVE | MEM_COMMIT,
|
|
597 PAGE_EXECUTE_READWRITE );
|
|
598 if (!load_addr) {
|
|
599 FIXME_(win32)(
|
|
600 "FATAL: Couldn't load module %s (out of memory, %d needed)!\n", filename, vma_size);
|
|
601 goto error;
|
|
602 }
|
|
603 }
|
|
604
|
|
605 TRACE("Load addr is %lx (base %lx), range %x\n",
|
|
606 load_addr, nt->OptionalHeader.ImageBase, vma_size );
|
|
607 TRACE_(segment)("Loading %s at %lx, range %x\n",
|
|
608 filename, load_addr, vma_size );
|
|
609
|
|
610 #if 0
|
|
611
|
|
612 *(PIMAGE_DOS_HEADER)load_addr = *(PIMAGE_DOS_HEADER)hModule;
|
|
613 *PE_HEADER( load_addr ) = *nt;
|
|
614 memcpy( PE_SECTIONS(load_addr), PE_SECTIONS(hModule),
|
|
615 sizeof(IMAGE_SECTION_HEADER) * nt->FileHeader.NumberOfSections );
|
|
616
|
|
617
|
|
618 memcpy( load_addr, hModule, lowest_fa );
|
|
619 #endif
|
|
620
|
|
621 if ((void*)FILE_dommap( handle, (void *)load_addr, 0, nt->OptionalHeader.SizeOfHeaders,
|
|
622 0, 0, PROT_EXEC | PROT_WRITE | PROT_READ,
|
|
623 MAP_PRIVATE | MAP_FIXED ) != (void*)load_addr)
|
|
624 {
|
|
625 ERR_(win32)( "Critical Error: failed to map PE header to necessary address.\n");
|
|
626 goto error;
|
|
627 }
|
|
628
|
|
629
|
|
630 pe_sec = PE_SECTIONS( hModule );
|
|
631 for (i = 0; i < nt->FileHeader.NumberOfSections; i++, pe_sec++)
|
|
632 {
|
|
633 if (!pe_sec->SizeOfRawData || !pe_sec->PointerToRawData) continue;
|
|
634 TRACE("%s: mmaping section %s at %p off %lx size %lx/%lx\n",
|
|
635 filename, pe_sec->Name, (void*)RVA(pe_sec->VirtualAddress),
|
|
636 pe_sec->PointerToRawData, pe_sec->SizeOfRawData, pe_sec->Misc.VirtualSize );
|
|
637 if ((void*)FILE_dommap( unix_handle, (void*)RVA(pe_sec->VirtualAddress),
|
|
638 0, pe_sec->SizeOfRawData, 0, pe_sec->PointerToRawData,
|
|
639 PROT_EXEC | PROT_WRITE | PROT_READ,
|
|
640 MAP_PRIVATE | MAP_FIXED ) != (void*)RVA(pe_sec->VirtualAddress))
|
|
641 {
|
|
642
|
|
643 ERR_(win32)( "Critical Error: failed to map PE section to necessary address.\n");
|
|
644 goto error;
|
|
645 }
|
|
646 if ((pe_sec->SizeOfRawData < pe_sec->Misc.VirtualSize) &&
|
|
647 (pe_sec->SizeOfRawData & (page_size-1)))
|
|
648 {
|
|
649 DWORD end = (pe_sec->SizeOfRawData & ~(page_size-1)) + page_size;
|
|
650 if (end > pe_sec->Misc.VirtualSize) end = pe_sec->Misc.VirtualSize;
|
|
651 TRACE("clearing %p - %p\n",
|
|
652 RVA(pe_sec->VirtualAddress) + pe_sec->SizeOfRawData,
|
|
653 RVA(pe_sec->VirtualAddress) + end );
|
|
654 memset( (char*)RVA(pe_sec->VirtualAddress) + pe_sec->SizeOfRawData, 0,
|
|
655 end - pe_sec->SizeOfRawData );
|
|
656 }
|
|
657 }
|
|
658
|
|
659
|
|
660 if ( reloc )
|
|
661 do_relocations( load_addr, (IMAGE_BASE_RELOCATION *)RVA(reloc) );
|
|
662
|
|
663
|
|
664 *version = ( (nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 )
|
|
665 | (nt->OptionalHeader.MinorSubsystemVersion & 0xff);
|
|
666
|
|
667
|
|
668 UnmapViewOfFile( (LPVOID)hModule );
|
|
669 return (HMODULE)load_addr;
|
|
670
|
|
671 error:
|
|
672 if (unix_handle != -1) close( unix_handle );
|
128
|
673 if (load_addr)
|
|
674 VirtualFree( (LPVOID)load_addr, 0, MEM_RELEASE );
|
1
|
675 UnmapViewOfFile( (LPVOID)hModule );
|
|
676 return 0;
|
|
677 }
|
|
678
|
|
679 /**********************************************************************
|
|
680 * PE_CreateModule
|
|
681 *
|
|
682 * Create WINE_MODREF structure for loaded HMODULE32, link it into
|
|
683 * process modref_list, and fixup all imports.
|
|
684 *
|
|
685 * Note: hModule must point to a correctly allocated PE image,
|
|
686 * with base relocations applied; the 16-bit dummy module
|
|
687 * associated to hModule must already exist.
|
|
688 *
|
|
689 * Note: This routine must always be called in the context of the
|
|
690 * process that is to own the module to be created.
|
|
691 */
|
|
692 WINE_MODREF *PE_CreateModule( HMODULE hModule,
|
|
693 LPCSTR filename, DWORD flags, WIN_BOOL builtin )
|
|
694 {
|
|
695 DWORD load_addr = (DWORD)hModule;
|
|
696 IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
|
|
697 IMAGE_DATA_DIRECTORY *dir;
|
|
698 IMAGE_IMPORT_DESCRIPTOR *pe_import = NULL;
|
|
699 IMAGE_EXPORT_DIRECTORY *pe_export = NULL;
|
|
700 IMAGE_RESOURCE_DIRECTORY *pe_resource = NULL;
|
|
701 WINE_MODREF *wm;
|
|
702 int result;
|
|
703
|
|
704
|
|
705
|
|
706
|
|
707 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
|
|
708 if (dir->Size)
|
|
709 pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(dir->VirtualAddress);
|
|
710
|
|
711 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
|
|
712 if (dir->Size)
|
|
713 pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(dir->VirtualAddress);
|
|
714
|
|
715 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
|
|
716 if (dir->Size)
|
|
717 pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(dir->VirtualAddress);
|
|
718
|
|
719 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXCEPTION;
|
|
720 if (dir->Size) FIXME("Exception directory ignored\n" );
|
|
721
|
|
722 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_SECURITY;
|
|
723 if (dir->Size) FIXME("Security directory ignored\n" );
|
|
724
|
|
725
|
|
726
|
|
727
|
|
728 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_DEBUG;
|
|
729 if (dir->Size) TRACE("Debug directory ignored\n" );
|
|
730
|
|
731 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_COPYRIGHT;
|
|
732 if (dir->Size) FIXME("Copyright string ignored\n" );
|
|
733
|
|
734 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_GLOBALPTR;
|
|
735 if (dir->Size) FIXME("Global Pointer (MIPS) ignored\n" );
|
|
736
|
|
737
|
|
738
|
|
739 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG;
|
|
740 if (dir->Size) FIXME("Load Configuration directory ignored\n" );
|
|
741
|
|
742 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT;
|
|
743 if (dir->Size) TRACE("Bound Import directory ignored\n" );
|
|
744
|
|
745 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IAT;
|
|
746 if (dir->Size) TRACE("Import Address Table directory ignored\n" );
|
|
747
|
|
748 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT;
|
|
749 if (dir->Size)
|
|
750 {
|
|
751 TRACE("Delayed import, stub calls LoadLibrary\n" );
|
|
752 /*
|
|
753 * Nothing to do here.
|
|
754 */
|
|
755
|
|
756 #ifdef ImgDelayDescr
|
|
757 /*
|
|
758 * This code is useful to observe what the heck is going on.
|
|
759 */
|
|
760 {
|
|
761 ImgDelayDescr *pe_delay = NULL;
|
|
762 pe_delay = (PImgDelayDescr)RVA(dir->VirtualAddress);
|
|
763 TRACE_(delayhlp)("pe_delay->grAttrs = %08x\n", pe_delay->grAttrs);
|
|
764 TRACE_(delayhlp)("pe_delay->szName = %s\n", pe_delay->szName);
|
|
765 TRACE_(delayhlp)("pe_delay->phmod = %08x\n", pe_delay->phmod);
|
|
766 TRACE_(delayhlp)("pe_delay->pIAT = %08x\n", pe_delay->pIAT);
|
|
767 TRACE_(delayhlp)("pe_delay->pINT = %08x\n", pe_delay->pINT);
|
|
768 TRACE_(delayhlp)("pe_delay->pBoundIAT = %08x\n", pe_delay->pBoundIAT);
|
|
769 TRACE_(delayhlp)("pe_delay->pUnloadIAT = %08x\n", pe_delay->pUnloadIAT);
|
|
770 TRACE_(delayhlp)("pe_delay->dwTimeStamp = %08x\n", pe_delay->dwTimeStamp);
|
|
771 }
|
|
772 #endif
|
|
773 }
|
|
774
|
|
775 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR;
|
|
776 if (dir->Size) FIXME("Unknown directory 14 ignored\n" );
|
|
777
|
|
778 dir = nt->OptionalHeader.DataDirectory+15;
|
|
779 if (dir->Size) FIXME("Unknown directory 15 ignored\n" );
|
|
780
|
|
781
|
|
782
|
|
783
|
|
784 wm = (WINE_MODREF *)HeapAlloc( GetProcessHeap(),
|
|
785 HEAP_ZERO_MEMORY, sizeof(*wm) );
|
|
786 wm->module = hModule;
|
|
787
|
|
788 if ( builtin )
|
|
789 wm->flags |= WINE_MODREF_INTERNAL;
|
|
790 if ( flags & DONT_RESOLVE_DLL_REFERENCES )
|
|
791 wm->flags |= WINE_MODREF_DONT_RESOLVE_REFS;
|
|
792 if ( flags & LOAD_LIBRARY_AS_DATAFILE )
|
|
793 wm->flags |= WINE_MODREF_LOAD_AS_DATAFILE;
|
|
794
|
|
795 wm->type = MODULE32_PE;
|
|
796 wm->binfmt.pe.pe_export = pe_export;
|
|
797 wm->binfmt.pe.pe_import = pe_import;
|
|
798 wm->binfmt.pe.pe_resource = pe_resource;
|
|
799 wm->binfmt.pe.tlsindex = -1;
|
|
800
|
|
801 wm->filename = malloc(strlen(filename)+1);
|
|
802 strcpy(wm->filename, filename );
|
|
803 wm->modname = strrchr( wm->filename, '\\' );
|
|
804 if (!wm->modname) wm->modname = wm->filename;
|
|
805 else wm->modname++;
|
|
806
|
|
807 if ( pe_export )
|
|
808 dump_exports( hModule );
|
|
809
|
|
810 /* Fixup Imports */
|
|
811
|
|
812 if ( pe_import
|
|
813 && !( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE )
|
|
814 && !( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS )
|
|
815 && fixup_imports( wm ) )
|
|
816 {
|
|
817 /* remove entry from modref chain */
|
|
818 return NULL;
|
|
819 }
|
|
820
|
|
821 return wm;
|
|
822
|
|
823 return wm;
|
|
824 }
|
|
825
|
|
826 /******************************************************************************
|
|
827 * The PE Library Loader frontend.
|
|
828 * FIXME: handle the flags.
|
|
829 */
|
|
830 WINE_MODREF *PE_LoadLibraryExA (LPCSTR name, DWORD flags)
|
|
831 {
|
|
832 HMODULE hModule32;
|
|
833 WINE_MODREF *wm;
|
|
834 char filename[256];
|
|
835 int hFile;
|
|
836 WORD version = 0;
|
|
837
|
|
838
|
|
839 strncpy(filename, name, sizeof(filename));
|
|
840 hFile=open(filename, O_RDONLY);
|
|
841 if(hFile==-1)
|
|
842 return NULL;
|
|
843
|
|
844
|
|
845 hModule32 = PE_LoadImage( hFile, filename, &version );
|
|
846 if (!hModule32)
|
|
847 {
|
|
848 SetLastError( ERROR_OUTOFMEMORY );
|
|
849 return NULL;
|
|
850 }
|
|
851
|
|
852 if ( !(wm = PE_CreateModule( hModule32, filename, flags, FALSE )) )
|
|
853 {
|
|
854 ERR( "can't load %s\n", filename );
|
|
855 SetLastError( ERROR_OUTOFMEMORY );
|
|
856 return NULL;
|
|
857 }
|
|
858 close(hFile);
|
|
859 return wm;
|
|
860 }
|
|
861
|
|
862
|
|
863 /*****************************************************************************
|
|
864 * PE_UnloadLibrary
|
|
865 *
|
|
866 * Unload the library unmapping the image and freeing the modref structure.
|
|
867 */
|
|
868 void PE_UnloadLibrary(WINE_MODREF *wm)
|
|
869 {
|
|
870 TRACE(" unloading %s\n", wm->filename);
|
|
871
|
|
872 HeapFree( GetProcessHeap(), 0, wm->filename );
|
|
873 HeapFree( GetProcessHeap(), 0, wm->short_filename );
|
128
|
874 VirtualFree( (LPVOID)wm->module, 0, MEM_RELEASE );
|
1
|
875 HeapFree( GetProcessHeap(), 0, wm );
|
|
876 }
|
|
877
|
|
878 /*****************************************************************************
|
|
879 * Load the PE main .EXE. All other loading is done by PE_LoadLibraryExA
|
|
880 * FIXME: this function should use PE_LoadLibraryExA, but currently can't
|
|
881 * due to the PROCESS_Create stuff.
|
|
882 */
|
|
883
|
|
884 /* Called if the library is loaded or freed.
|
|
885 * NOTE: if a thread attaches a DLL, the current thread will only do
|
|
886 * DLL_PROCESS_ATTACH. Only new created threads do DLL_THREAD_ATTACH
|
|
887 * (SDK)
|
|
888 */
|
128
|
889 extern void This_Is_Dirty_Hack()
|
|
890 {
|
|
891 void* mem=alloca(0x20000);
|
|
892 *(int*)mem=0x1234;
|
|
893 }
|
|
894
|
1
|
895 WIN_BOOL PE_InitDLL( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
|
|
896 {
|
|
897 WIN_BOOL retv = TRUE;
|
|
898 assert( wm->type == MODULE32_PE );
|
|
899
|
|
900
|
|
901 if ((PE_HEADER(wm->module)->FileHeader.Characteristics & IMAGE_FILE_DLL) &&
|
|
902 (PE_HEADER(wm->module)->OptionalHeader.AddressOfEntryPoint)
|
|
903 ) {
|
|
904 DLLENTRYPROC entry ;
|
|
905 entry = (void*)PE_FindExportedFunction(wm, "DllMain", 0);
|
|
906 if(entry==NULL)
|
|
907 entry = (void*)RVA_PTR( wm->module,OptionalHeader.AddressOfEntryPoint );
|
|
908
|
|
909 TRACE_(relay)("CallTo32(entryproc=%p,module=%08x,type=%ld,res=%p)\n",
|
|
910 entry, wm->module, type, lpReserved );
|
128
|
911
|
|
912
|
|
913 TRACE("Entering DllMain(");
|
1
|
914 switch(type)
|
|
915 {
|
|
916 case DLL_PROCESS_DETACH:
|
128
|
917 TRACE("DLL_PROCESS_DETACH) ");
|
1
|
918 break;
|
|
919 case DLL_PROCESS_ATTACH:
|
128
|
920 TRACE("DLL_PROCESS_ATTACH) ");
|
1
|
921 break;
|
|
922 case DLL_THREAD_DETACH:
|
128
|
923 TRACE("DLL_THREAD_DETACH) ");
|
1
|
924 break;
|
|
925 case DLL_THREAD_ATTACH:
|
128
|
926 TRACE("DLL_THREAD_ATTACH) ");
|
1
|
927 break;
|
|
928 }
|
128
|
929 TRACE("for %s\n", wm->filename);
|
|
930 This_Is_Dirty_Hack();
|
1
|
931 retv = entry( wm->module, type, lpReserved );
|
|
932 }
|
|
933
|
|
934 return retv;
|
|
935 }
|
|
936
|
|
937 static LPVOID
|
|
938 _fixup_address(PIMAGE_OPTIONAL_HEADER opt,int delta,LPVOID addr) {
|
|
939 if ( ((DWORD)addr>opt->ImageBase) &&
|
|
940 ((DWORD)addr<opt->ImageBase+opt->SizeOfImage)
|
|
941 )
|
|
942
|
|
943 return (LPVOID)(((DWORD)addr)+delta);
|
|
944 else
|
|
945
|
|
946 return addr;
|
|
947 }
|