2 * Copyright 1994 Eric Youndale & Erik Bos
3 * Copyright 1995 Martin von Löwis
4 * Copyright 1996 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
19 #include <sys/types.h>
41 static void PE_InitDLL(PE_MODREF
* modref
, DWORD type
, LPVOID lpReserved
);
43 /* convert PE image VirtualAddress to Real Address */
44 #define RVA(x) ((unsigned int)load_addr+(unsigned int)(x))
46 void dump_exports(IMAGE_EXPORT_DIRECTORY
* pe_exports
, unsigned int load_addr
)
52 u_long
*function
,*functions
;
59 Module
= (char*)RVA(pe_exports
->Name
);
60 dprintf_win32(stddeb
,"\n*******EXPORT DATA*******\nModule name is %s, %ld functions, %ld names\n",
62 pe_exports
->NumberOfFunctions
,
63 pe_exports
->NumberOfNames
);
65 ordinal
=(u_short
*) RVA(pe_exports
->AddressOfNameOrdinals
);
66 functions
=function
=(u_long
*) RVA(pe_exports
->AddressOfFunctions
);
67 name
=(u_char
**) RVA(pe_exports
->AddressOfNames
);
69 dprintf_win32(stddeb
,"%-32s Ordinal Virt Addr\n", "Function Name");
70 for (i
=0;i
<pe_exports
->NumberOfFunctions
;i
++) {
71 if (i
<pe_exports
->NumberOfNames
) {
72 ename
=(char*)RVA(*name
++);
73 dprintf_win32(stddeb
,"%-32s %4d %8.8lx (%8.8lx)\n",ename
,*ordinal
,functions
[*ordinal
],*function
);
74 sprintf(buffer
,"%s_%s",Module
,ename
);
75 daddr
.off
=RVA(functions
[*ordinal
]);
79 /* ordinals/names no longer valid, but we still got functions */
80 dprintf_win32(stddeb
,"%-32s %4s %8s %8.8lx\n","","","",*function
);
81 sprintf(buffer
,"%s_%d",Module
,i
);
82 daddr
.off
=RVA(*functions
);
85 DEBUG_AddSymbol(buffer
,&daddr
, NULL
, SYM_WIN32
| SYM_FUNC
);
90 /* Look up the specified function or ordinal in the exportlist:
92 * - look up the name in the Name list.
93 * - look up the ordinal with that index.
94 * - use the ordinal as offset into the functionlist
96 * - use ordinal-pe_export->Base as offset into the functionlist
98 FARPROC32
PE_FindExportedFunction(struct pe_data
*pe
, LPCSTR funcName
)
100 IMAGE_EXPORT_DIRECTORY
*exports
;
104 u_char
** name
, *ename
;
106 PDB32
*process
=(PDB32
*)GetCurrentProcessId();
109 pem
= process
->modref_list
;
110 while (pem
&& (pem
->pe_module
!= pe
))
113 fprintf(stderr
,"No MODREF found for PE_MODULE %p in process %p\n",pe
,process
);
116 load_addr
= pem
->load_addr
;
117 exports
= pem
->pe_export
;
119 if (HIWORD(funcName
))
120 dprintf_win32(stddeb
,"PE_FindExportedFunction(%s)\n",funcName
);
122 dprintf_win32(stddeb
,"PE_FindExportedFunction(%d)\n",(int)funcName
);
124 fprintf(stderr
,"Module %p/MODREF %p doesn't have a exports table.\n",pe
,pem
);
127 ordinal
= (u_short
*) RVA(exports
->AddressOfNameOrdinals
);
128 function
= (u_long
*) RVA(exports
->AddressOfFunctions
);
129 name
= (u_char
**) RVA(exports
->AddressOfNames
);
131 if (HIWORD(funcName
)) {
132 for(i
=0; i
<exports
->NumberOfNames
; i
++) {
133 ename
=(char*)RVA(*name
);
134 if(!strcmp(ename
,funcName
))
135 return (FARPROC32
) RVA(function
[*ordinal
]);
140 if (LOWORD(funcName
)-exports
->Base
> exports
->NumberOfFunctions
) {
141 dprintf_win32(stddeb
," ordinal %d out of range!\n",
145 return (FARPROC32
) RVA(function
[(int)funcName
-exports
->Base
]);
151 fixup_imports (PDB32
*process
,PE_MODREF
*pem
)
153 PE_MODULE
*pe
= pem
->pe_module
;
154 IMAGE_IMPORT_DESCRIPTOR
*pe_imp
;
155 int fixup_failed
= 0;
156 unsigned int load_addr
= pem
->load_addr
;
161 modname
= (char*) RVA(pem
->pe_export
->Name
);
163 modname
= "<unknown>";
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
= pem
->pe_import
;
171 fprintf(stderr
,"no import directory????\n");
173 /* FIXME: should terminate on 0 Characteristics */
174 for (i
= 0; pe_imp
->Name
; pe_imp
++)
177 /* load the imported modules. They are automatically
178 * added to the modref list of the process.
181 /* FIXME: should terminate on 0 Characteristics */
182 for (i
= 0, pe_imp
= pem
->pe_import
; pe_imp
->Name
; pe_imp
++) {
184 PE_MODREF
*xpem
,**ypem
;
187 char *name
= (char *) RVA(pe_imp
->Name
);
189 /* don't use MODULE_Load, Win32 creates new task differently */
190 res
= PE_LoadLibraryEx32A( name
, 0, 0 );
191 if (res
<= (HMODULE32
) 32) {
192 char *p
, buffer
[256];
194 /* Try with prepending the path of the current module */
195 GetModuleFileName32A (pe
->mappeddll
, buffer
, sizeof (buffer
));
196 if (!(p
= strrchr (buffer
, '\\')))
198 strcpy (p
+ 1, name
);
199 res
= PE_LoadLibraryEx32A( buffer
, 0, 0 );
201 if (res
<= (HMODULE32
) 32) {
202 fprintf (stderr
, "Module %s not found\n", name
);
205 res
= MODULE_HANDLEtoHMODULE32(res
);
208 if (xpem
->pe_module
->mappeddll
== res
)
213 /* it has been loaded *BEFORE* us, so we have to init
214 * it before us. we just swap the two modules which should
217 /* unlink xpem from chain */
218 ypem
= &(process
->modref_list
);
222 ypem
= &((*ypem
)->next
);
226 /* link it directly before pem */
227 ypem
= &(process
->modref_list
);
231 ypem
= &((*ypem
)->next
);
239 pe_imp
= pem
->pe_import
;
240 while (pe_imp
->Name
) {
242 IMAGE_IMPORT_BY_NAME
*pe_name
;
243 LPIMAGE_THUNK_DATA import_list
,thunk_list
;
247 Module
= (char *) RVA(pe_imp
->Name
);
248 dprintf_win32 (stddeb
, "%s\n", Module
);
250 /* FIXME: forwarder entries ... */
252 if (pe_imp
->u
.OriginalFirstThunk
!= 0) { /* original MS style */
253 dprintf_win32 (stddeb
, "Microsoft style imports used\n");
254 import_list
=(LPIMAGE_THUNK_DATA
) RVA(pe_imp
->u
.OriginalFirstThunk
);
255 thunk_list
= (LPIMAGE_THUNK_DATA
) RVA(pe_imp
->FirstThunk
);
257 while (import_list
->u1
.Ordinal
) {
258 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
)) {
259 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
261 if(!lstrncmpi32A(Module
,"kernel32",8) && !ordimportwarned
){
262 fprintf(stderr
,"%s imports kernel32.dll by ordinal. May crash.\n",modname
);
265 dprintf_win32 (stddeb
, "--- Ordinal %s,%d\n", Module
, ordinal
);
266 thunk_list
->u1
.Function
=(LPDWORD
)GetProcAddress32(MODULE_FindModule(Module
),(LPCSTR
)ordinal
);
267 if (!thunk_list
->u1
.Function
) {
268 fprintf(stderr
,"No implementation for %s.%d, setting to NULL\n",
270 /* fixup_failed=1; */
272 } else { /* import by name */
273 pe_name
= (LPIMAGE_IMPORT_BY_NAME
)RVA(import_list
->u1
.AddressOfData
);
274 dprintf_win32 (stddeb
, "--- %s %s.%d\n", pe_name
->Name
, Module
, pe_name
->Hint
);
275 thunk_list
->u1
.Function
=(LPDWORD
)GetProcAddress32(
276 MODULE_FindModule (Module
),
278 if (!thunk_list
->u1
.Function
) {
279 fprintf(stderr
,"No implementation for %s.%d(%s), setting to NULL\n",
280 Module
,pe_name
->Hint
,pe_name
->Name
);
281 /* fixup_failed=1; */
287 } else { /* Borland style */
288 dprintf_win32 (stddeb
, "Borland style imports used\n");
289 thunk_list
= (LPIMAGE_THUNK_DATA
) RVA(pe_imp
->FirstThunk
);
290 while (thunk_list
->u1
.Ordinal
) {
291 if (IMAGE_SNAP_BY_ORDINAL(thunk_list
->u1
.Ordinal
)) {
292 /* not sure about this branch, but it seems to work */
293 int ordinal
= IMAGE_ORDINAL(thunk_list
->u1
.Ordinal
);
296 if (!lstrncmpi32A(Module
,"kernel32",8) &&
299 fprintf(stderr
,"%s imports kernel32.dll by ordinal. May crash.\n",modname
);
302 dprintf_win32(stddeb
,"--- Ordinal %s.%d\n",Module
,ordinal
);
303 thunk_list
->u1
.Function
=(LPDWORD
)GetProcAddress32(MODULE_FindModule(Module
),
305 if (!thunk_list
->u1
.Function
) {
306 fprintf(stderr
, "No implementation for %s.%d, setting to NULL\n",
308 /* fixup_failed=1; */
311 pe_name
=(LPIMAGE_IMPORT_BY_NAME
) RVA(thunk_list
->u1
.AddressOfData
);
312 dprintf_win32(stddeb
,"--- %s %s.%d\n",
313 pe_name
->Name
,Module
,pe_name
->Hint
);
314 thunk_list
->u1
.Function
=(LPDWORD
)GetProcAddress32(MODULE_FindModule(Module
),pe_name
->Name
);
315 if (!thunk_list
->u1
.Function
) {
316 fprintf(stderr
, "No implementation for %s.%d, setting to NULL\n",
317 Module
, pe_name
->Hint
);
318 /* fixup_failed=1; */
326 if (fixup_failed
) exit(1);
329 static int calc_vma_size(struct pe_data
*pe
)
333 dprintf_win32(stddeb
, "Dump of segment table\n");
334 dprintf_win32(stddeb
, " Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
335 for(i
=0; i
< pe
->pe_header
->FileHeader
.NumberOfSections
; i
++)
337 dprintf_win32(stddeb
, "%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
339 pe
->pe_seg
[i
].Misc
.VirtualSize
,
340 pe
->pe_seg
[i
].VirtualAddress
,
341 pe
->pe_seg
[i
].SizeOfRawData
,
342 pe
->pe_seg
[i
].PointerToRawData
,
343 pe
->pe_seg
[i
].PointerToRelocations
,
344 pe
->pe_seg
[i
].PointerToLinenumbers
,
345 pe
->pe_seg
[i
].NumberOfRelocations
,
346 pe
->pe_seg
[i
].NumberOfLinenumbers
,
347 pe
->pe_seg
[i
].Characteristics
);
348 vma_size
= MAX(vma_size
,
349 pe
->pe_seg
[i
].VirtualAddress
+
350 pe
->pe_seg
[i
].SizeOfRawData
);
355 static void do_relocations(PE_MODREF
*pem
)
357 int delta
= pem
->load_addr
- pem
->pe_module
->pe_header
->OptionalHeader
.ImageBase
;
359 unsigned int load_addr
= pem
->load_addr
;
360 IMAGE_BASE_RELOCATION
*r
= pem
->pe_reloc
;
361 int hdelta
= (delta
>> 16) & 0xFFFF;
362 int ldelta
= delta
& 0xFFFF;
364 /* int reloc_size = */
369 while(r
->VirtualAddress
)
371 char *page
= (char*) RVA(r
->VirtualAddress
);
372 int count
= (r
->SizeOfBlock
- 8)/2;
374 dprintf_fixup(stddeb
, "%x relocations for page %lx\n",
375 count
, r
->VirtualAddress
);
376 /* patching in reverse order */
379 int offset
= r
->TypeOffset
[i
] & 0xFFF;
380 int type
= r
->TypeOffset
[i
] >> 12;
381 dprintf_fixup(stddeb
,"patching %x type %x\n", offset
, type
);
384 case IMAGE_REL_BASED_ABSOLUTE
: break;
385 case IMAGE_REL_BASED_HIGH
:
386 *(short*)(page
+offset
) += hdelta
;
388 case IMAGE_REL_BASED_LOW
:
389 *(short*)(page
+offset
) += ldelta
;
391 case IMAGE_REL_BASED_HIGHLOW
:
393 *(int*)(page
+offset
) += delta
;
395 { int h
=*(unsigned short*)(page
+offset
);
396 int l
=r
->TypeOffset
[++i
];
397 *(unsigned int*)(page
+ offset
) = (h
<<16) + l
+ delta
;
401 case IMAGE_REL_BASED_HIGHADJ
:
402 fprintf(stderr
, "Don't know what to do with IMAGE_REL_BASED_HIGHADJ\n");
404 case IMAGE_REL_BASED_MIPS_JMPADDR
:
405 fprintf(stderr
, "Is this a MIPS machine ???\n");
408 fprintf(stderr
, "Unknown fixup type\n");
412 r
= (IMAGE_BASE_RELOCATION
*)((char*)r
+ r
->SizeOfBlock
);
420 /**********************************************************************
422 * Load one PE format DLL/EXE into memory
424 * Unluckily we can't just mmap the sections where we want them, for
425 * (at least) Linux does only support offset with are multiples of the
426 * underlying filesystemblocksize, but PE DLLs usually have alignments of 512
427 * byte. This fails for instance when you try to map from CDROM (bsize 2048).
429 * BUT we have to map the whole image anyway, for Win32 programs sometimes
430 * want to access them. (HMODULE32 point to the start of it)
432 static PE_MODULE
*PE_LoadImage( int fd
)
437 if (-1==fstat(fd
,&stbuf
)) {
438 perror("PE_LoadImage:fstat");
441 pe
= xmalloc(sizeof(struct pe_data
));
442 memset(pe
,0,sizeof(struct pe_data
));
444 /* map the PE image somewhere */
445 pe
->mappeddll
= (HMODULE32
)mmap(NULL
,stbuf
.st_size
,PROT_READ
,MAP_SHARED
,fd
,0);
446 if (!pe
->mappeddll
|| pe
->mappeddll
==-1) {
447 if (errno
==ENOEXEC
) {
448 int res
=0,curread
= 0;
450 lseek(fd
,0,SEEK_SET
);
451 /* linux: some filesystems don't support mmap (samba,
452 * ntfs apparently) so we have to read the image the
455 pe
->mappeddll
= xmalloc(stbuf
.st_size
);
456 while (curread
< stbuf
.st_size
) {
457 res
= read(fd
,pe
->mappeddll
+curread
,stbuf
.st_size
-curread
);
463 perror("PE_LoadImage:mmap compat read");
470 perror("PE_LoadImage:mmap");
476 pe
->pe_header
= (IMAGE_NT_HEADERS
*)(pe
->mappeddll
+(((IMAGE_DOS_HEADER
*)pe
->mappeddll
)->e_lfanew
));
477 if (pe
->pe_header
->Signature
!=IMAGE_NT_SIGNATURE
) {
478 fprintf(stderr
,"image doesn't have PE signature, but 0x%08lx\n",
479 pe
->pe_header
->Signature
485 if (pe
->pe_header
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
) {
486 fprintf(stderr
,"trying to load PE image for unsupported architecture (");
487 switch (pe
->pe_header
->FileHeader
.Machine
) {
488 case IMAGE_FILE_MACHINE_UNKNOWN
:
489 fprintf(stderr
,"Unknown");break;
490 case IMAGE_FILE_MACHINE_I860
:
491 fprintf(stderr
,"I860");break;
492 case IMAGE_FILE_MACHINE_R3000
:
493 fprintf(stderr
,"R3000");break;
494 case IMAGE_FILE_MACHINE_R4000
:
495 fprintf(stderr
,"R4000");break;
496 case IMAGE_FILE_MACHINE_R10000
:
497 fprintf(stderr
,"R10000");break;
498 case IMAGE_FILE_MACHINE_ALPHA
:
499 fprintf(stderr
,"Alpha");break;
500 case IMAGE_FILE_MACHINE_POWERPC
:
501 fprintf(stderr
,"PowerPC");break;
503 fprintf(stderr
,"Unknown-%04x",pe
->pe_header
->FileHeader
.Machine
);break;
505 fprintf(stderr
,")\n");
508 pe
->pe_seg
= (IMAGE_SECTION_HEADER
*)(((LPBYTE
)(pe
->pe_header
+1))-
509 (16 - pe
->pe_header
->OptionalHeader
.NumberOfRvaAndSizes
) * sizeof(IMAGE_DATA_DIRECTORY
));
511 /* FIXME: the (16-...) is a *horrible* hack to make COMDLG32.DLL load OK. The
512 * problem needs to be fixed properly at some stage.
517 /**********************************************************************
518 * This maps a loaded PE dll into the address space of the specified process.
521 PE_MapImage(PE_MODULE
*pe
,PDB32
*process
, OFSTRUCT
*ofs
, DWORD flags
) {
525 IMAGE_DATA_DIRECTORY dir
;
530 pem
= (PE_MODREF
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*pem
));
531 /* NOTE: fixup_imports takes care of the correct order */
532 pem
->next
= process
->modref_list
;
533 process
->modref_list
= pem
;
536 if (!(pe
->pe_header
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)) {
537 if (process
->exe_modref
)
538 fprintf(stderr
,"overwriting old exe_modref... arrgh\n");
539 process
->exe_modref
= pem
;
542 load_addr
= pe
->pe_header
->OptionalHeader
.ImageBase
;
543 dprintf_win32(stddeb
, "Load addr is %x\n",load_addr
);
544 vma_size
= calc_vma_size(pe
);
545 load_addr
= (int) VirtualAlloc( (void*)load_addr
, vma_size
, MEM_RESERVE
|MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
546 pem
->load_addr
= load_addr
;
548 dprintf_win32(stddeb
, "Load addr is really %lx, range %x\n",
549 pem
->load_addr
, vma_size
);
552 for(i
=0; i
< pe
->pe_header
->FileHeader
.NumberOfSections
; i
++)
554 /* memcpy only non-BSS segments */
555 /* FIXME: this should be done by mmap(..MAP_PRIVATE|MAP_FIXED..)
556 * but it is not possible for (at least) Linux needs an offset
557 * aligned to a block on the filesystem.
559 if(!(pe
->pe_seg
[i
].Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
))
560 memcpy((char*)RVA(pe
->pe_seg
[i
].VirtualAddress
),
561 (char*)(pe
->mappeddll
+pe
->pe_seg
[i
].PointerToRawData
),
562 pe
->pe_seg
[i
].SizeOfRawData
565 result
= RVA (pe
->pe_seg
[i
].VirtualAddress
);
567 /* not needed, memory is zero */
568 if(strcmp(pe
->pe_seg
[i
].Name
, ".bss") == 0)
569 memset((void *)result
, 0,
570 pe
->pe_seg
[i
].Misc
.VirtualSize
?
571 pe
->pe_seg
[i
].Misc
.VirtualSize
:
572 pe
->pe_seg
[i
].SizeOfRawData
);
575 if(strcmp(pe
->pe_seg
[i
].Name
, ".idata") == 0)
576 pem
->pe_import
= (LPIMAGE_IMPORT_DESCRIPTOR
) result
;
578 if(strcmp(pe
->pe_seg
[i
].Name
, ".edata") == 0)
579 pem
->pe_export
= (LPIMAGE_EXPORT_DIRECTORY
) result
;
581 if(strcmp(pe
->pe_seg
[i
].Name
, ".rsrc") == 0)
582 pem
->pe_resource
= (LPIMAGE_RESOURCE_DIRECTORY
) result
;
584 if(strcmp(pe
->pe_seg
[i
].Name
, ".reloc") == 0)
585 pem
->pe_reloc
= (LPIMAGE_BASE_RELOCATION
) result
;
588 /* There is word that the actual loader does not care about the
589 section names, and only goes for the DataDirectory */
590 dir
=pe
->pe_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
];
593 if(pem
->pe_export
&& (int)pem
->pe_export
!=RVA(dir
.VirtualAddress
))
594 fprintf(stderr
,"wrong export directory??\n");
595 /* always trust the directory */
596 pem
->pe_export
= (LPIMAGE_EXPORT_DIRECTORY
) RVA(dir
.VirtualAddress
);
599 dir
=pe
->pe_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
];
602 if(pem
->pe_import
&& (int)pem
->pe_import
!=RVA(dir
.VirtualAddress
))
603 fprintf(stderr
,"wrong import directory??\n");
604 pem
->pe_import
= (LPIMAGE_IMPORT_DESCRIPTOR
) RVA(dir
.VirtualAddress
);
607 dir
=pe
->pe_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
];
610 if(pem
->pe_resource
&& (int)pem
->pe_resource
!=RVA(dir
.VirtualAddress
))
611 fprintf(stderr
,"wrong resource directory??\n");
612 pem
->pe_resource
= (LPIMAGE_RESOURCE_DIRECTORY
) RVA(dir
.VirtualAddress
);
615 if(pe
->pe_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXCEPTION
].Size
)
616 dprintf_win32(stdnimp
,"Exception directory ignored\n");
618 if(pe
->pe_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
)
619 dprintf_win32(stdnimp
,"Security directory ignored\n");
623 dir
=pe
->pe_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
626 if(pem
->pe_reloc
&& (int)pem
->pe_reloc
!= RVA(dir
.VirtualAddress
))
627 fprintf(stderr
,"wrong relocation list??\n");
628 pem
->pe_reloc
= (void *) RVA(dir
.VirtualAddress
);
632 if(pe
->pe_header
->OptionalHeader
.DataDirectory
633 [IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
)
635 DEBUG_RegisterDebugInfo(pe
, load_addr
,
636 pe
->pe_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
,
637 pe
->pe_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
);
641 if(pe
->pe_header
->OptionalHeader
.DataDirectory
642 [IMAGE_DIRECTORY_ENTRY_COPYRIGHT
].Size
)
643 dprintf_win32(stdnimp
,"Copyright string ignored\n");
645 if(pe
->pe_header
->OptionalHeader
.DataDirectory
646 [IMAGE_DIRECTORY_ENTRY_GLOBALPTR
].Size
)
647 dprintf_win32(stdnimp
,"Global Pointer (MIPS) ignored\n");
649 if(pe
->pe_header
->OptionalHeader
.DataDirectory
650 [IMAGE_DIRECTORY_ENTRY_TLS
].Size
)
651 fprintf(stdnimp
,"Thread local storage ignored\n");
653 if(pe
->pe_header
->OptionalHeader
.DataDirectory
654 [IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
].Size
)
655 dprintf_win32(stdnimp
,"Load Configuration directory ignored\n");
657 if(pe
->pe_header
->OptionalHeader
.DataDirectory
658 [IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
].Size
)
659 dprintf_win32(stdnimp
,"Bound Import directory ignored\n");
661 if(pe
->pe_header
->OptionalHeader
.DataDirectory
662 [IMAGE_DIRECTORY_ENTRY_IAT
].Size
)
663 dprintf_win32(stdnimp
,"Import Address Table directory ignored\n");
664 if(pe
->pe_header
->OptionalHeader
.DataDirectory
[13].Size
)
665 dprintf_win32(stdnimp
,"Unknown directory 13 ignored\n");
666 if(pe
->pe_header
->OptionalHeader
.DataDirectory
[14].Size
)
667 dprintf_win32(stdnimp
,"Unknown directory 14 ignored\n");
668 if(pe
->pe_header
->OptionalHeader
.DataDirectory
[15].Size
)
669 dprintf_win32(stdnimp
,"Unknown directory 15 ignored\n");
671 if(pem
->pe_reloc
) do_relocations(pem
);
672 if(pem
->pe_export
) dump_exports(pem
->pe_export
,load_addr
);
673 if(pem
->pe_import
) fixup_imports(process
,pem
);
676 modname
= (char*)RVA(pem
->pe_export
->Name
);
679 modname
= s
= ofs
->szPathName
;
680 while ((s
=strchr(modname
,'\\')))
682 if ((s
=strchr(modname
,'.')))
688 DBG_ADDR daddr
= { NULL
, 0, 0 };
689 /* add start of sections as debugsymbols */
690 for(i
=0;i
<pe
->pe_header
->FileHeader
.NumberOfSections
;i
++) {
691 sprintf(buffer
,"%s_%s",modname
,pe
->pe_seg
[i
].Name
);
692 daddr
.off
= RVA(pe
->pe_seg
[i
].VirtualAddress
);
693 DEBUG_AddSymbol(buffer
,&daddr
, NULL
, SYM_WIN32
| SYM_FUNC
);
695 /* add entry point */
696 sprintf(buffer
,"%s_EntryPoint",modname
);
697 daddr
.off
=RVA(pe
->pe_header
->OptionalHeader
.AddressOfEntryPoint
);
698 DEBUG_AddSymbol(buffer
,&daddr
, NULL
, SYM_WIN32
| SYM_FUNC
);
699 /* add start of DLL */
701 DEBUG_AddSymbol(modname
,&daddr
, NULL
, SYM_WIN32
| SYM_FUNC
);
706 HINSTANCE16
MODULE_CreateInstance(HMODULE16 hModule
,LOADPARAMS
*params
);
708 /******************************************************************************
709 * The PE Library Loader frontend.
710 * FIXME: handle the flags.
712 HMODULE32
PE_LoadLibraryEx32A (LPCSTR name
, HFILE32 hFile
, DWORD flags
) {
718 if ((hModule
= MODULE_FindModule( name
))) {
719 /* the .DLL is either loaded or internal */
720 hModule
= MODULE_HANDLEtoHMODULE32(hModule
);
721 if (!HIWORD(hModule
)) /* internal (or bad) */
723 /* check if this module is already mapped */
724 pem
= ((PDB32
*)GetCurrentProcessId())->modref_list
;
726 if (pem
->pe_module
->mappeddll
== hModule
)
730 pModule
= MODULE_GetPtr(hModule
);
733 /* try to load builtin, enabled modules first */
734 if ((hModule
= BUILTIN_LoadModule( name
, FALSE
)))
737 /* try to open the specified file */
738 if (HFILE_ERROR32
==(hFile
=OpenFile32(name
,&ofs
,OF_READ
))) {
739 /* Now try the built-in even if disabled */
740 if ((hModule
= BUILTIN_LoadModule( name
, TRUE
))) {
741 fprintf( stderr
, "Warning: could not load Windows DLL '%s', using built-in module.\n", name
);
746 if ((hModule
= MODULE_CreateDummyModule( &ofs
)) < 32) {
750 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
751 pModule
->flags
= NE_FFLAGS_WIN32
;
752 pModule
->pe_module
= PE_LoadImage( FILE_GetUnixHandle(hFile
) );
754 if (!pModule
->pe_module
)
758 PE_MapImage(pModule
->pe_module
,(PDB32
*)GetCurrentProcessId(),&ofs
,flags
);
759 return pModule
->pe_module
->mappeddll
;
762 /*****************************************************************************
763 * Load the PE main .EXE. All other loading is done by PE_LoadLibraryEx32A
764 * FIXME: this function should use PE_LoadLibraryEx32A, but currently can't
765 * due to the TASK_CreateTask stuff.
767 HINSTANCE16
PE_LoadModule( HFILE32 hFile
, OFSTRUCT
*ofs
, LOADPARAMS
* params
)
770 HINSTANCE16 hInstance
;
773 if ((hModule
= MODULE_CreateDummyModule( ofs
)) < 32) return hModule
;
774 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
775 pModule
->flags
= NE_FFLAGS_WIN32
;
777 pModule
->pe_module
= PE_LoadImage( FILE_GetUnixHandle(hFile
) );
779 if (!pModule
->pe_module
)
782 hInstance
= MODULE_CreateInstance( hModule
, params
);
783 if (!(pModule
->pe_module
->pe_header
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
785 TASK_CreateTask( hModule
, hInstance
, 0,
786 params
->hEnvironment
,
787 (LPSTR
)PTR_SEG_TO_LIN( params
->cmdLine
),
788 *((WORD
*)PTR_SEG_TO_LIN(params
->showCmd
) + 1) );
790 PE_MapImage(pModule
->pe_module
,(PDB32
*)GetCurrentProcessId(),ofs
,0);
794 int PE_UnloadImage( HMODULE32 hModule
)
796 printf("PEunloadImage() called!\n");
797 /* free resources, image, unmap */
801 /* Called if the library is loaded or freed.
802 * NOTE: if a thread attaches a DLL, the current thread will only do
803 * DLL_PROCESS_ATTACH. Only new created threads do DLL_THREAD_ATTACH
806 static void PE_InitDLL(PE_MODREF
*pem
, DWORD type
,LPVOID lpReserved
)
808 PE_MODULE
*pe
= pem
->pe_module
;
809 unsigned int load_addr
= pem
->load_addr
;
811 if (type
==DLL_PROCESS_ATTACH
)
812 pem
->flags
|= PE_MODREF_PROCESS_ATTACHED
;
815 DBG_ADDR addr
= { NULL
, 0, RVA(pe
->pe_header
->OptionalHeader
.AddressOfEntryPoint
) };
816 DEBUG_AddBreakpoint( &addr
);
817 DEBUG_SetBreakpoints(TRUE
);
821 /* DLL_ATTACH_PROCESS:
822 * lpreserved is NULL for dynamic loads, not-NULL for static loads
823 * DLL_DETACH_PROCESS:
824 * lpreserved is NULL if called by FreeLibrary, not-NULL otherwise
825 * the SDK doesn't mention anything for DLL_THREAD_*
828 /* Is this a library? And has it got an entrypoint? */
829 if ( (pe
->pe_header
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) &&
830 (pe
->pe_header
->OptionalHeader
.AddressOfEntryPoint
)
832 FARPROC32 entry
= (FARPROC32
)RVA(pe
->pe_header
->OptionalHeader
.AddressOfEntryPoint
);
833 dprintf_relay( stddeb
, "CallTo32(entryproc=%p,module=%d,type=%ld,res=%p)\n",
834 entry
, pe
->mappeddll
, type
, lpReserved
);
835 entry( pe
->mappeddll
, type
, lpReserved
);
839 /* Call the DLLentry function of all dlls used by that process.
840 * (NOTE: this may recursively call this function (if a library calls
841 * LoadLibrary) ... but it won't matter)
843 void PE_InitializeDLLs(PDB32
*process
,DWORD type
,LPVOID lpReserved
) {
846 pem
= process
->modref_list
;
848 if (pem
->flags
& PE_MODREF_NO_DLL_CALLS
) {
852 if (type
==DLL_PROCESS_ATTACH
) {
853 if (pem
->flags
& PE_MODREF_PROCESS_ATTACHED
) {
858 PE_InitDLL( pem
, type
, lpReserved
);
863 void PE_InitTls(PDB32
*pdb
)
865 /* FIXME: tls callbacks ??? */
867 IMAGE_NT_HEADERS
*peh
;
868 DWORD size
,datasize
,index
;
870 LPIMAGE_TLS_DIRECTORY pdir
;
872 pem
= pdb
->modref_list
;
874 peh
= pem
->pe_module
->pe_header
;
875 if (!peh
->OptionalHeader
.DataDirectory
[IMAGE_FILE_THREAD_LOCAL_STORAGE
].VirtualAddress
) {
879 pdir
= (LPVOID
)(pem
->load_addr
+ peh
->OptionalHeader
.
880 DataDirectory
[IMAGE_FILE_THREAD_LOCAL_STORAGE
].VirtualAddress
);
882 datasize
= pdir
->EndAddressOfRawData
-pdir
->StartAddressOfRawData
;
883 size
= datasize
+ pdir
->SizeOfZeroFill
;
884 mem
=VirtualAlloc(0,size
,MEM_RESERVE
|MEM_COMMIT
,PAGE_READWRITE
);
885 memcpy(mem
,(LPVOID
) pdir
->StartAddressOfRawData
, datasize
);
886 TlsSetValue(index
,mem
);
887 *(pdir
->AddressOfIndex
)=index
;
892 /****************************************************************************
893 * DisableThreadLibraryCalls (KERNEL32.74)
894 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
896 BOOL32 WINAPI
DisableThreadLibraryCalls(HMODULE32 hModule
)
898 PDB32
*process
= (PDB32
*)GetCurrentProcessId();
899 PE_MODREF
*pem
= process
->modref_list
;
902 if (pem
->pe_module
->mappeddll
== hModule
)
903 pem
->flags
|=PE_MODREF_NO_DLL_CALLS
;