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