Define NONAMELESS{STRUCT,UNION} explicitly in the files that need them.
[wine/multimedia.git] / library / loader.c
blobd97043580dbda2e5277cb75c46d2bcb1618f4c1d
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 <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
35 #include "windef.h"
36 #include "wine/library.h"
38 /* argc/argv for the Windows application */
39 int __wine_main_argc = 0;
40 char **__wine_main_argv = NULL;
41 WCHAR **__wine_main_wargv = NULL;
43 #define MAX_DLLS 100
45 static struct
47 const IMAGE_NT_HEADERS *nt; /* NT header */
48 const char *filename; /* DLL file name */
49 } builtin_dlls[MAX_DLLS];
51 static int nb_dlls;
53 static const IMAGE_NT_HEADERS *main_exe;
55 static load_dll_callback_t load_dll_callback;
57 static const char **dll_paths;
58 static int nb_dll_paths;
59 static int dll_path_maxlen;
60 static int init_done;
63 /* build the dll load path from the WINEDLLPATH variable */
64 static void build_dll_path(void)
66 static const char * const dlldir = DLLDIR;
67 int len, count = 0;
68 char *p, *path = getenv( "WINEDLLPATH" );
70 init_done = 1;
72 if (path)
74 /* count how many path elements we need */
75 path = strdup(path);
76 p = path;
77 while (*p)
79 while (*p == ':') p++;
80 if (!*p) break;
81 count++;
82 while (*p && *p != ':') p++;
86 dll_paths = malloc( (count+1) * sizeof(*dll_paths) );
88 if (count)
90 p = path;
91 nb_dll_paths = 0;
92 while (*p)
94 while (*p == ':') *p++ = 0;
95 if (!*p) break;
96 dll_paths[nb_dll_paths] = p;
97 while (*p && *p != ':') p++;
98 if (p - dll_paths[nb_dll_paths] > dll_path_maxlen)
99 dll_path_maxlen = p - dll_paths[nb_dll_paths];
100 nb_dll_paths++;
104 /* append default dll dir (if not empty) to path */
105 if ((len = strlen(dlldir)))
107 if (len > dll_path_maxlen) dll_path_maxlen = len;
108 dll_paths[nb_dll_paths++] = dlldir;
112 /* check if a given file can be opened */
113 inline static int file_exists( const char *name )
115 int fd = open( name, O_RDONLY );
116 if (fd != -1) close( fd );
117 return (fd != -1);
120 /* open a library for a given dll, searching in the dll path
121 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
122 static void *dlopen_dll( const char *name, char *error, int errorsize, int test_only )
124 int i, namelen = strlen(name);
125 char *buffer, *p;
126 void *ret = NULL;
128 if (!init_done) build_dll_path();
130 buffer = malloc( dll_path_maxlen + namelen + 5 );
132 /* store the name at the end of the buffer, followed by .so */
133 p = buffer + dll_path_maxlen;
134 *p++ = '/';
135 memcpy( p, name, namelen );
136 strcpy( p + namelen, ".so" );
138 for (i = 0; i < nb_dll_paths; i++)
140 int len = strlen(dll_paths[i]);
141 p = buffer + dll_path_maxlen - len;
142 memcpy( p, dll_paths[i], len );
143 if (test_only) /* just test for file existence */
145 if ((ret = (void *)file_exists( p ))) break;
147 else
149 if ((ret = wine_dlopen( p, RTLD_NOW, error, errorsize ))) break;
150 if (file_exists( p )) break; /* exists but cannot be loaded, return the error */
153 free( buffer );
154 return ret;
158 /* adjust an array of pointers to make them into RVAs */
159 static inline void fixup_rva_ptrs( void *array, void *base, int count )
161 void **ptr = (void **)array;
162 while (count--)
164 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
165 ptr++;
170 /* fixup RVAs in the import directory */
171 static void fixup_imports( IMAGE_IMPORT_DESCRIPTOR *dir, DWORD size, void *base )
173 int count = size / sizeof(void *);
174 void **ptr = (void **)dir;
176 /* everything is either a pointer or a ordinal value below 0x10000 */
177 while (count--)
179 if (*ptr >= (void *)0x10000) *ptr = (void *)((char *)*ptr - (char *)base);
180 else if (*ptr) *ptr = (void *)(0x80000000 | (unsigned int)*ptr);
181 ptr++;
186 /* fixup RVAs in the resource directory */
187 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, char *root, void *base )
189 IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
190 int i;
192 entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
193 for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
195 void *ptr = root + entry->u2.s3.OffsetToDirectory;
196 if (entry->u2.s3.DataIsDirectory) fixup_resources( ptr, root, base );
197 else
199 IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
200 fixup_rva_ptrs( &data->OffsetToData, base, 1 );
206 /* map a builtin dll in memory and fixup RVAs */
207 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
209 #ifdef HAVE_MMAP
210 IMAGE_DATA_DIRECTORY *dir;
211 IMAGE_DOS_HEADER *dos;
212 IMAGE_NT_HEADERS *nt;
213 IMAGE_SECTION_HEADER *sec;
214 BYTE *addr, *code_start, *data_start;
215 size_t page_size = getpagesize();
216 int nb_sections = 2; /* code + data */
218 size_t size = (sizeof(IMAGE_DOS_HEADER)
219 + sizeof(IMAGE_NT_HEADERS)
220 + nb_sections * sizeof(IMAGE_SECTION_HEADER));
222 assert( size <= page_size );
224 /* module address must be aligned on 64K boundary */
225 addr = (BYTE *)((nt_descr->OptionalHeader.ImageBase + 0xffff) & ~0xffff);
226 if (wine_anon_mmap( addr, page_size, PROT_READ|PROT_WRITE, MAP_FIXED ) != addr) return NULL;
228 dos = (IMAGE_DOS_HEADER *)addr;
229 nt = (IMAGE_NT_HEADERS *)(dos + 1);
230 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
231 code_start = addr + page_size;
233 /* HACK! */
234 data_start = code_start + page_size;
236 /* Build the DOS and NT headers */
238 dos->e_magic = IMAGE_DOS_SIGNATURE;
239 dos->e_lfanew = sizeof(*dos);
241 *nt = *nt_descr;
243 nt->FileHeader.NumberOfSections = nb_sections;
244 nt->OptionalHeader.SizeOfCode = data_start - code_start;
245 nt->OptionalHeader.SizeOfInitializedData = 0;
246 nt->OptionalHeader.SizeOfUninitializedData = 0;
247 nt->OptionalHeader.ImageBase = (DWORD)addr;
249 fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
251 /* Build the code section */
253 strcpy( sec->Name, ".text" );
254 sec->SizeOfRawData = data_start - code_start;
255 sec->Misc.VirtualSize = sec->SizeOfRawData;
256 sec->VirtualAddress = code_start - addr;
257 sec->PointerToRawData = code_start - addr;
258 sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
259 sec++;
261 /* Build the data section */
263 strcpy( sec->Name, ".data" );
264 sec->SizeOfRawData = 0;
265 sec->Misc.VirtualSize = sec->SizeOfRawData;
266 sec->VirtualAddress = data_start - addr;
267 sec->PointerToRawData = data_start - addr;
268 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
269 IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
270 sec++;
272 /* Build the import directory */
274 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
275 if (dir->Size)
277 IMAGE_IMPORT_DESCRIPTOR *imports = (void *)dir->VirtualAddress;
278 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
279 fixup_imports( imports, dir->Size, addr );
282 /* Build the resource directory */
284 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
285 if (dir->Size)
287 void *ptr = (void *)dir->VirtualAddress;
288 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
289 fixup_resources( ptr, ptr, addr );
292 /* Build the export directory */
294 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
295 if (dir->Size)
297 IMAGE_EXPORT_DIRECTORY *exports = (void *)dir->VirtualAddress;
298 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
299 fixup_rva_ptrs( (void *)exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
300 fixup_rva_ptrs( (void *)exports->AddressOfNames, addr, exports->NumberOfNames );
301 fixup_rva_ptrs( &exports->Name, addr, 1 );
302 fixup_rva_ptrs( &exports->AddressOfFunctions, addr, 1 );
303 fixup_rva_ptrs( &exports->AddressOfNames, addr, 1 );
304 fixup_rva_ptrs( &exports->AddressOfNameOrdinals, addr, 1 );
306 return addr;
307 #else /* HAVE_MMAP */
308 return NULL;
309 #endif /* HAVE_MMAP */
313 /***********************************************************************
314 * __wine_dll_register
316 * Register a built-in DLL descriptor.
318 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
320 if (load_dll_callback) load_dll_callback( map_dll(header), filename );
321 else
323 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
324 main_exe = header;
325 else
327 assert( nb_dlls < MAX_DLLS );
328 builtin_dlls[nb_dlls].nt = header;
329 builtin_dlls[nb_dlls].filename = filename;
330 nb_dlls++;
336 /***********************************************************************
337 * wine_dll_set_callback
339 * Set the callback function for dll loading, and call it
340 * for all dlls that were implicitly loaded already.
342 void wine_dll_set_callback( load_dll_callback_t load )
344 int i;
345 load_dll_callback = load;
346 for (i = 0; i < nb_dlls; i++)
348 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
349 if (!nt) continue;
350 builtin_dlls[i].nt = NULL;
351 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
353 nb_dlls = 0;
354 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
358 /***********************************************************************
359 * wine_dll_load
361 * Load a builtin dll.
363 void *wine_dll_load( const char *filename, char *error, int errorsize )
365 int i;
367 /* callback must have been set already */
368 assert( load_dll_callback );
370 /* check if we have it in the list */
371 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
372 for (i = 0; i < nb_dlls; i++)
374 if (!builtin_dlls[i].nt) continue;
375 if (!strcmp( builtin_dlls[i].filename, filename ))
377 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
378 builtin_dlls[i].nt = NULL;
379 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
380 return (void *)1;
383 return dlopen_dll( filename, error, errorsize, 0 );
387 /***********************************************************************
388 * wine_dll_unload
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, int test_only )
406 return dlopen_dll( name, error, errorsize, test_only );