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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_MMAN_H
35 #ifdef HAVE_SYS_RESOURCE_H
36 # include <sys/resource.h>
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
46 #include "wine/library.h"
49 #include <crt_externs.h>
50 #define environ (*_NSGetEnviron())
52 extern char **environ
;
55 /* argc/argv for the Windows application */
56 int __wine_main_argc
= 0;
57 char **__wine_main_argv
= NULL
;
58 WCHAR
**__wine_main_wargv
= NULL
;
59 char **__wine_main_environ
= NULL
;
61 struct dll_path_context
63 unsigned int index
; /* current index in the dll path list */
64 char *buffer
; /* buffer used for storing path names */
65 char *name
; /* start of file name part in buffer (including leading slash) */
66 int namelen
; /* length of file name without .so extension */
73 const IMAGE_NT_HEADERS
*nt
; /* NT header */
74 const char *filename
; /* DLL file name */
75 } builtin_dlls
[MAX_DLLS
];
79 static const IMAGE_NT_HEADERS
*main_exe
;
81 static load_dll_callback_t load_dll_callback
;
83 static const char *build_dir
;
84 static const char *default_dlldir
;
85 static const char **dll_paths
;
86 static unsigned int nb_dll_paths
;
87 static int dll_path_maxlen
;
89 extern void mmap_init(void);
90 extern void debug_init(void);
91 extern const char *get_dlldir( const char **default_dlldir
);
93 /* build the dll load path from the WINEDLLPATH variable */
94 static void build_dll_path(void)
97 char *p
, *path
= getenv( "WINEDLLPATH" );
98 const char *dlldir
= get_dlldir( &default_dlldir
);
102 /* count how many path elements we need */
107 while (*p
== ':') p
++;
110 while (*p
&& *p
!= ':') p
++;
114 dll_paths
= malloc( (count
+2) * sizeof(*dll_paths
) );
119 dll_path_maxlen
= strlen(dlldir
);
120 dll_paths
[nb_dll_paths
++] = dlldir
;
122 else if ((build_dir
= wine_get_build_dir()))
124 dll_path_maxlen
= strlen(build_dir
) + sizeof("/programs");
132 while (*p
== ':') *p
++ = 0;
134 dll_paths
[nb_dll_paths
] = p
;
135 while (*p
&& *p
!= ':') p
++;
136 if (p
- dll_paths
[nb_dll_paths
] > dll_path_maxlen
)
137 dll_path_maxlen
= p
- dll_paths
[nb_dll_paths
];
142 /* append default dll dir (if not empty) to path */
143 if ((len
= strlen(default_dlldir
)) > 0)
145 if (len
> dll_path_maxlen
) dll_path_maxlen
= len
;
146 dll_paths
[nb_dll_paths
++] = default_dlldir
;
150 /* check if a given file can be opened */
151 static inline int file_exists( const char *name
)
153 int fd
= open( name
, O_RDONLY
);
154 if (fd
!= -1) close( fd
);
158 static inline char *prepend( char *buffer
, const char *str
, size_t len
)
160 return memcpy( buffer
- len
, str
, len
);
163 /* get a filename from the next entry in the dll path */
164 static char *next_dll_path( struct dll_path_context
*context
)
166 unsigned int index
= context
->index
++;
167 int namelen
= context
->namelen
;
168 char *path
= context
->name
;
172 case 0: /* try programs dir for .exe files */
173 if (namelen
> 4 && !memcmp( context
->name
+ namelen
- 4, ".exe", 4 ))
175 path
= prepend( path
, context
->name
, namelen
- 4 );
176 path
= prepend( path
, "/programs", sizeof("/programs") - 1 );
177 path
= prepend( path
, build_dir
, strlen(build_dir
) );
182 case 1: /* try dlls dir with subdir prefix */
183 if (namelen
> 4 && !memcmp( context
->name
+ namelen
- 4, ".dll", 4 )) namelen
-= 4;
184 path
= prepend( path
, context
->name
, namelen
);
185 path
= prepend( path
, "/dlls", sizeof("/dlls") - 1 );
186 path
= prepend( path
, build_dir
, strlen(build_dir
) );
190 if (index
< nb_dll_paths
)
191 return prepend( context
->name
, dll_paths
[index
], strlen( dll_paths
[index
] ));
198 /* get a filename from the first entry in the dll path */
199 static char *first_dll_path( const char *name
, const char *ext
, struct dll_path_context
*context
)
202 int namelen
= strlen( name
);
204 context
->buffer
= malloc( dll_path_maxlen
+ 2 * namelen
+ strlen(ext
) + 3 );
205 context
->index
= build_dir
? 0 : 2; /* if no build dir skip all the build dir magic cases */
206 context
->name
= context
->buffer
+ dll_path_maxlen
+ namelen
+ 1;
207 context
->namelen
= namelen
+ 1;
209 /* store the name at the end of the buffer, followed by extension */
212 memcpy( p
, name
, namelen
);
213 strcpy( p
+ namelen
, ext
);
214 return next_dll_path( context
);
218 /* free the dll path context created by first_dll_path */
219 static inline void free_dll_path( struct dll_path_context
*context
)
221 free( context
->buffer
);
225 /* open a library for a given dll, searching in the dll path
226 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
227 static void *dlopen_dll( const char *name
, char *error
, int errorsize
,
228 int test_only
, int *exists
)
230 struct dll_path_context context
;
235 for (path
= first_dll_path( name
, ".so", &context
); path
; path
= next_dll_path( &context
))
237 if (!test_only
&& (ret
= wine_dlopen( path
, RTLD_NOW
, error
, errorsize
))) break;
238 if ((*exists
= file_exists( path
))) break; /* exists but cannot be loaded, return the error */
240 free_dll_path( &context
);
245 /* adjust an array of pointers to make them into RVAs */
246 static inline void fixup_rva_ptrs( void *array
, BYTE
*base
, unsigned int count
)
248 void **src
= (void **)array
;
249 DWORD
*dst
= (DWORD
*)array
;
252 *dst
++ = *src
? (BYTE
*)*src
- base
: 0;
257 /* fixup an array of RVAs by adding the specified delta */
258 static inline void fixup_rva_dwords( DWORD
*ptr
, int delta
, unsigned int count
)
262 if (*ptr
) *ptr
+= delta
;
268 /* fixup RVAs in the import directory */
269 static void fixup_imports( IMAGE_IMPORT_DESCRIPTOR
*dir
, BYTE
*base
, int delta
)
275 fixup_rva_dwords( &dir
->u
.OriginalFirstThunk
, delta
, 1 );
276 fixup_rva_dwords( &dir
->Name
, delta
, 1 );
277 fixup_rva_dwords( &dir
->FirstThunk
, delta
, 1 );
278 ptr
= (UINT_PTR
*)(base
+ dir
->FirstThunk
);
281 if (!(*ptr
& IMAGE_ORDINAL_FLAG
)) *ptr
+= delta
;
289 /* fixup RVAs in the export directory */
290 static void fixup_exports( IMAGE_EXPORT_DIRECTORY
*dir
, BYTE
*base
, int delta
)
292 fixup_rva_dwords( &dir
->Name
, delta
, 1 );
293 fixup_rva_dwords( &dir
->AddressOfFunctions
, delta
, 1 );
294 fixup_rva_dwords( &dir
->AddressOfNames
, delta
, 1 );
295 fixup_rva_dwords( &dir
->AddressOfNameOrdinals
, delta
, 1 );
296 fixup_rva_dwords( (DWORD
*)(base
+ dir
->AddressOfNames
), delta
, dir
->NumberOfNames
);
297 fixup_rva_ptrs( (base
+ dir
->AddressOfFunctions
), base
, dir
->NumberOfFunctions
);
301 /* fixup RVAs in the resource directory */
302 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY
*dir
, BYTE
*root
, int delta
)
304 IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
307 entry
= (IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
308 for (i
= 0; i
< dir
->NumberOfNamedEntries
+ dir
->NumberOfIdEntries
; i
++, entry
++)
310 void *ptr
= root
+ entry
->u2
.s3
.OffsetToDirectory
;
311 if (entry
->u2
.s3
.DataIsDirectory
) fixup_resources( ptr
, root
, delta
);
314 IMAGE_RESOURCE_DATA_ENTRY
*data
= ptr
;
315 fixup_rva_dwords( &data
->OffsetToData
, delta
, 1 );
321 /* map a builtin dll in memory and fixup RVAs */
322 static void *map_dll( const IMAGE_NT_HEADERS
*nt_descr
)
325 IMAGE_DATA_DIRECTORY
*dir
;
326 IMAGE_DOS_HEADER
*dos
;
327 IMAGE_NT_HEADERS
*nt
;
328 IMAGE_SECTION_HEADER
*sec
;
330 DWORD code_start
, data_start
, data_end
;
331 const size_t page_size
= getpagesize();
332 const size_t page_mask
= page_size
- 1;
333 int delta
, nb_sections
= 2; /* code + data */
336 size_t size
= (sizeof(IMAGE_DOS_HEADER
)
337 + sizeof(IMAGE_NT_HEADERS
)
338 + nb_sections
* sizeof(IMAGE_SECTION_HEADER
));
340 assert( size
<= page_size
);
342 /* module address must be aligned on 64K boundary */
343 addr
= (BYTE
*)((nt_descr
->OptionalHeader
.ImageBase
+ 0xffff) & ~0xffff);
344 if (wine_anon_mmap( addr
, page_size
, PROT_READ
|PROT_WRITE
, MAP_FIXED
) != addr
) return NULL
;
346 dos
= (IMAGE_DOS_HEADER
*)addr
;
347 nt
= (IMAGE_NT_HEADERS
*)(dos
+ 1);
348 sec
= (IMAGE_SECTION_HEADER
*)(nt
+ 1);
350 /* Build the DOS and NT headers */
352 dos
->e_magic
= IMAGE_DOS_SIGNATURE
;
353 dos
->e_cblp
= sizeof(*dos
);
355 dos
->e_cparhdr
= (sizeof(*dos
)+0xf)/0x10;
357 dos
->e_maxalloc
= 0xffff;
360 dos
->e_lfarlc
= sizeof(*dos
);
361 dos
->e_lfanew
= sizeof(*dos
);
365 delta
= (const BYTE
*)nt_descr
- addr
;
366 code_start
= page_size
;
367 data_start
= delta
& ~page_mask
;
368 data_end
= (nt
->OptionalHeader
.SizeOfImage
+ delta
+ page_mask
) & ~page_mask
;
370 fixup_rva_ptrs( &nt
->OptionalHeader
.AddressOfEntryPoint
, addr
, 1 );
372 nt
->FileHeader
.NumberOfSections
= nb_sections
;
373 nt
->OptionalHeader
.BaseOfCode
= code_start
;
375 nt
->OptionalHeader
.BaseOfData
= data_start
;
377 nt
->OptionalHeader
.SizeOfCode
= data_start
- code_start
;
378 nt
->OptionalHeader
.SizeOfInitializedData
= data_end
- data_start
;
379 nt
->OptionalHeader
.SizeOfUninitializedData
= 0;
380 nt
->OptionalHeader
.SizeOfImage
= data_end
;
381 nt
->OptionalHeader
.ImageBase
= (ULONG_PTR
)addr
;
383 /* Build the code section */
385 memcpy( sec
->Name
, ".text", sizeof(".text") );
386 sec
->SizeOfRawData
= data_start
- code_start
;
387 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
388 sec
->VirtualAddress
= code_start
;
389 sec
->PointerToRawData
= code_start
;
390 sec
->Characteristics
= (IMAGE_SCN_CNT_CODE
| IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
);
393 /* Build the data section */
395 memcpy( sec
->Name
, ".data", sizeof(".data") );
396 sec
->SizeOfRawData
= data_end
- data_start
;
397 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
398 sec
->VirtualAddress
= data_start
;
399 sec
->PointerToRawData
= data_start
;
400 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
|
401 IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_READ
);
404 for (i
= 0; i
< nt
->OptionalHeader
.NumberOfRvaAndSizes
; i
++)
405 fixup_rva_dwords( &nt
->OptionalHeader
.DataDirectory
[i
].VirtualAddress
, delta
, 1 );
407 /* Build the import directory */
409 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
];
412 IMAGE_IMPORT_DESCRIPTOR
*imports
= (void *)(addr
+ dir
->VirtualAddress
);
413 fixup_imports( imports
, addr
, delta
);
416 /* Build the resource directory */
418 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_RESOURCE_DIRECTORY
];
421 void *ptr
= (void *)(addr
+ dir
->VirtualAddress
);
422 fixup_resources( ptr
, ptr
, delta
);
425 /* Build the export directory */
427 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_EXPORT_DIRECTORY
];
430 IMAGE_EXPORT_DIRECTORY
*exports
= (void *)(addr
+ dir
->VirtualAddress
);
431 fixup_exports( exports
, addr
, delta
);
434 #else /* HAVE_MMAP */
436 #endif /* HAVE_MMAP */
440 /***********************************************************************
441 * __wine_dll_register
443 * Register a built-in DLL descriptor.
445 void __wine_dll_register( const IMAGE_NT_HEADERS
*header
, const char *filename
)
447 if (load_dll_callback
) load_dll_callback( map_dll(header
), filename
);
450 if (!(header
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
454 assert( nb_dlls
< MAX_DLLS
);
455 builtin_dlls
[nb_dlls
].nt
= header
;
456 builtin_dlls
[nb_dlls
].filename
= filename
;
463 /***********************************************************************
464 * wine_dll_set_callback
466 * Set the callback function for dll loading, and call it
467 * for all dlls that were implicitly loaded already.
469 void wine_dll_set_callback( load_dll_callback_t load
)
472 load_dll_callback
= load
;
473 for (i
= 0; i
< nb_dlls
; i
++)
475 const IMAGE_NT_HEADERS
*nt
= builtin_dlls
[i
].nt
;
477 builtin_dlls
[i
].nt
= NULL
;
478 load_dll_callback( map_dll(nt
), builtin_dlls
[i
].filename
);
481 if (main_exe
) load_dll_callback( map_dll(main_exe
), "" );
485 /***********************************************************************
488 * Load a builtin dll.
490 void *wine_dll_load( const char *filename
, char *error
, int errorsize
, int *file_exists
)
494 /* callback must have been set already */
495 assert( load_dll_callback
);
497 /* check if we have it in the list */
498 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
499 for (i
= 0; i
< nb_dlls
; i
++)
501 if (!builtin_dlls
[i
].nt
) continue;
502 if (!strcmp( builtin_dlls
[i
].filename
, filename
))
504 const IMAGE_NT_HEADERS
*nt
= builtin_dlls
[i
].nt
;
505 builtin_dlls
[i
].nt
= NULL
;
506 load_dll_callback( map_dll(nt
), builtin_dlls
[i
].filename
);
511 return dlopen_dll( filename
, error
, errorsize
, 0, file_exists
);
515 /***********************************************************************
518 * Unload a builtin dll.
520 void wine_dll_unload( void *handle
)
522 if (handle
!= (void *)1)
523 wine_dlclose( handle
, NULL
, 0 );
527 /***********************************************************************
528 * wine_dll_load_main_exe
530 * Try to load the .so for the main exe.
532 void *wine_dll_load_main_exe( const char *name
, char *error
, int errorsize
,
533 int test_only
, int *file_exists
)
535 return dlopen_dll( name
, error
, errorsize
, test_only
, file_exists
);
539 /***********************************************************************
540 * wine_dll_enum_load_path
542 * Enumerate the dll load path.
544 const char *wine_dll_enum_load_path( unsigned int index
)
546 if (index
>= nb_dll_paths
) return NULL
;
547 return dll_paths
[index
];
551 /***********************************************************************
554 * Retrieve the name of the 32-bit owner dll for a 16-bit dll.
555 * Return 0 if OK, -1 on error.
557 int wine_dll_get_owner( const char *name
, char *buffer
, int size
, int *exists
)
561 struct dll_path_context context
;
565 for (path
= first_dll_path( name
, "16", &context
); path
; path
= next_dll_path( &context
))
567 int fd
= open( path
, O_RDONLY
);
570 int res
= read( fd
, buffer
, size
- 1 );
571 while (res
> 0 && (buffer
[res
-1] == '\n' || buffer
[res
-1] == '\r')) res
--;
579 free_dll_path( &context
);
580 if (ret
!= -1) return ret
;
582 /* try old method too for backwards compatibility; will be removed later on */
583 for (path
= first_dll_path( name
, ".so", &context
); path
; path
= next_dll_path( &context
))
585 int res
= readlink( path
, buffer
, size
);
586 if (res
!= -1) /* got a symlink */
589 if (res
< 4 || res
>= size
) break;
591 if (strchr( buffer
, '/' )) break; /* contains a path, not valid */
592 if (strcmp( buffer
+ res
- 3, ".so" )) break; /* does not end in .so, not valid */
593 buffer
[res
- 3] = 0; /* remove .so */
597 if ((*exists
= file_exists( path
))) break; /* exists but not a symlink, return the error */
599 free_dll_path( &context
);
604 /***********************************************************************
607 * Set a user limit to the maximum allowed value.
609 static void set_max_limit( int limit
)
611 #ifdef HAVE_SETRLIMIT
612 struct rlimit rlimit
;
614 if (!getrlimit( limit
, &rlimit
))
616 rlimit
.rlim_cur
= rlimit
.rlim_max
;
617 setrlimit( limit
, &rlimit
);
623 /***********************************************************************
626 * Main Wine initialisation.
628 void wine_init( int argc
, char *argv
[], char *error
, int error_size
)
630 struct dll_path_context context
;
633 void (*init_func
)(void);
635 /* force a few limits that are set too low on some platforms */
637 set_max_limit( RLIMIT_NOFILE
);
640 set_max_limit( RLIMIT_AS
);
643 wine_init_argv0_path( argv
[0] );
645 __wine_main_argc
= argc
;
646 __wine_main_argv
= argv
;
647 __wine_main_environ
= environ
;
651 for (path
= first_dll_path( "ntdll.dll", ".so", &context
); path
; path
= next_dll_path( &context
))
653 if ((ntdll
= wine_dlopen( path
, RTLD_NOW
, error
, error_size
)))
655 /* if we didn't use the default dll dir, remove it from the search path */
656 if (default_dlldir
[0] && context
.index
< nb_dll_paths
+ 2) nb_dll_paths
--;
660 free_dll_path( &context
);
663 if (!(init_func
= wine_dlsym( ntdll
, "__wine_process_init", error
, error_size
))) return;
669 * These functions provide wrappers around dlopen() and associated
670 * functions. They work around a bug in glibc 2.1.x where calling
671 * a dl*() function after a previous dl*() function has failed
672 * without a dlerror() call between the two will cause a crash.
673 * They all take a pointer to a buffer that
674 * will receive the error description (from dlerror()). This
675 * parameter may be NULL if the error description is not required.
682 /***********************************************************************
685 void *wine_dlopen( const char *filename
, int flag
, char *error
, size_t errorsize
)
690 dlerror(); dlerror();
692 if (strchr( filename
, ':' ))
695 /* Solaris' brain damaged dlopen() treats ':' as a path separator */
696 realpath( filename
, path
);
697 ret
= dlopen( path
, flag
| RTLD_FIRST
);
701 ret
= dlopen( filename
, flag
| RTLD_FIRST
);
703 if (error
&& errorsize
)
707 size_t len
= strlen(s
);
708 if (len
>= errorsize
) len
= errorsize
- 1;
709 memcpy( error
, s
, len
);
719 static const char msg
[] = "dlopen interface not detected by configure";
720 size_t len
= min( errorsize
, sizeof(msg
) );
721 memcpy( error
, msg
, len
);
728 /***********************************************************************
731 void *wine_dlsym( void *handle
, const char *symbol
, char *error
, size_t errorsize
)
736 dlerror(); dlerror();
737 ret
= dlsym( handle
, symbol
);
739 if (error
&& errorsize
)
743 size_t len
= strlen(s
);
744 if (len
>= errorsize
) len
= errorsize
- 1;
745 memcpy( error
, s
, len
);
755 static const char msg
[] = "dlopen interface not detected by configure";
756 size_t len
= min( errorsize
, sizeof(msg
) );
757 memcpy( error
, msg
, len
);
764 /***********************************************************************
767 int wine_dlclose( void *handle
, char *error
, size_t errorsize
)
772 dlerror(); dlerror();
773 ret
= dlclose( handle
);
775 if (error
&& errorsize
)
779 size_t len
= strlen(s
);
780 if (len
>= errorsize
) len
= errorsize
- 1;
781 memcpy( error
, s
, len
);
791 static const char msg
[] = "dlopen interface not detected by configure";
792 size_t len
= min( errorsize
, sizeof(msg
) );
793 memcpy( error
, msg
, len
);