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