preloader.c 48 KB

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