2 * Emulator initialisation code
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
28 #include <sys/types.h>
33 #ifdef HAVE_SYS_SYSCTL_H
34 # include <sys/sysctl.h>
39 extern char **environ
;
41 /* the preloader will set this variable */
42 const struct wine_preload_info
*wine_main_preload_info
= NULL
;
44 /* canonicalize path and return its directory name */
45 static char *realpath_dirname( const char *name
)
47 char *p
, *fullpath
= realpath( name
, NULL
);
51 p
= strrchr( fullpath
, '/' );
52 if (p
== fullpath
) p
++;
58 /* if string ends with tail, remove it */
59 static char *remove_tail( const char *str
, const char *tail
)
61 size_t len
= strlen( str
);
62 size_t tail_len
= strlen( tail
);
65 if (len
< tail_len
) return NULL
;
66 if (strcmp( str
+ len
- tail_len
, tail
)) return NULL
;
67 ret
= malloc( len
- tail_len
+ 1 );
68 memcpy( ret
, str
, len
- tail_len
);
69 ret
[len
- tail_len
] = 0;
73 /* build a path from the specified dir and name */
74 static char *build_path( const char *dir
, const char *name
)
76 size_t len
= strlen( dir
);
77 char *ret
= malloc( len
+ strlen( name
) + 2 );
79 memcpy( ret
, dir
, len
);
80 if (len
&& ret
[len
- 1] != '/') ret
[len
++] = '/';
81 strcpy( ret
+ len
, name
);
85 static const char *get_self_exe( char *argv0
)
87 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
88 return "/proc/self/exe";
89 #elif defined (__FreeBSD__) || defined(__DragonFly__)
90 static int pathname
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1 };
91 size_t path_size
= PATH_MAX
;
92 char *path
= malloc( path_size
);
93 if (path
&& !sysctl( pathname
, sizeof(pathname
)/sizeof(pathname
[0]), path
, &path_size
, NULL
, 0 ))
98 if (!strchr( argv0
, '/' )) /* search in PATH */
100 char *p
, *path
= getenv( "PATH" );
102 if (!path
|| !(path
= strdup(path
))) return NULL
;
103 for (p
= strtok( path
, ":" ); p
; p
= strtok( NULL
, ":" ))
105 char *name
= build_path( p
, argv0
);
106 if (!access( name
, X_OK
))
119 static void *try_dlopen( const char *dir
, const char *name
)
121 char *path
= build_path( dir
, name
);
122 void *handle
= dlopen( path
, RTLD_NOW
);
127 static void *load_ntdll( char *argv0
)
130 #define SO_DIR "i386-unix/"
131 #elif defined(__x86_64__)
132 #define SO_DIR "x86_64-unix/"
133 #elif defined(__arm__)
134 #define SO_DIR "arm-unix/"
135 #elif defined(__aarch64__)
136 #define SO_DIR "aarch64-unix/"
140 const char *self
= get_self_exe( argv0
);
144 if (self
&& ((path
= realpath_dirname( self
))))
146 if ((p
= remove_tail( path
, "/loader" )))
148 handle
= try_dlopen( p
, "dlls/ntdll/ntdll.so" );
151 else handle
= try_dlopen( path
, BIN_TO_DLLDIR
"/" SO_DIR
"ntdll.so" );
155 if (!handle
&& (path
= getenv( "WINEDLLPATH" )))
157 path
= strdup( path
);
158 for (p
= strtok( path
, ":" ); p
; p
= strtok( NULL
, ":" ))
160 handle
= try_dlopen( p
, SO_DIR
"ntdll.so" );
161 if (!handle
) handle
= try_dlopen( p
, "ntdll.so" );
167 if (!handle
&& !self
) handle
= try_dlopen( DLLDIR
, SO_DIR
"ntdll.so" );
173 /**********************************************************************
176 int main( int argc
, char *argv
[] )
180 if ((handle
= load_ntdll( argv
[0] )))
182 void (*init_func
)(int, char **, char **) = dlsym( handle
, "__wine_main" );
183 if (init_func
) init_func( argc
, argv
, environ
);
184 fprintf( stderr
, "wine: __wine_main function not found in ntdll.so\n" );
188 fprintf( stderr
, "wine: could not load ntdll.so: %s\n", dlerror() );
189 pthread_detach( pthread_self() ); /* force importing libpthread for OpenGL */