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