fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / mips / kernel / irixelf.c
index 6a2b362..37cad5d 100644 (file)
@@ -1,15 +1,16 @@
 /*
- * irixelf.c: Code to load IRIX ELF executables which conform to
- *            the MIPS ABI.
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file "COPYING" in the main directory of this archive
+ * for more details.
  *
- * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
+ * irixelf.c: Code to load IRIX ELF executables conforming to the MIPS ABI.
+ *            Based off of work by Eric Youngdale.
  *
- * Based upon work which is:
- * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
+ * Copyright (C) 1993 - 1994 Eric Youngdale <ericy@cais.com>
+ * Copyright (C) 1996 - 2004 David S. Miller <dm@engr.sgi.com>
+ * Copyright (C) 2004 - 2005 Steven J. Hill <sjhill@realitydiluted.com>
  */
-
 #include <linux/module.h>
-
 #include <linux/fs.h>
 #include <linux/stat.h>
 #include <linux/sched.h>
 #include <linux/elfcore.h>
 #include <linux/smp_lock.h>
 
-#include <asm/uaccess.h>
 #include <asm/mipsregs.h>
+#include <asm/namei.h>
 #include <asm/prctl.h>
+#include <asm/uaccess.h>
 
 #define DLINFO_ITEMS 12
 
 #include <linux/elf.h>
 
-#undef DEBUG_ELF
+#undef DEBUG
 
 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs);
 static int load_irix_library(struct file *);
@@ -50,11 +52,7 @@ static struct linux_binfmt irix_format = {
        irix_core_dump, PAGE_SIZE
 };
 
-#ifndef elf_addr_t
-#define elf_addr_t unsigned long
-#endif
-
-#ifdef DEBUG_ELF
+#ifdef DEBUG
 /* Debugging routines. */
 static char *get_elf_p_type(Elf32_Word p_type)
 {
@@ -119,7 +117,7 @@ static void dump_phdrs(struct elf_phdr *ep, int pnum)
                        print_phdr(i, ep);
        }
 }
-#endif /* (DEBUG_ELF) */
+#endif /* DEBUG */
 
 static void set_brk(unsigned long start, unsigned long end)
 {
@@ -127,7 +125,9 @@ static void set_brk(unsigned long start, unsigned long end)
        end = PAGE_ALIGN(end);
        if (end <= start)
                return;
+       down_write(&current->mm->mmap_sem);
        do_brk(start, end - start);
+       up_write(&current->mm->mmap_sem);
 }
 
 
@@ -143,20 +143,20 @@ static void padzero(unsigned long elf_bss)
        nbyte = elf_bss & (PAGE_SIZE-1);
        if (nbyte) {
                nbyte = PAGE_SIZE - nbyte;
-               clear_user((void *) elf_bss, nbyte);
+               clear_user((void __user *) elf_bss, nbyte);
        }
 }
 
-unsigned long * create_irix_tables(char * p, int argc, int envc,
-                                  struct elfhdr * exec, unsigned int load_addr,
-                                  unsigned int interp_load_addr,
-                                  struct pt_regs *regs, struct elf_phdr *ephdr)
+static unsigned long * create_irix_tables(char * p, int argc, int envc,
+       struct elfhdr * exec, unsigned int load_addr,
+       unsigned int interp_load_addr, struct pt_regs *regs,
+       struct elf_phdr *ephdr)
 {
        elf_addr_t *argv;
        elf_addr_t *envp;
        elf_addr_t *sp, *csp;
 
-#ifdef DEBUG_ELF
+#ifdef DEBUG
        printk("create_irix_tables: p[%p] argc[%d] envc[%d] "
               "load_addr[%08x] interp_load_addr[%08x]\n",
               p, argc, envc, load_addr, interp_load_addr);
@@ -209,13 +209,13 @@ unsigned long * create_irix_tables(char * p, int argc, int envc,
                __put_user((unsigned long)p,argv++);
                p += strlen_user(p);
        }
-       __put_user(NULL, argv);
+       __put_user((unsigned long) NULL, argv);
        current->mm->arg_end = current->mm->env_start = (unsigned long) p;
        while (envc-->0) {
                __put_user((unsigned long)p,envp++);
                p += strlen_user(p);
        }
-       __put_user(NULL, envp);
+       __put_user((unsigned long) NULL, envp);
        current->mm->env_end = (unsigned long) p;
        return sp;
 }
