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.
44 #include <sys/types.h>
62 static void PE_InitDLL(WINE_MODREF
*wm
, DWORD type
, LPVOID lpReserved
);
64 /* convert PE image VirtualAddress to Real Address */
65 #define RVA(x) ((unsigned int)load_addr+(unsigned int)(x))
67 #define AdjustPtr(ptr,delta) ((char *)(ptr) + (delta))
69 void dump_exports( HMODULE32 hModule
)
74 u_long
*function
,*functions
;
76 unsigned int load_addr
= hModule
;
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
);
84 Module
= (char*)RVA(pe_exports
->Name
);
85 TRACE(win32
,"*******EXPORT DATA*******\n");
86 TRACE(win32
,"Module name is %s, %ld functions, %ld names\n",
87 Module
, pe_exports
->NumberOfFunctions
, pe_exports
->NumberOfNames
);
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
);
93 TRACE(win32
," Ord RVA Addr Name\n" );
94 for (i
=0;i
<pe_exports
->NumberOfFunctions
;i
++, function
++)
96 if (!*function
) continue; /* No such function */
98 dbg_decl_str(win32
, 1024);
100 dsprintf(win32
,"%4ld %08lx %08x",
101 i
+ pe_exports
->Base
, *function
, RVA(*function
) );
102 /* Check if we have a name for it */
103 for (j
= 0; j
< pe_exports
->NumberOfNames
; j
++)
105 dsprintf(win32
, " %s", (char*)RVA(name
[j
]) );
106 if ((*function
>= rva_start
) && (*function
<= rva_end
))
107 dsprintf(win32
, " (forwarded -> %s)", (char *)RVA(*function
));
108 TRACE(win32
,"%s\n", dbg_str(win32
));
113 /* Look up the specified function or ordinal in the exportlist:
115 * - look up the name in the Name list.
116 * - look up the ordinal with that index.
117 * - use the ordinal as offset into the functionlist
118 * If it is a ordinal:
119 * - use ordinal-pe_export->Base as offset into the functionlist
121 FARPROC32
PE_FindExportedFunction(
122 PDB32
*process
, /* [in] process context */
123 WINE_MODREF
*wm
, /* [in] WINE modreference */
124 LPCSTR funcName
) /* [in] function name */
128 u_char
** name
, *ename
;
130 PE_MODREF
*pem
= &(wm
->binfmt
.pe
);
131 IMAGE_EXPORT_DIRECTORY
*exports
= pem
->pe_export
;
132 unsigned int load_addr
= wm
->module
;
133 u_long rva_start
, rva_end
, addr
;
136 if (HIWORD(funcName
))
137 TRACE(win32
,"(%s)\n",funcName
);
139 TRACE(win32
,"(%d)\n",(int)funcName
);
141 /* Not a fatal problem, some apps do
142 * GetProcAddress(0,"RegisterPenApp") which triggers this
145 WARN(win32
,"Module %08x(%s)/MODREF %p doesn't have a exports table.\n",wm
->module
,wm
->modname
,pem
);
148 ordinal
= (u_short
*) RVA(exports
->AddressOfNameOrdinals
);
149 function
= (u_long
*) RVA(exports
->AddressOfFunctions
);
150 name
= (u_char
**) RVA(exports
->AddressOfNames
);
152 rva_start
= PE_HEADER(wm
->module
)->OptionalHeader
153 .DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
].VirtualAddress
;
154 rva_end
= rva_start
+ PE_HEADER(wm
->module
)->OptionalHeader
155 .DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
].Size
;
157 if (HIWORD(funcName
)) {
158 for(i
=0; i
<exports
->NumberOfNames
; i
++) {
159 ename
=(char*)RVA(*name
);
160 if(!strcmp(ename
,funcName
))
162 addr
= function
[*ordinal
];
163 if (!addr
) return NULL
;
164 if ((addr
< rva_start
) || (addr
>= rva_end
))
165 return (FARPROC32
)RVA(addr
);
166 forward
= (char *)RVA(addr
);
173 if (LOWORD(funcName
)-exports
->Base
> exports
->NumberOfFunctions
) {
174 TRACE(win32
," ordinal %d out of range!\n",
178 addr
= function
[(int)funcName
-exports
->Base
];
179 if (!addr
) return NULL
;
180 if ((addr
< rva_start
) || (addr
>= rva_end
))
181 return (FARPROC32
)RVA(addr
);
182 forward
= (char *)RVA(addr
);
188 char *end
= strchr(forward
, '.');
190 if (!end
) return NULL
;
191 assert(end
-forward
<256);
192 strncpy(module
, forward
, (end
- forward
));
193 module
[end
-forward
] = 0;
194 hMod
= MODULE_FindModule32(process
,module
);
196 return MODULE_GetProcAddress32( process
, hMod
, end
+ 1);
201 DWORD
fixup_imports (PDB32
*process
,WINE_MODREF
*wm
)
203 IMAGE_IMPORT_DESCRIPTOR
*pe_imp
;
206 int fixup_failed
= 0;
207 unsigned int load_addr
= wm
->module
;
211 assert(wm
->type
==MODULE32_PE
);
212 pem
= &(wm
->binfmt
.pe
);
214 modname
= (char*) RVA(pem
->pe_export
->Name
);
216 modname
= "<unknown>";
218 /* OK, now dump the import list */
219 TRACE(win32
, "Dumping imports list\n");
221 /* first, count the number of imported non-internal modules */
222 pe_imp
= pem
->pe_import
;
224 ERR(win32
, "no import directory????\n");
226 /* FIXME: should terminate on 0 Characteristics */
227 for (i
= 0; pe_imp
->Name
; pe_imp
++)
230 /* load the imported modules. They are automatically
231 * added to the modref list of the process.
234 /* FIXME: should terminate on 0 Characteristics */
235 for (i
= 0, pe_imp
= pem
->pe_import
; pe_imp
->Name
; pe_imp
++) {
238 char *name
= (char *) RVA(pe_imp
->Name
);
240 /* don't use MODULE_Load, Win32 creates new task differently */
241 res
= PE_LoadLibraryEx32A( name
, process
, 0, 0 );
242 if (res
<= (HMODULE32
) 32) {
243 char *p
,buffer
[2000];
245 /* GetModuleFileName would use the wrong process, so don't use it */
246 strcpy(buffer
,wm
->shortname
);
247 if (!(p
= strrchr (buffer
, '\\')))
249 strcpy (p
+ 1, name
);
250 res
= PE_LoadLibraryEx32A( buffer
, process
, 0, 0 );
252 if (res
<= (HMODULE32
) 32) {
253 ERR (module
, "Module %s not found\n", name
);
258 if (xwm
->module
== res
)
263 /* It has been loaded *BEFORE* us, so we have to initialize
264 * it before us. We cannot just link in the xwm before wm,
265 * since xwm might reference more dlls which would be in the
266 * wrong order after that.
267 * Instead we link in wm right AFTER xwm, which should keep
268 * the correct order. (I am not 100% sure about that.)
270 /* unlink wm from chain */
271 ywm
= &(process
->modref_list
);
275 ywm
= &((*ywm
)->next
);
279 /* link wm directly AFTER xwm */
280 wm
->next
= xwm
->next
;
286 pe_imp
= pem
->pe_import
;
287 while (pe_imp
->Name
) {
289 IMAGE_IMPORT_BY_NAME
*pe_name
;
290 LPIMAGE_THUNK_DATA import_list
,thunk_list
;
291 HMODULE32 hImpModule
;
293 Module
= (char *) RVA(pe_imp
->Name
);
294 hImpModule
= MODULE_FindModule32(process
,Module
);
295 assert(hImpModule
); /* we have imported it, so it MUST be there */
296 TRACE(win32
, "%s\n", Module
);
298 /* FIXME: forwarder entries ... */
300 if (pe_imp
->u
.OriginalFirstThunk
!= 0) { /* original MS style */
301 TRACE(win32
, "Microsoft style imports used\n");
302 import_list
=(LPIMAGE_THUNK_DATA
) RVA(pe_imp
->u
.OriginalFirstThunk
);
303 thunk_list
= (LPIMAGE_THUNK_DATA
) RVA(pe_imp
->FirstThunk
);
305 while (import_list
->u1
.Ordinal
) {
306 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
)) {
307 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
309 TRACE(win32
, "--- Ordinal %s,%d\n", Module
, ordinal
);
310 thunk_list
->u1
.Function
=(LPDWORD
)MODULE_GetProcAddress32(
311 process
, hImpModule
, (LPCSTR
)ordinal
313 if (!thunk_list
->u1
.Function
) {
314 WARN(win32
,"No implementation for %s.%d, setting to NULL\n",
316 /* fixup_failed=1; */
318 } else { /* import by name */
319 pe_name
= (LPIMAGE_IMPORT_BY_NAME
)RVA(import_list
->u1
.AddressOfData
);
320 TRACE(win32
, "--- %s %s.%d\n", pe_name
->Name
, Module
, pe_name
->Hint
);
321 thunk_list
->u1
.Function
=(LPDWORD
)MODULE_GetProcAddress32(
322 process
, hImpModule
, pe_name
->Name
324 if (!thunk_list
->u1
.Function
) {
325 WARN(win32
,"No implementation for %s.%d(%s), setting to NULL\n",
326 Module
,pe_name
->Hint
,pe_name
->Name
);
327 /* fixup_failed=1; */
333 } else { /* Borland style */
334 TRACE(win32
, "Borland style imports used\n");
335 thunk_list
= (LPIMAGE_THUNK_DATA
) RVA(pe_imp
->FirstThunk
);
336 while (thunk_list
->u1
.Ordinal
) {
337 if (IMAGE_SNAP_BY_ORDINAL(thunk_list
->u1
.Ordinal
)) {
338 /* not sure about this branch, but it seems to work */
339 int ordinal
= IMAGE_ORDINAL(thunk_list
->u1
.Ordinal
);
341 TRACE(win32
,"--- Ordinal %s.%d\n",Module
,ordinal
);
342 thunk_list
->u1
.Function
=(LPDWORD
)MODULE_GetProcAddress32(
343 process
, hImpModule
, (LPCSTR
) ordinal
345 if (!thunk_list
->u1
.Function
) {
346 WARN(win32
, "No implementation for %s.%d, setting to NULL\n",
348 /* fixup_failed=1; */
351 pe_name
=(LPIMAGE_IMPORT_BY_NAME
) RVA(thunk_list
->u1
.AddressOfData
);
352 TRACE(win32
,"--- %s %s.%d\n",
353 pe_name
->Name
,Module
,pe_name
->Hint
);
354 thunk_list
->u1
.Function
=(LPDWORD
)MODULE_GetProcAddress32(
355 process
, hImpModule
, pe_name
->Name
357 if (!thunk_list
->u1
.Function
) {
358 WARN(win32
, "No implementation for %s.%d, setting to NULL\n",
359 Module
, pe_name
->Hint
);
360 /* fixup_failed=1; */
368 if (fixup_failed
) return 22;
372 static int calc_vma_size( HMODULE32 hModule
)
375 IMAGE_SECTION_HEADER
*pe_seg
= PE_SECTIONS(hModule
);
377 TRACE(win32
, "Dump of segment table\n");
378 TRACE(win32
, " Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
379 for (i
= 0; i
< PE_HEADER(hModule
)->FileHeader
.NumberOfSections
; i
++)
381 TRACE(win32
, "%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
383 pe_seg
->Misc
.VirtualSize
,
384 pe_seg
->VirtualAddress
,
385 pe_seg
->SizeOfRawData
,
386 pe_seg
->PointerToRawData
,
387 pe_seg
->PointerToRelocations
,
388 pe_seg
->PointerToLinenumbers
,
389 pe_seg
->NumberOfRelocations
,
390 pe_seg
->NumberOfLinenumbers
,
391 pe_seg
->Characteristics
);
392 vma_size
= MAX(vma_size
, pe_seg
->VirtualAddress
+pe_seg
->SizeOfRawData
);
398 static void do_relocations(WINE_MODREF
*wm
)
400 PE_MODREF
*pem
= &(wm
->binfmt
.pe
);
401 int delta
= wm
->module
- PE_HEADER(wm
->module
)->OptionalHeader
.ImageBase
;
402 unsigned int load_addr
= wm
->module
;
404 IMAGE_BASE_RELOCATION
*r
= pem
->pe_reloc
;
405 int hdelta
= (delta
>> 16) & 0xFFFF;
406 int ldelta
= delta
& 0xFFFF;
408 /* int reloc_size = */
413 while(r
->VirtualAddress
)
415 char *page
= (char*) RVA(r
->VirtualAddress
);
416 int count
= (r
->SizeOfBlock
- 8)/2;
418 TRACE(fixup
, "%x relocations for page %lx\n",
419 count
, r
->VirtualAddress
);
420 /* patching in reverse order */
423 int offset
= r
->TypeOffset
[i
] & 0xFFF;
424 int type
= r
->TypeOffset
[i
] >> 12;
425 TRACE(fixup
,"patching %x type %x\n", offset
, type
);
428 case IMAGE_REL_BASED_ABSOLUTE
: break;
429 case IMAGE_REL_BASED_HIGH
:
430 *(short*)(page
+offset
) += hdelta
;
432 case IMAGE_REL_BASED_LOW
:
433 *(short*)(page
+offset
) += ldelta
;
435 case IMAGE_REL_BASED_HIGHLOW
:
437 *(int*)(page
+offset
) += delta
;
439 { int h
=*(unsigned short*)(page
+offset
);
440 int l
=r
->TypeOffset
[++i
];
441 *(unsigned int*)(page
+ offset
) = (h
<<16) + l
+ delta
;
445 case IMAGE_REL_BASED_HIGHADJ
:
446 WARN(win32
, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
448 case IMAGE_REL_BASED_MIPS_JMPADDR
:
449 WARN(win32
, "Is this a MIPS machine ???\n");
452 WARN(win32
, "Unknown fixup type\n");
456 r
= (IMAGE_BASE_RELOCATION
*)((char*)r
+ r
->SizeOfBlock
);
464 /**********************************************************************
466 * Load one PE format DLL/EXE into memory
468 * Unluckily we can't just mmap the sections where we want them, for
469 * (at least) Linux does only support offsets which are page-aligned.
471 * BUT we have to map the whole image anyway, for Win32 programs sometimes
472 * want to access them. (HMODULE32 point to the start of it)
474 static HMODULE32
PE_LoadImage( HFILE32 hFile
)
479 /* map the PE file somewhere */
480 mapping
= CreateFileMapping32A( hFile
, NULL
, PAGE_READONLY
| SEC_COMMIT
,
484 WARN( win32
, "CreateFileMapping error %ld\n",
488 hModule
= (HMODULE32
)MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
489 CloseHandle( mapping
);
492 WARN( win32
, "PE_LoadImage: MapViewOfFile error %ld\n",
497 if (PE_HEADER(hModule
)->Signature
!= IMAGE_NT_SIGNATURE
)
499 WARN(win32
,"image doesn't have PE signature, but 0x%08lx\n",
500 PE_HEADER(hModule
)->Signature
);
504 if (PE_HEADER(hModule
)->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
506 MSG("Trying to load PE image for unsupported architecture (");
507 switch (PE_HEADER(hModule
)->FileHeader
.Machine
)
509 case IMAGE_FILE_MACHINE_UNKNOWN
: MSG("Unknown\n"); break;
510 case IMAGE_FILE_MACHINE_I860
: MSG("I860\n"); break;
511 case IMAGE_FILE_MACHINE_R3000
: MSG("R3000\n"); break;
512 case IMAGE_FILE_MACHINE_R4000
: MSG("R4000\n"); break;
513 case IMAGE_FILE_MACHINE_R10000
: MSG("R10000\n"); break;
514 case IMAGE_FILE_MACHINE_ALPHA
: MSG("Alpha\n"); break;
515 case IMAGE_FILE_MACHINE_POWERPC
: MSG("PowerPC\n"); break;
516 default: MSG("Unknown-%04x\n",
517 PE_HEADER(hModule
)->FileHeader
.Machine
); break;
524 UnmapViewOfFile( (LPVOID
)hModule
);
528 /**********************************************************************
529 * This maps a loaded PE dll into the address space of the specified process.
531 static BOOL32
PE_MapImage( PDB32
*process
,WINE_MODREF
*wm
, OFSTRUCT
*ofs
, DWORD flags
)
536 IMAGE_DATA_DIRECTORY dir
;
539 HMODULE32 hModule
= wm
->module
;
541 IMAGE_SECTION_HEADER
*pe_seg
;
542 IMAGE_DOS_HEADER
*dos_header
= (IMAGE_DOS_HEADER
*)hModule
;
543 IMAGE_NT_HEADERS
*nt_header
= PE_HEADER(hModule
);
545 pem
= &(wm
->binfmt
.pe
);
547 result
= GetLongPathName32A(ofs
->szPathName
,NULL
,0);
548 wm
->longname
= (char*)HeapAlloc(process
->heap
,0,result
+1);
549 GetLongPathName32A(ofs
->szPathName
,wm
->longname
,result
+1);
551 wm
->shortname
= HEAP_strdupA(process
->heap
,0,ofs
->szPathName
);
553 if (!(nt_header
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
555 if (process
->exe_modref
)
556 FIXME(win32
,"overwriting old exe_modref... arrgh\n");
557 process
->exe_modref
= wm
;
560 load_addr
= nt_header
->OptionalHeader
.ImageBase
;
561 vma_size
= calc_vma_size( hModule
);
562 TRACE(win32
, "Load addr is %lx\n",load_addr
);
563 load_addr
= (DWORD
)VirtualAlloc( (void*)load_addr
, vma_size
,
564 MEM_RESERVE
| MEM_COMMIT
,
565 PAGE_EXECUTE_READWRITE
);
566 if (load_addr
== 0) {
567 load_addr
= (DWORD
)VirtualAlloc( NULL
, vma_size
,
568 MEM_RESERVE
| MEM_COMMIT
,
569 PAGE_EXECUTE_READWRITE
);
571 /* NOTE: this changes a value in the process modref chain, which can
572 * be accessed independently from this function
574 wm
->module
= (HMODULE32
)load_addr
;
576 TRACE(win32
, "Load addr is really %lx, range %x\n",
577 load_addr
, vma_size
);
579 TRACE(segment
, "Loading %s at %lx, range %x\n",
580 ofs
->szPathName
, load_addr
, vma_size
);
582 /* Store the NT header at the load addr
583 * (FIXME: should really use mmap)
585 *(IMAGE_DOS_HEADER
*)load_addr
= *dos_header
;
586 *(IMAGE_NT_HEADERS
*)(load_addr
+ dos_header
->e_lfanew
) = *nt_header
;
588 pe_seg
= PE_SECTIONS(hModule
);
589 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++, pe_seg
++)
591 /* memcpy only non-BSS segments */
592 /* FIXME: this should be done by mmap(..MAP_PRIVATE|MAP_FIXED..)
593 * but it is not possible for (at least) Linux needs
594 * a page-aligned offset.
596 if(!(pe_seg
->Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
))
597 memcpy((char*)RVA(pe_seg
->VirtualAddress
),
598 (char*)(hModule
+ pe_seg
->PointerToRawData
),
599 pe_seg
->SizeOfRawData
602 result
= RVA (pe_seg
->VirtualAddress
);
604 /* not needed, memory is zero */
605 if(strcmp(pe_seg
->Name
, ".bss") == 0)
606 memset((void *)result
, 0,
607 pe_seg
->Misc
.VirtualSize
?
608 pe_seg
->Misc
.VirtualSize
:
609 pe_seg
->SizeOfRawData
);
612 if(strcmp(pe_seg
->Name
, ".idata") == 0)
613 pem
->pe_import
= (LPIMAGE_IMPORT_DESCRIPTOR
) result
;
615 if(strcmp(pe_seg
->Name
, ".edata") == 0)
616 pem
->pe_export
= (LPIMAGE_EXPORT_DIRECTORY
) result
;
618 if(strcmp(pe_seg
->Name
, ".rsrc") == 0)
619 pem
->pe_resource
= (LPIMAGE_RESOURCE_DIRECTORY
) result
;
621 if(strcmp(pe_seg
->Name
, ".reloc") == 0)
622 pem
->pe_reloc
= (LPIMAGE_BASE_RELOCATION
) result
;
625 /* There is word that the actual loader does not care about the
626 section names, and only goes for the DataDirectory */
627 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
];
630 if(pem
->pe_export
&& (int)pem
->pe_export
!=RVA(dir
.VirtualAddress
))
631 WARN(win32
,"wrong export directory??\n");
632 /* always trust the directory */
633 pem
->pe_export
= (LPIMAGE_EXPORT_DIRECTORY
) RVA(dir
.VirtualAddress
);
636 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
];
640 if(pem->pe_import && (int)pem->pe_import!=RVA(dir.VirtualAddress))
641 WARN(win32,"wrong import directory??\n");
643 pem
->pe_import
= (LPIMAGE_IMPORT_DESCRIPTOR
) RVA(dir
.VirtualAddress
);
646 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
];
649 if(pem
->pe_resource
&& (int)pem
->pe_resource
!=RVA(dir
.VirtualAddress
))
650 WARN(win32
,"wrong resource directory??\n");
651 pem
->pe_resource
= (LPIMAGE_RESOURCE_DIRECTORY
) RVA(dir
.VirtualAddress
);
654 if(nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXCEPTION
].Size
)
655 FIXME(win32
,"Exception directory ignored\n");
657 if(nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
)
658 FIXME(win32
,"Security directory ignored\n");
662 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
665 if(pem
->pe_reloc
&& (int)pem
->pe_reloc
!= RVA(dir
.VirtualAddress
))
666 WARN(win32
,"wrong relocation list??\n");
667 pem
->pe_reloc
= (void *) RVA(dir
.VirtualAddress
);
670 if(nt_header
->OptionalHeader
.DataDirectory
671 [IMAGE_DIRECTORY_ENTRY_COPYRIGHT
].Size
)
672 FIXME(win32
,"Copyright string ignored\n");
674 if(nt_header
->OptionalHeader
.DataDirectory
675 [IMAGE_DIRECTORY_ENTRY_GLOBALPTR
].Size
)
676 FIXME(win32
,"Global Pointer (MIPS) ignored\n");
678 if(nt_header
->OptionalHeader
.DataDirectory
679 [IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
].Size
)
680 FIXME(win32
,"Load Configuration directory ignored\n");
682 if(nt_header
->OptionalHeader
.DataDirectory
683 [IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
].Size
)
684 FIXME(win32
,"Bound Import directory ignored\n");
686 if(nt_header
->OptionalHeader
.DataDirectory
687 [IMAGE_DIRECTORY_ENTRY_IAT
].Size
)
688 FIXME(win32
,"Import Address Table directory ignored\n");
689 if(nt_header
->OptionalHeader
.DataDirectory
[13].Size
)
690 FIXME(win32
,"Unknown directory 13 ignored\n");
691 if(nt_header
->OptionalHeader
.DataDirectory
[14].Size
)
692 FIXME(win32
,"Unknown directory 14 ignored\n");
693 if(nt_header
->OptionalHeader
.DataDirectory
[15].Size
)
694 FIXME(win32
,"Unknown directory 15 ignored\n");
696 if(pem
->pe_reloc
) do_relocations(wm
);
698 dump_exports(wm
->module
);
700 wm
->modname
= HEAP_strdupA(process
->heap
,0,(char*)RVA(pem
->pe_export
->Name
));
702 /* try to find out the name from the OFSTRUCT */
704 modname
= s
= ofs
->szPathName
;
705 while ((s
=strchr(modname
,'\\')))
707 wm
->modname
= HEAP_strdupA(process
->heap
,0,modname
);
710 if (fixup_imports(process
,wm
)) {
713 /* remove entry from modref chain */
714 xwm
= &(process
->modref_list
);
720 xwm
= &((*xwm
)->next
);
722 /* FIXME: there are several more dangling references
723 * left. Including dlls loaded by this dll before the
724 * failed one. Unrolling is rather difficult with the
725 * current structure and we can leave it them lying
726 * around with no problems, so we don't care
732 /* Now that we got everything at the right address,
733 * we can unmap the previous module */
734 UnmapViewOfFile( (LPVOID
)hModule
);
738 /******************************************************************************
739 * The PE Library Loader frontend.
740 * FIXME: handle the flags.
741 * internal module handling should be made better here (and in builtin.c)
743 HMODULE32
PE_LoadLibraryEx32A (LPCSTR name
, PDB32
*process
,
744 HFILE32 hFile
, DWORD flags
)
751 if ((hModule
= MODULE_FindModule32( process
, name
))) {
752 for (wm
= process
->modref_list
;wm
;wm
=wm
->next
)
753 if (wm
->module
== hModule
)
755 /* Since MODULE_FindModule32 uses the modref chain too, the
756 * module MUST have been found above. If not, something has gone
761 /* try to load builtin, enabled modules first */
762 if ((hModule
= BUILTIN32_LoadModule( name
, FALSE
, process
)))
765 /* try to load the specified dll/exe */
766 if (HFILE_ERROR32
==(hFile
=OpenFile32(name
,&ofs
,OF_READ
))) {
767 /* Now try the built-in even if disabled */
768 if ((hModule
= BUILTIN32_LoadModule( name
, TRUE
, process
))) {
769 WARN( module
, "Could not load external DLL '%s', using built-in module.\n", name
);
774 /* will go away ... */
775 if ((hModule
= MODULE_CreateDummyModule( &ofs
)) < 32) {
779 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
780 pModule
->flags
= NE_FFLAGS_WIN32
;
783 wm
=(WINE_MODREF
*)HeapAlloc(process
->heap
,HEAP_ZERO_MEMORY
,sizeof(*wm
));
784 wm
->type
= MODULE32_PE
;
785 /* NOTE: fixup_imports takes care of the correct order */
786 wm
->next
= process
->modref_list
;
787 process
->modref_list
= wm
;
789 wm
->module
= pModule
->module32
= PE_LoadImage( hFile
);
791 CloseHandle( hFile
);
794 process
->modref_list
= wm
->next
;
795 FreeLibrary16( hModule
);
796 HeapFree(process
->heap
,0,wm
);
797 ERR(win32
,"can't load %s\n",ofs
.szPathName
);
798 return 21; /* FIXME: probably 0 */
801 /* (possible) recursion */
802 if (!PE_MapImage(process
,wm
,&ofs
,flags
)) {
803 /* ERROR cleanup ... */
806 ERR(win32
,"couldn't load %s\n",ofs
.szPathName
);
807 /* unlink from process modref chain */
808 for ( xwm
=&(process
->modref_list
);
817 pModule
->module32
= wm
->module
;
821 /*****************************************************************************
822 * Load the PE main .EXE. All other loading is done by PE_LoadLibraryEx32A
823 * FIXME: this function should use PE_LoadLibraryEx32A, but currently can't
824 * due to the PROCESS_Create stuff.
826 HINSTANCE16
PE_LoadModule( LPCSTR name
, LPCSTR cmd_line
,
827 LPCSTR env
, UINT16 show_cmd
)
831 HINSTANCE16 hInstance
;
835 THDB
*thdb
= THREAD_Current();
839 if ((hFile
= OpenFile32( name
, &ofs
, OF_READ
)) == HFILE_ERROR32
)
840 return 2; /* File not found */
842 if ((hModule16
= MODULE_CreateDummyModule( &ofs
)) < 32) return hModule16
;
843 pModule
= (NE_MODULE
*)GlobalLock16( hModule16
);
844 pModule
->flags
= NE_FFLAGS_WIN32
;
846 pModule
->module32
= hModule32
= PE_LoadImage( hFile
);
847 if (hModule32
< 32) return 21;
849 hInstance
= NE_CreateInstance( pModule
, NULL
, (cmd_line
== NULL
) );
851 !(PE_HEADER(hModule32
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
853 PDB32
*pdb
= PROCESS_Create( pModule
, cmd_line
, env
,
854 hInstance
, 0, show_cmd
);
855 TDB
*pTask
= (TDB
*)GlobalLock16( pdb
->task
);
859 process
= thdb
->process
;
861 wm
=(WINE_MODREF
*)HeapAlloc(process
->heap
,HEAP_ZERO_MEMORY
,sizeof(*wm
));
862 wm
->type
= MODULE32_PE
;
863 /* NOTE: fixup_imports takes care of the correct order */
864 wm
->next
= process
->modref_list
;
865 wm
->module
= hModule32
;
866 process
->modref_list
= wm
;
867 if (!PE_MapImage( process
, wm
, &ofs
, 0 ))
869 /* FIXME: should destroy the task created and free referenced stuff */
872 pModule
->module32
= wm
->module
;
873 /* FIXME: Yuck. Is there no other good place to do that? */
878 /*********************************************************************
879 * PE_UnloadImage [internal]
881 int PE_UnloadImage( HMODULE32 hModule
)
883 FIXME(win32
,"stub.\n");
884 /* free resources, image, unmap */
888 /* Called if the library is loaded or freed.
889 * NOTE: if a thread attaches a DLL, the current thread will only do
890 * DLL_PROCESS_ATTACH. Only new created threads do DLL_THREAD_ATTACH
893 static void PE_InitDLL(WINE_MODREF
*wm
, DWORD type
,LPVOID lpReserved
)
895 if (wm
->type
!=MODULE32_PE
)
897 if (type
==DLL_PROCESS_ATTACH
)
898 wm
->binfmt
.pe
.flags
|= PE_MODREF_PROCESS_ATTACHED
;
900 /* DLL_ATTACH_PROCESS:
901 * lpreserved is NULL for dynamic loads, not-NULL for static loads
902 * DLL_DETACH_PROCESS:
903 * lpreserved is NULL if called by FreeLibrary, not-NULL otherwise
904 * the SDK doesn't mention anything for DLL_THREAD_*
907 /* Is this a library? And has it got an entrypoint? */
908 if ((PE_HEADER(wm
->module
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) &&
909 (PE_HEADER(wm
->module
)->OptionalHeader
.AddressOfEntryPoint
)
911 FARPROC32 entry
= (FARPROC32
)RVA_PTR( wm
->module
,
912 OptionalHeader
.AddressOfEntryPoint
);
913 TRACE(relay
, "CallTo32(entryproc=%p,module=%08x,type=%ld,res=%p)\n",
914 entry
, wm
->module
, type
, lpReserved
);
915 entry( wm
->module
, type
, lpReserved
);
919 /* Call the DLLentry function of all dlls used by that process.
920 * (NOTE: this may recursively call this function (if a library calls
921 * LoadLibrary) ... but it won't matter)
923 void PE_InitializeDLLs(PDB32
*process
,DWORD type
,LPVOID lpReserved
) {
926 for (wm
= process
->modref_list
;wm
;wm
=wm
->next
) {
927 PE_MODREF
*pem
= NULL
;
928 if (wm
->type
!=MODULE32_PE
)
930 pem
= &(wm
->binfmt
.pe
);
931 if (pem
->flags
& PE_MODREF_NO_DLL_CALLS
)
933 if (type
==DLL_PROCESS_ATTACH
) {
934 if (pem
->flags
& PE_MODREF_PROCESS_ATTACHED
)
937 PE_InitDLL( wm
, type
, lpReserved
);
941 void PE_InitTls(THDB
*thdb
)
945 IMAGE_NT_HEADERS
*peh
;
948 LPIMAGE_TLS_DIRECTORY pdir
;
949 PDB32
*pdb
= thdb
->process
;
952 for (wm
= pdb
->modref_list
;wm
;wm
=wm
->next
) {
953 if (wm
->type
!=MODULE32_PE
)
955 pem
= &(wm
->binfmt
.pe
);
956 peh
= PE_HEADER(wm
->module
);
957 delta
= wm
->module
- peh
->OptionalHeader
.ImageBase
;
958 if (!peh
->OptionalHeader
.DataDirectory
[IMAGE_FILE_THREAD_LOCAL_STORAGE
].VirtualAddress
)
960 pdir
= (LPVOID
)(wm
->module
+ peh
->OptionalHeader
.
961 DataDirectory
[IMAGE_FILE_THREAD_LOCAL_STORAGE
].VirtualAddress
);
964 if (!(pem
->flags
& PE_MODREF_TLS_ALLOCED
)) {
965 pem
->tlsindex
= THREAD_TlsAlloc(thdb
);
966 *(LPDWORD
)AdjustPtr(pdir
->AddressOfIndex
,delta
)
969 pem
->flags
|= PE_MODREF_TLS_ALLOCED
;
970 datasize
= pdir
->EndAddressOfRawData
-pdir
->StartAddressOfRawData
;
971 size
= datasize
+ pdir
->SizeOfZeroFill
;
972 mem
=VirtualAlloc(0,size
,MEM_RESERVE
|MEM_COMMIT
,PAGE_READWRITE
);
974 AdjustPtr(pdir
->StartAddressOfRawData
,delta
),
977 /* don't use TlsSetValue, we are in the wrong thread */
978 if (pdir
->AddressOfCallBacks
) {
979 LPIMAGE_TLS_CALLBACK
*cbs
=
980 (LPIMAGE_TLS_CALLBACK
*)
981 AdjustPtr(pdir
->AddressOfCallBacks
, delta
);
984 FIXME(win32
, "TLS Callbacks aren't going to be called\n");
987 thdb
->tls_array
[pem
->tlsindex
] = mem
;
991 /****************************************************************************
992 * DisableThreadLibraryCalls (KERNEL32.74)
993 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
995 BOOL32 WINAPI
DisableThreadLibraryCalls(HMODULE32 hModule
)
999 for (wm
=PROCESS_Current()->modref_list
;wm
;wm
=wm
->next
)
1000 if ((wm
->module
== hModule
) && (wm
->type
==MODULE32_PE
))
1001 wm
->binfmt
.pe
.flags
|=PE_MODREF_NO_DLL_CALLS
;