vserver 1.9.5.x5
[linux-2.6.git] / fs / binfmt_elf.c
1 /*
2  * linux/fs/binfmt_elf.c
3  *
4  * These are the functions used to load ELF format executables as used
5  * on SVr4 machines.  Information on the format may be found in the book
6  * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
7  * Tools".
8  *
9  * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
10  */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/fs.h>
15 #include <linux/stat.h>
16 #include <linux/time.h>
17 #include <linux/mm.h>
18 #include <linux/mman.h>
19 #include <linux/a.out.h>
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/binfmts.h>
23 #include <linux/string.h>
24 #include <linux/file.h>
25 #include <linux/fcntl.h>
26 #include <linux/ptrace.h>
27 #include <linux/slab.h>
28 #include <linux/shm.h>
29 #include <linux/personality.h>
30 #include <linux/elfcore.h>
31 #include <linux/init.h>
32 #include <linux/highuid.h>
33 #include <linux/smp.h>
34 #include <linux/smp_lock.h>
35 #include <linux/compiler.h>
36 #include <linux/highmem.h>
37 #include <linux/pagemap.h>
38 #include <linux/security.h>
39 #include <linux/syscalls.h>
40 #include <linux/vs_memory.h>
41
42 #include <asm/uaccess.h>
43 #include <asm/param.h>
44 #include <asm/page.h>
45
46 #include <linux/elf.h>
47
48 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs);
49 static int load_elf_library(struct file*);
50 static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int);
51 extern int dump_fpu (struct pt_regs *, elf_fpregset_t *);
52
53 #ifndef elf_addr_t
54 #define elf_addr_t unsigned long
55 #endif
56
57 /*
58  * If we don't support core dumping, then supply a NULL so we
59  * don't even try.
60  */
61 #ifdef USE_ELF_CORE_DUMP
62 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file);
63 #else
64 #define elf_core_dump   NULL
65 #endif
66
67 #if ELF_EXEC_PAGESIZE > PAGE_SIZE
68 # define ELF_MIN_ALIGN  ELF_EXEC_PAGESIZE
69 #else
70 # define ELF_MIN_ALIGN  PAGE_SIZE
71 #endif
72
73 #ifndef ELF_CORE_EFLAGS
74 #define ELF_CORE_EFLAGS 0
75 #endif
76
77 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
78 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
79 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
80
81 static struct linux_binfmt elf_format = {
82                 .module         = THIS_MODULE,
83                 .load_binary    = load_elf_binary,
84                 .load_shlib     = load_elf_library,
85                 .core_dump      = elf_core_dump,
86                 .min_coredump   = ELF_EXEC_PAGESIZE
87 };
88
89 #define BAD_ADDR(x)     ((unsigned long)(x) > TASK_SIZE)
90
91 static int set_brk(unsigned long start, unsigned long end)
92 {
93         start = ELF_PAGEALIGN(start);
94         end = ELF_PAGEALIGN(end);
95         if (end > start) {
96                 unsigned long addr;
97                 down_write(&current->mm->mmap_sem);
98                 addr = do_brk(start, end - start);
99                 up_write(&current->mm->mmap_sem);
100                 if (BAD_ADDR(addr))
101                         return addr;
102         }
103         current->mm->start_brk = current->mm->brk = end;
104         return 0;
105 }
106
107
108 /* We need to explicitly zero any fractional pages
109    after the data section (i.e. bss).  This would
110    contain the junk from the file that should not
111    be in memory */
112
113
114 static int padzero(unsigned long elf_bss)
115 {
116         unsigned long nbyte;
117
118         nbyte = ELF_PAGEOFFSET(elf_bss);
119         if (nbyte) {
120                 nbyte = ELF_MIN_ALIGN - nbyte;
121                 if (clear_user((void __user *) elf_bss, nbyte))
122                         return -EFAULT;
123         }
124         return 0;
125 }
126
127 /* Let's use some macros to make this stack manipulation a litle clearer */
128 #ifdef CONFIG_STACK_GROWSUP
129 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
130 #define STACK_ROUND(sp, items) \
131         ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
132 #define STACK_ALLOC(sp, len) ({ elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; old_sp; })
133 #else
134 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
135 #define STACK_ROUND(sp, items) \
136         (((unsigned long) (sp - items)) &~ 15UL)
137 #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
138 #endif
139
140 static int
141 create_elf_tables(struct linux_binprm *bprm, struct elfhdr * exec,
142                 int interp_aout, unsigned long load_addr,
143                 unsigned long interp_load_addr)
144 {
145         unsigned long p = bprm->p;
146         int argc = bprm->argc;
147         int envc = bprm->envc;
148         elf_addr_t __user *argv;
149         elf_addr_t __user *envp;
150         elf_addr_t __user *sp;
151         elf_addr_t __user *u_platform;
152         const char *k_platform = ELF_PLATFORM;
153         int items;
154         elf_addr_t *elf_info;
155         int ei_index = 0;
156         struct task_struct *tsk = current;
157
158         /*
159          * If this architecture has a platform capability string, copy it
160          * to userspace.  In some cases (Sparc), this info is impossible
161          * for userspace to get any other way, in others (i386) it is
162          * merely difficult.
163          */
164
165         u_platform = NULL;
166         if (k_platform) {
167                 size_t len = strlen(k_platform) + 1;
168
169 #ifdef CONFIG_X86_HT
170                 /*
171                  * In some cases (e.g. Hyper-Threading), we want to avoid L1
172                  * evictions by the processes running on the same package. One
173                  * thing we can do is to shuffle the initial stack for them.
174                  *
175                  * The conditionals here are unneeded, but kept in to make the
176                  * code behaviour the same as pre change unless we have
177                  * hyperthreaded processors. This should be cleaned up
178                  * before 2.6
179                  */
180          
181                 if (smp_num_siblings > 1)
182                         STACK_ALLOC(p, ((current->pid % 64) << 7));
183 #endif
184                 u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
185                 if (__copy_to_user(u_platform, k_platform, len))
186                         return -EFAULT;
187         }
188
189         /* Create the ELF interpreter info */
190         elf_info = (elf_addr_t *) current->mm->saved_auxv;
191 #define NEW_AUX_ENT(id, val) \
192         do { elf_info[ei_index++] = id; elf_info[ei_index++] = val; } while (0)
193
194 #ifdef ARCH_DLINFO
195         /* 
196          * ARCH_DLINFO must come first so PPC can do its special alignment of
197          * AUXV.
198          */
199         ARCH_DLINFO;
200 #endif
201         NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
202         NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
203         NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
204         NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
205         NEW_AUX_ENT(AT_PHENT, sizeof (struct elf_phdr));
206         NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
207         NEW_AUX_ENT(AT_BASE, interp_load_addr);
208         NEW_AUX_ENT(AT_FLAGS, 0);
209         NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
210         NEW_AUX_ENT(AT_UID, (elf_addr_t) tsk->uid);
211         NEW_AUX_ENT(AT_EUID, (elf_addr_t) tsk->euid);
212         NEW_AUX_ENT(AT_GID, (elf_addr_t) tsk->gid);
213         NEW_AUX_ENT(AT_EGID, (elf_addr_t) tsk->egid);
214         NEW_AUX_ENT(AT_SECURE, (elf_addr_t) security_bprm_secureexec(bprm));
215         if (k_platform) {
216                 NEW_AUX_ENT(AT_PLATFORM, (elf_addr_t)(unsigned long)u_platform);
217         }
218         if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
219                 NEW_AUX_ENT(AT_EXECFD, (elf_addr_t) bprm->interp_data);
220         }
221 #undef NEW_AUX_ENT
222         /* AT_NULL is zero; clear the rest too */
223         memset(&elf_info[ei_index], 0,
224                sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
225
226         /* And advance past the AT_NULL entry.  */
227         ei_index += 2;
228
229         sp = STACK_ADD(p, ei_index);
230
231         items = (argc + 1) + (envc + 1);
232         if (interp_aout) {
233                 items += 3; /* a.out interpreters require argv & envp too */
234         } else {
235                 items += 1; /* ELF interpreters only put argc on the stack */
236         }
237         bprm->p = STACK_ROUND(sp, items);
238
239         /* Point sp at the lowest address on the stack */
240 #ifdef CONFIG_STACK_GROWSUP
241         sp = (elf_addr_t __user *)bprm->p - items - ei_index;
242         bprm->exec = (unsigned long) sp; /* XXX: PARISC HACK */
243 #else
244         sp = (elf_addr_t __user *)bprm->p;
245 #endif
246
247         /* Now, let's put argc (and argv, envp if appropriate) on the stack */
248         if (__put_user(argc, sp++))
249                 return -EFAULT;
250         if (interp_aout) {
251                 argv = sp + 2;
252                 envp = argv + argc + 1;
253                 __put_user((elf_addr_t)(unsigned long)argv, sp++);
254                 __put_user((elf_addr_t)(unsigned long)envp, sp++);
255         } else {
256                 argv = sp;
257                 envp = argv + argc + 1;
258         }
259
260         /* Populate argv and envp */
261         p = current->mm->arg_start;
262         while (argc-- > 0) {
263                 size_t len;
264                 __put_user((elf_addr_t)p, argv++);
265                 len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
266                 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
267                         return 0;
268                 p += len;
269         }
270         if (__put_user(0, argv))
271                 return -EFAULT;
272         current->mm->arg_end = current->mm->env_start = p;
273         while (envc-- > 0) {
274                 size_t len;
275                 __put_user((elf_addr_t)p, envp++);
276                 len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
277                 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
278                         return 0;
279                 p += len;
280         }
281         if (__put_user(0, envp))
282                 return -EFAULT;
283         current->mm->env_end = p;
284
285         /* Put the elf_info on the stack in the right place.  */
286         sp = (elf_addr_t __user *)envp + 1;
287         if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t)))
288                 return -EFAULT;
289         return 0;
290 }
291
292 #ifndef elf_map
293
294 static unsigned long elf_map(struct file *filep, unsigned long addr,
295                         struct elf_phdr *eppnt, int prot, int type)
296 {
297         unsigned long map_addr;
298
299         down_write(&current->mm->mmap_sem);
300         map_addr = do_mmap(filep, ELF_PAGESTART(addr),
301                            eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr), prot, type,
302                            eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
303         up_write(&current->mm->mmap_sem);
304         return(map_addr);
305 }
306
307 #endif /* !elf_map */
308
309 /* This is much more generalized than the library routine read function,
310    so we keep this separate.  Technically the library read function
311    is only provided so that we can read a.out libraries that have
312    an ELF header */
313
314 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
315                                      struct file * interpreter,
316                                      unsigned long *interp_load_addr)
317 {
318         struct elf_phdr *elf_phdata;
319         struct elf_phdr *eppnt;
320         unsigned long load_addr = 0;
321         int load_addr_set = 0;
322         unsigned long last_bss = 0, elf_bss = 0;
323         unsigned long error = ~0UL;
324         int retval, i, size;
325
326         /* First of all, some simple consistency checks */
327         if (interp_elf_ex->e_type != ET_EXEC &&
328             interp_elf_ex->e_type != ET_DYN)
329                 goto out;
330         if (!elf_check_arch(interp_elf_ex))
331                 goto out;
332         if (!interpreter->f_op || !interpreter->f_op->mmap)
333                 goto out;
334
335         /*
336          * If the size of this structure has changed, then punt, since
337          * we will be doing the wrong thing.
338          */
339         if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
340                 goto out;
341         if (interp_elf_ex->e_phnum < 1 ||
342                 interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
343                 goto out;
344
345         /* Now read in all of the header information */
346
347         size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
348         if (size > ELF_MIN_ALIGN)
349                 goto out;
350         elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
351         if (!elf_phdata)
352                 goto out;
353
354         retval = kernel_read(interpreter,interp_elf_ex->e_phoff,(char *)elf_phdata,size);
355         error = -EIO;
356         if (retval != size) {
357                 if (retval < 0)
358                         error = retval; 
359                 goto out_close;
360         }
361
362         eppnt = elf_phdata;
363         for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
364           if (eppnt->p_type == PT_LOAD) {
365             int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
366             int elf_prot = 0;
367             unsigned long vaddr = 0;
368             unsigned long k, map_addr;
369
370             if (eppnt->p_flags & PF_R) elf_prot =  PROT_READ;
371             if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
372             if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
373             vaddr = eppnt->p_vaddr;
374             if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
375                 elf_type |= MAP_FIXED;
376
377             map_addr = elf_map(interpreter, load_addr + vaddr, eppnt, elf_prot, elf_type);
378             error = map_addr;
379             if (BAD_ADDR(map_addr))
380                 goto out_close;
381
382             if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
383                 load_addr = map_addr - ELF_PAGESTART(vaddr);
384                 load_addr_set = 1;
385             }
386
387             /*
388              * Check to see if the section's size will overflow the
389              * allowed task size. Note that p_filesz must always be
390              * <= p_memsize so it is only necessary to check p_memsz.
391              */
392             k = load_addr + eppnt->p_vaddr;
393             if (k > TASK_SIZE || eppnt->p_filesz > eppnt->p_memsz ||
394                 eppnt->p_memsz > TASK_SIZE || TASK_SIZE - eppnt->p_memsz < k) {
395                 error = -ENOMEM;
396                 goto out_close;
397             }
398
399             /*
400              * Find the end of the file mapping for this phdr, and keep
401              * track of the largest address we see for this.
402              */
403             k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
404             if (k > elf_bss)
405                 elf_bss = k;
406
407             /*
408              * Do the same thing for the memory mapping - between
409              * elf_bss and last_bss is the bss section.
410              */
411             k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
412             if (k > last_bss)
413                 last_bss = k;
414           }
415         }
416
417         /*
418          * Now fill out the bss section.  First pad the last page up
419          * to the page boundary, and then perform a mmap to make sure
420          * that there are zero-mapped pages up to and including the 
421          * last bss page.
422          */
423         if (padzero(elf_bss)) {
424                 error = -EFAULT;
425                 goto out_close;
426         }
427
428         elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);   /* What we have mapped so far */
429
430         /* Map the last of the bss segment */
431         if (last_bss > elf_bss) {
432                 down_write(&current->mm->mmap_sem);
433                 error = do_brk(elf_bss, last_bss - elf_bss);
434                 up_write(&current->mm->mmap_sem);
435                 if (BAD_ADDR(error))
436                         goto out_close;
437         }
438
439         *interp_load_addr = load_addr;
440         error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
441
442 out_close:
443         kfree(elf_phdata);
444 out:
445         return error;
446 }
447
448 static unsigned long load_aout_interp(struct exec * interp_ex,
449                              struct file * interpreter)
450 {
451         unsigned long text_data, elf_entry = ~0UL;
452         char __user * addr;
453         loff_t offset;
454
455         current->mm->end_code = interp_ex->a_text;
456         text_data = interp_ex->a_text + interp_ex->a_data;
457         current->mm->end_data = text_data;
458         current->mm->brk = interp_ex->a_bss + text_data;
459
460         switch (N_MAGIC(*interp_ex)) {
461         case OMAGIC:
462                 offset = 32;
463                 addr = (char __user *)0;
464                 break;
465         case ZMAGIC:
466         case QMAGIC:
467                 offset = N_TXTOFF(*interp_ex);
468                 addr = (char __user *) N_TXTADDR(*interp_ex);
469                 break;
470         default:
471                 goto out;
472         }
473
474         down_write(&current->mm->mmap_sem);     
475         do_brk(0, text_data);
476         up_write(&current->mm->mmap_sem);
477         if (!interpreter->f_op || !interpreter->f_op->read)
478                 goto out;
479         if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
480                 goto out;
481         flush_icache_range((unsigned long)addr,
482                            (unsigned long)addr + text_data);
483
484
485         down_write(&current->mm->mmap_sem);     
486         do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
487                 interp_ex->a_bss);
488         up_write(&current->mm->mmap_sem);
489         elf_entry = interp_ex->a_entry;
490
491 out:
492         return elf_entry;
493 }
494
495 /*
496  * These are the functions used to load ELF style executables and shared
497  * libraries.  There is no binary dependent code anywhere else.
498  */
499
500 #define INTERPRETER_NONE 0
501 #define INTERPRETER_AOUT 1
502 #define INTERPRETER_ELF 2
503
504
505 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
506 {
507         struct file *interpreter = NULL; /* to shut gcc up */
508         unsigned long load_addr = 0, load_bias = 0;
509         int load_addr_set = 0;
510         char * elf_interpreter = NULL;
511         unsigned int interpreter_type = INTERPRETER_NONE;
512         unsigned char ibcs2_interpreter = 0;
513         unsigned long error;
514         struct elf_phdr * elf_ppnt, *elf_phdata;
515         unsigned long elf_bss, elf_brk;
516         int elf_exec_fileno;
517         int retval, i;
518         unsigned int size;
519         unsigned long elf_entry, interp_load_addr = 0;
520         unsigned long start_code, end_code, start_data, end_data;
521         unsigned long reloc_func_desc = 0;
522         char passed_fileno[6];
523         struct files_struct *files;
524         int have_pt_gnu_stack, executable_stack = EXSTACK_DEFAULT;
525         unsigned long def_flags = 0;
526         struct {
527                 struct elfhdr elf_ex;
528                 struct elfhdr interp_elf_ex;
529                 struct exec interp_ex;
530         } *loc;
531
532         loc = kmalloc(sizeof(*loc), GFP_KERNEL);
533         if (!loc) {
534                 retval = -ENOMEM;
535                 goto out_ret;
536         }
537         
538         /* Get the exec-header */
539         loc->elf_ex = *((struct elfhdr *) bprm->buf);
540
541         retval = -ENOEXEC;
542         /* First of all, some simple consistency checks */
543         if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
544                 goto out;
545
546         if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
547                 goto out;
548         if (!elf_check_arch(&loc->elf_ex))
549                 goto out;
550         if (!bprm->file->f_op||!bprm->file->f_op->mmap)
551                 goto out;
552
553         /* Now read in all of the header information */
554
555         if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
556                 goto out;
557         if (loc->elf_ex.e_phnum < 1 ||
558                 loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
559                 goto out;
560         size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
561         retval = -ENOMEM;
562         elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
563         if (!elf_phdata)
564                 goto out;
565
566         retval = kernel_read(bprm->file, loc->elf_ex.e_phoff, (char *) elf_phdata, size);
567         if (retval != size) {
568                 if (retval >= 0)
569                         retval = -EIO;
570                 goto out_free_ph;
571         }
572
573         files = current->files;         /* Refcounted so ok */
574         retval = unshare_files();
575         if (retval < 0)
576                 goto out_free_ph;
577         if (files == current->files) {
578                 put_files_struct(files);
579                 files = NULL;
580         }
581
582         /* exec will make our files private anyway, but for the a.out
583            loader stuff we need to do it earlier */
584
585         retval = get_unused_fd();
586         if (retval < 0)
587                 goto out_free_fh;
588         get_file(bprm->file);
589         fd_install(elf_exec_fileno = retval, bprm->file);
590
591         elf_ppnt = elf_phdata;
592         elf_bss = 0;
593         elf_brk = 0;
594
595         start_code = ~0UL;
596         end_code = 0;
597         start_data = 0;
598         end_data = 0;
599
600         for (i = 0; i < loc->elf_ex.e_phnum; i++) {
601                 if (elf_ppnt->p_type == PT_INTERP) {
602                         /* This is the program interpreter used for
603                          * shared libraries - for now assume that this
604                          * is an a.out format binary
605                          */
606
607                         retval = -ENOEXEC;
608                         if (elf_ppnt->p_filesz > PATH_MAX || 
609                             elf_ppnt->p_filesz < 2)
610                                 goto out_free_file;
611
612                         retval = -ENOMEM;
613                         elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
614                                                            GFP_KERNEL);
615                         if (!elf_interpreter)
616                                 goto out_free_file;
617
618                         retval = kernel_read(bprm->file, elf_ppnt->p_offset,
619                                            elf_interpreter,
620                                            elf_ppnt->p_filesz);
621                         if (retval != elf_ppnt->p_filesz) {
622                                 if (retval >= 0)
623                                         retval = -EIO;
624                                 goto out_free_interp;
625                         }
626                         /* make sure path is NULL terminated */
627                         retval = -ENOEXEC;
628                         if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
629                                 goto out_free_interp;
630
631                         /* If the program interpreter is one of these two,
632                          * then assume an iBCS2 image. Otherwise assume
633                          * a native linux image.
634                          */
635                         if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
636                             strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
637                                 ibcs2_interpreter = 1;
638
639                         /*
640                          * The early SET_PERSONALITY here is so that the lookup
641                          * for the interpreter happens in the namespace of the 
642                          * to-be-execed image.  SET_PERSONALITY can select an
643                          * alternate root.
644                          *
645                          * However, SET_PERSONALITY is NOT allowed to switch
646                          * this task into the new images's memory mapping
647                          * policy - that is, TASK_SIZE must still evaluate to
648                          * that which is appropriate to the execing application.
649                          * This is because exit_mmap() needs to have TASK_SIZE
650                          * evaluate to the size of the old image.
651                          *
652                          * So if (say) a 64-bit application is execing a 32-bit
653                          * application it is the architecture's responsibility
654                          * to defer changing the value of TASK_SIZE until the
655                          * switch really is going to happen - do this in
656                          * flush_thread().      - akpm
657                          */
658                         SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
659
660                         interpreter = open_exec(elf_interpreter);
661                         retval = PTR_ERR(interpreter);
662                         if (IS_ERR(interpreter))
663                                 goto out_free_interp;
664                         retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
665                         if (retval != BINPRM_BUF_SIZE) {
666                                 if (retval >= 0)
667                                         retval = -EIO;
668                                 goto out_free_dentry;
669                         }
670
671                         /* Get the exec headers */
672                         loc->interp_ex = *((struct exec *) bprm->buf);
673                         loc->interp_elf_ex = *((struct elfhdr *) bprm->buf);
674                         break;
675                 }
676                 elf_ppnt++;
677         }
678
679         elf_ppnt = elf_phdata;
680         for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
681                 if (elf_ppnt->p_type == PT_GNU_STACK) {
682                         if (elf_ppnt->p_flags & PF_X)
683                                 executable_stack = EXSTACK_ENABLE_X;
684                         else
685                                 executable_stack = EXSTACK_DISABLE_X;
686                         break;
687                 }
688         have_pt_gnu_stack = (i < loc->elf_ex.e_phnum);
689
690         /* Some simple consistency checks for the interpreter */
691         if (elf_interpreter) {
692                 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
693
694                 /* Now figure out which format our binary is */
695                 if ((N_MAGIC(loc->interp_ex) != OMAGIC) &&
696                     (N_MAGIC(loc->interp_ex) != ZMAGIC) &&
697                     (N_MAGIC(loc->interp_ex) != QMAGIC))
698                         interpreter_type = INTERPRETER_ELF;
699
700                 if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
701                         interpreter_type &= ~INTERPRETER_ELF;
702
703                 retval = -ELIBBAD;
704                 if (!interpreter_type)
705                         goto out_free_dentry;
706
707                 /* Make sure only one type was selected */
708                 if ((interpreter_type & INTERPRETER_ELF) &&
709                      interpreter_type != INTERPRETER_ELF) {
710                         // FIXME - ratelimit this before re-enabling
711                         // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
712                         interpreter_type = INTERPRETER_ELF;
713                 }
714                 /* Verify the interpreter has a valid arch */
715                 if ((interpreter_type == INTERPRETER_ELF) &&
716                     !elf_check_arch(&loc->interp_elf_ex))
717                         goto out_free_dentry;
718         } else {
719                 /* Executables without an interpreter also need a personality  */
720                 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
721         }
722
723         /* OK, we are done with that, now set up the arg stuff,
724            and then start this sucker up */
725
726         if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
727                 char *passed_p = passed_fileno;
728                 sprintf(passed_fileno, "%d", elf_exec_fileno);
729
730                 if (elf_interpreter) {
731                         retval = copy_strings_kernel(1, &passed_p, bprm);
732                         if (retval)
733                                 goto out_free_dentry; 
734                         bprm->argc++;
735                 }
736         }
737
738         /* Flush all traces of the currently running executable */
739         retval = flush_old_exec(bprm);
740         if (retval)
741                 goto out_free_dentry;
742
743         /* Discard our unneeded old files struct */
744         if (files) {
745                 steal_locks(files);
746                 put_files_struct(files);
747                 files = NULL;
748         }
749
750         /* OK, This is the point of no return */
751         current->mm->start_data = 0;
752         current->mm->end_data = 0;
753         current->mm->end_code = 0;
754         current->mm->mmap = NULL;
755         current->flags &= ~PF_FORKNOEXEC;
756         current->mm->def_flags = def_flags;
757
758         /* Do this immediately, since STACK_TOP as used in setup_arg_pages
759            may depend on the personality.  */
760         SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
761         if (elf_read_implies_exec(loc->elf_ex, executable_stack))
762                 current->personality |= READ_IMPLIES_EXEC;
763
764         arch_pick_mmap_layout(current->mm);
765
766         /* Do this so that we can load the interpreter, if need be.  We will
767            change some of these later */
768         // current->mm->rss = 0;
769         vx_rsspages_sub(current->mm, current->mm->rss);
770         current->mm->free_area_cache = current->mm->mmap_base;
771         retval = setup_arg_pages(bprm, STACK_TOP, executable_stack);
772         if (retval < 0) {
773                 send_sig(SIGKILL, current, 0);
774                 goto out_free_dentry;
775         }
776         
777         current->mm->start_stack = bprm->p;
778
779         /* Now we do a little grungy work by mmaping the ELF image into
780            the correct location in memory.  At this point, we assume that
781            the image should be loaded at fixed address, not at a variable
782            address. */
783
784         for(i = 0, elf_ppnt = elf_phdata; i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
785                 int elf_prot = 0, elf_flags;
786                 unsigned long k, vaddr;
787
788                 if (elf_ppnt->p_type != PT_LOAD)
789                         continue;
790
791                 if (unlikely (elf_brk > elf_bss)) {
792                         unsigned long nbyte;
793                     
794                         /* There was a PT_LOAD segment with p_memsz > p_filesz
795                            before this one. Map anonymous pages, if needed,
796                            and clear the area.  */
797                         retval = set_brk (elf_bss + load_bias,
798                                           elf_brk + load_bias);
799                         if (retval) {
800                                 send_sig(SIGKILL, current, 0);
801                                 goto out_free_dentry;
802                         }
803                         nbyte = ELF_PAGEOFFSET(elf_bss);
804                         if (nbyte) {
805                                 nbyte = ELF_MIN_ALIGN - nbyte;
806                                 if (nbyte > elf_brk - elf_bss)
807                                         nbyte = elf_brk - elf_bss;
808                                 if (clear_user((void __user *)elf_bss +
809                                                         load_bias, nbyte)) {
810                                         /*
811                                          * This bss-zeroing can fail if the ELF
812                                          * file specifies odd protections.  So
813                                          * we don't check the return value
814                                          */
815                                 }
816                         }
817                 }
818
819                 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
820                 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
821                 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
822
823                 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
824
825                 vaddr = elf_ppnt->p_vaddr;
826                 if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
827                         elf_flags |= MAP_FIXED;
828                 } else if (loc->elf_ex.e_type == ET_DYN) {
829                         /* Try and get dynamic programs out of the way of the default mmap
830                            base, as well as whatever program they might try to exec.  This
831                            is because the brk will follow the loader, and is not movable.  */
832                         load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
833                 }
834
835                 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags);
836                 if (BAD_ADDR(error)) {
837                         send_sig(SIGKILL, current, 0);
838                         goto out_free_dentry;
839                 }
840
841                 if (!load_addr_set) {
842                         load_addr_set = 1;
843                         load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
844                         if (loc->elf_ex.e_type == ET_DYN) {
845                                 load_bias += error -
846                                              ELF_PAGESTART(load_bias + vaddr);
847                                 load_addr += load_bias;
848                                 reloc_func_desc = load_bias;
849                         }
850                 }
851                 k = elf_ppnt->p_vaddr;
852                 if (k < start_code) start_code = k;
853                 if (start_data < k) start_data = k;
854
855                 /*
856                  * Check to see if the section's size will overflow the
857                  * allowed task size. Note that p_filesz must always be
858                  * <= p_memsz so it is only necessary to check p_memsz.
859                  */
860                 if (k > TASK_SIZE || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
861                     elf_ppnt->p_memsz > TASK_SIZE ||
862                     TASK_SIZE - elf_ppnt->p_memsz < k) {
863                         /* set_brk can never work.  Avoid overflows.  */
864                         send_sig(SIGKILL, current, 0);
865                         goto out_free_dentry;
866                 }
867
868                 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
869
870                 if (k > elf_bss)
871                         elf_bss = k;
872                 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
873                         end_code = k;
874                 if (end_data < k)
875                         end_data = k;
876                 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
877                 if (k > elf_brk)
878                         elf_brk = k;
879         }
880
881         loc->elf_ex.e_entry += load_bias;
882         elf_bss += load_bias;
883         elf_brk += load_bias;
884         start_code += load_bias;
885         end_code += load_bias;
886         start_data += load_bias;
887         end_data += load_bias;
888
889         /* Calling set_brk effectively mmaps the pages that we need
890          * for the bss and break sections.  We must do this before
891          * mapping in the interpreter, to make sure it doesn't wind
892          * up getting placed where the bss needs to go.
893          */
894         retval = set_brk(elf_bss, elf_brk);
895         if (retval) {
896                 send_sig(SIGKILL, current, 0);
897                 goto out_free_dentry;
898         }
899         if (padzero(elf_bss)) {
900                 send_sig(SIGSEGV, current, 0);
901                 retval = -EFAULT; /* Nobody gets to see this, but.. */
902                 goto out_free_dentry;
903         }
904
905         if (elf_interpreter) {
906                 if (interpreter_type == INTERPRETER_AOUT)
907                         elf_entry = load_aout_interp(&loc->interp_ex,
908                                                      interpreter);
909                 else
910                         elf_entry = load_elf_interp(&loc->interp_elf_ex,
911                                                     interpreter,
912                                                     &interp_load_addr);
913                 if (BAD_ADDR(elf_entry)) {
914                         printk(KERN_ERR "Unable to load interpreter %.128s\n",
915                                 elf_interpreter);
916                         force_sig(SIGSEGV, current);
917                         retval = -ENOEXEC; /* Nobody gets to see this, but.. */
918                         goto out_free_dentry;
919                 }
920                 reloc_func_desc = interp_load_addr;
921
922                 allow_write_access(interpreter);
923                 fput(interpreter);
924                 kfree(elf_interpreter);
925         } else {
926                 elf_entry = loc->elf_ex.e_entry;
927         }
928
929         kfree(elf_phdata);
930
931         if (interpreter_type != INTERPRETER_AOUT)
932                 sys_close(elf_exec_fileno);
933
934         set_binfmt(&elf_format);
935
936         compute_creds(bprm);
937         current->flags &= ~PF_FORKNOEXEC;
938         create_elf_tables(bprm, &loc->elf_ex, (interpreter_type == INTERPRETER_AOUT),
939                         load_addr, interp_load_addr);
940         /* N.B. passed_fileno might not be initialized? */
941         if (interpreter_type == INTERPRETER_AOUT)
942                 current->mm->arg_start += strlen(passed_fileno) + 1;
943         current->mm->end_code = end_code;
944         current->mm->start_code = start_code;
945         current->mm->start_data = start_data;
946         current->mm->end_data = end_data;
947         current->mm->start_stack = bprm->p;
948
949         if (current->personality & MMAP_PAGE_ZERO) {
950                 /* Why this, you ask???  Well SVr4 maps page 0 as read-only,
951                    and some applications "depend" upon this behavior.
952                    Since we do not have the power to recompile these, we
953                    emulate the SVr4 behavior.  Sigh.  */
954                 down_write(&current->mm->mmap_sem);
955                 error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
956                                 MAP_FIXED | MAP_PRIVATE, 0);
957                 up_write(&current->mm->mmap_sem);
958         }
959
960 #ifdef ELF_PLAT_INIT
961         /*
962          * The ABI may specify that certain registers be set up in special
963          * ways (on i386 %edx is the address of a DT_FINI function, for
964          * example.  In addition, it may also specify (eg, PowerPC64 ELF)
965          * that the e_entry field is the address of the function descriptor
966          * for the startup routine, rather than the address of the startup
967          * routine itself.  This macro performs whatever initialization to
968          * the regs structure is required as well as any relocations to the
969          * function descriptor entries when executing dynamically links apps.
970          */
971         ELF_PLAT_INIT(regs, reloc_func_desc);
972 #endif
973
974         start_thread(regs, elf_entry, bprm->p);
975         if (unlikely(current->ptrace & PT_PTRACED)) {
976                 if (current->ptrace & PT_TRACE_EXEC)
977                         ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
978                 else
979                         send_sig(SIGTRAP, current, 0);
980         }
981         retval = 0;
982 out:
983         kfree(loc);
984 out_ret:
985         return retval;
986
987         /* error cleanup */
988 out_free_dentry:
989         allow_write_access(interpreter);
990         if (interpreter)
991                 fput(interpreter);
992 out_free_interp:
993         if (elf_interpreter)
994                 kfree(elf_interpreter);
995 out_free_file:
996         sys_close(elf_exec_fileno);
997 out_free_fh:
998         if (files) {
999                 put_files_struct(current->files);
1000                 current->files = files;
1001         }
1002 out_free_ph:
1003         kfree(elf_phdata);
1004         goto out;
1005 }
1006
1007 /* This is really simpleminded and specialized - we are loading an
1008    a.out library that is given an ELF header. */
1009
1010 static int load_elf_library(struct file *file)
1011 {
1012         struct elf_phdr *elf_phdata;
1013         struct elf_phdr *eppnt;
1014         unsigned long elf_bss, bss, len;
1015         int retval, error, i, j;
1016         struct elfhdr elf_ex;
1017
1018         error = -ENOEXEC;
1019         retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
1020         if (retval != sizeof(elf_ex))
1021                 goto out;
1022
1023         if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
1024                 goto out;
1025
1026         /* First of all, some simple consistency checks */
1027         if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
1028            !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
1029                 goto out;
1030
1031         /* Now read in all of the header information */
1032
1033         j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
1034         /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1035
1036         error = -ENOMEM;
1037         elf_phdata = kmalloc(j, GFP_KERNEL);
1038         if (!elf_phdata)
1039                 goto out;
1040
1041         eppnt = elf_phdata;
1042         error = -ENOEXEC;
1043         retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
1044         if (retval != j)
1045                 goto out_free_ph;
1046
1047         for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
1048                 if ((eppnt + i)->p_type == PT_LOAD)
1049                         j++;
1050         if (j != 1)
1051                 goto out_free_ph;
1052
1053         while (eppnt->p_type != PT_LOAD)
1054                 eppnt++;
1055
1056         /* Now use mmap to map the library into memory. */
1057         down_write(&current->mm->mmap_sem);
1058         error = do_mmap(file,
1059                         ELF_PAGESTART(eppnt->p_vaddr),
1060                         (eppnt->p_filesz +
1061                          ELF_PAGEOFFSET(eppnt->p_vaddr)),
1062                         PROT_READ | PROT_WRITE | PROT_EXEC,
1063                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
1064                         (eppnt->p_offset -
1065                          ELF_PAGEOFFSET(eppnt->p_vaddr)));
1066         up_write(&current->mm->mmap_sem);
1067         if (error != ELF_PAGESTART(eppnt->p_vaddr))
1068                 goto out_free_ph;
1069
1070         elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
1071         if (padzero(elf_bss)) {
1072                 error = -EFAULT;
1073                 goto out_free_ph;
1074         }
1075
1076         len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr + ELF_MIN_ALIGN - 1);
1077         bss = eppnt->p_memsz + eppnt->p_vaddr;
1078         if (bss > len) {
1079                 down_write(&current->mm->mmap_sem);
1080                 do_brk(len, bss - len);
1081                 up_write(&current->mm->mmap_sem);
1082         }
1083         error = 0;
1084
1085 out_free_ph:
1086         kfree(elf_phdata);
1087 out:
1088         return error;
1089 }
1090
1091 /*
1092  * Note that some platforms still use traditional core dumps and not
1093  * the ELF core dump.  Each platform can select it as appropriate.
1094  */
1095 #ifdef USE_ELF_CORE_DUMP
1096
1097 /*
1098  * ELF core dumper
1099  *
1100  * Modelled on fs/exec.c:aout_core_dump()
1101  * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1102  */
1103 /*
1104  * These are the only things you should do on a core-file: use only these
1105  * functions to write out all the necessary info.
1106  */
1107 static int dump_write(struct file *file, const void *addr, int nr)
1108 {
1109         return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1110 }
1111
1112 static int dump_seek(struct file *file, off_t off)
1113 {
1114         if (file->f_op->llseek) {
1115                 if (file->f_op->llseek(file, off, 0) != off)
1116                         return 0;
1117         } else
1118                 file->f_pos = off;
1119         return 1;
1120 }
1121
1122 /*
1123  * Decide whether a segment is worth dumping; default is yes to be
1124  * sure (missing info is worse than too much; etc).
1125  * Personally I'd include everything, and use the coredump limit...
1126  *
1127  * I think we should skip something. But I am not sure how. H.J.
1128  */
1129 static int maydump(struct vm_area_struct *vma)
1130 {
1131         /* Do not dump I/O mapped devices, shared memory, or special mappings */
1132         if (vma->vm_flags & (VM_IO | VM_SHARED | VM_RESERVED))
1133                 return 0;
1134
1135         /* If it hasn't been written to, don't write it out */
1136         if (!vma->anon_vma)
1137                 return 0;
1138
1139         return 1;
1140 }
1141
1142 #define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))
1143
1144 /* An ELF note in memory */
1145 struct memelfnote
1146 {
1147         const char *name;
1148         int type;
1149         unsigned int datasz;
1150         void *data;
1151 };
1152
1153 static int notesize(struct memelfnote *en)
1154 {
1155         int sz;
1156
1157         sz = sizeof(struct elf_note);
1158         sz += roundup(strlen(en->name) + 1, 4);
1159         sz += roundup(en->datasz, 4);
1160
1161         return sz;
1162 }
1163
1164 #define DUMP_WRITE(addr, nr)    \
1165         do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1166 #define DUMP_SEEK(off)  \
1167         do { if (!dump_seek(file, (off))) return 0; } while(0)
1168
1169 static int writenote(struct memelfnote *men, struct file *file)
1170 {
1171         struct elf_note en;
1172
1173         en.n_namesz = strlen(men->name) + 1;
1174         en.n_descsz = men->datasz;
1175         en.n_type = men->type;
1176
1177         DUMP_WRITE(&en, sizeof(en));
1178         DUMP_WRITE(men->name, en.n_namesz);
1179         /* XXX - cast from long long to long to avoid need for libgcc.a */
1180         DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));      /* XXX */
1181         DUMP_WRITE(men->data, men->datasz);
1182         DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));      /* XXX */
1183
1184         return 1;
1185 }
1186 #undef DUMP_WRITE
1187 #undef DUMP_SEEK
1188
1189 #define DUMP_WRITE(addr, nr)    \
1190         if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1191                 goto end_coredump;
1192 #define DUMP_SEEK(off)  \
1193         if (!dump_seek(file, (off))) \
1194                 goto end_coredump;
1195
1196 static inline void fill_elf_header(struct elfhdr *elf, int segs)
1197 {
1198         memcpy(elf->e_ident, ELFMAG, SELFMAG);
1199         elf->e_ident[EI_CLASS] = ELF_CLASS;
1200         elf->e_ident[EI_DATA] = ELF_DATA;
1201         elf->e_ident[EI_VERSION] = EV_CURRENT;
1202         elf->e_ident[EI_OSABI] = ELF_OSABI;
1203         memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1204
1205         elf->e_type = ET_CORE;
1206         elf->e_machine = ELF_ARCH;
1207         elf->e_version = EV_CURRENT;
1208         elf->e_entry = 0;
1209         elf->e_phoff = sizeof(struct elfhdr);
1210         elf->e_shoff = 0;
1211         elf->e_flags = ELF_CORE_EFLAGS;
1212         elf->e_ehsize = sizeof(struct elfhdr);
1213         elf->e_phentsize = sizeof(struct elf_phdr);
1214         elf->e_phnum = segs;
1215         elf->e_shentsize = 0;
1216         elf->e_shnum = 0;
1217         elf->e_shstrndx = 0;
1218         return;
1219 }
1220
1221 static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
1222 {
1223         phdr->p_type = PT_NOTE;
1224         phdr->p_offset = offset;
1225         phdr->p_vaddr = 0;
1226         phdr->p_paddr = 0;
1227         phdr->p_filesz = sz;
1228         phdr->p_memsz = 0;
1229         phdr->p_flags = 0;
1230         phdr->p_align = 0;
1231         return;
1232 }
1233
1234 static void fill_note(struct memelfnote *note, const char *name, int type, 
1235                 unsigned int sz, void *data)
1236 {
1237         note->name = name;
1238         note->type = type;
1239         note->datasz = sz;
1240         note->data = data;
1241         return;
1242 }
1243
1244 /*
1245  * fill up all the fields in prstatus from the given task struct, except registers
1246  * which need to be filled up separately.
1247  */
1248 static void fill_prstatus(struct elf_prstatus *prstatus,
1249                         struct task_struct *p, long signr) 
1250 {
1251         prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1252         prstatus->pr_sigpend = p->pending.signal.sig[0];
1253         prstatus->pr_sighold = p->blocked.sig[0];
1254         prstatus->pr_pid = p->pid;
1255         prstatus->pr_ppid = p->parent->pid;
1256         prstatus->pr_pgrp = process_group(p);
1257         prstatus->pr_sid = p->signal->session;
1258         if (thread_group_leader(p)) {
1259                 /*
1260                  * This is the record for the group leader.  Add in the
1261                  * cumulative times of previous dead threads.  This total
1262                  * won't include the time of each live thread whose state
1263                  * is included in the core dump.  The final total reported
1264                  * to our parent process when it calls wait4 will include
1265                  * those sums as well as the little bit more time it takes
1266                  * this and each other thread to finish dying after the
1267                  * core dump synchronization phase.
1268                  */
1269                 cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
1270                                    &prstatus->pr_utime);
1271                 cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
1272                                    &prstatus->pr_stime);
1273         } else {
1274                 cputime_to_timeval(p->utime, &prstatus->pr_utime);
1275                 cputime_to_timeval(p->stime, &prstatus->pr_stime);
1276         }
1277         cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
1278         cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
1279 }
1280
1281 static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1282                        struct mm_struct *mm)
1283 {
1284         int i, len;
1285         
1286         /* first copy the parameters from user space */
1287         memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1288
1289         len = mm->arg_end - mm->arg_start;
1290         if (len >= ELF_PRARGSZ)
1291                 len = ELF_PRARGSZ-1;
1292         if (copy_from_user(&psinfo->pr_psargs,
1293                            (const char __user *)mm->arg_start, len))
1294                 return -EFAULT;
1295         for(i = 0; i < len; i++)
1296                 if (psinfo->pr_psargs[i] == 0)
1297                         psinfo->pr_psargs[i] = ' ';
1298         psinfo->pr_psargs[len] = 0;
1299
1300         psinfo->pr_pid = p->pid;
1301         psinfo->pr_ppid = p->parent->pid;
1302         psinfo->pr_pgrp = process_group(p);
1303         psinfo->pr_sid = p->signal->session;
1304
1305         i = p->state ? ffz(~p->state) + 1 : 0;
1306         psinfo->pr_state = i;
1307         psinfo->pr_sname = (i < 0 || i > 5) ? '.' : "RSDTZW"[i];
1308         psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1309         psinfo->pr_nice = task_nice(p);
1310         psinfo->pr_flag = p->flags;
1311         SET_UID(psinfo->pr_uid, p->uid);
1312         SET_GID(psinfo->pr_gid, p->gid);
1313         strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1314         
1315         return 0;
1316 }
1317
1318 /* Here is the structure in which status of each thread is captured. */
1319 struct elf_thread_status
1320 {
1321         struct list_head list;
1322         struct elf_prstatus prstatus;   /* NT_PRSTATUS */
1323         elf_fpregset_t fpu;             /* NT_PRFPREG */
1324         struct task_struct *thread;
1325 #ifdef ELF_CORE_COPY_XFPREGS
1326         elf_fpxregset_t xfpu;           /* NT_PRXFPREG */
1327 #endif
1328         struct memelfnote notes[3];
1329         int num_notes;
1330 };
1331
1332 /*
1333  * In order to add the specific thread information for the elf file format,
1334  * we need to keep a linked list of every threads pr_status and then
1335  * create a single section for them in the final core file.
1336  */
1337 static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1338 {
1339         int sz = 0;
1340         struct task_struct *p = t->thread;
1341         t->num_notes = 0;
1342
1343         fill_prstatus(&t->prstatus, p, signr);
1344         elf_core_copy_task_regs(p, &t->prstatus.pr_reg);        
1345         
1346         fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus), &(t->prstatus));
1347         t->num_notes++;
1348         sz += notesize(&t->notes[0]);
1349
1350         if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, &t->fpu))) {
1351                 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu), &(t->fpu));
1352                 t->num_notes++;
1353                 sz += notesize(&t->notes[1]);
1354         }
1355
1356 #ifdef ELF_CORE_COPY_XFPREGS
1357         if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
1358                 fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu), &t->xfpu);
1359                 t->num_notes++;
1360                 sz += notesize(&t->notes[2]);
1361         }
1362 #endif  
1363         return sz;
1364 }
1365
1366 /*
1367  * Actual dumper
1368  *
1369  * This is a two-pass process; first we find the offsets of the bits,
1370  * and then they are actually written out.  If we run out of core limit
1371  * we just truncate.
1372  */
1373 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
1374 {
1375 #define NUM_NOTES       6
1376         int has_dumped = 0;
1377         mm_segment_t fs;
1378         int segs;
1379         size_t size = 0;
1380         int i;
1381         struct vm_area_struct *vma;
1382         struct elfhdr *elf = NULL;
1383         off_t offset = 0, dataoff;
1384         unsigned long limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1385         int numnote;
1386         struct memelfnote *notes = NULL;
1387         struct elf_prstatus *prstatus = NULL;   /* NT_PRSTATUS */
1388         struct elf_prpsinfo *psinfo = NULL;     /* NT_PRPSINFO */
1389         struct task_struct *g, *p;
1390         LIST_HEAD(thread_list);
1391         struct list_head *t;
1392         elf_fpregset_t *fpu = NULL;
1393 #ifdef ELF_CORE_COPY_XFPREGS
1394         elf_fpxregset_t *xfpu = NULL;
1395 #endif
1396         int thread_status_size = 0;
1397         elf_addr_t *auxv;
1398
1399         /*
1400          * We no longer stop all VM operations.
1401          * 
1402          * This is because those proceses that could possibly change map_count or
1403          * the mmap / vma pages are now blocked in do_exit on current finishing
1404          * this core dump.
1405          *
1406          * Only ptrace can touch these memory addresses, but it doesn't change
1407          * the map_count or the pages allocated.  So no possibility of crashing
1408          * exists while dumping the mm->vm_next areas to the core file.
1409          */
1410   
1411         /* alloc memory for large data structures: too large to be on stack */
1412         elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1413         if (!elf)
1414                 goto cleanup;
1415         prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
1416         if (!prstatus)
1417                 goto cleanup;
1418         psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1419         if (!psinfo)
1420                 goto cleanup;
1421         notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1422         if (!notes)
1423                 goto cleanup;
1424         fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1425         if (!fpu)
1426                 goto cleanup;
1427 #ifdef ELF_CORE_COPY_XFPREGS
1428         xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1429         if (!xfpu)
1430                 goto cleanup;
1431 #endif
1432
1433         if (signr) {
1434                 struct elf_thread_status *tmp;
1435                 read_lock(&tasklist_lock);
1436                 do_each_thread(g,p)
1437                         if (current->mm == p->mm && current != p) {
1438                                 tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC);
1439                                 if (!tmp) {
1440                                         read_unlock(&tasklist_lock);
1441                                         goto cleanup;
1442                                 }
1443                                 memset(tmp, 0, sizeof(*tmp));
1444                                 INIT_LIST_HEAD(&tmp->list);
1445                                 tmp->thread = p;
1446                                 list_add(&tmp->list, &thread_list);
1447                         }
1448                 while_each_thread(g,p);
1449                 read_unlock(&tasklist_lock);
1450                 list_for_each(t, &thread_list) {
1451                         struct elf_thread_status *tmp;
1452                         int sz;
1453
1454                         tmp = list_entry(t, struct elf_thread_status, list);
1455                         sz = elf_dump_thread_status(signr, tmp);
1456                         thread_status_size += sz;
1457                 }
1458         }
1459         /* now collect the dump for the current */
1460         memset(prstatus, 0, sizeof(*prstatus));
1461         fill_prstatus(prstatus, current, signr);
1462         elf_core_copy_regs(&prstatus->pr_reg, regs);
1463         
1464         segs = current->mm->map_count;
1465 #ifdef ELF_CORE_EXTRA_PHDRS
1466         segs += ELF_CORE_EXTRA_PHDRS;
1467 #endif
1468
1469         /* Set up header */
1470         fill_elf_header(elf, segs+1);   /* including notes section */
1471
1472         has_dumped = 1;
1473         current->flags |= PF_DUMPCORE;
1474
1475         /*
1476          * Set up the notes in similar form to SVR4 core dumps made
1477          * with info from their /proc.
1478          */
1479
1480         fill_note(notes +0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
1481         
1482         fill_psinfo(psinfo, current->group_leader, current->mm);
1483         fill_note(notes +1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1484         
1485         fill_note(notes +2, "CORE", NT_TASKSTRUCT, sizeof(*current), current);
1486   
1487         numnote = 3;
1488
1489         auxv = (elf_addr_t *) current->mm->saved_auxv;
1490
1491         i = 0;
1492         do
1493                 i += 2;
1494         while (auxv[i - 2] != AT_NULL);
1495         fill_note(&notes[numnote++], "CORE", NT_AUXV,
1496                   i * sizeof (elf_addr_t), auxv);
1497
1498         /* Try to dump the FPU. */
1499         if ((prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, regs, fpu)))
1500                 fill_note(notes + numnote++,
1501                           "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1502 #ifdef ELF_CORE_COPY_XFPREGS
1503         if (elf_core_copy_task_xfpregs(current, xfpu))
1504                 fill_note(notes + numnote++,
1505                           "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
1506 #endif  
1507   
1508         fs = get_fs();
1509         set_fs(KERNEL_DS);
1510
1511         DUMP_WRITE(elf, sizeof(*elf));
1512         offset += sizeof(*elf);                         /* Elf header */
1513         offset += (segs+1) * sizeof(struct elf_phdr);   /* Program headers */
1514
1515         /* Write notes phdr entry */
1516         {
1517                 struct elf_phdr phdr;
1518                 int sz = 0;
1519
1520                 for (i = 0; i < numnote; i++)
1521                         sz += notesize(notes + i);
1522                 
1523                 sz += thread_status_size;
1524
1525                 fill_elf_note_phdr(&phdr, sz, offset);
1526                 offset += sz;
1527                 DUMP_WRITE(&phdr, sizeof(phdr));
1528         }
1529
1530         /* Page-align dumped data */
1531         dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1532
1533         /* Write program headers for segments dump */
1534         for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1535                 struct elf_phdr phdr;
1536                 size_t sz;
1537
1538                 sz = vma->vm_end - vma->vm_start;
1539
1540                 phdr.p_type = PT_LOAD;
1541                 phdr.p_offset = offset;
1542                 phdr.p_vaddr = vma->vm_start;
1543                 phdr.p_paddr = 0;
1544                 phdr.p_filesz = maydump(vma) ? sz : 0;
1545                 phdr.p_memsz = sz;
1546                 offset += phdr.p_filesz;
1547                 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1548                 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1549                 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1550                 phdr.p_align = ELF_EXEC_PAGESIZE;
1551
1552                 DUMP_WRITE(&phdr, sizeof(phdr));
1553         }
1554
1555 #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1556         ELF_CORE_WRITE_EXTRA_PHDRS;
1557 #endif
1558
1559         /* write out the notes section */
1560         for (i = 0; i < numnote; i++)
1561                 if (!writenote(notes + i, file))
1562                         goto end_coredump;
1563
1564         /* write out the thread status notes section */
1565         list_for_each(t, &thread_list) {
1566                 struct elf_thread_status *tmp = list_entry(t, struct elf_thread_status, list);
1567                 for (i = 0; i < tmp->num_notes; i++)
1568                         if (!writenote(&tmp->notes[i], file))
1569                                 goto end_coredump;
1570         }
1571  
1572         DUMP_SEEK(dataoff);
1573
1574         for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1575                 unsigned long addr;
1576
1577                 if (!maydump(vma))
1578                         continue;
1579
1580                 for (addr = vma->vm_start;
1581                      addr < vma->vm_end;
1582                      addr += PAGE_SIZE) {
1583                         struct page* page;
1584                         struct vm_area_struct *vma;
1585
1586                         if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1587                                                 &page, &vma) <= 0) {
1588                                 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1589                         } else {
1590                                 if (page == ZERO_PAGE(addr)) {
1591                                         DUMP_SEEK (file->f_pos + PAGE_SIZE);
1592                                 } else {
1593                                         void *kaddr;
1594                                         flush_cache_page(vma, addr);
1595                                         kaddr = kmap(page);
1596                                         if ((size += PAGE_SIZE) > limit ||
1597                                             !dump_write(file, kaddr,
1598                                             PAGE_SIZE)) {
1599                                                 kunmap(page);
1600                                                 page_cache_release(page);
1601                                                 goto end_coredump;
1602                                         }
1603                                         kunmap(page);
1604                                 }
1605                                 page_cache_release(page);
1606                         }
1607                 }
1608         }
1609
1610 #ifdef ELF_CORE_WRITE_EXTRA_DATA
1611         ELF_CORE_WRITE_EXTRA_DATA;
1612 #endif
1613
1614         if ((off_t) file->f_pos != offset) {
1615                 /* Sanity check */
1616                 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1617                        (off_t) file->f_pos, offset);
1618         }
1619
1620 end_coredump:
1621         set_fs(fs);
1622
1623 cleanup:
1624         while(!list_empty(&thread_list)) {
1625                 struct list_head *tmp = thread_list.next;
1626                 list_del(tmp);
1627                 kfree(list_entry(tmp, struct elf_thread_status, list));
1628         }
1629
1630         kfree(elf);
1631         kfree(prstatus);
1632         kfree(psinfo);
1633         kfree(notes);
1634         kfree(fpu);
1635 #ifdef ELF_CORE_COPY_XFPREGS
1636         kfree(xfpu);
1637 #endif
1638         return has_dumped;
1639 #undef NUM_NOTES
1640 }
1641
1642 #endif          /* USE_ELF_CORE_DUMP */
1643
1644 static int __init init_elf_binfmt(void)
1645 {
1646         return register_binfmt(&elf_format);
1647 }
1648
1649 static void __exit exit_elf_binfmt(void)
1650 {
1651         /* Remove the COFF and ELF loaders. */
1652         unregister_binfmt(&elf_format);
1653 }
1654
1655 core_initcall(init_elf_binfmt);
1656 module_exit(exit_elf_binfmt);
1657 MODULE_LICENSE("GPL");