2 * irixelf.c: Code to load IRIX ELF executables which conform to
5 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
7 * Based upon work which is:
8 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
11 #include <linux/module.h>
14 #include <linux/stat.h>
15 #include <linux/sched.h>
17 #include <linux/mman.h>
18 #include <linux/a.out.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/signal.h>
22 #include <linux/binfmts.h>
23 #include <linux/string.h>
24 #include <linux/file.h>
25 #include <linux/fcntl.h>
26 #include <linux/ptrace.h>
27 #include <linux/slab.h>
28 #include <linux/shm.h>
29 #include <linux/personality.h>
30 #include <linux/elfcore.h>
31 #include <linux/smp_lock.h>
33 #include <asm/uaccess.h>
34 #include <asm/mipsregs.h>
35 #include <asm/prctl.h>
37 #define DLINFO_ITEMS 12
39 #include <linux/elf.h>
43 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs);
44 static int load_irix_library(struct file *);
45 static int irix_core_dump(long signr, struct pt_regs * regs,
48 static struct linux_binfmt irix_format = {
49 NULL, THIS_MODULE, load_irix_binary, load_irix_library,
50 irix_core_dump, PAGE_SIZE
54 #define elf_addr_t unsigned long
58 /* Debugging routines. */
59 static char *get_elf_p_type(Elf32_Word p_type)
64 case PT_NULL: return("PT_NULL"); break;
65 case PT_LOAD: return("PT_LOAD"); break;
66 case PT_DYNAMIC: return("PT_DYNAMIC"); break;
67 case PT_INTERP: return("PT_INTERP"); break;
68 case PT_NOTE: return("PT_NOTE"); break;
69 case PT_SHLIB: return("PT_SHLIB"); break;
70 case PT_PHDR: return("PT_PHDR"); break;
71 case PT_LOPROC: return("PT_LOPROC/REGINFO"); break;
72 case PT_HIPROC: return("PT_HIPROC"); break;
73 default: return("PT_BOGUS"); break;
77 static void print_elfhdr(struct elfhdr *ehp)
81 printk("ELFHDR: e_ident<");
82 for(i = 0; i < (EI_NIDENT - 1); i++) printk("%x ", ehp->e_ident[i]);
83 printk("%x>\n", ehp->e_ident[i]);
84 printk(" e_type[%04x] e_machine[%04x] e_version[%08lx]\n",
85 (unsigned short) ehp->e_type, (unsigned short) ehp->e_machine,
86 (unsigned long) ehp->e_version);
87 printk(" e_entry[%08lx] e_phoff[%08lx] e_shoff[%08lx] "
89 (unsigned long) ehp->e_entry, (unsigned long) ehp->e_phoff,
90 (unsigned long) ehp->e_shoff, (unsigned long) ehp->e_flags);
91 printk(" e_ehsize[%04x] e_phentsize[%04x] e_phnum[%04x]\n",
92 (unsigned short) ehp->e_ehsize, (unsigned short) ehp->e_phentsize,
93 (unsigned short) ehp->e_phnum);
94 printk(" e_shentsize[%04x] e_shnum[%04x] e_shstrndx[%04x]\n",
95 (unsigned short) ehp->e_shentsize, (unsigned short) ehp->e_shnum,
96 (unsigned short) ehp->e_shstrndx);
99 static void print_phdr(int i, struct elf_phdr *ep)
101 printk("PHDR[%d]: p_type[%s] p_offset[%08lx] p_vaddr[%08lx] "
102 "p_paddr[%08lx]\n", i, get_elf_p_type(ep->p_type),
103 (unsigned long) ep->p_offset, (unsigned long) ep->p_vaddr,
104 (unsigned long) ep->p_paddr);
105 printk(" p_filesz[%08lx] p_memsz[%08lx] p_flags[%08lx] "
106 "p_align[%08lx]\n", (unsigned long) ep->p_filesz,
107 (unsigned long) ep->p_memsz, (unsigned long) ep->p_flags,
108 (unsigned long) ep->p_align);
111 static void dump_phdrs(struct elf_phdr *ep, int pnum)
115 for(i = 0; i < pnum; i++, ep++) {
116 if((ep->p_type == PT_LOAD) ||
117 (ep->p_type == PT_INTERP) ||
118 (ep->p_type == PT_PHDR))
122 #endif /* (DEBUG_ELF) */
124 static void set_brk(unsigned long start, unsigned long end)
126 start = PAGE_ALIGN(start);
127 end = PAGE_ALIGN(end);
130 do_brk(start, end - start);
134 /* We need to explicitly zero any fractional pages
135 * after the data section (i.e. bss). This would
136 * contain the junk from the file that should not
139 static void padzero(unsigned long elf_bss)
143 nbyte = elf_bss & (PAGE_SIZE-1);
145 nbyte = PAGE_SIZE - nbyte;
146 clear_user((void *) elf_bss, nbyte);
150 unsigned long * create_irix_tables(char * p, int argc, int envc,
151 struct elfhdr * exec, unsigned int load_addr,
152 unsigned int interp_load_addr,
153 struct pt_regs *regs, struct elf_phdr *ephdr)
157 elf_addr_t *sp, *csp;
160 printk("create_irix_tables: p[%p] argc[%d] envc[%d] "
161 "load_addr[%08x] interp_load_addr[%08x]\n",
162 p, argc, envc, load_addr, interp_load_addr);
164 sp = (elf_addr_t *) (~15UL & (unsigned long) p);
166 csp -= exec ? DLINFO_ITEMS*2 : 2;
169 csp -= 1; /* argc itself */
170 if ((unsigned long)csp & 15UL) {
171 sp -= (16UL - ((unsigned long)csp & 15UL)) / sizeof(*sp);
175 * Put the ELF interpreter info on the stack
177 #define NEW_AUX_ENT(nr, id, val) \
178 __put_user ((id), sp+(nr*2)); \
179 __put_user ((val), sp+(nr*2+1)); \
182 NEW_AUX_ENT(0, AT_NULL, 0);
187 NEW_AUX_ENT (0, AT_PHDR, load_addr + exec->e_phoff);
188 NEW_AUX_ENT (1, AT_PHENT, sizeof (struct elf_phdr));
189 NEW_AUX_ENT (2, AT_PHNUM, exec->e_phnum);
190 NEW_AUX_ENT (3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
191 NEW_AUX_ENT (4, AT_BASE, interp_load_addr);
192 NEW_AUX_ENT (5, AT_FLAGS, 0);
193 NEW_AUX_ENT (6, AT_ENTRY, (elf_addr_t) exec->e_entry);
194 NEW_AUX_ENT (7, AT_UID, (elf_addr_t) current->uid);
195 NEW_AUX_ENT (8, AT_EUID, (elf_addr_t) current->euid);
196 NEW_AUX_ENT (9, AT_GID, (elf_addr_t) current->gid);
197 NEW_AUX_ENT (10, AT_EGID, (elf_addr_t) current->egid);
206 __put_user((elf_addr_t)argc,--sp);
207 current->mm->arg_start = (unsigned long) p;
209 __put_user((unsigned long)p,argv++);
212 __put_user(NULL, argv);
213 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
215 __put_user((unsigned long)p,envp++);
218 __put_user(NULL, envp);
219 current->mm->env_end = (unsigned long) p;
224 /* This is much more generalized than the library routine read function,
225 * so we keep this separate. Technically the library read function
226 * is only provided so that we can read a.out libraries that have
229 static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
230 struct file * interpreter,
231 unsigned int *interp_load_addr)
233 struct elf_phdr *elf_phdata = NULL;
234 struct elf_phdr *eppnt;
236 unsigned int load_addr;
239 unsigned int last_bss;
246 error = load_addr = 0;
249 print_elfhdr(interp_elf_ex);
252 /* First of all, some simple consistency checks */
253 if ((interp_elf_ex->e_type != ET_EXEC &&
254 interp_elf_ex->e_type != ET_DYN) ||
255 !irix_elf_check_arch(interp_elf_ex) ||
256 !interpreter->f_op->mmap) {
257 printk("IRIX interp has bad e_type %d\n", interp_elf_ex->e_type);
261 /* Now read in all of the header information */
262 if(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > PAGE_SIZE) {
263 printk("IRIX interp header bigger than a page (%d)\n",
264 (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum));
268 elf_phdata = kmalloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum,
272 printk("Cannot kmalloc phdata for IRIX interp.\n");
276 /* If the size of this structure has changed, then punt, since
277 * we will be doing the wrong thing.
279 if(interp_elf_ex->e_phentsize != 32) {
280 printk("IRIX interp e_phentsize == %d != 32 ",
281 interp_elf_ex->e_phentsize);
286 retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
288 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
291 dump_phdrs(elf_phdata, interp_elf_ex->e_phnum);
295 for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
296 if(eppnt->p_type == PT_LOAD) {
297 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
299 unsigned long vaddr = 0;
300 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
301 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
302 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
303 elf_type |= MAP_FIXED;
304 vaddr = eppnt->p_vaddr;
307 printk("INTERP do_mmap(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
309 (unsigned long) (eppnt->p_filesz + (eppnt->p_vaddr & 0xfff)),
310 (unsigned long) elf_prot, (unsigned long) elf_type,
311 (unsigned long) (eppnt->p_offset & 0xfffff000));
313 down_write(¤t->mm->mmap_sem);
314 error = do_mmap(interpreter, vaddr,
315 eppnt->p_filesz + (eppnt->p_vaddr & 0xfff),
317 eppnt->p_offset & 0xfffff000);
318 up_write(¤t->mm->mmap_sem);
320 if(error < 0 && error > -1024) {
321 printk("Aieee IRIX interp mmap error=%d\n", error);
322 break; /* Real error */
325 printk("error=%08lx ", (unsigned long) error);
327 if(!load_addr && interp_elf_ex->e_type == ET_DYN) {
330 printk("load_addr = error ");
334 /* Find the end of the file mapping for this phdr, and keep
335 * track of the largest address we see for this.
337 k = eppnt->p_vaddr + eppnt->p_filesz;
338 if(k > elf_bss) elf_bss = k;
340 /* Do the same thing for the memory mapping - between
341 * elf_bss and last_bss is the bss section.
343 k = eppnt->p_memsz + eppnt->p_vaddr;
344 if(k > last_bss) last_bss = k;
351 /* Now use mmap to map the library into memory. */
352 if(error < 0 && error > -1024) {
354 printk("got error %d\n", error);
360 /* Now fill out the bss section. First pad the last page up
361 * to the page boundary, and then perform a mmap to make sure
362 * that there are zero-mapped pages up to and including the
366 printk("padzero(%08lx) ", (unsigned long) (elf_bss));
369 len = (elf_bss + 0xfff) & 0xfffff000; /* What we have mapped so far */
372 printk("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss,
373 (unsigned long) len);
376 /* Map the last of the bss segment */
377 if (last_bss > len) {
378 do_brk(len, (last_bss - len));
382 *interp_load_addr = load_addr;
383 return ((unsigned int) interp_elf_ex->e_entry);
386 /* Check sanity of IRIX elf executable header. */
387 static int verify_binary(struct elfhdr *ehp, struct linux_binprm *bprm)
389 if (memcmp(ehp->e_ident, ELFMAG, SELFMAG) != 0)
392 /* First of all, some simple consistency checks */
393 if((ehp->e_type != ET_EXEC && ehp->e_type != ET_DYN) ||
394 !irix_elf_check_arch(ehp) || !bprm->file->f_op->mmap) {
398 /* Only support MIPS ARCH2 or greater IRIX binaries for now. */
399 if(!(ehp->e_flags & EF_MIPS_ARCH) && !(ehp->e_flags & 0x04)) {
403 /* XXX Don't support N32 or 64bit binaries yet because they can
404 * XXX and do execute 64 bit instructions and expect all registers
405 * XXX to be 64 bit as well. We need to make the kernel save
406 * XXX all registers as 64bits on cpu's capable of this at
407 * XXX exception time plus frob the XTLB exception vector.
409 if((ehp->e_flags & 0x20)) {
413 return 0; /* It's ok. */
416 #define IRIX_INTERP_PREFIX "/usr/gnemul/irix"
418 /* Look for an IRIX ELF interpreter. */
419 static inline int look_for_irix_interpreter(char **name,
420 struct file **interpreter,
421 struct elfhdr *interp_elf_ex,
422 struct elf_phdr *epp,
423 struct linux_binprm *bprm, int pnum)
426 int retval = -EINVAL;
427 struct file *file = NULL;
430 for(i = 0; i < pnum; i++, epp++) {
431 if (epp->p_type != PT_INTERP)
434 /* It is illegal to have two interpreters for one executable. */
438 *name = kmalloc((epp->p_filesz + strlen(IRIX_INTERP_PREFIX)),
443 strcpy(*name, IRIX_INTERP_PREFIX);
444 retval = kernel_read(bprm->file, epp->p_offset, (*name + 16),
449 file = open_exec(*name);
451 retval = PTR_ERR(file);
454 retval = kernel_read(file, 0, bprm->buf, 128);
458 *interp_elf_ex = *(struct elfhdr *) bprm->buf;
470 static inline int verify_irix_interpreter(struct elfhdr *ihp)
472 if (memcmp(ihp->e_ident, ELFMAG, SELFMAG) != 0)
477 #define EXEC_MAP_FLAGS (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE)
479 static inline void map_executable(struct file *fp, struct elf_phdr *epp, int pnum,
480 unsigned int *estack, unsigned int *laddr,
481 unsigned int *scode, unsigned int *ebss,
482 unsigned int *ecode, unsigned int *edata,
488 for(i = 0; i < pnum; i++, epp++) {
489 if(epp->p_type != PT_LOAD)
493 prot = (epp->p_flags & PF_R) ? PROT_READ : 0;
494 prot |= (epp->p_flags & PF_W) ? PROT_WRITE : 0;
495 prot |= (epp->p_flags & PF_X) ? PROT_EXEC : 0;
496 down_write(¤t->mm->mmap_sem);
497 (void) do_mmap(fp, (epp->p_vaddr & 0xfffff000),
498 (epp->p_filesz + (epp->p_vaddr & 0xfff)),
499 prot, EXEC_MAP_FLAGS,
500 (epp->p_offset & 0xfffff000));
501 up_write(¤t->mm->mmap_sem);
503 /* Fixup location tracking vars. */
504 if((epp->p_vaddr & 0xfffff000) < *estack)
505 *estack = (epp->p_vaddr & 0xfffff000);
507 *laddr = epp->p_vaddr - epp->p_offset;
508 if(epp->p_vaddr < *scode)
509 *scode = epp->p_vaddr;
511 tmp = epp->p_vaddr + epp->p_filesz;
514 if((epp->p_flags & PF_X) && *ecode < tmp)
519 tmp = epp->p_vaddr + epp->p_memsz;
526 static inline int map_interpreter(struct elf_phdr *epp, struct elfhdr *ihp,
527 struct file *interp, unsigned int *iladdr,
528 int pnum, mm_segment_t old_fs,
529 unsigned int *eentry)
533 *eentry = 0xffffffff;
534 for(i = 0; i < pnum; i++, epp++) {
535 if(epp->p_type != PT_INTERP)
538 /* We should have fielded this error elsewhere... */
539 if(*eentry != 0xffffffff)
543 *eentry = load_irix_interp(ihp, interp, iladdr);
549 if (*eentry == 0xffffffff)
556 * IRIX maps a page at 0x200000 that holds information about the
557 * process and the system, here we map the page and fill the
560 void irix_map_prda_page (void)
565 v = do_brk (PRDA_ADDRESS, PAGE_SIZE);
570 pp = (struct prda *) v;
571 pp->prda_sys.t_pid = current->pid;
572 pp->prda_sys.t_prid = read_c0_prid();
573 pp->prda_sys.t_rpid = current->pid;
575 /* We leave the rest set to zero */
580 /* These are the functions used to load ELF style executables and shared
581 * libraries. There is no binary dependent code anywhere else.
583 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
585 struct elfhdr elf_ex, interp_elf_ex;
586 struct file *interpreter;
587 struct elf_phdr *elf_phdata, *elf_ihdr, *elf_ephdr;
588 unsigned int load_addr, elf_bss, elf_brk;
589 unsigned int elf_entry, interp_load_addr = 0;
590 unsigned int start_code, end_code, end_data, elf_stack;
591 int retval, has_interp, has_ephdr, size, i;
592 char *elf_interpreter;
596 has_interp = has_ephdr = 0;
597 elf_ihdr = elf_ephdr = 0;
598 elf_ex = *((struct elfhdr *) bprm->buf);
601 if (verify_binary(&elf_ex, bprm))
605 print_elfhdr(&elf_ex);
608 /* Now read in all of the header information */
609 size = elf_ex.e_phentsize * elf_ex.e_phnum;
612 elf_phdata = kmalloc(size, GFP_KERNEL);
613 if (elf_phdata == NULL) {
618 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *)elf_phdata, size);
623 dump_phdrs(elf_phdata, elf_ex.e_phnum);
626 /* Set some things for later. */
627 for(i = 0; i < elf_ex.e_phnum; i++) {
628 switch(elf_phdata[i].p_type) {
631 elf_ihdr = &elf_phdata[i];
635 elf_ephdr = &elf_phdata[i];
646 elf_stack = 0xffffffff;
647 elf_interpreter = NULL;
648 start_code = 0xffffffff;
652 retval = look_for_irix_interpreter(&elf_interpreter,
654 &interp_elf_ex, elf_phdata, bprm,
659 if (elf_interpreter) {
660 retval = verify_irix_interpreter(&interp_elf_ex);
662 goto out_free_interp;
665 /* OK, we are done with that, now set up the arg stuff,
666 * and then start this sucker up.
669 if (!bprm->sh_bang && !bprm->p)
670 goto out_free_interp;
672 /* Flush all traces of the currently running executable */
673 retval = flush_old_exec(bprm);
675 goto out_free_dentry;
677 /* OK, This is the point of no return */
678 current->mm->end_data = 0;
679 current->mm->end_code = 0;
680 current->mm->mmap = NULL;
681 current->flags &= ~PF_FORKNOEXEC;
682 elf_entry = (unsigned int) elf_ex.e_entry;
684 /* Do this so that we can load the interpreter, if need be. We will
685 * change some of these later.
687 current->mm->rss = 0;
688 setup_arg_pages(bprm, EXSTACK_DEFAULT);
689 current->mm->start_stack = bprm->p;
691 /* At this point, we assume that the image should be loaded at
692 * fixed address, not at a variable address.
697 map_executable(bprm->file, elf_phdata, elf_ex.e_phnum, &elf_stack,
698 &load_addr, &start_code, &elf_bss, &end_code,
699 &end_data, &elf_brk);
701 if(elf_interpreter) {
702 retval = map_interpreter(elf_phdata, &interp_elf_ex,
703 interpreter, &interp_load_addr,
704 elf_ex.e_phnum, old_fs, &elf_entry);
705 kfree(elf_interpreter);
708 printk("Unable to load IRIX ELF interpreter\n");
709 send_sig(SIGSEGV, current, 0);
718 set_personality(PER_IRIX32);
719 set_binfmt(&irix_format);
721 current->flags &= ~PF_FORKNOEXEC;
722 bprm->p = (unsigned long)
723 create_irix_tables((char *)bprm->p, bprm->argc, bprm->envc,
724 (elf_interpreter ? &elf_ex : NULL),
725 load_addr, interp_load_addr, regs, elf_ephdr);
726 current->mm->start_brk = current->mm->brk = elf_brk;
727 current->mm->end_code = end_code;
728 current->mm->start_code = start_code;
729 current->mm->end_data = end_data;
730 current->mm->start_stack = bprm->p;
732 /* Calling set_brk effectively mmaps the pages that we need for the
733 * bss and break sections.
735 set_brk(elf_bss, elf_brk);
738 * IRIX maps a page at 0x200000 which holds some system
739 * information. Programs depend on this.
741 irix_map_prda_page ();
746 printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
747 printk("(end_code) %lx\n" , (long) current->mm->end_code);
748 printk("(start_code) %lx\n" , (long) current->mm->start_code);
749 printk("(end_data) %lx\n" , (long) current->mm->end_data);
750 printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
751 printk("(brk) %lx\n" , (long) current->mm->brk);
754 #if 0 /* XXX No fucking way dude... */
755 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
756 * and some applications "depend" upon this behavior.
757 * Since we do not have the power to recompile these, we
758 * emulate the SVr4 behavior. Sigh.
760 down_write(¤t->mm->mmap_sem);
761 (void) do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
762 MAP_FIXED | MAP_PRIVATE, 0);
763 up_write(¤t->mm->mmap_sem);
766 start_thread(regs, elf_entry, bprm->p);
767 if (current->ptrace & PT_PTRACED)
768 send_sig(SIGTRAP, current, 0);
774 allow_write_access(interpreter);
778 kfree(elf_interpreter);
785 /* This is really simpleminded and specialized - we are loading an
786 * a.out library that is given an ELF header.
788 static int load_irix_library(struct file *file)
790 struct elfhdr elf_ex;
791 struct elf_phdr *elf_phdata = NULL;
792 unsigned int len = 0;
799 error = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
800 if (error != sizeof(elf_ex))
803 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
806 /* First of all, some simple consistency checks. */
807 if(elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
808 !irix_elf_check_arch(&elf_ex) || !file->f_op->mmap)
811 /* Now read in all of the header information. */
812 if(sizeof(struct elf_phdr) * elf_ex.e_phnum > PAGE_SIZE)
815 elf_phdata = kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
816 if (elf_phdata == NULL)
819 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata,
820 sizeof(struct elf_phdr) * elf_ex.e_phnum);
823 for(i=0; i<elf_ex.e_phnum; i++)
824 if((elf_phdata + i)->p_type == PT_LOAD) j++;
831 while(elf_phdata->p_type != PT_LOAD) elf_phdata++;
833 /* Now use mmap to map the library into memory. */
834 down_write(¤t->mm->mmap_sem);
835 error = do_mmap(file,
836 elf_phdata->p_vaddr & 0xfffff000,
837 elf_phdata->p_filesz + (elf_phdata->p_vaddr & 0xfff),
838 PROT_READ | PROT_WRITE | PROT_EXEC,
839 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
840 elf_phdata->p_offset & 0xfffff000);
841 up_write(¤t->mm->mmap_sem);
843 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
844 if (k > elf_bss) elf_bss = k;
846 if (error != (elf_phdata->p_vaddr & 0xfffff000)) {
853 len = (elf_phdata->p_filesz + elf_phdata->p_vaddr+ 0xfff) & 0xfffff000;
854 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
856 do_brk(len, bss-len);
861 /* Called through irix_syssgi() to map an elf image given an FD,
862 * a phdr ptr USER_PHDRP in userspace, and a count CNT telling how many
863 * phdrs there are in the USER_PHDRP array. We return the vaddr the
864 * first phdr was successfully mapped to.
866 unsigned long irix_mapelf(int fd, struct elf_phdr *user_phdrp, int cnt)
873 printk("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
874 fd, user_phdrp, cnt);
877 /* First get the verification out of the way. */
879 retval = verify_area(VERIFY_READ, hp, (sizeof(struct elf_phdr) * cnt));
882 printk("irix_mapelf: verify_area fails!\n");
888 dump_phdrs(user_phdrp, cnt);
891 for(i = 0; i < cnt; i++, hp++)
892 if(hp->p_type != PT_LOAD) {
893 printk("irix_mapelf: One section is not PT_LOAD!\n");
901 printk("irix_mapelf: Bogon filp!\n");
907 for(i = 0; i < cnt; i++, hp++) {
910 prot = (hp->p_flags & PF_R) ? PROT_READ : 0;
911 prot |= (hp->p_flags & PF_W) ? PROT_WRITE : 0;
912 prot |= (hp->p_flags & PF_X) ? PROT_EXEC : 0;
913 down_write(¤t->mm->mmap_sem);
914 retval = do_mmap(filp, (hp->p_vaddr & 0xfffff000),
915 (hp->p_filesz + (hp->p_vaddr & 0xfff)),
916 prot, (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
917 (hp->p_offset & 0xfffff000));
918 up_write(¤t->mm->mmap_sem);
920 if(retval != (hp->p_vaddr & 0xfffff000)) {
921 printk("irix_mapelf: do_mmap fails with %d!\n", retval);
928 printk("irix_mapelf: Success, returning %08lx\n", user_phdrp->p_vaddr);
931 return user_phdrp->p_vaddr;
937 * Modelled on fs/exec.c:aout_core_dump()
938 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
941 /* These are the only things you should do on a core-file: use only these
942 * functions to write out all the necessary info.
944 static int dump_write(struct file *file, const void *addr, int nr)
946 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
949 static int dump_seek(struct file *file, off_t off)
951 if (file->f_op->llseek) {
952 if (file->f_op->llseek(file, off, 0) != off)
959 /* Decide whether a segment is worth dumping; default is yes to be
960 * sure (missing info is worse than too much; etc).
961 * Personally I'd include everything, and use the coredump limit...
963 * I think we should skip something. But I am not sure how. H.J.
965 static inline int maydump(struct vm_area_struct *vma)
967 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
970 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
972 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
978 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
980 /* An ELF note in memory. */
989 static int notesize(struct memelfnote *en)
993 sz = sizeof(struct elf_note);
994 sz += roundup(strlen(en->name), 4);
995 sz += roundup(en->datasz, 4);
1002 #define DUMP_WRITE(addr, nr) \
1003 if (!dump_write(file, (addr), (nr))) \
1005 #define DUMP_SEEK(off) \
1006 if (!dump_seek(file, (off))) \
1009 static int writenote(struct memelfnote *men, struct file *file)
1013 en.n_namesz = strlen(men->name);
1014 en.n_descsz = men->datasz;
1015 en.n_type = men->type;
1017 DUMP_WRITE(&en, sizeof(en));
1018 DUMP_WRITE(men->name, en.n_namesz);
1019 /* XXX - cast from long long to long to avoid need for libgcc.a */
1020 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1021 DUMP_WRITE(men->data, men->datasz);
1022 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1032 #define DUMP_WRITE(addr, nr) \
1033 if (!dump_write(file, (addr), (nr))) \
1035 #define DUMP_SEEK(off) \
1036 if (!dump_seek(file, (off))) \
1041 * This is a two-pass process; first we find the offsets of the bits,
1042 * and then they are actually written out. If we run out of core limit
1045 static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
1052 struct vm_area_struct *vma;
1054 off_t offset = 0, dataoff;
1055 int limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1057 struct memelfnote notes[4];
1058 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1059 elf_fpregset_t fpu; /* NT_PRFPREG */
1060 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1062 /* Count what's needed to dump, up to the limit of coredump size. */
1065 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1068 int sz = vma->vm_end-vma->vm_start;
1070 if (size+sz >= limit)
1079 printk("irix_core_dump: %d segs taking %d bytes\n", segs, size);
1082 /* Set up header. */
1083 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1084 elf.e_ident[EI_CLASS] = ELFCLASS32;
1085 elf.e_ident[EI_DATA] = ELFDATA2LSB;
1086 elf.e_ident[EI_VERSION] = EV_CURRENT;
1087 elf.e_ident[EI_OSABI] = ELF_OSABI;
1088 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1090 elf.e_type = ET_CORE;
1091 elf.e_machine = ELF_ARCH;
1092 elf.e_version = EV_CURRENT;
1094 elf.e_phoff = sizeof(elf);
1097 elf.e_ehsize = sizeof(elf);
1098 elf.e_phentsize = sizeof(struct elf_phdr);
1099 elf.e_phnum = segs+1; /* Include notes. */
1100 elf.e_shentsize = 0;
1108 current->flags |= PF_DUMPCORE;
1110 DUMP_WRITE(&elf, sizeof(elf));
1111 offset += sizeof(elf); /* Elf header. */
1112 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers. */
1114 /* Set up the notes in similar form to SVR4 core dumps made
1115 * with info from their /proc.
1117 memset(&psinfo, 0, sizeof(psinfo));
1118 memset(&prstatus, 0, sizeof(prstatus));
1120 notes[0].name = "CORE";
1121 notes[0].type = NT_PRSTATUS;
1122 notes[0].datasz = sizeof(prstatus);
1123 notes[0].data = &prstatus;
1124 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1125 prstatus.pr_sigpend = current->pending.signal.sig[0];
1126 prstatus.pr_sighold = current->blocked.sig[0];
1127 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1128 psinfo.pr_ppid = prstatus.pr_ppid = current->parent->pid;
1129 psinfo.pr_pgrp = prstatus.pr_pgrp = process_group(current);
1130 psinfo.pr_sid = prstatus.pr_sid = current->signal->session;
1131 if (current->pid == current->tgid) {
1133 * This is the record for the group leader. Add in the
1134 * cumulative times of previous dead threads. This total
1135 * won't include the time of each live thread whose state
1136 * is included in the core dump. The final total reported
1137 * to our parent process when it calls wait4 will include
1138 * those sums as well as the little bit more time it takes
1139 * this and each other thread to finish dying after the
1140 * core dump synchronization phase.
1142 jiffies_to_timeval(current->utime + current->signal->utime,
1143 &prstatus.pr_utime);
1144 jiffies_to_timeval(current->stime + current->signal->stime,
1145 &prstatus.pr_stime);
1147 jiffies_to_timeval(current->utime, &prstatus.pr_utime);
1148 jiffies_to_timeval(current->stime, &prstatus.pr_stime);
1150 jiffies_to_timeval(current->signal->cutime, &prstatus.pr_cutime);
1151 jiffies_to_timeval(current->signal->cstime, &prstatus.pr_cstime);
1153 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs)) {
1154 printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
1155 "(%d)\n", sizeof(elf_gregset_t), sizeof(struct pt_regs));
1157 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1160 notes[1].name = "CORE";
1161 notes[1].type = NT_PRPSINFO;
1162 notes[1].datasz = sizeof(psinfo);
1163 notes[1].data = &psinfo;
1164 i = current->state ? ffz(~current->state) + 1 : 0;
1165 psinfo.pr_state = i;
1166 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1167 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1168 psinfo.pr_nice = task_nice(current);
1169 psinfo.pr_flag = current->flags;
1170 psinfo.pr_uid = current->uid;
1171 psinfo.pr_gid = current->gid;
1177 len = current->mm->arg_end - current->mm->arg_start;
1178 len = len >= ELF_PRARGSZ ? ELF_PRARGSZ : len;
1179 copy_from_user(&psinfo.pr_psargs,
1180 (const char *)current->mm->arg_start, len);
1181 for(i = 0; i < len; i++)
1182 if (psinfo.pr_psargs[i] == 0)
1183 psinfo.pr_psargs[i] = ' ';
1184 psinfo.pr_psargs[len] = 0;
1188 strlcpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1190 notes[2].name = "CORE";
1191 notes[2].type = NT_TASKSTRUCT;
1192 notes[2].datasz = sizeof(*current);
1193 notes[2].data = current;
1195 /* Try to dump the FPU. */
1196 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1197 if (!prstatus.pr_fpvalid) {
1200 notes[3].name = "CORE";
1201 notes[3].type = NT_PRFPREG;
1202 notes[3].datasz = sizeof(fpu);
1203 notes[3].data = &fpu;
1206 /* Write notes phdr entry. */
1208 struct elf_phdr phdr;
1211 for(i = 0; i < numnote; i++)
1212 sz += notesize(¬es[i]);
1214 phdr.p_type = PT_NOTE;
1215 phdr.p_offset = offset;
1223 offset += phdr.p_filesz;
1224 DUMP_WRITE(&phdr, sizeof(phdr));
1227 /* Page-align dumped data. */
1228 dataoff = offset = roundup(offset, PAGE_SIZE);
1230 /* Write program headers for segments dump. */
1231 for(vma = current->mm->mmap, i = 0;
1232 i < segs && vma != NULL; vma = vma->vm_next) {
1233 struct elf_phdr phdr;
1238 sz = vma->vm_end - vma->vm_start;
1240 phdr.p_type = PT_LOAD;
1241 phdr.p_offset = offset;
1242 phdr.p_vaddr = vma->vm_start;
1244 phdr.p_filesz = maydump(vma) ? sz : 0;
1246 offset += phdr.p_filesz;
1247 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1248 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1249 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1250 phdr.p_align = PAGE_SIZE;
1252 DUMP_WRITE(&phdr, sizeof(phdr));
1255 for(i = 0; i < numnote; i++)
1256 if (!writenote(¬es[i], file))
1263 for(i = 0, vma = current->mm->mmap;
1264 i < segs && vma != NULL;
1265 vma = vma->vm_next) {
1266 unsigned long addr = vma->vm_start;
1267 unsigned long len = vma->vm_end - vma->vm_start;
1273 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1275 DUMP_WRITE((void *)addr, len);
1278 if ((off_t) file->f_pos != offset) {
1280 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1281 (off_t) file->f_pos, offset);
1289 static int __init init_irix_binfmt(void)
1291 return register_binfmt(&irix_format);
1294 static void __exit exit_irix_binfmt(void)
1296 /* Remove the IRIX ELF loaders. */
1297 unregister_binfmt(&irix_format);
1300 module_init(init_irix_binfmt)
1301 module_exit(exit_irix_binfmt)