Release 960225
[wine.git] / loader / pe_image.c
blob5abdeae350f77ecf433e14ef1c0791579ef9a848
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 "selectors.h"
33 #include "stddebug.h"
34 #include "debug.h"
35 #include "xmalloc.h"
37 struct w_files *wine_files = NULL;
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 #ifdef __svr4__
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 DWORD PE_FindExportedFunction(struct w_files* wpnt, char* funcName)
110 struct PE_Export_Directory * exports = wpnt->pe->pe_export;
111 unsigned load_addr = wpnt->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 struct w_files *wpnt;
141 for(wpnt=wine_files;wpnt;wpnt=wpnt->next)
142 if(wpnt->hModule==hModule) break;
143 if(!wpnt)return 0;
144 if(wpnt->builtin)
146 if(HIWORD(function))
147 return RELAY32_GetEntryPoint(wpnt->builtin,function,0);
148 else
149 return RELAY32_GetEntryPoint(wpnt->builtin,0,(int)function);
151 return PE_FindExportedFunction(wpnt,function);
154 void fixup_imports(struct w_files* wpnt)
156 struct PE_Import_Directory * pe_imp;
157 int fixup_failed=0;
158 unsigned int load_addr = wpnt->pe->load_addr;
159 int i;
160 NE_MODULE *ne_mod;
161 HMODULE *mod_ptr;
163 /* OK, now dump the import list */
164 dprintf_win32(stddeb, "\nDumping imports list\n");
166 /* first, count the number of imported non-internal modules */
167 pe_imp = wpnt->pe->pe_import;
168 for(i=0;pe_imp->ModuleName;pe_imp++)
169 i++;
171 /* Now, allocate memory for dlls_to_init */
172 ne_mod = GlobalLock(wpnt->hModule);
173 ne_mod->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT,(i+1) * sizeof(HMODULE),
174 wpnt->hModule, FALSE, FALSE, FALSE );
175 mod_ptr = GlobalLock(ne_mod->dlls_to_init);
176 /* load the modules and put their handles into the list */
177 for(i=0,pe_imp = wpnt->pe->pe_import;pe_imp->ModuleName;pe_imp++)
179 char *name = (char*)load_addr+pe_imp->ModuleName;
180 if(RELAY32_GetBuiltinDLL(name))
181 continue;
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 = wpnt->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 + *import_list);
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 w_files *wpnt)
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< wpnt->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 wpnt->pe->pe_seg[i].Name,
281 wpnt->pe->pe_seg[i].Virtual_Size,
282 wpnt->pe->pe_seg[i].Virtual_Address,
283 wpnt->pe->pe_seg[i].Size_Of_Raw_Data,
284 wpnt->pe->pe_seg[i].PointerToRawData,
285 wpnt->pe->pe_seg[i].PointerToRelocations,
286 wpnt->pe->pe_seg[i].PointerToLinenumbers,
287 wpnt->pe->pe_seg[i].NumberOfRelocations,
288 wpnt->pe->pe_seg[i].NumberOfLinenumbers,
289 wpnt->pe->pe_seg[i].Characteristics);
290 wpnt->pe->vma_size = max(wpnt->pe->vma_size,
291 wpnt->pe->pe_seg[i].Virtual_Address +
292 wpnt->pe->pe_seg[i].Size_Of_Raw_Data);
296 static void do_relocations(struct w_files *wpnt)
298 int delta = wpnt->pe->load_addr - wpnt->pe->base_addr;
299 struct PE_Reloc_Block *r = wpnt->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*)wpnt->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 HINSTANCE PE_LoadImage( int fd, struct w_files *wpnt )
363 int i, result;
364 unsigned int load_addr;
365 struct Directory dir;
367 wpnt->pe = xmalloc(sizeof(struct pe_data));
368 memset(wpnt->pe,0,sizeof(struct pe_data));
369 wpnt->pe->pe_header = xmalloc(sizeof(struct pe_header_s));
371 /* read PE header */
372 lseek( fd, wpnt->mz_header->ne_offset, SEEK_SET);
373 read( fd, wpnt->pe->pe_header, sizeof(struct pe_header_s));
375 /* read sections */
376 wpnt->pe->pe_seg = xmalloc(sizeof(struct pe_segment_table) *
377 wpnt->pe->pe_header->coff.NumberOfSections);
378 read( fd, wpnt->pe->pe_seg, sizeof(struct pe_segment_table) *
379 wpnt->pe->pe_header->coff.NumberOfSections);
381 load_addr = wpnt->pe->pe_header->opt_coff.BaseOfImage;
382 wpnt->pe->base_addr=load_addr;
383 wpnt->pe->vma_size=0;
384 dprintf_win32(stddeb, "Load addr is %x\n",load_addr);
385 calc_vma_size(wpnt);
387 /* We use malloc here, while a huge part of that address space does
388 not be supported by actual memory. It has to be contiguous, though.
389 I don't know if mmap("/dev/null"); would do any better.
390 What I'd really like to do is a Win32 style VirtualAlloc/MapViewOfFile
391 sequence */
392 load_addr = wpnt->pe->load_addr = malloc(wpnt->pe->vma_size);
393 dprintf_win32(stddeb, "Load addr is really %x, range %x\n",
394 wpnt->pe->load_addr, wpnt->pe->vma_size);
397 for(i=0; i < wpnt->pe->pe_header->coff.NumberOfSections; i++)
399 /* load only non-BSS segments */
400 if(wpnt->pe->pe_seg[i].Characteristics &
401 ~ IMAGE_SCN_TYPE_CNT_UNINITIALIZED_DATA)
402 if(lseek(fd,wpnt->pe->pe_seg[i].PointerToRawData,SEEK_SET) == -1
403 || read(fd,load_addr + wpnt->pe->pe_seg[i].Virtual_Address,
404 wpnt->pe->pe_seg[i].Size_Of_Raw_Data)
405 != wpnt->pe->pe_seg[i].Size_Of_Raw_Data)
407 fprintf(stderr,"Failed to load section %x\n", i);
408 exit(0);
410 result = load_addr + wpnt->pe->pe_seg[i].Virtual_Address;
411 #if 0
412 if(!load_addr) {
414 result = (int)xmmap((char *)0, wpnt->pe->pe_seg[i].Virtual_Size,
415 wpnt->pe->pe_seg[i].Size_Of_Raw_Data, 7,
416 MAP_PRIVATE, fd, wpnt->pe->pe_seg[i].PointerToRawData);
417 load_addr = (unsigned int) result - wpnt->pe->pe_seg[i].Virtual_Address;
418 } else {
419 result = (int)xmmap((char *) load_addr + wpnt->pe->pe_seg[i].Virtual_Address,
420 wpnt->pe->pe_seg[i].Virtual_Size,
421 wpnt->pe->pe_seg[i].Size_Of_Raw_Data, 7, MAP_PRIVATE | MAP_FIXED,
422 fd, wpnt->pe->pe_seg[i].PointerToRawData);
424 if(result==-1){
425 fprintf(stderr,"Could not load section %x to desired address %lx\n",
426 i, load_addr+wpnt->pe->pe_seg[i].Virtual_Address);
427 fprintf(stderr,"Need to implement relocations now\n");
428 exit(0);
430 #endif
432 if(strcmp(wpnt->pe->pe_seg[i].Name, ".bss") == 0)
433 memset((void *)result, 0,
434 wpnt->pe->pe_seg[i].Virtual_Size ?
435 wpnt->pe->pe_seg[i].Virtual_Size :
436 wpnt->pe->pe_seg[i].Size_Of_Raw_Data);
438 if(strcmp(wpnt->pe->pe_seg[i].Name, ".idata") == 0)
439 wpnt->pe->pe_import = (struct PE_Import_Directory *) result;
441 if(strcmp(wpnt->pe->pe_seg[i].Name, ".edata") == 0)
442 wpnt->pe->pe_export = (struct PE_Export_Directory *) result;
444 if(strcmp(wpnt->pe->pe_seg[i].Name, ".rsrc") == 0) {
445 wpnt->pe->pe_resource = (struct PE_Resource_Directory *) result;
446 #if 0
447 /* FIXME pe->resource_offset should be deleted from structure if this
448 ifdef doesn't break anything */
449 /* save offset for PE_FindResource */
450 wpnt->pe->resource_offset = wpnt->pe->pe_seg[i].Virtual_Address -
451 wpnt->pe->pe_seg[i].PointerToRawData;
452 #endif
454 if(strcmp(wpnt->pe->pe_seg[i].Name, ".reloc") == 0)
455 wpnt->pe->pe_reloc = (struct PE_Reloc_Block *) result;
459 /* There is word that the actual loader does not care about the
460 section names, and only goes for the DataDirectory */
461 dir=wpnt->pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
462 if(dir.Size)
464 if(wpnt->pe->pe_export &&
465 wpnt->pe->pe_export!=load_addr+dir.Virtual_address)
466 fprintf(stderr,"wrong export directory??\n");
467 /* always trust the directory */
468 wpnt->pe->pe_export = load_addr+dir.Virtual_address;
471 dir=wpnt->pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
472 if(dir.Size)
474 if(wpnt->pe->pe_import &&
475 wpnt->pe->pe_import!=load_addr+dir.Virtual_address)
476 fprintf(stderr,"wrong import directory??\n");
477 wpnt->pe->pe_import = load_addr+dir.Virtual_address;
480 dir=wpnt->pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
481 if(dir.Size)
483 if(wpnt->pe->pe_resource &&
484 wpnt->pe->pe_resource!=load_addr+dir.Virtual_address)
485 fprintf(stderr,"wrong resource directory??\n");
486 wpnt->pe->pe_resource = load_addr+dir.Virtual_address;
489 dir=wpnt->pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_BASE_RELOCATION_TABLE];
490 if(dir.Size)
492 if(wpnt->pe->pe_reloc &&
493 wpnt->pe->pe_reloc!=load_addr+dir.Virtual_address)
494 fprintf(stderr,"wrong relocation list??\n");
495 wpnt->pe->pe_reloc = load_addr+dir.Virtual_address;
498 if(wpnt->pe->pe_header->opt_coff.DataDirectory
499 [IMAGE_FILE_EXCEPTION_DIRECTORY].Size)
500 dprintf_win32(stdnimp,"Exception directory ignored\n");
502 if(wpnt->pe->pe_header->opt_coff.DataDirectory
503 [IMAGE_FILE_SECURITY_DIRECTORY].Size)
504 dprintf_win32(stdnimp,"Security directory ignored\n");
506 if(wpnt->pe->pe_header->opt_coff.DataDirectory
507 [IMAGE_FILE_DEBUG_DIRECTORY].Size)
508 dprintf_win32(stdnimp,"Debug directory ignored\n");
510 if(wpnt->pe->pe_header->opt_coff.DataDirectory
511 [IMAGE_FILE_DESCRIPTION_STRING].Size)
512 dprintf_win32(stdnimp,"Description string ignored\n");
514 if(wpnt->pe->pe_header->opt_coff.DataDirectory
515 [IMAGE_FILE_MACHINE_VALUE].Size)
516 dprintf_win32(stdnimp,"Machine Value ignored\n");
518 if(wpnt->pe->pe_header->opt_coff.DataDirectory
519 [IMAGE_FILE_THREAD_LOCAL_STORAGE].Size)
520 dprintf_win32(stdnimp,"Thread local storage ignored\n");
522 if(wpnt->pe->pe_header->opt_coff.DataDirectory
523 [IMAGE_FILE_CALLBACK_DIRECTORY].Size)
524 dprintf_win32(stdnimp,"Callback directory ignored\n");
527 if(wpnt->pe->pe_import) fixup_imports(wpnt);
528 if(wpnt->pe->pe_export) dump_exports(wpnt->pe->pe_export,load_addr);
529 if(wpnt->pe->pe_reloc) do_relocations(wpnt);
531 wpnt->hinstance = (HINSTANCE)0x8000;
532 wpnt->load_addr = load_addr;
533 return (wpnt->hinstance);
536 HINSTANCE MODULE_CreateInstance(HMODULE hModule,LOADPARAMS *params);
537 void InitTask(struct sigcontext_struct context);
539 HINSTANCE PE_LoadModule(int fd, OFSTRUCT *ofs, LOADPARAMS* params)
541 struct w_files *wpnt;
542 int size;
543 NE_MODULE *pModule;
544 LOADEDFILEINFO *pFileInfo;
545 SEGTABLEENTRY *pSegment;
546 char *pStr;
547 DWORD cts;
548 HMODULE hModule;
549 HINSTANCE hInstance;
551 ALIAS_UseAliases=1;
553 wpnt=xmalloc(sizeof(struct w_files));
554 wpnt->ofs=*ofs;
555 wpnt->type=0;
556 wpnt->hinstance=0;
557 wpnt->hModule=0;
558 wpnt->initialised=0;
559 wpnt->builtin=0;
560 lseek(fd,0,SEEK_SET);
561 wpnt->mz_header=xmalloc(sizeof(struct mz_header_s));
562 read(fd,wpnt->mz_header,sizeof(struct mz_header_s));
564 size=sizeof(NE_MODULE) +
565 /* loaded file info */
566 sizeof(LOADEDFILEINFO) + strlen(ofs->szPathName) +
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=PE_SIGNATURE;
584 pModule->count=1;
585 pModule->next=0;
586 pModule->flags=0;
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)+
597 sizeof(LOADEDFILEINFO)+strlen(ofs->szPathName);
598 pModule->fileinfo=sizeof(NE_MODULE);
599 pModule->os_flags=NE_OSFLAGS_WINDOWS;
600 pModule->expected_version=0x30A;
602 pFileInfo=(LOADEDFILEINFO *)(pModule + 1);
603 pFileInfo->length = sizeof(LOADEDFILEINFO)+strlen(ofs->szPathName)-1;
604 strcpy(pFileInfo->filename,ofs->szPathName);
606 pSegment=(SEGTABLEENTRY*)((char*)pFileInfo+pFileInfo->length+1);
607 pModule->dgroup_entry=(int)pSegment-(int)pModule;
608 pSegment->size=0;
609 pSegment->flags=NE_SEGFLAGS_DATA;
610 pSegment->minsize=0x1000;
611 pSegment++;
613 cts=(DWORD)GetWndProcEntry16("Win32CallToStart");
614 #ifdef WINELIB32
615 pSegment->selector=(void*)cts;
616 pModule->ip=0;
617 #else
618 pSegment->selector=cts>>16;
619 pModule->ip=cts & 0xFFFF;
620 #endif
621 pSegment++;
623 pStr=(char*)pSegment;
624 pModule->name_table=(int)pStr-(int)pModule;
625 strcpy(pStr,"\x08W32SXXXX");
626 pStr+=9;
628 /* All tables zero terminated */
629 pModule->res_table=pModule->import_table=pModule->entry_table=
630 (int)pStr-(int)pModule;
632 MODULE_RegisterModule(hModule);
634 PE_LoadImage( fd, wpnt );
636 pModule->heap_size=0x1000;
637 pModule->stack_size=0xE000;
639 /* CreateInstance allocates now 64KB */
640 hInstance=MODULE_CreateInstance(hModule,NULL /* FIX: NULL? really? */);
641 wpnt->hinstance=hInstance;
643 if (wpnt->pe->pe_export) {
644 pStr = ((unsigned char *)(wpnt->load_addr))+wpnt->pe->pe_export->Name;
645 wpnt->name = xstrdup(pStr);
646 } else {
647 wpnt->name = xstrdup( ofs->szPathName );
650 wpnt->next=wine_files;
651 wine_files=wpnt;
653 /* FIXME: Is this really the correct place to initialise the DLL? */
654 if ((wpnt->pe->pe_header->coff.Characteristics & IMAGE_FILE_DLL)) {
655 PE_InitDLL(wpnt);
656 } else {
657 TASK_CreateTask(hModule,hInstance,0,
658 params->hEnvironment,(LPSTR)PTR_SEG_TO_LIN(params->cmdLine),
659 *((WORD*)PTR_SEG_TO_LIN(params->showCmd)+1));
661 return hInstance;
664 int USER_InitApp(HINSTANCE hInstance);
665 void PE_InitTEB(int hTEB);
667 void PE_Win32CallToStart(struct sigcontext_struct context)
669 int fs;
670 struct w_files *wpnt=wine_files;
671 dprintf_win32(stddeb,"Going to start Win32 program\n");
672 InitTask(context);
673 USER_InitApp(wpnt->hModule);
674 fs=(int)GlobalAlloc(GHND,0x10000);
675 PE_InitTEB(fs);
676 __asm__ __volatile__("movw %w0,%%fs"::"r" (fs));
677 ((void(*)())(wpnt->load_addr+wpnt->pe->pe_header->opt_coff.AddressOfEntryPoint))();
680 int PE_UnloadImage(struct w_files *wpnt)
682 printf("PEunloadImage() called!\n");
683 /* free resources, image, unmap */
684 return 1;
687 void PE_InitDLL(HMODULE hModule)
689 struct w_files *wpnt;
690 hModule = GetExePtr(hModule);
691 for(wpnt = wine_files;wpnt && wpnt->hModule != hModule;
692 wpnt = wpnt->next) /*nothing*/;
693 if(!wpnt || wpnt->initialised)
694 return;
695 /* FIXME: What are the correct values for parameters 2 and 3? */
697 /* Is this a library? */
698 if (wpnt->pe->pe_header->coff.Characteristics & IMAGE_FILE_DLL) {
699 /* Should call DLLEntryPoint here */
700 printf("InitPEDLL() called!\n");
701 wpnt->initialised = 1;
702 ((void(*)())(wpnt->load_addr+wpnt->pe->pe_header->opt_coff.AddressOfEntryPoint))(wpnt->hModule, 0, 0);
707 /* FIXME: This stuff is all on a "well it works" basis. An implementation
708 based on some kind of documentation would be greatly appreciated :-) */
710 typedef struct
712 void *Except;
713 void *stack;
714 int dummy1[4];
715 struct TEB *TEBDSAlias;
716 int dummy2[2];
717 int taskid;
718 } TEB;
720 void PE_InitTEB(int hTEB)
722 TDB *pTask;
723 TEB *pTEB;
725 pTask = (TDB *)(GlobalLock(GetCurrentTask() & 0xffff));
726 pTEB = (TEB *)(PTR_SEG_OFF_TO_LIN(hTEB, 0));
727 pTEB->stack = (void *)(GlobalLock(pTask->hStack32));
728 pTEB->Except = (void *)(-1);
729 pTEB->TEBDSAlias = pTEB;
730 pTEB->taskid = getpid();
733 void PE_InitializeDLLs(HMODULE hModule)
735 NE_MODULE *pModule;
736 HMODULE *pDLL;
737 pModule = (NE_MODULE *)GlobalLock( GetExePtr(hModule) );
738 if (pModule->dlls_to_init)
740 HANDLE to_init = pModule->dlls_to_init;
741 pModule->dlls_to_init = 0;
742 for (pDLL = (HMODULE *)GlobalLock( to_init ); *pDLL; pDLL++)
744 PE_InitializeDLLs( *pDLL );
745 PE_InitDLL( *pDLL );
747 GlobalFree( to_init );
749 PE_InitDLL( hModule );
751 #endif /* WINELIB */