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
50 #include "wine/server.h"
51 #include "wine/debug.h"
52 #include "ntdll_misc.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(win32
);
55 WINE_DECLARE_DEBUG_CHANNEL(module
);
56 WINE_DECLARE_DEBUG_CHANNEL(relay
);
59 /* convert PE image VirtualAddress to Real Address */
60 inline static void *get_rva( HMODULE module
, DWORD va
)
62 return (void *)((char *)module
+ va
);
65 #define AdjustPtr(ptr,delta) ((char *)(ptr) + (delta))
67 void dump_exports( HMODULE hModule
)
72 DWORD
*function
,*functions
;
74 IMAGE_EXPORT_DIRECTORY
*pe_exports
;
75 DWORD rva_start
, size
;
77 pe_exports
= RtlImageDirectoryEntryToData( hModule
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
);
78 rva_start
= (char *)pe_exports
- (char *)hModule
;
80 Module
= get_rva(hModule
, pe_exports
->Name
);
81 DPRINTF("*******EXPORT DATA*******\n");
82 DPRINTF("Module name is %s, %ld functions, %ld names\n",
83 Module
, pe_exports
->NumberOfFunctions
, pe_exports
->NumberOfNames
);
85 ordinal
= get_rva(hModule
, pe_exports
->AddressOfNameOrdinals
);
86 functions
= function
= get_rva(hModule
, pe_exports
->AddressOfFunctions
);
87 name
= get_rva(hModule
, pe_exports
->AddressOfNames
);
89 DPRINTF(" Ord RVA Addr Name\n" );
90 for (i
=0;i
<pe_exports
->NumberOfFunctions
;i
++, function
++)
92 if (!*function
) continue; /* No such function */
93 DPRINTF( "%4ld %08lx %p", i
+ pe_exports
->Base
, *function
, get_rva(hModule
, *function
) );
94 /* Check if we have a name for it */
95 for (j
= 0; j
< pe_exports
->NumberOfNames
; j
++)
98 DPRINTF( " %s", (char*)get_rva(hModule
, name
[j
]) );
101 if ((*function
>= rva_start
) && (*function
<= rva_start
+ size
))
102 DPRINTF(" (forwarded -> %s)", (char *)get_rva(hModule
, *function
));
107 /**********************************************************************
109 * Load one PE format DLL/EXE into memory
111 * Unluckily we can't just mmap the sections where we want them, for
112 * (at least) Linux does only support offsets which are page-aligned.
114 * BUT we have to map the whole image anyway, for Win32 programs sometimes
115 * want to access them. (HMODULE points to the start of it)
117 HMODULE
PE_LoadImage( HANDLE hFile
, LPCSTR filename
, DWORD flags
)
119 IMAGE_NT_HEADERS
*nt
;
123 OBJECT_ATTRIBUTES attr
;
124 LARGE_INTEGER lg_int
;
128 TRACE_(module
)( "loading %s\n", filename
);
130 attr
.Length
= sizeof(attr
);
131 attr
.RootDirectory
= 0;
132 attr
.ObjectName
= NULL
;
134 attr
.SecurityDescriptor
= NULL
;
135 attr
.SecurityQualityOfService
= NULL
;
139 if (NtCreateSection( &mapping
,
140 STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
141 &attr
, &lg_int
, 0, SEC_IMAGE
, hFile
) != STATUS_SUCCESS
)
144 nts
= NtMapViewOfSection( mapping
, GetCurrentProcess(),
145 &base
, 0, 0, &lg_int
, &len
, ViewShare
, 0,
149 if (nts
!= STATUS_SUCCESS
) return 0;
153 hModule
= (HMODULE
)base
;
154 nt
= RtlImageNtHeader( hModule
);
156 if (nt
->OptionalHeader
.AddressOfEntryPoint
)
158 if (!RtlImageRvaToSection( nt
, hModule
, nt
->OptionalHeader
.AddressOfEntryPoint
))
159 MESSAGE("VIRUS WARNING: PE module has an invalid entrypoint (0x%08lx) "
160 "outside all sections (possibly infected by Tchernobyl/SpaceFiller virus)!\n",
161 nt
->OptionalHeader
.AddressOfEntryPoint
);
167 /**********************************************************************
170 * Create WINE_MODREF structure for loaded HMODULE, link it into
171 * process modref_list, and fixup all imports.
173 * Note: hModule must point to a correctly allocated PE image,
174 * with base relocations applied; the 16-bit dummy module
175 * associated to hModule must already exist.
177 * Note: This routine must always be called in the context of the
178 * process that is to own the module to be created.
180 * Note: Assumes that the process critical section is held
182 WINE_MODREF
*PE_CreateModule( HMODULE hModule
, LPCSTR filename
, DWORD flags
,
183 HANDLE hFile
, BOOL builtin
)
185 IMAGE_NT_HEADERS
*nt
;
186 IMAGE_DATA_DIRECTORY
*dir
;
187 IMAGE_EXPORT_DIRECTORY
*pe_export
= NULL
;
190 /* Retrieve DataDirectory entries */
192 nt
= RtlImageNtHeader(hModule
);
193 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_EXPORT
;
194 if (dir
->Size
) pe_export
= get_rva(hModule
, dir
->VirtualAddress
);
196 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_EXCEPTION
;
197 if (dir
->Size
) FIXME("Exception directory ignored\n" );
199 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_SECURITY
;
200 if (dir
->Size
) FIXME("Security directory ignored\n" );
202 /* IMAGE_DIRECTORY_ENTRY_BASERELOC handled in PE_LoadImage */
203 /* IMAGE_DIRECTORY_ENTRY_DEBUG handled by debugger */
205 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_GLOBALPTR
;
206 if (dir
->Size
) FIXME("Global Pointer (MIPS) ignored\n" );
208 /* IMAGE_DIRECTORY_ENTRY_TLS handled in PE_TlsInit */
210 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
;
211 if (dir
->Size
) FIXME("Load Configuration directory ignored\n" );
213 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
;
214 if (dir
->Size
) TRACE("Bound Import directory ignored\n" );
216 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_IAT
;
217 if (dir
->Size
) TRACE("Import Address Table directory ignored\n" );
219 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
;
222 TRACE("Delayed import, stub calls LoadLibrary\n" );
224 * Nothing to do here.
229 * This code is useful to observe what the heck is going on.
232 ImgDelayDescr
*pe_delay
= NULL
;
233 pe_delay
= get_rva(hModule
, dir
->VirtualAddress
);
234 TRACE("pe_delay->grAttrs = %08x\n", pe_delay
->grAttrs
);
235 TRACE("pe_delay->szName = %s\n", pe_delay
->szName
);
236 TRACE("pe_delay->phmod = %08x\n", pe_delay
->phmod
);
237 TRACE("pe_delay->pIAT = %08x\n", pe_delay
->pIAT
);
238 TRACE("pe_delay->pINT = %08x\n", pe_delay
->pINT
);
239 TRACE("pe_delay->pBoundIAT = %08x\n", pe_delay
->pBoundIAT
);
240 TRACE("pe_delay->pUnloadIAT = %08x\n", pe_delay
->pUnloadIAT
);
241 TRACE("pe_delay->dwTimeStamp = %08x\n", pe_delay
->dwTimeStamp
);
243 #endif /* ImgDelayDescr */
246 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
;
247 if (dir
->Size
) FIXME("Unknown directory 14 ignored\n" );
249 dir
= nt
->OptionalHeader
.DataDirectory
+15;
250 if (dir
->Size
) FIXME("Unknown directory 15 ignored\n" );
252 /* Allocate and fill WINE_MODREF */
254 if (!(wm
= MODULE_AllocModRef( hModule
, filename
))) return NULL
;
257 wm
->ldr
.Flags
|= LDR_WINE_INTERNAL
;
258 else if ( flags
& DONT_RESOLVE_DLL_REFERENCES
)
259 wm
->ldr
.Flags
|= LDR_DONT_RESOLVE_REFS
;
263 if (pe_export
&& TRACE_ON(win32
))
264 dump_exports( hModule
);
268 if (!(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
) &&
269 PE_fixup_imports( wm
))
271 /* the module has only be inserted in the load & memory order lists */
272 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
273 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
275 /* FIXME: there are several more dangling references
276 * left. Including dlls loaded by this dll before the
277 * failed one. Unrolling is rather difficult with the
278 * current structure and we can leave them lying
279 * around with no problems, so we don't care.
280 * As these might reference our wm, we don't free it.
285 if (!builtin
&& pe_export
)
286 SNOOP_RegisterDLL( hModule
, wm
->modname
, pe_export
->Base
, pe_export
->NumberOfFunctions
);
288 /* Send DLL load event */
289 /* we don't need to send a dll event for the main exe */
291 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
295 UINT drive_type
= GetDriveTypeA( wm
->short_filename
);
296 /* don't keep the file handle open on removable media */
297 if (drive_type
== DRIVE_REMOVABLE
|| drive_type
== DRIVE_CDROM
) hFile
= 0;
299 SERVER_START_REQ( load_dll
)
302 req
->base
= (void *)hModule
;
303 req
->size
= nt
->OptionalHeader
.SizeOfImage
;
304 req
->dbg_offset
= nt
->FileHeader
.PointerToSymbolTable
;
305 req
->dbg_size
= nt
->FileHeader
.NumberOfSymbols
;
306 req
->name
= &wm
->filename
;
307 wine_server_add_data( req
, wm
->filename
, strlen(wm
->filename
) );
308 wine_server_call( req
);
316 /******************************************************************************
317 * The PE Library Loader frontend.
318 * FIXME: handle the flags.
320 NTSTATUS
PE_LoadLibraryExA (LPCSTR name
, DWORD flags
, WINE_MODREF
** pwm
)
325 hFile
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
326 NULL
, OPEN_EXISTING
, 0, 0 );
327 if ( hFile
== INVALID_HANDLE_VALUE
)
329 /* keep it that way until we transform CreateFile into NtCreateFile */
330 return (GetLastError() == ERROR_FILE_NOT_FOUND
) ?
331 STATUS_NO_SUCH_FILE
: STATUS_INTERNAL_ERROR
;
335 hModule32
= PE_LoadImage( hFile
, name
, flags
);
338 CloseHandle( hFile
);
339 return STATUS_INTERNAL_ERROR
;
342 /* Create 32-bit MODREF */
343 if ( !(*pwm
= PE_CreateModule( hModule32
, name
, flags
, hFile
, FALSE
)) )
345 ERR( "can't load %s\n", name
);
346 CloseHandle( hFile
);
347 return STATUS_NO_MEMORY
; /* FIXME */
350 CloseHandle( hFile
);
351 return STATUS_SUCCESS
;