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>
43 #include <crt_externs.h>
44 #define environ (*_NSGetEnviron())
45 #include <CoreFoundation/CoreFoundation.h>
46 #define LoadResource MacLoadResource
47 #define GetCurrentThread MacGetCurrentThread
48 #include <CoreServices/CoreServices.h>
50 #undef GetCurrentThread
53 extern char **environ
;
60 #define NONAMELESSUNION
61 #define NONAMELESSSTRUCT
64 #include "wine/library.h"
66 /* argc/argv for the Windows application */
67 int __wine_main_argc
= 0;
68 char **__wine_main_argv
= NULL
;
69 WCHAR
**__wine_main_wargv
= NULL
;
70 char **__wine_main_environ
= NULL
;
72 struct dll_path_context
74 unsigned int index
; /* current index in the dll path list */
75 char *buffer
; /* buffer used for storing path names */
76 char *name
; /* start of file name part in buffer (including leading slash) */
77 int namelen
; /* length of file name without .so extension */
78 int win16
; /* 16-bit dll search */
85 const IMAGE_NT_HEADERS
*nt
; /* NT header */
86 const char *filename
; /* DLL file name */
87 } builtin_dlls
[MAX_DLLS
];
91 static const IMAGE_NT_HEADERS
*main_exe
;
93 static load_dll_callback_t load_dll_callback
;
95 static const char *build_dir
;
96 static const char *default_dlldir
;
97 static const char **dll_paths
;
98 static unsigned int nb_dll_paths
;
99 static int dll_path_maxlen
;
101 extern void mmap_init(void);
102 extern const char *get_dlldir( const char **default_dlldir
);
104 /* build the dll load path from the WINEDLLPATH variable */
105 static void build_dll_path(void)
108 char *p
, *path
= getenv( "WINEDLLPATH" );
109 const char *dlldir
= get_dlldir( &default_dlldir
);
113 /* count how many path elements we need */
118 while (*p
== ':') p
++;
121 while (*p
&& *p
!= ':') p
++;
125 dll_paths
= malloc( (count
+2) * sizeof(*dll_paths
) );
130 dll_path_maxlen
= strlen(dlldir
);
131 dll_paths
[nb_dll_paths
++] = dlldir
;
133 else if ((build_dir
= wine_get_build_dir()))
135 dll_path_maxlen
= strlen(build_dir
) + sizeof("/programs");
143 while (*p
== ':') *p
++ = 0;
145 dll_paths
[nb_dll_paths
] = p
;
146 while (*p
&& *p
!= ':') p
++;
147 if (p
- dll_paths
[nb_dll_paths
] > dll_path_maxlen
)
148 dll_path_maxlen
= p
- dll_paths
[nb_dll_paths
];
153 /* append default dll dir (if not empty) to path */
154 if ((len
= strlen(default_dlldir
)) > 0)
156 if (len
> dll_path_maxlen
) dll_path_maxlen
= len
;
157 dll_paths
[nb_dll_paths
++] = default_dlldir
;
161 /* check if the library is the correct architecture */
162 /* only returns false for a valid library of the wrong arch */
163 static int check_library_arch( int fd
)
166 struct /* Mach-O header */
169 unsigned int cputype
;
172 if (read( fd
, &header
, sizeof(header
) ) != sizeof(header
)) return 1;
173 if (header
.magic
!= 0xfeedface) return 1;
174 if (sizeof(void *) == sizeof(int)) return !(header
.cputype
>> 24);
175 else return (header
.cputype
>> 24) == 1; /* CPU_ARCH_ABI64 */
177 struct /* ELF header */
179 unsigned char magic
[4];
182 unsigned char version
;
185 if (read( fd
, &header
, sizeof(header
) ) != sizeof(header
)) return 1;
186 if (memcmp( header
.magic
, "\177ELF", 4 )) return 1;
187 if (header
.version
!= 1 /* EV_CURRENT */) return 1;
188 #ifdef WORDS_BIGENDIAN
189 if (header
.data
!= 2 /* ELFDATA2MSB */) return 1;
191 if (header
.data
!= 1 /* ELFDATA2LSB */) return 1;
193 if (sizeof(void *) == sizeof(int)) return header
.class == 1; /* ELFCLASS32 */
194 else return header
.class == 2; /* ELFCLASS64 */
198 /* check if a given file can be opened */
199 static inline int file_exists( const char *name
)
202 int fd
= open( name
, O_RDONLY
);
205 ret
= check_library_arch( fd
);
211 static inline char *prepend( char *buffer
, const char *str
, size_t len
)
213 return memcpy( buffer
- len
, str
, len
);
216 /* get a filename from the next entry in the dll path */
217 static char *next_dll_path( struct dll_path_context
*context
)
219 unsigned int index
= context
->index
++;
220 int namelen
= context
->namelen
;
221 char *path
= context
->name
;
225 case 0: /* try dlls dir with subdir prefix */
226 if (namelen
> 4 && !memcmp( context
->name
+ namelen
- 4, ".dll", 4 )) namelen
-= 4;
227 if (!context
->win16
) path
= prepend( path
, context
->name
, namelen
);
228 path
= prepend( path
, "/dlls", sizeof("/dlls") - 1 );
229 path
= prepend( path
, build_dir
, strlen(build_dir
) );
231 case 1: /* try programs dir with subdir prefix */
234 if (namelen
> 4 && !memcmp( context
->name
+ namelen
- 4, ".exe", 4 )) namelen
-= 4;
235 path
= prepend( path
, context
->name
, namelen
);
236 path
= prepend( path
, "/programs", sizeof("/programs") - 1 );
237 path
= prepend( path
, build_dir
, strlen(build_dir
) );
244 if (index
>= nb_dll_paths
) return NULL
;
245 path
= prepend( path
, dll_paths
[index
], strlen( dll_paths
[index
] ));
251 /* get a filename from the first entry in the dll path */
252 static char *first_dll_path( const char *name
, int win16
, struct dll_path_context
*context
)
255 int namelen
= strlen( name
);
256 const char *ext
= win16
? "16" : ".so";
258 context
->buffer
= malloc( dll_path_maxlen
+ 2 * namelen
+ strlen(ext
) + 3 );
259 context
->index
= build_dir
? 0 : 2; /* if no build dir skip all the build dir magic cases */
260 context
->name
= context
->buffer
+ dll_path_maxlen
+ namelen
+ 1;
261 context
->namelen
= namelen
+ 1;
262 context
->win16
= win16
;
264 /* store the name at the end of the buffer, followed by extension */
267 memcpy( p
, name
, namelen
);
268 strcpy( p
+ namelen
, ext
);
269 return next_dll_path( context
);
273 /* free the dll path context created by first_dll_path */
274 static inline void free_dll_path( struct dll_path_context
*context
)
276 free( context
->buffer
);
280 /* open a library for a given dll, searching in the dll path
281 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
282 static void *dlopen_dll( const char *name
, char *error
, int errorsize
,
283 int test_only
, int *exists
)
285 struct dll_path_context context
;
290 for (path
= first_dll_path( name
, 0, &context
); path
; path
= next_dll_path( &context
))
292 if (!test_only
&& (ret
= wine_dlopen( path
, RTLD_NOW
, error
, errorsize
))) break;
293 if ((*exists
= file_exists( path
))) break; /* exists but cannot be loaded, return the error */
295 free_dll_path( &context
);
300 /* adjust an array of pointers to make them into RVAs */
301 static inline void fixup_rva_ptrs( void *array
, BYTE
*base
, unsigned int count
)
303 void **src
= (void **)array
;
304 DWORD
*dst
= (DWORD
*)array
;
307 *dst
++ = *src
? (BYTE
*)*src
- base
: 0;
312 /* fixup an array of RVAs by adding the specified delta */
313 static inline void fixup_rva_dwords( DWORD
*ptr
, int delta
, unsigned int count
)
317 if (*ptr
) *ptr
+= delta
;
323 /* fixup RVAs in the import directory */
324 static void fixup_imports( IMAGE_IMPORT_DESCRIPTOR
*dir
, BYTE
*base
, int delta
)
330 fixup_rva_dwords( &dir
->u
.OriginalFirstThunk
, delta
, 1 );
331 fixup_rva_dwords( &dir
->Name
, delta
, 1 );
332 fixup_rva_dwords( &dir
->FirstThunk
, delta
, 1 );
333 ptr
= (UINT_PTR
*)(base
+ (dir
->u
.OriginalFirstThunk
? dir
->u
.OriginalFirstThunk
: dir
->FirstThunk
));
336 if (!(*ptr
& IMAGE_ORDINAL_FLAG
)) *ptr
+= delta
;
344 /* fixup RVAs in the export directory */
345 static void fixup_exports( IMAGE_EXPORT_DIRECTORY
*dir
, BYTE
*base
, int delta
)
347 fixup_rva_dwords( &dir
->Name
, delta
, 1 );
348 fixup_rva_dwords( &dir
->AddressOfFunctions
, delta
, 1 );
349 fixup_rva_dwords( &dir
->AddressOfNames
, delta
, 1 );
350 fixup_rva_dwords( &dir
->AddressOfNameOrdinals
, delta
, 1 );
351 fixup_rva_dwords( (DWORD
*)(base
+ dir
->AddressOfNames
), delta
, dir
->NumberOfNames
);
352 fixup_rva_ptrs( (base
+ dir
->AddressOfFunctions
), base
, dir
->NumberOfFunctions
);
356 /* fixup RVAs in the resource directory */
357 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY
*dir
, BYTE
*root
, int delta
)
359 IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
362 entry
= (IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
363 for (i
= 0; i
< dir
->NumberOfNamedEntries
+ dir
->NumberOfIdEntries
; i
++, entry
++)
365 void *ptr
= root
+ entry
->u2
.s2
.OffsetToDirectory
;
366 if (entry
->u2
.s2
.DataIsDirectory
) fixup_resources( ptr
, root
, delta
);
369 IMAGE_RESOURCE_DATA_ENTRY
*data
= ptr
;
370 fixup_rva_dwords( &data
->OffsetToData
, delta
, 1 );
376 /* map a builtin dll in memory and fixup RVAs */
377 static void *map_dll( const IMAGE_NT_HEADERS
*nt_descr
)
380 IMAGE_DATA_DIRECTORY
*dir
;
381 IMAGE_DOS_HEADER
*dos
;
382 IMAGE_NT_HEADERS
*nt
;
383 IMAGE_SECTION_HEADER
*sec
;
385 DWORD code_start
, data_start
, data_end
;
386 const size_t page_size
= sysconf( _SC_PAGESIZE
);
387 const size_t page_mask
= page_size
- 1;
388 int delta
, nb_sections
= 2; /* code + data */
391 size_t size
= (sizeof(IMAGE_DOS_HEADER
)
392 + sizeof(IMAGE_NT_HEADERS
)
393 + nb_sections
* sizeof(IMAGE_SECTION_HEADER
));
395 assert( size
<= page_size
);
397 /* module address must be aligned on 64K boundary */
398 addr
= (BYTE
*)((nt_descr
->OptionalHeader
.ImageBase
+ 0xffff) & ~0xffff);
399 if (wine_anon_mmap( addr
, page_size
, PROT_READ
|PROT_WRITE
, MAP_FIXED
) != addr
) return NULL
;
401 dos
= (IMAGE_DOS_HEADER
*)addr
;
402 nt
= (IMAGE_NT_HEADERS
*)(dos
+ 1);
403 sec
= (IMAGE_SECTION_HEADER
*)(nt
+ 1);
405 /* Build the DOS and NT headers */
407 dos
->e_magic
= IMAGE_DOS_SIGNATURE
;
410 dos
->e_cparhdr
= (sizeof(*dos
)+0xf)/0x10;
412 dos
->e_maxalloc
= 0xffff;
415 dos
->e_lfarlc
= sizeof(*dos
);
416 dos
->e_lfanew
= sizeof(*dos
);
420 delta
= (const BYTE
*)nt_descr
- addr
;
421 code_start
= page_size
;
422 data_start
= delta
& ~page_mask
;
423 data_end
= (nt
->OptionalHeader
.SizeOfImage
+ delta
+ page_mask
) & ~page_mask
;
425 fixup_rva_ptrs( &nt
->OptionalHeader
.AddressOfEntryPoint
, addr
, 1 );
427 nt
->FileHeader
.NumberOfSections
= nb_sections
;
428 nt
->OptionalHeader
.BaseOfCode
= code_start
;
430 nt
->OptionalHeader
.BaseOfData
= data_start
;
432 nt
->OptionalHeader
.SizeOfCode
= data_start
- code_start
;
433 nt
->OptionalHeader
.SizeOfInitializedData
= data_end
- data_start
;
434 nt
->OptionalHeader
.SizeOfUninitializedData
= 0;
435 nt
->OptionalHeader
.SizeOfImage
= data_end
;
436 nt
->OptionalHeader
.ImageBase
= (ULONG_PTR
)addr
;
438 /* Build the code section */
440 memcpy( sec
->Name
, ".text", sizeof(".text") );
441 sec
->SizeOfRawData
= data_start
- code_start
;
442 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
443 sec
->VirtualAddress
= code_start
;
444 sec
->PointerToRawData
= code_start
;
445 sec
->Characteristics
= (IMAGE_SCN_CNT_CODE
| IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
);
448 /* Build the data section */
450 memcpy( sec
->Name
, ".data", sizeof(".data") );
451 sec
->SizeOfRawData
= data_end
- data_start
;
452 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
453 sec
->VirtualAddress
= data_start
;
454 sec
->PointerToRawData
= data_start
;
455 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
|
456 IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_READ
);
459 for (i
= 0; i
< nt
->OptionalHeader
.NumberOfRvaAndSizes
; i
++)
460 fixup_rva_dwords( &nt
->OptionalHeader
.DataDirectory
[i
].VirtualAddress
, delta
, 1 );
462 /* Build the import directory */
464 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
];
467 IMAGE_IMPORT_DESCRIPTOR
*imports
= (void *)(addr
+ dir
->VirtualAddress
);
468 fixup_imports( imports
, addr
, delta
);
471 /* Build the resource directory */
473 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_RESOURCE_DIRECTORY
];
476 void *ptr
= (void *)(addr
+ dir
->VirtualAddress
);
477 fixup_resources( ptr
, ptr
, delta
);
480 /* Build the export directory */
482 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_EXPORT_DIRECTORY
];
485 IMAGE_EXPORT_DIRECTORY
*exports
= (void *)(addr
+ dir
->VirtualAddress
);
486 fixup_exports( exports
, addr
, delta
);
489 #else /* HAVE_MMAP */
491 #endif /* HAVE_MMAP */
495 /***********************************************************************
496 * __wine_get_main_environment
498 * Return an environment pointer to work around lack of environ variable.
499 * Only exported on Mac OS.
501 char **__wine_get_main_environment(void)
507 /***********************************************************************
508 * __wine_dll_register
510 * Register a built-in DLL descriptor.
512 void __wine_dll_register( const IMAGE_NT_HEADERS
*header
, const char *filename
)
514 if (load_dll_callback
) load_dll_callback( map_dll(header
), filename
);
517 if (!(header
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
521 assert( nb_dlls
< MAX_DLLS
);
522 builtin_dlls
[nb_dlls
].nt
= header
;
523 builtin_dlls
[nb_dlls
].filename
= filename
;
530 /***********************************************************************
531 * wine_dll_set_callback
533 * Set the callback function for dll loading, and call it
534 * for all dlls that were implicitly loaded already.
536 void wine_dll_set_callback( load_dll_callback_t load
)
539 load_dll_callback
= load
;
540 for (i
= 0; i
< nb_dlls
; i
++)
542 const IMAGE_NT_HEADERS
*nt
= builtin_dlls
[i
].nt
;
544 builtin_dlls
[i
].nt
= NULL
;
545 load_dll_callback( map_dll(nt
), builtin_dlls
[i
].filename
);
548 if (main_exe
) load_dll_callback( map_dll(main_exe
), "" );
552 /***********************************************************************
555 * Load a builtin dll.
557 void *wine_dll_load( const char *filename
, char *error
, int errorsize
, int *file_exists
)
561 /* callback must have been set already */
562 assert( load_dll_callback
);
564 /* check if we have it in the list */
565 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
566 for (i
= 0; i
< nb_dlls
; i
++)
568 if (!builtin_dlls
[i
].nt
) continue;
569 if (!strcmp( builtin_dlls
[i
].filename
, filename
))
571 const IMAGE_NT_HEADERS
*nt
= builtin_dlls
[i
].nt
;
572 builtin_dlls
[i
].nt
= NULL
;
573 load_dll_callback( map_dll(nt
), builtin_dlls
[i
].filename
);
578 return dlopen_dll( filename
, error
, errorsize
, 0, file_exists
);
582 /***********************************************************************
585 * Unload a builtin dll.
587 void wine_dll_unload( void *handle
)
589 if (handle
!= (void *)1)
590 wine_dlclose( handle
, NULL
, 0 );
594 /***********************************************************************
595 * wine_dll_load_main_exe
597 * Try to load the .so for the main exe.
599 void *wine_dll_load_main_exe( const char *name
, char *error
, int errorsize
,
600 int test_only
, int *file_exists
)
602 return dlopen_dll( name
, error
, errorsize
, test_only
, file_exists
);
606 /***********************************************************************
607 * wine_dll_enum_load_path
609 * Enumerate the dll load path.
611 const char *wine_dll_enum_load_path( unsigned int index
)
613 if (index
>= nb_dll_paths
) return NULL
;
614 return dll_paths
[index
];
618 /***********************************************************************
621 * Retrieve the name of the 32-bit owner dll for a 16-bit dll.
622 * Return 0 if OK, -1 on error.
624 int wine_dll_get_owner( const char *name
, char *buffer
, int size
, int *exists
)
628 struct dll_path_context context
;
632 for (path
= first_dll_path( name
, 1, &context
); path
; path
= next_dll_path( &context
))
634 int fd
= open( path
, O_RDONLY
);
637 int res
= read( fd
, buffer
, size
- 1 );
638 while (res
> 0 && (buffer
[res
-1] == '\n' || buffer
[res
-1] == '\r')) res
--;
646 free_dll_path( &context
);
651 /***********************************************************************
654 * Set a user limit to the maximum allowed value.
656 static void set_max_limit( int limit
)
658 #ifdef HAVE_SETRLIMIT
659 struct rlimit rlimit
;
661 if (!getrlimit( limit
, &rlimit
))
663 rlimit
.rlim_cur
= rlimit
.rlim_max
;
664 if (setrlimit( limit
, &rlimit
) != 0)
666 #if defined(__APPLE__) && defined(RLIMIT_NOFILE) && defined(OPEN_MAX)
667 /* On Leopard, setrlimit(RLIMIT_NOFILE, ...) fails on attempts to set
668 * rlim_cur above OPEN_MAX (even if rlim_max > OPEN_MAX). */
669 if (limit
== RLIMIT_NOFILE
&& rlimit
.rlim_cur
> OPEN_MAX
)
671 rlimit
.rlim_cur
= OPEN_MAX
;
672 setrlimit( limit
, &rlimit
);
682 struct apple_stack_info
688 /***********************************************************************
689 * apple_alloc_thread_stack
691 * Callback for wine_mmap_enum_reserved_areas to allocate space for
692 * the secondary thread's stack.
694 static int apple_alloc_thread_stack( void *base
, size_t size
, void *arg
)
696 struct apple_stack_info
*info
= arg
;
698 /* For mysterious reasons, putting the thread stack at the very top
699 * of the address space causes subsequent execs to fail, even on the
700 * child side of a fork. Avoid the top 16MB. */
701 char * const limit
= (char*)0xff000000;
702 if ((char *)base
>= limit
) return 0;
703 if (size
> limit
- (char*)base
)
704 size
= limit
- (char*)base
;
705 if (size
< info
->desired_size
) return 0;
706 info
->stack
= wine_anon_mmap( (char *)base
+ size
- info
->desired_size
,
707 info
->desired_size
, PROT_READ
|PROT_WRITE
, MAP_FIXED
);
708 return (info
->stack
!= (void *)-1);
711 /***********************************************************************
712 * apple_create_wine_thread
714 * Spin off a secondary thread to complete Wine initialization, leaving
715 * the original thread for the Mac frameworks.
717 * Invoked as a CFRunLoopSource perform callback.
719 static void apple_create_wine_thread( void *init_func
)
725 if (!pthread_attr_init( &attr
))
727 struct apple_stack_info info
;
729 /* Try to put the new thread's stack in the reserved area. If this
730 * fails, just let it go wherever. It'll be a waste of space, but we
732 if (!pthread_attr_getstacksize( &attr
, &info
.desired_size
) &&
733 wine_mmap_enum_reserved_areas( apple_alloc_thread_stack
, &info
, 1 ))
735 wine_mmap_remove_reserved_area( info
.stack
, info
.desired_size
, 0 );
736 pthread_attr_setstackaddr( &attr
, (char*)info
.stack
+ info
.desired_size
);
739 if (!pthread_attr_setdetachstate( &attr
, PTHREAD_CREATE_JOINABLE
) &&
740 !pthread_create( &thread
, &attr
, init_func
, NULL
))
743 pthread_attr_destroy( &attr
);
746 /* Failure is indicated by returning from wine_init(). Stopping
747 * the run loop allows apple_main_thread() and thus wine_init() to
750 CFRunLoopStop( CFRunLoopGetCurrent() );
754 /***********************************************************************
757 * Park the process's original thread in a Core Foundation run loop for
758 * use by the Mac frameworks, especially receiving and handling
759 * distributed notifications. Spin off a new thread for the rest of the
760 * Wine initialization.
762 static void apple_main_thread( void (*init_func
)(void) )
764 CFRunLoopSourceContext source_context
= { 0 };
765 CFRunLoopSourceRef source
;
767 if (!pthread_main_np())
773 /* Multi-processing Services can get confused about the main thread if the
774 * first time it's used is on a secondary thread. Use it here to make sure
775 * that doesn't happen. */
776 MPTaskIsPreemptive(MPCurrentTaskID());
778 /* Give ourselves the best chance of having the distributed notification
779 * center scheduled on this thread's run loop. In theory, it's scheduled
780 * in the first thread to ask for it. */
781 CFNotificationCenterGetDistributedCenter();
783 /* We use this run loop source for two purposes. First, a run loop exits
784 * if it has no more sources scheduled. So, we need at least one source
785 * to keep the run loop running. Second, although it's not critical, it's
786 * preferable for the Wine initialization to not proceed until we know
787 * the run loop is running. So, we signal our source immediately after
788 * adding it and have its callback spin off the Wine thread. */
789 source_context
.info
= init_func
;
790 source_context
.perform
= apple_create_wine_thread
;
791 source
= CFRunLoopSourceCreate( NULL
, 0, &source_context
);
795 CFRunLoopAddSource( CFRunLoopGetCurrent(), source
, kCFRunLoopCommonModes
);
796 CFRunLoopSourceSignal( source
);
799 CFRunLoopRun(); /* Should never return, except on error. */
802 /* If we get here (i.e. return), that indicates failure to our caller. */
809 #ifndef WINE_JAVA_CLASS
810 #define WINE_JAVA_CLASS "org/winehq/wine/WineActivity"
813 static JavaVM
*java_vm
;
814 static jobject java_object
;
816 /* return the Java VM that was used for JNI initialisation */
817 JavaVM
*wine_get_java_vm(void)
822 /* return the Java object that called the wine_init method */
823 jobject
wine_get_java_object(void)
828 /* main Wine initialisation */
829 static jstring
wine_init_jni( JNIEnv
*env
, jobject obj
, jobjectArray cmdline
, jobjectArray environment
)
836 /* get the command line array */
838 argc
= (*env
)->GetArrayLength( env
, cmdline
);
839 for (i
= length
= 0; i
< argc
; i
++)
841 jobject str_obj
= (*env
)->GetObjectArrayElement( env
, cmdline
, i
);
842 length
+= (*env
)->GetStringUTFLength( env
, str_obj
) + 1;
845 argv
= malloc( (argc
+ 1) * sizeof(*argv
) + length
);
846 str
= (char *)(argv
+ argc
+ 1);
847 for (i
= 0; i
< argc
; i
++)
849 jobject str_obj
= (*env
)->GetObjectArrayElement( env
, cmdline
, i
);
850 length
= (*env
)->GetStringUTFLength( env
, str_obj
);
851 (*env
)->GetStringUTFRegion( env
, str_obj
, 0,
852 (*env
)->GetStringLength( env
, str_obj
), str
);
859 /* set the environment variables */
863 int count
= (*env
)->GetArrayLength( env
, environment
);
864 for (i
= 0; i
< count
- 1; i
+= 2)
866 jobject var_obj
= (*env
)->GetObjectArrayElement( env
, environment
, i
);
867 jobject val_obj
= (*env
)->GetObjectArrayElement( env
, environment
, i
+ 1 );
868 const char *var
= (*env
)->GetStringUTFChars( env
, var_obj
, NULL
);
872 const char *val
= (*env
)->GetStringUTFChars( env
, val_obj
, NULL
);
873 setenv( var
, val
, 1 );
874 if (!strcmp( var
, "LD_LIBRARY_PATH" ))
876 void (*update_func
)( const char * ) = dlsym( RTLD_DEFAULT
,
877 "android_update_LD_LIBRARY_PATH" );
878 if (update_func
) update_func( val
);
880 else if (!strcmp( var
, "WINEDEBUGLOG" ))
882 int fd
= open( val
, O_WRONLY
| O_CREAT
| O_APPEND
, 0666 );
889 (*env
)->ReleaseStringUTFChars( env
, val_obj
, val
);
891 else unsetenv( var
);
893 (*env
)->ReleaseStringUTFChars( env
, var_obj
, var
);
897 java_object
= (*env
)->NewGlobalRef( env
, obj
);
901 unsigned short java_fs
= wine_get_fs();
903 wine_init( argc
, argv
, error
, sizeof(error
) );
904 wine_set_fs( java_fs
);
907 wine_init( argc
, argv
, error
, sizeof(error
) );
909 return (*env
)->NewStringUTF( env
, error
);
912 jint
JNI_OnLoad( JavaVM
*vm
, void *reserved
)
914 static const JNINativeMethod method
=
916 "wine_init", "([Ljava/lang/String;[Ljava/lang/String;)Ljava/lang/String;", wine_init_jni
923 if ((*vm
)->AttachCurrentThread( vm
, &env
, NULL
) != JNI_OK
) return JNI_ERR
;
924 if (!(class = (*env
)->FindClass( env
, WINE_JAVA_CLASS
))) return JNI_ERR
;
925 (*env
)->RegisterNatives( env
, class, &method
, 1 );
926 return JNI_VERSION_1_6
;
929 #endif /* __ANDROID__ */
931 /***********************************************************************
934 * Main Wine initialisation.
936 void wine_init( int argc
, char *argv
[], char *error
, int error_size
)
938 struct dll_path_context context
;
941 void (*init_func
)(void);
943 /* force a few limits that are set too low on some platforms */
945 set_max_limit( RLIMIT_NOFILE
);
948 set_max_limit( RLIMIT_AS
);
951 wine_init_argv0_path( argv
[0] );
953 __wine_main_argc
= argc
;
954 __wine_main_argv
= argv
;
955 __wine_main_environ
= __wine_get_main_environment();
958 for (path
= first_dll_path( "ntdll.dll", 0, &context
); path
; path
= next_dll_path( &context
))
960 if ((ntdll
= wine_dlopen( path
, RTLD_NOW
, error
, error_size
)))
962 /* if we didn't use the default dll dir, remove it from the search path */
963 if (default_dlldir
[0] && context
.index
< nb_dll_paths
+ 2) nb_dll_paths
--;
967 free_dll_path( &context
);
970 if (!(init_func
= wine_dlsym( ntdll
, "__wine_process_init", error
, error_size
))) return;
972 apple_main_thread( init_func
);
980 * These functions provide wrappers around dlopen() and associated
981 * functions. They work around a bug in glibc 2.1.x where calling
982 * a dl*() function after a previous dl*() function has failed
983 * without a dlerror() call between the two will cause a crash.
984 * They all take a pointer to a buffer that
985 * will receive the error description (from dlerror()). This
986 * parameter may be NULL if the error description is not required.
993 /***********************************************************************
996 void *wine_dlopen( const char *filename
, int flag
, char *error
, size_t errorsize
)
1003 /* the Mac OS loader pretends to be able to load PE files, so avoid them here */
1004 unsigned char magic
[2];
1005 int fd
= open( filename
, O_RDONLY
);
1008 if (pread( fd
, magic
, 2, 0 ) == 2 && magic
[0] == 'M' && magic
[1] == 'Z')
1010 if (error
&& errorsize
)
1012 static const char msg
[] = "MZ format";
1013 size_t len
= min( errorsize
, sizeof(msg
) );
1014 memcpy( error
, msg
, len
);
1023 dlerror(); dlerror();
1025 if (strchr( filename
, ':' ))
1027 char path
[PATH_MAX
];
1028 /* Solaris' brain damaged dlopen() treats ':' as a path separator */
1029 realpath( filename
, path
);
1030 ret
= dlopen( path
, flag
| RTLD_FIRST
);
1034 ret
= dlopen( filename
, flag
| RTLD_FIRST
);
1036 if (error
&& errorsize
)
1040 size_t len
= strlen(s
);
1041 if (len
>= errorsize
) len
= errorsize
- 1;
1042 memcpy( error
, s
, len
);
1052 static const char msg
[] = "dlopen interface not detected by configure";
1053 size_t len
= min( errorsize
, sizeof(msg
) );
1054 memcpy( error
, msg
, len
);
1061 /***********************************************************************
1064 void *wine_dlsym( void *handle
, const char *symbol
, char *error
, size_t errorsize
)
1069 dlerror(); dlerror();
1070 ret
= dlsym( handle
, symbol
);
1072 if (error
&& errorsize
)
1076 size_t len
= strlen(s
);
1077 if (len
>= errorsize
) len
= errorsize
- 1;
1078 memcpy( error
, s
, len
);
1088 static const char msg
[] = "dlopen interface not detected by configure";
1089 size_t len
= min( errorsize
, sizeof(msg
) );
1090 memcpy( error
, msg
, len
);
1097 /***********************************************************************
1100 int wine_dlclose( void *handle
, char *error
, size_t errorsize
)
1105 dlerror(); dlerror();
1106 ret
= dlclose( handle
);
1108 if (error
&& errorsize
)
1112 size_t len
= strlen(s
);
1113 if (len
>= errorsize
) len
= errorsize
- 1;
1114 memcpy( error
, s
, len
);
1124 static const char msg
[] = "dlopen interface not detected by configure";
1125 size_t len
= min( errorsize
, sizeof(msg
) );
1126 memcpy( error
, msg
, len
);