3 * Copyright 1994 Eric Youndale & Erik Bos
4 * Copyright 1995 Martin von Löwis
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
18 #include <sys/types.h>
31 #include "registers.h"
32 #include "selectors.h"
37 struct w_files
*wine_files
= NULL
;
39 void my_wcstombs(char * result
, u_short
* source
, int len
)
42 /* this used to be isascii, but see isascii implementation in Linux'
44 if(*source
<255) *result
++ = *source
++;
46 printf("Unable to handle unicode right now\n");
53 char * xmmap(char * vaddr
, unsigned int v_size
, unsigned int r_size
,
54 int prot
, int flags
, int fd
, unsigned int file_offset
)
57 /* .bss has no associated storage in the PE file */
62 fprintf(stderr
,"xmmap: %s line %d doesn't support MAP_ANON\n",__FILE__
, __LINE__
);
66 result
= mmap(vaddr
, v_size
, prot
, flags
, fd
, file_offset
);
67 if((unsigned int) result
!= 0xffffffff) return result
;
69 /* Sigh. Alignment must be wrong for mmap. Do this the hard way. */
70 if(!(flags
& MAP_FIXED
)) {
71 vaddr
= (char *)0x40000000;
75 mmap(vaddr
, v_size
, prot
, MAP_ANONYMOUS
| flags
, 0, 0);
76 lseek(fd
, file_offset
, SEEK_SET
);
77 read(fd
, vaddr
, v_size
);
82 void dump_exports(struct PE_Export_Directory
* pe_exports
, unsigned int load_addr
)
88 u_char
** name
, *ename
;
90 Module
= ((char *) load_addr
) + pe_exports
->Name
;
91 dprintf_win32(stddeb
,"\n*******EXPORT DATA*******\nModule name is %s, %ld functions, %ld names\n",
93 pe_exports
->Number_Of_Functions
,
94 pe_exports
->Number_Of_Names
);
96 ordinal
= (u_short
*) (((char *) load_addr
) + (int) pe_exports
->Address_Of_Name_Ordinals
);
97 function
= (u_long
*) (((char *) load_addr
) + (int) pe_exports
->AddressOfFunctions
);
98 name
= (u_char
**) (((char *) load_addr
) + (int) pe_exports
->AddressOfNames
);
100 dprintf_win32(stddeb
,"%-32s Ordinal Virt Addr\n", "Function Name");
101 for(i
=0; i
< pe_exports
->Number_Of_Functions
; i
++)
103 ename
= (char *) (((char *) load_addr
) + (int) *name
++);
104 dprintf_win32(stddeb
,"%-32s %4d %8.8lx\n", ename
, *ordinal
++, *function
++);
108 DWORD
PE_FindExportedFunction(struct w_files
* wpnt
, char* funcName
)
110 struct PE_Export_Directory
* exports
= wpnt
->pe
->pe_export
;
111 unsigned load_addr
= wpnt
->pe
->load_addr
;
114 u_char
** name
, *ename
;
116 if(!exports
)return 0;
117 ordinal
= (u_short
*) (((char *) load_addr
) + (int) exports
->Address_Of_Name_Ordinals
);
118 function
= (u_long
*) (((char *) load_addr
) + (int) exports
->AddressOfFunctions
);
119 name
= (u_char
**) (((char *) load_addr
) + (int) exports
->AddressOfNames
);
120 for(i
=0; i
<exports
->Number_Of_Functions
; i
++)
124 ename
= (char *) (((char *) load_addr
) + (int) *name
);
125 if(strcmp(ename
,funcName
)==0)
126 return load_addr
+*function
;
128 if(funcName
== (int)*ordinal
+ exports
->Base
)
129 return load_addr
+*function
;
138 DWORD
PE_GetProcAddress(HMODULE hModule
, char* function
)
140 struct w_files
*wpnt
;
141 for(wpnt
=wine_files
;wpnt
;wpnt
=wpnt
->next
)
142 if(wpnt
->hModule
==hModule
) break;
147 return RELAY32_GetEntryPoint(wpnt
->builtin
,function
,0);
149 return RELAY32_GetEntryPoint(wpnt
->builtin
,0,(int)function
);
151 return PE_FindExportedFunction(wpnt
,function
);
154 void fixup_imports(struct w_files
* wpnt
)
156 struct PE_Import_Directory
* pe_imp
;
158 unsigned int load_addr
= wpnt
->pe
->load_addr
;
163 /* OK, now dump the import list */
164 dprintf_win32(stddeb
, "\nDumping imports list\n");
166 /* first, count the number of imported non-internal modules */
167 pe_imp
= wpnt
->pe
->pe_import
;
168 for(i
=0;pe_imp
->ModuleName
;pe_imp
++)
171 /* Now, allocate memory for dlls_to_init */
172 ne_mod
= GlobalLock(wpnt
->hModule
);
173 ne_mod
->dlls_to_init
= GLOBAL_Alloc(GMEM_ZEROINIT
,(i
+1) * sizeof(HMODULE
),
174 wpnt
->hModule
, FALSE
, FALSE
, FALSE
);
175 mod_ptr
= GlobalLock(ne_mod
->dlls_to_init
);
176 /* load the modules and put their handles into the list */
177 for(i
=0,pe_imp
= wpnt
->pe
->pe_import
;pe_imp
->ModuleName
;pe_imp
++)
179 char *name
= (char*)load_addr
+pe_imp
->ModuleName
;
180 if(RELAY32_GetBuiltinDLL(name
))
182 mod_ptr
[i
] = LoadModule(name
,(LPVOID
)-1);
183 if(mod_ptr
[i
]<=(HMODULE
)32)
185 fprintf(stderr
,"Module %s not found\n",name
);
190 pe_imp
= wpnt
->pe
->pe_import
;
191 while (pe_imp
->ModuleName
)
194 struct pe_import_name
* pe_name
;
195 unsigned int * import_list
, *thunk_list
;
197 Module
= ((char *) load_addr
) + pe_imp
->ModuleName
;
198 dprintf_win32(stddeb
, "%s\n", Module
);
200 if(pe_imp
->Import_List
!= 0) { /* original microsoft style */
201 dprintf_win32(stddeb
, "Microsoft style imports used\n");
202 import_list
= (unsigned int *)
203 (((unsigned int) load_addr
) + pe_imp
->Import_List
);
204 thunk_list
= (unsigned int *)
205 (((unsigned int) load_addr
) + pe_imp
->Thunk_List
);
210 pe_name
= (struct pe_import_name
*) ((int) load_addr
+ *import_list
);
211 if((unsigned)*import_list
& 0x80000000)
213 int ordinal
=*import_list
& (0x80000000-1);
214 dprintf_win32(stddeb
,"--- Ordinal %s,%d\n", Module
, ordinal
);
215 *thunk_list
= WIN32_GetProcAddress(MODULE_FindModule(Module
),
217 }else{ /* import by name */
218 dprintf_win32(stddeb
, "--- %s %s.%d\n", pe_name
->Name
, Module
, pe_name
->Hint
);
219 #ifndef WINELIB /* FIXME: JBP: Should this happen in libwine.a? */
220 *thunk_list
= WIN32_GetProcAddress(MODULE_FindModule(Module
),
224 fprintf(stderr
,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
229 fprintf(stderr
,"No implementation for %s.%d(%s)\n",Module
,
230 pe_name
->Hint
, pe_name
->Name
);
237 } else { /* Borland style */
238 dprintf_win32(stddeb
, "Borland style imports used\n");
239 thunk_list
= (unsigned int *)
240 (((unsigned int) load_addr
) + pe_imp
->Thunk_List
);
242 pe_name
= (struct pe_import_name
*) ((int) load_addr
+ *thunk_list
);
243 if((unsigned)pe_name
& 0x80000000) {
244 fprintf(stderr
,"Import by ordinal not supported\n");
247 dprintf_win32(stddeb
, "--- %s %s.%d\n", pe_name
->Name
, Module
, pe_name
->Hint
);
248 #ifndef WINELIB /* FIXME: JBP: Should this happen in libwine.a? */
249 /* FIXME: Both calls should be unified into GetProcAddress */
251 *thunk_list
=(unsigned int)RELAY32_GetEntryPoint(Module
,pe_name
->Name
,pe_name
->Hint
);
254 *thunk_list
= WIN32_GetProcAddress(MODULE_FindModule(Module
),
257 fprintf(stderr
,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
260 fprintf(stderr
,"No implementation for %s.%d\n",Module
, pe_name
->Hint
);
268 if(fixup_failed
)exit(1);
271 static void calc_vma_size(struct w_files
*wpnt
)
275 dprintf_win32(stddeb
, "Dump of segment table\n");
276 dprintf_win32(stddeb
, " Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
277 for(i
=0; i
< wpnt
->pe
->pe_header
->coff
.NumberOfSections
; i
++)
279 dprintf_win32(stddeb
, "%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
280 wpnt
->pe
->pe_seg
[i
].Name
,
281 wpnt
->pe
->pe_seg
[i
].Virtual_Size
,
282 wpnt
->pe
->pe_seg
[i
].Virtual_Address
,
283 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
,
284 wpnt
->pe
->pe_seg
[i
].PointerToRawData
,
285 wpnt
->pe
->pe_seg
[i
].PointerToRelocations
,
286 wpnt
->pe
->pe_seg
[i
].PointerToLinenumbers
,
287 wpnt
->pe
->pe_seg
[i
].NumberOfRelocations
,
288 wpnt
->pe
->pe_seg
[i
].NumberOfLinenumbers
,
289 wpnt
->pe
->pe_seg
[i
].Characteristics
);
290 wpnt
->pe
->vma_size
= max(wpnt
->pe
->vma_size
,
291 wpnt
->pe
->pe_seg
[i
].Virtual_Address
+
292 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
);
296 static void do_relocations(struct w_files
*wpnt
)
298 int delta
= wpnt
->pe
->load_addr
- wpnt
->pe
->base_addr
;
299 struct PE_Reloc_Block
*r
= wpnt
->pe
->pe_reloc
;
300 int hdelta
= (delta
>> 16) & 0xFFFF;
301 int ldelta
= delta
& 0xFFFF;
302 /* int reloc_size = */
308 char *page
= (char*)wpnt
->pe
->load_addr
+ r
->PageRVA
;
309 int count
= (r
->BlockSize
- 8)/2;
311 dprintf_fixup(stddeb
, "%x relocations for page %lx\n",
313 /* patching in reverse order */
316 int offset
= r
->Relocations
[i
] & 0xFFF;
317 int type
= r
->Relocations
[i
] >> 12;
318 dprintf_fixup(stddeb
,"patching %x type %x\n", offset
, type
);
321 case IMAGE_REL_BASED_ABSOLUTE
: break;
322 case IMAGE_REL_BASED_HIGH
:
323 *(short*)(page
+offset
) += hdelta
;
325 case IMAGE_REL_BASED_LOW
:
326 *(short*)(page
+offset
) += ldelta
;
328 case IMAGE_REL_BASED_HIGHLOW
:
330 *(int*)(page
+offset
) += delta
;
332 { int h
=*(unsigned short*)(page
+offset
);
333 int l
=r
->Relocations
[++i
];
334 *(unsigned int*)(page
+ offset
) = (h
<<16) + l
+ delta
;
338 case IMAGE_REL_BASED_HIGHADJ
:
339 fprintf(stderr
, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
341 case IMAGE_REL_BASED_MIPS_JMPADDR
:
342 fprintf(stderr
, "Is this a MIPS machine ???\n");
345 fprintf(stderr
, "Unknown fixup type\n");
349 r
= (struct PE_Reloc_Block
*)((char*)r
+ r
->BlockSize
);
357 /**********************************************************************
359 * Load one PE format executable into memory
361 static HINSTANCE
PE_LoadImage( int fd
, struct w_files
*wpnt
)
364 unsigned int load_addr
;
365 struct Directory dir
;
367 wpnt
->pe
= xmalloc(sizeof(struct pe_data
));
368 memset(wpnt
->pe
,0,sizeof(struct pe_data
));
369 wpnt
->pe
->pe_header
= xmalloc(sizeof(struct pe_header_s
));
372 lseek( fd
, wpnt
->mz_header
->ne_offset
, SEEK_SET
);
373 read( fd
, wpnt
->pe
->pe_header
, sizeof(struct pe_header_s
));
376 wpnt
->pe
->pe_seg
= xmalloc(sizeof(struct pe_segment_table
) *
377 wpnt
->pe
->pe_header
->coff
.NumberOfSections
);
378 read( fd
, wpnt
->pe
->pe_seg
, sizeof(struct pe_segment_table
) *
379 wpnt
->pe
->pe_header
->coff
.NumberOfSections
);
381 load_addr
= wpnt
->pe
->pe_header
->opt_coff
.BaseOfImage
;
382 wpnt
->pe
->base_addr
=load_addr
;
383 wpnt
->pe
->vma_size
=0;
384 dprintf_win32(stddeb
, "Load addr is %x\n",load_addr
);
387 /* We use malloc here, while a huge part of that address space does
388 not be supported by actual memory. It has to be contiguous, though.
389 I don't know if mmap("/dev/null"); would do any better.
390 What I'd really like to do is a Win32 style VirtualAlloc/MapViewOfFile
392 load_addr
= wpnt
->pe
->load_addr
= malloc(wpnt
->pe
->vma_size
);
393 dprintf_win32(stddeb
, "Load addr is really %x, range %x\n",
394 wpnt
->pe
->load_addr
, wpnt
->pe
->vma_size
);
397 for(i
=0; i
< wpnt
->pe
->pe_header
->coff
.NumberOfSections
; i
++)
399 /* load only non-BSS segments */
400 if(wpnt
->pe
->pe_seg
[i
].Characteristics
&
401 ~ IMAGE_SCN_TYPE_CNT_UNINITIALIZED_DATA
)
402 if(lseek(fd
,wpnt
->pe
->pe_seg
[i
].PointerToRawData
,SEEK_SET
) == -1
403 || read(fd
,load_addr
+ wpnt
->pe
->pe_seg
[i
].Virtual_Address
,
404 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
)
405 != wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
)
407 fprintf(stderr
,"Failed to load section %x\n", i
);
410 result
= load_addr
+ wpnt
->pe
->pe_seg
[i
].Virtual_Address
;
414 result
= (int)xmmap((char *)0, wpnt
->pe
->pe_seg
[i
].Virtual_Size
,
415 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
, 7,
416 MAP_PRIVATE
, fd
, wpnt
->pe
->pe_seg
[i
].PointerToRawData
);
417 load_addr
= (unsigned int) result
- wpnt
->pe
->pe_seg
[i
].Virtual_Address
;
419 result
= (int)xmmap((char *) load_addr
+ wpnt
->pe
->pe_seg
[i
].Virtual_Address
,
420 wpnt
->pe
->pe_seg
[i
].Virtual_Size
,
421 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
, 7, MAP_PRIVATE
| MAP_FIXED
,
422 fd
, wpnt
->pe
->pe_seg
[i
].PointerToRawData
);
425 fprintf(stderr
,"Could not load section %x to desired address %lx\n",
426 i
, load_addr
+wpnt
->pe
->pe_seg
[i
].Virtual_Address
);
427 fprintf(stderr
,"Need to implement relocations now\n");
432 if(strcmp(wpnt
->pe
->pe_seg
[i
].Name
, ".bss") == 0)
433 memset((void *)result
, 0,
434 wpnt
->pe
->pe_seg
[i
].Virtual_Size
?
435 wpnt
->pe
->pe_seg
[i
].Virtual_Size
:
436 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
);
438 if(strcmp(wpnt
->pe
->pe_seg
[i
].Name
, ".idata") == 0)
439 wpnt
->pe
->pe_import
= (struct PE_Import_Directory
*) result
;
441 if(strcmp(wpnt
->pe
->pe_seg
[i
].Name
, ".edata") == 0)
442 wpnt
->pe
->pe_export
= (struct PE_Export_Directory
*) result
;
444 if(strcmp(wpnt
->pe
->pe_seg
[i
].Name
, ".rsrc") == 0) {
445 wpnt
->pe
->pe_resource
= (struct PE_Resource_Directory
*) result
;
447 /* FIXME pe->resource_offset should be deleted from structure if this
448 ifdef doesn't break anything */
449 /* save offset for PE_FindResource */
450 wpnt
->pe
->resource_offset
= wpnt
->pe
->pe_seg
[i
].Virtual_Address
-
451 wpnt
->pe
->pe_seg
[i
].PointerToRawData
;
454 if(strcmp(wpnt
->pe
->pe_seg
[i
].Name
, ".reloc") == 0)
455 wpnt
->pe
->pe_reloc
= (struct PE_Reloc_Block
*) result
;
459 /* There is word that the actual loader does not care about the
460 section names, and only goes for the DataDirectory */
461 dir
=wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
[IMAGE_FILE_EXPORT_DIRECTORY
];
464 if(wpnt
->pe
->pe_export
&&
465 wpnt
->pe
->pe_export
!=load_addr
+dir
.Virtual_address
)
466 fprintf(stderr
,"wrong export directory??\n");
467 /* always trust the directory */
468 wpnt
->pe
->pe_export
= load_addr
+dir
.Virtual_address
;
471 dir
=wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
];
474 if(wpnt
->pe
->pe_import
&&
475 wpnt
->pe
->pe_import
!=load_addr
+dir
.Virtual_address
)
476 fprintf(stderr
,"wrong import directory??\n");
477 wpnt
->pe
->pe_import
= load_addr
+dir
.Virtual_address
;
480 dir
=wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
[IMAGE_FILE_RESOURCE_DIRECTORY
];
483 if(wpnt
->pe
->pe_resource
&&
484 wpnt
->pe
->pe_resource
!=load_addr
+dir
.Virtual_address
)
485 fprintf(stderr
,"wrong resource directory??\n");
486 wpnt
->pe
->pe_resource
= load_addr
+dir
.Virtual_address
;
489 dir
=wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
[IMAGE_FILE_BASE_RELOCATION_TABLE
];
492 if(wpnt
->pe
->pe_reloc
&&
493 wpnt
->pe
->pe_reloc
!=load_addr
+dir
.Virtual_address
)
494 fprintf(stderr
,"wrong relocation list??\n");
495 wpnt
->pe
->pe_reloc
= load_addr
+dir
.Virtual_address
;
498 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
499 [IMAGE_FILE_EXCEPTION_DIRECTORY
].Size
)
500 dprintf_win32(stdnimp
,"Exception directory ignored\n");
502 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
503 [IMAGE_FILE_SECURITY_DIRECTORY
].Size
)
504 dprintf_win32(stdnimp
,"Security directory ignored\n");
506 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
507 [IMAGE_FILE_DEBUG_DIRECTORY
].Size
)
508 dprintf_win32(stdnimp
,"Debug directory ignored\n");
510 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
511 [IMAGE_FILE_DESCRIPTION_STRING
].Size
)
512 dprintf_win32(stdnimp
,"Description string ignored\n");
514 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
515 [IMAGE_FILE_MACHINE_VALUE
].Size
)
516 dprintf_win32(stdnimp
,"Machine Value ignored\n");
518 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
519 [IMAGE_FILE_THREAD_LOCAL_STORAGE
].Size
)
520 dprintf_win32(stdnimp
,"Thread local storage ignored\n");
522 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
523 [IMAGE_FILE_CALLBACK_DIRECTORY
].Size
)
524 dprintf_win32(stdnimp
,"Callback directory ignored\n");
527 if(wpnt
->pe
->pe_import
) fixup_imports(wpnt
);
528 if(wpnt
->pe
->pe_export
) dump_exports(wpnt
->pe
->pe_export
,load_addr
);
529 if(wpnt
->pe
->pe_reloc
) do_relocations(wpnt
);
531 wpnt
->hinstance
= (HINSTANCE
)0x8000;
532 wpnt
->load_addr
= load_addr
;
533 return (wpnt
->hinstance
);
536 HINSTANCE
MODULE_CreateInstance(HMODULE hModule
,LOADPARAMS
*params
);
537 void InitTask(struct sigcontext_struct context
);
539 HINSTANCE
PE_LoadModule(int fd
, OFSTRUCT
*ofs
, LOADPARAMS
* params
)
541 struct w_files
*wpnt
;
544 LOADEDFILEINFO
*pFileInfo
;
545 SEGTABLEENTRY
*pSegment
;
553 wpnt
=xmalloc(sizeof(struct w_files
));
560 lseek(fd
,0,SEEK_SET
);
561 wpnt
->mz_header
=xmalloc(sizeof(struct mz_header_s
));
562 read(fd
,wpnt
->mz_header
,sizeof(struct mz_header_s
));
564 size
=sizeof(NE_MODULE
) +
565 /* loaded file info */
566 sizeof(LOADEDFILEINFO
) + strlen(ofs
->szPathName
) +
567 /* segment table: DS,CS */
568 2 * sizeof(SEGTABLEENTRY
) +
571 /* several empty tables */
574 hModule
= GlobalAlloc( GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
575 wpnt
->hModule
=hModule
;
576 if (!hModule
) return (HINSTANCE
)11; /* invalid exe */
578 FarSetOwner( hModule
, hModule
);
580 pModule
= (NE_MODULE
*)GlobalLock(hModule
);
582 /* Set all used entries */
583 pModule
->magic
=PE_SIGNATURE
;
590 /* Who wants to LocalAlloc for a PE Module? */
591 pModule
->heap_size
=0x1000;
592 pModule
->stack_size
=0xF000;
593 pModule
->seg_count
=1;
594 pModule
->modref_count
=0;
595 pModule
->nrname_size
=0;
596 pModule
->seg_table
=sizeof(NE_MODULE
)+
597 sizeof(LOADEDFILEINFO
)+strlen(ofs
->szPathName
);
598 pModule
->fileinfo
=sizeof(NE_MODULE
);
599 pModule
->os_flags
=NE_OSFLAGS_WINDOWS
;
600 pModule
->expected_version
=0x30A;
602 pFileInfo
=(LOADEDFILEINFO
*)(pModule
+ 1);
603 pFileInfo
->length
= sizeof(LOADEDFILEINFO
)+strlen(ofs
->szPathName
)-1;
604 strcpy(pFileInfo
->filename
,ofs
->szPathName
);
606 pSegment
=(SEGTABLEENTRY
*)((char*)pFileInfo
+pFileInfo
->length
+1);
607 pModule
->dgroup_entry
=(int)pSegment
-(int)pModule
;
609 pSegment
->flags
=NE_SEGFLAGS_DATA
;
610 pSegment
->minsize
=0x1000;
613 cts
=(DWORD
)GetWndProcEntry16("Win32CallToStart");
615 pSegment
->selector
=(void*)cts
;
618 pSegment
->selector
=cts
>>16;
619 pModule
->ip
=cts
& 0xFFFF;
623 pStr
=(char*)pSegment
;
624 pModule
->name_table
=(int)pStr
-(int)pModule
;
625 strcpy(pStr
,"\x08W32SXXXX");
628 /* All tables zero terminated */
629 pModule
->res_table
=pModule
->import_table
=pModule
->entry_table
=
630 (int)pStr
-(int)pModule
;
632 MODULE_RegisterModule(hModule
);
634 PE_LoadImage( fd
, wpnt
);
636 pModule
->heap_size
=0x1000;
637 pModule
->stack_size
=0xE000;
639 /* CreateInstance allocates now 64KB */
640 hInstance
=MODULE_CreateInstance(hModule
,NULL
/* FIX: NULL? really? */);
641 wpnt
->hinstance
=hInstance
;
643 if (wpnt
->pe
->pe_export
) {
644 pStr
= ((unsigned char *)(wpnt
->load_addr
))+wpnt
->pe
->pe_export
->Name
;
645 wpnt
->name
= xstrdup(pStr
);
647 wpnt
->name
= xstrdup( ofs
->szPathName
);
650 wpnt
->next
=wine_files
;
653 /* FIXME: Is this really the correct place to initialise the DLL? */
654 if ((wpnt
->pe
->pe_header
->coff
.Characteristics
& IMAGE_FILE_DLL
)) {
657 TASK_CreateTask(hModule
,hInstance
,0,
658 params
->hEnvironment
,(LPSTR
)PTR_SEG_TO_LIN(params
->cmdLine
),
659 *((WORD
*)PTR_SEG_TO_LIN(params
->showCmd
)+1));
664 int USER_InitApp(HINSTANCE hInstance
);
665 void PE_InitTEB(int hTEB
);
667 void PE_Win32CallToStart(struct sigcontext_struct context
)
670 struct w_files
*wpnt
=wine_files
;
671 dprintf_win32(stddeb
,"Going to start Win32 program\n");
673 USER_InitApp(wpnt
->hModule
);
674 fs
=(int)GlobalAlloc(GHND
,0x10000);
676 __asm__
__volatile__("movw %w0,%%fs"::"r" (fs
));
677 ((void(*)())(wpnt
->load_addr
+wpnt
->pe
->pe_header
->opt_coff
.AddressOfEntryPoint
))();
680 int PE_UnloadImage(struct w_files
*wpnt
)
682 printf("PEunloadImage() called!\n");
683 /* free resources, image, unmap */
687 void PE_InitDLL(HMODULE hModule
)
689 struct w_files
*wpnt
;
690 hModule
= GetExePtr(hModule
);
691 for(wpnt
= wine_files
;wpnt
&& wpnt
->hModule
!= hModule
;
692 wpnt
= wpnt
->next
) /*nothing*/;
693 if(!wpnt
|| wpnt
->initialised
)
695 /* FIXME: What are the correct values for parameters 2 and 3? */
697 /* Is this a library? */
698 if (wpnt
->pe
->pe_header
->coff
.Characteristics
& IMAGE_FILE_DLL
) {
699 /* Should call DLLEntryPoint here */
700 printf("InitPEDLL() called!\n");
701 wpnt
->initialised
= 1;
702 ((void(*)())(wpnt
->load_addr
+wpnt
->pe
->pe_header
->opt_coff
.AddressOfEntryPoint
))(wpnt
->hModule
, 0, 0);
707 /* FIXME: This stuff is all on a "well it works" basis. An implementation
708 based on some kind of documentation would be greatly appreciated :-) */
715 struct TEB
*TEBDSAlias
;
720 void PE_InitTEB(int hTEB
)
725 pTask
= (TDB
*)(GlobalLock(GetCurrentTask() & 0xffff));
726 pTEB
= (TEB
*)(PTR_SEG_OFF_TO_LIN(hTEB
, 0));
727 pTEB
->stack
= (void *)(GlobalLock(pTask
->hStack32
));
728 pTEB
->Except
= (void *)(-1);
729 pTEB
->TEBDSAlias
= pTEB
;
730 pTEB
->taskid
= getpid();
733 void PE_InitializeDLLs(HMODULE hModule
)
737 pModule
= (NE_MODULE
*)GlobalLock( GetExePtr(hModule
) );
738 if (pModule
->dlls_to_init
)
740 HANDLE to_init
= pModule
->dlls_to_init
;
741 pModule
->dlls_to_init
= 0;
742 for (pDLL
= (HMODULE
*)GlobalLock( to_init
); *pDLL
; pDLL
++)
744 PE_InitializeDLLs( *pDLL
);
747 GlobalFree( to_init
);
749 PE_InitDLL( hModule
);