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 struct dll_path_context
60 const IMAGE_NT_HEADERS *nt; /* NT header */
61 const char *filename; /* DLL file name */
62 } builtin_dlls[MAX_DLLS];
66 static const IMAGE_NT_HEADERS *main_exe;
68 static load_dll_callback_t load_dll_callback;
70 static const char **dll_paths;
71 static int nb_dll_paths;
72 static int dll_path_maxlen;
74 extern void mmap_init(void);
76 /* build the dll load path from the WINEDLLPATH variable */
77 static void build_dll_path(void)
79 static const char * const dlldir = DLLDIR;
81 char *p, *path = getenv( "WINEDLLPATH" );
85 /* count how many path elements we need */
90 while (*p == ':') p++;
93 while (*p && *p != ':') p++;
97 dll_paths = malloc( (count+1) * sizeof(*dll_paths) );
105 while (*p == ':') *p++ = 0;
107 dll_paths[nb_dll_paths] = p;
108 while (*p && *p != ':') p++;
109 if (p - dll_paths[nb_dll_paths] > dll_path_maxlen)
110 dll_path_maxlen = p - dll_paths[nb_dll_paths];
115 /* append default dll dir (if not empty) to path */
116 if ((len = strlen(dlldir)))
118 if (len > dll_path_maxlen) dll_path_maxlen = len;
119 dll_paths[nb_dll_paths++] = dlldir;
123 /* check if a given file can be opened */
124 inline static int file_exists( const char *name )
126 int fd = open( name, O_RDONLY );
127 if (fd != -1) close( fd );
132 /* get a filename from the next entry in the dll path */
133 static char *next_dll_path( struct dll_path_context *context )
135 int index = context->index++;
137 if (index < nb_dll_paths)
139 int len = strlen( dll_paths[index] );
140 char *path = context->buffer + dll_path_maxlen - len;
141 memcpy( path, dll_paths[index], len );
148 /* get a filename from the first entry in the dll path */
149 static char *first_dll_path( const char *name, const char *ext, struct dll_path_context *context )
152 int namelen = strlen( name );
154 context->buffer = p = malloc( dll_path_maxlen + namelen + strlen(ext) + 2 );
157 /* store the name at the end of the buffer, followed by extension */
158 p += dll_path_maxlen;
160 memcpy( p, name, namelen );
161 strcpy( p + namelen, ext );
162 return next_dll_path( context );
166 /* free the dll path context created by first_dll_path */
167 inline static void free_dll_path( struct dll_path_context *context )
169 free( context->buffer );
173 /* open a library for a given dll, searching in the dll path
174 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
175 static void *dlopen_dll( const char *name, char *error, int errorsize,
176 int test_only, int *exists )
178 struct dll_path_context context;
183 for (path = first_dll_path( name, ".so", &context ); path; path = next_dll_path( &context ))
185 if (!test_only && (ret = wine_dlopen( path, RTLD_NOW, error, errorsize ))) break;
186 if ((*exists = file_exists( path ))) break; /* exists but cannot be loaded, return the error */
188 free_dll_path( &context );
193 /* adjust an array of pointers to make them into RVAs */
194 static inline void fixup_rva_ptrs( void *array, void *base, int count )
196 void **ptr = (void **)array;
199 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
205 /* fixup RVAs in the import directory */
206 static void fixup_imports( IMAGE_IMPORT_DESCRIPTOR *dir, DWORD size, void *base )
208 int count = size / sizeof(void *);
209 void **ptr = (void **)dir;
211 /* everything is either a pointer or a ordinal value below 0x10000 */
214 if (*ptr >= (void *)0x10000) *ptr = (void *)((char *)*ptr - (char *)base);
215 else if (*ptr) *ptr = (void *)(0x80000000 | (unsigned int)*ptr);
221 /* fixup RVAs in the resource directory */
222 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, char *root, void *base )
224 IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
227 entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
228 for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
230 void *ptr = root + entry->u2.s3.OffsetToDirectory;
231 if (entry->u2.s3.DataIsDirectory) fixup_resources( ptr, root, base );
234 IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
235 fixup_rva_ptrs( &data->OffsetToData, base, 1 );
241 /* map a builtin dll in memory and fixup RVAs */
242 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
245 IMAGE_DATA_DIRECTORY *dir;
246 IMAGE_DOS_HEADER *dos;
247 IMAGE_NT_HEADERS *nt;
248 IMAGE_SECTION_HEADER *sec;
250 DWORD code_start, data_start, data_end;
251 const size_t page_size = getpagesize();
252 const size_t page_mask = page_size - 1;
253 int nb_sections = 2; /* code + data */
255 size_t size = (sizeof(IMAGE_DOS_HEADER)
256 + sizeof(IMAGE_NT_HEADERS)
257 + nb_sections * sizeof(IMAGE_SECTION_HEADER));
259 assert( size <= page_size );
261 /* module address must be aligned on 64K boundary */
262 addr = (BYTE *)((nt_descr->OptionalHeader.ImageBase + 0xffff) & ~0xffff);
263 if (wine_anon_mmap( addr, page_size, PROT_READ|PROT_WRITE, MAP_FIXED ) != addr) return NULL;
265 dos = (IMAGE_DOS_HEADER *)addr;
266 nt = (IMAGE_NT_HEADERS *)(dos + 1);
267 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
269 /* Build the DOS and NT headers */
271 dos->e_magic = IMAGE_DOS_SIGNATURE;
272 dos->e_lfanew = sizeof(*dos);
276 code_start = page_size;
277 data_start = ((BYTE *)nt->OptionalHeader.BaseOfData - addr) & ~page_mask;
278 data_end = (((BYTE *)nt->OptionalHeader.SizeOfImage - addr) + page_mask) & ~page_mask;
280 nt->FileHeader.NumberOfSections = nb_sections;
281 nt->OptionalHeader.BaseOfCode = code_start;
282 nt->OptionalHeader.BaseOfData = data_start;
283 nt->OptionalHeader.SizeOfCode = data_start - code_start;
284 nt->OptionalHeader.SizeOfInitializedData = data_end - data_start;
285 nt->OptionalHeader.SizeOfUninitializedData = 0;
286 nt->OptionalHeader.SizeOfImage = data_end;
287 nt->OptionalHeader.ImageBase = (DWORD)addr;
289 fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
291 /* Build the code section */
293 strcpy( sec->Name, ".text" );
294 sec->SizeOfRawData = data_start - code_start;
295 sec->Misc.VirtualSize = sec->SizeOfRawData;
296 sec->VirtualAddress = code_start;
297 sec->PointerToRawData = code_start;
298 sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
301 /* Build the data section */
303 strcpy( sec->Name, ".data" );
304 sec->SizeOfRawData = data_end - data_start;
305 sec->Misc.VirtualSize = sec->SizeOfRawData;
306 sec->VirtualAddress = data_start;
307 sec->PointerToRawData = data_start;
308 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
309 IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
312 /* Build the import directory */
314 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
317 IMAGE_IMPORT_DESCRIPTOR *imports = (void *)dir->VirtualAddress;
318 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
319 fixup_imports( imports, dir->Size, addr );
322 /* Build the resource directory */
324 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
327 void *ptr = (void *)dir->VirtualAddress;
328 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
329 fixup_resources( ptr, ptr, addr );
332 /* Build the export directory */
334 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
337 IMAGE_EXPORT_DIRECTORY *exports = (void *)dir->VirtualAddress;
338 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
339 fixup_rva_ptrs( (void *)exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
340 fixup_rva_ptrs( (void *)exports->AddressOfNames, addr, exports->NumberOfNames );
341 fixup_rva_ptrs( &exports->Name, addr, 1 );
342 fixup_rva_ptrs( &exports->AddressOfFunctions, addr, 1 );
343 fixup_rva_ptrs( &exports->AddressOfNames, addr, 1 );
344 fixup_rva_ptrs( &exports->AddressOfNameOrdinals, addr, 1 );
347 #else /* HAVE_MMAP */
349 #endif /* HAVE_MMAP */
353 /***********************************************************************
354 * __wine_dll_register
356 * Register a built-in DLL descriptor.
358 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
360 if (load_dll_callback) load_dll_callback( map_dll(header), filename );
363 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
367 assert( nb_dlls < MAX_DLLS );
368 builtin_dlls[nb_dlls].nt = header;
369 builtin_dlls[nb_dlls].filename = filename;
376 /***********************************************************************
377 * wine_dll_set_callback
379 * Set the callback function for dll loading, and call it
380 * for all dlls that were implicitly loaded already.
382 void wine_dll_set_callback( load_dll_callback_t load )
385 load_dll_callback = load;
386 for (i = 0; i < nb_dlls; i++)
388 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
390 builtin_dlls[i].nt = NULL;
391 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
394 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
398 /***********************************************************************
401 * Load a builtin dll.
403 void *wine_dll_load( const char *filename, char *error, int errorsize, int *file_exists )
407 /* callback must have been set already */
408 assert( load_dll_callback );
410 /* check if we have it in the list */
411 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
412 for (i = 0; i < nb_dlls; i++)
414 if (!builtin_dlls[i].nt) continue;
415 if (!strcmp( builtin_dlls[i].filename, filename ))
417 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
418 builtin_dlls[i].nt = NULL;
419 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
424 return dlopen_dll( filename, error, errorsize, 0, file_exists );
428 /***********************************************************************
431 * Unload a builtin dll.
433 void wine_dll_unload( void *handle )
435 if (handle != (void *)1)
436 wine_dlclose( handle, NULL, 0 );
440 /***********************************************************************
441 * wine_dll_load_main_exe
443 * Try to load the .so for the main exe.
445 void *wine_dll_load_main_exe( const char *name, char *error, int errorsize,
446 int test_only, int *file_exists )
448 return dlopen_dll( name, error, errorsize, test_only, file_exists );
452 /***********************************************************************
455 * Retrieve the name of the 32-bit owner dll for a 16-bit dll.
456 * Return 0 if OK, -1 on error.
458 int wine_dll_get_owner( const char *name, char *buffer, int size, int *exists )
462 struct dll_path_context context;
465 for (path = first_dll_path( name, ".so", &context ); path; path = next_dll_path( &context ))
467 int res = readlink( path, buffer, size );
468 if (res != -1) /* got a symlink */
471 if (res < 4 || res >= size) break;
473 if (strchr( buffer, '/' )) break; /* contains a path, not valid */
474 if (strcmp( buffer + res - 3, ".so" )) break; /* does not end in .so, not valid */
475 buffer[res - 3] = 0; /* remove .so */
479 if ((*exists = file_exists( path ))) break; /* exists but not a symlink, return the error */
481 free_dll_path( &context );
486 /***********************************************************************
489 static void debug_usage(void)
491 static const char usage[] =
492 "Syntax of the WINEDEBUG variable:\n"
493 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
494 "Example: WINEDEBUG=+all,warn-heap\n"
495 " turns on all messages except warning heap messages\n"
496 "Available message classes: err, warn, fixme, trace\n";
497 write( 2, usage, sizeof(usage) - 1 );
502 /***********************************************************************
505 * Main Wine initialisation.
507 void wine_init( int argc, char *argv[], char *error, int error_size )
512 void (*init_func)(void);
515 wine_init_argv0_path( argv[0] );
516 __wine_main_argc = argc;
517 __wine_main_argv = argv;
518 __wine_main_environ = environ;
521 if ((wine_debug = getenv("WINEDEBUG")))
523 if (!strcmp( wine_debug, "help" )) debug_usage();
524 wine_dbg_parse_options( wine_debug );
527 if (!(ntdll = dlopen_dll( "ntdll.dll", error, error_size, 0, &file_exists ))) return;
528 if (!(init_func = wine_dlsym( ntdll, "__wine_process_init", error, error_size ))) return;
534 * These functions provide wrappers around dlopen() and associated
535 * functions. They work around a bug in glibc 2.1.x where calling
536 * a dl*() function after a previous dl*() function has failed
537 * without a dlerror() call between the two will cause a crash.
538 * They all take a pointer to a buffer that
539 * will receive the error description (from dlerror()). This
540 * parameter may be NULL if the error description is not required.
543 /***********************************************************************
546 void *wine_dlopen( const char *filename, int flag, char *error, int errorsize )
551 dlerror(); dlerror();
552 ret = dlopen( filename, flag );
556 strncpy( error, s ? s : "", errorsize );
557 error[errorsize - 1] = '\0';
564 strncpy( error, "dlopen interface not detected by configure", errorsize );
565 error[errorsize - 1] = '\0';
571 /***********************************************************************
574 void *wine_dlsym( void *handle, const char *symbol, char *error, int errorsize )
579 dlerror(); dlerror();
580 ret = dlsym( handle, symbol );
584 strncpy( error, s ? s : "", errorsize );
585 error[errorsize - 1] = '\0';
592 strncpy( error, "dlopen interface not detected by configure", errorsize );
593 error[errorsize - 1] = '\0';
599 /***********************************************************************
602 int wine_dlclose( void *handle, char *error, int errorsize )
607 dlerror(); dlerror();
608 ret = dlclose( handle );
612 strncpy( error, s ? s : "", errorsize );
613 error[errorsize - 1] = '\0';
620 strncpy( error, "dlopen interface not detected by configure", errorsize );
621 error[errorsize - 1] = '\0';