Release 960414
[wine/dcerpc.git] / loader / pe_image.c
blob79789b7e2dea24ac29ded0d1f833f80b89cf1499
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 "callback.h"
22 #include "dlls.h"
23 #include "neexe.h"
24 #include "peexe.h"
25 #include "pe_image.h"
26 #include "relay32.h"
27 #include "module.h"
28 #include "alias.h"
29 #include "global.h"
30 #include "task.h"
31 #include "ldt.h"
32 #include "registers.h"
33 #include "stddebug.h"
34 #include "debug.h"
35 #include "xmalloc.h"
37 static HANDLE32 ProcessHeap; /* FIXME: should be in process database */
39 void my_wcstombs(char * result, u_short * source, int len)
41 while(len--) {
42 /* this used to be isascii, but see isascii implementation in Linux'
43 ctype.h */
44 if(*source<255) *result++ = *source++;
45 else {
46 printf("Unable to handle unicode right now\n");
47 exit(0);
52 #if 0
53 char * xmmap(char * vaddr, unsigned int v_size, unsigned int r_size,
54 int prot, int flags, int fd, unsigned int file_offset)
56 char * result;
57 /* .bss has no associated storage in the PE file */
58 if(r_size)
59 v_size=r_size;
60 else
61 #if defined(__svr4__) || defined(_SCO_DS)
62 fprintf(stderr,"xmmap: %s line %d doesn't support MAP_ANON\n",__FILE__, __LINE__);
63 #else
64 flags |= MAP_ANON;
65 #endif
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;
72 flags |= MAP_FIXED;
75 mmap(vaddr, v_size, prot, MAP_ANONYMOUS | flags, 0, 0);
76 lseek(fd, file_offset, SEEK_SET);
77 read(fd, vaddr, v_size);
78 return vaddr;
80 #endif
82 void dump_exports(struct PE_Export_Directory * pe_exports, unsigned int load_addr)
84 char * Module;
85 int i;
86 u_short * ordinal;
87 u_long * function;
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",
92 Module,
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 static DWORD PE_FindExportedFunction(struct pe_data *pe, char* funcName)
110 struct PE_Export_Directory * exports = pe->pe_export;
111 unsigned load_addr = pe->load_addr;
112 u_short * ordinal;
113 u_long * function;
114 u_char ** name, *ename;
115 int i;
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++)
122 if(HIWORD(funcName))
124 ename = (char *) (((char *) load_addr) + (int) *name);
125 if(strcmp(ename,funcName)==0)
126 return load_addr+*function;
127 }else{
128 if(funcName == (int)*ordinal + exports->Base)
129 return load_addr+*function;
131 function++;
132 ordinal++;
133 name++;
135 return 0;
138 DWORD PE_GetProcAddress(HMODULE hModule, char* function)
140 NE_MODULE *pModule;
142 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
143 if (!(pModule->flags & NE_FFLAGS_WIN32)) return 0;
144 if (pModule->flags & NE_FFLAGS_BUILTIN)
146 BUILTIN_DLL *dll = (BUILTIN_DLL *)pModule->pe_module;
147 if(HIWORD(function))
148 return RELAY32_GetEntryPoint(dll,function,0);
149 else
150 return RELAY32_GetEntryPoint(dll,0,(int)function);
152 if (!pModule->pe_module) return 0;
153 return PE_FindExportedFunction( pModule->pe_module, function );
156 void fixup_imports(struct pe_data *pe, HMODULE hModule)
158 struct PE_Import_Directory * pe_imp;
159 int fixup_failed=0;
160 unsigned int load_addr = pe->load_addr;
161 int i;
162 NE_MODULE *ne_mod;
163 HMODULE *mod_ptr;
165 /* OK, now dump the import list */
166 dprintf_win32(stddeb, "\nDumping imports list\n");
168 /* first, count the number of imported non-internal modules */
169 pe_imp = pe->pe_import;
170 for(i=0;pe_imp->ModuleName;pe_imp++)
171 i++;
173 /* Now, allocate memory for dlls_to_init */
174 ne_mod = GlobalLock(hModule);
175 ne_mod->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT,(i+1) * sizeof(HMODULE),
176 hModule, FALSE, FALSE, FALSE );
177 mod_ptr = GlobalLock(ne_mod->dlls_to_init);
178 /* load the modules and put their handles into the list */
179 for(i=0,pe_imp = pe->pe_import;pe_imp->ModuleName;pe_imp++)
181 char *name = (char*)load_addr+pe_imp->ModuleName;
182 mod_ptr[i] = LoadModule(name,(LPVOID)-1);
183 if(mod_ptr[i]<=(HMODULE)32)
185 fprintf(stderr,"Module %s not found\n",name);
186 exit(0);
188 i++;
190 pe_imp = pe->pe_import;
191 while (pe_imp->ModuleName)
193 char * Module;
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);
208 while(*import_list)
210 pe_name = (struct pe_import_name *) ((int) load_addr + ((unsigned)*import_list & ~0x80000000));
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),
216 ordinal);
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),
221 pe_name->Name);
223 #else
224 fprintf(stderr,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
225 #endif
227 if(!*thunk_list)
229 fprintf(stderr,"No implementation for %s.%d(%s)\n",Module,
230 pe_name->Hint, pe_name->Name);
231 fixup_failed=1;
234 import_list++;
235 thunk_list++;
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);
241 while(*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");
245 exit(0);
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 */
250 #if 0
251 *thunk_list=(unsigned int)RELAY32_GetEntryPoint(Module,pe_name->Name,pe_name->Hint);
252 if(*thunk_list == 0)
253 #endif
254 *thunk_list = WIN32_GetProcAddress(MODULE_FindModule(Module),
255 pe_name->Name);
256 #else
257 fprintf(stderr,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
258 #endif
259 if(!*thunk_list) {
260 fprintf(stderr,"No implementation for %s.%d\n",Module, pe_name->Hint);
261 fixup_failed=1;
263 thunk_list++;
266 pe_imp++;
268 if(fixup_failed)exit(1);
271 static void calc_vma_size(struct pe_data *pe)
273 int i;
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< 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 pe->pe_seg[i].Name,
281 pe->pe_seg[i].Virtual_Size,
282 pe->pe_seg[i].Virtual_Address,
283 pe->pe_seg[i].Size_Of_Raw_Data,
284 pe->pe_seg[i].PointerToRawData,
285 pe->pe_seg[i].PointerToRelocations,
286 pe->pe_seg[i].PointerToLinenumbers,
287 pe->pe_seg[i].NumberOfRelocations,
288 pe->pe_seg[i].NumberOfLinenumbers,
289 pe->pe_seg[i].Characteristics);
290 pe->vma_size = MAX(pe->vma_size,
291 pe->pe_seg[i].Virtual_Address +
292 pe->pe_seg[i].Size_Of_Raw_Data);
296 static void do_relocations(struct pe_data *pe)
298 int delta = pe->load_addr - pe->base_addr;
299 struct PE_Reloc_Block *r = pe->pe_reloc;
300 int hdelta = (delta >> 16) & 0xFFFF;
301 int ldelta = delta & 0xFFFF;
302 /* int reloc_size = */
303 if(delta == 0)
304 /* Nothing to do */
305 return;
306 while(r->PageRVA)
308 char *page = (char*)pe->load_addr + r->PageRVA;
309 int count = (r->BlockSize - 8)/2;
310 int i;
311 dprintf_fixup(stddeb, "%x relocations for page %lx\n",
312 count, r->PageRVA);
313 /* patching in reverse order */
314 for(i=0;i<count;i++)
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);
319 switch(type)
321 case IMAGE_REL_BASED_ABSOLUTE: break;
322 case IMAGE_REL_BASED_HIGH:
323 *(short*)(page+offset) += hdelta;
324 break;
325 case IMAGE_REL_BASED_LOW:
326 *(short*)(page+offset) += ldelta;
327 break;
328 case IMAGE_REL_BASED_HIGHLOW:
329 #if 1
330 *(int*)(page+offset) += delta;
331 #else
332 { int h=*(unsigned short*)(page+offset);
333 int l=r->Relocations[++i];
334 *(unsigned int*)(page + offset) = (h<<16) + l + delta;
336 #endif
337 break;
338 case IMAGE_REL_BASED_HIGHADJ:
339 fprintf(stderr, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
340 break;
341 case IMAGE_REL_BASED_MIPS_JMPADDR:
342 fprintf(stderr, "Is this a MIPS machine ???\n");
343 break;
344 default:
345 fprintf(stderr, "Unknown fixup type\n");
346 break;
349 r = (struct PE_Reloc_Block*)((char*)r + r->BlockSize);
357 /**********************************************************************
358 * PE_LoadImage
359 * Load one PE format executable into memory
361 static struct pe_data *PE_LoadImage( int fd, HMODULE hModule, WORD offset )
363 struct pe_data *pe;
364 int i, result;
365 unsigned int load_addr;
366 struct Directory dir;
368 pe = xmalloc(sizeof(struct pe_data));
369 memset(pe,0,sizeof(struct pe_data));
370 pe->pe_header = xmalloc(sizeof(struct pe_header_s));
372 /* read PE header */
373 lseek( fd, offset, SEEK_SET);
374 read( fd, pe->pe_header, sizeof(struct pe_header_s));
376 /* read sections */
377 pe->pe_seg = xmalloc(sizeof(struct pe_segment_table) *
378 pe->pe_header->coff.NumberOfSections);
379 read( fd, pe->pe_seg, sizeof(struct pe_segment_table) *
380 pe->pe_header->coff.NumberOfSections);
382 load_addr = pe->pe_header->opt_coff.BaseOfImage;
383 pe->base_addr=load_addr;
384 pe->vma_size=0;
385 dprintf_win32(stddeb, "Load addr is %x\n",load_addr);
386 calc_vma_size(pe);
388 /* We use malloc here, while a huge part of that address space does
389 not be supported by actual memory. It has to be contiguous, though.
390 I don't know if mmap("/dev/null"); would do any better.
391 What I'd really like to do is a Win32 style VirtualAlloc/MapViewOfFile
392 sequence */
393 load_addr = pe->load_addr = malloc(pe->vma_size);
394 dprintf_win32(stddeb, "Load addr is really %x, range %x\n",
395 pe->load_addr, pe->vma_size);
398 for(i=0; i < pe->pe_header->coff.NumberOfSections; i++)
400 /* load only non-BSS segments */
401 if(pe->pe_seg[i].Characteristics &
402 ~ IMAGE_SCN_TYPE_CNT_UNINITIALIZED_DATA)
403 if(lseek(fd,pe->pe_seg[i].PointerToRawData,SEEK_SET) == -1
404 || read(fd,load_addr + pe->pe_seg[i].Virtual_Address,
405 pe->pe_seg[i].Size_Of_Raw_Data)
406 != pe->pe_seg[i].Size_Of_Raw_Data)
408 fprintf(stderr,"Failed to load section %x\n", i);
409 exit(0);
411 result = load_addr + pe->pe_seg[i].Virtual_Address;
412 #if 0
413 if(!load_addr) {
415 result = (int)xmmap((char *)0, pe->pe_seg[i].Virtual_Size,
416 pe->pe_seg[i].Size_Of_Raw_Data, 7,
417 MAP_PRIVATE, fd, pe->pe_seg[i].PointerToRawData);
418 load_addr = (unsigned int) result - pe->pe_seg[i].Virtual_Address;
419 } else {
420 result = (int)xmmap((char *) load_addr + pe->pe_seg[i].Virtual_Address,
421 pe->pe_seg[i].Virtual_Size,
422 pe->pe_seg[i].Size_Of_Raw_Data, 7, MAP_PRIVATE | MAP_FIXED,
423 fd, pe->pe_seg[i].PointerToRawData);
425 if(result==-1){
426 fprintf(stderr,"Could not load section %x to desired address %lx\n",
427 i, load_addr+pe->pe_seg[i].Virtual_Address);
428 fprintf(stderr,"Need to implement relocations now\n");
429 exit(0);
431 #endif
433 if(strcmp(pe->pe_seg[i].Name, ".bss") == 0)
434 memset((void *)result, 0,
435 pe->pe_seg[i].Virtual_Size ?
436 pe->pe_seg[i].Virtual_Size :
437 pe->pe_seg[i].Size_Of_Raw_Data);
439 if(strcmp(pe->pe_seg[i].Name, ".idata") == 0)
440 pe->pe_import = (struct PE_Import_Directory *) result;
442 if(strcmp(pe->pe_seg[i].Name, ".edata") == 0)
443 pe->pe_export = (struct PE_Export_Directory *) result;
445 if(strcmp(pe->pe_seg[i].Name, ".rsrc") == 0) {
446 pe->pe_resource = (struct PE_Resource_Directory *) result;
447 #if 0
448 /* FIXME pe->resource_offset should be deleted from structure if this
449 ifdef doesn't break anything */
450 /* save offset for PE_FindResource */
451 pe->resource_offset = pe->pe_seg[i].Virtual_Address -
452 pe->pe_seg[i].PointerToRawData;
453 #endif
455 if(strcmp(pe->pe_seg[i].Name, ".reloc") == 0)
456 pe->pe_reloc = (struct PE_Reloc_Block *) result;
460 /* There is word that the actual loader does not care about the
461 section names, and only goes for the DataDirectory */
462 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
463 if(dir.Size)
465 if(pe->pe_export &&
466 pe->pe_export!=load_addr+dir.Virtual_address)
467 fprintf(stderr,"wrong export directory??\n");
468 /* always trust the directory */
469 pe->pe_export = load_addr+dir.Virtual_address;
472 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
473 if(dir.Size)
475 if(pe->pe_import &&
476 pe->pe_import!=load_addr+dir.Virtual_address)
477 fprintf(stderr,"wrong import directory??\n");
478 pe->pe_import = load_addr+dir.Virtual_address;
481 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
482 if(dir.Size)
484 if(pe->pe_resource &&
485 pe->pe_resource!=load_addr+dir.Virtual_address)
486 fprintf(stderr,"wrong resource directory??\n");
487 pe->pe_resource = load_addr+dir.Virtual_address;
490 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_BASE_RELOCATION_TABLE];
491 if(dir.Size)
493 if(pe->pe_reloc &&
494 pe->pe_reloc!=load_addr+dir.Virtual_address)
495 fprintf(stderr,"wrong relocation list??\n");
496 pe->pe_reloc = load_addr+dir.Virtual_address;
499 if(pe->pe_header->opt_coff.DataDirectory
500 [IMAGE_FILE_EXCEPTION_DIRECTORY].Size)
501 dprintf_win32(stdnimp,"Exception directory ignored\n");
503 if(pe->pe_header->opt_coff.DataDirectory
504 [IMAGE_FILE_SECURITY_DIRECTORY].Size)
505 dprintf_win32(stdnimp,"Security directory ignored\n");
507 if(pe->pe_header->opt_coff.DataDirectory
508 [IMAGE_FILE_DEBUG_DIRECTORY].Size)
509 dprintf_win32(stdnimp,"Debug directory ignored\n");
511 if(pe->pe_header->opt_coff.DataDirectory
512 [IMAGE_FILE_DESCRIPTION_STRING].Size)
513 dprintf_win32(stdnimp,"Description string ignored\n");
515 if(pe->pe_header->opt_coff.DataDirectory
516 [IMAGE_FILE_MACHINE_VALUE].Size)
517 dprintf_win32(stdnimp,"Machine Value ignored\n");
519 if(pe->pe_header->opt_coff.DataDirectory
520 [IMAGE_FILE_THREAD_LOCAL_STORAGE].Size)
521 dprintf_win32(stdnimp,"Thread local storage ignored\n");
523 if(pe->pe_header->opt_coff.DataDirectory
524 [IMAGE_FILE_CALLBACK_DIRECTORY].Size)
525 dprintf_win32(stdnimp,"Callback directory ignored\n");
528 if(pe->pe_import) fixup_imports(pe, hModule);
529 if(pe->pe_export) dump_exports(pe->pe_export,load_addr);
530 if(pe->pe_reloc) do_relocations(pe);
531 return pe;
534 HINSTANCE MODULE_CreateInstance(HMODULE hModule,LOADPARAMS *params);
535 void InitTask(struct sigcontext_struct context);
537 HINSTANCE PE_LoadModule( int fd, OFSTRUCT *ofs, LOADPARAMS* params )
539 PE_MODULE *pe;
540 int size, of_size;
541 NE_MODULE *pModule;
542 SEGTABLEENTRY *pSegment;
543 char *pStr;
544 DWORD cts;
545 HMODULE hModule;
546 HINSTANCE hInstance;
547 struct mz_header_s mz_header;
549 ALIAS_UseAliases=1;
551 lseek(fd,0,SEEK_SET);
552 read( fd, &mz_header, sizeof(mz_header) );
554 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
555 + strlen(ofs->szPathName) + 1;
556 size = sizeof(NE_MODULE) +
557 /* loaded file info */
558 of_size +
559 /* segment table: DS,CS */
560 2 * sizeof(SEGTABLEENTRY) +
561 /* name table */
563 /* several empty tables */
566 hModule = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
567 if (!hModule) return (HINSTANCE)11; /* invalid exe */
569 FarSetOwner( hModule, hModule );
571 pModule = (NE_MODULE*)GlobalLock(hModule);
573 /* Set all used entries */
574 pModule->magic=NE_SIGNATURE;
575 pModule->count=1;
576 pModule->next=0;
577 pModule->flags=NE_FFLAGS_WIN32;
578 pModule->dgroup=1;
579 pModule->ss=1;
580 pModule->cs=2;
581 /* Who wants to LocalAlloc for a PE Module? */
582 pModule->heap_size=0x1000;
583 pModule->stack_size=0xF000;
584 pModule->seg_count=1;
585 pModule->modref_count=0;
586 pModule->nrname_size=0;
587 pModule->fileinfo=sizeof(NE_MODULE);
588 pModule->os_flags=NE_OSFLAGS_WINDOWS;
589 pModule->expected_version=0x30A;
590 pModule->self = hModule;
592 /* Set loaded file information */
593 memcpy( pModule + 1, ofs, of_size );
594 ((OFSTRUCT *)(pModule+1))->cBytes = of_size - 1;
596 pSegment=(SEGTABLEENTRY*)((char*)(pModule + 1) + of_size);
597 pModule->seg_table=pModule->dgroup_entry=(int)pSegment-(int)pModule;
598 pSegment->size=0;
599 pSegment->flags=NE_SEGFLAGS_DATA;
600 pSegment->minsize=0x1000;
601 pSegment++;
603 cts=(DWORD)MODULE_GetWndProcEntry16("Win32CallToStart");
604 #ifdef WINELIB32
605 pSegment->selector=(void*)cts;
606 pModule->ip=0;
607 #else
608 pSegment->selector=cts>>16;
609 pModule->ip=cts & 0xFFFF;
610 #endif
611 pSegment++;
613 pStr=(char*)pSegment;
614 pModule->name_table=(int)pStr-(int)pModule;
615 strcpy(pStr,"\x08W32SXXXX");
616 pStr+=9;
618 /* All tables zero terminated */
619 pModule->res_table=pModule->import_table=pModule->entry_table=
620 (int)pStr-(int)pModule;
622 MODULE_RegisterModule(hModule);
624 pe = PE_LoadImage( fd, hModule, mz_header.ne_offset );
626 pModule->pe_module = pe;
627 pModule->heap_size=0x1000;
628 pModule->stack_size=0xE000;
630 /* CreateInstance allocates now 64KB */
631 hInstance=MODULE_CreateInstance(hModule,NULL /* FIX: NULL? really? */);
633 /* FIXME: Is this really the correct place to initialise the DLL? */
634 if ((pe->pe_header->coff.Characteristics & IMAGE_FILE_DLL)) {
635 /* PE_InitDLL(hModule); */
636 } else {
637 ProcessHeap = HeapCreate( 0, 0x10000, 0 );
638 TASK_CreateTask(hModule,hInstance,0,
639 params->hEnvironment,(LPSTR)PTR_SEG_TO_LIN(params->cmdLine),
640 *((WORD*)PTR_SEG_TO_LIN(params->showCmd)+1));
641 PE_InitializeDLLs(hModule);
643 return hInstance;
646 HANDLE32 GetProcessHeap(void)
648 return ProcessHeap;
651 int USER_InitApp(HINSTANCE hInstance);
652 void PE_InitTEB(int hTEB);
654 void PE_Win32CallToStart(struct sigcontext_struct context)
656 int fs;
657 HMODULE hModule;
658 NE_MODULE *pModule;
659 struct pe_data *pe;
661 dprintf_win32(stddeb,"Going to start Win32 program\n");
662 InitTask(context);
663 hModule = GetExePtr( GetCurrentTask() );
664 pModule = MODULE_GetPtr( hModule );
665 USER_InitApp( hModule );
666 fs=(int)GlobalAlloc(GHND,0x10000);
667 PE_InitTEB(fs);
668 __asm__ __volatile__("movw %w0,%%fs"::"r" (fs));
669 CallTaskStart32( (FARPROC)(pModule->pe_module->load_addr +
670 pModule->pe_module->pe_header->opt_coff.AddressOfEntryPoint) );
673 int PE_UnloadImage( HMODULE hModule )
675 printf("PEunloadImage() called!\n");
676 /* free resources, image, unmap */
677 return 1;
680 static void PE_InitDLL(HMODULE hModule)
682 NE_MODULE *pModule;
683 PE_MODULE *pe;
685 hModule = GetExePtr(hModule);
686 if (!(pModule = MODULE_GetPtr(hModule))) return;
687 if (!(pModule->flags & NE_FFLAGS_WIN32) || !(pe = pModule->pe_module))
688 return;
690 /* FIXME: What are the correct values for parameters 2 and 3? */
692 /* Is this a library? */
693 if (pe->pe_header->coff.Characteristics & IMAGE_FILE_DLL)
695 printf("InitPEDLL() called!\n");
696 CallDLLEntryProc32( (FARPROC)(pe->load_addr +
697 pe->pe_header->opt_coff.AddressOfEntryPoint),
698 hModule, 0, 0 );
703 /* FIXME: This stuff is all on a "well it works" basis. An implementation
704 based on some kind of documentation would be greatly appreciated :-) */
706 typedef struct
708 void *Except;
709 void *stack;
710 int dummy1[4];
711 struct TEB *TEBDSAlias;
712 int dummy2[2];
713 int taskid;
714 } TEB;
716 void PE_InitTEB(int hTEB)
718 TDB *pTask;
719 TEB *pTEB;
721 pTask = (TDB *)(GlobalLock(GetCurrentTask() & 0xffff));
722 pTEB = (TEB *)(PTR_SEG_OFF_TO_LIN(hTEB, 0));
723 pTEB->stack = (void *)(GlobalLock(pTask->hStack32));
724 pTEB->Except = (void *)(-1);
725 pTEB->TEBDSAlias = pTEB;
726 pTEB->taskid = getpid();
729 void PE_InitializeDLLs(HMODULE hModule)
731 NE_MODULE *pModule;
732 HMODULE *pDLL;
733 pModule = MODULE_GetPtr( GetExePtr(hModule) );
734 if (pModule->dlls_to_init)
736 HANDLE to_init = pModule->dlls_to_init;
737 pModule->dlls_to_init = 0;
738 for (pDLL = (HMODULE *)GlobalLock( to_init ); *pDLL; pDLL++)
740 PE_InitializeDLLs( *pDLL );
741 PE_InitDLL( *pDLL );
743 GlobalFree( to_init );
745 PE_InitDLL( hModule );
747 #endif /* WINELIB */