2 * Copyright 1994 Eric Youndale & Erik Bos
3 * Copyright 1995 Martin von Löwis
4 * Copyright 1996-98 Marcus Meissner
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
13 * Before you start changing something in this file be aware of the following:
15 * - There are several functions called recursively. In a very subtle and
16 * obscure way. DLLs can reference each other recursively etc.
17 * - If you want to enhance, speed up or clean up something in here, think
18 * twice WHY it is implemented in that strange way. There is usually a reason.
19 * Though sometimes it might just be lazyness ;)
20 * - In PE_MapImage, right before fixup_imports() all external and internal
21 * state MUST be correct since this function can be called with the SAME image
22 * AGAIN. (Thats recursion for you.) That means MODREF.module and
24 * - No, you cannot use Linux mmap() to mmap() the images directly. Linux aligns
25 * them at pagesize (4096), Win32 requires 512 byte alignment.
26 * - All those function map things into a new addresspace. From the wrong
27 * process and the wrong thread. So calling other API functions will mess
28 * things up badly sometimes.
31 /*#include <ctype.h>*/
37 #include <sys/types.h>
56 static void PE_InitDLL(PE_MODREF* modref, DWORD type, LPVOID lpReserved);
58 /* convert PE image VirtualAddress to Real Address */
59 #define RVA(x) ((unsigned int)load_addr+(unsigned int)(x))
61 void dump_exports( HMODULE32 hModule )
66 u_long *function,*functions;
68 unsigned int load_addr = hModule;
70 DWORD rva_start = PE_HEADER(hModule)->OptionalHeader
71 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
72 DWORD rva_end = rva_start + PE_HEADER(hModule)->OptionalHeader
73 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
74 IMAGE_EXPORT_DIRECTORY *pe_exports = (IMAGE_EXPORT_DIRECTORY*)RVA(rva_start);
76 Module = (char*)RVA(pe_exports->Name);
77 dprintf_info(win32,"\n*******EXPORT DATA*******\nModule name is %s, %ld functions, %ld names\n",
79 pe_exports->NumberOfFunctions,
80 pe_exports->NumberOfNames);
82 ordinal=(u_short*) RVA(pe_exports->AddressOfNameOrdinals);
83 functions=function=(u_long*) RVA(pe_exports->AddressOfFunctions);
84 name=(u_char**) RVA(pe_exports->AddressOfNames);
86 dprintf_info(win32," Ord RVA Addr Name\n" );
87 for (i=0;i<pe_exports->NumberOfFunctions;i++, function++)
89 if (!*function) continue; /* No such function */
90 if (debugging_info(win32)){
91 dbg_decl_str(win32, 1024);
93 dsprintf(win32,"%4ld %08lx %08x",
94 i + pe_exports->Base, *function, RVA(*function) );
95 /* Check if we have a name for it */
96 for (j = 0; j < pe_exports->NumberOfNames; j++)
98 dsprintf(win32, " %s", (char*)RVA(name[j]) );
99 if ((*function >= rva_start) && (*function <= rva_end))
100 dsprintf(win32, " (forwarded -> %s)", (char *)RVA(*function));
101 dprintf_info(win32,"%s\n", dbg_str(win32));
106 /* Look up the specified function or ordinal in the exportlist:
108 * - look up the name in the Name list.
109 * - look up the ordinal with that index.
110 * - use the ordinal as offset into the functionlist
111 * If it is a ordinal:
112 * - use ordinal-pe_export->Base as offset into the functionlist
114 FARPROC32 PE_FindExportedFunction( PDB32 *process, HMODULE32 hModule,
117 IMAGE_EXPORT_DIRECTORY *exports;
121 u_char ** name, *ename;
124 u_long rva_start, rva_end, addr;
127 pem = process->modref_list;
128 while (pem && (pem->module != hModule))
131 fprintf(stderr,"No MODREF found for PE_MODULE %08x in process %p\n",hModule,process);
135 exports = pem->pe_export;
137 if (HIWORD(funcName))
138 dprintf_info(win32,"PE_FindExportedFunction(%s)\n",funcName);
140 dprintf_info(win32,"PE_FindExportedFunction(%d)\n",(int)funcName);
142 fprintf(stderr,"Module %08x/MODREF %p doesn't have a exports table.\n",hModule,pem);
145 ordinal = (u_short*) RVA(exports->AddressOfNameOrdinals);
146 function= (u_long*) RVA(exports->AddressOfFunctions);
147 name = (u_char **) RVA(exports->AddressOfNames);
149 rva_start = PE_HEADER(hModule)->OptionalHeader
150 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
151 rva_end = rva_start + PE_HEADER(hModule)->OptionalHeader
152 .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
154 if (HIWORD(funcName)) {
155 for(i=0; i<exports->NumberOfNames; i++) {
156 ename=(char*)RVA(*name);
157 if(!strcmp(ename,funcName))
159 addr = function[*ordinal];
160 if (!addr) return NULL;
161 if ((addr < rva_start) || (addr >= rva_end))
162 return (FARPROC32)RVA(addr);
163 forward = (char *)RVA(addr);
170 if (LOWORD(funcName)-exports->Base > exports->NumberOfFunctions) {
171 dprintf_info(win32," ordinal %d out of range!\n",
175 addr = function[(int)funcName-exports->Base];
176 if (!addr) return NULL;
177 if ((addr < rva_start) || (addr >= rva_end))
178 return (FARPROC32)RVA(addr);
179 forward = (char *)RVA(addr);
185 char *end = strchr(forward, '.');
186 if (!end) return NULL;
187 strncpy(module, forward, (end - forward));
188 module[end-forward] = 0;
189 hMod = MODULE_HANDLEtoHMODULE32( MODULE_FindModule(module) );
190 return PE_FindExportedFunction( process, hMod, end + 1);
195 DWORD fixup_imports (PDB32 *process,PE_MODREF *pem,HMODULE32 hModule)
197 IMAGE_IMPORT_DESCRIPTOR *pe_imp;
198 int fixup_failed = 0;
199 unsigned int load_addr = pem->module;
204 modname = (char*) RVA(pem->pe_export->Name);
206 modname = "<unknown>";
208 /* OK, now dump the import list */
209 dprintf_info(win32, "\nDumping imports list\n");
211 /* first, count the number of imported non-internal modules */
212 pe_imp = pem->pe_import;
214 fprintf(stderr,"no import directory????\n");
216 /* FIXME: should terminate on 0 Characteristics */
217 for (i = 0; pe_imp->Name; pe_imp++)
220 /* load the imported modules. They are automatically
221 * added to the modref list of the process.
224 /* FIXME: should terminate on 0 Characteristics */
225 for (i = 0, pe_imp = pem->pe_import; pe_imp->Name; pe_imp++) {
227 PE_MODREF *xpem,**ypem;
230 char *name = (char *) RVA(pe_imp->Name);
232 /* don't use MODULE_Load, Win32 creates new task differently */
233 res = PE_LoadLibraryEx32A( name, process, 0, 0 );
234 if (res <= (HMODULE32) 32) {
235 char *p, buffer[1024];
237 /* Try with prepending the path of the current module */
238 GetModuleFileName32A( hModule, buffer, sizeof (buffer));
239 if (!(p = strrchr (buffer, '\\')))
241 strcpy (p + 1, name);
242 res = PE_LoadLibraryEx32A( buffer, process, 0, 0 );
244 if (res <= (HMODULE32) 32) {
245 fprintf (stderr, "Module %s not found\n", name);
248 res = MODULE_HANDLEtoHMODULE32(res);
251 if (xpem->module == res)
256 /* It has been loaded *BEFORE* us, so we have to initialize
257 * it before us. We cannot just link in the xpem before pem,
258 * since xpem might reference more dlls which would be in the
259 * wrong order after that.
260 * Instead we link in pem right AFTER xpem, which should keep
261 * the correct order. (I am not 100% sure about that.)
263 /* unlink pem from chain */
264 ypem = &(process->modref_list);
268 ypem = &((*ypem)->next);
272 /* link pem directly AFTER xpem */
273 pem->next = xpem->next;
279 pe_imp = pem->pe_import;
280 while (pe_imp->Name) {
282 IMAGE_IMPORT_BY_NAME *pe_name;
283 LPIMAGE_THUNK_DATA import_list,thunk_list;
284 HMODULE32 hImpModule;
286 Module = (char *) RVA(pe_imp->Name);
287 hImpModule = MODULE_HANDLEtoHMODULE32( MODULE_FindModule(Module) );
288 dprintf_info(win32, "%s\n", Module);
290 /* FIXME: forwarder entries ... */
292 if (pe_imp->u.OriginalFirstThunk != 0) { /* original MS style */
293 dprintf_info(win32, "Microsoft style imports used\n");
294 import_list =(LPIMAGE_THUNK_DATA) RVA(pe_imp->u.OriginalFirstThunk);
295 thunk_list = (LPIMAGE_THUNK_DATA) RVA(pe_imp->FirstThunk);
297 while (import_list->u1.Ordinal) {
298 if (IMAGE_SNAP_BY_ORDINAL(import_list->u1.Ordinal)) {
299 int ordinal = IMAGE_ORDINAL(import_list->u1.Ordinal);
301 dprintf_info(win32, "--- Ordinal %s,%d\n", Module, ordinal);
302 thunk_list->u1.Function=(LPDWORD)PE_FindExportedFunction(
303 process, hImpModule, (LPCSTR)ordinal);
304 if (!thunk_list->u1.Function) {
305 fprintf(stderr,"No implementation for %s.%d, setting to NULL\n",
307 /* fixup_failed=1; */
309 } else { /* import by name */
310 pe_name = (LPIMAGE_IMPORT_BY_NAME)RVA(import_list->u1.AddressOfData);
311 dprintf_info(win32, "--- %s %s.%d\n", pe_name->Name, Module, pe_name->Hint);
312 thunk_list->u1.Function=(LPDWORD)PE_FindExportedFunction(
313 process, hImpModule, pe_name->Name);
314 if (!thunk_list->u1.Function) {
315 fprintf(stderr,"No implementation for %s.%d(%s), setting to NULL\n",
316 Module,pe_name->Hint,pe_name->Name);
317 /* fixup_failed=1; */
323 } else { /* Borland style */
324 dprintf_info(win32, "Borland style imports used\n");
325 thunk_list = (LPIMAGE_THUNK_DATA) RVA(pe_imp->FirstThunk);
326 while (thunk_list->u1.Ordinal) {
327 if (IMAGE_SNAP_BY_ORDINAL(thunk_list->u1.Ordinal)) {
328 /* not sure about this branch, but it seems to work */
329 int ordinal = IMAGE_ORDINAL(thunk_list->u1.Ordinal);
331 dprintf_info(win32,"--- Ordinal %s.%d\n",Module,ordinal);
332 thunk_list->u1.Function=(LPDWORD)PE_FindExportedFunction(
333 process, hImpModule, (LPCSTR) ordinal);
334 if (!thunk_list->u1.Function) {
335 fprintf(stderr, "No implementation for %s.%d, setting to NULL\n",
337 /* fixup_failed=1; */
340 pe_name=(LPIMAGE_IMPORT_BY_NAME) RVA(thunk_list->u1.AddressOfData);
341 dprintf_info(win32,"--- %s %s.%d\n",
342 pe_name->Name,Module,pe_name->Hint);
343 thunk_list->u1.Function=(LPDWORD)PE_FindExportedFunction(
344 process, hImpModule, pe_name->Name );
345 if (!thunk_list->u1.Function) {
346 fprintf(stderr, "No implementation for %s.%d, setting to NULL\n",
347 Module, pe_name->Hint);
348 /* fixup_failed=1; */
356 if (fixup_failed) return 22;
360 static int calc_vma_size( HMODULE32 hModule )
363 IMAGE_SECTION_HEADER *pe_seg = PE_SECTIONS(hModule);
365 dprintf_info(win32, "Dump of segment table\n");
366 dprintf_info(win32, " Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
367 for (i = 0; i< PE_HEADER(hModule)->FileHeader.NumberOfSections; i++)
369 dprintf_info(win32, "%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
371 pe_seg->Misc.VirtualSize,
372 pe_seg->VirtualAddress,
373 pe_seg->SizeOfRawData,
374 pe_seg->PointerToRawData,
375 pe_seg->PointerToRelocations,
376 pe_seg->PointerToLinenumbers,
377 pe_seg->NumberOfRelocations,
378 pe_seg->NumberOfLinenumbers,
379 pe_seg->Characteristics);
380 vma_size = MAX(vma_size, pe_seg->VirtualAddress+pe_seg->SizeOfRawData);
386 static void do_relocations(PE_MODREF *pem)
388 int delta = pem->module - PE_HEADER(pem->module)->OptionalHeader.ImageBase;
389 unsigned int load_addr= pem->module;
390 IMAGE_BASE_RELOCATION *r = pem->pe_reloc;
391 int hdelta = (delta >> 16) & 0xFFFF;
392 int ldelta = delta & 0xFFFF;
394 /* int reloc_size = */
399 while(r->VirtualAddress)
401 char *page = (char*) RVA(r->VirtualAddress);
402 int count = (r->SizeOfBlock - 8)/2;
404 dprintf_info(fixup, "%x relocations for page %lx\n",
405 count, r->VirtualAddress);
406 /* patching in reverse order */
409 int offset = r->TypeOffset[i] & 0xFFF;
410 int type = r->TypeOffset[i] >> 12;
411 dprintf_info(fixup,"patching %x type %x\n", offset, type);
414 case IMAGE_REL_BASED_ABSOLUTE: break;
415 case IMAGE_REL_BASED_HIGH:
416 *(short*)(page+offset) += hdelta;
418 case IMAGE_REL_BASED_LOW:
419 *(short*)(page+offset) += ldelta;
421 case IMAGE_REL_BASED_HIGHLOW:
423 *(int*)(page+offset) += delta;
425 { int h=*(unsigned short*)(page+offset);
426 int l=r->TypeOffset[++i];
427 *(unsigned int*)(page + offset) = (h<<16) + l + delta;
431 case IMAGE_REL_BASED_HIGHADJ:
432 fprintf(stderr, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
434 case IMAGE_REL_BASED_MIPS_JMPADDR:
435 fprintf(stderr, "Is this a MIPS machine ???\n");
438 fprintf(stderr, "Unknown fixup type\n");
442 r = (IMAGE_BASE_RELOCATION*)((char*)r + r->SizeOfBlock);
450 /**********************************************************************
452 * Load one PE format DLL/EXE into memory
454 * Unluckily we can't just mmap the sections where we want them, for
455 * (at least) Linux does only support offsets which are page-aligned.
457 * BUT we have to map the whole image anyway, for Win32 programs sometimes
458 * want to access them. (HMODULE32 point to the start of it)
460 static HMODULE32 PE_LoadImage( HFILE32 hFile )
465 /* map the PE file somewhere */
466 mapping = CreateFileMapping32A( hFile, NULL, PAGE_READONLY | SEC_COMMIT,
470 fprintf( stderr, "PE_LoadImage: CreateFileMapping error %ld\n",
474 hModule = (HMODULE32)MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
475 CloseHandle( mapping );
478 fprintf( stderr, "PE_LoadImage: MapViewOfFile error %ld\n",
483 if (PE_HEADER(hModule)->Signature != IMAGE_NT_SIGNATURE)
485 fprintf(stderr,"image doesn't have PE signature, but 0x%08lx\n",
486 PE_HEADER(hModule)->Signature );
490 if (PE_HEADER(hModule)->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
492 fprintf(stderr,"trying to load PE image for unsupported architecture (");
493 switch (PE_HEADER(hModule)->FileHeader.Machine)
495 case IMAGE_FILE_MACHINE_UNKNOWN: fprintf(stderr,"Unknown"); break;
496 case IMAGE_FILE_MACHINE_I860: fprintf(stderr,"I860"); break;
497 case IMAGE_FILE_MACHINE_R3000: fprintf(stderr,"R3000"); break;
498 case IMAGE_FILE_MACHINE_R4000: fprintf(stderr,"R4000"); break;
499 case IMAGE_FILE_MACHINE_R10000: fprintf(stderr,"R10000"); break;
500 case IMAGE_FILE_MACHINE_ALPHA: fprintf(stderr,"Alpha"); break;
501 case IMAGE_FILE_MACHINE_POWERPC: fprintf(stderr,"PowerPC"); break;
502 default: fprintf(stderr,"Unknown-%04x",
503 PE_HEADER(hModule)->FileHeader.Machine); break;
505 fprintf(stderr,")\n");
511 UnmapViewOfFile( (LPVOID)hModule );
515 /**********************************************************************
516 * This maps a loaded PE dll into the address space of the specified process.
518 static BOOL32 PE_MapImage( HMODULE32 *phModule, PDB32 *process,
519 OFSTRUCT *ofs, DWORD flags )
524 IMAGE_DATA_DIRECTORY dir;
527 HMODULE32 hModule = *phModule;
529 IMAGE_SECTION_HEADER *pe_seg;
530 IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *)hModule;
531 IMAGE_NT_HEADERS *nt_header = PE_HEADER(hModule);
533 pem = (PE_MODREF*)HeapAlloc(process->heap,HEAP_ZERO_MEMORY,
535 /* NOTE: fixup_imports takes care of the correct order */
536 pem->next = process->modref_list;
537 process->modref_list = pem;
539 if (!(nt_header->FileHeader.Characteristics & IMAGE_FILE_DLL))
541 if (process->exe_modref)
542 fprintf(stderr,"overwriting old exe_modref... arrgh\n");
543 process->exe_modref = pem;
546 load_addr = nt_header->OptionalHeader.ImageBase;
547 vma_size = calc_vma_size( hModule );
548 dprintf_info(win32, "Load addr is %lx\n",load_addr);
549 load_addr = (DWORD)VirtualAlloc( (void*)load_addr, vma_size,
550 MEM_RESERVE | MEM_COMMIT,
551 PAGE_EXECUTE_READWRITE );
552 if (load_addr == 0) {
553 load_addr = (DWORD)VirtualAlloc( NULL, vma_size,
554 MEM_RESERVE | MEM_COMMIT,
555 PAGE_EXECUTE_READWRITE );
557 /* *phModule is the module32 entry in the NE_MODULE. We need to
558 * change it here, since it can get referenced by fixup_imports()
560 pem->module = *phModule = (HMODULE32)load_addr;
562 dprintf_info(win32, "Load addr is really %lx, range %x\n",
563 load_addr, vma_size);
565 /* Store the NT header at the load addr
566 * (FIXME: should really use mmap)
568 *(IMAGE_DOS_HEADER *)load_addr = *dos_header;
569 *(IMAGE_NT_HEADERS *)(load_addr + dos_header->e_lfanew) = *nt_header;
571 pe_seg = PE_SECTIONS(hModule);
572 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++, pe_seg++)
574 /* memcpy only non-BSS segments */
575 /* FIXME: this should be done by mmap(..MAP_PRIVATE|MAP_FIXED..)
576 * but it is not possible for (at least) Linux needs
577 * a page-aligned offset.
579 if(!(pe_seg->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA))
580 memcpy((char*)RVA(pe_seg->VirtualAddress),
581 (char*)(hModule + pe_seg->PointerToRawData),
582 pe_seg->SizeOfRawData
585 result = RVA (pe_seg->VirtualAddress);
587 /* not needed, memory is zero */
588 if(strcmp(pe_seg->Name, ".bss") == 0)
589 memset((void *)result, 0,
590 pe_seg->Misc.VirtualSize ?
591 pe_seg->Misc.VirtualSize :
592 pe_seg->SizeOfRawData);
595 if(strcmp(pe_seg->Name, ".idata") == 0)
596 pem->pe_import = (LPIMAGE_IMPORT_DESCRIPTOR) result;
598 if(strcmp(pe_seg->Name, ".edata") == 0)
599 pem->pe_export = (LPIMAGE_EXPORT_DIRECTORY) result;
601 if(strcmp(pe_seg->Name, ".rsrc") == 0)
602 pem->pe_resource = (LPIMAGE_RESOURCE_DIRECTORY) result;
604 if(strcmp(pe_seg->Name, ".reloc") == 0)
605 pem->pe_reloc = (LPIMAGE_BASE_RELOCATION) result;
608 /* There is word that the actual loader does not care about the
609 section names, and only goes for the DataDirectory */
610 dir=nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
613 if(pem->pe_export && (int)pem->pe_export!=RVA(dir.VirtualAddress))
614 fprintf(stderr,"wrong export directory??\n");
615 /* always trust the directory */
616 pem->pe_export = (LPIMAGE_EXPORT_DIRECTORY) RVA(dir.VirtualAddress);
619 dir=nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
623 if(pem->pe_import && (int)pem->pe_import!=RVA(dir.VirtualAddress))
624 fprintf(stderr,"wrong import directory??\n");
626 pem->pe_import = (LPIMAGE_IMPORT_DESCRIPTOR) RVA(dir.VirtualAddress);
629 dir=nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
632 if(pem->pe_resource && (int)pem->pe_resource!=RVA(dir.VirtualAddress))
633 fprintf(stderr,"wrong resource directory??\n");
634 pem->pe_resource = (LPIMAGE_RESOURCE_DIRECTORY) RVA(dir.VirtualAddress);
637 if(nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION].Size)
638 dprintf_fixme(win32,"Exception directory ignored\n");
640 if(nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY].Size)
641 dprintf_fixme(win32,"Security directory ignored\n");
645 dir=nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
648 if(pem->pe_reloc && (int)pem->pe_reloc!= RVA(dir.VirtualAddress))
649 fprintf(stderr,"wrong relocation list??\n");
650 pem->pe_reloc = (void *) RVA(dir.VirtualAddress);
653 if(nt_header->OptionalHeader.DataDirectory
654 [IMAGE_DIRECTORY_ENTRY_COPYRIGHT].Size)
655 dprintf_fixme(win32,"Copyright string ignored\n");
657 if(nt_header->OptionalHeader.DataDirectory
658 [IMAGE_DIRECTORY_ENTRY_GLOBALPTR].Size)
659 dprintf_fixme(win32,"Global Pointer (MIPS) ignored\n");
661 if(nt_header->OptionalHeader.DataDirectory
662 [IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].Size)
663 dprintf_fixme(win32,"Load Configuration directory ignored\n");
665 if(nt_header->OptionalHeader.DataDirectory
666 [IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT].Size)
667 dprintf_fixme(win32,"Bound Import directory ignored\n");
669 if(nt_header->OptionalHeader.DataDirectory
670 [IMAGE_DIRECTORY_ENTRY_IAT].Size)
671 dprintf_fixme(win32,"Import Address Table directory ignored\n");
672 if(nt_header->OptionalHeader.DataDirectory[13].Size)
673 dprintf_fixme(win32,"Unknown directory 13 ignored\n");
674 if(nt_header->OptionalHeader.DataDirectory[14].Size)
675 dprintf_fixme(win32,"Unknown directory 14 ignored\n");
676 if(nt_header->OptionalHeader.DataDirectory[15].Size)
677 dprintf_fixme(win32,"Unknown directory 15 ignored\n");
679 if(pem->pe_reloc) do_relocations(pem);
680 if(pem->pe_export) dump_exports(pem->module);
682 if (fixup_imports(process,pem,hModule)) {
685 /* remove entry from modref chain */
686 xpem = &(process->modref_list);
692 xpem = &((*xpem)->next);
694 /* FIXME: there are several more dangling references
695 * left. Including dlls loaded by this dll before the
696 * failed one. Unrolling is rather difficult with the
697 * current structure and we can leave it them lying
698 * around with no problems, so we don't care
705 modname = (char*)RVA(pem->pe_export->Name);
708 modname = s = ofs->szPathName;
709 while ((s=strchr(modname,'\\')))
711 if ((s=strchr(modname,'.')))
715 /* Now that we got everything at the right address,
716 * we can unmap the previous module */
717 UnmapViewOfFile( (LPVOID)hModule );
721 HINSTANCE16 MODULE_CreateInstance(HMODULE16 hModule,LOADPARAMS *params);
723 /******************************************************************************
724 * The PE Library Loader frontend.
725 * FIXME: handle the flags.
726 * internal module handling should be made better here (and in builtin.c)
728 HMODULE32 PE_LoadLibraryEx32A (LPCSTR name, PDB32 *process,
729 HFILE32 hFile, DWORD flags)
736 if ((hModule = MODULE_FindModule( name ))) {
737 /* the .DLL is either loaded or internal */
738 hModule = MODULE_HANDLEtoHMODULE32(hModule);
739 if (!HIWORD(hModule)) /* internal (or bad) */
741 /* check if this module is already mapped */
742 pem = process->modref_list;
744 if (pem->module == hModule) return hModule;
747 pModule = MODULE_GetPtr(hModule);
748 if (pModule->flags & NE_FFLAGS_BUILTIN) {
749 IMAGE_DOS_HEADER *dh;
750 IMAGE_NT_HEADERS *nh;
751 IMAGE_SECTION_HEADER *sh;
753 /* we only come here if we already have 'loaded' the
754 * internal dll but in another process. Just create
755 * a PE_MODREF and return.
757 pem = (PE_MODREF*)HeapAlloc(process->heap,
758 HEAP_ZERO_MEMORY,sizeof(*pem));
759 pem->module = hModule;
760 dh = (IMAGE_DOS_HEADER*)pem->module;
761 nh = (IMAGE_NT_HEADERS*)(dh+1);
762 sh = (IMAGE_SECTION_HEADER*)(nh+1);
763 pem->pe_export = (IMAGE_EXPORT_DIRECTORY*)(sh+2);
764 pem->next = process->modref_list;
765 process->modref_list = pem;
770 /* try to load builtin, enabled modules first */
771 if ((hModule = BUILTIN32_LoadModule( name, FALSE, process )))
772 return MODULE_HANDLEtoHMODULE32( hModule );
774 /* try to open the specified file */
775 if (HFILE_ERROR32==(hFile=OpenFile32(name,&ofs,OF_READ))) {
776 /* Now try the built-in even if disabled */
777 if ((hModule = BUILTIN32_LoadModule( name, TRUE, process ))) {
778 fprintf( stderr, "Warning: could not load Windows DLL '%s', using built-in module.\n", name );
779 return MODULE_HANDLEtoHMODULE32( hModule );
783 if ((hModule = MODULE_CreateDummyModule( &ofs )) < 32) {
787 pModule = (NE_MODULE *)GlobalLock16( hModule );
788 pModule->flags = NE_FFLAGS_WIN32;
789 pModule->module32 = PE_LoadImage( hFile );
790 CloseHandle( hFile );
791 if (pModule->module32 < 32) return 21;
794 if (!PE_MapImage( &(pModule->module32), process, &ofs,flags)) {
795 /* should free this module and the already referenced ones */
798 return pModule->module32;
801 /*****************************************************************************
802 * Load the PE main .EXE. All other loading is done by PE_LoadLibraryEx32A
803 * FIXME: this function should use PE_LoadLibraryEx32A, but currently can't
804 * due to the TASK_CreateTask stuff.
806 HINSTANCE16 PE_LoadModule( HFILE32 hFile, OFSTRUCT *ofs, LOADPARAMS* params )
810 HINSTANCE16 hInstance;
812 THDB *thdb = THREAD_Current();
814 if ((hModule16 = MODULE_CreateDummyModule( ofs )) < 32) return hModule16;
815 pModule = (NE_MODULE *)GlobalLock16( hModule16 );
816 pModule->flags = NE_FFLAGS_WIN32;
818 pModule->module32 = hModule32 = PE_LoadImage( hFile );
819 CloseHandle( hFile );
820 if (hModule32 < 32) return 21;
822 hInstance = MODULE_CreateInstance( hModule16, params );
823 if (!(PE_HEADER(hModule32)->FileHeader.Characteristics & IMAGE_FILE_DLL))
825 HTASK16 hTask = TASK_CreateTask( hModule16, hInstance, 0,
826 params->hEnvironment,
827 (LPSTR)PTR_SEG_TO_LIN( params->cmdLine ),
828 *((WORD*)PTR_SEG_TO_LIN(params->showCmd) + 1) );
829 TDB *pTask = (TDB *)GlobalLock16( hTask );
832 if (!PE_MapImage( &(pModule->module32), thdb->process, ofs, 0 ))
834 /* FIXME: should destroy the task created and free referenced stuff */
837 /* FIXME: Yuck. Is there no other good place to do that? */
842 int PE_UnloadImage( HMODULE32 hModule )
844 fprintf(stderr,"PEunloadImage() called!\n");
845 /* free resources, image, unmap */
849 /* Called if the library is loaded or freed.
850 * NOTE: if a thread attaches a DLL, the current thread will only do
851 * DLL_PROCESS_ATTACH. Only new created threads do DLL_THREAD_ATTACH
854 static void PE_InitDLL(PE_MODREF *pem, DWORD type,LPVOID lpReserved)
856 if (type==DLL_PROCESS_ATTACH)
857 pem->flags |= PE_MODREF_PROCESS_ATTACHED;
859 /* DLL_ATTACH_PROCESS:
860 * lpreserved is NULL for dynamic loads, not-NULL for static loads
861 * DLL_DETACH_PROCESS:
862 * lpreserved is NULL if called by FreeLibrary, not-NULL otherwise
863 * the SDK doesn't mention anything for DLL_THREAD_*
866 /* Is this a library? And has it got an entrypoint? */
867 if ((PE_HEADER(pem->module)->FileHeader.Characteristics & IMAGE_FILE_DLL) &&
868 (PE_HEADER(pem->module)->OptionalHeader.AddressOfEntryPoint)
870 FARPROC32 entry = (FARPROC32)RVA_PTR( pem->module,
871 OptionalHeader.AddressOfEntryPoint );
872 dprintf_info(relay, "CallTo32(entryproc=%p,module=%08x,type=%ld,res=%p)\n",
873 entry, pem->module, type, lpReserved );
874 entry( pem->module, type, lpReserved );
878 /* Call the DLLentry function of all dlls used by that process.
879 * (NOTE: this may recursively call this function (if a library calls
880 * LoadLibrary) ... but it won't matter)
882 void PE_InitializeDLLs(PDB32 *process,DWORD type,LPVOID lpReserved) {
885 pem = process->modref_list;
887 if (pem->flags & PE_MODREF_NO_DLL_CALLS) {
891 if (type==DLL_PROCESS_ATTACH) {
892 if (pem->flags & PE_MODREF_PROCESS_ATTACHED) {
897 PE_InitDLL( pem, type, lpReserved );
902 void PE_InitTls(THDB *thdb)
904 /* FIXME: tls callbacks ??? */
906 IMAGE_NT_HEADERS *peh;
909 LPIMAGE_TLS_DIRECTORY pdir;
910 PDB32 *pdb = thdb->process;
912 pem = pdb->modref_list;
914 peh = PE_HEADER(pem->module);
915 if (!peh->OptionalHeader.DataDirectory[IMAGE_FILE_THREAD_LOCAL_STORAGE].VirtualAddress) {
919 pdir = (LPVOID)(pem->module + peh->OptionalHeader.
920 DataDirectory[IMAGE_FILE_THREAD_LOCAL_STORAGE].VirtualAddress);
922 if (!(pem->flags & PE_MODREF_TLS_ALLOCED)) {
923 pem->tlsindex = THREAD_TlsAlloc(thdb);
924 *(pdir->AddressOfIndex)=pem->tlsindex;
926 pem->flags |= PE_MODREF_TLS_ALLOCED;
927 datasize= pdir->EndAddressOfRawData-pdir->StartAddressOfRawData;
928 size = datasize + pdir->SizeOfZeroFill;
929 mem=VirtualAlloc(0,size,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);
930 memcpy(mem,(LPVOID) pdir->StartAddressOfRawData, datasize);
931 /* don't use TlsSetValue, we are in the wrong thread */
932 thdb->tls_array[pem->tlsindex] = mem;
937 /****************************************************************************
938 * DisableThreadLibraryCalls (KERNEL32.74)
939 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
941 BOOL32 WINAPI DisableThreadLibraryCalls(HMODULE32 hModule)
943 PDB32 *process = PROCESS_Current();
944 PE_MODREF *pem = process->modref_list;
947 if (pem->module == hModule)
948 pem->flags|=PE_MODREF_NO_DLL_CALLS;