Silence one FIXME(). Implement ITypeInfo::GetDocumentation for
[wine.git] / library / loader.c
blob54e01fba831d95703b3808b36e68eec9ef12ac8c
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 #include "winnt.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;
41 #define MAX_DLLS 100
43 static struct
45 const IMAGE_NT_HEADERS *nt; /* NT header */
46 const char *filename; /* DLL file name */
47 } builtin_dlls[MAX_DLLS];
49 static int nb_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;
58 static int init_done;
61 /* build the dll load path from the WINEDLLPATH variable */
62 static void build_dll_path(void)
64 static const char * const dlldir = DLLDIR;
65 int len, count = 0;
66 char *p, *path = getenv( "WINEDLLPATH" );
68 init_done = 1;
70 if (path)
72 /* count how many path elements we need */
73 path = strdup(path);
74 p = path;
75 while (*p)
77 while (*p == ':') p++;
78 if (!*p) break;
79 count++;
80 while (*p && *p != ':') p++;
84 dll_paths = malloc( (count+1) * sizeof(*dll_paths) );
86 if (count)
88 p = path;
89 nb_dll_paths = 0;
90 while (*p)
92 while (*p == ':') *p++ = 0;
93 if (!*p) break;
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];
98 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 );
115 return (fd != -1);
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);
123 char *buffer, *p;
124 void *ret = NULL;
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;
132 *p++ = '/';
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;
145 else
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 */
151 free( buffer );
152 return ret;
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;
160 while (count--)
162 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
163 ptr++;
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 */
175 while (count--)
177 if (*ptr >= (void *)0x10000) *ptr = (void *)((char *)*ptr - (char *)base);
178 else if (*ptr) *ptr = (void *)(0x80000000 | (unsigned int)*ptr);
179 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;
188 int i;
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 );
195 else
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 )
207 #ifdef HAVE_MMAP
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;
231 /* HACK! */
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);
239 *nt = *nt_descr;
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);
257 sec++;
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);
268 sec++;
270 /* Build the import directory */
272 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
273 if (dir->Size)
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];
283 if (dir->Size)
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];
293 if (dir->Size)
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 );
304 return addr;
305 #else /* HAVE_MMAP */
306 return NULL;
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 );
319 else
321 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
322 main_exe = header;
323 else
325 assert( nb_dlls < MAX_DLLS );
326 builtin_dlls[nb_dlls].nt = header;
327 builtin_dlls[nb_dlls].filename = filename;
328 nb_dlls++;
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 )
342 int i;
343 load_dll_callback = load;
344 for (i = 0; i < nb_dlls; i++)
346 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
347 if (!nt) continue;
348 builtin_dlls[i].nt = NULL;
349 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
351 nb_dlls = 0;
352 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
356 /***********************************************************************
357 * wine_dll_load
359 * Load a builtin dll.
361 void *wine_dll_load( const char *filename, char *error, int errorsize )
363 int i;
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 );
378 return (void *)1;
381 return dlopen_dll( filename, error, errorsize, 0 );
385 /***********************************************************************
386 * wine_dll_unload
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 );