preloader.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. /*
  2. * Preloader for ld.so
  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. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. /*
  23. * Design notes
  24. *
  25. * The goal of this program is to be a workaround for exec-shield, as used
  26. * by the Linux kernel distributed with Fedora Core and other distros.
  27. *
  28. * To do this, we implement our own shared object loader that reserves memory
  29. * that is important to Wine, and then loads the main binary and its ELF
  30. * interpreter.
  31. *
  32. * We will try to set up the stack and memory area so that the program that
  33. * loads after us (eg. the wine binary) never knows we were here, except that
  34. * areas of memory it needs are already magically reserved.
  35. *
  36. * The following memory areas are important to Wine:
  37. * 0x00000000 - 0x00110000 the DOS area
  38. * 0x80000000 - 0x81000000 the shared heap
  39. * ??? - ??? the PE binary load address (usually starting at 0x00400000)
  40. *
  41. * If this program is used as the shared object loader, the only difference
  42. * that the loaded programs should see is that this loader will be mapped
  43. * into memory when it starts.
  44. */
  45. /*
  46. * References (things I consulted to understand how ELF loading works):
  47. *
  48. * glibc 2.3.2 elf/dl-load.c
  49. * http://www.gnu.org/directory/glibc.html
  50. *
  51. * Linux 2.6.4 fs/binfmt_elf.c
  52. * ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.4.tar.bz2
  53. *
  54. * Userland exec, by <grugq@hcunix.net>
  55. * http://cert.uni-stuttgart.de/archive/bugtraq/2004/01/msg00002.html
  56. *
  57. * The ELF specification:
  58. * http://www.linuxbase.org/spec/booksets/LSB-Embedded/LSB-Embedded/book387.html
  59. */
  60. #ifdef __linux__
  61. #include "config.h"
  62. #include "wine/port.h"
  63. #include <stdarg.h>
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <string.h>
  67. #include <sys/types.h>
  68. #ifdef HAVE_SYS_STAT_H
  69. # include <sys/stat.h>
  70. #endif
  71. #include <fcntl.h>
  72. #ifdef HAVE_SYS_MMAN_H
  73. # include <sys/mman.h>
  74. #endif
  75. #ifdef HAVE_SYS_SYSCALL_H
  76. # include <sys/syscall.h>
  77. #endif
  78. #ifdef HAVE_UNISTD_H
  79. # include <unistd.h>
  80. #endif
  81. #ifdef HAVE_ELF_H
  82. # include <elf.h>
  83. #endif
  84. #ifdef HAVE_LINK_H
  85. # include <link.h>
  86. #endif
  87. #ifdef HAVE_SYS_LINK_H
  88. # include <sys/link.h>
  89. #endif
  90. #include "wine/asm.h"
  91. #include "main.h"
  92. /* ELF definitions */
  93. #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
  94. #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
  95. #define MAP_BASE_ADDR(l) 0
  96. #ifndef MAP_COPY
  97. #define MAP_COPY MAP_PRIVATE
  98. #endif
  99. #ifndef MAP_NORESERVE
  100. #define MAP_NORESERVE 0
  101. #endif
  102. static struct wine_preload_info preload_info[] =
  103. {
  104. #ifdef __i386__
  105. { (void *)0x00000000, 0x00010000 }, /* low 64k */
  106. { (void *)0x00010000, 0x00100000 }, /* DOS area */
  107. { (void *)0x00110000, 0x67ef0000 }, /* low memory area */
  108. { (void *)0x7f000000, 0x03000000 }, /* top-down allocations + shared heap + virtual heap */
  109. #else
  110. { (void *)0x000000010000, 0x00100000 }, /* DOS area */
  111. { (void *)0x000000110000, 0x67ef0000 }, /* low memory area */
  112. { (void *)0x00007ffe0000, 0x10000 }, /* shared user data */
  113. { (void *)0x3ffffe000000, 0x01ff0000 }, /* top-down allocations + virtual heap */
  114. #endif
  115. { 0, 0 }, /* PE exe range set with WINEPRELOADRESERVE */
  116. { 0, 0 } /* end of list */
  117. };
  118. /* debugging */
  119. #undef DUMP_SEGMENTS
  120. #undef DUMP_AUX_INFO
  121. #undef DUMP_SYMS
  122. #undef DUMP_MAPS
  123. /* older systems may not define these */
  124. #ifndef PT_TLS
  125. #define PT_TLS 7
  126. #endif
  127. #ifndef AT_SYSINFO
  128. #define AT_SYSINFO 32
  129. #endif
  130. #ifndef AT_SYSINFO_EHDR
  131. #define AT_SYSINFO_EHDR 33
  132. #endif
  133. #ifndef DT_GNU_HASH
  134. #define DT_GNU_HASH 0x6ffffef5
  135. #endif
  136. static size_t page_size, page_mask;
  137. static char *preloader_start, *preloader_end;
  138. struct wld_link_map {
  139. ElfW(Addr) l_addr;
  140. ElfW(Dyn) *l_ld;
  141. ElfW(Phdr)*l_phdr;
  142. ElfW(Addr) l_entry;
  143. ElfW(Half) l_ldnum;
  144. ElfW(Half) l_phnum;
  145. ElfW(Addr) l_map_start, l_map_end;
  146. ElfW(Addr) l_interp;
  147. };
  148. struct wld_auxv
  149. {
  150. ElfW(Addr) a_type;
  151. union
  152. {
  153. ElfW(Addr) a_val;
  154. } a_un;
  155. };
  156. /*
  157. * The __bb_init_func is an empty function only called when file is
  158. * compiled with gcc flags "-fprofile-arcs -ftest-coverage". This
  159. * function is normally provided by libc's startup files, but since we
  160. * build the preloader with "-nostartfiles -nodefaultlibs", we have to
  161. * provide our own (empty) version, otherwise linker fails.
  162. */
  163. void __bb_init_func(void) { return; }
  164. /* similar to the above but for -fstack-protector */
  165. void *__stack_chk_guard = 0;
  166. void __stack_chk_fail_local(void) { return; }
  167. void __stack_chk_fail(void) { return; }
  168. #ifdef __i386__
  169. /* data for setting up the glibc-style thread-local storage in %gs */
  170. static int thread_data[256];
  171. struct
  172. {
  173. /* this is the kernel modify_ldt struct */
  174. unsigned int entry_number;
  175. unsigned long base_addr;
  176. unsigned int limit;
  177. unsigned int seg_32bit : 1;
  178. unsigned int contents : 2;
  179. unsigned int read_exec_only : 1;
  180. unsigned int limit_in_pages : 1;
  181. unsigned int seg_not_present : 1;
  182. unsigned int usable : 1;
  183. unsigned int garbage : 25;
  184. } thread_ldt = { -1, (unsigned long)thread_data, 0xfffff, 1, 0, 0, 1, 0, 1, 0 };
  185. /*
  186. * The _start function is the entry and exit point of this program
  187. *
  188. * It calls wld_start, passing a pointer to the args it receives
  189. * then jumps to the address wld_start returns.
  190. */
  191. void _start(void);
  192. extern char _end[];
  193. __ASM_GLOBAL_FUNC(_start,
  194. __ASM_CFI("\t.cfi_undefined %eip\n")
  195. "\tmovl $243,%eax\n" /* SYS_set_thread_area */
  196. "\tmovl $thread_ldt,%ebx\n"
  197. "\tint $0x80\n" /* allocate gs segment */
  198. "\torl %eax,%eax\n"
  199. "\tjl 1f\n"
  200. "\tmovl thread_ldt,%eax\n" /* thread_ldt.entry_number */
  201. "\tshl $3,%eax\n"
  202. "\torl $3,%eax\n"
  203. "\tmov %ax,%gs\n"
  204. "\tmov %ax,%fs\n" /* set %fs too so libwine can retrieve it later on */
  205. "1:\tmovl %esp,%eax\n"
  206. "\tleal -136(%esp),%esp\n" /* allocate some space for extra aux values */
  207. "\tpushl %eax\n" /* orig stack pointer */
  208. "\tpushl %esp\n" /* ptr to orig stack pointer */
  209. "\tcall wld_start\n"
  210. "\tpopl %ecx\n" /* remove ptr to stack pointer */
  211. "\tpopl %esp\n" /* new stack pointer */
  212. "\tpush %eax\n" /* ELF interpreter entry point */
  213. "\txor %eax,%eax\n"
  214. "\txor %ecx,%ecx\n"
  215. "\txor %edx,%edx\n"
  216. "\tmov %ax,%gs\n" /* clear %gs again */
  217. "\tret\n")
  218. /* wrappers for Linux system calls */
  219. #define SYSCALL_RET(ret) (((ret) < 0 && (ret) > -4096) ? -1 : (ret))
  220. static inline __attribute__((noreturn)) void wld_exit( int code )
  221. {
  222. for (;;) /* avoid warning */
  223. __asm__ __volatile__( "pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
  224. : : "a" (1 /* SYS_exit */), "r" (code) );
  225. }
  226. static inline int wld_open( const char *name, int flags )
  227. {
  228. int ret;
  229. __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
  230. : "=a" (ret) : "0" (5 /* SYS_open */), "r" (name), "c" (flags) );
  231. return SYSCALL_RET(ret);
  232. }
  233. static inline int wld_close( int fd )
  234. {
  235. int ret;
  236. __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
  237. : "=a" (ret) : "0" (6 /* SYS_close */), "r" (fd) );
  238. return SYSCALL_RET(ret);
  239. }
  240. static inline ssize_t wld_read( int fd, void *buffer, size_t len )
  241. {
  242. int ret;
  243. __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
  244. : "=a" (ret)
  245. : "0" (3 /* SYS_read */), "r" (fd), "c" (buffer), "d" (len)
  246. : "memory" );
  247. return SYSCALL_RET(ret);
  248. }
  249. static inline ssize_t wld_write( int fd, const void *buffer, size_t len )
  250. {
  251. int ret;
  252. __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
  253. : "=a" (ret) : "0" (4 /* SYS_write */), "r" (fd), "c" (buffer), "d" (len) );
  254. return SYSCALL_RET(ret);
  255. }
  256. static inline int wld_mprotect( const void *addr, size_t len, int prot )
  257. {
  258. int ret;
  259. __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
  260. : "=a" (ret) : "0" (125 /* SYS_mprotect */), "r" (addr), "c" (len), "d" (prot) );
  261. return SYSCALL_RET(ret);
  262. }
  263. void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, unsigned int offset );
  264. __ASM_GLOBAL_FUNC(wld_mmap,
  265. "\tpushl %ebp\n"
  266. __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
  267. "\tpushl %ebx\n"
  268. __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
  269. "\tpushl %esi\n"
  270. __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
  271. "\tpushl %edi\n"
  272. __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
  273. "\tmovl $192,%eax\n" /* SYS_mmap2 */
  274. "\tmovl 20(%esp),%ebx\n" /* start */
  275. "\tmovl 24(%esp),%ecx\n" /* len */
  276. "\tmovl 28(%esp),%edx\n" /* prot */
  277. "\tmovl 32(%esp),%esi\n" /* flags */
  278. "\tmovl 36(%esp),%edi\n" /* fd */
  279. "\tmovl 40(%esp),%ebp\n" /* offset */
  280. "\tshrl $12,%ebp\n"
  281. "\tint $0x80\n"
  282. "\tcmpl $-4096,%eax\n"
  283. "\tjbe 2f\n"
  284. "\tcmpl $-38,%eax\n" /* ENOSYS */
  285. "\tjne 1f\n"
  286. "\tmovl $90,%eax\n" /* SYS_mmap */
  287. "\tleal 20(%esp),%ebx\n"
  288. "\tint $0x80\n"
  289. "\tcmpl $-4096,%eax\n"
  290. "\tjbe 2f\n"
  291. "1:\tmovl $-1,%eax\n"
  292. "2:\tpopl %edi\n"
  293. __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
  294. "\tpopl %esi\n"
  295. __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
  296. "\tpopl %ebx\n"
  297. __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
  298. "\tpopl %ebp\n"
  299. __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
  300. "\tret\n" )
  301. static inline int wld_prctl( int code, long arg )
  302. {
  303. int ret;
  304. __asm__ __volatile__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
  305. : "=a" (ret) : "0" (172 /* SYS_prctl */), "r" (code), "c" (arg) );
  306. return SYSCALL_RET(ret);
  307. }
  308. #elif defined(__x86_64__)
  309. void *thread_data[256];
  310. /*
  311. * The _start function is the entry and exit point of this program
  312. *
  313. * It calls wld_start, passing a pointer to the args it receives
  314. * then jumps to the address wld_start returns.
  315. */
  316. void _start(void);
  317. extern char _end[];
  318. __ASM_GLOBAL_FUNC(_start,
  319. __ASM_CFI(".cfi_undefined %rip\n\t")
  320. "movq %rsp,%rax\n\t"
  321. "leaq -144(%rsp),%rsp\n\t" /* allocate some space for extra aux values */
  322. "movq %rax,(%rsp)\n\t" /* orig stack pointer */
  323. "movq $thread_data,%rsi\n\t"
  324. "movq $0x1002,%rdi\n\t" /* ARCH_SET_FS */
  325. "movq $158,%rax\n\t" /* SYS_arch_prctl */
  326. "syscall\n\t"
  327. "movq %rsp,%rdi\n\t" /* ptr to orig stack pointer */
  328. "call wld_start\n\t"
  329. "movq (%rsp),%rsp\n\t" /* new stack pointer */
  330. "pushq %rax\n\t" /* ELF interpreter entry point */
  331. "xorq %rax,%rax\n\t"
  332. "xorq %rcx,%rcx\n\t"
  333. "xorq %rdx,%rdx\n\t"
  334. "xorq %rsi,%rsi\n\t"
  335. "xorq %rdi,%rdi\n\t"
  336. "xorq %r8,%r8\n\t"
  337. "xorq %r9,%r9\n\t"
  338. "xorq %r10,%r10\n\t"
  339. "xorq %r11,%r11\n\t"
  340. "ret")
  341. #define SYSCALL_FUNC( name, nr ) \
  342. __ASM_GLOBAL_FUNC( name, \
  343. "movq $" #nr ",%rax\n\t" \
  344. "movq %rcx,%r10\n\t" \
  345. "syscall\n\t" \
  346. "leaq 4096(%rax),%rcx\n\t" \
  347. "movq $-1,%rdx\n\t" \
  348. "cmp $4096,%rcx\n\t" \
  349. "cmovb %rdx,%rax\n\t" \
  350. "ret" )
  351. #define SYSCALL_NOERR( name, nr ) \
  352. __ASM_GLOBAL_FUNC( name, \
  353. "movq $" #nr ",%rax\n\t" \
  354. "syscall\n\t" \
  355. "ret" )
  356. void wld_exit( int code ) __attribute__((noreturn));
  357. SYSCALL_NOERR( wld_exit, 60 /* SYS_exit */ );
  358. ssize_t wld_read( int fd, void *buffer, size_t len );
  359. SYSCALL_FUNC( wld_read, 0 /* SYS_read */ );
  360. ssize_t wld_write( int fd, const void *buffer, size_t len );
  361. SYSCALL_FUNC( wld_write, 1 /* SYS_write */ );
  362. int wld_open( const char *name, int flags );
  363. SYSCALL_FUNC( wld_open, 2 /* SYS_open */ );
  364. int wld_close( int fd );
  365. SYSCALL_FUNC( wld_close, 3 /* SYS_close */ );
  366. void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
  367. SYSCALL_FUNC( wld_mmap, 9 /* SYS_mmap */ );
  368. int wld_mprotect( const void *addr, size_t len, int prot );
  369. SYSCALL_FUNC( wld_mprotect, 10 /* SYS_mprotect */ );
  370. int wld_prctl( int code, long arg );
  371. SYSCALL_FUNC( wld_prctl, 157 /* SYS_prctl */ );
  372. uid_t wld_getuid(void);
  373. SYSCALL_NOERR( wld_getuid, 102 /* SYS_getuid */ );
  374. gid_t wld_getgid(void);
  375. SYSCALL_NOERR( wld_getgid, 104 /* SYS_getgid */ );
  376. uid_t wld_geteuid(void);
  377. SYSCALL_NOERR( wld_geteuid, 107 /* SYS_geteuid */ );
  378. gid_t wld_getegid(void);
  379. SYSCALL_NOERR( wld_getegid, 108 /* SYS_getegid */ );
  380. #elif defined(__aarch64__)
  381. void *thread_data[256];
  382. /*
  383. * The _start function is the entry and exit point of this program
  384. *
  385. * It calls wld_start, passing a pointer to the args it receives
  386. * then jumps to the address wld_start returns.
  387. */
  388. void _start(void);
  389. extern char _end[];
  390. __ASM_GLOBAL_FUNC(_start,
  391. "mov x0, SP\n\t"
  392. "sub SP, SP, #144\n\t" /* allocate some space for extra aux values */
  393. "str x0, [SP]\n\t" /* orig stack pointer */
  394. "ldr x0, =thread_data\n\t"
  395. "msr tpidr_el0, x0\n\t"
  396. "mov x0, SP\n\t" /* ptr to orig stack pointer */
  397. "bl wld_start\n\t"
  398. "ldr x1, [SP]\n\t" /* new stack pointer */
  399. "mov SP, x1\n\t"
  400. "mov x30, x0\n\t"
  401. "mov x0, #0\n\t"
  402. "mov x1, #0\n\t"
  403. "mov x2, #0\n\t"
  404. "mov x3, #0\n\t"
  405. "mov x4, #0\n\t"
  406. "mov x5, #0\n\t"
  407. "mov x6, #0\n\t"
  408. "mov x7, #0\n\t"
  409. "mov x8, #0\n\t"
  410. "mov x9, #0\n\t"
  411. "mov x10, #0\n\t"
  412. "mov x11, #0\n\t"
  413. "mov x12, #0\n\t"
  414. "mov x13, #0\n\t"
  415. "mov x14, #0\n\t"
  416. "mov x15, #0\n\t"
  417. "mov x16, #0\n\t"
  418. "mov x17, #0\n\t"
  419. "mov x18, #0\n\t"
  420. "ret")
  421. #define SYSCALL_FUNC( name, nr ) \
  422. __ASM_GLOBAL_FUNC( name, \
  423. "stp x8, x9, [SP, #-16]!\n\t" \
  424. "mov x8, #" #nr "\n\t" \
  425. "svc #0\n\t" \
  426. "ldp x8, x9, [SP], #16\n\t" \
  427. "cmn x0, #1, lsl#12\n\t" \
  428. "cinv x0, x0, hi\n\t" \
  429. "b.hi 1f\n\t" \
  430. "ret\n\t" \
  431. "1: mov x0, #-1\n\t" \
  432. "ret" )
  433. #define SYSCALL_NOERR( name, nr ) \
  434. __ASM_GLOBAL_FUNC( name, \
  435. "stp x8, x9, [SP, #-16]!\n\t" \
  436. "mov x8, #" #nr "\n\t" \
  437. "svc #0\n\t" \
  438. "ldp x8, x9, [SP], #16\n\t" \
  439. "ret" )
  440. void wld_exit( int code ) __attribute__((noreturn));
  441. SYSCALL_NOERR( wld_exit, 93 /* SYS_exit */ );
  442. ssize_t wld_read( int fd, void *buffer, size_t len );
  443. SYSCALL_FUNC( wld_read, 63 /* SYS_read */ );
  444. ssize_t wld_write( int fd, const void *buffer, size_t len );
  445. SYSCALL_FUNC( wld_write, 64 /* SYS_write */ );
  446. int wld_openat( int dirfd, const char *name, int flags );
  447. SYSCALL_FUNC( wld_openat, 56 /* SYS_openat */ );
  448. int wld_open( const char *name, int flags )
  449. {
  450. return wld_openat(-100 /* AT_FDCWD */, name, flags);
  451. }
  452. int wld_close( int fd );
  453. SYSCALL_FUNC( wld_close, 57 /* SYS_close */ );
  454. void *wld_mmap( void *start, size_t len, int prot, int flags, int fd, off_t offset );
  455. SYSCALL_FUNC( wld_mmap, 222 /* SYS_mmap */ );
  456. int wld_mprotect( const void *addr, size_t len, int prot );
  457. SYSCALL_FUNC( wld_mprotect, 226 /* SYS_mprotect */ );
  458. int wld_prctl( int code, long arg );
  459. SYSCALL_FUNC( wld_prctl, 167 /* SYS_prctl */ );
  460. uid_t wld_getuid(void);
  461. SYSCALL_NOERR( wld_getuid, 174 /* SYS_getuid */ );
  462. gid_t wld_getgid(void);
  463. SYSCALL_NOERR( wld_getgid, 176 /* SYS_getgid */ );
  464. uid_t wld_geteuid(void);
  465. SYSCALL_NOERR( wld_geteuid, 175 /* SYS_geteuid */ );
  466. gid_t wld_getegid(void);
  467. SYSCALL_NOERR( wld_getegid, 177 /* SYS_getegid */ );
  468. #else
  469. #error preloader not implemented for this CPU
  470. #endif
  471. /* replacement for libc functions */
  472. static int wld_strcmp( const char *str1, const char *str2 )
  473. {
  474. while (*str1 && (*str1 == *str2)) { str1++; str2++; }
  475. return *str1 - *str2;
  476. }
  477. static int wld_strncmp( const char *str1, const char *str2, size_t len )
  478. {
  479. if (len <= 0) return 0;
  480. while ((--len > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
  481. return *str1 - *str2;
  482. }
  483. static inline void *wld_memset( void *dest, int val, size_t len )
  484. {
  485. char *dst = dest;
  486. while (len--) *dst++ = val;
  487. return dest;
  488. }
  489. /*
  490. * wld_printf - just the basics
  491. *
  492. * %x prints a hex number
  493. * %s prints a string
  494. * %p prints a pointer
  495. */
  496. static int wld_vsprintf(char *buffer, const char *fmt, va_list args )
  497. {
  498. static const char hex_chars[16] = "0123456789abcdef";
  499. const char *p = fmt;
  500. char *str = buffer;
  501. int i;
  502. while( *p )
  503. {
  504. if( *p == '%' )
  505. {
  506. p++;
  507. if( *p == 'x' )
  508. {
  509. unsigned int x = va_arg( args, unsigned int );
  510. for (i = 2*sizeof(x) - 1; i >= 0; i--)
  511. *str++ = hex_chars[(x>>(i*4))&0xf];
  512. }
  513. else if (p[0] == 'l' && p[1] == 'x')
  514. {
  515. unsigned long x = va_arg( args, unsigned long );
  516. for (i = 2*sizeof(x) - 1; i >= 0; i--)
  517. *str++ = hex_chars[(x>>(i*4))&0xf];
  518. p++;
  519. }
  520. else if( *p == 'p' )
  521. {
  522. unsigned long x = (unsigned long)va_arg( args, void * );
  523. for (i = 2*sizeof(x) - 1; i >= 0; i--)
  524. *str++ = hex_chars[(x>>(i*4))&0xf];
  525. }
  526. else if( *p == 's' )
  527. {
  528. char *s = va_arg( args, char * );
  529. while(*s)
  530. *str++ = *s++;
  531. }
  532. else if( *p == 0 )
  533. break;
  534. p++;
  535. }
  536. *str++ = *p++;
  537. }
  538. *str = 0;
  539. return str - buffer;
  540. }
  541. static __attribute__((format(printf,1,2))) void wld_printf(const char *fmt, ... )
  542. {
  543. va_list args;
  544. char buffer[256];
  545. int len;
  546. va_start( args, fmt );
  547. len = wld_vsprintf(buffer, fmt, args );
  548. va_end( args );
  549. wld_write(2, buffer, len);
  550. }
  551. static __attribute__((noreturn,format(printf,1,2))) void fatal_error(const char *fmt, ... )
  552. {
  553. va_list args;
  554. char buffer[256];
  555. int len;
  556. va_start( args, fmt );
  557. len = wld_vsprintf(buffer, fmt, args );
  558. va_end( args );
  559. wld_write(2, buffer, len);
  560. wld_exit(1);
  561. }
  562. #ifdef DUMP_AUX_INFO
  563. /*
  564. * Dump interesting bits of the ELF auxv_t structure that is passed
  565. * as the 4th parameter to the _start function
  566. */
  567. static void dump_auxiliary( struct wld_auxv *av )
  568. {
  569. #define NAME(at) { at, #at }
  570. static const struct { int val; const char *name; } names[] =
  571. {
  572. NAME(AT_BASE),
  573. NAME(AT_CLKTCK),
  574. NAME(AT_EGID),
  575. NAME(AT_ENTRY),
  576. NAME(AT_EUID),
  577. NAME(AT_FLAGS),
  578. NAME(AT_GID),
  579. NAME(AT_HWCAP),
  580. NAME(AT_PAGESZ),
  581. NAME(AT_PHDR),
  582. NAME(AT_PHENT),
  583. NAME(AT_PHNUM),
  584. NAME(AT_PLATFORM),
  585. NAME(AT_SYSINFO),
  586. NAME(AT_SYSINFO_EHDR),
  587. NAME(AT_UID),
  588. { 0, NULL }
  589. };
  590. #undef NAME
  591. int i;
  592. for ( ; av->a_type != AT_NULL; av++)
  593. {
  594. for (i = 0; names[i].name; i++) if (names[i].val == av->a_type) break;
  595. if (names[i].name) wld_printf("%s = %lx\n", names[i].name, (unsigned long)av->a_un.a_val);
  596. else wld_printf( "%lx = %lx\n", (unsigned long)av->a_type, (unsigned long)av->a_un.a_val );
  597. }
  598. }
  599. #endif
  600. /*
  601. * set_auxiliary_values
  602. *
  603. * Set the new auxiliary values
  604. */
  605. static void set_auxiliary_values( struct wld_auxv *av, const struct wld_auxv *new_av,
  606. const struct wld_auxv *delete_av, void **stack )
  607. {
  608. int i, j, av_count = 0, new_count = 0, delete_count = 0;
  609. char *src, *dst;
  610. /* count how many aux values we have already */
  611. while (av[av_count].a_type != AT_NULL) av_count++;
  612. /* delete unwanted values */
  613. for (j = 0; delete_av[j].a_type != AT_NULL; j++)
  614. {
  615. for (i = 0; i < av_count; i++) if (av[i].a_type == delete_av[j].a_type)
  616. {
  617. av[i].a_type = av[av_count-1].a_type;
  618. av[i].a_un.a_val = av[av_count-1].a_un.a_val;
  619. av[--av_count].a_type = AT_NULL;
  620. delete_count++;
  621. break;
  622. }
  623. }
  624. /* count how many values we have in new_av that aren't in av */
  625. for (j = 0; new_av[j].a_type != AT_NULL; j++)
  626. {
  627. for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
  628. if (i == av_count) new_count++;
  629. }
  630. src = (char *)*stack;
  631. dst = src - (new_count - delete_count) * sizeof(*av);
  632. dst = (char *)((unsigned long)dst & ~15);
  633. if (dst < src) /* need to make room for the extra values */
  634. {
  635. int len = (char *)(av + av_count + 1) - src;
  636. for (i = 0; i < len; i++) dst[i] = src[i];
  637. }
  638. else if (dst > src) /* get rid of unused values */
  639. {
  640. int len = (char *)(av + av_count + 1) - src;
  641. for (i = len - 1; i >= 0; i--) dst[i] = src[i];
  642. }
  643. *stack = dst;
  644. av = (struct wld_auxv *)((char *)av + (dst - src));
  645. /* now set the values */
  646. for (j = 0; new_av[j].a_type != AT_NULL; j++)
  647. {
  648. for (i = 0; i < av_count; i++) if (av[i].a_type == new_av[j].a_type) break;
  649. if (i < av_count) av[i].a_un.a_val = new_av[j].a_un.a_val;
  650. else
  651. {
  652. av[av_count].a_type = new_av[j].a_type;
  653. av[av_count].a_un.a_val = new_av[j].a_un.a_val;
  654. av_count++;
  655. }
  656. }
  657. #ifdef DUMP_AUX_INFO
  658. wld_printf("New auxiliary info:\n");
  659. dump_auxiliary( av );
  660. #endif
  661. }
  662. /*
  663. * get_auxiliary
  664. *
  665. * Get a field of the auxiliary structure
  666. */
  667. static int get_auxiliary( struct wld_auxv *av, int type, int def_val )
  668. {
  669. for ( ; av->a_type != AT_NULL; av++)
  670. if( av->a_type == type ) return av->a_un.a_val;
  671. return def_val;
  672. }
  673. /*
  674. * map_so_lib
  675. *
  676. * modelled after _dl_map_object_from_fd() from glibc-2.3.1/elf/dl-load.c
  677. *
  678. * This function maps the segments from an ELF object, and optionally
  679. * stores information about the mapping into the auxv_t structure.
  680. */
  681. static void map_so_lib( const char *name, struct wld_link_map *l)
  682. {
  683. int fd;
  684. unsigned char buf[0x800];
  685. ElfW(Ehdr) *header = (ElfW(Ehdr)*)buf;
  686. ElfW(Phdr) *phdr, *ph;
  687. /* Scan the program header table, collecting its load commands. */
  688. struct loadcmd
  689. {
  690. ElfW(Addr) mapstart, mapend, dataend, allocend;
  691. off_t mapoff;
  692. int prot;
  693. } loadcmds[16], *c;
  694. size_t nloadcmds = 0, maplength;
  695. fd = wld_open( name, O_RDONLY );
  696. if (fd == -1) fatal_error("%s: could not open\n", name );
  697. if (wld_read( fd, buf, sizeof(buf) ) != sizeof(buf))
  698. fatal_error("%s: failed to read ELF header\n", name);
  699. phdr = (void*) (((unsigned char*)buf) + header->e_phoff);
  700. if( ( header->e_ident[0] != 0x7f ) ||
  701. ( header->e_ident[1] != 'E' ) ||
  702. ( header->e_ident[2] != 'L' ) ||
  703. ( header->e_ident[3] != 'F' ) )
  704. fatal_error( "%s: not an ELF binary... don't know how to load it\n", name );
  705. #ifdef __i386__
  706. if( header->e_machine != EM_386 )
  707. fatal_error("%s: not an i386 ELF binary... don't know how to load it\n", name );
  708. #elif defined(__x86_64__)
  709. if( header->e_machine != EM_X86_64 )
  710. fatal_error("%s: not an x86-64 ELF binary... don't know how to load it\n", name );
  711. #elif defined(__aarch64__)
  712. if( header->e_machine != EM_AARCH64 )
  713. fatal_error("%s: not an aarch64 ELF binary... don't know how to load it\n", name );
  714. #endif
  715. if (header->e_phnum > sizeof(loadcmds)/sizeof(loadcmds[0]))
  716. fatal_error( "%s: oops... not enough space for load commands\n", name );
  717. maplength = header->e_phnum * sizeof (ElfW(Phdr));
  718. if (header->e_phoff + maplength > sizeof(buf))
  719. fatal_error( "%s: oops... not enough space for ELF headers\n", name );
  720. l->l_ld = 0;
  721. l->l_addr = 0;
  722. l->l_phdr = 0;
  723. l->l_phnum = header->e_phnum;
  724. l->l_entry = header->e_entry;
  725. l->l_interp = 0;
  726. for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
  727. {
  728. #ifdef DUMP_SEGMENTS
  729. wld_printf( "ph = %p\n", ph );
  730. wld_printf( " p_type = %lx\n", (unsigned long)ph->p_type );
  731. wld_printf( " p_flags = %lx\n", (unsigned long)ph->p_flags );
  732. wld_printf( " p_offset = %lx\n", (unsigned long)ph->p_offset );
  733. wld_printf( " p_vaddr = %lx\n", (unsigned long)ph->p_vaddr );
  734. wld_printf( " p_paddr = %lx\n", (unsigned long)ph->p_paddr );
  735. wld_printf( " p_filesz = %lx\n", (unsigned long)ph->p_filesz );
  736. wld_printf( " p_memsz = %lx\n", (unsigned long)ph->p_memsz );
  737. wld_printf( " p_align = %lx\n", (unsigned long)ph->p_align );
  738. #endif
  739. switch (ph->p_type)
  740. {
  741. /* These entries tell us where to find things once the file's
  742. segments are mapped in. We record the addresses it says
  743. verbatim, and later correct for the run-time load address. */
  744. case PT_DYNAMIC:
  745. l->l_ld = (void *) ph->p_vaddr;
  746. l->l_ldnum = ph->p_memsz / sizeof (Elf32_Dyn);
  747. break;
  748. case PT_PHDR:
  749. l->l_phdr = (void *) ph->p_vaddr;
  750. break;
  751. case PT_LOAD:
  752. {
  753. if ((ph->p_align & page_mask) != 0)
  754. fatal_error( "%s: ELF load command alignment not page-aligned\n", name );
  755. if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)
  756. fatal_error( "%s: ELF load command address/offset not properly aligned\n", name );
  757. c = &loadcmds[nloadcmds++];
  758. c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
  759. c->mapend = ((ph->p_vaddr + ph->p_filesz + page_mask) & ~page_mask);
  760. c->dataend = ph->p_vaddr + ph->p_filesz;
  761. c->allocend = ph->p_vaddr + ph->p_memsz;
  762. c->mapoff = ph->p_offset & ~(ph->p_align - 1);
  763. c->prot = 0;
  764. if (ph->p_flags & PF_R)
  765. c->prot |= PROT_READ;
  766. if (ph->p_flags & PF_W)
  767. c->prot |= PROT_WRITE;
  768. if (ph->p_flags & PF_X)
  769. c->prot |= PROT_EXEC;
  770. }
  771. break;
  772. case PT_INTERP:
  773. l->l_interp = ph->p_vaddr;
  774. break;
  775. case PT_TLS:
  776. /*
  777. * We don't need to set anything up because we're
  778. * emulating the kernel, not ld-linux.so.2
  779. * The ELF loader will set up the TLS data itself.
  780. */
  781. case PT_SHLIB:
  782. case PT_NOTE:
  783. default:
  784. break;
  785. }
  786. }
  787. /* Now process the load commands and map segments into memory. */
  788. if (!nloadcmds)
  789. fatal_error( "%s: no segments to load\n", name );
  790. c = loadcmds;
  791. /* Length of the sections to be loaded. */
  792. maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
  793. if( header->e_type == ET_DYN )
  794. {
  795. ElfW(Addr) mappref;
  796. mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
  797. - MAP_BASE_ADDR (l));
  798. /* Remember which part of the address space this object uses. */
  799. l->l_map_start = (ElfW(Addr)) wld_mmap ((void *) mappref, maplength,
  800. c->prot, MAP_COPY | MAP_FILE,
  801. fd, c->mapoff);
  802. /* wld_printf("set : offset = %x\n", c->mapoff); */
  803. /* wld_printf("l->l_map_start = %x\n", l->l_map_start); */
  804. l->l_map_end = l->l_map_start + maplength;
  805. l->l_addr = l->l_map_start - c->mapstart;
  806. wld_mprotect ((caddr_t) (l->l_addr + c->mapend),
  807. loadcmds[nloadcmds - 1].allocend - c->mapend,
  808. PROT_NONE);
  809. goto postmap;
  810. }
  811. else
  812. {
  813. /* sanity check */
  814. if ((char *)c->mapstart + maplength > preloader_start &&
  815. (char *)c->mapstart <= preloader_end)
  816. fatal_error( "%s: binary overlaps preloader (%p-%p)\n",
  817. name, (char *)c->mapstart, (char *)c->mapstart + maplength );
  818. ELF_FIXED_ADDRESS (loader, c->mapstart);
  819. }
  820. /* Remember which part of the address space this object uses. */
  821. l->l_map_start = c->mapstart + l->l_addr;
  822. l->l_map_end = l->l_map_start + maplength;
  823. while (c < &loadcmds[nloadcmds])
  824. {
  825. if (c->mapend > c->mapstart)
  826. /* Map the segment contents from the file. */
  827. wld_mmap ((void *) (l->l_addr + c->mapstart),
  828. c->mapend - c->mapstart, c->prot,
  829. MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff);
  830. postmap:
  831. if (l->l_phdr == 0
  832. && (ElfW(Off)) c->mapoff <= header->e_phoff
  833. && ((size_t) (c->mapend - c->mapstart + c->mapoff)
  834. >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
  835. /* Found the program header in this segment. */
  836. l->l_phdr = (void *)(unsigned long)(c->mapstart + header->e_phoff - c->mapoff);
  837. if (c->allocend > c->dataend)
  838. {
  839. /* Extra zero pages should appear at the end of this segment,
  840. after the data mapped from the file. */
  841. ElfW(Addr) zero, zeroend, zeropage;
  842. zero = l->l_addr + c->dataend;
  843. zeroend = l->l_addr + c->allocend;
  844. zeropage = (zero + page_mask) & ~page_mask;
  845. /*
  846. * This is different from the dl-load load...
  847. * ld-linux.so.2 relies on the whole page being zero'ed
  848. */
  849. zeroend = (zeroend + page_mask) & ~page_mask;
  850. if (zeroend < zeropage)
  851. {
  852. /* All the extra data is in the last page of the segment.
  853. We can just zero it. */
  854. zeropage = zeroend;
  855. }
  856. if (zeropage > zero)
  857. {
  858. /* Zero the final part of the last page of the segment. */
  859. if ((c->prot & PROT_WRITE) == 0)
  860. {
  861. /* Dag nab it. */
  862. wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot|PROT_WRITE);
  863. }
  864. wld_memset ((void *) zero, '\0', zeropage - zero);
  865. if ((c->prot & PROT_WRITE) == 0)
  866. wld_mprotect ((caddr_t) (zero & ~page_mask), page_size, c->prot);
  867. }
  868. if (zeroend > zeropage)
  869. {
  870. /* Map the remaining zero pages in from the zero fill FD. */
  871. wld_mmap ((caddr_t) zeropage, zeroend - zeropage,
  872. c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
  873. -1, 0);
  874. }
  875. }
  876. ++c;
  877. }
  878. if (l->l_phdr == NULL) fatal_error("no program header\n");
  879. l->l_phdr = (void *)((ElfW(Addr))l->l_phdr + l->l_addr);
  880. l->l_entry += l->l_addr;
  881. wld_close( fd );
  882. }
  883. static unsigned int wld_elf_hash( const char *name )
  884. {
  885. unsigned int hi, hash = 0;
  886. while (*name)
  887. {
  888. hash = (hash << 4) + (unsigned char)*name++;
  889. hi = hash & 0xf0000000;
  890. hash ^= hi;
  891. hash ^= hi >> 24;
  892. }
  893. return hash;
  894. }
  895. static unsigned int gnu_hash( const char *name )
  896. {
  897. unsigned int h = 5381;
  898. while (*name) h = h * 33 + (unsigned char)*name++;
  899. return h;
  900. }
  901. /*
  902. * Find a symbol in the symbol table of the executable loaded
  903. */
  904. static void *find_symbol( const struct wld_link_map *map, const char *var, int type )
  905. {
  906. const ElfW(Dyn) *dyn = NULL;
  907. const ElfW(Phdr) *ph;
  908. const ElfW(Sym) *symtab = NULL;
  909. const Elf32_Word *hashtab = NULL;
  910. const Elf32_Word *gnu_hashtab = NULL;
  911. const char *strings = NULL;
  912. Elf32_Word idx;
  913. /* check the values */
  914. #ifdef DUMP_SYMS
  915. wld_printf("%p %x\n", map->l_phdr, map->l_phnum );
  916. #endif
  917. /* parse the (already loaded) ELF executable's header */
  918. for (ph = map->l_phdr; ph < &map->l_phdr[map->l_phnum]; ++ph)
  919. {
  920. if( PT_DYNAMIC == ph->p_type )
  921. {
  922. dyn = (void *)(ph->p_vaddr + map->l_addr);
  923. break;
  924. }
  925. }
  926. if( !dyn ) return NULL;
  927. while( dyn->d_tag )
  928. {
  929. if( dyn->d_tag == DT_STRTAB )
  930. strings = (const char*)(dyn->d_un.d_ptr + map->l_addr);
  931. if( dyn->d_tag == DT_SYMTAB )
  932. symtab = (const ElfW(Sym) *)(dyn->d_un.d_ptr + map->l_addr);
  933. if( dyn->d_tag == DT_HASH )
  934. hashtab = (const Elf32_Word *)(dyn->d_un.d_ptr + map->l_addr);
  935. if( dyn->d_tag == DT_GNU_HASH )
  936. gnu_hashtab = (const Elf32_Word *)(dyn->d_un.d_ptr + map->l_addr);
  937. #ifdef DUMP_SYMS
  938. wld_printf("%lx %p\n", (unsigned long)dyn->d_tag, (void *)dyn->d_un.d_ptr );
  939. #endif
  940. dyn++;
  941. }
  942. if( (!symtab) || (!strings) ) return NULL;
  943. if (gnu_hashtab) /* new style hash table */
  944. {
  945. const unsigned int hash = gnu_hash(var);
  946. const Elf32_Word nbuckets = gnu_hashtab[0];
  947. const Elf32_Word symbias = gnu_hashtab[1];
  948. const Elf32_Word nwords = gnu_hashtab[2];
  949. const ElfW(Addr) *bitmask = (const ElfW(Addr) *)(gnu_hashtab + 4);
  950. const Elf32_Word *buckets = (const Elf32_Word *)(bitmask + nwords);
  951. const Elf32_Word *chains = buckets + nbuckets - symbias;
  952. if (!(idx = buckets[hash % nbuckets])) return NULL;
  953. do
  954. {
  955. if ((chains[idx] & ~1u) == (hash & ~1u) &&
  956. ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
  957. ELF32_ST_TYPE(symtab[idx].st_info) == type &&
  958. !wld_strcmp( strings + symtab[idx].st_name, var ))
  959. goto found;
  960. } while (!(chains[idx++] & 1u));
  961. }
  962. else if (hashtab) /* old style hash table */
  963. {
  964. const unsigned int hash = wld_elf_hash(var);
  965. const Elf32_Word nbuckets = hashtab[0];
  966. const Elf32_Word *buckets = hashtab + 2;
  967. const Elf32_Word *chains = buckets + nbuckets;
  968. for (idx = buckets[hash % nbuckets]; idx; idx = chains[idx])
  969. {
  970. if (ELF32_ST_BIND(symtab[idx].st_info) == STB_GLOBAL &&
  971. ELF32_ST_TYPE(symtab[idx].st_info) == type &&
  972. !wld_strcmp( strings + symtab[idx].st_name, var ))
  973. goto found;
  974. }
  975. }
  976. return NULL;
  977. found:
  978. #ifdef DUMP_SYMS
  979. wld_printf("Found %s -> %p\n", strings + symtab[idx].st_name, (void *)symtab[idx].st_value );
  980. #endif
  981. return (void *)(symtab[idx].st_value + map->l_addr);
  982. }
  983. /*
  984. * preload_reserve
  985. *
  986. * Reserve a range specified in string format
  987. */
  988. static void preload_reserve( const char *str )
  989. {
  990. const char *p;
  991. unsigned long result = 0;
  992. void *start = NULL, *end = NULL;
  993. int i, first = 1;
  994. for (p = str; *p; p++)
  995. {
  996. if (*p >= '0' && *p <= '9') result = result * 16 + *p - '0';
  997. else if (*p >= 'a' && *p <= 'f') result = result * 16 + *p - 'a' + 10;
  998. else if (*p >= 'A' && *p <= 'F') result = result * 16 + *p - 'A' + 10;
  999. else if (*p == '-')
  1000. {
  1001. if (!first) goto error;
  1002. start = (void *)(result & ~page_mask);
  1003. result = 0;
  1004. first = 0;
  1005. }
  1006. else goto error;
  1007. }
  1008. if (!first) end = (void *)((result + page_mask) & ~page_mask);
  1009. else if (result) goto error; /* single value '0' is allowed */
  1010. /* sanity checks */
  1011. if (end <= start) start = end = NULL;
  1012. else if ((char *)end > preloader_start &&
  1013. (char *)start <= preloader_end)
  1014. {
  1015. wld_printf( "WINEPRELOADRESERVE range %p-%p overlaps preloader %p-%p\n",
  1016. start, end, preloader_start, preloader_end );
  1017. start = end = NULL;
  1018. }
  1019. /* check for overlap with low memory areas */
  1020. for (i = 0; preload_info[i].size; i++)
  1021. {
  1022. if ((char *)preload_info[i].addr > (char *)0x00110000) break;
  1023. if ((char *)end <= (char *)preload_info[i].addr + preload_info[i].size)
  1024. {
  1025. start = end = NULL;
  1026. break;
  1027. }
  1028. if ((char *)start < (char *)preload_info[i].addr + preload_info[i].size)
  1029. start = (char *)preload_info[i].addr + preload_info[i].size;
  1030. }
  1031. while (preload_info[i].size) i++;
  1032. preload_info[i].addr = start;
  1033. preload_info[i].size = (char *)end - (char *)start;
  1034. return;
  1035. error:
  1036. fatal_error( "invalid WINEPRELOADRESERVE value '%s'\n", str );
  1037. }
  1038. /* check if address is in one of the reserved ranges */
  1039. static int is_addr_reserved( const void *addr )
  1040. {
  1041. int i;
  1042. for (i = 0; preload_info[i].size; i++)
  1043. {
  1044. if ((const char *)addr >= (const char *)preload_info[i].addr &&
  1045. (const char *)addr < (const char *)preload_info[i].addr + preload_info[i].size)
  1046. return 1;
  1047. }
  1048. return 0;
  1049. }
  1050. /* remove a range from the preload list */
  1051. static void remove_preload_range( int i )
  1052. {
  1053. while (preload_info[i].size)
  1054. {
  1055. preload_info[i].addr = preload_info[i+1].addr;
  1056. preload_info[i].size = preload_info[i+1].size;
  1057. i++;
  1058. }
  1059. }
  1060. /*
  1061. * is_in_preload_range
  1062. *
  1063. * Check if address of the given aux value is in one of the reserved ranges
  1064. */
  1065. static int is_in_preload_range( const struct wld_auxv *av, int type )
  1066. {
  1067. while (av->a_type != AT_NULL)
  1068. {
  1069. if (av->a_type == type) return is_addr_reserved( (const void *)av->a_un.a_val );
  1070. av++;
  1071. }
  1072. return 0;
  1073. }
  1074. /* set the process name if supported */
  1075. static void set_process_name( int argc, char *argv[] )
  1076. {
  1077. int i;
  1078. unsigned int off;
  1079. char *p, *name, *end;
  1080. /* set the process short name */
  1081. for (p = name = argv[1]; *p; p++) if (p[0] == '/' && p[1]) name = p + 1;
  1082. if (wld_prctl( 15 /* PR_SET_NAME */, (long)name ) == -1) return;
  1083. /* find the end of the argv array and move everything down */
  1084. end = argv[argc - 1];
  1085. while (*end) end++;
  1086. off = argv[1] - argv[0];
  1087. for (p = argv[1]; p <= end; p++) *(p - off) = *p;
  1088. wld_memset( end - off, 0, off );
  1089. for (i = 1; i < argc; i++) argv[i] -= off;
  1090. }
  1091. /*
  1092. * wld_start
  1093. *
  1094. * Repeat the actions the kernel would do when loading a dynamically linked .so
  1095. * Load the binary and then its ELF interpreter.
  1096. * Note, we assume that the binary is a dynamically linked ELF shared object.
  1097. */
  1098. void* wld_start( void **stack )
  1099. {
  1100. long i, *pargc;
  1101. char **argv, **p;
  1102. char *interp, *reserve = NULL;
  1103. struct wld_auxv new_av[8], delete_av[3], *av;
  1104. struct wld_link_map main_binary_map, ld_so_map;
  1105. struct wine_preload_info **wine_main_preload_info;
  1106. pargc = *stack;
  1107. argv = (char **)pargc + 1;
  1108. if (*pargc < 2) fatal_error( "Usage: %s wine_binary [args]\n", argv[0] );
  1109. /* skip over the parameters */
  1110. p = argv + *pargc + 1;
  1111. /* skip over the environment */
  1112. while (*p)
  1113. {
  1114. static const char res[] = "WINEPRELOADRESERVE=";
  1115. if (!wld_strncmp( *p, res, sizeof(res)-1 )) reserve = *p + sizeof(res) - 1;
  1116. p++;
  1117. }
  1118. av = (struct wld_auxv *)(p+1);
  1119. page_size = get_auxiliary( av, AT_PAGESZ, 4096 );
  1120. page_mask = page_size - 1;
  1121. preloader_start = (char *)_start - ((unsigned long)_start & page_mask);
  1122. preloader_end = (char *)((unsigned long)(_end + page_mask) & ~page_mask);
  1123. #ifdef DUMP_AUX_INFO
  1124. wld_printf( "stack = %p\n", *stack );
  1125. for( i = 0; i < *pargc; i++ ) wld_printf("argv[%lx] = %s\n", i, argv[i]);
  1126. dump_auxiliary( av );
  1127. #endif
  1128. /* reserve memory that Wine needs */
  1129. if (reserve) preload_reserve( reserve );
  1130. for (i = 0; preload_info[i].size; i++)
  1131. {
  1132. if ((char *)av >= (char *)preload_info[i].addr &&
  1133. (char *)pargc <= (char *)preload_info[i].addr + preload_info[i].size)
  1134. {
  1135. remove_preload_range( i );
  1136. i--;
  1137. }
  1138. else if (wld_mmap( preload_info[i].addr, preload_info[i].size, PROT_NONE,
  1139. MAP_FIXED | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0 ) == (void *)-1)
  1140. {
  1141. /* don't warn for low 64k */
  1142. if (preload_info[i].addr >= (void *)0x10000
  1143. #ifdef __aarch64__
  1144. && preload_info[i].addr < (void *)0x7fffffffff /* ARM64 address space might end here*/
  1145. #endif
  1146. )
  1147. wld_printf( "preloader: Warning: failed to reserve range %p-%p\n",
  1148. preload_info[i].addr, (char *)preload_info[i].addr + preload_info[i].size );
  1149. remove_preload_range( i );
  1150. i--;
  1151. }
  1152. }
  1153. /* add an executable page at the top of the address space to defeat
  1154. * broken no-exec protections that play with the code selector limit */
  1155. if (is_addr_reserved( (char *)0x80000000 - page_size ))
  1156. wld_mprotect( (char *)0x80000000 - page_size, page_size, PROT_EXEC | PROT_READ );
  1157. /* load the main binary */
  1158. map_so_lib( argv[1], &main_binary_map );
  1159. /* load the ELF interpreter */
  1160. interp = (char *)main_binary_map.l_addr + main_binary_map.l_interp;
  1161. map_so_lib( interp, &ld_so_map );
  1162. /* store pointer to the preload info into the appropriate main binary variable */
  1163. wine_main_preload_info = find_symbol( &main_binary_map, "wine_main_preload_info", STT_OBJECT );
  1164. if (wine_main_preload_info) *wine_main_preload_info = preload_info;
  1165. else wld_printf( "wine_main_preload_info not found\n" );
  1166. #define SET_NEW_AV(n,type,val) new_av[n].a_type = (type); new_av[n].a_un.a_val = (val);
  1167. SET_NEW_AV( 0, AT_PHDR, (unsigned long)main_binary_map.l_phdr );
  1168. SET_NEW_AV( 1, AT_PHENT, sizeof(ElfW(Phdr)) );
  1169. SET_NEW_AV( 2, AT_PHNUM, main_binary_map.l_phnum );
  1170. SET_NEW_AV( 3, AT_PAGESZ, page_size );
  1171. SET_NEW_AV( 4, AT_BASE, ld_so_map.l_addr );
  1172. SET_NEW_AV( 5, AT_FLAGS, get_auxiliary( av, AT_FLAGS, 0 ) );
  1173. SET_NEW_AV( 6, AT_ENTRY, main_binary_map.l_entry );
  1174. SET_NEW_AV( 7, AT_NULL, 0 );
  1175. #undef SET_NEW_AV
  1176. i = 0;
  1177. /* delete sysinfo values if addresses conflict */
  1178. if (is_in_preload_range( av, AT_SYSINFO ) || is_in_preload_range( av, AT_SYSINFO_EHDR ))
  1179. {
  1180. delete_av[i++].a_type = AT_SYSINFO;
  1181. delete_av[i++].a_type = AT_SYSINFO_EHDR;
  1182. }
  1183. delete_av[i].a_type = AT_NULL;
  1184. /* get rid of first argument */
  1185. set_process_name( *pargc, argv );
  1186. pargc[1] = pargc[0] - 1;
  1187. *stack = pargc + 1;
  1188. set_auxiliary_values( av, new_av, delete_av, stack );
  1189. #ifdef DUMP_AUX_INFO
  1190. wld_printf("new stack = %p\n", *stack);
  1191. wld_printf("jumping to %p\n", (void *)ld_so_map.l_entry);
  1192. #endif
  1193. #ifdef DUMP_MAPS
  1194. {
  1195. char buffer[1024];
  1196. int len, fd = wld_open( "/proc/self/maps", O_RDONLY );
  1197. if (fd != -1)
  1198. {
  1199. while ((len = wld_read( fd, buffer, sizeof(buffer) )) > 0) wld_write( 2, buffer, len );
  1200. wld_close( fd );
  1201. }
  1202. }
  1203. #endif
  1204. return (void *)ld_so_map.l_entry;
  1205. }
  1206. #endif /* __linux__ */