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
33 #include <sys/types.h>
37 #ifdef HAVE_SYS_SYSCALL_H
38 # include <sys/syscall.h>
42 #ifdef HAVE_MACH_O_LOADER_H
43 #include <mach/thread_status.h>
44 #include <mach-o/loader.h>
45 #include <mach-o/ldsyms.h>
51 /* Rosetta on Apple Silicon allocates memory starting at 0x100000000 (the 4GB line)
52 * before the preloader runs, which prevents any nonrelocatable EXEs with that
53 * base address from running.
55 * This empty linker section forces Rosetta's allocations (currently ~132 MB)
56 * to start at 0x114000000, and they should end below 0x120000000.
58 #if defined(__x86_64__)
59 __asm__(".zerofill WINE_4GB_RESERVE,WINE_4GB_RESERVE,___wine_4gb_reserve,0x14000000");
63 #define LC_MAIN 0x80000028
64 struct entry_point_command
73 static struct wine_preload_info preload_info
[] =
75 /* On macOS, we allocate the low 64k area in two steps because PAGEZERO
76 * might not always be available. */
78 { (void *)0x00000000, 0x00001000 }, /* first page */
79 { (void *)0x00001000, 0x0000f000 }, /* low 64k */
80 { (void *)0x00010000, 0x00100000 }, /* DOS area */
81 { (void *)0x00110000, 0x67ef0000 }, /* low memory area */
82 { (void *)0x7f000000, 0x03000000 }, /* top-down allocations + shared heap + virtual heap */
84 { (void *)0x000000010000, 0x00100000 }, /* DOS area */
85 { (void *)0x000000110000, 0x67ef0000 }, /* low memory area */
86 { (void *)0x00007ff00000, 0x000f0000 }, /* shared user data */
87 { (void *)0x000100000000, 0x14000000 }, /* WINE_4GB_RESERVE section */
88 { (void *)0x7ffd00000000, 0x01ff0000 }, /* top-down allocations + virtual heap */
90 { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */
91 { 0, 0 } /* end of list */
95 * These functions are only called when file is compiled with -fstack-protector.
96 * They are normally provided by libc's startup files, but since we
97 * build the preloader with "-nostartfiles -nodefaultlibs", we have to
98 * provide our own versions, otherwise the linker fails.
100 void *__stack_chk_guard
= 0;
101 void __stack_chk_fail_local(void) { return; }
102 void __stack_chk_fail(void) { return; }
105 * When 'start' is called, stack frame looks like:
112 * | exec_path | extra "apple" parameters start after NULL terminating env array
132 * | argc | argc is always 4 bytes long, even in 64-bit architectures
133 * +-------------+ <- sp
135 * Where arg[i] and env[i] point into the STRING AREA
138 * macOS C runtime 'start':
139 * <https://github.com/apple-oss-distributions/Csu/blob/Csu-88/start.s>
141 * macOS dyld '__dyld_start' (pre-dyld4):
142 * <https://github.com/apple-oss-distributions/dyld/blob/dyld-852.2/src/dyldStartup.s>
147 static const size_t page_size
= 0x1000;
148 static const size_t page_mask
= 0xfff;
149 #define target_mach_header mach_header
150 #define target_segment_command segment_command
151 #define TARGET_LC_SEGMENT LC_SEGMENT
152 #define target_thread_state_t i386_thread_state_t
153 #ifdef __DARWIN_UNIX03
154 #define target_thread_ip(x) (x)->__eip
156 #define target_thread_ip(x) (x)->eip
159 #define SYSCALL_FUNC( name, nr ) \
160 __ASM_GLOBAL_FUNC( name, \
161 "\tmovl $" #nr ",%eax\n" \
164 "\tmovl $-1,%eax\n" \
167 #define SYSCALL_NOERR( name, nr ) \
168 __ASM_GLOBAL_FUNC( name, \
169 "\tmovl $" #nr ",%eax\n" \
173 __ASM_GLOBAL_FUNC( start
,
174 __ASM_CFI("\t.cfi_undefined %eip\n")
175 /* The first 16 bytes are used as a function signature on i386 */
176 "\t.byte 0x6a,0x00\n" /* pushl $0: push a zero for debugger end of frames marker */
177 "\t.byte 0x89,0xe5\n" /* movl %esp,%ebp: pointer to base of kernel frame */
178 "\t.byte 0x83,0xe4,0xf0\n" /* andl $-16,%esp: force SSE alignment */
179 "\t.byte 0x83,0xec,0x10\n" /* subl $16,%esp: room for new argc, argv, & envp, SSE aligned */
180 "\t.byte 0x8b,0x5d,0x04\n" /* movl 4(%ebp),%ebx: pickup argc in %ebx */
181 "\t.byte 0x89,0x5c,0x24,0x00\n" /* movl %ebx,0(%esp): argc to reserved stack word */
183 /* call wld_start(stack, &is_unix_thread) */
184 "\tleal 4(%ebp),%eax\n"
185 "\tmovl %eax,0(%esp)\n" /* stack */
186 "\tleal 8(%esp),%eax\n"
187 "\tmovl %eax,4(%esp)\n" /* &is_unix_thread */
189 "\tcall _wld_start\n"
191 /* jmp based on is_unix_thread */
192 "\tcmpl $0,8(%esp)\n"
195 "\tmovl 4(%ebp),%edi\n" /* %edi = argc */
196 "\tleal 8(%ebp),%esi\n" /* %esi = argv */
197 "\tleal 4(%esi,%edi,4),%edx\n" /* %edx = env */
199 "1:\tmovl (%ecx),%ebx\n"
201 "\torl %ebx,%ebx\n" /* look for the NULL ending the env[] array */
202 "\tjnz 1b\n" /* %ecx = apple data */
205 "\tmovl %edi,0(%esp)\n" /* argc */
206 "\tmovl %esi,4(%esp)\n" /* argv */
207 "\tmovl %edx,8(%esp)\n" /* env */
208 "\tmovl %ecx,12(%esp)\n" /* apple data */
209 "\tcall *%eax\n" /* call main(argc,argv,env,apple) */
210 "\tmovl %eax,(%esp)\n" /* pass result from main() to exit() */
211 "\tcall _wld_exit\n" /* need to use call to keep stack aligned */
215 "\t2:movl %ebp,%esp\n" /* restore the unaligned stack pointer */
216 "\taddl $4,%esp\n" /* remove the debugger end frame marker */
217 "\tmovl $0,%ebp\n" /* restore ebp back to zero */
218 "\tjmpl *%eax\n" ) /* jump to the entry point */
220 #elif defined(__x86_64__)
222 static const size_t page_size
= 0x1000;
223 static const size_t page_mask
= 0xfff;
224 #define target_mach_header mach_header_64
225 #define target_segment_command segment_command_64
226 #define TARGET_LC_SEGMENT LC_SEGMENT_64
227 #define target_thread_state_t x86_thread_state64_t
228 #ifdef __DARWIN_UNIX03
229 #define target_thread_ip(x) (x)->__rip
231 #define target_thread_ip(x) (x)->rip
234 #define SYSCALL_FUNC( name, nr ) \
235 __ASM_GLOBAL_FUNC( name, \
236 "\tmovq %rcx, %r10\n" \
237 "\tmovq $(" #nr "|0x2000000),%rax\n" \
240 "\tmovq $-1,%rax\n" \
243 #define SYSCALL_NOERR( name, nr ) \
244 __ASM_GLOBAL_FUNC( name, \
245 "\tmovq %rcx, %r10\n" \
246 "\tmovq $(" #nr "|0x2000000),%rax\n" \
250 __ASM_GLOBAL_FUNC( start
,
251 __ASM_CFI("\t.cfi_undefined %rip\n")
252 "\tpushq $0\n" /* push a zero for debugger end of frames marker */
253 "\tmovq %rsp,%rbp\n" /* pointer to base of kernel frame */
254 "\tandq $-16,%rsp\n" /* force SSE alignment */
255 "\tsubq $16,%rsp\n" /* room for local variables */
257 /* call wld_start(stack, &is_unix_thread) */
258 "\tleaq 8(%rbp),%rdi\n" /* stack */
259 "\tmovq %rsp,%rsi\n" /* &is_unix_thread */
261 "\tcall _wld_start\n"
263 /* jmp based on is_unix_thread */
264 "\tcmpl $0,0(%rsp)\n"
268 "\tmovq 8(%rbp),%rdi\n" /* %rdi = argc */
269 "\tleaq 16(%rbp),%rsi\n" /* %rsi = argv */
270 "\tleaq 8(%rsi,%rdi,8),%rdx\n" /* %rdx = env */
272 "1:\tmovq (%rcx),%r8\n"
274 "\torq %r8,%r8\n" /* look for the NULL ending the env[] array */
275 "\tjnz 1b\n" /* %rcx = apple data */
277 "\taddq $16,%rsp\n" /* remove local variables */
278 "\tcall *%rax\n" /* call main(argc,argv,env,apple) */
279 "\tmovq %rax,%rdi\n" /* pass result from main() to exit() */
280 "\tcall _wld_exit\n" /* need to use call to keep stack aligned */
284 "\t2:movq %rbp,%rsp\n" /* restore the unaligned stack pointer */
285 "\taddq $8,%rsp\n" /* remove the debugger end frame marker */
286 "\tmovq $0,%rbp\n" /* restore ebp back to zero */
287 "\tjmpq *%rax\n" ) /* jump to the entry point */
290 #error preloader not implemented for this CPU
293 void wld_exit( int code
) __attribute__((noreturn
));
294 SYSCALL_NOERR( wld_exit
, 1 /* SYS_exit */ );
296 ssize_t
wld_write( int fd
, const void *buffer
, size_t len
);
297 SYSCALL_FUNC( wld_write
, 4 /* SYS_write */ );
299 void *wld_mmap( void *start
, size_t len
, int prot
, int flags
, int fd
, off_t offset
);
300 SYSCALL_FUNC( wld_mmap
, 197 /* SYS_mmap */ );
302 void *wld_munmap( void *start
, size_t len
);
303 SYSCALL_FUNC( wld_munmap
, 73 /* SYS_munmap */ );
305 int wld_mincore( void *addr
, size_t length
, unsigned char *vec
);
306 SYSCALL_FUNC( wld_mincore
, 78 /* SYS_mincore */ );
308 static intptr_t (*p_dyld_get_image_slide
)( const struct target_mach_header
* mh
);
310 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
311 MAKE_FUNCPTR(dlopen
);
313 MAKE_FUNCPTR(dladdr
);
316 extern int _dyld_func_lookup( const char *dyld_func_name
, void **address
);
318 /* replacement for libc functions */
320 void * memmove( void *dst
, const void *src
, size_t len
)
329 const char *lasts
= s
+ (len
-1);
330 char *lastd
= d
+ (len
-1);
337 static int wld_strncmp( const char *str1
, const char *str2
, size_t len
)
339 if (len
<= 0) return 0;
340 while ((--len
> 0) && *str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
341 return *str1
- *str2
;
345 * wld_printf - just the basics
347 * %x prints a hex number
349 * %p prints a pointer
351 static int wld_vsprintf(char *buffer
, const char *fmt
, va_list args
)
353 static const char hex_chars
[16] = "0123456789abcdef";
365 unsigned int x
= va_arg( args
, unsigned int );
366 for (i
= 2*sizeof(x
) - 1; i
>= 0; i
--)
367 *str
++ = hex_chars
[(x
>>(i
*4))&0xf];
369 else if (p
[0] == 'l' && p
[1] == 'x')
371 unsigned long x
= va_arg( args
, unsigned long );
372 for (i
= 2*sizeof(x
) - 1; i
>= 0; i
--)
373 *str
++ = hex_chars
[(x
>>(i
*4))&0xf];
378 unsigned long x
= (unsigned long)va_arg( args
, void * );
379 for (i
= 2*sizeof(x
) - 1; i
>= 0; i
--)
380 *str
++ = hex_chars
[(x
>>(i
*4))&0xf];
384 char *s
= va_arg( args
, char * );
398 static __attribute__((format(printf
,1,2))) void wld_printf(const char *fmt
, ... )
404 va_start( args
, fmt
);
405 len
= wld_vsprintf(buffer
, fmt
, args
);
407 wld_write(2, buffer
, len
);
410 static __attribute__((noreturn
,format(printf
,1,2))) void fatal_error(const char *fmt
, ... )
416 va_start( args
, fmt
);
417 len
= wld_vsprintf(buffer
, fmt
, args
);
419 wld_write(2, buffer
, len
);
423 static int preloader_overlaps_range( const void *start
, const void *end
)
425 intptr_t slide
= p_dyld_get_image_slide(&_mh_execute_header
);
426 struct load_command
*cmd
= (struct load_command
*)(&_mh_execute_header
+ 1);
429 for (i
= 0; i
< _mh_execute_header
.ncmds
; ++i
)
431 if (cmd
->cmd
== TARGET_LC_SEGMENT
)
433 struct target_segment_command
*seg
= (struct target_segment_command
*)cmd
;
434 const void *seg_start
= (const void*)(seg
->vmaddr
+ slide
);
435 const void *seg_end
= (const char*)seg_start
+ seg
->vmsize
;
436 static const char reserved_segname
[] = "WINE_4GB_RESERVE";
438 if (!wld_strncmp( seg
->segname
, reserved_segname
, sizeof(reserved_segname
)-1 ))
441 if (end
> seg_start
&& start
<= seg_end
)
443 char segname
[sizeof(seg
->segname
) + 1];
444 memcpy(segname
, seg
->segname
, sizeof(seg
->segname
));
445 segname
[sizeof(segname
) - 1] = 0;
446 wld_printf( "WINEPRELOADRESERVE range %p-%p overlaps preloader %s segment %p-%p\n",
447 start
, end
, segname
, seg_start
, seg_end
);
451 cmd
= (struct load_command
*)((char*)cmd
+ cmd
->cmdsize
);
460 * Reserve a range specified in string format
462 static void preload_reserve( const char *str
)
465 unsigned long result
= 0;
466 void *start
= NULL
, *end
= NULL
;
469 for (p
= str
; *p
; p
++)
471 if (*p
>= '0' && *p
<= '9') result
= result
* 16 + *p
- '0';
472 else if (*p
>= 'a' && *p
<= 'f') result
= result
* 16 + *p
- 'a' + 10;
473 else if (*p
>= 'A' && *p
<= 'F') result
= result
* 16 + *p
- 'A' + 10;
476 if (!first
) goto error
;
477 start
= (void *)(result
& ~page_mask
);
483 if (!first
) end
= (void *)((result
+ page_mask
) & ~page_mask
);
484 else if (result
) goto error
; /* single value '0' is allowed */
487 if (end
<= start
|| preloader_overlaps_range(start
, end
))
490 /* check for overlap with low memory areas */
491 for (i
= 0; preload_info
[i
].size
; i
++)
493 if ((char *)preload_info
[i
].addr
> (char *)0x00110000) break;
494 if ((char *)end
<= (char *)preload_info
[i
].addr
+ preload_info
[i
].size
)
499 if ((char *)start
< (char *)preload_info
[i
].addr
+ preload_info
[i
].size
)
500 start
= (char *)preload_info
[i
].addr
+ preload_info
[i
].size
;
503 while (preload_info
[i
].size
) i
++;
504 preload_info
[i
].addr
= start
;
505 preload_info
[i
].size
= (char *)end
- (char *)start
;
509 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str
);
512 /* remove a range from the preload list */
513 static void remove_preload_range( int i
)
515 while (preload_info
[i
].size
)
517 preload_info
[i
].addr
= preload_info
[i
+1].addr
;
518 preload_info
[i
].size
= preload_info
[i
+1].size
;
523 static void *get_entry_point( struct target_mach_header
*mh
, intptr_t slide
, int *unix_thread
)
525 struct entry_point_command
*entry
;
526 target_thread_state_t
*state
;
527 struct load_command
*cmd
;
530 /* try LC_MAIN first */
531 cmd
= (struct load_command
*)(mh
+ 1);
532 for (i
= 0; i
< mh
->ncmds
; i
++)
534 if (cmd
->cmd
== LC_MAIN
)
536 *unix_thread
= FALSE
;
537 entry
= (struct entry_point_command
*)cmd
;
538 return (char *)mh
+ entry
->entryoff
;
540 cmd
= (struct load_command
*)((char *)cmd
+ cmd
->cmdsize
);
543 /* then try LC_UNIXTHREAD */
544 cmd
= (struct load_command
*)(mh
+ 1);
545 for (i
= 0; i
< mh
->ncmds
; i
++)
547 if (cmd
->cmd
== LC_UNIXTHREAD
)
550 state
= (target_thread_state_t
*)((char *)cmd
+ 16);
551 return (void *)(target_thread_ip(state
) + slide
);
553 cmd
= (struct load_command
*)((char *)cmd
+ cmd
->cmdsize
);
559 static int is_region_empty( struct wine_preload_info
*info
)
561 unsigned char vec
[1024];
562 size_t pos
, size
, block
= 1024 * page_size
;
565 for (pos
= 0; pos
< info
->size
; pos
+= size
)
567 size
= (pos
+ block
<= info
->size
) ? block
: (info
->size
- pos
);
568 if (wld_mincore( (char *)info
->addr
+ pos
, size
, vec
) == -1)
570 if (size
<= page_size
) continue;
571 block
= page_size
; size
= 0; /* retry with smaller block size */
575 for (i
= 0; i
< size
/ page_size
; i
++)
576 if (vec
[i
] & 1) return 0;
583 static int map_region( struct wine_preload_info
*info
)
585 int flags
= MAP_PRIVATE
| MAP_ANON
;
588 if (!info
->addr
) flags
|= MAP_FIXED
;
592 ret
= wld_mmap( info
->addr
, info
->size
, PROT_NONE
, flags
, -1, 0 );
593 if (ret
== info
->addr
) return 1;
594 if (ret
!= (void *)-1) wld_munmap( ret
, info
->size
);
595 if (flags
& MAP_FIXED
) break;
597 /* Some versions of macOS ignore the address hint passed to mmap -
598 * use mincore() to check if its empty and then use MAP_FIXED */
599 if (!is_region_empty( info
)) break;
603 /* don't warn for zero page */
604 if (info
->addr
>= (void *)0x1000)
605 wld_printf( "preloader: Warning: failed to reserve range %p-%p\n",
606 info
->addr
, (char *)info
->addr
+ info
->size
);
610 static inline void get_dyld_func( const char *name
, void **func
)
612 _dyld_func_lookup( name
, func
);
613 if (!*func
) fatal_error( "Failed to get function pointer for %s\n", name
);
616 #define LOAD_POSIX_DYLD_FUNC(f) get_dyld_func( "__dyld_" #f, (void **)&p##f )
617 #define LOAD_MACHO_DYLD_FUNC(f) get_dyld_func( "_" #f, (void **)&p##f )
619 static void fixup_stack( void *stack
)
622 char **argv
, **env
, **apple
, **apple_end
;
625 argv
= (char **)pargc
+ 1;
626 env
= &argv
[*pargc
-1] + 2;
638 /* decrement argc, and move all the data between &argv[1] and apple_end down to start at &argv[0] */
640 memmove(&argv
[0], &argv
[1], (char *)apple_end
- (char *)&argv
[1]);
643 static void set_program_vars( void *stack
, void *mod
)
647 int *wine_NXArgc
= pdlsym( mod
, "NXArgc" );
648 char ***wine_NXArgv
= pdlsym( mod
, "NXArgv" );
649 char ***wine_environ
= pdlsym( mod
, "environ" );
652 argv
= (char **)pargc
+ 1;
653 env
= &argv
[*pargc
-1] + 2;
656 *wine_NXArgc
= *pargc
;
658 wld_printf( "preloader: Warning: failed to set NXArgc\n" );
663 wld_printf( "preloader: Warning: failed to set NXArgv\n" );
668 wld_printf( "preloader: Warning: failed to set environ\n" );
671 void *wld_start( void *stack
, int *is_unix_thread
)
673 struct wine_preload_info builtin_dlls
= { (void *)0x7a000000, 0x02000000 };
674 struct wine_preload_info
**wine_main_preload_info
;
675 char **argv
, **p
, *reserve
= NULL
;
676 struct target_mach_header
*mh
;
682 argv
= (char **)pargc
+ 1;
683 if (*pargc
< 2) fatal_error( "Usage: %s wine_binary [args]\n", argv
[0] );
685 /* skip over the parameters */
686 p
= argv
+ *pargc
+ 1;
688 /* skip over the environment */
691 static const char res
[] = "WINEPRELOADRESERVE=";
692 if (!wld_strncmp( *p
, res
, sizeof(res
)-1 )) reserve
= *p
+ sizeof(res
) - 1;
696 LOAD_POSIX_DYLD_FUNC( dlopen
);
697 LOAD_POSIX_DYLD_FUNC( dlsym
);
698 LOAD_POSIX_DYLD_FUNC( dladdr
);
699 LOAD_MACHO_DYLD_FUNC( _dyld_get_image_slide
);
701 /* reserve memory that Wine needs */
702 if (reserve
) preload_reserve( reserve
);
703 for (i
= 0; preload_info
[i
].size
; i
++)
705 if (!map_region( &preload_info
[i
] ))
707 remove_preload_range( i
);
712 if (!map_region( &builtin_dlls
))
713 builtin_dlls
.size
= 0;
715 /* load the main binary */
716 if (!(mod
= pdlopen( argv
[1], RTLD_NOW
)))
717 fatal_error( "%s: could not load binary\n", argv
[1] );
719 if (builtin_dlls
.size
)
720 wld_munmap( builtin_dlls
.addr
, builtin_dlls
.size
);
722 /* store pointer to the preload info into the appropriate main binary variable */
723 wine_main_preload_info
= pdlsym( mod
, "wine_main_preload_info" );
724 if (wine_main_preload_info
) *wine_main_preload_info
= preload_info
;
725 else wld_printf( "wine_main_preload_info not found\n" );
727 if (!pdladdr( wine_main_preload_info
, &info
) || !(mh
= info
.dli_fbase
))
728 fatal_error( "%s: could not find mach header\n", argv
[1] );
729 if (!(entry
= get_entry_point( mh
, p_dyld_get_image_slide(mh
), is_unix_thread
)))
730 fatal_error( "%s: could not find entry point\n", argv
[1] );
732 /* decrement argc and "remove" argv[0] */
735 /* Set NXArgc, NXArgv, and environ in the new binary.
736 * On different configurations these were either NULL/0 or still had their
737 * values from this preloader's launch.
739 * In particular, environ was not being updated, resulting in environ[0] being lost.
740 * And for LC_UNIXTHREAD binaries on Monterey and later, environ was just NULL.
742 set_program_vars( stack
, mod
);
747 #endif /* __APPLE__ */