2 * Copyright 1994 Eric Youndale & Erik Bos
3 * Copyright 1995 Martin von Löwis
4 * Copyright 1996-98 Marcus Meissner
6 * based on Eric Youndale's pe-test and:
8 * ftp.microsoft.com:/pub/developer/MSDN/CD8/PEFILE.ZIP
10 * ftp.microsoft.com:/developr/MSDN/OctCD/PEFILE.ZIP
13 * Before you start changing something in this file be aware of the following:
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
24 * - No, you (usually) cannot use Linux mmap() to mmap() the images directly.
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. For x86 the sections 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 do this. However, this could be less
33 * difficult to support... (See mm/filemap.c).
34 * - All those function map things into a new addresspace. From the wrong
35 * process and the wrong thread. So calling other API functions will mess
36 * things up badly sometimes.
45 #include <sys/types.h>
63 static void PE_InitDLL(WINE_MODREF
*wm
, DWORD type
, LPVOID lpReserved
);
65 /* convert PE image VirtualAddress to Real Address */
66 #define RVA(x) ((unsigned int)load_addr+(unsigned int)(x))
68 #define AdjustPtr(ptr,delta) ((char *)(ptr) + (delta))
70 void dump_exports( HMODULE32 hModule
)
75 u_long
*function
,*functions
;
77 unsigned int load_addr
= hModule
;
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
);
85 Module
= (char*)RVA(pe_exports
->Name
);
86 TRACE(win32
,"*******EXPORT DATA*******\n");
87 TRACE(win32
,"Module name is %s, %ld functions, %ld names\n",
88 Module
, pe_exports
->NumberOfFunctions
, pe_exports
->NumberOfNames
);
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
);
94 TRACE(win32
," Ord RVA Addr Name\n" );
95 for (i
=0;i
<pe_exports
->NumberOfFunctions
;i
++, function
++)
97 if (!*function
) continue; /* No such function */
99 dbg_decl_str(win32
, 1024);
101 dsprintf(win32
,"%4ld %08lx %08x",
102 i
+ pe_exports
->Base
, *function
, RVA(*function
) );
103 /* Check if we have a name for it */
104 for (j
= 0; j
< pe_exports
->NumberOfNames
; j
++)
106 dsprintf(win32
, " %s", (char*)RVA(name
[j
]) );
107 if ((*function
>= rva_start
) && (*function
<= rva_end
))
108 dsprintf(win32
, " (forwarded -> %s)", (char *)RVA(*function
));
109 TRACE(win32
,"%s\n", dbg_str(win32
));
114 /* Look up the specified function or ordinal in the exportlist:
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
122 FARPROC32
PE_FindExportedFunction(
123 PDB32
*process
, /* [in] process context */
124 WINE_MODREF
*wm
, /* [in] WINE modreference */
125 LPCSTR funcName
) /* [in] function name */
129 u_char
** name
, *ename
;
131 PE_MODREF
*pem
= &(wm
->binfmt
.pe
);
132 IMAGE_EXPORT_DIRECTORY
*exports
= pem
->pe_export
;
133 unsigned int load_addr
= wm
->module
;
134 u_long rva_start
, rva_end
, addr
;
137 if (HIWORD(funcName
))
138 TRACE(win32
,"(%s)\n",funcName
);
140 TRACE(win32
,"(%d)\n",(int)funcName
);
142 /* Not a fatal problem, some apps do
143 * GetProcAddress(0,"RegisterPenApp") which triggers this
146 WARN(win32
,"Module %08x(%s)/MODREF %p doesn't have a exports table.\n",wm
->module
,wm
->modname
,pem
);
149 ordinal
= (u_short
*) RVA(exports
->AddressOfNameOrdinals
);
150 function
= (u_long
*) RVA(exports
->AddressOfFunctions
);
151 name
= (u_char
**) RVA(exports
->AddressOfNames
);
153 rva_start
= PE_HEADER(wm
->module
)->OptionalHeader
154 .DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
].VirtualAddress
;
155 rva_end
= rva_start
+ PE_HEADER(wm
->module
)->OptionalHeader
156 .DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
].Size
;
158 if (HIWORD(funcName
)) {
159 for(i
=0; i
<exports
->NumberOfNames
; i
++) {
160 ename
=(char*)RVA(*name
);
161 if(!strcmp(ename
,funcName
))
163 addr
= function
[*ordinal
];
164 if (!addr
) return NULL
;
165 if ((addr
< rva_start
) || (addr
>= rva_end
))
166 return (FARPROC32
)RVA(addr
);
167 forward
= (char *)RVA(addr
);
174 if (LOWORD(funcName
)-exports
->Base
> exports
->NumberOfFunctions
) {
175 TRACE(win32
," ordinal %d out of range!\n",
179 addr
= function
[(int)funcName
-exports
->Base
];
180 if (!addr
) return NULL
;
181 if ((addr
< rva_start
) || (addr
>= rva_end
))
182 return (FARPROC32
)RVA(addr
);
183 forward
= (char *)RVA(addr
);
189 char *end
= strchr(forward
, '.');
191 if (!end
) return NULL
;
192 assert(end
-forward
<256);
193 strncpy(module
, forward
, (end
- forward
));
194 module
[end
-forward
] = 0;
195 hMod
= MODULE_FindModule32(process
,module
);
197 return MODULE_GetProcAddress32( process
, hMod
, end
+ 1);
202 DWORD
fixup_imports (PDB32
*process
,WINE_MODREF
*wm
)
204 IMAGE_IMPORT_DESCRIPTOR
*pe_imp
;
207 int fixup_failed
= 0;
208 unsigned int load_addr
= wm
->module
;
212 assert(wm
->type
==MODULE32_PE
);
213 pem
= &(wm
->binfmt
.pe
);
215 modname
= (char*) RVA(pem
->pe_export
->Name
);
217 modname
= "<unknown>";
219 /* OK, now dump the import list */
220 TRACE(win32
, "Dumping imports list\n");
222 /* first, count the number of imported non-internal modules */
223 pe_imp
= pem
->pe_import
;
225 ERR(win32
, "no import directory????\n");
227 /* FIXME: should terminate on 0 Characteristics */
228 for (i
= 0; pe_imp
->Name
; pe_imp
++)
231 /* load the imported modules. They are automatically
232 * added to the modref list of the process.
235 /* FIXME: should terminate on 0 Characteristics */
236 for (i
= 0, pe_imp
= pem
->pe_import
; pe_imp
->Name
; pe_imp
++) {
239 char *name
= (char *) RVA(pe_imp
->Name
);
241 /* don't use MODULE_Load, Win32 creates new task differently */
242 res
= PE_LoadLibraryEx32A( name
, process
, 0, 0 );
243 if (res
<= (HMODULE32
) 32) {
246 /* Try with prepending the path of the current module */
247 if (GetModuleFileName32A( wm
->module
, buffer
, sizeof (buffer
))) {
250 if (!(p
= strrchr (buffer
, '\\')))
252 strcpy (p
+ 1, name
);
253 res
= PE_LoadLibraryEx32A( buffer
, process
, 0, 0 );
255 ERR(win32
,"cannot find the module just loaded!\n");
257 if (res
<= (HMODULE32
) 32) {
258 WARN (module
, "Module %s not found\n", name
);
263 if (xwm
->module
== res
)
268 /* It has been loaded *BEFORE* us, so we have to initialize
269 * it before us. We cannot just link in the xwm before wm,
270 * since xwm might reference more dlls which would be in the
271 * wrong order after that.
272 * Instead we link in wm right AFTER xwm, which should keep
273 * the correct order. (I am not 100% sure about that.)
275 /* unlink wm from chain */
276 ywm
= &(process
->modref_list
);
280 ywm
= &((*ywm
)->next
);
284 /* link wm directly AFTER xwm */
285 wm
->next
= xwm
->next
;
291 pe_imp
= pem
->pe_import
;
292 while (pe_imp
->Name
) {
294 IMAGE_IMPORT_BY_NAME
*pe_name
;
295 LPIMAGE_THUNK_DATA import_list
,thunk_list
;
296 HMODULE32 hImpModule
;
298 Module
= (char *) RVA(pe_imp
->Name
);
299 hImpModule
= MODULE_FindModule32(process
,Module
);
300 assert(hImpModule
); /* we have imported it, so it MUST be there */
301 TRACE(win32
, "%s\n", Module
);
303 /* FIXME: forwarder entries ... */
305 if (pe_imp
->u
.OriginalFirstThunk
!= 0) { /* original MS style */
306 TRACE(win32
, "Microsoft style imports used\n");
307 import_list
=(LPIMAGE_THUNK_DATA
) RVA(pe_imp
->u
.OriginalFirstThunk
);
308 thunk_list
= (LPIMAGE_THUNK_DATA
) RVA(pe_imp
->FirstThunk
);
310 while (import_list
->u1
.Ordinal
) {
311 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
)) {
312 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
314 TRACE(win32
, "--- Ordinal %s,%d\n", Module
, ordinal
);
315 thunk_list
->u1
.Function
=(LPDWORD
)MODULE_GetProcAddress32(
316 process
, hImpModule
, (LPCSTR
)ordinal
318 if (!thunk_list
->u1
.Function
) {
319 WARN(win32
,"No implementation for %s.%d, setting to NULL\n",
321 /* fixup_failed=1; */
323 } else { /* import by name */
324 pe_name
= (LPIMAGE_IMPORT_BY_NAME
)RVA(import_list
->u1
.AddressOfData
);
325 TRACE(win32
, "--- %s %s.%d\n", pe_name
->Name
, Module
, pe_name
->Hint
);
326 thunk_list
->u1
.Function
=(LPDWORD
)MODULE_GetProcAddress32(
327 process
, hImpModule
, pe_name
->Name
329 if (!thunk_list
->u1
.Function
) {
330 WARN(win32
,"No implementation for %s.%d(%s), setting to NULL\n",
331 Module
,pe_name
->Hint
,pe_name
->Name
);
332 /* fixup_failed=1; */
338 } else { /* Borland style */
339 TRACE(win32
, "Borland style imports used\n");
340 thunk_list
= (LPIMAGE_THUNK_DATA
) RVA(pe_imp
->FirstThunk
);
341 while (thunk_list
->u1
.Ordinal
) {
342 if (IMAGE_SNAP_BY_ORDINAL(thunk_list
->u1
.Ordinal
)) {
343 /* not sure about this branch, but it seems to work */
344 int ordinal
= IMAGE_ORDINAL(thunk_list
->u1
.Ordinal
);
346 TRACE(win32
,"--- Ordinal %s.%d\n",Module
,ordinal
);
347 thunk_list
->u1
.Function
=(LPDWORD
)MODULE_GetProcAddress32(
348 process
, hImpModule
, (LPCSTR
) ordinal
350 if (!thunk_list
->u1
.Function
) {
351 WARN(win32
, "No implementation for %s.%d, setting to NULL\n",
353 /* fixup_failed=1; */
356 pe_name
=(LPIMAGE_IMPORT_BY_NAME
) RVA(thunk_list
->u1
.AddressOfData
);
357 TRACE(win32
,"--- %s %s.%d\n",
358 pe_name
->Name
,Module
,pe_name
->Hint
);
359 thunk_list
->u1
.Function
=(LPDWORD
)MODULE_GetProcAddress32(
360 process
, hImpModule
, pe_name
->Name
362 if (!thunk_list
->u1
.Function
) {
363 WARN(win32
, "No implementation for %s.%d, setting to NULL\n",
364 Module
, pe_name
->Hint
);
365 /* fixup_failed=1; */
373 if (fixup_failed
) return 22;
377 static int calc_vma_size( HMODULE32 hModule
)
380 IMAGE_SECTION_HEADER
*pe_seg
= PE_SECTIONS(hModule
);
382 TRACE(win32
, "Dump of segment table\n");
383 TRACE(win32
, " Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
384 for (i
= 0; i
< PE_HEADER(hModule
)->FileHeader
.NumberOfSections
; i
++)
386 TRACE(win32
, "%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
388 pe_seg
->Misc
.VirtualSize
,
389 pe_seg
->VirtualAddress
,
390 pe_seg
->SizeOfRawData
,
391 pe_seg
->PointerToRawData
,
392 pe_seg
->PointerToRelocations
,
393 pe_seg
->PointerToLinenumbers
,
394 pe_seg
->NumberOfRelocations
,
395 pe_seg
->NumberOfLinenumbers
,
396 pe_seg
->Characteristics
);
397 vma_size
= MAX(vma_size
, pe_seg
->VirtualAddress
+pe_seg
->SizeOfRawData
);
403 static void do_relocations(WINE_MODREF
*wm
)
405 PE_MODREF
*pem
= &(wm
->binfmt
.pe
);
406 int delta
= wm
->module
- PE_HEADER(wm
->module
)->OptionalHeader
.ImageBase
;
407 unsigned int load_addr
= wm
->module
;
409 IMAGE_BASE_RELOCATION
*r
= pem
->pe_reloc
;
410 int hdelta
= (delta
>> 16) & 0xFFFF;
411 int ldelta
= delta
& 0xFFFF;
413 /* int reloc_size = */
418 while(r
->VirtualAddress
)
420 char *page
= (char*) RVA(r
->VirtualAddress
);
421 int count
= (r
->SizeOfBlock
- 8)/2;
423 TRACE(fixup
, "%x relocations for page %lx\n",
424 count
, r
->VirtualAddress
);
425 /* patching in reverse order */
428 int offset
= r
->TypeOffset
[i
] & 0xFFF;
429 int type
= r
->TypeOffset
[i
] >> 12;
430 TRACE(fixup
,"patching %x type %x\n", offset
, type
);
433 case IMAGE_REL_BASED_ABSOLUTE
: break;
434 case IMAGE_REL_BASED_HIGH
:
435 *(short*)(page
+offset
) += hdelta
;
437 case IMAGE_REL_BASED_LOW
:
438 *(short*)(page
+offset
) += ldelta
;
440 case IMAGE_REL_BASED_HIGHLOW
:
442 *(int*)(page
+offset
) += delta
;
444 { int h
=*(unsigned short*)(page
+offset
);
445 int l
=r
->TypeOffset
[++i
];
446 *(unsigned int*)(page
+ offset
) = (h
<<16) + l
+ delta
;
450 case IMAGE_REL_BASED_HIGHADJ
:
451 WARN(win32
, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
453 case IMAGE_REL_BASED_MIPS_JMPADDR
:
454 WARN(win32
, "Is this a MIPS machine ???\n");
457 WARN(win32
, "Unknown fixup type\n");
461 r
= (IMAGE_BASE_RELOCATION
*)((char*)r
+ r
->SizeOfBlock
);
469 /**********************************************************************
471 * Load one PE format DLL/EXE into memory
473 * Unluckily we can't just mmap the sections where we want them, for
474 * (at least) Linux does only support offsets which are page-aligned.
476 * BUT we have to map the whole image anyway, for Win32 programs sometimes
477 * want to access them. (HMODULE32 point to the start of it)
479 static HMODULE32
PE_LoadImage( HFILE32 hFile
)
484 /* map the PE file somewhere */
485 mapping
= CreateFileMapping32A( hFile
, NULL
, PAGE_READONLY
| SEC_COMMIT
,
489 WARN( win32
, "CreateFileMapping error %ld\n",
493 hModule
= (HMODULE32
)MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
494 CloseHandle( mapping
);
497 WARN( win32
, "PE_LoadImage: MapViewOfFile error %ld\n",
502 if (PE_HEADER(hModule
)->Signature
!= IMAGE_NT_SIGNATURE
)
504 WARN(win32
,"image doesn't have PE signature, but 0x%08lx\n",
505 PE_HEADER(hModule
)->Signature
);
509 if (PE_HEADER(hModule
)->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
511 MSG("Trying to load PE image for unsupported architecture (");
512 switch (PE_HEADER(hModule
)->FileHeader
.Machine
)
514 case IMAGE_FILE_MACHINE_UNKNOWN
: MSG("Unknown\n"); break;
515 case IMAGE_FILE_MACHINE_I860
: MSG("I860\n"); break;
516 case IMAGE_FILE_MACHINE_R3000
: MSG("R3000\n"); break;
517 case IMAGE_FILE_MACHINE_R4000
: MSG("R4000\n"); break;
518 case IMAGE_FILE_MACHINE_R10000
: MSG("R10000\n"); break;
519 case IMAGE_FILE_MACHINE_ALPHA
: MSG("Alpha\n"); break;
520 case IMAGE_FILE_MACHINE_POWERPC
: MSG("PowerPC\n"); break;
521 default: MSG("Unknown-%04x\n",
522 PE_HEADER(hModule
)->FileHeader
.Machine
); break;
529 UnmapViewOfFile( (LPVOID
)hModule
);
533 /**********************************************************************
534 * This maps a loaded PE dll into the address space of the specified process.
536 static BOOL32
PE_MapImage( HMODULE32
*phModule
, PDB32
*process
,
537 OFSTRUCT
*ofs
, DWORD flags
)
543 IMAGE_DATA_DIRECTORY dir
;
546 HMODULE32 hModule
= *phModule
;
548 IMAGE_SECTION_HEADER
*pe_seg
;
549 IMAGE_DOS_HEADER
*dos_header
= (IMAGE_DOS_HEADER
*)hModule
;
550 IMAGE_NT_HEADERS
*nt_header
= PE_HEADER(hModule
);
553 wm
= (WINE_MODREF
*)HeapAlloc(process
->heap
,HEAP_ZERO_MEMORY
,
555 wm
->type
= MODULE32_PE
;
556 pem
= &(wm
->binfmt
.pe
);
558 /* NOTE: fixup_imports takes care of the correct order */
559 wm
->next
= process
->modref_list
;
560 process
->modref_list
= wm
;
562 if (!(nt_header
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
564 if (process
->exe_modref
)
565 WARN(win32
,"overwriting old exe_modref... arrgh\n");
566 process
->exe_modref
= wm
;
569 load_addr
= nt_header
->OptionalHeader
.ImageBase
;
570 vma_size
= calc_vma_size( hModule
);
571 TRACE(win32
, "Load addr is %lx\n",load_addr
);
572 load_addr
= (DWORD
)VirtualAlloc( (void*)load_addr
, vma_size
,
573 MEM_RESERVE
| MEM_COMMIT
,
574 PAGE_EXECUTE_READWRITE
);
575 if (load_addr
== 0) {
576 load_addr
= (DWORD
)VirtualAlloc( NULL
, vma_size
,
577 MEM_RESERVE
| MEM_COMMIT
,
578 PAGE_EXECUTE_READWRITE
);
580 /* *phModule is the module32 entry in the NE_MODULE. We need to
581 * change it here, since it can get referenced by fixup_imports()
583 wm
->module
= *phModule
= (HMODULE32
)load_addr
;
585 TRACE(win32
, "Load addr is really %lx, range %x\n",
586 load_addr
, vma_size
);
588 TRACE(segment
, "Loading %s at %lx, range %x\n",
589 ofs
->szPathName
, load_addr
, vma_size
);
591 /* Store the NT header at the load addr
592 * (FIXME: should really use mmap)
594 *(IMAGE_DOS_HEADER
*)load_addr
= *dos_header
;
595 *(IMAGE_NT_HEADERS
*)(load_addr
+ dos_header
->e_lfanew
) = *nt_header
;
597 pe_seg
= PE_SECTIONS(hModule
);
598 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++, pe_seg
++)
600 /* memcpy only non-BSS segments */
601 /* FIXME: this should be done by mmap(..MAP_PRIVATE|MAP_FIXED..)
602 * but it is not possible for (at least) Linux needs
603 * a page-aligned offset.
605 if(!(pe_seg
->Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
))
606 memcpy((char*)RVA(pe_seg
->VirtualAddress
),
607 (char*)(hModule
+ pe_seg
->PointerToRawData
),
608 pe_seg
->SizeOfRawData
611 result
= RVA (pe_seg
->VirtualAddress
);
613 /* not needed, memory is zero */
614 if(strcmp(pe_seg
->Name
, ".bss") == 0)
615 memset((void *)result
, 0,
616 pe_seg
->Misc
.VirtualSize
?
617 pe_seg
->Misc
.VirtualSize
:
618 pe_seg
->SizeOfRawData
);
621 if(strcmp(pe_seg
->Name
, ".idata") == 0)
622 pem
->pe_import
= (LPIMAGE_IMPORT_DESCRIPTOR
) result
;
624 if(strcmp(pe_seg
->Name
, ".edata") == 0)
625 pem
->pe_export
= (LPIMAGE_EXPORT_DIRECTORY
) result
;
627 if(strcmp(pe_seg
->Name
, ".rsrc") == 0)
628 pem
->pe_resource
= (LPIMAGE_RESOURCE_DIRECTORY
) result
;
630 if(strcmp(pe_seg
->Name
, ".reloc") == 0)
631 pem
->pe_reloc
= (LPIMAGE_BASE_RELOCATION
) result
;
634 /* There is word that the actual loader does not care about the
635 section names, and only goes for the DataDirectory */
636 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
];
639 if(pem
->pe_export
&& (int)pem
->pe_export
!=RVA(dir
.VirtualAddress
))
640 WARN(win32
,"wrong export directory??\n");
641 /* always trust the directory */
642 pem
->pe_export
= (LPIMAGE_EXPORT_DIRECTORY
) RVA(dir
.VirtualAddress
);
645 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
];
649 if(pem->pe_import && (int)pem->pe_import!=RVA(dir.VirtualAddress))
650 WARN(win32,"wrong import directory??\n");
652 pem
->pe_import
= (LPIMAGE_IMPORT_DESCRIPTOR
) RVA(dir
.VirtualAddress
);
655 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
];
658 if(pem
->pe_resource
&& (int)pem
->pe_resource
!=RVA(dir
.VirtualAddress
))
659 WARN(win32
,"wrong resource directory??\n");
660 pem
->pe_resource
= (LPIMAGE_RESOURCE_DIRECTORY
) RVA(dir
.VirtualAddress
);
663 if(nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXCEPTION
].Size
)
664 FIXME(win32
,"Exception directory ignored\n");
666 if(nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
)
667 FIXME(win32
,"Security directory ignored\n");
671 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
674 if(pem
->pe_reloc
&& (int)pem
->pe_reloc
!= RVA(dir
.VirtualAddress
))
675 WARN(win32
,"wrong relocation list??\n");
676 pem
->pe_reloc
= (void *) RVA(dir
.VirtualAddress
);
679 if(nt_header
->OptionalHeader
.DataDirectory
680 [IMAGE_DIRECTORY_ENTRY_COPYRIGHT
].Size
)
681 FIXME(win32
,"Copyright string ignored\n");
683 if(nt_header
->OptionalHeader
.DataDirectory
684 [IMAGE_DIRECTORY_ENTRY_GLOBALPTR
].Size
)
685 FIXME(win32
,"Global Pointer (MIPS) ignored\n");
687 if(nt_header
->OptionalHeader
.DataDirectory
688 [IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
].Size
)
689 FIXME(win32
,"Load Configuration directory ignored\n");
691 if(nt_header
->OptionalHeader
.DataDirectory
692 [IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
].Size
)
693 FIXME(win32
,"Bound Import directory ignored\n");
695 if(nt_header
->OptionalHeader
.DataDirectory
696 [IMAGE_DIRECTORY_ENTRY_IAT
].Size
)
697 FIXME(win32
,"Import Address Table directory ignored\n");
698 if(nt_header
->OptionalHeader
.DataDirectory
[13].Size
)
699 FIXME(win32
,"Unknown directory 13 ignored\n");
700 if(nt_header
->OptionalHeader
.DataDirectory
[14].Size
)
701 FIXME(win32
,"Unknown directory 14 ignored\n");
702 if(nt_header
->OptionalHeader
.DataDirectory
[15].Size
)
703 FIXME(win32
,"Unknown directory 15 ignored\n");
705 if(pem
->pe_reloc
) do_relocations(wm
);
707 dump_exports(wm
->module
);
709 wm
->modname
= HEAP_strdupA(process
->heap
,0,(char*)RVA(pem
->pe_export
->Name
));
711 /* try to find out the name from the OFSTRUCT */
713 modname
= s
= ofs
->szPathName
;
714 while ((s
=strchr(modname
,'\\')))
716 if ((s
=strchr(modname
,'.')))
718 wm
->modname
= HEAP_strdupA(process
->heap
,0,modname
);
721 if (fixup_imports(process
,wm
)) {
724 /* remove entry from modref chain */
725 xwm
= &(process
->modref_list
);
731 xwm
= &((*xwm
)->next
);
733 /* FIXME: there are several more dangling references
734 * left. Including dlls loaded by this dll before the
735 * failed one. Unrolling is rather difficult with the
736 * current structure and we can leave it them lying
737 * around with no problems, so we don't care
743 /* Now that we got everything at the right address,
744 * we can unmap the previous module */
745 UnmapViewOfFile( (LPVOID
)hModule
);
749 /******************************************************************************
750 * The PE Library Loader frontend.
751 * FIXME: handle the flags.
752 * internal module handling should be made better here (and in builtin.c)
754 HMODULE32
PE_LoadLibraryEx32A (LPCSTR name
, PDB32
*process
,
755 HFILE32 hFile
, DWORD flags
)
762 if ((hModule
= MODULE_FindModule32( process
, name
))) {
764 pModule
= MODULE_GetPtr32(hModule
);
765 for (wm
= process
->modref_list
;wm
;wm
=wm
->next
)
766 if (wm
->module
== hModule
) {
770 /* Since MODULE_FindModule32 uses the modref chain too, the
771 * module MUST have been found above. If not, something has gone
776 /* try to load builtin, enabled modules first */
777 if ((hModule
= BUILTIN32_LoadModule( name
, FALSE
, process
)))
780 /* try to load the specified dll/exe */
781 if (HFILE_ERROR32
==(hFile
=OpenFile32(name
,&ofs
,OF_READ
))) {
782 /* Now try the built-in even if disabled */
783 if ((hModule
= BUILTIN32_LoadModule( name
, TRUE
, process
))) {
784 fprintf( stderr
, "Warning: could not load external DLL '%s', using built-in module.\n", name
);
789 if ((hModule
= MODULE_CreateDummyModule( &ofs
)) < 32) {
793 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
794 pModule
->flags
= NE_FFLAGS_WIN32
;
795 pModule
->module32
= PE_LoadImage( hFile
);
796 CloseHandle( hFile
);
797 if (pModule
->module32
< 32)
799 FreeLibrary32( hModule
);
800 return 21; /* FIXME: probably 0 */
803 /* (possible) recursion */
804 if (!PE_MapImage( &(pModule
->module32
), process
, &ofs
,flags
)) {
805 /* FIXME: should free this module and its referenced ones */
808 return pModule
->module32
;
811 /*****************************************************************************
812 * Load the PE main .EXE. All other loading is done by PE_LoadLibraryEx32A
813 * FIXME: this function should use PE_LoadLibraryEx32A, but currently can't
814 * due to the PROCESS_Create stuff.
816 HINSTANCE16
PE_LoadModule( HFILE32 hFile
, OFSTRUCT
*ofs
, LPCSTR cmd_line
,
817 LPCSTR env
, UINT16 show_cmd
)
821 HINSTANCE16 hInstance
;
823 THDB
*thdb
= THREAD_Current();
825 if ((hModule16
= MODULE_CreateDummyModule( ofs
)) < 32) return hModule16
;
826 pModule
= (NE_MODULE
*)GlobalLock16( hModule16
);
827 pModule
->flags
= NE_FFLAGS_WIN32
;
829 pModule
->module32
= hModule32
= PE_LoadImage( hFile
);
830 if (hModule32
< 32) return 21;
832 hInstance
= MODULE_CreateInstance( hModule16
, NULL
, (cmd_line
== NULL
) );
834 !(PE_HEADER(hModule32
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
836 PDB32
*pdb
= PROCESS_Create( pModule
, cmd_line
, env
,
837 hInstance
, 0, show_cmd
);
838 TDB
*pTask
= (TDB
*)GlobalLock16( pdb
->task
);
841 if (!PE_MapImage( &(pModule
->module32
), thdb
->process
, ofs
, 0 ))
843 /* FIXME: should destroy the task created and free referenced stuff */
846 /* FIXME: Yuck. Is there no other good place to do that? */
851 /*********************************************************************
852 * PE_UnloadImage [internal]
854 int PE_UnloadImage( HMODULE32 hModule
)
856 FIXME(win32
,"stub.\n");
857 /* free resources, image, unmap */
861 /* Called if the library is loaded or freed.
862 * NOTE: if a thread attaches a DLL, the current thread will only do
863 * DLL_PROCESS_ATTACH. Only new created threads do DLL_THREAD_ATTACH
866 static void PE_InitDLL(WINE_MODREF
*wm
, DWORD type
,LPVOID lpReserved
)
868 if (wm
->type
!=MODULE32_PE
)
870 if (type
==DLL_PROCESS_ATTACH
)
871 wm
->binfmt
.pe
.flags
|= PE_MODREF_PROCESS_ATTACHED
;
873 /* DLL_ATTACH_PROCESS:
874 * lpreserved is NULL for dynamic loads, not-NULL for static loads
875 * DLL_DETACH_PROCESS:
876 * lpreserved is NULL if called by FreeLibrary, not-NULL otherwise
877 * the SDK doesn't mention anything for DLL_THREAD_*
880 /* Is this a library? And has it got an entrypoint? */
881 if ((PE_HEADER(wm
->module
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) &&
882 (PE_HEADER(wm
->module
)->OptionalHeader
.AddressOfEntryPoint
)
884 FARPROC32 entry
= (FARPROC32
)RVA_PTR( wm
->module
,
885 OptionalHeader
.AddressOfEntryPoint
);
886 TRACE(relay
, "CallTo32(entryproc=%p,module=%08x,type=%ld,res=%p)\n",
887 entry
, wm
->module
, type
, lpReserved
);
888 entry( wm
->module
, type
, lpReserved
);
892 /* Call the DLLentry function of all dlls used by that process.
893 * (NOTE: this may recursively call this function (if a library calls
894 * LoadLibrary) ... but it won't matter)
896 void PE_InitializeDLLs(PDB32
*process
,DWORD type
,LPVOID lpReserved
) {
899 for (wm
= process
->modref_list
;wm
;wm
=wm
->next
) {
900 PE_MODREF
*pem
= NULL
;
901 if (wm
->type
!=MODULE32_PE
)
903 pem
= &(wm
->binfmt
.pe
);
904 if (pem
->flags
& PE_MODREF_NO_DLL_CALLS
)
906 if (type
==DLL_PROCESS_ATTACH
) {
907 if (pem
->flags
& PE_MODREF_PROCESS_ATTACHED
)
910 PE_InitDLL( wm
, type
, lpReserved
);
914 void PE_InitTls(THDB
*thdb
)
918 IMAGE_NT_HEADERS
*peh
;
921 LPIMAGE_TLS_DIRECTORY pdir
;
922 PDB32
*pdb
= thdb
->process
;
925 for (wm
= pdb
->modref_list
;wm
;wm
=wm
->next
) {
926 if (wm
->type
!=MODULE32_PE
)
928 pem
= &(wm
->binfmt
.pe
);
929 peh
= PE_HEADER(wm
->module
);
930 delta
= wm
->module
- peh
->OptionalHeader
.ImageBase
;
931 if (!peh
->OptionalHeader
.DataDirectory
[IMAGE_FILE_THREAD_LOCAL_STORAGE
].VirtualAddress
)
933 pdir
= (LPVOID
)(wm
->module
+ peh
->OptionalHeader
.
934 DataDirectory
[IMAGE_FILE_THREAD_LOCAL_STORAGE
].VirtualAddress
);
937 if (!(pem
->flags
& PE_MODREF_TLS_ALLOCED
)) {
938 pem
->tlsindex
= THREAD_TlsAlloc(thdb
);
939 *(LPDWORD
)AdjustPtr(pdir
->AddressOfIndex
,delta
)
942 pem
->flags
|= PE_MODREF_TLS_ALLOCED
;
943 datasize
= pdir
->EndAddressOfRawData
-pdir
->StartAddressOfRawData
;
944 size
= datasize
+ pdir
->SizeOfZeroFill
;
945 mem
=VirtualAlloc(0,size
,MEM_RESERVE
|MEM_COMMIT
,PAGE_READWRITE
);
947 AdjustPtr(pdir
->StartAddressOfRawData
,delta
),
950 /* don't use TlsSetValue, we are in the wrong thread */
951 if (pdir
->AddressOfCallBacks
) {
952 LPIMAGE_TLS_CALLBACK
*cbs
=
953 (LPIMAGE_TLS_CALLBACK
*)
954 AdjustPtr(pdir
->AddressOfCallBacks
, delta
);
957 FIXME(win32
, "TLS Callbacks aren't going to be called\n");
960 thdb
->tls_array
[pem
->tlsindex
] = mem
;
964 /****************************************************************************
965 * DisableThreadLibraryCalls (KERNEL32.74)
966 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
968 BOOL32 WINAPI
DisableThreadLibraryCalls(HMODULE32 hModule
)
972 for (wm
=PROCESS_Current()->modref_list
;wm
;wm
=wm
->next
)
973 if ((wm
->module
== hModule
) && (wm
->type
==MODULE32_PE
))
974 wm
->binfmt
.pe
.flags
|=PE_MODREF_NO_DLL_CALLS
;