Don't make the spec file constructors static so that they don't get
[wine/multimedia.git] / library / loader.c
blob35a7b4229728a3a1134045926eace0785b8f8804
1 /*
2 * Win32 builtin dlls support
4 * Copyright 2000 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <ctype.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15 #ifdef HAVE_SYS_MMAN_H
16 #include <sys/mman.h>
17 #endif
18 #ifdef HAVE_DL_API
19 #include <dlfcn.h>
20 #endif
22 #include "winnt.h"
23 #include "wine/library.h"
24 #include "wine/port.h"
26 #define MAX_DLLS 100
28 static struct
30 const IMAGE_NT_HEADERS *nt; /* NT header */
31 const char *filename; /* DLL file name */
32 } builtin_dlls[MAX_DLLS];
34 static int nb_dlls;
36 static const IMAGE_NT_HEADERS *main_exe;
38 static load_dll_callback_t load_dll_callback;
40 static char **dll_paths;
41 static int nb_dll_paths;
42 static int dll_path_maxlen;
43 static int init_done;
46 /* build the dll load path from the WINEDLLPATH variable */
47 static void build_dll_path(void)
49 int count = 0;
50 char *p, *path = getenv( "WINEDLLPATH" );
52 init_done = 1;
53 if (!path) return;
54 path = strdup(path);
55 p = path;
56 while (*p)
58 while (*p == ':') p++;
59 if (!*p) break;
60 count++;
61 while (*p && *p != ':') p++;
64 dll_paths = malloc( count * sizeof(*dll_paths) );
65 p = path;
66 nb_dll_paths = 0;
67 while (*p)
69 while (*p == ':') *p++ = 0;
70 if (!*p) break;
71 dll_paths[nb_dll_paths] = p;
72 while (*p && *p != ':') p++;
73 if (p - dll_paths[nb_dll_paths] > dll_path_maxlen)
74 dll_path_maxlen = p - dll_paths[nb_dll_paths];
75 nb_dll_paths++;
80 /* open a library for a given dll, searching in the dll path
81 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
82 static void *dlopen_dll( const char *name )
84 #ifdef HAVE_DL_API
85 int i, namelen = strlen(name);
86 char *buffer, *p, *ext;
87 void *ret = NULL;
89 if (!init_done) build_dll_path();
91 /* clear dlerror to avoid glibc bug */
92 dlerror();
94 buffer = malloc( dll_path_maxlen + namelen + 8 );
96 /* store the name at the end of the buffer, prefixed by /lib and followed by .so */
97 p = buffer + dll_path_maxlen;
98 memcpy( p, "/lib", 4 );
99 p += 4;
100 memcpy( p, name, namelen+1 );
101 ext = strrchr( p, '.' );
102 p += namelen;
103 /* check for .dll or .exe extension to remove */
104 if (ext && (!strcmp( ext, ".dll" ) || !strcmp( ext, ".exe" ))) p = ext;
105 memcpy( p, ".so", 4 );
107 for (i = 0; i < nb_dll_paths; i++)
109 int len = strlen(dll_paths[i]);
110 char *p = buffer + dll_path_maxlen - len;
111 memcpy( p, dll_paths[i], len );
112 if ((ret = dlopen( p, RTLD_NOW ))) break;
113 dlerror(); /* clear dlerror to avoid glibc bug */
116 /* now try the default dlopen search path */
117 if (!ret) ret = dlopen( buffer + dll_path_maxlen + 1, RTLD_NOW );
118 free( buffer );
119 return ret;
120 #else
121 return NULL;
122 #endif
126 /* adjust an array of pointers to make them into RVAs */
127 static inline void fixup_rva_ptrs( void *array, void *base, int count )
129 void **ptr = (void **)array;
130 while (count--)
132 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
133 ptr++;
138 /* fixup RVAs in the resource directory */
139 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, char *root, void *base )
141 IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
142 int i;
144 entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
145 for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
147 void *ptr = root + entry->u2.s2.OffsetToDirectory;
148 if (entry->u2.s2.DataIsDirectory) fixup_resources( ptr, root, base );
149 else
151 IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
152 fixup_rva_ptrs( &data->OffsetToData, base, 1 );
158 /* map a builtin dll in memory and fixup RVAs */
159 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
161 IMAGE_DATA_DIRECTORY *dir;
162 IMAGE_DOS_HEADER *dos;
163 IMAGE_NT_HEADERS *nt;
164 IMAGE_SECTION_HEADER *sec;
165 BYTE *addr, *code_start, *data_start;
166 size_t page_size = getpagesize();
167 int nb_sections = 2; /* code + data */
169 size_t size = (sizeof(IMAGE_DOS_HEADER)
170 + sizeof(IMAGE_NT_HEADERS)
171 + nb_sections * sizeof(IMAGE_SECTION_HEADER));
173 assert( size <= page_size );
175 if (nt_descr->OptionalHeader.ImageBase)
177 addr = wine_anon_mmap( (void *)nt_descr->OptionalHeader.ImageBase,
178 page_size, PROT_READ|PROT_WRITE, MAP_FIXED );
179 if (addr != (BYTE *)nt_descr->OptionalHeader.ImageBase) return NULL;
181 else
183 /* this will leak memory; but it should never happen */
184 addr = wine_anon_mmap( NULL, page_size, PROT_READ|PROT_WRITE, 0 );
185 if (addr == (BYTE *)-1) return NULL;
188 dos = (IMAGE_DOS_HEADER *)addr;
189 nt = (IMAGE_NT_HEADERS *)(dos + 1);
190 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
191 code_start = addr + page_size;
193 /* HACK! */
194 data_start = code_start + page_size;
196 /* Build the DOS and NT headers */
198 dos->e_magic = IMAGE_DOS_SIGNATURE;
199 dos->e_lfanew = sizeof(*dos);
201 *nt = *nt_descr;
203 nt->FileHeader.NumberOfSections = nb_sections;
204 nt->OptionalHeader.SizeOfCode = data_start - code_start;
205 nt->OptionalHeader.SizeOfInitializedData = 0;
206 nt->OptionalHeader.SizeOfUninitializedData = 0;
207 nt->OptionalHeader.ImageBase = (DWORD)addr;
209 fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
211 /* Build the code section */
213 strcpy( sec->Name, ".text" );
214 sec->SizeOfRawData = data_start - code_start;
215 sec->Misc.VirtualSize = sec->SizeOfRawData;
216 sec->VirtualAddress = code_start - addr;
217 sec->PointerToRawData = code_start - addr;
218 sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
219 sec++;
221 /* Build the data section */
223 strcpy( sec->Name, ".data" );
224 sec->SizeOfRawData = 0;
225 sec->Misc.VirtualSize = sec->SizeOfRawData;
226 sec->VirtualAddress = data_start - addr;
227 sec->PointerToRawData = data_start - addr;
228 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
229 IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
230 sec++;
232 /* Build the import directory */
234 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
235 if (dir->Size)
237 IMAGE_IMPORT_DESCRIPTOR *imports = (void *)dir->VirtualAddress;
238 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
239 /* we can fixup everything at once since we only have pointers and 0 values */
240 fixup_rva_ptrs( imports, addr, dir->Size / sizeof(void*) );
243 /* Build the resource directory */
245 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
246 if (dir->Size)
248 void *ptr = (void *)dir->VirtualAddress;
249 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
250 fixup_resources( ptr, ptr, addr );
253 /* Build the export directory */
255 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
256 if (dir->Size)
258 IMAGE_EXPORT_DIRECTORY *exports = (void *)dir->VirtualAddress;
259 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
260 fixup_rva_ptrs( (void *)exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
261 fixup_rva_ptrs( (void *)exports->AddressOfNames, addr, exports->NumberOfNames );
262 fixup_rva_ptrs( &exports->Name, addr, 1 );
263 fixup_rva_ptrs( &exports->AddressOfFunctions, addr, 1 );
264 fixup_rva_ptrs( &exports->AddressOfNames, addr, 1 );
265 fixup_rva_ptrs( &exports->AddressOfNameOrdinals, addr, 1 );
267 return addr;
271 /***********************************************************************
272 * __wine_dll_register
274 * Register a built-in DLL descriptor.
276 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
278 if (load_dll_callback) load_dll_callback( map_dll(header), filename );
279 else
281 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
282 main_exe = header;
283 else
285 assert( nb_dlls < MAX_DLLS );
286 builtin_dlls[nb_dlls].nt = header;
287 builtin_dlls[nb_dlls].filename = filename;
288 nb_dlls++;
294 /***********************************************************************
295 * wine_dll_set_callback
297 * Set the callback function for dll loading, and call it
298 * for all dlls that were implicitly loaded already.
300 void wine_dll_set_callback( load_dll_callback_t load )
302 int i;
303 load_dll_callback = load;
304 for (i = 0; i < nb_dlls; i++)
306 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
307 if (!nt) continue;
308 builtin_dlls[i].nt = NULL;
309 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
311 nb_dlls = 0;
312 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
316 /***********************************************************************
317 * wine_dll_load
319 * Load a builtin dll.
321 void *wine_dll_load( const char *filename )
323 int i;
325 /* callback must have been set already */
326 assert( load_dll_callback );
328 /* check if we have it in the list */
329 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
330 for (i = 0; i < nb_dlls; i++)
332 if (!builtin_dlls[i].nt) continue;
333 if (!strcmp( builtin_dlls[i].filename, filename ))
335 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
336 builtin_dlls[i].nt = NULL;
337 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
338 return (void *)1;
341 return dlopen_dll( filename );
345 /***********************************************************************
346 * wine_dll_unload
348 * Unload a builtin dll.
350 void wine_dll_unload( void *handle )
352 #ifdef HAVE_DL_API
353 if (handle != (void *)1) dlclose( handle );
354 #endif
358 /***********************************************************************
359 * wine_dll_load_main_exe
361 * Try to load the .so for the main exe, optionally searching for it in PATH.
362 * Note: dlerror() is cleared before returning because of a glibc bug.
364 void *wine_dll_load_main_exe( const char *name, int search_path )
366 void *ret = NULL;
367 #ifdef HAVE_DL_API
368 const char *path = NULL;
369 if (search_path) path = getenv( "PATH" );
371 if (!path)
373 /* no path, try only the specified name */
374 dlerror(); /* clear dlerror to avoid glibc bug */
375 ret = dlopen( name, RTLD_NOW );
377 else
379 char buffer[128], *tmp = buffer;
380 size_t namelen = strlen(name);
381 size_t pathlen = strlen(path);
383 if (namelen + pathlen + 2 > sizeof(buffer)) tmp = malloc( namelen + pathlen + 2 );
384 if (tmp)
386 char *basename = tmp + pathlen;
387 *basename = '/';
388 strcpy( basename + 1, name );
389 for (;;)
391 int len;
392 const char *p = strchr( path, ':' );
393 if (!p) p = path + strlen(path);
394 if ((len = p - path) > 0)
396 memcpy( basename - len, path, len );
397 dlerror(); /* clear dlerror to avoid glibc bug */
398 if ((ret = dlopen( basename - len, RTLD_NOW ))) break;
400 if (!*p) break;
401 path = p + 1;
403 if (tmp != buffer) free( tmp );
406 if (!ret) dlerror(); /* clear dlerror to avoid glibc bug */
407 #endif /* HAVE_DL_API */
408 return ret;