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
10 * ftp.microsoft.com:/developr/MSDN/OctCD/PEFILE.ZIP
18 #include <sys/types.h>
32 #include "registers.h"
37 static HANDLE32 ProcessHeap
; /* FIXME: should be in process database */
39 void my_wcstombs(char * result
, u_short
* source
, int len
)
42 /* this used to be isascii, but see isascii implementation in Linux'
44 if(*source
<255) *result
++ = *source
++;
46 printf("Unable to handle unicode right now\n");
53 char * xmmap(char * vaddr
, unsigned int v_size
, unsigned int r_size
,
54 int prot
, int flags
, int fd
, unsigned int file_offset
)
57 /* .bss has no associated storage in the PE file */
61 #if defined(__svr4__) || defined(_SCO_DS)
62 fprintf(stderr
,"xmmap: %s line %d doesn't support MAP_ANON\n",__FILE__
, __LINE__
);
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;
75 mmap(vaddr
, v_size
, prot
, MAP_ANONYMOUS
| flags
, 0, 0);
76 lseek(fd
, file_offset
, SEEK_SET
);
77 read(fd
, vaddr
, v_size
);
82 void dump_exports(struct PE_Export_Directory
* pe_exports
, unsigned int load_addr
)
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",
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
;
114 u_char
** name
, *ename
;
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
++)
124 ename
= (char *) (((char *) load_addr
) + (int) *name
);
125 if(strcmp(ename
,funcName
)==0)
126 return load_addr
+*function
;
128 if(funcName
== (int)*ordinal
+ exports
->Base
)
129 return load_addr
+*function
;
138 DWORD
PE_GetProcAddress(HMODULE hModule
, char* function
)
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
;
148 return RELAY32_GetEntryPoint(dll
,function
,0);
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
;
160 unsigned int load_addr
= pe
->load_addr
;
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
++)
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
);
190 pe_imp
= pe
->pe_import
;
191 while (pe_imp
->ModuleName
)
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
);
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
),
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
),
224 fprintf(stderr
,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
229 fprintf(stderr
,"No implementation for %s.%d(%s)\n",Module
,
230 pe_name
->Hint
, pe_name
->Name
);
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
);
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");
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 */
251 *thunk_list
=(unsigned int)RELAY32_GetEntryPoint(Module
,pe_name
->Name
,pe_name
->Hint
);
254 *thunk_list
= WIN32_GetProcAddress(MODULE_FindModule(Module
),
257 fprintf(stderr
,"JBP: Call to RELAY32_GetEntryPoint being ignored.\n");
260 fprintf(stderr
,"No implementation for %s.%d\n",Module
, pe_name
->Hint
);
268 if(fixup_failed
)exit(1);
271 static void calc_vma_size(struct pe_data
*pe
)
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",
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 = */
308 char *page
= (char*)pe
->load_addr
+ r
->PageRVA
;
309 int count
= (r
->BlockSize
- 8)/2;
311 dprintf_fixup(stddeb
, "%x relocations for page %lx\n",
313 /* patching in reverse order */
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
);
321 case IMAGE_REL_BASED_ABSOLUTE
: break;
322 case IMAGE_REL_BASED_HIGH
:
323 *(short*)(page
+offset
) += hdelta
;
325 case IMAGE_REL_BASED_LOW
:
326 *(short*)(page
+offset
) += ldelta
;
328 case IMAGE_REL_BASED_HIGHLOW
:
330 *(int*)(page
+offset
) += delta
;
332 { int h
=*(unsigned short*)(page
+offset
);
333 int l
=r
->Relocations
[++i
];
334 *(unsigned int*)(page
+ offset
) = (h
<<16) + l
+ delta
;
338 case IMAGE_REL_BASED_HIGHADJ
:
339 fprintf(stderr
, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
341 case IMAGE_REL_BASED_MIPS_JMPADDR
:
342 fprintf(stderr
, "Is this a MIPS machine ???\n");
345 fprintf(stderr
, "Unknown fixup type\n");
349 r
= (struct PE_Reloc_Block
*)((char*)r
+ r
->BlockSize
);
357 /**********************************************************************
359 * Load one PE format executable into memory
361 static struct pe_data
*PE_LoadImage( int fd
, HMODULE hModule
, WORD offset
)
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
));
373 lseek( fd
, offset
, SEEK_SET
);
374 read( fd
, pe
->pe_header
, sizeof(struct pe_header_s
));
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
;
385 dprintf_win32(stddeb
, "Load addr is %x\n",load_addr
);
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
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
);
411 result
= load_addr
+ pe
->pe_seg
[i
].Virtual_Address
;
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
;
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
);
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");
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
;
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
;
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
];
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
];
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
];
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
];
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
);
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
)
542 SEGTABLEENTRY
*pSegment
;
547 struct mz_header_s mz_header
;
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 */
559 /* segment table: DS,CS */
560 2 * sizeof(SEGTABLEENTRY
) +
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
;
577 pModule
->flags
=NE_FFLAGS_WIN32
;
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
;
599 pSegment
->flags
=NE_SEGFLAGS_DATA
;
600 pSegment
->minsize
=0x1000;
603 cts
=(DWORD
)MODULE_GetWndProcEntry16("Win32CallToStart");
605 pSegment
->selector
=(void*)cts
;
608 pSegment
->selector
=cts
>>16;
609 pModule
->ip
=cts
& 0xFFFF;
613 pStr
=(char*)pSegment
;
614 pModule
->name_table
=(int)pStr
-(int)pModule
;
615 strcpy(pStr
,"\x08W32SXXXX");
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); */
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
);
646 HANDLE32
GetProcessHeap(void)
651 int USER_InitApp(HINSTANCE hInstance
);
652 void PE_InitTEB(int hTEB
);
654 void PE_Win32CallToStart(struct sigcontext_struct context
)
661 dprintf_win32(stddeb
,"Going to start Win32 program\n");
663 hModule
= GetExePtr( GetCurrentTask() );
664 pModule
= MODULE_GetPtr( hModule
);
665 USER_InitApp( hModule
);
666 fs
=(int)GlobalAlloc(GHND
,0x10000);
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 */
680 static void PE_InitDLL(HMODULE hModule
)
685 hModule
= GetExePtr(hModule
);
686 if (!(pModule
= MODULE_GetPtr(hModule
))) return;
687 if (!(pModule
->flags
& NE_FFLAGS_WIN32
) || !(pe
= pModule
->pe_module
))
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
),
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 :-) */
711 struct TEB
*TEBDSAlias
;
716 void PE_InitTEB(int hTEB
)
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
)
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
);
743 GlobalFree( to_init
);
745 PE_InitDLL( hModule
);