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>
16 #include <linux/mman.h>
17 #include <linux/errno.h>
18 #include <linux/signal.h>
19 #include <linux/binfmts.h>
20 #include <linux/string.h>
21 #include <linux/file.h>
22 #include <linux/slab.h>
23 #include <linux/personality.h>
24 #include <linux/elfcore.h>
25 #include <linux/init.h>
26 #include <linux/highuid.h>
27 #include <linux/compiler.h>
28 #include <linux/highmem.h>
29 #include <linux/pagemap.h>
30 #include <linux/security.h>
31 #include <linux/random.h>
32 #include <linux/elf.h>
33 #include <linux/utsname.h>
34 #include <linux/coredump.h>
35 #include <asm/uaccess.h>
36 #include <asm/param.h>
39 static int load_elf_binary(struct linux_binprm
*bprm
, struct pt_regs
*regs
);
40 static int load_elf_library(struct file
*);
41 static unsigned long elf_map(struct file
*, unsigned long, struct elf_phdr
*,
42 int, int, unsigned long);
45 * If we don't support core dumping, then supply a NULL so we
48 #ifdef CONFIG_ELF_CORE
49 static int elf_core_dump(struct coredump_params
*cprm
);
51 #define elf_core_dump NULL
54 #if ELF_EXEC_PAGESIZE > PAGE_SIZE
55 #define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
57 #define ELF_MIN_ALIGN PAGE_SIZE
60 #ifndef ELF_CORE_EFLAGS
61 #define ELF_CORE_EFLAGS 0
64 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
65 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
66 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
68 static struct linux_binfmt elf_format
= {
69 .module
= THIS_MODULE
,
70 .load_binary
= load_elf_binary
,
71 .load_shlib
= load_elf_library
,
72 .core_dump
= elf_core_dump
,
73 .min_coredump
= ELF_EXEC_PAGESIZE
,
77 #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
79 static int set_brk(unsigned long start
, unsigned long end
)
81 start
= ELF_PAGEALIGN(start
);
82 end
= ELF_PAGEALIGN(end
);
85 down_write(¤t
->mm
->mmap_sem
);
86 addr
= do_brk(start
, end
- start
);
87 up_write(¤t
->mm
->mmap_sem
);
91 current
->mm
->start_brk
= current
->mm
->brk
= end
;
95 /* We need to explicitly zero any fractional pages
96 after the data section (i.e. bss). This would
97 contain the junk from the file that should not
100 static int padzero(unsigned long elf_bss
)
104 nbyte
= ELF_PAGEOFFSET(elf_bss
);
106 nbyte
= ELF_MIN_ALIGN
- nbyte
;
107 if (clear_user((void __user
*) elf_bss
, nbyte
))
113 /* Let's use some macros to make this stack manipulation a little clearer */
114 #ifdef CONFIG_STACK_GROWSUP
115 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
116 #define STACK_ROUND(sp, items) \
117 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
118 #define STACK_ALLOC(sp, len) ({ \
119 elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
122 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
123 #define STACK_ROUND(sp, items) \
124 (((unsigned long) (sp - items)) &~ 15UL)
125 #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
128 #ifndef ELF_BASE_PLATFORM
130 * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture.
131 * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value
132 * will be copied to the user stack in the same manner as AT_PLATFORM.
134 #define ELF_BASE_PLATFORM NULL
138 create_elf_tables(struct linux_binprm
*bprm
, struct elfhdr
*exec
,
139 unsigned long load_addr
, unsigned long interp_load_addr
)
141 unsigned long p
= bprm
->p
;
142 int argc
= bprm
->argc
;
143 int envc
= bprm
->envc
;
144 elf_addr_t __user
*argv
;
145 elf_addr_t __user
*envp
;
146 elf_addr_t __user
*sp
;
147 elf_addr_t __user
*u_platform
;
148 elf_addr_t __user
*u_base_platform
;
149 elf_addr_t __user
*u_rand_bytes
;
150 const char *k_platform
= ELF_PLATFORM
;
151 const char *k_base_platform
= ELF_BASE_PLATFORM
;
152 unsigned char k_rand_bytes
[16];
154 elf_addr_t
*elf_info
;
156 const struct cred
*cred
= current_cred();
157 struct vm_area_struct
*vma
;
160 * In some cases (e.g. Hyper-Threading), we want to avoid L1
161 * evictions by the processes running on the same package. One
162 * thing we can do is to shuffle the initial stack for them.
165 p
= arch_align_stack(p
);
168 * If this architecture has a platform capability string, copy it
169 * to userspace. In some cases (Sparc), this info is impossible
170 * for userspace to get any other way, in others (i386) it is
175 size_t len
= strlen(k_platform
) + 1;
177 u_platform
= (elf_addr_t __user
*)STACK_ALLOC(p
, len
);
178 if (__copy_to_user(u_platform
, k_platform
, len
))
183 * If this architecture has a "base" platform capability
184 * string, copy it to userspace.
186 u_base_platform
= NULL
;
187 if (k_base_platform
) {
188 size_t len
= strlen(k_base_platform
) + 1;
190 u_base_platform
= (elf_addr_t __user
*)STACK_ALLOC(p
, len
);
191 if (__copy_to_user(u_base_platform
, k_base_platform
, len
))
196 * Generate 16 random bytes for userspace PRNG seeding.
198 get_random_bytes(k_rand_bytes
, sizeof(k_rand_bytes
));
199 u_rand_bytes
= (elf_addr_t __user
*)
200 STACK_ALLOC(p
, sizeof(k_rand_bytes
));
201 if (__copy_to_user(u_rand_bytes
, k_rand_bytes
, sizeof(k_rand_bytes
)))
204 /* Create the ELF interpreter info */
205 elf_info
= (elf_addr_t
*)current
->mm
->saved_auxv
;
206 /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
207 #define NEW_AUX_ENT(id, val) \
209 elf_info[ei_index++] = id; \
210 elf_info[ei_index++] = val; \
215 * ARCH_DLINFO must come first so PPC can do its special alignment of
217 * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
218 * ARCH_DLINFO changes
222 NEW_AUX_ENT(AT_HWCAP
, ELF_HWCAP
);
223 NEW_AUX_ENT(AT_PAGESZ
, ELF_EXEC_PAGESIZE
);
224 NEW_AUX_ENT(AT_CLKTCK
, CLOCKS_PER_SEC
);
225 NEW_AUX_ENT(AT_PHDR
, load_addr
+ exec
->e_phoff
);
226 NEW_AUX_ENT(AT_PHENT
, sizeof(struct elf_phdr
));
227 NEW_AUX_ENT(AT_PHNUM
, exec
->e_phnum
);
228 NEW_AUX_ENT(AT_BASE
, interp_load_addr
);
229 NEW_AUX_ENT(AT_FLAGS
, 0);
230 NEW_AUX_ENT(AT_ENTRY
, exec
->e_entry
);
231 NEW_AUX_ENT(AT_UID
, cred
->uid
);
232 NEW_AUX_ENT(AT_EUID
, cred
->euid
);
233 NEW_AUX_ENT(AT_GID
, cred
->gid
);
234 NEW_AUX_ENT(AT_EGID
, cred
->egid
);
235 NEW_AUX_ENT(AT_SECURE
, security_bprm_secureexec(bprm
));
236 NEW_AUX_ENT(AT_RANDOM
, (elf_addr_t
)(unsigned long)u_rand_bytes
);
237 NEW_AUX_ENT(AT_EXECFN
, bprm
->exec
);
239 NEW_AUX_ENT(AT_PLATFORM
,
240 (elf_addr_t
)(unsigned long)u_platform
);
242 if (k_base_platform
) {
243 NEW_AUX_ENT(AT_BASE_PLATFORM
,
244 (elf_addr_t
)(unsigned long)u_base_platform
);
246 if (bprm
->interp_flags
& BINPRM_FLAGS_EXECFD
) {
247 NEW_AUX_ENT(AT_EXECFD
, bprm
->interp_data
);
250 /* AT_NULL is zero; clear the rest too */
251 memset(&elf_info
[ei_index
], 0,
252 sizeof current
->mm
->saved_auxv
- ei_index
* sizeof elf_info
[0]);
254 /* And advance past the AT_NULL entry. */
257 sp
= STACK_ADD(p
, ei_index
);
259 items
= (argc
+ 1) + (envc
+ 1) + 1;
260 bprm
->p
= STACK_ROUND(sp
, items
);
262 /* Point sp at the lowest address on the stack */
263 #ifdef CONFIG_STACK_GROWSUP
264 sp
= (elf_addr_t __user
*)bprm
->p
- items
- ei_index
;
265 bprm
->exec
= (unsigned long)sp
; /* XXX: PARISC HACK */
267 sp
= (elf_addr_t __user
*)bprm
->p
;
272 * Grow the stack manually; some architectures have a limit on how
273 * far ahead a user-space access may be in order to grow the stack.
275 vma
= find_extend_vma(current
->mm
, bprm
->p
);
279 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
280 if (__put_user(argc
, sp
++))
283 envp
= argv
+ argc
+ 1;
285 /* Populate argv and envp */
286 p
= current
->mm
->arg_end
= current
->mm
->arg_start
;
289 if (__put_user((elf_addr_t
)p
, argv
++))
291 len
= strnlen_user((void __user
*)p
, MAX_ARG_STRLEN
);
292 if (!len
|| len
> MAX_ARG_STRLEN
)
296 if (__put_user(0, argv
))
298 current
->mm
->arg_end
= current
->mm
->env_start
= p
;
301 if (__put_user((elf_addr_t
)p
, envp
++))
303 len
= strnlen_user((void __user
*)p
, MAX_ARG_STRLEN
);
304 if (!len
|| len
> MAX_ARG_STRLEN
)
308 if (__put_user(0, envp
))
310 current
->mm
->env_end
= p
;
312 /* Put the elf_info on the stack in the right place. */
313 sp
= (elf_addr_t __user
*)envp
+ 1;
314 if (copy_to_user(sp
, elf_info
, ei_index
* sizeof(elf_addr_t
)))
321 static unsigned long elf_map(struct file
*filep
, unsigned long addr
,
322 struct elf_phdr
*eppnt
, int prot
, int type
,
323 unsigned long total_size
)
325 unsigned long map_addr
;
326 unsigned long size
= eppnt
->p_filesz
+ ELF_PAGEOFFSET(eppnt
->p_vaddr
);
327 unsigned long off
= eppnt
->p_offset
- ELF_PAGEOFFSET(eppnt
->p_vaddr
);
328 addr
= ELF_PAGESTART(addr
);
329 size
= ELF_PAGEALIGN(size
);
331 /* mmap() will return -EINVAL if given a zero size, but a
332 * segment with zero filesize is perfectly valid */
336 down_write(¤t
->mm
->mmap_sem
);
338 * total_size is the size of the ELF (interpreter) image.
339 * The _first_ mmap needs to know the full size, otherwise
340 * randomization might put this image into an overlapping
341 * position with the ELF binary image. (since size < total_size)
342 * So we first map the 'big' image - and unmap the remainder at
343 * the end. (which unmap is needed for ELF images with holes.)
346 total_size
= ELF_PAGEALIGN(total_size
);
347 map_addr
= do_mmap(filep
, addr
, total_size
, prot
, type
, off
);
348 if (!BAD_ADDR(map_addr
))
349 do_munmap(current
->mm
, map_addr
+size
, total_size
-size
);
351 map_addr
= do_mmap(filep
, addr
, size
, prot
, type
, off
);
353 up_write(¤t
->mm
->mmap_sem
);
357 #endif /* !elf_map */
359 static unsigned long total_mapping_size(struct elf_phdr
*cmds
, int nr
)
361 int i
, first_idx
= -1, last_idx
= -1;
363 for (i
= 0; i
< nr
; i
++) {
364 if (cmds
[i
].p_type
== PT_LOAD
) {
373 return cmds
[last_idx
].p_vaddr
+ cmds
[last_idx
].p_memsz
-
374 ELF_PAGESTART(cmds
[first_idx
].p_vaddr
);
378 /* This is much more generalized than the library routine read function,
379 so we keep this separate. Technically the library read function
380 is only provided so that we can read a.out libraries that have
383 static unsigned long load_elf_interp(struct elfhdr
*interp_elf_ex
,
384 struct file
*interpreter
, unsigned long *interp_map_addr
,
385 unsigned long no_base
)
387 struct elf_phdr
*elf_phdata
;
388 struct elf_phdr
*eppnt
;
389 unsigned long load_addr
= 0;
390 int load_addr_set
= 0;
391 unsigned long last_bss
= 0, elf_bss
= 0;
392 unsigned long error
= ~0UL;
393 unsigned long total_size
;
396 /* First of all, some simple consistency checks */
397 if (interp_elf_ex
->e_type
!= ET_EXEC
&&
398 interp_elf_ex
->e_type
!= ET_DYN
)
400 if (!elf_check_arch(interp_elf_ex
))
402 if (!interpreter
->f_op
|| !interpreter
->f_op
->mmap
)
406 * If the size of this structure has changed, then punt, since
407 * we will be doing the wrong thing.
409 if (interp_elf_ex
->e_phentsize
!= sizeof(struct elf_phdr
))
411 if (interp_elf_ex
->e_phnum
< 1 ||
412 interp_elf_ex
->e_phnum
> 65536U / sizeof(struct elf_phdr
))
415 /* Now read in all of the header information */
416 size
= sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
;
417 if (size
> ELF_MIN_ALIGN
)
419 elf_phdata
= kmalloc(size
, GFP_KERNEL
);
423 retval
= kernel_read(interpreter
, interp_elf_ex
->e_phoff
,
424 (char *)elf_phdata
,size
);
426 if (retval
!= size
) {
432 total_size
= total_mapping_size(elf_phdata
, interp_elf_ex
->e_phnum
);
439 for (i
= 0; i
< interp_elf_ex
->e_phnum
; i
++, eppnt
++) {
440 if (eppnt
->p_type
== PT_LOAD
) {
441 int elf_type
= MAP_PRIVATE
| MAP_DENYWRITE
;
443 unsigned long vaddr
= 0;
444 unsigned long k
, map_addr
;
446 if (eppnt
->p_flags
& PF_R
)
447 elf_prot
= PROT_READ
;
448 if (eppnt
->p_flags
& PF_W
)
449 elf_prot
|= PROT_WRITE
;
450 if (eppnt
->p_flags
& PF_X
)
451 elf_prot
|= PROT_EXEC
;
452 vaddr
= eppnt
->p_vaddr
;
453 if (interp_elf_ex
->e_type
== ET_EXEC
|| load_addr_set
)
454 elf_type
|= MAP_FIXED
;
455 else if (no_base
&& interp_elf_ex
->e_type
== ET_DYN
)
458 map_addr
= elf_map(interpreter
, load_addr
+ vaddr
,
459 eppnt
, elf_prot
, elf_type
, total_size
);
461 if (!*interp_map_addr
)
462 *interp_map_addr
= map_addr
;
464 if (BAD_ADDR(map_addr
))
467 if (!load_addr_set
&&
468 interp_elf_ex
->e_type
== ET_DYN
) {
469 load_addr
= map_addr
- ELF_PAGESTART(vaddr
);
474 * Check to see if the section's size will overflow the
475 * allowed task size. Note that p_filesz must always be
476 * <= p_memsize so it's only necessary to check p_memsz.
478 k
= load_addr
+ eppnt
->p_vaddr
;
480 eppnt
->p_filesz
> eppnt
->p_memsz
||
481 eppnt
->p_memsz
> TASK_SIZE
||
482 TASK_SIZE
- eppnt
->p_memsz
< k
) {
488 * Find the end of the file mapping for this phdr, and
489 * keep track of the largest address we see for this.
491 k
= load_addr
+ eppnt
->p_vaddr
+ eppnt
->p_filesz
;
496 * Do the same thing for the memory mapping - between
497 * elf_bss and last_bss is the bss section.
499 k
= load_addr
+ eppnt
->p_memsz
+ eppnt
->p_vaddr
;
505 if (last_bss
> elf_bss
) {
507 * Now fill out the bss section. First pad the last page up
508 * to the page boundary, and then perform a mmap to make sure
509 * that there are zero-mapped pages up to and including the
512 if (padzero(elf_bss
)) {
517 /* What we have mapped so far */
518 elf_bss
= ELF_PAGESTART(elf_bss
+ ELF_MIN_ALIGN
- 1);
520 /* Map the last of the bss segment */
521 down_write(¤t
->mm
->mmap_sem
);
522 error
= do_brk(elf_bss
, last_bss
- elf_bss
);
523 up_write(¤t
->mm
->mmap_sem
);
537 * These are the functions used to load ELF style executables and shared
538 * libraries. There is no binary dependent code anywhere else.
541 #define INTERPRETER_NONE 0
542 #define INTERPRETER_ELF 2
544 #ifndef STACK_RND_MASK
545 #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
548 static unsigned long randomize_stack_top(unsigned long stack_top
)
550 unsigned int random_variable
= 0;
552 if ((current
->flags
& PF_RANDOMIZE
) &&
553 !(current
->personality
& ADDR_NO_RANDOMIZE
)) {
554 random_variable
= get_random_int() & STACK_RND_MASK
;
555 random_variable
<<= PAGE_SHIFT
;
557 #ifdef CONFIG_STACK_GROWSUP
558 return PAGE_ALIGN(stack_top
) + random_variable
;
560 return PAGE_ALIGN(stack_top
) - random_variable
;
564 static int load_elf_binary(struct linux_binprm
*bprm
, struct pt_regs
*regs
)
566 struct file
*interpreter
= NULL
; /* to shut gcc up */
567 unsigned long load_addr
= 0, load_bias
= 0;
568 int load_addr_set
= 0;
569 char * elf_interpreter
= NULL
;
571 struct elf_phdr
*elf_ppnt
, *elf_phdata
;
572 unsigned long elf_bss
, elf_brk
;
575 unsigned long elf_entry
;
576 unsigned long interp_load_addr
= 0;
577 unsigned long start_code
, end_code
, start_data
, end_data
;
578 unsigned long reloc_func_desc
= 0;
579 int executable_stack
= EXSTACK_DEFAULT
;
580 unsigned long def_flags
= 0;
582 struct elfhdr elf_ex
;
583 struct elfhdr interp_elf_ex
;
586 loc
= kmalloc(sizeof(*loc
), GFP_KERNEL
);
592 /* Get the exec-header */
593 loc
->elf_ex
= *((struct elfhdr
*)bprm
->buf
);
596 /* First of all, some simple consistency checks */
597 if (memcmp(loc
->elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
600 if (loc
->elf_ex
.e_type
!= ET_EXEC
&& loc
->elf_ex
.e_type
!= ET_DYN
)
602 if (!elf_check_arch(&loc
->elf_ex
))
604 if (!bprm
->file
->f_op
||!bprm
->file
->f_op
->mmap
)
607 /* Now read in all of the header information */
608 if (loc
->elf_ex
.e_phentsize
!= sizeof(struct elf_phdr
))
610 if (loc
->elf_ex
.e_phnum
< 1 ||
611 loc
->elf_ex
.e_phnum
> 65536U / sizeof(struct elf_phdr
))
613 size
= loc
->elf_ex
.e_phnum
* sizeof(struct elf_phdr
);
615 elf_phdata
= kmalloc(size
, GFP_KERNEL
);
619 retval
= kernel_read(bprm
->file
, loc
->elf_ex
.e_phoff
,
620 (char *)elf_phdata
, size
);
621 if (retval
!= size
) {
627 elf_ppnt
= elf_phdata
;
636 for (i
= 0; i
< loc
->elf_ex
.e_phnum
; i
++) {
637 if (elf_ppnt
->p_type
== PT_INTERP
) {
638 /* This is the program interpreter used for
639 * shared libraries - for now assume that this
640 * is an a.out format binary
643 if (elf_ppnt
->p_filesz
> PATH_MAX
||
644 elf_ppnt
->p_filesz
< 2)
648 elf_interpreter
= kmalloc(elf_ppnt
->p_filesz
,
650 if (!elf_interpreter
)
653 retval
= kernel_read(bprm
->file
, elf_ppnt
->p_offset
,
656 if (retval
!= elf_ppnt
->p_filesz
) {
659 goto out_free_interp
;
661 /* make sure path is NULL terminated */
663 if (elf_interpreter
[elf_ppnt
->p_filesz
- 1] != '\0')
664 goto out_free_interp
;
666 interpreter
= open_exec(elf_interpreter
);
667 retval
= PTR_ERR(interpreter
);
668 if (IS_ERR(interpreter
))
669 goto out_free_interp
;
672 * If the binary is not readable then enforce
673 * mm->dumpable = 0 regardless of the interpreter's
676 if (file_permission(interpreter
, MAY_READ
) < 0)
677 bprm
->interp_flags
|= BINPRM_FLAGS_ENFORCE_NONDUMP
;
679 retval
= kernel_read(interpreter
, 0, bprm
->buf
,
681 if (retval
!= BINPRM_BUF_SIZE
) {
684 goto out_free_dentry
;
687 /* Get the exec headers */
688 loc
->interp_elf_ex
= *((struct elfhdr
*)bprm
->buf
);
694 elf_ppnt
= elf_phdata
;
695 for (i
= 0; i
< loc
->elf_ex
.e_phnum
; i
++, elf_ppnt
++)
696 if (elf_ppnt
->p_type
== PT_GNU_STACK
) {
697 if (elf_ppnt
->p_flags
& PF_X
)
698 executable_stack
= EXSTACK_ENABLE_X
;
700 executable_stack
= EXSTACK_DISABLE_X
;
704 /* Some simple consistency checks for the interpreter */
705 if (elf_interpreter
) {
707 /* Not an ELF interpreter */
708 if (memcmp(loc
->interp_elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
709 goto out_free_dentry
;
710 /* Verify the interpreter has a valid arch */
711 if (!elf_check_arch(&loc
->interp_elf_ex
))
712 goto out_free_dentry
;
715 /* Flush all traces of the currently running executable */
716 retval
= flush_old_exec(bprm
);
718 goto out_free_dentry
;
720 /* OK, This is the point of no return */
721 current
->flags
&= ~PF_FORKNOEXEC
;
722 current
->mm
->def_flags
= def_flags
;
724 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
725 may depend on the personality. */
726 SET_PERSONALITY(loc
->elf_ex
);
727 if (elf_read_implies_exec(loc
->elf_ex
, executable_stack
))
728 current
->personality
|= READ_IMPLIES_EXEC
;
730 if (!(current
->personality
& ADDR_NO_RANDOMIZE
) && randomize_va_space
)
731 current
->flags
|= PF_RANDOMIZE
;
733 setup_new_exec(bprm
);
735 /* Do this so that we can load the interpreter, if need be. We will
736 change some of these later */
737 current
->mm
->free_area_cache
= current
->mm
->mmap_base
;
738 current
->mm
->cached_hole_size
= 0;
739 retval
= setup_arg_pages(bprm
, randomize_stack_top(STACK_TOP
),
742 send_sig(SIGKILL
, current
, 0);
743 goto out_free_dentry
;
746 current
->mm
->start_stack
= bprm
->p
;
748 /* Now we do a little grungy work by mmapping the ELF image into
749 the correct location in memory. */
750 for(i
= 0, elf_ppnt
= elf_phdata
;
751 i
< loc
->elf_ex
.e_phnum
; i
++, elf_ppnt
++) {
752 int elf_prot
= 0, elf_flags
;
753 unsigned long k
, vaddr
;
755 if (elf_ppnt
->p_type
!= PT_LOAD
)
758 if (unlikely (elf_brk
> elf_bss
)) {
761 /* There was a PT_LOAD segment with p_memsz > p_filesz
762 before this one. Map anonymous pages, if needed,
763 and clear the area. */
764 retval
= set_brk (elf_bss
+ load_bias
,
765 elf_brk
+ load_bias
);
767 send_sig(SIGKILL
, current
, 0);
768 goto out_free_dentry
;
770 nbyte
= ELF_PAGEOFFSET(elf_bss
);
772 nbyte
= ELF_MIN_ALIGN
- nbyte
;
773 if (nbyte
> elf_brk
- elf_bss
)
774 nbyte
= elf_brk
- elf_bss
;
775 if (clear_user((void __user
*)elf_bss
+
778 * This bss-zeroing can fail if the ELF
779 * file specifies odd protections. So
780 * we don't check the return value
786 if (elf_ppnt
->p_flags
& PF_R
)
787 elf_prot
|= PROT_READ
;
788 if (elf_ppnt
->p_flags
& PF_W
)
789 elf_prot
|= PROT_WRITE
;
790 if (elf_ppnt
->p_flags
& PF_X
)
791 elf_prot
|= PROT_EXEC
;
793 elf_flags
= MAP_PRIVATE
| MAP_DENYWRITE
| MAP_EXECUTABLE
;
795 vaddr
= elf_ppnt
->p_vaddr
;
796 if (loc
->elf_ex
.e_type
== ET_EXEC
|| load_addr_set
) {
797 elf_flags
|= MAP_FIXED
;
798 } else if (loc
->elf_ex
.e_type
== ET_DYN
) {
799 /* Try and get dynamic programs out of the way of the
800 * default mmap base, as well as whatever program they
801 * might try to exec. This is because the brk will
802 * follow the loader, and is not movable. */
803 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
806 load_bias
= ELF_PAGESTART(ELF_ET_DYN_BASE
- vaddr
);
810 error
= elf_map(bprm
->file
, load_bias
+ vaddr
, elf_ppnt
,
811 elf_prot
, elf_flags
, 0);
812 if (BAD_ADDR(error
)) {
813 send_sig(SIGKILL
, current
, 0);
814 retval
= IS_ERR((void *)error
) ?
815 PTR_ERR((void*)error
) : -EINVAL
;
816 goto out_free_dentry
;
819 if (!load_addr_set
) {
821 load_addr
= (elf_ppnt
->p_vaddr
- elf_ppnt
->p_offset
);
822 if (loc
->elf_ex
.e_type
== ET_DYN
) {
824 ELF_PAGESTART(load_bias
+ vaddr
);
825 load_addr
+= load_bias
;
826 reloc_func_desc
= load_bias
;
829 k
= elf_ppnt
->p_vaddr
;
836 * Check to see if the section's size will overflow the
837 * allowed task size. Note that p_filesz must always be
838 * <= p_memsz so it is only necessary to check p_memsz.
840 if (BAD_ADDR(k
) || elf_ppnt
->p_filesz
> elf_ppnt
->p_memsz
||
841 elf_ppnt
->p_memsz
> TASK_SIZE
||
842 TASK_SIZE
- elf_ppnt
->p_memsz
< k
) {
843 /* set_brk can never work. Avoid overflows. */
844 send_sig(SIGKILL
, current
, 0);
846 goto out_free_dentry
;
849 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_filesz
;
853 if ((elf_ppnt
->p_flags
& PF_X
) && end_code
< k
)
857 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_memsz
;
862 loc
->elf_ex
.e_entry
+= load_bias
;
863 elf_bss
+= load_bias
;
864 elf_brk
+= load_bias
;
865 start_code
+= load_bias
;
866 end_code
+= load_bias
;
867 start_data
+= load_bias
;
868 end_data
+= load_bias
;
870 /* Calling set_brk effectively mmaps the pages that we need
871 * for the bss and break sections. We must do this before
872 * mapping in the interpreter, to make sure it doesn't wind
873 * up getting placed where the bss needs to go.
875 retval
= set_brk(elf_bss
, elf_brk
);
877 send_sig(SIGKILL
, current
, 0);
878 goto out_free_dentry
;
880 if (likely(elf_bss
!= elf_brk
) && unlikely(padzero(elf_bss
))) {
881 send_sig(SIGSEGV
, current
, 0);
882 retval
= -EFAULT
; /* Nobody gets to see this, but.. */
883 goto out_free_dentry
;
886 if (elf_interpreter
) {
887 unsigned long uninitialized_var(interp_map_addr
);
889 elf_entry
= load_elf_interp(&loc
->interp_elf_ex
,
893 if (!IS_ERR((void *)elf_entry
)) {
895 * load_elf_interp() returns relocation
898 interp_load_addr
= elf_entry
;
899 elf_entry
+= loc
->interp_elf_ex
.e_entry
;
901 if (BAD_ADDR(elf_entry
)) {
902 force_sig(SIGSEGV
, current
);
903 retval
= IS_ERR((void *)elf_entry
) ?
904 (int)elf_entry
: -EINVAL
;
905 goto out_free_dentry
;
907 reloc_func_desc
= interp_load_addr
;
909 allow_write_access(interpreter
);
911 kfree(elf_interpreter
);
913 elf_entry
= loc
->elf_ex
.e_entry
;
914 if (BAD_ADDR(elf_entry
)) {
915 force_sig(SIGSEGV
, current
);
917 goto out_free_dentry
;
923 set_binfmt(&elf_format
);
925 #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
926 retval
= arch_setup_additional_pages(bprm
, !!elf_interpreter
);
928 send_sig(SIGKILL
, current
, 0);
931 #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
933 install_exec_creds(bprm
);
934 current
->flags
&= ~PF_FORKNOEXEC
;
935 retval
= create_elf_tables(bprm
, &loc
->elf_ex
,
936 load_addr
, interp_load_addr
);
938 send_sig(SIGKILL
, current
, 0);
941 /* N.B. passed_fileno might not be initialized? */
942 current
->mm
->end_code
= end_code
;
943 current
->mm
->start_code
= start_code
;
944 current
->mm
->start_data
= start_data
;
945 current
->mm
->end_data
= end_data
;
946 current
->mm
->start_stack
= bprm
->p
;
948 #ifdef arch_randomize_brk
949 if ((current
->flags
& PF_RANDOMIZE
) && (randomize_va_space
> 1))
950 current
->mm
->brk
= current
->mm
->start_brk
=
951 arch_randomize_brk(current
->mm
);
954 if (current
->personality
& MMAP_PAGE_ZERO
) {
955 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
956 and some applications "depend" upon this behavior.
957 Since we do not have the power to recompile these, we
958 emulate the SVr4 behavior. Sigh. */
959 down_write(¤t
->mm
->mmap_sem
);
960 error
= do_mmap(NULL
, 0, PAGE_SIZE
, PROT_READ
| PROT_EXEC
,
961 MAP_FIXED
| MAP_PRIVATE
, 0);
962 up_write(¤t
->mm
->mmap_sem
);
967 * The ABI may specify that certain registers be set up in special
968 * ways (on i386 %edx is the address of a DT_FINI function, for
969 * example. In addition, it may also specify (eg, PowerPC64 ELF)
970 * that the e_entry field is the address of the function descriptor
971 * for the startup routine, rather than the address of the startup
972 * routine itself. This macro performs whatever initialization to
973 * the regs structure is required as well as any relocations to the
974 * function descriptor entries when executing dynamically links apps.
976 ELF_PLAT_INIT(regs
, reloc_func_desc
);
979 start_thread(regs
, elf_entry
, bprm
->p
);
988 allow_write_access(interpreter
);
992 kfree(elf_interpreter
);
998 /* This is really simpleminded and specialized - we are loading an
999 a.out library that is given an ELF header. */
1000 static int load_elf_library(struct file
*file
)
1002 struct elf_phdr
*elf_phdata
;
1003 struct elf_phdr
*eppnt
;
1004 unsigned long elf_bss
, bss
, len
;
1005 int retval
, error
, i
, j
;
1006 struct elfhdr elf_ex
;
1009 retval
= kernel_read(file
, 0, (char *)&elf_ex
, sizeof(elf_ex
));
1010 if (retval
!= sizeof(elf_ex
))
1013 if (memcmp(elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
1016 /* First of all, some simple consistency checks */
1017 if (elf_ex
.e_type
!= ET_EXEC
|| elf_ex
.e_phnum
> 2 ||
1018 !elf_check_arch(&elf_ex
) || !file
->f_op
|| !file
->f_op
->mmap
)
1021 /* Now read in all of the header information */
1023 j
= sizeof(struct elf_phdr
) * elf_ex
.e_phnum
;
1024 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1027 elf_phdata
= kmalloc(j
, GFP_KERNEL
);
1033 retval
= kernel_read(file
, elf_ex
.e_phoff
, (char *)eppnt
, j
);
1037 for (j
= 0, i
= 0; i
<elf_ex
.e_phnum
; i
++)
1038 if ((eppnt
+ i
)->p_type
== PT_LOAD
)
1043 while (eppnt
->p_type
!= PT_LOAD
)
1046 /* Now use mmap to map the library into memory. */
1047 down_write(¤t
->mm
->mmap_sem
);
1048 error
= do_mmap(file
,
1049 ELF_PAGESTART(eppnt
->p_vaddr
),
1051 ELF_PAGEOFFSET(eppnt
->p_vaddr
)),
1052 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
1053 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
,
1055 ELF_PAGEOFFSET(eppnt
->p_vaddr
)));
1056 up_write(¤t
->mm
->mmap_sem
);
1057 if (error
!= ELF_PAGESTART(eppnt
->p_vaddr
))
1060 elf_bss
= eppnt
->p_vaddr
+ eppnt
->p_filesz
;
1061 if (padzero(elf_bss
)) {
1066 len
= ELF_PAGESTART(eppnt
->p_filesz
+ eppnt
->p_vaddr
+
1068 bss
= eppnt
->p_memsz
+ eppnt
->p_vaddr
;
1070 down_write(¤t
->mm
->mmap_sem
);
1071 do_brk(len
, bss
- len
);
1072 up_write(¤t
->mm
->mmap_sem
);
1082 #ifdef CONFIG_ELF_CORE
1086 * Modelled on fs/exec.c:aout_core_dump()
1087 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1091 * Decide what to dump of a segment, part, all or none.
1093 static unsigned long vma_dump_size(struct vm_area_struct
*vma
,
1094 unsigned long mm_flags
)
1096 #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1098 /* The vma can be set up to tell us the answer directly. */
1099 if (vma
->vm_flags
& VM_ALWAYSDUMP
)
1102 /* Hugetlb memory check */
1103 if (vma
->vm_flags
& VM_HUGETLB
) {
1104 if ((vma
->vm_flags
& VM_SHARED
) && FILTER(HUGETLB_SHARED
))
1106 if (!(vma
->vm_flags
& VM_SHARED
) && FILTER(HUGETLB_PRIVATE
))
1110 /* Do not dump I/O mapped devices or special mappings */
1111 if (vma
->vm_flags
& (VM_IO
| VM_RESERVED
))
1114 /* By default, dump shared memory if mapped from an anonymous file. */
1115 if (vma
->vm_flags
& VM_SHARED
) {
1116 if (vma
->vm_file
->f_path
.dentry
->d_inode
->i_nlink
== 0 ?
1117 FILTER(ANON_SHARED
) : FILTER(MAPPED_SHARED
))
1122 /* Dump segments that have been written to. */
1123 if (vma
->anon_vma
&& FILTER(ANON_PRIVATE
))
1125 if (vma
->vm_file
== NULL
)
1128 if (FILTER(MAPPED_PRIVATE
))
1132 * If this looks like the beginning of a DSO or executable mapping,
1133 * check for an ELF header. If we find one, dump the first page to
1134 * aid in determining what was mapped here.
1136 if (FILTER(ELF_HEADERS
) &&
1137 vma
->vm_pgoff
== 0 && (vma
->vm_flags
& VM_READ
)) {
1138 u32 __user
*header
= (u32 __user
*) vma
->vm_start
;
1140 mm_segment_t fs
= get_fs();
1142 * Doing it this way gets the constant folded by GCC.
1146 char elfmag
[SELFMAG
];
1148 BUILD_BUG_ON(SELFMAG
!= sizeof word
);
1149 magic
.elfmag
[EI_MAG0
] = ELFMAG0
;
1150 magic
.elfmag
[EI_MAG1
] = ELFMAG1
;
1151 magic
.elfmag
[EI_MAG2
] = ELFMAG2
;
1152 magic
.elfmag
[EI_MAG3
] = ELFMAG3
;
1154 * Switch to the user "segment" for get_user(),
1155 * then put back what elf_core_dump() had in place.
1158 if (unlikely(get_user(word
, header
)))
1161 if (word
== magic
.cmp
)
1170 return vma
->vm_end
- vma
->vm_start
;
1173 /* An ELF note in memory */
1178 unsigned int datasz
;
1182 static int notesize(struct memelfnote
*en
)
1186 sz
= sizeof(struct elf_note
);
1187 sz
+= roundup(strlen(en
->name
) + 1, 4);
1188 sz
+= roundup(en
->datasz
, 4);
1193 #define DUMP_WRITE(addr, nr, foffset) \
1194 do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
1196 static int alignfile(struct file
*file
, loff_t
*foffset
)
1198 static const char buf
[4] = { 0, };
1199 DUMP_WRITE(buf
, roundup(*foffset
, 4) - *foffset
, foffset
);
1203 static int writenote(struct memelfnote
*men
, struct file
*file
,
1207 en
.n_namesz
= strlen(men
->name
) + 1;
1208 en
.n_descsz
= men
->datasz
;
1209 en
.n_type
= men
->type
;
1211 DUMP_WRITE(&en
, sizeof(en
), foffset
);
1212 DUMP_WRITE(men
->name
, en
.n_namesz
, foffset
);
1213 if (!alignfile(file
, foffset
))
1215 DUMP_WRITE(men
->data
, men
->datasz
, foffset
);
1216 if (!alignfile(file
, foffset
))
1223 static void fill_elf_header(struct elfhdr
*elf
, int segs
,
1224 u16 machine
, u32 flags
, u8 osabi
)
1226 memset(elf
, 0, sizeof(*elf
));
1228 memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
1229 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
1230 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
1231 elf
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1232 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
1234 elf
->e_type
= ET_CORE
;
1235 elf
->e_machine
= machine
;
1236 elf
->e_version
= EV_CURRENT
;
1237 elf
->e_phoff
= sizeof(struct elfhdr
);
1238 elf
->e_flags
= flags
;
1239 elf
->e_ehsize
= sizeof(struct elfhdr
);
1240 elf
->e_phentsize
= sizeof(struct elf_phdr
);
1241 elf
->e_phnum
= segs
;
1246 static void fill_elf_note_phdr(struct elf_phdr
*phdr
, int sz
, loff_t offset
)
1248 phdr
->p_type
= PT_NOTE
;
1249 phdr
->p_offset
= offset
;
1252 phdr
->p_filesz
= sz
;
1259 static void fill_note(struct memelfnote
*note
, const char *name
, int type
,
1260 unsigned int sz
, void *data
)
1270 * fill up all the fields in prstatus from the given task struct, except
1271 * registers which need to be filled up separately.
1273 static void fill_prstatus(struct elf_prstatus
*prstatus
,
1274 struct task_struct
*p
, long signr
)
1276 prstatus
->pr_info
.si_signo
= prstatus
->pr_cursig
= signr
;
1277 prstatus
->pr_sigpend
= p
->pending
.signal
.sig
[0];
1278 prstatus
->pr_sighold
= p
->blocked
.sig
[0];
1280 prstatus
->pr_ppid
= task_pid_vnr(rcu_dereference(p
->real_parent
));
1282 prstatus
->pr_pid
= task_pid_vnr(p
);
1283 prstatus
->pr_pgrp
= task_pgrp_vnr(p
);
1284 prstatus
->pr_sid
= task_session_vnr(p
);
1285 if (thread_group_leader(p
)) {
1286 struct task_cputime cputime
;
1289 * This is the record for the group leader. It shows the
1290 * group-wide total, not its individual thread total.
1292 thread_group_cputime(p
, &cputime
);
1293 cputime_to_timeval(cputime
.utime
, &prstatus
->pr_utime
);
1294 cputime_to_timeval(cputime
.stime
, &prstatus
->pr_stime
);
1296 cputime_to_timeval(p
->utime
, &prstatus
->pr_utime
);
1297 cputime_to_timeval(p
->stime
, &prstatus
->pr_stime
);
1299 cputime_to_timeval(p
->signal
->cutime
, &prstatus
->pr_cutime
);
1300 cputime_to_timeval(p
->signal
->cstime
, &prstatus
->pr_cstime
);
1303 static int fill_psinfo(struct elf_prpsinfo
*psinfo
, struct task_struct
*p
,
1304 struct mm_struct
*mm
)
1306 const struct cred
*cred
;
1307 unsigned int i
, len
;
1309 /* first copy the parameters from user space */
1310 memset(psinfo
, 0, sizeof(struct elf_prpsinfo
));
1312 len
= mm
->arg_end
- mm
->arg_start
;
1313 if (len
>= ELF_PRARGSZ
)
1314 len
= ELF_PRARGSZ
-1;
1315 if (copy_from_user(&psinfo
->pr_psargs
,
1316 (const char __user
*)mm
->arg_start
, len
))
1318 for(i
= 0; i
< len
; i
++)
1319 if (psinfo
->pr_psargs
[i
] == 0)
1320 psinfo
->pr_psargs
[i
] = ' ';
1321 psinfo
->pr_psargs
[len
] = 0;
1324 psinfo
->pr_ppid
= task_pid_vnr(rcu_dereference(p
->real_parent
));
1326 psinfo
->pr_pid
= task_pid_vnr(p
);
1327 psinfo
->pr_pgrp
= task_pgrp_vnr(p
);
1328 psinfo
->pr_sid
= task_session_vnr(p
);
1330 i
= p
->state
? ffz(~p
->state
) + 1 : 0;
1331 psinfo
->pr_state
= i
;
1332 psinfo
->pr_sname
= (i
> 5) ? '.' : "RSDTZW"[i
];
1333 psinfo
->pr_zomb
= psinfo
->pr_sname
== 'Z';
1334 psinfo
->pr_nice
= task_nice(p
);
1335 psinfo
->pr_flag
= p
->flags
;
1337 cred
= __task_cred(p
);
1338 SET_UID(psinfo
->pr_uid
, cred
->uid
);
1339 SET_GID(psinfo
->pr_gid
, cred
->gid
);
1341 strncpy(psinfo
->pr_fname
, p
->comm
, sizeof(psinfo
->pr_fname
));
1346 static void fill_auxv_note(struct memelfnote
*note
, struct mm_struct
*mm
)
1348 elf_addr_t
*auxv
= (elf_addr_t
*) mm
->saved_auxv
;
1352 while (auxv
[i
- 2] != AT_NULL
);
1353 fill_note(note
, "CORE", NT_AUXV
, i
* sizeof(elf_addr_t
), auxv
);
1356 #ifdef CORE_DUMP_USE_REGSET
1357 #include <linux/regset.h>
1359 struct elf_thread_core_info
{
1360 struct elf_thread_core_info
*next
;
1361 struct task_struct
*task
;
1362 struct elf_prstatus prstatus
;
1363 struct memelfnote notes
[0];
1366 struct elf_note_info
{
1367 struct elf_thread_core_info
*thread
;
1368 struct memelfnote psinfo
;
1369 struct memelfnote auxv
;
1375 * When a regset has a writeback hook, we call it on each thread before
1376 * dumping user memory. On register window machines, this makes sure the
1377 * user memory backing the register data is up to date before we read it.
1379 static void do_thread_regset_writeback(struct task_struct
*task
,
1380 const struct user_regset
*regset
)
1382 if (regset
->writeback
)
1383 regset
->writeback(task
, regset
, 1);
1386 static int fill_thread_core_info(struct elf_thread_core_info
*t
,
1387 const struct user_regset_view
*view
,
1388 long signr
, size_t *total
)
1393 * NT_PRSTATUS is the one special case, because the regset data
1394 * goes into the pr_reg field inside the note contents, rather
1395 * than being the whole note contents. We fill the reset in here.
1396 * We assume that regset 0 is NT_PRSTATUS.
1398 fill_prstatus(&t
->prstatus
, t
->task
, signr
);
1399 (void) view
->regsets
[0].get(t
->task
, &view
->regsets
[0],
1400 0, sizeof(t
->prstatus
.pr_reg
),
1401 &t
->prstatus
.pr_reg
, NULL
);
1403 fill_note(&t
->notes
[0], "CORE", NT_PRSTATUS
,
1404 sizeof(t
->prstatus
), &t
->prstatus
);
1405 *total
+= notesize(&t
->notes
[0]);
1407 do_thread_regset_writeback(t
->task
, &view
->regsets
[0]);
1410 * Each other regset might generate a note too. For each regset
1411 * that has no core_note_type or is inactive, we leave t->notes[i]
1412 * all zero and we'll know to skip writing it later.
1414 for (i
= 1; i
< view
->n
; ++i
) {
1415 const struct user_regset
*regset
= &view
->regsets
[i
];
1416 do_thread_regset_writeback(t
->task
, regset
);
1417 if (regset
->core_note_type
&&
1418 (!regset
->active
|| regset
->active(t
->task
, regset
))) {
1420 size_t size
= regset
->n
* regset
->size
;
1421 void *data
= kmalloc(size
, GFP_KERNEL
);
1422 if (unlikely(!data
))
1424 ret
= regset
->get(t
->task
, regset
,
1425 0, size
, data
, NULL
);
1429 if (regset
->core_note_type
!= NT_PRFPREG
)
1430 fill_note(&t
->notes
[i
], "LINUX",
1431 regset
->core_note_type
,
1434 t
->prstatus
.pr_fpvalid
= 1;
1435 fill_note(&t
->notes
[i
], "CORE",
1436 NT_PRFPREG
, size
, data
);
1438 *total
+= notesize(&t
->notes
[i
]);
1446 static int fill_note_info(struct elfhdr
*elf
, int phdrs
,
1447 struct elf_note_info
*info
,
1448 long signr
, struct pt_regs
*regs
)
1450 struct task_struct
*dump_task
= current
;
1451 const struct user_regset_view
*view
= task_user_regset_view(dump_task
);
1452 struct elf_thread_core_info
*t
;
1453 struct elf_prpsinfo
*psinfo
;
1454 struct core_thread
*ct
;
1458 info
->thread
= NULL
;
1460 psinfo
= kmalloc(sizeof(*psinfo
), GFP_KERNEL
);
1464 fill_note(&info
->psinfo
, "CORE", NT_PRPSINFO
, sizeof(*psinfo
), psinfo
);
1467 * Figure out how many notes we're going to need for each thread.
1469 info
->thread_notes
= 0;
1470 for (i
= 0; i
< view
->n
; ++i
)
1471 if (view
->regsets
[i
].core_note_type
!= 0)
1472 ++info
->thread_notes
;
1475 * Sanity check. We rely on regset 0 being in NT_PRSTATUS,
1476 * since it is our one special case.
1478 if (unlikely(info
->thread_notes
== 0) ||
1479 unlikely(view
->regsets
[0].core_note_type
!= NT_PRSTATUS
)) {
1485 * Initialize the ELF file header.
1487 fill_elf_header(elf
, phdrs
,
1488 view
->e_machine
, view
->e_flags
, view
->ei_osabi
);
1491 * Allocate a structure for each thread.
1493 for (ct
= &dump_task
->mm
->core_state
->dumper
; ct
; ct
= ct
->next
) {
1494 t
= kzalloc(offsetof(struct elf_thread_core_info
,
1495 notes
[info
->thread_notes
]),
1501 if (ct
->task
== dump_task
|| !info
->thread
) {
1502 t
->next
= info
->thread
;
1506 * Make sure to keep the original task at
1507 * the head of the list.
1509 t
->next
= info
->thread
->next
;
1510 info
->thread
->next
= t
;
1515 * Now fill in each thread's information.
1517 for (t
= info
->thread
; t
!= NULL
; t
= t
->next
)
1518 if (!fill_thread_core_info(t
, view
, signr
, &info
->size
))
1522 * Fill in the two process-wide notes.
1524 fill_psinfo(psinfo
, dump_task
->group_leader
, dump_task
->mm
);
1525 info
->size
+= notesize(&info
->psinfo
);
1527 fill_auxv_note(&info
->auxv
, current
->mm
);
1528 info
->size
+= notesize(&info
->auxv
);
1533 static size_t get_note_info_size(struct elf_note_info
*info
)
1539 * Write all the notes for each thread. When writing the first thread, the
1540 * process-wide notes are interleaved after the first thread-specific note.
1542 static int write_note_info(struct elf_note_info
*info
,
1543 struct file
*file
, loff_t
*foffset
)
1546 struct elf_thread_core_info
*t
= info
->thread
;
1551 if (!writenote(&t
->notes
[0], file
, foffset
))
1554 if (first
&& !writenote(&info
->psinfo
, file
, foffset
))
1556 if (first
&& !writenote(&info
->auxv
, file
, foffset
))
1559 for (i
= 1; i
< info
->thread_notes
; ++i
)
1560 if (t
->notes
[i
].data
&&
1561 !writenote(&t
->notes
[i
], file
, foffset
))
1571 static void free_note_info(struct elf_note_info
*info
)
1573 struct elf_thread_core_info
*threads
= info
->thread
;
1576 struct elf_thread_core_info
*t
= threads
;
1578 WARN_ON(t
->notes
[0].data
&& t
->notes
[0].data
!= &t
->prstatus
);
1579 for (i
= 1; i
< info
->thread_notes
; ++i
)
1580 kfree(t
->notes
[i
].data
);
1583 kfree(info
->psinfo
.data
);
1588 /* Here is the structure in which status of each thread is captured. */
1589 struct elf_thread_status
1591 struct list_head list
;
1592 struct elf_prstatus prstatus
; /* NT_PRSTATUS */
1593 elf_fpregset_t fpu
; /* NT_PRFPREG */
1594 struct task_struct
*thread
;
1595 #ifdef ELF_CORE_COPY_XFPREGS
1596 elf_fpxregset_t xfpu
; /* ELF_CORE_XFPREG_TYPE */
1598 struct memelfnote notes
[3];
1603 * In order to add the specific thread information for the elf file format,
1604 * we need to keep a linked list of every threads pr_status and then create
1605 * a single section for them in the final core file.
1607 static int elf_dump_thread_status(long signr
, struct elf_thread_status
*t
)
1610 struct task_struct
*p
= t
->thread
;
1613 fill_prstatus(&t
->prstatus
, p
, signr
);
1614 elf_core_copy_task_regs(p
, &t
->prstatus
.pr_reg
);
1616 fill_note(&t
->notes
[0], "CORE", NT_PRSTATUS
, sizeof(t
->prstatus
),
1619 sz
+= notesize(&t
->notes
[0]);
1621 if ((t
->prstatus
.pr_fpvalid
= elf_core_copy_task_fpregs(p
, NULL
,
1623 fill_note(&t
->notes
[1], "CORE", NT_PRFPREG
, sizeof(t
->fpu
),
1626 sz
+= notesize(&t
->notes
[1]);
1629 #ifdef ELF_CORE_COPY_XFPREGS
1630 if (elf_core_copy_task_xfpregs(p
, &t
->xfpu
)) {
1631 fill_note(&t
->notes
[2], "LINUX", ELF_CORE_XFPREG_TYPE
,
1632 sizeof(t
->xfpu
), &t
->xfpu
);
1634 sz
+= notesize(&t
->notes
[2]);
1640 struct elf_note_info
{
1641 struct memelfnote
*notes
;
1642 struct elf_prstatus
*prstatus
; /* NT_PRSTATUS */
1643 struct elf_prpsinfo
*psinfo
; /* NT_PRPSINFO */
1644 struct list_head thread_list
;
1645 elf_fpregset_t
*fpu
;
1646 #ifdef ELF_CORE_COPY_XFPREGS
1647 elf_fpxregset_t
*xfpu
;
1649 int thread_status_size
;
1653 static int elf_note_info_init(struct elf_note_info
*info
)
1655 memset(info
, 0, sizeof(*info
));
1656 INIT_LIST_HEAD(&info
->thread_list
);
1658 /* Allocate space for six ELF notes */
1659 info
->notes
= kmalloc(6 * sizeof(struct memelfnote
), GFP_KERNEL
);
1662 info
->psinfo
= kmalloc(sizeof(*info
->psinfo
), GFP_KERNEL
);
1665 info
->prstatus
= kmalloc(sizeof(*info
->prstatus
), GFP_KERNEL
);
1666 if (!info
->prstatus
)
1668 info
->fpu
= kmalloc(sizeof(*info
->fpu
), GFP_KERNEL
);
1671 #ifdef ELF_CORE_COPY_XFPREGS
1672 info
->xfpu
= kmalloc(sizeof(*info
->xfpu
), GFP_KERNEL
);
1677 #ifdef ELF_CORE_COPY_XFPREGS
1682 kfree(info
->prstatus
);
1684 kfree(info
->psinfo
);
1690 static int fill_note_info(struct elfhdr
*elf
, int phdrs
,
1691 struct elf_note_info
*info
,
1692 long signr
, struct pt_regs
*regs
)
1694 struct list_head
*t
;
1696 if (!elf_note_info_init(info
))
1700 struct core_thread
*ct
;
1701 struct elf_thread_status
*ets
;
1703 for (ct
= current
->mm
->core_state
->dumper
.next
;
1704 ct
; ct
= ct
->next
) {
1705 ets
= kzalloc(sizeof(*ets
), GFP_KERNEL
);
1709 ets
->thread
= ct
->task
;
1710 list_add(&ets
->list
, &info
->thread_list
);
1713 list_for_each(t
, &info
->thread_list
) {
1716 ets
= list_entry(t
, struct elf_thread_status
, list
);
1717 sz
= elf_dump_thread_status(signr
, ets
);
1718 info
->thread_status_size
+= sz
;
1721 /* now collect the dump for the current */
1722 memset(info
->prstatus
, 0, sizeof(*info
->prstatus
));
1723 fill_prstatus(info
->prstatus
, current
, signr
);
1724 elf_core_copy_regs(&info
->prstatus
->pr_reg
, regs
);
1727 fill_elf_header(elf
, phdrs
, ELF_ARCH
, ELF_CORE_EFLAGS
, ELF_OSABI
);
1730 * Set up the notes in similar form to SVR4 core dumps made
1731 * with info from their /proc.
1734 fill_note(info
->notes
+ 0, "CORE", NT_PRSTATUS
,
1735 sizeof(*info
->prstatus
), info
->prstatus
);
1736 fill_psinfo(info
->psinfo
, current
->group_leader
, current
->mm
);
1737 fill_note(info
->notes
+ 1, "CORE", NT_PRPSINFO
,
1738 sizeof(*info
->psinfo
), info
->psinfo
);
1742 fill_auxv_note(&info
->notes
[info
->numnote
++], current
->mm
);
1744 /* Try to dump the FPU. */
1745 info
->prstatus
->pr_fpvalid
= elf_core_copy_task_fpregs(current
, regs
,
1747 if (info
->prstatus
->pr_fpvalid
)
1748 fill_note(info
->notes
+ info
->numnote
++,
1749 "CORE", NT_PRFPREG
, sizeof(*info
->fpu
), info
->fpu
);
1750 #ifdef ELF_CORE_COPY_XFPREGS
1751 if (elf_core_copy_task_xfpregs(current
, info
->xfpu
))
1752 fill_note(info
->notes
+ info
->numnote
++,
1753 "LINUX", ELF_CORE_XFPREG_TYPE
,
1754 sizeof(*info
->xfpu
), info
->xfpu
);
1760 static size_t get_note_info_size(struct elf_note_info
*info
)
1765 for (i
= 0; i
< info
->numnote
; i
++)
1766 sz
+= notesize(info
->notes
+ i
);
1768 sz
+= info
->thread_status_size
;
1773 static int write_note_info(struct elf_note_info
*info
,
1774 struct file
*file
, loff_t
*foffset
)
1777 struct list_head
*t
;
1779 for (i
= 0; i
< info
->numnote
; i
++)
1780 if (!writenote(info
->notes
+ i
, file
, foffset
))
1783 /* write out the thread status notes section */
1784 list_for_each(t
, &info
->thread_list
) {
1785 struct elf_thread_status
*tmp
=
1786 list_entry(t
, struct elf_thread_status
, list
);
1788 for (i
= 0; i
< tmp
->num_notes
; i
++)
1789 if (!writenote(&tmp
->notes
[i
], file
, foffset
))
1796 static void free_note_info(struct elf_note_info
*info
)
1798 while (!list_empty(&info
->thread_list
)) {
1799 struct list_head
*tmp
= info
->thread_list
.next
;
1801 kfree(list_entry(tmp
, struct elf_thread_status
, list
));
1804 kfree(info
->prstatus
);
1805 kfree(info
->psinfo
);
1808 #ifdef ELF_CORE_COPY_XFPREGS
1815 static struct vm_area_struct
*first_vma(struct task_struct
*tsk
,
1816 struct vm_area_struct
*gate_vma
)
1818 struct vm_area_struct
*ret
= tsk
->mm
->mmap
;
1825 * Helper function for iterating across a vma list. It ensures that the caller
1826 * will visit `gate_vma' prior to terminating the search.
1828 static struct vm_area_struct
*next_vma(struct vm_area_struct
*this_vma
,
1829 struct vm_area_struct
*gate_vma
)
1831 struct vm_area_struct
*ret
;
1833 ret
= this_vma
->vm_next
;
1836 if (this_vma
== gate_vma
)
1841 static void fill_extnum_info(struct elfhdr
*elf
, struct elf_shdr
*shdr4extnum
,
1842 elf_addr_t e_shoff
, int segs
)
1844 elf
->e_shoff
= e_shoff
;
1845 elf
->e_shentsize
= sizeof(*shdr4extnum
);
1847 elf
->e_shstrndx
= SHN_UNDEF
;
1849 memset(shdr4extnum
, 0, sizeof(*shdr4extnum
));
1851 shdr4extnum
->sh_type
= SHT_NULL
;
1852 shdr4extnum
->sh_size
= elf
->e_shnum
;
1853 shdr4extnum
->sh_link
= elf
->e_shstrndx
;
1854 shdr4extnum
->sh_info
= segs
;
1857 static size_t elf_core_vma_data_size(struct vm_area_struct
*gate_vma
,
1858 unsigned long mm_flags
)
1860 struct vm_area_struct
*vma
;
1863 for (vma
= first_vma(current
, gate_vma
); vma
!= NULL
;
1864 vma
= next_vma(vma
, gate_vma
))
1865 size
+= vma_dump_size(vma
, mm_flags
);
1872 * This is a two-pass process; first we find the offsets of the bits,
1873 * and then they are actually written out. If we run out of core limit
1876 static int elf_core_dump(struct coredump_params
*cprm
)
1882 struct vm_area_struct
*vma
, *gate_vma
;
1883 struct elfhdr
*elf
= NULL
;
1884 loff_t offset
= 0, dataoff
, foffset
;
1885 struct elf_note_info info
;
1886 struct elf_phdr
*phdr4note
= NULL
;
1887 struct elf_shdr
*shdr4extnum
= NULL
;
1892 * We no longer stop all VM operations.
1894 * This is because those proceses that could possibly change map_count
1895 * or the mmap / vma pages are now blocked in do_exit on current
1896 * finishing this core dump.
1898 * Only ptrace can touch these memory addresses, but it doesn't change
1899 * the map_count or the pages allocated. So no possibility of crashing
1900 * exists while dumping the mm->vm_next areas to the core file.
1903 /* alloc memory for large data structures: too large to be on stack */
1904 elf
= kmalloc(sizeof(*elf
), GFP_KERNEL
);
1908 * The number of segs are recored into ELF header as 16bit value.
1909 * Please check DEFAULT_MAX_MAP_COUNT definition when you modify here.
1911 segs
= current
->mm
->map_count
;
1912 segs
+= elf_core_extra_phdrs();
1914 gate_vma
= get_gate_vma(current
);
1915 if (gate_vma
!= NULL
)
1918 /* for notes section */
1921 /* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid
1922 * this, kernel supports extended numbering. Have a look at
1923 * include/linux/elf.h for further information. */
1924 e_phnum
= segs
> PN_XNUM
? PN_XNUM
: segs
;
1927 * Collect all the non-memory information about the process for the
1928 * notes. This also sets up the file header.
1930 if (!fill_note_info(elf
, e_phnum
, &info
, cprm
->signr
, cprm
->regs
))
1934 current
->flags
|= PF_DUMPCORE
;
1939 offset
+= sizeof(*elf
); /* Elf header */
1940 offset
+= segs
* sizeof(struct elf_phdr
); /* Program headers */
1943 /* Write notes phdr entry */
1945 size_t sz
= get_note_info_size(&info
);
1947 sz
+= elf_coredump_extra_notes_size();
1949 phdr4note
= kmalloc(sizeof(*phdr4note
), GFP_KERNEL
);
1953 fill_elf_note_phdr(phdr4note
, sz
, offset
);
1957 dataoff
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
1959 offset
+= elf_core_vma_data_size(gate_vma
, cprm
->mm_flags
);
1960 offset
+= elf_core_extra_data_size();
1963 if (e_phnum
== PN_XNUM
) {
1964 shdr4extnum
= kmalloc(sizeof(*shdr4extnum
), GFP_KERNEL
);
1967 fill_extnum_info(elf
, shdr4extnum
, e_shoff
, segs
);
1972 size
+= sizeof(*elf
);
1973 if (size
> cprm
->limit
|| !dump_write(cprm
->file
, elf
, sizeof(*elf
)))
1976 size
+= sizeof(*phdr4note
);
1977 if (size
> cprm
->limit
1978 || !dump_write(cprm
->file
, phdr4note
, sizeof(*phdr4note
)))
1981 /* Write program headers for segments dump */
1982 for (vma
= first_vma(current
, gate_vma
); vma
!= NULL
;
1983 vma
= next_vma(vma
, gate_vma
)) {
1984 struct elf_phdr phdr
;
1986 phdr
.p_type
= PT_LOAD
;
1987 phdr
.p_offset
= offset
;
1988 phdr
.p_vaddr
= vma
->vm_start
;
1990 phdr
.p_filesz
= vma_dump_size(vma
, cprm
->mm_flags
);
1991 phdr
.p_memsz
= vma
->vm_end
- vma
->vm_start
;
1992 offset
+= phdr
.p_filesz
;
1993 phdr
.p_flags
= vma
->vm_flags
& VM_READ
? PF_R
: 0;
1994 if (vma
->vm_flags
& VM_WRITE
)
1995 phdr
.p_flags
|= PF_W
;
1996 if (vma
->vm_flags
& VM_EXEC
)
1997 phdr
.p_flags
|= PF_X
;
1998 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
2000 size
+= sizeof(phdr
);
2001 if (size
> cprm
->limit
2002 || !dump_write(cprm
->file
, &phdr
, sizeof(phdr
)))
2006 if (!elf_core_write_extra_phdrs(cprm
->file
, offset
, &size
, cprm
->limit
))
2009 /* write out the notes section */
2010 if (!write_note_info(&info
, cprm
->file
, &foffset
))
2013 if (elf_coredump_extra_notes_write(cprm
->file
, &foffset
))
2017 if (!dump_seek(cprm
->file
, dataoff
- foffset
))
2020 for (vma
= first_vma(current
, gate_vma
); vma
!= NULL
;
2021 vma
= next_vma(vma
, gate_vma
)) {
2025 end
= vma
->vm_start
+ vma_dump_size(vma
, cprm
->mm_flags
);
2027 for (addr
= vma
->vm_start
; addr
< end
; addr
+= PAGE_SIZE
) {
2031 page
= get_dump_page(addr
);
2033 void *kaddr
= kmap(page
);
2034 stop
= ((size
+= PAGE_SIZE
) > cprm
->limit
) ||
2035 !dump_write(cprm
->file
, kaddr
,
2038 page_cache_release(page
);
2040 stop
= !dump_seek(cprm
->file
, PAGE_SIZE
);
2046 if (!elf_core_write_extra_data(cprm
->file
, &size
, cprm
->limit
))
2049 if (e_phnum
== PN_XNUM
) {
2050 size
+= sizeof(*shdr4extnum
);
2051 if (size
> cprm
->limit
2052 || !dump_write(cprm
->file
, shdr4extnum
,
2053 sizeof(*shdr4extnum
)))
2061 free_note_info(&info
);
2069 #endif /* CONFIG_ELF_CORE */
2071 static int __init
init_elf_binfmt(void)
2073 return register_binfmt(&elf_format
);
2076 static void __exit
exit_elf_binfmt(void)
2078 /* Remove the COFF and ELF loaders. */
2079 unregister_binfmt(&elf_format
);
2082 core_initcall(init_elf_binfmt
);
2083 module_exit(exit_elf_binfmt
);
2084 MODULE_LICENSE("GPL");