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>
33 #include <asm/uaccess.h>
34 #include <asm/pgtable.h>
36 #include <linux/config.h>
38 #define DLINFO_ITEMS 13
40 #include <linux/elf.h>
42 static int load_elf_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
);
43 static int load_elf_library(int fd
);
44 extern int dump_fpu (struct pt_regs
*, elf_fpregset_t
*);
45 extern void dump_thread(struct pt_regs
*, struct user
*);
48 extern unsigned long get_unmapped_area(unsigned long addr
, unsigned long len
);
52 #define elf_addr_t unsigned long
53 #define elf_caddr_t char *
57 * If we don't support core dumping, then supply a NULL so we
60 #ifdef USE_ELF_CORE_DUMP
61 static int elf_core_dump(long signr
, struct pt_regs
* regs
);
63 #define elf_core_dump NULL
66 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_EXEC_PAGESIZE-1))
67 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_EXEC_PAGESIZE-1))
68 #define ELF_PAGEALIGN(_v) (((_v) + ELF_EXEC_PAGESIZE - 1) & ~(ELF_EXEC_PAGESIZE - 1))
70 static struct linux_binfmt elf_format
= {
72 NULL
, NULL
, load_elf_binary
, load_elf_library
, elf_core_dump
74 NULL
, &__this_module
, load_elf_binary
, load_elf_library
, elf_core_dump
78 static void set_brk(unsigned long start
, unsigned long end
)
80 start
= ELF_PAGEALIGN(start
);
81 end
= ELF_PAGEALIGN(end
);
84 do_mmap(NULL
, start
, end
- start
,
85 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
86 MAP_FIXED
| MAP_PRIVATE
, 0);
90 /* We need to explicitly zero any fractional pages
91 after the data section (i.e. bss). This would
92 contain the junk from the file that should not
96 static void padzero(unsigned long elf_bss
)
100 nbyte
= ELF_PAGEOFFSET(elf_bss
);
102 nbyte
= ELF_EXEC_PAGESIZE
- nbyte
;
103 clear_user((void *) elf_bss
, nbyte
);
108 create_elf_tables(char *p
, int argc
, int envc
,
109 struct elfhdr
* exec
,
110 unsigned long load_addr
,
111 unsigned long load_bias
,
112 unsigned long interp_load_addr
, int ibcs
)
116 elf_addr_t
*sp
, *csp
;
117 char *k_platform
, *u_platform
;
119 size_t platform_len
= 0;
122 * Get hold of platform and hardware capabilities masks for
123 * the machine we are running on. In some cases (Sparc),
124 * this info is impossible to get, in others (i386) it is
129 k_platform
= ELF_PLATFORM
;
132 platform_len
= strlen(k_platform
) + 1;
133 u_platform
= p
- platform_len
;
134 __copy_to_user(u_platform
, k_platform
, platform_len
);
139 * Force 16 byte _final_ alignment here for generality.
140 * Leave an extra 16 bytes free so that on the PowerPC we
141 * can move the aux table up to start on a 16-byte boundary.
143 sp
= (elf_addr_t
*)((~15UL & (unsigned long)(u_platform
)) - 16UL);
145 csp
-= ((exec
? DLINFO_ITEMS
*2 : 4) + (k_platform
? 2 : 0));
148 csp
-= (!ibcs
? 3 : 1); /* argc itself */
149 if ((unsigned long)csp
& 15UL)
150 sp
-= ((unsigned long)csp
& 15UL) / sizeof(*sp
);
153 * Put the ELF interpreter info on the stack
155 #define NEW_AUX_ENT(nr, id, val) \
156 __put_user ((id), sp+(nr*2)); \
157 __put_user ((val), sp+(nr*2+1)); \
160 NEW_AUX_ENT(0, AT_NULL
, 0);
163 NEW_AUX_ENT(0, AT_PLATFORM
, (elf_addr_t
)(unsigned long) u_platform
);
166 NEW_AUX_ENT(0, AT_HWCAP
, hwcap
);
171 NEW_AUX_ENT(0, AT_PHDR
, load_addr
+ exec
->e_phoff
);
172 NEW_AUX_ENT(1, AT_PHENT
, sizeof (struct elf_phdr
));
173 NEW_AUX_ENT(2, AT_PHNUM
, exec
->e_phnum
);
174 NEW_AUX_ENT(3, AT_PAGESZ
, ELF_EXEC_PAGESIZE
);
175 NEW_AUX_ENT(4, AT_BASE
, interp_load_addr
);
176 NEW_AUX_ENT(5, AT_FLAGS
, 0);
177 NEW_AUX_ENT(6, AT_ENTRY
, load_bias
+ exec
->e_entry
);
178 NEW_AUX_ENT(7, AT_UID
, (elf_addr_t
) current
->uid
);
179 NEW_AUX_ENT(8, AT_EUID
, (elf_addr_t
) current
->euid
);
180 NEW_AUX_ENT(9, AT_GID
, (elf_addr_t
) current
->gid
);
181 NEW_AUX_ENT(10, AT_EGID
, (elf_addr_t
) current
->egid
);
186 envp
= (elf_caddr_t
*) sp
;
188 argv
= (elf_caddr_t
*) sp
;
190 __put_user((elf_addr_t
)(unsigned long) envp
,--sp
);
191 __put_user((elf_addr_t
)(unsigned long) argv
,--sp
);
194 __put_user((elf_addr_t
)argc
,--sp
);
195 current
->mm
->arg_start
= (unsigned long) p
;
197 __put_user((elf_caddr_t
)(unsigned long)p
,argv
++);
200 __put_user(NULL
, argv
);
201 current
->mm
->arg_end
= current
->mm
->env_start
= (unsigned long) p
;
203 __put_user((elf_caddr_t
)(unsigned long)p
,envp
++);
206 __put_user(NULL
, envp
);
207 current
->mm
->env_end
= (unsigned long) p
;
212 /* This is much more generalized than the library routine read function,
213 so we keep this separate. Technically the library read function
214 is only provided so that we can read a.out libraries that have
217 static unsigned long load_elf_interp(struct elfhdr
* interp_elf_ex
,
218 struct dentry
* interpreter_dentry
,
219 unsigned long *interp_load_addr
)
222 struct elf_phdr
*elf_phdata
;
223 struct elf_phdr
*eppnt
;
224 unsigned long load_addr
= 0;
225 int load_addr_set
= 0;
226 unsigned long last_bss
= 0, elf_bss
= 0;
227 unsigned long error
= ~0UL;
231 /* First of all, some simple consistency checks */
232 if (interp_elf_ex
->e_type
!= ET_EXEC
&&
233 interp_elf_ex
->e_type
!= ET_DYN
)
235 if (!elf_check_arch(interp_elf_ex
->e_machine
))
237 if (!interpreter_dentry
->d_inode
->i_op
||
238 !interpreter_dentry
->d_inode
->i_op
->default_file_ops
->mmap
)
242 * If the size of this structure has changed, then punt, since
243 * we will be doing the wrong thing.
245 if (interp_elf_ex
->e_phentsize
!= sizeof(struct elf_phdr
))
248 /* Now read in all of the header information */
250 size
= sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
;
251 if (size
> ELF_EXEC_PAGESIZE
)
253 elf_phdata
= (struct elf_phdr
*) kmalloc(size
, GFP_KERNEL
);
257 retval
= read_exec(interpreter_dentry
, interp_elf_ex
->e_phoff
,
258 (char *) elf_phdata
, size
, 1);
264 elf_exec_fileno
= open_dentry(interpreter_dentry
, O_RDONLY
);
265 if (elf_exec_fileno
< 0)
267 file
= fget(elf_exec_fileno
);
270 for (i
=0; i
<interp_elf_ex
->e_phnum
; i
++, eppnt
++) {
271 if (eppnt
->p_type
== PT_LOAD
) {
272 int elf_type
= MAP_PRIVATE
| MAP_DENYWRITE
;
274 unsigned long vaddr
= 0;
275 unsigned long k
, map_addr
;
277 if (eppnt
->p_flags
& PF_R
) elf_prot
= PROT_READ
;
278 if (eppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
279 if (eppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
280 if (interp_elf_ex
->e_type
== ET_EXEC
|| load_addr_set
) {
281 elf_type
|= MAP_FIXED
;
282 vaddr
= eppnt
->p_vaddr
;
285 load_addr
= get_unmapped_area(0, eppnt
->p_filesz
+
286 ELF_PAGEOFFSET(eppnt
->p_vaddr
));
290 map_addr
= do_mmap(file
,
291 load_addr
+ ELF_PAGESTART(vaddr
),
292 eppnt
->p_filesz
+ ELF_PAGEOFFSET(eppnt
->p_vaddr
),
295 eppnt
->p_offset
- ELF_PAGEOFFSET(eppnt
->p_vaddr
));
296 if (map_addr
> -1024UL) /* Real error */
299 if (!load_addr_set
&& interp_elf_ex
->e_type
== ET_DYN
) {
300 load_addr
= map_addr
;
305 * Find the end of the file mapping for this phdr, and keep
306 * track of the largest address we see for this.
308 k
= load_addr
+ eppnt
->p_vaddr
+ eppnt
->p_filesz
;
313 * Do the same thing for the memory mapping - between
314 * elf_bss and last_bss is the bss section.
316 k
= load_addr
+ eppnt
->p_memsz
+ eppnt
->p_vaddr
;
322 /* Now use mmap to map the library into memory. */
325 * Now fill out the bss section. First pad the last page up
326 * to the page boundary, and then perform a mmap to make sure
327 * that there are zero-mapped pages up to and including the
331 elf_bss
= ELF_PAGESTART(elf_bss
+ ELF_EXEC_PAGESIZE
- 1); /* What we have mapped so far */
333 /* Map the last of the bss segment */
334 if (last_bss
> elf_bss
)
335 do_mmap(NULL
, elf_bss
, last_bss
- elf_bss
,
336 PROT_READ
|PROT_WRITE
|PROT_EXEC
,
337 MAP_FIXED
|MAP_PRIVATE
, 0);
339 *interp_load_addr
= load_addr
;
340 error
= ((unsigned long) interp_elf_ex
->e_entry
) + load_addr
;
344 sys_close(elf_exec_fileno
);
351 static unsigned long load_aout_interp(struct exec
* interp_ex
,
352 struct dentry
* interpreter_dentry
)
354 unsigned long text_data
, offset
, elf_entry
= ~0UL;
358 current
->mm
->end_code
= interp_ex
->a_text
;
359 text_data
= interp_ex
->a_text
+ interp_ex
->a_data
;
360 current
->mm
->end_data
= text_data
;
361 current
->mm
->brk
= interp_ex
->a_bss
+ text_data
;
363 switch (N_MAGIC(*interp_ex
)) {
370 offset
= N_TXTOFF(*interp_ex
);
371 addr
= (char *) N_TXTADDR(*interp_ex
);
377 do_mmap(NULL
, 0, text_data
,
378 PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_FIXED
|MAP_PRIVATE
, 0);
379 retval
= read_exec(interpreter_dentry
, offset
, addr
, text_data
, 0);
383 do_mmap(NULL
, ELF_PAGESTART(text_data
+ ELF_EXEC_PAGESIZE
- 1),
385 PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_FIXED
|MAP_PRIVATE
, 0);
386 elf_entry
= interp_ex
->a_entry
;
393 * These are the functions used to load ELF style executables and shared
394 * libraries. There is no binary dependent code anywhere else.
397 #define INTERPRETER_NONE 0
398 #define INTERPRETER_AOUT 1
399 #define INTERPRETER_ELF 2
403 do_load_elf_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
406 struct dentry
*interpreter_dentry
= NULL
; /* to shut gcc up */
407 unsigned long load_addr
= 0, load_bias
;
408 int load_addr_set
= 0;
409 char * elf_interpreter
= NULL
;
410 unsigned int interpreter_type
= INTERPRETER_NONE
;
411 unsigned char ibcs2_interpreter
= 0;
414 struct elf_phdr
* elf_ppnt
, *elf_phdata
;
415 unsigned long elf_bss
, k
, elf_brk
;
418 unsigned long elf_entry
, interp_load_addr
= 0;
419 unsigned long start_code
, end_code
, end_data
;
420 struct elfhdr elf_ex
;
421 struct elfhdr interp_elf_ex
;
422 struct exec interp_ex
;
423 char passed_fileno
[6];
425 /* Get the exec-header */
426 elf_ex
= *((struct elfhdr
*) bprm
->buf
);
429 /* First of all, some simple consistency checks */
430 if (elf_ex
.e_ident
[0] != 0x7f ||
431 strncmp(&elf_ex
.e_ident
[1], "ELF", 3) != 0)
434 if (elf_ex
.e_type
!= ET_EXEC
&& elf_ex
.e_type
!= ET_DYN
)
436 if (!elf_check_arch(elf_ex
.e_machine
))
438 if (!bprm
->dentry
->d_inode
->i_op
||
439 !bprm
->dentry
->d_inode
->i_op
->default_file_ops
||
440 !bprm
->dentry
->d_inode
->i_op
->default_file_ops
->mmap
)
443 /* Now read in all of the header information */
446 size
= elf_ex
.e_phentsize
* elf_ex
.e_phnum
;
447 elf_phdata
= (struct elf_phdr
*) kmalloc(size
, GFP_KERNEL
);
451 retval
= read_exec(bprm
->dentry
, elf_ex
.e_phoff
,
452 (char *) elf_phdata
, size
, 1);
456 retval
= open_dentry(bprm
->dentry
, O_RDONLY
);
459 elf_exec_fileno
= retval
;
460 file
= fget(elf_exec_fileno
);
462 elf_ppnt
= elf_phdata
;
470 for (i
= 0; i
< elf_ex
.e_phnum
; i
++) {
471 if (elf_ppnt
->p_type
== PT_INTERP
) {
474 goto out_free_interp
;
476 /* This is the program interpreter used for
477 * shared libraries - for now assume that this
478 * is an a.out format binary
482 elf_interpreter
= (char *) kmalloc(elf_ppnt
->p_filesz
,
484 if (!elf_interpreter
)
487 retval
= read_exec(bprm
->dentry
, elf_ppnt
->p_offset
,
489 elf_ppnt
->p_filesz
, 1);
491 goto out_free_interp
;
492 /* If the program interpreter is one of these two,
493 * then assume an iBCS2 image. Otherwise assume
494 * a native linux image.
496 if (strcmp(elf_interpreter
,"/usr/lib/libc.so.1") == 0 ||
497 strcmp(elf_interpreter
,"/usr/lib/ld.so.1") == 0)
498 ibcs2_interpreter
= 1;
500 printk("Using ELF interpreter %s\n", elf_interpreter
);
502 old_fs
= get_fs(); /* This could probably be optimized */
505 if (ibcs2_interpreter
) {
506 unsigned long old_pers
= current
->personality
;
508 current
->personality
= PER_SVR4
;
509 interpreter_dentry
= open_namei(elf_interpreter
,
511 current
->personality
= old_pers
;
514 interpreter_dentry
= open_namei(elf_interpreter
,
517 retval
= PTR_ERR(interpreter_dentry
);
518 if (IS_ERR(interpreter_dentry
))
519 goto out_free_interp
;
520 retval
= permission(interpreter_dentry
->d_inode
, MAY_EXEC
);
522 goto out_free_dentry
;
523 retval
= read_exec(interpreter_dentry
, 0, bprm
->buf
, 128, 1);
525 goto out_free_dentry
;
527 /* Get the exec headers */
528 interp_ex
= *((struct exec
*) bprm
->buf
);
529 interp_elf_ex
= *((struct elfhdr
*) bprm
->buf
);
534 /* Some simple consistency checks for the interpreter */
535 if (elf_interpreter
) {
536 interpreter_type
= INTERPRETER_ELF
| INTERPRETER_AOUT
;
538 /* Now figure out which format our binary is */
539 if ((N_MAGIC(interp_ex
) != OMAGIC
) &&
540 (N_MAGIC(interp_ex
) != ZMAGIC
) &&
541 (N_MAGIC(interp_ex
) != QMAGIC
))
542 interpreter_type
= INTERPRETER_ELF
;
544 if (interp_elf_ex
.e_ident
[0] != 0x7f ||
545 strncmp(&interp_elf_ex
.e_ident
[1], "ELF", 3) != 0)
546 interpreter_type
&= ~INTERPRETER_ELF
;
549 if (!interpreter_type
)
550 goto out_free_dentry
;
552 /* Make sure only one type was selected */
553 if ((interpreter_type
& INTERPRETER_ELF
) &&
554 interpreter_type
!= INTERPRETER_ELF
) {
555 printk(KERN_WARNING
"ELF: Ambiguous type, using ELF\n");
556 interpreter_type
= INTERPRETER_ELF
;
560 /* OK, we are done with that, now set up the arg stuff,
561 and then start this sucker up */
563 if (!bprm
->sh_bang
) {
566 if (interpreter_type
== INTERPRETER_AOUT
) {
567 sprintf(passed_fileno
, "%d", elf_exec_fileno
);
568 passed_p
= passed_fileno
;
570 if (elf_interpreter
) {
571 bprm
->p
= copy_strings(1,&passed_p
,bprm
->page
,bprm
->p
,2);
577 goto out_free_dentry
;
580 /* Flush all traces of the currently running executable */
581 retval
= flush_old_exec(bprm
);
583 goto out_free_dentry
;
585 /* OK, This is the point of no return */
586 current
->mm
->end_data
= 0;
587 current
->mm
->end_code
= 0;
588 current
->mm
->mmap
= NULL
;
589 current
->flags
&= ~PF_FORKNOEXEC
;
590 elf_entry
= (unsigned long) elf_ex
.e_entry
;
592 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
593 may depend on the personality. */
594 SET_PERSONALITY(elf_ex
, ibcs2_interpreter
);
596 /* Do this so that we can load the interpreter, if need be. We will
597 change some of these later */
598 current
->mm
->rss
= 0;
599 bprm
->p
= setup_arg_pages(bprm
->p
, bprm
);
600 current
->mm
->start_stack
= bprm
->p
;
602 /* Try and get dynamic programs out of the way of the default mmap
603 base, as well as whatever program they might try to exec. This
604 is because the brk will follow the loader, and is not movable. */
606 load_bias
= (elf_ex
.e_type
== ET_DYN
? ELF_ET_DYN_BASE
: 0);
608 /* Now we do a little grungy work by mmaping the ELF image into
609 the correct location in memory. At this point, we assume that
610 the image should be loaded at fixed address, not at a variable
615 for(i
= 0, elf_ppnt
= elf_phdata
; i
< elf_ex
.e_phnum
; i
++, elf_ppnt
++) {
616 if (elf_ppnt
->p_type
== PT_LOAD
) {
617 int elf_prot
= 0, elf_flags
;
618 unsigned long vaddr
= 0;
620 if (elf_ppnt
->p_flags
& PF_R
) elf_prot
|= PROT_READ
;
621 if (elf_ppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
622 if (elf_ppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
624 elf_flags
= MAP_PRIVATE
|MAP_DENYWRITE
|MAP_EXECUTABLE
;
626 if (elf_ex
.e_type
== ET_EXEC
|| load_addr_set
) {
627 vaddr
= elf_ppnt
->p_vaddr
;
628 elf_flags
|= MAP_FIXED
;
631 error
= do_mmap(file
,
632 ELF_PAGESTART(load_bias
+ vaddr
),
633 (elf_ppnt
->p_filesz
+
634 ELF_PAGEOFFSET(elf_ppnt
->p_vaddr
)),
636 (elf_ppnt
->p_offset
-
637 ELF_PAGEOFFSET(elf_ppnt
->p_vaddr
)));
639 if (!load_addr_set
) {
641 load_addr
= (elf_ppnt
->p_vaddr
-
643 if (elf_ex
.e_type
== ET_DYN
) {
648 k
= elf_ppnt
->p_vaddr
;
649 if (k
< start_code
) start_code
= k
;
650 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_filesz
;
653 if ((elf_ppnt
->p_flags
& PF_X
) && end_code
< k
)
657 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_memsz
;
663 fput(file
); /* all done with the file */
665 elf_entry
+= load_bias
;
666 elf_bss
+= load_bias
;
667 elf_brk
+= load_bias
;
668 start_code
+= load_bias
;
669 end_code
+= load_bias
;
670 end_data
+= load_bias
;
672 if (elf_interpreter
) {
673 if (interpreter_type
== INTERPRETER_AOUT
)
674 elf_entry
= load_aout_interp(&interp_ex
,
677 elf_entry
= load_elf_interp(&interp_elf_ex
,
681 dput(interpreter_dentry
);
682 kfree(elf_interpreter
);
684 if (elf_entry
== ~0UL) {
685 printk(KERN_ERR
"Unable to load interpreter\n");
687 send_sig(SIGSEGV
, current
, 0);
694 if (interpreter_type
!= INTERPRETER_AOUT
)
695 sys_close(elf_exec_fileno
);
697 if (current
->exec_domain
&& current
->exec_domain
->module
)
698 __MOD_DEC_USE_COUNT(current
->exec_domain
->module
);
699 if (current
->binfmt
&& current
->binfmt
->module
)
700 __MOD_DEC_USE_COUNT(current
->binfmt
->module
);
701 current
->exec_domain
= lookup_exec_domain(current
->personality
);
702 current
->binfmt
= &elf_format
;
703 if (current
->exec_domain
&& current
->exec_domain
->module
)
704 __MOD_INC_USE_COUNT(current
->exec_domain
->module
);
705 if (current
->binfmt
&& current
->binfmt
->module
)
706 __MOD_INC_USE_COUNT(current
->binfmt
->module
);
708 #ifndef VM_STACK_FLAGS
709 current
->executable
= dget(bprm
->dentry
);
712 current
->flags
&= ~PF_FORKNOEXEC
;
713 bprm
->p
= (unsigned long)
714 create_elf_tables((char *)bprm
->p
,
717 (interpreter_type
== INTERPRETER_ELF
? &elf_ex
: NULL
),
718 load_addr
, load_bias
,
720 (interpreter_type
== INTERPRETER_AOUT
? 0 : 1));
721 /* N.B. passed_fileno might not be initialized? */
722 if (interpreter_type
== INTERPRETER_AOUT
)
723 current
->mm
->arg_start
+= strlen(passed_fileno
) + 1;
724 current
->mm
->start_brk
= current
->mm
->brk
= elf_brk
;
725 current
->mm
->end_code
= end_code
;
726 current
->mm
->start_code
= start_code
;
727 current
->mm
->end_data
= end_data
;
728 current
->mm
->start_stack
= bprm
->p
;
730 /* Calling set_brk effectively mmaps the pages that we need
731 * for the bss and break sections
733 set_brk(elf_bss
, elf_brk
);
738 printk("(start_brk) %x\n" , current
->mm
->start_brk
);
739 printk("(end_code) %x\n" , current
->mm
->end_code
);
740 printk("(start_code) %x\n" , current
->mm
->start_code
);
741 printk("(end_data) %x\n" , current
->mm
->end_data
);
742 printk("(start_stack) %x\n" , current
->mm
->start_stack
);
743 printk("(brk) %x\n" , current
->mm
->brk
);
746 if ( current
->personality
== PER_SVR4
)
748 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
749 and some applications "depend" upon this behavior.
750 Since we do not have the power to recompile these, we
751 emulate the SVr4 behavior. Sigh. */
752 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
753 error
= do_mmap(NULL
, 0, 4096, PROT_READ
| PROT_EXEC
,
754 MAP_FIXED
| MAP_PRIVATE
, 0);
759 * The ABI may specify that certain registers be set up in special
760 * ways (on i386 %edx is the address of a DT_FINI function, for
761 * example. This macro performs whatever initialization to
762 * the regs structure is required.
767 start_thread(regs
, elf_entry
, bprm
->p
);
768 if (current
->flags
& PF_PTRACED
)
769 send_sig(SIGTRAP
, current
, 0);
776 dput(interpreter_dentry
);
779 kfree(elf_interpreter
);
782 sys_close(elf_exec_fileno
);
789 load_elf_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
794 retval
= do_load_elf_binary(bprm
, regs
);
799 /* This is really simpleminded and specialized - we are loading an
800 a.out library that is given an ELF header. */
803 do_load_elf_library(int fd
)
806 struct dentry
* dentry
;
807 struct inode
* inode
;
808 struct elf_phdr
*elf_phdata
;
809 unsigned long elf_bss
= 0, bss
, len
, k
;
810 int retval
, error
, i
, j
;
811 struct elfhdr elf_ex
;
816 if (!file
|| !file
->f_op
)
818 dentry
= file
->f_dentry
;
819 inode
= dentry
->d_inode
;
821 /* seek to the beginning of the file */
824 /* N.B. save current DS?? */
826 retval
= file
->f_op
->read(file
, (char *) &elf_ex
, sizeof(elf_ex
), &offset
);
828 if (retval
!= sizeof(elf_ex
))
831 if (elf_ex
.e_ident
[0] != 0x7f ||
832 strncmp(&elf_ex
.e_ident
[1], "ELF", 3) != 0)
835 /* First of all, some simple consistency checks */
836 if (elf_ex
.e_type
!= ET_EXEC
|| elf_ex
.e_phnum
> 2 ||
837 !elf_check_arch(elf_ex
.e_machine
) ||
838 (!inode
->i_op
|| !inode
->i_op
->default_file_ops
->mmap
))
841 /* Now read in all of the header information */
843 j
= sizeof(struct elf_phdr
) * elf_ex
.e_phnum
;
844 if (j
> ELF_EXEC_PAGESIZE
)
848 elf_phdata
= (struct elf_phdr
*) kmalloc(j
, GFP_KERNEL
);
852 /* N.B. check for error return?? */
853 retval
= read_exec(dentry
, elf_ex
.e_phoff
, (char *) elf_phdata
,
854 sizeof(struct elf_phdr
) * elf_ex
.e_phnum
, 1);
857 for (j
= 0, i
= 0; i
<elf_ex
.e_phnum
; i
++)
858 if ((elf_phdata
+ i
)->p_type
== PT_LOAD
) j
++;
862 while (elf_phdata
->p_type
!= PT_LOAD
) elf_phdata
++;
864 /* Now use mmap to map the library into memory. */
865 error
= do_mmap(file
,
866 ELF_PAGESTART(elf_phdata
->p_vaddr
),
867 (elf_phdata
->p_filesz
+
868 ELF_PAGEOFFSET(elf_phdata
->p_vaddr
)),
869 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
870 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
,
871 (elf_phdata
->p_offset
-
872 ELF_PAGEOFFSET(elf_phdata
->p_vaddr
)));
873 if (error
!= ELF_PAGESTART(elf_phdata
->p_vaddr
))
876 k
= elf_phdata
->p_vaddr
+ elf_phdata
->p_filesz
;
881 len
= ELF_PAGESTART(elf_phdata
->p_filesz
+ elf_phdata
->p_vaddr
+
882 ELF_EXEC_PAGESIZE
- 1);
883 bss
= elf_phdata
->p_memsz
+ elf_phdata
->p_vaddr
;
885 do_mmap(NULL
, len
, bss
- len
,
886 PROT_READ
|PROT_WRITE
|PROT_EXEC
,
887 MAP_FIXED
|MAP_PRIVATE
, 0);
898 static int load_elf_library(int fd
)
903 retval
= do_load_elf_library(fd
);
909 * Note that some platforms still use traditional core dumps and not
910 * the ELF core dump. Each platform can select it as appropriate.
912 #ifdef USE_ELF_CORE_DUMP
917 * Modelled on fs/exec.c:aout_core_dump()
918 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
921 * These are the only things you should do on a core-file: use only these
922 * functions to write out all the necessary info.
924 static int dump_write(struct file
*file
, const void *addr
, int nr
)
926 return file
->f_op
->write(file
, addr
, nr
, &file
->f_pos
) == nr
;
929 static int dump_seek(struct file
*file
, off_t off
)
931 if (file
->f_op
->llseek
) {
932 if (file
->f_op
->llseek(file
, off
, 0) != off
)
940 * Decide whether a segment is worth dumping; default is yes to be
941 * sure (missing info is worse than too much; etc).
942 * Personally I'd include everything, and use the coredump limit...
944 * I think we should skip something. But I am not sure how. H.J.
946 static inline int maydump(struct vm_area_struct
*vma
)
948 if (!(vma
->vm_flags
& (VM_READ
|VM_WRITE
|VM_EXEC
)))
951 /* Do not dump I/O mapped devices! -DaveM */
952 if(vma
->vm_flags
& VM_IO
)
955 if (vma
->vm_flags
& (VM_WRITE
|VM_GROWSUP
|VM_GROWSDOWN
))
957 if (vma
->vm_flags
& (VM_READ
|VM_EXEC
|VM_EXECUTABLE
|VM_SHARED
))
963 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
965 /* An ELF note in memory */
974 static int notesize(struct memelfnote
*en
)
978 sz
= sizeof(struct elf_note
);
979 sz
+= roundup(strlen(en
->name
), 4);
980 sz
+= roundup(en
->datasz
, 4);
988 static void dump_regs(const char *str
, elf_greg_t
*r
)
991 static const char *regs
[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
992 "eax", "ds", "es", "fs", "gs",
993 "orig_eax", "eip", "cs",
994 "efl", "uesp", "ss"};
995 printk("Registers: %s\n", str
);
997 for(i
= 0; i
< ELF_NGREG
; i
++)
999 unsigned long val
= r
[i
];
1000 printk(" %-2d %-5s=%08lx %lu\n", i
, regs
[i
], val
, val
);
1005 #define DUMP_WRITE(addr, nr) \
1006 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1007 #define DUMP_SEEK(off) \
1008 do { if (!dump_seek(file, (off))) return 0; } while(0)
1010 static int writenote(struct memelfnote
*men
, struct file
*file
)
1014 en
.n_namesz
= strlen(men
->name
);
1015 en
.n_descsz
= men
->datasz
;
1016 en
.n_type
= men
->type
;
1018 DUMP_WRITE(&en
, sizeof(en
));
1019 DUMP_WRITE(men
->name
, en
.n_namesz
);
1020 /* XXX - cast from long long to long to avoid need for libgcc.a */
1021 DUMP_SEEK(roundup((unsigned long)file
->f_pos
, 4)); /* XXX */
1022 DUMP_WRITE(men
->data
, men
->datasz
);
1023 DUMP_SEEK(roundup((unsigned long)file
->f_pos
, 4)); /* XXX */
1030 #define DUMP_WRITE(addr, nr) \
1031 if (!dump_write(&file, (addr), (nr))) \
1032 goto close_coredump;
1033 #define DUMP_SEEK(off) \
1034 if (!dump_seek(&file, (off))) \
1035 goto close_coredump;
1039 * This is a two-pass process; first we find the offsets of the bits,
1040 * and then they are actually written out. If we run out of core limit
1043 static int elf_core_dump(long signr
, struct pt_regs
* regs
)
1047 struct dentry
*dentry
;
1048 struct inode
*inode
;
1050 char corefile
[6+sizeof(current
->comm
)];
1054 struct vm_area_struct
*vma
;
1056 off_t offset
= 0, dataoff
;
1057 unsigned long limit
= current
->rlim
[RLIMIT_CORE
].rlim_cur
;
1059 struct memelfnote notes
[4];
1060 struct elf_prstatus prstatus
; /* NT_PRSTATUS */
1061 elf_fpregset_t fpu
; /* NT_PRFPREG */
1062 struct elf_prpsinfo psinfo
; /* NT_PRPSINFO */
1064 if (!current
->dumpable
||
1065 limit
< ELF_EXEC_PAGESIZE
||
1066 atomic_read(¤t
->mm
->count
) != 1)
1068 current
->dumpable
= 0;
1070 #ifndef CONFIG_BINFMT_ELF
1074 /* Count what's needed to dump, up to the limit of coredump size */
1077 for(vma
= current
->mm
->mmap
; vma
!= NULL
; vma
= vma
->vm_next
) {
1080 unsigned long sz
= vma
->vm_end
-vma
->vm_start
;
1082 if (size
+sz
>= limit
)
1091 printk("elf_core_dump: %d segs taking %d bytes\n", segs
, size
);
1095 memcpy(elf
.e_ident
, ELFMAG
, SELFMAG
);
1096 elf
.e_ident
[EI_CLASS
] = ELF_CLASS
;
1097 elf
.e_ident
[EI_DATA
] = ELF_DATA
;
1098 elf
.e_ident
[EI_VERSION
] = EV_CURRENT
;
1099 memset(elf
.e_ident
+EI_PAD
, 0, EI_NIDENT
-EI_PAD
);
1101 elf
.e_type
= ET_CORE
;
1102 elf
.e_machine
= ELF_ARCH
;
1103 elf
.e_version
= EV_CURRENT
;
1105 elf
.e_phoff
= sizeof(elf
);
1108 elf
.e_ehsize
= sizeof(elf
);
1109 elf
.e_phentsize
= sizeof(struct elf_phdr
);
1110 elf
.e_phnum
= segs
+1; /* Include notes */
1111 elf
.e_shentsize
= 0;
1117 memcpy(corefile
,"core.",5);
1119 memcpy(corefile
+5,current
->comm
,sizeof(current
->comm
));
1123 dentry
= open_namei(corefile
, O_CREAT
| 2 | O_TRUNC
, 0600);
1124 if (IS_ERR(dentry
)) {
1128 inode
= dentry
->d_inode
;
1129 if (!S_ISREG(inode
->i_mode
))
1131 if (!inode
->i_op
|| !inode
->i_op
->default_file_ops
)
1133 if (init_private_file(&file
, dentry
, 3))
1135 if (!file
.f_op
->write
)
1136 goto close_coredump
;
1138 current
->flags
|= PF_DUMPCORE
;
1140 DUMP_WRITE(&elf
, sizeof(elf
));
1141 offset
+= sizeof(elf
); /* Elf header */
1142 offset
+= (segs
+1) * sizeof(struct elf_phdr
); /* Program headers */
1145 * Set up the notes in similar form to SVR4 core dumps made
1146 * with info from their /proc.
1148 memset(&psinfo
, 0, sizeof(psinfo
));
1149 memset(&prstatus
, 0, sizeof(prstatus
));
1151 notes
[0].name
= "CORE";
1152 notes
[0].type
= NT_PRSTATUS
;
1153 notes
[0].datasz
= sizeof(prstatus
);
1154 notes
[0].data
= &prstatus
;
1155 prstatus
.pr_info
.si_signo
= prstatus
.pr_cursig
= signr
;
1156 prstatus
.pr_sigpend
= current
->signal
.sig
[0];
1157 prstatus
.pr_sighold
= current
->blocked
.sig
[0];
1158 psinfo
.pr_pid
= prstatus
.pr_pid
= current
->pid
;
1159 psinfo
.pr_ppid
= prstatus
.pr_ppid
= current
->p_pptr
->pid
;
1160 psinfo
.pr_pgrp
= prstatus
.pr_pgrp
= current
->pgrp
;
1161 psinfo
.pr_sid
= prstatus
.pr_sid
= current
->session
;
1162 prstatus
.pr_utime
.tv_sec
= CT_TO_SECS(current
->times
.tms_utime
);
1163 prstatus
.pr_utime
.tv_usec
= CT_TO_USECS(current
->times
.tms_utime
);
1164 prstatus
.pr_stime
.tv_sec
= CT_TO_SECS(current
->times
.tms_stime
);
1165 prstatus
.pr_stime
.tv_usec
= CT_TO_USECS(current
->times
.tms_stime
);
1166 prstatus
.pr_cutime
.tv_sec
= CT_TO_SECS(current
->times
.tms_cutime
);
1167 prstatus
.pr_cutime
.tv_usec
= CT_TO_USECS(current
->times
.tms_cutime
);
1168 prstatus
.pr_cstime
.tv_sec
= CT_TO_SECS(current
->times
.tms_cstime
);
1169 prstatus
.pr_cstime
.tv_usec
= CT_TO_USECS(current
->times
.tms_cstime
);
1172 * This transfers the registers from regs into the standard
1173 * coredump arrangement, whatever that is.
1175 #ifdef ELF_CORE_COPY_REGS
1176 ELF_CORE_COPY_REGS(prstatus
.pr_reg
, regs
)
1178 if (sizeof(elf_gregset_t
) != sizeof(struct pt_regs
))
1180 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1181 (long)sizeof(elf_gregset_t
), (long)sizeof(struct pt_regs
));
1184 *(struct pt_regs
*)&prstatus
.pr_reg
= *regs
;
1188 dump_regs("Passed in regs", (elf_greg_t
*)regs
);
1189 dump_regs("prstatus regs", (elf_greg_t
*)&prstatus
.pr_reg
);
1192 notes
[1].name
= "CORE";
1193 notes
[1].type
= NT_PRPSINFO
;
1194 notes
[1].datasz
= sizeof(psinfo
);
1195 notes
[1].data
= &psinfo
;
1196 i
= current
->state
? ffz(~current
->state
) + 1 : 0;
1197 psinfo
.pr_state
= i
;
1198 psinfo
.pr_sname
= (i
< 0 || i
> 5) ? '.' : "RSDZTD"[i
];
1199 psinfo
.pr_zomb
= psinfo
.pr_sname
== 'Z';
1200 psinfo
.pr_nice
= current
->priority
-15;
1201 psinfo
.pr_flag
= current
->flags
;
1202 psinfo
.pr_uid
= current
->uid
;
1203 psinfo
.pr_gid
= current
->gid
;
1209 len
= current
->mm
->arg_end
- current
->mm
->arg_start
;
1210 if (len
>= ELF_PRARGSZ
)
1211 len
= ELF_PRARGSZ
-1;
1212 copy_from_user(&psinfo
.pr_psargs
,
1213 (const char *)current
->mm
->arg_start
, len
);
1214 for(i
= 0; i
< len
; i
++)
1215 if (psinfo
.pr_psargs
[i
] == 0)
1216 psinfo
.pr_psargs
[i
] = ' ';
1217 psinfo
.pr_psargs
[len
] = 0;
1221 strncpy(psinfo
.pr_fname
, current
->comm
, sizeof(psinfo
.pr_fname
));
1223 notes
[2].name
= "CORE";
1224 notes
[2].type
= NT_TASKSTRUCT
;
1225 notes
[2].datasz
= sizeof(*current
);
1226 notes
[2].data
= current
;
1228 /* Try to dump the FPU. */
1229 prstatus
.pr_fpvalid
= dump_fpu (regs
, &fpu
);
1230 if (!prstatus
.pr_fpvalid
)
1236 notes
[3].name
= "CORE";
1237 notes
[3].type
= NT_PRFPREG
;
1238 notes
[3].datasz
= sizeof(fpu
);
1239 notes
[3].data
= &fpu
;
1242 /* Write notes phdr entry */
1244 struct elf_phdr phdr
;
1247 for(i
= 0; i
< numnote
; i
++)
1248 sz
+= notesize(¬es
[i
]);
1250 phdr
.p_type
= PT_NOTE
;
1251 phdr
.p_offset
= offset
;
1259 offset
+= phdr
.p_filesz
;
1260 DUMP_WRITE(&phdr
, sizeof(phdr
));
1263 /* Page-align dumped data */
1264 dataoff
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
1266 /* Write program headers for segments dump */
1267 for(vma
= current
->mm
->mmap
, i
= 0;
1268 i
< segs
&& vma
!= NULL
; vma
= vma
->vm_next
) {
1269 struct elf_phdr phdr
;
1274 sz
= vma
->vm_end
- vma
->vm_start
;
1276 phdr
.p_type
= PT_LOAD
;
1277 phdr
.p_offset
= offset
;
1278 phdr
.p_vaddr
= vma
->vm_start
;
1280 phdr
.p_filesz
= maydump(vma
) ? sz
: 0;
1282 offset
+= phdr
.p_filesz
;
1283 phdr
.p_flags
= vma
->vm_flags
& VM_READ
? PF_R
: 0;
1284 if (vma
->vm_flags
& VM_WRITE
) phdr
.p_flags
|= PF_W
;
1285 if (vma
->vm_flags
& VM_EXEC
) phdr
.p_flags
|= PF_X
;
1286 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
1288 DUMP_WRITE(&phdr
, sizeof(phdr
));
1291 for(i
= 0; i
< numnote
; i
++)
1292 if (!writenote(¬es
[i
], &file
))
1293 goto close_coredump
;
1299 for(i
= 0, vma
= current
->mm
->mmap
;
1300 i
< segs
&& vma
!= NULL
;
1301 vma
= vma
->vm_next
) {
1302 unsigned long addr
= vma
->vm_start
;
1303 unsigned long len
= vma
->vm_end
- vma
->vm_start
;
1309 printk("elf_core_dump: writing %08lx %lx\n", addr
, len
);
1311 DUMP_WRITE((void *)addr
, len
);
1314 if ((off_t
) file
.f_pos
!= offset
) {
1316 printk("elf_core_dump: file.f_pos (%ld) != offset (%ld)\n",
1317 (off_t
) file
.f_pos
, offset
);
1321 if (file
.f_op
->release
)
1322 file
.f_op
->release(inode
,&file
);
1327 #ifndef CONFIG_BINFMT_ELF
1332 #endif /* USE_ELF_CORE_DUMP */
1334 int __init
init_elf_binfmt(void)
1336 return register_binfmt(&elf_format
);
1341 int init_module(void)
1343 /* Install the COFF, ELF and XOUT loaders.
1344 * N.B. We *rely* on the table being the right size with the
1345 * right number of free slots...
1347 return init_elf_binfmt();
1351 void cleanup_module( void)
1353 /* Remove the COFF and ELF loaders. */
1354 unregister_binfmt(&elf_format
);