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 #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(struct file
*);
44 extern int dump_fpu (struct pt_regs
*, elf_fpregset_t
*);
45 extern void dump_thread(struct pt_regs
*, struct user
*);
48 #define elf_addr_t unsigned long
49 #define elf_caddr_t char *
53 * If we don't support core dumping, then supply a NULL so we
56 #ifdef USE_ELF_CORE_DUMP
57 static int elf_core_dump(long signr
, struct pt_regs
* regs
, struct file
* file
);
59 #define elf_core_dump NULL
62 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_EXEC_PAGESIZE-1))
63 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_EXEC_PAGESIZE-1))
64 #define ELF_PAGEALIGN(_v) (((_v) + ELF_EXEC_PAGESIZE - 1) & ~(ELF_EXEC_PAGESIZE - 1))
66 static struct linux_binfmt elf_format
= {
67 NULL
, THIS_MODULE
, load_elf_binary
, load_elf_library
, elf_core_dump
, ELF_EXEC_PAGESIZE
70 static void set_brk(unsigned long start
, unsigned long end
)
72 start
= ELF_PAGEALIGN(start
);
73 end
= ELF_PAGEALIGN(end
);
76 do_brk(start
, end
- start
);
80 /* We need to explicitly zero any fractional pages
81 after the data section (i.e. bss). This would
82 contain the junk from the file that should not
86 static void padzero(unsigned long elf_bss
)
90 nbyte
= ELF_PAGEOFFSET(elf_bss
);
92 nbyte
= ELF_EXEC_PAGESIZE
- nbyte
;
93 clear_user((void *) elf_bss
, nbyte
);
98 create_elf_tables(char *p
, int argc
, int envc
,
100 unsigned long load_addr
,
101 unsigned long load_bias
,
102 unsigned long interp_load_addr
, int ibcs
)
106 elf_addr_t
*sp
, *csp
;
107 char *k_platform
, *u_platform
;
109 size_t platform_len
= 0;
112 * Get hold of platform and hardware capabilities masks for
113 * the machine we are running on. In some cases (Sparc),
114 * this info is impossible to get, in others (i386) it is
119 k_platform
= ELF_PLATFORM
;
122 platform_len
= strlen(k_platform
) + 1;
123 u_platform
= p
- platform_len
;
124 __copy_to_user(u_platform
, k_platform
, platform_len
);
129 * Force 16 byte _final_ alignment here for generality.
130 * Leave an extra 16 bytes free so that on the PowerPC we
131 * can move the aux table up to start on a 16-byte boundary.
133 sp
= (elf_addr_t
*)((~15UL & (unsigned long)(u_platform
)) - 16UL);
135 csp
-= ((exec
? DLINFO_ITEMS
*2 : 4) + (k_platform
? 2 : 0));
138 csp
-= (!ibcs
? 3 : 1); /* argc itself */
139 if ((unsigned long)csp
& 15UL)
140 sp
-= ((unsigned long)csp
& 15UL) / sizeof(*sp
);
143 * Put the ELF interpreter info on the stack
145 #define NEW_AUX_ENT(nr, id, val) \
146 __put_user ((id), sp+(nr*2)); \
147 __put_user ((val), sp+(nr*2+1)); \
150 NEW_AUX_ENT(0, AT_NULL
, 0);
153 NEW_AUX_ENT(0, AT_PLATFORM
, (elf_addr_t
)(unsigned long) u_platform
);
156 NEW_AUX_ENT(0, AT_HWCAP
, hwcap
);
161 NEW_AUX_ENT(0, AT_PHDR
, load_addr
+ exec
->e_phoff
);
162 NEW_AUX_ENT(1, AT_PHENT
, sizeof (struct elf_phdr
));
163 NEW_AUX_ENT(2, AT_PHNUM
, exec
->e_phnum
);
164 NEW_AUX_ENT(3, AT_PAGESZ
, ELF_EXEC_PAGESIZE
);
165 NEW_AUX_ENT(4, AT_BASE
, interp_load_addr
);
166 NEW_AUX_ENT(5, AT_FLAGS
, 0);
167 NEW_AUX_ENT(6, AT_ENTRY
, load_bias
+ exec
->e_entry
);
168 NEW_AUX_ENT(7, AT_UID
, (elf_addr_t
) current
->uid
);
169 NEW_AUX_ENT(8, AT_EUID
, (elf_addr_t
) current
->euid
);
170 NEW_AUX_ENT(9, AT_GID
, (elf_addr_t
) current
->gid
);
171 NEW_AUX_ENT(10, AT_EGID
, (elf_addr_t
) current
->egid
);
176 envp
= (elf_caddr_t
*) sp
;
178 argv
= (elf_caddr_t
*) sp
;
180 __put_user((elf_addr_t
)(unsigned long) envp
,--sp
);
181 __put_user((elf_addr_t
)(unsigned long) argv
,--sp
);
184 __put_user((elf_addr_t
)argc
,--sp
);
185 current
->mm
->arg_start
= (unsigned long) p
;
187 __put_user((elf_caddr_t
)(unsigned long)p
,argv
++);
190 __put_user(NULL
, argv
);
191 current
->mm
->arg_end
= current
->mm
->env_start
= (unsigned long) p
;
193 __put_user((elf_caddr_t
)(unsigned long)p
,envp
++);
196 __put_user(NULL
, envp
);
197 current
->mm
->env_end
= (unsigned long) p
;
202 /* This is much more generalized than the library routine read function,
203 so we keep this separate. Technically the library read function
204 is only provided so that we can read a.out libraries that have
207 static unsigned long load_elf_interp(struct elfhdr
* interp_elf_ex
,
208 struct file
* interpreter
,
209 unsigned long *interp_load_addr
)
211 struct elf_phdr
*elf_phdata
;
212 struct elf_phdr
*eppnt
;
213 unsigned long load_addr
= 0;
214 int load_addr_set
= 0;
215 unsigned long last_bss
= 0, elf_bss
= 0;
216 unsigned long error
= ~0UL;
219 /* First of all, some simple consistency checks */
220 if (interp_elf_ex
->e_type
!= ET_EXEC
&&
221 interp_elf_ex
->e_type
!= ET_DYN
)
223 if (!elf_check_arch(interp_elf_ex
->e_machine
))
225 if (!interpreter
->f_op
->mmap
)
229 * If the size of this structure has changed, then punt, since
230 * we will be doing the wrong thing.
232 if (interp_elf_ex
->e_phentsize
!= sizeof(struct elf_phdr
))
235 /* Now read in all of the header information */
237 size
= sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
;
238 if (size
> ELF_EXEC_PAGESIZE
)
240 elf_phdata
= (struct elf_phdr
*) kmalloc(size
, GFP_KERNEL
);
244 retval
= kernel_read(interpreter
,interp_elf_ex
->e_phoff
,(char *)elf_phdata
,size
);
250 for (i
=0; i
<interp_elf_ex
->e_phnum
; i
++, eppnt
++) {
251 if (eppnt
->p_type
== PT_LOAD
) {
252 int elf_type
= MAP_PRIVATE
| MAP_DENYWRITE
;
254 unsigned long vaddr
= 0;
255 unsigned long k
, map_addr
;
257 if (eppnt
->p_flags
& PF_R
) elf_prot
= PROT_READ
;
258 if (eppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
259 if (eppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
260 vaddr
= eppnt
->p_vaddr
;
261 if (interp_elf_ex
->e_type
== ET_EXEC
|| load_addr_set
)
262 elf_type
|= MAP_FIXED
;
264 down(¤t
->mm
->mmap_sem
);
265 map_addr
= do_mmap(interpreter
,
266 load_addr
+ ELF_PAGESTART(vaddr
),
267 eppnt
->p_filesz
+ ELF_PAGEOFFSET(eppnt
->p_vaddr
),
270 eppnt
->p_offset
- ELF_PAGEOFFSET(eppnt
->p_vaddr
));
271 up(¤t
->mm
->mmap_sem
);
272 if (map_addr
> -1024UL) /* Real error */
275 if (!load_addr_set
&& interp_elf_ex
->e_type
== ET_DYN
) {
276 load_addr
= map_addr
- ELF_PAGESTART(vaddr
);
281 * Find the end of the file mapping for this phdr, and keep
282 * track of the largest address we see for this.
284 k
= load_addr
+ eppnt
->p_vaddr
+ eppnt
->p_filesz
;
289 * Do the same thing for the memory mapping - between
290 * elf_bss and last_bss is the bss section.
292 k
= load_addr
+ eppnt
->p_memsz
+ eppnt
->p_vaddr
;
298 /* Now use mmap to map the library into memory. */
301 * Now fill out the bss section. First pad the last page up
302 * to the page boundary, and then perform a mmap to make sure
303 * that there are zero-mapped pages up to and including the
307 elf_bss
= ELF_PAGESTART(elf_bss
+ ELF_EXEC_PAGESIZE
- 1); /* What we have mapped so far */
309 /* Map the last of the bss segment */
310 if (last_bss
> elf_bss
)
311 do_brk(elf_bss
, last_bss
- elf_bss
);
313 *interp_load_addr
= load_addr
;
314 error
= ((unsigned long) interp_elf_ex
->e_entry
) + load_addr
;
322 static unsigned long load_aout_interp(struct exec
* interp_ex
,
323 struct file
* interpreter
)
325 unsigned long text_data
, elf_entry
= ~0UL;
330 current
->mm
->end_code
= interp_ex
->a_text
;
331 text_data
= interp_ex
->a_text
+ interp_ex
->a_data
;
332 current
->mm
->end_data
= text_data
;
333 current
->mm
->brk
= interp_ex
->a_bss
+ text_data
;
335 switch (N_MAGIC(*interp_ex
)) {
342 offset
= N_TXTOFF(*interp_ex
);
343 addr
= (char *) N_TXTADDR(*interp_ex
);
349 do_brk(0, text_data
);
351 if (!interpreter
->f_op
->read
)
353 retval
= interpreter
->f_op
->read(interpreter
, addr
, text_data
, &offset
);
356 flush_icache_range((unsigned long)addr
,
357 (unsigned long)addr
+ text_data
);
359 do_brk(ELF_PAGESTART(text_data
+ ELF_EXEC_PAGESIZE
- 1),
361 elf_entry
= interp_ex
->a_entry
;
368 * These are the functions used to load ELF style executables and shared
369 * libraries. There is no binary dependent code anywhere else.
372 #define INTERPRETER_NONE 0
373 #define INTERPRETER_AOUT 1
374 #define INTERPRETER_ELF 2
377 static int load_elf_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
379 struct file
*interpreter
= NULL
; /* to shut gcc up */
380 unsigned long load_addr
= 0, load_bias
;
381 int load_addr_set
= 0;
382 char * elf_interpreter
= NULL
;
383 unsigned int interpreter_type
= INTERPRETER_NONE
;
384 unsigned char ibcs2_interpreter
= 0;
387 struct elf_phdr
* elf_ppnt
, *elf_phdata
;
388 unsigned long elf_bss
, k
, elf_brk
;
391 unsigned long elf_entry
, interp_load_addr
= 0;
392 unsigned long start_code
, end_code
, start_data
, end_data
;
393 struct elfhdr elf_ex
;
394 struct elfhdr interp_elf_ex
;
395 struct exec interp_ex
;
396 char passed_fileno
[6];
398 /* Get the exec-header */
399 elf_ex
= *((struct elfhdr
*) bprm
->buf
);
402 /* First of all, some simple consistency checks */
403 if (memcmp(elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
406 if (elf_ex
.e_type
!= ET_EXEC
&& elf_ex
.e_type
!= ET_DYN
)
408 if (!elf_check_arch(elf_ex
.e_machine
))
411 /* IRIX binaries handled elsewhere. */
412 if (elf_ex
.e_flags
& EF_MIPS_ARCH
) {
417 if (!bprm
->file
->f_op
||!bprm
->file
->f_op
->mmap
)
420 /* Now read in all of the header information */
423 size
= elf_ex
.e_phentsize
* elf_ex
.e_phnum
;
426 elf_phdata
= (struct elf_phdr
*) kmalloc(size
, GFP_KERNEL
);
430 retval
= kernel_read(bprm
->file
, elf_ex
.e_phoff
, (char *) elf_phdata
, size
);
434 retval
= get_unused_fd();
437 get_file(bprm
->file
);
438 fd_install(elf_exec_fileno
= retval
, bprm
->file
);
440 elf_ppnt
= elf_phdata
;
449 for (i
= 0; i
< elf_ex
.e_phnum
; i
++) {
450 if (elf_ppnt
->p_type
== PT_INTERP
) {
453 goto out_free_dentry
;
455 /* This is the program interpreter used for
456 * shared libraries - for now assume that this
457 * is an a.out format binary
461 elf_interpreter
= (char *) kmalloc(elf_ppnt
->p_filesz
,
463 if (!elf_interpreter
)
466 retval
= kernel_read(bprm
->file
, elf_ppnt
->p_offset
,
470 goto out_free_interp
;
471 /* If the program interpreter is one of these two,
472 * then assume an iBCS2 image. Otherwise assume
473 * a native linux image.
475 if (strcmp(elf_interpreter
,"/usr/lib/libc.so.1") == 0 ||
476 strcmp(elf_interpreter
,"/usr/lib/ld.so.1") == 0)
477 ibcs2_interpreter
= 1;
479 printk("Using ELF interpreter %s\n", elf_interpreter
);
482 if (ibcs2_interpreter
) {
483 unsigned long old_pers
= current
->personality
;
484 struct exec_domain
*old_domain
= current
->exec_domain
;
485 struct exec_domain
*new_domain
;
486 struct fs_struct
*old_fs
= current
->fs
, *new_fs
;
487 get_exec_domain(old_domain
);
488 atomic_inc(&old_fs
->count
);
490 set_personality(PER_SVR4
);
491 interpreter
= open_exec(elf_interpreter
);
493 new_domain
= current
->exec_domain
;
494 new_fs
= current
->fs
;
495 current
->personality
= old_pers
;
496 current
->exec_domain
= old_domain
;
497 current
->fs
= old_fs
;
498 put_exec_domain(new_domain
);
499 put_fs_struct(new_fs
);
503 interpreter
= open_exec(elf_interpreter
);
505 retval
= PTR_ERR(interpreter
);
506 if (IS_ERR(interpreter
))
507 goto out_free_interp
;
508 retval
= kernel_read(interpreter
, 0, bprm
->buf
, 128);
510 goto out_free_dentry
;
512 /* Get the exec headers */
513 interp_ex
= *((struct exec
*) bprm
->buf
);
514 interp_elf_ex
= *((struct elfhdr
*) bprm
->buf
);
519 /* Some simple consistency checks for the interpreter */
520 if (elf_interpreter
) {
521 interpreter_type
= INTERPRETER_ELF
| INTERPRETER_AOUT
;
523 /* Now figure out which format our binary is */
524 if ((N_MAGIC(interp_ex
) != OMAGIC
) &&
525 (N_MAGIC(interp_ex
) != ZMAGIC
) &&
526 (N_MAGIC(interp_ex
) != QMAGIC
))
527 interpreter_type
= INTERPRETER_ELF
;
529 if (memcmp(interp_elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
530 interpreter_type
&= ~INTERPRETER_ELF
;
533 if (!interpreter_type
)
534 goto out_free_dentry
;
536 /* Make sure only one type was selected */
537 if ((interpreter_type
& INTERPRETER_ELF
) &&
538 interpreter_type
!= INTERPRETER_ELF
) {
539 printk(KERN_WARNING
"ELF: Ambiguous type, using ELF\n");
540 interpreter_type
= INTERPRETER_ELF
;
544 /* OK, we are done with that, now set up the arg stuff,
545 and then start this sucker up */
547 if (!bprm
->sh_bang
) {
550 if (interpreter_type
== INTERPRETER_AOUT
) {
551 sprintf(passed_fileno
, "%d", elf_exec_fileno
);
552 passed_p
= passed_fileno
;
554 if (elf_interpreter
) {
555 retval
= copy_strings_kernel(1,&passed_p
,bprm
);
557 goto out_free_dentry
;
563 /* Flush all traces of the currently running executable */
564 retval
= flush_old_exec(bprm
);
566 goto out_free_dentry
;
568 /* OK, This is the point of no return */
569 current
->mm
->start_data
= 0;
570 current
->mm
->end_data
= 0;
571 current
->mm
->end_code
= 0;
572 current
->mm
->mmap
= NULL
;
573 current
->flags
&= ~PF_FORKNOEXEC
;
574 elf_entry
= (unsigned long) elf_ex
.e_entry
;
576 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
577 may depend on the personality. */
578 SET_PERSONALITY(elf_ex
, ibcs2_interpreter
);
580 /* Do this so that we can load the interpreter, if need be. We will
581 change some of these later */
582 current
->mm
->rss
= 0;
583 setup_arg_pages(bprm
); /* XXX: check error */
584 current
->mm
->start_stack
= bprm
->p
;
586 /* Try and get dynamic programs out of the way of the default mmap
587 base, as well as whatever program they might try to exec. This
588 is because the brk will follow the loader, and is not movable. */
590 load_bias
= ELF_PAGESTART(elf_ex
.e_type
==ET_DYN
? ELF_ET_DYN_BASE
: 0);
592 /* Now we do a little grungy work by mmaping the ELF image into
593 the correct location in memory. At this point, we assume that
594 the image should be loaded at fixed address, not at a variable
599 for(i
= 0, elf_ppnt
= elf_phdata
; i
< elf_ex
.e_phnum
; i
++, elf_ppnt
++) {
600 int elf_prot
= 0, elf_flags
;
603 if (elf_ppnt
->p_type
!= PT_LOAD
)
606 if (elf_ppnt
->p_flags
& PF_R
) elf_prot
|= PROT_READ
;
607 if (elf_ppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
608 if (elf_ppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
610 elf_flags
= MAP_PRIVATE
|MAP_DENYWRITE
|MAP_EXECUTABLE
;
612 vaddr
= elf_ppnt
->p_vaddr
;
613 if (elf_ex
.e_type
== ET_EXEC
|| load_addr_set
) {
614 elf_flags
|= MAP_FIXED
;
617 down(¤t
->mm
->mmap_sem
);
618 error
= do_mmap(bprm
->file
, ELF_PAGESTART(load_bias
+ vaddr
),
619 (elf_ppnt
->p_filesz
+
620 ELF_PAGEOFFSET(elf_ppnt
->p_vaddr
)),
621 elf_prot
, elf_flags
, (elf_ppnt
->p_offset
-
622 ELF_PAGEOFFSET(elf_ppnt
->p_vaddr
)));
623 up(¤t
->mm
->mmap_sem
);
625 if (!load_addr_set
) {
627 load_addr
= (elf_ppnt
->p_vaddr
- elf_ppnt
->p_offset
);
628 if (elf_ex
.e_type
== ET_DYN
) {
630 ELF_PAGESTART(load_bias
+ vaddr
);
634 k
= elf_ppnt
->p_vaddr
;
635 if (k
< start_code
) start_code
= k
;
636 if (start_data
< k
) start_data
= k
;
638 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_filesz
;
642 if ((elf_ppnt
->p_flags
& PF_X
) && end_code
< k
)
646 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_memsz
;
652 elf_entry
+= load_bias
;
653 elf_bss
+= load_bias
;
654 elf_brk
+= load_bias
;
655 start_code
+= load_bias
;
656 end_code
+= load_bias
;
657 start_data
+= load_bias
;
658 end_data
+= load_bias
;
660 if (elf_interpreter
) {
661 if (interpreter_type
== INTERPRETER_AOUT
)
662 elf_entry
= load_aout_interp(&interp_ex
,
665 elf_entry
= load_elf_interp(&interp_elf_ex
,
672 kfree(elf_interpreter
);
674 if (elf_entry
== ~0UL) {
675 printk(KERN_ERR
"Unable to load interpreter\n");
677 send_sig(SIGSEGV
, current
, 0);
684 if (interpreter_type
!= INTERPRETER_AOUT
)
685 sys_close(elf_exec_fileno
);
687 set_binfmt(&elf_format
);
690 current
->flags
&= ~PF_FORKNOEXEC
;
691 bprm
->p
= (unsigned long)
692 create_elf_tables((char *)bprm
->p
,
695 (interpreter_type
== INTERPRETER_ELF
? &elf_ex
: NULL
),
696 load_addr
, load_bias
,
698 (interpreter_type
== INTERPRETER_AOUT
? 0 : 1));
699 /* N.B. passed_fileno might not be initialized? */
700 if (interpreter_type
== INTERPRETER_AOUT
)
701 current
->mm
->arg_start
+= strlen(passed_fileno
) + 1;
702 current
->mm
->start_brk
= current
->mm
->brk
= elf_brk
;
703 current
->mm
->end_code
= end_code
;
704 current
->mm
->start_code
= start_code
;
705 current
->mm
->start_data
= start_data
;
706 current
->mm
->end_data
= end_data
;
707 current
->mm
->start_stack
= bprm
->p
;
709 /* Calling set_brk effectively mmaps the pages that we need
710 * for the bss and break sections
712 set_brk(elf_bss
, elf_brk
);
717 printk("(start_brk) %lx\n" , (long) current
->mm
->start_brk
);
718 printk("(end_code) %lx\n" , (long) current
->mm
->end_code
);
719 printk("(start_code) %lx\n" , (long) current
->mm
->start_code
);
720 printk("(start_data) %lx\n" , (long) current
->mm
->start_data
);
721 printk("(end_data) %lx\n" , (long) current
->mm
->end_data
);
722 printk("(start_stack) %lx\n" , (long) current
->mm
->start_stack
);
723 printk("(brk) %lx\n" , (long) current
->mm
->brk
);
726 if ( current
->personality
== PER_SVR4
)
728 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
729 and some applications "depend" upon this behavior.
730 Since we do not have the power to recompile these, we
731 emulate the SVr4 behavior. Sigh. */
732 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
733 down(¤t
->mm
->mmap_sem
);
734 error
= do_mmap(NULL
, 0, 4096, PROT_READ
| PROT_EXEC
,
735 MAP_FIXED
| MAP_PRIVATE
, 0);
736 up(¤t
->mm
->mmap_sem
);
741 * The ABI may specify that certain registers be set up in special
742 * ways (on i386 %edx is the address of a DT_FINI function, for
743 * example. This macro performs whatever initialization to
744 * the regs structure is required.
749 start_thread(regs
, elf_entry
, bprm
->p
);
750 if (current
->ptrace
& PT_PTRACED
)
751 send_sig(SIGTRAP
, current
, 0);
763 kfree(elf_interpreter
);
765 sys_close(elf_exec_fileno
);
771 /* This is really simpleminded and specialized - we are loading an
772 a.out library that is given an ELF header. */
774 static int load_elf_library(struct file
*file
)
776 struct elf_phdr
*elf_phdata
;
777 unsigned long elf_bss
= 0, bss
, len
, k
;
778 int retval
, error
, i
, j
;
779 struct elfhdr elf_ex
;
782 retval
= kernel_read(file
, 0, (char *) &elf_ex
, sizeof(elf_ex
));
783 if (retval
!= sizeof(elf_ex
))
786 if (memcmp(elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
789 /* First of all, some simple consistency checks */
790 if (elf_ex
.e_type
!= ET_EXEC
|| elf_ex
.e_phnum
> 2 ||
791 !elf_check_arch(elf_ex
.e_machine
) || !file
->f_op
->mmap
)
794 /* Now read in all of the header information */
796 j
= sizeof(struct elf_phdr
) * elf_ex
.e_phnum
;
797 if (j
> ELF_EXEC_PAGESIZE
)
801 elf_phdata
= (struct elf_phdr
*) kmalloc(j
, GFP_KERNEL
);
805 /* N.B. check for error return?? */
806 retval
= kernel_read(file
, elf_ex
.e_phoff
, (char *) elf_phdata
,
807 sizeof(struct elf_phdr
) * elf_ex
.e_phnum
);
810 for (j
= 0, i
= 0; i
<elf_ex
.e_phnum
; i
++)
811 if ((elf_phdata
+ i
)->p_type
== PT_LOAD
) j
++;
815 while (elf_phdata
->p_type
!= PT_LOAD
) elf_phdata
++;
817 /* Now use mmap to map the library into memory. */
818 down(¤t
->mm
->mmap_sem
);
819 error
= do_mmap(file
,
820 ELF_PAGESTART(elf_phdata
->p_vaddr
),
821 (elf_phdata
->p_filesz
+
822 ELF_PAGEOFFSET(elf_phdata
->p_vaddr
)),
823 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
824 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
,
825 (elf_phdata
->p_offset
-
826 ELF_PAGEOFFSET(elf_phdata
->p_vaddr
)));
827 up(¤t
->mm
->mmap_sem
);
828 if (error
!= ELF_PAGESTART(elf_phdata
->p_vaddr
))
831 k
= elf_phdata
->p_vaddr
+ elf_phdata
->p_filesz
;
836 len
= ELF_PAGESTART(elf_phdata
->p_filesz
+ elf_phdata
->p_vaddr
+
837 ELF_EXEC_PAGESIZE
- 1);
838 bss
= elf_phdata
->p_memsz
+ elf_phdata
->p_vaddr
;
840 do_brk(len
, bss
- len
);
850 * Note that some platforms still use traditional core dumps and not
851 * the ELF core dump. Each platform can select it as appropriate.
853 #ifdef USE_ELF_CORE_DUMP
858 * Modelled on fs/exec.c:aout_core_dump()
859 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
862 * These are the only things you should do on a core-file: use only these
863 * functions to write out all the necessary info.
865 static int dump_write(struct file
*file
, const void *addr
, int nr
)
867 return file
->f_op
->write(file
, addr
, nr
, &file
->f_pos
) == nr
;
870 static int dump_seek(struct file
*file
, off_t off
)
872 if (file
->f_op
->llseek
) {
873 if (file
->f_op
->llseek(file
, off
, 0) != off
)
881 * Decide whether a segment is worth dumping; default is yes to be
882 * sure (missing info is worse than too much; etc).
883 * Personally I'd include everything, and use the coredump limit...
885 * I think we should skip something. But I am not sure how. H.J.
887 static inline int maydump(struct vm_area_struct
*vma
)
889 if (!(vma
->vm_flags
& (VM_READ
|VM_WRITE
|VM_EXEC
)))
892 /* Do not dump I/O mapped devices! -DaveM */
893 if(vma
->vm_flags
& VM_IO
)
896 if (vma
->vm_flags
& (VM_WRITE
|VM_GROWSUP
|VM_GROWSDOWN
))
898 if (vma
->vm_flags
& (VM_READ
|VM_EXEC
|VM_EXECUTABLE
|VM_SHARED
))
904 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
906 /* An ELF note in memory */
915 static int notesize(struct memelfnote
*en
)
919 sz
= sizeof(struct elf_note
);
920 sz
+= roundup(strlen(en
->name
), 4);
921 sz
+= roundup(en
->datasz
, 4);
929 static void dump_regs(const char *str
, elf_greg_t
*r
)
932 static const char *regs
[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
933 "eax", "ds", "es", "fs", "gs",
934 "orig_eax", "eip", "cs",
935 "efl", "uesp", "ss"};
936 printk("Registers: %s\n", str
);
938 for(i
= 0; i
< ELF_NGREG
; i
++)
940 unsigned long val
= r
[i
];
941 printk(" %-2d %-5s=%08lx %lu\n", i
, regs
[i
], val
, val
);
946 #define DUMP_WRITE(addr, nr) \
947 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
948 #define DUMP_SEEK(off) \
949 do { if (!dump_seek(file, (off))) return 0; } while(0)
951 static int writenote(struct memelfnote
*men
, struct file
*file
)
955 en
.n_namesz
= strlen(men
->name
);
956 en
.n_descsz
= men
->datasz
;
957 en
.n_type
= men
->type
;
959 DUMP_WRITE(&en
, sizeof(en
));
960 DUMP_WRITE(men
->name
, en
.n_namesz
);
961 /* XXX - cast from long long to long to avoid need for libgcc.a */
962 DUMP_SEEK(roundup((unsigned long)file
->f_pos
, 4)); /* XXX */
963 DUMP_WRITE(men
->data
, men
->datasz
);
964 DUMP_SEEK(roundup((unsigned long)file
->f_pos
, 4)); /* XXX */
971 #define DUMP_WRITE(addr, nr) \
972 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
974 #define DUMP_SEEK(off) \
975 if (!dump_seek(file, (off))) \
980 * This is a two-pass process; first we find the offsets of the bits,
981 * and then they are actually written out. If we run out of core limit
984 static int elf_core_dump(long signr
, struct pt_regs
* regs
, struct file
* file
)
991 struct vm_area_struct
*vma
;
993 off_t offset
= 0, dataoff
;
994 unsigned long limit
= current
->rlim
[RLIMIT_CORE
].rlim_cur
;
996 struct memelfnote notes
[4];
997 struct elf_prstatus prstatus
; /* NT_PRSTATUS */
998 elf_fpregset_t fpu
; /* NT_PRFPREG */
999 struct elf_prpsinfo psinfo
; /* NT_PRPSINFO */
1001 segs
= current
->mm
->map_count
;
1004 printk("elf_core_dump: %d segs %lu limit\n", segs
, limit
);
1008 memcpy(elf
.e_ident
, ELFMAG
, SELFMAG
);
1009 elf
.e_ident
[EI_CLASS
] = ELF_CLASS
;
1010 elf
.e_ident
[EI_DATA
] = ELF_DATA
;
1011 elf
.e_ident
[EI_VERSION
] = EV_CURRENT
;
1012 memset(elf
.e_ident
+EI_PAD
, 0, EI_NIDENT
-EI_PAD
);
1014 elf
.e_type
= ET_CORE
;
1015 elf
.e_machine
= ELF_ARCH
;
1016 elf
.e_version
= EV_CURRENT
;
1018 elf
.e_phoff
= sizeof(elf
);
1021 elf
.e_ehsize
= sizeof(elf
);
1022 elf
.e_phentsize
= sizeof(struct elf_phdr
);
1023 elf
.e_phnum
= segs
+1; /* Include notes */
1024 elf
.e_shentsize
= 0;
1032 current
->flags
|= PF_DUMPCORE
;
1034 DUMP_WRITE(&elf
, sizeof(elf
));
1035 offset
+= sizeof(elf
); /* Elf header */
1036 offset
+= (segs
+1) * sizeof(struct elf_phdr
); /* Program headers */
1039 * Set up the notes in similar form to SVR4 core dumps made
1040 * with info from their /proc.
1042 memset(&psinfo
, 0, sizeof(psinfo
));
1043 memset(&prstatus
, 0, sizeof(prstatus
));
1045 notes
[0].name
= "CORE";
1046 notes
[0].type
= NT_PRSTATUS
;
1047 notes
[0].datasz
= sizeof(prstatus
);
1048 notes
[0].data
= &prstatus
;
1049 prstatus
.pr_info
.si_signo
= prstatus
.pr_cursig
= signr
;
1050 prstatus
.pr_sigpend
= current
->signal
.sig
[0];
1051 prstatus
.pr_sighold
= current
->blocked
.sig
[0];
1052 psinfo
.pr_pid
= prstatus
.pr_pid
= current
->pid
;
1053 psinfo
.pr_ppid
= prstatus
.pr_ppid
= current
->p_pptr
->pid
;
1054 psinfo
.pr_pgrp
= prstatus
.pr_pgrp
= current
->pgrp
;
1055 psinfo
.pr_sid
= prstatus
.pr_sid
= current
->session
;
1056 prstatus
.pr_utime
.tv_sec
= CT_TO_SECS(current
->times
.tms_utime
);
1057 prstatus
.pr_utime
.tv_usec
= CT_TO_USECS(current
->times
.tms_utime
);
1058 prstatus
.pr_stime
.tv_sec
= CT_TO_SECS(current
->times
.tms_stime
);
1059 prstatus
.pr_stime
.tv_usec
= CT_TO_USECS(current
->times
.tms_stime
);
1060 prstatus
.pr_cutime
.tv_sec
= CT_TO_SECS(current
->times
.tms_cutime
);
1061 prstatus
.pr_cutime
.tv_usec
= CT_TO_USECS(current
->times
.tms_cutime
);
1062 prstatus
.pr_cstime
.tv_sec
= CT_TO_SECS(current
->times
.tms_cstime
);
1063 prstatus
.pr_cstime
.tv_usec
= CT_TO_USECS(current
->times
.tms_cstime
);
1066 * This transfers the registers from regs into the standard
1067 * coredump arrangement, whatever that is.
1069 #ifdef ELF_CORE_COPY_REGS
1070 ELF_CORE_COPY_REGS(prstatus
.pr_reg
, regs
)
1072 if (sizeof(elf_gregset_t
) != sizeof(struct pt_regs
))
1074 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1075 (long)sizeof(elf_gregset_t
), (long)sizeof(struct pt_regs
));
1078 *(struct pt_regs
*)&prstatus
.pr_reg
= *regs
;
1082 dump_regs("Passed in regs", (elf_greg_t
*)regs
);
1083 dump_regs("prstatus regs", (elf_greg_t
*)&prstatus
.pr_reg
);
1086 notes
[1].name
= "CORE";
1087 notes
[1].type
= NT_PRPSINFO
;
1088 notes
[1].datasz
= sizeof(psinfo
);
1089 notes
[1].data
= &psinfo
;
1090 i
= current
->state
? ffz(~current
->state
) + 1 : 0;
1091 psinfo
.pr_state
= i
;
1092 psinfo
.pr_sname
= (i
< 0 || i
> 5) ? '.' : "RSDZTD"[i
];
1093 psinfo
.pr_zomb
= psinfo
.pr_sname
== 'Z';
1094 psinfo
.pr_nice
= current
->priority
-15;
1095 psinfo
.pr_flag
= current
->flags
;
1096 psinfo
.pr_uid
= NEW_TO_OLD_UID(current
->uid
);
1097 psinfo
.pr_gid
= NEW_TO_OLD_GID(current
->gid
);
1103 len
= current
->mm
->arg_end
- current
->mm
->arg_start
;
1104 if (len
>= ELF_PRARGSZ
)
1105 len
= ELF_PRARGSZ
-1;
1106 copy_from_user(&psinfo
.pr_psargs
,
1107 (const char *)current
->mm
->arg_start
, len
);
1108 for(i
= 0; i
< len
; i
++)
1109 if (psinfo
.pr_psargs
[i
] == 0)
1110 psinfo
.pr_psargs
[i
] = ' ';
1111 psinfo
.pr_psargs
[len
] = 0;
1115 strncpy(psinfo
.pr_fname
, current
->comm
, sizeof(psinfo
.pr_fname
));
1117 notes
[2].name
= "CORE";
1118 notes
[2].type
= NT_TASKSTRUCT
;
1119 notes
[2].datasz
= sizeof(*current
);
1120 notes
[2].data
= current
;
1122 /* Try to dump the FPU. */
1123 prstatus
.pr_fpvalid
= dump_fpu (regs
, &fpu
);
1124 if (!prstatus
.pr_fpvalid
)
1130 notes
[3].name
= "CORE";
1131 notes
[3].type
= NT_PRFPREG
;
1132 notes
[3].datasz
= sizeof(fpu
);
1133 notes
[3].data
= &fpu
;
1136 /* Write notes phdr entry */
1138 struct elf_phdr phdr
;
1141 for(i
= 0; i
< numnote
; i
++)
1142 sz
+= notesize(¬es
[i
]);
1144 phdr
.p_type
= PT_NOTE
;
1145 phdr
.p_offset
= offset
;
1153 offset
+= phdr
.p_filesz
;
1154 DUMP_WRITE(&phdr
, sizeof(phdr
));
1157 /* Page-align dumped data */
1158 dataoff
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
1160 /* Write program headers for segments dump */
1161 for(vma
= current
->mm
->mmap
; vma
!= NULL
; vma
= vma
->vm_next
) {
1162 struct elf_phdr phdr
;
1165 sz
= vma
->vm_end
- vma
->vm_start
;
1167 phdr
.p_type
= PT_LOAD
;
1168 phdr
.p_offset
= offset
;
1169 phdr
.p_vaddr
= vma
->vm_start
;
1171 phdr
.p_filesz
= maydump(vma
) ? sz
: 0;
1173 offset
+= phdr
.p_filesz
;
1174 phdr
.p_flags
= vma
->vm_flags
& VM_READ
? PF_R
: 0;
1175 if (vma
->vm_flags
& VM_WRITE
) phdr
.p_flags
|= PF_W
;
1176 if (vma
->vm_flags
& VM_EXEC
) phdr
.p_flags
|= PF_X
;
1177 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
1179 DUMP_WRITE(&phdr
, sizeof(phdr
));
1182 for(i
= 0; i
< numnote
; i
++)
1183 if (!writenote(¬es
[i
], file
))
1190 for(vma
= current
->mm
->mmap
; vma
!= NULL
; vma
= vma
->vm_next
) {
1196 printk("elf_core_dump: writing %08lx %lx\n", addr
, len
);
1198 for (addr
= vma
->vm_start
;
1200 addr
+= PAGE_SIZE
) {
1205 pgd
= pgd_offset(vma
->vm_mm
, addr
);
1206 pmd
= pmd_alloc(pgd
, addr
);
1210 pte
= pte_alloc(pmd
, addr
);
1213 if (!pte_present(*pte
) &&
1215 DUMP_SEEK (file
->f_pos
+ PAGE_SIZE
);
1217 DUMP_WRITE((void*)addr
, PAGE_SIZE
);
1222 if ((off_t
) file
->f_pos
!= offset
) {
1224 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1225 (off_t
) file
->f_pos
, offset
);
1232 #endif /* USE_ELF_CORE_DUMP */
1234 static int __init
init_elf_binfmt(void)
1236 return register_binfmt(&elf_format
);
1239 static void __exit
exit_elf_binfmt(void)
1241 /* Remove the COFF and ELF loaders. */
1242 unregister_binfmt(&elf_format
);
1245 module_init(init_elf_binfmt
)
1246 module_exit(exit_elf_binfmt
)