Release 960331
[wine/hacks.git] / loader / pe_image.c
blobced45d012b2e756e2102b207c63e6defa78bc096
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 "dlls.h"
22 #include "neexe.h"
23 #include "peexe.h"
24 #include "pe_image.h"
25 #include "relay32.h"
26 #include "module.h"
27 #include "alias.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 "xmalloc.h"
36 struct w_files *wine_files = NULL;
38 void my_wcstombs(char * result, u_short * source, int len)
40 while(len--) {
41 /* this used to be isascii, but see isascii implementation in Linux'
42 ctype.h */
43 if(*source<255) *result++ = *source++;
44 else {
45 printf("Unable to handle unicode right now\n");
46 exit(0);
51 #if 0
52 char * xmmap(char * vaddr, unsigned int v_size, unsigned int r_size,
53 int prot, int flags, int fd, unsigned int file_offset)
55 char * result;
56 /* .bss has no associated storage in the PE file */
57 if(r_size)
58 v_size=r_size;
59 else
60 #ifdef __svr4__
61 fprintf(stderr,"xmmap: %s line %d doesn't support MAP_ANON\n",__FILE__, __LINE__);
62 #else
63 flags |= MAP_ANON;
64 #endif
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;
71 flags |= MAP_FIXED;
74 mmap(vaddr, v_size, prot, MAP_ANONYMOUS | flags, 0, 0);
75 lseek(fd, file_offset, SEEK_SET);
76 read(fd, vaddr, v_size);
77 return vaddr;
79 #endif
81 void dump_exports(struct PE_Export_Directory * pe_exports, unsigned int load_addr)
83 char * Module;
84 int i;
85 u_short * ordinal;
86 u_long * function;
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",
91 Module,
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;
111 u_short * ordinal;
112 u_long * function;
113 u_char ** name, *ename;
114 int i;
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++)
121 if(HIWORD(funcName))
123 ename = (char *) (((char *) load_addr) + (int) *name);
124 if(strcmp(ename,funcName)==0)
125 return load_addr+*function;
126 }else{
127 if(funcName == (int)*ordinal + exports->Base)
128 return load_addr+*function;
130 function++;
131 ordinal++;
132 name++;
134 return 0;
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;
142 if(!wpnt)return 0;
143 if(wpnt->builtin)
145 if(HIWORD(function))
146 return RELAY32_GetEntryPoint(wpnt->builtin,function,0);
147 else
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;
156 int fixup_failed=0;
157 unsigned int load_addr = wpnt->pe->load_addr;
158 int i;
159 NE_MODULE *ne_mod;
160 HMODULE *mod_ptr;
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++)
168 i++;
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))
180 continue;
181 mod_ptr[i] = LoadModule(name,(LPVOID)-1);
182 if(mod_ptr[i]<=(HMODULE)32)
184 fprintf(stderr,"Module %s not found\n",name);
185 exit(0);
187 i++;
189 pe_imp = wpnt->pe->pe_import;
190 while (pe_imp->ModuleName)
192 char * Module;
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);
207 while(*import_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),
215 ordinal);
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),
220 pe_name->Name);
222 #else
223 fprintf(stderr,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
224 #endif
226 if(!*thunk_list)
228 fprintf(stderr,"No implementation for %s.%d(%s)\n",Module,
229 pe_name->Hint, pe_name->Name);
230 fixup_failed=1;
233 import_list++;
234 thunk_list++;
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);
240 while(*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");
244 exit(0);
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 */
249 #if 0
250 *thunk_list=(unsigned int)RELAY32_GetEntryPoint(Module,pe_name->Name,pe_name->Hint);
251 if(*thunk_list == 0)
252 #endif
253 *thunk_list = WIN32_GetProcAddress(MODULE_FindModule(Module),
254 pe_name->Name);
255 #else
256 fprintf(stderr,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
257 #endif
258 if(!*thunk_list) {
259 fprintf(stderr,"No implementation for %s.%d\n",Module, pe_name->Hint);
260 fixup_failed=1;
262 thunk_list++;
265 pe_imp++;
267 if(fixup_failed)exit(1);
270 static void calc_vma_size(struct w_files *wpnt)
272 int i;
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 = */
302 if(delta == 0)
303 /* Nothing to do */
304 return;
305 while(r->PageRVA)
307 char *page = (char*)wpnt->pe->load_addr + r->PageRVA;
308 int count = (r->BlockSize - 8)/2;
309 int i;
310 dprintf_fixup(stddeb, "%x relocations for page %lx\n",
311 count, r->PageRVA);
312 /* patching in reverse order */
313 for(i=0;i<count;i++)
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);
318 switch(type)
320 case IMAGE_REL_BASED_ABSOLUTE: break;
321 case IMAGE_REL_BASED_HIGH:
322 *(short*)(page+offset) += hdelta;
323 break;
324 case IMAGE_REL_BASED_LOW:
325 *(short*)(page+offset) += ldelta;
326 break;
327 case IMAGE_REL_BASED_HIGHLOW:
328 #if 1
329 *(int*)(page+offset) += delta;
330 #else
331 { int h=*(unsigned short*)(page+offset);
332 int l=r->Relocations[++i];
333 *(unsigned int*)(page + offset) = (h<<16) + l + delta;
335 #endif
336 break;
337 case IMAGE_REL_BASED_HIGHADJ:
338 fprintf(stderr, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
339 break;
340 case IMAGE_REL_BASED_MIPS_JMPADDR:
341 fprintf(stderr, "Is this a MIPS machine ???\n");
342 break;
343 default:
344 fprintf(stderr, "Unknown fixup type\n");
345 break;
348 r = (struct PE_Reloc_Block*)((char*)r + r->BlockSize);
356 /**********************************************************************
357 * PE_LoadImage
358 * Load one PE format executable into memory
360 static HINSTANCE PE_LoadImage( int fd, struct w_files *wpnt )
362 int i, result;
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));
370 /* read PE header */
371 lseek( fd, wpnt->mz_header->ne_offset, SEEK_SET);
372 read( fd, wpnt->pe->pe_header, sizeof(struct pe_header_s));
374 /* read sections */
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);
384 calc_vma_size(wpnt);
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
390 sequence */
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);
407 exit(0);
409 result = load_addr + wpnt->pe->pe_seg[i].Virtual_Address;
410 #if 0
411 if(!load_addr) {
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;
417 } else {
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);
423 if(result==-1){
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");
427 exit(0);
429 #endif
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;
445 #if 0
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;
451 #endif
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];
461 if(dir.Size)
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];
471 if(dir.Size)
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];
480 if(dir.Size)
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];
489 if(dir.Size)
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;
541 int size, of_size;
542 NE_MODULE *pModule;
543 SEGTABLEENTRY *pSegment;
544 char *pStr;
545 DWORD cts;
546 HMODULE hModule;
547 HINSTANCE hInstance;
549 ALIAS_UseAliases=1;
551 wpnt=xmalloc(sizeof(struct w_files));
552 wpnt->ofs=*ofs;
553 wpnt->type=0;
554 wpnt->hinstance=0;
555 wpnt->hModule=0;
556 wpnt->initialised=0;
557 wpnt->builtin=0;
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 */
566 of_size +
567 /* segment table: DS,CS */
568 2 * sizeof(SEGTABLEENTRY) +
569 /* name table */
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;
584 pModule->count=1;
585 pModule->next=0;
586 pModule->flags=NE_FFLAGS_WIN32;
587 pModule->dgroup=1;
588 pModule->ss=1;
589 pModule->cs=2;
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;
607 pSegment->size=0;
608 pSegment->flags=NE_SEGFLAGS_DATA;
609 pSegment->minsize=0x1000;
610 pSegment++;
612 cts=(DWORD)MODULE_GetWndProcEntry16("Win32CallToStart");
613 #ifdef WINELIB32
614 pSegment->selector=(void*)cts;
615 pModule->ip=0;
616 #else
617 pSegment->selector=cts>>16;
618 pModule->ip=cts & 0xFFFF;
619 #endif
620 pSegment++;
622 pStr=(char*)pSegment;
623 pModule->name_table=(int)pStr-(int)pModule;
624 strcpy(pStr,"\x08W32SXXXX");
625 pStr+=9;
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);
645 } else {
646 wpnt->name = xstrdup( ofs->szPathName );
649 wpnt->next=wine_files;
650 wine_files=wpnt;
652 /* FIXME: Is this really the correct place to initialise the DLL? */
653 if ((wpnt->pe->pe_header->coff.Characteristics & IMAGE_FILE_DLL)) {
654 PE_InitDLL(hModule);
655 } else {
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));
660 return hInstance;
663 int USER_InitApp(HINSTANCE hInstance);
664 void PE_InitTEB(int hTEB);
666 void PE_Win32CallToStart(struct sigcontext_struct context)
668 int fs;
669 struct w_files *wpnt=wine_files;
670 dprintf_win32(stddeb,"Going to start Win32 program\n");
671 InitTask(context);
672 USER_InitApp(wpnt->hModule);
673 fs=(int)GlobalAlloc(GHND,0x10000);
674 PE_InitTEB(fs);
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 */
683 return 1;
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)
693 return;
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 :-) */
709 typedef struct
711 void *Except;
712 void *stack;
713 int dummy1[4];
714 struct TEB *TEBDSAlias;
715 int dummy2[2];
716 int taskid;
717 } TEB;
719 void PE_InitTEB(int hTEB)
721 TDB *pTask;
722 TEB *pTEB;
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)
734 NE_MODULE *pModule;
735 HMODULE *pDLL;
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 );
744 PE_InitDLL( *pDLL );
746 GlobalFree( to_init );
748 PE_InitDLL( hModule );
750 #endif /* WINELIB */