Release 960928
[wine/multimedia.git] / loader / pe_image.c
blob8ab35e9f59592792afdb0838c7d81ad33a4385d5
1 #ifndef WINELIB
2 /*
3 * Copyright 1994 Eric Youndale & Erik Bos
4 * Copyright 1995 Martin von Löwis
5 * Copyright 1996 Marcus Meissner
7 * based on Eric Youndale's pe-test and:
9 * ftp.microsoft.com:/pub/developer/MSDN/CD8/PEFILE.ZIP
10 * make that:
11 * ftp.microsoft.com:/developr/MSDN/OctCD/PEFILE.ZIP
14 #include <ctype.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/mman.h>
21 #include "windows.h"
22 #include "winbase.h"
23 #include "callback.h"
24 #include "neexe.h"
25 #include "peexe.h"
26 #include "pe_image.h"
27 #include "module.h"
28 #include "global.h"
29 #include "task.h"
30 #include "ldt.h"
31 #include "registers.h"
32 #include "stddebug.h"
33 #include "debug.h"
34 #include "debugger.h"
35 #include "xmalloc.h"
37 void my_wcstombs(char * result, u_short * source, int len)
39 while(len--) {
40 /* this used to be isascii, but see isascii implementation in Linux'
41 ctype.h */
42 if(*source<255) *result++ = *source++;
43 else {
44 printf("Unable to handle unicode right now\n");
45 exit(0);
50 #if 0
51 char * xmmap(char * vaddr, unsigned int v_size, unsigned int r_size,
52 int prot, int flags, int fd, unsigned int file_offset)
54 char * result;
55 /* .bss has no associated storage in the PE file */
56 if(r_size)
57 v_size=r_size;
58 else
59 #if defined(__svr4__) || defined(_SCO_DS)
60 fprintf(stderr,"xmmap: %s line %d doesn't support MAP_ANON\n",__FILE__, __LINE__);
61 #else
62 flags |= MAP_ANON;
63 #endif
64 result = mmap(vaddr, v_size, prot, flags, fd, file_offset);
65 if((unsigned int) result != 0xffffffff) return result;
67 /* Sigh. Alignment must be wrong for mmap. Do this the hard way. */
68 if(!(flags & MAP_FIXED)) {
69 vaddr = (char *)0x40000000;
70 flags |= MAP_FIXED;
73 mmap(vaddr, v_size, prot, MAP_ANONYMOUS | flags, 0, 0);
74 lseek(fd, file_offset, SEEK_SET);
75 read(fd, vaddr, v_size);
76 return vaddr;
78 #endif
80 void dump_exports(struct PE_Export_Directory * pe_exports, unsigned int load_addr)
82 char *Module;
83 int i;
84 u_short *ordinal;
85 u_long *function,*functions;
86 u_char **name,*ename;
87 char buffer[1000];
88 DBG_ADDR daddr;
90 daddr.seg = 0;
91 Module = ((char*)load_addr)+pe_exports->Name;
92 dprintf_win32(stddeb,"\n*******EXPORT DATA*******\nModule name is %s, %ld functions, %ld names\n",
93 Module,
94 pe_exports->Number_Of_Functions,
95 pe_exports->Number_Of_Names);
97 ordinal=(u_short*)(((char*)load_addr)+(int)pe_exports->Address_Of_Name_Ordinals);
98 functions=function=(u_long*)(((char*)load_addr)+(int)pe_exports->AddressOfFunctions);
99 name=(u_char**)(((char*)load_addr)+(int)pe_exports->AddressOfNames);
101 dprintf_win32(stddeb,"%-32s Ordinal Virt Addr\n", "Function Name");
102 for (i=0;i<pe_exports->Number_Of_Functions;i++) {
103 if (i<pe_exports->Number_Of_Names) {
104 ename=(char*)(((char*)load_addr)+(int)*name++);
105 dprintf_win32(stddeb,"%-32s %4d %8.8lx (%8.8lx)\n",ename,*ordinal,functions[*ordinal],*function);
106 sprintf(buffer,"%s.%s",Module,ename);
107 daddr.off=load_addr+functions[*ordinal];
108 ordinal++;
109 function++;
110 } else {
111 /* ordinals/names no longer valid, but we still got functions */
112 dprintf_win32(stddeb,"%-32s %4s %8s %8.8lx\n","","","",*function);
113 sprintf(buffer,"%s.%d",Module,i);
114 daddr.off=load_addr+*functions;
115 function++;
117 DEBUG_AddSymbol(buffer,&daddr);
121 /* Look up the specified function or ordinal in the exportlist:
122 * If it is a string:
123 * - look up the name in the Name list.
124 * - look up the ordinal with that index.
125 * - use the ordinal as offset into the functionlist
126 * If it is a ordinal:
127 * - use ordinal-pe_export->Base as offset into the functionlist
129 FARPROC32 PE_FindExportedFunction(struct pe_data *pe, LPCSTR funcName)
131 struct PE_Export_Directory * exports = pe->pe_export;
132 unsigned load_addr = pe->load_addr;
133 u_short * ordinal;
134 u_long * function;
135 u_char ** name, *ename;
136 int i;
138 if (HIWORD(funcName))
139 dprintf_win32(stddeb,"PE_FindExportedFunction(%s)\n",funcName);
140 else
141 dprintf_win32(stddeb,"PE_FindExportedFunction(%d)\n",(int)funcName);
142 if (!exports)
143 return NULL;
144 ordinal=(u_short*)(((char*)load_addr)+(int)exports->Address_Of_Name_Ordinals);
145 function=(u_long*)(((char*)load_addr)+(int)exports->AddressOfFunctions);
146 name=(u_char **)(((char*)load_addr)+(int)exports->AddressOfNames);
147 if (HIWORD(funcName)) {
148 for(i=0; i<exports->Number_Of_Names; i++) {
149 ename=(char*)(((char*)load_addr)+(int)*name);
150 if(!strcmp(ename,funcName))
151 return (FARPROC32)(load_addr+function[*ordinal]);
152 ordinal++;
153 name++;
155 } else {
156 if (funcName-exports->Base > exports->Number_Of_Functions) {
157 dprintf_win32(stddeb," ordinal %d out of range!\n",funcName);
158 return NULL;
160 return (FARPROC32)(load_addr+function[(int)funcName-exports->Base]);
162 return NULL;
165 void
166 fixup_imports (struct pe_data *pe, HMODULE16 hModule)
168 struct PE_Import_Directory *pe_imp;
169 int fixup_failed = 0;
170 unsigned int load_addr = pe->load_addr;
171 int i;
172 NE_MODULE *ne_mod;
173 HMODULE16 *mod_ptr;
175 /* OK, now dump the import list */
176 dprintf_win32 (stddeb, "\nDumping imports list\n");
178 /* first, count the number of imported non-internal modules */
179 pe_imp = pe->pe_import;
180 for (i = 0; pe_imp->ModuleName; pe_imp++)
181 i++;
183 /* Now, allocate memory for dlls_to_init */
184 ne_mod = GlobalLock16 (hModule);
185 ne_mod->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT, (i+1)*sizeof(HMODULE16),
186 hModule, FALSE, FALSE, FALSE);
187 mod_ptr = GlobalLock16 (ne_mod->dlls_to_init);
188 /* load the modules and put their handles into the list */
189 for (i = 0, pe_imp = pe->pe_import; pe_imp->ModuleName; pe_imp++) {
190 char *name = (char *) load_addr + pe_imp->ModuleName;
191 mod_ptr[i] = LoadModule (name, (LPVOID) - 1);
192 if (mod_ptr[i] <= (HMODULE16) 32) {
193 char *p, buffer[256];
195 /* Try with prepending the path of the current module */
196 GetModuleFileName (hModule, buffer, sizeof (buffer));
197 if (!(p = strrchr (buffer, '\\')))
198 p = buffer;
199 strcpy (p + 1, name);
200 mod_ptr[i] = LoadModule (buffer, (LPVOID) - 1);
202 if (mod_ptr[i] <= (HMODULE16) 32) {
203 fprintf (stderr, "Module %s not found\n", name);
204 exit (0);
206 i++;
208 pe_imp = pe->pe_import;
209 while (pe_imp->ModuleName) {
210 char *Module;
211 struct pe_import_name *pe_name;
212 unsigned int *import_list, *thunk_list;
214 Module = ((char *) load_addr) + pe_imp->ModuleName;
215 dprintf_win32 (stddeb, "%s\n", Module);
217 if (pe_imp->Import_List != 0) { /* original microsoft style */
218 dprintf_win32 (stddeb, "Microsoft style imports used\n");
219 import_list = (unsigned int *)(((unsigned int)load_addr)+pe_imp->Import_List);
220 thunk_list = (unsigned int *)(((unsigned int)load_addr)+pe_imp->Thunk_List);
222 while (*import_list) {
223 pe_name = (struct pe_import_name *) ((int) load_addr + ((unsigned) *import_list & ~0x80000000));
224 if ((unsigned) *import_list & 0x80000000) {
225 int ordinal = *import_list & (0x80000000 - 1);
226 dprintf_win32 (stddeb, "--- Ordinal %s,%d\n", Module, ordinal);
227 *thunk_list = GetProcAddress32(MODULE_FindModule (Module),
228 (LPCSTR) ordinal);
229 if (!*thunk_list) {
230 fprintf(stderr,"No implementation for %s.%d, setting to NULL\n",
231 Module, ordinal);
232 /* fixup_failed=1; */
234 } else { /* import by name */
235 dprintf_win32 (stddeb, "--- %s %s.%d\n", pe_name->Name, Module, pe_name->Hint);
236 *thunk_list = GetProcAddress32(MODULE_FindModule (Module),
237 pe_name->Name);
238 if (!*thunk_list) {
239 fprintf(stderr, "No implementation for %s.%d(%s), setting to NULL\n",
240 Module, pe_name->Hint, pe_name->Name);
241 /* fixup_failed=1; */
244 import_list++;
245 thunk_list++;
247 } else { /* Borland style */
248 dprintf_win32 (stddeb, "Borland style imports used\n");
249 thunk_list = (unsigned int *)(((unsigned int)load_addr)+pe_imp->Thunk_List);
250 while (*thunk_list) {
251 pe_name=(struct pe_import_name *)((int)load_addr+*thunk_list);
252 if ((unsigned) pe_name & 0x80000000) {
253 /* not sure about this branch, but it seems to work */
254 int ordinal = *thunk_list & ~0x80000000;
255 dprintf_win32(stddeb,"--- Ordinal %s.%d\n",Module,ordinal);
256 *thunk_list = GetProcAddress32(MODULE_FindModule (Module),
257 (LPCSTR) ordinal);
258 if (!*thunk_list) {
259 fprintf(stderr, "No implementation for %s.%d, setting to NULL\n",
260 Module,ordinal);
261 /* fixup_failed=1; */
263 } else {
264 dprintf_win32(stddeb,"--- %s %s.%d\n",
265 pe_name->Name, Module, pe_name->Hint);
266 *thunk_list = GetProcAddress32(MODULE_FindModule(Module),
267 pe_name->Name);
268 if (!*thunk_list) {
269 fprintf(stderr, "No implementation for %s.%d, setting to NULL\n",
270 Module, pe_name->Hint);
271 /* fixup_failed=1; */
274 thunk_list++;
277 pe_imp++;
279 if (fixup_failed) exit(1);
282 static void calc_vma_size(struct pe_data *pe)
284 int i;
286 dprintf_win32(stddeb, "Dump of segment table\n");
287 dprintf_win32(stddeb, " Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
288 for(i=0; i< pe->pe_header->coff.NumberOfSections; i++)
290 dprintf_win32(stddeb, "%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
291 pe->pe_seg[i].Name,
292 pe->pe_seg[i].Virtual_Size,
293 pe->pe_seg[i].Virtual_Address,
294 pe->pe_seg[i].Size_Of_Raw_Data,
295 pe->pe_seg[i].PointerToRawData,
296 pe->pe_seg[i].PointerToRelocations,
297 pe->pe_seg[i].PointerToLinenumbers,
298 pe->pe_seg[i].NumberOfRelocations,
299 pe->pe_seg[i].NumberOfLinenumbers,
300 pe->pe_seg[i].Characteristics);
301 pe->vma_size = MAX(pe->vma_size,
302 pe->pe_seg[i].Virtual_Address +
303 pe->pe_seg[i].Size_Of_Raw_Data);
307 static void do_relocations(struct pe_data *pe)
309 int delta = pe->load_addr - pe->base_addr;
310 struct PE_Reloc_Block *r = pe->pe_reloc;
311 int hdelta = (delta >> 16) & 0xFFFF;
312 int ldelta = delta & 0xFFFF;
313 /* int reloc_size = */
314 if(delta == 0)
315 /* Nothing to do */
316 return;
317 while(r->PageRVA)
319 char *page = (char*)pe->load_addr + r->PageRVA;
320 int count = (r->BlockSize - 8)/2;
321 int i;
322 dprintf_fixup(stddeb, "%x relocations for page %lx\n",
323 count, r->PageRVA);
324 /* patching in reverse order */
325 for(i=0;i<count;i++)
327 int offset = r->Relocations[i] & 0xFFF;
328 int type = r->Relocations[i] >> 12;
329 dprintf_fixup(stddeb,"patching %x type %x\n", offset, type);
330 switch(type)
332 case IMAGE_REL_BASED_ABSOLUTE: break;
333 case IMAGE_REL_BASED_HIGH:
334 *(short*)(page+offset) += hdelta;
335 break;
336 case IMAGE_REL_BASED_LOW:
337 *(short*)(page+offset) += ldelta;
338 break;
339 case IMAGE_REL_BASED_HIGHLOW:
340 #if 1
341 *(int*)(page+offset) += delta;
342 #else
343 { int h=*(unsigned short*)(page+offset);
344 int l=r->Relocations[++i];
345 *(unsigned int*)(page + offset) = (h<<16) + l + delta;
347 #endif
348 break;
349 case IMAGE_REL_BASED_HIGHADJ:
350 fprintf(stderr, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
351 break;
352 case IMAGE_REL_BASED_MIPS_JMPADDR:
353 fprintf(stderr, "Is this a MIPS machine ???\n");
354 break;
355 default:
356 fprintf(stderr, "Unknown fixup type\n");
357 break;
360 r = (struct PE_Reloc_Block*)((char*)r + r->BlockSize);
368 /**********************************************************************
369 * PE_LoadImage
370 * Load one PE format executable into memory
372 static struct pe_data *PE_LoadImage( int fd, HMODULE16 hModule, WORD offset )
374 struct pe_data *pe;
375 int i, result;
376 unsigned int load_addr;
377 struct Directory dir;
378 char buffer[200];
379 DBG_ADDR daddr;
381 daddr.seg=0;
383 pe = xmalloc(sizeof(struct pe_data));
384 memset(pe,0,sizeof(struct pe_data));
385 pe->pe_header = xmalloc(sizeof(struct pe_header_s));
387 /* read PE header */
388 lseek( fd, offset, SEEK_SET);
389 read( fd, pe->pe_header, sizeof(struct pe_header_s));
391 /* read sections */
392 pe->pe_seg = xmalloc(sizeof(struct pe_segment_table) *
393 pe->pe_header->coff.NumberOfSections);
394 read( fd, pe->pe_seg, sizeof(struct pe_segment_table) *
395 pe->pe_header->coff.NumberOfSections);
397 load_addr = pe->pe_header->opt_coff.BaseOfImage;
398 pe->base_addr=load_addr;
399 pe->vma_size=0;
400 dprintf_win32(stddeb, "Load addr is %x\n",load_addr);
401 calc_vma_size(pe);
403 /* We use malloc here, while a huge part of that address space does
404 not be supported by actual memory. It has to be contiguous, though.
405 I don't know if mmap("/dev/null"); would do any better.
406 What I'd really like to do is a Win32 style VirtualAlloc/MapViewOfFile
407 sequence */
408 load_addr = pe->load_addr = malloc(pe->vma_size);
409 dprintf_win32(stddeb, "Load addr is really %x, range %x\n",
410 pe->load_addr, pe->vma_size);
413 for(i=0; i < pe->pe_header->coff.NumberOfSections; i++)
415 /* load only non-BSS segments */
416 if(pe->pe_seg[i].Characteristics &
417 ~ IMAGE_SCN_TYPE_CNT_UNINITIALIZED_DATA)
418 if(lseek(fd,pe->pe_seg[i].PointerToRawData,SEEK_SET) == -1
419 || read(fd,load_addr + pe->pe_seg[i].Virtual_Address,
420 pe->pe_seg[i].Size_Of_Raw_Data)
421 != pe->pe_seg[i].Size_Of_Raw_Data)
423 fprintf(stderr,"Failed to load section %x\n", i);
424 exit(0);
426 result = load_addr + pe->pe_seg[i].Virtual_Address;
427 #if 0
428 if(!load_addr) {
430 result = (int)xmmap((char *)0, pe->pe_seg[i].Virtual_Size,
431 pe->pe_seg[i].Size_Of_Raw_Data, 7,
432 MAP_PRIVATE, fd, pe->pe_seg[i].PointerToRawData);
433 load_addr = (unsigned int) result - pe->pe_seg[i].Virtual_Address;
434 } else {
435 result = (int)xmmap((char *) load_addr + pe->pe_seg[i].Virtual_Address,
436 pe->pe_seg[i].Virtual_Size,
437 pe->pe_seg[i].Size_Of_Raw_Data, 7, MAP_PRIVATE | MAP_FIXED,
438 fd, pe->pe_seg[i].PointerToRawData);
440 if(result==-1){
441 fprintf(stderr,"Could not load section %x to desired address %lx\n",
442 i, load_addr+pe->pe_seg[i].Virtual_Address);
443 fprintf(stderr,"Need to implement relocations now\n");
444 exit(0);
446 #endif
448 if(strcmp(pe->pe_seg[i].Name, ".bss") == 0)
449 memset((void *)result, 0,
450 pe->pe_seg[i].Virtual_Size ?
451 pe->pe_seg[i].Virtual_Size :
452 pe->pe_seg[i].Size_Of_Raw_Data);
454 if(strcmp(pe->pe_seg[i].Name, ".idata") == 0)
455 pe->pe_import = (struct PE_Import_Directory *) result;
457 if(strcmp(pe->pe_seg[i].Name, ".edata") == 0)
458 pe->pe_export = (struct PE_Export_Directory *) result;
460 if(strcmp(pe->pe_seg[i].Name, ".rsrc") == 0)
461 pe->pe_resource = (struct PE_Resource_Directory *) result;
463 if(strcmp(pe->pe_seg[i].Name, ".reloc") == 0)
464 pe->pe_reloc = (struct PE_Reloc_Block *) result;
468 /* There is word that the actual loader does not care about the
469 section names, and only goes for the DataDirectory */
470 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
471 if(dir.Size)
473 if(pe->pe_export &&
474 pe->pe_export!=load_addr+dir.Virtual_address)
475 fprintf(stderr,"wrong export directory??\n");
476 /* always trust the directory */
477 pe->pe_export = load_addr+dir.Virtual_address;
480 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
481 if(dir.Size)
483 if(pe->pe_import &&
484 pe->pe_import!=load_addr+dir.Virtual_address)
485 fprintf(stderr,"wrong import directory??\n");
486 pe->pe_import = load_addr+dir.Virtual_address;
489 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
490 if(dir.Size)
492 if(pe->pe_resource &&
493 pe->pe_resource!=load_addr+dir.Virtual_address)
494 fprintf(stderr,"wrong resource directory??\n");
495 pe->pe_resource = load_addr+dir.Virtual_address;
498 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_BASE_RELOCATION_TABLE];
499 if(dir.Size)
501 if(pe->pe_reloc &&
502 pe->pe_reloc!=load_addr+dir.Virtual_address)
503 fprintf(stderr,"wrong relocation list??\n");
504 pe->pe_reloc = load_addr+dir.Virtual_address;
507 if(pe->pe_header->opt_coff.DataDirectory
508 [IMAGE_FILE_EXCEPTION_DIRECTORY].Size)
509 dprintf_win32(stdnimp,"Exception directory ignored\n");
511 if(pe->pe_header->opt_coff.DataDirectory
512 [IMAGE_FILE_SECURITY_DIRECTORY].Size)
513 dprintf_win32(stdnimp,"Security directory ignored\n");
515 if(pe->pe_header->opt_coff.DataDirectory
516 [IMAGE_FILE_DEBUG_DIRECTORY].Size)
517 dprintf_win32(stdnimp,"Debug directory ignored\n");
519 if(pe->pe_header->opt_coff.DataDirectory
520 [IMAGE_FILE_DESCRIPTION_STRING].Size)
521 dprintf_win32(stdnimp,"Description string ignored\n");
523 if(pe->pe_header->opt_coff.DataDirectory
524 [IMAGE_FILE_MACHINE_VALUE].Size)
525 dprintf_win32(stdnimp,"Machine Value ignored\n");
527 if(pe->pe_header->opt_coff.DataDirectory
528 [IMAGE_FILE_THREAD_LOCAL_STORAGE].Size)
529 dprintf_win32(stdnimp,"Thread local storage ignored\n");
531 if(pe->pe_header->opt_coff.DataDirectory
532 [IMAGE_FILE_CALLBACK_DIRECTORY].Size)
533 dprintf_win32(stdnimp,"Callback directory ignored\n");
536 if(pe->pe_reloc) do_relocations(pe);
537 if(pe->pe_import) fixup_imports(pe, hModule);
538 if(pe->pe_export) dump_exports(pe->pe_export,load_addr);
540 if (pe->pe_export) {
541 /* add start of sections as debugsymbols */
542 for(i=0;i<pe->pe_header->coff.NumberOfSections;i++) {
543 sprintf(buffer,"%s.%s",
544 ((char*)load_addr)+pe->pe_export->Name,
545 pe->pe_seg[i].Name
547 daddr.off=load_addr+pe->pe_seg[i].Virtual_Address;
548 DEBUG_AddSymbol(buffer,&daddr);
550 /* add entry point */
551 sprintf(buffer,"%s.EntryPoint",((char*)load_addr)+pe->pe_export->Name);
552 daddr.off=load_addr+pe->pe_header->opt_coff.AddressOfEntryPoint;
553 DEBUG_AddSymbol(buffer,&daddr);
554 /* add start of DLL */
555 daddr.off=load_addr;
556 DEBUG_AddSymbol(((char*)load_addr)+pe->pe_export->Name,&daddr);
558 return pe;
561 HINSTANCE MODULE_CreateInstance(HMODULE16 hModule,LOADPARAMS *params);
562 void InitTask( SIGCONTEXT *context );
564 HINSTANCE PE_LoadModule( int fd, OFSTRUCT *ofs, LOADPARAMS* params )
566 HMODULE16 hModule;
567 HINSTANCE16 hInstance;
568 NE_MODULE *pModule;
569 SEGTABLEENTRY *pSegment;
570 FARPROC16 startup;
571 struct mz_header_s mz_header;
573 if ((hModule = MODULE_CreateDummyModule( ofs )) < 32) return hModule;
574 pModule = (NE_MODULE *)GlobalLock16( hModule );
575 pModule->flags = NE_FFLAGS_WIN32;
577 lseek( fd, 0, SEEK_SET );
578 read( fd, &mz_header, sizeof(mz_header) );
580 /* Set the startup address */
582 startup = MODULE_GetWndProcEntry16("Win32CallToStart");
583 pSegment = NE_SEG_TABLE(pModule) + pModule->cs - 1;
584 pSegment->selector = SELECTOROF(startup); /* FIXME */
585 pModule->ip = OFFSETOF(startup);
587 pModule->pe_module = PE_LoadImage( fd, hModule, mz_header.ne_offset );
589 hInstance = MODULE_CreateInstance( hModule, params );
591 if (!(pModule->pe_module->pe_header->coff.Characteristics & IMAGE_FILE_DLL))
593 TASK_CreateTask( hModule, hInstance, 0,
594 params->hEnvironment,
595 (LPSTR)PTR_SEG_TO_LIN( params->cmdLine ),
596 *((WORD*)PTR_SEG_TO_LIN(params->showCmd) + 1) );
598 return hInstance;
601 int USER_InitApp(HINSTANCE hInstance);
602 void PE_InitTEB(int hTEB);
604 void PE_InitializeDLLs(HMODULE16 hModule);
605 void PE_Win32CallToStart( SIGCONTEXT *context )
607 int fs;
608 HMODULE16 hModule;
609 NE_MODULE *pModule;
611 dprintf_win32(stddeb,"Going to start Win32 program\n");
612 InitTask( context );
613 hModule = GetExePtr( GetCurrentTask() );
614 pModule = MODULE_GetPtr( hModule );
615 USER_InitApp( hModule );
616 fs=(int)GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, 0x10000 );
617 PE_InitTEB(fs);
618 __asm__ __volatile__("movw %w0,%%fs"::"r" (fs));
619 PE_InitializeDLLs( hModule );
620 CallTaskStart32( (FARPROC32)(pModule->pe_module->load_addr +
621 pModule->pe_module->pe_header->opt_coff.AddressOfEntryPoint) );
624 int PE_UnloadImage( HMODULE16 hModule )
626 printf("PEunloadImage() called!\n");
627 /* free resources, image, unmap */
628 return 1;
631 static void PE_InitDLL(HMODULE16 hModule)
633 NE_MODULE *pModule;
634 PE_MODULE *pe;
636 hModule = GetExePtr(hModule);
637 if (!(pModule = MODULE_GetPtr(hModule))) return;
638 if (!(pModule->flags & NE_FFLAGS_WIN32) || !(pe = pModule->pe_module))
639 return;
641 /* FIXME: What is the correct value for parameter 3?
642 * (the MSDN library JAN96 says 'reserved for future use')
645 /* Is this a library? */
646 if (pe->pe_header->coff.Characteristics & IMAGE_FILE_DLL)
648 printf("InitPEDLL() called!\n");
649 CallDLLEntryProc32( (FARPROC32)(pe->load_addr +
650 pe->pe_header->opt_coff.AddressOfEntryPoint),
651 hModule, DLL_PROCESS_ATTACH, (void *)-1 );
656 /* FIXME: This stuff is all on a "well it works" basis. An implementation
657 based on some kind of documentation would be greatly appreciated :-) */
659 typedef struct
661 void *Except;
662 void *stack;
663 int dummy1[4];
664 struct TEB *TEBDSAlias;
665 int dummy2[2];
666 int taskid;
667 } TEB;
669 void PE_InitTEB(int hTEB)
671 TDB *pTask;
672 TEB *pTEB;
674 pTask = (TDB *)(GlobalLock16(GetCurrentTask() & 0xffff));
675 pTEB = (TEB *)(GlobalLock16(hTEB));
676 pTEB->stack = pTask->esp;
677 pTEB->Except = (void *)(-1);
678 pTEB->TEBDSAlias = pTEB;
679 pTEB->taskid = getpid();
682 void PE_InitializeDLLs(HMODULE16 hModule)
684 NE_MODULE *pModule;
685 HMODULE16 *pDLL;
686 pModule = MODULE_GetPtr( GetExePtr(hModule) );
687 if (pModule->dlls_to_init)
689 HANDLE to_init = pModule->dlls_to_init;
690 pModule->dlls_to_init = 0;
691 for (pDLL = (HMODULE16 *)GlobalLock16( to_init ); *pDLL; pDLL++)
693 PE_InitializeDLLs( *pDLL );
694 PE_InitDLL( *pDLL );
696 GlobalFree16( to_init );
698 PE_InitDLL( hModule );
700 #endif /* WINELIB */