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>
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 */
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 snoop
? SNOOP_GetProcAddress32(wm
->module
,ename
,*ordinal
,(FARPROC32
)RVA(addr
))
167 : (FARPROC32
)RVA(addr
);
168 forward
= (char *)RVA(addr
);
176 if (LOWORD(funcName
)-exports
->Base
> exports
->NumberOfFunctions
) {
177 TRACE(win32
," ordinal %d out of range!\n",
181 addr
= function
[(int)funcName
-exports
->Base
];
182 if (!addr
) return NULL
;
185 for (i
=0;i
<exports
->NumberOfNames
;i
++) {
186 ename
= (char*)RVA(*name
);
187 if (*ordinal
== LOWORD(funcName
)-exports
->Base
)
192 if (i
==exports
->NumberOfNames
)
195 if ((addr
< rva_start
) || (addr
>= rva_end
))
196 return snoop
? SNOOP_GetProcAddress32(wm
->module
,ename
,(DWORD
)funcName
-exports
->Base
,(FARPROC32
)RVA(addr
))
197 : (FARPROC32
)RVA(addr
);
198 forward
= (char *)RVA(addr
);
204 char *end
= strchr(forward
, '.');
206 if (!end
) return NULL
;
207 assert(end
-forward
<256);
208 strncpy(module
, forward
, (end
- forward
));
209 module
[end
-forward
] = 0;
210 hMod
= MODULE_FindModule32(process
,module
);
212 return MODULE_GetProcAddress32( process
, hMod
, end
+ 1, snoop
);
217 DWORD
fixup_imports (PDB32
*process
,WINE_MODREF
*wm
)
219 IMAGE_IMPORT_DESCRIPTOR
*pe_imp
;
222 unsigned int load_addr
= wm
->module
;
223 int i
,characteristics_detection
=1;
226 assert(wm
->type
==MODULE32_PE
);
227 pem
= &(wm
->binfmt
.pe
);
229 modname
= (char*) RVA(pem
->pe_export
->Name
);
231 modname
= "<unknown>";
233 /* OK, now dump the import list */
234 TRACE(win32
, "Dumping imports list\n");
236 /* first, count the number of imported non-internal modules */
237 pe_imp
= pem
->pe_import
;
239 ERR(win32
, "no import directory????\n");
241 /* We assume that we have at least one import with !0 characteristics and
242 * detect broken imports with all characteristsics 0 (notably Borland) and
243 * switch the detection off for them.
245 for (i
= 0; pe_imp
->Name
; pe_imp
++) {
246 if (!i
&& !pe_imp
->u
.Characteristics
)
247 characteristics_detection
= 0;
248 if (characteristics_detection
&& !pe_imp
->u
.Characteristics
)
253 /* Allocate module dependency list */
255 wm
->deps
= HeapAlloc(process
->heap
, 0, i
*sizeof(WINE_MODREF
*));
257 /* load the imported modules. They are automatically
258 * added to the modref list of the process.
261 for (i
= 0, pe_imp
= pem
->pe_import
; pe_imp
->Name
; pe_imp
++) {
262 HMODULE32 hImpModule
;
263 IMAGE_IMPORT_BY_NAME
*pe_name
;
264 PIMAGE_THUNK_DATA import_list
,thunk_list
;
265 char *name
= (char *) RVA(pe_imp
->Name
);
267 if (characteristics_detection
&& !pe_imp
->u
.Characteristics
)
270 /* don't use MODULE_Load, Win32 creates new task differently */
271 hImpModule
= MODULE_LoadLibraryEx32A( name
, process
, 0, 0 );
273 char *p
,buffer
[2000];
275 /* GetModuleFileName would use the wrong process, so don't use it */
276 strcpy(buffer
,wm
->shortname
);
277 if (!(p
= strrchr (buffer
, '\\')))
279 strcpy (p
+ 1, name
);
280 hImpModule
= MODULE_LoadLibraryEx32A( buffer
, process
, 0, 0 );
283 ERR (module
, "Module %s not found\n", name
);
286 xwm
= MODULE32_LookupHMODULE(process
, hImpModule
);
290 /* FIXME: forwarder entries ... */
292 if (pe_imp
->u
.OriginalFirstThunk
!= 0) { /* original MS style */
293 TRACE(win32
, "Microsoft style imports used\n");
294 import_list
=(PIMAGE_THUNK_DATA
) RVA(pe_imp
->u
.OriginalFirstThunk
);
295 thunk_list
= (PIMAGE_THUNK_DATA
) RVA(pe_imp
->FirstThunk
);
297 while (import_list
->u1
.Ordinal
) {
298 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
)) {
299 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
301 TRACE(win32
, "--- Ordinal %s,%d\n", name
, ordinal
);
302 thunk_list
->u1
.Function
=MODULE_GetProcAddress32(
303 process
, hImpModule
, (LPCSTR
)ordinal
, TRUE
305 if (!thunk_list
->u1
.Function
) {
306 ERR(win32
,"No implementation for %s.%d, setting to 0xdeadbeef\n",
308 thunk_list
->u1
.Function
= (FARPROC32
)0xdeadbeef;
310 } else { /* import by name */
311 pe_name
= (PIMAGE_IMPORT_BY_NAME
)RVA(import_list
->u1
.AddressOfData
);
312 TRACE(win32
, "--- %s %s.%d\n", pe_name
->Name
, name
, pe_name
->Hint
);
313 thunk_list
->u1
.Function
=MODULE_GetProcAddress32(
314 process
, hImpModule
, pe_name
->Name
, TRUE
316 if (!thunk_list
->u1
.Function
) {
317 ERR(win32
,"No implementation for %s.%d(%s), setting to 0xdeadbeef\n",
318 name
,pe_name
->Hint
,pe_name
->Name
);
319 thunk_list
->u1
.Function
= (FARPROC32
)0xdeadbeef;
325 } else { /* Borland style */
326 TRACE(win32
, "Borland style imports used\n");
327 thunk_list
= (PIMAGE_THUNK_DATA
) RVA(pe_imp
->FirstThunk
);
328 while (thunk_list
->u1
.Ordinal
) {
329 if (IMAGE_SNAP_BY_ORDINAL(thunk_list
->u1
.Ordinal
)) {
330 /* not sure about this branch, but it seems to work */
331 int ordinal
= IMAGE_ORDINAL(thunk_list
->u1
.Ordinal
);
333 TRACE(win32
,"--- Ordinal %s.%d\n",name
,ordinal
);
334 thunk_list
->u1
.Function
=MODULE_GetProcAddress32(
335 process
, hImpModule
, (LPCSTR
) ordinal
, TRUE
337 if (!thunk_list
->u1
.Function
) {
338 ERR(win32
, "No implementation for %s.%d, setting to 0xdeadbeef\n",
340 thunk_list
->u1
.Function
= (FARPROC32
)0xdeadbeef;
343 pe_name
=(PIMAGE_IMPORT_BY_NAME
) RVA(thunk_list
->u1
.AddressOfData
);
344 TRACE(win32
,"--- %s %s.%d\n",
345 pe_name
->Name
,name
,pe_name
->Hint
);
346 thunk_list
->u1
.Function
=MODULE_GetProcAddress32(
347 process
, hImpModule
, pe_name
->Name
, TRUE
349 if (!thunk_list
->u1
.Function
) {
350 ERR(win32
, "No implementation for %s.%d, setting to 0xdeadbeef\n",
351 name
, pe_name
->Hint
);
352 thunk_list
->u1
.Function
= (FARPROC32
)0xdeadbeef;
362 static int calc_vma_size( HMODULE32 hModule
)
365 IMAGE_SECTION_HEADER
*pe_seg
= PE_SECTIONS(hModule
);
367 TRACE(win32
, "Dump of segment table\n");
368 TRACE(win32
, " Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
369 for (i
= 0; i
< PE_HEADER(hModule
)->FileHeader
.NumberOfSections
; i
++)
371 TRACE(win32
, "%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
373 pe_seg
->Misc
.VirtualSize
,
374 pe_seg
->VirtualAddress
,
375 pe_seg
->SizeOfRawData
,
376 pe_seg
->PointerToRawData
,
377 pe_seg
->PointerToRelocations
,
378 pe_seg
->PointerToLinenumbers
,
379 pe_seg
->NumberOfRelocations
,
380 pe_seg
->NumberOfLinenumbers
,
381 pe_seg
->Characteristics
);
382 vma_size
=MAX(vma_size
, pe_seg
->VirtualAddress
+pe_seg
->SizeOfRawData
);
383 vma_size
=MAX(vma_size
, pe_seg
->VirtualAddress
+pe_seg
->Misc
.VirtualSize
);
389 static void do_relocations(WINE_MODREF
*wm
)
391 PE_MODREF
*pem
= &(wm
->binfmt
.pe
);
392 int delta
= wm
->module
- PE_HEADER(wm
->module
)->OptionalHeader
.ImageBase
;
393 unsigned int load_addr
= wm
->module
;
395 IMAGE_BASE_RELOCATION
*r
= pem
->pe_reloc
;
396 int hdelta
= (delta
>> 16) & 0xFFFF;
397 int ldelta
= delta
& 0xFFFF;
399 /* int reloc_size = */
404 while(r
->VirtualAddress
)
406 char *page
= (char*) RVA(r
->VirtualAddress
);
407 int count
= (r
->SizeOfBlock
- 8)/2;
409 TRACE(fixup
, "%x relocations for page %lx\n",
410 count
, r
->VirtualAddress
);
411 /* patching in reverse order */
414 int offset
= r
->TypeOffset
[i
] & 0xFFF;
415 int type
= r
->TypeOffset
[i
] >> 12;
416 TRACE(fixup
,"patching %x type %x\n", offset
, type
);
419 case IMAGE_REL_BASED_ABSOLUTE
: break;
420 case IMAGE_REL_BASED_HIGH
:
421 *(short*)(page
+offset
) += hdelta
;
423 case IMAGE_REL_BASED_LOW
:
424 *(short*)(page
+offset
) += ldelta
;
426 case IMAGE_REL_BASED_HIGHLOW
:
428 *(int*)(page
+offset
) += delta
;
430 { int h
=*(unsigned short*)(page
+offset
);
431 int l
=r
->TypeOffset
[++i
];
432 *(unsigned int*)(page
+ offset
) = (h
<<16) + l
+ delta
;
436 case IMAGE_REL_BASED_HIGHADJ
:
437 FIXME(win32
, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
439 case IMAGE_REL_BASED_MIPS_JMPADDR
:
440 FIXME(win32
, "Is this a MIPS machine ???\n");
443 FIXME(win32
, "Unknown fixup type\n");
447 r
= (IMAGE_BASE_RELOCATION
*)((char*)r
+ r
->SizeOfBlock
);
455 /**********************************************************************
457 * Load one PE format DLL/EXE into memory
459 * Unluckily we can't just mmap the sections where we want them, for
460 * (at least) Linux does only support offsets which are page-aligned.
462 * BUT we have to map the whole image anyway, for Win32 programs sometimes
463 * want to access them. (HMODULE32 point to the start of it)
465 static HMODULE32
PE_LoadImage( HFILE32 hFile
)
470 IMAGE_SECTION_HEADER
*pe_sec
;
471 BY_HANDLE_FILE_INFORMATION bhfi
;
474 /* map the PE file somewhere */
475 mapping
= CreateFileMapping32A( hFile
, NULL
, PAGE_READONLY
| SEC_COMMIT
,
479 WARN( win32
, "CreateFileMapping error %ld\n",
483 hModule
= (HMODULE32
)MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
484 CloseHandle( mapping
);
487 WARN( win32
, "PE_LoadImage: MapViewOfFile error %ld\n",
492 if (PE_HEADER(hModule
)->Signature
!= IMAGE_NT_SIGNATURE
)
494 WARN(win32
,"image doesn't have PE signature, but 0x%08lx\n",
495 PE_HEADER(hModule
)->Signature
);
499 if (PE_HEADER(hModule
)->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
501 MSG("Trying to load PE image for unsupported architecture (");
502 switch (PE_HEADER(hModule
)->FileHeader
.Machine
)
504 case IMAGE_FILE_MACHINE_UNKNOWN
: MSG("Unknown"); break;
505 case IMAGE_FILE_MACHINE_I860
: MSG("I860"); break;
506 case IMAGE_FILE_MACHINE_R3000
: MSG("R3000"); break;
507 case IMAGE_FILE_MACHINE_R4000
: MSG("R4000"); break;
508 case IMAGE_FILE_MACHINE_R10000
: MSG("R10000"); break;
509 case IMAGE_FILE_MACHINE_ALPHA
: MSG("Alpha"); break;
510 case IMAGE_FILE_MACHINE_POWERPC
: MSG("PowerPC"); break;
511 default: MSG("Unknown-%04x",
512 PE_HEADER(hModule
)->FileHeader
.Machine
); break;
517 /* find out how large this executeable should be */
518 pe_sec
= PE_SECTIONS(hModule
);
519 for (i
=0;i
<PE_HEADER(hModule
)->FileHeader
.NumberOfSections
;i
++) {
520 if (pe_sec
[i
].Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
522 if (pe_sec
[i
].PointerToRawData
+pe_sec
[i
].SizeOfRawData
> rawsize
)
523 rawsize
= pe_sec
[i
].PointerToRawData
+pe_sec
[i
].SizeOfRawData
;
525 if (GetFileInformationByHandle(hFile
,&bhfi
)) {
527 if (bhfi
.nFileSizeLow
< rawsize
) {
528 ERR(win32
,"PE module is too small (header: %d, filesize: %d), probably truncated download?\n",rawsize
,bhfi
.nFileSizeLow
);
532 /* Else ... Hmm, we have opened it, so we should be able to get info?
533 * Anyway, don't care in this case
538 UnmapViewOfFile( (LPVOID
)hModule
);
542 /**********************************************************************
543 * This maps a loaded PE dll into the address space of the specified process.
545 static BOOL32
PE_MapImage( PDB32
*process
,WINE_MODREF
*wm
, OFSTRUCT
*ofs
, DWORD flags
)
549 DWORD load_addr
,lowest_va
,lowest_fa
;
550 IMAGE_DATA_DIRECTORY dir
;
553 HMODULE32 hModule
= wm
->module
;
554 DWORD aoep
= PE_HEADER(hModule
)->OptionalHeader
.AddressOfEntryPoint
;
555 IMAGE_SECTION_HEADER
*pe_seg
;
556 IMAGE_DOS_HEADER
*dos_header
= (IMAGE_DOS_HEADER
*)hModule
;
557 IMAGE_NT_HEADERS
*nt_header
= PE_HEADER(hModule
);
559 pem
= &(wm
->binfmt
.pe
);
561 result
= GetLongPathName32A(ofs
->szPathName
,NULL
,0);
562 wm
->longname
= (char*)HeapAlloc(process
->heap
,0,result
+1);
563 GetLongPathName32A(ofs
->szPathName
,wm
->longname
,result
+1);
565 wm
->shortname
= HEAP_strdupA(process
->heap
,0,ofs
->szPathName
);
567 if (!(nt_header
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
569 if (process
->exe_modref
)
570 FIXME(win32
,"overwriting old exe_modref... arrgh\n");
571 process
->exe_modref
= wm
;
574 load_addr
= nt_header
->OptionalHeader
.ImageBase
;
575 vma_size
= calc_vma_size( hModule
);
576 TRACE(win32
, "Load addr is %lx\n",load_addr
);
577 load_addr
= (DWORD
)VirtualAlloc( (void*)load_addr
, vma_size
,
578 MEM_RESERVE
| MEM_COMMIT
,
579 PAGE_EXECUTE_READWRITE
);
580 if (load_addr
== 0) {
581 load_addr
= (DWORD
)VirtualAlloc( NULL
, vma_size
,
582 MEM_RESERVE
| MEM_COMMIT
,
583 PAGE_EXECUTE_READWRITE
);
585 /* NOTE: this changes a value in the process modref chain, which can
586 * be accessed independently from this function
588 wm
->module
= (HMODULE32
)load_addr
;
590 TRACE(win32
, "Load addr is really %lx, range %x\n",
591 load_addr
, vma_size
);
593 TRACE(segment
, "Loading %s at %lx, range %x\n",
594 ofs
->szPathName
, load_addr
, vma_size
);
596 /* Find out where this executeable should start */
597 pe_seg
= PE_SECTIONS(hModule
);lowest_va
= 0x10000;lowest_fa
= 0x10000;
598 for (i
=0;i
<PE_HEADER(hModule
)->FileHeader
.NumberOfSections
;i
++) {
599 if (lowest_va
> pe_seg
[i
].VirtualAddress
)
600 lowest_va
= pe_seg
[i
].VirtualAddress
;
601 if (!(pe_seg
[i
].Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
602 && (pe_seg
[i
].PointerToRawData
<lowest_fa
)
604 lowest_fa
= pe_seg
[i
].PointerToRawData
;
606 if (aoep
&& (aoep
<lowest_va
))
607 FIXME(win32
,"WARNING: '%s' has an invalid entrypoint (0x%08lx) below the first virtual address (0x%08lx) (possible Virus Infection or broken binary)!\n",ofs
->szPathName
,aoep
,lowest_va
);
609 /* Store the NT header at the load addr
610 * (FIXME: should really use mmap)
612 *(IMAGE_DOS_HEADER
*)load_addr
= *dos_header
;
613 *(IMAGE_NT_HEADERS
*)(load_addr
+ dos_header
->e_lfanew
) = *nt_header
;
614 memcpy(PE_SECTIONS(load_addr
),PE_SECTIONS(hModule
),sizeof(IMAGE_SECTION_HEADER
)*nt_header
->FileHeader
.NumberOfSections
);
616 /* Copies all stuff up to the first section. Including win32 viruses. */
617 memcpy(load_addr
,hModule
,lowest_fa
);
619 pe_seg
= PE_SECTIONS(hModule
);
620 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++, pe_seg
++)
622 /* memcpy only non-BSS segments */
623 /* FIXME: this should be done by mmap(..MAP_PRIVATE|MAP_FIXED..)
624 * but it is not possible for (at least) Linux needs
625 * a page-aligned offset.
627 if(!(pe_seg
->Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
))
628 memcpy((char*)RVA(pe_seg
->VirtualAddress
),
629 (char*)(hModule
+ pe_seg
->PointerToRawData
),
630 pe_seg
->SizeOfRawData
633 result
= RVA (pe_seg
->VirtualAddress
);
635 /* not needed, memory is zero */
636 if(strcmp(pe_seg
->Name
, ".bss") == 0)
637 memset((void *)result
, 0,
638 pe_seg
->Misc
.VirtualSize
?
639 pe_seg
->Misc
.VirtualSize
:
640 pe_seg
->SizeOfRawData
);
643 if(strcmp(pe_seg
->Name
, ".idata") == 0)
644 pem
->pe_import
= (PIMAGE_IMPORT_DESCRIPTOR
) result
;
646 if(strcmp(pe_seg
->Name
, ".edata") == 0)
647 pem
->pe_export
= (PIMAGE_EXPORT_DIRECTORY
) result
;
649 if(strcmp(pe_seg
->Name
, ".rsrc") == 0)
650 pem
->pe_resource
= (PIMAGE_RESOURCE_DIRECTORY
) result
;
652 if(strcmp(pe_seg
->Name
, ".reloc") == 0)
653 pem
->pe_reloc
= (PIMAGE_BASE_RELOCATION
) result
;
656 /* There is word that the actual loader does not care about the
657 section names, and only goes for the DataDirectory */
658 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
];
661 if(pem
->pe_export
&& (int)pem
->pe_export
!=RVA(dir
.VirtualAddress
))
662 WARN(win32
,"wrong export directory??\n");
663 /* always trust the directory */
664 pem
->pe_export
= (PIMAGE_EXPORT_DIRECTORY
) RVA(dir
.VirtualAddress
);
667 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
];
671 if(pem->pe_import && (int)pem->pe_import!=RVA(dir.VirtualAddress))
672 WARN(win32,"wrong import directory??\n");
674 pem
->pe_import
= (PIMAGE_IMPORT_DESCRIPTOR
) RVA(dir
.VirtualAddress
);
677 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
];
680 if(pem
->pe_resource
&& (int)pem
->pe_resource
!=RVA(dir
.VirtualAddress
))
681 WARN(win32
,"wrong resource directory??\n");
682 pem
->pe_resource
= (PIMAGE_RESOURCE_DIRECTORY
) RVA(dir
.VirtualAddress
);
685 if(nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXCEPTION
].Size
)
686 FIXME(win32
,"Exception directory ignored\n");
688 if(nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
)
689 FIXME(win32
,"Security directory ignored\n");
693 dir
=nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
696 if(pem
->pe_reloc
&& (int)pem
->pe_reloc
!= RVA(dir
.VirtualAddress
))
697 WARN(win32
,"wrong relocation list??\n");
698 pem
->pe_reloc
= (void *) RVA(dir
.VirtualAddress
);
701 if(nt_header
->OptionalHeader
.DataDirectory
702 [IMAGE_DIRECTORY_ENTRY_COPYRIGHT
].Size
)
703 FIXME(win32
,"Copyright string ignored\n");
705 if(nt_header
->OptionalHeader
.DataDirectory
706 [IMAGE_DIRECTORY_ENTRY_GLOBALPTR
].Size
)
707 FIXME(win32
,"Global Pointer (MIPS) ignored\n");
709 if(nt_header
->OptionalHeader
.DataDirectory
710 [IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
].Size
)
711 FIXME(win32
,"Load Configuration directory ignored\n");
713 if(nt_header
->OptionalHeader
.DataDirectory
714 [IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
].Size
)
715 TRACE(win32
,"Bound Import directory ignored\n");
717 if(nt_header
->OptionalHeader
.DataDirectory
718 [IMAGE_DIRECTORY_ENTRY_IAT
].Size
)
719 TRACE(win32
,"Import Address Table directory ignored\n");
720 if(nt_header
->OptionalHeader
.DataDirectory
[13].Size
)
721 FIXME(win32
,"Unknown directory 13 ignored\n");
722 if(nt_header
->OptionalHeader
.DataDirectory
[14].Size
)
723 FIXME(win32
,"Unknown directory 14 ignored\n");
724 if(nt_header
->OptionalHeader
.DataDirectory
[15].Size
)
725 FIXME(win32
,"Unknown directory 15 ignored\n");
727 if(pem
->pe_reloc
) do_relocations(wm
);
729 dump_exports(wm
->module
);
731 wm
->modname
= HEAP_strdupA(process
->heap
,0,(char*)RVA(pem
->pe_export
->Name
));
733 /* try to find out the name from the OFSTRUCT */
735 modname
= s
= ofs
->szPathName
;
736 while ((s
=strchr(modname
,'\\')))
738 wm
->modname
= HEAP_strdupA(process
->heap
,0,modname
);
741 if (fixup_imports(process
,wm
)) {
744 /* remove entry from modref chain */
745 xwm
= &(process
->modref_list
);
751 xwm
= &((*xwm
)->next
);
753 /* FIXME: there are several more dangling references
754 * left. Including dlls loaded by this dll before the
755 * failed one. Unrolling is rather difficult with the
756 * current structure and we can leave it them lying
757 * around with no problems, so we don't care
763 /* Now that we got everything at the right address,
764 * we can unmap the previous module */
765 UnmapViewOfFile( (LPVOID
)hModule
);
769 /******************************************************************************
770 * The PE Library Loader frontend.
771 * FIXME: handle the flags.
772 * internal module handling should be made better here (and in builtin.c)
774 HMODULE32
PE_LoadLibraryEx32A (LPCSTR name
, PDB32
*process
,
775 HFILE32 hFile
, DWORD flags
)
782 if ((hModule
= MODULE_FindModule32( process
, name
))) {
783 for (wm
= process
->modref_list
;wm
;wm
=wm
->next
)
784 if (wm
->module
== hModule
)
786 /* Since MODULE_FindModule32 uses the modref chain too, the
787 * module MUST have been found above. If not, something has gone
792 /* try to load builtin, enabled modules first */
793 if ((hModule
= BUILTIN32_LoadModule( name
, FALSE
, process
)))
796 /* try to load the specified dll/exe */
797 if (HFILE_ERROR32
==(hFile
=OpenFile32(name
,&ofs
,OF_READ
))) {
798 /* Now try the built-in even if disabled */
799 if ((hModule
= BUILTIN32_LoadModule( name
, TRUE
, process
))) {
800 WARN( module
, "Could not load external DLL '%s', using built-in module.\n", name
);
805 /* will go away ... */
806 if ((hModule
= MODULE_CreateDummyModule( &ofs
)) < 32) {
810 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
811 pModule
->flags
= NE_FFLAGS_WIN32
;
814 wm
=(WINE_MODREF
*)HeapAlloc(process
->heap
,HEAP_ZERO_MEMORY
,sizeof(*wm
));
815 wm
->type
= MODULE32_PE
;
816 /* NOTE: fixup_imports takes care of the correct order */
817 wm
->next
= process
->modref_list
;
818 process
->modref_list
= wm
;
820 wm
->module
= pModule
->module32
= PE_LoadImage( hFile
);
822 CloseHandle( hFile
);
825 process
->modref_list
= wm
->next
;
826 FreeLibrary16( hModule
);
827 HeapFree(process
->heap
,0,wm
);
828 ERR(win32
,"can't load %s\n",ofs
.szPathName
);
832 /* (possible) recursion */
833 if (!PE_MapImage(process
,wm
,&ofs
,flags
)) {
834 /* ERROR cleanup ... */
837 ERR(win32
,"couldn't load %s\n",ofs
.szPathName
);
838 /* unlink from process modref chain */
839 for ( xwm
=&(process
->modref_list
);
848 pModule
->module32
= wm
->module
;
849 if (wm
->binfmt
.pe
.pe_export
)
850 SNOOP_RegisterDLL(wm
->module
,wm
->modname
,wm
->binfmt
.pe
.pe_export
->NumberOfFunctions
);
854 /*****************************************************************************
855 * Load the PE main .EXE. All other loading is done by PE_LoadLibraryEx32A
856 * FIXME: this function should use PE_LoadLibraryEx32A, but currently can't
857 * due to the PROCESS_Create stuff.
859 HINSTANCE16
PE_CreateProcess( LPCSTR name
, LPCSTR cmd_line
,
860 LPCSTR env
, LPSTARTUPINFO32A startup
,
861 LPPROCESS_INFORMATION info
)
865 HINSTANCE16 hInstance
;
873 if ((hFile
= OpenFile32( name
, &ofs
, OF_READ
)) == HFILE_ERROR32
)
874 return 2; /* File not found */
876 if ((hModule16
= MODULE_CreateDummyModule( &ofs
)) < 32) return hModule16
;
877 pModule
= (NE_MODULE
*)GlobalLock16( hModule16
);
878 pModule
->flags
= NE_FFLAGS_WIN32
;
880 pModule
->module32
= hModule32
= PE_LoadImage( hFile
);
881 if (hModule32
< 32) return 21;
883 if (PE_HEADER(hModule32
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
886 hInstance
= NE_CreateInstance( pModule
, NULL
, FALSE
);
887 process
= PROCESS_Create( pModule
, cmd_line
, env
,
888 hInstance
, 0, startup
, info
);
889 pTask
= (TDB
*)GlobalLock16( process
->task
);
891 wm
=(WINE_MODREF
*)HeapAlloc(process
->heap
,HEAP_ZERO_MEMORY
,sizeof(*wm
));
892 wm
->type
= MODULE32_PE
;
893 /* NOTE: fixup_imports takes care of the correct order */
894 wm
->next
= process
->modref_list
;
895 wm
->module
= hModule32
;
896 process
->modref_list
= wm
;
897 if (!PE_MapImage( process
, wm
, &ofs
, 0 ))
899 /* FIXME: should destroy the task created and free referenced stuff */
902 pModule
->module32
= wm
->module
;
904 /* FIXME: Yuck. Is there no other good place to do that? */
905 PE_InitTls( pTask
->thdb
);
910 /*********************************************************************
911 * PE_UnloadImage [internal]
913 int PE_UnloadImage( HMODULE32 hModule
)
915 FIXME(win32
,"stub.\n");
916 /* free resources, image, unmap */
920 /* Called if the library is loaded or freed.
921 * NOTE: if a thread attaches a DLL, the current thread will only do
922 * DLL_PROCESS_ATTACH. Only new created threads do DLL_THREAD_ATTACH
925 void PE_InitDLL(WINE_MODREF
*wm
, DWORD type
, LPVOID lpReserved
)
927 if (wm
->type
!=MODULE32_PE
)
929 if (wm
->binfmt
.pe
.flags
& PE_MODREF_NO_DLL_CALLS
)
931 if (type
==DLL_PROCESS_ATTACH
)
933 if (wm
->binfmt
.pe
.flags
& PE_MODREF_PROCESS_ATTACHED
)
936 wm
->binfmt
.pe
.flags
|= PE_MODREF_PROCESS_ATTACHED
;
939 /* DLL_ATTACH_PROCESS:
940 * lpreserved is NULL for dynamic loads, not-NULL for static loads
941 * DLL_DETACH_PROCESS:
942 * lpreserved is NULL if called by FreeLibrary, not-NULL otherwise
943 * the SDK doesn't mention anything for DLL_THREAD_*
946 /* Is this a library? And has it got an entrypoint? */
947 if ((PE_HEADER(wm
->module
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) &&
948 (PE_HEADER(wm
->module
)->OptionalHeader
.AddressOfEntryPoint
)
950 DLLENTRYPROC32 entry
= (void*)RVA_PTR( wm
->module
,OptionalHeader
.AddressOfEntryPoint
);
951 TRACE(relay
, "CallTo32(entryproc=%p,module=%08x,type=%ld,res=%p)\n",
952 entry
, wm
->module
, type
, lpReserved
);
954 entry( wm
->module
, type
, lpReserved
);
958 /************************************************************************
959 * PE_InitTls (internal)
961 * If included, initialises the thread local storages of modules.
962 * Pointers in those structs are not RVAs but real pointers which have been
963 * relocated by do_relocations() already.
965 void PE_InitTls(THDB
*thdb
)
969 IMAGE_NT_HEADERS
*peh
;
972 PIMAGE_TLS_DIRECTORY pdir
;
973 PDB32
*pdb
= thdb
->process
;
976 for (wm
= pdb
->modref_list
;wm
;wm
=wm
->next
) {
977 if (wm
->type
!=MODULE32_PE
)
979 pem
= &(wm
->binfmt
.pe
);
980 peh
= PE_HEADER(wm
->module
);
981 delta
= wm
->module
- peh
->OptionalHeader
.ImageBase
;
982 if (!peh
->OptionalHeader
.DataDirectory
[IMAGE_FILE_THREAD_LOCAL_STORAGE
].VirtualAddress
)
984 FIXME(win32
,"%s has TLS directory.\n",wm
->longname
);
985 pdir
= (LPVOID
)(wm
->module
+ peh
->OptionalHeader
.
986 DataDirectory
[IMAGE_FILE_THREAD_LOCAL_STORAGE
].VirtualAddress
);
989 if (!(pem
->flags
& PE_MODREF_TLS_ALLOCED
)) {
990 pem
->tlsindex
= THREAD_TlsAlloc(thdb
);
991 *pdir
->AddressOfIndex
=pem
->tlsindex
;
993 pem
->flags
|= PE_MODREF_TLS_ALLOCED
;
994 datasize
= pdir
->EndAddressOfRawData
-pdir
->StartAddressOfRawData
;
995 size
= datasize
+ pdir
->SizeOfZeroFill
;
996 mem
=VirtualAlloc(0,size
,MEM_RESERVE
|MEM_COMMIT
,PAGE_READWRITE
);
997 memcpy(mem
,(LPVOID
)pdir
->StartAddressOfRawData
,datasize
);
998 if (pdir
->AddressOfCallBacks
) {
999 PIMAGE_TLS_CALLBACK
*cbs
=
1000 (PIMAGE_TLS_CALLBACK
*)pdir
->AddressOfCallBacks
;
1003 FIXME(win32
, "TLS Callbacks aren't going to be called\n");
1005 /* Don't use TlsSetValue, we are in the wrong thread */
1006 thdb
->tls_array
[pem
->tlsindex
] = mem
;
1010 /****************************************************************************
1011 * DisableThreadLibraryCalls (KERNEL32.74)
1012 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
1014 BOOL32 WINAPI
DisableThreadLibraryCalls(HMODULE32 hModule
)
1018 for (wm
=PROCESS_Current()->modref_list
;wm
;wm
=wm
->next
)
1019 if ((wm
->module
== hModule
) && (wm
->type
==MODULE32_PE
))
1020 wm
->binfmt
.pe
.flags
|=PE_MODREF_NO_DLL_CALLS
;