Release 960521
[wine/multimedia.git] / loader / pe_image.c
blobfffff8a4748befc16f21d78ce62764b3d82ea96b
1 #ifndef WINELIB
2 /*
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
9 * make that:
10 * ftp.microsoft.com:/developr/MSDN/OctCD/PEFILE.ZIP
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <sys/mman.h>
20 #include "windows.h"
21 #include "winbase.h"
22 #include "callback.h"
23 #include "neexe.h"
24 #include "peexe.h"
25 #include "pe_image.h"
26 #include "module.h"
27 #include "global.h"
28 #include "task.h"
29 #include "ldt.h"
30 #include "registers.h"
31 #include "stddebug.h"
32 #include "debug.h"
33 #include "xmalloc.h"
35 void my_wcstombs(char * result, u_short * source, int len)
37 while(len--) {
38 /* this used to be isascii, but see isascii implementation in Linux'
39 ctype.h */
40 if(*source<255) *result++ = *source++;
41 else {
42 printf("Unable to handle unicode right now\n");
43 exit(0);
48 #if 0
49 char * xmmap(char * vaddr, unsigned int v_size, unsigned int r_size,
50 int prot, int flags, int fd, unsigned int file_offset)
52 char * result;
53 /* .bss has no associated storage in the PE file */
54 if(r_size)
55 v_size=r_size;
56 else
57 #if defined(__svr4__) || defined(_SCO_DS)
58 fprintf(stderr,"xmmap: %s line %d doesn't support MAP_ANON\n",__FILE__, __LINE__);
59 #else
60 flags |= MAP_ANON;
61 #endif
62 result = mmap(vaddr, v_size, prot, flags, fd, file_offset);
63 if((unsigned int) result != 0xffffffff) return result;
65 /* Sigh. Alignment must be wrong for mmap. Do this the hard way. */
66 if(!(flags & MAP_FIXED)) {
67 vaddr = (char *)0x40000000;
68 flags |= MAP_FIXED;
71 mmap(vaddr, v_size, prot, MAP_ANONYMOUS | flags, 0, 0);
72 lseek(fd, file_offset, SEEK_SET);
73 read(fd, vaddr, v_size);
74 return vaddr;
76 #endif
78 void dump_exports(struct PE_Export_Directory * pe_exports, unsigned int load_addr)
80 char * Module;
81 int i;
82 u_short * ordinal;
83 u_long * function;
84 u_char ** name, *ename;
86 Module = ((char *) load_addr) + pe_exports->Name;
87 dprintf_win32(stddeb,"\n*******EXPORT DATA*******\nModule name is %s, %ld functions, %ld names\n",
88 Module,
89 pe_exports->Number_Of_Functions,
90 pe_exports->Number_Of_Names);
92 ordinal = (u_short *) (((char *) load_addr) + (int) pe_exports->Address_Of_Name_Ordinals);
93 function = (u_long *) (((char *) load_addr) + (int) pe_exports->AddressOfFunctions);
94 name = (u_char **) (((char *) load_addr) + (int) pe_exports->AddressOfNames);
96 dprintf_win32(stddeb,"%-32s Ordinal Virt Addr\n", "Function Name");
97 for(i=0; i< pe_exports->Number_Of_Functions; i++)
99 ename = (char *) (((char *) load_addr) + (int) *name++);
100 dprintf_win32(stddeb,"%-32s %4d %8.8lx\n", ename, *ordinal++, *function++);
104 static DWORD PE_FindExportedFunction(struct pe_data *pe, char* funcName)
106 struct PE_Export_Directory * exports = pe->pe_export;
107 unsigned load_addr = pe->load_addr;
108 u_short * ordinal;
109 u_long * function;
110 u_char ** name, *ename;
111 int i;
112 if(!exports)return 0;
113 ordinal = (u_short *) (((char *) load_addr) + (int) exports->Address_Of_Name_Ordinals);
114 function = (u_long *) (((char *) load_addr) + (int) exports->AddressOfFunctions);
115 name = (u_char **) (((char *) load_addr) + (int) exports->AddressOfNames);
116 for(i=0; i<exports->Number_Of_Functions; i++)
118 if(HIWORD(funcName))
120 ename = (char *) (((char *) load_addr) + (int) *name);
121 if(strcmp(ename,funcName)==0)
122 return load_addr+*function;
123 }else{
124 if(funcName == (int)*ordinal + exports->Base)
125 return load_addr+*function;
127 function++;
128 ordinal++;
129 name++;
131 return 0;
134 DWORD PE_GetProcAddress(HMODULE hModule, char* function)
136 NE_MODULE *pModule;
138 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
139 if (!(pModule->flags & NE_FFLAGS_WIN32) || !pModule->pe_module) return 0;
140 if (pModule->flags & NE_FFLAGS_BUILTIN)
141 return BUILTIN_GetProcAddress32( pModule, function );
142 return PE_FindExportedFunction( pModule->pe_module, function );
145 void fixup_imports(struct pe_data *pe, HMODULE hModule)
147 struct PE_Import_Directory * pe_imp;
148 int fixup_failed=0;
149 unsigned int load_addr = pe->load_addr;
150 int i;
151 NE_MODULE *ne_mod;
152 HMODULE *mod_ptr;
154 /* OK, now dump the import list */
155 dprintf_win32(stddeb, "\nDumping imports list\n");
157 /* first, count the number of imported non-internal modules */
158 pe_imp = pe->pe_import;
159 for(i=0;pe_imp->ModuleName;pe_imp++)
160 i++;
162 /* Now, allocate memory for dlls_to_init */
163 ne_mod = GlobalLock16(hModule);
164 ne_mod->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT,(i+1) * sizeof(HMODULE),
165 hModule, FALSE, FALSE, FALSE );
166 mod_ptr = GlobalLock16(ne_mod->dlls_to_init);
167 /* load the modules and put their handles into the list */
168 for(i=0,pe_imp = pe->pe_import;pe_imp->ModuleName;pe_imp++)
170 char *name = (char*)load_addr+pe_imp->ModuleName;
171 mod_ptr[i] = LoadModule(name,(LPVOID)-1);
172 if(mod_ptr[i]<=(HMODULE)32)
174 fprintf(stderr,"Module %s not found\n",name);
175 exit(0);
177 i++;
179 pe_imp = pe->pe_import;
180 while (pe_imp->ModuleName)
182 char * Module;
183 struct pe_import_name * pe_name;
184 unsigned int * import_list, *thunk_list;
186 Module = ((char *) load_addr) + pe_imp->ModuleName;
187 dprintf_win32(stddeb, "%s\n", Module);
189 if(pe_imp->Import_List != 0) { /* original microsoft style */
190 dprintf_win32(stddeb, "Microsoft style imports used\n");
191 import_list = (unsigned int *)
192 (((unsigned int) load_addr) + pe_imp->Import_List);
193 thunk_list = (unsigned int *)
194 (((unsigned int) load_addr) + pe_imp->Thunk_List);
197 while(*import_list)
199 pe_name = (struct pe_import_name *) ((int) load_addr + ((unsigned)*import_list & ~0x80000000));
200 if((unsigned)*import_list & 0x80000000)
202 int ordinal=*import_list & (0x80000000-1);
203 dprintf_win32(stddeb,"--- Ordinal %s,%d\n", Module, ordinal);
204 *thunk_list = WIN32_GetProcAddress(MODULE_FindModule(Module),
205 ordinal);
206 if(!*thunk_list)
208 fprintf(stderr,"No implementation for %s.%d\n",Module,
209 ordinal);
210 fixup_failed=1;
212 }else{ /* import by name */
213 dprintf_win32(stddeb, "--- %s %s.%d\n", pe_name->Name, Module, pe_name->Hint);
214 #ifndef WINELIB /* FIXME: JBP: Should this happen in libwine.a? */
215 *thunk_list = WIN32_GetProcAddress(MODULE_FindModule(Module),
216 pe_name->Name);
217 if(!*thunk_list)
219 fprintf(stderr,"No implementation for %s.%d(%s)\n",Module,
220 pe_name->Hint, pe_name->Name);
221 fixup_failed=1;
224 #else
225 fprintf(stderr,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
226 #endif
229 import_list++;
230 thunk_list++;
232 } else { /* Borland style */
233 dprintf_win32(stddeb, "Borland style imports used\n");
234 thunk_list = (unsigned int *)
235 (((unsigned int) load_addr) + pe_imp->Thunk_List);
236 while(*thunk_list) {
237 pe_name = (struct pe_import_name *) ((int) load_addr + *thunk_list);
238 if((unsigned)pe_name & 0x80000000) {
239 fprintf(stderr,"Import by ordinal not supported\n");
240 exit(0);
242 dprintf_win32(stddeb, "--- %s %s.%d\n", pe_name->Name, Module, pe_name->Hint);
243 #ifndef WINELIB /* FIXME: JBP: Should this happen in libwine.a? */
244 /* FIXME: Both calls should be unified into GetProcAddress */
245 *thunk_list = WIN32_GetProcAddress(MODULE_FindModule(Module),
246 pe_name->Name);
247 #else
248 fprintf(stderr,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
249 #endif
250 if(!*thunk_list) {
251 fprintf(stderr,"No implementation for %s.%d\n",Module, pe_name->Hint);
252 fixup_failed=1;
254 thunk_list++;
257 pe_imp++;
259 if(fixup_failed)exit(1);
262 static void calc_vma_size(struct pe_data *pe)
264 int i;
266 dprintf_win32(stddeb, "Dump of segment table\n");
267 dprintf_win32(stddeb, " Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
268 for(i=0; i< pe->pe_header->coff.NumberOfSections; i++)
270 dprintf_win32(stddeb, "%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
271 pe->pe_seg[i].Name,
272 pe->pe_seg[i].Virtual_Size,
273 pe->pe_seg[i].Virtual_Address,
274 pe->pe_seg[i].Size_Of_Raw_Data,
275 pe->pe_seg[i].PointerToRawData,
276 pe->pe_seg[i].PointerToRelocations,
277 pe->pe_seg[i].PointerToLinenumbers,
278 pe->pe_seg[i].NumberOfRelocations,
279 pe->pe_seg[i].NumberOfLinenumbers,
280 pe->pe_seg[i].Characteristics);
281 pe->vma_size = MAX(pe->vma_size,
282 pe->pe_seg[i].Virtual_Address +
283 pe->pe_seg[i].Size_Of_Raw_Data);
287 static void do_relocations(struct pe_data *pe)
289 int delta = pe->load_addr - pe->base_addr;
290 struct PE_Reloc_Block *r = pe->pe_reloc;
291 int hdelta = (delta >> 16) & 0xFFFF;
292 int ldelta = delta & 0xFFFF;
293 /* int reloc_size = */
294 if(delta == 0)
295 /* Nothing to do */
296 return;
297 while(r->PageRVA)
299 char *page = (char*)pe->load_addr + r->PageRVA;
300 int count = (r->BlockSize - 8)/2;
301 int i;
302 dprintf_fixup(stddeb, "%x relocations for page %lx\n",
303 count, r->PageRVA);
304 /* patching in reverse order */
305 for(i=0;i<count;i++)
307 int offset = r->Relocations[i] & 0xFFF;
308 int type = r->Relocations[i] >> 12;
309 dprintf_fixup(stddeb,"patching %x type %x\n", offset, type);
310 switch(type)
312 case IMAGE_REL_BASED_ABSOLUTE: break;
313 case IMAGE_REL_BASED_HIGH:
314 *(short*)(page+offset) += hdelta;
315 break;
316 case IMAGE_REL_BASED_LOW:
317 *(short*)(page+offset) += ldelta;
318 break;
319 case IMAGE_REL_BASED_HIGHLOW:
320 #if 1
321 *(int*)(page+offset) += delta;
322 #else
323 { int h=*(unsigned short*)(page+offset);
324 int l=r->Relocations[++i];
325 *(unsigned int*)(page + offset) = (h<<16) + l + delta;
327 #endif
328 break;
329 case IMAGE_REL_BASED_HIGHADJ:
330 fprintf(stderr, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
331 break;
332 case IMAGE_REL_BASED_MIPS_JMPADDR:
333 fprintf(stderr, "Is this a MIPS machine ???\n");
334 break;
335 default:
336 fprintf(stderr, "Unknown fixup type\n");
337 break;
340 r = (struct PE_Reloc_Block*)((char*)r + r->BlockSize);
348 /**********************************************************************
349 * PE_LoadImage
350 * Load one PE format executable into memory
352 static struct pe_data *PE_LoadImage( int fd, HMODULE hModule, WORD offset )
354 struct pe_data *pe;
355 int i, result;
356 unsigned int load_addr;
357 struct Directory dir;
359 pe = xmalloc(sizeof(struct pe_data));
360 memset(pe,0,sizeof(struct pe_data));
361 pe->pe_header = xmalloc(sizeof(struct pe_header_s));
363 /* read PE header */
364 lseek( fd, offset, SEEK_SET);
365 read( fd, pe->pe_header, sizeof(struct pe_header_s));
367 /* read sections */
368 pe->pe_seg = xmalloc(sizeof(struct pe_segment_table) *
369 pe->pe_header->coff.NumberOfSections);
370 read( fd, pe->pe_seg, sizeof(struct pe_segment_table) *
371 pe->pe_header->coff.NumberOfSections);
373 load_addr = pe->pe_header->opt_coff.BaseOfImage;
374 pe->base_addr=load_addr;
375 pe->vma_size=0;
376 dprintf_win32(stddeb, "Load addr is %x\n",load_addr);
377 calc_vma_size(pe);
379 /* We use malloc here, while a huge part of that address space does
380 not be supported by actual memory. It has to be contiguous, though.
381 I don't know if mmap("/dev/null"); would do any better.
382 What I'd really like to do is a Win32 style VirtualAlloc/MapViewOfFile
383 sequence */
384 load_addr = pe->load_addr = malloc(pe->vma_size);
385 dprintf_win32(stddeb, "Load addr is really %x, range %x\n",
386 pe->load_addr, pe->vma_size);
389 for(i=0; i < pe->pe_header->coff.NumberOfSections; i++)
391 /* load only non-BSS segments */
392 if(pe->pe_seg[i].Characteristics &
393 ~ IMAGE_SCN_TYPE_CNT_UNINITIALIZED_DATA)
394 if(lseek(fd,pe->pe_seg[i].PointerToRawData,SEEK_SET) == -1
395 || read(fd,load_addr + pe->pe_seg[i].Virtual_Address,
396 pe->pe_seg[i].Size_Of_Raw_Data)
397 != pe->pe_seg[i].Size_Of_Raw_Data)
399 fprintf(stderr,"Failed to load section %x\n", i);
400 exit(0);
402 result = load_addr + pe->pe_seg[i].Virtual_Address;
403 #if 0
404 if(!load_addr) {
406 result = (int)xmmap((char *)0, pe->pe_seg[i].Virtual_Size,
407 pe->pe_seg[i].Size_Of_Raw_Data, 7,
408 MAP_PRIVATE, fd, pe->pe_seg[i].PointerToRawData);
409 load_addr = (unsigned int) result - pe->pe_seg[i].Virtual_Address;
410 } else {
411 result = (int)xmmap((char *) load_addr + pe->pe_seg[i].Virtual_Address,
412 pe->pe_seg[i].Virtual_Size,
413 pe->pe_seg[i].Size_Of_Raw_Data, 7, MAP_PRIVATE | MAP_FIXED,
414 fd, pe->pe_seg[i].PointerToRawData);
416 if(result==-1){
417 fprintf(stderr,"Could not load section %x to desired address %lx\n",
418 i, load_addr+pe->pe_seg[i].Virtual_Address);
419 fprintf(stderr,"Need to implement relocations now\n");
420 exit(0);
422 #endif
424 if(strcmp(pe->pe_seg[i].Name, ".bss") == 0)
425 memset((void *)result, 0,
426 pe->pe_seg[i].Virtual_Size ?
427 pe->pe_seg[i].Virtual_Size :
428 pe->pe_seg[i].Size_Of_Raw_Data);
430 if(strcmp(pe->pe_seg[i].Name, ".idata") == 0)
431 pe->pe_import = (struct PE_Import_Directory *) result;
433 if(strcmp(pe->pe_seg[i].Name, ".edata") == 0)
434 pe->pe_export = (struct PE_Export_Directory *) result;
436 if(strcmp(pe->pe_seg[i].Name, ".rsrc") == 0) {
437 pe->pe_resource = (struct PE_Resource_Directory *) result;
438 #if 0
439 /* FIXME pe->resource_offset should be deleted from structure if this
440 ifdef doesn't break anything */
441 /* save offset for PE_FindResource */
442 pe->resource_offset = pe->pe_seg[i].Virtual_Address -
443 pe->pe_seg[i].PointerToRawData;
444 #endif
446 if(strcmp(pe->pe_seg[i].Name, ".reloc") == 0)
447 pe->pe_reloc = (struct PE_Reloc_Block *) result;
451 /* There is word that the actual loader does not care about the
452 section names, and only goes for the DataDirectory */
453 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
454 if(dir.Size)
456 if(pe->pe_export &&
457 pe->pe_export!=load_addr+dir.Virtual_address)
458 fprintf(stderr,"wrong export directory??\n");
459 /* always trust the directory */
460 pe->pe_export = load_addr+dir.Virtual_address;
463 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
464 if(dir.Size)
466 if(pe->pe_import &&
467 pe->pe_import!=load_addr+dir.Virtual_address)
468 fprintf(stderr,"wrong import directory??\n");
469 pe->pe_import = load_addr+dir.Virtual_address;
472 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
473 if(dir.Size)
475 if(pe->pe_resource &&
476 pe->pe_resource!=load_addr+dir.Virtual_address)
477 fprintf(stderr,"wrong resource directory??\n");
478 pe->pe_resource = load_addr+dir.Virtual_address;
481 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_BASE_RELOCATION_TABLE];
482 if(dir.Size)
484 if(pe->pe_reloc &&
485 pe->pe_reloc!=load_addr+dir.Virtual_address)
486 fprintf(stderr,"wrong relocation list??\n");
487 pe->pe_reloc = load_addr+dir.Virtual_address;
490 if(pe->pe_header->opt_coff.DataDirectory
491 [IMAGE_FILE_EXCEPTION_DIRECTORY].Size)
492 dprintf_win32(stdnimp,"Exception directory ignored\n");
494 if(pe->pe_header->opt_coff.DataDirectory
495 [IMAGE_FILE_SECURITY_DIRECTORY].Size)
496 dprintf_win32(stdnimp,"Security directory ignored\n");
498 if(pe->pe_header->opt_coff.DataDirectory
499 [IMAGE_FILE_DEBUG_DIRECTORY].Size)
500 dprintf_win32(stdnimp,"Debug directory ignored\n");
502 if(pe->pe_header->opt_coff.DataDirectory
503 [IMAGE_FILE_DESCRIPTION_STRING].Size)
504 dprintf_win32(stdnimp,"Description string ignored\n");
506 if(pe->pe_header->opt_coff.DataDirectory
507 [IMAGE_FILE_MACHINE_VALUE].Size)
508 dprintf_win32(stdnimp,"Machine Value ignored\n");
510 if(pe->pe_header->opt_coff.DataDirectory
511 [IMAGE_FILE_THREAD_LOCAL_STORAGE].Size)
512 dprintf_win32(stdnimp,"Thread local storage ignored\n");
514 if(pe->pe_header->opt_coff.DataDirectory
515 [IMAGE_FILE_CALLBACK_DIRECTORY].Size)
516 dprintf_win32(stdnimp,"Callback directory ignored\n");
519 if(pe->pe_import) fixup_imports(pe, hModule);
520 if(pe->pe_export) dump_exports(pe->pe_export,load_addr);
521 if(pe->pe_reloc) do_relocations(pe);
522 return pe;
525 HINSTANCE MODULE_CreateInstance(HMODULE hModule,LOADPARAMS *params);
526 void InitTask(struct sigcontext_struct context);
528 HINSTANCE PE_LoadModule( int fd, OFSTRUCT *ofs, LOADPARAMS* params )
530 PE_MODULE *pe;
531 int size, of_size;
532 NE_MODULE *pModule;
533 SEGTABLEENTRY *pSegment;
534 char *pStr;
535 DWORD cts;
536 HMODULE hModule;
537 HINSTANCE hInstance;
538 struct mz_header_s mz_header;
540 lseek(fd,0,SEEK_SET);
541 read( fd, &mz_header, sizeof(mz_header) );
543 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
544 + strlen(ofs->szPathName) + 1;
545 size = sizeof(NE_MODULE) +
546 /* loaded file info */
547 of_size +
548 /* segment table: DS,CS */
549 2 * sizeof(SEGTABLEENTRY) +
550 /* name table */
552 /* several empty tables */
555 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
556 if (!hModule) return (HINSTANCE)11; /* invalid exe */
558 FarSetOwner( hModule, hModule );
560 pModule = (NE_MODULE*)GlobalLock16(hModule);
562 /* Set all used entries */
563 pModule->magic=NE_SIGNATURE;
564 pModule->count=1;
565 pModule->next=0;
566 pModule->flags=NE_FFLAGS_WIN32;
567 pModule->dgroup=1;
568 pModule->ss=1;
569 pModule->cs=2;
570 /* Who wants to LocalAlloc for a PE Module? */
571 pModule->heap_size=0x1000;
572 pModule->stack_size=0xF000;
573 pModule->seg_count=1;
574 pModule->modref_count=0;
575 pModule->nrname_size=0;
576 pModule->fileinfo=sizeof(NE_MODULE);
577 pModule->os_flags=NE_OSFLAGS_WINDOWS;
578 pModule->expected_version=0x30A;
579 pModule->self = hModule;
581 /* Set loaded file information */
582 memcpy( pModule + 1, ofs, of_size );
583 ((OFSTRUCT *)(pModule+1))->cBytes = of_size - 1;
585 pSegment=(SEGTABLEENTRY*)((char*)(pModule + 1) + of_size);
586 pModule->seg_table=pModule->dgroup_entry=(int)pSegment-(int)pModule;
587 pSegment->size=0;
588 pSegment->flags=NE_SEGFLAGS_DATA;
589 pSegment->minsize=0x1000;
590 pSegment++;
592 cts=(DWORD)MODULE_GetWndProcEntry16("Win32CallToStart");
593 #ifdef WINELIB32
594 pSegment->selector=(void*)cts;
595 pModule->ip=0;
596 #else
597 pSegment->selector=cts>>16;
598 pModule->ip=cts & 0xFFFF;
599 #endif
600 pSegment++;
602 pStr=(char*)pSegment;
603 pModule->name_table=(int)pStr-(int)pModule;
604 strcpy(pStr,"\x08W32SXXXX");
605 pStr+=9;
607 /* All tables zero terminated */
608 pModule->res_table=pModule->import_table=pModule->entry_table=
609 (int)pStr-(int)pModule;
611 MODULE_RegisterModule( pModule );
613 pe = PE_LoadImage( fd, hModule, mz_header.ne_offset );
615 pModule->pe_module = pe;
616 pModule->heap_size=0x1000;
617 pModule->stack_size=0xE000;
619 /* CreateInstance allocates now 64KB */
620 hInstance=MODULE_CreateInstance(hModule,NULL /* FIX: NULL? really? */);
622 /* FIXME: Is this really the correct place to initialise the DLL? */
623 if ((pe->pe_header->coff.Characteristics & IMAGE_FILE_DLL)) {
624 /* PE_InitDLL(hModule); */
625 } else {
626 TASK_CreateTask(hModule,hInstance,0,
627 params->hEnvironment,(LPSTR)PTR_SEG_TO_LIN(params->cmdLine),
628 *((WORD*)PTR_SEG_TO_LIN(params->showCmd)+1));
629 PE_InitializeDLLs(hModule);
631 return hInstance;
634 int USER_InitApp(HINSTANCE hInstance);
635 void PE_InitTEB(int hTEB);
637 void PE_Win32CallToStart(struct sigcontext_struct context)
639 int fs;
640 HMODULE hModule;
641 NE_MODULE *pModule;
643 dprintf_win32(stddeb,"Going to start Win32 program\n");
644 InitTask(context);
645 hModule = GetExePtr( GetCurrentTask() );
646 pModule = MODULE_GetPtr( hModule );
647 USER_InitApp( hModule );
648 fs=(int)GlobalAlloc16(GHND,0x10000);
649 PE_InitTEB(fs);
650 __asm__ __volatile__("movw %w0,%%fs"::"r" (fs));
651 CallTaskStart32( (FARPROC)(pModule->pe_module->load_addr +
652 pModule->pe_module->pe_header->opt_coff.AddressOfEntryPoint) );
655 int PE_UnloadImage( HMODULE hModule )
657 printf("PEunloadImage() called!\n");
658 /* free resources, image, unmap */
659 return 1;
662 static void PE_InitDLL(HMODULE hModule)
664 NE_MODULE *pModule;
665 PE_MODULE *pe;
667 hModule = GetExePtr(hModule);
668 if (!(pModule = MODULE_GetPtr(hModule))) return;
669 if (!(pModule->flags & NE_FFLAGS_WIN32) || !(pe = pModule->pe_module))
670 return;
672 /* FIXME: What are the correct values for parameters 2 and 3? */
674 /* Is this a library? */
675 if (pe->pe_header->coff.Characteristics & IMAGE_FILE_DLL)
677 printf("InitPEDLL() called!\n");
678 CallDLLEntryProc32( (FARPROC)(pe->load_addr +
679 pe->pe_header->opt_coff.AddressOfEntryPoint),
680 hModule, 0, 0 );
685 /* FIXME: This stuff is all on a "well it works" basis. An implementation
686 based on some kind of documentation would be greatly appreciated :-) */
688 typedef struct
690 void *Except;
691 void *stack;
692 int dummy1[4];
693 struct TEB *TEBDSAlias;
694 int dummy2[2];
695 int taskid;
696 } TEB;
698 void PE_InitTEB(int hTEB)
700 TDB *pTask;
701 TEB *pTEB;
703 pTask = (TDB *)(GlobalLock16(GetCurrentTask() & 0xffff));
704 pTEB = (TEB *)(PTR_SEG_OFF_TO_LIN(hTEB, 0));
705 pTEB->stack = (void *)(GlobalLock16(pTask->hStack32));
706 pTEB->Except = (void *)(-1);
707 pTEB->TEBDSAlias = pTEB;
708 pTEB->taskid = getpid();
711 void PE_InitializeDLLs(HMODULE hModule)
713 NE_MODULE *pModule;
714 HMODULE *pDLL;
715 pModule = MODULE_GetPtr( GetExePtr(hModule) );
716 if (pModule->dlls_to_init)
718 HANDLE to_init = pModule->dlls_to_init;
719 pModule->dlls_to_init = 0;
720 for (pDLL = (HMODULE *)GlobalLock16( to_init ); *pDLL; pDLL++)
722 PE_InitializeDLLs( *pDLL );
723 PE_InitDLL( *pDLL );
725 GlobalFree16( to_init );
727 PE_InitDLL( hModule );
729 #endif /* WINELIB */