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:
7 * ftp.microsoft.com:/developr/MSDN/OctCD/PEFILE.ZIP
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Before you start changing something in this file be aware of the following:
26 * - There are several functions called recursively. In a very subtle and
27 * obscure way. DLLs can reference each other recursively etc.
28 * - If you want to enhance, speed up or clean up something in here, think
29 * twice WHY it is implemented in that strange way. There is usually a reason.
30 * Though sometimes it might just be lazyness ;)
31 * - In PE_MapImage, right before PE_fixup_imports() all external and internal
32 * state MUST be correct since this function can be called with the SAME image
33 * AGAIN. (Thats recursion for you.) That means MODREF.module and
39 #include <sys/types.h>
40 #ifdef HAVE_SYS_MMAN_H
44 #include "wine/winbase16.h"
47 #include "wine/server.h"
48 #include "wine/debug.h"
49 #include "ntdll_misc.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(win32
);
52 WINE_DECLARE_DEBUG_CHANNEL(module
);
53 WINE_DECLARE_DEBUG_CHANNEL(relay
);
56 /* convert PE image VirtualAddress to Real Address */
57 inline static void *get_rva( HMODULE module
, DWORD va
)
59 return (void *)((char *)module
+ va
);
62 #define AdjustPtr(ptr,delta) ((char *)(ptr) + (delta))
64 void dump_exports( HMODULE hModule
)
69 DWORD
*function
,*functions
;
71 IMAGE_EXPORT_DIRECTORY
*pe_exports
;
72 DWORD rva_start
, size
;
74 pe_exports
= RtlImageDirectoryEntryToData( hModule
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
);
75 rva_start
= (char *)pe_exports
- (char *)hModule
;
77 Module
= get_rva(hModule
, pe_exports
->Name
);
78 DPRINTF("*******EXPORT DATA*******\n");
79 DPRINTF("Module name is %s, %ld functions, %ld names\n",
80 Module
, pe_exports
->NumberOfFunctions
, pe_exports
->NumberOfNames
);
82 ordinal
= get_rva(hModule
, pe_exports
->AddressOfNameOrdinals
);
83 functions
= function
= get_rva(hModule
, pe_exports
->AddressOfFunctions
);
84 name
= get_rva(hModule
, pe_exports
->AddressOfNames
);
86 DPRINTF(" Ord RVA Addr Name\n" );
87 for (i
=0;i
<pe_exports
->NumberOfFunctions
;i
++, function
++)
89 if (!*function
) continue; /* No such function */
90 DPRINTF( "%4ld %08lx %p", i
+ pe_exports
->Base
, *function
, get_rva(hModule
, *function
) );
91 /* Check if we have a name for it */
92 for (j
= 0; j
< pe_exports
->NumberOfNames
; j
++)
95 DPRINTF( " %s", (char*)get_rva(hModule
, name
[j
]) );
98 if ((*function
>= rva_start
) && (*function
<= rva_start
+ size
))
99 DPRINTF(" (forwarded -> %s)", (char *)get_rva(hModule
, *function
));
104 /* Look up the specified function or ordinal in the export list:
106 * - look up the name in the name list.
107 * - look up the ordinal with that index.
108 * - use the ordinal as offset into the functionlist
109 * If it is an ordinal:
110 * - use ordinal-pe_export->Base as offset into the function list
112 static FARPROC
PE_FindExportedFunction(
113 WINE_MODREF
*wm
, /* [in] WINE modreference */
114 LPCSTR funcName
, /* [in] function name */
121 DWORD rva_start
, addr
;
126 IMAGE_EXPORT_DIRECTORY
*exports
;
129 if (!(exports
= RtlImageDirectoryEntryToData( wm
->module
, TRUE
,
130 IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
)))
133 if (HIWORD(funcName
)) TRACE("(%s)\n",funcName
);
134 else TRACE("(%d)\n",LOWORD(funcName
));
136 ordinals
= get_rva(wm
->module
, exports
->AddressOfNameOrdinals
);
137 function
= get_rva(wm
->module
, exports
->AddressOfFunctions
);
138 name
= get_rva(wm
->module
, exports
->AddressOfNames
);
140 rva_start
= (char *)exports
- (char *)wm
->module
;
142 if (HIWORD(funcName
))
144 int min
= 0, max
= exports
->NumberOfNames
- 1;
146 /* first check the hint */
147 if (hint
>= 0 && hint
<= max
)
149 ename
= get_rva(wm
->module
, name
[hint
]);
150 if (!strcmp( ename
, funcName
))
152 ordinal
= ordinals
[hint
];
157 /* then do a binary search */
160 int res
, pos
= (min
+ max
) / 2;
161 ename
= get_rva(wm
->module
, name
[pos
]);
162 if (!(res
= strcmp( ename
, funcName
)))
164 ordinal
= ordinals
[pos
];
167 if (res
> 0) max
= pos
- 1;
172 else /* find by ordinal */
174 ordinal
= LOWORD(funcName
) - exports
->Base
;
175 if (snoop
&& name
) /* need to find a name for it */
177 for (i
= 0; i
< exports
->NumberOfNames
; i
++)
178 if (ordinals
[i
] == ordinal
)
180 ename
= get_rva(wm
->module
, name
[i
]);
187 if (ordinal
>= exports
->NumberOfFunctions
)
189 TRACE(" ordinal %ld out of range!\n", ordinal
+ exports
->Base
);
192 addr
= function
[ordinal
];
193 if (!addr
) return NULL
;
195 proc
= get_rva(wm
->module
, addr
);
196 if (((char *)proc
< (char *)exports
) || ((char *)proc
>= (char *)exports
+ exp_size
))
200 if (!ename
) ename
= "@";
201 proc
= SNOOP_GetProcAddress(wm
->module
,ename
,ordinal
,proc
);
205 else /* forward entry point */
208 char *forward
= (char *)proc
;
210 char *end
= strchr(forward
, '.');
212 if (!end
) return NULL
;
213 if (end
- forward
>= sizeof(module
)) return NULL
;
214 memcpy( module
, forward
, end
- forward
);
215 module
[end
-forward
] = 0;
216 if (!(wm_fw
= MODULE_FindModule( module
)))
218 ERR("module not found for forward '%s' used by '%s'\n", forward
, wm
->modname
);
221 if (!(proc
= MODULE_GetProcAddress( wm_fw
->module
, end
+ 1, -1, snoop
)))
222 ERR("function not found for forward '%s' used by '%s'. If you are using builtin '%s', try using the native one instead.\n", forward
, wm
->modname
, wm
->modname
);
227 /****************************************************************
230 DWORD
PE_fixup_imports( WINE_MODREF
*wm
)
232 int i
,characteristics_detection
=1;
233 IMAGE_IMPORT_DESCRIPTOR
*imports
, *pe_imp
;
236 imports
= RtlImageDirectoryEntryToData( wm
->module
, TRUE
, IMAGE_DIRECTORY_ENTRY_IMPORT
, &size
);
238 /* first, count the number of imported non-internal modules */
240 if (!pe_imp
) return 0;
242 /* OK, now dump the import list */
243 TRACE("Dumping imports list\n");
245 /* We assume that we have at least one import with !0 characteristics and
246 * detect broken imports with all characteristics 0 (notably Borland) and
247 * switch the detection off for them.
249 for (i
= 0; pe_imp
->Name
; pe_imp
++) {
250 if (!i
&& !pe_imp
->u
.Characteristics
)
251 characteristics_detection
= 0;
252 if (characteristics_detection
&& !pe_imp
->u
.Characteristics
)
256 if (!i
) return 0; /* no imports */
258 /* Allocate module dependency list */
260 wm
->deps
= RtlAllocateHeap( ntdll_get_process_heap(), 0, i
*sizeof(WINE_MODREF
*) );
262 /* load the imported modules. They are automatically
263 * added to the modref list of the process.
266 for (i
= 0, pe_imp
= imports
; pe_imp
->Name
; pe_imp
++) {
268 IMAGE_IMPORT_BY_NAME
*pe_name
;
269 PIMAGE_THUNK_DATA import_list
,thunk_list
;
270 char *name
= get_rva(wm
->module
, pe_imp
->Name
);
273 if (characteristics_detection
&& !pe_imp
->u
.Characteristics
)
276 nts
= MODULE_LoadLibraryExA( name
, 0, &wmImp
);
281 case STATUS_NO_SUCH_FILE
:
282 ERR_(module
)("Module (file) %s (which is needed by %s) not found\n", name
, wm
->filename
);
285 ERR_(module
)("Loading module (file) %s (which is needed by %s) failed (error %ld).\n",
286 name
, wm
->filename
, GetLastError());
289 wm
->deps
[i
++] = wmImp
;
291 /* FIXME: forwarder entries ... */
293 if (pe_imp
->u
.OriginalFirstThunk
!= 0) { /* original MS style */
294 TRACE("Microsoft style imports used\n");
295 import_list
= get_rva(wm
->module
, (DWORD
)pe_imp
->u
.OriginalFirstThunk
);
296 thunk_list
= get_rva(wm
->module
, (DWORD
)pe_imp
->FirstThunk
);
298 while (import_list
->u1
.Ordinal
) {
299 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
)) {
300 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
302 TRACE("--- Ordinal %s,%d\n", name
, ordinal
);
303 thunk_list
->u1
.Function
=(PDWORD
)MODULE_GetProcAddress(
304 wmImp
->module
, (LPCSTR
)ordinal
, -1, TRUE
306 if (!thunk_list
->u1
.Function
) {
307 ERR("No implementation for %s.%d imported from %s, setting to 0xdeadbeef\n",
308 name
, ordinal
, wm
->filename
);
309 thunk_list
->u1
.Function
= (PDWORD
)0xdeadbeef;
311 } else { /* import by name */
312 pe_name
= get_rva(wm
->module
, (DWORD
)import_list
->u1
.AddressOfData
);
313 TRACE("--- %s %s.%d\n", pe_name
->Name
, name
, pe_name
->Hint
);
314 thunk_list
->u1
.Function
=(PDWORD
)MODULE_GetProcAddress(
315 wmImp
->module
, pe_name
->Name
, pe_name
->Hint
, TRUE
317 if (!thunk_list
->u1
.Function
) {
318 ERR("No implementation for %s.%d(%s) imported from %s, setting to 0xdeadbeef\n",
319 name
,pe_name
->Hint
,pe_name
->Name
,wm
->filename
);
320 thunk_list
->u1
.Function
= (PDWORD
)0xdeadbeef;
326 } else { /* Borland style */
327 TRACE("Borland style imports used\n");
328 thunk_list
= get_rva(wm
->module
, (DWORD
)pe_imp
->FirstThunk
);
329 while (thunk_list
->u1
.Ordinal
) {
330 if (IMAGE_SNAP_BY_ORDINAL(thunk_list
->u1
.Ordinal
)) {
331 /* not sure about this branch, but it seems to work */
332 int ordinal
= IMAGE_ORDINAL(thunk_list
->u1
.Ordinal
);
334 TRACE("--- Ordinal %s.%d\n",name
,ordinal
);
335 thunk_list
->u1
.Function
=(PDWORD
)MODULE_GetProcAddress(
336 wmImp
->module
, (LPCSTR
) ordinal
, -1, TRUE
338 if (!thunk_list
->u1
.Function
) {
339 ERR("No implementation for %s.%d imported from %s, setting to 0xdeadbeef\n",
340 name
,ordinal
, wm
->filename
);
341 thunk_list
->u1
.Function
= (PDWORD
)0xdeadbeef;
344 pe_name
=get_rva(wm
->module
, (DWORD
)thunk_list
->u1
.AddressOfData
);
345 TRACE("--- %s %s.%d\n",
346 pe_name
->Name
,name
,pe_name
->Hint
);
347 thunk_list
->u1
.Function
=(PDWORD
)MODULE_GetProcAddress(
348 wmImp
->module
, pe_name
->Name
, pe_name
->Hint
, TRUE
350 if (!thunk_list
->u1
.Function
) {
351 ERR("No implementation for %s.%d(%s) imported from %s, setting to 0xdeadbeef\n",
352 name
, pe_name
->Hint
, pe_name
->Name
, wm
->filename
);
353 thunk_list
->u1
.Function
= (PDWORD
)0xdeadbeef;
363 /**********************************************************************
365 * Load one PE format DLL/EXE into memory
367 * Unluckily we can't just mmap the sections where we want them, for
368 * (at least) Linux does only support offsets which are page-aligned.
370 * BUT we have to map the whole image anyway, for Win32 programs sometimes
371 * want to access them. (HMODULE points to the start of it)
373 HMODULE
PE_LoadImage( HANDLE hFile
, LPCSTR filename
, DWORD flags
)
375 IMAGE_NT_HEADERS
*nt
;
380 TRACE_(module
)( "loading %s\n", filename
);
382 mapping
= CreateFileMappingA( hFile
, NULL
, SEC_IMAGE
, 0, 0, NULL
);
383 if (!mapping
) return 0;
384 base
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
385 CloseHandle( mapping
);
390 hModule
= (HMODULE
)base
;
391 nt
= RtlImageNtHeader( hModule
);
393 if (nt
->OptionalHeader
.AddressOfEntryPoint
)
395 if (!RtlImageRvaToSection( nt
, hModule
, nt
->OptionalHeader
.AddressOfEntryPoint
))
396 MESSAGE("VIRUS WARNING: PE module has an invalid entrypoint (0x%08lx) "
397 "outside all sections (possibly infected by Tchernobyl/SpaceFiller virus)!\n",
398 nt
->OptionalHeader
.AddressOfEntryPoint
);
404 /**********************************************************************
407 * Create WINE_MODREF structure for loaded HMODULE, link it into
408 * process modref_list, and fixup all imports.
410 * Note: hModule must point to a correctly allocated PE image,
411 * with base relocations applied; the 16-bit dummy module
412 * associated to hModule must already exist.
414 * Note: This routine must always be called in the context of the
415 * process that is to own the module to be created.
417 * Note: Assumes that the process critical section is held
419 WINE_MODREF
*PE_CreateModule( HMODULE hModule
, LPCSTR filename
, DWORD flags
,
420 HANDLE hFile
, BOOL builtin
)
422 IMAGE_NT_HEADERS
*nt
;
423 IMAGE_DATA_DIRECTORY
*dir
;
424 IMAGE_EXPORT_DIRECTORY
*pe_export
= NULL
;
428 /* Retrieve DataDirectory entries */
430 nt
= RtlImageNtHeader(hModule
);
431 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_EXPORT
;
432 if (dir
->Size
) pe_export
= get_rva(hModule
, dir
->VirtualAddress
);
434 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_EXCEPTION
;
435 if (dir
->Size
) FIXME("Exception directory ignored\n" );
437 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_SECURITY
;
438 if (dir
->Size
) FIXME("Security directory ignored\n" );
440 /* IMAGE_DIRECTORY_ENTRY_BASERELOC handled in PE_LoadImage */
441 /* IMAGE_DIRECTORY_ENTRY_DEBUG handled by debugger */
443 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_GLOBALPTR
;
444 if (dir
->Size
) FIXME("Global Pointer (MIPS) ignored\n" );
446 /* IMAGE_DIRECTORY_ENTRY_TLS handled in PE_TlsInit */
448 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
;
449 if (dir
->Size
) FIXME("Load Configuration directory ignored\n" );
451 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
;
452 if (dir
->Size
) TRACE("Bound Import directory ignored\n" );
454 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_IAT
;
455 if (dir
->Size
) TRACE("Import Address Table directory ignored\n" );
457 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
;
460 TRACE("Delayed import, stub calls LoadLibrary\n" );
462 * Nothing to do here.
467 * This code is useful to observe what the heck is going on.
470 ImgDelayDescr
*pe_delay
= NULL
;
471 pe_delay
= get_rva(hModule
, dir
->VirtualAddress
);
472 TRACE("pe_delay->grAttrs = %08x\n", pe_delay
->grAttrs
);
473 TRACE("pe_delay->szName = %s\n", pe_delay
->szName
);
474 TRACE("pe_delay->phmod = %08x\n", pe_delay
->phmod
);
475 TRACE("pe_delay->pIAT = %08x\n", pe_delay
->pIAT
);
476 TRACE("pe_delay->pINT = %08x\n", pe_delay
->pINT
);
477 TRACE("pe_delay->pBoundIAT = %08x\n", pe_delay
->pBoundIAT
);
478 TRACE("pe_delay->pUnloadIAT = %08x\n", pe_delay
->pUnloadIAT
);
479 TRACE("pe_delay->dwTimeStamp = %08x\n", pe_delay
->dwTimeStamp
);
481 #endif /* ImgDelayDescr */
484 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
;
485 if (dir
->Size
) FIXME("Unknown directory 14 ignored\n" );
487 dir
= nt
->OptionalHeader
.DataDirectory
+15;
488 if (dir
->Size
) FIXME("Unknown directory 15 ignored\n" );
490 /* Create 16-bit dummy module */
492 if ((hModule16
= MODULE_CreateDummyModule( filename
, hModule
)) < 32)
494 SetLastError( (DWORD
)hModule16
); /* This should give the correct error */
498 /* Allocate and fill WINE_MODREF */
500 if (!(wm
= MODULE_AllocModRef( hModule
, filename
)))
502 FreeLibrary16( hModule16
);
505 wm
->hDummyMod
= hModule16
;
509 NE_MODULE
*pModule
= (NE_MODULE
*)GlobalLock16( hModule16
);
510 pModule
->flags
|= NE_FFLAGS_BUILTIN
;
511 wm
->flags
|= WINE_MODREF_INTERNAL
;
513 else if ( flags
& DONT_RESOLVE_DLL_REFERENCES
)
514 wm
->flags
|= WINE_MODREF_DONT_RESOLVE_REFS
;
516 wm
->find_export
= PE_FindExportedFunction
;
520 if (pe_export
&& TRACE_ON(win32
))
521 dump_exports( hModule
);
525 if (!(wm
->flags
& WINE_MODREF_DONT_RESOLVE_REFS
) &&
526 PE_fixup_imports( wm
))
528 /* remove entry from modref chain */
531 MODULE_modref_list
= wm
->next
;
533 wm
->prev
->next
= wm
->next
;
535 if ( wm
->next
) wm
->next
->prev
= wm
->prev
;
536 wm
->next
= wm
->prev
= NULL
;
538 /* FIXME: there are several more dangling references
539 * left. Including dlls loaded by this dll before the
540 * failed one. Unrolling is rather difficult with the
541 * current structure and we can leave them lying
542 * around with no problems, so we don't care.
543 * As these might reference our wm, we don't free it.
548 if (!builtin
&& pe_export
)
549 SNOOP_RegisterDLL( hModule
, wm
->modname
, pe_export
->Base
, pe_export
->NumberOfFunctions
);
551 /* Send DLL load event */
552 /* we don't need to send a dll event for the main exe */
554 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
558 UINT drive_type
= GetDriveTypeA( wm
->short_filename
);
559 /* don't keep the file handle open on removable media */
560 if (drive_type
== DRIVE_REMOVABLE
|| drive_type
== DRIVE_CDROM
) hFile
= 0;
562 SERVER_START_REQ( load_dll
)
565 req
->base
= (void *)hModule
;
566 req
->size
= nt
->OptionalHeader
.SizeOfImage
;
567 req
->dbg_offset
= nt
->FileHeader
.PointerToSymbolTable
;
568 req
->dbg_size
= nt
->FileHeader
.NumberOfSymbols
;
569 req
->name
= &wm
->filename
;
570 wine_server_add_data( req
, wm
->filename
, strlen(wm
->filename
) );
571 wine_server_call( req
);
579 /******************************************************************************
580 * The PE Library Loader frontend.
581 * FIXME: handle the flags.
583 NTSTATUS
PE_LoadLibraryExA (LPCSTR name
, DWORD flags
, WINE_MODREF
** pwm
)
588 hFile
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
589 NULL
, OPEN_EXISTING
, 0, 0 );
590 if ( hFile
== INVALID_HANDLE_VALUE
)
592 /* keep it that way until we transform CreateFile into NtCreateFile */
593 return (GetLastError() == ERROR_FILE_NOT_FOUND
) ?
594 STATUS_NO_SUCH_FILE
: STATUS_INTERNAL_ERROR
;
598 hModule32
= PE_LoadImage( hFile
, name
, flags
);
601 CloseHandle( hFile
);
602 return STATUS_INTERNAL_ERROR
;
605 /* Create 32-bit MODREF */
606 if ( !(*pwm
= PE_CreateModule( hModule32
, name
, flags
, hFile
, FALSE
)) )
608 ERR( "can't load %s\n", name
);
609 CloseHandle( hFile
);
610 return STATUS_NO_MEMORY
; /* FIXME */
613 CloseHandle( hFile
);
614 return STATUS_SUCCESS
;
618 /* Called if the library is loaded or freed.
619 * NOTE: if a thread attaches a DLL, the current thread will only do
620 * DLL_PROCESS_ATTACH. Only newly created threads do DLL_THREAD_ATTACH
623 typedef DWORD (CALLBACK
*DLLENTRYPROC
)(HMODULE
,DWORD
,LPVOID
);
625 BOOL
PE_InitDLL( HMODULE module
, DWORD type
, LPVOID lpReserved
)
628 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader(module
);
630 /* Is this a library? And has it got an entrypoint? */
631 if (nt
&& (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) &&
632 (nt
->OptionalHeader
.AddressOfEntryPoint
))
634 DLLENTRYPROC entry
= (void*)((char*)module
+ nt
->OptionalHeader
.AddressOfEntryPoint
);
636 DPRINTF("%04lx:Call PE DLL (proc=%p,module=%p,type=%ld,res=%p)\n",
637 GetCurrentThreadId(), entry
, module
, type
, lpReserved
);
638 retv
= entry( module
, type
, lpReserved
);
640 DPRINTF("%04lx:Ret PE DLL (proc=%p,module=%p,type=%ld,res=%p) retval=%x\n",
641 GetCurrentThreadId(), entry
, module
, type
, lpReserved
, retv
);
647 /************************************************************************
648 * PE_InitTls (internal)
650 * If included, initialises the thread local storages of modules.
651 * Pointers in those structs are not RVAs but real pointers which have been
652 * relocated by do_relocations() already.
655 _fixup_address(PIMAGE_OPTIONAL_HEADER opt
,int delta
,LPVOID addr
) {
656 if ( ((DWORD
)addr
>opt
->ImageBase
) &&
657 ((DWORD
)addr
<opt
->ImageBase
+opt
->SizeOfImage
)
659 /* the address has not been relocated! */
660 return (LPVOID
)(((DWORD
)addr
)+delta
);
662 /* the address has been relocated already */
665 void PE_InitTls( void )
668 IMAGE_NT_HEADERS
*peh
;
669 DWORD size
,datasize
,dirsize
;
671 PIMAGE_TLS_DIRECTORY pdir
;
674 for (wm
= MODULE_modref_list
;wm
;wm
=wm
->next
) {
675 peh
= RtlImageNtHeader(wm
->module
);
676 pdir
= RtlImageDirectoryEntryToData( wm
->module
, TRUE
,
677 IMAGE_DIRECTORY_ENTRY_TLS
, &dirsize
);
679 delta
= (char *)wm
->module
- (char *)peh
->OptionalHeader
.ImageBase
;
681 if ( wm
->tlsindex
== -1 ) {
683 wm
->tlsindex
= TlsAlloc();
684 xaddr
= _fixup_address(&(peh
->OptionalHeader
),delta
,
689 datasize
= pdir
->EndAddressOfRawData
-pdir
->StartAddressOfRawData
;
690 size
= datasize
+ pdir
->SizeOfZeroFill
;
691 mem
=VirtualAlloc(0,size
,MEM_RESERVE
|MEM_COMMIT
,PAGE_READWRITE
);
692 memcpy(mem
,_fixup_address(&(peh
->OptionalHeader
),delta
,(LPVOID
)pdir
->StartAddressOfRawData
),datasize
);
693 if (pdir
->AddressOfCallBacks
) {
694 PIMAGE_TLS_CALLBACK
*cbs
;
696 cbs
= _fixup_address(&(peh
->OptionalHeader
),delta
,pdir
->AddressOfCallBacks
);
698 FIXME("TLS Callbacks aren't going to be called\n");
701 TlsSetValue( wm
->tlsindex
, mem
);