Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/loader/main.c
4388 views
1
/*
2
* Emulator initialisation code
3
*
4
* Copyright 2000 Alexandre Julliard
5
*
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.
10
*
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.
15
*
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
19
*/
20
21
#include "config.h"
22
23
#include <fcntl.h>
24
#include <pthread.h>
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <string.h>
28
#include <sys/mman.h>
29
#include <sys/types.h>
30
#include <sys/stat.h>
31
#include <unistd.h>
32
#include <dlfcn.h>
33
#include <limits.h>
34
#ifdef HAVE_SYS_SYSCTL_H
35
# include <sys/sysctl.h>
36
#endif
37
#ifdef __APPLE__
38
# include <mach-o/dyld.h>
39
#endif
40
41
#include "main.h"
42
43
#if defined(__APPLE__) && defined(__x86_64__) && !defined(HAVE_WINE_PRELOADER)
44
45
/* Not using the preloader on x86_64:
46
* Reserve the same areas as the preloader does, but using zero-fill sections
47
* (the only way to prevent system frameworks from using them, including allocations
48
* before main() runs).
49
*/
50
__asm__(".zerofill WINE_RESERVE,WINE_RESERVE");
51
static char __wine_reserve[0x1fffff000] __attribute__((section("WINE_RESERVE, WINE_RESERVE")));
52
53
__asm__(".zerofill WINE_TOP_DOWN,WINE_TOP_DOWN");
54
static char __wine_top_down[0x001ff0000] __attribute__((section("WINE_TOP_DOWN, WINE_TOP_DOWN")));
55
56
static const struct wine_preload_info preload_info[] =
57
{
58
{ __wine_reserve, sizeof(__wine_reserve) }, /* 0x1000 - 0x200000000: low 8GB */
59
{ __wine_top_down, sizeof(__wine_top_down) }, /* 0x7ff000000000 - 0x7ff001ff0000: top-down allocations + virtual heap */
60
{ 0, 0 } /* end of list */
61
};
62
63
const __attribute((visibility("default"))) struct wine_preload_info *wine_main_preload_info = preload_info;
64
65
static void init_reserved_areas(void)
66
{
67
int i;
68
69
for (i = 0; wine_main_preload_info[i].size != 0; i++)
70
{
71
/* Match how the preloader maps reserved areas: */
72
mmap(wine_main_preload_info[i].addr, wine_main_preload_info[i].size, PROT_NONE,
73
MAP_FIXED | MAP_NORESERVE | MAP_PRIVATE | MAP_ANON, -1, 0);
74
}
75
}
76
77
#else
78
79
/* the preloader will set this variable */
80
const __attribute((visibility("default"))) struct wine_preload_info *wine_main_preload_info = NULL;
81
82
static void init_reserved_areas(void)
83
{
84
}
85
86
#endif
87
88
/* canonicalize path and return its directory name */
89
static char *realpath_dirname( const char *name )
90
{
91
char *p, *fullpath = realpath( name, NULL );
92
93
if (fullpath)
94
{
95
p = strrchr( fullpath, '/' );
96
if (p == fullpath) p++;
97
if (p) *p = 0;
98
}
99
return fullpath;
100
}
101
102
/* if string ends with tail, remove it */
103
static char *remove_tail( const char *str, const char *tail )
104
{
105
size_t len = strlen( str );
106
size_t tail_len = strlen( tail );
107
char *ret;
108
109
if (len < tail_len) return NULL;
110
if (strcmp( str + len - tail_len, tail )) return NULL;
111
ret = malloc( len - tail_len + 1 );
112
memcpy( ret, str, len - tail_len );
113
ret[len - tail_len] = 0;
114
return ret;
115
}
116
117
/* build a path from the specified dir and name */
118
static char *build_path( const char *dir, const char *name )
119
{
120
size_t len = strlen( dir );
121
char *ret = malloc( len + strlen( name ) + 2 );
122
123
memcpy( ret, dir, len );
124
if (len && ret[len - 1] != '/') ret[len++] = '/';
125
strcpy( ret + len, name );
126
return ret;
127
}
128
129
static const char *get_self_exe(void)
130
{
131
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
132
return "/proc/self/exe";
133
#elif defined (__FreeBSD__) || defined(__DragonFly__)
134
static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
135
size_t path_size = PATH_MAX;
136
char *path = malloc( path_size );
137
if (path && !sysctl( pathname, sizeof(pathname)/sizeof(pathname[0]), path, &path_size, NULL, 0 ))
138
return path;
139
free( path );
140
#elif defined(__APPLE__)
141
uint32_t path_size = PATH_MAX;
142
char *path = malloc( path_size );
143
if (path && !_NSGetExecutablePath( path, &path_size ))
144
return path;
145
free( path );
146
#endif
147
return NULL;
148
}
149
150
static void *try_dlopen( const char *argv0 )
151
{
152
char *dir, *path, *p;
153
void *handle;
154
155
if (!argv0) return NULL;
156
if (!(dir = realpath_dirname( argv0 ))) return NULL;
157
158
if ((p = remove_tail( dir, "/loader" )))
159
path = build_path( p, "dlls/ntdll/ntdll.so" );
160
else
161
path = build_path( dir, "ntdll.so" );
162
163
handle = dlopen( path, RTLD_NOW );
164
free( p );
165
free( dir );
166
free( path );
167
return handle;
168
}
169
170
171
/**********************************************************************
172
* main
173
*/
174
int main( int argc, char *argv[] )
175
{
176
void *handle;
177
178
init_reserved_areas();
179
180
if ((handle = try_dlopen( get_self_exe() )) ||
181
(handle = try_dlopen( argv[0] )))
182
{
183
void (*init_func)(int, char **) = dlsym( handle, "__wine_main" );
184
if (init_func) init_func( argc, argv );
185
fprintf( stderr, "wine: __wine_main function not found in ntdll.so\n" );
186
exit(1);
187
}
188
189
fprintf( stderr, "wine: could not load ntdll.so: %s\n", dlerror() );
190
pthread_detach( pthread_self() ); /* force importing libpthread for OpenGL */
191
exit(1);
192
}
193
194