Mercurial > mplayer.hg
annotate loader/pe_image.c @ 25721:c0e76b0bb543
Don't oversize realchallenge buffers
author | rtogni |
---|---|
date | Sun, 13 Jan 2008 21:12:02 +0000 |
parents | d4674d333c9c |
children | 2c8cdb9123b8 |
rev | line source |
---|---|
1 | 1 /* |
2 * Copyright 1994 Eric Youndale & Erik Bos | |
23734 | 3 * Copyright 1995 Martin von Löwis |
1 | 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 | |
15166
f5537cc95b02
Mark modified imported files as such to comply with GPL ¡ø2a.
diego
parents:
13375
diff
changeset
|
11 * |
18783 | 12 * Modified for use with MPlayer, detailed changelog at |
13 * http://svn.mplayerhq.hu/mplayer/trunk/ | |
15166
f5537cc95b02
Mark modified imported files as such to comply with GPL ¡ø2a.
diego
parents:
13375
diff
changeset
|
14 * $Id$ |
f5537cc95b02
Mark modified imported files as such to comply with GPL ¡ø2a.
diego
parents:
13375
diff
changeset
|
15 * |
1 | 16 */ |
17 /* Notes: | |
18 * Before you start changing something in this file be aware of the following: | |
19 * | |
20 * - There are several functions called recursively. In a very subtle and | |
21 * obscure way. DLLs can reference each other recursively etc. | |
22 * - If you want to enhance, speed up or clean up something in here, think | |
23 * twice WHY it is implemented in that strange way. There is usually a reason. | |
24 * Though sometimes it might just be lazyness ;) | |
25 * - In PE_MapImage, right before fixup_imports() all external and internal | |
26 * state MUST be correct since this function can be called with the SAME image | |
27 * AGAIN. (Thats recursion for you.) That means MODREF.module and | |
28 * NE_MODULE.module32. | |
29 * - Sometimes, we can't use Linux mmap() to mmap() the images directly. | |
30 * | |
31 * The problem is, that there is not direct 1:1 mapping from a diskimage and | |
32 * a memoryimage. The headers at the start are mapped linear, but the sections | |
33 * are not. Older x86 pe binaries are 512 byte aligned in file and 4096 byte | |
34 * aligned in memory. Linux likes them 4096 byte aligned in memory (due to | |
35 * x86 pagesize, this cannot be fixed without a rather large kernel rewrite) | |
36 * and 'blocksize' file-aligned (offsets). Since we have 512/1024/2048 (CDROM) | |
37 * and other byte blocksizes, we can't always do this. We *can* do this for | |
38 * newer pe binaries produced by MSVC 5 and later, since they are also aligned | |
39 * to 4096 byte boundaries on disk. | |
40 */ | |
2069 | 41 #include "config.h" |
21261
a2e02e6b6379
Rename config.h --> debug.h and include config.h explicitly.
diego
parents:
18783
diff
changeset
|
42 #include "debug.h" |
1 | 43 |
44 #include <errno.h> | |
45 #include <assert.h> | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
46 #include <stdio.h> |
1 | 47 #include <stdlib.h> |
48 #include <string.h> | |
49 #include <unistd.h> | |
50 #include <sys/types.h> | |
51 #include <sys/stat.h> | |
52 #include <fcntl.h> | |
53 #ifdef HAVE_SYS_MMAN_H | |
54 #include <sys/mman.h> | |
55 #endif | |
7386 | 56 #include "wine/windef.h" |
57 #include "wine/winbase.h" | |
58 #include "wine/winerror.h" | |
59 #include "wine/heap.h" | |
60 #include "wine/pe_image.h" | |
61 #include "wine/module.h" | |
62 #include "wine/debugtools.h" | |
2069 | 63 #include "ext.h" |
1 | 64 #include "win32.h" |
65 | |
66 #define RVA(x) ((void *)((char *)load_addr+(unsigned int)(x))) | |
67 | |
68 #define AdjustPtr(ptr,delta) ((char *)(ptr) + (delta)) | |
69 | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
70 static void dump_exports( HMODULE hModule ) |
1 | 71 { |
72 char *Module; | |
7386 | 73 unsigned int i, j; |
1 | 74 u_short *ordinal; |
75 u_long *function,*functions; | |
76 u_char **name; | |
77 unsigned int load_addr = hModule; | |
78 | |
79 DWORD rva_start = PE_HEADER(hModule)->OptionalHeader | |
80 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; | |
81 DWORD rva_end = rva_start + PE_HEADER(hModule)->OptionalHeader | |
82 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size; | |
83 IMAGE_EXPORT_DIRECTORY *pe_exports = (IMAGE_EXPORT_DIRECTORY*)RVA(rva_start); | |
84 | |
85 Module = (char*)RVA(pe_exports->Name); | |
86 TRACE("*******EXPORT DATA*******\n"); | |
87 TRACE("Module name is %s, %ld functions, %ld names\n", | |
88 Module, pe_exports->NumberOfFunctions, pe_exports->NumberOfNames); | |
89 | |
90 ordinal=(u_short*) RVA(pe_exports->AddressOfNameOrdinals); | |
91 functions=function=(u_long*) RVA(pe_exports->AddressOfFunctions); | |
92 name=(u_char**) RVA(pe_exports->AddressOfNames); | |
93 | |
94 TRACE(" Ord RVA Addr Name\n" ); | |
95 for (i=0;i<pe_exports->NumberOfFunctions;i++, function++) | |
96 { | |
97 if (!*function) continue; | |
98 if (TRACE_ON(win32)) | |
99 { | |
100 DPRINTF( "%4ld %08lx %p", i + pe_exports->Base, *function, RVA(*function) ); | |
101 | |
102 for (j = 0; j < pe_exports->NumberOfNames; j++) | |
103 if (ordinal[j] == i) | |
104 { | |
105 DPRINTF( " %s", (char*)RVA(name[j]) ); | |
106 break; | |
107 } | |
108 if ((*function >= rva_start) && (*function <= rva_end)) | |
109 DPRINTF(" (forwarded -> %s)", (char *)RVA(*function)); | |
110 DPRINTF("\n"); | |
111 } | |
112 } | |
113 } | |
114 | |
115 /* Look up the specified function or ordinal in the exportlist: | |
116 * If it is a string: | |
117 * - look up the name in the Name list. | |
118 * - look up the ordinal with that index. | |
119 * - use the ordinal as offset into the functionlist | |
120 * If it is a ordinal: | |
121 * - use ordinal-pe_export->Base as offset into the functionlist | |
122 */ | |
123 FARPROC PE_FindExportedFunction( | |
124 WINE_MODREF *wm, | |
125 LPCSTR funcName, | |
126 WIN_BOOL snoop ) | |
127 { | |
128 u_short * ordinals; | |
129 u_long * function; | |
7386 | 130 u_char ** name; |
131 const char *ename = NULL; | |
1 | 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; | |
7386 | 167 ename = (const char*) RVA(name[pos]); |
168 if (!(res = strcmp( ename, funcName ))) | |
1 | 169 { |
170 ordinal = ordinals[pos]; | |
171 goto found; | |
172 } | |
173 if (res > 0) max = pos - 1; | |
174 else min = pos + 1; | |
175 } | |
176 | |
7386 | 177 for (i = 0; i < exports->NumberOfNames; i++) |
1 | 178 { |
7386 | 179 ename = (const char*) RVA(name[i]); |
1 | 180 if (!strcmp( ename, funcName )) |
181 { | |
2069 | 182 ERR( "%s.%s required a linear search\n", wm->modname, funcName ); |
1 | 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 IMAGE_IMPORT_BY_NAME *pe_name; | |
288 PIMAGE_THUNK_DATA import_list,thunk_list; | |
289 char *name = (char *) RVA(pe_imp->Name); | |
290 | |
291 if (characteristics_detection && !pe_imp->u.Characteristics) | |
292 break; | |
293 | |
294 //#warning FIXME: here we should fill imports | |
295 TRACE("Loading imports for %s.dll\n", name); | |
296 | |
297 if (pe_imp->u.OriginalFirstThunk != 0) { | |
298 TRACE("Microsoft style imports used\n"); | |
299 import_list =(PIMAGE_THUNK_DATA) RVA(pe_imp->u.OriginalFirstThunk); | |
300 thunk_list = (PIMAGE_THUNK_DATA) RVA(pe_imp->FirstThunk); | |
301 | |
302 while (import_list->u1.Ordinal) { | |
303 if (IMAGE_SNAP_BY_ORDINAL(import_list->u1.Ordinal)) { | |
304 int ordinal = IMAGE_ORDINAL(import_list->u1.Ordinal); | |
305 | |
306 // TRACE("--- Ordinal %s,%d\n", name, ordinal); | |
307 | |
2651
958d10763c34
partially synced with avifile... (TODO: migrate to new registry.c and driver.c)
arpi
parents:
2069
diff
changeset
|
308 thunk_list->u1.Function=LookupExternal(name, ordinal); |
1 | 309 } else { |
310 pe_name = (PIMAGE_IMPORT_BY_NAME)RVA(import_list->u1.AddressOfData); | |
311 // TRACE("--- %s %s.%d\n", pe_name->Name, name, pe_name->Hint); | |
2651
958d10763c34
partially synced with avifile... (TODO: migrate to new registry.c and driver.c)
arpi
parents:
2069
diff
changeset
|
312 thunk_list->u1.Function=LookupExternalByName(name, pe_name->Name); |
1 | 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 return 0; | |
340 } | |
341 | |
342 static int calc_vma_size( HMODULE hModule ) | |
343 { | |
344 int i,vma_size = 0; | |
345 IMAGE_SECTION_HEADER *pe_seg = PE_SECTIONS(hModule); | |
346 | |
347 TRACE("Dump of segment table\n"); | |
348 TRACE(" Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n"); | |
349 for (i = 0; i< PE_HEADER(hModule)->FileHeader.NumberOfSections; i++) | |
350 { | |
351 TRACE("%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n", | |
352 pe_seg->Name, | |
353 pe_seg->Misc.VirtualSize, | |
354 pe_seg->VirtualAddress, | |
355 pe_seg->SizeOfRawData, | |
356 pe_seg->PointerToRawData, | |
357 pe_seg->PointerToRelocations, | |
358 pe_seg->PointerToLinenumbers, | |
359 pe_seg->NumberOfRelocations, | |
360 pe_seg->NumberOfLinenumbers, | |
361 pe_seg->Characteristics); | |
362 vma_size=max(vma_size, pe_seg->VirtualAddress+pe_seg->SizeOfRawData); | |
363 vma_size=max(vma_size, pe_seg->VirtualAddress+pe_seg->Misc.VirtualSize); | |
364 pe_seg++; | |
365 } | |
366 return vma_size; | |
367 } | |
368 | |
369 static void do_relocations( unsigned int load_addr, IMAGE_BASE_RELOCATION *r ) | |
370 { | |
371 int delta = load_addr - PE_HEADER(load_addr)->OptionalHeader.ImageBase; | |
372 int hdelta = (delta >> 16) & 0xFFFF; | |
373 int ldelta = delta & 0xFFFF; | |
374 | |
375 if(delta == 0) | |
376 | |
377 return; | |
378 while(r->VirtualAddress) | |
379 { | |
380 char *page = (char*) RVA(r->VirtualAddress); | |
381 int count = (r->SizeOfBlock - 8)/2; | |
382 int i; | |
383 TRACE_(fixup)("%x relocations for page %lx\n", | |
384 count, r->VirtualAddress); | |
385 | |
386 for(i=0;i<count;i++) | |
387 { | |
388 int offset = r->TypeOffset[i] & 0xFFF; | |
389 int type = r->TypeOffset[i] >> 12; | |
390 // TRACE_(fixup)("patching %x type %x\n", offset, type); | |
391 switch(type) | |
392 { | |
393 case IMAGE_REL_BASED_ABSOLUTE: break; | |
394 case IMAGE_REL_BASED_HIGH: | |
395 *(short*)(page+offset) += hdelta; | |
396 break; | |
397 case IMAGE_REL_BASED_LOW: | |
398 *(short*)(page+offset) += ldelta; | |
399 break; | |
400 case IMAGE_REL_BASED_HIGHLOW: | |
401 *(int*)(page+offset) += delta; | |
402 | |
403 break; | |
404 case IMAGE_REL_BASED_HIGHADJ: | |
405 FIXME("Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n"); | |
406 break; | |
407 case IMAGE_REL_BASED_MIPS_JMPADDR: | |
408 FIXME("Is this a MIPS machine ???\n"); | |
409 break; | |
410 default: | |
411 FIXME("Unknown fixup type\n"); | |
412 break; | |
413 } | |
414 } | |
415 r = (IMAGE_BASE_RELOCATION*)((char*)r + r->SizeOfBlock); | |
416 } | |
417 } | |
418 | |
419 | |
420 | |
421 | |
422 | |
423 /********************************************************************** | |
424 * PE_LoadImage | |
425 * Load one PE format DLL/EXE into memory | |
426 * | |
427 * Unluckily we can't just mmap the sections where we want them, for | |
428 * (at least) Linux does only support offsets which are page-aligned. | |
429 * | |
430 * BUT we have to map the whole image anyway, for Win32 programs sometimes | |
431 * want to access them. (HMODULE32 point to the start of it) | |
432 */ | |
433 HMODULE PE_LoadImage( int handle, LPCSTR filename, WORD *version ) | |
434 { | |
435 HMODULE hModule; | |
436 HANDLE mapping; | |
437 | |
438 IMAGE_NT_HEADERS *nt; | |
439 IMAGE_SECTION_HEADER *pe_sec; | |
440 IMAGE_DATA_DIRECTORY *dir; | |
24381 | 441 // BY_HANDLE_FILE_INFORMATION bhfi; |
1 | 442 int i, rawsize, lowest_va, vma_size, file_size = 0; |
443 DWORD load_addr = 0, aoep, reloc = 0; | |
444 // struct get_read_fd_request *req = get_req_buffer(); | |
445 int unix_handle = handle; | |
446 int page_size = getpagesize(); | |
447 | |
448 | |
449 // if ( GetFileInformationByHandle( hFile, &bhfi ) ) | |
450 // file_size = bhfi.nFileSizeLow; | |
451 file_size=lseek(handle, 0, SEEK_END); | |
452 lseek(handle, 0, SEEK_SET); | |
453 | |
454 //#warning fix CreateFileMappingA | |
455 mapping = CreateFileMappingA( handle, NULL, PAGE_READONLY | SEC_COMMIT, | |
456 0, 0, NULL ); | |
457 if (!mapping) | |
458 { | |
459 WARN("CreateFileMapping error %ld\n", GetLastError() ); | |
460 return 0; | |
461 } | |
462 // hModule = (HMODULE)MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 ); | |
463 hModule=(HMODULE)mapping; | |
464 // CloseHandle( mapping ); | |
465 if (!hModule) | |
466 { | |
467 WARN("MapViewOfFile error %ld\n", GetLastError() ); | |
468 return 0; | |
469 } | |
470 if ( *(WORD*)hModule !=IMAGE_DOS_SIGNATURE) | |
471 { | |
472 WARN("%s image doesn't have DOS signature, but 0x%04x\n", filename,*(WORD*)hModule); | |
473 goto error; | |
474 } | |
475 | |
476 nt = PE_HEADER( hModule ); | |
477 | |
478 | |
479 if ( nt->Signature != IMAGE_NT_SIGNATURE ) | |
480 { | |
481 WARN("%s image doesn't have PE signature, but 0x%08lx\n", filename, nt->Signature ); | |
482 goto error; | |
483 } | |
484 | |
485 | |
486 if ( nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386 ) | |
487 { | |
488 MESSAGE("Trying to load PE image for unsupported architecture ("); | |
489 switch (nt->FileHeader.Machine) | |
490 { | |
491 case IMAGE_FILE_MACHINE_UNKNOWN: MESSAGE("Unknown"); break; | |
492 case IMAGE_FILE_MACHINE_I860: MESSAGE("I860"); break; | |
493 case IMAGE_FILE_MACHINE_R3000: MESSAGE("R3000"); break; | |
494 case IMAGE_FILE_MACHINE_R4000: MESSAGE("R4000"); break; | |
495 case IMAGE_FILE_MACHINE_R10000: MESSAGE("R10000"); break; | |
496 case IMAGE_FILE_MACHINE_ALPHA: MESSAGE("Alpha"); break; | |
497 case IMAGE_FILE_MACHINE_POWERPC: MESSAGE("PowerPC"); break; | |
498 default: MESSAGE("Unknown-%04x", nt->FileHeader.Machine); break; | |
499 } | |
500 MESSAGE(")\n"); | |
501 goto error; | |
502 } | |
503 | |
504 | |
505 pe_sec = PE_SECTIONS( hModule ); | |
506 rawsize = 0; lowest_va = 0x10000; | |
507 for (i = 0; i < nt->FileHeader.NumberOfSections; i++) | |
508 { | |
509 if (lowest_va > pe_sec[i].VirtualAddress) | |
510 lowest_va = pe_sec[i].VirtualAddress; | |
511 if (pe_sec[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) | |
512 continue; | |
513 if (pe_sec[i].PointerToRawData+pe_sec[i].SizeOfRawData > rawsize) | |
514 rawsize = pe_sec[i].PointerToRawData+pe_sec[i].SizeOfRawData; | |
515 } | |
516 | |
517 | |
518 if ( file_size && file_size < rawsize ) | |
519 { | |
520 ERR("PE module is too small (header: %d, filesize: %d), " | |
521 "probably truncated download?\n", | |
522 rawsize, file_size ); | |
523 goto error; | |
524 } | |
525 | |
526 | |
527 aoep = nt->OptionalHeader.AddressOfEntryPoint; | |
528 if (aoep && (aoep < lowest_va)) | |
529 FIXME("VIRUS WARNING: '%s' has an invalid entrypoint (0x%08lx) " | |
530 "below the first virtual address (0x%08x) " | |
531 "(possibly infected by Tchernobyl/SpaceFiller virus)!\n", | |
532 filename, aoep, lowest_va ); | |
533 | |
534 | |
535 /* FIXME: Hack! While we don't really support shared sections yet, | |
536 * this checks for those special cases where the whole DLL | |
537 * consists only of shared sections and is mapped into the | |
538 * shared address space > 2GB. In this case, we assume that | |
539 * the module got mapped at its base address. Thus we simply | |
540 * check whether the module has actually been mapped there | |
541 * and use it, if so. This is needed to get Win95 USER32.DLL | |
542 * to work (until we support shared sections properly). | |
543 */ | |
544 | |
23961 | 545 if ( nt->OptionalHeader.ImageBase & 0x80000000 && |
546 !strstr(filename, "xanlib.dll")) | |
1 | 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 | |
2069 | 564 |
1 | 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 | |
703 | |
704 | |
705 | |
706 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT; | |
707 if (dir->Size) | |
708 pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(dir->VirtualAddress); | |
709 | |
710 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT; | |
711 if (dir->Size) | |
712 pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(dir->VirtualAddress); | |
713 | |
714 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE; | |
715 if (dir->Size) | |
716 pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(dir->VirtualAddress); | |
717 | |
718 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXCEPTION; | |
719 if (dir->Size) FIXME("Exception directory ignored\n" ); | |
720 | |
721 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_SECURITY; | |
722 if (dir->Size) FIXME("Security directory ignored\n" ); | |
723 | |
724 | |
725 | |
726 | |
727 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_DEBUG; | |
728 if (dir->Size) TRACE("Debug directory ignored\n" ); | |
729 | |
730 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_COPYRIGHT; | |
731 if (dir->Size) FIXME("Copyright string ignored\n" ); | |
732 | |
733 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_GLOBALPTR; | |
734 if (dir->Size) FIXME("Global Pointer (MIPS) ignored\n" ); | |
735 | |
736 | |
737 | |
738 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG; | |
739 if (dir->Size) FIXME("Load Configuration directory ignored\n" ); | |
740 | |
741 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT; | |
742 if (dir->Size) TRACE("Bound Import directory ignored\n" ); | |
743 | |
744 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IAT; | |
745 if (dir->Size) TRACE("Import Address Table directory ignored\n" ); | |
746 | |
747 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT; | |
748 if (dir->Size) | |
749 { | |
750 TRACE("Delayed import, stub calls LoadLibrary\n" ); | |
751 /* | |
752 * Nothing to do here. | |
753 */ | |
754 | |
755 #ifdef ImgDelayDescr | |
756 /* | |
757 * This code is useful to observe what the heck is going on. | |
758 */ | |
759 { | |
760 ImgDelayDescr *pe_delay = NULL; | |
761 pe_delay = (PImgDelayDescr)RVA(dir->VirtualAddress); | |
762 TRACE_(delayhlp)("pe_delay->grAttrs = %08x\n", pe_delay->grAttrs); | |
763 TRACE_(delayhlp)("pe_delay->szName = %s\n", pe_delay->szName); | |
764 TRACE_(delayhlp)("pe_delay->phmod = %08x\n", pe_delay->phmod); | |
765 TRACE_(delayhlp)("pe_delay->pIAT = %08x\n", pe_delay->pIAT); | |
766 TRACE_(delayhlp)("pe_delay->pINT = %08x\n", pe_delay->pINT); | |
767 TRACE_(delayhlp)("pe_delay->pBoundIAT = %08x\n", pe_delay->pBoundIAT); | |
768 TRACE_(delayhlp)("pe_delay->pUnloadIAT = %08x\n", pe_delay->pUnloadIAT); | |
769 TRACE_(delayhlp)("pe_delay->dwTimeStamp = %08x\n", pe_delay->dwTimeStamp); | |
770 } | |
771 #endif | |
772 } | |
773 | |
774 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR; | |
775 if (dir->Size) FIXME("Unknown directory 14 ignored\n" ); | |
776 | |
777 dir = nt->OptionalHeader.DataDirectory+15; | |
778 if (dir->Size) FIXME("Unknown directory 15 ignored\n" ); | |
779 | |
780 | |
781 | |
782 | |
783 wm = (WINE_MODREF *)HeapAlloc( GetProcessHeap(), | |
784 HEAP_ZERO_MEMORY, sizeof(*wm) ); | |
785 wm->module = hModule; | |
786 | |
787 if ( builtin ) | |
788 wm->flags |= WINE_MODREF_INTERNAL; | |
789 if ( flags & DONT_RESOLVE_DLL_REFERENCES ) | |
790 wm->flags |= WINE_MODREF_DONT_RESOLVE_REFS; | |
791 if ( flags & LOAD_LIBRARY_AS_DATAFILE ) | |
792 wm->flags |= WINE_MODREF_LOAD_AS_DATAFILE; | |
793 | |
794 wm->type = MODULE32_PE; | |
795 wm->binfmt.pe.pe_export = pe_export; | |
796 wm->binfmt.pe.pe_import = pe_import; | |
797 wm->binfmt.pe.pe_resource = pe_resource; | |
798 wm->binfmt.pe.tlsindex = -1; | |
799 | |
800 wm->filename = malloc(strlen(filename)+1); | |
801 strcpy(wm->filename, filename ); | |
802 wm->modname = strrchr( wm->filename, '\\' ); | |
803 if (!wm->modname) wm->modname = wm->filename; | |
804 else wm->modname++; | |
805 | |
806 if ( pe_export ) | |
807 dump_exports( hModule ); | |
808 | |
809 /* Fixup Imports */ | |
810 | |
811 if ( pe_import | |
812 && !( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) | |
813 && !( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS ) | |
814 && fixup_imports( wm ) ) | |
815 { | |
816 /* remove entry from modref chain */ | |
817 return NULL; | |
818 } | |
819 | |
820 return wm; | |
821 } | |
822 | |
823 /****************************************************************************** | |
824 * The PE Library Loader frontend. | |
825 * FIXME: handle the flags. | |
826 */ | |
827 WINE_MODREF *PE_LoadLibraryExA (LPCSTR name, DWORD flags) | |
828 { | |
829 HMODULE hModule32; | |
830 WINE_MODREF *wm; | |
831 char filename[256]; | |
832 int hFile; | |
833 WORD version = 0; | |
834 | |
835 | |
836 strncpy(filename, name, sizeof(filename)); | |
837 hFile=open(filename, O_RDONLY); | |
838 if(hFile==-1) | |
839 return NULL; | |
840 | |
841 | |
842 hModule32 = PE_LoadImage( hFile, filename, &version ); | |
843 if (!hModule32) | |
844 { | |
845 SetLastError( ERROR_OUTOFMEMORY ); | |
846 return NULL; | |
847 } | |
848 | |
849 if ( !(wm = PE_CreateModule( hModule32, filename, flags, FALSE )) ) | |
850 { | |
851 ERR( "can't load %s\n", filename ); | |
852 SetLastError( ERROR_OUTOFMEMORY ); | |
853 return NULL; | |
854 } | |
855 close(hFile); | |
2651
958d10763c34
partially synced with avifile... (TODO: migrate to new registry.c and driver.c)
arpi
parents:
2069
diff
changeset
|
856 //printf("^^^^^^^^^^^^^^^^Alloc VM1 %p\n", wm); |
1 | 857 return wm; |
858 } | |
859 | |
860 | |
861 /***************************************************************************** | |
862 * PE_UnloadLibrary | |
863 * | |
864 * Unload the library unmapping the image and freeing the modref structure. | |
865 */ | |
866 void PE_UnloadLibrary(WINE_MODREF *wm) | |
867 { | |
868 TRACE(" unloading %s\n", wm->filename); | |
869 | |
2651
958d10763c34
partially synced with avifile... (TODO: migrate to new registry.c and driver.c)
arpi
parents:
2069
diff
changeset
|
870 if (wm->filename) |
958d10763c34
partially synced with avifile... (TODO: migrate to new registry.c and driver.c)
arpi
parents:
2069
diff
changeset
|
871 free(wm->filename); |
958d10763c34
partially synced with avifile... (TODO: migrate to new registry.c and driver.c)
arpi
parents:
2069
diff
changeset
|
872 if (wm->short_filename) |
958d10763c34
partially synced with avifile... (TODO: migrate to new registry.c and driver.c)
arpi
parents:
2069
diff
changeset
|
873 free(wm->short_filename); |
958d10763c34
partially synced with avifile... (TODO: migrate to new registry.c and driver.c)
arpi
parents:
2069
diff
changeset
|
874 HeapFree( GetProcessHeap(), 0, wm->deps ); |
128 | 875 VirtualFree( (LPVOID)wm->module, 0, MEM_RELEASE ); |
1 | 876 HeapFree( GetProcessHeap(), 0, wm ); |
2651
958d10763c34
partially synced with avifile... (TODO: migrate to new registry.c and driver.c)
arpi
parents:
2069
diff
changeset
|
877 //printf("^^^^^^^^^^^^^^^^Free VM1 %p\n", wm); |
1 | 878 } |
879 | |
880 /***************************************************************************** | |
881 * Load the PE main .EXE. All other loading is done by PE_LoadLibraryExA | |
882 * FIXME: this function should use PE_LoadLibraryExA, but currently can't | |
883 * due to the PROCESS_Create stuff. | |
884 */ | |
885 | |
2069 | 886 |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
887 /* |
1411
db849cee5777
Pre-allocate some stack space to work around a problem with DLL alloca() code
jkeil
parents:
1307
diff
changeset
|
888 * 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
|
889 * 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
|
890 * 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
|
891 * 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
|
892 * 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
|
893 * 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
|
894 * 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
|
895 * 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
|
896 * 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
|
897 * of memory on the stack, for use by the DLLs alloca routine. |
13375
460281609f28
force compilers not to optimize/inline extend_stack_for_dll_alloca
reimar
parents:
7386
diff
changeset
|
898 * Added the noinline attribute as e.g. gcc 3.2.2 inlines this function |
460281609f28
force compilers not to optimize/inline extend_stack_for_dll_alloca
reimar
parents:
7386
diff
changeset
|
899 * in a way that breaks it. |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
900 */ |
13375
460281609f28
force compilers not to optimize/inline extend_stack_for_dll_alloca
reimar
parents:
7386
diff
changeset
|
901 static void __attribute__((noinline)) 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
|
902 { |
15566 | 903 #if !defined(__FreeBSD__) && !defined(__DragonFly__) |
13375
460281609f28
force compilers not to optimize/inline extend_stack_for_dll_alloca
reimar
parents:
7386
diff
changeset
|
904 volatile int* mem=alloca(0x20000); |
460281609f28
force compilers not to optimize/inline extend_stack_for_dll_alloca
reimar
parents:
7386
diff
changeset
|
905 *mem=0x1234; |
2069 | 906 #endif |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
907 } |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
908 |
1 | 909 /* Called if the library is loaded or freed. |
910 * NOTE: if a thread attaches a DLL, the current thread will only do | |
911 * DLL_PROCESS_ATTACH. Only new created threads do DLL_THREAD_ATTACH | |
912 * (SDK) | |
913 */ | |
914 WIN_BOOL PE_InitDLL( WINE_MODREF *wm, DWORD type, LPVOID lpReserved ) | |
915 { | |
916 WIN_BOOL retv = TRUE; | |
917 assert( wm->type == MODULE32_PE ); | |
918 | |
919 | |
920 if ((PE_HEADER(wm->module)->FileHeader.Characteristics & IMAGE_FILE_DLL) && | |
921 (PE_HEADER(wm->module)->OptionalHeader.AddressOfEntryPoint) | |
922 ) { | |
923 DLLENTRYPROC entry ; | |
924 entry = (void*)PE_FindExportedFunction(wm, "DllMain", 0); | |
925 if(entry==NULL) | |
926 entry = (void*)RVA_PTR( wm->module,OptionalHeader.AddressOfEntryPoint ); | |
927 | |
928 TRACE_(relay)("CallTo32(entryproc=%p,module=%08x,type=%ld,res=%p)\n", | |
929 entry, wm->module, type, lpReserved ); | |
128 | 930 |
931 | |
932 TRACE("Entering DllMain("); | |
1 | 933 switch(type) |
934 { | |
935 case DLL_PROCESS_DETACH: | |
128 | 936 TRACE("DLL_PROCESS_DETACH) "); |
1 | 937 break; |
938 case DLL_PROCESS_ATTACH: | |
128 | 939 TRACE("DLL_PROCESS_ATTACH) "); |
1 | 940 break; |
941 case DLL_THREAD_DETACH: | |
128 | 942 TRACE("DLL_THREAD_DETACH) "); |
1 | 943 break; |
944 case DLL_THREAD_ATTACH: | |
128 | 945 TRACE("DLL_THREAD_ATTACH) "); |
1 | 946 break; |
947 } | |
128 | 948 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
|
949 extend_stack_for_dll_alloca(); |
1 | 950 retv = entry( wm->module, type, lpReserved ); |
951 } | |
952 | |
953 return retv; | |
954 } | |
955 | |
956 static LPVOID | |
957 _fixup_address(PIMAGE_OPTIONAL_HEADER opt,int delta,LPVOID addr) { | |
958 if ( ((DWORD)addr>opt->ImageBase) && | |
959 ((DWORD)addr<opt->ImageBase+opt->SizeOfImage) | |
960 ) | |
961 | |
962 return (LPVOID)(((DWORD)addr)+delta); | |
963 else | |
964 | |
965 return addr; | |
966 } |