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