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