1 /* $Id: irixelf.c,v 1.17 1999/06/17 13:25:45 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/fcntl.h>
26 #include <linux/ptrace.h>
27 #include <linux/malloc.h>
28 #include <linux/shm.h>
29 #include <linux/personality.h>
30 #include <linux/elfcore.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgtable.h>
34 #include <asm/mipsregs.h>
35 #include <asm/prctl.h>
37 #include <linux/config.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
);
48 extern int dump_fpu (elf_fpregset_t
*);
50 static struct linux_binfmt irix_format
= {
55 &__this_module
.usecount
,
57 load_irix_binary
, load_irix_library
, irix_core_dump
, PAGE_SIZE
61 #define elf_addr_t unsigned long
62 #define elf_caddr_t char *
66 /* Debugging routines. */
67 static char *get_elf_p_type(Elf32_Word p_type
)
72 case PT_NULL
: return("PT_NULL"); break;
73 case PT_LOAD
: return("PT_LOAD"); break;
74 case PT_DYNAMIC
: return("PT_DYNAMIC"); break;
75 case PT_INTERP
: return("PT_INTERP"); break;
76 case PT_NOTE
: return("PT_NOTE"); break;
77 case PT_SHLIB
: return("PT_SHLIB"); break;
78 case PT_PHDR
: return("PT_PHDR"); break;
79 case PT_LOPROC
: return("PT_LOPROC/REGINFO"); break;
80 case PT_HIPROC
: return("PT_HIPROC"); break;
81 default: return("PT_BOGUS"); break;
85 static void print_elfhdr(struct elfhdr
*ehp
)
89 printk("ELFHDR: e_ident<");
90 for(i
= 0; i
< (EI_NIDENT
- 1); i
++) printk("%x ", ehp
->e_ident
[i
]);
91 printk("%x>\n", ehp
->e_ident
[i
]);
92 printk(" e_type[%04x] e_machine[%04x] e_version[%08lx]\n",
93 (unsigned short) ehp
->e_type
, (unsigned short) ehp
->e_machine
,
94 (unsigned long) ehp
->e_version
);
95 printk(" e_entry[%08lx] e_phoff[%08lx] e_shoff[%08lx] "
97 (unsigned long) ehp
->e_entry
, (unsigned long) ehp
->e_phoff
,
98 (unsigned long) ehp
->e_shoff
, (unsigned long) ehp
->e_flags
);
99 printk(" e_ehsize[%04x] e_phentsize[%04x] e_phnum[%04x]\n",
100 (unsigned short) ehp
->e_ehsize
, (unsigned short) ehp
->e_phentsize
,
101 (unsigned short) ehp
->e_phnum
);
102 printk(" e_shentsize[%04x] e_shnum[%04x] e_shstrndx[%04x]\n",
103 (unsigned short) ehp
->e_shentsize
, (unsigned short) ehp
->e_shnum
,
104 (unsigned short) ehp
->e_shstrndx
);
107 static void print_phdr(int i
, struct elf_phdr
*ep
)
109 printk("PHDR[%d]: p_type[%s] p_offset[%08lx] p_vaddr[%08lx] "
110 "p_paddr[%08lx]\n", i
, get_elf_p_type(ep
->p_type
),
111 (unsigned long) ep
->p_offset
, (unsigned long) ep
->p_vaddr
,
112 (unsigned long) ep
->p_paddr
);
113 printk(" p_filesz[%08lx] p_memsz[%08lx] p_flags[%08lx] "
114 "p_align[%08lx]\n", (unsigned long) ep
->p_filesz
,
115 (unsigned long) ep
->p_memsz
, (unsigned long) ep
->p_flags
,
116 (unsigned long) ep
->p_align
);
119 static void dump_phdrs(struct elf_phdr
*ep
, int pnum
)
123 for(i
= 0; i
< pnum
; i
++, ep
++) {
124 if((ep
->p_type
== PT_LOAD
) ||
125 (ep
->p_type
== PT_INTERP
) ||
126 (ep
->p_type
== PT_PHDR
))
130 #endif /* (DEBUG_ELF) */
132 static void set_brk(unsigned long start
, unsigned long end
)
134 start
= PAGE_ALIGN(start
);
135 end
= PAGE_ALIGN(end
);
138 do_brk(start
, end
- start
);
142 /* We need to explicitly zero any fractional pages
143 * after the data section (i.e. bss). This would
144 * contain the junk from the file that should not
147 static void padzero(unsigned long elf_bss
)
151 nbyte
= elf_bss
& (PAGE_SIZE
-1);
153 nbyte
= PAGE_SIZE
- nbyte
;
154 clear_user((void *) elf_bss
, nbyte
);
158 unsigned long * create_irix_tables(char * p
, int argc
, int envc
,
159 struct elfhdr
* exec
, unsigned int load_addr
,
160 unsigned int interp_load_addr
,
161 struct pt_regs
*regs
, struct elf_phdr
*ephdr
)
165 elf_addr_t
*sp
, *csp
;
168 printk("create_irix_tables: p[%p] argc[%d] envc[%d] "
169 "load_addr[%08x] interp_load_addr[%08x]\n",
170 p
, argc
, envc
, load_addr
, interp_load_addr
);
172 sp
= (elf_addr_t
*) (~15UL & (unsigned long) p
);
174 csp
-= exec
? DLINFO_ITEMS
*2 : 2;
177 csp
-= 1; /* argc itself */
178 if ((unsigned long)csp
& 15UL) {
179 sp
-= (16UL - ((unsigned long)csp
& 15UL)) / sizeof(*sp
);
183 * Put the ELF interpreter info on the stack
185 #define NEW_AUX_ENT(nr, id, val) \
186 __put_user ((id), sp+(nr*2)); \
187 __put_user ((val), sp+(nr*2+1)); \
190 NEW_AUX_ENT(0, AT_NULL
, 0);
195 NEW_AUX_ENT (0, AT_PHDR
, load_addr
+ exec
->e_phoff
);
196 NEW_AUX_ENT (1, AT_PHENT
, sizeof (struct elf_phdr
));
197 NEW_AUX_ENT (2, AT_PHNUM
, exec
->e_phnum
);
198 NEW_AUX_ENT (3, AT_PAGESZ
, ELF_EXEC_PAGESIZE
);
199 NEW_AUX_ENT (4, AT_BASE
, interp_load_addr
);
200 NEW_AUX_ENT (5, AT_FLAGS
, 0);
201 NEW_AUX_ENT (6, AT_ENTRY
, (elf_addr_t
) exec
->e_entry
);
202 NEW_AUX_ENT (7, AT_UID
, (elf_addr_t
) current
->uid
);
203 NEW_AUX_ENT (8, AT_EUID
, (elf_addr_t
) current
->euid
);
204 NEW_AUX_ENT (9, AT_GID
, (elf_addr_t
) current
->gid
);
205 NEW_AUX_ENT (10, AT_EGID
, (elf_addr_t
) current
->egid
);
210 envp
= (elf_caddr_t
*) sp
;
212 argv
= (elf_caddr_t
*) sp
;
214 __put_user((elf_addr_t
)argc
,--sp
);
215 current
->mm
->arg_start
= (unsigned long) p
;
217 __put_user((elf_caddr_t
)(unsigned long)p
,argv
++);
220 __put_user(NULL
, argv
);
221 current
->mm
->arg_end
= current
->mm
->env_start
= (unsigned long) p
;
223 __put_user((elf_caddr_t
)(unsigned long)p
,envp
++);
226 __put_user(NULL
, envp
);
227 current
->mm
->env_end
= (unsigned long) p
;
232 /* This is much more generalized than the library routine read function,
233 * so we keep this separate. Technically the library read function
234 * is only provided so that we can read a.out libraries that have
237 static unsigned int load_irix_interp(struct elfhdr
* interp_elf_ex
,
238 struct dentry
* interpreter_dentry
,
239 unsigned int *interp_load_addr
)
242 struct elf_phdr
*elf_phdata
= NULL
;
243 struct elf_phdr
*eppnt
;
245 unsigned int load_addr
;
249 unsigned int last_bss
;
256 error
= load_addr
= 0;
259 print_elfhdr(interp_elf_ex
);
262 /* First of all, some simple consistency checks */
263 if((interp_elf_ex
->e_type
!= ET_EXEC
&&
264 interp_elf_ex
->e_type
!= ET_DYN
) ||
265 !elf_check_arch(interp_elf_ex
->e_machine
) ||
266 (!interpreter_dentry
->d_inode
->i_op
||
267 !interpreter_dentry
->d_inode
->i_op
->default_file_ops
->mmap
)) {
268 printk("IRIX interp has bad e_type %d\n", interp_elf_ex
->e_type
);
272 /* Now read in all of the header information */
273 if(sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
> PAGE_SIZE
) {
274 printk("IRIX interp header bigger than a page (%d)\n",
275 (sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
));
279 elf_phdata
= (struct elf_phdr
*)
280 kmalloc(sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
,
284 printk("Cannot kmalloc phdata for IRIX interp.\n");
288 /* If the size of this structure has changed, then punt, since
289 * we will be doing the wrong thing.
291 if(interp_elf_ex
->e_phentsize
!= 32) {
292 printk("IRIX interp e_phentsize == %d != 32 ",
293 interp_elf_ex
->e_phentsize
);
298 retval
= read_exec(interpreter_dentry
, interp_elf_ex
->e_phoff
,
300 sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
, 1);
303 dump_phdrs(elf_phdata
, interp_elf_ex
->e_phnum
);
306 elf_exec_fileno
= open_dentry(interpreter_dentry
, O_RDONLY
);
307 if (elf_exec_fileno
< 0) {
308 printk("Could not open IRIX interp inode.\n");
313 file
= fget(elf_exec_fileno
);
316 for(i
=0; i
<interp_elf_ex
->e_phnum
; i
++, eppnt
++) {
317 if(eppnt
->p_type
== PT_LOAD
) {
318 int elf_type
= MAP_PRIVATE
| MAP_DENYWRITE
;
320 unsigned long vaddr
= 0;
321 if (eppnt
->p_flags
& PF_R
) elf_prot
= PROT_READ
;
322 if (eppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
323 if (eppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
324 elf_type
|= MAP_FIXED
;
325 vaddr
= eppnt
->p_vaddr
;
328 printk("INTERP do_mmap(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
330 (unsigned long) (eppnt
->p_filesz
+ (eppnt
->p_vaddr
& 0xfff)),
331 (unsigned long) elf_prot
, (unsigned long) elf_type
,
332 (unsigned long) (eppnt
->p_offset
& 0xfffff000));
334 error
= do_mmap(file
, vaddr
,
335 eppnt
->p_filesz
+ (eppnt
->p_vaddr
& 0xfff),
337 eppnt
->p_offset
& 0xfffff000);
339 if(error
< 0 && error
> -1024) {
340 printk("Aieee IRIX interp mmap error=%d\n", error
);
341 break; /* Real error */
344 printk("error=%08lx ", (unsigned long) error
);
346 if(!load_addr
&& interp_elf_ex
->e_type
== ET_DYN
) {
349 printk("load_addr = error ");
353 /* Find the end of the file mapping for this phdr, and keep
354 * track of the largest address we see for this.
356 k
= eppnt
->p_vaddr
+ eppnt
->p_filesz
;
357 if(k
> elf_bss
) elf_bss
= k
;
359 /* Do the same thing for the memory mapping - between
360 * elf_bss and last_bss is the bss section.
362 k
= eppnt
->p_memsz
+ eppnt
->p_vaddr
;
363 if(k
> last_bss
) last_bss
= k
;
370 /* Now use mmap to map the library into memory. */
372 sys_close(elf_exec_fileno
);
373 if(error
< 0 && error
> -1024) {
375 printk("got error %d\n", error
);
381 /* Now fill out the bss section. First pad the last page up
382 * to the page boundary, and then perform a mmap to make sure
383 * that there are zero-mapped pages up to and including the
387 printk("padzero(%08lx) ", (unsigned long) (elf_bss
));
390 len
= (elf_bss
+ 0xfff) & 0xfffff000; /* What we have mapped so far */
393 printk("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss
,
394 (unsigned long) len
);
397 /* Map the last of the bss segment */
398 if (last_bss
> len
) {
399 do_brk(len
, (last_bss
- len
));
403 *interp_load_addr
= load_addr
;
404 return ((unsigned int) interp_elf_ex
->e_entry
);
407 /* Check sanity of IRIX elf executable header. */
408 static int verify_binary(struct elfhdr
*ehp
, struct linux_binprm
*bprm
)
410 if (ehp
->e_ident
[0] != 0x7f || strncmp(&ehp
->e_ident
[1], "ELF", 3)) {
414 /* First of all, some simple consistency checks */
415 if((ehp
->e_type
!= ET_EXEC
&& ehp
->e_type
!= ET_DYN
) ||
416 !elf_check_arch(ehp
->e_machine
) ||
417 (!bprm
->dentry
->d_inode
->i_op
||
418 !bprm
->dentry
->d_inode
->i_op
->default_file_ops
||
419 !bprm
->dentry
->d_inode
->i_op
->default_file_ops
->mmap
)) {
423 /* Only support MIPS ARCH2 or greater IRIX binaries for now. */
424 if(!(ehp
->e_flags
& EF_MIPS_ARCH
) && !(ehp
->e_flags
& 0x04)) {
428 /* XXX Don't support N32 or 64bit binaries yet because they can
429 * XXX and do execute 64 bit instructions and expect all registers
430 * XXX to be 64 bit as well. We need to make the kernel save
431 * XXX all registers as 64bits on cpu's capable of this at
432 * XXX exception time plus frob the XTLB exception vector.
434 if((ehp
->e_flags
& 0x20)) {
438 return 0; /* It's ok. */
441 #define IRIX_INTERP_PREFIX "/usr/gnemul/irix"
443 /* Look for an IRIX ELF interpreter. */
444 static inline int look_for_irix_interpreter(char **name
,
445 struct dentry
**interpreter_dentry
,
446 struct elfhdr
*interp_elf_ex
,
447 struct elf_phdr
*epp
,
448 struct linux_binprm
*bprm
, int pnum
)
452 int retval
= -EINVAL
;
453 struct dentry
*dentry
= NULL
;
456 for(i
= 0; i
< pnum
; i
++, epp
++) {
457 if(epp
->p_type
!= PT_INTERP
)
460 /* It is illegal to have two interpreters for one executable. */
464 *name
= (char *) kmalloc((epp
->p_filesz
+
465 strlen(IRIX_INTERP_PREFIX
)),
470 strcpy(*name
, IRIX_INTERP_PREFIX
);
471 retval
= read_exec(bprm
->dentry
, epp
->p_offset
, (*name
+ 16),
476 old_fs
= get_fs(); set_fs(get_ds());
477 dentry
= namei(*name
);
480 retval
= PTR_ERR(dentry
);
483 retval
= read_exec(dentry
, 0, bprm
->buf
, 128, 1);
487 *interp_elf_ex
= *((struct elfhdr
*) bprm
->buf
);
489 *interpreter_dentry
= dentry
;
499 static inline int verify_irix_interpreter(struct elfhdr
*ihp
)
501 if(ihp
->e_ident
[0] != 0x7f || strncmp(&ihp
->e_ident
[1], "ELF", 3))
506 #define EXEC_MAP_FLAGS (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE)
508 static inline void map_executable(struct file
*fp
, struct elf_phdr
*epp
, int pnum
,
509 unsigned int *estack
, unsigned int *laddr
,
510 unsigned int *scode
, unsigned int *ebss
,
511 unsigned int *ecode
, unsigned int *edata
,
517 for(i
= 0; i
< pnum
; i
++, epp
++) {
518 if(epp
->p_type
!= PT_LOAD
)
522 prot
= (epp
->p_flags
& PF_R
) ? PROT_READ
: 0;
523 prot
|= (epp
->p_flags
& PF_W
) ? PROT_WRITE
: 0;
524 prot
|= (epp
->p_flags
& PF_X
) ? PROT_EXEC
: 0;
525 (void) do_mmap(fp
, (epp
->p_vaddr
& 0xfffff000),
526 (epp
->p_filesz
+ (epp
->p_vaddr
& 0xfff)),
527 prot
, EXEC_MAP_FLAGS
,
528 (epp
->p_offset
& 0xfffff000));
530 /* Fixup location tracking vars. */
531 if((epp
->p_vaddr
& 0xfffff000) < *estack
)
532 *estack
= (epp
->p_vaddr
& 0xfffff000);
534 *laddr
= epp
->p_vaddr
- epp
->p_offset
;
535 if(epp
->p_vaddr
< *scode
)
536 *scode
= epp
->p_vaddr
;
538 tmp
= epp
->p_vaddr
+ epp
->p_filesz
;
541 if((epp
->p_flags
& PF_X
) && *ecode
< tmp
)
546 tmp
= epp
->p_vaddr
+ epp
->p_memsz
;
553 static inline int map_interpreter(struct elf_phdr
*epp
, struct elfhdr
*ihp
,
554 struct dentry
*identry
, unsigned int *iladdr
,
555 int pnum
, mm_segment_t old_fs
,
556 unsigned int *eentry
)
560 *eentry
= 0xffffffff;
561 for(i
= 0; i
< pnum
; i
++, epp
++) {
562 if(epp
->p_type
!= PT_INTERP
)
565 /* We should have fielded this error elsewhere... */
566 if(*eentry
!= 0xffffffff)
570 *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 inline int do_load_irix_binary(struct linux_binprm
* bprm
,
611 struct pt_regs
* regs
)
613 struct elfhdr elf_ex
, interp_elf_ex
;
614 struct dentry
*interpreter_dentry
;
615 struct elf_phdr
*elf_phdata
, *elf_ihdr
, *elf_ephdr
;
616 unsigned int load_addr
, elf_bss
, elf_brk
;
617 unsigned int elf_entry
, interp_load_addr
= 0;
618 unsigned int start_code
, end_code
, end_data
, elf_stack
;
619 int elf_exec_fileno
, retval
, has_interp
, has_ephdr
, size
, i
;
620 char *elf_interpreter
;
625 has_interp
= has_ephdr
= 0;
626 elf_ihdr
= elf_ephdr
= 0;
627 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
;
638 elf_phdata
= (struct elf_phdr
*) kmalloc(size
, GFP_KERNEL
);
639 if (elf_phdata
== NULL
)
642 retval
= read_exec(bprm
->dentry
, elf_ex
.e_phoff
,
643 (char *) elf_phdata
, size
, 1);
648 dump_phdrs(elf_phdata
, elf_ex
.e_phnum
);
651 /* Set some things for later. */
652 for(i
= 0; i
< elf_ex
.e_phnum
; i
++) {
653 switch(elf_phdata
[i
].p_type
) {
656 elf_ihdr
= &elf_phdata
[i
];
660 elf_ephdr
= &elf_phdata
[i
];
670 retval
= open_dentry(bprm
->dentry
, O_RDONLY
);
673 file
= fget(elf_exec_fileno
= retval
);
675 elf_stack
= 0xffffffff;
676 elf_interpreter
= NULL
;
677 start_code
= 0xffffffff;
681 retval
= look_for_irix_interpreter(&elf_interpreter
,
683 &interp_elf_ex
, elf_phdata
, bprm
,
688 if(elf_interpreter
) {
689 retval
= verify_irix_interpreter(&interp_elf_ex
);
694 /* OK, we are done with that, now set up the arg stuff,
695 * and then start this sucker up.
698 if (!bprm
->sh_bang
&& !bprm
->p
)
701 /* Flush all traces of the currently running executable */
702 retval
= flush_old_exec(bprm
);
706 /* OK, This is the point of no return */
707 current
->mm
->end_data
= 0;
708 current
->mm
->end_code
= 0;
709 current
->mm
->mmap
= NULL
;
710 current
->flags
&= ~PF_FORKNOEXEC
;
711 elf_entry
= (unsigned int) elf_ex
.e_entry
;
713 /* Do this so that we can load the interpreter, if need be. We will
714 * change some of these later.
716 current
->mm
->rss
= 0;
717 setup_arg_pages(bprm
);
718 current
->mm
->start_stack
= bprm
->p
;
720 /* At this point, we assume that the image should be loaded at
721 * fixed address, not at a variable address.
726 map_executable(file
, elf_phdata
, elf_ex
.e_phnum
, &elf_stack
, &load_addr
,
727 &start_code
, &elf_bss
, &end_code
, &end_data
, &elf_brk
);
729 if(elf_interpreter
) {
730 retval
= map_interpreter(elf_phdata
, &interp_elf_ex
,
731 interpreter_dentry
, &interp_load_addr
,
732 elf_ex
.e_phnum
, old_fs
, &elf_entry
);
733 kfree(elf_interpreter
);
736 printk("Unable to load IRIX ELF interpreter\n");
737 send_sig(SIGSEGV
, current
, 0);
747 sys_close(elf_exec_fileno
);
748 current
->personality
= PER_IRIX32
;
750 if (current
->exec_domain
&& current
->exec_domain
->module
)
751 __MOD_DEC_USE_COUNT(current
->exec_domain
->module
);
752 if (current
->binfmt
&& current
->binfmt
->module
)
753 __MOD_DEC_USE_COUNT(current
->binfmt
->module
);
754 current
->exec_domain
= lookup_exec_domain(current
->personality
);
755 current
->binfmt
= &irix_format
;
756 if (current
->exec_domain
&& current
->exec_domain
->module
)
757 __MOD_INC_USE_COUNT(current
->exec_domain
->module
);
758 if (current
->binfmt
&& current
->binfmt
->module
)
759 __MOD_INC_USE_COUNT(current
->binfmt
->module
);
762 current
->flags
&= ~PF_FORKNOEXEC
;
763 bprm
->p
= (unsigned long)
764 create_irix_tables((char *)bprm
->p
, bprm
->argc
, bprm
->envc
,
765 (elf_interpreter
? &elf_ex
: NULL
),
766 load_addr
, interp_load_addr
, regs
, elf_ephdr
);
767 current
->mm
->start_brk
= current
->mm
->brk
= elf_brk
;
768 current
->mm
->end_code
= end_code
;
769 current
->mm
->start_code
= start_code
;
770 current
->mm
->end_data
= end_data
;
771 current
->mm
->start_stack
= bprm
->p
;
773 /* Calling set_brk effectively mmaps the pages that we need for the
774 * bss and break sections.
776 set_brk(elf_bss
, elf_brk
);
779 * IRIX maps a page at 0x200000 which holds some system
780 * information. Programs depend on this.
782 irix_map_prda_page ();
787 printk("(start_brk) %lx\n" , (long) current
->mm
->start_brk
);
788 printk("(end_code) %lx\n" , (long) current
->mm
->end_code
);
789 printk("(start_code) %lx\n" , (long) current
->mm
->start_code
);
790 printk("(end_data) %lx\n" , (long) current
->mm
->end_data
);
791 printk("(start_stack) %lx\n" , (long) current
->mm
->start_stack
);
792 printk("(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 (void) do_mmap(NULL
, 0, 4096, PROT_READ
| PROT_EXEC
,
802 MAP_FIXED
| MAP_PRIVATE
, 0);
805 start_thread(regs
, elf_entry
, bprm
->p
);
806 if (current
->flags
& PF_PTRACED
)
807 send_sig(SIGTRAP
, current
, 0);
811 if(elf_interpreter
) {
812 kfree(elf_interpreter
);
816 sys_close(elf_exec_fileno
);
822 static int load_irix_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
827 retval
= do_load_irix_binary(bprm
, regs
);
832 /* This is really simpleminded and specialized - we are loading an
833 * a.out library that is given an ELF header.
835 static inline int do_load_irix_library(struct file
*file
)
837 struct elfhdr elf_ex
;
838 struct elf_phdr
*elf_phdata
= NULL
;
839 struct dentry
*dentry
;
851 dentry
= file
->f_dentry
;
852 inode
= dentry
->d_inode
;
855 /* Seek to the beginning of the file. */
856 if (file
->f_op
->llseek
) {
857 if ((error
= file
->f_op
->llseek(file
, 0, 0)) != 0)
863 error
= file
->f_op
->read(file
, (char *) &elf_ex
, sizeof(elf_ex
),
866 if (error
!= sizeof(elf_ex
))
869 if (elf_ex
.e_ident
[0] != 0x7f ||
870 strncmp(&elf_ex
.e_ident
[1], "ELF",3) != 0)
873 /* First of all, some simple consistency checks. */
874 if(elf_ex
.e_type
!= ET_EXEC
|| elf_ex
.e_phnum
> 2 ||
875 !elf_check_arch(elf_ex
.e_machine
) ||
876 (!dentry
->d_inode
->i_op
||
877 !dentry
->d_inode
->i_op
->default_file_ops
->mmap
))
880 /* Now read in all of the header information. */
881 if(sizeof(struct elf_phdr
) * elf_ex
.e_phnum
> PAGE_SIZE
)
884 elf_phdata
= (struct elf_phdr
*)
885 kmalloc(sizeof(struct elf_phdr
) * elf_ex
.e_phnum
, GFP_KERNEL
);
886 if (elf_phdata
== NULL
)
889 retval
= read_exec(dentry
, elf_ex
.e_phoff
, (char *) elf_phdata
,
890 sizeof(struct elf_phdr
) * elf_ex
.e_phnum
, 1);
893 for(i
=0; i
<elf_ex
.e_phnum
; i
++)
894 if((elf_phdata
+ i
)->p_type
== PT_LOAD
) j
++;
901 while(elf_phdata
->p_type
!= PT_LOAD
) elf_phdata
++;
903 /* Now use mmap to map the library into memory. */
904 error
= do_mmap(file
,
905 elf_phdata
->p_vaddr
& 0xfffff000,
906 elf_phdata
->p_filesz
+ (elf_phdata
->p_vaddr
& 0xfff),
907 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
908 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
,
909 elf_phdata
->p_offset
& 0xfffff000);
911 k
= elf_phdata
->p_vaddr
+ elf_phdata
->p_filesz
;
912 if(k
> elf_bss
) elf_bss
= k
;
914 if (error
!= (elf_phdata
->p_vaddr
& 0xfffff000)) {
921 len
= (elf_phdata
->p_filesz
+ elf_phdata
->p_vaddr
+ 0xfff) & 0xfffff000;
922 bss
= elf_phdata
->p_memsz
+ elf_phdata
->p_vaddr
;
924 do_brk(len
, bss
-len
);
929 static int load_irix_library(int fd
)
931 int retval
= -EACCES
;
937 retval
= do_load_irix_library(file
);
944 /* Called through irix_syssgi() to map an elf image given an FD,
945 * a phdr ptr USER_PHDRP in userspace, and a count CNT telling how many
946 * phdrs there are in the USER_PHDRP array. We return the vaddr the
947 * first phdr was successfully mapped to.
949 unsigned long irix_mapelf(int fd
, struct elf_phdr
*user_phdrp
, int cnt
)
956 printk("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
957 fd
, user_phdrp
, cnt
);
960 /* First get the verification out of the way. */
962 retval
= verify_area(VERIFY_READ
, hp
, (sizeof(struct elf_phdr
) * cnt
));
965 printk("irix_mapelf: verify_area fails!\n");
971 dump_phdrs(user_phdrp
, cnt
);
974 for(i
= 0; i
< cnt
; i
++, hp
++)
975 if(hp
->p_type
!= PT_LOAD
) {
976 printk("irix_mapelf: One section is not PT_LOAD!\n");
984 printk("irix_mapelf: Bogon filp!\n");
990 for(i
= 0; i
< cnt
; i
++, hp
++) {
993 prot
= (hp
->p_flags
& PF_R
) ? PROT_READ
: 0;
994 prot
|= (hp
->p_flags
& PF_W
) ? PROT_WRITE
: 0;
995 prot
|= (hp
->p_flags
& PF_X
) ? PROT_EXEC
: 0;
996 retval
= do_mmap(filp
, (hp
->p_vaddr
& 0xfffff000),
997 (hp
->p_filesz
+ (hp
->p_vaddr
& 0xfff)),
998 prot
, (MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
),
999 (hp
->p_offset
& 0xfffff000));
1001 if(retval
!= (hp
->p_vaddr
& 0xfffff000)) {
1002 printk("irix_mapelf: do_mmap fails with %d!\n", retval
);
1009 printk("irix_mapelf: Success, returning %08lx\n", user_phdrp
->p_vaddr
);
1012 return user_phdrp
->p_vaddr
;
1018 * Modelled on fs/exec.c:aout_core_dump()
1019 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1022 /* These are the only things you should do on a core-file: use only these
1023 * functions to write out all the necessary info.
1025 static int dump_write(struct file
*file
, const void *addr
, int nr
)
1027 return file
->f_op
->write(file
, addr
, nr
, &file
->f_pos
) == nr
;
1030 static int dump_seek(struct file
*file
, off_t off
)
1032 if (file
->f_op
->llseek
) {
1033 if (file
->f_op
->llseek(file
, off
, 0) != off
)
1040 /* Decide whether a segment is worth dumping; default is yes to be
1041 * sure (missing info is worse than too much; etc).
1042 * Personally I'd include everything, and use the coredump limit...
1044 * I think we should skip something. But I am not sure how. H.J.
1046 static inline int maydump(struct vm_area_struct
*vma
)
1048 if (!(vma
->vm_flags
& (VM_READ
|VM_WRITE
|VM_EXEC
)))
1051 if (vma
->vm_flags
& (VM_WRITE
|VM_GROWSUP
|VM_GROWSDOWN
))
1053 if (vma
->vm_flags
& (VM_READ
|VM_EXEC
|VM_EXECUTABLE
|VM_SHARED
))
1059 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
1061 /* An ELF note in memory. */
1066 unsigned int datasz
;
1070 static int notesize(struct memelfnote
*en
)
1074 sz
= sizeof(struct elf_note
);
1075 sz
+= roundup(strlen(en
->name
), 4);
1076 sz
+= roundup(en
->datasz
, 4);
1083 #define DUMP_WRITE(addr, nr) \
1084 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1085 #define DUMP_SEEK(off) \
1086 do { if (!dump_seek(file, (off))) return 0; } while(0)
1088 static int writenote(struct memelfnote
*men
, struct file
*file
)
1092 en
.n_namesz
= strlen(men
->name
);
1093 en
.n_descsz
= men
->datasz
;
1094 en
.n_type
= men
->type
;
1096 DUMP_WRITE(&en
, sizeof(en
));
1097 DUMP_WRITE(men
->name
, en
.n_namesz
);
1098 /* XXX - cast from long long to long to avoid need for libgcc.a */
1099 DUMP_SEEK(roundup((unsigned long)file
->f_pos
, 4)); /* XXX */
1100 DUMP_WRITE(men
->data
, men
->datasz
);
1101 DUMP_SEEK(roundup((unsigned long)file
->f_pos
, 4)); /* XXX */
1108 #define DUMP_WRITE(addr, nr) \
1109 if (!dump_write(file, (addr), (nr))) \
1111 #define DUMP_SEEK(off) \
1112 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 #ifndef CONFIG_BINFMT_IRIX
1141 /* Count what's needed to dump, up to the limit of coredump size. */
1144 for(vma
= current
->mm
->mmap
; vma
!= NULL
; vma
= vma
->vm_next
) {
1147 int sz
= vma
->vm_end
-vma
->vm_start
;
1149 if (size
+sz
>= limit
)
1158 printk("irix_core_dump: %d segs taking %d bytes\n", segs
, size
);
1161 /* Set up header. */
1162 memcpy(elf
.e_ident
, ELFMAG
, SELFMAG
);
1163 elf
.e_ident
[EI_CLASS
] = ELFCLASS32
;
1164 elf
.e_ident
[EI_DATA
] = ELFDATA2LSB
;
1165 elf
.e_ident
[EI_VERSION
] = EV_CURRENT
;
1166 memset(elf
.e_ident
+EI_PAD
, 0, EI_NIDENT
-EI_PAD
);
1168 elf
.e_type
= ET_CORE
;
1169 elf
.e_machine
= ELF_ARCH
;
1170 elf
.e_version
= EV_CURRENT
;
1172 elf
.e_phoff
= sizeof(elf
);
1175 elf
.e_ehsize
= sizeof(elf
);
1176 elf
.e_phentsize
= sizeof(struct elf_phdr
);
1177 elf
.e_phnum
= segs
+1; /* Include notes. */
1178 elf
.e_shentsize
= 0;
1186 current
->flags
|= PF_DUMPCORE
;
1188 DUMP_WRITE(&elf
, sizeof(elf
));
1189 offset
+= sizeof(elf
); /* Elf header. */
1190 offset
+= (segs
+1) * sizeof(struct elf_phdr
); /* Program headers. */
1192 /* Set up the notes in similar form to SVR4 core dumps made
1193 * with info from their /proc.
1195 memset(&psinfo
, 0, sizeof(psinfo
));
1196 memset(&prstatus
, 0, sizeof(prstatus
));
1198 notes
[0].name
= "CORE";
1199 notes
[0].type
= NT_PRSTATUS
;
1200 notes
[0].datasz
= sizeof(prstatus
);
1201 notes
[0].data
= &prstatus
;
1202 prstatus
.pr_info
.si_signo
= prstatus
.pr_cursig
= signr
;
1203 prstatus
.pr_sigpend
= current
->signal
.sig
[0];
1204 prstatus
.pr_sighold
= current
->blocked
.sig
[0];
1205 psinfo
.pr_pid
= prstatus
.pr_pid
= current
->pid
;
1206 psinfo
.pr_ppid
= prstatus
.pr_ppid
= current
->p_pptr
->pid
;
1207 psinfo
.pr_pgrp
= prstatus
.pr_pgrp
= current
->pgrp
;
1208 psinfo
.pr_sid
= prstatus
.pr_sid
= current
->session
;
1209 prstatus
.pr_utime
.tv_sec
= CT_TO_SECS(current
->times
.tms_utime
);
1210 prstatus
.pr_utime
.tv_usec
= CT_TO_USECS(current
->times
.tms_utime
);
1211 prstatus
.pr_stime
.tv_sec
= CT_TO_SECS(current
->times
.tms_stime
);
1212 prstatus
.pr_stime
.tv_usec
= CT_TO_USECS(current
->times
.tms_stime
);
1213 prstatus
.pr_cutime
.tv_sec
= CT_TO_SECS(current
->times
.tms_cutime
);
1214 prstatus
.pr_cutime
.tv_usec
= CT_TO_USECS(current
->times
.tms_cutime
);
1215 prstatus
.pr_cstime
.tv_sec
= CT_TO_SECS(current
->times
.tms_cstime
);
1216 prstatus
.pr_cstime
.tv_usec
= CT_TO_USECS(current
->times
.tms_cstime
);
1217 if (sizeof(elf_gregset_t
) != sizeof(struct pt_regs
)) {
1218 printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
1219 "(%d)\n", sizeof(elf_gregset_t
), sizeof(struct pt_regs
));
1221 *(struct pt_regs
*)&prstatus
.pr_reg
= *regs
;
1224 notes
[1].name
= "CORE";
1225 notes
[1].type
= NT_PRPSINFO
;
1226 notes
[1].datasz
= sizeof(psinfo
);
1227 notes
[1].data
= &psinfo
;
1228 i
= current
->state
? ffz(~current
->state
) + 1 : 0;
1229 psinfo
.pr_state
= i
;
1230 psinfo
.pr_sname
= (i
< 0 || i
> 5) ? '.' : "RSDZTD"[i
];
1231 psinfo
.pr_zomb
= psinfo
.pr_sname
== 'Z';
1232 psinfo
.pr_nice
= current
->priority
-15;
1233 psinfo
.pr_flag
= current
->flags
;
1234 psinfo
.pr_uid
= current
->uid
;
1235 psinfo
.pr_gid
= current
->gid
;
1241 len
= current
->mm
->arg_end
- current
->mm
->arg_start
;
1242 len
= len
>= ELF_PRARGSZ
? ELF_PRARGSZ
: len
;
1243 copy_from_user(&psinfo
.pr_psargs
,
1244 (const char *)current
->mm
->arg_start
, len
);
1245 for(i
= 0; i
< len
; i
++)
1246 if (psinfo
.pr_psargs
[i
] == 0)
1247 psinfo
.pr_psargs
[i
] = ' ';
1248 psinfo
.pr_psargs
[len
] = 0;
1252 strncpy(psinfo
.pr_fname
, current
->comm
, sizeof(psinfo
.pr_fname
));
1254 notes
[2].name
= "CORE";
1255 notes
[2].type
= NT_TASKSTRUCT
;
1256 notes
[2].datasz
= sizeof(*current
);
1257 notes
[2].data
= current
;
1259 /* Try to dump the FPU. */
1260 prstatus
.pr_fpvalid
= dump_fpu (&fpu
);
1261 if (!prstatus
.pr_fpvalid
) {
1264 notes
[3].name
= "CORE";
1265 notes
[3].type
= NT_PRFPREG
;
1266 notes
[3].datasz
= sizeof(fpu
);
1267 notes
[3].data
= &fpu
;
1270 /* Write notes phdr entry. */
1272 struct elf_phdr phdr
;
1275 for(i
= 0; i
< numnote
; i
++)
1276 sz
+= notesize(¬es
[i
]);
1278 phdr
.p_type
= PT_NOTE
;
1279 phdr
.p_offset
= offset
;
1287 offset
+= phdr
.p_filesz
;
1288 DUMP_WRITE(&phdr
, sizeof(phdr
));
1291 /* Page-align dumped data. */
1292 dataoff
= offset
= roundup(offset
, PAGE_SIZE
);
1294 /* Write program headers for segments dump. */
1295 for(vma
= current
->mm
->mmap
, i
= 0;
1296 i
< segs
&& vma
!= NULL
; vma
= vma
->vm_next
) {
1297 struct elf_phdr phdr
;
1302 sz
= vma
->vm_end
- vma
->vm_start
;
1304 phdr
.p_type
= PT_LOAD
;
1305 phdr
.p_offset
= offset
;
1306 phdr
.p_vaddr
= vma
->vm_start
;
1308 phdr
.p_filesz
= maydump(vma
) ? sz
: 0;
1310 offset
+= phdr
.p_filesz
;
1311 phdr
.p_flags
= vma
->vm_flags
& VM_READ
? PF_R
: 0;
1312 if (vma
->vm_flags
& VM_WRITE
) phdr
.p_flags
|= PF_W
;
1313 if (vma
->vm_flags
& VM_EXEC
) phdr
.p_flags
|= PF_X
;
1314 phdr
.p_align
= PAGE_SIZE
;
1316 DUMP_WRITE(&phdr
, sizeof(phdr
));
1319 for(i
= 0; i
< numnote
; i
++)
1320 if (!writenote(¬es
[i
], file
))
1327 for(i
= 0, vma
= current
->mm
->mmap
;
1328 i
< segs
&& vma
!= NULL
;
1329 vma
= vma
->vm_next
) {
1330 unsigned long addr
= vma
->vm_start
;
1331 unsigned long len
= vma
->vm_end
- vma
->vm_start
;
1337 printk("elf_core_dump: writing %08lx %lx\n", addr
, len
);
1339 DUMP_WRITE((void *)addr
, len
);
1342 if ((off_t
) file
->f_pos
!= offset
) {
1344 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1345 (off_t
) file
->f_pos
, offset
);
1350 #ifndef CONFIG_BINFMT_ELF
1356 int __init
init_irix_binfmt(void)
1358 return register_binfmt(&irix_format
);
1363 int init_module(void)
1365 /* Install the COFF, ELF and XOUT loaders.
1366 * N.B. We *rely* on the table being the right size with the
1367 * right number of free slots...
1369 return init_irix_binfmt();
1373 void cleanup_module( void)
1375 /* Remove the IRIX ELF loaders. */
1376 unregister_binfmt(&irix_format
);