preloader_mac.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * Preloader for macOS
  3. *
  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
  9. *
  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.
  14. *
  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.
  19. *
  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
  23. */
  24. #ifdef __APPLE__
  25. #include "config.h"
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include <sys/mman.h>
  34. #ifdef HAVE_SYS_SYSCALL_H
  35. # include <sys/syscall.h>
  36. #endif
  37. #include <unistd.h>
  38. #include <dlfcn.h>
  39. #ifdef HAVE_MACH_O_LOADER_H
  40. #include <mach/thread_status.h>
  41. #include <mach-o/loader.h>
  42. #include <mach-o/ldsyms.h>
  43. #endif
  44. #include "wine/asm.h"
  45. #include "main.h"
  46. /* Rosetta on Apple Silicon allocates memory starting at 0x100000000 (the 4GB line)
  47. * before the preloader runs, which prevents any nonrelocatable EXEs with that
  48. * base address from running.
  49. *
  50. * This empty linker section forces Rosetta's allocations (currently ~132 MB)
  51. * to start at 0x114000000, and they should end below 0x120000000.
  52. */
  53. #if defined(__x86_64__)
  54. __asm__(".zerofill WINE_4GB_RESERVE,WINE_4GB_RESERVE,___wine_4gb_reserve,0x14000000");
  55. #endif
  56. #ifndef LC_MAIN
  57. #define LC_MAIN 0x80000028
  58. struct entry_point_command
  59. {
  60. uint32_t cmd;
  61. uint32_t cmdsize;
  62. uint64_t entryoff;
  63. uint64_t stacksize;
  64. };
  65. #endif
  66. static struct wine_preload_info preload_info[] =
  67. {
  68. /* On macOS, we allocate the low 64k area in two steps because PAGEZERO
  69. * might not always be available. */
  70. #ifdef __i386__
  71. { (void *)0x00000000, 0x00001000 }, /* first page */
  72. { (void *)0x00001000, 0x0000f000 }, /* low 64k */
  73. { (void *)0x00010000, 0x00100000 }, /* DOS area */
  74. { (void *)0x00110000, 0x67ef0000 }, /* low memory area */
  75. { (void *)0x7f000000, 0x03000000 }, /* top-down allocations + shared heap + virtual heap */
  76. #else /* __i386__ */
  77. { (void *)0x000000010000, 0x00100000 }, /* DOS area */
  78. { (void *)0x000000110000, 0x67ef0000 }, /* low memory area */
  79. { (void *)0x00007ff00000, 0x000f0000 }, /* shared user data */
  80. { (void *)0x000100000000, 0x14000000 }, /* WINE_4GB_RESERVE section */
  81. { (void *)0x7ffd00000000, 0x01ff0000 }, /* top-down allocations + virtual heap */
  82. #endif /* __i386__ */
  83. { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */
  84. { 0, 0 } /* end of list */
  85. };
  86. /*
  87. * These functions are only called when file is compiled with -fstack-protector.
  88. * They are normally provided by libc's startup files, but since we
  89. * build the preloader with "-nostartfiles -nodefaultlibs", we have to
  90. * provide our own versions, otherwise the linker fails.
  91. */
  92. void *__stack_chk_guard = 0;
  93. void __stack_chk_fail_local(void) { return; }
  94. void __stack_chk_fail(void) { return; }
  95. #ifdef __i386__
  96. static const size_t page_size = 0x1000;
  97. static const size_t page_mask = 0xfff;
  98. #define target_mach_header mach_header
  99. #define target_segment_command segment_command
  100. #define TARGET_LC_SEGMENT LC_SEGMENT
  101. #define target_thread_state_t i386_thread_state_t
  102. #ifdef __DARWIN_UNIX03
  103. #define target_thread_ip(x) (x)->__eip
  104. #else
  105. #define target_thread_ip(x) (x)->eip
  106. #endif
  107. #define SYSCALL_FUNC( name, nr ) \
  108. __ASM_GLOBAL_FUNC( name, \
  109. "\tmovl $" #nr ",%eax\n" \
  110. "\tint $0x80\n" \
  111. "\tjnb 1f\n" \
  112. "\tmovl $-1,%eax\n" \
  113. "1:\tret\n" )
  114. #define SYSCALL_NOERR( name, nr ) \
  115. __ASM_GLOBAL_FUNC( name, \
  116. "\tmovl $" #nr ",%eax\n" \
  117. "\tint $0x80\n" \
  118. "\tret\n" )
  119. __ASM_GLOBAL_FUNC( start,
  120. __ASM_CFI("\t.cfi_undefined %eip\n")
  121. /* The first 16 bytes are used as a function signature on i386 */
  122. "\t.byte 0x6a,0x00\n" /* pushl $0 */
  123. "\t.byte 0x89,0xe5\n" /* movl %esp,%ebp */
  124. "\t.byte 0x83,0xe4,0xf0\n" /* andl $-16,%esp */
  125. "\t.byte 0x83,0xec,0x10\n" /* subl $16,%esp */
  126. "\t.byte 0x8b,0x5d,0x04\n" /* movl 4(%ebp),%ebx */
  127. "\t.byte 0x89,0x5c,0x24,0x00\n" /* movl %ebx,0(%esp) */
  128. "\tleal 4(%ebp),%eax\n"
  129. "\tmovl %eax,0(%esp)\n" /* stack */
  130. "\tleal 8(%esp),%eax\n"
  131. "\tmovl %eax,4(%esp)\n" /* &is_unix_thread */
  132. "\tmovl $0,(%eax)\n"
  133. "\tcall _wld_start\n"
  134. "\tmovl 4(%ebp),%edi\n"
  135. "\tdecl %edi\n" /* argc */
  136. "\tleal 12(%ebp),%esi\n" /* argv */
  137. "\tleal 4(%esi,%edi,4),%edx\n" /* env */
  138. "\tmovl %edx,%ecx\n" /* apple data */
  139. "1:\tmovl (%ecx),%ebx\n"
  140. "\tadd $4,%ecx\n"
  141. "\torl %ebx,%ebx\n"
  142. "\tjnz 1b\n"
  143. "\tcmpl $0,8(%esp)\n"
  144. "\tjne 2f\n"
  145. /* LC_MAIN */
  146. "\tmovl %edi,0(%esp)\n" /* argc */
  147. "\tmovl %esi,4(%esp)\n" /* argv */
  148. "\tmovl %edx,8(%esp)\n" /* env */
  149. "\tmovl %ecx,12(%esp)\n" /* apple data */
  150. "\tcall *%eax\n"
  151. "\tmovl %eax,(%esp)\n"
  152. "\tcall _wld_exit\n"
  153. "\thlt\n"
  154. /* LC_UNIXTHREAD */
  155. "2:\tmovl (%ecx),%ebx\n"
  156. "\tadd $4,%ecx\n"
  157. "\torl %ebx,%ebx\n"
  158. "\tjnz 2b\n"
  159. "\tsubl %ebp,%ecx\n"
  160. "\tsubl $8,%ecx\n"
  161. "\tleal 4(%ebp),%esp\n"
  162. "\tsubl %ecx,%esp\n"
  163. "\tmovl %edi,(%esp)\n" /* argc */
  164. "\tleal 4(%esp),%edi\n"
  165. "\tshrl $2,%ecx\n"
  166. "\tcld\n"
  167. "\trep; movsd\n" /* argv, ... */
  168. "\tmovl $0,%ebp\n"
  169. "\tjmpl *%eax\n" )
  170. #elif defined(__x86_64__)
  171. static const size_t page_size = 0x1000;
  172. static const size_t page_mask = 0xfff;
  173. #define target_mach_header mach_header_64
  174. #define target_segment_command segment_command_64
  175. #define TARGET_LC_SEGMENT LC_SEGMENT_64
  176. #define target_thread_state_t x86_thread_state64_t
  177. #ifdef __DARWIN_UNIX03
  178. #define target_thread_ip(x) (x)->__rip
  179. #else
  180. #define target_thread_ip(x) (x)->rip
  181. #endif
  182. #define SYSCALL_FUNC( name, nr ) \
  183. __ASM_GLOBAL_FUNC( name, \
  184. "\tmovq %rcx, %r10\n" \
  185. "\tmovq $(" #nr "|0x2000000),%rax\n" \
  186. "\tsyscall\n" \
  187. "\tjnb 1f\n" \
  188. "\tmovq $-1,%rax\n" \
  189. "1:\tret\n" )
  190. #define SYSCALL_NOERR( name, nr ) \
  191. __ASM_GLOBAL_FUNC( name, \
  192. "\tmovq %rcx, %r10\n" \
  193. "\tmovq $(" #nr "|0x2000000),%rax\n" \
  194. "\tsyscall\n" \
  195. "\tret\n" )
  196. __ASM_GLOBAL_FUNC( start,
  197. __ASM_CFI("\t.cfi_undefined %rip\n")
  198. "\tpushq $0\n"
  199. "\tmovq %rsp,%rbp\n"
  200. "\tandq $-16,%rsp\n"
  201. "\tsubq $16,%rsp\n"
  202. "\tleaq 8(%rbp),%rdi\n" /* stack */
  203. "\tmovq %rsp,%rsi\n" /* &is_unix_thread */
  204. "\tmovq $0,(%rsi)\n"
  205. "\tcall _wld_start\n"
  206. "\tmovq 8(%rbp),%rdi\n"
  207. "\tdec %rdi\n" /* argc */
  208. "\tleaq 24(%rbp),%rsi\n" /* argv */
  209. "\tleaq 8(%rsi,%rdi,8),%rdx\n" /* env */
  210. "\tmovq %rdx,%rcx\n" /* apple data */
  211. "1:\tmovq (%rcx),%r8\n"
  212. "\taddq $8,%rcx\n"
  213. "\torq %r8,%r8\n"
  214. "\tjnz 1b\n"
  215. "\tcmpl $0,0(%rsp)\n"
  216. "\tjne 2f\n"
  217. /* LC_MAIN */
  218. "\taddq $16,%rsp\n"
  219. "\tcall *%rax\n"
  220. "\tmovq %rax,%rdi\n"
  221. "\tcall _wld_exit\n"
  222. "\thlt\n"
  223. /* LC_UNIXTHREAD */
  224. "2:\tmovq (%rcx),%r8\n"
  225. "\taddq $8,%rcx\n"
  226. "\torq %r8,%r8\n"
  227. "\tjnz 2b\n"
  228. "\tsubq %rbp,%rcx\n"
  229. "\tsubq $16,%rcx\n"
  230. "\tleaq 8(%rbp),%rsp\n"
  231. "\tsubq %rcx,%rsp\n"
  232. "\tmovq %rdi,(%rsp)\n" /* argc */
  233. "\tleaq 8(%rsp),%rdi\n"
  234. "\tshrq $3,%rcx\n"
  235. "\tcld\n"
  236. "\trep; movsq\n" /* argv, ... */
  237. "\tmovq $0,%rbp\n"
  238. "\tjmpq *%rax\n" )
  239. #else
  240. #error preloader not implemented for this CPU
  241. #endif
  242. void wld_exit( int code ) __attribute__((noreturn));
  243. SYSCALL_NOERR( wld_exit, 1 /* SYS_exit */ );
  244. ssize_t wld_write( int fd, const void *buffer, size_t len );
  245. SYSCALL_FUNC( wld_write, 4 /* SYS_write */ );
  246. void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
  247. SYSCALL_FUNC( wld_mmap, 197 /* SYS_mmap */ );
  248. void *wld_munmap( void *start, size_t len );
  249. SYSCALL_FUNC( wld_munmap, 73 /* SYS_munmap */ );
  250. int wld_mincore( void *addr, size_t length, unsigned char *vec );
  251. SYSCALL_FUNC( wld_mincore, 78 /* SYS_mincore */ );
  252. static intptr_t (*p_dyld_get_image_slide)( const struct target_mach_header* mh );
  253. #define MAKE_FUNCPTR(f) static typeof(f) * p##f
  254. MAKE_FUNCPTR(dlopen);
  255. MAKE_FUNCPTR(dlsym);
  256. MAKE_FUNCPTR(dladdr);
  257. #undef MAKE_FUNCPTR
  258. extern int _dyld_func_lookup( const char *dyld_func_name, void **address );
  259. /* replacement for libc functions */
  260. static int wld_strncmp( const char *str1, const char *str2, size_t len )
  261. {
  262. if (len <= 0) return 0;
  263. while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
  264. return *str1 - *str2;
  265. }
  266. /*
  267. * wld_printf - just the basics
  268. *
  269. * %x prints a hex number
  270. * %s prints a string
  271. * %p prints a pointer
  272. */
  273. static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
  274. {
  275. static const char hex_chars[16] = "0123456789abcdef";
  276. const char *p = fmt;
  277. char *str = buffer;
  278. int i;
  279. while( *p )
  280. {
  281. if( *p == '%' )
  282. {
  283. p++;
  284. if( *p == 'x' )
  285. {
  286. unsigned int x = va_arg( args, unsigned int );
  287. for (i = 2*sizeof(x) - 1; i >= 0; i--)
  288. *str++ = hex_chars[(x>>(i*4))&0xf];
  289. }
  290. else if (p[0] == 'l' && p[1] == 'x')
  291. {
  292. unsigned long x = va_arg( args, unsigned long );
  293. for (i = 2*sizeof(x) - 1; i >= 0; i--)
  294. *str++ = hex_chars[(x>>(i*4))&0xf];
  295. p++;
  296. }
  297. else if( *p == 'p' )
  298. {
  299. unsigned long x = (unsigned long)va_arg( args, void * );
  300. for (i = 2*sizeof(x) - 1; i >= 0; i--)
  301. *str++ = hex_chars[(x>>(i*4))&0xf];
  302. }
  303. else if( *p == 's' )
  304. {
  305. char *s = va_arg( args, char * );
  306. while(*s)
  307. *str++ = *s++;
  308. }
  309. else if( *p == 0 )
  310. break;
  311. p++;
  312. }
  313. *str++ = *p++;
  314. }
  315. *str = 0;
  316. return str - buffer;
  317. }
  318. static __attribute__((format(printf,1,2))) void wld_printf(const char *fmt, ... )
  319. {
  320. va_list args;
  321. char buffer[256];
  322. int len;
  323. va_start( args, fmt );
  324. len = wld_vsprintf(buffer, fmt, args );
  325. va_end( args );
  326. wld_write(2, buffer, len);
  327. }
  328. static __attribute__((noreturn,format(printf,1,2))) void fatal_error(const char *fmt, ... )
  329. {
  330. va_list args;
  331. char buffer[256];
  332. int len;
  333. va_start( args, fmt );
  334. len = wld_vsprintf(buffer, fmt, args );
  335. va_end( args );
  336. wld_write(2, buffer, len);
  337. wld_exit(1);
  338. }
  339. static int preloader_overlaps_range( const void *start, const void *end )
  340. {
  341. intptr_t slide = p_dyld_get_image_slide(&_mh_execute_header);
  342. struct load_command *cmd = (struct load_command*)(&_mh_execute_header + 1);
  343. int i;
  344. for (i = 0; i < _mh_execute_header.ncmds; ++i)
  345. {
  346. if (cmd->cmd == TARGET_LC_SEGMENT)
  347. {
  348. struct target_segment_command *seg = (struct target_segment_command*)cmd;
  349. const void *seg_start = (const void*)(seg->vmaddr + slide);
  350. const void *seg_end = (const char*)seg_start + seg->vmsize;
  351. static const char reserved_segname[] = "WINE_4GB_RESERVE";
  352. if (!wld_strncmp( seg->segname, reserved_segname, sizeof(reserved_segname)-1 ))
  353. continue;
  354. if (end > seg_start && start <= seg_end)
  355. {
  356. char segname[sizeof(seg->segname) + 1];
  357. memcpy(segname, seg->segname, sizeof(seg->segname));
  358. segname[sizeof(segname) - 1] = 0;
  359. wld_printf( "WINEPRELOADRESERVE range %p-%p overlaps preloader %s segment %p-%p\n",
  360. start, end, segname, seg_start, seg_end );
  361. return 1;
  362. }
  363. }
  364. cmd = (struct load_command*)((char*)cmd + cmd->cmdsize);
  365. }
  366. return 0;
  367. }
  368. /*
  369. * preload_reserve
  370. *
  371. * Reserve a range specified in string format
  372. */
  373. static void preload_reserve( const char *str )
  374. {
  375. const char *p;
  376. unsigned long result = 0;
  377. void *start = NULL, *end = NULL;
  378. int i, first = 1;
  379. for (p = str; *p; p++)
  380. {
  381. if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
  382. else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
  383. else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
  384. else if (*p == '-')
  385. {
  386. if (!first) goto error;
  387. start = (void *)(result & ~page_mask);
  388. result = 0;
  389. first = 0;
  390. }
  391. else goto error;
  392. }
  393. if (!first) end = (void *)((result + page_mask) & ~page_mask);
  394. else if (result) goto error; /* single value '0' is allowed */
  395. /* sanity checks */
  396. if (end <= start || preloader_overlaps_range(start, end))
  397. start = end = NULL;
  398. /* check for overlap with low memory areas */
  399. for (i = 0; preload_info[i].size; i++)
  400. {
  401. if ((char *)preload_info[i].addr > (char *)0x00110000) break;
  402. if ((char *)end <= (char *)preload_info[i].addr + preload_info[i].size)
  403. {
  404. start = end = NULL;
  405. break;
  406. }
  407. if ((char *)start < (char *)preload_info[i].addr + preload_info[i].size)
  408. start = (char *)preload_info[i].addr + preload_info[i].size;
  409. }
  410. while (preload_info[i].size) i++;
  411. preload_info[i].addr = start;
  412. preload_info[i].size = (char *)end - (char *)start;
  413. return;
  414. error:
  415. fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
  416. }
  417. /* remove a range from the preload list */
  418. static void remove_preload_range( int i )
  419. {
  420. while (preload_info[i].size)
  421. {
  422. preload_info[i].addr = preload_info[i+1].addr;
  423. preload_info[i].size = preload_info[i+1].size;
  424. i++;
  425. }
  426. }
  427. static void *get_entry_point( struct target_mach_header *mh, intptr_t slide, int *unix_thread )
  428. {
  429. struct entry_point_command *entry;
  430. target_thread_state_t *state;
  431. struct load_command *cmd;
  432. int i;
  433. /* try LC_MAIN first */
  434. cmd = (struct load_command *)(mh + 1);
  435. for (i = 0; i < mh->ncmds; i++)
  436. {
  437. if (cmd->cmd == LC_MAIN)
  438. {
  439. *unix_thread = FALSE;
  440. entry = (struct entry_point_command *)cmd;
  441. return (char *)mh + entry->entryoff;
  442. }
  443. cmd = (struct load_command *)((char *)cmd + cmd->cmdsize);
  444. }
  445. /* then try LC_UNIXTHREAD */
  446. cmd = (struct load_command *)(mh + 1);
  447. for (i = 0; i < mh->ncmds; i++)
  448. {
  449. if (cmd->cmd == LC_UNIXTHREAD)
  450. {
  451. *unix_thread = TRUE;
  452. state = (target_thread_state_t *)((char *)cmd + 16);
  453. return (void *)(target_thread_ip(state) + slide);
  454. }
  455. cmd = (struct load_command *)((char *)cmd + cmd->cmdsize);
  456. }
  457. return NULL;
  458. };
  459. static int is_region_empty( struct wine_preload_info *info )
  460. {
  461. unsigned char vec[1024];
  462. size_t pos, size, block = 1024 * page_size;
  463. int i;
  464. for (pos = 0; pos < info->size; pos += size)
  465. {
  466. size = (pos + block <= info->size) ? block : (info->size - pos);
  467. if (wld_mincore( (char *)info->addr + pos, size, vec ) == -1)
  468. {
  469. if (size <= page_size) continue;
  470. block = page_size; size = 0; /* retry with smaller block size */
  471. }
  472. else
  473. {
  474. for (i = 0; i < size / page_size; i++)
  475. if (vec[i] & 1) return 0;
  476. }
  477. }
  478. return 1;
  479. }
  480. static int map_region( struct wine_preload_info *info )
  481. {
  482. int flags = MAP_PRIVATE | MAP_ANON;
  483. void *ret;
  484. if (!info->addr) flags |= MAP_FIXED;
  485. for (;;)
  486. {
  487. ret = wld_mmap( info->addr, info->size, PROT_NONE, flags, -1, 0 );
  488. if (ret == info->addr) return 1;
  489. if (ret != (void *)-1) wld_munmap( ret, info->size );
  490. if (flags & MAP_FIXED) break;
  491. /* Some versions of macOS ignore the address hint passed to mmap -
  492. * use mincore() to check if its empty and then use MAP_FIXED */
  493. if (!is_region_empty( info )) break;
  494. flags |= MAP_FIXED;
  495. }
  496. /* don't warn for zero page */
  497. if (info->addr >= (void *)0x1000)
  498. wld_printf( "preloader: Warning: failed to reserve range %p-%p\n",
  499. info->addr, (char *)info->addr + info->size );
  500. return 0;
  501. }
  502. static inline void get_dyld_func( const char *name, void **func )
  503. {
  504. _dyld_func_lookup( name, func );
  505. if (!*func) fatal_error( "Failed to get function pointer for %s\n", name );
  506. }
  507. #define LOAD_POSIX_DYLD_FUNC(f) get_dyld_func( "__dyld_" #f, (void **)&p##f )
  508. #define LOAD_MACHO_DYLD_FUNC(f) get_dyld_func( "_" #f, (void **)&p##f )
  509. void *wld_start( void *stack, int *is_unix_thread )
  510. {
  511. struct wine_preload_info builtin_dlls = { (void *)0x7a000000, 0x02000000 };
  512. struct wine_preload_info **wine_main_preload_info;
  513. char **argv, **p, *reserve = NULL;
  514. struct target_mach_header *mh;
  515. void *mod, *entry;
  516. int *pargc, i;
  517. Dl_info info;
  518. pargc = stack;
  519. argv = (char **)pargc + 1;
  520. if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
  521. /* skip over the parameters */
  522. p = argv + *pargc + 1;
  523. /* skip over the environment */
  524. while (*p)
  525. {
  526. static const char res[] = "WINEPRELOADRESERVE=";
  527. if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
  528. p++;
  529. }
  530. LOAD_POSIX_DYLD_FUNC( dlopen );
  531. LOAD_POSIX_DYLD_FUNC( dlsym );
  532. LOAD_POSIX_DYLD_FUNC( dladdr );
  533. LOAD_MACHO_DYLD_FUNC( _dyld_get_image_slide );
  534. /* reserve memory that Wine needs */
  535. if (reserve) preload_reserve( reserve );
  536. for (i = 0; preload_info[i].size; i++)
  537. {
  538. if (!map_region( &preload_info[i] ))
  539. {
  540. remove_preload_range( i );
  541. i--;
  542. }
  543. }
  544. if (!map_region( &builtin_dlls ))
  545. builtin_dlls.size = 0;
  546. /* load the main binary */
  547. if (!(mod = pdlopen( argv[1], RTLD_NOW )))
  548. fatal_error( "%s: could not load binary\n", argv[1] );
  549. if (builtin_dlls.size)
  550. wld_munmap( builtin_dlls.addr, builtin_dlls.size );
  551. /* store pointer to the preload info into the appropriate main binary variable */
  552. wine_main_preload_info = pdlsym( mod, "wine_main_preload_info" );
  553. if (wine_main_preload_info) *wine_main_preload_info = preload_info;
  554. else wld_printf( "wine_main_preload_info not found\n" );
  555. if (!pdladdr( wine_main_preload_info, &info ) || !(mh = info.dli_fbase))
  556. fatal_error( "%s: could not find mach header\n", argv[1] );
  557. if (!(entry = get_entry_point( mh, p_dyld_get_image_slide(mh), is_unix_thread )))
  558. fatal_error( "%s: could not find entry point\n", argv[1] );
  559. return entry;
  560. }
  561. #endif /* __APPLE__ */