Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / libs / wine / loader.c
blob32e0d9eafe370f618c6b312a5e137ba03eb2d247
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 <fcntl.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_MMAN_H
32 #include <sys/mman.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
38 #define NONAMELESSUNION
39 #define NONAMELESSSTRUCT
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wine/library.h"
44 /* argc/argv for the Windows application */
45 int __wine_main_argc = 0;
46 char **__wine_main_argv = NULL;
47 WCHAR **__wine_main_wargv = NULL;
49 #define MAX_DLLS 100
51 static struct
53 const IMAGE_NT_HEADERS *nt; /* NT header */
54 const char *filename; /* DLL file name */
55 } builtin_dlls[MAX_DLLS];
57 static int nb_dlls;
59 static const IMAGE_NT_HEADERS *main_exe;
61 static load_dll_callback_t load_dll_callback;
63 static const char **dll_paths;
64 static int nb_dll_paths;
65 static int dll_path_maxlen;
66 static int init_done;
69 /* build the dll load path from the WINEDLLPATH variable */
70 static void build_dll_path(void)
72 static const char * const dlldir = DLLDIR;
73 int len, count = 0;
74 char *p, *path = getenv( "WINEDLLPATH" );
76 init_done = 1;
78 if (path)
80 /* count how many path elements we need */
81 path = strdup(path);
82 p = path;
83 while (*p)
85 while (*p == ':') p++;
86 if (!*p) break;
87 count++;
88 while (*p && *p != ':') p++;
92 dll_paths = malloc( (count+1) * sizeof(*dll_paths) );
94 if (count)
96 p = path;
97 nb_dll_paths = 0;
98 while (*p)
100 while (*p == ':') *p++ = 0;
101 if (!*p) break;
102 dll_paths[nb_dll_paths] = p;
103 while (*p && *p != ':') p++;
104 if (p - dll_paths[nb_dll_paths] > dll_path_maxlen)
105 dll_path_maxlen = p - dll_paths[nb_dll_paths];
106 nb_dll_paths++;
110 /* append default dll dir (if not empty) to path */
111 if ((len = strlen(dlldir)))
113 if (len > dll_path_maxlen) dll_path_maxlen = len;
114 dll_paths[nb_dll_paths++] = dlldir;
118 /* check if a given file can be opened */
119 inline static int file_exists( const char *name )
121 int fd = open( name, O_RDONLY );
122 if (fd != -1) close( fd );
123 return (fd != -1);
126 /* open a library for a given dll, searching in the dll path
127 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
128 static void *dlopen_dll( const char *name, char *error, int errorsize,
129 int test_only, int *exists )
131 int i, namelen = strlen(name);
132 char *buffer, *p;
133 void *ret = NULL;
135 if (!init_done) build_dll_path();
137 buffer = malloc( dll_path_maxlen + namelen + 5 );
139 /* store the name at the end of the buffer, followed by .so */
140 p = buffer + dll_path_maxlen;
141 *p++ = '/';
142 memcpy( p, name, namelen );
143 strcpy( p + namelen, ".so" );
144 *exists = 0;
146 for (i = 0; i < nb_dll_paths; i++)
148 int len = strlen(dll_paths[i]);
149 p = buffer + dll_path_maxlen - len;
150 memcpy( p, dll_paths[i], len );
151 if (!test_only && (ret = wine_dlopen( p, RTLD_NOW, error, errorsize ))) break;
152 if ((*exists = file_exists( p ))) break; /* exists but cannot be loaded, return the error */
154 free( buffer );
155 return ret;
159 /* adjust an array of pointers to make them into RVAs */
160 static inline void fixup_rva_ptrs( void *array, void *base, int count )
162 void **ptr = (void **)array;
163 while (count--)
165 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
166 ptr++;
171 /* fixup RVAs in the import directory */
172 static void fixup_imports( IMAGE_IMPORT_DESCRIPTOR *dir, DWORD size, void *base )
174 int count = size / sizeof(void *);
175 void **ptr = (void **)dir;
177 /* everything is either a pointer or a ordinal value below 0x10000 */
178 while (count--)
180 if (*ptr >= (void *)0x10000) *ptr = (void *)((char *)*ptr - (char *)base);
181 else if (*ptr) *ptr = (void *)(0x80000000 | (unsigned int)*ptr);
182 ptr++;
187 /* fixup RVAs in the resource directory */
188 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, char *root, void *base )
190 IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
191 int i;
193 entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
194 for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
196 void *ptr = root + entry->u2.s3.OffsetToDirectory;
197 if (entry->u2.s3.DataIsDirectory) fixup_resources( ptr, root, base );
198 else
200 IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
201 fixup_rva_ptrs( &data->OffsetToData, base, 1 );
207 /* map a builtin dll in memory and fixup RVAs */
208 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
210 #ifdef HAVE_MMAP
211 IMAGE_DATA_DIRECTORY *dir;
212 IMAGE_DOS_HEADER *dos;
213 IMAGE_NT_HEADERS *nt;
214 IMAGE_SECTION_HEADER *sec;
215 BYTE *addr, *code_start, *data_start;
216 size_t page_size = getpagesize();
217 int nb_sections = 2; /* code + data */
219 size_t size = (sizeof(IMAGE_DOS_HEADER)
220 + sizeof(IMAGE_NT_HEADERS)
221 + nb_sections * sizeof(IMAGE_SECTION_HEADER));
223 assert( size <= page_size );
225 /* module address must be aligned on 64K boundary */
226 addr = (BYTE *)((nt_descr->OptionalHeader.ImageBase + 0xffff) & ~0xffff);
227 if (wine_anon_mmap( addr, page_size, PROT_READ|PROT_WRITE, MAP_FIXED ) != addr) return NULL;
229 dos = (IMAGE_DOS_HEADER *)addr;
230 nt = (IMAGE_NT_HEADERS *)(dos + 1);
231 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
232 code_start = addr + page_size;
234 /* HACK! */
235 data_start = code_start + page_size;
237 /* Build the DOS and NT headers */
239 dos->e_magic = IMAGE_DOS_SIGNATURE;
240 dos->e_lfanew = sizeof(*dos);
242 *nt = *nt_descr;
244 nt->FileHeader.NumberOfSections = nb_sections;
245 nt->OptionalHeader.SizeOfCode = data_start - code_start;
246 nt->OptionalHeader.SizeOfInitializedData = 0;
247 nt->OptionalHeader.SizeOfUninitializedData = 0;
248 nt->OptionalHeader.ImageBase = (DWORD)addr;
250 fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
252 /* Build the code section */
254 strcpy( sec->Name, ".text" );
255 sec->SizeOfRawData = data_start - code_start;
256 sec->Misc.VirtualSize = sec->SizeOfRawData;
257 sec->VirtualAddress = code_start - addr;
258 sec->PointerToRawData = code_start - addr;
259 sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
260 sec++;
262 /* Build the data section */
264 strcpy( sec->Name, ".data" );
265 sec->SizeOfRawData = 0;
266 sec->Misc.VirtualSize = sec->SizeOfRawData;
267 sec->VirtualAddress = data_start - addr;
268 sec->PointerToRawData = data_start - addr;
269 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
270 IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
271 sec++;
273 /* Build the import directory */
275 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
276 if (dir->Size)
278 IMAGE_IMPORT_DESCRIPTOR *imports = (void *)dir->VirtualAddress;
279 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
280 fixup_imports( imports, dir->Size, addr );
283 /* Build the resource directory */
285 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
286 if (dir->Size)
288 void *ptr = (void *)dir->VirtualAddress;
289 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
290 fixup_resources( ptr, ptr, addr );
293 /* Build the export directory */
295 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
296 if (dir->Size)
298 IMAGE_EXPORT_DIRECTORY *exports = (void *)dir->VirtualAddress;
299 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
300 fixup_rva_ptrs( (void *)exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
301 fixup_rva_ptrs( (void *)exports->AddressOfNames, addr, exports->NumberOfNames );
302 fixup_rva_ptrs( &exports->Name, addr, 1 );
303 fixup_rva_ptrs( &exports->AddressOfFunctions, addr, 1 );
304 fixup_rva_ptrs( &exports->AddressOfNames, addr, 1 );
305 fixup_rva_ptrs( &exports->AddressOfNameOrdinals, addr, 1 );
307 return addr;
308 #else /* HAVE_MMAP */
309 return NULL;
310 #endif /* HAVE_MMAP */
314 /***********************************************************************
315 * __wine_dll_register
317 * Register a built-in DLL descriptor.
319 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
321 if (load_dll_callback) load_dll_callback( map_dll(header), filename );
322 else
324 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
325 main_exe = header;
326 else
328 assert( nb_dlls < MAX_DLLS );
329 builtin_dlls[nb_dlls].nt = header;
330 builtin_dlls[nb_dlls].filename = filename;
331 nb_dlls++;
337 /***********************************************************************
338 * wine_dll_set_callback
340 * Set the callback function for dll loading, and call it
341 * for all dlls that were implicitly loaded already.
343 void wine_dll_set_callback( load_dll_callback_t load )
345 int i;
346 load_dll_callback = load;
347 for (i = 0; i < nb_dlls; i++)
349 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
350 if (!nt) continue;
351 builtin_dlls[i].nt = NULL;
352 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
354 nb_dlls = 0;
355 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
359 /***********************************************************************
360 * wine_dll_load
362 * Load a builtin dll.
364 void *wine_dll_load( const char *filename, char *error, int errorsize, int *file_exists )
366 int i;
368 /* callback must have been set already */
369 assert( load_dll_callback );
371 /* check if we have it in the list */
372 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
373 for (i = 0; i < nb_dlls; i++)
375 if (!builtin_dlls[i].nt) continue;
376 if (!strcmp( builtin_dlls[i].filename, filename ))
378 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
379 builtin_dlls[i].nt = NULL;
380 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
381 *file_exists = 1;
382 return (void *)1;
385 return dlopen_dll( filename, error, errorsize, 0, file_exists );
389 /***********************************************************************
390 * wine_dll_unload
392 * Unload a builtin dll.
394 void wine_dll_unload( void *handle )
396 if (handle != (void *)1)
397 wine_dlclose( handle, NULL, 0 );
401 /***********************************************************************
402 * wine_dll_load_main_exe
404 * Try to load the .so for the main exe.
406 void *wine_dll_load_main_exe( const char *name, char *error, int errorsize,
407 int test_only, int *file_exists )
409 return dlopen_dll( name, error, errorsize, test_only, file_exists );
413 /***********************************************************************
414 * wine_init
416 * Main Wine initialisation.
418 void wine_init( int argc, char *argv[], char *error, int error_size )
420 int file_exists;
421 void *ntdll;
422 void (*init_func)(int, char **);
424 if (!(ntdll = dlopen_dll( "ntdll.dll", error, error_size, 0, &file_exists ))) return;
425 /* make sure kernel32 is loaded too */
426 if (!dlopen_dll( "kernel32.dll", error, error_size, 0, &file_exists )) return;
427 if (!(init_func = wine_dlsym( ntdll, "__wine_process_init", error, error_size ))) return;
428 init_func( argc, argv );
433 * These functions provide wrappers around dlopen() and associated
434 * functions. They work around a bug in glibc 2.1.x where calling
435 * a dl*() function after a previous dl*() function has failed
436 * without a dlerror() call between the two will cause a crash.
437 * They all take a pointer to a buffer that
438 * will receive the error description (from dlerror()). This
439 * parameter may be NULL if the error description is not required.
442 /***********************************************************************
443 * wine_dlopen
445 void *wine_dlopen( const char *filename, int flag, char *error, int errorsize )
447 #ifdef HAVE_DLOPEN
448 void *ret;
449 const char *s;
450 dlerror(); dlerror();
451 ret = dlopen( filename, flag );
452 s = dlerror();
453 if (error)
455 strncpy( error, s ? s : "", errorsize );
456 error[errorsize - 1] = '\0';
458 dlerror();
459 return ret;
460 #else
461 if (error)
463 strncpy( error, "dlopen interface not detected by configure", errorsize );
464 error[errorsize - 1] = '\0';
466 return NULL;
467 #endif
470 /***********************************************************************
471 * wine_dlsym
473 void *wine_dlsym( void *handle, const char *symbol, char *error, int errorsize )
475 #ifdef HAVE_DLOPEN
476 void *ret;
477 const char *s;
478 dlerror(); dlerror();
479 ret = dlsym( handle, symbol );
480 s = dlerror();
481 if (error)
483 strncpy( error, s ? s : "", errorsize );
484 error[errorsize - 1] = '\0';
486 dlerror();
487 return ret;
488 #else
489 if (error)
491 strncpy( error, "dlopen interface not detected by configure", errorsize );
492 error[errorsize - 1] = '\0';
494 return NULL;
495 #endif
498 /***********************************************************************
499 * wine_dlclose
501 int wine_dlclose( void *handle, char *error, int errorsize )
503 #ifdef HAVE_DLOPEN
504 int ret;
505 const char *s;
506 dlerror(); dlerror();
507 ret = dlclose( handle );
508 s = dlerror();
509 if (error)
511 strncpy( error, s ? s : "", errorsize );
512 error[errorsize - 1] = '\0';
514 dlerror();
515 return ret;
516 #else
517 if (error)
519 strncpy( error, "dlopen interface not detected by configure", errorsize );
520 error[errorsize - 1] = '\0';
522 return 1;
523 #endif