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"
36 struct w_files
*wine_files
= NULL
;
38 void my_wcstombs(char * result
, u_short
* source
, int len
)
41 /* this used to be isascii, but see isascii implementation in Linux'
43 if(*source
<255) *result
++ = *source
++;
45 printf("Unable to handle unicode right now\n");
52 char * xmmap(char * vaddr
, unsigned int v_size
, unsigned int r_size
,
53 int prot
, int flags
, int fd
, unsigned int file_offset
)
56 /* .bss has no associated storage in the PE file */
61 fprintf(stderr
,"xmmap: %s line %d doesn't support MAP_ANON\n",__FILE__
, __LINE__
);
65 result
= mmap(vaddr
, v_size
, prot
, flags
, fd
, file_offset
);
66 if((unsigned int) result
!= 0xffffffff) return result
;
68 /* Sigh. Alignment must be wrong for mmap. Do this the hard way. */
69 if(!(flags
& MAP_FIXED
)) {
70 vaddr
= (char *)0x40000000;
74 mmap(vaddr
, v_size
, prot
, MAP_ANONYMOUS
| flags
, 0, 0);
75 lseek(fd
, file_offset
, SEEK_SET
);
76 read(fd
, vaddr
, v_size
);
81 void dump_exports(struct PE_Export_Directory
* pe_exports
, unsigned int load_addr
)
87 u_char
** name
, *ename
;
89 Module
= ((char *) load_addr
) + pe_exports
->Name
;
90 dprintf_win32(stddeb
,"\n*******EXPORT DATA*******\nModule name is %s, %ld functions, %ld names\n",
92 pe_exports
->Number_Of_Functions
,
93 pe_exports
->Number_Of_Names
);
95 ordinal
= (u_short
*) (((char *) load_addr
) + (int) pe_exports
->Address_Of_Name_Ordinals
);
96 function
= (u_long
*) (((char *) load_addr
) + (int) pe_exports
->AddressOfFunctions
);
97 name
= (u_char
**) (((char *) load_addr
) + (int) pe_exports
->AddressOfNames
);
99 dprintf_win32(stddeb
,"%-32s Ordinal Virt Addr\n", "Function Name");
100 for(i
=0; i
< pe_exports
->Number_Of_Functions
; i
++)
102 ename
= (char *) (((char *) load_addr
) + (int) *name
++);
103 dprintf_win32(stddeb
,"%-32s %4d %8.8lx\n", ename
, *ordinal
++, *function
++);
107 DWORD
PE_FindExportedFunction(struct w_files
* wpnt
, char* funcName
)
109 struct PE_Export_Directory
* exports
= wpnt
->pe
->pe_export
;
110 unsigned load_addr
= wpnt
->pe
->load_addr
;
113 u_char
** name
, *ename
;
115 if(!exports
)return 0;
116 ordinal
= (u_short
*) (((char *) load_addr
) + (int) exports
->Address_Of_Name_Ordinals
);
117 function
= (u_long
*) (((char *) load_addr
) + (int) exports
->AddressOfFunctions
);
118 name
= (u_char
**) (((char *) load_addr
) + (int) exports
->AddressOfNames
);
119 for(i
=0; i
<exports
->Number_Of_Functions
; i
++)
123 ename
= (char *) (((char *) load_addr
) + (int) *name
);
124 if(strcmp(ename
,funcName
)==0)
125 return load_addr
+*function
;
127 if(funcName
== (int)*ordinal
+ exports
->Base
)
128 return load_addr
+*function
;
137 DWORD
PE_GetProcAddress(HMODULE hModule
, char* function
)
139 struct w_files
*wpnt
;
140 for(wpnt
=wine_files
;wpnt
;wpnt
=wpnt
->next
)
141 if(wpnt
->hModule
==hModule
) break;
146 return RELAY32_GetEntryPoint(wpnt
->builtin
,function
,0);
148 return RELAY32_GetEntryPoint(wpnt
->builtin
,0,(int)function
);
150 return PE_FindExportedFunction(wpnt
,function
);
153 void fixup_imports(struct w_files
* wpnt
)
155 struct PE_Import_Directory
* pe_imp
;
157 unsigned int load_addr
= wpnt
->pe
->load_addr
;
162 /* OK, now dump the import list */
163 dprintf_win32(stddeb
, "\nDumping imports list\n");
165 /* first, count the number of imported non-internal modules */
166 pe_imp
= wpnt
->pe
->pe_import
;
167 for(i
=0;pe_imp
->ModuleName
;pe_imp
++)
170 /* Now, allocate memory for dlls_to_init */
171 ne_mod
= GlobalLock(wpnt
->hModule
);
172 ne_mod
->dlls_to_init
= GLOBAL_Alloc(GMEM_ZEROINIT
,(i
+1) * sizeof(HMODULE
),
173 wpnt
->hModule
, FALSE
, FALSE
, FALSE
);
174 mod_ptr
= GlobalLock(ne_mod
->dlls_to_init
);
175 /* load the modules and put their handles into the list */
176 for(i
=0,pe_imp
= wpnt
->pe
->pe_import
;pe_imp
->ModuleName
;pe_imp
++)
178 char *name
= (char*)load_addr
+pe_imp
->ModuleName
;
179 if(RELAY32_GetBuiltinDLL(name
))
181 mod_ptr
[i
] = LoadModule(name
,(LPVOID
)-1);
182 if(mod_ptr
[i
]<=(HMODULE
)32)
184 fprintf(stderr
,"Module %s not found\n",name
);
189 pe_imp
= wpnt
->pe
->pe_import
;
190 while (pe_imp
->ModuleName
)
193 struct pe_import_name
* pe_name
;
194 unsigned int * import_list
, *thunk_list
;
196 Module
= ((char *) load_addr
) + pe_imp
->ModuleName
;
197 dprintf_win32(stddeb
, "%s\n", Module
);
199 if(pe_imp
->Import_List
!= 0) { /* original microsoft style */
200 dprintf_win32(stddeb
, "Microsoft style imports used\n");
201 import_list
= (unsigned int *)
202 (((unsigned int) load_addr
) + pe_imp
->Import_List
);
203 thunk_list
= (unsigned int *)
204 (((unsigned int) load_addr
) + pe_imp
->Thunk_List
);
209 pe_name
= (struct pe_import_name
*) ((int) load_addr
+ ((unsigned)*import_list
& ~0x80000000));
210 if((unsigned)*import_list
& 0x80000000)
212 int ordinal
=*import_list
& (0x80000000-1);
213 dprintf_win32(stddeb
,"--- Ordinal %s,%d\n", Module
, ordinal
);
214 *thunk_list
= WIN32_GetProcAddress(MODULE_FindModule(Module
),
216 }else{ /* import by name */
217 dprintf_win32(stddeb
, "--- %s %s.%d\n", pe_name
->Name
, Module
, pe_name
->Hint
);
218 #ifndef WINELIB /* FIXME: JBP: Should this happen in libwine.a? */
219 *thunk_list
= WIN32_GetProcAddress(MODULE_FindModule(Module
),
223 fprintf(stderr
,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
228 fprintf(stderr
,"No implementation for %s.%d(%s)\n",Module
,
229 pe_name
->Hint
, pe_name
->Name
);
236 } else { /* Borland style */
237 dprintf_win32(stddeb
, "Borland style imports used\n");
238 thunk_list
= (unsigned int *)
239 (((unsigned int) load_addr
) + pe_imp
->Thunk_List
);
241 pe_name
= (struct pe_import_name
*) ((int) load_addr
+ *thunk_list
);
242 if((unsigned)pe_name
& 0x80000000) {
243 fprintf(stderr
,"Import by ordinal not supported\n");
246 dprintf_win32(stddeb
, "--- %s %s.%d\n", pe_name
->Name
, Module
, pe_name
->Hint
);
247 #ifndef WINELIB /* FIXME: JBP: Should this happen in libwine.a? */
248 /* FIXME: Both calls should be unified into GetProcAddress */
250 *thunk_list
=(unsigned int)RELAY32_GetEntryPoint(Module
,pe_name
->Name
,pe_name
->Hint
);
253 *thunk_list
= WIN32_GetProcAddress(MODULE_FindModule(Module
),
256 fprintf(stderr
,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
259 fprintf(stderr
,"No implementation for %s.%d\n",Module
, pe_name
->Hint
);
267 if(fixup_failed
)exit(1);
270 static void calc_vma_size(struct w_files
*wpnt
)
274 dprintf_win32(stddeb
, "Dump of segment table\n");
275 dprintf_win32(stddeb
, " Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
276 for(i
=0; i
< wpnt
->pe
->pe_header
->coff
.NumberOfSections
; i
++)
278 dprintf_win32(stddeb
, "%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
279 wpnt
->pe
->pe_seg
[i
].Name
,
280 wpnt
->pe
->pe_seg
[i
].Virtual_Size
,
281 wpnt
->pe
->pe_seg
[i
].Virtual_Address
,
282 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
,
283 wpnt
->pe
->pe_seg
[i
].PointerToRawData
,
284 wpnt
->pe
->pe_seg
[i
].PointerToRelocations
,
285 wpnt
->pe
->pe_seg
[i
].PointerToLinenumbers
,
286 wpnt
->pe
->pe_seg
[i
].NumberOfRelocations
,
287 wpnt
->pe
->pe_seg
[i
].NumberOfLinenumbers
,
288 wpnt
->pe
->pe_seg
[i
].Characteristics
);
289 wpnt
->pe
->vma_size
= max(wpnt
->pe
->vma_size
,
290 wpnt
->pe
->pe_seg
[i
].Virtual_Address
+
291 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
);
295 static void do_relocations(struct w_files
*wpnt
)
297 int delta
= wpnt
->pe
->load_addr
- wpnt
->pe
->base_addr
;
298 struct PE_Reloc_Block
*r
= wpnt
->pe
->pe_reloc
;
299 int hdelta
= (delta
>> 16) & 0xFFFF;
300 int ldelta
= delta
& 0xFFFF;
301 /* int reloc_size = */
307 char *page
= (char*)wpnt
->pe
->load_addr
+ r
->PageRVA
;
308 int count
= (r
->BlockSize
- 8)/2;
310 dprintf_fixup(stddeb
, "%x relocations for page %lx\n",
312 /* patching in reverse order */
315 int offset
= r
->Relocations
[i
] & 0xFFF;
316 int type
= r
->Relocations
[i
] >> 12;
317 dprintf_fixup(stddeb
,"patching %x type %x\n", offset
, type
);
320 case IMAGE_REL_BASED_ABSOLUTE
: break;
321 case IMAGE_REL_BASED_HIGH
:
322 *(short*)(page
+offset
) += hdelta
;
324 case IMAGE_REL_BASED_LOW
:
325 *(short*)(page
+offset
) += ldelta
;
327 case IMAGE_REL_BASED_HIGHLOW
:
329 *(int*)(page
+offset
) += delta
;
331 { int h
=*(unsigned short*)(page
+offset
);
332 int l
=r
->Relocations
[++i
];
333 *(unsigned int*)(page
+ offset
) = (h
<<16) + l
+ delta
;
337 case IMAGE_REL_BASED_HIGHADJ
:
338 fprintf(stderr
, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
340 case IMAGE_REL_BASED_MIPS_JMPADDR
:
341 fprintf(stderr
, "Is this a MIPS machine ???\n");
344 fprintf(stderr
, "Unknown fixup type\n");
348 r
= (struct PE_Reloc_Block
*)((char*)r
+ r
->BlockSize
);
356 /**********************************************************************
358 * Load one PE format executable into memory
360 static HINSTANCE
PE_LoadImage( int fd
, struct w_files
*wpnt
)
363 unsigned int load_addr
;
364 struct Directory dir
;
366 wpnt
->pe
= xmalloc(sizeof(struct pe_data
));
367 memset(wpnt
->pe
,0,sizeof(struct pe_data
));
368 wpnt
->pe
->pe_header
= xmalloc(sizeof(struct pe_header_s
));
371 lseek( fd
, wpnt
->mz_header
->ne_offset
, SEEK_SET
);
372 read( fd
, wpnt
->pe
->pe_header
, sizeof(struct pe_header_s
));
375 wpnt
->pe
->pe_seg
= xmalloc(sizeof(struct pe_segment_table
) *
376 wpnt
->pe
->pe_header
->coff
.NumberOfSections
);
377 read( fd
, wpnt
->pe
->pe_seg
, sizeof(struct pe_segment_table
) *
378 wpnt
->pe
->pe_header
->coff
.NumberOfSections
);
380 load_addr
= wpnt
->pe
->pe_header
->opt_coff
.BaseOfImage
;
381 wpnt
->pe
->base_addr
=load_addr
;
382 wpnt
->pe
->vma_size
=0;
383 dprintf_win32(stddeb
, "Load addr is %x\n",load_addr
);
386 /* We use malloc here, while a huge part of that address space does
387 not be supported by actual memory. It has to be contiguous, though.
388 I don't know if mmap("/dev/null"); would do any better.
389 What I'd really like to do is a Win32 style VirtualAlloc/MapViewOfFile
391 load_addr
= wpnt
->pe
->load_addr
= malloc(wpnt
->pe
->vma_size
);
392 dprintf_win32(stddeb
, "Load addr is really %x, range %x\n",
393 wpnt
->pe
->load_addr
, wpnt
->pe
->vma_size
);
396 for(i
=0; i
< wpnt
->pe
->pe_header
->coff
.NumberOfSections
; i
++)
398 /* load only non-BSS segments */
399 if(wpnt
->pe
->pe_seg
[i
].Characteristics
&
400 ~ IMAGE_SCN_TYPE_CNT_UNINITIALIZED_DATA
)
401 if(lseek(fd
,wpnt
->pe
->pe_seg
[i
].PointerToRawData
,SEEK_SET
) == -1
402 || read(fd
,load_addr
+ wpnt
->pe
->pe_seg
[i
].Virtual_Address
,
403 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
)
404 != wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
)
406 fprintf(stderr
,"Failed to load section %x\n", i
);
409 result
= load_addr
+ wpnt
->pe
->pe_seg
[i
].Virtual_Address
;
413 result
= (int)xmmap((char *)0, wpnt
->pe
->pe_seg
[i
].Virtual_Size
,
414 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
, 7,
415 MAP_PRIVATE
, fd
, wpnt
->pe
->pe_seg
[i
].PointerToRawData
);
416 load_addr
= (unsigned int) result
- wpnt
->pe
->pe_seg
[i
].Virtual_Address
;
418 result
= (int)xmmap((char *) load_addr
+ wpnt
->pe
->pe_seg
[i
].Virtual_Address
,
419 wpnt
->pe
->pe_seg
[i
].Virtual_Size
,
420 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
, 7, MAP_PRIVATE
| MAP_FIXED
,
421 fd
, wpnt
->pe
->pe_seg
[i
].PointerToRawData
);
424 fprintf(stderr
,"Could not load section %x to desired address %lx\n",
425 i
, load_addr
+wpnt
->pe
->pe_seg
[i
].Virtual_Address
);
426 fprintf(stderr
,"Need to implement relocations now\n");
431 if(strcmp(wpnt
->pe
->pe_seg
[i
].Name
, ".bss") == 0)
432 memset((void *)result
, 0,
433 wpnt
->pe
->pe_seg
[i
].Virtual_Size
?
434 wpnt
->pe
->pe_seg
[i
].Virtual_Size
:
435 wpnt
->pe
->pe_seg
[i
].Size_Of_Raw_Data
);
437 if(strcmp(wpnt
->pe
->pe_seg
[i
].Name
, ".idata") == 0)
438 wpnt
->pe
->pe_import
= (struct PE_Import_Directory
*) result
;
440 if(strcmp(wpnt
->pe
->pe_seg
[i
].Name
, ".edata") == 0)
441 wpnt
->pe
->pe_export
= (struct PE_Export_Directory
*) result
;
443 if(strcmp(wpnt
->pe
->pe_seg
[i
].Name
, ".rsrc") == 0) {
444 wpnt
->pe
->pe_resource
= (struct PE_Resource_Directory
*) result
;
446 /* FIXME pe->resource_offset should be deleted from structure if this
447 ifdef doesn't break anything */
448 /* save offset for PE_FindResource */
449 wpnt
->pe
->resource_offset
= wpnt
->pe
->pe_seg
[i
].Virtual_Address
-
450 wpnt
->pe
->pe_seg
[i
].PointerToRawData
;
453 if(strcmp(wpnt
->pe
->pe_seg
[i
].Name
, ".reloc") == 0)
454 wpnt
->pe
->pe_reloc
= (struct PE_Reloc_Block
*) result
;
458 /* There is word that the actual loader does not care about the
459 section names, and only goes for the DataDirectory */
460 dir
=wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
[IMAGE_FILE_EXPORT_DIRECTORY
];
463 if(wpnt
->pe
->pe_export
&&
464 wpnt
->pe
->pe_export
!=load_addr
+dir
.Virtual_address
)
465 fprintf(stderr
,"wrong export directory??\n");
466 /* always trust the directory */
467 wpnt
->pe
->pe_export
= load_addr
+dir
.Virtual_address
;
470 dir
=wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
];
473 if(wpnt
->pe
->pe_import
&&
474 wpnt
->pe
->pe_import
!=load_addr
+dir
.Virtual_address
)
475 fprintf(stderr
,"wrong import directory??\n");
476 wpnt
->pe
->pe_import
= load_addr
+dir
.Virtual_address
;
479 dir
=wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
[IMAGE_FILE_RESOURCE_DIRECTORY
];
482 if(wpnt
->pe
->pe_resource
&&
483 wpnt
->pe
->pe_resource
!=load_addr
+dir
.Virtual_address
)
484 fprintf(stderr
,"wrong resource directory??\n");
485 wpnt
->pe
->pe_resource
= load_addr
+dir
.Virtual_address
;
488 dir
=wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
[IMAGE_FILE_BASE_RELOCATION_TABLE
];
491 if(wpnt
->pe
->pe_reloc
&&
492 wpnt
->pe
->pe_reloc
!=load_addr
+dir
.Virtual_address
)
493 fprintf(stderr
,"wrong relocation list??\n");
494 wpnt
->pe
->pe_reloc
= load_addr
+dir
.Virtual_address
;
497 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
498 [IMAGE_FILE_EXCEPTION_DIRECTORY
].Size
)
499 dprintf_win32(stdnimp
,"Exception directory ignored\n");
501 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
502 [IMAGE_FILE_SECURITY_DIRECTORY
].Size
)
503 dprintf_win32(stdnimp
,"Security directory ignored\n");
505 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
506 [IMAGE_FILE_DEBUG_DIRECTORY
].Size
)
507 dprintf_win32(stdnimp
,"Debug directory ignored\n");
509 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
510 [IMAGE_FILE_DESCRIPTION_STRING
].Size
)
511 dprintf_win32(stdnimp
,"Description string ignored\n");
513 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
514 [IMAGE_FILE_MACHINE_VALUE
].Size
)
515 dprintf_win32(stdnimp
,"Machine Value ignored\n");
517 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
518 [IMAGE_FILE_THREAD_LOCAL_STORAGE
].Size
)
519 dprintf_win32(stdnimp
,"Thread local storage ignored\n");
521 if(wpnt
->pe
->pe_header
->opt_coff
.DataDirectory
522 [IMAGE_FILE_CALLBACK_DIRECTORY
].Size
)
523 dprintf_win32(stdnimp
,"Callback directory ignored\n");
526 if(wpnt
->pe
->pe_import
) fixup_imports(wpnt
);
527 if(wpnt
->pe
->pe_export
) dump_exports(wpnt
->pe
->pe_export
,load_addr
);
528 if(wpnt
->pe
->pe_reloc
) do_relocations(wpnt
);
530 wpnt
->hinstance
= (HINSTANCE
)0x8000;
531 wpnt
->load_addr
= load_addr
;
532 return (wpnt
->hinstance
);
535 HINSTANCE
MODULE_CreateInstance(HMODULE hModule
,LOADPARAMS
*params
);
536 void InitTask(struct sigcontext_struct context
);
538 HINSTANCE
PE_LoadModule( int fd
, OFSTRUCT
*ofs
, LOADPARAMS
* params
)
540 struct w_files
*wpnt
;
543 SEGTABLEENTRY
*pSegment
;
551 wpnt
=xmalloc(sizeof(struct w_files
));
558 lseek(fd
,0,SEEK_SET
);
559 wpnt
->mz_header
=xmalloc(sizeof(struct mz_header_s
));
560 read(fd
,wpnt
->mz_header
,sizeof(struct mz_header_s
));
562 of_size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
)
563 + strlen(ofs
->szPathName
) + 1;
564 size
= sizeof(NE_MODULE
) +
565 /* loaded file info */
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
=NE_SIGNATURE
;
586 pModule
->flags
=NE_FFLAGS_WIN32
;
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
) + of_size
;
597 pModule
->fileinfo
=sizeof(NE_MODULE
);
598 pModule
->os_flags
=NE_OSFLAGS_WINDOWS
;
599 pModule
->expected_version
=0x30A;
601 /* Set loaded file information */
602 memcpy( pModule
+ 1, ofs
, of_size
);
603 ((OFSTRUCT
*)(pModule
+1))->cBytes
= of_size
- 1;
605 pSegment
=(SEGTABLEENTRY
*)((char*)(pModule
+ 1) + of_size
);
606 pModule
->dgroup_entry
=(int)pSegment
-(int)pModule
;
608 pSegment
->flags
=NE_SEGFLAGS_DATA
;
609 pSegment
->minsize
=0x1000;
612 cts
=(DWORD
)MODULE_GetWndProcEntry16("Win32CallToStart");
614 pSegment
->selector
=(void*)cts
;
617 pSegment
->selector
=cts
>>16;
618 pModule
->ip
=cts
& 0xFFFF;
622 pStr
=(char*)pSegment
;
623 pModule
->name_table
=(int)pStr
-(int)pModule
;
624 strcpy(pStr
,"\x08W32SXXXX");
627 /* All tables zero terminated */
628 pModule
->res_table
=pModule
->import_table
=pModule
->entry_table
=
629 (int)pStr
-(int)pModule
;
631 MODULE_RegisterModule(hModule
);
633 PE_LoadImage( fd
, wpnt
);
635 pModule
->heap_size
=0x1000;
636 pModule
->stack_size
=0xE000;
638 /* CreateInstance allocates now 64KB */
639 hInstance
=MODULE_CreateInstance(hModule
,NULL
/* FIX: NULL? really? */);
640 wpnt
->hinstance
=hInstance
;
642 if (wpnt
->pe
->pe_export
) {
643 pStr
= ((unsigned char *)(wpnt
->load_addr
))+wpnt
->pe
->pe_export
->Name
;
644 wpnt
->name
= xstrdup(pStr
);
646 wpnt
->name
= xstrdup( ofs
->szPathName
);
649 wpnt
->next
=wine_files
;
652 /* FIXME: Is this really the correct place to initialise the DLL? */
653 if ((wpnt
->pe
->pe_header
->coff
.Characteristics
& IMAGE_FILE_DLL
)) {
656 TASK_CreateTask(hModule
,hInstance
,0,
657 params
->hEnvironment
,(LPSTR
)PTR_SEG_TO_LIN(params
->cmdLine
),
658 *((WORD
*)PTR_SEG_TO_LIN(params
->showCmd
)+1));
663 int USER_InitApp(HINSTANCE hInstance
);
664 void PE_InitTEB(int hTEB
);
666 void PE_Win32CallToStart(struct sigcontext_struct context
)
669 struct w_files
*wpnt
=wine_files
;
670 dprintf_win32(stddeb
,"Going to start Win32 program\n");
672 USER_InitApp(wpnt
->hModule
);
673 fs
=(int)GlobalAlloc(GHND
,0x10000);
675 __asm__
__volatile__("movw %w0,%%fs"::"r" (fs
));
676 ((void(*)())(wpnt
->load_addr
+wpnt
->pe
->pe_header
->opt_coff
.AddressOfEntryPoint
))();
679 int PE_UnloadImage(struct w_files
*wpnt
)
681 printf("PEunloadImage() called!\n");
682 /* free resources, image, unmap */
686 void PE_InitDLL(HMODULE hModule
)
688 struct w_files
*wpnt
;
689 hModule
= GetExePtr(hModule
);
690 for(wpnt
= wine_files
;wpnt
&& wpnt
->hModule
!= hModule
;
691 wpnt
= wpnt
->next
) /*nothing*/;
692 if(!wpnt
|| wpnt
->initialised
)
694 /* FIXME: What are the correct values for parameters 2 and 3? */
696 /* Is this a library? */
697 if (wpnt
->pe
->pe_header
->coff
.Characteristics
& IMAGE_FILE_DLL
) {
698 /* Should call DLLEntryPoint here */
699 printf("InitPEDLL() called!\n");
700 wpnt
->initialised
= 1;
701 ((void(*)())(wpnt
->load_addr
+wpnt
->pe
->pe_header
->opt_coff
.AddressOfEntryPoint
))(wpnt
->hModule
, 0, 0);
706 /* FIXME: This stuff is all on a "well it works" basis. An implementation
707 based on some kind of documentation would be greatly appreciated :-) */
714 struct TEB
*TEBDSAlias
;
719 void PE_InitTEB(int hTEB
)
724 pTask
= (TDB
*)(GlobalLock(GetCurrentTask() & 0xffff));
725 pTEB
= (TEB
*)(PTR_SEG_OFF_TO_LIN(hTEB
, 0));
726 pTEB
->stack
= (void *)(GlobalLock(pTask
->hStack32
));
727 pTEB
->Except
= (void *)(-1);
728 pTEB
->TEBDSAlias
= pTEB
;
729 pTEB
->taskid
= getpid();
732 void PE_InitializeDLLs(HMODULE hModule
)
736 pModule
= (NE_MODULE
*)GlobalLock( GetExePtr(hModule
) );
737 if (pModule
->dlls_to_init
)
739 HANDLE to_init
= pModule
->dlls_to_init
;
740 pModule
->dlls_to_init
= 0;
741 for (pDLL
= (HMODULE
*)GlobalLock( to_init
); *pDLL
; pDLL
++)
743 PE_InitializeDLLs( *pDLL
);
746 GlobalFree( to_init
);
748 PE_InitDLL( hModule
);