Use the font charset to obtain a codepage for A->W conversion in the
[wine] / library / loader.c
1 /*
2  * Win32 builtin dlls support
3  *
4  * Copyright 2000 Alexandre Julliard
5  */
6
7 #include "config.h"
8 #include "wine/port.h"
9
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
19
20 #include "winnt.h"
21 #include "wine/library.h"
22
23 #define MAX_DLLS 100
24
25 static struct
26 {
27     const IMAGE_NT_HEADERS *nt;           /* NT header */
28     const char             *filename;     /* DLL file name */
29 } builtin_dlls[MAX_DLLS];
30
31 static int nb_dlls;
32
33 static const IMAGE_NT_HEADERS *main_exe;
34
35 static load_dll_callback_t load_dll_callback;
36
37 static char **dll_paths;
38 static int nb_dll_paths;
39 static int dll_path_maxlen;
40 static int init_done;
41
42
43 /* build the dll load path from the WINEDLLPATH variable */
44 static void build_dll_path(void)
45 {
46     int count = 0;
47     char *p, *path = getenv( "WINEDLLPATH" );
48
49     init_done = 1;
50     if (!path) return;
51     path = strdup(path);
52     p = path;
53     while (*p)
54     {
55         while (*p == ':') p++;
56         if (!*p) break;
57         count++;
58         while (*p && *p != ':') p++;
59     }
60
61     dll_paths = malloc( count * sizeof(*dll_paths) );
62     p = path;
63     nb_dll_paths = 0;
64     while (*p)
65     {
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++;
73     }
74 }
75
76
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 )
80 {
81     int i, namelen = strlen(name);
82     char *buffer, *p, *ext;
83     void *ret = NULL;
84
85     if (!init_done) build_dll_path();
86
87     buffer = malloc( dll_path_maxlen + namelen + 8 );
88
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 );
99
100     for (i = 0; i < nb_dll_paths; i++)
101     {
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;
106     }
107
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;
113 }
114
115
116 /* adjust an array of pointers to make them into RVAs */
117 static inline void fixup_rva_ptrs( void *array, void *base, int count )
118 {
119     void **ptr = (void **)array;
120     while (count--)
121     {
122         if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
123         ptr++;
124     }
125 }
126
127
128 /* fixup RVAs in the resource directory */
129 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, char *root, void *base )
130 {
131     IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
132     int i;
133
134     entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
135     for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
136     {
137         void *ptr = root + entry->u2.s3.OffsetToDirectory;
138         if (entry->u2.s3.DataIsDirectory) fixup_resources( ptr, root, base );
139         else
140         {
141             IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
142             fixup_rva_ptrs( &data->OffsetToData, base, 1 );
143         }
144     }
145 }
146
147
148 /* map a builtin dll in memory and fixup RVAs */
149 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
150 {
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 */
158
159     size_t size = (sizeof(IMAGE_DOS_HEADER)
160                    + sizeof(IMAGE_NT_HEADERS)
161                    + nb_sections * sizeof(IMAGE_SECTION_HEADER));
162
163     assert( size <= page_size );
164
165     if (nt_descr->OptionalHeader.ImageBase)
166     {
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;
170     }
171     else
172     {
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;
176     }
177
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;
182
183     /* HACK! */
184     data_start = code_start + page_size;
185
186     /* Build the DOS and NT headers */
187
188     dos->e_magic  = IMAGE_DOS_SIGNATURE;
189     dos->e_lfanew = sizeof(*dos);
190
191     *nt = *nt_descr;
192
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;
198
199     fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
200
201     /* Build the code section */
202
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++;
210
211     /* Build the data section */
212
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++;
221
222     /* Build the import directory */
223
224     dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
225     if (dir->Size)
226     {
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*) );
231     }
232
233     /* Build the resource directory */
234
235     dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
236     if (dir->Size)
237     {
238         void *ptr = (void *)dir->VirtualAddress;
239         fixup_rva_ptrs( &dir->VirtualAddress, addr, 1 );
240         fixup_resources( ptr, ptr, addr );
241     }
242
243     /* Build the export directory */
244
245     dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
246     if (dir->Size)
247     {
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 );
256     }
257     return addr;
258 }
259
260
261 /***********************************************************************
262  *           __wine_dll_register
263  *
264  * Register a built-in DLL descriptor.
265  */
266 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
267 {
268     if (load_dll_callback) load_dll_callback( map_dll(header), filename );
269     else
270     {
271         if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
272             main_exe = header;
273         else
274         {
275             assert( nb_dlls < MAX_DLLS );
276             builtin_dlls[nb_dlls].nt = header;
277             builtin_dlls[nb_dlls].filename = filename;
278             nb_dlls++;
279         }
280     }
281 }
282
283
284 /***********************************************************************
285  *           wine_dll_set_callback
286  *
287  * Set the callback function for dll loading, and call it
288  * for all dlls that were implicitly loaded already.
289  */
290 void wine_dll_set_callback( load_dll_callback_t load )
291 {
292     int i;
293     load_dll_callback = load;
294     for (i = 0; i < nb_dlls; i++)
295     {
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 );
300     }
301     nb_dlls = 0;
302     if (main_exe) load_dll_callback( map_dll(main_exe), "" );
303 }
304
305
306 /***********************************************************************
307  *           wine_dll_load
308  *
309  * Load a builtin dll.
310  */
311 void *wine_dll_load( const char *filename, char *error, int errorsize )
312 {
313     int i;
314
315     /* callback must have been set already */
316     assert( load_dll_callback );
317
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++)
321     {
322         if (!builtin_dlls[i].nt) continue;
323         if (!strcmp( builtin_dlls[i].filename, filename ))
324         {
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;
329         }
330     }
331     return dlopen_dll( filename, error, errorsize );
332 }
333
334
335 /***********************************************************************
336  *           wine_dll_unload
337  *
338  * Unload a builtin dll.
339  */
340 void wine_dll_unload( void *handle )
341 {
342     if (handle != (void *)1)
343         wine_dlclose( handle, NULL, 0 );
344 }
345
346
347 /***********************************************************************
348  *           wine_dll_load_main_exe
349  *
350  * Try to load the .so for the main exe, optionally searching for it in PATH.
351  */
352 void *wine_dll_load_main_exe( const char *name, int search_path, char *error, int errorsize )
353 {
354     void *ret = NULL;
355     const char *path = NULL;
356     if (search_path) path = getenv( "PATH" );
357
358     if (!path)
359     {
360         /* no path, try only the specified name */
361         ret = wine_dlopen( name, RTLD_NOW, error, errorsize );
362     }
363     else
364     {
365         char buffer[128], *tmp = buffer;
366         size_t namelen = strlen(name);
367         size_t pathlen = strlen(path);
368
369         if (namelen + pathlen + 2 > sizeof(buffer)) tmp = malloc( namelen + pathlen + 2 );
370         if (tmp)
371         {
372             char *basename = tmp + pathlen;
373             *basename = '/';
374             strcpy( basename + 1, name );
375             for (;;)
376             {
377                 int len;
378                 const char *p = strchr( path, ':' );
379                 if (!p) p = path + strlen(path);
380                 if ((len = p - path) > 0)
381                 {
382                     memcpy( basename - len, path, len );
383                     if ((ret = wine_dlopen( basename - len, RTLD_NOW, error, errorsize ))) break;
384                 }
385                 if (!*p) break;
386                 path = p + 1;
387             }
388             if (tmp != buffer) free( tmp );
389         }
390     }
391     return ret;
392 }