ntdll: Fix a couple of compiler warnings on ARM64EC.
[wine.git] / loader / main.c
blobc258e0251830758e8ed698f215735e44ec6e5935
1 /*
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
21 #include "config.h"
23 #include <fcntl.h>
24 #include <pthread.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <dlfcn.h>
32 #include <limits.h>
33 #ifdef HAVE_SYS_SYSCTL_H
34 # include <sys/sysctl.h>
35 #endif
37 #include "main.h"
39 extern char **environ;
41 /* the preloader will set this variable */
42 const __attribute((visibility("default"))) 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 );
49 if (fullpath)
51 p = strrchr( fullpath, '/' );
52 if (p == fullpath) p++;
53 if (p) *p = 0;
55 return fullpath;
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 );
63 char *ret;
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;
70 return ret;
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 );
82 return ret;
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 ))
94 return path;
95 free( path );
96 #endif
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 ))
108 free( path );
109 return name;
111 free( name );
113 free( path );
114 return NULL;
116 return argv0;
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 );
123 free( path );
124 return handle;
127 static void *load_ntdll( char *argv0 )
129 #ifdef __i386__
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/"
137 #else
138 #define SO_DIR ""
139 #endif
140 const char *self = get_self_exe( argv0 );
141 char *path, *p;
142 void *handle = NULL;
144 if (self && ((path = realpath_dirname( self ))))
146 if ((p = remove_tail( path, "/loader" )))
148 handle = try_dlopen( p, "dlls/ntdll/ntdll.so" );
149 free( p );
151 else handle = try_dlopen( path, BIN_TO_DLLDIR "/" SO_DIR "ntdll.so" );
152 free( path );
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" );
162 if (handle) break;
164 free( path );
167 if (!handle && !self) handle = try_dlopen( DLLDIR, SO_DIR "ntdll.so" );
169 return handle;
173 /**********************************************************************
174 * main
176 int main( int argc, char *argv[] )
178 void *handle;
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" );
185 exit(1);
188 fprintf( stderr, "wine: could not load ntdll.so: %s\n", dlerror() );
189 pthread_detach( pthread_self() ); /* force importing libpthread for OpenGL */
190 exit(1);