2 * Win32 builtin dlls support
4 * Copyright 2000 Alexandre Julliard
14 #include <sys/types.h>
16 #ifdef HAVE_SYS_MMAN_H
21 #include "wine/library.h"
27 const IMAGE_NT_HEADERS *nt; /* NT header */
28 const char *filename; /* DLL file name */
29 } builtin_dlls[MAX_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;
43 /* build the dll load path from the WINEDLLPATH variable */
44 static void build_dll_path(void)
47 char *p, *path = getenv( "WINEDLLPATH" );
55 while (*p == ':') p++;
58 while (*p && *p != ':') p++;
61 dll_paths = malloc( count * sizeof(*dll_paths) );
66 while (*p == ':') *p++ = 0;
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];
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;
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 );
93 memcpy( p, name, namelen+1 );
94 ext = strrchr( p, '.' );
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 */
110 ret = wine_dlopen( buffer + dll_path_maxlen + 1, RTLD_NOW, error, errorsize );
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;
122 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
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;
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 );
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;
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;
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);
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);
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);
222 /* Build the import directory */
224 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
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];
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];
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 );
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 );
271 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
275 assert( nb_dlls < MAX_DLLS );
276 builtin_dlls[nb_dlls].nt = header;
277 builtin_dlls[nb_dlls].filename = filename;
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 )
293 load_dll_callback = load;
294 for (i = 0; i < nb_dlls; i++)
296 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
298 builtin_dlls[i].nt = NULL;
299 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
302 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
306 /***********************************************************************
309 * Load a builtin dll.
311 void *wine_dll_load( const char *filename, char *error, int errorsize )
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 );
331 return dlopen_dll( filename, error, errorsize );
335 /***********************************************************************
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 )
355 const char *path = NULL;
356 if (search_path) path = getenv( "PATH" );
360 /* no path, try only the specified name */
361 ret = wine_dlopen( name, RTLD_NOW, error, errorsize );
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 );
372 char *basename = tmp + pathlen;
374 strcpy( basename + 1, name );
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;
388 if (tmp != buffer) free( tmp );