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;
53 const IMAGE_NT_HEADERS *nt; /* NT header */
54 const char *filename; /* DLL file name */
55 } builtin_dlls[MAX_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;
69 /* build the dll load path from the WINEDLLPATH variable */
70 static void build_dll_path(void)
72 static const char * const dlldir = DLLDIR;
74 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 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;
142 memcpy( p, name, namelen );
143 strcpy( p + namelen, ".so" );
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 */
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;
165 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
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 */
180 if (*ptr >= (void *)0x10000) *ptr = (void *)((char *)*ptr - (char *)base);
181 else if (*ptr) *ptr = (void *)(0x80000000 | (unsigned int)*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;
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 );
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 )
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;
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);
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);
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);
273 /* Build the import directory */
275 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
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];
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];
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 );
308 #else /* HAVE_MMAP */
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 );
324 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
328 assert( nb_dlls < MAX_DLLS );
329 builtin_dlls[nb_dlls].nt = header;
330 builtin_dlls[nb_dlls].filename = filename;
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 )
346 load_dll_callback = load;
347 for (i = 0; i < nb_dlls; i++)
349 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
351 builtin_dlls[i].nt = NULL;
352 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
355 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
359 /***********************************************************************
362 * Load a builtin dll.
364 void *wine_dll_load( const char *filename, char *error, int errorsize, int *file_exists )
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 );
385 return dlopen_dll( filename, error, errorsize, 0, file_exists );
389 /***********************************************************************
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 /***********************************************************************
416 * Main Wine initialisation.
418 void wine_init( int argc, char *argv[], char *error, int error_size )
422 void (*init_func)(int, char **);
424 if (!dlopen_dll( "ntdll.dll", error, error_size, 0, &file_exists )) return;
425 /* make sure kernel32 is loaded too */
426 if (!(kernel = dlopen_dll( "kernel32.dll", error, error_size, 0, &file_exists ))) return;
427 if (!(init_func = wine_dlsym( kernel, "__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 /***********************************************************************
445 void *wine_dlopen( const char *filename, int flag, char *error, int errorsize )
450 dlerror(); dlerror();
451 ret = dlopen( filename, flag );
455 strncpy( error, s ? s : "", errorsize );
456 error[errorsize - 1] = '\0';
463 strncpy( error, "dlopen interface not detected by configure", errorsize );
464 error[errorsize - 1] = '\0';
470 /***********************************************************************
473 void *wine_dlsym( void *handle, const char *symbol, char *error, int errorsize )
478 dlerror(); dlerror();
479 ret = dlsym( handle, symbol );
483 strncpy( error, s ? s : "", errorsize );
484 error[errorsize - 1] = '\0';
491 strncpy( error, "dlopen interface not detected by configure", errorsize );
492 error[errorsize - 1] = '\0';
498 /***********************************************************************
501 int wine_dlclose( void *handle, char *error, int errorsize )
506 dlerror(); dlerror();
507 ret = dlclose( handle );
511 strncpy( error, s ? s : "", errorsize );
512 error[errorsize - 1] = '\0';
519 strncpy( error, "dlopen interface not detected by configure", errorsize );
520 error[errorsize - 1] = '\0';