Sparseify MIPS.
[pandora-kernel.git] / arch / mips / kernel / irixelf.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * irixelf.c: Code to load IRIX ELF executables conforming to the MIPS ABI.
7  *            Based off of work by Eric Youngdale.
8  *
9  * Copyright (C) 1993 - 1994 Eric Youngdale <ericy@cais.com>
10  * Copyright (C) 1996 - 2004 David S. Miller <dm@engr.sgi.com>
11  * Copyright (C) 2004 - 2005 Steven J. Hill <sjhill@realitydiluted.com>
12  */
13 #include <linux/module.h>
14 #include <linux/fs.h>
15 #include <linux/stat.h>
16 #include <linux/sched.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/init.h>
22 #include <linux/signal.h>
23 #include <linux/binfmts.h>
24 #include <linux/string.h>
25 #include <linux/file.h>
26 #include <linux/fcntl.h>
27 #include <linux/ptrace.h>
28 #include <linux/slab.h>
29 #include <linux/shm.h>
30 #include <linux/personality.h>
31 #include <linux/elfcore.h>
32 #include <linux/smp_lock.h>
33
34 #include <asm/mipsregs.h>
35 #include <asm/namei.h>
36 #include <asm/prctl.h>
37 #include <asm/uaccess.h>
38
39 #define DLINFO_ITEMS 12
40
41 #include <linux/elf.h>
42
43 #undef DEBUG
44
45 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs);
46 static int load_irix_library(struct file *);
47 static int irix_core_dump(long signr, struct pt_regs * regs,
48                           struct file *file);
49
50 static struct linux_binfmt irix_format = {
51         NULL, THIS_MODULE, load_irix_binary, load_irix_library,
52         irix_core_dump, PAGE_SIZE
53 };
54
55 #ifndef elf_addr_t
56 #define elf_addr_t unsigned long
57 #endif
58
59 #ifdef DEBUG
60 /* Debugging routines. */
61 static char *get_elf_p_type(Elf32_Word p_type)
62 {
63         int i = (int) p_type;
64
65         switch(i) {
66         case PT_NULL: return("PT_NULL"); break;
67         case PT_LOAD: return("PT_LOAD"); break;
68         case PT_DYNAMIC: return("PT_DYNAMIC"); break;
69         case PT_INTERP: return("PT_INTERP"); break;
70         case PT_NOTE: return("PT_NOTE"); break;
71         case PT_SHLIB: return("PT_SHLIB"); break;
72         case PT_PHDR: return("PT_PHDR"); break;
73         case PT_LOPROC: return("PT_LOPROC/REGINFO"); break;
74         case PT_HIPROC: return("PT_HIPROC"); break;
75         default: return("PT_BOGUS"); break;
76         }
77 }
78
79 static void print_elfhdr(struct elfhdr *ehp)
80 {
81         int i;
82
83         printk("ELFHDR: e_ident<");
84         for(i = 0; i < (EI_NIDENT - 1); i++) printk("%x ", ehp->e_ident[i]);
85         printk("%x>\n", ehp->e_ident[i]);
86         printk("        e_type[%04x] e_machine[%04x] e_version[%08lx]\n",
87                (unsigned short) ehp->e_type, (unsigned short) ehp->e_machine,
88                (unsigned long) ehp->e_version);
89         printk("        e_entry[%08lx] e_phoff[%08lx] e_shoff[%08lx] "
90                "e_flags[%08lx]\n",
91                (unsigned long) ehp->e_entry, (unsigned long) ehp->e_phoff,
92                (unsigned long) ehp->e_shoff, (unsigned long) ehp->e_flags);
93         printk("        e_ehsize[%04x] e_phentsize[%04x] e_phnum[%04x]\n",
94                (unsigned short) ehp->e_ehsize, (unsigned short) ehp->e_phentsize,
95                (unsigned short) ehp->e_phnum);
96         printk("        e_shentsize[%04x] e_shnum[%04x] e_shstrndx[%04x]\n",
97                (unsigned short) ehp->e_shentsize, (unsigned short) ehp->e_shnum,
98                (unsigned short) ehp->e_shstrndx);
99 }
100
101 static void print_phdr(int i, struct elf_phdr *ep)
102 {
103         printk("PHDR[%d]: p_type[%s] p_offset[%08lx] p_vaddr[%08lx] "
104                "p_paddr[%08lx]\n", i, get_elf_p_type(ep->p_type),
105                (unsigned long) ep->p_offset, (unsigned long) ep->p_vaddr,
106                (unsigned long) ep->p_paddr);
107         printk("         p_filesz[%08lx] p_memsz[%08lx] p_flags[%08lx] "
108                "p_align[%08lx]\n", (unsigned long) ep->p_filesz,
109                (unsigned long) ep->p_memsz, (unsigned long) ep->p_flags,
110                (unsigned long) ep->p_align);
111 }
112
113 static void dump_phdrs(struct elf_phdr *ep, int pnum)
114 {
115         int i;
116
117         for(i = 0; i < pnum; i++, ep++) {
118                 if((ep->p_type == PT_LOAD) ||
119                    (ep->p_type == PT_INTERP) ||
120                    (ep->p_type == PT_PHDR))
121                         print_phdr(i, ep);
122         }
123 }
124 #endif /* DEBUG */
125
126 static void set_brk(unsigned long start, unsigned long end)
127 {
128         start = PAGE_ALIGN(start);
129         end = PAGE_ALIGN(end);
130         if (end <= start)
131                 return;
132         down_write(&current->mm->mmap_sem);
133         do_brk(start, end - start);
134         up_write(&current->mm->mmap_sem);
135 }
136
137
138 /* We need to explicitly zero any fractional pages
139  * after the data section (i.e. bss).  This would
140  * contain the junk from the file that should not
141  * be in memory.
142  */
143 static void padzero(unsigned long elf_bss)
144 {
145         unsigned long nbyte;
146
147         nbyte = elf_bss & (PAGE_SIZE-1);
148         if (nbyte) {
149                 nbyte = PAGE_SIZE - nbyte;
150                 clear_user((void __user *) elf_bss, nbyte);
151         }
152 }
153
154 static unsigned long * create_irix_tables(char * p, int argc, int envc,
155         struct elfhdr * exec, unsigned int load_addr,
156         unsigned int interp_load_addr, struct pt_regs *regs,
157         struct elf_phdr *ephdr)
158 {
159         elf_addr_t *argv;
160         elf_addr_t *envp;
161         elf_addr_t *sp, *csp;
162
163 #ifdef DEBUG
164         printk("create_irix_tables: p[%p] argc[%d] envc[%d] "
165                "load_addr[%08x] interp_load_addr[%08x]\n",
166                p, argc, envc, load_addr, interp_load_addr);
167 #endif
168         sp = (elf_addr_t *) (~15UL & (unsigned long) p);
169         csp = sp;
170         csp -= exec ? DLINFO_ITEMS*2 : 2;
171         csp -= envc+1;
172         csp -= argc+1;
173         csp -= 1;               /* argc itself */
174         if ((unsigned long)csp & 15UL) {
175                 sp -= (16UL - ((unsigned long)csp & 15UL)) / sizeof(*sp);
176         }
177
178         /*
179          * Put the ELF interpreter info on the stack
180          */
181 #define NEW_AUX_ENT(nr, id, val) \
182           __put_user ((id), sp+(nr*2)); \
183           __put_user ((val), sp+(nr*2+1)); \
184
185         sp -= 2;
186         NEW_AUX_ENT(0, AT_NULL, 0);
187
188         if(exec) {
189                 sp -= 11*2;
190
191                 NEW_AUX_ENT (0, AT_PHDR, load_addr + exec->e_phoff);
192                 NEW_AUX_ENT (1, AT_PHENT, sizeof (struct elf_phdr));
193                 NEW_AUX_ENT (2, AT_PHNUM, exec->e_phnum);
194                 NEW_AUX_ENT (3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
195                 NEW_AUX_ENT (4, AT_BASE, interp_load_addr);
196                 NEW_AUX_ENT (5, AT_FLAGS, 0);
197                 NEW_AUX_ENT (6, AT_ENTRY, (elf_addr_t) exec->e_entry);
198                 NEW_AUX_ENT (7, AT_UID, (elf_addr_t) current->uid);
199                 NEW_AUX_ENT (8, AT_EUID, (elf_addr_t) current->euid);
200                 NEW_AUX_ENT (9, AT_GID, (elf_addr_t) current->gid);
201                 NEW_AUX_ENT (10, AT_EGID, (elf_addr_t) current->egid);
202         }
203 #undef NEW_AUX_ENT
204
205         sp -= envc+1;
206         envp = sp;
207         sp -= argc+1;
208         argv = sp;
209
210         __put_user((elf_addr_t)argc,--sp);
211         current->mm->arg_start = (unsigned long) p;
212         while (argc-->0) {
213                 __put_user((unsigned long)p,argv++);
214                 p += strlen_user(p);
215         }
216         __put_user((unsigned long) NULL, argv);
217         current->mm->arg_end = current->mm->env_start = (unsigned long) p;
218         while (envc-->0) {
219                 __put_user((unsigned long)p,envp++);
220                 p += strlen_user(p);
221         }
222         __put_user((unsigned long) NULL, envp);
223         current->mm->env_end = (unsigned long) p;
224         return sp;
225 }
226
227
228 /* This is much more generalized than the library routine read function,
229  * so we keep this separate.  Technically the library read function
230  * is only provided so that we can read a.out libraries that have
231  * an ELF header.
232  */
233 static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
234                                      struct file * interpreter,
235                                      unsigned int *interp_load_addr)
236 {
237         struct elf_phdr *elf_phdata  =  NULL;
238         struct elf_phdr *eppnt;
239         unsigned int len;
240         unsigned int load_addr;
241         int elf_bss;
242         int retval;
243         unsigned int last_bss;
244         int error;
245         int i;
246         unsigned int k;
247
248         elf_bss = 0;
249         last_bss = 0;
250         error = load_addr = 0;
251
252 #ifdef DEBUG
253         print_elfhdr(interp_elf_ex);
254 #endif
255
256         /* First of all, some simple consistency checks */
257         if ((interp_elf_ex->e_type != ET_EXEC &&
258              interp_elf_ex->e_type != ET_DYN) ||
259              !interpreter->f_op->mmap) {
260                 printk("IRIX interp has bad e_type %d\n", interp_elf_ex->e_type);
261                 return 0xffffffff;
262         }
263
264         /* Now read in all of the header information */
265         if(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > PAGE_SIZE) {
266             printk("IRIX interp header bigger than a page (%d)\n",
267                    (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum));
268             return 0xffffffff;
269         }
270
271         elf_phdata = kmalloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum,
272                              GFP_KERNEL);
273
274         if(!elf_phdata) {
275           printk("Cannot kmalloc phdata for IRIX interp.\n");
276           return 0xffffffff;
277         }
278
279         /* If the size of this structure has changed, then punt, since
280          * we will be doing the wrong thing.
281          */
282         if(interp_elf_ex->e_phentsize != 32) {
283                 printk("IRIX interp e_phentsize == %d != 32 ",
284                        interp_elf_ex->e_phentsize);
285                 kfree(elf_phdata);
286                 return 0xffffffff;
287         }
288
289         retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
290                            (char *) elf_phdata,
291                            sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
292
293 #ifdef DEBUG
294         dump_phdrs(elf_phdata, interp_elf_ex->e_phnum);
295 #endif
296
297         eppnt = elf_phdata;
298         for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
299           if(eppnt->p_type == PT_LOAD) {
300             int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
301             int elf_prot = 0;
302             unsigned long vaddr = 0;
303             if (eppnt->p_flags & PF_R) elf_prot =  PROT_READ;
304             if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
305             if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
306             elf_type |= MAP_FIXED;
307             vaddr = eppnt->p_vaddr;
308
309             pr_debug("INTERP do_mmap(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
310                    interpreter, vaddr,
311                    (unsigned long) (eppnt->p_filesz + (eppnt->p_vaddr & 0xfff)),
312                    (unsigned long) elf_prot, (unsigned long) elf_type,
313                    (unsigned long) (eppnt->p_offset & 0xfffff000));
314             down_write(&current->mm->mmap_sem);
315             error = do_mmap(interpreter, vaddr,
316                             eppnt->p_filesz + (eppnt->p_vaddr & 0xfff),
317                             elf_prot, elf_type,
318                             eppnt->p_offset & 0xfffff000);
319             up_write(&current->mm->mmap_sem);
320
321             if(error < 0 && error > -1024) {
322                     printk("Aieee IRIX interp mmap error=%d\n", error);
323                     break;  /* Real error */
324             }
325             pr_debug("error=%08lx ", (unsigned long) error);
326             if(!load_addr && interp_elf_ex->e_type == ET_DYN) {
327               load_addr = error;
328               pr_debug("load_addr = error ");
329             }
330
331             /* Find the end of the file  mapping for this phdr, and keep
332              * track of the largest address we see for this.
333              */
334             k = eppnt->p_vaddr + eppnt->p_filesz;
335             if(k > elf_bss) elf_bss = k;
336
337             /* Do the same thing for the memory mapping - between
338              * elf_bss and last_bss is the bss section.
339              */
340             k = eppnt->p_memsz + eppnt->p_vaddr;
341             if(k > last_bss) last_bss = k;
342             pr_debug("\n");
343           }
344         }
345
346         /* Now use mmap to map the library into memory. */
347         if(error < 0 && error > -1024) {
348                 pr_debug("got error %d\n", error);
349                 kfree(elf_phdata);
350                 return 0xffffffff;
351         }
352
353         /* Now fill out the bss section.  First pad the last page up
354          * to the page boundary, and then perform a mmap to make sure
355          * that there are zero-mapped pages up to and including the
356          * last bss page.
357          */
358         pr_debug("padzero(%08lx) ", (unsigned long) (elf_bss));
359         padzero(elf_bss);
360         len = (elf_bss + 0xfff) & 0xfffff000; /* What we have mapped so far */
361
362         pr_debug("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss,
363                  (unsigned long) len);
364
365         /* Map the last of the bss segment */
366         if (last_bss > len) {
367                 down_write(&current->mm->mmap_sem);
368                 do_brk(len, (last_bss - len));
369                 up_write(&current->mm->mmap_sem);
370         }
371         kfree(elf_phdata);
372
373         *interp_load_addr = load_addr;
374         return ((unsigned int) interp_elf_ex->e_entry);
375 }
376
377 /* Check sanity of IRIX elf executable header. */
378 static int verify_binary(struct elfhdr *ehp, struct linux_binprm *bprm)
379 {
380         if (memcmp(ehp->e_ident, ELFMAG, SELFMAG) != 0)
381                 return -ENOEXEC;
382
383         /* First of all, some simple consistency checks */
384         if((ehp->e_type != ET_EXEC && ehp->e_type != ET_DYN) ||
385             !bprm->file->f_op->mmap) {
386                 return -ENOEXEC;
387         }
388
389         /* XXX Don't support N32 or 64bit binaries yet because they can
390          * XXX and do execute 64 bit instructions and expect all registers
391          * XXX to be 64 bit as well.  We need to make the kernel save
392          * XXX all registers as 64bits on cpu's capable of this at
393          * XXX exception time plus frob the XTLB exception vector.
394          */
395         if((ehp->e_flags & EF_MIPS_ABI2))
396                 return -ENOEXEC;
397
398         return 0;
399 }
400
401 /*
402  * This is where the detailed check is performed. Irix binaries
403  * use interpreters with 'libc.so' in the name, so this function
404  * can differentiate between Linux and Irix binaries.
405  */
406 static inline int look_for_irix_interpreter(char **name,
407                                             struct file **interpreter,
408                                             struct elfhdr *interp_elf_ex,
409                                             struct elf_phdr *epp,
410                                             struct linux_binprm *bprm, int pnum)
411 {
412         int i;
413         int retval = -EINVAL;
414         struct file *file = NULL;
415
416         *name = NULL;
417         for(i = 0; i < pnum; i++, epp++) {
418                 if (epp->p_type != PT_INTERP)
419                         continue;
420
421                 /* It is illegal to have two interpreters for one executable. */
422                 if (*name != NULL)
423                         goto out;
424
425                 *name = (char *) kmalloc((epp->p_filesz +
426                                           strlen(IRIX_EMUL)),
427                                          GFP_KERNEL);
428                 if (!*name)
429                         return -ENOMEM;
430
431                 strcpy(*name, IRIX_EMUL);
432                 retval = kernel_read(bprm->file, epp->p_offset, (*name + 16),
433                                      epp->p_filesz);
434                 if (retval < 0)
435                         goto out;
436
437                 file = open_exec(*name);
438                 if (IS_ERR(file)) {
439                         retval = PTR_ERR(file);
440                         goto out;
441                 }
442                 retval = kernel_read(file, 0, bprm->buf, 128);
443                 if (retval < 0)
444                         goto dput_and_out;
445
446                 *interp_elf_ex = *(struct elfhdr *) bprm->buf;
447         }
448         *interpreter = file;
449         return 0;
450
451 dput_and_out:
452         fput(file);
453 out:
454         kfree(*name);
455         return retval;
456 }
457
458 static inline int verify_irix_interpreter(struct elfhdr *ihp)
459 {
460         if (memcmp(ihp->e_ident, ELFMAG, SELFMAG) != 0)
461                 return -ELIBBAD;
462         return 0;
463 }
464
465 #define EXEC_MAP_FLAGS (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE)
466
467 static inline void map_executable(struct file *fp, struct elf_phdr *epp, int pnum,
468                                   unsigned int *estack, unsigned int *laddr,
469                                   unsigned int *scode, unsigned int *ebss,
470                                   unsigned int *ecode, unsigned int *edata,
471                                   unsigned int *ebrk)
472 {
473         unsigned int tmp;
474         int i, prot;
475
476         for(i = 0; i < pnum; i++, epp++) {
477                 if(epp->p_type != PT_LOAD)
478                         continue;
479
480                 /* Map it. */
481                 prot  = (epp->p_flags & PF_R) ? PROT_READ : 0;
482                 prot |= (epp->p_flags & PF_W) ? PROT_WRITE : 0;
483                 prot |= (epp->p_flags & PF_X) ? PROT_EXEC : 0;
484                 down_write(&current->mm->mmap_sem);
485                 (void) do_mmap(fp, (epp->p_vaddr & 0xfffff000),
486                                (epp->p_filesz + (epp->p_vaddr & 0xfff)),
487                                prot, EXEC_MAP_FLAGS,
488                                (epp->p_offset & 0xfffff000));
489                 up_write(&current->mm->mmap_sem);
490
491                 /* Fixup location tracking vars. */
492                 if((epp->p_vaddr & 0xfffff000) < *estack)
493                         *estack = (epp->p_vaddr & 0xfffff000);
494                 if(!*laddr)
495                         *laddr = epp->p_vaddr - epp->p_offset;
496                 if(epp->p_vaddr < *scode)
497                         *scode = epp->p_vaddr;
498
499                 tmp = epp->p_vaddr + epp->p_filesz;
500                 if(tmp > *ebss)
501                         *ebss = tmp;
502                 if((epp->p_flags & PF_X) && *ecode < tmp)
503                         *ecode = tmp;
504                 if(*edata < tmp)
505                         *edata = tmp;
506
507                 tmp = epp->p_vaddr + epp->p_memsz;
508                 if(tmp > *ebrk)
509                         *ebrk = tmp;
510         }
511
512 }
513
514 static inline int map_interpreter(struct elf_phdr *epp, struct elfhdr *ihp,
515                                   struct file *interp, unsigned int *iladdr,
516                                   int pnum, mm_segment_t old_fs,
517                                   unsigned int *eentry)
518 {
519         int i;
520
521         *eentry = 0xffffffff;
522         for(i = 0; i < pnum; i++, epp++) {
523                 if(epp->p_type != PT_INTERP)
524                         continue;
525
526                 /* We should have fielded this error elsewhere... */
527                 if(*eentry != 0xffffffff)
528                         return -1;
529
530                 set_fs(old_fs);
531                 *eentry = load_irix_interp(ihp, interp, iladdr);
532                 old_fs = get_fs();
533                 set_fs(get_ds());
534
535                 fput(interp);
536
537                 if (*eentry == 0xffffffff)
538                         return -1;
539         }
540         return 0;
541 }
542
543 /*
544  * IRIX maps a page at 0x200000 that holds information about the
545  * process and the system, here we map the page and fill the
546  * structure
547  */
548 static void irix_map_prda_page(void)
549 {
550         unsigned long v;
551         struct prda *pp;
552
553         down_write(&current->mm->mmap_sem);
554         v =  do_brk (PRDA_ADDRESS, PAGE_SIZE);
555         up_write(&current->mm->mmap_sem);
556
557         if (v < 0)
558                 return;
559
560         pp = (struct prda *) v;
561         pp->prda_sys.t_pid  = current->pid;
562         pp->prda_sys.t_prid = read_c0_prid();
563         pp->prda_sys.t_rpid = current->pid;
564
565         /* We leave the rest set to zero */
566 }
567
568
569
570 /* These are the functions used to load ELF style executables and shared
571  * libraries.  There is no binary dependent code anywhere else.
572  */
573 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
574 {
575         struct elfhdr elf_ex, interp_elf_ex;
576         struct file *interpreter;
577         struct elf_phdr *elf_phdata, *elf_ihdr, *elf_ephdr;
578         unsigned int load_addr, elf_bss, elf_brk;
579         unsigned int elf_entry, interp_load_addr = 0;
580         unsigned int start_code, end_code, end_data, elf_stack;
581         int retval, has_interp, has_ephdr, size, i;
582         char *elf_interpreter;
583         mm_segment_t old_fs;
584
585         load_addr = 0;
586         has_interp = has_ephdr = 0;
587         elf_ihdr = elf_ephdr = NULL;
588         elf_ex = *((struct elfhdr *) bprm->buf);
589         retval = -ENOEXEC;
590
591         if (verify_binary(&elf_ex, bprm))
592                 goto out;
593
594         /*
595          * Telling -o32 static binaries from Linux and Irix apart from each
596          * other is difficult. There are 2 differences to be noted for static
597          * binaries from the 2 operating systems:
598          *
599          *    1) Irix binaries have their .text section before their .init
600          *       section. Linux binaries are just the opposite.
601          *
602          *    2) Irix binaries usually have <= 12 sections and Linux
603          *       binaries have > 20.
604          *
605          * We will use Method #2 since Method #1 would require us to read in
606          * the section headers which is way too much overhead. This appears
607          * to work for everything we have ran into so far. If anyone has a
608          * better method to tell the binaries apart, I'm listening.
609          */
610         if (elf_ex.e_shnum > 20)
611                 goto out;
612
613 #ifdef DEBUG
614         print_elfhdr(&elf_ex);
615 #endif
616
617         /* Now read in all of the header information */
618         size = elf_ex.e_phentsize * elf_ex.e_phnum;
619         if (size > 65536)
620                 goto out;
621         elf_phdata = kmalloc(size, GFP_KERNEL);
622         if (elf_phdata == NULL) {
623                 retval = -ENOMEM;
624                 goto out;
625         }
626
627         retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *)elf_phdata, size);
628         if (retval < 0)
629                 goto out_free_ph;
630
631 #ifdef DEBUG
632         dump_phdrs(elf_phdata, elf_ex.e_phnum);
633 #endif
634
635         /* Set some things for later. */
636         for(i = 0; i < elf_ex.e_phnum; i++) {
637                 switch(elf_phdata[i].p_type) {
638                 case PT_INTERP:
639                         has_interp = 1;
640                         elf_ihdr = &elf_phdata[i];
641                         break;
642                 case PT_PHDR:
643                         has_ephdr = 1;
644                         elf_ephdr = &elf_phdata[i];
645                         break;
646                 };
647         }
648
649         pr_debug("\n");
650
651         elf_bss = 0;
652         elf_brk = 0;
653
654         elf_stack = 0xffffffff;
655         elf_interpreter = NULL;
656         start_code = 0xffffffff;
657         end_code = 0;
658         end_data = 0;
659
660         /*
661          * If we get a return value, we change the value to be ENOEXEC
662          * so that we can exit gracefully and the main binary format
663          * search loop in 'fs/exec.c' will move onto the next handler
664          * which should be the normal ELF binary handler.
665          */
666         retval = look_for_irix_interpreter(&elf_interpreter, &interpreter,
667                                            &interp_elf_ex, elf_phdata, bprm,
668                                            elf_ex.e_phnum);
669         if (retval) {
670                 retval = -ENOEXEC;
671                 goto out_free_file;
672         }
673
674         if (elf_interpreter) {
675                 retval = verify_irix_interpreter(&interp_elf_ex);
676                 if(retval)
677                         goto out_free_interp;
678         }
679
680         /* OK, we are done with that, now set up the arg stuff,
681          * and then start this sucker up.
682          */
683         retval = -E2BIG;
684         if (!bprm->sh_bang && !bprm->p)
685                 goto out_free_interp;
686
687         /* Flush all traces of the currently running executable */
688         retval = flush_old_exec(bprm);
689         if (retval)
690                 goto out_free_dentry;
691
692         /* OK, This is the point of no return */
693         current->mm->end_data = 0;
694         current->mm->end_code = 0;
695         current->mm->mmap = NULL;
696         current->flags &= ~PF_FORKNOEXEC;
697         elf_entry = (unsigned int) elf_ex.e_entry;
698
699         /* Do this so that we can load the interpreter, if need be.  We will
700          * change some of these later.
701          */
702         set_mm_counter(current->mm, rss, 0);
703         setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
704         current->mm->start_stack = bprm->p;
705
706         /* At this point, we assume that the image should be loaded at
707          * fixed address, not at a variable address.
708          */
709         old_fs = get_fs();
710         set_fs(get_ds());
711
712         map_executable(bprm->file, elf_phdata, elf_ex.e_phnum, &elf_stack,
713                        &load_addr, &start_code, &elf_bss, &end_code,
714                        &end_data, &elf_brk);
715
716         if(elf_interpreter) {
717                 retval = map_interpreter(elf_phdata, &interp_elf_ex,
718                                          interpreter, &interp_load_addr,
719                                          elf_ex.e_phnum, old_fs, &elf_entry);
720                 kfree(elf_interpreter);
721                 if(retval) {
722                         set_fs(old_fs);
723                         printk("Unable to load IRIX ELF interpreter\n");
724                         send_sig(SIGSEGV, current, 0);
725                         retval = 0;
726                         goto out_free_file;
727                 }
728         }
729
730         set_fs(old_fs);
731
732         kfree(elf_phdata);
733         set_personality(PER_IRIX32);
734         set_binfmt(&irix_format);
735         compute_creds(bprm);
736         current->flags &= ~PF_FORKNOEXEC;
737         bprm->p = (unsigned long)
738           create_irix_tables((char *)bprm->p, bprm->argc, bprm->envc,
739                         (elf_interpreter ? &elf_ex : NULL),
740                         load_addr, interp_load_addr, regs, elf_ephdr);
741         current->mm->start_brk = current->mm->brk = elf_brk;
742         current->mm->end_code = end_code;
743         current->mm->start_code = start_code;
744         current->mm->end_data = end_data;
745         current->mm->start_stack = bprm->p;
746
747         /* Calling set_brk effectively mmaps the pages that we need for the
748          * bss and break sections.
749          */
750         set_brk(elf_bss, elf_brk);
751
752         /*
753          * IRIX maps a page at 0x200000 which holds some system
754          * information.  Programs depend on this.
755          */
756         irix_map_prda_page();
757
758         padzero(elf_bss);
759
760         pr_debug("(start_brk) %lx\n" , (long) current->mm->start_brk);
761         pr_debug("(end_code) %lx\n" , (long) current->mm->end_code);
762         pr_debug("(start_code) %lx\n" , (long) current->mm->start_code);
763         pr_debug("(end_data) %lx\n" , (long) current->mm->end_data);
764         pr_debug("(start_stack) %lx\n" , (long) current->mm->start_stack);
765         pr_debug("(brk) %lx\n" , (long) current->mm->brk);
766
767 #if 0 /* XXX No fucking way dude... */
768         /* Why this, you ask???  Well SVr4 maps page 0 as read-only,
769          * and some applications "depend" upon this behavior.
770          * Since we do not have the power to recompile these, we
771          * emulate the SVr4 behavior.  Sigh.
772          */
773         down_write(&current->mm->mmap_sem);
774         (void) do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
775                        MAP_FIXED | MAP_PRIVATE, 0);
776         up_write(&current->mm->mmap_sem);
777 #endif
778
779         start_thread(regs, elf_entry, bprm->p);
780         if (current->ptrace & PT_PTRACED)
781                 send_sig(SIGTRAP, current, 0);
782         return 0;
783 out:
784         return retval;
785
786 out_free_dentry:
787         allow_write_access(interpreter);
788         fput(interpreter);
789 out_free_interp:
790         kfree(elf_interpreter);
791 out_free_file:
792 out_free_ph:
793         kfree (elf_phdata);
794         goto out;
795 }
796
797 /* This is really simpleminded and specialized - we are loading an
798  * a.out library that is given an ELF header.
799  */
800 static int load_irix_library(struct file *file)
801 {
802         struct elfhdr elf_ex;
803         struct elf_phdr *elf_phdata  =  NULL;
804         unsigned int len = 0;
805         int elf_bss = 0;
806         int retval;
807         unsigned int bss;
808         int error;
809         int i,j, k;
810
811         error = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
812         if (error != sizeof(elf_ex))
813                 return -ENOEXEC;
814
815         if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
816                 return -ENOEXEC;
817
818         /* First of all, some simple consistency checks. */
819         if(elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
820            !file->f_op->mmap)
821                 return -ENOEXEC;
822
823         /* Now read in all of the header information. */
824         if(sizeof(struct elf_phdr) * elf_ex.e_phnum > PAGE_SIZE)
825                 return -ENOEXEC;
826
827         elf_phdata = kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
828         if (elf_phdata == NULL)
829                 return -ENOMEM;
830
831         retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata,
832                            sizeof(struct elf_phdr) * elf_ex.e_phnum);
833
834         j = 0;
835         for(i=0; i<elf_ex.e_phnum; i++)
836                 if((elf_phdata + i)->p_type == PT_LOAD) j++;
837
838         if(j != 1)  {
839                 kfree(elf_phdata);
840                 return -ENOEXEC;
841         }
842
843         while(elf_phdata->p_type != PT_LOAD) elf_phdata++;
844
845         /* Now use mmap to map the library into memory. */
846         down_write(&current->mm->mmap_sem);
847         error = do_mmap(file,
848                         elf_phdata->p_vaddr & 0xfffff000,
849                         elf_phdata->p_filesz + (elf_phdata->p_vaddr & 0xfff),
850                         PROT_READ | PROT_WRITE | PROT_EXEC,
851                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
852                         elf_phdata->p_offset & 0xfffff000);
853         up_write(&current->mm->mmap_sem);
854
855         k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
856         if (k > elf_bss) elf_bss = k;
857
858         if (error != (elf_phdata->p_vaddr & 0xfffff000)) {
859                 kfree(elf_phdata);
860                 return error;
861         }
862
863         padzero(elf_bss);
864
865         len = (elf_phdata->p_filesz + elf_phdata->p_vaddr+ 0xfff) & 0xfffff000;
866         bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
867         if (bss > len) {
868           down_write(&current->mm->mmap_sem);
869           do_brk(len, bss-len);
870           up_write(&current->mm->mmap_sem);
871         }
872         kfree(elf_phdata);
873         return 0;
874 }
875
876 /* Called through irix_syssgi() to map an elf image given an FD,
877  * a phdr ptr USER_PHDRP in userspace, and a count CNT telling how many
878  * phdrs there are in the USER_PHDRP array.  We return the vaddr the
879  * first phdr was successfully mapped to.
880  */
881 unsigned long irix_mapelf(int fd, struct elf_phdr __user *user_phdrp, int cnt)
882 {
883         unsigned long type, vaddr, filesz, offset, flags;
884         struct elf_phdr __user *hp;
885         struct file *filp;
886         int i, retval;
887
888         pr_debug("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
889                  fd, user_phdrp, cnt);
890
891         /* First get the verification out of the way. */
892         hp = user_phdrp;
893         if (!access_ok(VERIFY_READ, hp, (sizeof(struct elf_phdr) * cnt))) {
894                 pr_debug("irix_mapelf: bad pointer to ELF PHDR!\n");
895
896                 return -EFAULT;
897         }
898
899 #ifdef DEBUG
900         dump_phdrs(user_phdrp, cnt);
901 #endif
902
903         for (i = 0; i < cnt; i++, hp++) {
904                 if (__get_user(type, &hp->p_type))
905                         return -EFAULT;
906                 if (type != PT_LOAD) {
907                         printk("irix_mapelf: One section is not PT_LOAD!\n");
908                         return -ENOEXEC;
909                 }
910         }
911
912         filp = fget(fd);
913         if (!filp)
914                 return -EACCES;
915         if(!filp->f_op) {
916                 printk("irix_mapelf: Bogon filp!\n");
917                 fput(filp);
918                 return -EACCES;
919         }
920
921         hp = user_phdrp;
922         for(i = 0; i < cnt; i++, hp++) {
923                 int prot;
924
925                 retval = __get_user(vaddr, &hp->p_vaddr);
926                 retval |= __get_user(filesz, &hp->p_filesz);
927                 retval |= __get_user(offset, &hp->p_offset);
928                 retval |= __get_user(flags, &hp->p_flags);
929                 if (retval)
930                         return retval;
931
932                 prot  = (flags & PF_R) ? PROT_READ : 0;
933                 prot |= (flags & PF_W) ? PROT_WRITE : 0;
934                 prot |= (flags & PF_X) ? PROT_EXEC : 0;
935
936                 down_write(&current->mm->mmap_sem);
937                 retval = do_mmap(filp, (vaddr & 0xfffff000),
938                                  (filesz + (vaddr & 0xfff)),
939                                  prot, (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
940                                  (offset & 0xfffff000));
941                 up_write(&current->mm->mmap_sem);
942
943                 if (retval != (vaddr & 0xfffff000)) {
944                         printk("irix_mapelf: do_mmap fails with %d!\n", retval);
945                         fput(filp);
946                         return retval;
947                 }
948         }
949
950         pr_debug("irix_mapelf: Success, returning %08lx\n",
951                  (unsigned long) user_phdrp->p_vaddr);
952
953         fput(filp);
954
955         if (__get_user(vaddr, &user_phdrp->p_vaddr))
956                 return -EFAULT;
957
958         return vaddr;
959 }
960
961 /*
962  * ELF core dumper
963  *
964  * Modelled on fs/exec.c:aout_core_dump()
965  * Jeremy Fitzhardinge <jeremy@sw.oz.au>
966  */
967
968 /* These are the only things you should do on a core-file: use only these
969  * functions to write out all the necessary info.
970  */
971 static int dump_write(struct file *file, const void __user *addr, int nr)
972 {
973         return file->f_op->write(file, (const char __user *) addr, nr, &file->f_pos) == nr;
974 }
975
976 static int dump_seek(struct file *file, off_t off)
977 {
978         if (file->f_op->llseek) {
979                 if (file->f_op->llseek(file, off, 0) != off)
980                         return 0;
981         } else
982                 file->f_pos = off;
983         return 1;
984 }
985
986 /* Decide whether a segment is worth dumping; default is yes to be
987  * sure (missing info is worse than too much; etc).
988  * Personally I'd include everything, and use the coredump limit...
989  *
990  * I think we should skip something. But I am not sure how. H.J.
991  */
992 static inline int maydump(struct vm_area_struct *vma)
993 {
994         if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
995                 return 0;
996 #if 1
997         if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
998                 return 1;
999         if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
1000                 return 0;
1001 #endif
1002         return 1;
1003 }
1004
1005 #define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))
1006
1007 /* An ELF note in memory. */
1008 struct memelfnote
1009 {
1010         const char *name;
1011         int type;
1012         unsigned int datasz;
1013         void *data;
1014 };
1015
1016 static int notesize(struct memelfnote *en)
1017 {
1018         int sz;
1019
1020         sz = sizeof(struct elf_note);
1021         sz += roundup(strlen(en->name), 4);
1022         sz += roundup(en->datasz, 4);
1023
1024         return sz;
1025 }
1026
1027 /* #define DEBUG */
1028
1029 #define DUMP_WRITE(addr, nr)    \
1030         if (!dump_write(file, (addr), (nr))) \
1031                 goto end_coredump;
1032 #define DUMP_SEEK(off)  \
1033         if (!dump_seek(file, (off))) \
1034                 goto end_coredump;
1035
1036 static int writenote(struct memelfnote *men, struct file *file)
1037 {
1038         struct elf_note en;
1039
1040         en.n_namesz = strlen(men->name);
1041         en.n_descsz = men->datasz;
1042         en.n_type = men->type;
1043
1044         DUMP_WRITE(&en, sizeof(en));
1045         DUMP_WRITE(men->name, en.n_namesz);
1046         /* XXX - cast from long long to long to avoid need for libgcc.a */
1047         DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));      /* XXX */
1048         DUMP_WRITE(men->data, men->datasz);
1049         DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));      /* XXX */
1050
1051         return 1;
1052
1053 end_coredump:
1054         return 0;
1055 }
1056 #undef DUMP_WRITE
1057 #undef DUMP_SEEK
1058
1059 #define DUMP_WRITE(addr, nr)    \
1060         if (!dump_write(file, (addr), (nr))) \
1061                 goto end_coredump;
1062 #define DUMP_SEEK(off)  \
1063         if (!dump_seek(file, (off))) \
1064                 goto end_coredump;
1065
1066 /* Actual dumper.
1067  *
1068  * This is a two-pass process; first we find the offsets of the bits,
1069  * and then they are actually written out.  If we run out of core limit
1070  * we just truncate.
1071  */
1072 static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
1073 {
1074         int has_dumped = 0;
1075         mm_segment_t fs;
1076         int segs;
1077         int i;
1078         size_t size;
1079         struct vm_area_struct *vma;
1080         struct elfhdr elf;
1081         off_t offset = 0, dataoff;
1082         int limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1083         int numnote = 4;
1084         struct memelfnote notes[4];
1085         struct elf_prstatus prstatus;   /* NT_PRSTATUS */
1086         elf_fpregset_t fpu;             /* NT_PRFPREG */
1087         struct elf_prpsinfo psinfo;     /* NT_PRPSINFO */
1088
1089         /* Count what's needed to dump, up to the limit of coredump size. */
1090         segs = 0;
1091         size = 0;
1092         for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1093                 if (maydump(vma))
1094                 {
1095                         int sz = vma->vm_end-vma->vm_start;
1096
1097                         if (size+sz >= limit)
1098                                 break;
1099                         else
1100                                 size += sz;
1101                 }
1102
1103                 segs++;
1104         }
1105 #ifdef DEBUG
1106         printk("irix_core_dump: %d segs taking %d bytes\n", segs, size);
1107 #endif
1108
1109         /* Set up header. */
1110         memcpy(elf.e_ident, ELFMAG, SELFMAG);
1111         elf.e_ident[EI_CLASS] = ELFCLASS32;
1112         elf.e_ident[EI_DATA] = ELFDATA2LSB;
1113         elf.e_ident[EI_VERSION] = EV_CURRENT;
1114         elf.e_ident[EI_OSABI] = ELF_OSABI;
1115         memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1116
1117         elf.e_type = ET_CORE;
1118         elf.e_machine = ELF_ARCH;
1119         elf.e_version = EV_CURRENT;
1120         elf.e_entry = 0;
1121         elf.e_phoff = sizeof(elf);
1122         elf.e_shoff = 0;
1123         elf.e_flags = 0;
1124         elf.e_ehsize = sizeof(elf);
1125         elf.e_phentsize = sizeof(struct elf_phdr);
1126         elf.e_phnum = segs+1;           /* Include notes. */
1127         elf.e_shentsize = 0;
1128         elf.e_shnum = 0;
1129         elf.e_shstrndx = 0;
1130
1131         fs = get_fs();
1132         set_fs(KERNEL_DS);
1133
1134         has_dumped = 1;
1135         current->flags |= PF_DUMPCORE;
1136
1137         DUMP_WRITE(&elf, sizeof(elf));
1138         offset += sizeof(elf);                          /* Elf header. */
1139         offset += (segs+1) * sizeof(struct elf_phdr);   /* Program headers. */
1140
1141         /* Set up the notes in similar form to SVR4 core dumps made
1142          * with info from their /proc.
1143          */
1144         memset(&psinfo, 0, sizeof(psinfo));
1145         memset(&prstatus, 0, sizeof(prstatus));
1146
1147         notes[0].name = "CORE";
1148         notes[0].type = NT_PRSTATUS;
1149         notes[0].datasz = sizeof(prstatus);
1150         notes[0].data = &prstatus;
1151         prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1152         prstatus.pr_sigpend = current->pending.signal.sig[0];
1153         prstatus.pr_sighold = current->blocked.sig[0];
1154         psinfo.pr_pid = prstatus.pr_pid = current->pid;
1155         psinfo.pr_ppid = prstatus.pr_ppid = current->parent->pid;
1156         psinfo.pr_pgrp = prstatus.pr_pgrp = process_group(current);
1157         psinfo.pr_sid = prstatus.pr_sid = current->signal->session;
1158         if (current->pid == current->tgid) {
1159                 /*
1160                  * This is the record for the group leader.  Add in the
1161                  * cumulative times of previous dead threads.  This total
1162                  * won't include the time of each live thread whose state
1163                  * is included in the core dump.  The final total reported
1164                  * to our parent process when it calls wait4 will include
1165                  * those sums as well as the little bit more time it takes
1166                  * this and each other thread to finish dying after the
1167                  * core dump synchronization phase.
1168                  */
1169                 jiffies_to_timeval(current->utime + current->signal->utime,
1170                                    &prstatus.pr_utime);
1171                 jiffies_to_timeval(current->stime + current->signal->stime,
1172                                    &prstatus.pr_stime);
1173         } else {
1174                 jiffies_to_timeval(current->utime, &prstatus.pr_utime);
1175                 jiffies_to_timeval(current->stime, &prstatus.pr_stime);
1176         }
1177         jiffies_to_timeval(current->signal->cutime, &prstatus.pr_cutime);
1178         jiffies_to_timeval(current->signal->cstime, &prstatus.pr_cstime);
1179
1180         if (sizeof(elf_gregset_t) != sizeof(struct pt_regs)) {
1181                 printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
1182                        "(%d)\n", sizeof(elf_gregset_t), sizeof(struct pt_regs));
1183         } else {
1184                 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1185         }
1186
1187         notes[1].name = "CORE";
1188         notes[1].type = NT_PRPSINFO;
1189         notes[1].datasz = sizeof(psinfo);
1190         notes[1].data = &psinfo;
1191         i = current->state ? ffz(~current->state) + 1 : 0;
1192         psinfo.pr_state = i;
1193         psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1194         psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1195         psinfo.pr_nice = task_nice(current);
1196         psinfo.pr_flag = current->flags;
1197         psinfo.pr_uid = current->uid;
1198         psinfo.pr_gid = current->gid;
1199         {
1200                 int i, len;
1201
1202                 set_fs(fs);
1203
1204                 len = current->mm->arg_end - current->mm->arg_start;
1205                 len = len >= ELF_PRARGSZ ? ELF_PRARGSZ : len;
1206                 (void *) copy_from_user(&psinfo.pr_psargs,
1207                                (const char __user *)current->mm->arg_start, len);
1208                 for (i = 0; i < len; i++)
1209                         if (psinfo.pr_psargs[i] == 0)
1210                                 psinfo.pr_psargs[i] = ' ';
1211                 psinfo.pr_psargs[len] = 0;
1212
1213                 set_fs(KERNEL_DS);
1214         }
1215         strlcpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1216
1217         notes[2].name = "CORE";
1218         notes[2].type = NT_TASKSTRUCT;
1219         notes[2].datasz = sizeof(*current);
1220         notes[2].data = current;
1221
1222         /* Try to dump the FPU. */
1223         prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1224         if (!prstatus.pr_fpvalid) {
1225                 numnote--;
1226         } else {
1227                 notes[3].name = "CORE";
1228                 notes[3].type = NT_PRFPREG;
1229                 notes[3].datasz = sizeof(fpu);
1230                 notes[3].data = &fpu;
1231         }
1232
1233         /* Write notes phdr entry. */
1234         {
1235                 struct elf_phdr phdr;
1236                 int sz = 0;
1237
1238                 for(i = 0; i < numnote; i++)
1239                         sz += notesize(&notes[i]);
1240
1241                 phdr.p_type = PT_NOTE;
1242                 phdr.p_offset = offset;
1243                 phdr.p_vaddr = 0;
1244                 phdr.p_paddr = 0;
1245                 phdr.p_filesz = sz;
1246                 phdr.p_memsz = 0;
1247                 phdr.p_flags = 0;
1248                 phdr.p_align = 0;
1249
1250                 offset += phdr.p_filesz;
1251                 DUMP_WRITE(&phdr, sizeof(phdr));
1252         }
1253
1254         /* Page-align dumped data. */
1255         dataoff = offset = roundup(offset, PAGE_SIZE);
1256
1257         /* Write program headers for segments dump. */
1258         for(vma = current->mm->mmap, i = 0;
1259                 i < segs && vma != NULL; vma = vma->vm_next) {
1260                 struct elf_phdr phdr;
1261                 size_t sz;
1262
1263                 i++;
1264
1265                 sz = vma->vm_end - vma->vm_start;
1266
1267                 phdr.p_type = PT_LOAD;
1268                 phdr.p_offset = offset;
1269                 phdr.p_vaddr = vma->vm_start;
1270                 phdr.p_paddr = 0;
1271                 phdr.p_filesz = maydump(vma) ? sz : 0;
1272                 phdr.p_memsz = sz;
1273                 offset += phdr.p_filesz;
1274                 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1275                 if (vma->vm_flags & VM_WRITE)
1276                         phdr.p_flags |= PF_W;
1277                 if (vma->vm_flags & VM_EXEC)
1278                         phdr.p_flags |= PF_X;
1279                 phdr.p_align = PAGE_SIZE;
1280
1281                 DUMP_WRITE(&phdr, sizeof(phdr));
1282         }
1283
1284         for(i = 0; i < numnote; i++)
1285                 if (!writenote(&notes[i], file))
1286                         goto end_coredump;
1287
1288         set_fs(fs);
1289
1290         DUMP_SEEK(dataoff);
1291
1292         for(i = 0, vma = current->mm->mmap;
1293             i < segs && vma != NULL;
1294             vma = vma->vm_next) {
1295                 unsigned long addr = vma->vm_start;
1296                 unsigned long len = vma->vm_end - vma->vm_start;
1297
1298                 if (!maydump(vma))
1299                         continue;
1300                 i++;
1301 #ifdef DEBUG
1302                 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1303 #endif
1304                 DUMP_WRITE((void __user *)addr, len);
1305         }
1306
1307         if ((off_t) file->f_pos != offset) {
1308                 /* Sanity check. */
1309                 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1310                        (off_t) file->f_pos, offset);
1311         }
1312
1313 end_coredump:
1314         set_fs(fs);
1315         return has_dumped;
1316 }
1317
1318 static int __init init_irix_binfmt(void)
1319 {
1320         extern int init_inventory(void);
1321         extern asmlinkage unsigned long sys_call_table;
1322         extern asmlinkage unsigned long sys_call_table_irix5;
1323
1324         init_inventory();
1325
1326         /*
1327          * Copy the IRIX5 syscall table (8000 bytes) into the main syscall
1328          * table. The IRIX5 calls are located by an offset of 8000 bytes
1329          * from the beginning of the main table.
1330          */
1331         memcpy((void *) ((unsigned long) &sys_call_table + 8000),
1332                 &sys_call_table_irix5, 8000);
1333
1334         return register_binfmt(&irix_format);
1335 }
1336
1337 static void __exit exit_irix_binfmt(void)
1338 {
1339         /*
1340          * Remove the Irix ELF loader.
1341          */
1342         unregister_binfmt(&irix_format);
1343 }
1344
1345 module_init(init_irix_binfmt)
1346 module_exit(exit_irix_binfmt)