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