1 /* $Id: irixelf.c,v 1.26 2000/03/07 15:45:28 ralf Exp $
3 * irixelf.c: Code to load IRIX ELF executables which conform to
6 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
8 * Based upon work which is:
9 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
12 #include <linux/module.h>
15 #include <linux/stat.h>
16 #include <linux/sched.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/malloc.h>
29 #include <linux/shm.h>
30 #include <linux/personality.h>
31 #include <linux/elfcore.h>
32 #include <linux/smp_lock.h>
34 #include <asm/uaccess.h>
35 #include <asm/pgalloc.h>
36 #include <asm/mipsregs.h>
37 #include <asm/prctl.h>
39 #define DLINFO_ITEMS 12
41 #include <linux/elf.h>
45 static int load_irix_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
);
46 static int load_irix_library(int fd
);
47 static int irix_core_dump(long signr
, struct pt_regs
* regs
,
49 extern int dump_fpu (elf_fpregset_t
*);
51 static struct linux_binfmt irix_format
= {
52 NULL
, THIS_MODULE
, load_irix_binary
, load_irix_library
,
53 irix_core_dump
, PAGE_SIZE
57 #define elf_addr_t unsigned long
58 #define elf_caddr_t char *
62 /* Debugging routines. */
63 static char *get_elf_p_type(Elf32_Word p_type
)
68 case PT_NULL
: return("PT_NULL"); break;
69 case PT_LOAD
: return("PT_LOAD"); break;
70 case PT_DYNAMIC
: return("PT_DYNAMIC"); break;
71 case PT_INTERP
: return("PT_INTERP"); break;
72 case PT_NOTE
: return("PT_NOTE"); break;
73 case PT_SHLIB
: return("PT_SHLIB"); break;
74 case PT_PHDR
: return("PT_PHDR"); break;
75 case PT_LOPROC
: return("PT_LOPROC/REGINFO"); break;
76 case PT_HIPROC
: return("PT_HIPROC"); break;
77 default: return("PT_BOGUS"); break;
81 static void print_elfhdr(struct elfhdr
*ehp
)
85 printk("ELFHDR: e_ident<");
86 for(i
= 0; i
< (EI_NIDENT
- 1); i
++) printk("%x ", ehp
->e_ident
[i
]);
87 printk("%x>\n", ehp
->e_ident
[i
]);
88 printk(" e_type[%04x] e_machine[%04x] e_version[%08lx]\n",
89 (unsigned short) ehp
->e_type
, (unsigned short) ehp
->e_machine
,
90 (unsigned long) ehp
->e_version
);
91 printk(" e_entry[%08lx] e_phoff[%08lx] e_shoff[%08lx] "
93 (unsigned long) ehp
->e_entry
, (unsigned long) ehp
->e_phoff
,
94 (unsigned long) ehp
->e_shoff
, (unsigned long) ehp
->e_flags
);
95 printk(" e_ehsize[%04x] e_phentsize[%04x] e_phnum[%04x]\n",
96 (unsigned short) ehp
->e_ehsize
, (unsigned short) ehp
->e_phentsize
,
97 (unsigned short) ehp
->e_phnum
);
98 printk(" e_shentsize[%04x] e_shnum[%04x] e_shstrndx[%04x]\n",
99 (unsigned short) ehp
->e_shentsize
, (unsigned short) ehp
->e_shnum
,
100 (unsigned short) ehp
->e_shstrndx
);
103 static void print_phdr(int i
, struct elf_phdr
*ep
)
105 printk("PHDR[%d]: p_type[%s] p_offset[%08lx] p_vaddr[%08lx] "
106 "p_paddr[%08lx]\n", i
, get_elf_p_type(ep
->p_type
),
107 (unsigned long) ep
->p_offset
, (unsigned long) ep
->p_vaddr
,
108 (unsigned long) ep
->p_paddr
);
109 printk(" p_filesz[%08lx] p_memsz[%08lx] p_flags[%08lx] "
110 "p_align[%08lx]\n", (unsigned long) ep
->p_filesz
,
111 (unsigned long) ep
->p_memsz
, (unsigned long) ep
->p_flags
,
112 (unsigned long) ep
->p_align
);
115 static void dump_phdrs(struct elf_phdr
*ep
, int pnum
)
119 for(i
= 0; i
< pnum
; i
++, ep
++) {
120 if((ep
->p_type
== PT_LOAD
) ||
121 (ep
->p_type
== PT_INTERP
) ||
122 (ep
->p_type
== PT_PHDR
))
126 #endif /* (DEBUG_ELF) */
128 static void set_brk(unsigned long start
, unsigned long end
)
130 start
= PAGE_ALIGN(start
);
131 end
= PAGE_ALIGN(end
);
134 do_brk(start
, end
- start
);
138 /* We need to explicitly zero any fractional pages
139 * after the data section (i.e. bss). This would
140 * contain the junk from the file that should not
143 static void padzero(unsigned long elf_bss
)
147 nbyte
= elf_bss
& (PAGE_SIZE
-1);
149 nbyte
= PAGE_SIZE
- nbyte
;
150 clear_user((void *) elf_bss
, nbyte
);
154 unsigned long * create_irix_tables(char * p
, int argc
, int envc
,
155 struct elfhdr
* exec
, unsigned int load_addr
,
156 unsigned int interp_load_addr
,
157 struct pt_regs
*regs
, struct elf_phdr
*ephdr
)
161 elf_addr_t
*sp
, *csp
;
164 printk("create_irix_tables: p[%p] argc[%d] envc[%d] "
165 "load_addr[%08x] interp_load_addr[%08x]\n",
166 p
, argc
, envc
, load_addr
, interp_load_addr
);
168 sp
= (elf_addr_t
*) (~15UL & (unsigned long) p
);
170 csp
-= exec
? DLINFO_ITEMS
*2 : 2;
173 csp
-= 1; /* argc itself */
174 if ((unsigned long)csp
& 15UL) {
175 sp
-= (16UL - ((unsigned long)csp
& 15UL)) / sizeof(*sp
);
179 * Put the ELF interpreter info on the stack
181 #define NEW_AUX_ENT(nr, id, val) \
182 __put_user ((id), sp+(nr*2)); \
183 __put_user ((val), sp+(nr*2+1)); \
186 NEW_AUX_ENT(0, AT_NULL
, 0);
191 NEW_AUX_ENT (0, AT_PHDR
, load_addr
+ exec
->e_phoff
);
192 NEW_AUX_ENT (1, AT_PHENT
, sizeof (struct elf_phdr
));
193 NEW_AUX_ENT (2, AT_PHNUM
, exec
->e_phnum
);
194 NEW_AUX_ENT (3, AT_PAGESZ
, ELF_EXEC_PAGESIZE
);
195 NEW_AUX_ENT (4, AT_BASE
, interp_load_addr
);
196 NEW_AUX_ENT (5, AT_FLAGS
, 0);
197 NEW_AUX_ENT (6, AT_ENTRY
, (elf_addr_t
) exec
->e_entry
);
198 NEW_AUX_ENT (7, AT_UID
, (elf_addr_t
) current
->uid
);
199 NEW_AUX_ENT (8, AT_EUID
, (elf_addr_t
) current
->euid
);
200 NEW_AUX_ENT (9, AT_GID
, (elf_addr_t
) current
->gid
);
201 NEW_AUX_ENT (10, AT_EGID
, (elf_addr_t
) current
->egid
);
206 envp
= (elf_caddr_t
*) sp
;
208 argv
= (elf_caddr_t
*) sp
;
210 __put_user((elf_addr_t
)argc
,--sp
);
211 current
->mm
->arg_start
= (unsigned long) p
;
213 __put_user((elf_caddr_t
)(unsigned long)p
,argv
++);
216 __put_user(NULL
, argv
);
217 current
->mm
->arg_end
= current
->mm
->env_start
= (unsigned long) p
;
219 __put_user((elf_caddr_t
)(unsigned long)p
,envp
++);
222 __put_user(NULL
, envp
);
223 current
->mm
->env_end
= (unsigned long) p
;
228 /* This is much more generalized than the library routine read function,
229 * so we keep this separate. Technically the library read function
230 * is only provided so that we can read a.out libraries that have
233 static unsigned int load_irix_interp(struct elfhdr
* interp_elf_ex
,
234 struct dentry
* interpreter_dentry
,
235 unsigned int *interp_load_addr
)
238 struct elf_phdr
*elf_phdata
= NULL
;
239 struct elf_phdr
*eppnt
;
241 unsigned int load_addr
;
245 unsigned int last_bss
;
252 error
= load_addr
= 0;
255 print_elfhdr(interp_elf_ex
);
258 /* First of all, some simple consistency checks */
259 if ((interp_elf_ex
->e_type
!= ET_EXEC
&&
260 interp_elf_ex
->e_type
!= ET_DYN
) ||
261 !elf_check_arch(interp_elf_ex
->e_machine
) ||
262 (!interpreter_dentry
->d_inode
->i_fop
||
263 !interpreter_dentry
->d_inode
->i_fop
->mmap
)) {
264 printk("IRIX interp has bad e_type %d\n", interp_elf_ex
->e_type
);
268 /* Now read in all of the header information */
269 if(sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
> PAGE_SIZE
) {
270 printk("IRIX interp header bigger than a page (%d)\n",
271 (sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
));
275 elf_phdata
= (struct elf_phdr
*)
276 kmalloc(sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
,
280 printk("Cannot kmalloc phdata for IRIX interp.\n");
284 /* If the size of this structure has changed, then punt, since
285 * we will be doing the wrong thing.
287 if(interp_elf_ex
->e_phentsize
!= 32) {
288 printk("IRIX interp e_phentsize == %d != 32 ",
289 interp_elf_ex
->e_phentsize
);
294 retval
= read_exec(interpreter_dentry
, interp_elf_ex
->e_phoff
,
296 sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
, 1);
299 dump_phdrs(elf_phdata
, interp_elf_ex
->e_phnum
);
302 elf_exec_fileno
= open_dentry(interpreter_dentry
, O_RDONLY
);
303 if (elf_exec_fileno
< 0) {
304 printk("Could not open IRIX interp inode.\n");
309 file
= fget(elf_exec_fileno
);
312 for(i
=0; i
<interp_elf_ex
->e_phnum
; i
++, eppnt
++) {
313 if(eppnt
->p_type
== PT_LOAD
) {
314 int elf_type
= MAP_PRIVATE
| MAP_DENYWRITE
;
316 unsigned long vaddr
= 0;
317 if (eppnt
->p_flags
& PF_R
) elf_prot
= PROT_READ
;
318 if (eppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
319 if (eppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
320 elf_type
|= MAP_FIXED
;
321 vaddr
= eppnt
->p_vaddr
;
324 printk("INTERP do_mmap(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
326 (unsigned long) (eppnt
->p_filesz
+ (eppnt
->p_vaddr
& 0xfff)),
327 (unsigned long) elf_prot
, (unsigned long) elf_type
,
328 (unsigned long) (eppnt
->p_offset
& 0xfffff000));
330 error
= do_mmap(file
, vaddr
,
331 eppnt
->p_filesz
+ (eppnt
->p_vaddr
& 0xfff),
333 eppnt
->p_offset
& 0xfffff000);
335 if(error
< 0 && error
> -1024) {
336 printk("Aieee IRIX interp mmap error=%d\n", error
);
337 break; /* Real error */
340 printk("error=%08lx ", (unsigned long) error
);
342 if(!load_addr
&& interp_elf_ex
->e_type
== ET_DYN
) {
345 printk("load_addr = error ");
349 /* Find the end of the file mapping for this phdr, and keep
350 * track of the largest address we see for this.
352 k
= eppnt
->p_vaddr
+ eppnt
->p_filesz
;
353 if(k
> elf_bss
) elf_bss
= k
;
355 /* Do the same thing for the memory mapping - between
356 * elf_bss and last_bss is the bss section.
358 k
= eppnt
->p_memsz
+ eppnt
->p_vaddr
;
359 if(k
> last_bss
) last_bss
= k
;
366 /* Now use mmap to map the library into memory. */
368 sys_close(elf_exec_fileno
);
369 if(error
< 0 && error
> -1024) {
371 printk("got error %d\n", error
);
377 /* Now fill out the bss section. First pad the last page up
378 * to the page boundary, and then perform a mmap to make sure
379 * that there are zero-mapped pages up to and including the
383 printk("padzero(%08lx) ", (unsigned long) (elf_bss
));
386 len
= (elf_bss
+ 0xfff) & 0xfffff000; /* What we have mapped so far */
389 printk("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss
,
390 (unsigned long) len
);
393 /* Map the last of the bss segment */
394 if (last_bss
> len
) {
395 do_brk(len
, (last_bss
- len
));
399 *interp_load_addr
= load_addr
;
400 return ((unsigned int) interp_elf_ex
->e_entry
);
403 /* Check sanity of IRIX elf executable header. */
404 static int verify_binary(struct elfhdr
*ehp
, struct linux_binprm
*bprm
)
406 if (memcmp(ehp
->e_ident
, ELFMAG
, SELFMAG
) != 0)
409 /* First of all, some simple consistency checks */
410 if((ehp
->e_type
!= ET_EXEC
&& ehp
->e_type
!= ET_DYN
) ||
411 !elf_check_arch(ehp
->e_machine
) ||
412 (!bprm
->dentry
->d_inode
->i_fop
||
413 !bprm
->dentry
->d_inode
->i_fop
->mmap
)) {
417 /* Only support MIPS ARCH2 or greater IRIX binaries for now. */
418 if(!(ehp
->e_flags
& EF_MIPS_ARCH
) && !(ehp
->e_flags
& 0x04)) {
422 /* XXX Don't support N32 or 64bit binaries yet because they can
423 * XXX and do execute 64 bit instructions and expect all registers
424 * XXX to be 64 bit as well. We need to make the kernel save
425 * XXX all registers as 64bits on cpu's capable of this at
426 * XXX exception time plus frob the XTLB exception vector.
428 if((ehp
->e_flags
& 0x20)) {
432 return 0; /* It's ok. */
435 #define IRIX_INTERP_PREFIX "/usr/gnemul/irix"
437 /* Look for an IRIX ELF interpreter. */
438 static inline int look_for_irix_interpreter(char **name
,
439 struct dentry
**interpreter_dentry
,
440 struct elfhdr
*interp_elf_ex
,
441 struct elf_phdr
*epp
,
442 struct linux_binprm
*bprm
, int pnum
)
446 int retval
= -EINVAL
;
447 struct dentry
*dentry
= NULL
;
450 for(i
= 0; i
< pnum
; i
++, epp
++) {
451 if (epp
->p_type
!= PT_INTERP
)
454 /* It is illegal to have two interpreters for one executable. */
458 *name
= (char *) kmalloc((epp
->p_filesz
+
459 strlen(IRIX_INTERP_PREFIX
)),
464 strcpy(*name
, IRIX_INTERP_PREFIX
);
465 retval
= read_exec(bprm
->dentry
, epp
->p_offset
, (*name
+ 16),
470 old_fs
= get_fs(); set_fs(get_ds());
472 dentry
= namei(*name
);
475 if (IS_ERR(dentry
)) {
476 retval
= PTR_ERR(dentry
);
479 retval
= read_exec(dentry
, 0, bprm
->buf
, 128, 1);
483 *interp_elf_ex
= *((struct elfhdr
*) bprm
->buf
);
485 *interpreter_dentry
= dentry
;
497 static inline int verify_irix_interpreter(struct elfhdr
*ihp
)
499 if (memcmp(ihp
->e_ident
, ELFMAG
, SELFMAG
) != 0)
504 #define EXEC_MAP_FLAGS (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE)
506 static inline void map_executable(struct file
*fp
, struct elf_phdr
*epp
, int pnum
,
507 unsigned int *estack
, unsigned int *laddr
,
508 unsigned int *scode
, unsigned int *ebss
,
509 unsigned int *ecode
, unsigned int *edata
,
515 for(i
= 0; i
< pnum
; i
++, epp
++) {
516 if(epp
->p_type
!= PT_LOAD
)
520 prot
= (epp
->p_flags
& PF_R
) ? PROT_READ
: 0;
521 prot
|= (epp
->p_flags
& PF_W
) ? PROT_WRITE
: 0;
522 prot
|= (epp
->p_flags
& PF_X
) ? PROT_EXEC
: 0;
523 (void) do_mmap(fp
, (epp
->p_vaddr
& 0xfffff000),
524 (epp
->p_filesz
+ (epp
->p_vaddr
& 0xfff)),
525 prot
, EXEC_MAP_FLAGS
,
526 (epp
->p_offset
& 0xfffff000));
528 /* Fixup location tracking vars. */
529 if((epp
->p_vaddr
& 0xfffff000) < *estack
)
530 *estack
= (epp
->p_vaddr
& 0xfffff000);
532 *laddr
= epp
->p_vaddr
- epp
->p_offset
;
533 if(epp
->p_vaddr
< *scode
)
534 *scode
= epp
->p_vaddr
;
536 tmp
= epp
->p_vaddr
+ epp
->p_filesz
;
539 if((epp
->p_flags
& PF_X
) && *ecode
< tmp
)
544 tmp
= epp
->p_vaddr
+ epp
->p_memsz
;
551 static inline int map_interpreter(struct elf_phdr
*epp
, struct elfhdr
*ihp
,
552 struct dentry
*identry
, unsigned int *iladdr
,
553 int pnum
, mm_segment_t old_fs
,
554 unsigned int *eentry
)
558 *eentry
= 0xffffffff;
559 for(i
= 0; i
< pnum
; i
++, epp
++) {
560 if(epp
->p_type
!= PT_INTERP
)
563 /* We should have fielded this error elsewhere... */
564 if(*eentry
!= 0xffffffff)
568 *eentry
= load_irix_interp(ihp
, identry
, iladdr
);
576 if(*eentry
== 0xffffffff)
583 * IRIX maps a page at 0x200000 that holds information about the
584 * process and the system, here we map the page and fill the
587 void irix_map_prda_page (void)
592 v
= do_brk (PRDA_ADDRESS
, PAGE_SIZE
);
597 pp
= (struct prda
*) v
;
598 pp
->prda_sys
.t_pid
= current
->pid
;
599 pp
->prda_sys
.t_prid
= read_32bit_cp0_register (CP0_PRID
);
600 pp
->prda_sys
.t_rpid
= current
->pid
;
602 /* We leave the rest set to zero */
607 /* These are the functions used to load ELF style executables and shared
608 * libraries. There is no binary dependent code anywhere else.
610 static int load_irix_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
612 struct elfhdr elf_ex
, interp_elf_ex
;
613 struct dentry
*interpreter_dentry
;
614 struct elf_phdr
*elf_phdata
, *elf_ihdr
, *elf_ephdr
;
615 unsigned int load_addr
, elf_bss
, elf_brk
;
616 unsigned int elf_entry
, interp_load_addr
= 0;
617 unsigned int start_code
, end_code
, end_data
, elf_stack
;
618 int elf_exec_fileno
, retval
, has_interp
, has_ephdr
, size
, i
;
619 char *elf_interpreter
;
624 has_interp
= has_ephdr
= 0;
625 elf_ihdr
= elf_ephdr
= 0;
626 elf_ex
= *((struct elfhdr
*) bprm
->buf
);
629 if (verify_binary(&elf_ex
, bprm
))
633 print_elfhdr(&elf_ex
);
636 /* Now read in all of the header information */
637 size
= elf_ex
.e_phentsize
* elf_ex
.e_phnum
;
640 elf_phdata
= (struct elf_phdr
*) kmalloc(size
, GFP_KERNEL
);
641 if (elf_phdata
== NULL
) {
646 retval
= read_exec(bprm
->dentry
, elf_ex
.e_phoff
,
647 (char *) elf_phdata
, size
, 1);
652 dump_phdrs(elf_phdata
, elf_ex
.e_phnum
);
655 /* Set some things for later. */
656 for(i
= 0; i
< elf_ex
.e_phnum
; i
++) {
657 switch(elf_phdata
[i
].p_type
) {
660 elf_ihdr
= &elf_phdata
[i
];
664 elf_ephdr
= &elf_phdata
[i
];
674 retval
= open_dentry(bprm
->dentry
, O_RDONLY
);
677 file
= fget(elf_exec_fileno
= retval
);
679 elf_stack
= 0xffffffff;
680 elf_interpreter
= NULL
;
681 start_code
= 0xffffffff;
685 retval
= look_for_irix_interpreter(&elf_interpreter
,
687 &interp_elf_ex
, elf_phdata
, bprm
,
692 if (elf_interpreter
) {
693 retval
= verify_irix_interpreter(&interp_elf_ex
);
695 goto out_free_interp
;
698 /* OK, we are done with that, now set up the arg stuff,
699 * and then start this sucker up.
702 if (!bprm
->sh_bang
&& !bprm
->p
)
703 goto out_free_interp
;
705 /* Flush all traces of the currently running executable */
706 retval
= flush_old_exec(bprm
);
708 goto out_free_dentry
;
710 /* OK, This is the point of no return */
711 current
->mm
->end_data
= 0;
712 current
->mm
->end_code
= 0;
713 current
->mm
->mmap
= NULL
;
714 current
->flags
&= ~PF_FORKNOEXEC
;
715 elf_entry
= (unsigned int) elf_ex
.e_entry
;
717 /* Do this so that we can load the interpreter, if need be. We will
718 * change some of these later.
720 current
->mm
->rss
= 0;
721 setup_arg_pages(bprm
);
722 current
->mm
->start_stack
= bprm
->p
;
724 /* At this point, we assume that the image should be loaded at
725 * fixed address, not at a variable address.
730 map_executable(file
, elf_phdata
, elf_ex
.e_phnum
, &elf_stack
,
731 &load_addr
, &start_code
, &elf_bss
, &end_code
,
732 &end_data
, &elf_brk
);
734 if(elf_interpreter
) {
735 retval
= map_interpreter(elf_phdata
, &interp_elf_ex
,
736 interpreter_dentry
, &interp_load_addr
,
737 elf_ex
.e_phnum
, old_fs
, &elf_entry
);
738 kfree(elf_interpreter
);
741 printk("Unable to load IRIX ELF interpreter\n");
742 send_sig(SIGSEGV
, current
, 0);
752 sys_close(elf_exec_fileno
);
753 current
->personality
= PER_IRIX32
;
755 put_exec_domain(current
->exec_domain
);
756 if (current
->binfmt
&& current
->binfmt
->module
)
757 __MOD_DEC_USE_COUNT(current
->binfmt
->module
);
758 current
->exec_domain
= lookup_exec_domain(current
->personality
);
759 current
->binfmt
= &irix_format
;
760 if (current
->binfmt
&& current
->binfmt
->module
)
761 __MOD_INC_USE_COUNT(current
->binfmt
->module
);
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 ();
789 printk("(start_brk) %lx\n" , (long) current
->mm
->start_brk
);
790 printk("(end_code) %lx\n" , (long) current
->mm
->end_code
);
791 printk("(start_code) %lx\n" , (long) current
->mm
->start_code
);
792 printk("(end_data) %lx\n" , (long) current
->mm
->end_data
);
793 printk("(start_stack) %lx\n" , (long) current
->mm
->start_stack
);
794 printk("(brk) %lx\n" , (long) current
->mm
->brk
);
797 #if 0 /* XXX No fucking way dude... */
798 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
799 * and some applications "depend" upon this behavior.
800 * Since we do not have the power to recompile these, we
801 * emulate the SVr4 behavior. Sigh.
803 (void) do_mmap(NULL
, 0, 4096, PROT_READ
| PROT_EXEC
,
804 MAP_FIXED
| MAP_PRIVATE
, 0);
807 start_thread(regs
, elf_entry
, bprm
->p
);
808 if (current
->flags
& PF_PTRACED
)
809 send_sig(SIGTRAP
, current
, 0);
816 dput(interpreter_dentry
);
820 kfree(elf_interpreter
);
823 sys_close(elf_exec_fileno
);
829 /* This is really simpleminded and specialized - we are loading an
830 * a.out library that is given an ELF header.
832 static inline int do_load_irix_library(struct file
*file
)
834 struct elfhdr elf_ex
;
835 struct elf_phdr
*elf_phdata
= NULL
;
836 struct dentry
*dentry
;
848 dentry
= file
->f_dentry
;
849 inode
= dentry
->d_inode
;
852 /* Seek to the beginning of the file. */
853 if (file
->f_op
->llseek
) {
854 if ((error
= file
->f_op
->llseek(file
, 0, 0)) != 0)
860 error
= file
->f_op
->read(file
, (char *) &elf_ex
, sizeof(elf_ex
),
863 if (error
!= sizeof(elf_ex
))
866 if (memcmp(elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
869 /* First of all, some simple consistency checks. */
870 if(elf_ex
.e_type
!= ET_EXEC
|| elf_ex
.e_phnum
> 2 ||
871 !elf_check_arch(elf_ex
.e_machine
) ||
872 (!dentry
->d_inode
->i_fop
||
873 !dentry
->d_inode
->i_fop
->mmap
))
876 /* Now read in all of the header information. */
877 if(sizeof(struct elf_phdr
) * elf_ex
.e_phnum
> PAGE_SIZE
)
880 elf_phdata
= (struct elf_phdr
*)
881 kmalloc(sizeof(struct elf_phdr
) * elf_ex
.e_phnum
, GFP_KERNEL
);
882 if (elf_phdata
== NULL
)
885 retval
= read_exec(dentry
, elf_ex
.e_phoff
, (char *) elf_phdata
,
886 sizeof(struct elf_phdr
) * elf_ex
.e_phnum
, 1);
889 for(i
=0; i
<elf_ex
.e_phnum
; i
++)
890 if((elf_phdata
+ i
)->p_type
== PT_LOAD
) j
++;
897 while(elf_phdata
->p_type
!= PT_LOAD
) elf_phdata
++;
899 /* Now use mmap to map the library into memory. */
900 error
= do_mmap(file
,
901 elf_phdata
->p_vaddr
& 0xfffff000,
902 elf_phdata
->p_filesz
+ (elf_phdata
->p_vaddr
& 0xfff),
903 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
904 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
,
905 elf_phdata
->p_offset
& 0xfffff000);
907 k
= elf_phdata
->p_vaddr
+ elf_phdata
->p_filesz
;
908 if(k
> elf_bss
) elf_bss
= k
;
910 if (error
!= (elf_phdata
->p_vaddr
& 0xfffff000)) {
917 len
= (elf_phdata
->p_filesz
+ elf_phdata
->p_vaddr
+ 0xfff) & 0xfffff000;
918 bss
= elf_phdata
->p_memsz
+ elf_phdata
->p_vaddr
;
920 do_brk(len
, bss
-len
);
925 static int load_irix_library(int fd
)
927 int retval
= -EACCES
;
932 retval
= do_load_irix_library(file
);
938 /* Called through irix_syssgi() to map an elf image given an FD,
939 * a phdr ptr USER_PHDRP in userspace, and a count CNT telling how many
940 * phdrs there are in the USER_PHDRP array. We return the vaddr the
941 * first phdr was successfully mapped to.
943 unsigned long irix_mapelf(int fd
, struct elf_phdr
*user_phdrp
, int cnt
)
950 printk("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
951 fd
, user_phdrp
, cnt
);
954 /* First get the verification out of the way. */
956 retval
= verify_area(VERIFY_READ
, hp
, (sizeof(struct elf_phdr
) * cnt
));
959 printk("irix_mapelf: verify_area fails!\n");
965 dump_phdrs(user_phdrp
, cnt
);
968 for(i
= 0; i
< cnt
; i
++, hp
++)
969 if(hp
->p_type
!= PT_LOAD
) {
970 printk("irix_mapelf: One section is not PT_LOAD!\n");
978 printk("irix_mapelf: Bogon filp!\n");
984 for(i
= 0; i
< cnt
; i
++, hp
++) {
987 prot
= (hp
->p_flags
& PF_R
) ? PROT_READ
: 0;
988 prot
|= (hp
->p_flags
& PF_W
) ? PROT_WRITE
: 0;
989 prot
|= (hp
->p_flags
& PF_X
) ? PROT_EXEC
: 0;
990 retval
= do_mmap(filp
, (hp
->p_vaddr
& 0xfffff000),
991 (hp
->p_filesz
+ (hp
->p_vaddr
& 0xfff)),
992 prot
, (MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
),
993 (hp
->p_offset
& 0xfffff000));
995 if(retval
!= (hp
->p_vaddr
& 0xfffff000)) {
996 printk("irix_mapelf: do_mmap fails with %d!\n", retval
);
1003 printk("irix_mapelf: Success, returning %08lx\n", user_phdrp
->p_vaddr
);
1006 return user_phdrp
->p_vaddr
;
1012 * Modelled on fs/exec.c:aout_core_dump()
1013 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1016 /* These are the only things you should do on a core-file: use only these
1017 * functions to write out all the necessary info.
1019 static int dump_write(struct file
*file
, const void *addr
, int nr
)
1021 return file
->f_op
->write(file
, addr
, nr
, &file
->f_pos
) == nr
;
1024 static int dump_seek(struct file
*file
, off_t off
)
1026 if (file
->f_op
->llseek
) {
1027 if (file
->f_op
->llseek(file
, off
, 0) != off
)
1034 /* Decide whether a segment is worth dumping; default is yes to be
1035 * sure (missing info is worse than too much; etc).
1036 * Personally I'd include everything, and use the coredump limit...
1038 * I think we should skip something. But I am not sure how. H.J.
1040 static inline int maydump(struct vm_area_struct
*vma
)
1042 if (!(vma
->vm_flags
& (VM_READ
|VM_WRITE
|VM_EXEC
)))
1045 if (vma
->vm_flags
& (VM_WRITE
|VM_GROWSUP
|VM_GROWSDOWN
))
1047 if (vma
->vm_flags
& (VM_READ
|VM_EXEC
|VM_EXECUTABLE
|VM_SHARED
))
1053 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
1055 /* An ELF note in memory. */
1060 unsigned int datasz
;
1064 static int notesize(struct memelfnote
*en
)
1068 sz
= sizeof(struct elf_note
);
1069 sz
+= roundup(strlen(en
->name
), 4);
1070 sz
+= roundup(en
->datasz
, 4);
1077 #define DUMP_WRITE(addr, nr) \
1078 if (!dump_write(file, (addr), (nr))) \
1080 #define DUMP_SEEK(off) \
1081 if (!dump_seek(file, (off))) \
1084 static int writenote(struct memelfnote
*men
, struct file
*file
)
1088 en
.n_namesz
= strlen(men
->name
);
1089 en
.n_descsz
= men
->datasz
;
1090 en
.n_type
= men
->type
;
1092 DUMP_WRITE(&en
, sizeof(en
));
1093 DUMP_WRITE(men
->name
, en
.n_namesz
);
1094 /* XXX - cast from long long to long to avoid need for libgcc.a */
1095 DUMP_SEEK(roundup((unsigned long)file
->f_pos
, 4)); /* XXX */
1096 DUMP_WRITE(men
->data
, men
->datasz
);
1097 DUMP_SEEK(roundup((unsigned long)file
->f_pos
, 4)); /* XXX */
1107 #define DUMP_WRITE(addr, nr) \
1108 if (!dump_write(file, (addr), (nr))) \
1110 #define DUMP_SEEK(off) \
1111 if (!dump_seek(file, (off))) \
1116 * This is a two-pass process; first we find the offsets of the bits,
1117 * and then they are actually written out. If we run out of core limit
1120 static int irix_core_dump(long signr
, struct pt_regs
* regs
, struct file
*file
)
1127 struct vm_area_struct
*vma
;
1129 off_t offset
= 0, dataoff
;
1130 int limit
= current
->rlim
[RLIMIT_CORE
].rlim_cur
;
1132 struct memelfnote notes
[4];
1133 struct elf_prstatus prstatus
; /* NT_PRSTATUS */
1134 elf_fpregset_t fpu
; /* NT_PRFPREG */
1135 struct elf_prpsinfo psinfo
; /* NT_PRPSINFO */
1137 /* Count what's needed to dump, up to the limit of coredump size. */
1140 for(vma
= current
->mm
->mmap
; vma
!= NULL
; vma
= vma
->vm_next
) {
1143 int sz
= vma
->vm_end
-vma
->vm_start
;
1145 if (size
+sz
>= limit
)
1154 printk("irix_core_dump: %d segs taking %d bytes\n", segs
, size
);
1157 /* Set up header. */
1158 memcpy(elf
.e_ident
, ELFMAG
, SELFMAG
);
1159 elf
.e_ident
[EI_CLASS
] = ELFCLASS32
;
1160 elf
.e_ident
[EI_DATA
] = ELFDATA2LSB
;
1161 elf
.e_ident
[EI_VERSION
] = EV_CURRENT
;
1162 memset(elf
.e_ident
+EI_PAD
, 0, EI_NIDENT
-EI_PAD
);
1164 elf
.e_type
= ET_CORE
;
1165 elf
.e_machine
= ELF_ARCH
;
1166 elf
.e_version
= EV_CURRENT
;
1168 elf
.e_phoff
= sizeof(elf
);
1171 elf
.e_ehsize
= sizeof(elf
);
1172 elf
.e_phentsize
= sizeof(struct elf_phdr
);
1173 elf
.e_phnum
= segs
+1; /* Include notes. */
1174 elf
.e_shentsize
= 0;
1182 current
->flags
|= PF_DUMPCORE
;
1184 DUMP_WRITE(&elf
, sizeof(elf
));
1185 offset
+= sizeof(elf
); /* Elf header. */
1186 offset
+= (segs
+1) * sizeof(struct elf_phdr
); /* Program headers. */
1188 /* Set up the notes in similar form to SVR4 core dumps made
1189 * with info from their /proc.
1191 memset(&psinfo
, 0, sizeof(psinfo
));
1192 memset(&prstatus
, 0, sizeof(prstatus
));
1194 notes
[0].name
= "CORE";
1195 notes
[0].type
= NT_PRSTATUS
;
1196 notes
[0].datasz
= sizeof(prstatus
);
1197 notes
[0].data
= &prstatus
;
1198 prstatus
.pr_info
.si_signo
= prstatus
.pr_cursig
= signr
;
1199 prstatus
.pr_sigpend
= current
->signal
.sig
[0];
1200 prstatus
.pr_sighold
= current
->blocked
.sig
[0];
1201 psinfo
.pr_pid
= prstatus
.pr_pid
= current
->pid
;
1202 psinfo
.pr_ppid
= prstatus
.pr_ppid
= current
->p_pptr
->pid
;
1203 psinfo
.pr_pgrp
= prstatus
.pr_pgrp
= current
->pgrp
;
1204 psinfo
.pr_sid
= prstatus
.pr_sid
= current
->session
;
1205 prstatus
.pr_utime
.tv_sec
= CT_TO_SECS(current
->times
.tms_utime
);
1206 prstatus
.pr_utime
.tv_usec
= CT_TO_USECS(current
->times
.tms_utime
);
1207 prstatus
.pr_stime
.tv_sec
= CT_TO_SECS(current
->times
.tms_stime
);
1208 prstatus
.pr_stime
.tv_usec
= CT_TO_USECS(current
->times
.tms_stime
);
1209 prstatus
.pr_cutime
.tv_sec
= CT_TO_SECS(current
->times
.tms_cutime
);
1210 prstatus
.pr_cutime
.tv_usec
= CT_TO_USECS(current
->times
.tms_cutime
);
1211 prstatus
.pr_cstime
.tv_sec
= CT_TO_SECS(current
->times
.tms_cstime
);
1212 prstatus
.pr_cstime
.tv_usec
= CT_TO_USECS(current
->times
.tms_cstime
);
1213 if (sizeof(elf_gregset_t
) != sizeof(struct pt_regs
)) {
1214 printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
1215 "(%d)\n", sizeof(elf_gregset_t
), sizeof(struct pt_regs
));
1217 *(struct pt_regs
*)&prstatus
.pr_reg
= *regs
;
1220 notes
[1].name
= "CORE";
1221 notes
[1].type
= NT_PRPSINFO
;
1222 notes
[1].datasz
= sizeof(psinfo
);
1223 notes
[1].data
= &psinfo
;
1224 i
= current
->state
? ffz(~current
->state
) + 1 : 0;
1225 psinfo
.pr_state
= i
;
1226 psinfo
.pr_sname
= (i
< 0 || i
> 5) ? '.' : "RSDZTD"[i
];
1227 psinfo
.pr_zomb
= psinfo
.pr_sname
== 'Z';
1228 psinfo
.pr_nice
= current
->priority
-15;
1229 psinfo
.pr_flag
= current
->flags
;
1230 psinfo
.pr_uid
= current
->uid
;
1231 psinfo
.pr_gid
= current
->gid
;
1237 len
= current
->mm
->arg_end
- current
->mm
->arg_start
;
1238 len
= len
>= ELF_PRARGSZ
? ELF_PRARGSZ
: len
;
1239 copy_from_user(&psinfo
.pr_psargs
,
1240 (const char *)current
->mm
->arg_start
, len
);
1241 for(i
= 0; i
< len
; i
++)
1242 if (psinfo
.pr_psargs
[i
] == 0)
1243 psinfo
.pr_psargs
[i
] = ' ';
1244 psinfo
.pr_psargs
[len
] = 0;
1248 strncpy(psinfo
.pr_fname
, current
->comm
, sizeof(psinfo
.pr_fname
));
1250 notes
[2].name
= "CORE";
1251 notes
[2].type
= NT_TASKSTRUCT
;
1252 notes
[2].datasz
= sizeof(*current
);
1253 notes
[2].data
= current
;
1255 /* Try to dump the FPU. */
1256 prstatus
.pr_fpvalid
= dump_fpu (&fpu
);
1257 if (!prstatus
.pr_fpvalid
) {
1260 notes
[3].name
= "CORE";
1261 notes
[3].type
= NT_PRFPREG
;
1262 notes
[3].datasz
= sizeof(fpu
);
1263 notes
[3].data
= &fpu
;
1266 /* Write notes phdr entry. */
1268 struct elf_phdr phdr
;
1271 for(i
= 0; i
< numnote
; i
++)
1272 sz
+= notesize(¬es
[i
]);
1274 phdr
.p_type
= PT_NOTE
;
1275 phdr
.p_offset
= offset
;
1283 offset
+= phdr
.p_filesz
;
1284 DUMP_WRITE(&phdr
, sizeof(phdr
));
1287 /* Page-align dumped data. */
1288 dataoff
= offset
= roundup(offset
, PAGE_SIZE
);
1290 /* Write program headers for segments dump. */
1291 for(vma
= current
->mm
->mmap
, i
= 0;
1292 i
< segs
&& vma
!= NULL
; vma
= vma
->vm_next
) {
1293 struct elf_phdr phdr
;
1298 sz
= vma
->vm_end
- vma
->vm_start
;
1300 phdr
.p_type
= PT_LOAD
;
1301 phdr
.p_offset
= offset
;
1302 phdr
.p_vaddr
= vma
->vm_start
;
1304 phdr
.p_filesz
= maydump(vma
) ? sz
: 0;
1306 offset
+= phdr
.p_filesz
;
1307 phdr
.p_flags
= vma
->vm_flags
& VM_READ
? PF_R
: 0;
1308 if (vma
->vm_flags
& VM_WRITE
) phdr
.p_flags
|= PF_W
;
1309 if (vma
->vm_flags
& VM_EXEC
) phdr
.p_flags
|= PF_X
;
1310 phdr
.p_align
= PAGE_SIZE
;
1312 DUMP_WRITE(&phdr
, sizeof(phdr
));
1315 for(i
= 0; i
< numnote
; i
++)
1316 if (!writenote(¬es
[i
], file
))
1323 for(i
= 0, vma
= current
->mm
->mmap
;
1324 i
< segs
&& vma
!= NULL
;
1325 vma
= vma
->vm_next
) {
1326 unsigned long addr
= vma
->vm_start
;
1327 unsigned long len
= vma
->vm_end
- vma
->vm_start
;
1333 printk("elf_core_dump: writing %08lx %lx\n", addr
, len
);
1335 DUMP_WRITE((void *)addr
, len
);
1338 if ((off_t
) file
->f_pos
!= offset
) {
1340 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1341 (off_t
) file
->f_pos
, offset
);
1349 static int __init
init_irix_binfmt(void)
1351 return register_binfmt(&irix_format
);
1354 static void __exit
cleanup_module(void)
1356 /* Remove the IRIX ELF loaders. */
1357 unregister_binfmt(&irix_format
);
1360 module_init(init_irix_binfmt
)
1361 module_exit(exit_irix_binfmt
)