@@ -245,14 +245,13 @@ static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
        last_bss = 0;
        error = load_addr = 0;
 
-#ifdef DEBUG_ELF
+#ifdef DEBUG
        print_elfhdr(interp_elf_ex);
 #endif
 
        /* First of all, some simple consistency checks */
        if ((interp_elf_ex->e_type != ET_EXEC &&
             interp_elf_ex->e_type != ET_DYN) ||
-            !irix_elf_check_arch(interp_elf_ex) ||
             !interpreter->f_op->mmap) {
                printk("IRIX interp has bad e_type %d\n", interp_elf_ex->e_type);
                return 0xffffffff;
@@ -265,9 +264,8 @@ static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
            return 0xffffffff;
        }
 
-       elf_phdata =  (struct elf_phdr *)
-               kmalloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum,
-                       GFP_KERNEL);
+       elf_phdata = kmalloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum,
+                            GFP_KERNEL);
 
        if(!elf_phdata) {
           printk("Cannot kmalloc phdata for IRIX interp.\n");
@@ -288,7 +286,7 @@ static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
                           (char *) elf_phdata,
                           sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
 
-#ifdef DEBUG_ELF
+#ifdef DEBUG
        dump_phdrs(elf_phdata, interp_elf_ex->e_phnum);
 #endif
 
@@ -304,13 +302,11 @@ static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
            elf_type |= MAP_FIXED;
            vaddr = eppnt->p_vaddr;
 
-#ifdef DEBUG_ELF
-           printk("INTERP do_mmap(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
+           pr_debug("INTERP do_mmap(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
                   interpreter, vaddr,
                   (unsigned long) (eppnt->p_filesz + (eppnt->p_vaddr & 0xfff)),
                   (unsigned long) elf_prot, (unsigned long) elf_type,
                   (unsigned long) (eppnt->p_offset & 0xfffff000));
-#endif
            down_write(&current->mm->mmap_sem);
            error = do_mmap(interpreter, vaddr,
                            eppnt->p_filesz + (eppnt->p_vaddr & 0xfff),
@@ -322,14 +318,10 @@ static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
                    printk("Aieee IRIX interp mmap error=%d\n", error);
                    break;  /* Real error */
            }
-#ifdef DEBUG_ELF
-           printk("error=%08lx ", (unsigned long) error);
-#endif
+           pr_debug("error=%08lx ", (unsigned long) error);
            if(!load_addr && interp_elf_ex->e_type == ET_DYN) {
              load_addr = error;
-#ifdef DEBUG_ELF
-              printk("load_addr = error ");
-#endif
+              pr_debug("load_addr = error ");
            }
 
            /* Find the end of the file  mapping for this phdr, and keep
@@ -343,17 +335,13 @@ static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
             */
            k = eppnt->p_memsz + eppnt->p_vaddr;
            if(k > last_bss) last_bss = k;
-#ifdef DEBUG_ELF
-           printk("\n");
-#endif
+           pr_debug("\n");
          }
        }
 
        /* Now use mmap to map the library into memory. */
        if(error < 0 && error > -1024) {
-#ifdef DEBUG_ELF
-               printk("got error %d\n", error);
-#endif
+               pr_debug("got error %d\n", error);
                kfree(elf_phdata);
                return 0xffffffff;
        }
@@ -363,20 +351,18 @@ static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
         * that there are zero-mapped pages up to and including the
         * last bss page.
         */
-#ifdef DEBUG_ELF
-       printk("padzero(%08lx) ", (unsigned long) (elf_bss));
-#endif
+       pr_debug("padzero(%08lx) ", (unsigned long) (elf_bss));
        padzero(elf_bss);
        len = (elf_bss + 0xfff) & 0xfffff000; /* What we have mapped so far */
 
-#ifdef DEBUG_ELF
-       printk("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss,
-              (unsigned long) len);
-#endif
+       pr_debug("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss,
+                (unsigned long) len);
 
        /* Map the last of the bss segment */
        if (last_bss > len) {
+               down_write(&current->mm->mmap_sem);
                do_brk(len, (last_bss - len));
+               up_write(&current->mm->mmap_sem);
        }
        kfree(elf_phdata);
 
