vserver 2.0-rc4
[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_end = 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         vx_rsspages_sub(current->mm, current->mm->rss);
769         current->mm->free_area_cache = current->mm->mmap_base;
770         retval = setup_arg_pages(bprm, STACK_TOP, executable_stack);
771         if (retval < 0) {
772                 send_sig(SIGKILL, current, 0);
773                 goto out_free_dentry;
774         }
775         
776         current->mm->start_stack = bprm->p;
777
778         /* Now we do a little grungy work by mmaping the ELF image into
779            the correct location in memory.  At this point, we assume that
780            the image should be loaded at fixed address, not at a variable
781            address. */
782
783         for(i = 0, elf_ppnt = elf_phdata; i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
784                 int elf_prot = 0, elf_flags;
785                 unsigned long k, vaddr;
786
787                 if (elf_ppnt->p_type != PT_LOAD)
788                         continue;
789
790                 if (unlikely (elf_brk > elf_bss)) {
791                         unsigned long nbyte;
792                     
793                         /* There was a PT_LOAD segment with p_memsz > p_filesz
794                            before this one. Map anonymous pages, if needed,
795                            and clear the area.  */
796                         retval = set_brk (elf_bss + load_bias,
797                                           elf_brk + load_bias);
798                         if (retval) {
799                                 send_sig(SIGKILL, current, 0);
800                                 goto out_free_dentry;
801                         }
802                         nbyte = ELF_PAGEOFFSET(elf_bss);
803                         if (nbyte) {
804                                 nbyte = ELF_MIN_ALIGN - nbyte;
805                                 if (nbyte > elf_brk - elf_bss)
806                                         nbyte = elf_brk - elf_bss;
807                                 if (clear_user((void __user *)elf_bss +
808                                                         load_bias, nbyte)) {
809                                         /*
810                                          * This bss-zeroing can fail if the ELF
811                                          * file specifies odd protections.  So
812                                          * we don't check the return value
813                                          */
814                                 }
815                         }
816                 }
817
818                 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
819                 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
820                 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
821
822                 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
823
824                 vaddr = elf_ppnt->p_vaddr;
825                 if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
826                         elf_flags |= MAP_FIXED;
827                 } else if (loc->elf_ex.e_type == ET_DYN) {
828                         /* Try and get dynamic programs out of the way of the default mmap
829                            base, as well as whatever program they might try to exec.  This
830                            is because the brk will follow the loader, and is not movable.  */
831                         load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
832                 }
833
834                 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags);
835                 if (BAD_ADDR(error)) {
836                         send_sig(SIGKILL, current, 0);
837                         goto out_free_dentry;
838                 }
839
840                 if (!load_addr_set) {
841                         load_addr_set = 1;
842                         load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
843                         if (loc->elf_ex.e_type == ET_DYN) {
844                                 load_bias += error -
845                                              ELF_PAGESTART(load_bias + vaddr);
846                                 load_addr += load_bias;
847                                 reloc_func_desc = load_bias;
848                         }
849                 }
850                 k = elf_ppnt->p_vaddr;
851                 if (k < start_code) start_code = k;
852                 if (start_data < k) start_data = k;
853
854                 /*
855                  * Check to see if the section's size will overflow the
856                  * allowed task size. Note that p_filesz must always be
857                  * <= p_memsz so it is only necessary to check p_memsz.
858                  */
859                 if (k > TASK_SIZE || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
860                     elf_ppnt->p_memsz > TASK_SIZE ||
861                     TASK_SIZE - elf_ppnt->p_memsz < k) {
862                         /* set_brk can never work.  Avoid overflows.  */
863                         send_sig(SIGKILL, current, 0);
864                         goto out_free_dentry;
865                 }
866
867                 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
868
869                 if (k > elf_bss)
870                         elf_bss = k;
871                 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
872                         end_code = k;
873                 if (end_data < k)
874                         end_data = k;
875                 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
876                 if (k > elf_brk)
877                         elf_brk = k;
878         }
879
880         loc->elf_ex.e_entry += load_bias;
881         elf_bss += load_bias;
882         elf_brk += load_bias;
883         start_code += load_bias;
884         end_code += load_bias;
885         start_data += load_bias;
886         end_data += load_bias;
887
888         /* Calling set_brk effectively mmaps the pages that we need
889          * for the bss and break sections.  We must do this before
890          * mapping in the interpreter, to make sure it doesn't wind
891          * up getting placed where the bss needs to go.
892          */
893         retval = set_brk(elf_bss, elf_brk);
894         if (retval) {
895                 send_sig(SIGKILL, current, 0);
896                 goto out_free_dentry;
897         }
898         if (padzero(elf_bss)) {
899                 send_sig(SIGSEGV, current, 0);
900                 retval = -EFAULT; /* Nobody gets to see this, but.. */
901                 goto out_free_dentry;
902         }
903
904         if (elf_interpreter) {
905                 if (interpreter_type == INTERPRETER_AOUT)
906                         elf_entry = load_aout_interp(&loc->interp_ex,
907                                                      interpreter);
908                 else
909                         elf_entry = load_elf_interp(&loc->interp_elf_ex,
910                                                     interpreter,
911                                                     &interp_load_addr);
912                 if (BAD_ADDR(elf_entry)) {
913                         printk(KERN_ERR "Unable to load interpreter %.128s\n",
914                                 elf_interpreter);
915                         force_sig(SIGSEGV, current);
916                         retval = -ENOEXEC; /* Nobody gets to see this, but.. */
917                         goto out_free_dentry;
918                 }
919                 reloc_func_desc = interp_load_addr;
920
921                 allow_write_access(interpreter);
922                 fput(interpreter);
923                 kfree(elf_interpreter);
924         } else {
925                 elf_entry = loc->elf_ex.e_entry;
926         }
927
928         kfree(elf_phdata);
929
930         if (interpreter_type != INTERPRETER_AOUT)
931                 sys_close(elf_exec_fileno);
932
933         set_binfmt(&elf_format);
934
935         compute_creds(bprm);
936         current->flags &= ~PF_FORKNOEXEC;
937         create_elf_tables(bprm, &loc->elf_ex, (interpreter_type == INTERPRETER_AOUT),
938                         load_addr, interp_load_addr);
939         /* N.B. passed_fileno might not be initialized? */
940         if (interpreter_type == INTERPRETER_AOUT)
941                 current->mm->arg_start += strlen(passed_fileno) + 1;
942         current->mm->end_code = end_code;
943         current->mm->start_code = start_code;
944         current->mm->start_data = start_data;
945         current->mm->end_data = end_data;
946         current->mm->start_stack = bprm->p;
947
948         if (current->personality & MMAP_PAGE_ZERO) {
949                 /* Why this, you ask???  Well SVr4 maps page 0 as read-only,
950                    and some applications "depend" upon this behavior.
951                    Since we do not have the power to recompile these, we
952                    emulate the SVr4 behavior.  Sigh.  */
953                 down_write(&current->mm->mmap_sem);
954                 error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
955                                 MAP_FIXED | MAP_PRIVATE, 0);
956                 up_write(&current->mm->mmap_sem);
957         }
958
959 #ifdef ELF_PLAT_INIT
960         /*
961          * The ABI may specify that certain registers be set up in special
962          * ways (on i386 %edx is the address of a DT_FINI function, for
963          * example.  In addition, it may also specify (eg, PowerPC64 ELF)
964          * that the e_entry field is the address of the function descriptor
965          * for the startup routine, rather than the address of the startup
966          * routine itself.  This macro performs whatever initialization to
967          * the regs structure is required as well as any relocations to the
968          * function descriptor entries when executing dynamically links apps.
969          */
970         ELF_PLAT_INIT(regs, reloc_func_desc);
971 #endif
972
973         start_thread(regs, elf_entry, bprm->p);
974         if (unlikely(current->ptrace & PT_PTRACED)) {
975                 if (current->ptrace & PT_TRACE_EXEC)
976                         ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
977                 else
978                         send_sig(SIGTRAP, current, 0);
979         }
980         retval = 0;
981 out:
982         kfree(loc);
983 out_ret:
984         return retval;
985
986         /* error cleanup */
987 out_free_dentry:
988         allow_write_access(interpreter);
989         if (interpreter)
990                 fput(interpreter);
991 out_free_interp:
992         if (elf_interpreter)
993                 kfree(elf_interpreter);
994 out_free_file:
995         sys_close(elf_exec_fileno);
996 out_free_fh:
997         if (files) {
998                 put_files_struct(current->files);
999                 current->files = files;
1000         }
1001 out_free_ph:
1002         kfree(elf_phdata);
1003         goto out;
1004 }
1005
1006 /* This is really simpleminded and specialized - we are loading an
1007    a.out library that is given an ELF header. */
1008
1009 static int load_elf_library(struct file *file)
1010 {
1011         struct elf_phdr *elf_phdata;
1012         struct elf_phdr *eppnt;
1013         unsigned long elf_bss, bss, len;
1014         int retval, error, i, j;
1015         struct elfhdr elf_ex;
1016
1017         error = -ENOEXEC;
1018         retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
1019         if (retval != sizeof(elf_ex))
1020                 goto out;
1021
1022         if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
1023                 goto out;
1024
1025         /* First of all, some simple consistency checks */
1026         if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
1027            !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
1028                 goto out;
1029
1030         /* Now read in all of the header information */
1031
1032         j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
1033         /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1034
1035         error = -ENOMEM;
1036         elf_phdata = kmalloc(j, GFP_KERNEL);
1037         if (!elf_phdata)
1038                 goto out;
1039
1040         eppnt = elf_phdata;
1041         error = -ENOEXEC;
1042         retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
1043         if (retval != j)
1044                 goto out_free_ph;
1045
1046         for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
1047                 if ((eppnt + i)->p_type == PT_LOAD)
1048                         j++;
1049         if (j != 1)
1050                 goto out_free_ph;
1051
1052         while (eppnt->p_type != PT_LOAD)
1053                 eppnt++;
1054
1055         /* Now use mmap to map the library into memory. */
1056         down_write(&current->mm->mmap_sem);
1057         error = do_mmap(file,
1058                         ELF_PAGESTART(eppnt->p_vaddr),
1059                         (eppnt->p_filesz +
1060                          ELF_PAGEOFFSET(eppnt->p_vaddr)),
1061                         PROT_READ | PROT_WRITE | PROT_EXEC,
1062                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
1063                         (eppnt->p_offset -
1064                          ELF_PAGEOFFSET(eppnt->p_vaddr)));
1065         up_write(&current->mm->mmap_sem);
1066         if (error != ELF_PAGESTART(eppnt->p_vaddr))
1067                 goto out_free_ph;
1068
1069         elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
1070         if (padzero(elf_bss)) {
1071                 error = -EFAULT;
1072                 goto out_free_ph;
1073         }
1074
1075         len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr + ELF_MIN_ALIGN - 1);
1076         bss = eppnt->p_memsz + eppnt->p_vaddr;
1077         if (bss > len) {
1078                 down_write(&current->mm->mmap_sem);
1079                 do_brk(len, bss - len);
1080                 up_write(&current->mm->mmap_sem);
1081         }
1082         error = 0;
1083
1084 out_free_ph:
1085         kfree(elf_phdata);
1086 out:
1087         return error;
1088 }
1089
1090 /*
1091  * Note that some platforms still use traditional core dumps and not
1092  * the ELF core dump.  Each platform can select it as appropriate.
1093  */
1094 #ifdef USE_ELF_CORE_DUMP
1095
1096 /*
1097  * ELF core dumper
1098  *
1099  * Modelled on fs/exec.c:aout_core_dump()
1100  * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1101  */
1102 /*
1103  * These are the only things you should do on a core-file: use only these
1104  * functions to write out all the necessary info.
1105  */
1106 static int dump_write(struct file *file, const void *addr, int nr)
1107 {
1108         return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1109 }
1110
1111 static int dump_seek(struct file *file, off_t off)
1112 {
1113         if (file->f_op->llseek) {
1114                 if (file->f_op->llseek(file, off, 0) != off)
1115                         return 0;
1116         } else
1117                 file->f_pos = off;
1118         return 1;
1119 }
1120
1121 /*
1122  * Decide whether a segment is worth dumping; default is yes to be
1123  * sure (missing info is worse than too much; etc).
1124  * Personally I'd include everything, and use the coredump limit...
1125  *
1126  * I think we should skip something. But I am not sure how. H.J.
1127  */
1128 static int maydump(struct vm_area_struct *vma)
1129 {
1130         /* Do not dump I/O mapped devices, shared memory, or special mappings */
1131         if (vma->vm_flags & (VM_IO | VM_SHARED | VM_RESERVED))
1132                 return 0;
1133
1134         /* If it hasn't been written to, don't write it out */
1135         if (!vma->anon_vma)
1136                 return 0;
1137
1138         return 1;
1139 }
1140
1141 #define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))
1142
1143 /* An ELF note in memory */
1144 struct memelfnote
1145 {
1146         const char *name;
1147         int type;
1148         unsigned int datasz;
1149         void *data;
1150 };
1151
1152 static int notesize(struct memelfnote *en)
1153 {
1154         int sz;
1155
1156         sz = sizeof(struct elf_note);
1157         sz += roundup(strlen(en->name) + 1, 4);
1158         sz += roundup(en->datasz, 4);
1159
1160         return sz;
1161 }
1162
1163 #define DUMP_WRITE(addr, nr)    \
1164         do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1165 #define DUMP_SEEK(off)  \
1166         do { if (!dump_seek(file, (off))) return 0; } while(0)
1167
1168 static int writenote(struct memelfnote *men, struct file *file)
1169 {
1170         struct elf_note en;
1171
1172         en.n_namesz = strlen(men->name) + 1;
1173         en.n_descsz = men->datasz;
1174         en.n_type = men->type;
1175
1176         DUMP_WRITE(&en, sizeof(en));
1177         DUMP_WRITE(men->name, en.n_namesz);
1178         /* XXX - cast from long long to long to avoid need for libgcc.a */
1179         DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));      /* XXX */
1180         DUMP_WRITE(men->data, men->datasz);
1181         DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));      /* XXX */
1182
1183         return 1;
1184 }
1185 #undef DUMP_WRITE
1186 #undef DUMP_SEEK
1187
1188 #define DUMP_WRITE(addr, nr)    \
1189         if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1190                 goto end_coredump;
1191 #define DUMP_SEEK(off)  \
1192         if (!dump_seek(file, (off))) \
1193                 goto end_coredump;
1194
1195 static inline void fill_elf_header(struct elfhdr *elf, int segs)
1196 {
1197         memcpy(elf->e_ident, ELFMAG, SELFMAG);
1198         elf->e_ident[EI_CLASS] = ELF_CLASS;
1199         elf->e_ident[EI_DATA] = ELF_DATA;
1200         elf->e_ident[EI_VERSION] = EV_CURRENT;
1201         elf->e_ident[EI_OSABI] = ELF_OSABI;
1202         memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1203
1204         elf->e_type = ET_CORE;
1205         elf->e_machine = ELF_ARCH;
1206         elf->e_version = EV_CURRENT;
1207         elf->e_entry = 0;
1208         elf->e_phoff = sizeof(struct elfhdr);
1209         elf->e_shoff = 0;
1210         elf->e_flags = ELF_CORE_EFLAGS;
1211         elf->e_ehsize = sizeof(struct elfhdr);
1212         elf->e_phentsize = sizeof(struct elf_phdr);
1213         elf->e_phnum = segs;
1214         elf->e_shentsize = 0;
1215         elf->e_shnum = 0;
1216         elf->e_shstrndx = 0;
1217         return;
1218 }
1219
1220 static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
1221 {
1222         phdr->p_type = PT_NOTE;
1223         phdr->p_offset = offset;
1224         phdr->p_vaddr = 0;
1225         phdr->p_paddr = 0;
1226         phdr->p_filesz = sz;
1227         phdr->p_memsz = 0;
1228         phdr->p_flags = 0;
1229         phdr->p_align = 0;
1230         return;
1231 }
1232
1233 static void fill_note(struct memelfnote *note, const char *name, int type, 
1234                 unsigned int sz, void *data)
1235 {
1236         note->name = name;
1237         note->type = type;
1238         note->datasz = sz;
1239         note->data = data;
1240         return;
1241 }
1242
1243 /*
1244  * fill up all the fields in prstatus from the given task struct, except registers
1245  * which need to be filled up separately.
1246  */
1247 static void fill_prstatus(struct elf_prstatus *prstatus,
1248                         struct task_struct *p, long signr) 
1249 {
1250         prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1251         prstatus->pr_sigpend = p->pending.signal.sig[0];
1252         prstatus->pr_sighold = p->blocked.sig[0];
1253         prstatus->pr_pid = p->pid;
1254         prstatus->pr_ppid = p->parent->pid;
1255         prstatus->pr_pgrp = process_group(p);
1256         prstatus->pr_sid = p->signal->session;
1257         if (thread_group_leader(p)) {
1258                 /*
1259                  * This is the record for the group leader.  Add in the
1260                  * cumulative times of previous dead threads.  This total
1261                  * won't include the time of each live thread whose state
1262                  * is included in the core dump.  The final total reported
1263                  * to our parent process when it calls wait4 will include
1264                  * those sums as well as the little bit more time it takes
1265                  * this and each other thread to finish dying after the
1266                  * core dump synchronization phase.
1267                  */
1268                 cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
1269                                    &prstatus->pr_utime);
1270                 cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
1271                                    &prstatus->pr_stime);
1272         } else {
1273                 cputime_to_timeval(p->utime, &prstatus->pr_utime);
1274                 cputime_to_timeval(p->stime, &prstatus->pr_stime);
1275         }
1276         cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
1277         cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
1278 }
1279
1280 static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1281                        struct mm_struct *mm)
1282 {
1283         unsigned int i, len;
1284         
1285         /* first copy the parameters from user space */
1286         memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1287
1288         len = mm->arg_end - mm->arg_start;
1289         if (len >= ELF_PRARGSZ)
1290                 len = ELF_PRARGSZ-1;
1291         if (copy_from_user(&psinfo->pr_psargs,
1292                            (const char __user *)mm->arg_start, len))
1293                 return -EFAULT;
1294         for(i = 0; i < len; i++)
1295                 if (psinfo->pr_psargs[i] == 0)
1296                         psinfo->pr_psargs[i] = ' ';
1297         psinfo->pr_psargs[len] = 0;
1298
1299         psinfo->pr_pid = p->pid;
1300         psinfo->pr_ppid = p->parent->pid;
1301         psinfo->pr_pgrp = process_group(p);
1302         psinfo->pr_sid = p->signal->session;
1303
1304         i = p->state ? ffz(~p->state) + 1 : 0;
1305         psinfo->pr_state = i;
1306         psinfo->pr_sname = (i < 0 || i > 5) ? '.' : "RSDTZW"[i];
1307         psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1308         psinfo->pr_nice = task_nice(p);
1309         psinfo->pr_flag = p->flags;
1310         SET_UID(psinfo->pr_uid, p->uid);
1311         SET_GID(psinfo->pr_gid, p->gid);
1312         strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1313         
1314         return 0;
1315 }
1316
1317 /* Here is the structure in which status of each thread is captured. */
1318 struct elf_thread_status
1319 {
1320         struct list_head list;
1321         struct elf_prstatus prstatus;   /* NT_PRSTATUS */
1322         elf_fpregset_t fpu;             /* NT_PRFPREG */
1323         struct task_struct *thread;
1324 #ifdef ELF_CORE_COPY_XFPREGS
1325         elf_fpxregset_t xfpu;           /* NT_PRXFPREG */
1326 #endif
1327         struct memelfnote notes[3];
1328         int num_notes;
1329 };
1330
1331 /*
1332  * In order to add the specific thread information for the elf file format,
1333  * we need to keep a linked list of every threads pr_status and then
1334  * create a single section for them in the final core file.
1335  */
1336 static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1337 {
1338         int sz = 0;
1339         struct task_struct *p = t->thread;
1340         t->num_notes = 0;
1341
1342         fill_prstatus(&t->prstatus, p, signr);
1343         elf_core_copy_task_regs(p, &t->prstatus.pr_reg);        
1344         
1345         fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus), &(t->prstatus));
1346         t->num_notes++;
1347         sz += notesize(&t->notes[0]);
1348
1349         if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, &t->fpu))) {
1350                 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu), &(t->fpu));
1351                 t->num_notes++;
1352                 sz += notesize(&t->notes[1]);
1353         }
1354
1355 #ifdef ELF_CORE_COPY_XFPREGS
1356         if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
1357                 fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu), &t->xfpu);
1358                 t->num_notes++;
1359                 sz += notesize(&t->notes[2]);
1360         }
1361 #endif  
1362         return sz;
1363 }
1364
1365 /*
1366  * Actual dumper
1367  *
1368  * This is a two-pass process; first we find the offsets of the bits,
1369  * and then they are actually written out.  If we run out of core limit
1370  * we just truncate.
1371  */
1372 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
1373 {
1374 #define NUM_NOTES       6
1375         int has_dumped = 0;
1376         mm_segment_t fs;
1377         int segs;
1378         size_t size = 0;
1379         int i;
1380         struct vm_area_struct *vma;
1381         struct elfhdr *elf = NULL;
1382         off_t offset = 0, dataoff;
1383         unsigned long limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1384         int numnote;
1385         struct memelfnote *notes = NULL;
1386         struct elf_prstatus *prstatus = NULL;   /* NT_PRSTATUS */
1387         struct elf_prpsinfo *psinfo = NULL;     /* NT_PRPSINFO */
1388         struct task_struct *g, *p;
1389         LIST_HEAD(thread_list);
1390         struct list_head *t;
1391         elf_fpregset_t *fpu = NULL;
1392 #ifdef ELF_CORE_COPY_XFPREGS
1393         elf_fpxregset_t *xfpu = NULL;
1394 #endif
1395         int thread_status_size = 0;
1396         elf_addr_t *auxv;
1397
1398         /*
1399          * We no longer stop all VM operations.
1400          * 
1401          * This is because those proceses that could possibly change map_count or
1402          * the mmap / vma pages are now blocked in do_exit on current finishing
1403          * this core dump.
1404          *
1405          * Only ptrace can touch these memory addresses, but it doesn't change
1406          * the map_count or the pages allocated.  So no possibility of crashing
1407          * exists while dumping the mm->vm_next areas to the core file.
1408          */
1409   
1410         /* alloc memory for large data structures: too large to be on stack */
1411         elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1412         if (!elf)
1413                 goto cleanup;
1414         prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
1415         if (!prstatus)
1416                 goto cleanup;
1417         psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1418         if (!psinfo)
1419                 goto cleanup;
1420         notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1421         if (!notes)
1422                 goto cleanup;
1423         fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1424         if (!fpu)
1425                 goto cleanup;
1426 #ifdef ELF_CORE_COPY_XFPREGS
1427         xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1428         if (!xfpu)
1429                 goto cleanup;
1430 #endif
1431
1432         if (signr) {
1433                 struct elf_thread_status *tmp;
1434                 read_lock(&tasklist_lock);
1435                 do_each_thread(g,p)
1436                         if (current->mm == p->mm && current != p) {
1437                                 tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC);
1438                                 if (!tmp) {
1439                                         read_unlock(&tasklist_lock);
1440                                         goto cleanup;
1441                                 }
1442                                 memset(tmp, 0, sizeof(*tmp));
1443                                 INIT_LIST_HEAD(&tmp->list);
1444                                 tmp->thread = p;
1445                                 list_add(&tmp->list, &thread_list);
1446                         }
1447                 while_each_thread(g,p);
1448                 read_unlock(&tasklist_lock);
1449                 list_for_each(t, &thread_list) {
1450                         struct elf_thread_status *tmp;
1451                         int sz;
1452
1453                         tmp = list_entry(t, struct elf_thread_status, list);
1454                         sz = elf_dump_thread_status(signr, tmp);
1455                         thread_status_size += sz;
1456                 }
1457         }
1458         /* now collect the dump for the current */
1459         memset(prstatus, 0, sizeof(*prstatus));
1460         fill_prstatus(prstatus, current, signr);
1461         elf_core_copy_regs(&prstatus->pr_reg, regs);
1462         
1463         segs = current->mm->map_count;
1464 #ifdef ELF_CORE_EXTRA_PHDRS
1465         segs += ELF_CORE_EXTRA_PHDRS;
1466 #endif
1467
1468         /* Set up header */
1469         fill_elf_header(elf, segs+1);   /* including notes section */
1470
1471         has_dumped = 1;
1472         current->flags |= PF_DUMPCORE;
1473
1474         /*
1475          * Set up the notes in similar form to SVR4 core dumps made
1476          * with info from their /proc.
1477          */
1478
1479         fill_note(notes +0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
1480         
1481         fill_psinfo(psinfo, current->group_leader, current->mm);
1482         fill_note(notes +1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1483         
1484         fill_note(notes +2, "CORE", NT_TASKSTRUCT, sizeof(*current), current);
1485   
1486         numnote = 3;
1487
1488         auxv = (elf_addr_t *) current->mm->saved_auxv;
1489
1490         i = 0;
1491         do
1492                 i += 2;
1493         while (auxv[i - 2] != AT_NULL);
1494         fill_note(&notes[numnote++], "CORE", NT_AUXV,
1495                   i * sizeof (elf_addr_t), auxv);
1496
1497         /* Try to dump the FPU. */
1498         if ((prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, regs, fpu)))
1499                 fill_note(notes + numnote++,
1500                           "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1501 #ifdef ELF_CORE_COPY_XFPREGS
1502         if (elf_core_copy_task_xfpregs(current, xfpu))
1503                 fill_note(notes + numnote++,
1504                           "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
1505 #endif  
1506   
1507         fs = get_fs();
1508         set_fs(KERNEL_DS);
1509
1510         DUMP_WRITE(elf, sizeof(*elf));
1511         offset += sizeof(*elf);                         /* Elf header */
1512         offset += (segs+1) * sizeof(struct elf_phdr);   /* Program headers */
1513
1514         /* Write notes phdr entry */
1515         {
1516                 struct elf_phdr phdr;
1517                 int sz = 0;
1518
1519                 for (i = 0; i < numnote; i++)
1520                         sz += notesize(notes + i);
1521                 
1522                 sz += thread_status_size;
1523
1524                 fill_elf_note_phdr(&phdr, sz, offset);
1525                 offset += sz;
1526                 DUMP_WRITE(&phdr, sizeof(phdr));
1527         }
1528
1529         /* Page-align dumped data */
1530         dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1531
1532         /* Write program headers for segments dump */
1533         for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1534                 struct elf_phdr phdr;
1535                 size_t sz;
1536
1537                 sz = vma->vm_end - vma->vm_start;
1538
1539                 phdr.p_type = PT_LOAD;
1540                 phdr.p_offset = offset;
1541                 phdr.p_vaddr = vma->vm_start;
1542                 phdr.p_paddr = 0;
1543                 phdr.p_filesz = maydump(vma) ? sz : 0;
1544                 phdr.p_memsz = sz;
1545                 offset += phdr.p_filesz;
1546                 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1547                 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1548                 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1549                 phdr.p_align = ELF_EXEC_PAGESIZE;
1550
1551                 DUMP_WRITE(&phdr, sizeof(phdr));
1552         }
1553
1554 #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1555         ELF_CORE_WRITE_EXTRA_PHDRS;
1556 #endif
1557
1558         /* write out the notes section */
1559         for (i = 0; i < numnote; i++)
1560                 if (!writenote(notes + i, file))
1561                         goto end_coredump;
1562
1563         /* write out the thread status notes section */
1564         list_for_each(t, &thread_list) {
1565                 struct elf_thread_status *tmp = list_entry(t, struct elf_thread_status, list);
1566                 for (i = 0; i < tmp->num_notes; i++)
1567                         if (!writenote(&tmp->notes[i], file))
1568                                 goto end_coredump;
1569         }
1570  
1571         DUMP_SEEK(dataoff);
1572
1573         for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1574                 unsigned long addr;
1575
1576                 if (!maydump(vma))
1577                         continue;
1578
1579                 for (addr = vma->vm_start;
1580                      addr < vma->vm_end;
1581                      addr += PAGE_SIZE) {
1582                         struct page* page;
1583                         struct vm_area_struct *vma;
1584
1585                         if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1586                                                 &page, &vma) <= 0) {
1587                                 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1588                         } else {
1589                                 if (page == ZERO_PAGE(addr)) {
1590                                         DUMP_SEEK (file->f_pos + PAGE_SIZE);
1591                                 } else {
1592                                         void *kaddr;
1593                                         flush_cache_page(vma, addr);
1594                                         kaddr = kmap(page);
1595                                         if ((size += PAGE_SIZE) > limit ||
1596                                             !dump_write(file, kaddr,
1597                                             PAGE_SIZE)) {
1598                                                 kunmap(page);
1599                                                 page_cache_release(page);
1600                                                 goto end_coredump;
1601                                         }
1602                                         kunmap(page);
1603                                 }
1604                                 page_cache_release(page);
1605                         }
1606                 }
1607         }
1608
1609 #ifdef ELF_CORE_WRITE_EXTRA_DATA
1610         ELF_CORE_WRITE_EXTRA_DATA;
1611 #endif
1612
1613         if ((off_t) file->f_pos != offset) {
1614                 /* Sanity check */
1615                 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1616                        (off_t) file->f_pos, offset);
1617         }
1618
1619 end_coredump:
1620         set_fs(fs);
1621
1622 cleanup:
1623         while(!list_empty(&thread_list)) {
1624                 struct list_head *tmp = thread_list.next;
1625                 list_del(tmp);
1626                 kfree(list_entry(tmp, struct elf_thread_status, list));
1627         }
1628
1629         kfree(elf);
1630         kfree(prstatus);
1631         kfree(psinfo);
1632         kfree(notes);
1633         kfree(fpu);
1634 #ifdef ELF_CORE_COPY_XFPREGS
1635         kfree(xfpu);
1636 #endif
1637         return has_dumped;
1638 #undef NUM_NOTES
1639 }
1640
1641 #endif          /* USE_ELF_CORE_DUMP */
1642
1643 static int __init init_elf_binfmt(void)
1644 {
1645         return register_binfmt(&elf_format);
1646 }
1647
1648 static void __exit exit_elf_binfmt(void)
1649 {
1650         /* Remove the COFF and ELF loaders. */
1651         unregister_binfmt(&elf_format);
1652 }
1653
1654 core_initcall(init_elf_binfmt);
1655 module_exit(exit_elf_binfmt);
1656 MODULE_LICENSE("GPL");