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>
13 #include <linux/kernel.h>
15 #include <linux/stat.h>
16 #include <linux/time.h>
18 #include <linux/mman.h>
19 #include <linux/errno.h>
20 #include <linux/signal.h>
21 #include <linux/binfmts.h>
22 #include <linux/string.h>
23 #include <linux/file.h>
24 #include <linux/fcntl.h>
25 #include <linux/ptrace.h>
26 #include <linux/slab.h>
27 #include <linux/shm.h>
28 #include <linux/personality.h>
29 #include <linux/elfcore.h>
30 #include <linux/init.h>
31 #include <linux/highuid.h>
32 #include <linux/smp.h>
33 #include <linux/compiler.h>
34 #include <linux/highmem.h>
35 #include <linux/pagemap.h>
36 #include <linux/security.h>
37 #include <linux/syscalls.h>
38 #include <linux/random.h>
39 #include <linux/elf.h>
40 #include <linux/utsname.h>
41 #include <asm/uaccess.h>
42 #include <asm/param.h>
45 static int load_elf_binary(struct linux_binprm
*bprm
, struct pt_regs
*regs
);
46 static int load_elf_library(struct file
*);
47 static unsigned long elf_map(struct file
*, unsigned long, struct elf_phdr
*,
48 int, int, unsigned long);
51 * If we don't support core dumping, then supply a NULL so we
54 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
55 static int elf_core_dump(long signr
, struct pt_regs
*regs
, struct file
*file
, unsigned long limit
);
57 #define elf_core_dump NULL
60 #if ELF_EXEC_PAGESIZE > PAGE_SIZE
61 #define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
63 #define ELF_MIN_ALIGN PAGE_SIZE
66 #ifndef ELF_CORE_EFLAGS
67 #define ELF_CORE_EFLAGS 0
70 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
71 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
72 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
74 static struct linux_binfmt elf_format
= {
75 .module
= THIS_MODULE
,
76 .load_binary
= load_elf_binary
,
77 .load_shlib
= load_elf_library
,
78 .core_dump
= elf_core_dump
,
79 .min_coredump
= ELF_EXEC_PAGESIZE
,
83 #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
85 static int set_brk(unsigned long start
, unsigned long end
)
87 start
= ELF_PAGEALIGN(start
);
88 end
= ELF_PAGEALIGN(end
);
91 down_write(¤t
->mm
->mmap_sem
);
92 addr
= do_brk(start
, end
- start
);
93 up_write(¤t
->mm
->mmap_sem
);
97 current
->mm
->start_brk
= current
->mm
->brk
= end
;
101 /* We need to explicitly zero any fractional pages
102 after the data section (i.e. bss). This would
103 contain the junk from the file that should not
106 static int padzero(unsigned long elf_bss
)
110 nbyte
= ELF_PAGEOFFSET(elf_bss
);
112 nbyte
= ELF_MIN_ALIGN
- nbyte
;
113 if (clear_user((void __user
*) elf_bss
, nbyte
))
119 /* Let's use some macros to make this stack manipulation a little clearer */
120 #ifdef CONFIG_STACK_GROWSUP
121 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
122 #define STACK_ROUND(sp, items) \
123 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
124 #define STACK_ALLOC(sp, len) ({ \
125 elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
128 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
129 #define STACK_ROUND(sp, items) \
130 (((unsigned long) (sp - items)) &~ 15UL)
131 #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
134 #ifndef ELF_BASE_PLATFORM
136 * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture.
137 * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value
138 * will be copied to the user stack in the same manner as AT_PLATFORM.
140 #define ELF_BASE_PLATFORM NULL
144 create_elf_tables(struct linux_binprm
*bprm
, struct elfhdr
*exec
,
145 unsigned long load_addr
, unsigned long interp_load_addr
)
147 unsigned long p
= bprm
->p
;
148 int argc
= bprm
->argc
;
149 int envc
= bprm
->envc
;
150 elf_addr_t __user
*argv
;
151 elf_addr_t __user
*envp
;
152 elf_addr_t __user
*sp
;
153 elf_addr_t __user
*u_platform
;
154 elf_addr_t __user
*u_base_platform
;
155 const char *k_platform
= ELF_PLATFORM
;
156 const char *k_base_platform
= ELF_BASE_PLATFORM
;
158 elf_addr_t
*elf_info
;
160 struct task_struct
*tsk
= current
;
161 struct vm_area_struct
*vma
;
164 * In some cases (e.g. Hyper-Threading), we want to avoid L1
165 * evictions by the processes running on the same package. One
166 * thing we can do is to shuffle the initial stack for them.
169 p
= arch_align_stack(p
);
172 * If this architecture has a platform capability string, copy it
173 * to userspace. In some cases (Sparc), this info is impossible
174 * for userspace to get any other way, in others (i386) it is
179 size_t len
= strlen(k_platform
) + 1;
181 u_platform
= (elf_addr_t __user
*)STACK_ALLOC(p
, len
);
182 if (__copy_to_user(u_platform
, k_platform
, len
))
187 * If this architecture has a "base" platform capability
188 * string, copy it to userspace.
190 u_base_platform
= NULL
;
191 if (k_base_platform
) {
192 size_t len
= strlen(k_base_platform
) + 1;
194 u_base_platform
= (elf_addr_t __user
*)STACK_ALLOC(p
, len
);
195 if (__copy_to_user(u_base_platform
, k_base_platform
, len
))
199 /* Create the ELF interpreter info */
200 elf_info
= (elf_addr_t
*)current
->mm
->saved_auxv
;
201 /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
202 #define NEW_AUX_ENT(id, val) \
204 elf_info[ei_index++] = id; \
205 elf_info[ei_index++] = val; \
210 * ARCH_DLINFO must come first so PPC can do its special alignment of
212 * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
213 * ARCH_DLINFO changes
217 NEW_AUX_ENT(AT_HWCAP
, ELF_HWCAP
);
218 NEW_AUX_ENT(AT_PAGESZ
, ELF_EXEC_PAGESIZE
);
219 NEW_AUX_ENT(AT_CLKTCK
, CLOCKS_PER_SEC
);
220 NEW_AUX_ENT(AT_PHDR
, load_addr
+ exec
->e_phoff
);
221 NEW_AUX_ENT(AT_PHENT
, sizeof(struct elf_phdr
));
222 NEW_AUX_ENT(AT_PHNUM
, exec
->e_phnum
);
223 NEW_AUX_ENT(AT_BASE
, interp_load_addr
);
224 NEW_AUX_ENT(AT_FLAGS
, 0);
225 NEW_AUX_ENT(AT_ENTRY
, exec
->e_entry
);
226 NEW_AUX_ENT(AT_UID
, tsk
->uid
);
227 NEW_AUX_ENT(AT_EUID
, tsk
->euid
);
228 NEW_AUX_ENT(AT_GID
, tsk
->gid
);
229 NEW_AUX_ENT(AT_EGID
, tsk
->egid
);
230 NEW_AUX_ENT(AT_SECURE
, security_bprm_secureexec(bprm
));
231 NEW_AUX_ENT(AT_EXECFN
, bprm
->exec
);
233 NEW_AUX_ENT(AT_PLATFORM
,
234 (elf_addr_t
)(unsigned long)u_platform
);
236 if (k_base_platform
) {
237 NEW_AUX_ENT(AT_BASE_PLATFORM
,
238 (elf_addr_t
)(unsigned long)u_base_platform
);
240 if (bprm
->interp_flags
& BINPRM_FLAGS_EXECFD
) {
241 NEW_AUX_ENT(AT_EXECFD
, bprm
->interp_data
);
244 /* AT_NULL is zero; clear the rest too */
245 memset(&elf_info
[ei_index
], 0,
246 sizeof current
->mm
->saved_auxv
- ei_index
* sizeof elf_info
[0]);
248 /* And advance past the AT_NULL entry. */
251 sp
= STACK_ADD(p
, ei_index
);
253 items
= (argc
+ 1) + (envc
+ 1) + 1;
254 bprm
->p
= STACK_ROUND(sp
, items
);
256 /* Point sp at the lowest address on the stack */
257 #ifdef CONFIG_STACK_GROWSUP
258 sp
= (elf_addr_t __user
*)bprm
->p
- items
- ei_index
;
259 bprm
->exec
= (unsigned long)sp
; /* XXX: PARISC HACK */
261 sp
= (elf_addr_t __user
*)bprm
->p
;
266 * Grow the stack manually; some architectures have a limit on how
267 * far ahead a user-space access may be in order to grow the stack.
269 vma
= find_extend_vma(current
->mm
, bprm
->p
);
273 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
274 if (__put_user(argc
, sp
++))
277 envp
= argv
+ argc
+ 1;
279 /* Populate argv and envp */
280 p
= current
->mm
->arg_end
= current
->mm
->arg_start
;
283 if (__put_user((elf_addr_t
)p
, argv
++))
285 len
= strnlen_user((void __user
*)p
, MAX_ARG_STRLEN
);
286 if (!len
|| len
> MAX_ARG_STRLEN
)
290 if (__put_user(0, argv
))
292 current
->mm
->arg_end
= current
->mm
->env_start
= p
;
295 if (__put_user((elf_addr_t
)p
, envp
++))
297 len
= strnlen_user((void __user
*)p
, MAX_ARG_STRLEN
);
298 if (!len
|| len
> MAX_ARG_STRLEN
)
302 if (__put_user(0, envp
))
304 current
->mm
->env_end
= p
;
306 /* Put the elf_info on the stack in the right place. */
307 sp
= (elf_addr_t __user
*)envp
+ 1;
308 if (copy_to_user(sp
, elf_info
, ei_index
* sizeof(elf_addr_t
)))
315 static unsigned long elf_map(struct file
*filep
, unsigned long addr
,
316 struct elf_phdr
*eppnt
, int prot
, int type
,
317 unsigned long total_size
)
319 unsigned long map_addr
;
320 unsigned long size
= eppnt
->p_filesz
+ ELF_PAGEOFFSET(eppnt
->p_vaddr
);
321 unsigned long off
= eppnt
->p_offset
- ELF_PAGEOFFSET(eppnt
->p_vaddr
);
322 addr
= ELF_PAGESTART(addr
);
323 size
= ELF_PAGEALIGN(size
);
325 /* mmap() will return -EINVAL if given a zero size, but a
326 * segment with zero filesize is perfectly valid */
330 down_write(¤t
->mm
->mmap_sem
);
332 * total_size is the size of the ELF (interpreter) image.
333 * The _first_ mmap needs to know the full size, otherwise
334 * randomization might put this image into an overlapping
335 * position with the ELF binary image. (since size < total_size)
336 * So we first map the 'big' image - and unmap the remainder at
337 * the end. (which unmap is needed for ELF images with holes.)
340 total_size
= ELF_PAGEALIGN(total_size
);
341 map_addr
= do_mmap(filep
, addr
, total_size
, prot
, type
, off
);
342 if (!BAD_ADDR(map_addr
))
343 do_munmap(current
->mm
, map_addr
+size
, total_size
-size
);
345 map_addr
= do_mmap(filep
, addr
, size
, prot
, type
, off
);
347 up_write(¤t
->mm
->mmap_sem
);
351 #endif /* !elf_map */
353 static unsigned long total_mapping_size(struct elf_phdr
*cmds
, int nr
)
355 int i
, first_idx
= -1, last_idx
= -1;
357 for (i
= 0; i
< nr
; i
++) {
358 if (cmds
[i
].p_type
== PT_LOAD
) {
367 return cmds
[last_idx
].p_vaddr
+ cmds
[last_idx
].p_memsz
-
368 ELF_PAGESTART(cmds
[first_idx
].p_vaddr
);
372 /* This is much more generalized than the library routine read function,
373 so we keep this separate. Technically the library read function
374 is only provided so that we can read a.out libraries that have
377 static unsigned long load_elf_interp(struct elfhdr
*interp_elf_ex
,
378 struct file
*interpreter
, unsigned long *interp_map_addr
,
379 unsigned long no_base
)
381 struct elf_phdr
*elf_phdata
;
382 struct elf_phdr
*eppnt
;
383 unsigned long load_addr
= 0;
384 int load_addr_set
= 0;
385 unsigned long last_bss
= 0, elf_bss
= 0;
386 unsigned long error
= ~0UL;
387 unsigned long total_size
;
390 /* First of all, some simple consistency checks */
391 if (interp_elf_ex
->e_type
!= ET_EXEC
&&
392 interp_elf_ex
->e_type
!= ET_DYN
)
394 if (!elf_check_arch(interp_elf_ex
))
396 if (!interpreter
->f_op
|| !interpreter
->f_op
->mmap
)
400 * If the size of this structure has changed, then punt, since
401 * we will be doing the wrong thing.
403 if (interp_elf_ex
->e_phentsize
!= sizeof(struct elf_phdr
))
405 if (interp_elf_ex
->e_phnum
< 1 ||
406 interp_elf_ex
->e_phnum
> 65536U / sizeof(struct elf_phdr
))
409 /* Now read in all of the header information */
410 size
= sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
;
411 if (size
> ELF_MIN_ALIGN
)
413 elf_phdata
= kmalloc(size
, GFP_KERNEL
);
417 retval
= kernel_read(interpreter
, interp_elf_ex
->e_phoff
,
418 (char *)elf_phdata
,size
);
420 if (retval
!= size
) {
426 total_size
= total_mapping_size(elf_phdata
, interp_elf_ex
->e_phnum
);
433 for (i
= 0; i
< interp_elf_ex
->e_phnum
; i
++, eppnt
++) {
434 if (eppnt
->p_type
== PT_LOAD
) {
435 int elf_type
= MAP_PRIVATE
| MAP_DENYWRITE
;
437 unsigned long vaddr
= 0;
438 unsigned long k
, map_addr
;
440 if (eppnt
->p_flags
& PF_R
)
441 elf_prot
= PROT_READ
;
442 if (eppnt
->p_flags
& PF_W
)
443 elf_prot
|= PROT_WRITE
;
444 if (eppnt
->p_flags
& PF_X
)
445 elf_prot
|= PROT_EXEC
;
446 vaddr
= eppnt
->p_vaddr
;
447 if (interp_elf_ex
->e_type
== ET_EXEC
|| load_addr_set
)
448 elf_type
|= MAP_FIXED
;
449 else if (no_base
&& interp_elf_ex
->e_type
== ET_DYN
)
452 map_addr
= elf_map(interpreter
, load_addr
+ vaddr
,
453 eppnt
, elf_prot
, elf_type
, total_size
);
455 if (!*interp_map_addr
)
456 *interp_map_addr
= map_addr
;
458 if (BAD_ADDR(map_addr
))
461 if (!load_addr_set
&&
462 interp_elf_ex
->e_type
== ET_DYN
) {
463 load_addr
= map_addr
- ELF_PAGESTART(vaddr
);
468 * Check to see if the section's size will overflow the
469 * allowed task size. Note that p_filesz must always be
470 * <= p_memsize so it's only necessary to check p_memsz.
472 k
= load_addr
+ eppnt
->p_vaddr
;
474 eppnt
->p_filesz
> eppnt
->p_memsz
||
475 eppnt
->p_memsz
> TASK_SIZE
||
476 TASK_SIZE
- eppnt
->p_memsz
< k
) {
482 * Find the end of the file mapping for this phdr, and
483 * keep track of the largest address we see for this.
485 k
= load_addr
+ eppnt
->p_vaddr
+ eppnt
->p_filesz
;
490 * Do the same thing for the memory mapping - between
491 * elf_bss and last_bss is the bss section.
493 k
= load_addr
+ eppnt
->p_memsz
+ eppnt
->p_vaddr
;
500 * Now fill out the bss section. First pad the last page up
501 * to the page boundary, and then perform a mmap to make sure
502 * that there are zero-mapped pages up to and including the
505 if (padzero(elf_bss
)) {
510 /* What we have mapped so far */
511 elf_bss
= ELF_PAGESTART(elf_bss
+ ELF_MIN_ALIGN
- 1);
513 /* Map the last of the bss segment */
514 if (last_bss
> elf_bss
) {
515 down_write(¤t
->mm
->mmap_sem
);
516 error
= do_brk(elf_bss
, last_bss
- elf_bss
);
517 up_write(¤t
->mm
->mmap_sem
);
531 * These are the functions used to load ELF style executables and shared
532 * libraries. There is no binary dependent code anywhere else.
535 #define INTERPRETER_NONE 0
536 #define INTERPRETER_ELF 2
538 #ifndef STACK_RND_MASK
539 #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
542 static unsigned long randomize_stack_top(unsigned long stack_top
)
544 unsigned int random_variable
= 0;
546 if ((current
->flags
& PF_RANDOMIZE
) &&
547 !(current
->personality
& ADDR_NO_RANDOMIZE
)) {
548 random_variable
= get_random_int() & STACK_RND_MASK
;
549 random_variable
<<= PAGE_SHIFT
;
551 #ifdef CONFIG_STACK_GROWSUP
552 return PAGE_ALIGN(stack_top
) + random_variable
;
554 return PAGE_ALIGN(stack_top
) - random_variable
;
558 static int load_elf_binary(struct linux_binprm
*bprm
, struct pt_regs
*regs
)
560 struct file
*interpreter
= NULL
; /* to shut gcc up */
561 unsigned long load_addr
= 0, load_bias
= 0;
562 int load_addr_set
= 0;
563 char * elf_interpreter
= NULL
;
565 struct elf_phdr
*elf_ppnt
, *elf_phdata
;
566 unsigned long elf_bss
, elf_brk
;
570 unsigned long elf_entry
;
571 unsigned long interp_load_addr
= 0;
572 unsigned long start_code
, end_code
, start_data
, end_data
;
573 unsigned long reloc_func_desc
= 0;
574 int executable_stack
= EXSTACK_DEFAULT
;
575 unsigned long def_flags
= 0;
577 struct elfhdr elf_ex
;
578 struct elfhdr interp_elf_ex
;
581 loc
= kmalloc(sizeof(*loc
), GFP_KERNEL
);
587 /* Get the exec-header */
588 loc
->elf_ex
= *((struct elfhdr
*)bprm
->buf
);
591 /* First of all, some simple consistency checks */
592 if (memcmp(loc
->elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
595 if (loc
->elf_ex
.e_type
!= ET_EXEC
&& loc
->elf_ex
.e_type
!= ET_DYN
)
597 if (!elf_check_arch(&loc
->elf_ex
))
599 if (!bprm
->file
->f_op
||!bprm
->file
->f_op
->mmap
)
602 /* Now read in all of the header information */
603 if (loc
->elf_ex
.e_phentsize
!= sizeof(struct elf_phdr
))
605 if (loc
->elf_ex
.e_phnum
< 1 ||
606 loc
->elf_ex
.e_phnum
> 65536U / sizeof(struct elf_phdr
))
608 size
= loc
->elf_ex
.e_phnum
* sizeof(struct elf_phdr
);
610 elf_phdata
= kmalloc(size
, GFP_KERNEL
);
614 retval
= kernel_read(bprm
->file
, loc
->elf_ex
.e_phoff
,
615 (char *)elf_phdata
, size
);
616 if (retval
!= size
) {
622 retval
= get_unused_fd();
625 get_file(bprm
->file
);
626 fd_install(elf_exec_fileno
= retval
, bprm
->file
);
628 elf_ppnt
= elf_phdata
;
637 for (i
= 0; i
< loc
->elf_ex
.e_phnum
; i
++) {
638 if (elf_ppnt
->p_type
== PT_INTERP
) {
639 /* This is the program interpreter used for
640 * shared libraries - for now assume that this
641 * is an a.out format binary
644 if (elf_ppnt
->p_filesz
> PATH_MAX
||
645 elf_ppnt
->p_filesz
< 2)
649 elf_interpreter
= kmalloc(elf_ppnt
->p_filesz
,
651 if (!elf_interpreter
)
654 retval
= kernel_read(bprm
->file
, elf_ppnt
->p_offset
,
657 if (retval
!= elf_ppnt
->p_filesz
) {
660 goto out_free_interp
;
662 /* make sure path is NULL terminated */
664 if (elf_interpreter
[elf_ppnt
->p_filesz
- 1] != '\0')
665 goto out_free_interp
;
668 * The early SET_PERSONALITY here is so that the lookup
669 * for the interpreter happens in the namespace of the
670 * to-be-execed image. SET_PERSONALITY can select an
673 * However, SET_PERSONALITY is NOT allowed to switch
674 * this task into the new images's memory mapping
675 * policy - that is, TASK_SIZE must still evaluate to
676 * that which is appropriate to the execing application.
677 * This is because exit_mmap() needs to have TASK_SIZE
678 * evaluate to the size of the old image.
680 * So if (say) a 64-bit application is execing a 32-bit
681 * application it is the architecture's responsibility
682 * to defer changing the value of TASK_SIZE until the
683 * switch really is going to happen - do this in
684 * flush_thread(). - akpm
686 SET_PERSONALITY(loc
->elf_ex
, 0);
688 interpreter
= open_exec(elf_interpreter
);
689 retval
= PTR_ERR(interpreter
);
690 if (IS_ERR(interpreter
))
691 goto out_free_interp
;
694 * If the binary is not readable then enforce
695 * mm->dumpable = 0 regardless of the interpreter's
698 if (file_permission(interpreter
, MAY_READ
) < 0)
699 bprm
->interp_flags
|= BINPRM_FLAGS_ENFORCE_NONDUMP
;
701 retval
= kernel_read(interpreter
, 0, bprm
->buf
,
703 if (retval
!= BINPRM_BUF_SIZE
) {
706 goto out_free_dentry
;
709 /* Get the exec headers */
710 loc
->interp_elf_ex
= *((struct elfhdr
*)bprm
->buf
);
716 elf_ppnt
= elf_phdata
;
717 for (i
= 0; i
< loc
->elf_ex
.e_phnum
; i
++, elf_ppnt
++)
718 if (elf_ppnt
->p_type
== PT_GNU_STACK
) {
719 if (elf_ppnt
->p_flags
& PF_X
)
720 executable_stack
= EXSTACK_ENABLE_X
;
722 executable_stack
= EXSTACK_DISABLE_X
;
726 /* Some simple consistency checks for the interpreter */
727 if (elf_interpreter
) {
729 /* Not an ELF interpreter */
730 if (memcmp(loc
->interp_elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
731 goto out_free_dentry
;
732 /* Verify the interpreter has a valid arch */
733 if (!elf_check_arch(&loc
->interp_elf_ex
))
734 goto out_free_dentry
;
736 /* Executables without an interpreter also need a personality */
737 SET_PERSONALITY(loc
->elf_ex
, 0);
740 /* Flush all traces of the currently running executable */
741 retval
= flush_old_exec(bprm
);
743 goto out_free_dentry
;
745 /* OK, This is the point of no return */
746 current
->flags
&= ~PF_FORKNOEXEC
;
747 current
->mm
->def_flags
= def_flags
;
749 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
750 may depend on the personality. */
751 SET_PERSONALITY(loc
->elf_ex
, 0);
752 if (elf_read_implies_exec(loc
->elf_ex
, executable_stack
))
753 current
->personality
|= READ_IMPLIES_EXEC
;
755 if (!(current
->personality
& ADDR_NO_RANDOMIZE
) && randomize_va_space
)
756 current
->flags
|= PF_RANDOMIZE
;
757 arch_pick_mmap_layout(current
->mm
);
759 /* Do this so that we can load the interpreter, if need be. We will
760 change some of these later */
761 current
->mm
->free_area_cache
= current
->mm
->mmap_base
;
762 current
->mm
->cached_hole_size
= 0;
763 retval
= setup_arg_pages(bprm
, randomize_stack_top(STACK_TOP
),
766 send_sig(SIGKILL
, current
, 0);
767 goto out_free_dentry
;
770 current
->mm
->start_stack
= bprm
->p
;
772 /* Now we do a little grungy work by mmaping the ELF image into
773 the correct location in memory. */
774 for(i
= 0, elf_ppnt
= elf_phdata
;
775 i
< loc
->elf_ex
.e_phnum
; i
++, elf_ppnt
++) {
776 int elf_prot
= 0, elf_flags
;
777 unsigned long k
, vaddr
;
779 if (elf_ppnt
->p_type
!= PT_LOAD
)
782 if (unlikely (elf_brk
> elf_bss
)) {
785 /* There was a PT_LOAD segment with p_memsz > p_filesz
786 before this one. Map anonymous pages, if needed,
787 and clear the area. */
788 retval
= set_brk (elf_bss
+ load_bias
,
789 elf_brk
+ load_bias
);
791 send_sig(SIGKILL
, current
, 0);
792 goto out_free_dentry
;
794 nbyte
= ELF_PAGEOFFSET(elf_bss
);
796 nbyte
= ELF_MIN_ALIGN
- nbyte
;
797 if (nbyte
> elf_brk
- elf_bss
)
798 nbyte
= elf_brk
- elf_bss
;
799 if (clear_user((void __user
*)elf_bss
+
802 * This bss-zeroing can fail if the ELF
803 * file specifies odd protections. So
804 * we don't check the return value
810 if (elf_ppnt
->p_flags
& PF_R
)
811 elf_prot
|= PROT_READ
;
812 if (elf_ppnt
->p_flags
& PF_W
)
813 elf_prot
|= PROT_WRITE
;
814 if (elf_ppnt
->p_flags
& PF_X
)
815 elf_prot
|= PROT_EXEC
;
817 elf_flags
= MAP_PRIVATE
| MAP_DENYWRITE
| MAP_EXECUTABLE
;
819 vaddr
= elf_ppnt
->p_vaddr
;
820 if (loc
->elf_ex
.e_type
== ET_EXEC
|| load_addr_set
) {
821 elf_flags
|= MAP_FIXED
;
822 } else if (loc
->elf_ex
.e_type
== ET_DYN
) {
823 /* Try and get dynamic programs out of the way of the
824 * default mmap base, as well as whatever program they
825 * might try to exec. This is because the brk will
826 * follow the loader, and is not movable. */
830 load_bias
= ELF_PAGESTART(ELF_ET_DYN_BASE
- vaddr
);
834 error
= elf_map(bprm
->file
, load_bias
+ vaddr
, elf_ppnt
,
835 elf_prot
, elf_flags
, 0);
836 if (BAD_ADDR(error
)) {
837 send_sig(SIGKILL
, current
, 0);
838 retval
= IS_ERR((void *)error
) ?
839 PTR_ERR((void*)error
) : -EINVAL
;
840 goto out_free_dentry
;
843 if (!load_addr_set
) {
845 load_addr
= (elf_ppnt
->p_vaddr
- elf_ppnt
->p_offset
);
846 if (loc
->elf_ex
.e_type
== ET_DYN
) {
848 ELF_PAGESTART(load_bias
+ vaddr
);
849 load_addr
+= load_bias
;
850 reloc_func_desc
= load_bias
;
853 k
= elf_ppnt
->p_vaddr
;
860 * Check to see if the section's size will overflow the
861 * allowed task size. Note that p_filesz must always be
862 * <= p_memsz so it is only necessary to check p_memsz.
864 if (BAD_ADDR(k
) || elf_ppnt
->p_filesz
> elf_ppnt
->p_memsz
||
865 elf_ppnt
->p_memsz
> TASK_SIZE
||
866 TASK_SIZE
- elf_ppnt
->p_memsz
< k
) {
867 /* set_brk can never work. Avoid overflows. */
868 send_sig(SIGKILL
, current
, 0);
870 goto out_free_dentry
;
873 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_filesz
;
877 if ((elf_ppnt
->p_flags
& PF_X
) && end_code
< k
)
881 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_memsz
;
886 loc
->elf_ex
.e_entry
+= load_bias
;
887 elf_bss
+= load_bias
;
888 elf_brk
+= load_bias
;
889 start_code
+= load_bias
;
890 end_code
+= load_bias
;
891 start_data
+= load_bias
;
892 end_data
+= load_bias
;
894 /* Calling set_brk effectively mmaps the pages that we need
895 * for the bss and break sections. We must do this before
896 * mapping in the interpreter, to make sure it doesn't wind
897 * up getting placed where the bss needs to go.
899 retval
= set_brk(elf_bss
, elf_brk
);
901 send_sig(SIGKILL
, current
, 0);
902 goto out_free_dentry
;
904 if (likely(elf_bss
!= elf_brk
) && unlikely(padzero(elf_bss
))) {
905 send_sig(SIGSEGV
, current
, 0);
906 retval
= -EFAULT
; /* Nobody gets to see this, but.. */
907 goto out_free_dentry
;
910 if (elf_interpreter
) {
911 unsigned long uninitialized_var(interp_map_addr
);
913 elf_entry
= load_elf_interp(&loc
->interp_elf_ex
,
917 if (!IS_ERR((void *)elf_entry
)) {
919 * load_elf_interp() returns relocation
922 interp_load_addr
= elf_entry
;
923 elf_entry
+= loc
->interp_elf_ex
.e_entry
;
925 if (BAD_ADDR(elf_entry
)) {
926 force_sig(SIGSEGV
, current
);
927 retval
= IS_ERR((void *)elf_entry
) ?
928 (int)elf_entry
: -EINVAL
;
929 goto out_free_dentry
;
931 reloc_func_desc
= interp_load_addr
;
933 allow_write_access(interpreter
);
935 kfree(elf_interpreter
);
937 elf_entry
= loc
->elf_ex
.e_entry
;
938 if (BAD_ADDR(elf_entry
)) {
939 force_sig(SIGSEGV
, current
);
941 goto out_free_dentry
;
947 sys_close(elf_exec_fileno
);
949 set_binfmt(&elf_format
);
951 #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
952 retval
= arch_setup_additional_pages(bprm
, executable_stack
);
954 send_sig(SIGKILL
, current
, 0);
957 #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
960 current
->flags
&= ~PF_FORKNOEXEC
;
961 retval
= create_elf_tables(bprm
, &loc
->elf_ex
,
962 load_addr
, interp_load_addr
);
964 send_sig(SIGKILL
, current
, 0);
967 /* N.B. passed_fileno might not be initialized? */
968 current
->mm
->end_code
= end_code
;
969 current
->mm
->start_code
= start_code
;
970 current
->mm
->start_data
= start_data
;
971 current
->mm
->end_data
= end_data
;
972 current
->mm
->start_stack
= bprm
->p
;
974 #ifdef arch_randomize_brk
975 if ((current
->flags
& PF_RANDOMIZE
) && (randomize_va_space
> 1))
976 current
->mm
->brk
= current
->mm
->start_brk
=
977 arch_randomize_brk(current
->mm
);
980 if (current
->personality
& MMAP_PAGE_ZERO
) {
981 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
982 and some applications "depend" upon this behavior.
983 Since we do not have the power to recompile these, we
984 emulate the SVr4 behavior. Sigh. */
985 down_write(¤t
->mm
->mmap_sem
);
986 error
= do_mmap(NULL
, 0, PAGE_SIZE
, PROT_READ
| PROT_EXEC
,
987 MAP_FIXED
| MAP_PRIVATE
, 0);
988 up_write(¤t
->mm
->mmap_sem
);
993 * The ABI may specify that certain registers be set up in special
994 * ways (on i386 %edx is the address of a DT_FINI function, for
995 * example. In addition, it may also specify (eg, PowerPC64 ELF)
996 * that the e_entry field is the address of the function descriptor
997 * for the startup routine, rather than the address of the startup
998 * routine itself. This macro performs whatever initialization to
999 * the regs structure is required as well as any relocations to the
1000 * function descriptor entries when executing dynamically links apps.
1002 ELF_PLAT_INIT(regs
, reloc_func_desc
);
1005 start_thread(regs
, elf_entry
, bprm
->p
);
1014 allow_write_access(interpreter
);
1018 kfree(elf_interpreter
);
1020 sys_close(elf_exec_fileno
);
1026 /* This is really simpleminded and specialized - we are loading an
1027 a.out library that is given an ELF header. */
1028 static int load_elf_library(struct file
*file
)
1030 struct elf_phdr
*elf_phdata
;
1031 struct elf_phdr
*eppnt
;
1032 unsigned long elf_bss
, bss
, len
;
1033 int retval
, error
, i
, j
;
1034 struct elfhdr elf_ex
;
1037 retval
= kernel_read(file
, 0, (char *)&elf_ex
, sizeof(elf_ex
));
1038 if (retval
!= sizeof(elf_ex
))
1041 if (memcmp(elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
1044 /* First of all, some simple consistency checks */
1045 if (elf_ex
.e_type
!= ET_EXEC
|| elf_ex
.e_phnum
> 2 ||
1046 !elf_check_arch(&elf_ex
) || !file
->f_op
|| !file
->f_op
->mmap
)
1049 /* Now read in all of the header information */
1051 j
= sizeof(struct elf_phdr
) * elf_ex
.e_phnum
;
1052 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1055 elf_phdata
= kmalloc(j
, GFP_KERNEL
);
1061 retval
= kernel_read(file
, elf_ex
.e_phoff
, (char *)eppnt
, j
);
1065 for (j
= 0, i
= 0; i
<elf_ex
.e_phnum
; i
++)
1066 if ((eppnt
+ i
)->p_type
== PT_LOAD
)
1071 while (eppnt
->p_type
!= PT_LOAD
)
1074 /* Now use mmap to map the library into memory. */
1075 down_write(¤t
->mm
->mmap_sem
);
1076 error
= do_mmap(file
,
1077 ELF_PAGESTART(eppnt
->p_vaddr
),
1079 ELF_PAGEOFFSET(eppnt
->p_vaddr
)),
1080 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
1081 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
,
1083 ELF_PAGEOFFSET(eppnt
->p_vaddr
)));
1084 up_write(¤t
->mm
->mmap_sem
);
1085 if (error
!= ELF_PAGESTART(eppnt
->p_vaddr
))
1088 elf_bss
= eppnt
->p_vaddr
+ eppnt
->p_filesz
;
1089 if (padzero(elf_bss
)) {
1094 len
= ELF_PAGESTART(eppnt
->p_filesz
+ eppnt
->p_vaddr
+
1096 bss
= eppnt
->p_memsz
+ eppnt
->p_vaddr
;
1098 down_write(¤t
->mm
->mmap_sem
);
1099 do_brk(len
, bss
- len
);
1100 up_write(¤t
->mm
->mmap_sem
);
1111 * Note that some platforms still use traditional core dumps and not
1112 * the ELF core dump. Each platform can select it as appropriate.
1114 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1119 * Modelled on fs/exec.c:aout_core_dump()
1120 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1123 * These are the only things you should do on a core-file: use only these
1124 * functions to write out all the necessary info.
1126 static int dump_write(struct file
*file
, const void *addr
, int nr
)
1128 return file
->f_op
->write(file
, addr
, nr
, &file
->f_pos
) == nr
;
1131 static int dump_seek(struct file
*file
, loff_t off
)
1133 if (file
->f_op
->llseek
&& file
->f_op
->llseek
!= no_llseek
) {
1134 if (file
->f_op
->llseek(file
, off
, SEEK_CUR
) < 0)
1137 char *buf
= (char *)get_zeroed_page(GFP_KERNEL
);
1141 unsigned long n
= off
;
1144 if (!dump_write(file
, buf
, n
))
1148 free_page((unsigned long)buf
);
1154 * Decide what to dump of a segment, part, all or none.
1156 static unsigned long vma_dump_size(struct vm_area_struct
*vma
,
1157 unsigned long mm_flags
)
1159 /* The vma can be set up to tell us the answer directly. */
1160 if (vma
->vm_flags
& VM_ALWAYSDUMP
)
1163 /* Do not dump I/O mapped devices or special mappings */
1164 if (vma
->vm_flags
& (VM_IO
| VM_RESERVED
))
1167 #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1169 /* By default, dump shared memory if mapped from an anonymous file. */
1170 if (vma
->vm_flags
& VM_SHARED
) {
1171 if (vma
->vm_file
->f_path
.dentry
->d_inode
->i_nlink
== 0 ?
1172 FILTER(ANON_SHARED
) : FILTER(MAPPED_SHARED
))
1177 /* Dump segments that have been written to. */
1178 if (vma
->anon_vma
&& FILTER(ANON_PRIVATE
))
1180 if (vma
->vm_file
== NULL
)
1183 if (FILTER(MAPPED_PRIVATE
))
1187 * If this looks like the beginning of a DSO or executable mapping,
1188 * check for an ELF header. If we find one, dump the first page to
1189 * aid in determining what was mapped here.
1191 if (FILTER(ELF_HEADERS
) &&
1192 vma
->vm_pgoff
== 0 && (vma
->vm_flags
& VM_READ
)) {
1193 u32 __user
*header
= (u32 __user
*) vma
->vm_start
;
1195 mm_segment_t fs
= get_fs();
1197 * Doing it this way gets the constant folded by GCC.
1201 char elfmag
[SELFMAG
];
1203 BUILD_BUG_ON(SELFMAG
!= sizeof word
);
1204 magic
.elfmag
[EI_MAG0
] = ELFMAG0
;
1205 magic
.elfmag
[EI_MAG1
] = ELFMAG1
;
1206 magic
.elfmag
[EI_MAG2
] = ELFMAG2
;
1207 magic
.elfmag
[EI_MAG3
] = ELFMAG3
;
1209 * Switch to the user "segment" for get_user(),
1210 * then put back what elf_core_dump() had in place.
1213 if (unlikely(get_user(word
, header
)))
1216 if (word
== magic
.cmp
)
1225 return vma
->vm_end
- vma
->vm_start
;
1228 /* An ELF note in memory */
1233 unsigned int datasz
;
1237 static int notesize(struct memelfnote
*en
)
1241 sz
= sizeof(struct elf_note
);
1242 sz
+= roundup(strlen(en
->name
) + 1, 4);
1243 sz
+= roundup(en
->datasz
, 4);
1248 #define DUMP_WRITE(addr, nr, foffset) \
1249 do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
1251 static int alignfile(struct file
*file
, loff_t
*foffset
)
1253 static const char buf
[4] = { 0, };
1254 DUMP_WRITE(buf
, roundup(*foffset
, 4) - *foffset
, foffset
);
1258 static int writenote(struct memelfnote
*men
, struct file
*file
,
1262 en
.n_namesz
= strlen(men
->name
) + 1;
1263 en
.n_descsz
= men
->datasz
;
1264 en
.n_type
= men
->type
;
1266 DUMP_WRITE(&en
, sizeof(en
), foffset
);
1267 DUMP_WRITE(men
->name
, en
.n_namesz
, foffset
);
1268 if (!alignfile(file
, foffset
))
1270 DUMP_WRITE(men
->data
, men
->datasz
, foffset
);
1271 if (!alignfile(file
, foffset
))
1278 #define DUMP_WRITE(addr, nr) \
1279 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1281 #define DUMP_SEEK(off) \
1282 if (!dump_seek(file, (off))) \
1285 static void fill_elf_header(struct elfhdr
*elf
, int segs
,
1286 u16 machine
, u32 flags
, u8 osabi
)
1288 memset(elf
, 0, sizeof(*elf
));
1290 memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
1291 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
1292 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
1293 elf
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1294 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
1296 elf
->e_type
= ET_CORE
;
1297 elf
->e_machine
= machine
;
1298 elf
->e_version
= EV_CURRENT
;
1299 elf
->e_phoff
= sizeof(struct elfhdr
);
1300 elf
->e_flags
= flags
;
1301 elf
->e_ehsize
= sizeof(struct elfhdr
);
1302 elf
->e_phentsize
= sizeof(struct elf_phdr
);
1303 elf
->e_phnum
= segs
;
1308 static void fill_elf_note_phdr(struct elf_phdr
*phdr
, int sz
, loff_t offset
)
1310 phdr
->p_type
= PT_NOTE
;
1311 phdr
->p_offset
= offset
;
1314 phdr
->p_filesz
= sz
;
1321 static void fill_note(struct memelfnote
*note
, const char *name
, int type
,
1322 unsigned int sz
, void *data
)
1332 * fill up all the fields in prstatus from the given task struct, except
1333 * registers which need to be filled up separately.
1335 static void fill_prstatus(struct elf_prstatus
*prstatus
,
1336 struct task_struct
*p
, long signr
)
1338 prstatus
->pr_info
.si_signo
= prstatus
->pr_cursig
= signr
;
1339 prstatus
->pr_sigpend
= p
->pending
.signal
.sig
[0];
1340 prstatus
->pr_sighold
= p
->blocked
.sig
[0];
1341 prstatus
->pr_pid
= task_pid_vnr(p
);
1342 prstatus
->pr_ppid
= task_pid_vnr(p
->real_parent
);
1343 prstatus
->pr_pgrp
= task_pgrp_vnr(p
);
1344 prstatus
->pr_sid
= task_session_vnr(p
);
1345 if (thread_group_leader(p
)) {
1347 * This is the record for the group leader. Add in the
1348 * cumulative times of previous dead threads. This total
1349 * won't include the time of each live thread whose state
1350 * is included in the core dump. The final total reported
1351 * to our parent process when it calls wait4 will include
1352 * those sums as well as the little bit more time it takes
1353 * this and each other thread to finish dying after the
1354 * core dump synchronization phase.
1356 cputime_to_timeval(cputime_add(p
->utime
, p
->signal
->utime
),
1357 &prstatus
->pr_utime
);
1358 cputime_to_timeval(cputime_add(p
->stime
, p
->signal
->stime
),
1359 &prstatus
->pr_stime
);
1361 cputime_to_timeval(p
->utime
, &prstatus
->pr_utime
);
1362 cputime_to_timeval(p
->stime
, &prstatus
->pr_stime
);
1364 cputime_to_timeval(p
->signal
->cutime
, &prstatus
->pr_cutime
);
1365 cputime_to_timeval(p
->signal
->cstime
, &prstatus
->pr_cstime
);
1368 static int fill_psinfo(struct elf_prpsinfo
*psinfo
, struct task_struct
*p
,
1369 struct mm_struct
*mm
)
1371 unsigned int i
, len
;
1373 /* first copy the parameters from user space */
1374 memset(psinfo
, 0, sizeof(struct elf_prpsinfo
));
1376 len
= mm
->arg_end
- mm
->arg_start
;
1377 if (len
>= ELF_PRARGSZ
)
1378 len
= ELF_PRARGSZ
-1;
1379 if (copy_from_user(&psinfo
->pr_psargs
,
1380 (const char __user
*)mm
->arg_start
, len
))
1382 for(i
= 0; i
< len
; i
++)
1383 if (psinfo
->pr_psargs
[i
] == 0)
1384 psinfo
->pr_psargs
[i
] = ' ';
1385 psinfo
->pr_psargs
[len
] = 0;
1387 psinfo
->pr_pid
= task_pid_vnr(p
);
1388 psinfo
->pr_ppid
= task_pid_vnr(p
->real_parent
);
1389 psinfo
->pr_pgrp
= task_pgrp_vnr(p
);
1390 psinfo
->pr_sid
= task_session_vnr(p
);
1392 i
= p
->state
? ffz(~p
->state
) + 1 : 0;
1393 psinfo
->pr_state
= i
;
1394 psinfo
->pr_sname
= (i
> 5) ? '.' : "RSDTZW"[i
];
1395 psinfo
->pr_zomb
= psinfo
->pr_sname
== 'Z';
1396 psinfo
->pr_nice
= task_nice(p
);
1397 psinfo
->pr_flag
= p
->flags
;
1398 SET_UID(psinfo
->pr_uid
, p
->uid
);
1399 SET_GID(psinfo
->pr_gid
, p
->gid
);
1400 strncpy(psinfo
->pr_fname
, p
->comm
, sizeof(psinfo
->pr_fname
));
1405 static void fill_auxv_note(struct memelfnote
*note
, struct mm_struct
*mm
)
1407 elf_addr_t
*auxv
= (elf_addr_t
*) mm
->saved_auxv
;
1411 while (auxv
[i
- 2] != AT_NULL
);
1412 fill_note(note
, "CORE", NT_AUXV
, i
* sizeof(elf_addr_t
), auxv
);
1415 #ifdef CORE_DUMP_USE_REGSET
1416 #include <linux/regset.h>
1418 struct elf_thread_core_info
{
1419 struct elf_thread_core_info
*next
;
1420 struct task_struct
*task
;
1421 struct elf_prstatus prstatus
;
1422 struct memelfnote notes
[0];
1425 struct elf_note_info
{
1426 struct elf_thread_core_info
*thread
;
1427 struct memelfnote psinfo
;
1428 struct memelfnote auxv
;
1434 * When a regset has a writeback hook, we call it on each thread before
1435 * dumping user memory. On register window machines, this makes sure the
1436 * user memory backing the register data is up to date before we read it.
1438 static void do_thread_regset_writeback(struct task_struct
*task
,
1439 const struct user_regset
*regset
)
1441 if (regset
->writeback
)
1442 regset
->writeback(task
, regset
, 1);
1445 static int fill_thread_core_info(struct elf_thread_core_info
*t
,
1446 const struct user_regset_view
*view
,
1447 long signr
, size_t *total
)
1452 * NT_PRSTATUS is the one special case, because the regset data
1453 * goes into the pr_reg field inside the note contents, rather
1454 * than being the whole note contents. We fill the reset in here.
1455 * We assume that regset 0 is NT_PRSTATUS.
1457 fill_prstatus(&t
->prstatus
, t
->task
, signr
);
1458 (void) view
->regsets
[0].get(t
->task
, &view
->regsets
[0],
1459 0, sizeof(t
->prstatus
.pr_reg
),
1460 &t
->prstatus
.pr_reg
, NULL
);
1462 fill_note(&t
->notes
[0], "CORE", NT_PRSTATUS
,
1463 sizeof(t
->prstatus
), &t
->prstatus
);
1464 *total
+= notesize(&t
->notes
[0]);
1466 do_thread_regset_writeback(t
->task
, &view
->regsets
[0]);
1469 * Each other regset might generate a note too. For each regset
1470 * that has no core_note_type or is inactive, we leave t->notes[i]
1471 * all zero and we'll know to skip writing it later.
1473 for (i
= 1; i
< view
->n
; ++i
) {
1474 const struct user_regset
*regset
= &view
->regsets
[i
];
1475 do_thread_regset_writeback(t
->task
, regset
);
1476 if (regset
->core_note_type
&&
1477 (!regset
->active
|| regset
->active(t
->task
, regset
))) {
1479 size_t size
= regset
->n
* regset
->size
;
1480 void *data
= kmalloc(size
, GFP_KERNEL
);
1481 if (unlikely(!data
))
1483 ret
= regset
->get(t
->task
, regset
,
1484 0, size
, data
, NULL
);
1488 if (regset
->core_note_type
!= NT_PRFPREG
)
1489 fill_note(&t
->notes
[i
], "LINUX",
1490 regset
->core_note_type
,
1493 t
->prstatus
.pr_fpvalid
= 1;
1494 fill_note(&t
->notes
[i
], "CORE",
1495 NT_PRFPREG
, size
, data
);
1497 *total
+= notesize(&t
->notes
[i
]);
1505 static int fill_note_info(struct elfhdr
*elf
, int phdrs
,
1506 struct elf_note_info
*info
,
1507 long signr
, struct pt_regs
*regs
)
1509 struct task_struct
*dump_task
= current
;
1510 const struct user_regset_view
*view
= task_user_regset_view(dump_task
);
1511 struct elf_thread_core_info
*t
;
1512 struct elf_prpsinfo
*psinfo
;
1513 struct core_thread
*ct
;
1517 info
->thread
= NULL
;
1519 psinfo
= kmalloc(sizeof(*psinfo
), GFP_KERNEL
);
1520 fill_note(&info
->psinfo
, "CORE", NT_PRPSINFO
, sizeof(*psinfo
), psinfo
);
1526 * Figure out how many notes we're going to need for each thread.
1528 info
->thread_notes
= 0;
1529 for (i
= 0; i
< view
->n
; ++i
)
1530 if (view
->regsets
[i
].core_note_type
!= 0)
1531 ++info
->thread_notes
;
1534 * Sanity check. We rely on regset 0 being in NT_PRSTATUS,
1535 * since it is our one special case.
1537 if (unlikely(info
->thread_notes
== 0) ||
1538 unlikely(view
->regsets
[0].core_note_type
!= NT_PRSTATUS
)) {
1544 * Initialize the ELF file header.
1546 fill_elf_header(elf
, phdrs
,
1547 view
->e_machine
, view
->e_flags
, view
->ei_osabi
);
1550 * Allocate a structure for each thread.
1552 for (ct
= &dump_task
->mm
->core_state
->dumper
; ct
; ct
= ct
->next
) {
1553 t
= kzalloc(offsetof(struct elf_thread_core_info
,
1554 notes
[info
->thread_notes
]),
1560 if (ct
->task
== dump_task
|| !info
->thread
) {
1561 t
->next
= info
->thread
;
1565 * Make sure to keep the original task at
1566 * the head of the list.
1568 t
->next
= info
->thread
->next
;
1569 info
->thread
->next
= t
;
1574 * Now fill in each thread's information.
1576 for (t
= info
->thread
; t
!= NULL
; t
= t
->next
)
1577 if (!fill_thread_core_info(t
, view
, signr
, &info
->size
))
1581 * Fill in the two process-wide notes.
1583 fill_psinfo(psinfo
, dump_task
->group_leader
, dump_task
->mm
);
1584 info
->size
+= notesize(&info
->psinfo
);
1586 fill_auxv_note(&info
->auxv
, current
->mm
);
1587 info
->size
+= notesize(&info
->auxv
);
1592 static size_t get_note_info_size(struct elf_note_info
*info
)
1598 * Write all the notes for each thread. When writing the first thread, the
1599 * process-wide notes are interleaved after the first thread-specific note.
1601 static int write_note_info(struct elf_note_info
*info
,
1602 struct file
*file
, loff_t
*foffset
)
1605 struct elf_thread_core_info
*t
= info
->thread
;
1610 if (!writenote(&t
->notes
[0], file
, foffset
))
1613 if (first
&& !writenote(&info
->psinfo
, file
, foffset
))
1615 if (first
&& !writenote(&info
->auxv
, file
, foffset
))
1618 for (i
= 1; i
< info
->thread_notes
; ++i
)
1619 if (t
->notes
[i
].data
&&
1620 !writenote(&t
->notes
[i
], file
, foffset
))
1630 static void free_note_info(struct elf_note_info
*info
)
1632 struct elf_thread_core_info
*threads
= info
->thread
;
1635 struct elf_thread_core_info
*t
= threads
;
1637 WARN_ON(t
->notes
[0].data
&& t
->notes
[0].data
!= &t
->prstatus
);
1638 for (i
= 1; i
< info
->thread_notes
; ++i
)
1639 kfree(t
->notes
[i
].data
);
1642 kfree(info
->psinfo
.data
);
1647 /* Here is the structure in which status of each thread is captured. */
1648 struct elf_thread_status
1650 struct list_head list
;
1651 struct elf_prstatus prstatus
; /* NT_PRSTATUS */
1652 elf_fpregset_t fpu
; /* NT_PRFPREG */
1653 struct task_struct
*thread
;
1654 #ifdef ELF_CORE_COPY_XFPREGS
1655 elf_fpxregset_t xfpu
; /* ELF_CORE_XFPREG_TYPE */
1657 struct memelfnote notes
[3];
1662 * In order to add the specific thread information for the elf file format,
1663 * we need to keep a linked list of every threads pr_status and then create
1664 * a single section for them in the final core file.
1666 static int elf_dump_thread_status(long signr
, struct elf_thread_status
*t
)
1669 struct task_struct
*p
= t
->thread
;
1672 fill_prstatus(&t
->prstatus
, p
, signr
);
1673 elf_core_copy_task_regs(p
, &t
->prstatus
.pr_reg
);
1675 fill_note(&t
->notes
[0], "CORE", NT_PRSTATUS
, sizeof(t
->prstatus
),
1678 sz
+= notesize(&t
->notes
[0]);
1680 if ((t
->prstatus
.pr_fpvalid
= elf_core_copy_task_fpregs(p
, NULL
,
1682 fill_note(&t
->notes
[1], "CORE", NT_PRFPREG
, sizeof(t
->fpu
),
1685 sz
+= notesize(&t
->notes
[1]);
1688 #ifdef ELF_CORE_COPY_XFPREGS
1689 if (elf_core_copy_task_xfpregs(p
, &t
->xfpu
)) {
1690 fill_note(&t
->notes
[2], "LINUX", ELF_CORE_XFPREG_TYPE
,
1691 sizeof(t
->xfpu
), &t
->xfpu
);
1693 sz
+= notesize(&t
->notes
[2]);
1699 struct elf_note_info
{
1700 struct memelfnote
*notes
;
1701 struct elf_prstatus
*prstatus
; /* NT_PRSTATUS */
1702 struct elf_prpsinfo
*psinfo
; /* NT_PRPSINFO */
1703 struct list_head thread_list
;
1704 elf_fpregset_t
*fpu
;
1705 #ifdef ELF_CORE_COPY_XFPREGS
1706 elf_fpxregset_t
*xfpu
;
1708 int thread_status_size
;
1712 static int fill_note_info(struct elfhdr
*elf
, int phdrs
,
1713 struct elf_note_info
*info
,
1714 long signr
, struct pt_regs
*regs
)
1717 struct list_head
*t
;
1720 info
->prstatus
= NULL
;
1721 info
->psinfo
= NULL
;
1723 #ifdef ELF_CORE_COPY_XFPREGS
1726 INIT_LIST_HEAD(&info
->thread_list
);
1728 info
->notes
= kmalloc(NUM_NOTES
* sizeof(struct memelfnote
),
1732 info
->psinfo
= kmalloc(sizeof(*info
->psinfo
), GFP_KERNEL
);
1735 info
->prstatus
= kmalloc(sizeof(*info
->prstatus
), GFP_KERNEL
);
1736 if (!info
->prstatus
)
1738 info
->fpu
= kmalloc(sizeof(*info
->fpu
), GFP_KERNEL
);
1741 #ifdef ELF_CORE_COPY_XFPREGS
1742 info
->xfpu
= kmalloc(sizeof(*info
->xfpu
), GFP_KERNEL
);
1747 info
->thread_status_size
= 0;
1749 struct core_thread
*ct
;
1750 struct elf_thread_status
*ets
;
1752 for (ct
= current
->mm
->core_state
->dumper
.next
;
1753 ct
; ct
= ct
->next
) {
1754 ets
= kzalloc(sizeof(*ets
), GFP_KERNEL
);
1758 ets
->thread
= ct
->task
;
1759 list_add(&ets
->list
, &info
->thread_list
);
1762 list_for_each(t
, &info
->thread_list
) {
1765 ets
= list_entry(t
, struct elf_thread_status
, list
);
1766 sz
= elf_dump_thread_status(signr
, ets
);
1767 info
->thread_status_size
+= sz
;
1770 /* now collect the dump for the current */
1771 memset(info
->prstatus
, 0, sizeof(*info
->prstatus
));
1772 fill_prstatus(info
->prstatus
, current
, signr
);
1773 elf_core_copy_regs(&info
->prstatus
->pr_reg
, regs
);
1776 fill_elf_header(elf
, phdrs
, ELF_ARCH
, ELF_CORE_EFLAGS
, ELF_OSABI
);
1779 * Set up the notes in similar form to SVR4 core dumps made
1780 * with info from their /proc.
1783 fill_note(info
->notes
+ 0, "CORE", NT_PRSTATUS
,
1784 sizeof(*info
->prstatus
), info
->prstatus
);
1785 fill_psinfo(info
->psinfo
, current
->group_leader
, current
->mm
);
1786 fill_note(info
->notes
+ 1, "CORE", NT_PRPSINFO
,
1787 sizeof(*info
->psinfo
), info
->psinfo
);
1791 fill_auxv_note(&info
->notes
[info
->numnote
++], current
->mm
);
1793 /* Try to dump the FPU. */
1794 info
->prstatus
->pr_fpvalid
= elf_core_copy_task_fpregs(current
, regs
,
1796 if (info
->prstatus
->pr_fpvalid
)
1797 fill_note(info
->notes
+ info
->numnote
++,
1798 "CORE", NT_PRFPREG
, sizeof(*info
->fpu
), info
->fpu
);
1799 #ifdef ELF_CORE_COPY_XFPREGS
1800 if (elf_core_copy_task_xfpregs(current
, info
->xfpu
))
1801 fill_note(info
->notes
+ info
->numnote
++,
1802 "LINUX", ELF_CORE_XFPREG_TYPE
,
1803 sizeof(*info
->xfpu
), info
->xfpu
);
1811 static size_t get_note_info_size(struct elf_note_info
*info
)
1816 for (i
= 0; i
< info
->numnote
; i
++)
1817 sz
+= notesize(info
->notes
+ i
);
1819 sz
+= info
->thread_status_size
;
1824 static int write_note_info(struct elf_note_info
*info
,
1825 struct file
*file
, loff_t
*foffset
)
1828 struct list_head
*t
;
1830 for (i
= 0; i
< info
->numnote
; i
++)
1831 if (!writenote(info
->notes
+ i
, file
, foffset
))
1834 /* write out the thread status notes section */
1835 list_for_each(t
, &info
->thread_list
) {
1836 struct elf_thread_status
*tmp
=
1837 list_entry(t
, struct elf_thread_status
, list
);
1839 for (i
= 0; i
< tmp
->num_notes
; i
++)
1840 if (!writenote(&tmp
->notes
[i
], file
, foffset
))
1847 static void free_note_info(struct elf_note_info
*info
)
1849 while (!list_empty(&info
->thread_list
)) {
1850 struct list_head
*tmp
= info
->thread_list
.next
;
1852 kfree(list_entry(tmp
, struct elf_thread_status
, list
));
1855 kfree(info
->prstatus
);
1856 kfree(info
->psinfo
);
1859 #ifdef ELF_CORE_COPY_XFPREGS
1866 static struct vm_area_struct
*first_vma(struct task_struct
*tsk
,
1867 struct vm_area_struct
*gate_vma
)
1869 struct vm_area_struct
*ret
= tsk
->mm
->mmap
;
1876 * Helper function for iterating across a vma list. It ensures that the caller
1877 * will visit `gate_vma' prior to terminating the search.
1879 static struct vm_area_struct
*next_vma(struct vm_area_struct
*this_vma
,
1880 struct vm_area_struct
*gate_vma
)
1882 struct vm_area_struct
*ret
;
1884 ret
= this_vma
->vm_next
;
1887 if (this_vma
== gate_vma
)
1895 * This is a two-pass process; first we find the offsets of the bits,
1896 * and then they are actually written out. If we run out of core limit
1899 static int elf_core_dump(long signr
, struct pt_regs
*regs
, struct file
*file
, unsigned long limit
)
1905 struct vm_area_struct
*vma
, *gate_vma
;
1906 struct elfhdr
*elf
= NULL
;
1907 loff_t offset
= 0, dataoff
, foffset
;
1908 unsigned long mm_flags
;
1909 struct elf_note_info info
;
1912 * We no longer stop all VM operations.
1914 * This is because those proceses that could possibly change map_count
1915 * or the mmap / vma pages are now blocked in do_exit on current
1916 * finishing this core dump.
1918 * Only ptrace can touch these memory addresses, but it doesn't change
1919 * the map_count or the pages allocated. So no possibility of crashing
1920 * exists while dumping the mm->vm_next areas to the core file.
1923 /* alloc memory for large data structures: too large to be on stack */
1924 elf
= kmalloc(sizeof(*elf
), GFP_KERNEL
);
1928 segs
= current
->mm
->map_count
;
1929 #ifdef ELF_CORE_EXTRA_PHDRS
1930 segs
+= ELF_CORE_EXTRA_PHDRS
;
1933 gate_vma
= get_gate_vma(current
);
1934 if (gate_vma
!= NULL
)
1938 * Collect all the non-memory information about the process for the
1939 * notes. This also sets up the file header.
1941 if (!fill_note_info(elf
, segs
+ 1, /* including notes section */
1942 &info
, signr
, regs
))
1946 current
->flags
|= PF_DUMPCORE
;
1951 DUMP_WRITE(elf
, sizeof(*elf
));
1952 offset
+= sizeof(*elf
); /* Elf header */
1953 offset
+= (segs
+ 1) * sizeof(struct elf_phdr
); /* Program headers */
1956 /* Write notes phdr entry */
1958 struct elf_phdr phdr
;
1959 size_t sz
= get_note_info_size(&info
);
1961 sz
+= elf_coredump_extra_notes_size();
1963 fill_elf_note_phdr(&phdr
, sz
, offset
);
1965 DUMP_WRITE(&phdr
, sizeof(phdr
));
1968 dataoff
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
1971 * We must use the same mm->flags while dumping core to avoid
1972 * inconsistency between the program headers and bodies, otherwise an
1973 * unusable core file can be generated.
1975 mm_flags
= current
->mm
->flags
;
1977 /* Write program headers for segments dump */
1978 for (vma
= first_vma(current
, gate_vma
); vma
!= NULL
;
1979 vma
= next_vma(vma
, gate_vma
)) {
1980 struct elf_phdr phdr
;
1982 phdr
.p_type
= PT_LOAD
;
1983 phdr
.p_offset
= offset
;
1984 phdr
.p_vaddr
= vma
->vm_start
;
1986 phdr
.p_filesz
= vma_dump_size(vma
, mm_flags
);
1987 phdr
.p_memsz
= vma
->vm_end
- vma
->vm_start
;
1988 offset
+= phdr
.p_filesz
;
1989 phdr
.p_flags
= vma
->vm_flags
& VM_READ
? PF_R
: 0;
1990 if (vma
->vm_flags
& VM_WRITE
)
1991 phdr
.p_flags
|= PF_W
;
1992 if (vma
->vm_flags
& VM_EXEC
)
1993 phdr
.p_flags
|= PF_X
;
1994 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
1996 DUMP_WRITE(&phdr
, sizeof(phdr
));
1999 #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
2000 ELF_CORE_WRITE_EXTRA_PHDRS
;
2003 /* write out the notes section */
2004 if (!write_note_info(&info
, file
, &foffset
))
2007 if (elf_coredump_extra_notes_write(file
, &foffset
))
2011 DUMP_SEEK(dataoff
- foffset
);
2013 for (vma
= first_vma(current
, gate_vma
); vma
!= NULL
;
2014 vma
= next_vma(vma
, gate_vma
)) {
2018 end
= vma
->vm_start
+ vma_dump_size(vma
, mm_flags
);
2020 for (addr
= vma
->vm_start
; addr
< end
; addr
+= PAGE_SIZE
) {
2022 struct vm_area_struct
*tmp_vma
;
2024 if (get_user_pages(current
, current
->mm
, addr
, 1, 0, 1,
2025 &page
, &tmp_vma
) <= 0) {
2026 DUMP_SEEK(PAGE_SIZE
);
2028 if (page
== ZERO_PAGE(0)) {
2029 if (!dump_seek(file
, PAGE_SIZE
)) {
2030 page_cache_release(page
);
2035 flush_cache_page(tmp_vma
, addr
,
2038 if ((size
+= PAGE_SIZE
) > limit
||
2039 !dump_write(file
, kaddr
,
2042 page_cache_release(page
);
2047 page_cache_release(page
);
2052 #ifdef ELF_CORE_WRITE_EXTRA_DATA
2053 ELF_CORE_WRITE_EXTRA_DATA
;
2060 free_note_info(&info
);
2066 #endif /* USE_ELF_CORE_DUMP */
2068 static int __init
init_elf_binfmt(void)
2070 return register_binfmt(&elf_format
);
2073 static void __exit
exit_elf_binfmt(void)
2075 /* Remove the COFF and ELF loaders. */
2076 unregister_binfmt(&elf_format
);
2079 core_initcall(init_elf_binfmt
);
2080 module_exit(exit_elf_binfmt
);
2081 MODULE_LICENSE("GPL");