shell32/tests: Fix some leaks (Valgrind).
[wine.git] / loader / preloader_mac.c
blob83d24af85c2bfddf09500c71af58ca555c531b33
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 "main.h"
56 #ifndef LC_MAIN
57 #define LC_MAIN 0x80000028
58 struct entry_point_command
60 uint32_t cmd;
61 uint32_t cmdsize;
62 uint64_t entryoff;
63 uint64_t stacksize;
65 #endif
67 static struct wine_preload_info preload_info[] =
69 /* On macOS, we allocate the low 64k area in two steps because PAGEZERO
70 * might not always be available. */
71 #ifdef __i386__
72 { (void *)0x00000000, 0x00001000 }, /* first page */
73 { (void *)0x00001000, 0x0000f000 }, /* low 64k */
74 { (void *)0x00010000, 0x00100000 }, /* DOS area */
75 { (void *)0x00110000, 0x67ef0000 }, /* low memory area */
76 { (void *)0x7f000000, 0x03000000 }, /* top-down allocations + shared heap + virtual heap */
77 #else /* __i386__ */
78 { (void *)0x000000010000, 0x00100000 }, /* DOS area */
79 { (void *)0x000000110000, 0x67ef0000 }, /* low memory area */
80 { (void *)0x00007ff00000, 0x000f0000 }, /* shared user data */
81 { (void *)0x7fff40000000, 0x01ff0000 }, /* top-down allocations + virtual heap */
82 #endif /* __i386__ */
83 { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */
84 { 0, 0 } /* end of list */
88 * These functions are only called when file is compiled with -fstack-protector.
89 * They are normally provided by libc's startup files, but since we
90 * build the preloader with "-nostartfiles -nodefaultlibs", we have to
91 * provide our own versions, otherwise the linker fails.
93 void *__stack_chk_guard = 0;
94 void __stack_chk_fail_local(void) { return; }
95 void __stack_chk_fail(void) { return; }
97 #ifdef __i386__
99 static const size_t page_size = 0x1000;
100 static const size_t page_mask = 0xfff;
101 #define target_mach_header mach_header
102 #define target_segment_command segment_command
103 #define TARGET_LC_SEGMENT LC_SEGMENT
104 #define target_thread_state_t i386_thread_state_t
105 #ifdef __DARWIN_UNIX03
106 #define target_thread_ip(x) (x)->__eip
107 #else
108 #define target_thread_ip(x) (x)->eip
109 #endif
111 #define SYSCALL_FUNC( name, nr ) \
112 __ASM_GLOBAL_FUNC( name, \
113 "\tmovl $" #nr ",%eax\n" \
114 "\tint $0x80\n" \
115 "\tjnb 1f\n" \
116 "\tmovl $-1,%eax\n" \
117 "1:\tret\n" )
119 #define SYSCALL_NOERR( name, nr ) \
120 __ASM_GLOBAL_FUNC( name, \
121 "\tmovl $" #nr ",%eax\n" \
122 "\tint $0x80\n" \
123 "\tret\n" )
125 __ASM_GLOBAL_FUNC( start,
126 __ASM_CFI("\t.cfi_undefined %eip\n")
127 /* The first 16 bytes are used as a function signature on i386 */
128 "\t.byte 0x6a,0x00\n" /* pushl $0 */
129 "\t.byte 0x89,0xe5\n" /* movl %esp,%ebp */
130 "\t.byte 0x83,0xe4,0xf0\n" /* andl $-16,%esp */
131 "\t.byte 0x83,0xec,0x10\n" /* subl $16,%esp */
132 "\t.byte 0x8b,0x5d,0x04\n" /* movl 4(%ebp),%ebx */
133 "\t.byte 0x89,0x5c,0x24,0x00\n" /* movl %ebx,0(%esp) */
135 "\tleal 4(%ebp),%eax\n"
136 "\tmovl %eax,0(%esp)\n" /* stack */
137 "\tleal 8(%esp),%eax\n"
138 "\tmovl %eax,4(%esp)\n" /* &is_unix_thread */
139 "\tmovl $0,(%eax)\n"
140 "\tcall _wld_start\n"
142 "\tmovl 4(%ebp),%edi\n"
143 "\tdecl %edi\n" /* argc */
144 "\tleal 12(%ebp),%esi\n" /* argv */
145 "\tleal 4(%esi,%edi,4),%edx\n" /* env */
146 "\tmovl %edx,%ecx\n" /* apple data */
147 "1:\tmovl (%ecx),%ebx\n"
148 "\tadd $4,%ecx\n"
149 "\torl %ebx,%ebx\n"
150 "\tjnz 1b\n"
152 "\tcmpl $0,8(%esp)\n"
153 "\tjne 2f\n"
155 /* LC_MAIN */
156 "\tmovl %edi,0(%esp)\n" /* argc */
157 "\tmovl %esi,4(%esp)\n" /* argv */
158 "\tmovl %edx,8(%esp)\n" /* env */
159 "\tmovl %ecx,12(%esp)\n" /* apple data */
160 "\tcall *%eax\n"
161 "\tmovl %eax,(%esp)\n"
162 "\tcall _wld_exit\n"
163 "\thlt\n"
165 /* LC_UNIXTHREAD */
166 "2:\tmovl (%ecx),%ebx\n"
167 "\tadd $4,%ecx\n"
168 "\torl %ebx,%ebx\n"
169 "\tjnz 2b\n"
171 "\tsubl %ebp,%ecx\n"
172 "\tsubl $8,%ecx\n"
173 "\tleal 4(%ebp),%esp\n"
174 "\tsubl %ecx,%esp\n"
176 "\tmovl %edi,(%esp)\n" /* argc */
177 "\tleal 4(%esp),%edi\n"
178 "\tshrl $2,%ecx\n"
179 "\tcld\n"
180 "\trep; movsd\n" /* argv, ... */
182 "\tmovl $0,%ebp\n"
183 "\tjmpl *%eax\n" )
185 #elif defined(__x86_64__)
187 static const size_t page_size = 0x1000;
188 static const size_t page_mask = 0xfff;
189 #define target_mach_header mach_header_64
190 #define target_segment_command segment_command_64
191 #define TARGET_LC_SEGMENT LC_SEGMENT_64
192 #define target_thread_state_t x86_thread_state64_t
193 #ifdef __DARWIN_UNIX03
194 #define target_thread_ip(x) (x)->__rip
195 #else
196 #define target_thread_ip(x) (x)->rip
197 #endif
199 #define SYSCALL_FUNC( name, nr ) \
200 __ASM_GLOBAL_FUNC( name, \
201 "\tmovq %rcx, %r10\n" \
202 "\tmovq $(" #nr "|0x2000000),%rax\n" \
203 "\tsyscall\n" \
204 "\tjnb 1f\n" \
205 "\tmovq $-1,%rax\n" \
206 "1:\tret\n" )
208 #define SYSCALL_NOERR( name, nr ) \
209 __ASM_GLOBAL_FUNC( name, \
210 "\tmovq %rcx, %r10\n" \
211 "\tmovq $(" #nr "|0x2000000),%rax\n" \
212 "\tsyscall\n" \
213 "\tret\n" )
215 __ASM_GLOBAL_FUNC( start,
216 __ASM_CFI("\t.cfi_undefined %rip\n")
217 "\tpushq $0\n"
218 "\tmovq %rsp,%rbp\n"
219 "\tandq $-16,%rsp\n"
220 "\tsubq $16,%rsp\n"
222 "\tleaq 8(%rbp),%rdi\n" /* stack */
223 "\tmovq %rsp,%rsi\n" /* &is_unix_thread */
224 "\tmovq $0,(%rsi)\n"
225 "\tcall _wld_start\n"
227 "\tmovq 8(%rbp),%rdi\n"
228 "\tdec %rdi\n" /* argc */
229 "\tleaq 24(%rbp),%rsi\n" /* argv */
230 "\tleaq 8(%rsi,%rdi,8),%rdx\n" /* env */
231 "\tmovq %rdx,%rcx\n" /* apple data */
232 "1:\tmovq (%rcx),%r8\n"
233 "\taddq $8,%rcx\n"
234 "\torq %r8,%r8\n"
235 "\tjnz 1b\n"
237 "\tcmpl $0,0(%rsp)\n"
238 "\tjne 2f\n"
240 /* LC_MAIN */
241 "\taddq $16,%rsp\n"
242 "\tcall *%rax\n"
243 "\tmovq %rax,%rdi\n"
244 "\tcall _wld_exit\n"
245 "\thlt\n"
247 /* LC_UNIXTHREAD */
248 "2:\tmovq (%rcx),%r8\n"
249 "\taddq $8,%rcx\n"
250 "\torq %r8,%r8\n"
251 "\tjnz 2b\n"
253 "\tsubq %rbp,%rcx\n"
254 "\tsubq $16,%rcx\n"
255 "\tleaq 8(%rbp),%rsp\n"
256 "\tsubq %rcx,%rsp\n"
258 "\tmovq %rdi,(%rsp)\n" /* argc */
259 "\tleaq 8(%rsp),%rdi\n"
260 "\tshrq $3,%rcx\n"
261 "\tcld\n"
262 "\trep; movsq\n" /* argv, ... */
264 "\tmovq $0,%rbp\n"
265 "\tjmpq *%rax\n" )
267 #else
268 #error preloader not implemented for this CPU
269 #endif
271 void wld_exit( int code ) __attribute__((noreturn));
272 SYSCALL_NOERR( wld_exit, 1 /* SYS_exit */ );
274 ssize_t wld_write( int fd, const void *buffer, size_t len );
275 SYSCALL_FUNC( wld_write, 4 /* SYS_write */ );
277 void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
278 SYSCALL_FUNC( wld_mmap, 197 /* SYS_mmap */ );
280 void *wld_munmap( void *start, size_t len );
281 SYSCALL_FUNC( wld_munmap, 73 /* SYS_munmap */ );
283 int wld_mincore( void *addr, size_t length, unsigned char *vec );
284 SYSCALL_FUNC( wld_mincore, 78 /* SYS_mincore */ );
286 static intptr_t (*p_dyld_get_image_slide)( const struct target_mach_header* mh );
288 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
289 MAKE_FUNCPTR(dlopen);
290 MAKE_FUNCPTR(dlsym);
291 MAKE_FUNCPTR(dladdr);
292 #undef MAKE_FUNCPTR
294 extern int _dyld_func_lookup( const char *dyld_func_name, void **address );
296 /* replacement for libc functions */
298 static int wld_strncmp( const char *str1, const char *str2, size_t len )
300 if (len <= 0) return 0;
301 while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
302 return *str1 - *str2;
306 * wld_printf - just the basics
308 * %x prints a hex number
309 * %s prints a string
310 * %p prints a pointer
312 static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
314 static const char hex_chars[16] = "0123456789abcdef";
315 const char *p = fmt;
316 char *str = buffer;
317 int i;
319 while( *p )
321 if( *p == '%' )
323 p++;
324 if( *p == 'x' )
326 unsigned int x = va_arg( args, unsigned int );
327 for (i = 2*sizeof(x) - 1; i >= 0; i--)
328 *str++ = hex_chars[(x>>(i*4))&0xf];
330 else if (p[0] == 'l' && p[1] == 'x')
332 unsigned long x = va_arg( args, unsigned long );
333 for (i = 2*sizeof(x) - 1; i >= 0; i--)
334 *str++ = hex_chars[(x>>(i*4))&0xf];
335 p++;
337 else if( *p == 'p' )
339 unsigned long x = (unsigned long)va_arg( args, void * );
340 for (i = 2*sizeof(x) - 1; i >= 0; i--)
341 *str++ = hex_chars[(x>>(i*4))&0xf];
343 else if( *p == 's' )
345 char *s = va_arg( args, char * );
346 while(*s)
347 *str++ = *s++;
349 else if( *p == 0 )
350 break;
351 p++;
353 *str++ = *p++;
355 *str = 0;
356 return str - buffer;
359 static __attribute__((format(printf,1,2))) void wld_printf(const char *fmt, ... )
361 va_list args;
362 char buffer[256];
363 int len;
365 va_start( args, fmt );
366 len = wld_vsprintf(buffer, fmt, args );
367 va_end( args );
368 wld_write(2, buffer, len);
371 static __attribute__((noreturn,format(printf,1,2))) void fatal_error(const char *fmt, ... )
373 va_list args;
374 char buffer[256];
375 int len;
377 va_start( args, fmt );
378 len = wld_vsprintf(buffer, fmt, args );
379 va_end( args );
380 wld_write(2, buffer, len);
381 wld_exit(1);
384 static int preloader_overlaps_range( const void *start, const void *end )
386 intptr_t slide = p_dyld_get_image_slide(&_mh_execute_header);
387 struct load_command *cmd = (struct load_command*)(&_mh_execute_header + 1);
388 int i;
390 for (i = 0; i < _mh_execute_header.ncmds; ++i)
392 if (cmd->cmd == TARGET_LC_SEGMENT)
394 struct target_segment_command *seg = (struct target_segment_command*)cmd;
395 const void *seg_start = (const void*)(seg->vmaddr + slide);
396 const void *seg_end = (const char*)seg_start + seg->vmsize;
398 if (end > seg_start && start <= seg_end)
400 char segname[sizeof(seg->segname) + 1];
401 memcpy(segname, seg->segname, sizeof(seg->segname));
402 segname[sizeof(segname) - 1] = 0;
403 wld_printf( "WINEPRELOADRESERVE range %p-%p overlaps preloader %s segment %p-%p\n",
404 start, end, segname, seg_start, seg_end );
405 return 1;
408 cmd = (struct load_command*)((char*)cmd + cmd->cmdsize);
411 return 0;
415 * preload_reserve
417 * Reserve a range specified in string format
419 static void preload_reserve( const char *str )
421 const char *p;
422 unsigned long result = 0;
423 void *start = NULL, *end = NULL;
424 int i, first = 1;
426 for (p = str; *p; p++)
428 if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
429 else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
430 else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
431 else if (*p == '-')
433 if (!first) goto error;
434 start = (void *)(result & ~page_mask);
435 result = 0;
436 first = 0;
438 else goto error;
440 if (!first) end = (void *)((result + page_mask) & ~page_mask);
441 else if (result) goto error; /* single value '0' is allowed */
443 /* sanity checks */
444 if (end <= start || preloader_overlaps_range(start, end))
445 start = end = NULL;
447 /* check for overlap with low memory areas */
448 for (i = 0; preload_info[i].size; i++)
450 if ((char *)preload_info[i].addr > (char *)0x00110000) break;
451 if ((char *)end <= (char *)preload_info[i].addr + preload_info[i].size)
453 start = end = NULL;
454 break;
456 if ((char *)start < (char *)preload_info[i].addr + preload_info[i].size)
457 start = (char *)preload_info[i].addr + preload_info[i].size;
460 while (preload_info[i].size) i++;
461 preload_info[i].addr = start;
462 preload_info[i].size = (char *)end - (char *)start;
463 return;
465 error:
466 fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
469 /* remove a range from the preload list */
470 static void remove_preload_range( int i )
472 while (preload_info[i].size)
474 preload_info[i].addr = preload_info[i+1].addr;
475 preload_info[i].size = preload_info[i+1].size;
476 i++;
480 static void *get_entry_point( struct target_mach_header *mh, intptr_t slide, int *unix_thread )
482 struct entry_point_command *entry;
483 target_thread_state_t *state;
484 struct load_command *cmd;
485 int i;
487 /* try LC_MAIN first */
488 cmd = (struct load_command *)(mh + 1);
489 for (i = 0; i < mh->ncmds; i++)
491 if (cmd->cmd == LC_MAIN)
493 *unix_thread = FALSE;
494 entry = (struct entry_point_command *)cmd;
495 return (char *)mh + entry->entryoff;
497 cmd = (struct load_command *)((char *)cmd + cmd->cmdsize);
500 /* then try LC_UNIXTHREAD */
501 cmd = (struct load_command *)(mh + 1);
502 for (i = 0; i < mh->ncmds; i++)
504 if (cmd->cmd == LC_UNIXTHREAD)
506 *unix_thread = TRUE;
507 state = (target_thread_state_t *)((char *)cmd + 16);
508 return (void *)(target_thread_ip(state) + slide);
510 cmd = (struct load_command *)((char *)cmd + cmd->cmdsize);
513 return NULL;
516 static int is_region_empty( struct wine_preload_info *info )
518 unsigned char vec[1024];
519 size_t pos, size, block = 1024 * page_size;
520 int i;
522 for (pos = 0; pos < info->size; pos += size)
524 size = (pos + block <= info->size) ? block : (info->size - pos);
525 if (wld_mincore( (char *)info->addr + pos, size, vec ) == -1)
527 if (size <= page_size) continue;
528 block = page_size; size = 0; /* retry with smaller block size */
530 else
532 for (i = 0; i < size / page_size; i++)
533 if (vec[i] & 1) return 0;
537 return 1;
540 static int map_region( struct wine_preload_info *info )
542 int flags = MAP_PRIVATE | MAP_ANON;
543 void *ret;
545 if (!info->addr) flags |= MAP_FIXED;
547 for (;;)
549 ret = wld_mmap( info->addr, info->size, PROT_NONE, flags, -1, 0 );
550 if (ret == info->addr) return 1;
551 if (ret != (void *)-1) wld_munmap( ret, info->size );
552 if (flags & MAP_FIXED) break;
554 /* Some versions of macOS ignore the address hint passed to mmap -
555 * use mincore() to check if its empty and then use MAP_FIXED */
556 if (!is_region_empty( info )) break;
557 flags |= MAP_FIXED;
560 /* don't warn for zero page */
561 if (info->addr >= (void *)0x1000)
562 wld_printf( "preloader: Warning: failed to reserve range %p-%p\n",
563 info->addr, (char *)info->addr + info->size );
564 return 0;
567 static inline void get_dyld_func( const char *name, void **func )
569 _dyld_func_lookup( name, func );
570 if (!*func) fatal_error( "Failed to get function pointer for %s\n", name );
573 #define LOAD_POSIX_DYLD_FUNC(f) get_dyld_func( "__dyld_" #f, (void **)&p##f )
574 #define LOAD_MACHO_DYLD_FUNC(f) get_dyld_func( "_" #f, (void **)&p##f )
576 void *wld_start( void *stack, int *is_unix_thread )
578 struct wine_preload_info builtin_dlls = { (void *)0x7a000000, 0x02000000 };
579 struct wine_preload_info **wine_main_preload_info;
580 char **argv, **p, *reserve = NULL;
581 struct target_mach_header *mh;
582 void *mod, *entry;
583 int *pargc, i;
584 Dl_info info;
586 pargc = stack;
587 argv = (char **)pargc + 1;
588 if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
590 /* skip over the parameters */
591 p = argv + *pargc + 1;
593 /* skip over the environment */
594 while (*p)
596 static const char res[] = "WINEPRELOADRESERVE=";
597 if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
598 p++;
601 LOAD_POSIX_DYLD_FUNC( dlopen );
602 LOAD_POSIX_DYLD_FUNC( dlsym );
603 LOAD_POSIX_DYLD_FUNC( dladdr );
604 LOAD_MACHO_DYLD_FUNC( _dyld_get_image_slide );
606 /* reserve memory that Wine needs */
607 if (reserve) preload_reserve( reserve );
608 for (i = 0; preload_info[i].size; i++)
610 if (!map_region( &preload_info[i] ))
612 remove_preload_range( i );
613 i--;
617 if (!map_region( &builtin_dlls ))
618 builtin_dlls.size = 0;
620 /* load the main binary */
621 if (!(mod = pdlopen( argv[1], RTLD_NOW )))
622 fatal_error( "%s: could not load binary\n", argv[1] );
624 if (builtin_dlls.size)
625 wld_munmap( builtin_dlls.addr, builtin_dlls.size );
627 /* store pointer to the preload info into the appropriate main binary variable */
628 wine_main_preload_info = pdlsym( mod, "wine_main_preload_info" );
629 if (wine_main_preload_info) *wine_main_preload_info = preload_info;
630 else wld_printf( "wine_main_preload_info not found\n" );
632 if (!pdladdr( wine_main_preload_info, &info ) || !(mh = info.dli_fbase))
633 fatal_error( "%s: could not find mach header\n", argv[1] );
634 if (!(entry = get_entry_point( mh, p_dyld_get_image_slide(mh), is_unix_thread )))
635 fatal_error( "%s: could not find entry point\n", argv[1] );
637 return entry;
640 #endif /* __APPLE__ */