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
22 #include "wine/port.h"
28 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
35 #include "wine/library.h"
37 /* argc/argv for the Windows application */
38 int __wine_main_argc
= 0;
39 char **__wine_main_argv
= NULL
;
40 WCHAR
**__wine_main_wargv
= NULL
;
46 const IMAGE_NT_HEADERS
*nt
; /* NT header */
47 const char *filename
; /* DLL file name */
48 } builtin_dlls
[MAX_DLLS
];
52 static const IMAGE_NT_HEADERS
*main_exe
;
54 static load_dll_callback_t load_dll_callback
;
56 static const char **dll_paths
;
57 static int nb_dll_paths
;
58 static int dll_path_maxlen
;
62 /* build the dll load path from the WINEDLLPATH variable */
63 static void build_dll_path(void)
65 static const char * const dlldir
= DLLDIR
;
67 char *p
, *path
= getenv( "WINEDLLPATH" );
73 /* count how many path elements we need */
78 while (*p
== ':') p
++;
81 while (*p
&& *p
!= ':') p
++;
85 dll_paths
= malloc( (count
+1) * sizeof(*dll_paths
) );
93 while (*p
== ':') *p
++ = 0;
95 dll_paths
[nb_dll_paths
] = p
;
96 while (*p
&& *p
!= ':') p
++;
97 if (p
- dll_paths
[nb_dll_paths
] > dll_path_maxlen
)
98 dll_path_maxlen
= p
- dll_paths
[nb_dll_paths
];
103 /* append default dll dir (if not empty) to path */
104 if ((len
= strlen(dlldir
)))
106 if (len
> dll_path_maxlen
) dll_path_maxlen
= len
;
107 dll_paths
[nb_dll_paths
++] = dlldir
;
111 /* check if a given file can be opened */
112 inline static int file_exists( const char *name
)
114 int fd
= open( name
, O_RDONLY
);
115 if (fd
!= -1) close( fd
);
119 /* open a library for a given dll, searching in the dll path
120 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
121 static void *dlopen_dll( const char *name
, char *error
, int errorsize
)
123 int i
, namelen
= strlen(name
);
127 if (!init_done
) build_dll_path();
129 buffer
= malloc( dll_path_maxlen
+ namelen
+ 5 );
131 /* store the name at the end of the buffer, followed by .so */
132 p
= buffer
+ dll_path_maxlen
;
134 memcpy( p
, name
, namelen
);
135 strcpy( p
+ namelen
, ".so" );
137 for (i
= 0; i
< nb_dll_paths
; i
++)
139 int len
= strlen(dll_paths
[i
]);
140 p
= buffer
+ dll_path_maxlen
- len
;
141 memcpy( p
, dll_paths
[i
], len
);
142 if ((ret
= wine_dlopen( p
, RTLD_NOW
, error
, errorsize
))) break;
143 if (file_exists( p
)) /* exists but cannot be loaded, return the error */
150 /* now try the default dlopen search path */
152 ret
= wine_dlopen( buffer
+ dll_path_maxlen
+ 1, RTLD_NOW
, error
, errorsize
);
158 /* adjust an array of pointers to make them into RVAs */
159 static inline void fixup_rva_ptrs( void *array
, void *base
, int count
)
161 void **ptr
= (void **)array
;
164 if (*ptr
) *ptr
= (void *)((char *)*ptr
- (char *)base
);
170 /* fixup RVAs in the resource directory */
171 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY
*dir
, char *root
, void *base
)
173 IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
176 entry
= (IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
177 for (i
= 0; i
< dir
->NumberOfNamedEntries
+ dir
->NumberOfIdEntries
; i
++, entry
++)
179 void *ptr
= root
+ entry
->u2
.s3
.OffsetToDirectory
;
180 if (entry
->u2
.s3
.DataIsDirectory
) fixup_resources( ptr
, root
, base
);
183 IMAGE_RESOURCE_DATA_ENTRY
*data
= ptr
;
184 fixup_rva_ptrs( &data
->OffsetToData
, base
, 1 );
190 /* map a builtin dll in memory and fixup RVAs */
191 static void *map_dll( const IMAGE_NT_HEADERS
*nt_descr
)
194 IMAGE_DATA_DIRECTORY
*dir
;
195 IMAGE_DOS_HEADER
*dos
;
196 IMAGE_NT_HEADERS
*nt
;
197 IMAGE_SECTION_HEADER
*sec
;
198 BYTE
*addr
, *code_start
, *data_start
;
199 size_t page_size
= getpagesize();
200 int nb_sections
= 2; /* code + data */
202 size_t size
= (sizeof(IMAGE_DOS_HEADER
)
203 + sizeof(IMAGE_NT_HEADERS
)
204 + nb_sections
* sizeof(IMAGE_SECTION_HEADER
));
206 assert( size
<= page_size
);
208 if (nt_descr
->OptionalHeader
.ImageBase
)
210 addr
= wine_anon_mmap( (void *)nt_descr
->OptionalHeader
.ImageBase
,
211 page_size
, PROT_READ
|PROT_WRITE
, MAP_FIXED
);
212 if (addr
!= (BYTE
*)nt_descr
->OptionalHeader
.ImageBase
) return NULL
;
216 /* this will leak memory; but it should never happen */
217 addr
= wine_anon_mmap( NULL
, page_size
, PROT_READ
|PROT_WRITE
, 0 );
218 if (addr
== (BYTE
*)-1) return NULL
;
221 dos
= (IMAGE_DOS_HEADER
*)addr
;
222 nt
= (IMAGE_NT_HEADERS
*)(dos
+ 1);
223 sec
= (IMAGE_SECTION_HEADER
*)(nt
+ 1);
224 code_start
= addr
+ page_size
;
227 data_start
= code_start
+ page_size
;
229 /* Build the DOS and NT headers */
231 dos
->e_magic
= IMAGE_DOS_SIGNATURE
;
232 dos
->e_lfanew
= sizeof(*dos
);
236 nt
->FileHeader
.NumberOfSections
= nb_sections
;
237 nt
->OptionalHeader
.SizeOfCode
= data_start
- code_start
;
238 nt
->OptionalHeader
.SizeOfInitializedData
= 0;
239 nt
->OptionalHeader
.SizeOfUninitializedData
= 0;
240 nt
->OptionalHeader
.ImageBase
= (DWORD
)addr
;
242 fixup_rva_ptrs( &nt
->OptionalHeader
.AddressOfEntryPoint
, addr
, 1 );
244 /* Build the code section */
246 strcpy( sec
->Name
, ".text" );
247 sec
->SizeOfRawData
= data_start
- code_start
;
248 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
249 sec
->VirtualAddress
= code_start
- addr
;
250 sec
->PointerToRawData
= code_start
- addr
;
251 sec
->Characteristics
= (IMAGE_SCN_CNT_CODE
| IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
);
254 /* Build the data section */
256 strcpy( sec
->Name
, ".data" );
257 sec
->SizeOfRawData
= 0;
258 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
259 sec
->VirtualAddress
= data_start
- addr
;
260 sec
->PointerToRawData
= data_start
- addr
;
261 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
|
262 IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_READ
);
265 /* Build the import directory */
267 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
];
270 IMAGE_IMPORT_DESCRIPTOR
*imports
= (void *)dir
->VirtualAddress
;
271 fixup_rva_ptrs( &dir
->VirtualAddress
, addr
, 1 );
272 /* we can fixup everything at once since we only have pointers and 0 values */
273 fixup_rva_ptrs( imports
, addr
, dir
->Size
/ sizeof(void*) );
276 /* Build the resource directory */
278 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_RESOURCE_DIRECTORY
];
281 void *ptr
= (void *)dir
->VirtualAddress
;
282 fixup_rva_ptrs( &dir
->VirtualAddress
, addr
, 1 );
283 fixup_resources( ptr
, ptr
, addr
);
286 /* Build the export directory */
288 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_EXPORT_DIRECTORY
];
291 IMAGE_EXPORT_DIRECTORY
*exports
= (void *)dir
->VirtualAddress
;
292 fixup_rva_ptrs( &dir
->VirtualAddress
, addr
, 1 );
293 fixup_rva_ptrs( (void *)exports
->AddressOfFunctions
, addr
, exports
->NumberOfFunctions
);
294 fixup_rva_ptrs( (void *)exports
->AddressOfNames
, addr
, exports
->NumberOfNames
);
295 fixup_rva_ptrs( &exports
->Name
, addr
, 1 );
296 fixup_rva_ptrs( &exports
->AddressOfFunctions
, addr
, 1 );
297 fixup_rva_ptrs( &exports
->AddressOfNames
, addr
, 1 );
298 fixup_rva_ptrs( &exports
->AddressOfNameOrdinals
, addr
, 1 );
301 #else /* HAVE_MMAP */
303 #endif /* HAVE_MMAP */
307 /***********************************************************************
308 * __wine_dll_register
310 * Register a built-in DLL descriptor.
312 void __wine_dll_register( const IMAGE_NT_HEADERS
*header
, const char *filename
)
314 if (load_dll_callback
) load_dll_callback( map_dll(header
), filename
);
317 if (!(header
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
321 assert( nb_dlls
< MAX_DLLS
);
322 builtin_dlls
[nb_dlls
].nt
= header
;
323 builtin_dlls
[nb_dlls
].filename
= filename
;
330 /***********************************************************************
331 * wine_dll_set_callback
333 * Set the callback function for dll loading, and call it
334 * for all dlls that were implicitly loaded already.
336 void wine_dll_set_callback( load_dll_callback_t load
)
339 load_dll_callback
= load
;
340 for (i
= 0; i
< nb_dlls
; i
++)
342 const IMAGE_NT_HEADERS
*nt
= builtin_dlls
[i
].nt
;
344 builtin_dlls
[i
].nt
= NULL
;
345 load_dll_callback( map_dll(nt
), builtin_dlls
[i
].filename
);
348 if (main_exe
) load_dll_callback( map_dll(main_exe
), "" );
352 /***********************************************************************
355 * Load a builtin dll.
357 void *wine_dll_load( const char *filename
, char *error
, int errorsize
)
361 /* callback must have been set already */
362 assert( load_dll_callback
);
364 /* check if we have it in the list */
365 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
366 for (i
= 0; i
< nb_dlls
; i
++)
368 if (!builtin_dlls
[i
].nt
) continue;
369 if (!strcmp( builtin_dlls
[i
].filename
, filename
))
371 const IMAGE_NT_HEADERS
*nt
= builtin_dlls
[i
].nt
;
372 builtin_dlls
[i
].nt
= NULL
;
373 load_dll_callback( map_dll(nt
), builtin_dlls
[i
].filename
);
377 return dlopen_dll( filename
, error
, errorsize
);
381 /***********************************************************************
384 * Unload a builtin dll.
386 void wine_dll_unload( void *handle
)
388 if (handle
!= (void *)1)
389 wine_dlclose( handle
, NULL
, 0 );
393 /***********************************************************************
394 * wine_dll_load_main_exe
396 * Try to load the .so for the main exe, optionally searching for it in PATH.
398 void *wine_dll_load_main_exe( const char *name
, int search_path
, char *error
, int errorsize
)
401 const char *path
= NULL
;
402 if (search_path
) path
= getenv( "PATH" );
406 /* no path, try only the specified name */
407 ret
= wine_dlopen( name
, RTLD_NOW
, error
, errorsize
);
411 char buffer
[128], *tmp
= buffer
;
412 size_t namelen
= strlen(name
);
413 size_t pathlen
= strlen(path
);
415 if (namelen
+ pathlen
+ 2 > sizeof(buffer
)) tmp
= malloc( namelen
+ pathlen
+ 2 );
418 char *basename
= tmp
+ pathlen
;
420 strcpy( basename
+ 1, name
);
424 const char *p
= strchr( path
, ':' );
425 if (!p
) p
= path
+ strlen(path
);
426 if ((len
= p
- path
) > 0)
428 memcpy( basename
- len
, path
, len
);
429 if ((ret
= wine_dlopen( basename
- len
, RTLD_NOW
, error
, errorsize
))) break;
434 if (tmp
!= buffer
) free( tmp
);