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"
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_MMAN_H
34 #include "wine/library.h"
36 /* argc/argv for the Windows application */
37 int __wine_main_argc = 0;
38 char **__wine_main_argv = NULL;
39 WCHAR **__wine_main_wargv = NULL;
45 const IMAGE_NT_HEADERS *nt; /* NT header */
46 const char *filename; /* DLL file name */
47 } builtin_dlls[MAX_DLLS];
51 static const IMAGE_NT_HEADERS *main_exe;
53 static load_dll_callback_t load_dll_callback;
55 static const char **dll_paths;
56 static int nb_dll_paths;
57 static int dll_path_maxlen;
61 /* build the dll load path from the WINEDLLPATH variable */
62 static void build_dll_path(void)
64 static const char * const dlldir = DLLDIR;
66 char *p, *path = getenv( "WINEDLLPATH" );
72 /* count how many path elements we need */
77 while (*p == ':') p++;
80 while (*p && *p != ':') p++;
84 dll_paths = malloc( (count+1) * sizeof(*dll_paths) );
92 while (*p == ':') *p++ = 0;
94 dll_paths[nb_dll_paths] = p;
95 while (*p && *p != ':') p++;
96 if (p - dll_paths[nb_dll_paths] > dll_path_maxlen)
97 dll_path_maxlen = p - dll_paths[nb_dll_paths];
102 /* append default dll dir (if not empty) to path */
103 if ((len = strlen(dlldir)))
105 if (len > dll_path_maxlen) dll_path_maxlen = len;
106 dll_paths[nb_dll_paths++] = dlldir;
110 /* check if a given file can be opened */
111 inline static int file_exists( const char *name )
113 int fd = open( name, O_RDONLY );
114 if (fd != -1) close( fd );
118 /* open a library for a given dll, searching in the dll path
119 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
120 static void *dlopen_dll( const char *name, char *error, int errorsize, int test_only )
122 int i, namelen = strlen(name);
126 if (!init_done) build_dll_path();
128 buffer = malloc( dll_path_maxlen + namelen + 5 );
130 /* store the name at the end of the buffer, followed by .so */
131 p = buffer + dll_path_maxlen;
133 memcpy( p, name, namelen );
134 strcpy( p + namelen, ".so" );
136 for (i = 0; i < nb_dll_paths; i++)
138 int len = strlen(dll_paths[i]);
139 p = buffer + dll_path_maxlen - len;
140 memcpy( p, dll_paths[i], len );
141 if (test_only) /* just test for file existence */
143 if ((ret = (void *)file_exists( p ))) break;
147 if ((ret = wine_dlopen( p, RTLD_NOW, error, errorsize ))) break;
148 if (file_exists( p )) break; /* exists but cannot be loaded, return the error */
156 /* adjust an array of pointers to make them into RVAs */
157 static inline void fixup_rva_ptrs( void *array, void *base, int count )
159 void **ptr = (void **)array;
162 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
168 /* fixup RVAs in the import directory */
169 static void fixup_imports( IMAGE_IMPORT_DESCRIPTOR *dir, DWORD size, void *base )
171 int count = size / sizeof(void *);
172 void **ptr = (void **)dir;
174 /* everything is either a pointer or a ordinal value below 0x10000 */
177 if (*ptr >= (void *)0x10000) *ptr = (void *)((char *)*ptr - (char *)base);
178 else if (*ptr) *ptr = (void *)(0x80000000 | (unsigned int)*ptr);
184 /* fixup RVAs in the resource directory */
185 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, char *root, void *base )
187 IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
190 entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
191 for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
193 void *ptr = root + entry->u2.s3.OffsetToDirectory;
194 if (entry->u2.s3.DataIsDirectory) fixup_resources( ptr, root, base );
197 IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
198 fixup_rva_ptrs( &data->OffsetToData, base, 1 );
204 /* map a builtin dll in memory and fixup RVAs */
205 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
208 IMAGE_DATA_DIRECTORY *dir;
209 IMAGE_DOS_HEADER *dos;
210 IMAGE_NT_HEADERS *nt;
211 IMAGE_SECTION_HEADER *sec;
212 BYTE *addr, *code_start, *data_start;
213 size_t page_size = getpagesize();
214 int nb_sections = 2; /* code + data */
216 size_t size = (sizeof(IMAGE_DOS_HEADER)
217 + sizeof(IMAGE_NT_HEADERS)
218 + nb_sections * sizeof(IMAGE_SECTION_HEADER));
220 assert( size <= page_size );
222 /* module address must be aligned on 64K boundary */
223 addr = (BYTE *)((nt_descr->OptionalHeader.ImageBase + 0xffff) & ~0xffff);
224 if (wine_anon_mmap( addr, page_size, PROT_READ|PROT_WRITE, MAP_FIXED ) != addr) return NULL;
226 dos = (IMAGE_DOS_HEADER *)addr;
227 nt = (IMAGE_NT_HEADERS *)(dos + 1);
228 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
229 code_start = addr + page_size;
232 data_start = code_start + page_size;
234 /* Build the DOS and NT headers */
236 dos->e_magic = IMAGE_DOS_SIGNATURE;
237 dos->e_lfanew = sizeof(*dos);
241 nt->FileHeader.NumberOfSections = nb_sections;
242 nt->OptionalHeader.SizeOfCode = data_start - code_start;
243 nt->OptionalHeader.SizeOfInitializedData = 0;
244 nt->OptionalHeader.SizeOfUninitializedData = 0;
245 nt->OptionalHeader.ImageBase = (DWORD)addr;
247 fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
249 /* Build the code section */
251 strcpy( sec->Name, ".text" );
252 sec->SizeOfRawData = data_start - code_start;
253 sec->Misc.VirtualSize = sec->SizeOfRawData;
254 sec->VirtualAddress = code_start - addr;
255 sec->PointerToRawData = code_start - addr;
256 sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
259 /* Build the data section */
261 strcpy( sec->Name, ".data" );
262 sec->SizeOfRawData = 0;
263 sec->Misc.VirtualSize = sec->SizeOfRawData;
264 sec->VirtualAddress = data_start - addr;
265 sec->PointerToRawData = data_start - addr;
266 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
267 IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
270 /* Build the import directory */
272 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
275 IMAGE_IMPORT_DESCRIPTOR *imports = (void *)dir->VirtualAddress;
276 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
277 fixup_imports( imports, dir->Size, addr );
280 /* Build the resource directory */
282 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
285 void *ptr = (void *)dir->VirtualAddress;
286 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
287 fixup_resources( ptr, ptr, addr );
290 /* Build the export directory */
292 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
295 IMAGE_EXPORT_DIRECTORY *exports = (void *)dir->VirtualAddress;
296 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
297 fixup_rva_ptrs( (void *)exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
298 fixup_rva_ptrs( (void *)exports->AddressOfNames, addr, exports->NumberOfNames );
299 fixup_rva_ptrs( &exports->Name, addr, 1 );
300 fixup_rva_ptrs( &exports->AddressOfFunctions, addr, 1 );
301 fixup_rva_ptrs( &exports->AddressOfNames, addr, 1 );
302 fixup_rva_ptrs( &exports->AddressOfNameOrdinals, addr, 1 );
305 #else /* HAVE_MMAP */
307 #endif /* HAVE_MMAP */
311 /***********************************************************************
312 * __wine_dll_register
314 * Register a built-in DLL descriptor.
316 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
318 if (load_dll_callback) load_dll_callback( map_dll(header), filename );
321 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
325 assert( nb_dlls < MAX_DLLS );
326 builtin_dlls[nb_dlls].nt = header;
327 builtin_dlls[nb_dlls].filename = filename;
334 /***********************************************************************
335 * wine_dll_set_callback
337 * Set the callback function for dll loading, and call it
338 * for all dlls that were implicitly loaded already.
340 void wine_dll_set_callback( load_dll_callback_t load )
343 load_dll_callback = load;
344 for (i = 0; i < nb_dlls; i++)
346 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
348 builtin_dlls[i].nt = NULL;
349 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
352 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
356 /***********************************************************************
359 * Load a builtin dll.
361 void *wine_dll_load( const char *filename, char *error, int errorsize )
365 /* callback must have been set already */
366 assert( load_dll_callback );
368 /* check if we have it in the list */
369 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
370 for (i = 0; i < nb_dlls; i++)
372 if (!builtin_dlls[i].nt) continue;
373 if (!strcmp( builtin_dlls[i].filename, filename ))
375 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
376 builtin_dlls[i].nt = NULL;
377 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
381 return dlopen_dll( filename, error, errorsize, 0 );
385 /***********************************************************************
388 * Unload a builtin dll.
390 void wine_dll_unload( void *handle )
392 if (handle != (void *)1)
393 wine_dlclose( handle, NULL, 0 );
397 /***********************************************************************
398 * wine_dll_load_main_exe
400 * Try to load the .so for the main exe.
402 void *wine_dll_load_main_exe( const char *name, char *error, int errorsize, int test_only )
404 return dlopen_dll( name, error, errorsize, test_only );