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