2 * linux/fs/binfmt_elf.c
4 * These are the functions used to load ELF format executables as used
5 * on SVr4 machines. Information on the format may be found in the book
6 * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
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/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/malloc.h>
28 #include <linux/shm.h>
29 #include <linux/personality.h>
30 #include <linux/elfcore.h>
31 #include <linux/init.h>
32 #include <linux/highuid.h>
33 #include <linux/smp_lock.h>
35 #include <asm/uaccess.h>
36 #include <asm/pgalloc.h>
38 #include <linux/config.h>
40 #define DLINFO_ITEMS 13
42 #include <linux/elf.h>
44 static int load_elf_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
);
45 static int load_elf_library(int fd
);
46 extern int dump_fpu (struct pt_regs
*, elf_fpregset_t
*);
47 extern void dump_thread(struct pt_regs
*, struct user
*);
50 #define elf_addr_t unsigned long
51 #define elf_caddr_t char *
55 * If we don't support core dumping, then supply a NULL so we
58 #ifdef USE_ELF_CORE_DUMP
59 static int elf_core_dump(long signr
, struct pt_regs
* regs
, struct file
* file
);
61 #define elf_core_dump NULL
64 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_EXEC_PAGESIZE-1))
65 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_EXEC_PAGESIZE-1))
66 #define ELF_PAGEALIGN(_v) (((_v) + ELF_EXEC_PAGESIZE - 1) & ~(ELF_EXEC_PAGESIZE - 1))
68 static struct linux_binfmt elf_format
= {
69 NULL
, THIS_MODULE
, load_elf_binary
, load_elf_library
, elf_core_dump
, ELF_EXEC_PAGESIZE
72 static void set_brk(unsigned long start
, unsigned long end
)
74 start
= ELF_PAGEALIGN(start
);
75 end
= ELF_PAGEALIGN(end
);
78 do_brk(start
, end
- start
);
82 /* We need to explicitly zero any fractional pages
83 after the data section (i.e. bss). This would
84 contain the junk from the file that should not
88 static void padzero(unsigned long elf_bss
)
92 nbyte
= ELF_PAGEOFFSET(elf_bss
);
94 nbyte
= ELF_EXEC_PAGESIZE
- nbyte
;
95 clear_user((void *) elf_bss
, nbyte
);
100 create_elf_tables(char *p
, int argc
, int envc
,
101 struct elfhdr
* exec
,
102 unsigned long load_addr
,
103 unsigned long load_bias
,
104 unsigned long interp_load_addr
, int ibcs
)
108 elf_addr_t
*sp
, *csp
;
109 char *k_platform
, *u_platform
;
111 size_t platform_len
= 0;
114 * Get hold of platform and hardware capabilities masks for
115 * the machine we are running on. In some cases (Sparc),
116 * this info is impossible to get, in others (i386) it is
121 k_platform
= ELF_PLATFORM
;
124 platform_len
= strlen(k_platform
) + 1;
125 u_platform
= p
- platform_len
;
126 __copy_to_user(u_platform
, k_platform
, platform_len
);
131 * Force 16 byte _final_ alignment here for generality.
132 * Leave an extra 16 bytes free so that on the PowerPC we
133 * can move the aux table up to start on a 16-byte boundary.
135 sp
= (elf_addr_t
*)((~15UL & (unsigned long)(u_platform
)) - 16UL);
137 csp
-= ((exec
? DLINFO_ITEMS
*2 : 4) + (k_platform
? 2 : 0));
140 csp
-= (!ibcs
? 3 : 1); /* argc itself */
141 if ((unsigned long)csp
& 15UL)
142 sp
-= ((unsigned long)csp
& 15UL) / sizeof(*sp
);
145 * Put the ELF interpreter info on the stack
147 #define NEW_AUX_ENT(nr, id, val) \
148 __put_user ((id), sp+(nr*2)); \
149 __put_user ((val), sp+(nr*2+1)); \
152 NEW_AUX_ENT(0, AT_NULL
, 0);
155 NEW_AUX_ENT(0, AT_PLATFORM
, (elf_addr_t
)(unsigned long) u_platform
);
158 NEW_AUX_ENT(0, AT_HWCAP
, hwcap
);
163 NEW_AUX_ENT(0, AT_PHDR
, load_addr
+ exec
->e_phoff
);
164 NEW_AUX_ENT(1, AT_PHENT
, sizeof (struct elf_phdr
));
165 NEW_AUX_ENT(2, AT_PHNUM
, exec
->e_phnum
);
166 NEW_AUX_ENT(3, AT_PAGESZ
, ELF_EXEC_PAGESIZE
);
167 NEW_AUX_ENT(4, AT_BASE
, interp_load_addr
);
168 NEW_AUX_ENT(5, AT_FLAGS
, 0);
169 NEW_AUX_ENT(6, AT_ENTRY
, load_bias
+ exec
->e_entry
);
170 NEW_AUX_ENT(7, AT_UID
, (elf_addr_t
) current
->uid
);
171 NEW_AUX_ENT(8, AT_EUID
, (elf_addr_t
) current
->euid
);
172 NEW_AUX_ENT(9, AT_GID
, (elf_addr_t
) current
->gid
);
173 NEW_AUX_ENT(10, AT_EGID
, (elf_addr_t
) current
->egid
);
178 envp
= (elf_caddr_t
*) sp
;
180 argv
= (elf_caddr_t
*) sp
;
182 __put_user((elf_addr_t
)(unsigned long) envp
,--sp
);
183 __put_user((elf_addr_t
)(unsigned long) argv
,--sp
);
186 __put_user((elf_addr_t
)argc
,--sp
);
187 current
->mm
->arg_start
= (unsigned long) p
;
189 __put_user((elf_caddr_t
)(unsigned long)p
,argv
++);
192 __put_user(NULL
, argv
);
193 current
->mm
->arg_end
= current
->mm
->env_start
= (unsigned long) p
;
195 __put_user((elf_caddr_t
)(unsigned long)p
,envp
++);
198 __put_user(NULL
, envp
);
199 current
->mm
->env_end
= (unsigned long) p
;
204 /* This is much more generalized than the library routine read function,
205 so we keep this separate. Technically the library read function
206 is only provided so that we can read a.out libraries that have
209 static unsigned long load_elf_interp(struct elfhdr
* interp_elf_ex
,
210 struct dentry
* interpreter_dentry
,
211 unsigned long *interp_load_addr
)
214 struct elf_phdr
*elf_phdata
;
215 struct elf_phdr
*eppnt
;
216 unsigned long load_addr
= 0;
217 int load_addr_set
= 0;
218 unsigned long last_bss
= 0, elf_bss
= 0;
219 unsigned long error
= ~0UL;
223 /* First of all, some simple consistency checks */
224 if (interp_elf_ex
->e_type
!= ET_EXEC
&&
225 interp_elf_ex
->e_type
!= ET_DYN
)
227 if (!elf_check_arch(interp_elf_ex
->e_machine
))
229 if (!interpreter_dentry
->d_inode
->i_fop
||
230 !interpreter_dentry
->d_inode
->i_fop
->mmap
)
234 * If the size of this structure has changed, then punt, since
235 * we will be doing the wrong thing.
237 if (interp_elf_ex
->e_phentsize
!= sizeof(struct elf_phdr
))
240 /* Now read in all of the header information */
242 size
= sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
;
243 if (size
> ELF_EXEC_PAGESIZE
)
245 elf_phdata
= (struct elf_phdr
*) kmalloc(size
, GFP_KERNEL
);
249 retval
= read_exec(interpreter_dentry
, interp_elf_ex
->e_phoff
,
250 (char *) elf_phdata
, size
, 1);
256 elf_exec_fileno
= open_dentry(interpreter_dentry
, O_RDONLY
);
257 if (elf_exec_fileno
< 0)
259 file
= fget(elf_exec_fileno
);
262 for (i
=0; i
<interp_elf_ex
->e_phnum
; i
++, eppnt
++) {
263 if (eppnt
->p_type
== PT_LOAD
) {
264 int elf_type
= MAP_PRIVATE
| MAP_DENYWRITE
;
266 unsigned long vaddr
= 0;
267 unsigned long k
, map_addr
;
269 if (eppnt
->p_flags
& PF_R
) elf_prot
= PROT_READ
;
270 if (eppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
271 if (eppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
272 vaddr
= eppnt
->p_vaddr
;
273 if (interp_elf_ex
->e_type
== ET_EXEC
|| load_addr_set
)
274 elf_type
|= MAP_FIXED
;
276 map_addr
= do_mmap(file
,
277 load_addr
+ ELF_PAGESTART(vaddr
),
278 eppnt
->p_filesz
+ ELF_PAGEOFFSET(eppnt
->p_vaddr
),
281 eppnt
->p_offset
- ELF_PAGEOFFSET(eppnt
->p_vaddr
));
282 if (map_addr
> -1024UL) /* Real error */
285 if (!load_addr_set
&& interp_elf_ex
->e_type
== ET_DYN
) {
286 load_addr
= map_addr
- ELF_PAGESTART(vaddr
);
291 * Find the end of the file mapping for this phdr, and keep
292 * track of the largest address we see for this.
294 k
= load_addr
+ eppnt
->p_vaddr
+ eppnt
->p_filesz
;
299 * Do the same thing for the memory mapping - between
300 * elf_bss and last_bss is the bss section.
302 k
= load_addr
+ eppnt
->p_memsz
+ eppnt
->p_vaddr
;
308 /* Now use mmap to map the library into memory. */
311 * Now fill out the bss section. First pad the last page up
312 * to the page boundary, and then perform a mmap to make sure
313 * that there are zero-mapped pages up to and including the
317 elf_bss
= ELF_PAGESTART(elf_bss
+ ELF_EXEC_PAGESIZE
- 1); /* What we have mapped so far */
319 /* Map the last of the bss segment */
320 if (last_bss
> elf_bss
)
321 do_brk(elf_bss
, last_bss
- elf_bss
);
323 *interp_load_addr
= load_addr
;
324 error
= ((unsigned long) interp_elf_ex
->e_entry
) + load_addr
;
328 sys_close(elf_exec_fileno
);
335 static unsigned long load_aout_interp(struct exec
* interp_ex
,
336 struct dentry
* interpreter_dentry
)
338 unsigned long text_data
, offset
, elf_entry
= ~0UL;
342 current
->mm
->end_code
= interp_ex
->a_text
;
343 text_data
= interp_ex
->a_text
+ interp_ex
->a_data
;
344 current
->mm
->end_data
= text_data
;
345 current
->mm
->brk
= interp_ex
->a_bss
+ text_data
;
347 switch (N_MAGIC(*interp_ex
)) {
354 offset
= N_TXTOFF(*interp_ex
);
355 addr
= (char *) N_TXTADDR(*interp_ex
);
361 do_brk(0, text_data
);
362 retval
= read_exec(interpreter_dentry
, offset
, addr
, text_data
, 0);
365 flush_icache_range((unsigned long)addr
,
366 (unsigned long)addr
+ text_data
);
368 do_brk(ELF_PAGESTART(text_data
+ ELF_EXEC_PAGESIZE
- 1),
370 elf_entry
= interp_ex
->a_entry
;
377 * These are the functions used to load ELF style executables and shared
378 * libraries. There is no binary dependent code anywhere else.
381 #define INTERPRETER_NONE 0
382 #define INTERPRETER_AOUT 1
383 #define INTERPRETER_ELF 2
387 do_load_elf_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
390 struct dentry
*interpreter_dentry
= NULL
; /* to shut gcc up */
391 unsigned long load_addr
= 0, load_bias
;
392 int load_addr_set
= 0;
393 char * elf_interpreter
= NULL
;
394 unsigned int interpreter_type
= INTERPRETER_NONE
;
395 unsigned char ibcs2_interpreter
= 0;
398 struct elf_phdr
* elf_ppnt
, *elf_phdata
;
399 unsigned long elf_bss
, k
, elf_brk
;
402 unsigned long elf_entry
, interp_load_addr
= 0;
403 unsigned long start_code
, end_code
, start_data
, end_data
;
404 struct elfhdr elf_ex
;
405 struct elfhdr interp_elf_ex
;
406 struct exec interp_ex
;
407 char passed_fileno
[6];
409 /* Get the exec-header */
410 elf_ex
= *((struct elfhdr
*) bprm
->buf
);
413 /* First of all, some simple consistency checks */
414 if (memcmp(elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
417 if (elf_ex
.e_type
!= ET_EXEC
&& elf_ex
.e_type
!= ET_DYN
)
419 if (!elf_check_arch(elf_ex
.e_machine
))
422 /* IRIX binaries handled elsewhere. */
423 if (elf_ex
.e_flags
& EF_MIPS_ARCH
) {
428 if (!bprm
->dentry
->d_inode
->i_fop
||!bprm
->dentry
->d_inode
->i_fop
->mmap
)
431 /* Now read in all of the header information */
434 size
= elf_ex
.e_phentsize
* elf_ex
.e_phnum
;
437 elf_phdata
= (struct elf_phdr
*) kmalloc(size
, GFP_KERNEL
);
441 retval
= read_exec(bprm
->dentry
, elf_ex
.e_phoff
,
442 (char *) elf_phdata
, size
, 1);
446 retval
= open_dentry(bprm
->dentry
, O_RDONLY
);
449 elf_exec_fileno
= retval
;
450 file
= fget(elf_exec_fileno
);
452 elf_ppnt
= elf_phdata
;
461 for (i
= 0; i
< elf_ex
.e_phnum
; i
++) {
462 if (elf_ppnt
->p_type
== PT_INTERP
) {
465 goto out_free_interp
;
467 /* This is the program interpreter used for
468 * shared libraries - for now assume that this
469 * is an a.out format binary
473 elf_interpreter
= (char *) kmalloc(elf_ppnt
->p_filesz
,
475 if (!elf_interpreter
)
478 retval
= read_exec(bprm
->dentry
, elf_ppnt
->p_offset
,
480 elf_ppnt
->p_filesz
, 1);
482 goto out_free_interp
;
483 /* If the program interpreter is one of these two,
484 * then assume an iBCS2 image. Otherwise assume
485 * a native linux image.
487 if (strcmp(elf_interpreter
,"/usr/lib/libc.so.1") == 0 ||
488 strcmp(elf_interpreter
,"/usr/lib/ld.so.1") == 0)
489 ibcs2_interpreter
= 1;
491 printk("Using ELF interpreter %s\n", elf_interpreter
);
493 old_fs
= get_fs(); /* This could probably be optimized */
496 if (ibcs2_interpreter
) {
497 unsigned long old_pers
= current
->personality
;
499 current
->personality
= PER_SVR4
;
501 interpreter_dentry
= open_namei(elf_interpreter
,
504 current
->personality
= old_pers
;
509 interpreter_dentry
= open_namei(elf_interpreter
,
514 retval
= PTR_ERR(interpreter_dentry
);
515 if (IS_ERR(interpreter_dentry
))
516 goto out_free_interp
;
517 retval
= permission(interpreter_dentry
->d_inode
, MAY_EXEC
);
519 goto out_free_dentry
;
520 retval
= read_exec(interpreter_dentry
, 0, bprm
->buf
, 128, 1);
522 goto out_free_dentry
;
524 /* Get the exec headers */
525 interp_ex
= *((struct exec
*) bprm
->buf
);
526 interp_elf_ex
= *((struct elfhdr
*) bprm
->buf
);
531 /* Some simple consistency checks for the interpreter */
532 if (elf_interpreter
) {
533 interpreter_type
= INTERPRETER_ELF
| INTERPRETER_AOUT
;
535 /* Now figure out which format our binary is */
536 if ((N_MAGIC(interp_ex
) != OMAGIC
) &&
537 (N_MAGIC(interp_ex
) != ZMAGIC
) &&
538 (N_MAGIC(interp_ex
) != QMAGIC
))
539 interpreter_type
= INTERPRETER_ELF
;
541 if (memcmp(interp_elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
542 interpreter_type
&= ~INTERPRETER_ELF
;
545 if (!interpreter_type
)
546 goto out_free_dentry
;
548 /* Make sure only one type was selected */
549 if ((interpreter_type
& INTERPRETER_ELF
) &&
550 interpreter_type
!= INTERPRETER_ELF
) {
551 printk(KERN_WARNING
"ELF: Ambiguous type, using ELF\n");
552 interpreter_type
= INTERPRETER_ELF
;
556 /* OK, we are done with that, now set up the arg stuff,
557 and then start this sucker up */
559 if (!bprm
->sh_bang
) {
562 if (interpreter_type
== INTERPRETER_AOUT
) {
563 sprintf(passed_fileno
, "%d", elf_exec_fileno
);
564 passed_p
= passed_fileno
;
566 if (elf_interpreter
) {
567 retval
= copy_strings_kernel(1,&passed_p
,bprm
);
569 goto out_free_dentry
;
575 /* Flush all traces of the currently running executable */
576 retval
= flush_old_exec(bprm
);
578 goto out_free_dentry
;
580 /* OK, This is the point of no return */
581 current
->mm
->start_data
= 0;
582 current
->mm
->end_data
= 0;
583 current
->mm
->end_code
= 0;
584 current
->mm
->mmap
= NULL
;
585 current
->flags
&= ~PF_FORKNOEXEC
;
586 elf_entry
= (unsigned long) elf_ex
.e_entry
;
588 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
589 may depend on the personality. */
590 SET_PERSONALITY(elf_ex
, ibcs2_interpreter
);
592 /* Do this so that we can load the interpreter, if need be. We will
593 change some of these later */
594 current
->mm
->rss
= 0;
595 setup_arg_pages(bprm
); /* XXX: check error */
596 current
->mm
->start_stack
= bprm
->p
;
598 /* Try and get dynamic programs out of the way of the default mmap
599 base, as well as whatever program they might try to exec. This
600 is because the brk will follow the loader, and is not movable. */
602 load_bias
= ELF_PAGESTART(elf_ex
.e_type
==ET_DYN
? ELF_ET_DYN_BASE
: 0);
604 /* Now we do a little grungy work by mmaping the ELF image into
605 the correct location in memory. At this point, we assume that
606 the image should be loaded at fixed address, not at a variable
611 for(i
= 0, elf_ppnt
= elf_phdata
; i
< elf_ex
.e_phnum
; i
++, elf_ppnt
++) {
612 int elf_prot
= 0, elf_flags
;
615 if (elf_ppnt
->p_type
!= PT_LOAD
)
618 if (elf_ppnt
->p_flags
& PF_R
) elf_prot
|= PROT_READ
;
619 if (elf_ppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
620 if (elf_ppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
622 elf_flags
= MAP_PRIVATE
|MAP_DENYWRITE
|MAP_EXECUTABLE
;
624 vaddr
= elf_ppnt
->p_vaddr
;
625 if (elf_ex
.e_type
== ET_EXEC
|| load_addr_set
) {
626 elf_flags
|= MAP_FIXED
;
629 error
= do_mmap(file
, ELF_PAGESTART(load_bias
+ vaddr
),
630 (elf_ppnt
->p_filesz
+
631 ELF_PAGEOFFSET(elf_ppnt
->p_vaddr
)),
632 elf_prot
, elf_flags
, (elf_ppnt
->p_offset
-
633 ELF_PAGEOFFSET(elf_ppnt
->p_vaddr
)));
635 if (!load_addr_set
) {
637 load_addr
= (elf_ppnt
->p_vaddr
- elf_ppnt
->p_offset
);
638 if (elf_ex
.e_type
== ET_DYN
) {
640 ELF_PAGESTART(load_bias
+ vaddr
);
644 k
= elf_ppnt
->p_vaddr
;
645 if (k
< start_code
) start_code
= k
;
646 if (start_data
< k
) start_data
= k
;
648 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_filesz
;
652 if ((elf_ppnt
->p_flags
& PF_X
) && end_code
< k
)
656 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_memsz
;
661 fput(file
); /* all done with the file */
663 elf_entry
+= load_bias
;
664 elf_bss
+= load_bias
;
665 elf_brk
+= load_bias
;
666 start_code
+= load_bias
;
667 end_code
+= load_bias
;
668 start_data
+= load_bias
;
669 end_data
+= load_bias
;
671 if (elf_interpreter
) {
672 if (interpreter_type
== INTERPRETER_AOUT
)
673 elf_entry
= load_aout_interp(&interp_ex
,
676 elf_entry
= load_elf_interp(&interp_elf_ex
,
681 dput(interpreter_dentry
);
683 kfree(elf_interpreter
);
685 if (elf_entry
== ~0UL) {
686 printk(KERN_ERR
"Unable to load interpreter\n");
688 send_sig(SIGSEGV
, current
, 0);
695 if (interpreter_type
!= INTERPRETER_AOUT
)
696 sys_close(elf_exec_fileno
);
698 if (current
->exec_domain
&& current
->exec_domain
->module
)
699 __MOD_DEC_USE_COUNT(current
->exec_domain
->module
);
700 if (current
->binfmt
&& current
->binfmt
->module
)
701 __MOD_DEC_USE_COUNT(current
->binfmt
->module
);
702 current
->exec_domain
= lookup_exec_domain(current
->personality
);
703 current
->binfmt
= &elf_format
;
704 if (current
->exec_domain
&& current
->exec_domain
->module
)
705 __MOD_INC_USE_COUNT(current
->exec_domain
->module
);
706 if (current
->binfmt
&& current
->binfmt
->module
)
707 __MOD_INC_USE_COUNT(current
->binfmt
->module
);
709 #ifndef VM_STACK_FLAGS
710 current
->executable
= dget(bprm
->dentry
);
713 current
->flags
&= ~PF_FORKNOEXEC
;
714 bprm
->p
= (unsigned long)
715 create_elf_tables((char *)bprm
->p
,
718 (interpreter_type
== INTERPRETER_ELF
? &elf_ex
: NULL
),
719 load_addr
, load_bias
,
721 (interpreter_type
== INTERPRETER_AOUT
? 0 : 1));
722 /* N.B. passed_fileno might not be initialized? */
723 if (interpreter_type
== INTERPRETER_AOUT
)
724 current
->mm
->arg_start
+= strlen(passed_fileno
) + 1;
725 current
->mm
->start_brk
= current
->mm
->brk
= elf_brk
;
726 current
->mm
->end_code
= end_code
;
727 current
->mm
->start_code
= start_code
;
728 current
->mm
->start_data
= start_data
;
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
733 * for the bss and break sections
735 set_brk(elf_bss
, elf_brk
);
740 printk("(start_brk) %lx\n" , (long) current
->mm
->start_brk
);
741 printk("(end_code) %lx\n" , (long) current
->mm
->end_code
);
742 printk("(start_code) %lx\n" , (long) current
->mm
->start_code
);
743 printk("(start_data) %lx\n" , (long) current
->mm
->start_data
);
744 printk("(end_data) %lx\n" , (long) current
->mm
->end_data
);
745 printk("(start_stack) %lx\n" , (long) current
->mm
->start_stack
);
746 printk("(brk) %lx\n" , (long) current
->mm
->brk
);
749 if ( current
->personality
== PER_SVR4
)
751 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
752 and some applications "depend" upon this behavior.
753 Since we do not have the power to recompile these, we
754 emulate the SVr4 behavior. Sigh. */
755 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
756 error
= do_mmap(NULL
, 0, 4096, PROT_READ
| PROT_EXEC
,
757 MAP_FIXED
| MAP_PRIVATE
, 0);
762 * The ABI may specify that certain registers be set up in special
763 * ways (on i386 %edx is the address of a DT_FINI function, for
764 * example. This macro performs whatever initialization to
765 * the regs structure is required.
770 start_thread(regs
, elf_entry
, bprm
->p
);
771 if (current
->flags
& PF_PTRACED
)
772 send_sig(SIGTRAP
, current
, 0);
780 dput(interpreter_dentry
);
784 kfree(elf_interpreter
);
787 sys_close(elf_exec_fileno
);
794 load_elf_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
799 retval
= do_load_elf_binary(bprm
, regs
);
804 /* This is really simpleminded and specialized - we are loading an
805 a.out library that is given an ELF header. */
808 do_load_elf_library(int fd
)
811 struct dentry
* dentry
;
812 struct inode
* inode
;
813 struct elf_phdr
*elf_phdata
;
814 unsigned long elf_bss
= 0, bss
, len
, k
;
815 int retval
, error
, i
, j
;
816 struct elfhdr elf_ex
;
821 if (!file
|| !file
->f_op
)
823 dentry
= file
->f_dentry
;
824 inode
= dentry
->d_inode
;
826 /* seek to the beginning of the file */
829 /* N.B. save current DS?? */
831 retval
= file
->f_op
->read(file
, (char *) &elf_ex
, sizeof(elf_ex
), &offset
);
833 if (retval
!= sizeof(elf_ex
))
836 if (memcmp(elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
839 /* First of all, some simple consistency checks */
840 if (elf_ex
.e_type
!= ET_EXEC
|| elf_ex
.e_phnum
> 2 ||
841 !elf_check_arch(elf_ex
.e_machine
) ||
842 (!inode
->i_fop
|| !inode
->i_fop
->mmap
))
845 /* Now read in all of the header information */
847 j
= sizeof(struct elf_phdr
) * elf_ex
.e_phnum
;
848 if (j
> ELF_EXEC_PAGESIZE
)
852 elf_phdata
= (struct elf_phdr
*) kmalloc(j
, GFP_KERNEL
);
856 /* N.B. check for error return?? */
857 retval
= read_exec(dentry
, elf_ex
.e_phoff
, (char *) elf_phdata
,
858 sizeof(struct elf_phdr
) * elf_ex
.e_phnum
, 1);
861 for (j
= 0, i
= 0; i
<elf_ex
.e_phnum
; i
++)
862 if ((elf_phdata
+ i
)->p_type
== PT_LOAD
) j
++;
866 while (elf_phdata
->p_type
!= PT_LOAD
) elf_phdata
++;
868 /* Now use mmap to map the library into memory. */
869 error
= do_mmap(file
,
870 ELF_PAGESTART(elf_phdata
->p_vaddr
),
871 (elf_phdata
->p_filesz
+
872 ELF_PAGEOFFSET(elf_phdata
->p_vaddr
)),
873 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
874 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
,
875 (elf_phdata
->p_offset
-
876 ELF_PAGEOFFSET(elf_phdata
->p_vaddr
)));
877 if (error
!= ELF_PAGESTART(elf_phdata
->p_vaddr
))
880 k
= elf_phdata
->p_vaddr
+ elf_phdata
->p_filesz
;
885 len
= ELF_PAGESTART(elf_phdata
->p_filesz
+ elf_phdata
->p_vaddr
+
886 ELF_EXEC_PAGESIZE
- 1);
887 bss
= elf_phdata
->p_memsz
+ elf_phdata
->p_vaddr
;
889 do_brk(len
, bss
- len
);
900 static int load_elf_library(int fd
)
905 retval
= do_load_elf_library(fd
);
911 * Note that some platforms still use traditional core dumps and not
912 * the ELF core dump. Each platform can select it as appropriate.
914 #ifdef USE_ELF_CORE_DUMP
919 * Modelled on fs/exec.c:aout_core_dump()
920 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
923 * These are the only things you should do on a core-file: use only these
924 * functions to write out all the necessary info.
926 static int dump_write(struct file
*file
, const void *addr
, int nr
)
928 return file
->f_op
->write(file
, addr
, nr
, &file
->f_pos
) == nr
;
931 static int dump_seek(struct file
*file
, off_t off
)
933 if (file
->f_op
->llseek
) {
934 if (file
->f_op
->llseek(file
, off
, 0) != off
)
942 * Decide whether a segment is worth dumping; default is yes to be
943 * sure (missing info is worse than too much; etc).
944 * Personally I'd include everything, and use the coredump limit...
946 * I think we should skip something. But I am not sure how. H.J.
948 static inline int maydump(struct vm_area_struct
*vma
)
950 if (!(vma
->vm_flags
& (VM_READ
|VM_WRITE
|VM_EXEC
)))
953 /* Do not dump I/O mapped devices! -DaveM */
954 if(vma
->vm_flags
& VM_IO
)
957 if (vma
->vm_flags
& (VM_WRITE
|VM_GROWSUP
|VM_GROWSDOWN
))
959 if (vma
->vm_flags
& (VM_READ
|VM_EXEC
|VM_EXECUTABLE
|VM_SHARED
))
965 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
967 /* An ELF note in memory */
976 static int notesize(struct memelfnote
*en
)
980 sz
= sizeof(struct elf_note
);
981 sz
+= roundup(strlen(en
->name
), 4);
982 sz
+= roundup(en
->datasz
, 4);
990 static void dump_regs(const char *str
, elf_greg_t
*r
)
993 static const char *regs
[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
994 "eax", "ds", "es", "fs", "gs",
995 "orig_eax", "eip", "cs",
996 "efl", "uesp", "ss"};
997 printk("Registers: %s\n", str
);
999 for(i
= 0; i
< ELF_NGREG
; i
++)
1001 unsigned long val
= r
[i
];
1002 printk(" %-2d %-5s=%08lx %lu\n", i
, regs
[i
], val
, val
);
1007 #define DUMP_WRITE(addr, nr) \
1008 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1009 #define DUMP_SEEK(off) \
1010 do { if (!dump_seek(file, (off))) return 0; } while(0)
1012 static int writenote(struct memelfnote
*men
, struct file
*file
)
1016 en
.n_namesz
= strlen(men
->name
);
1017 en
.n_descsz
= men
->datasz
;
1018 en
.n_type
= men
->type
;
1020 DUMP_WRITE(&en
, sizeof(en
));
1021 DUMP_WRITE(men
->name
, en
.n_namesz
);
1022 /* XXX - cast from long long to long to avoid need for libgcc.a */
1023 DUMP_SEEK(roundup((unsigned long)file
->f_pos
, 4)); /* XXX */
1024 DUMP_WRITE(men
->data
, men
->datasz
);
1025 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 elf_core_dump(long signr
, struct pt_regs
* regs
, struct file
* file
)
1052 struct vm_area_struct
*vma
;
1054 off_t offset
= 0, dataoff
;
1055 unsigned long limit
= current
->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 #ifndef CONFIG_BINFMT_ELF
1066 /* Count what's needed to dump, up to the limit of coredump size */
1069 for(vma
= current
->mm
->mmap
; vma
!= NULL
; vma
= vma
->vm_next
) {
1072 unsigned long sz
= vma
->vm_end
-vma
->vm_start
;
1074 if (size
+sz
>= limit
)
1083 printk("elf_core_dump: %d segs taking %d bytes\n", segs
, size
);
1087 memcpy(elf
.e_ident
, ELFMAG
, SELFMAG
);
1088 elf
.e_ident
[EI_CLASS
] = ELF_CLASS
;
1089 elf
.e_ident
[EI_DATA
] = ELF_DATA
;
1090 elf
.e_ident
[EI_VERSION
] = EV_CURRENT
;
1091 memset(elf
.e_ident
+EI_PAD
, 0, EI_NIDENT
-EI_PAD
);
1093 elf
.e_type
= ET_CORE
;
1094 elf
.e_machine
= ELF_ARCH
;
1095 elf
.e_version
= EV_CURRENT
;
1097 elf
.e_phoff
= sizeof(elf
);
1100 elf
.e_ehsize
= sizeof(elf
);
1101 elf
.e_phentsize
= sizeof(struct elf_phdr
);
1102 elf
.e_phnum
= segs
+1; /* Include notes */
1103 elf
.e_shentsize
= 0;
1111 current
->flags
|= PF_DUMPCORE
;
1113 DUMP_WRITE(&elf
, sizeof(elf
));
1114 offset
+= sizeof(elf
); /* Elf header */
1115 offset
+= (segs
+1) * sizeof(struct elf_phdr
); /* Program headers */
1118 * Set up the notes in similar form to SVR4 core dumps made
1119 * with info from their /proc.
1121 memset(&psinfo
, 0, sizeof(psinfo
));
1122 memset(&prstatus
, 0, sizeof(prstatus
));
1124 notes
[0].name
= "CORE";
1125 notes
[0].type
= NT_PRSTATUS
;
1126 notes
[0].datasz
= sizeof(prstatus
);
1127 notes
[0].data
= &prstatus
;
1128 prstatus
.pr_info
.si_signo
= prstatus
.pr_cursig
= signr
;
1129 prstatus
.pr_sigpend
= current
->signal
.sig
[0];
1130 prstatus
.pr_sighold
= current
->blocked
.sig
[0];
1131 psinfo
.pr_pid
= prstatus
.pr_pid
= current
->pid
;
1132 psinfo
.pr_ppid
= prstatus
.pr_ppid
= current
->p_pptr
->pid
;
1133 psinfo
.pr_pgrp
= prstatus
.pr_pgrp
= current
->pgrp
;
1134 psinfo
.pr_sid
= prstatus
.pr_sid
= current
->session
;
1135 prstatus
.pr_utime
.tv_sec
= CT_TO_SECS(current
->times
.tms_utime
);
1136 prstatus
.pr_utime
.tv_usec
= CT_TO_USECS(current
->times
.tms_utime
);
1137 prstatus
.pr_stime
.tv_sec
= CT_TO_SECS(current
->times
.tms_stime
);
1138 prstatus
.pr_stime
.tv_usec
= CT_TO_USECS(current
->times
.tms_stime
);
1139 prstatus
.pr_cutime
.tv_sec
= CT_TO_SECS(current
->times
.tms_cutime
);
1140 prstatus
.pr_cutime
.tv_usec
= CT_TO_USECS(current
->times
.tms_cutime
);
1141 prstatus
.pr_cstime
.tv_sec
= CT_TO_SECS(current
->times
.tms_cstime
);
1142 prstatus
.pr_cstime
.tv_usec
= CT_TO_USECS(current
->times
.tms_cstime
);
1145 * This transfers the registers from regs into the standard
1146 * coredump arrangement, whatever that is.
1148 #ifdef ELF_CORE_COPY_REGS
1149 ELF_CORE_COPY_REGS(prstatus
.pr_reg
, regs
)
1151 if (sizeof(elf_gregset_t
) != sizeof(struct pt_regs
))
1153 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1154 (long)sizeof(elf_gregset_t
), (long)sizeof(struct pt_regs
));
1157 *(struct pt_regs
*)&prstatus
.pr_reg
= *regs
;
1161 dump_regs("Passed in regs", (elf_greg_t
*)regs
);
1162 dump_regs("prstatus regs", (elf_greg_t
*)&prstatus
.pr_reg
);
1165 notes
[1].name
= "CORE";
1166 notes
[1].type
= NT_PRPSINFO
;
1167 notes
[1].datasz
= sizeof(psinfo
);
1168 notes
[1].data
= &psinfo
;
1169 i
= current
->state
? ffz(~current
->state
) + 1 : 0;
1170 psinfo
.pr_state
= i
;
1171 psinfo
.pr_sname
= (i
< 0 || i
> 5) ? '.' : "RSDZTD"[i
];
1172 psinfo
.pr_zomb
= psinfo
.pr_sname
== 'Z';
1173 psinfo
.pr_nice
= current
->priority
-15;
1174 psinfo
.pr_flag
= current
->flags
;
1175 psinfo
.pr_uid
= NEW_TO_OLD_UID(current
->uid
);
1176 psinfo
.pr_gid
= NEW_TO_OLD_GID(current
->gid
);
1182 len
= current
->mm
->arg_end
- current
->mm
->arg_start
;
1183 if (len
>= ELF_PRARGSZ
)
1184 len
= ELF_PRARGSZ
-1;
1185 copy_from_user(&psinfo
.pr_psargs
,
1186 (const char *)current
->mm
->arg_start
, len
);
1187 for(i
= 0; i
< len
; i
++)
1188 if (psinfo
.pr_psargs
[i
] == 0)
1189 psinfo
.pr_psargs
[i
] = ' ';
1190 psinfo
.pr_psargs
[len
] = 0;
1194 strncpy(psinfo
.pr_fname
, current
->comm
, sizeof(psinfo
.pr_fname
));
1196 notes
[2].name
= "CORE";
1197 notes
[2].type
= NT_TASKSTRUCT
;
1198 notes
[2].datasz
= sizeof(*current
);
1199 notes
[2].data
= current
;
1201 /* Try to dump the FPU. */
1202 prstatus
.pr_fpvalid
= dump_fpu (regs
, &fpu
);
1203 if (!prstatus
.pr_fpvalid
)
1209 notes
[3].name
= "CORE";
1210 notes
[3].type
= NT_PRFPREG
;
1211 notes
[3].datasz
= sizeof(fpu
);
1212 notes
[3].data
= &fpu
;
1215 /* Write notes phdr entry */
1217 struct elf_phdr phdr
;
1220 for(i
= 0; i
< numnote
; i
++)
1221 sz
+= notesize(¬es
[i
]);
1223 phdr
.p_type
= PT_NOTE
;
1224 phdr
.p_offset
= offset
;
1232 offset
+= phdr
.p_filesz
;
1233 DUMP_WRITE(&phdr
, sizeof(phdr
));
1236 /* Page-align dumped data */
1237 dataoff
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
1239 /* Write program headers for segments dump */
1240 for(vma
= current
->mm
->mmap
, i
= 0;
1241 i
< segs
&& vma
!= NULL
; vma
= vma
->vm_next
) {
1242 struct elf_phdr phdr
;
1247 sz
= vma
->vm_end
- vma
->vm_start
;
1249 phdr
.p_type
= PT_LOAD
;
1250 phdr
.p_offset
= offset
;
1251 phdr
.p_vaddr
= vma
->vm_start
;
1253 phdr
.p_filesz
= maydump(vma
) ? sz
: 0;
1255 offset
+= phdr
.p_filesz
;
1256 phdr
.p_flags
= vma
->vm_flags
& VM_READ
? PF_R
: 0;
1257 if (vma
->vm_flags
& VM_WRITE
) phdr
.p_flags
|= PF_W
;
1258 if (vma
->vm_flags
& VM_EXEC
) phdr
.p_flags
|= PF_X
;
1259 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
1261 DUMP_WRITE(&phdr
, sizeof(phdr
));
1264 for(i
= 0; i
< numnote
; i
++)
1265 if (!writenote(¬es
[i
], file
))
1272 for(i
= 0, vma
= current
->mm
->mmap
;
1273 i
< segs
&& vma
!= NULL
;
1274 vma
= vma
->vm_next
) {
1275 unsigned long addr
= vma
->vm_start
;
1276 unsigned long len
= vma
->vm_end
- vma
->vm_start
;
1282 printk("elf_core_dump: writing %08lx %lx\n", addr
, len
);
1284 DUMP_WRITE((void *)addr
, len
);
1287 if ((off_t
) file
->f_pos
!= offset
) {
1289 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1290 (off_t
) file
->f_pos
, offset
);
1295 #ifndef CONFIG_BINFMT_ELF
1300 #endif /* USE_ELF_CORE_DUMP */
1302 static int __init
init_elf_binfmt(void)
1304 return register_binfmt(&elf_format
);
1307 static void __exit
exit_elf_binfmt(void)
1309 /* Remove the COFF and ELF loaders. */
1310 unregister_binfmt(&elf_format
);
1313 module_init(init_elf_binfmt
)
1314 module_exit(exit_elf_binfmt
)