Fix LUID definition.
[wine.git] / library / loader.c
blob930954626407b2d39b997742a6e05ba5f8e05ec9
1 /*
2 * Win32 builtin dlls support
4 * Copyright 2000 Alexandre Julliard
5 */
7 #include "config.h"
8 #include "wine/port.h"
10 #include <assert.h>
11 #include <ctype.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 #ifdef HAVE_SYS_MMAN_H
17 #include <sys/mman.h>
18 #endif
20 #include "winnt.h"
21 #include "wine/library.h"
23 #define MAX_DLLS 100
25 static struct
27 const IMAGE_NT_HEADERS *nt; /* NT header */
28 const char *filename; /* DLL file name */
29 } builtin_dlls[MAX_DLLS];
31 static int nb_dlls;
33 static const IMAGE_NT_HEADERS *main_exe;
35 static load_dll_callback_t load_dll_callback;
37 static char **dll_paths;
38 static int nb_dll_paths;
39 static int dll_path_maxlen;
40 static int init_done;
43 /* build the dll load path from the WINEDLLPATH variable */
44 static void build_dll_path(void)
46 int count = 0;
47 char *p, *path = getenv( "WINEDLLPATH" );
49 init_done = 1;
50 if (!path) return;
51 path = strdup(path);
52 p = path;
53 while (*p)
55 while (*p == ':') p++;
56 if (!*p) break;
57 count++;
58 while (*p && *p != ':') p++;
61 dll_paths = malloc( count * sizeof(*dll_paths) );
62 p = path;
63 nb_dll_paths = 0;
64 while (*p)
66 while (*p == ':') *p++ = 0;
67 if (!*p) break;
68 dll_paths[nb_dll_paths] = p;
69 while (*p && *p != ':') p++;
70 if (p - dll_paths[nb_dll_paths] > dll_path_maxlen)
71 dll_path_maxlen = p - dll_paths[nb_dll_paths];
72 nb_dll_paths++;
77 /* open a library for a given dll, searching in the dll path
78 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
79 static void *dlopen_dll( const char *name, char *error, int errorsize )
81 int i, namelen = strlen(name);
82 char *buffer, *p, *ext;
83 void *ret = NULL;
85 if (!init_done) build_dll_path();
87 buffer = malloc( dll_path_maxlen + namelen + 8 );
89 /* store the name at the end of the buffer, prefixed by /lib and followed by .so */
90 p = buffer + dll_path_maxlen;
91 memcpy( p, "/lib", 4 );
92 p += 4;
93 memcpy( p, name, namelen+1 );
94 ext = strrchr( p, '.' );
95 p += namelen;
96 /* check for .dll or .exe extension to remove */
97 if (ext && (!strcmp( ext, ".dll" ) || !strcmp( ext, ".exe" ))) p = ext;
98 memcpy( p, ".so", 4 );
100 for (i = 0; i < nb_dll_paths; i++)
102 int len = strlen(dll_paths[i]);
103 char *p = buffer + dll_path_maxlen - len;
104 memcpy( p, dll_paths[i], len );
105 if ((ret = wine_dlopen( p, RTLD_NOW, error, errorsize ))) break;
108 /* now try the default dlopen search path */
109 if (!ret)
110 ret = wine_dlopen( buffer + dll_path_maxlen + 1, RTLD_NOW, error, errorsize );
111 free( buffer );
112 return ret;
116 /* adjust an array of pointers to make them into RVAs */
117 static inline void fixup_rva_ptrs( void *array, void *base, int count )
119 void **ptr = (void **)array;
120 while (count--)
122 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
123 ptr++;
128 /* fixup RVAs in the resource directory */
129 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, char *root, void *base )
131 IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
132 int i;
134 entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
135 for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
137 void *ptr = root + entry->u2.s3.OffsetToDirectory;
138 if (entry->u2.s3.DataIsDirectory) fixup_resources( ptr, root, base );
139 else
141 IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
142 fixup_rva_ptrs( &data->OffsetToData, base, 1 );
148 /* map a builtin dll in memory and fixup RVAs */
149 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
151 IMAGE_DATA_DIRECTORY *dir;
152 IMAGE_DOS_HEADER *dos;
153 IMAGE_NT_HEADERS *nt;
154 IMAGE_SECTION_HEADER *sec;
155 BYTE *addr, *code_start, *data_start;
156 size_t page_size = getpagesize();
157 int nb_sections = 2; /* code + data */
159 size_t size = (sizeof(IMAGE_DOS_HEADER)
160 + sizeof(IMAGE_NT_HEADERS)
161 + nb_sections * sizeof(IMAGE_SECTION_HEADER));
163 assert( size <= page_size );
165 if (nt_descr->OptionalHeader.ImageBase)
167 addr = wine_anon_mmap( (void *)nt_descr->OptionalHeader.ImageBase,
168 page_size, PROT_READ|PROT_WRITE, MAP_FIXED );
169 if (addr != (BYTE *)nt_descr->OptionalHeader.ImageBase) return NULL;
171 else
173 /* this will leak memory; but it should never happen */
174 addr = wine_anon_mmap( NULL, page_size, PROT_READ|PROT_WRITE, 0 );
175 if (addr == (BYTE *)-1) return NULL;
178 dos = (IMAGE_DOS_HEADER *)addr;
179 nt = (IMAGE_NT_HEADERS *)(dos + 1);
180 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
181 code_start = addr + page_size;
183 /* HACK! */
184 data_start = code_start + page_size;
186 /* Build the DOS and NT headers */
188 dos->e_magic = IMAGE_DOS_SIGNATURE;
189 dos->e_lfanew = sizeof(*dos);
191 *nt = *nt_descr;
193 nt->FileHeader.NumberOfSections = nb_sections;
194 nt->OptionalHeader.SizeOfCode = data_start - code_start;
195 nt->OptionalHeader.SizeOfInitializedData = 0;
196 nt->OptionalHeader.SizeOfUninitializedData = 0;
197 nt->OptionalHeader.ImageBase = (DWORD)addr;
199 fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
201 /* Build the code section */
203 strcpy( sec->Name, ".text" );
204 sec->SizeOfRawData = data_start - code_start;
205 sec->Misc.VirtualSize = sec->SizeOfRawData;
206 sec->VirtualAddress = code_start - addr;
207 sec->PointerToRawData = code_start - addr;
208 sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
209 sec++;
211 /* Build the data section */
213 strcpy( sec->Name, ".data" );
214 sec->SizeOfRawData = 0;
215 sec->Misc.VirtualSize = sec->SizeOfRawData;
216 sec->VirtualAddress = data_start - addr;
217 sec->PointerToRawData = data_start - addr;
218 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
219 IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
220 sec++;
222 /* Build the import directory */
224 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
225 if (dir->Size)
227 IMAGE_IMPORT_DESCRIPTOR *imports = (void *)dir->VirtualAddress;
228 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
229 /* we can fixup everything at once since we only have pointers and 0 values */
230 fixup_rva_ptrs( imports, addr, dir->Size / sizeof(void*) );
233 /* Build the resource directory */
235 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
236 if (dir->Size)
238 void *ptr = (void *)dir->VirtualAddress;
239 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
240 fixup_resources( ptr, ptr, addr );
243 /* Build the export directory */
245 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
246 if (dir->Size)
248 IMAGE_EXPORT_DIRECTORY *exports = (void *)dir->VirtualAddress;
249 fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
250 fixup_rva_ptrs( (void *)exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
251 fixup_rva_ptrs( (void *)exports->AddressOfNames, addr, exports->NumberOfNames );
252 fixup_rva_ptrs( &exports->Name, addr, 1 );
253 fixup_rva_ptrs( &exports->AddressOfFunctions, addr, 1 );
254 fixup_rva_ptrs( &exports->AddressOfNames, addr, 1 );
255 fixup_rva_ptrs( &exports->AddressOfNameOrdinals, addr, 1 );
257 return addr;
261 /***********************************************************************
262 * __wine_dll_register
264 * Register a built-in DLL descriptor.
266 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
268 if (load_dll_callback) load_dll_callback( map_dll(header), filename );
269 else
271 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
272 main_exe = header;
273 else
275 assert( nb_dlls < MAX_DLLS );
276 builtin_dlls[nb_dlls].nt = header;
277 builtin_dlls[nb_dlls].filename = filename;
278 nb_dlls++;
284 /***********************************************************************
285 * wine_dll_set_callback
287 * Set the callback function for dll loading, and call it
288 * for all dlls that were implicitly loaded already.
290 void wine_dll_set_callback( load_dll_callback_t load )
292 int i;
293 load_dll_callback = load;
294 for (i = 0; i < nb_dlls; i++)
296 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
297 if (!nt) continue;
298 builtin_dlls[i].nt = NULL;
299 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
301 nb_dlls = 0;
302 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
306 /***********************************************************************
307 * wine_dll_load
309 * Load a builtin dll.
311 void *wine_dll_load( const char *filename, char *error, int errorsize )
313 int i;
315 /* callback must have been set already */
316 assert( load_dll_callback );
318 /* check if we have it in the list */
319 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
320 for (i = 0; i < nb_dlls; i++)
322 if (!builtin_dlls[i].nt) continue;
323 if (!strcmp( builtin_dlls[i].filename, filename ))
325 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
326 builtin_dlls[i].nt = NULL;
327 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
328 return (void *)1;
331 return dlopen_dll( filename, error, errorsize );
335 /***********************************************************************
336 * wine_dll_unload
338 * Unload a builtin dll.
340 void wine_dll_unload( void *handle )
342 if (handle != (void *)1)
343 wine_dlclose( handle, NULL, 0 );
347 /***********************************************************************
348 * wine_dll_load_main_exe
350 * Try to load the .so for the main exe, optionally searching for it in PATH.
352 void *wine_dll_load_main_exe( const char *name, int search_path, char *error, int errorsize )
354 void *ret = NULL;
355 const char *path = NULL;
356 if (search_path) path = getenv( "PATH" );
358 if (!path)
360 /* no path, try only the specified name */
361 ret = wine_dlopen( name, RTLD_NOW, error, errorsize );
363 else
365 char buffer[128], *tmp = buffer;
366 size_t namelen = strlen(name);
367 size_t pathlen = strlen(path);
369 if (namelen + pathlen + 2 > sizeof(buffer)) tmp = malloc( namelen + pathlen + 2 );
370 if (tmp)
372 char *basename = tmp + pathlen;
373 *basename = '/';
374 strcpy( basename + 1, name );
375 for (;;)
377 int len;
378 const char *p = strchr( path, ':' );
379 if (!p) p = path + strlen(path);
380 if ((len = p - path) > 0)
382 memcpy( basename - len, path, len );
383 if ((ret = wine_dlopen( basename - len, RTLD_NOW, error, errorsize ))) break;
385 if (!*p) break;
386 path = p + 1;
388 if (tmp != buffer) free( tmp );
391 return ret;