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,
49 static struct linux_binfmt irix_format = {
50 NULL, THIS_MODULE, load_irix_binary, load_irix_library,
51 irix_core_dump, PAGE_SIZE
54 /* Debugging routines. */
55 static char *get_elf_p_type(Elf32_Word p_type)
88 return "PT_LOPROC/REGINFO";
102 static void print_elfhdr(struct elfhdr *ehp)
106 pr_debug("ELFHDR: e_ident<");
107 for (i = 0; i < (EI_NIDENT - 1); i++)
108 pr_debug("%x ", ehp->e_ident[i]);
109 pr_debug("%x>\n", ehp->e_ident[i]);
110 pr_debug(" e_type[%04x] e_machine[%04x] e_version[%08lx]\n",
111 (unsigned short) ehp->e_type, (unsigned short) ehp->e_machine,
112 (unsigned long) ehp->e_version);
113 pr_debug(" e_entry[%08lx] e_phoff[%08lx] e_shoff[%08lx] "
115 (unsigned long) ehp->e_entry, (unsigned long) ehp->e_phoff,
116 (unsigned long) ehp->e_shoff, (unsigned long) ehp->e_flags);
117 pr_debug(" e_ehsize[%04x] e_phentsize[%04x] e_phnum[%04x]\n",
118 (unsigned short) ehp->e_ehsize,
119 (unsigned short) ehp->e_phentsize,
120 (unsigned short) ehp->e_phnum);
121 pr_debug(" e_shentsize[%04x] e_shnum[%04x] e_shstrndx[%04x]\n",
122 (unsigned short) ehp->e_shentsize,
123 (unsigned short) ehp->e_shnum,
124 (unsigned short) ehp->e_shstrndx);
127 static void print_phdr(int i, struct elf_phdr *ep)
129 pr_debug("PHDR[%d]: p_type[%s] p_offset[%08lx] p_vaddr[%08lx] "
130 "p_paddr[%08lx]\n", i, get_elf_p_type(ep->p_type),
131 (unsigned long) ep->p_offset, (unsigned long) ep->p_vaddr,
132 (unsigned long) ep->p_paddr);
133 pr_debug(" p_filesz[%08lx] p_memsz[%08lx] p_flags[%08lx] "
134 "p_align[%08lx]\n", (unsigned long) ep->p_filesz,
135 (unsigned long) ep->p_memsz, (unsigned long) ep->p_flags,
136 (unsigned long) ep->p_align);
139 static void dump_phdrs(struct elf_phdr *ep, int pnum)
143 for (i = 0; i < pnum; i++, ep++) {
144 if ((ep->p_type == PT_LOAD) ||
145 (ep->p_type == PT_INTERP) ||
146 (ep->p_type == PT_PHDR))
151 static void set_brk(unsigned long start, unsigned long end)
153 start = PAGE_ALIGN(start);
154 end = PAGE_ALIGN(end);
157 down_write(¤t->mm->mmap_sem);
158 do_brk(start, end - start);
159 up_write(¤t->mm->mmap_sem);
163 /* We need to explicitly zero any fractional pages
164 * after the data section (i.e. bss). This would
165 * contain the junk from the file that should not
168 static void padzero(unsigned long elf_bss)
172 nbyte = elf_bss & (PAGE_SIZE-1);
174 nbyte = PAGE_SIZE - nbyte;
175 clear_user((void __user *) elf_bss, nbyte);
179 static unsigned long * create_irix_tables(char * p, int argc, int envc,
180 struct elfhdr * exec, unsigned int load_addr,
181 unsigned int interp_load_addr, struct pt_regs *regs,
182 struct elf_phdr *ephdr)
186 elf_addr_t *sp, *csp;
188 pr_debug("create_irix_tables: p[%p] argc[%d] envc[%d] "
189 "load_addr[%08x] interp_load_addr[%08x]\n",
190 p, argc, envc, load_addr, interp_load_addr);
192 sp = (elf_addr_t *) (~15UL & (unsigned long) p);
194 csp -= exec ? DLINFO_ITEMS*2 : 2;
197 csp -= 1; /* argc itself */
198 if ((unsigned long)csp & 15UL) {
199 sp -= (16UL - ((unsigned long)csp & 15UL)) / sizeof(*sp);
203 * Put the ELF interpreter info on the stack
205 #define NEW_AUX_ENT(nr, id, val) \
206 __put_user ((id), sp+(nr*2)); \
207 __put_user ((val), sp+(nr*2+1)); \
210 NEW_AUX_ENT(0, AT_NULL, 0);
215 NEW_AUX_ENT (0, AT_PHDR, load_addr + exec->e_phoff);
216 NEW_AUX_ENT (1, AT_PHENT, sizeof (struct elf_phdr));
217 NEW_AUX_ENT (2, AT_PHNUM, exec->e_phnum);
218 NEW_AUX_ENT (3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
219 NEW_AUX_ENT (4, AT_BASE, interp_load_addr);
220 NEW_AUX_ENT (5, AT_FLAGS, 0);
221 NEW_AUX_ENT (6, AT_ENTRY, (elf_addr_t) exec->e_entry);
222 NEW_AUX_ENT (7, AT_UID, (elf_addr_t) current->uid);
223 NEW_AUX_ENT (8, AT_EUID, (elf_addr_t) current->euid);
224 NEW_AUX_ENT (9, AT_GID, (elf_addr_t) current->gid);
225 NEW_AUX_ENT (10, AT_EGID, (elf_addr_t) current->egid);
234 __put_user((elf_addr_t)argc,--sp);
235 current->mm->arg_start = (unsigned long) p;
237 __put_user((unsigned long)p,argv++);
240 __put_user((unsigned long) NULL, argv);
241 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
243 __put_user((unsigned long)p,envp++);
246 __put_user((unsigned long) NULL, envp);
247 current->mm->env_end = (unsigned long) p;
252 /* This is much more generalized than the library routine read function,
253 * so we keep this separate. Technically the library read function
254 * is only provided so that we can read a.out libraries that have
257 static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
258 struct file * interpreter,
259 unsigned int *interp_load_addr)
261 struct elf_phdr *elf_phdata = NULL;
262 struct elf_phdr *eppnt;
264 unsigned int load_addr;
267 unsigned int last_bss;
274 error = load_addr = 0;
276 print_elfhdr(interp_elf_ex);
278 /* First of all, some simple consistency checks */
279 if ((interp_elf_ex->e_type != ET_EXEC &&
280 interp_elf_ex->e_type != ET_DYN) ||
281 !interpreter->f_op->mmap) {
282 printk("IRIX interp has bad e_type %d\n", interp_elf_ex->e_type);
286 /* Now read in all of the header information */
287 if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > PAGE_SIZE) {
288 printk("IRIX interp header bigger than a page (%d)\n",
289 (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum));
293 elf_phdata = kmalloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum,
297 printk("Cannot kmalloc phdata for IRIX interp.\n");
301 /* If the size of this structure has changed, then punt, since
302 * we will be doing the wrong thing.
304 if (interp_elf_ex->e_phentsize != 32) {
305 printk("IRIX interp e_phentsize == %d != 32 ",
306 interp_elf_ex->e_phentsize);
311 retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
313 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
315 dump_phdrs(elf_phdata, interp_elf_ex->e_phnum);
318 for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
319 if (eppnt->p_type == PT_LOAD) {
320 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
322 unsigned long vaddr = 0;
323 if (eppnt->p_flags & PF_R)
324 elf_prot = PROT_READ;
325 if (eppnt->p_flags & PF_W)
326 elf_prot |= PROT_WRITE;
327 if (eppnt->p_flags & PF_X)
328 elf_prot |= PROT_EXEC;
329 elf_type |= MAP_FIXED;
330 vaddr = eppnt->p_vaddr;
332 pr_debug("INTERP do_mmap"
333 "(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
336 (eppnt->p_filesz + (eppnt->p_vaddr & 0xfff)),
338 elf_prot, (unsigned long) elf_type,
340 (eppnt->p_offset & 0xfffff000));
342 down_write(¤t->mm->mmap_sem);
343 error = do_mmap(interpreter, vaddr,
344 eppnt->p_filesz + (eppnt->p_vaddr & 0xfff),
346 eppnt->p_offset & 0xfffff000);
347 up_write(¤t->mm->mmap_sem);
349 if (error < 0 && error > -1024) {
350 printk("Aieee IRIX interp mmap error=%d\n",
352 break; /* Real error */
354 pr_debug("error=%08lx ", (unsigned long) error);
355 if (!load_addr && interp_elf_ex->e_type == ET_DYN) {
357 pr_debug("load_addr = error ");
361 * Find the end of the file mapping for this phdr, and
362 * keep track of the largest address we see for this.
364 k = eppnt->p_vaddr + eppnt->p_filesz;
368 /* Do the same thing for the memory mapping - between
369 * elf_bss and last_bss is the bss section.
371 k = eppnt->p_memsz + eppnt->p_vaddr;
378 /* Now use mmap to map the library into memory. */
379 if (error < 0 && error > -1024) {
380 pr_debug("got error %d\n", error);
385 /* Now fill out the bss section. First pad the last page up
386 * to the page boundary, and then perform a mmap to make sure
387 * that there are zero-mapped pages up to and including the
390 pr_debug("padzero(%08lx) ", (unsigned long) (elf_bss));
392 len = (elf_bss + 0xfff) & 0xfffff000; /* What we have mapped so far */
394 pr_debug("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss,
395 (unsigned long) len);
397 /* Map the last of the bss segment */
398 if (last_bss > len) {
399 down_write(¤t->mm->mmap_sem);
400 do_brk(len, (last_bss - len));
401 up_write(¤t->mm->mmap_sem);
405 *interp_load_addr = load_addr;
406 return ((unsigned int) interp_elf_ex->e_entry);
409 /* Check sanity of IRIX elf executable header. */
410 static int verify_binary(struct elfhdr *ehp, struct linux_binprm *bprm)
412 if (memcmp(ehp->e_ident, ELFMAG, SELFMAG) != 0)
415 /* First of all, some simple consistency checks */
416 if ((ehp->e_type != ET_EXEC && ehp->e_type != ET_DYN) ||
417 !bprm->file->f_op->mmap) {
421 /* XXX Don't support N32 or 64bit binaries yet because they can
422 * XXX and do execute 64 bit instructions and expect all registers
423 * XXX to be 64 bit as well. We need to make the kernel save
424 * XXX all registers as 64bits on cpu's capable of this at
425 * XXX exception time plus frob the XTLB exception vector.
427 if ((ehp->e_flags & EF_MIPS_ABI2))
434 * This is where the detailed check is performed. Irix binaries
435 * use interpreters with 'libc.so' in the name, so this function
436 * can differentiate between Linux and Irix binaries.
438 static inline int look_for_irix_interpreter(char **name,
439 struct file **interpreter,
440 struct elfhdr *interp_elf_ex,
441 struct elf_phdr *epp,
442 struct linux_binprm *bprm, int pnum)
445 int retval = -EINVAL;
446 struct file *file = NULL;
449 for (i = 0; i < pnum; i++, epp++) {
450 if (epp->p_type != PT_INTERP)
453 /* It is illegal to have two interpreters for one executable. */
457 *name = kmalloc(epp->p_filesz + strlen(IRIX_EMUL), GFP_KERNEL);
461 strcpy(*name, IRIX_EMUL);
462 retval = kernel_read(bprm->file, epp->p_offset, (*name + 16),
467 file = open_exec(*name);
469 retval = PTR_ERR(file);
472 retval = kernel_read(file, 0, bprm->buf, 128);
476 *interp_elf_ex = *(struct elfhdr *) bprm->buf;
488 static inline int verify_irix_interpreter(struct elfhdr *ihp)
490 if (memcmp(ihp->e_ident, ELFMAG, SELFMAG) != 0)
495 #define EXEC_MAP_FLAGS (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE)
497 static inline void map_executable(struct file *fp, struct elf_phdr *epp, int pnum,
498 unsigned int *estack, unsigned int *laddr,
499 unsigned int *scode, unsigned int *ebss,
500 unsigned int *ecode, unsigned int *edata,
506 for (i = 0; i < pnum; i++, epp++) {
507 if (epp->p_type != PT_LOAD)
511 prot = (epp->p_flags & PF_R) ? PROT_READ : 0;
512 prot |= (epp->p_flags & PF_W) ? PROT_WRITE : 0;
513 prot |= (epp->p_flags & PF_X) ? PROT_EXEC : 0;
514 down_write(¤t->mm->mmap_sem);
515 (void) do_mmap(fp, (epp->p_vaddr & 0xfffff000),
516 (epp->p_filesz + (epp->p_vaddr & 0xfff)),
517 prot, EXEC_MAP_FLAGS,
518 (epp->p_offset & 0xfffff000));
519 up_write(¤t->mm->mmap_sem);
521 /* Fixup location tracking vars. */
522 if ((epp->p_vaddr & 0xfffff000) < *estack)
523 *estack = (epp->p_vaddr & 0xfffff000);
525 *laddr = epp->p_vaddr - epp->p_offset;
526 if (epp->p_vaddr < *scode)
527 *scode = epp->p_vaddr;
529 tmp = epp->p_vaddr + epp->p_filesz;
532 if ((epp->p_flags & PF_X) && *ecode < tmp)
537 tmp = epp->p_vaddr + epp->p_memsz;
544 static inline int map_interpreter(struct elf_phdr *epp, struct elfhdr *ihp,
545 struct file *interp, unsigned int *iladdr,
546 int pnum, mm_segment_t old_fs,
547 unsigned int *eentry)
551 *eentry = 0xffffffff;
552 for (i = 0; i < pnum; i++, epp++) {
553 if (epp->p_type != PT_INTERP)
556 /* We should have fielded this error elsewhere... */
557 if (*eentry != 0xffffffff)
561 *eentry = load_irix_interp(ihp, interp, iladdr);
567 if (*eentry == 0xffffffff)
574 * IRIX maps a page at 0x200000 that holds information about the
575 * process and the system, here we map the page and fill the
578 static void irix_map_prda_page(void)
583 down_write(¤t->mm->mmap_sem);
584 v = do_brk (PRDA_ADDRESS, PAGE_SIZE);
585 up_write(¤t->mm->mmap_sem);
590 pp = (struct prda *) v;
591 pp->prda_sys.t_pid = current->pid;
592 pp->prda_sys.t_prid = read_c0_prid();
593 pp->prda_sys.t_rpid = current->pid;
595 /* We leave the rest set to zero */
600 /* These are the functions used to load ELF style executables and shared
601 * libraries. There is no binary dependent code anywhere else.
603 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
605 struct elfhdr elf_ex, interp_elf_ex;
606 struct file *interpreter;
607 struct elf_phdr *elf_phdata, *elf_ihdr, *elf_ephdr;
608 unsigned int load_addr, elf_bss, elf_brk;
609 unsigned int elf_entry, interp_load_addr = 0;
610 unsigned int start_code, end_code, end_data, elf_stack;
611 int retval, has_interp, has_ephdr, size, i;
612 char *elf_interpreter;
616 has_interp = has_ephdr = 0;
617 elf_ihdr = elf_ephdr = NULL;
618 elf_ex = *((struct elfhdr *) bprm->buf);
621 if (verify_binary(&elf_ex, bprm))
625 * Telling -o32 static binaries from Linux and Irix apart from each
626 * other is difficult. There are 2 differences to be noted for static
627 * binaries from the 2 operating systems:
629 * 1) Irix binaries have their .text section before their .init
630 * section. Linux binaries are just the opposite.
632 * 2) Irix binaries usually have <= 12 sections and Linux
633 * binaries have > 20.
635 * We will use Method #2 since Method #1 would require us to read in
636 * the section headers which is way too much overhead. This appears
637 * to work for everything we have ran into so far. If anyone has a
638 * better method to tell the binaries apart, I'm listening.
640 if (elf_ex.e_shnum > 20)
643 print_elfhdr(&elf_ex);
645 /* Now read in all of the header information */
646 size = elf_ex.e_phentsize * elf_ex.e_phnum;
649 elf_phdata = kmalloc(size, GFP_KERNEL);
650 if (elf_phdata == NULL) {
655 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *)elf_phdata, size);
659 dump_phdrs(elf_phdata, elf_ex.e_phnum);
661 /* Set some things for later. */
662 for (i = 0; i < elf_ex.e_phnum; i++) {
663 switch (elf_phdata[i].p_type) {
666 elf_ihdr = &elf_phdata[i];
670 elf_ephdr = &elf_phdata[i];
680 elf_stack = 0xffffffff;
681 elf_interpreter = NULL;
682 start_code = 0xffffffff;
687 * If we get a return value, we change the value to be ENOEXEC
688 * so that we can exit gracefully and the main binary format
689 * search loop in 'fs/exec.c' will move onto the next handler
690 * which should be the normal ELF binary handler.
692 retval = look_for_irix_interpreter(&elf_interpreter, &interpreter,
693 &interp_elf_ex, elf_phdata, bprm,
700 if (elf_interpreter) {
701 retval = verify_irix_interpreter(&interp_elf_ex);
703 goto out_free_interp;
706 /* OK, we are done with that, now set up the arg stuff,
707 * and then start this sucker up.
710 if (!bprm->sh_bang && !bprm->p)
711 goto out_free_interp;
713 /* Flush all traces of the currently running executable */
714 retval = flush_old_exec(bprm);
716 goto out_free_dentry;
718 /* OK, This is the point of no return */
719 current->mm->end_data = 0;
720 current->mm->end_code = 0;
721 current->mm->mmap = NULL;
722 current->flags &= ~PF_FORKNOEXEC;
723 elf_entry = (unsigned int) elf_ex.e_entry;
725 /* Do this so that we can load the interpreter, if need be. We will
726 * change some of these later.
728 setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
729 current->mm->start_stack = bprm->p;
731 /* At this point, we assume that the image should be loaded at
732 * fixed address, not at a variable address.
737 map_executable(bprm->file, elf_phdata, elf_ex.e_phnum, &elf_stack,
738 &load_addr, &start_code, &elf_bss, &end_code,
739 &end_data, &elf_brk);
741 if (elf_interpreter) {
742 retval = map_interpreter(elf_phdata, &interp_elf_ex,
743 interpreter, &interp_load_addr,
744 elf_ex.e_phnum, old_fs, &elf_entry);
745 kfree(elf_interpreter);
748 printk("Unable to load IRIX ELF interpreter\n");
749 send_sig(SIGSEGV, current, 0);
758 set_personality(PER_IRIX32);
759 set_binfmt(&irix_format);
761 current->flags &= ~PF_FORKNOEXEC;
762 bprm->p = (unsigned long)
763 create_irix_tables((char *)bprm->p, bprm->argc, bprm->envc,
764 (elf_interpreter ? &elf_ex : NULL),
765 load_addr, interp_load_addr, regs, elf_ephdr);
766 current->mm->start_brk = current->mm->brk = elf_brk;
767 current->mm->end_code = end_code;
768 current->mm->start_code = start_code;
769 current->mm->end_data = end_data;
770 current->mm->start_stack = bprm->p;
772 /* Calling set_brk effectively mmaps the pages that we need for the
773 * bss and break sections.
775 set_brk(elf_bss, elf_brk);
778 * IRIX maps a page at 0x200000 which holds some system
779 * information. Programs depend on this.
781 irix_map_prda_page();
785 pr_debug("(start_brk) %lx\n" , (long) current->mm->start_brk);
786 pr_debug("(end_code) %lx\n" , (long) current->mm->end_code);
787 pr_debug("(start_code) %lx\n" , (long) current->mm->start_code);
788 pr_debug("(end_data) %lx\n" , (long) current->mm->end_data);
789 pr_debug("(start_stack) %lx\n" , (long) current->mm->start_stack);
790 pr_debug("(brk) %lx\n" , (long) current->mm->brk);
792 #if 0 /* XXX No fucking way dude... */
793 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
794 * and some applications "depend" upon this behavior.
795 * Since we do not have the power to recompile these, we
796 * emulate the SVr4 behavior. Sigh.
798 down_write(¤t->mm->mmap_sem);
799 (void) do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
800 MAP_FIXED | MAP_PRIVATE, 0);
801 up_write(¤t->mm->mmap_sem);
804 start_thread(regs, elf_entry, bprm->p);
805 if (current->ptrace & PT_PTRACED)
806 send_sig(SIGTRAP, current, 0);
812 allow_write_access(interpreter);
815 kfree(elf_interpreter);
822 /* This is really simpleminded and specialized - we are loading an
823 * a.out library that is given an ELF header.
825 static int load_irix_library(struct file *file)
827 struct elfhdr elf_ex;
828 struct elf_phdr *elf_phdata = NULL;
829 unsigned int len = 0;
836 error = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
837 if (error != sizeof(elf_ex))
840 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
843 /* First of all, some simple consistency checks. */
844 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
848 /* Now read in all of the header information. */
849 if (sizeof(struct elf_phdr) * elf_ex.e_phnum > PAGE_SIZE)
852 elf_phdata = kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
853 if (elf_phdata == NULL)
856 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata,
857 sizeof(struct elf_phdr) * elf_ex.e_phnum);
860 for (i=0; i<elf_ex.e_phnum; i++)
861 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
868 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
870 /* Now use mmap to map the library into memory. */
871 down_write(¤t->mm->mmap_sem);
872 error = do_mmap(file,
873 elf_phdata->p_vaddr & 0xfffff000,
874 elf_phdata->p_filesz + (elf_phdata->p_vaddr & 0xfff),
875 PROT_READ | PROT_WRITE | PROT_EXEC,
876 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
877 elf_phdata->p_offset & 0xfffff000);
878 up_write(¤t->mm->mmap_sem);
880 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
881 if (k > elf_bss) elf_bss = k;
883 if (error != (elf_phdata->p_vaddr & 0xfffff000)) {
890 len = (elf_phdata->p_filesz + elf_phdata->p_vaddr+ 0xfff) & 0xfffff000;
891 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
893 down_write(¤t->mm->mmap_sem);
894 do_brk(len, bss-len);
895 up_write(¤t->mm->mmap_sem);
901 /* Called through irix_syssgi() to map an elf image given an FD,
902 * a phdr ptr USER_PHDRP in userspace, and a count CNT telling how many
903 * phdrs there are in the USER_PHDRP array. We return the vaddr the
904 * first phdr was successfully mapped to.
906 unsigned long irix_mapelf(int fd, struct elf_phdr __user *user_phdrp, int cnt)
908 unsigned long type, vaddr, filesz, offset, flags;
909 struct elf_phdr __user *hp;
913 pr_debug("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
914 fd, user_phdrp, cnt);
916 /* First get the verification out of the way. */
918 if (!access_ok(VERIFY_READ, hp, (sizeof(struct elf_phdr) * cnt))) {
919 pr_debug("irix_mapelf: bad pointer to ELF PHDR!\n");
924 dump_phdrs(user_phdrp, cnt);
926 for (i = 0; i < cnt; i++, hp++) {
927 if (__get_user(type, &hp->p_type))
929 if (type != PT_LOAD) {
930 printk("irix_mapelf: One section is not PT_LOAD!\n");
939 printk("irix_mapelf: Bogon filp!\n");
945 for (i = 0; i < cnt; i++, hp++) {
948 retval = __get_user(vaddr, &hp->p_vaddr);
949 retval |= __get_user(filesz, &hp->p_filesz);
950 retval |= __get_user(offset, &hp->p_offset);
951 retval |= __get_user(flags, &hp->p_flags);
955 prot = (flags & PF_R) ? PROT_READ : 0;
956 prot |= (flags & PF_W) ? PROT_WRITE : 0;
957 prot |= (flags & PF_X) ? PROT_EXEC : 0;
959 down_write(¤t->mm->mmap_sem);
960 retval = do_mmap(filp, (vaddr & 0xfffff000),
961 (filesz + (vaddr & 0xfff)),
962 prot, (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
963 (offset & 0xfffff000));
964 up_write(¤t->mm->mmap_sem);
966 if (retval != (vaddr & 0xfffff000)) {
967 printk("irix_mapelf: do_mmap fails with %d!\n", retval);
973 pr_debug("irix_mapelf: Success, returning %08lx\n",
974 (unsigned long) user_phdrp->p_vaddr);
978 if (__get_user(vaddr, &user_phdrp->p_vaddr))
987 * Modelled on fs/exec.c:aout_core_dump()
988 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
991 /* These are the only things you should do on a core-file: use only these
992 * functions to write out all the necessary info.
994 static int dump_write(struct file *file, const void __user *addr, int nr)
996 return file->f_op->write(file, (const char __user *) addr, nr, &file->f_pos) == nr;
999 static int dump_seek(struct file *file, off_t off)
1001 if (file->f_op->llseek) {
1002 if (file->f_op->llseek(file, off, 0) != off)
1009 /* Decide whether a segment is worth dumping; default is yes to be
1010 * sure (missing info is worse than too much; etc).
1011 * Personally I'd include everything, and use the coredump limit...
1013 * I think we should skip something. But I am not sure how. H.J.
1015 static inline int maydump(struct vm_area_struct *vma)
1017 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
1020 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
1022 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
1028 /* An ELF note in memory. */
1033 unsigned int datasz;
1037 static int notesize(struct memelfnote *en)
1041 sz = sizeof(struct elf_note);
1042 sz += roundup(strlen(en->name) + 1, 4);
1043 sz += roundup(en->datasz, 4);
1048 #define DUMP_WRITE(addr, nr) \
1049 if (!dump_write(file, (addr), (nr))) \
1051 #define DUMP_SEEK(off) \
1052 if (!dump_seek(file, (off))) \
1055 static int writenote(struct memelfnote *men, struct file *file)
1059 en.n_namesz = strlen(men->name) + 1;
1060 en.n_descsz = men->datasz;
1061 en.n_type = men->type;
1063 DUMP_WRITE(&en, sizeof(en));
1064 DUMP_WRITE(men->name, en.n_namesz);
1065 /* XXX - cast from long long to long to avoid need for libgcc.a */
1066 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1067 DUMP_WRITE(men->data, men->datasz);
1068 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1078 #define DUMP_WRITE(addr, nr) \
1079 if (!dump_write(file, (addr), (nr))) \
1081 #define DUMP_SEEK(off) \
1082 if (!dump_seek(file, (off))) \
1087 * This is a two-pass process; first we find the offsets of the bits,
1088 * and then they are actually written out. If we run out of core limit
1091 static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
1098 struct vm_area_struct *vma;
1100 off_t offset = 0, dataoff;
1101 int limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1103 struct memelfnote notes[3];
1104 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1105 elf_fpregset_t fpu; /* NT_PRFPREG */
1106 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1108 /* Count what's needed to dump, up to the limit of coredump size. */
1111 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1114 int sz = vma->vm_end-vma->vm_start;
1116 if (size+sz >= limit)
1124 pr_debug("irix_core_dump: %d segs taking %d bytes\n", segs, size);
1126 /* Set up header. */
1127 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1128 elf.e_ident[EI_CLASS] = ELFCLASS32;
1129 elf.e_ident[EI_DATA] = ELFDATA2LSB;
1130 elf.e_ident[EI_VERSION] = EV_CURRENT;
1131 elf.e_ident[EI_OSABI] = ELF_OSABI;
1132 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1134 elf.e_type = ET_CORE;
1135 elf.e_machine = ELF_ARCH;
1136 elf.e_version = EV_CURRENT;
1138 elf.e_phoff = sizeof(elf);
1141 elf.e_ehsize = sizeof(elf);
1142 elf.e_phentsize = sizeof(struct elf_phdr);
1143 elf.e_phnum = segs+1; /* Include notes. */
1144 elf.e_shentsize = 0;
1152 current->flags |= PF_DUMPCORE;
1154 DUMP_WRITE(&elf, sizeof(elf));
1155 offset += sizeof(elf); /* Elf header. */
1156 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers. */
1158 /* Set up the notes in similar form to SVR4 core dumps made
1159 * with info from their /proc.
1161 memset(&psinfo, 0, sizeof(psinfo));
1162 memset(&prstatus, 0, sizeof(prstatus));
1164 notes[0].name = "CORE";
1165 notes[0].type = NT_PRSTATUS;
1166 notes[0].datasz = sizeof(prstatus);
1167 notes[0].data = &prstatus;
1168 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1169 prstatus.pr_sigpend = current->pending.signal.sig[0];
1170 prstatus.pr_sighold = current->blocked.sig[0];
1171 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1172 psinfo.pr_ppid = prstatus.pr_ppid = current->parent->pid;
1173 psinfo.pr_pgrp = prstatus.pr_pgrp = process_group(current);
1174 psinfo.pr_sid = prstatus.pr_sid = process_session(current);
1175 if (current->pid == current->tgid) {
1177 * This is the record for the group leader. Add in the
1178 * cumulative times of previous dead threads. This total
1179 * won't include the time of each live thread whose state
1180 * is included in the core dump. The final total reported
1181 * to our parent process when it calls wait4 will include
1182 * those sums as well as the little bit more time it takes
1183 * this and each other thread to finish dying after the
1184 * core dump synchronization phase.
1186 jiffies_to_timeval(current->utime + current->signal->utime,
1187 &prstatus.pr_utime);
1188 jiffies_to_timeval(current->stime + current->signal->stime,
1189 &prstatus.pr_stime);
1191 jiffies_to_timeval(current->utime, &prstatus.pr_utime);
1192 jiffies_to_timeval(current->stime, &prstatus.pr_stime);
1194 jiffies_to_timeval(current->signal->cutime, &prstatus.pr_cutime);
1195 jiffies_to_timeval(current->signal->cstime, &prstatus.pr_cstime);
1197 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs)) {
1198 printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
1199 "(%d)\n", sizeof(elf_gregset_t), sizeof(struct pt_regs));
1201 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1204 notes[1].name = "CORE";
1205 notes[1].type = NT_PRPSINFO;
1206 notes[1].datasz = sizeof(psinfo);
1207 notes[1].data = &psinfo;
1208 i = current->state ? ffz(~current->state) + 1 : 0;
1209 psinfo.pr_state = i;
1210 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1211 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1212 psinfo.pr_nice = task_nice(current);
1213 psinfo.pr_flag = current->flags;
1214 psinfo.pr_uid = current->uid;
1215 psinfo.pr_gid = current->gid;
1221 len = current->mm->arg_end - current->mm->arg_start;
1222 len = len >= ELF_PRARGSZ ? ELF_PRARGSZ : len;
1223 (void *) copy_from_user(&psinfo.pr_psargs,
1224 (const char __user *)current->mm->arg_start, len);
1225 for (i = 0; i < len; i++)
1226 if (psinfo.pr_psargs[i] == 0)
1227 psinfo.pr_psargs[i] = ' ';
1228 psinfo.pr_psargs[len] = 0;
1232 strlcpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1234 /* Try to dump the FPU. */
1235 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1236 if (!prstatus.pr_fpvalid) {
1239 notes[2].name = "CORE";
1240 notes[2].type = NT_PRFPREG;
1241 notes[2].datasz = sizeof(fpu);
1242 notes[2].data = &fpu;
1245 /* Write notes phdr entry. */
1247 struct elf_phdr phdr;
1250 for (i = 0; i < numnote; i++)
1251 sz += notesize(¬es[i]);
1253 phdr.p_type = PT_NOTE;
1254 phdr.p_offset = offset;
1262 offset += phdr.p_filesz;
1263 DUMP_WRITE(&phdr, sizeof(phdr));
1266 /* Page-align dumped data. */
1267 dataoff = offset = roundup(offset, PAGE_SIZE);
1269 /* Write program headers for segments dump. */
1270 for (vma = current->mm->mmap, i = 0;
1271 i < segs && vma != NULL; vma = vma->vm_next) {
1272 struct elf_phdr phdr;
1277 sz = vma->vm_end - vma->vm_start;
1279 phdr.p_type = PT_LOAD;
1280 phdr.p_offset = offset;
1281 phdr.p_vaddr = vma->vm_start;
1283 phdr.p_filesz = maydump(vma) ? sz : 0;
1285 offset += phdr.p_filesz;
1286 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1287 if (vma->vm_flags & VM_WRITE)
1288 phdr.p_flags |= PF_W;
1289 if (vma->vm_flags & VM_EXEC)
1290 phdr.p_flags |= PF_X;
1291 phdr.p_align = PAGE_SIZE;
1293 DUMP_WRITE(&phdr, sizeof(phdr));
1296 for (i = 0; i < numnote; i++)
1297 if (!writenote(¬es[i], file))
1304 for (i = 0, vma = current->mm->mmap;
1305 i < segs && vma != NULL;
1306 vma = vma->vm_next) {
1307 unsigned long addr = vma->vm_start;
1308 unsigned long len = vma->vm_end - vma->vm_start;
1313 pr_debug("elf_core_dump: writing %08lx %lx\n", addr, len);
1314 DUMP_WRITE((void __user *)addr, len);
1317 if ((off_t) file->f_pos != offset) {
1319 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1320 (off_t) file->f_pos, offset);
1328 static int __init init_irix_binfmt(void)
1330 extern int init_inventory(void);
1331 extern asmlinkage unsigned long sys_call_table;
1332 extern asmlinkage unsigned long sys_call_table_irix5;
1337 * Copy the IRIX5 syscall table (8000 bytes) into the main syscall
1338 * table. The IRIX5 calls are located by an offset of 8000 bytes
1339 * from the beginning of the main table.
1341 memcpy((void *) ((unsigned long) &sys_call_table + 8000),
1342 &sys_call_table_irix5, 8000);
1344 return register_binfmt(&irix_format);
1347 static void __exit exit_irix_binfmt(void)
1350 * Remove the Irix ELF loader.
1352 unregister_binfmt(&irix_format);
1355 module_init(init_irix_binfmt)
1356 module_exit(exit_irix_binfmt)