RDW_FRAME should be RDW_NOFRAME when validating (spotted by Duane
[wine/multimedia.git] / library / loader.c
blob60aefe4bed5b0fdbf2f7cd4119568a6468f50485
1 /*
2 * Win32 builtin dlls support
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #ifdef HAVE_SYS_MMAN_H
31 #include <sys/mman.h>
32 #endif
34 #include "winnt.h"
35 #include "wine/library.h"
37 /* argc/argv for the Windows application */
38 int __wine_main_argc = 0;
39 char **__wine_main_argv = NULL;
40 WCHAR **__wine_main_wargv = NULL;
42 #define MAX_DLLS 100
44 static struct
46 const IMAGE_NT_HEADERS *nt; /* NT header */
47 const char *filename; /* DLL file name */
48 } builtin_dlls[MAX_DLLS];
50 static int nb_dlls;
52 static const IMAGE_NT_HEADERS *main_exe;
54 static load_dll_callback_t load_dll_callback;
56 static const char **dll_paths;
57 static int nb_dll_paths;
58 static int dll_path_maxlen;
59 static int init_done;
62 /* build the dll load path from the WINEDLLPATH variable */
63 static void build_dll_path(void)
65 static const char * const dlldir = DLLDIR;
66 int len, count = 0;
67 char *p, *path = getenv( "WINEDLLPATH" );
69 init_done = 1;
71 if (path)
73 /* count how many path elements we need */
74 path = strdup(path);
75 p = path;
76 while (*p)
78 while (*p == ':') p++;
79 if (!*p) break;
80 count++;
81 while (*p && *p != ':') p++;
85 dll_paths = malloc( (count+1) * sizeof(*dll_paths) );
87 if (count)
89 p = path;
90 nb_dll_paths = 0;
91 while (*p)
93 while (*p == ':') *p++ = 0;
94 if (!*p) break;
95 dll_paths[nb_dll_paths] = p;
96 while (*p && *p != ':') p++;
97 if (p - dll_paths[nb_dll_paths] > dll_path_maxlen)
98 dll_path_maxlen = p - dll_paths[nb_dll_paths];
99 nb_dll_paths++;
103 /* append default dll dir (if not empty) to path */
104 if ((len = strlen(dlldir)))
106 if (len > dll_path_maxlen) dll_path_maxlen = len;
107 dll_paths[nb_dll_paths++] = dlldir;
111 /* check if a given file can be opened */
112 inline static int file_exists( const char *name )
114 int fd = open( name, O_RDONLY );
115 if (fd != -1) close( fd );
116 return (fd != -1);
119 /* open a library for a given dll, searching in the dll path
120 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
121 static void *dlopen_dll( const char *name, char *error, int errorsize, int test_only )
123 int i, namelen = strlen(name);
124 char *buffer, *p;
125 void *ret = NULL;
127 if (!init_done) build_dll_path();
129 buffer = malloc( dll_path_maxlen + namelen + 5 );
131 /* store the name at the end of the buffer, followed by .so */
132 p = buffer + dll_path_maxlen;
133 *p++ = '/';
134 memcpy( p, name, namelen );
135 strcpy( p + namelen, ".so" );
137 for (i = 0; i < nb_dll_paths; i++)
139 int len = strlen(dll_paths[i]);
140 p = buffer + dll_path_maxlen - len;
141 memcpy( p, dll_paths[i], len );
142 if (test_only) /* just test for file existence */
144 if ((ret = (void *)file_exists( p ))) break;
146 else
148 if ((ret = wine_dlopen( p, RTLD_NOW, error, errorsize ))) break;
149 if (file_exists( p )) break; /* exists but cannot be loaded, return the error */
152 free( buffer );
153 return ret;
157 /* adjust an array of pointers to make them into RVAs */
158 static inline void fixup_rva_ptrs( void *array, void *base, int count )
160 void **ptr = (void **)array;
161 while (count--)
163 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
164 ptr++;
169 /* fixup RVAs in the resource directory */
170 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, char *root, void *base )
172 IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
173 int i;
175 entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
176 for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
178 void *ptr = root + entry->u2.s3.OffsetToDirectory;
179 if (entry->u2.s3.DataIsDirectory) fixup_resources( ptr, root, base );
180 else
182 IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
183 fixup_rva_ptrs( &data->OffsetToData, base, 1 );
189 /* map a builtin dll in memory and fixup RVAs */
190 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
192 #ifdef HAVE_MMAP
193 IMAGE_DATA_DIRECTORY *dir;
194 IMAGE_DOS_HEADER *dos;
195 IMAGE_NT_HEADERS *nt;
196 IMAGE_SECTION_HEADER *sec;
197 BYTE *addr, *code_start, *data_start;
198 size_t page_size = getpagesize();
199 int nb_sections = 2; /* code + data */
201 size_t size = (sizeof(IMAGE_DOS_HEADER)
202 + sizeof(IMAGE_NT_HEADERS)
203 + nb_sections * sizeof(IMAGE_SECTION_HEADER));
205 assert( size <= page_size );
207 if (nt_descr->OptionalHeader.ImageBase)
209 addr = wine_anon_mmap( (void *)nt_descr->OptionalHeader.ImageBase,
210 page_size, PROT_READ|PROT_WRITE, MAP_FIXED );
211 if (addr != (BYTE *)nt_descr->OptionalHeader.ImageBase) return NULL;
213 else
215 /* this will leak memory; but it should never happen */
216 addr = wine_anon_mmap( NULL, page_size, PROT_READ|PROT_WRITE, 0 );
217 if (addr == (BYTE *)-1) return NULL;
220 dos = (IMAGE_DOS_HEADER *)addr;
221 nt = (IMAGE_NT_HEADERS *)(dos + 1);
222 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
223 code_start = addr + page_size;
225 /* HACK! */
226 data_start = code_start + page_size;
228 /* Build the DOS and NT headers */
230 dos->e_magic = IMAGE_DOS_SIGNATURE;
231 dos->e_lfanew = sizeof(*dos);
233 *nt = *nt_descr;
235 nt->FileHeader.NumberOfSections = nb_sections;
236 nt->OptionalHeader.SizeOfCode = data_start - code_start;
237 nt->OptionalHeader.SizeOfInitializedData = 0;
238 nt->OptionalHeader.SizeOfUninitializedData = 0;
239 nt->OptionalHeader.ImageBase = (DWORD)addr;
241 fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
243 /* Build the code section */
245 strcpy( sec->Name, ".text" );
246 sec->SizeOfRawData = data_start - code_start;
247 sec->Misc.VirtualSize = sec->SizeOfRawData;
248 sec->VirtualAddress = code_start - addr;
249 sec->PointerToRawData = code_start - addr;
250 sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
251 sec++;
253 /* Build the data section */
255 strcpy( sec->Name, ".data" );
256 sec->SizeOfRawData = 0;
257 sec->Misc.VirtualSize = sec->SizeOfRawData;
258 sec->VirtualAddress = data_start - addr;
259 sec->PointerToRawData = data_start - addr;
260 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
261 IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
262 sec++;
264 /* Build the import directory */
266 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
267 if (dir->Size)
269 IMAGE_IMPORT_DESCRIPTOR *imports = (void *)dir->VirtualAddress;
270 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
271 /* we can fixup everything at once since we only have pointers and 0 values */
272 fixup_rva_ptrs( imports, addr, dir->Size / sizeof(void*) );
275 /* Build the resource directory */
277 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
278 if (dir->Size)
280 void *ptr = (void *)dir->VirtualAddress;
281 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
282 fixup_resources( ptr, ptr, addr );
285 /* Build the export directory */
287 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
288 if (dir->Size)
290 IMAGE_EXPORT_DIRECTORY *exports = (void *)dir->VirtualAddress;
291 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
292 fixup_rva_ptrs( (void *)exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
293 fixup_rva_ptrs( (void *)exports->AddressOfNames, addr, exports->NumberOfNames );
294 fixup_rva_ptrs( &exports->Name, addr, 1 );
295 fixup_rva_ptrs( &exports->AddressOfFunctions, addr, 1 );
296 fixup_rva_ptrs( &exports->AddressOfNames, addr, 1 );
297 fixup_rva_ptrs( &exports->AddressOfNameOrdinals, addr, 1 );
299 return addr;
300 #else /* HAVE_MMAP */
301 return NULL;
302 #endif /* HAVE_MMAP */
306 /***********************************************************************
307 * __wine_dll_register
309 * Register a built-in DLL descriptor.
311 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
313 if (load_dll_callback) load_dll_callback( map_dll(header), filename );
314 else
316 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
317 main_exe = header;
318 else
320 assert( nb_dlls < MAX_DLLS );
321 builtin_dlls[nb_dlls].nt = header;
322 builtin_dlls[nb_dlls].filename = filename;
323 nb_dlls++;
329 /***********************************************************************
330 * wine_dll_set_callback
332 * Set the callback function for dll loading, and call it
333 * for all dlls that were implicitly loaded already.
335 void wine_dll_set_callback( load_dll_callback_t load )
337 int i;
338 load_dll_callback = load;
339 for (i = 0; i < nb_dlls; i++)
341 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
342 if (!nt) continue;
343 builtin_dlls[i].nt = NULL;
344 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
346 nb_dlls = 0;
347 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
351 /***********************************************************************
352 * wine_dll_load
354 * Load a builtin dll.
356 void *wine_dll_load( const char *filename, char *error, int errorsize )
358 int i;
360 /* callback must have been set already */
361 assert( load_dll_callback );
363 /* check if we have it in the list */
364 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
365 for (i = 0; i < nb_dlls; i++)
367 if (!builtin_dlls[i].nt) continue;
368 if (!strcmp( builtin_dlls[i].filename, filename ))
370 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
371 builtin_dlls[i].nt = NULL;
372 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
373 return (void *)1;
376 return dlopen_dll( filename, error, errorsize, 0 );
380 /***********************************************************************
381 * wine_dll_unload
383 * Unload a builtin dll.
385 void wine_dll_unload( void *handle )
387 if (handle != (void *)1)
388 wine_dlclose( handle, NULL, 0 );
392 /***********************************************************************
393 * wine_dll_load_main_exe
395 * Try to load the .so for the main exe.
397 void *wine_dll_load_main_exe( const char *name, char *error, int errorsize, int test_only )
399 return dlopen_dll( name, error, errorsize, test_only );