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