@@ -392,12 +378,7 @@ static int verify_binary(struct elfhdr *ehp, struct linux_binprm *bprm)
 
        /* First of all, some simple consistency checks */
        if((ehp->e_type != ET_EXEC && ehp->e_type != ET_DYN) ||
-           !irix_elf_check_arch(ehp) || !bprm->file->f_op->mmap) {
-               return -ENOEXEC;
-       }
-
-       /* Only support MIPS ARCH2 or greater IRIX binaries for now. */
-       if(!(ehp->e_flags & EF_MIPS_ARCH) && !(ehp->e_flags & 0x04)) {
+           !bprm->file->f_op->mmap) {
                return -ENOEXEC;
        }
 
@@ -407,16 +388,17 @@ static int verify_binary(struct elfhdr *ehp, struct linux_binprm *bprm)
         * XXX all registers as 64bits on cpu's capable of this at
         * XXX exception time plus frob the XTLB exception vector.
         */
-       if((ehp->e_flags & 0x20)) {
+       if((ehp->e_flags & EF_MIPS_ABI2))
                return -ENOEXEC;
-       }
 
-       return 0; /* It's ok. */
+       return 0;
 }
 
-#define IRIX_INTERP_PREFIX "/usr/gnemul/irix"
-
-/* Look for an IRIX ELF interpreter. */
+/*
+ * This is where the detailed check is performed. Irix binaries
+ * use interpreters with 'libc.so' in the name, so this function
+ * can differentiate between Linux and Irix binaries.
+ */
 static inline int look_for_irix_interpreter(char **name,
                                            struct file **interpreter,
                                            struct elfhdr *interp_elf_ex,
@@ -436,13 +418,11 @@ static inline int look_for_irix_interpreter(char **name,
                if (*name != NULL)
                        goto out;
 
-               *name = (char *) kmalloc((epp->p_filesz +
-                                         strlen(IRIX_INTERP_PREFIX)),
-                                        GFP_KERNEL);
+               *name = kmalloc(epp->p_filesz + strlen(IRIX_EMUL), GFP_KERNEL);
                if (!*name)
                        return -ENOMEM;
 
-               strcpy(*name, IRIX_INTERP_PREFIX);
+               strcpy(*name, IRIX_EMUL);
                retval = kernel_read(bprm->file, epp->p_offset, (*name + 16),
                                     epp->p_filesz);
                if (retval < 0)
@@ -559,12 +539,14 @@ static inline int map_interpreter(struct elf_phdr *epp, struct elfhdr *ihp,
  * process and the system, here we map the page and fill the
  * structure
  */
-void irix_map_prda_page (void)
+static void irix_map_prda_page(void)
 {
        unsigned long v;
        struct prda *pp;
 
+       down_write(&current->mm->mmap_sem);
        v =  do_brk (PRDA_ADDRESS, PAGE_SIZE);
+       up_write(&current->mm->mmap_sem);
 
        if (v < 0)
                return;
@@ -596,14 +578,33 @@ static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
 
        load_addr = 0;
        has_interp = has_ephdr = 0;
-       elf_ihdr = elf_ephdr = 0;
+       elf_ihdr = elf_ephdr = NULL;
        elf_ex = *((struct elfhdr *) bprm->buf);
        retval = -ENOEXEC;
 
        if (verify_binary(&elf_ex, bprm))
                goto out;
 
-#ifdef DEBUG_ELF
+       /*
+        * Telling -o32 static binaries from Linux and Irix apart from each
+        * other is difficult. There are 2 differences to be noted for static
+        * binaries from the 2 operating systems:
+        *
+        *    1) Irix binaries have their .text section before their .init
+        *       section. Linux binaries are just the opposite.
+        *
+        *    2) Irix binaries usually have <= 12 sections and Linux
+        *       binaries have > 20.
+        *
+        * We will use Method #2 since Method #1 would require us to read in
+        * the section headers which is way too much overhead. This appears
+        * to work for everything we have ran into so far. If anyone has a
+        * better method to tell the binaries apart, I'm listening.
+        */
+       if (elf_ex.e_shnum > 20)
+               goto out;
+
+#ifdef DEBUG
        print_elfhdr(&elf_ex);
 #endif
 
@@ -611,7 +612,7 @@ static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
        size = elf_ex.e_phentsize * elf_ex.e_phnum;
        if (size > 65536)
                goto out;
-       elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
+       elf_phdata = kmalloc(size, GFP_KERNEL);
        if (elf_phdata == NULL) {
                retval = -ENOMEM;
                goto out;
@@ -621,7 +622,7 @@ static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
        if (retval < 0)
                goto out_free_ph;
 
-#ifdef DEBUG_ELF
+#ifdef DEBUG
        dump_phdrs(elf_phdata, elf_ex.e_phnum);
 #endif
 
@@ -638,9 +639,8 @@ static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
                        break;
                };
        }
-#ifdef DEBUG_ELF
-       printk("\n");
-#endif
+
+       pr_debug("\n");
 
        elf_bss = 0;
        elf_brk = 0;
@@ -651,12 +651,19 @@ static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
        end_code = 0;
        end_data = 0;
 
-       retval = look_for_irix_interpreter(&elf_interpreter,
-                                          &interpreter,
+       /*
+        * If we get a return value, we change the value to be ENOEXEC
+        * so that we can exit gracefully and the main binary format
+        * search loop in 'fs/exec.c' will move onto the next handler
+        * which should be the normal ELF binary handler.
+        */
+       retval = look_for_irix_interpreter(&elf_interpreter, &interpreter,
                                           &interp_elf_ex, elf_phdata, bprm,
                                           elf_ex.e_phnum);
-       if (retval)
+       if (retval) {
+               retval = -ENOEXEC;
                goto out_free_file;
+       }
 
        if (elf_interpreter) {
                retval = verify_irix_interpreter(&interp_elf_ex);
@@ -686,8 +693,7 @@ static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
        /* Do this so that we can load the interpreter, if need be.  We will
         * change some of these later.
         */
-       current->mm->rss = 0;
-       setup_arg_pages(bprm, EXSTACK_DEFAULT);
+       setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
        current->mm->start_stack = bprm->p;
 
        /* At this point, we assume that the image should be loaded at
@@ -740,18 +746,16 @@ static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
         * IRIX maps a page at 0x200000 which holds some system
         * information.  Programs depend on this.
         */
-       irix_map_prda_page ();
+       irix_map_prda_page();
 
        padzero(elf_bss);
 
-#ifdef DEBUG_ELF
-       printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
-       printk("(end_code) %lx\n" , (long) current->mm->end_code);
-       printk("(start_code) %lx\n" , (long) current->mm->start_code);
-       printk("(end_data) %lx\n" , (long) current->mm->end_data);
-       printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
-       printk("(brk) %lx\n" , (long) current->mm->brk);
-#endif
+       pr_debug("(start_brk) %lx\n" , (long) current->mm->start_brk);
+       pr_debug("(end_code) %lx\n" , (long) current->mm->end_code);
+       pr_debug("(start_code) %lx\n" , (long) current->mm->start_code);
+       pr_debug("(end_data) %lx\n" , (long) current->mm->end_data);
+       pr_debug("(start_stack) %lx\n" , (long) current->mm->start_stack);
+       pr_debug("(brk) %lx\n" , (long) current->mm->brk);
 
 #if 0 /* XXX No fucking way dude... */
        /* Why this, you ask???  Well SVr4 maps page 0 as read-only,
@@ -776,8 +780,7 @@ out_free_dentry:
        allow_write_access(interpreter);
        fput(interpreter);
 out_free_interp:
-       if (elf_interpreter)
-               kfree(elf_interpreter);
+       kfree(elf_interpreter);
 out_free_file:
 out_free_ph:
        kfree (elf_phdata);
@@ -807,15 +810,14 @@ static int load_irix_library(struct file *file)
 
        /* First of all, some simple consistency checks. */
        if(elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
-          !irix_elf_check_arch(&elf_ex) || !file->f_op->mmap)
+          !file->f_op->mmap)
                return -ENOEXEC;
 
        /* Now read in all of the header information. */
        if(sizeof(struct elf_phdr) * elf_ex.e_phnum > PAGE_SIZE)
                return -ENOEXEC;
 
-       elf_phdata =  (struct elf_phdr *)
-               kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
+       elf_phdata = kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
        if (elf_phdata == NULL)
                return -ENOMEM;
 
@@ -855,8 +857,11 @@ static int load_irix_library(struct file *file)
 
        len = (elf_phdata->p_filesz + elf_phdata->p_vaddr+ 0xfff) & 0xfffff000;
        bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
-       if (bss > len)
+       if (bss > len) {
+         down_write(&current->mm->mmap_sem);
          do_brk(len, bss-len);
+         up_write(&current->mm->mmap_sem);
+       }
        kfree(elf_phdata);
        return 0;
 }
@@ -866,36 +871,36 @@ static int load_irix_library(struct file *file)
  * phdrs there are in the USER_PHDRP array.  We return the vaddr the
  * first phdr was successfully mapped to.
  */
-unsigned long irix_mapelf(int fd, struct elf_phdr *user_phdrp, int cnt)
+unsigned long irix_mapelf(int fd, struct elf_phdr __user *user_phdrp, int cnt)
 {
-       struct elf_phdr *hp;
+       unsigned long type, vaddr, filesz, offset, flags;
+       struct elf_phdr __user *hp;
        struct file *filp;
        int i, retval;
 
-#ifdef DEBUG_ELF
-       printk("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
-              fd, user_phdrp, cnt);
-#endif
+       pr_debug("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
+                fd, user_phdrp, cnt);
 
        /* First get the verification out of the way. */
        hp = user_phdrp;
-       retval = verify_area(VERIFY_READ, hp, (sizeof(struct elf_phdr) * cnt));
-       if(retval) {
-#ifdef DEBUG_ELF
-               printk("irix_mapelf: verify_area fails!\n");
-#endif
-               return retval;
+       if (!access_ok(VERIFY_READ, hp, (sizeof(struct elf_phdr) * cnt))) {
+               pr_debug("irix_mapelf: bad pointer to ELF PHDR!\n");
+
+               return -EFAULT;
        }
 
-#ifdef DEBUG_ELF
+#ifdef DEBUG
        dump_phdrs(user_phdrp, cnt);
 #endif
 
-       for(i = 0; i < cnt; i++, hp++)
-               if(hp->p_type != PT_LOAD) {
+       for (i = 0; i < cnt; i++, hp++) {
+               if (__get_user(type, &hp->p_type))
+                       return -EFAULT;
+               if (type != PT_LOAD) {
                        printk("irix_mapelf: One section is not PT_LOAD!\n");
                        return -ENOEXEC;
                }
+       }
 
        filp = fget(fd);
        if (!filp)
@@ -910,28 +915,40 @@ unsigned long irix_mapelf(int fd, struct elf_phdr *user_phdrp, int cnt)
        for(i = 0; i < cnt; i++, hp++) {
                int prot;
 
-               prot  = (hp->p_flags & PF_R) ? PROT_READ : 0;
-               prot |= (hp->p_flags & PF_W) ? PROT_WRITE : 0;
-               prot |= (hp->p_flags & PF_X) ? PROT_EXEC : 0;
+               retval = __get_user(vaddr, &hp->p_vaddr);
+               retval |= __get_user(filesz, &hp->p_filesz);
+               retval |= __get_user(offset, &hp->p_offset);
+               retval |= __get_user(flags, &hp->p_flags);
+               if (retval)
+                       return retval;
+
+               prot  = (flags & PF_R) ? PROT_READ : 0;
+               prot |= (flags & PF_W) ? PROT_WRITE : 0;
+               prot |= (flags & PF_X) ? PROT_EXEC : 0;
+
                down_write(&current->mm->mmap_sem);
-               retval = do_mmap(filp, (hp->p_vaddr & 0xfffff000),
-                                (hp->p_filesz + (hp->p_vaddr & 0xfff)),
+               retval = do_mmap(filp, (vaddr & 0xfffff000),
+                                (filesz + (vaddr & 0xfff)),
                                 prot, (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
-                                (hp->p_offset & 0xfffff000));
+                                (offset & 0xfffff000));
                up_write(&current->mm->mmap_sem);
 
-               if(retval != (hp->p_vaddr & 0xfffff000)) {
+               if (retval != (vaddr & 0xfffff000)) {
                        printk("irix_mapelf: do_mmap fails with %d!\n", retval);
                        fput(filp);
                        return retval;
                }
        }
 
-#ifdef DEBUG_ELF
-       printk("irix_mapelf: Success, returning %08lx\n", user_phdrp->p_vaddr);
-#endif
+       pr_debug("irix_mapelf: Success, returning %08lx\n",
+                (unsigned long) user_phdrp->p_vaddr);
+
        fput(filp);
-       return user_phdrp->p_vaddr;
+
+       if (__get_user(vaddr, &user_phdrp->p_vaddr))
+               return -EFAULT;
+
+       return vaddr;
 }
 
 /*
@@ -944,9 +961,9 @@ unsigned long irix_mapelf(int fd, struct elf_phdr *user_phdrp, int cnt)
 /* These are the only things you should do on a core-file: use only these
  * functions to write out all the necessary info.
  */
-static int dump_write(struct file *file, const void *addr, int nr)
+static int dump_write(struct file *file, const void __user *addr, int nr)
 {
-       return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
+       return file->f_op->write(file, (const char __user *) addr, nr, &file->f_pos) == nr;
 }
 
 static int dump_seek(struct file *file, off_t off)
@@ -978,8 +995,6 @@ static inline int maydump(struct vm_area_struct *vma)
        return 1;
 }
 
-#define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))
-
 /* An ELF note in memory. */
 struct memelfnote
 {
@@ -994,7 +1009,7 @@ static int notesize(struct memelfnote *en)
        int sz;
 
        sz = sizeof(struct elf_note);
-       sz += roundup(strlen(en->name), 4);
+       sz += roundup(strlen(en->name) + 1, 4);
        sz += roundup(en->datasz, 4);
 
        return sz;
@@ -1013,7 +1028,7 @@ static int writenote(struct memelfnote *men, struct file *file)
 {
        struct elf_note en;
 
-       en.n_namesz = strlen(men->name);
+       en.n_namesz = strlen(men->name) + 1;
        en.n_descsz = men->datasz;
        en.n_type = men->type;
 
@@ -1055,9 +1070,9 @@ static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
        struct vm_area_struct *vma;
        struct elfhdr elf;
        off_t offset = 0, dataoff;
-       int limit = current->rlim[RLIMIT_CORE].rlim_cur;
-       int numnote = 4;
-       struct memelfnote notes[4];
+       int limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
+       int numnote = 3;
+       struct memelfnote notes[3];
        struct elf_prstatus prstatus;   /* NT_PRSTATUS */
        elf_fpregset_t fpu;             /* NT_PRFPREG */
        struct elf_prpsinfo psinfo;     /* NT_PRPSINFO */
@@ -1065,7 +1080,7 @@ static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
        /* Count what's needed to dump, up to the limit of coredump size. */
        segs = 0;
        size = 0;
-       for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
+       for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
                if (maydump(vma))
                {
                        int sz = vma->vm_end-vma->vm_start;
@@ -1130,15 +1145,29 @@ static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
        psinfo.pr_pid = prstatus.pr_pid = current->pid;
        psinfo.pr_ppid = prstatus.pr_ppid = current->parent->pid;
        psinfo.pr_pgrp = prstatus.pr_pgrp = process_group(current);
-       psinfo.pr_sid = prstatus.pr_sid = current->session;
-       prstatus.pr_utime.tv_sec = CT_TO_SECS(current->utime);
-       prstatus.pr_utime.tv_usec = CT_TO_USECS(current->utime);
-       prstatus.pr_stime.tv_sec = CT_TO_SECS(current->stime);
-       prstatus.pr_stime.tv_usec = CT_TO_USECS(current->stime);
-       prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->cutime);
-       prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->cutime);
-       prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->cstime);
-       prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->cstime);
+       psinfo.pr_sid = prstatus.pr_sid = process_session(current);
+       if (current->pid == current->tgid) {
+               /*
+                * This is the record for the group leader.  Add in the
+                * cumulative times of previous dead threads.  This total
+                * won't include the time of each live thread whose state
+                * is included in the core dump.  The final total reported
+                * to our parent process when it calls wait4 will include
+                * those sums as well as the little bit more time it takes
+                * this and each other thread to finish dying after the
+                * core dump synchronization phase.
+                */
+               jiffies_to_timeval(current->utime + current->signal->utime,
+                                  &prstatus.pr_utime);
+               jiffies_to_timeval(current->stime + current->signal->stime,
+                                  &prstatus.pr_stime);
+       } else {
+               jiffies_to_timeval(current->utime, &prstatus.pr_utime);
+               jiffies_to_timeval(current->stime, &prstatus.pr_stime);
+       }
+       jiffies_to_timeval(current->signal->cutime, &prstatus.pr_cutime);
+       jiffies_to_timeval(current->signal->cstime, &prstatus.pr_cstime);
+
        if (sizeof(elf_gregset_t) != sizeof(struct pt_regs)) {
                printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
                       "(%d)\n", sizeof(elf_gregset_t), sizeof(struct pt_regs));
@@ -1165,9 +1194,9 @@ static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
 
                len = current->mm->arg_end - current->mm->arg_start;
                len = len >= ELF_PRARGSZ ? ELF_PRARGSZ : len;
-               copy_from_user(&psinfo.pr_psargs,
-                              (const char *)current->mm->arg_start, len);
-               for(i = 0; i < len; i++)
+               (void *) copy_from_user(&psinfo.pr_psargs,
+                              (const char __user *)current->mm->arg_start, len);
+               for (i = 0; i < len; i++)
                        if (psinfo.pr_psargs[i] == 0)
                                psinfo.pr_psargs[i] = ' ';
                psinfo.pr_psargs[len] = 0;
@@ -1176,20 +1205,15 @@ static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
        }
        strlcpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
 
-       notes[2].name = "CORE";
-       notes[2].type = NT_TASKSTRUCT;
-       notes[2].datasz = sizeof(*current);
-       notes[2].data = current;
-
        /* Try to dump the FPU. */
        prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
        if (!prstatus.pr_fpvalid) {
                numnote--;
        } else {
-               notes[3].name = "CORE";
-               notes[3].type = NT_PRFPREG;
-               notes[3].datasz = sizeof(fpu);
-               notes[3].data = &fpu;
+               notes[2].name = "CORE";
+               notes[2].type = NT_PRFPREG;
+               notes[2].datasz = sizeof(fpu);
+               notes[2].data = &fpu;
        }
 
        /* Write notes phdr entry. */
@@ -1234,8 +1258,10 @@ static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
                phdr.p_memsz = sz;
                offset += phdr.p_filesz;
                phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
-               if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
-               if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
+               if (vma->vm_flags & VM_WRITE)
+                       phdr.p_flags |= PF_W;
+               if (vma->vm_flags & VM_EXEC)
+                       phdr.p_flags |= PF_X;
                phdr.p_align = PAGE_SIZE;
 
                DUMP_WRITE(&phdr, sizeof(phdr));
@@ -1261,7 +1287,7 @@ static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
 #ifdef DEBUG
                printk("elf_core_dump: writing %08lx %lx\n", addr, len);
 #endif
-               DUMP_WRITE((void *)addr, len);
+               DUMP_WRITE((void __user *)addr, len);
        }
 
        if ((off_t) file->f_pos != offset) {
@@ -1277,12 +1303,28 @@ end_coredump:
 
 static int __init init_irix_binfmt(void)
 {
+       extern int init_inventory(void);
+       extern asmlinkage unsigned long sys_call_table;
+       extern asmlinkage unsigned long sys_call_table_irix5;
+
+       init_inventory();
+
+       /*
+        * Copy the IRIX5 syscall table (8000 bytes) into the main syscall
+        * table. The IRIX5 calls are located by an offset of 8000 bytes
+        * from the beginning of the main table.
+        */
+       memcpy((void *) ((unsigned long) &sys_call_table + 8000),
+               &sys_call_table_irix5, 8000);
+
        return register_binfmt(&irix_format);
 }
 
 static void __exit exit_irix_binfmt(void)
 {
-       /* Remove the IRIX ELF loaders. */
+       /*
+        * Remove the Irix ELF loader.
+        */
        unregister_binfmt(&irix_format);
 }