include/winnt.h: Fix arguments order for _InterlockedCompareExchange64() MSVC intrinsic.
[wine.git] / loader / preloader_mac.c
blobfc07b7da0edb29744102a2979ffc15cf2fa7e361
1 /*
2 * Preloader for macOS
4 * Copyright (C) 1995,96,97,98,99,2000,2001,2002 Free Software Foundation, Inc.
5 * Copyright (C) 2004 Mike McCormack for CodeWeavers
6 * Copyright (C) 2004 Alexandre Julliard
7 * Copyright (C) 2017 Michael Müller
8 * Copyright (C) 2017 Sebastian Lackner
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #ifdef __APPLE__
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
37 #endif
38 #include <fcntl.h>
39 #ifdef HAVE_SYS_MMAN_H
40 # include <sys/mman.h>
41 #endif
42 #ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
44 #endif
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48 #ifdef HAVE_MACH_O_LOADER_H
49 #include <mach/thread_status.h>
50 #include <mach-o/loader.h>
51 #include <mach-o/ldsyms.h>
52 #endif
54 #include "wine/asm.h"
55 #include "main.h"
57 /* Rosetta on Apple Silicon allocates memory starting at 0x100000000 (the 4GB line)
58 * before the preloader runs, which prevents any nonrelocatable EXEs with that
59 * base address from running.
61 * This empty linker section forces Rosetta's allocations (currently ~132 MB)
62 * to start at 0x114000000, and they should end below 0x120000000.
64 #if defined(__x86_64__)
65 __asm__(".zerofill WINE_4GB_RESERVE,WINE_4GB_RESERVE,___wine_4gb_reserve,0x14000000");
66 #endif
68 #ifndef LC_MAIN
69 #define LC_MAIN 0x80000028
70 struct entry_point_command
72 uint32_t cmd;
73 uint32_t cmdsize;
74 uint64_t entryoff;
75 uint64_t stacksize;
77 #endif
79 static struct wine_preload_info preload_info[] =
81 /* On macOS, we allocate the low 64k area in two steps because PAGEZERO
82 * might not always be available. */
83 #ifdef __i386__
84 { (void *)0x00000000, 0x00001000 }, /* first page */
85 { (void *)0x00001000, 0x0000f000 }, /* low 64k */
86 { (void *)0x00010000, 0x00100000 }, /* DOS area */
87 { (void *)0x00110000, 0x67ef0000 }, /* low memory area */
88 { (void *)0x7f000000, 0x03000000 }, /* top-down allocations + shared heap + virtual heap */
89 #else /* __i386__ */
90 { (void *)0x000000010000, 0x00100000 }, /* DOS area */
91 { (void *)0x000000110000, 0x67ef0000 }, /* low memory area */
92 { (void *)0x00007ff00000, 0x000f0000 }, /* shared user data */
93 { (void *)0x000100000000, 0x14000000 }, /* WINE_4GB_RESERVE section */
94 { (void *)0x7ffd00000000, 0x01ff0000 }, /* top-down allocations + virtual heap */
95 #endif /* __i386__ */
96 { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */
97 { 0, 0 } /* end of list */
101 * These functions are only called when file is compiled with -fstack-protector.
102 * They are normally provided by libc's startup files, but since we
103 * build the preloader with "-nostartfiles -nodefaultlibs", we have to
104 * provide our own versions, otherwise the linker fails.
106 void *__stack_chk_guard = 0;
107 void __stack_chk_fail_local(void) { return; }
108 void __stack_chk_fail(void) { return; }
110 #ifdef __i386__
112 static const size_t page_size = 0x1000;
113 static const size_t page_mask = 0xfff;
114 #define target_mach_header mach_header
115 #define target_segment_command segment_command
116 #define TARGET_LC_SEGMENT LC_SEGMENT
117 #define target_thread_state_t i386_thread_state_t
118 #ifdef __DARWIN_UNIX03
119 #define target_thread_ip(x) (x)->__eip
120 #else
121 #define target_thread_ip(x) (x)->eip
122 #endif
124 #define SYSCALL_FUNC( name, nr ) \
125 __ASM_GLOBAL_FUNC( name, \
126 "\tmovl $" #nr ",%eax\n" \
127 "\tint $0x80\n" \
128 "\tjnb 1f\n" \
129 "\tmovl $-1,%eax\n" \
130 "1:\tret\n" )
132 #define SYSCALL_NOERR( name, nr ) \
133 __ASM_GLOBAL_FUNC( name, \
134 "\tmovl $" #nr ",%eax\n" \
135 "\tint $0x80\n" \
136 "\tret\n" )
138 __ASM_GLOBAL_FUNC( start,
139 __ASM_CFI("\t.cfi_undefined %eip\n")
140 /* The first 16 bytes are used as a function signature on i386 */
141 "\t.byte 0x6a,0x00\n" /* pushl $0 */
142 "\t.byte 0x89,0xe5\n" /* movl %esp,%ebp */
143 "\t.byte 0x83,0xe4,0xf0\n" /* andl $-16,%esp */
144 "\t.byte 0x83,0xec,0x10\n" /* subl $16,%esp */
145 "\t.byte 0x8b,0x5d,0x04\n" /* movl 4(%ebp),%ebx */
146 "\t.byte 0x89,0x5c,0x24,0x00\n" /* movl %ebx,0(%esp) */
148 "\tleal 4(%ebp),%eax\n"
149 "\tmovl %eax,0(%esp)\n" /* stack */
150 "\tleal 8(%esp),%eax\n"
151 "\tmovl %eax,4(%esp)\n" /* &is_unix_thread */
152 "\tmovl $0,(%eax)\n"
153 "\tcall _wld_start\n"
155 "\tmovl 4(%ebp),%edi\n"
156 "\tdecl %edi\n" /* argc */
157 "\tleal 12(%ebp),%esi\n" /* argv */
158 "\tleal 4(%esi,%edi,4),%edx\n" /* env */
159 "\tmovl %edx,%ecx\n" /* apple data */
160 "1:\tmovl (%ecx),%ebx\n"
161 "\tadd $4,%ecx\n"
162 "\torl %ebx,%ebx\n"
163 "\tjnz 1b\n"
165 "\tcmpl $0,8(%esp)\n"
166 "\tjne 2f\n"
168 /* LC_MAIN */
169 "\tmovl %edi,0(%esp)\n" /* argc */
170 "\tmovl %esi,4(%esp)\n" /* argv */
171 "\tmovl %edx,8(%esp)\n" /* env */
172 "\tmovl %ecx,12(%esp)\n" /* apple data */
173 "\tcall *%eax\n"
174 "\tmovl %eax,(%esp)\n"
175 "\tcall _wld_exit\n"
176 "\thlt\n"
178 /* LC_UNIXTHREAD */
179 "2:\tmovl (%ecx),%ebx\n"
180 "\tadd $4,%ecx\n"
181 "\torl %ebx,%ebx\n"
182 "\tjnz 2b\n"
184 "\tsubl %ebp,%ecx\n"
185 "\tsubl $8,%ecx\n"
186 "\tleal 4(%ebp),%esp\n"
187 "\tsubl %ecx,%esp\n"
189 "\tmovl %edi,(%esp)\n" /* argc */
190 "\tleal 4(%esp),%edi\n"
191 "\tshrl $2,%ecx\n"
192 "\tcld\n"
193 "\trep; movsd\n" /* argv, ... */
195 "\tmovl $0,%ebp\n"
196 "\tjmpl *%eax\n" )
198 #elif defined(__x86_64__)
200 static const size_t page_size = 0x1000;
201 static const size_t page_mask = 0xfff;
202 #define target_mach_header mach_header_64
203 #define target_segment_command segment_command_64
204 #define TARGET_LC_SEGMENT LC_SEGMENT_64
205 #define target_thread_state_t x86_thread_state64_t
206 #ifdef __DARWIN_UNIX03
207 #define target_thread_ip(x) (x)->__rip
208 #else
209 #define target_thread_ip(x) (x)->rip
210 #endif
212 #define SYSCALL_FUNC( name, nr ) \
213 __ASM_GLOBAL_FUNC( name, \
214 "\tmovq %rcx, %r10\n" \
215 "\tmovq $(" #nr "|0x2000000),%rax\n" \
216 "\tsyscall\n" \
217 "\tjnb 1f\n" \
218 "\tmovq $-1,%rax\n" \
219 "1:\tret\n" )
221 #define SYSCALL_NOERR( name, nr ) \
222 __ASM_GLOBAL_FUNC( name, \
223 "\tmovq %rcx, %r10\n" \
224 "\tmovq $(" #nr "|0x2000000),%rax\n" \
225 "\tsyscall\n" \
226 "\tret\n" )
228 __ASM_GLOBAL_FUNC( start,
229 __ASM_CFI("\t.cfi_undefined %rip\n")
230 "\tpushq $0\n"
231 "\tmovq %rsp,%rbp\n"
232 "\tandq $-16,%rsp\n"
233 "\tsubq $16,%rsp\n"
235 "\tleaq 8(%rbp),%rdi\n" /* stack */
236 "\tmovq %rsp,%rsi\n" /* &is_unix_thread */
237 "\tmovq $0,(%rsi)\n"
238 "\tcall _wld_start\n"
240 "\tmovq 8(%rbp),%rdi\n"
241 "\tdec %rdi\n" /* argc */
242 "\tleaq 24(%rbp),%rsi\n" /* argv */
243 "\tleaq 8(%rsi,%rdi,8),%rdx\n" /* env */
244 "\tmovq %rdx,%rcx\n" /* apple data */
245 "1:\tmovq (%rcx),%r8\n"
246 "\taddq $8,%rcx\n"
247 "\torq %r8,%r8\n"
248 "\tjnz 1b\n"
250 "\tcmpl $0,0(%rsp)\n"
251 "\tjne 2f\n"
253 /* LC_MAIN */
254 "\taddq $16,%rsp\n"
255 "\tcall *%rax\n"
256 "\tmovq %rax,%rdi\n"
257 "\tcall _wld_exit\n"
258 "\thlt\n"
260 /* LC_UNIXTHREAD */
261 "2:\tmovq (%rcx),%r8\n"
262 "\taddq $8,%rcx\n"
263 "\torq %r8,%r8\n"
264 "\tjnz 2b\n"
266 "\tsubq %rbp,%rcx\n"
267 "\tsubq $16,%rcx\n"
268 "\tleaq 8(%rbp),%rsp\n"
269 "\tsubq %rcx,%rsp\n"
271 "\tmovq %rdi,(%rsp)\n" /* argc */
272 "\tleaq 8(%rsp),%rdi\n"
273 "\tshrq $3,%rcx\n"
274 "\tcld\n"
275 "\trep; movsq\n" /* argv, ... */
277 "\tmovq $0,%rbp\n"
278 "\tjmpq *%rax\n" )
280 #else
281 #error preloader not implemented for this CPU
282 #endif
284 void wld_exit( int code ) __attribute__((noreturn));
285 SYSCALL_NOERR( wld_exit, 1 /* SYS_exit */ );
287 ssize_t wld_write( int fd, const void *buffer, size_t len );
288 SYSCALL_FUNC( wld_write, 4 /* SYS_write */ );
290 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
291 SYSCALL_FUNC( wld_mmap, 197 /* SYS_mmap */ );
293 void *wld_munmap( void *start, size_t len );
294 SYSCALL_FUNC( wld_munmap, 73 /* SYS_munmap */ );
296 int wld_mincore( void *addr, size_t length, unsigned char *vec );
297 SYSCALL_FUNC( wld_mincore, 78 /* SYS_mincore */ );
299 static intptr_t (*p_dyld_get_image_slide)( const struct target_mach_header* mh );
301 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
302 MAKE_FUNCPTR(dlopen);
303 MAKE_FUNCPTR(dlsym);
304 MAKE_FUNCPTR(dladdr);
305 #undef MAKE_FUNCPTR
307 extern int _dyld_func_lookup( const char *dyld_func_name, void **address );
309 /* replacement for libc functions */
311 static int wld_strncmp( const char *str1, const char *str2, size_t len )
313 if (len <= 0) return 0;
314 while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
315 return *str1 - *str2;
319 * wld_printf - just the basics
321 * %x prints a hex number
322 * %s prints a string
323 * %p prints a pointer
325 static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
327 static const char hex_chars[16] = "0123456789abcdef";
328 const char *p = fmt;
329 char *str = buffer;
330 int i;
332 while( *p )
334 if( *p == '%' )
336 p++;
337 if( *p == 'x' )
339 unsigned int x = va_arg( args, unsigned int );
340 for (i = 2*sizeof(x) - 1; i >= 0; i--)
341 *str++ = hex_chars[(x>>(i*4))&0xf];
343 else if (p[0] == 'l' && p[1] == 'x')
345 unsigned long x = va_arg( args, unsigned long );
346 for (i = 2*sizeof(x) - 1; i >= 0; i--)
347 *str++ = hex_chars[(x>>(i*4))&0xf];
348 p++;
350 else if( *p == 'p' )
352 unsigned long x = (unsigned long)va_arg( args, void * );
353 for (i = 2*sizeof(x) - 1; i >= 0; i--)
354 *str++ = hex_chars[(x>>(i*4))&0xf];
356 else if( *p == 's' )
358 char *s = va_arg( args, char * );
359 while(*s)
360 *str++ = *s++;
362 else if( *p == 0 )
363 break;
364 p++;
366 *str++ = *p++;
368 *str = 0;
369 return str - buffer;
372 static __attribute__((format(printf,1,2))) void wld_printf(const char *fmt, ... )
374 va_list args;
375 char buffer[256];
376 int len;
378 va_start( args, fmt );
379 len = wld_vsprintf(buffer, fmt, args );
380 va_end( args );
381 wld_write(2, buffer, len);
384 static __attribute__((noreturn,format(printf,1,2))) void fatal_error(const char *fmt, ... )
386 va_list args;
387 char buffer[256];
388 int len;
390 va_start( args, fmt );
391 len = wld_vsprintf(buffer, fmt, args );
392 va_end( args );
393 wld_write(2, buffer, len);
394 wld_exit(1);
397 static int preloader_overlaps_range( const void *start, const void *end )
399 intptr_t slide = p_dyld_get_image_slide(&_mh_execute_header);
400 struct load_command *cmd = (struct load_command*)(&_mh_execute_header + 1);
401 int i;
403 for (i = 0; i < _mh_execute_header.ncmds; ++i)
405 if (cmd->cmd == TARGET_LC_SEGMENT)
407 struct target_segment_command *seg = (struct target_segment_command*)cmd;
408 const void *seg_start = (const void*)(seg->vmaddr + slide);
409 const void *seg_end = (const char*)seg_start + seg->vmsize;
410 static const char reserved_segname[] = "WINE_4GB_RESERVE";
412 if (!wld_strncmp( seg->segname, reserved_segname, sizeof(reserved_segname)-1 ))
413 continue;
415 if (end > seg_start && start <= seg_end)
417 char segname[sizeof(seg->segname) + 1];
418 memcpy(segname, seg->segname, sizeof(seg->segname));
419 segname[sizeof(segname) - 1] = 0;
420 wld_printf( "WINEPRELOADRESERVE range %p-%p overlaps preloader %s segment %p-%p\n",
421 start, end, segname, seg_start, seg_end );
422 return 1;
425 cmd = (struct load_command*)((char*)cmd + cmd->cmdsize);
428 return 0;
432 * preload_reserve
434 * Reserve a range specified in string format
436 static void preload_reserve( const char *str )
438 const char *p;
439 unsigned long result = 0;
440 void *start = NULL, *end = NULL;
441 int i, first = 1;
443 for (p = str; *p; p++)
445 if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
446 else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
447 else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
448 else if (*p == '-')
450 if (!first) goto error;
451 start = (void *)(result & ~page_mask);
452 result = 0;
453 first = 0;
455 else goto error;
457 if (!first) end = (void *)((result + page_mask) & ~page_mask);
458 else if (result) goto error; /* single value '0' is allowed */
460 /* sanity checks */
461 if (end <= start || preloader_overlaps_range(start, end))
462 start = end = NULL;
464 /* check for overlap with low memory areas */
465 for (i = 0; preload_info[i].size; i++)
467 if ((char *)preload_info[i].addr > (char *)0x00110000) break;
468 if ((char *)end <= (char *)preload_info[i].addr + preload_info[i].size)
470 start = end = NULL;
471 break;
473 if ((char *)start < (char *)preload_info[i].addr + preload_info[i].size)
474 start = (char *)preload_info[i].addr + preload_info[i].size;
477 while (preload_info[i].size) i++;
478 preload_info[i].addr = start;
479 preload_info[i].size = (char *)end - (char *)start;
480 return;
482 error:
483 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
486 /* remove a range from the preload list */
487 static void remove_preload_range( int i )
489 while (preload_info[i].size)
491 preload_info[i].addr = preload_info[i+1].addr;
492 preload_info[i].size = preload_info[i+1].size;
493 i++;
497 static void *get_entry_point( struct target_mach_header *mh, intptr_t slide, int *unix_thread )
499 struct entry_point_command *entry;
500 target_thread_state_t *state;
501 struct load_command *cmd;
502 int i;
504 /* try LC_MAIN first */
505 cmd = (struct load_command *)(mh + 1);
506 for (i = 0; i < mh->ncmds; i++)
508 if (cmd->cmd == LC_MAIN)
510 *unix_thread = FALSE;
511 entry = (struct entry_point_command *)cmd;
512 return (char *)mh + entry->entryoff;
514 cmd = (struct load_command *)((char *)cmd + cmd->cmdsize);
517 /* then try LC_UNIXTHREAD */
518 cmd = (struct load_command *)(mh + 1);
519 for (i = 0; i < mh->ncmds; i++)
521 if (cmd->cmd == LC_UNIXTHREAD)
523 *unix_thread = TRUE;
524 state = (target_thread_state_t *)((char *)cmd + 16);
525 return (void *)(target_thread_ip(state) + slide);
527 cmd = (struct load_command *)((char *)cmd + cmd->cmdsize);
530 return NULL;
533 static int is_region_empty( struct wine_preload_info *info )
535 unsigned char vec[1024];
536 size_t pos, size, block = 1024 * page_size;
537 int i;
539 for (pos = 0; pos < info->size; pos += size)
541 size = (pos + block <= info->size) ? block : (info->size - pos);
542 if (wld_mincore( (char *)info->addr + pos, size, vec ) == -1)
544 if (size <= page_size) continue;
545 block = page_size; size = 0; /* retry with smaller block size */
547 else
549 for (i = 0; i < size / page_size; i++)
550 if (vec[i] & 1) return 0;
554 return 1;
557 static int map_region( struct wine_preload_info *info )
559 int flags = MAP_PRIVATE | MAP_ANON;
560 void *ret;
562 if (!info->addr) flags |= MAP_FIXED;
564 for (;;)
566 ret = wld_mmap( info->addr, info->size, PROT_NONE, flags, -1, 0 );
567 if (ret == info->addr) return 1;
568 if (ret != (void *)-1) wld_munmap( ret, info->size );
569 if (flags & MAP_FIXED) break;
571 /* Some versions of macOS ignore the address hint passed to mmap -
572 * use mincore() to check if its empty and then use MAP_FIXED */
573 if (!is_region_empty( info )) break;
574 flags |= MAP_FIXED;
577 /* don't warn for zero page */
578 if (info->addr >= (void *)0x1000)
579 wld_printf( "preloader: Warning: failed to reserve range %p-%p\n",
580 info->addr, (char *)info->addr + info->size );
581 return 0;
584 static inline void get_dyld_func( const char *name, void **func )
586 _dyld_func_lookup( name, func );
587 if (!*func) fatal_error( "Failed to get function pointer for %s\n", name );
590 #define LOAD_POSIX_DYLD_FUNC(f) get_dyld_func( "__dyld_" #f, (void **)&p##f )
591 #define LOAD_MACHO_DYLD_FUNC(f) get_dyld_func( "_" #f, (void **)&p##f )
593 void *wld_start( void *stack, int *is_unix_thread )
595 struct wine_preload_info builtin_dlls = { (void *)0x7a000000, 0x02000000 };
596 struct wine_preload_info **wine_main_preload_info;
597 char **argv, **p, *reserve = NULL;
598 struct target_mach_header *mh;
599 void *mod, *entry;
600 int *pargc, i;
601 Dl_info info;
603 pargc = stack;
604 argv = (char **)pargc + 1;
605 if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
607 /* skip over the parameters */
608 p = argv + *pargc + 1;
610 /* skip over the environment */
611 while (*p)
613 static const char res[] = "WINEPRELOADRESERVE=";
614 if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
615 p++;
618 LOAD_POSIX_DYLD_FUNC( dlopen );
619 LOAD_POSIX_DYLD_FUNC( dlsym );
620 LOAD_POSIX_DYLD_FUNC( dladdr );
621 LOAD_MACHO_DYLD_FUNC( _dyld_get_image_slide );
623 /* reserve memory that Wine needs */
624 if (reserve) preload_reserve( reserve );
625 for (i = 0; preload_info[i].size; i++)
627 if (!map_region( &preload_info[i] ))
629 remove_preload_range( i );
630 i--;
634 if (!map_region( &builtin_dlls ))
635 builtin_dlls.size = 0;
637 /* load the main binary */
638 if (!(mod = pdlopen( argv[1], RTLD_NOW )))
639 fatal_error( "%s: could not load binary\n", argv[1] );
641 if (builtin_dlls.size)
642 wld_munmap( builtin_dlls.addr, builtin_dlls.size );
644 /* store pointer to the preload info into the appropriate main binary variable */
645 wine_main_preload_info = pdlsym( mod, "wine_main_preload_info" );
646 if (wine_main_preload_info) *wine_main_preload_info = preload_info;
647 else wld_printf( "wine_main_preload_info not found\n" );
649 if (!pdladdr( wine_main_preload_info, &info ) || !(mh = info.dli_fbase))
650 fatal_error( "%s: could not find mach header\n", argv[1] );
651 if (!(entry = get_entry_point( mh, p_dyld_get_image_slide(mh), is_unix_thread )))
652 fatal_error( "%s: could not find entry point\n", argv[1] );
654 return entry;
657 #endif /* __APPLE__ */