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
22 #include "wine/port.h"
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_MMAN_H
38 #define NONAMELESSUNION
39 #define NONAMELESSSTRUCT
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;
48 char **__wine_main_environ = NULL;
50 extern void init_argv0_path( const char *argv0 ); /* config.c */
56 const IMAGE_NT_HEADERS *nt; /* NT header */
57 const char *filename; /* DLL file name */
58 } builtin_dlls[MAX_DLLS];
62 static const IMAGE_NT_HEADERS *main_exe;
64 static load_dll_callback_t load_dll_callback;
66 static const char **dll_paths;
67 static int nb_dll_paths;
68 static int dll_path_maxlen;
71 /* build the dll load path from the WINEDLLPATH variable */
72 static void build_dll_path(void)
74 static const char * const dlldir = DLLDIR;
76 char *p, *path = getenv( "WINEDLLPATH" );
80 /* count how many path elements we need */
85 while (*p == ':') p++;
88 while (*p && *p != ':') p++;
92 dll_paths = malloc( (count+1) * sizeof(*dll_paths) );
100 while (*p == ':') *p++ = 0;
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];
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 );
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);
135 buffer = malloc( dll_path_maxlen + namelen + 5 );
137 /* store the name at the end of the buffer, followed by .so */
138 p = buffer + dll_path_maxlen;
140 memcpy( p, name, namelen );
141 strcpy( p + namelen, ".so" );
144 for (i = 0; i < nb_dll_paths; i++)
146 int len = strlen(dll_paths[i]);
147 p = buffer + dll_path_maxlen - len;
148 memcpy( p, dll_paths[i], len );
149 if (!test_only && (ret = wine_dlopen( p, RTLD_NOW, error, errorsize ))) break;
150 if ((*exists = file_exists( p ))) break; /* exists but cannot be loaded, return the error */
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;
163 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
169 /* fixup RVAs in the import directory */
170 static void fixup_imports( IMAGE_IMPORT_DESCRIPTOR *dir, DWORD size, void *base )
172 int count = size / sizeof(void *);
173 void **ptr = (void **)dir;
175 /* everything is either a pointer or a ordinal value below 0x10000 */
178 if (*ptr >= (void *)0x10000) *ptr = (void *)((char *)*ptr - (char *)base);
179 else if (*ptr) *ptr = (void *)(0x80000000 | (unsigned int)*ptr);
185 /* fixup RVAs in the resource directory */
186 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, char *root, void *base )
188 IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
191 entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
192 for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
194 void *ptr = root + entry->u2.s3.OffsetToDirectory;
195 if (entry->u2.s3.DataIsDirectory) fixup_resources( ptr, root, base );
198 IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
199 fixup_rva_ptrs( &data->OffsetToData, base, 1 );
205 /* map a builtin dll in memory and fixup RVAs */
206 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
209 IMAGE_DATA_DIRECTORY *dir;
210 IMAGE_DOS_HEADER *dos;
211 IMAGE_NT_HEADERS *nt;
212 IMAGE_SECTION_HEADER *sec;
213 BYTE *addr, *code_start, *data_start;
214 size_t page_size = getpagesize();
215 int nb_sections = 2; /* code + data */
217 size_t size = (sizeof(IMAGE_DOS_HEADER)
218 + sizeof(IMAGE_NT_HEADERS)
219 + nb_sections * sizeof(IMAGE_SECTION_HEADER));
221 assert( size <= page_size );
223 /* module address must be aligned on 64K boundary */
224 addr = (BYTE *)((nt_descr->OptionalHeader.ImageBase + 0xffff) & ~0xffff);
225 if (wine_anon_mmap( addr, page_size, PROT_READ|PROT_WRITE, MAP_FIXED ) != addr) return NULL;
227 dos = (IMAGE_DOS_HEADER *)addr;
228 nt = (IMAGE_NT_HEADERS *)(dos + 1);
229 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
230 code_start = addr + page_size;
233 data_start = code_start + page_size;
235 /* Build the DOS and NT headers */
237 dos->e_magic = IMAGE_DOS_SIGNATURE;
238 dos->e_lfanew = sizeof(*dos);
242 nt->FileHeader.NumberOfSections = nb_sections;
243 nt->OptionalHeader.SizeOfCode = data_start - code_start;
244 nt->OptionalHeader.SizeOfInitializedData = 0;
245 nt->OptionalHeader.SizeOfUninitializedData = 0;
246 nt->OptionalHeader.ImageBase = (DWORD)addr;
248 fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
250 /* Build the code section */
252 strcpy( sec->Name, ".text" );
253 sec->SizeOfRawData = data_start - code_start;
254 sec->Misc.VirtualSize = sec->SizeOfRawData;
255 sec->VirtualAddress = code_start - addr;
256 sec->PointerToRawData = code_start - addr;
257 sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
260 /* Build the data section */
262 strcpy( sec->Name, ".data" );
263 sec->SizeOfRawData = 0;
264 sec->Misc.VirtualSize = sec->SizeOfRawData;
265 sec->VirtualAddress = data_start - addr;
266 sec->PointerToRawData = data_start - addr;
267 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
268 IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
271 /* Build the import directory */
273 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
276 IMAGE_IMPORT_DESCRIPTOR *imports = (void *)dir->VirtualAddress;
277 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
278 fixup_imports( imports, dir->Size, addr );
281 /* Build the resource directory */
283 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
286 void *ptr = (void *)dir->VirtualAddress;
287 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
288 fixup_resources( ptr, ptr, addr );
291 /* Build the export directory */
293 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
296 IMAGE_EXPORT_DIRECTORY *exports = (void *)dir->VirtualAddress;
297 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
298 fixup_rva_ptrs( (void *)exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
299 fixup_rva_ptrs( (void *)exports->AddressOfNames, addr, exports->NumberOfNames );
300 fixup_rva_ptrs( &exports->Name, addr, 1 );
301 fixup_rva_ptrs( &exports->AddressOfFunctions, addr, 1 );
302 fixup_rva_ptrs( &exports->AddressOfNames, addr, 1 );
303 fixup_rva_ptrs( &exports->AddressOfNameOrdinals, addr, 1 );
306 #else /* HAVE_MMAP */
308 #endif /* HAVE_MMAP */
312 /***********************************************************************
313 * __wine_dll_register
315 * Register a built-in DLL descriptor.
317 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
319 if (load_dll_callback) load_dll_callback( map_dll(header), filename );
322 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
326 assert( nb_dlls < MAX_DLLS );
327 builtin_dlls[nb_dlls].nt = header;
328 builtin_dlls[nb_dlls].filename = filename;
335 /***********************************************************************
336 * wine_dll_set_callback
338 * Set the callback function for dll loading, and call it
339 * for all dlls that were implicitly loaded already.
341 void wine_dll_set_callback( load_dll_callback_t load )
344 load_dll_callback = load;
345 for (i = 0; i < nb_dlls; i++)
347 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
349 builtin_dlls[i].nt = NULL;
350 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
353 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
357 /***********************************************************************
360 * Load a builtin dll.
362 void *wine_dll_load( const char *filename, char *error, int errorsize, int *file_exists )
366 /* callback must have been set already */
367 assert( load_dll_callback );
369 /* check if we have it in the list */
370 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
371 for (i = 0; i < nb_dlls; i++)
373 if (!builtin_dlls[i].nt) continue;
374 if (!strcmp( builtin_dlls[i].filename, filename ))
376 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
377 builtin_dlls[i].nt = NULL;
378 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
383 return dlopen_dll( filename, error, errorsize, 0, file_exists );
387 /***********************************************************************
390 * Unload a builtin dll.
392 void wine_dll_unload( void *handle )
394 if (handle != (void *)1)
395 wine_dlclose( handle, NULL, 0 );
399 /***********************************************************************
400 * wine_dll_load_main_exe
402 * Try to load the .so for the main exe.
404 void *wine_dll_load_main_exe( const char *name, char *error, int errorsize,
405 int test_only, int *file_exists )
407 return dlopen_dll( name, error, errorsize, test_only, file_exists );
411 /***********************************************************************
414 * Main Wine initialisation.
416 void wine_init( int argc, char *argv[], char *error, int error_size )
418 extern char **environ;
421 void (*init_func)(void);
424 init_argv0_path( argv[0] );
425 __wine_main_argc = argc;
426 __wine_main_argv = argv;
427 __wine_main_environ = environ;
429 if (!(ntdll = dlopen_dll( "ntdll.dll", error, error_size, 0, &file_exists ))) return;
430 if (!(init_func = wine_dlsym( ntdll, "__wine_process_init", error, error_size ))) return;
436 * These functions provide wrappers around dlopen() and associated
437 * functions. They work around a bug in glibc 2.1.x where calling
438 * a dl*() function after a previous dl*() function has failed
439 * without a dlerror() call between the two will cause a crash.
440 * They all take a pointer to a buffer that
441 * will receive the error description (from dlerror()). This
442 * parameter may be NULL if the error description is not required.
445 /***********************************************************************
448 void *wine_dlopen( const char *filename, int flag, char *error, int errorsize )
453 dlerror(); dlerror();
454 ret = dlopen( filename, flag );
458 strncpy( error, s ? s : "", errorsize );
459 error[errorsize - 1] = '\0';
466 strncpy( error, "dlopen interface not detected by configure", errorsize );
467 error[errorsize - 1] = '\0';
473 /***********************************************************************
476 void *wine_dlsym( void *handle, const char *symbol, char *error, int errorsize )
481 dlerror(); dlerror();
482 ret = dlsym( handle, symbol );
486 strncpy( error, s ? s : "", errorsize );
487 error[errorsize - 1] = '\0';
494 strncpy( error, "dlopen interface not detected by configure", errorsize );
495 error[errorsize - 1] = '\0';
501 /***********************************************************************
504 int wine_dlclose( void *handle, char *error, int errorsize )
509 dlerror(); dlerror();
510 ret = dlclose( handle );
514 strncpy( error, s ? s : "", errorsize );
515 error[errorsize - 1] = '\0';
522 strncpy( error, "dlopen interface not detected by configure", errorsize );
523 error[errorsize - 1] = '\0';