Release 970112
[wine/multimedia.git] / loader / pe_image.c
blob73c9ce9f895d9d70b79dbf2c958f679d6aee1dee
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 "stddebug.h"
32 #include "debug.h"
33 #include "debugger.h"
34 #include "xmalloc.h"
36 void my_wcstombs(char * result, u_short * source, int len)
38 while(len--) {
39 /* this used to be isascii, but see isascii implementation in Linux'
40 ctype.h */
41 if(*source<255) *result++ = *source++;
42 else {
43 printf("Unable to handle unicode right now\n");
44 exit(0);
49 #if 0
50 char * xmmap(char * vaddr, unsigned int v_size, unsigned int r_size,
51 int prot, int flags, int fd, unsigned int file_offset)
53 char * result;
54 /* .bss has no associated storage in the PE file */
55 if(r_size)
56 v_size=r_size;
57 else
58 #if defined(__svr4__) || defined(_SCO_DS)
59 fprintf(stderr,"xmmap: %s line %d doesn't support MAP_ANON\n",__FILE__, __LINE__);
60 #else
61 flags |= MAP_ANON;
62 #endif
63 result = mmap(vaddr, v_size, prot, flags, fd, file_offset);
64 if((unsigned int) result != 0xffffffff) return result;
66 /* Sigh. Alignment must be wrong for mmap. Do this the hard way. */
67 if(!(flags & MAP_FIXED)) {
68 vaddr = (char *)0x40000000;
69 flags |= MAP_FIXED;
72 mmap(vaddr, v_size, prot, MAP_ANONYMOUS | flags, 0, 0);
73 lseek(fd, file_offset, SEEK_SET);
74 read(fd, vaddr, v_size);
75 return vaddr;
77 #endif
79 void dump_exports(struct PE_Export_Directory * pe_exports, unsigned int load_addr)
81 char *Module;
82 int i;
83 u_short *ordinal;
84 u_long *function,*functions;
85 u_char **name,*ename;
86 char buffer[1000];
87 DBG_ADDR daddr;
89 daddr.seg = 0;
90 daddr.type = NULL;
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, NULL, SYM_WIN32 | SYM_FUNC);
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 (LOWORD(funcName)-exports->Base > exports->Number_Of_Functions) {
157 dprintf_win32(stddeb," ordinal %d out of range!\n",
158 LOWORD(funcName));
159 return NULL;
161 return (FARPROC32)(load_addr+function[(int)funcName-exports->Base]);
163 return NULL;
166 void
167 fixup_imports (struct pe_data *pe, HMODULE16 hModule)
169 struct PE_Import_Directory *pe_imp;
170 int fixup_failed = 0;
171 unsigned int load_addr = pe->load_addr;
172 int i;
173 NE_MODULE *ne_mod;
174 HMODULE16 *mod_ptr;
176 /* OK, now dump the import list */
177 dprintf_win32 (stddeb, "\nDumping imports list\n");
179 /* first, count the number of imported non-internal modules */
180 pe_imp = pe->pe_import;
181 for (i = 0; pe_imp->ModuleName; pe_imp++)
182 i++;
184 /* Now, allocate memory for dlls_to_init */
185 ne_mod = GlobalLock16 (hModule);
186 ne_mod->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT, (i+1)*sizeof(HMODULE16),
187 hModule, FALSE, FALSE, FALSE);
188 mod_ptr = GlobalLock16 (ne_mod->dlls_to_init);
189 /* load the modules and put their handles into the list */
190 for (i = 0, pe_imp = pe->pe_import; pe_imp->ModuleName; pe_imp++) {
191 char *name = (char *) load_addr + pe_imp->ModuleName;
192 mod_ptr[i] = LoadModule (name, (LPVOID) - 1);
193 if (mod_ptr[i] <= (HMODULE16) 32) {
194 char *p, buffer[256];
196 /* Try with prepending the path of the current module */
197 GetModuleFileName16 (hModule, buffer, sizeof (buffer));
198 if (!(p = strrchr (buffer, '\\')))
199 p = buffer;
200 strcpy (p + 1, name);
201 mod_ptr[i] = LoadModule (buffer, (LPVOID) - 1);
203 if (mod_ptr[i] <= (HMODULE16) 32) {
204 fprintf (stderr, "Module %s not found\n", name);
205 exit (0);
207 i++;
209 pe_imp = pe->pe_import;
210 while (pe_imp->ModuleName) {
211 char *Module;
212 struct pe_import_name *pe_name;
213 unsigned int *import_list, *thunk_list;
215 Module = ((char *) load_addr) + pe_imp->ModuleName;
216 dprintf_win32 (stddeb, "%s\n", Module);
218 if (pe_imp->Import_List != 0) { /* original microsoft style */
219 dprintf_win32 (stddeb, "Microsoft style imports used\n");
220 import_list = (unsigned int *)(((unsigned int)load_addr)+pe_imp->Import_List);
221 thunk_list = (unsigned int *)(((unsigned int)load_addr)+pe_imp->Thunk_List);
223 while (*import_list) {
224 pe_name = (struct pe_import_name *) ((int) load_addr + ((unsigned) *import_list & ~0x80000000));
225 if ((unsigned) *import_list & 0x80000000) {
226 int ordinal = *import_list & (0x80000000 - 1);
227 dprintf_win32 (stddeb, "--- Ordinal %s,%d\n", Module, ordinal);
228 *thunk_list = (unsigned)GetProcAddress32(MODULE_FindModule (Module),
229 (LPCSTR) ordinal);
230 if (!*thunk_list) {
231 fprintf(stderr,"No implementation for %s.%d, setting to NULL\n",
232 Module, ordinal);
233 /* fixup_failed=1; */
235 } else { /* import by name */
236 dprintf_win32 (stddeb, "--- %s %s.%d\n", pe_name->Name, Module, pe_name->Hint);
237 *thunk_list = (unsigned)GetProcAddress32(MODULE_FindModule (Module),
238 pe_name->Name);
239 if (!*thunk_list) {
240 fprintf(stderr, "No implementation for %s.%d(%s), setting to NULL\n",
241 Module, pe_name->Hint, pe_name->Name);
242 /* fixup_failed=1; */
245 import_list++;
246 thunk_list++;
248 } else { /* Borland style */
249 dprintf_win32 (stddeb, "Borland style imports used\n");
250 thunk_list = (unsigned int *)(((unsigned int)load_addr)+pe_imp->Thunk_List);
251 while (*thunk_list) {
252 pe_name=(struct pe_import_name *)((int)load_addr+*thunk_list);
253 if ((unsigned) pe_name & 0x80000000) {
254 /* not sure about this branch, but it seems to work */
255 int ordinal = *thunk_list & ~0x80000000;
256 dprintf_win32(stddeb,"--- Ordinal %s.%d\n",Module,ordinal);
257 *thunk_list = (unsigned)GetProcAddress32(MODULE_FindModule (Module),
258 (LPCSTR) ordinal);
259 if (!*thunk_list) {
260 fprintf(stderr, "No implementation for %s.%d, setting to NULL\n",
261 Module,ordinal);
262 /* fixup_failed=1; */
264 } else {
265 dprintf_win32(stddeb,"--- %s %s.%d\n",
266 pe_name->Name, Module, pe_name->Hint);
267 *thunk_list = (unsigned)GetProcAddress32(MODULE_FindModule(Module),
268 pe_name->Name);
269 if (!*thunk_list) {
270 fprintf(stderr, "No implementation for %s.%d, setting to NULL\n",
271 Module, pe_name->Hint);
272 /* fixup_failed=1; */
275 thunk_list++;
278 pe_imp++;
280 if (fixup_failed) exit(1);
283 static void calc_vma_size(struct pe_data *pe)
285 int i;
287 dprintf_win32(stddeb, "Dump of segment table\n");
288 dprintf_win32(stddeb, " Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
289 for(i=0; i< pe->pe_header->coff.NumberOfSections; i++)
291 dprintf_win32(stddeb, "%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
292 pe->pe_seg[i].Name,
293 pe->pe_seg[i].Virtual_Size,
294 pe->pe_seg[i].Virtual_Address,
295 pe->pe_seg[i].Size_Of_Raw_Data,
296 pe->pe_seg[i].PointerToRawData,
297 pe->pe_seg[i].PointerToRelocations,
298 pe->pe_seg[i].PointerToLinenumbers,
299 pe->pe_seg[i].NumberOfRelocations,
300 pe->pe_seg[i].NumberOfLinenumbers,
301 pe->pe_seg[i].Characteristics);
302 pe->vma_size = MAX(pe->vma_size,
303 pe->pe_seg[i].Virtual_Address +
304 pe->pe_seg[i].Size_Of_Raw_Data);
308 static void do_relocations(struct pe_data *pe)
310 int delta = pe->load_addr - pe->base_addr;
311 struct PE_Reloc_Block *r = pe->pe_reloc;
312 int hdelta = (delta >> 16) & 0xFFFF;
313 int ldelta = delta & 0xFFFF;
314 /* int reloc_size = */
315 if(delta == 0)
316 /* Nothing to do */
317 return;
318 while(r->PageRVA)
320 char *page = (char*)pe->load_addr + r->PageRVA;
321 int count = (r->BlockSize - 8)/2;
322 int i;
323 dprintf_fixup(stddeb, "%x relocations for page %lx\n",
324 count, r->PageRVA);
325 /* patching in reverse order */
326 for(i=0;i<count;i++)
328 int offset = r->Relocations[i] & 0xFFF;
329 int type = r->Relocations[i] >> 12;
330 dprintf_fixup(stddeb,"patching %x type %x\n", offset, type);
331 switch(type)
333 case IMAGE_REL_BASED_ABSOLUTE: break;
334 case IMAGE_REL_BASED_HIGH:
335 *(short*)(page+offset) += hdelta;
336 break;
337 case IMAGE_REL_BASED_LOW:
338 *(short*)(page+offset) += ldelta;
339 break;
340 case IMAGE_REL_BASED_HIGHLOW:
341 #if 1
342 *(int*)(page+offset) += delta;
343 #else
344 { int h=*(unsigned short*)(page+offset);
345 int l=r->Relocations[++i];
346 *(unsigned int*)(page + offset) = (h<<16) + l + delta;
348 #endif
349 break;
350 case IMAGE_REL_BASED_HIGHADJ:
351 fprintf(stderr, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
352 break;
353 case IMAGE_REL_BASED_MIPS_JMPADDR:
354 fprintf(stderr, "Is this a MIPS machine ???\n");
355 break;
356 default:
357 fprintf(stderr, "Unknown fixup type\n");
358 break;
361 r = (struct PE_Reloc_Block*)((char*)r + r->BlockSize);
369 /**********************************************************************
370 * PE_LoadImage
371 * Load one PE format executable into memory
373 static struct pe_data *PE_LoadImage( int fd, HMODULE16 hModule, WORD offset )
375 struct pe_data *pe;
376 int i, result;
377 int load_addr;
378 struct Directory dir;
379 char buffer[200];
380 DBG_ADDR daddr;
382 daddr.seg=0;
383 daddr.type = NULL;
384 pe = xmalloc(sizeof(struct pe_data));
385 memset(pe,0,sizeof(struct pe_data));
386 pe->pe_header = xmalloc(sizeof(struct pe_header_s));
388 /* read PE header */
389 lseek( fd, offset, SEEK_SET);
390 read( fd, pe->pe_header, sizeof(struct pe_header_s));
392 /* FIXME: this is a *horrible* hack to make COMDLG32.DLL load OK. The
393 problem needs to be fixed properly at some stage */
395 if (pe->pe_header->opt_coff.NumberOfRvaAndSizes != 16) {
396 printf("Short PE Header!!!\n");
397 lseek( fd, -(16 - pe->pe_header->opt_coff.NumberOfRvaAndSizes) * sizeof (struct Directory), SEEK_CUR);
400 /* horrible hack ends !!! */
402 /* read sections */
403 pe->pe_seg = xmalloc(sizeof(struct pe_segment_table) *
404 pe->pe_header->coff.NumberOfSections);
405 read( fd, pe->pe_seg, sizeof(struct pe_segment_table) *
406 pe->pe_header->coff.NumberOfSections);
408 load_addr = pe->pe_header->opt_coff.BaseOfImage;
409 pe->base_addr=load_addr;
410 pe->vma_size=0;
411 dprintf_win32(stddeb, "Load addr is %x\n",load_addr);
412 calc_vma_size(pe);
414 #if 0
415 /* We use malloc here, while a huge part of that address space does
416 not be supported by actual memory. It has to be contiguous, though.
417 I don't know if mmap("/dev/null"); would do any better.
418 What I'd really like to do is a Win32 style VirtualAlloc/MapViewOfFile
419 sequence */
420 load_addr = pe->load_addr = (int)xmalloc(pe->vma_size);
421 memset( load_addr, 0, pe->vma_size);
422 #else
423 load_addr = pe->load_addr = VirtualAlloc( NULL, pe->vma_size,
424 MEM_COMMIT,
425 PAGE_EXECUTE_READWRITE );
426 #endif
428 dprintf_win32(stddeb, "Load addr is really %x, range %x\n",
429 pe->load_addr, pe->vma_size);
432 for(i=0; i < pe->pe_header->coff.NumberOfSections; i++)
434 /* load only non-BSS segments */
435 if(pe->pe_seg[i].Characteristics &
436 ~ IMAGE_SCN_TYPE_CNT_UNINITIALIZED_DATA)
437 if(lseek(fd,pe->pe_seg[i].PointerToRawData,SEEK_SET) == -1
438 || read(fd,(char *)load_addr + pe->pe_seg[i].Virtual_Address,
439 pe->pe_seg[i].Size_Of_Raw_Data)
440 != pe->pe_seg[i].Size_Of_Raw_Data)
442 fprintf(stderr,"Failed to load section %x\n", i);
443 exit(0);
445 result = load_addr + pe->pe_seg[i].Virtual_Address;
446 #if 0
447 if(!load_addr) {
449 result = (int)xmmap((char *)0, pe->pe_seg[i].Virtual_Size,
450 pe->pe_seg[i].Size_Of_Raw_Data, 7,
451 MAP_PRIVATE, fd, pe->pe_seg[i].PointerToRawData);
452 load_addr = (unsigned int) result - pe->pe_seg[i].Virtual_Address;
453 } else {
454 result = (int)xmmap((char *) load_addr + pe->pe_seg[i].Virtual_Address,
455 pe->pe_seg[i].Virtual_Size,
456 pe->pe_seg[i].Size_Of_Raw_Data, 7, MAP_PRIVATE | MAP_FIXED,
457 fd, pe->pe_seg[i].PointerToRawData);
459 if(result==-1){
460 fprintf(stderr,"Could not load section %x to desired address %lx\n",
461 i, load_addr+pe->pe_seg[i].Virtual_Address);
462 fprintf(stderr,"Need to implement relocations now\n");
463 exit(0);
465 #endif
467 #if 0
468 /* not needed, memory is zero */
469 if(strcmp(pe->pe_seg[i].Name, ".bss") == 0)
470 memset((void *)result, 0,
471 pe->pe_seg[i].Virtual_Size ?
472 pe->pe_seg[i].Virtual_Size :
473 pe->pe_seg[i].Size_Of_Raw_Data);
474 #endif
476 if(strcmp(pe->pe_seg[i].Name, ".idata") == 0)
477 pe->pe_import = (struct PE_Import_Directory *) result;
479 if(strcmp(pe->pe_seg[i].Name, ".edata") == 0)
480 pe->pe_export = (struct PE_Export_Directory *) result;
482 if(strcmp(pe->pe_seg[i].Name, ".rsrc") == 0)
483 pe->pe_resource = (struct PE_Resource_Directory *) result;
485 if(strcmp(pe->pe_seg[i].Name, ".reloc") == 0)
486 pe->pe_reloc = (struct PE_Reloc_Block *) result;
490 /* There is word that the actual loader does not care about the
491 section names, and only goes for the DataDirectory */
492 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
493 if(dir.Size)
495 if(pe->pe_export &&
496 (int)pe->pe_export!=load_addr+dir.Virtual_address)
497 fprintf(stderr,"wrong export directory??\n");
498 /* always trust the directory */
499 pe->pe_export = (void *)(load_addr+dir.Virtual_address);
502 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
503 if(dir.Size)
505 if(pe->pe_import &&
506 (int)pe->pe_import!=load_addr+dir.Virtual_address)
507 fprintf(stderr,"wrong import directory??\n");
508 pe->pe_import = (void *)(load_addr+dir.Virtual_address);
511 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
512 if(dir.Size)
514 if(pe->pe_resource &&
515 (int)pe->pe_resource!=load_addr+dir.Virtual_address)
516 fprintf(stderr,"wrong resource directory??\n");
517 pe->pe_resource = (void *)(load_addr+dir.Virtual_address);
520 dir=pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_BASE_RELOCATION_TABLE];
521 if(dir.Size)
523 if(pe->pe_reloc &&
524 (int)pe->pe_reloc!=load_addr+dir.Virtual_address)
525 fprintf(stderr,"wrong relocation list??\n");
526 pe->pe_reloc = (void *)(load_addr+dir.Virtual_address);
529 if(pe->pe_header->opt_coff.DataDirectory
530 [IMAGE_FILE_EXCEPTION_DIRECTORY].Size)
531 dprintf_win32(stdnimp,"Exception directory ignored\n");
533 if(pe->pe_header->opt_coff.DataDirectory
534 [IMAGE_FILE_SECURITY_DIRECTORY].Size)
535 dprintf_win32(stdnimp,"Security directory ignored\n");
537 if(pe->pe_header->opt_coff.DataDirectory
538 [IMAGE_FILE_DEBUG_DIRECTORY].Size)
540 DEBUG_RegisterDebugInfo(fd, pe, load_addr,
541 pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_DEBUG_DIRECTORY].Virtual_address,
542 pe->pe_header->opt_coff.DataDirectory[IMAGE_FILE_DEBUG_DIRECTORY].Size);
545 if(pe->pe_header->opt_coff.DataDirectory
546 [IMAGE_FILE_DESCRIPTION_STRING].Size)
547 dprintf_win32(stdnimp,"Description string ignored\n");
549 if(pe->pe_header->opt_coff.DataDirectory
550 [IMAGE_FILE_MACHINE_VALUE].Size)
551 dprintf_win32(stdnimp,"Machine Value ignored\n");
553 if(pe->pe_header->opt_coff.DataDirectory
554 [IMAGE_FILE_THREAD_LOCAL_STORAGE].Size)
555 dprintf_win32(stdnimp,"Thread local storage ignored\n");
557 if(pe->pe_header->opt_coff.DataDirectory
558 [IMAGE_FILE_CALLBACK_DIRECTORY].Size)
559 dprintf_win32(stdnimp,"Callback directory ignored\n");
562 if(pe->pe_reloc) do_relocations(pe);
563 if(pe->pe_import) fixup_imports(pe, hModule);
564 if(pe->pe_export) dump_exports(pe->pe_export,load_addr);
566 if (pe->pe_export) {
567 /* add start of sections as debugsymbols */
568 for(i=0;i<pe->pe_header->coff.NumberOfSections;i++) {
569 sprintf(buffer,"%s.%s",
570 ((char*)load_addr)+pe->pe_export->Name,
571 pe->pe_seg[i].Name
573 daddr.off=load_addr+pe->pe_seg[i].Virtual_Address;
574 DEBUG_AddSymbol(buffer,&daddr, NULL, SYM_WIN32 | SYM_FUNC);
576 /* add entry point */
577 sprintf(buffer,"%s.EntryPoint",((char*)load_addr)+pe->pe_export->Name);
578 daddr.off=load_addr+pe->pe_header->opt_coff.AddressOfEntryPoint;
579 DEBUG_AddSymbol(buffer,&daddr, NULL, SYM_WIN32 | SYM_FUNC);
580 /* add start of DLL */
581 daddr.off=load_addr;
582 DEBUG_AddSymbol(((char*)load_addr)+pe->pe_export->Name,&daddr,
583 NULL, SYM_WIN32 | SYM_FUNC);
585 return pe;
588 HINSTANCE16 MODULE_CreateInstance(HMODULE16 hModule,LOADPARAMS *params);
590 HINSTANCE16 PE_LoadModule( int fd, OFSTRUCT *ofs, LOADPARAMS* params )
592 HMODULE16 hModule;
593 HINSTANCE16 hInstance;
594 NE_MODULE *pModule;
595 struct mz_header_s mz_header;
597 if ((hModule = MODULE_CreateDummyModule( ofs )) < 32) return hModule;
598 pModule = (NE_MODULE *)GlobalLock16( hModule );
599 pModule->flags = NE_FFLAGS_WIN32;
601 lseek( fd, 0, SEEK_SET );
602 read( fd, &mz_header, sizeof(mz_header) );
604 pModule->pe_module = PE_LoadImage( fd, hModule, mz_header.ne_offset );
606 hInstance = MODULE_CreateInstance( hModule, params );
608 if (!(pModule->pe_module->pe_header->coff.Characteristics & IMAGE_FILE_DLL))
610 TASK_CreateTask( hModule, hInstance, 0,
611 params->hEnvironment,
612 (LPSTR)PTR_SEG_TO_LIN( params->cmdLine ),
613 *((WORD*)PTR_SEG_TO_LIN(params->showCmd) + 1) );
615 return hInstance;
618 int PE_UnloadImage( HMODULE16 hModule )
620 printf("PEunloadImage() called!\n");
621 /* free resources, image, unmap */
622 return 1;
625 static void PE_InitDLL(HMODULE16 hModule)
627 NE_MODULE *pModule;
628 PE_MODULE *pe;
630 hModule = GetExePtr(hModule);
631 if (!(pModule = MODULE_GetPtr(hModule))) return;
632 if (!(pModule->flags & NE_FFLAGS_WIN32) || !(pe = pModule->pe_module))
633 return;
635 /* FIXME: What is the correct value for parameter 3?
636 * (the MSDN library JAN96 says 'reserved for future use')
639 /* Is this a library? */
640 if (pe->pe_header->coff.Characteristics & IMAGE_FILE_DLL)
642 printf("InitPEDLL() called!\n");
643 CallDLLEntryProc32( (FARPROC32)(pe->load_addr +
644 pe->pe_header->opt_coff.AddressOfEntryPoint),
645 hModule, DLL_PROCESS_ATTACH, -1 );
649 void PE_InitializeDLLs(HMODULE16 hModule)
651 NE_MODULE *pModule;
652 HMODULE16 *pDLL;
653 pModule = MODULE_GetPtr( GetExePtr(hModule) );
654 if (pModule->dlls_to_init)
656 HGLOBAL16 to_init = pModule->dlls_to_init;
657 pModule->dlls_to_init = 0;
658 for (pDLL = (HMODULE16 *)GlobalLock16( to_init ); *pDLL; pDLL++)
660 PE_InitializeDLLs( *pDLL );
661 PE_InitDLL( *pDLL );
663 GlobalFree16( to_init );
665 PE_InitDLL( hModule );
667 #endif /* WINELIB */