msado15: Free recordset filter on close.
[wine.git] / loader / preloader_mac.c
blobce7a14e17aefa9aead8c41ac7405fe133d442d3c
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"
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <sys/mman.h>
37 #ifdef HAVE_SYS_SYSCALL_H
38 # include <sys/syscall.h>
39 #endif
40 #include <unistd.h>
41 #include <dlfcn.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>
46 #endif
48 #include "wine/asm.h"
49 #include "main.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");
60 #endif
62 #ifndef LC_MAIN
63 #define LC_MAIN 0x80000028
64 struct entry_point_command
66 uint32_t cmd;
67 uint32_t cmdsize;
68 uint64_t entryoff;
69 uint64_t stacksize;
71 #endif
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. */
77 #ifdef __i386__
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 */
83 #else /* __i386__ */
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 */
89 #endif /* __i386__ */
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:
108 * | STRING AREA |
109 * +-------------+
110 * | 0 |
111 * +-------------+
112 * | exec_path | extra "apple" parameters start after NULL terminating env array
113 * +-------------+
114 * | 0 |
115 * +-------------+
116 * | env[n] |
117 * +-------------+
120 * +-------------+
121 * | env[0] |
122 * +-------------+
123 * | 0 |
124 * +-------------+
125 * | arg[argc-1] |
126 * +-------------+
129 * +-------------+
130 * | arg[0] |
131 * +-------------+
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
137 * See also:
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>
145 #ifdef __i386__
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
155 #else
156 #define target_thread_ip(x) (x)->eip
157 #endif
159 #define SYSCALL_FUNC( name, nr ) \
160 __ASM_GLOBAL_FUNC( name, \
161 "\tmovl $" #nr ",%eax\n" \
162 "\tint $0x80\n" \
163 "\tjnb 1f\n" \
164 "\tmovl $-1,%eax\n" \
165 "1:\tret\n" )
167 #define SYSCALL_NOERR( name, nr ) \
168 __ASM_GLOBAL_FUNC( name, \
169 "\tmovl $" #nr ",%eax\n" \
170 "\tint $0x80\n" \
171 "\tret\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 */
188 "\tmovl $0,(%eax)\n"
189 "\tcall _wld_start\n"
191 /* jmp based on is_unix_thread */
192 "\tcmpl $0,8(%esp)\n"
193 "\tjne 2f\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 */
198 "\tmovl %edx,%ecx\n"
199 "1:\tmovl (%ecx),%ebx\n"
200 "\tadd $4,%ecx\n"
201 "\torl %ebx,%ebx\n" /* look for the NULL ending the env[] array */
202 "\tjnz 1b\n" /* %ecx = apple data */
204 /* LC_MAIN */
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 */
212 "\thlt\n"
214 /* LC_UNIXTHREAD */
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
230 #else
231 #define target_thread_ip(x) (x)->rip
232 #endif
234 #define SYSCALL_FUNC( name, nr ) \
235 __ASM_GLOBAL_FUNC( name, \
236 "\tmovq %rcx, %r10\n" \
237 "\tmovq $(" #nr "|0x2000000),%rax\n" \
238 "\tsyscall\n" \
239 "\tjnb 1f\n" \
240 "\tmovq $-1,%rax\n" \
241 "1:\tret\n" )
243 #define SYSCALL_NOERR( name, nr ) \
244 __ASM_GLOBAL_FUNC( name, \
245 "\tmovq %rcx, %r10\n" \
246 "\tmovq $(" #nr "|0x2000000),%rax\n" \
247 "\tsyscall\n" \
248 "\tret\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 */
260 "\tmovq $0,(%rsi)\n"
261 "\tcall _wld_start\n"
263 /* jmp based on is_unix_thread */
264 "\tcmpl $0,0(%rsp)\n"
265 "\tjne 2f\n"
267 /* LC_MAIN */
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 */
271 "\tmovq %rdx,%rcx\n"
272 "1:\tmovq (%rcx),%r8\n"
273 "\taddq $8,%rcx\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 */
281 "\thlt\n"
283 /* LC_UNIXTHREAD */
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 */
289 #else
290 #error preloader not implemented for this CPU
291 #endif
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);
312 MAKE_FUNCPTR(dlsym);
313 MAKE_FUNCPTR(dladdr);
314 #undef MAKE_FUNCPTR
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 )
322 char *d = dst;
323 const char *s = src;
324 if (d < s)
325 while (len--)
326 *d++ = *s++;
327 else
329 const char *lasts = s + (len-1);
330 char *lastd = d + (len-1);
331 while (len--)
332 *lastd-- = *lasts--;
334 return dst;
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
348 * %s prints a string
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";
354 const char *p = fmt;
355 char *str = buffer;
356 int i;
358 while( *p )
360 if( *p == '%' )
362 p++;
363 if( *p == 'x' )
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];
374 p++;
376 else if( *p == 'p' )
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];
382 else if( *p == 's' )
384 char *s = va_arg( args, char * );
385 while(*s)
386 *str++ = *s++;
388 else if( *p == 0 )
389 break;
390 p++;
392 *str++ = *p++;
394 *str = 0;
395 return str - buffer;
398 static __attribute__((format(printf,1,2))) void wld_printf(const char *fmt, ... )
400 va_list args;
401 char buffer[256];
402 int len;
404 va_start( args, fmt );
405 len = wld_vsprintf(buffer, fmt, args );
406 va_end( args );
407 wld_write(2, buffer, len);
410 static __attribute__((noreturn,format(printf,1,2))) void fatal_error(const char *fmt, ... )
412 va_list args;
413 char buffer[256];
414 int len;
416 va_start( args, fmt );
417 len = wld_vsprintf(buffer, fmt, args );
418 va_end( args );
419 wld_write(2, buffer, len);
420 wld_exit(1);
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);
427 int i;
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 ))
439 continue;
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 );
448 return 1;
451 cmd = (struct load_command*)((char*)cmd + cmd->cmdsize);
454 return 0;
458 * preload_reserve
460 * Reserve a range specified in string format
462 static void preload_reserve( const char *str )
464 const char *p;
465 unsigned long result = 0;
466 void *start = NULL, *end = NULL;
467 int i, first = 1;
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;
474 else if (*p == '-')
476 if (!first) goto error;
477 start = (void *)(result & ~page_mask);
478 result = 0;
479 first = 0;
481 else goto error;
483 if (!first) end = (void *)((result + page_mask) & ~page_mask);
484 else if (result) goto error; /* single value '0' is allowed */
486 /* sanity checks */
487 if (end <= start || preloader_overlaps_range(start, end))
488 start = end = NULL;
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)
496 start = end = NULL;
497 break;
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;
506 return;
508 error:
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;
519 i++;
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;
528 int i;
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)
549 *unix_thread = TRUE;
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);
556 return NULL;
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;
563 int i;
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 */
573 else
575 for (i = 0; i < size / page_size; i++)
576 if (vec[i] & 1) return 0;
580 return 1;
583 static int map_region( struct wine_preload_info *info )
585 int flags = MAP_PRIVATE | MAP_ANON;
586 void *ret;
588 if (!info->addr) flags |= MAP_FIXED;
590 for (;;)
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;
600 flags |= MAP_FIXED;
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 );
607 return 0;
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 )
621 int *pargc;
622 char **argv, **env_new;
623 static char dummyvar[] = "WINEPRELOADERDUMMYVAR=1";
625 pargc = stack;
626 argv = (char **)pargc + 1;
628 /* decrement argc, and "remove" argv[0] */
629 *pargc = *pargc - 1;
630 memmove( &argv[0], &argv[1], (*pargc + 1) * sizeof(char *) );
632 env_new = &argv[*pargc-1] + 2;
633 /* In the launched binary on some OSes, _NSGetEnviron() returns
634 * the original 'environ' pointer, so env_new[0] would be ignored.
635 * Put a dummy variable in env_new[0], so nothing is lost in this case.
637 env_new[0] = dummyvar;
640 static void set_program_vars( void *stack, void *mod )
642 int *pargc;
643 char **argv, **env;
644 int *wine_NXArgc = pdlsym( mod, "NXArgc" );
645 char ***wine_NXArgv = pdlsym( mod, "NXArgv" );
646 char ***wine_environ = pdlsym( mod, "environ" );
648 pargc = stack;
649 argv = (char **)pargc + 1;
650 env = &argv[*pargc-1] + 2;
652 if (wine_NXArgc)
653 *wine_NXArgc = *pargc;
654 else
655 wld_printf( "preloader: Warning: failed to set NXArgc\n" );
657 if (wine_NXArgv)
658 *wine_NXArgv = argv;
659 else
660 wld_printf( "preloader: Warning: failed to set NXArgv\n" );
662 if (wine_environ)
663 *wine_environ = env;
664 else
665 wld_printf( "preloader: Warning: failed to set environ\n" );
668 void *wld_start( void *stack, int *is_unix_thread )
670 struct wine_preload_info builtin_dlls = { (void *)0x7a000000, 0x02000000 };
671 struct wine_preload_info **wine_main_preload_info;
672 char **argv, **p, *reserve = NULL;
673 struct target_mach_header *mh;
674 void *mod, *entry;
675 int *pargc, i;
676 Dl_info info;
678 pargc = stack;
679 argv = (char **)pargc + 1;
680 if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
682 /* skip over the parameters */
683 p = argv + *pargc + 1;
685 /* skip over the environment */
686 while (*p)
688 static const char res[] = "WINEPRELOADRESERVE=";
689 if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
690 p++;
693 LOAD_POSIX_DYLD_FUNC( dlopen );
694 LOAD_POSIX_DYLD_FUNC( dlsym );
695 LOAD_POSIX_DYLD_FUNC( dladdr );
696 LOAD_MACHO_DYLD_FUNC( _dyld_get_image_slide );
698 /* reserve memory that Wine needs */
699 if (reserve) preload_reserve( reserve );
700 for (i = 0; preload_info[i].size; i++)
702 if (!map_region( &preload_info[i] ))
704 remove_preload_range( i );
705 i--;
709 if (!map_region( &builtin_dlls ))
710 builtin_dlls.size = 0;
712 /* load the main binary */
713 if (!(mod = pdlopen( argv[1], RTLD_NOW )))
714 fatal_error( "%s: could not load binary\n", argv[1] );
716 if (builtin_dlls.size)
717 wld_munmap( builtin_dlls.addr, builtin_dlls.size );
719 /* store pointer to the preload info into the appropriate main binary variable */
720 wine_main_preload_info = pdlsym( mod, "wine_main_preload_info" );
721 if (wine_main_preload_info) *wine_main_preload_info = preload_info;
722 else wld_printf( "wine_main_preload_info not found\n" );
724 if (!pdladdr( wine_main_preload_info, &info ) || !(mh = info.dli_fbase))
725 fatal_error( "%s: could not find mach header\n", argv[1] );
726 if (!(entry = get_entry_point( mh, p_dyld_get_image_slide(mh), is_unix_thread )))
727 fatal_error( "%s: could not find entry point\n", argv[1] );
729 /* decrement argc and "remove" argv[0] */
730 fixup_stack(stack);
732 /* Set NXArgc, NXArgv, and environ in the new binary.
733 * On different configurations these were either NULL/0 or still had their
734 * values from this preloader's launch.
736 * In particular, environ was not being updated, resulting in environ[0] being lost.
737 * And for LC_UNIXTHREAD binaries on Monterey and later, environ was just NULL.
739 set_program_vars( stack, mod );
741 return entry;
744 #endif /* __APPLE__ */