Remove references to PCVOID and LPUINT.
[wine] / library / loader.c
1 /*
2  * Win32 builtin dlls support
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
32
33 #include "winnt.h"
34 #include "wine/library.h"
35
36 /* argc/argv for the Windows application */
37 int __wine_main_argc = 0;
38 char **__wine_main_argv = NULL;
39 WCHAR **__wine_main_wargv = NULL;
40
41 #define MAX_DLLS 100
42
43 static struct
44 {
45     const IMAGE_NT_HEADERS *nt;           /* NT header */
46     const char             *filename;     /* DLL file name */
47 } builtin_dlls[MAX_DLLS];
48
49 static int nb_dlls;
50
51 static const IMAGE_NT_HEADERS *main_exe;
52
53 static load_dll_callback_t load_dll_callback;
54
55 static const char **dll_paths;
56 static int nb_dll_paths;
57 static int dll_path_maxlen;
58 static int init_done;
59
60
61 /* build the dll load path from the WINEDLLPATH variable */
62 static void build_dll_path(void)
63 {
64     static const char * const dlldir = DLLDIR;
65     int len, count = 0;
66     char *p, *path = getenv( "WINEDLLPATH" );
67
68     init_done = 1;
69
70     if (path)
71     {
72         /* count how many path elements we need */
73         path = strdup(path);
74         p = path;
75         while (*p)
76         {
77             while (*p == ':') p++;
78             if (!*p) break;
79             count++;
80             while (*p && *p != ':') p++;
81         }
82     }
83
84     dll_paths = malloc( (count+1) * sizeof(*dll_paths) );
85
86     if (count)
87     {
88         p = path;
89         nb_dll_paths = 0;
90         while (*p)
91         {
92             while (*p == ':') *p++ = 0;
93             if (!*p) break;
94             dll_paths[nb_dll_paths] = p;
95             while (*p && *p != ':') p++;
96             if (p - dll_paths[nb_dll_paths] > dll_path_maxlen)
97                 dll_path_maxlen = p - dll_paths[nb_dll_paths];
98             nb_dll_paths++;
99         }
100     }
101
102     /* append default dll dir (if not empty) to path */
103     if ((len = strlen(dlldir)))
104     {
105         if (len > dll_path_maxlen) dll_path_maxlen = len;
106         dll_paths[nb_dll_paths++] = dlldir;
107     }
108 }
109
110 /* check if a given file can be opened */
111 inline static int file_exists( const char *name )
112 {
113     int fd = open( name, O_RDONLY );
114     if (fd != -1) close( fd );
115     return (fd != -1);
116 }
117
118 /* open a library for a given dll, searching in the dll path
119  * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
120 static void *dlopen_dll( const char *name, char *error, int errorsize, int test_only )
121 {
122     int i, namelen = strlen(name);
123     char *buffer, *p;
124     void *ret = NULL;
125
126     if (!init_done) build_dll_path();
127
128     buffer = malloc( dll_path_maxlen + namelen + 5 );
129
130     /* store the name at the end of the buffer, followed by .so */
131     p = buffer + dll_path_maxlen;
132     *p++ = '/';
133     memcpy( p, name, namelen );
134     strcpy( p + namelen, ".so" );
135
136     for (i = 0; i < nb_dll_paths; i++)
137     {
138         int len = strlen(dll_paths[i]);
139         p = buffer + dll_path_maxlen - len;
140         memcpy( p, dll_paths[i], len );
141         if (test_only)  /* just test for file existence */
142         {
143             if ((ret = (void *)file_exists( p ))) break;
144         }
145         else
146         {
147             if ((ret = wine_dlopen( p, RTLD_NOW, error, errorsize ))) break;
148             if (file_exists( p )) break; /* exists but cannot be loaded, return the error */
149         }
150     }
151     free( buffer );
152     return ret;
153 }
154
155
156 /* adjust an array of pointers to make them into RVAs */
157 static inline void fixup_rva_ptrs( void *array, void *base, int count )
158 {
159     void **ptr = (void **)array;
160     while (count--)
161     {
162         if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
163         ptr++;
164     }
165 }
166
167
168 /* fixup RVAs in the import directory */
169 static void fixup_imports( IMAGE_IMPORT_DESCRIPTOR *dir, DWORD size, void *base )
170 {
171     int count = size / sizeof(void *);
172     void **ptr = (void **)dir;
173
174     /* everything is either a pointer or a ordinal value below 0x10000 */
175     while (count--)
176     {
177         if (*ptr >= (void *)0x10000) *ptr = (void *)((char *)*ptr - (char *)base);
178         else if (*ptr) *ptr = (void *)(0x80000000 | (unsigned int)*ptr);
179         ptr++;
180     }
181 }
182
183
184 /* fixup RVAs in the resource directory */
185 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, char *root, void *base )
186 {
187     IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
188     int i;
189
190     entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
191     for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
192     {
193         void *ptr = root + entry->u2.s3.OffsetToDirectory;
194         if (entry->u2.s3.DataIsDirectory) fixup_resources( ptr, root, base );
195         else
196         {
197             IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
198             fixup_rva_ptrs( &data->OffsetToData, base, 1 );
199         }
200     }
201 }
202
203
204 /* map a builtin dll in memory and fixup RVAs */
205 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
206 {
207 #ifdef HAVE_MMAP
208     IMAGE_DATA_DIRECTORY *dir;
209     IMAGE_DOS_HEADER *dos;
210     IMAGE_NT_HEADERS *nt;
211     IMAGE_SECTION_HEADER *sec;
212     BYTE *addr, *code_start, *data_start;
213     size_t page_size = getpagesize();
214     int nb_sections = 2;  /* code + data */
215
216     size_t size = (sizeof(IMAGE_DOS_HEADER)
217                    + sizeof(IMAGE_NT_HEADERS)
218                    + nb_sections * sizeof(IMAGE_SECTION_HEADER));
219
220     assert( size <= page_size );
221
222     /* module address must be aligned on 64K boundary */
223     addr = (BYTE *)((nt_descr->OptionalHeader.ImageBase + 0xffff) & ~0xffff);
224     if (wine_anon_mmap( addr, page_size, PROT_READ|PROT_WRITE, MAP_FIXED ) != addr) return NULL;
225
226     dos    = (IMAGE_DOS_HEADER *)addr;
227     nt     = (IMAGE_NT_HEADERS *)(dos + 1);
228     sec    = (IMAGE_SECTION_HEADER *)(nt + 1);
229     code_start = addr + page_size;
230
231     /* HACK! */
232     data_start = code_start + page_size;
233
234     /* Build the DOS and NT headers */
235
236     dos->e_magic  = IMAGE_DOS_SIGNATURE;
237     dos->e_lfanew = sizeof(*dos);
238
239     *nt = *nt_descr;
240
241     nt->FileHeader.NumberOfSections                = nb_sections;
242     nt->OptionalHeader.SizeOfCode                  = data_start - code_start;
243     nt->OptionalHeader.SizeOfInitializedData       = 0;
244     nt->OptionalHeader.SizeOfUninitializedData     = 0;
245     nt->OptionalHeader.ImageBase                   = (DWORD)addr;
246
247     fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
248
249     /* Build the code section */
250
251     strcpy( sec->Name, ".text" );
252     sec->SizeOfRawData = data_start - code_start;
253     sec->Misc.VirtualSize = sec->SizeOfRawData;
254     sec->VirtualAddress   = code_start - addr;
255     sec->PointerToRawData = code_start - addr;
256     sec->Characteristics  = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
257     sec++;
258
259     /* Build the data section */
260
261     strcpy( sec->Name, ".data" );
262     sec->SizeOfRawData = 0;
263     sec->Misc.VirtualSize = sec->SizeOfRawData;
264     sec->VirtualAddress   = data_start - addr;
265     sec->PointerToRawData = data_start - addr;
266     sec->Characteristics  = (IMAGE_SCN_CNT_INITIALIZED_DATA |
267                              IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
268     sec++;
269
270     /* Build the import directory */
271
272     dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
273     if (dir->Size)
274     {
275         IMAGE_IMPORT_DESCRIPTOR *imports = (void *)dir->VirtualAddress;
276         fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
277         fixup_imports( imports, dir->Size, addr );
278     }
279
280     /* Build the resource directory */
281
282     dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
283     if (dir->Size)
284     {
285         void *ptr = (void *)dir->VirtualAddress;
286         fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
287         fixup_resources( ptr, ptr, addr );
288     }
289
290     /* Build the export directory */
291
292     dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
293     if (dir->Size)
294     {
295         IMAGE_EXPORT_DIRECTORY *exports = (void *)dir->VirtualAddress;
296         fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
297         fixup_rva_ptrs( (void *)exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
298         fixup_rva_ptrs( (void *)exports->AddressOfNames, addr, exports->NumberOfNames );
299         fixup_rva_ptrs( &exports->Name, addr, 1 );
300         fixup_rva_ptrs( &exports->AddressOfFunctions, addr, 1 );
301         fixup_rva_ptrs( &exports->AddressOfNames, addr, 1 );
302         fixup_rva_ptrs( &exports->AddressOfNameOrdinals, addr, 1 );
303     }
304     return addr;
305 #else  /* HAVE_MMAP */
306     return NULL;
307 #endif  /* HAVE_MMAP */
308 }
309
310
311 /***********************************************************************
312  *           __wine_dll_register
313  *
314  * Register a built-in DLL descriptor.
315  */
316 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
317 {
318     if (load_dll_callback) load_dll_callback( map_dll(header), filename );
319     else
320     {
321         if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
322             main_exe = header;
323         else
324         {
325             assert( nb_dlls < MAX_DLLS );
326             builtin_dlls[nb_dlls].nt = header;
327             builtin_dlls[nb_dlls].filename = filename;
328             nb_dlls++;
329         }
330     }
331 }
332
333
334 /***********************************************************************
335  *           wine_dll_set_callback
336  *
337  * Set the callback function for dll loading, and call it
338  * for all dlls that were implicitly loaded already.
339  */
340 void wine_dll_set_callback( load_dll_callback_t load )
341 {
342     int i;
343     load_dll_callback = load;
344     for (i = 0; i < nb_dlls; i++)
345     {
346         const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
347         if (!nt) continue;
348         builtin_dlls[i].nt = NULL;
349         load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
350     }
351     nb_dlls = 0;
352     if (main_exe) load_dll_callback( map_dll(main_exe), "" );
353 }
354
355
356 /***********************************************************************
357  *           wine_dll_load
358  *
359  * Load a builtin dll.
360  */
361 void *wine_dll_load( const char *filename, char *error, int errorsize )
362 {
363     int i;
364
365     /* callback must have been set already */
366     assert( load_dll_callback );
367
368     /* check if we have it in the list */
369     /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
370     for (i = 0; i < nb_dlls; i++)
371     {
372         if (!builtin_dlls[i].nt) continue;
373         if (!strcmp( builtin_dlls[i].filename, filename ))
374         {
375             const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
376             builtin_dlls[i].nt = NULL;
377             load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
378             return (void *)1;
379         }
380     }
381     return dlopen_dll( filename, error, errorsize, 0 );
382 }
383
384
385 /***********************************************************************
386  *           wine_dll_unload
387  *
388  * Unload a builtin dll.
389  */
390 void wine_dll_unload( void *handle )
391 {
392     if (handle != (void *)1)
393         wine_dlclose( handle, NULL, 0 );
394 }
395
396
397 /***********************************************************************
398  *           wine_dll_load_main_exe
399  *
400  * Try to load the .so for the main exe.
401  */
402 void *wine_dll_load_main_exe( const char *name, char *error, int errorsize, int test_only )
403 {
404     return dlopen_dll( name, error, errorsize, test_only );
405 }