Fix the case of product and company names.
[wine] / loader / pe_image.c
1 /*
2  *  Copyright   1994    Eric Youndale & Erik Bos
3  *  Copyright   1995    Martin von Löwis
4  *  Copyright   1996-98 Marcus Meissner
5  *
6  *      based on Eric Youndale's pe-test and:
7  *      ftp.microsoft.com:/developr/MSDN/OctCD/PEFILE.ZIP
8  *
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.
13  *
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.
18  *
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
22  */
23 /* Notes:
24  * Before you start changing something in this file be aware of the following:
25  *
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
34  *   NE_MODULE.module32.
35  */
36
37 #include "config.h"
38
39 #include <sys/types.h>
40 #ifdef HAVE_SYS_MMAN_H
41 #include <sys/mman.h>
42 #endif
43 #include <stdarg.h>
44 #include <string.h>
45 #include "ntstatus.h"
46 #include "windef.h"
47 #include "winbase.h"
48 #include "winerror.h"
49 #include "snoop.h"
50 #include "wine/server.h"
51 #include "wine/debug.h"
52 #include "ntdll_misc.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(win32);
55 WINE_DECLARE_DEBUG_CHANNEL(module);
56 WINE_DECLARE_DEBUG_CHANNEL(relay);
57
58
59 /* convert PE image VirtualAddress to Real Address */
60 inline static void *get_rva( HMODULE module, DWORD va )
61 {
62     return (void *)((char *)module + va);
63 }
64
65 #define AdjustPtr(ptr,delta) ((char *)(ptr) + (delta))
66
67 void dump_exports( HMODULE hModule )
68 {
69   char          *Module;
70   int           i, j;
71   WORD          *ordinal;
72   DWORD         *function,*functions;
73   DWORD *name;
74   IMAGE_EXPORT_DIRECTORY *pe_exports;
75   DWORD rva_start, size;
76
77   pe_exports = RtlImageDirectoryEntryToData( hModule, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
78   rva_start = (char *)pe_exports - (char *)hModule;
79
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);
84
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);
88
89   DPRINTF(" Ord    RVA     Addr   Name\n" );
90   for (i=0;i<pe_exports->NumberOfFunctions;i++, function++)
91   {
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++)
96           if (ordinal[j] == i)
97           {
98               DPRINTF( "  %s", (char*)get_rva(hModule, name[j]) );
99               break;
100           }
101       if ((*function >= rva_start) && (*function <= rva_start + size))
102           DPRINTF(" (forwarded -> %s)", (char *)get_rva(hModule, *function));
103       DPRINTF("\n");
104   }
105 }
106
107 /**********************************************************************
108  *                      PE_LoadImage
109  * Load one PE format DLL/EXE into memory
110  *
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.
113  *
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)
116  */
117 HMODULE PE_LoadImage( HANDLE hFile, LPCSTR filename, DWORD flags )
118 {
119     IMAGE_NT_HEADERS *nt;
120     HMODULE hModule;
121     HANDLE mapping;
122     void *base = NULL;
123     OBJECT_ATTRIBUTES attr;
124     LARGE_INTEGER lg_int;
125     DWORD len = 0;
126     NTSTATUS nts;
127
128     TRACE_(module)( "loading %s\n", filename );
129
130     attr.Length                   = sizeof(attr);
131     attr.RootDirectory            = 0;
132     attr.ObjectName               = NULL;
133     attr.Attributes               = 0;
134     attr.SecurityDescriptor       = NULL;
135     attr.SecurityQualityOfService = NULL;
136     
137     lg_int.QuadPart = 0;
138
139     if (NtCreateSection( &mapping, 
140                          STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
141                          &attr, &lg_int, 0, SEC_IMAGE, hFile ) != STATUS_SUCCESS)
142         return 0;
143
144     nts = NtMapViewOfSection( mapping, GetCurrentProcess(),
145                               &base, 0, 0, &lg_int, &len, ViewShare, 0,
146                               PAGE_READONLY );
147     
148     NtClose( mapping );
149     if (nts != STATUS_SUCCESS) return 0;
150
151     /* virus check */
152
153     hModule = (HMODULE)base;
154     nt = RtlImageNtHeader( hModule );
155
156     if (nt->OptionalHeader.AddressOfEntryPoint)
157     {
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 );
162     }
163
164     return hModule;
165 }
166
167 /**********************************************************************
168  *                 PE_CreateModule
169  *
170  * Create WINE_MODREF structure for loaded HMODULE, link it into
171  * process modref_list, and fixup all imports.
172  *
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.
176  *
177  * Note: This routine must always be called in the context of the
178  *       process that is to own the module to be created.
179  *
180  * Note: Assumes that the process critical section is held
181  */
182 WINE_MODREF *PE_CreateModule( HMODULE hModule, LPCSTR filename, DWORD flags,
183                               HANDLE hFile, BOOL builtin )
184 {
185     IMAGE_NT_HEADERS *nt;
186     IMAGE_DATA_DIRECTORY *dir;
187     IMAGE_EXPORT_DIRECTORY *pe_export = NULL;
188     WINE_MODREF *wm;
189
190     /* Retrieve DataDirectory entries */
191
192     nt = RtlImageNtHeader(hModule);
193     dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
194     if (dir->Size) pe_export = get_rva(hModule, dir->VirtualAddress);
195
196     dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXCEPTION;
197     if (dir->Size) FIXME("Exception directory ignored\n" );
198
199     dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_SECURITY;
200     if (dir->Size) FIXME("Security directory ignored\n" );
201
202     /* IMAGE_DIRECTORY_ENTRY_BASERELOC handled in PE_LoadImage */
203     /* IMAGE_DIRECTORY_ENTRY_DEBUG handled by debugger */
204
205     dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_GLOBALPTR;
206     if (dir->Size) FIXME("Global Pointer (MIPS) ignored\n" );
207
208     /* IMAGE_DIRECTORY_ENTRY_TLS handled in PE_TlsInit */
209
210     dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG;
211     if (dir->Size) FIXME("Load Configuration directory ignored\n" );
212
213     dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT;
214     if (dir->Size) TRACE("Bound Import directory ignored\n" );
215
216     dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IAT;
217     if (dir->Size) TRACE("Import Address Table directory ignored\n" );
218
219     dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT;
220     if (dir->Size)
221     {
222         TRACE("Delayed import, stub calls LoadLibrary\n" );
223         /*
224          * Nothing to do here.
225          */
226
227 #ifdef ImgDelayDescr
228         /*
229          * This code is useful to observe what the heck is going on.
230          */
231         {
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);
242         }
243 #endif /* ImgDelayDescr */
244     }
245
246     dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR;
247     if (dir->Size) FIXME("Unknown directory 14 ignored\n" );
248
249     dir = nt->OptionalHeader.DataDirectory+15;
250     if (dir->Size) FIXME("Unknown directory 15 ignored\n" );
251
252     /* Allocate and fill WINE_MODREF */
253
254     if (!(wm = MODULE_AllocModRef( hModule, filename ))) return NULL;
255
256     if ( builtin )
257         wm->ldr.Flags |= LDR_WINE_INTERNAL;
258     else if ( flags & DONT_RESOLVE_DLL_REFERENCES )
259         wm->ldr.Flags |= LDR_DONT_RESOLVE_REFS;
260
261     /* Dump Exports */
262
263     if (pe_export && TRACE_ON(win32))
264         dump_exports( hModule );
265
266     /* Fixup Imports */
267
268     if (!(wm->ldr.Flags & LDR_DONT_RESOLVE_REFS) &&
269         PE_fixup_imports( wm ))
270     {
271         /* the module has only be inserted in the load & memory order lists */
272         RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
273         RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
274
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.
281          */
282          return NULL;
283     }
284
285     if (!builtin && pe_export)
286         SNOOP_RegisterDLL( hModule, wm->modname, pe_export->Base, pe_export->NumberOfFunctions );
287
288     /* Send DLL load event */
289     /* we don't need to send a dll event for the main exe */
290
291     if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
292     {
293         if (hFile)
294         {
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;
298         }
299         SERVER_START_REQ( load_dll )
300         {
301             req->handle     = hFile;
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 );
309         }
310         SERVER_END_REQ;
311     }
312
313     return wm;
314 }
315
316 /******************************************************************************
317  * The PE Library Loader frontend.
318  * FIXME: handle the flags.
319  */
320 NTSTATUS PE_LoadLibraryExA (LPCSTR name, DWORD flags, WINE_MODREF** pwm)
321 {
322         HMODULE         hModule32;
323         HANDLE          hFile;
324
325         hFile = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
326                              NULL, OPEN_EXISTING, 0, 0 );
327         if ( hFile == INVALID_HANDLE_VALUE ) 
328         {
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;
332         }
333
334         /* Load PE module */
335         hModule32 = PE_LoadImage( hFile, name, flags );
336         if (!hModule32)
337         {
338                 CloseHandle( hFile );
339                 return STATUS_INTERNAL_ERROR;
340         }
341
342         /* Create 32-bit MODREF */
343         if ( !(*pwm = PE_CreateModule( hModule32, name, flags, hFile, FALSE )) )
344         {
345                 ERR( "can't load %s\n", name );
346                 CloseHandle( hFile );
347                 return STATUS_NO_MEMORY; /* FIXME */
348         }
349
350         CloseHandle( hFile );
351         return STATUS_SUCCESS;
352 }