preloader_mac.c 20 KB

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