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
6 * irixelf.c: Code to load IRIX ELF executables conforming to the MIPS ABI.
7 * Based off of work by Eric Youngdale.
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>
15 #include <linux/module.h>
17 #include <linux/stat.h>
18 #include <linux/sched.h>
20 #include <linux/mman.h>
21 #include <linux/a.out.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/signal.h>
25 #include <linux/binfmts.h>
26 #include <linux/string.h>
27 #include <linux/file.h>
28 #include <linux/fcntl.h>
29 #include <linux/ptrace.h>
30 #include <linux/slab.h>
31 #include <linux/shm.h>
32 #include <linux/personality.h>
33 #include <linux/elfcore.h>
35 #include <asm/mipsregs.h>
36 #include <asm/namei.h>
37 #include <asm/prctl.h>
38 #include <asm/uaccess.h>
40 #define DLINFO_ITEMS 12
42 #include <linux/elf.h>
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, unsigned long limit);
49 static struct linux_binfmt irix_format = {
50 .module = THIS_MODULE,
51 .load_binary = load_irix_binary,
52 .load_shlib = load_irix_library,
53 .core_dump = irix_core_dump,
54 .min_coredump = PAGE_SIZE,
57 /* Debugging routines. */
58 static char *get_elf_p_type(Elf32_Word p_type)
91 return "PT_LOPROC/REGINFO";
105 static void print_elfhdr(struct elfhdr *ehp)
109 pr_debug("ELFHDR: e_ident<");
110 for (i = 0; i < (EI_NIDENT - 1); i++)
111 pr_debug("%x ", ehp->e_ident[i]);
112 pr_debug("%x>\n", ehp->e_ident[i]);
113 pr_debug(" e_type[%04x] e_machine[%04x] e_version[%08lx]\n",
114 (unsigned short) ehp->e_type, (unsigned short) ehp->e_machine,
115 (unsigned long) ehp->e_version);
116 pr_debug(" e_entry[%08lx] e_phoff[%08lx] e_shoff[%08lx] "
118 (unsigned long) ehp->e_entry, (unsigned long) ehp->e_phoff,
119 (unsigned long) ehp->e_shoff, (unsigned long) ehp->e_flags);
120 pr_debug(" e_ehsize[%04x] e_phentsize[%04x] e_phnum[%04x]\n",
121 (unsigned short) ehp->e_ehsize,
122 (unsigned short) ehp->e_phentsize,
123 (unsigned short) ehp->e_phnum);
124 pr_debug(" e_shentsize[%04x] e_shnum[%04x] e_shstrndx[%04x]\n",
125 (unsigned short) ehp->e_shentsize,
126 (unsigned short) ehp->e_shnum,
127 (unsigned short) ehp->e_shstrndx);
130 static void print_phdr(int i, struct elf_phdr *ep)
132 pr_debug("PHDR[%d]: p_type[%s] p_offset[%08lx] p_vaddr[%08lx] "
133 "p_paddr[%08lx]\n", i, get_elf_p_type(ep->p_type),
134 (unsigned long) ep->p_offset, (unsigned long) ep->p_vaddr,
135 (unsigned long) ep->p_paddr);
136 pr_debug(" p_filesz[%08lx] p_memsz[%08lx] p_flags[%08lx] "
137 "p_align[%08lx]\n", (unsigned long) ep->p_filesz,
138 (unsigned long) ep->p_memsz, (unsigned long) ep->p_flags,
139 (unsigned long) ep->p_align);
142 static void dump_phdrs(struct elf_phdr *ep, int pnum)
146 for (i = 0; i < pnum; i++, ep++) {
147 if ((ep->p_type == PT_LOAD) ||
148 (ep->p_type == PT_INTERP) ||
149 (ep->p_type == PT_PHDR))
154 static void set_brk(unsigned long start, unsigned long end)
156 start = PAGE_ALIGN(start);
157 end = PAGE_ALIGN(end);
160 down_write(¤t->mm->mmap_sem);
161 do_brk(start, end - start);
162 up_write(¤t->mm->mmap_sem);
166 /* We need to explicitly zero any fractional pages
167 * after the data section (i.e. bss). This would
168 * contain the junk from the file that should not
171 static void padzero(unsigned long elf_bss)
175 nbyte = elf_bss & (PAGE_SIZE-1);
177 nbyte = PAGE_SIZE - nbyte;
178 clear_user((void __user *) elf_bss, nbyte);
182 static unsigned long * create_irix_tables(char * p, int argc, int envc,
183 struct elfhdr * exec, unsigned int load_addr,
184 unsigned int interp_load_addr, struct pt_regs *regs,
185 struct elf_phdr *ephdr)
189 elf_addr_t *sp, *csp;
191 pr_debug("create_irix_tables: p[%p] argc[%d] envc[%d] "
192 "load_addr[%08x] interp_load_addr[%08x]\n",
193 p, argc, envc, load_addr, interp_load_addr);
195 sp = (elf_addr_t *) (~15UL & (unsigned long) p);
197 csp -= exec ? DLINFO_ITEMS*2 : 2;
200 csp -= 1; /* argc itself */
201 if ((unsigned long)csp & 15UL) {
202 sp -= (16UL - ((unsigned long)csp & 15UL)) / sizeof(*sp);
206 * Put the ELF interpreter info on the stack
208 #define NEW_AUX_ENT(nr, id, val) \
209 __put_user((id), sp+(nr*2)); \
210 __put_user((val), sp+(nr*2+1)); \
213 NEW_AUX_ENT(0, AT_NULL, 0);
218 NEW_AUX_ENT(0, AT_PHDR, load_addr + exec->e_phoff);
219 NEW_AUX_ENT(1, AT_PHENT, sizeof(struct elf_phdr));
220 NEW_AUX_ENT(2, AT_PHNUM, exec->e_phnum);
221 NEW_AUX_ENT(3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
222 NEW_AUX_ENT(4, AT_BASE, interp_load_addr);
223 NEW_AUX_ENT(5, AT_FLAGS, 0);
224 NEW_AUX_ENT(6, AT_ENTRY, (elf_addr_t) exec->e_entry);
225 NEW_AUX_ENT(7, AT_UID, (elf_addr_t) current->uid);
226 NEW_AUX_ENT(8, AT_EUID, (elf_addr_t) current->euid);
227 NEW_AUX_ENT(9, AT_GID, (elf_addr_t) current->gid);
228 NEW_AUX_ENT(10, AT_EGID, (elf_addr_t) current->egid);
237 __put_user((elf_addr_t)argc, --sp);
238 current->mm->arg_start = (unsigned long) p;
240 __put_user((unsigned long)p, argv++);
243 __put_user((unsigned long) NULL, argv);
244 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
246 __put_user((unsigned long)p, envp++);
249 __put_user((unsigned long) NULL, envp);
250 current->mm->env_end = (unsigned long) p;
255 /* This is much more generalized than the library routine read function,
256 * so we keep this separate. Technically the library read function
257 * is only provided so that we can read a.out libraries that have
260 static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
261 struct file * interpreter,
262 unsigned int *interp_load_addr)
264 struct elf_phdr *elf_phdata = NULL;
265 struct elf_phdr *eppnt;
267 unsigned int load_addr;
270 unsigned int last_bss;
277 error = load_addr = 0;
279 print_elfhdr(interp_elf_ex);
281 /* First of all, some simple consistency checks */
282 if ((interp_elf_ex->e_type != ET_EXEC &&
283 interp_elf_ex->e_type != ET_DYN) ||
284 !interpreter->f_op->mmap) {
285 printk("IRIX interp has bad e_type %d\n", interp_elf_ex->e_type);
289 /* Now read in all of the header information */
290 if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > PAGE_SIZE) {
291 printk("IRIX interp header bigger than a page (%d)\n",
292 (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum));
296 elf_phdata = kmalloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum,
300 printk("Cannot kmalloc phdata for IRIX interp.\n");
304 /* If the size of this structure has changed, then punt, since
305 * we will be doing the wrong thing.
307 if (interp_elf_ex->e_phentsize != 32) {
308 printk("IRIX interp e_phentsize == %d != 32 ",
309 interp_elf_ex->e_phentsize);
314 retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
316 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
318 dump_phdrs(elf_phdata, interp_elf_ex->e_phnum);
321 for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
322 if (eppnt->p_type == PT_LOAD) {
323 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
325 unsigned long vaddr = 0;
326 if (eppnt->p_flags & PF_R)
327 elf_prot = PROT_READ;
328 if (eppnt->p_flags & PF_W)
329 elf_prot |= PROT_WRITE;
330 if (eppnt->p_flags & PF_X)
331 elf_prot |= PROT_EXEC;
332 elf_type |= MAP_FIXED;
333 vaddr = eppnt->p_vaddr;
335 pr_debug("INTERP do_mmap"
336 "(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
339 (eppnt->p_filesz + (eppnt->p_vaddr & 0xfff)),
341 elf_prot, (unsigned long) elf_type,
343 (eppnt->p_offset & 0xfffff000));
345 down_write(¤t->mm->mmap_sem);
346 error = do_mmap(interpreter, vaddr,
347 eppnt->p_filesz + (eppnt->p_vaddr & 0xfff),
349 eppnt->p_offset & 0xfffff000);
350 up_write(¤t->mm->mmap_sem);
352 if (error < 0 && error > -1024) {
353 printk("Aieee IRIX interp mmap error=%d\n",
355 break; /* Real error */
357 pr_debug("error=%08lx ", (unsigned long) error);
358 if (!load_addr && interp_elf_ex->e_type == ET_DYN) {
360 pr_debug("load_addr = error ");
364 * Find the end of the file mapping for this phdr, and
365 * keep track of the largest address we see for this.
367 k = eppnt->p_vaddr + eppnt->p_filesz;
371 /* Do the same thing for the memory mapping - between
372 * elf_bss and last_bss is the bss section.
374 k = eppnt->p_memsz + eppnt->p_vaddr;
381 /* Now use mmap to map the library into memory. */
382 if (error < 0 && error > -1024) {
383 pr_debug("got error %d\n", error);
388 /* Now fill out the bss section. First pad the last page up
389 * to the page boundary, and then perform a mmap to make sure
390 * that there are zero-mapped pages up to and including the
393 pr_debug("padzero(%08lx) ", (unsigned long) (elf_bss));
395 len = (elf_bss + 0xfff) & 0xfffff000; /* What we have mapped so far */
397 pr_debug("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss,
398 (unsigned long) len);
400 /* Map the last of the bss segment */
401 if (last_bss > len) {
402 down_write(¤t->mm->mmap_sem);
403 do_brk(len, (last_bss - len));
404 up_write(¤t->mm->mmap_sem);
408 *interp_load_addr = load_addr;
409 return ((unsigned int) interp_elf_ex->e_entry);
412 /* Check sanity of IRIX elf executable header. */
413 static int verify_binary(struct elfhdr *ehp, struct linux_binprm *bprm)
415 if (memcmp(ehp->e_ident, ELFMAG, SELFMAG) != 0)
418 /* First of all, some simple consistency checks */
419 if ((ehp->e_type != ET_EXEC && ehp->e_type != ET_DYN) ||
420 !bprm->file->f_op->mmap) {
424 /* XXX Don't support N32 or 64bit binaries yet because they can
425 * XXX and do execute 64 bit instructions and expect all registers
426 * XXX to be 64 bit as well. We need to make the kernel save
427 * XXX all registers as 64bits on cpu's capable of this at
428 * XXX exception time plus frob the XTLB exception vector.
430 if ((ehp->e_flags & EF_MIPS_ABI2))
437 * This is where the detailed check is performed. Irix binaries
438 * use interpreters with 'libc.so' in the name, so this function
439 * can differentiate between Linux and Irix binaries.
441 static inline int look_for_irix_interpreter(char **name,
442 struct file **interpreter,
443 struct elfhdr *interp_elf_ex,
444 struct elf_phdr *epp,
445 struct linux_binprm *bprm, int pnum)
448 int retval = -EINVAL;
449 struct file *file = NULL;
452 for (i = 0; i < pnum; i++, epp++) {
453 if (epp->p_type != PT_INTERP)
456 /* It is illegal to have two interpreters for one executable. */
460 *name = kmalloc(epp->p_filesz + strlen(IRIX_EMUL), GFP_KERNEL);
464 strcpy(*name, IRIX_EMUL);
465 retval = kernel_read(bprm->file, epp->p_offset, (*name + 16),
470 file = open_exec(*name);
472 retval = PTR_ERR(file);
475 retval = kernel_read(file, 0, bprm->buf, 128);
479 *interp_elf_ex = *(struct elfhdr *) bprm->buf;
491 static inline int verify_irix_interpreter(struct elfhdr *ihp)
493 if (memcmp(ihp->e_ident, ELFMAG, SELFMAG) != 0)
498 #define EXEC_MAP_FLAGS (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE)
500 static inline void map_executable(struct file *fp, struct elf_phdr *epp, int pnum,
501 unsigned int *estack, unsigned int *laddr,
502 unsigned int *scode, unsigned int *ebss,
503 unsigned int *ecode, unsigned int *edata,
509 for (i = 0; i < pnum; i++, epp++) {
510 if (epp->p_type != PT_LOAD)
514 prot = (epp->p_flags & PF_R) ? PROT_READ : 0;
515 prot |= (epp->p_flags & PF_W) ? PROT_WRITE : 0;
516 prot |= (epp->p_flags & PF_X) ? PROT_EXEC : 0;
517 down_write(¤t->mm->mmap_sem);
518 (void) do_mmap(fp, (epp->p_vaddr & 0xfffff000),
519 (epp->p_filesz + (epp->p_vaddr & 0xfff)),
520 prot, EXEC_MAP_FLAGS,
521 (epp->p_offset & 0xfffff000));
522 up_write(¤t->mm->mmap_sem);
524 /* Fixup location tracking vars. */
525 if ((epp->p_vaddr & 0xfffff000) < *estack)
526 *estack = (epp->p_vaddr & 0xfffff000);
528 *laddr = epp->p_vaddr - epp->p_offset;
529 if (epp->p_vaddr < *scode)
530 *scode = epp->p_vaddr;
532 tmp = epp->p_vaddr + epp->p_filesz;
535 if ((epp->p_flags & PF_X) && *ecode < tmp)
540 tmp = epp->p_vaddr + epp->p_memsz;
547 static inline int map_interpreter(struct elf_phdr *epp, struct elfhdr *ihp,
548 struct file *interp, unsigned int *iladdr,
549 int pnum, mm_segment_t old_fs,
550 unsigned int *eentry)
554 *eentry = 0xffffffff;
555 for (i = 0; i < pnum; i++, epp++) {
556 if (epp->p_type != PT_INTERP)
559 /* We should have fielded this error elsewhere... */
560 if (*eentry != 0xffffffff)
564 *eentry = load_irix_interp(ihp, interp, iladdr);
570 if (*eentry == 0xffffffff)
577 * IRIX maps a page at 0x200000 that holds information about the
578 * process and the system, here we map the page and fill the
581 static void irix_map_prda_page(void)
586 down_write(¤t->mm->mmap_sem);
587 v = do_brk(PRDA_ADDRESS, PAGE_SIZE);
588 up_write(¤t->mm->mmap_sem);
593 pp = (struct prda *) v;
594 pp->prda_sys.t_pid = current->pid;
595 pp->prda_sys.t_prid = read_c0_prid();
596 pp->prda_sys.t_rpid = current->pid;
598 /* We leave the rest set to zero */
603 /* These are the functions used to load ELF style executables and shared
604 * libraries. There is no binary dependent code anywhere else.
606 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
608 struct elfhdr elf_ex, interp_elf_ex;
609 struct file *interpreter;
610 struct elf_phdr *elf_phdata, *elf_ihdr, *elf_ephdr;
611 unsigned int load_addr, elf_bss, elf_brk;
612 unsigned int elf_entry, interp_load_addr = 0;
613 unsigned int start_code, end_code, end_data, elf_stack;
614 int retval, has_interp, has_ephdr, size, i;
615 char *elf_interpreter;
619 has_interp = has_ephdr = 0;
620 elf_ihdr = elf_ephdr = NULL;
621 elf_ex = *((struct elfhdr *) bprm->buf);
624 if (verify_binary(&elf_ex, bprm))
628 * Telling -o32 static binaries from Linux and Irix apart from each
629 * other is difficult. There are 2 differences to be noted for static
630 * binaries from the 2 operating systems:
632 * 1) Irix binaries have their .text section before their .init
633 * section. Linux binaries are just the opposite.
635 * 2) Irix binaries usually have <= 12 sections and Linux
636 * binaries have > 20.
638 * We will use Method #2 since Method #1 would require us to read in
639 * the section headers which is way too much overhead. This appears
640 * to work for everything we have ran into so far. If anyone has a
641 * better method to tell the binaries apart, I'm listening.
643 if (elf_ex.e_shnum > 20)
646 print_elfhdr(&elf_ex);
648 /* Now read in all of the header information */
649 size = elf_ex.e_phentsize * elf_ex.e_phnum;
652 elf_phdata = kmalloc(size, GFP_KERNEL);
653 if (elf_phdata == NULL) {
658 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *)elf_phdata, size);
662 dump_phdrs(elf_phdata, elf_ex.e_phnum);
664 /* Set some things for later. */
665 for (i = 0; i < elf_ex.e_phnum; i++) {
666 switch (elf_phdata[i].p_type) {
669 elf_ihdr = &elf_phdata[i];
673 elf_ephdr = &elf_phdata[i];
683 elf_stack = 0xffffffff;
684 elf_interpreter = NULL;
685 start_code = 0xffffffff;
690 * If we get a return value, we change the value to be ENOEXEC
691 * so that we can exit gracefully and the main binary format
692 * search loop in 'fs/exec.c' will move onto the next handler
693 * which should be the normal ELF binary handler.
695 retval = look_for_irix_interpreter(&elf_interpreter, &interpreter,
696 &interp_elf_ex, elf_phdata, bprm,
703 if (elf_interpreter) {
704 retval = verify_irix_interpreter(&interp_elf_ex);
706 goto out_free_interp;
709 /* OK, we are done with that, now set up the arg stuff,
710 * and then start this sucker up.
713 if (!bprm->sh_bang && !bprm->p)
714 goto out_free_interp;
716 /* Flush all traces of the currently running executable */
717 retval = flush_old_exec(bprm);
719 goto out_free_dentry;
721 /* OK, This is the point of no return */
722 current->mm->end_data = 0;
723 current->mm->end_code = 0;
724 current->mm->mmap = NULL;
725 current->flags &= ~PF_FORKNOEXEC;
726 elf_entry = (unsigned int) elf_ex.e_entry;
728 /* Do this so that we can load the interpreter, if need be. We will
729 * change some of these later.
731 setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
732 current->mm->start_stack = bprm->p;
734 /* At this point, we assume that the image should be loaded at
735 * fixed address, not at a variable address.
740 map_executable(bprm->file, elf_phdata, elf_ex.e_phnum, &elf_stack,
741 &load_addr, &start_code, &elf_bss, &end_code,
742 &end_data, &elf_brk);
744 if (elf_interpreter) {
745 retval = map_interpreter(elf_phdata, &interp_elf_ex,
746 interpreter, &interp_load_addr,
747 elf_ex.e_phnum, old_fs, &elf_entry);
748 kfree(elf_interpreter);
751 printk("Unable to load IRIX ELF interpreter\n");
752 send_sig(SIGSEGV, current, 0);
761 set_personality(PER_IRIX32);
762 set_binfmt(&irix_format);
764 current->flags &= ~PF_FORKNOEXEC;
765 bprm->p = (unsigned long)
766 create_irix_tables((char *)bprm->p, bprm->argc, bprm->envc,
767 (elf_interpreter ? &elf_ex : NULL),
768 load_addr, interp_load_addr, regs, elf_ephdr);
769 current->mm->start_brk = current->mm->brk = elf_brk;
770 current->mm->end_code = end_code;
771 current->mm->start_code = start_code;
772 current->mm->end_data = end_data;
773 current->mm->start_stack = bprm->p;
775 /* Calling set_brk effectively mmaps the pages that we need for the
776 * bss and break sections.
778 set_brk(elf_bss, elf_brk);
781 * IRIX maps a page at 0x200000 which holds some system
782 * information. Programs depend on this.
784 irix_map_prda_page();
788 pr_debug("(start_brk) %lx\n" , (long) current->mm->start_brk);
789 pr_debug("(end_code) %lx\n" , (long) current->mm->end_code);
790 pr_debug("(start_code) %lx\n" , (long) current->mm->start_code);
791 pr_debug("(end_data) %lx\n" , (long) current->mm->end_data);
792 pr_debug("(start_stack) %lx\n" , (long) current->mm->start_stack);
793 pr_debug("(brk) %lx\n" , (long) current->mm->brk);
795 #if 0 /* XXX No fucking way dude... */
796 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
797 * and some applications "depend" upon this behavior.
798 * Since we do not have the power to recompile these, we
799 * emulate the SVr4 behavior. Sigh.
801 down_write(¤t->mm->mmap_sem);
802 (void) do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
803 MAP_FIXED | MAP_PRIVATE, 0);
804 up_write(¤t->mm->mmap_sem);
807 start_thread(regs, elf_entry, bprm->p);
808 if (current->ptrace & PT_PTRACED)
809 send_sig(SIGTRAP, current, 0);
815 allow_write_access(interpreter);
818 kfree(elf_interpreter);
825 /* This is really simpleminded and specialized - we are loading an
826 * a.out library that is given an ELF header.
828 static int load_irix_library(struct file *file)
830 struct elfhdr elf_ex;
831 struct elf_phdr *elf_phdata = NULL;
832 unsigned int len = 0;
839 error = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
840 if (error != sizeof(elf_ex))
843 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
846 /* First of all, some simple consistency checks. */
847 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
851 /* Now read in all of the header information. */
852 if (sizeof(struct elf_phdr) * elf_ex.e_phnum > PAGE_SIZE)
855 elf_phdata = kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
856 if (elf_phdata == NULL)
859 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata,
860 sizeof(struct elf_phdr) * elf_ex.e_phnum);
863 for (i=0; i<elf_ex.e_phnum; i++)
864 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
871 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
873 /* Now use mmap to map the library into memory. */
874 down_write(¤t->mm->mmap_sem);
875 error = do_mmap(file,
876 elf_phdata->p_vaddr & 0xfffff000,
877 elf_phdata->p_filesz + (elf_phdata->p_vaddr & 0xfff),
878 PROT_READ | PROT_WRITE | PROT_EXEC,
879 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
880 elf_phdata->p_offset & 0xfffff000);
881 up_write(¤t->mm->mmap_sem);
883 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
884 if (k > elf_bss) elf_bss = k;
886 if (error != (elf_phdata->p_vaddr & 0xfffff000)) {
893 len = (elf_phdata->p_filesz + elf_phdata->p_vaddr+ 0xfff) & 0xfffff000;
894 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
896 down_write(¤t->mm->mmap_sem);
897 do_brk(len, bss-len);
898 up_write(¤t->mm->mmap_sem);
904 /* Called through irix_syssgi() to map an elf image given an FD,
905 * a phdr ptr USER_PHDRP in userspace, and a count CNT telling how many
906 * phdrs there are in the USER_PHDRP array. We return the vaddr the
907 * first phdr was successfully mapped to.
909 unsigned long irix_mapelf(int fd, struct elf_phdr __user *user_phdrp, int cnt)
911 unsigned long type, vaddr, filesz, offset, flags;
912 struct elf_phdr __user *hp;
916 pr_debug("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
917 fd, user_phdrp, cnt);
919 /* First get the verification out of the way. */
921 if (!access_ok(VERIFY_READ, hp, (sizeof(struct elf_phdr) * cnt))) {
922 pr_debug("irix_mapelf: bad pointer to ELF PHDR!\n");
927 dump_phdrs(user_phdrp, cnt);
929 for (i = 0; i < cnt; i++, hp++) {
930 if (__get_user(type, &hp->p_type))
932 if (type != PT_LOAD) {
933 printk("irix_mapelf: One section is not PT_LOAD!\n");
942 printk("irix_mapelf: Bogon filp!\n");
948 for (i = 0; i < cnt; i++, hp++) {
951 retval = __get_user(vaddr, &hp->p_vaddr);
952 retval |= __get_user(filesz, &hp->p_filesz);
953 retval |= __get_user(offset, &hp->p_offset);
954 retval |= __get_user(flags, &hp->p_flags);
958 prot = (flags & PF_R) ? PROT_READ : 0;
959 prot |= (flags & PF_W) ? PROT_WRITE : 0;
960 prot |= (flags & PF_X) ? PROT_EXEC : 0;
962 down_write(¤t->mm->mmap_sem);
963 retval = do_mmap(filp, (vaddr & 0xfffff000),
964 (filesz + (vaddr & 0xfff)),
965 prot, (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
966 (offset & 0xfffff000));
967 up_write(¤t->mm->mmap_sem);
969 if (retval != (vaddr & 0xfffff000)) {
970 printk("irix_mapelf: do_mmap fails with %d!\n", retval);
976 pr_debug("irix_mapelf: Success, returning %08lx\n",
977 (unsigned long) user_phdrp->p_vaddr);
981 if (__get_user(vaddr, &user_phdrp->p_vaddr))
990 * Modelled on fs/exec.c:aout_core_dump()
991 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
994 /* These are the only things you should do on a core-file: use only these
995 * functions to write out all the necessary info.
997 static int dump_write(struct file *file, const void __user *addr, int nr)
999 return file->f_op->write(file, (const char __user *) addr, nr, &file->f_pos) == nr;
1002 static int dump_seek(struct file *file, off_t off)
1004 if (file->f_op->llseek) {
1005 if (file->f_op->llseek(file, off, 0) != off)
1012 /* Decide whether a segment is worth dumping; default is yes to be
1013 * sure (missing info is worse than too much; etc).
1014 * Personally I'd include everything, and use the coredump limit...
1016 * I think we should skip something. But I am not sure how. H.J.
1018 static inline int maydump(struct vm_area_struct *vma)
1020 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
1023 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
1025 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
1031 /* An ELF note in memory. */
1036 unsigned int datasz;
1040 static int notesize(struct memelfnote *en)
1044 sz = sizeof(struct elf_note);
1045 sz += roundup(strlen(en->name) + 1, 4);
1046 sz += roundup(en->datasz, 4);
1051 #define DUMP_WRITE(addr, nr) \
1052 if (!dump_write(file, (addr), (nr))) \
1054 #define DUMP_SEEK(off) \
1055 if (!dump_seek(file, (off))) \
1058 static int writenote(struct memelfnote *men, struct file *file)
1062 en.n_namesz = strlen(men->name) + 1;
1063 en.n_descsz = men->datasz;
1064 en.n_type = men->type;
1066 DUMP_WRITE(&en, sizeof(en));
1067 DUMP_WRITE(men->name, en.n_namesz);
1068 /* XXX - cast from long long to long to avoid need for libgcc.a */
1069 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1070 DUMP_WRITE(men->data, men->datasz);
1071 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1081 #define DUMP_WRITE(addr, nr) \
1082 if (!dump_write(file, (addr), (nr))) \
1084 #define DUMP_SEEK(off) \
1085 if (!dump_seek(file, (off))) \
1090 * This is a two-pass process; first we find the offsets of the bits,
1091 * and then they are actually written out. If we run out of core limit
1094 static int irix_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit)
1101 struct vm_area_struct *vma;
1103 off_t offset = 0, dataoff;
1105 struct memelfnote notes[3];
1106 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1107 elf_fpregset_t fpu; /* NT_PRFPREG */
1108 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1110 /* Count what's needed to dump, up to the limit of coredump size. */
1113 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1116 int sz = vma->vm_end-vma->vm_start;
1118 if (size+sz >= limit)
1126 pr_debug("irix_core_dump: %d segs taking %d bytes\n", segs, size);
1128 /* Set up header. */
1129 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1130 elf.e_ident[EI_CLASS] = ELFCLASS32;
1131 elf.e_ident[EI_DATA] = ELFDATA2LSB;
1132 elf.e_ident[EI_VERSION] = EV_CURRENT;
1133 elf.e_ident[EI_OSABI] = ELF_OSABI;
1134 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1136 elf.e_type = ET_CORE;
1137 elf.e_machine = ELF_ARCH;
1138 elf.e_version = EV_CURRENT;
1140 elf.e_phoff = sizeof(elf);
1143 elf.e_ehsize = sizeof(elf);
1144 elf.e_phentsize = sizeof(struct elf_phdr);
1145 elf.e_phnum = segs+1; /* Include notes. */
1146 elf.e_shentsize = 0;
1154 current->flags |= PF_DUMPCORE;
1156 DUMP_WRITE(&elf, sizeof(elf));
1157 offset += sizeof(elf); /* Elf header. */
1158 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers. */
1160 /* Set up the notes in similar form to SVR4 core dumps made
1161 * with info from their /proc.
1163 memset(&psinfo, 0, sizeof(psinfo));
1164 memset(&prstatus, 0, sizeof(prstatus));
1166 notes[0].name = "CORE";
1167 notes[0].type = NT_PRSTATUS;
1168 notes[0].datasz = sizeof(prstatus);
1169 notes[0].data = &prstatus;
1170 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1171 prstatus.pr_sigpend = current->pending.signal.sig[0];
1172 prstatus.pr_sighold = current->blocked.sig[0];
1173 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1174 psinfo.pr_ppid = prstatus.pr_ppid = current->parent->pid;
1175 psinfo.pr_pgrp = prstatus.pr_pgrp = process_group(current);
1176 psinfo.pr_sid = prstatus.pr_sid = process_session(current);
1177 if (current->pid == current->tgid) {
1179 * This is the record for the group leader. Add in the
1180 * cumulative times of previous dead threads. This total
1181 * won't include the time of each live thread whose state
1182 * is included in the core dump. The final total reported
1183 * to our parent process when it calls wait4 will include
1184 * those sums as well as the little bit more time it takes
1185 * this and each other thread to finish dying after the
1186 * core dump synchronization phase.
1188 jiffies_to_timeval(current->utime + current->signal->utime,
1189 &prstatus.pr_utime);
1190 jiffies_to_timeval(current->stime + current->signal->stime,
1191 &prstatus.pr_stime);
1193 jiffies_to_timeval(current->utime, &prstatus.pr_utime);
1194 jiffies_to_timeval(current->stime, &prstatus.pr_stime);
1196 jiffies_to_timeval(current->signal->cutime, &prstatus.pr_cutime);
1197 jiffies_to_timeval(current->signal->cstime, &prstatus.pr_cstime);
1199 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs)) {
1200 printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
1201 "(%d)\n", sizeof(elf_gregset_t), sizeof(struct pt_regs));
1203 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1206 notes[1].name = "CORE";
1207 notes[1].type = NT_PRPSINFO;
1208 notes[1].datasz = sizeof(psinfo);
1209 notes[1].data = &psinfo;
1210 i = current->state ? ffz(~current->state) + 1 : 0;
1211 psinfo.pr_state = i;
1212 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1213 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1214 psinfo.pr_nice = task_nice(current);
1215 psinfo.pr_flag = current->flags;
1216 psinfo.pr_uid = current->uid;
1217 psinfo.pr_gid = current->gid;
1223 len = current->mm->arg_end - current->mm->arg_start;
1224 len = len >= ELF_PRARGSZ ? ELF_PRARGSZ : len;
1225 (void *) copy_from_user(&psinfo.pr_psargs,
1226 (const char __user *)current->mm->arg_start, len);
1227 for (i = 0; i < len; i++)
1228 if (psinfo.pr_psargs[i] == 0)
1229 psinfo.pr_psargs[i] = ' ';
1230 psinfo.pr_psargs[len] = 0;
1234 strlcpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1236 /* Try to dump the FPU. */
1237 prstatus.pr_fpvalid = dump_fpu(regs, &fpu);
1238 if (!prstatus.pr_fpvalid) {
1241 notes[2].name = "CORE";
1242 notes[2].type = NT_PRFPREG;
1243 notes[2].datasz = sizeof(fpu);
1244 notes[2].data = &fpu;
1247 /* Write notes phdr entry. */
1249 struct elf_phdr phdr;
1252 for (i = 0; i < numnote; i++)
1253 sz += notesize(¬es[i]);
1255 phdr.p_type = PT_NOTE;
1256 phdr.p_offset = offset;
1264 offset += phdr.p_filesz;
1265 DUMP_WRITE(&phdr, sizeof(phdr));
1268 /* Page-align dumped data. */
1269 dataoff = offset = roundup(offset, PAGE_SIZE);
1271 /* Write program headers for segments dump. */
1272 for (vma = current->mm->mmap, i = 0;
1273 i < segs && vma != NULL; vma = vma->vm_next) {
1274 struct elf_phdr phdr;
1279 sz = vma->vm_end - vma->vm_start;
1281 phdr.p_type = PT_LOAD;
1282 phdr.p_offset = offset;
1283 phdr.p_vaddr = vma->vm_start;
1285 phdr.p_filesz = maydump(vma) ? sz : 0;
1287 offset += phdr.p_filesz;
1288 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1289 if (vma->vm_flags & VM_WRITE)
1290 phdr.p_flags |= PF_W;
1291 if (vma->vm_flags & VM_EXEC)
1292 phdr.p_flags |= PF_X;
1293 phdr.p_align = PAGE_SIZE;
1295 DUMP_WRITE(&phdr, sizeof(phdr));
1298 for (i = 0; i < numnote; i++)
1299 if (!writenote(¬es[i], file))
1306 for (i = 0, vma = current->mm->mmap;
1307 i < segs && vma != NULL;
1308 vma = vma->vm_next) {
1309 unsigned long addr = vma->vm_start;
1310 unsigned long len = vma->vm_end - vma->vm_start;
1315 pr_debug("elf_core_dump: writing %08lx %lx\n", addr, len);
1316 DUMP_WRITE((void __user *)addr, len);
1319 if ((off_t) file->f_pos != offset) {
1321 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1322 (off_t) file->f_pos, offset);
1330 static int __init init_irix_binfmt(void)
1332 extern int init_inventory(void);
1333 extern asmlinkage unsigned long sys_call_table;
1334 extern asmlinkage unsigned long sys_call_table_irix5;
1339 * Copy the IRIX5 syscall table (8000 bytes) into the main syscall
1340 * table. The IRIX5 calls are located by an offset of 8000 bytes
1341 * from the beginning of the main table.
1343 memcpy((void *) ((unsigned long) &sys_call_table + 8000),
1344 &sys_call_table_irix5, 8000);
1346 return register_binfmt(&irix_format);
1349 static void __exit exit_irix_binfmt(void)
1352 * Remove the Irix ELF loader.
1354 unregister_binfmt(&irix_format);
1357 module_init(init_irix_binfmt)
1358 module_exit(exit_irix_binfmt)