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/a.out.h>
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/binfmts.h>
23 #include <linux/string.h>
24 #include <linux/file.h>
25 #include <linux/fcntl.h>
26 #include <linux/ptrace.h>
27 #include <linux/slab.h>
28 #include <linux/shm.h>
29 #include <linux/personality.h>
30 #include <linux/elfcore.h>
31 #include <linux/init.h>
32 #include <linux/highuid.h>
33 #include <linux/smp.h>
34 #include <linux/compiler.h>
35 #include <linux/highmem.h>
36 #include <linux/pagemap.h>
37 #include <linux/security.h>
38 #include <linux/syscalls.h>
39 #include <linux/random.h>
40 #include <linux/elf.h>
41 #include <linux/utsname.h>
42 #include <asm/uaccess.h>
43 #include <asm/param.h>
46 static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs);
47 static int load_elf_library(struct file *);
48 static unsigned long elf_map(struct file *, unsigned long, struct elf_phdr *,
49 int, int, unsigned long);
52 * If we don't support core dumping, then supply a NULL so we
55 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
56 static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit);
58 #define elf_core_dump NULL
61 #if ELF_EXEC_PAGESIZE > PAGE_SIZE
62 #define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
64 #define ELF_MIN_ALIGN PAGE_SIZE
67 #ifndef ELF_CORE_EFLAGS
68 #define ELF_CORE_EFLAGS 0
71 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
72 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
73 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
75 static struct linux_binfmt elf_format = {
76 .module = THIS_MODULE,
77 .load_binary = load_elf_binary,
78 .load_shlib = load_elf_library,
79 .core_dump = elf_core_dump,
80 .min_coredump = ELF_EXEC_PAGESIZE,
84 #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
86 static int set_brk(unsigned long start, unsigned long end)
88 start = ELF_PAGEALIGN(start);
89 end = ELF_PAGEALIGN(end);
92 down_write(¤t->mm->mmap_sem);
93 addr = do_brk(start, end - start);
94 up_write(¤t->mm->mmap_sem);
98 current->mm->start_brk = current->mm->brk = end;
102 /* We need to explicitly zero any fractional pages
103 after the data section (i.e. bss). This would
104 contain the junk from the file that should not
107 static int padzero(unsigned long elf_bss)
111 nbyte = ELF_PAGEOFFSET(elf_bss);
113 nbyte = ELF_MIN_ALIGN - nbyte;
114 if (clear_user((void __user *) elf_bss, nbyte))
120 /* Let's use some macros to make this stack manipulation a little clearer */
121 #ifdef CONFIG_STACK_GROWSUP
122 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
123 #define STACK_ROUND(sp, items) \
124 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
125 #define STACK_ALLOC(sp, len) ({ \
126 elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
129 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
130 #define STACK_ROUND(sp, items) \
131 (((unsigned long) (sp - items)) &~ 15UL)
132 #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
136 create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
137 unsigned long load_addr, unsigned long interp_load_addr)
139 unsigned long p = bprm->p;
140 int argc = bprm->argc;
141 int envc = bprm->envc;
142 elf_addr_t __user *argv;
143 elf_addr_t __user *envp;
144 elf_addr_t __user *sp;
145 elf_addr_t __user *u_platform;
146 const char *k_platform = ELF_PLATFORM;
148 elf_addr_t *elf_info;
150 struct task_struct *tsk = current;
151 struct vm_area_struct *vma;
154 * In some cases (e.g. Hyper-Threading), we want to avoid L1
155 * evictions by the processes running on the same package. One
156 * thing we can do is to shuffle the initial stack for them.
159 p = arch_align_stack(p);
162 * If this architecture has a platform capability string, copy it
163 * to userspace. In some cases (Sparc), this info is impossible
164 * for userspace to get any other way, in others (i386) it is
169 size_t len = strlen(k_platform) + 1;
171 u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
172 if (__copy_to_user(u_platform, k_platform, len))
176 /* Create the ELF interpreter info */
177 elf_info = (elf_addr_t *)current->mm->saved_auxv;
178 /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
179 #define NEW_AUX_ENT(id, val) \
181 elf_info[ei_index++] = id; \
182 elf_info[ei_index++] = val; \
187 * ARCH_DLINFO must come first so PPC can do its special alignment of
189 * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
190 * ARCH_DLINFO changes
194 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
195 NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
196 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
197 NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
198 NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
199 NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
200 NEW_AUX_ENT(AT_BASE, interp_load_addr);
201 NEW_AUX_ENT(AT_FLAGS, 0);
202 NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
203 NEW_AUX_ENT(AT_UID, tsk->uid);
204 NEW_AUX_ENT(AT_EUID, tsk->euid);
205 NEW_AUX_ENT(AT_GID, tsk->gid);
206 NEW_AUX_ENT(AT_EGID, tsk->egid);
207 NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
209 NEW_AUX_ENT(AT_PLATFORM,
210 (elf_addr_t)(unsigned long)u_platform);
212 if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
213 NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
216 /* AT_NULL is zero; clear the rest too */
217 memset(&elf_info[ei_index], 0,
218 sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
220 /* And advance past the AT_NULL entry. */
223 sp = STACK_ADD(p, ei_index);
225 items = (argc + 1) + (envc + 1) + 1;
226 bprm->p = STACK_ROUND(sp, items);
228 /* Point sp at the lowest address on the stack */
229 #ifdef CONFIG_STACK_GROWSUP
230 sp = (elf_addr_t __user *)bprm->p - items - ei_index;
231 bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */
233 sp = (elf_addr_t __user *)bprm->p;
238 * Grow the stack manually; some architectures have a limit on how
239 * far ahead a user-space access may be in order to grow the stack.
241 vma = find_extend_vma(current->mm, bprm->p);
245 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
246 if (__put_user(argc, sp++))
249 envp = argv + argc + 1;
251 /* Populate argv and envp */
252 p = current->mm->arg_end = current->mm->arg_start;
255 if (__put_user((elf_addr_t)p, argv++))
257 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
258 if (!len || len > MAX_ARG_STRLEN)
262 if (__put_user(0, argv))
264 current->mm->arg_end = current->mm->env_start = p;
267 if (__put_user((elf_addr_t)p, envp++))
269 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
270 if (!len || len > MAX_ARG_STRLEN)
274 if (__put_user(0, envp))
276 current->mm->env_end = p;
278 /* Put the elf_info on the stack in the right place. */
279 sp = (elf_addr_t __user *)envp + 1;
280 if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t)))
287 static unsigned long elf_map(struct file *filep, unsigned long addr,
288 struct elf_phdr *eppnt, int prot, int type,
289 unsigned long total_size)
291 unsigned long map_addr;
292 unsigned long size = eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr);
293 unsigned long off = eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr);
294 addr = ELF_PAGESTART(addr);
295 size = ELF_PAGEALIGN(size);
297 /* mmap() will return -EINVAL if given a zero size, but a
298 * segment with zero filesize is perfectly valid */
302 down_write(¤t->mm->mmap_sem);
304 * total_size is the size of the ELF (interpreter) image.
305 * The _first_ mmap needs to know the full size, otherwise
306 * randomization might put this image into an overlapping
307 * position with the ELF binary image. (since size < total_size)
308 * So we first map the 'big' image - and unmap the remainder at
309 * the end. (which unmap is needed for ELF images with holes.)
312 total_size = ELF_PAGEALIGN(total_size);
313 map_addr = do_mmap(filep, addr, total_size, prot, type, off);
314 if (!BAD_ADDR(map_addr))
315 do_munmap(current->mm, map_addr+size, total_size-size);
317 map_addr = do_mmap(filep, addr, size, prot, type, off);
319 up_write(¤t->mm->mmap_sem);
323 #endif /* !elf_map */
325 static unsigned long total_mapping_size(struct elf_phdr *cmds, int nr)
327 int i, first_idx = -1, last_idx = -1;
329 for (i = 0; i < nr; i++) {
330 if (cmds[i].p_type == PT_LOAD) {
339 return cmds[last_idx].p_vaddr + cmds[last_idx].p_memsz -
340 ELF_PAGESTART(cmds[first_idx].p_vaddr);
344 /* This is much more generalized than the library routine read function,
345 so we keep this separate. Technically the library read function
346 is only provided so that we can read a.out libraries that have
349 static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
350 struct file *interpreter, unsigned long *interp_map_addr,
351 unsigned long no_base)
353 struct elf_phdr *elf_phdata;
354 struct elf_phdr *eppnt;
355 unsigned long load_addr = 0;
356 int load_addr_set = 0;
357 unsigned long last_bss = 0, elf_bss = 0;
358 unsigned long error = ~0UL;
359 unsigned long total_size;
362 /* First of all, some simple consistency checks */
363 if (interp_elf_ex->e_type != ET_EXEC &&
364 interp_elf_ex->e_type != ET_DYN)
366 if (!elf_check_arch(interp_elf_ex))
368 if (!interpreter->f_op || !interpreter->f_op->mmap)
372 * If the size of this structure has changed, then punt, since
373 * we will be doing the wrong thing.
375 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
377 if (interp_elf_ex->e_phnum < 1 ||
378 interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
381 /* Now read in all of the header information */
382 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
383 if (size > ELF_MIN_ALIGN)
385 elf_phdata = kmalloc(size, GFP_KERNEL);
389 retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
390 (char *)elf_phdata,size);
392 if (retval != size) {
398 total_size = total_mapping_size(elf_phdata, interp_elf_ex->e_phnum);
405 for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
406 if (eppnt->p_type == PT_LOAD) {
407 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
409 unsigned long vaddr = 0;
410 unsigned long k, map_addr;
412 if (eppnt->p_flags & PF_R)
413 elf_prot = PROT_READ;
414 if (eppnt->p_flags & PF_W)
415 elf_prot |= PROT_WRITE;
416 if (eppnt->p_flags & PF_X)
417 elf_prot |= PROT_EXEC;
418 vaddr = eppnt->p_vaddr;
419 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
420 elf_type |= MAP_FIXED;
421 else if (no_base && interp_elf_ex->e_type == ET_DYN)
424 map_addr = elf_map(interpreter, load_addr + vaddr,
425 eppnt, elf_prot, elf_type, total_size);
427 if (!*interp_map_addr)
428 *interp_map_addr = map_addr;
430 if (BAD_ADDR(map_addr))
433 if (!load_addr_set &&
434 interp_elf_ex->e_type == ET_DYN) {
435 load_addr = map_addr - ELF_PAGESTART(vaddr);
440 * Check to see if the section's size will overflow the
441 * allowed task size. Note that p_filesz must always be
442 * <= p_memsize so it's only necessary to check p_memsz.
444 k = load_addr + eppnt->p_vaddr;
446 eppnt->p_filesz > eppnt->p_memsz ||
447 eppnt->p_memsz > TASK_SIZE ||
448 TASK_SIZE - eppnt->p_memsz < k) {
454 * Find the end of the file mapping for this phdr, and
455 * keep track of the largest address we see for this.
457 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
462 * Do the same thing for the memory mapping - between
463 * elf_bss and last_bss is the bss section.
465 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
472 * Now fill out the bss section. First pad the last page up
473 * to the page boundary, and then perform a mmap to make sure
474 * that there are zero-mapped pages up to and including the
477 if (padzero(elf_bss)) {
482 /* What we have mapped so far */
483 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
485 /* Map the last of the bss segment */
486 if (last_bss > elf_bss) {
487 down_write(¤t->mm->mmap_sem);
488 error = do_brk(elf_bss, last_bss - elf_bss);
489 up_write(¤t->mm->mmap_sem);
503 * These are the functions used to load ELF style executables and shared
504 * libraries. There is no binary dependent code anywhere else.
507 #define INTERPRETER_NONE 0
508 #define INTERPRETER_ELF 2
510 #ifndef STACK_RND_MASK
511 #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
514 static unsigned long randomize_stack_top(unsigned long stack_top)
516 unsigned int random_variable = 0;
518 if ((current->flags & PF_RANDOMIZE) &&
519 !(current->personality & ADDR_NO_RANDOMIZE)) {
520 random_variable = get_random_int() & STACK_RND_MASK;
521 random_variable <<= PAGE_SHIFT;
523 #ifdef CONFIG_STACK_GROWSUP
524 return PAGE_ALIGN(stack_top) + random_variable;
526 return PAGE_ALIGN(stack_top) - random_variable;
530 static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
532 struct file *interpreter = NULL; /* to shut gcc up */
533 unsigned long load_addr = 0, load_bias = 0;
534 int load_addr_set = 0;
535 char * elf_interpreter = NULL;
537 struct elf_phdr *elf_ppnt, *elf_phdata;
538 unsigned long elf_bss, elf_brk;
542 unsigned long elf_entry;
543 unsigned long interp_load_addr = 0;
544 unsigned long start_code, end_code, start_data, end_data;
545 unsigned long reloc_func_desc = 0;
546 struct files_struct *files;
547 int executable_stack = EXSTACK_DEFAULT;
548 unsigned long def_flags = 0;
550 struct elfhdr elf_ex;
551 struct elfhdr interp_elf_ex;
552 struct exec interp_ex;
555 loc = kmalloc(sizeof(*loc), GFP_KERNEL);
561 /* Get the exec-header */
562 loc->elf_ex = *((struct elfhdr *)bprm->buf);
565 /* First of all, some simple consistency checks */
566 if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
569 if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
571 if (!elf_check_arch(&loc->elf_ex))
573 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
576 /* Now read in all of the header information */
577 if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
579 if (loc->elf_ex.e_phnum < 1 ||
580 loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
582 size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
584 elf_phdata = kmalloc(size, GFP_KERNEL);
588 retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
589 (char *)elf_phdata, size);
590 if (retval != size) {
596 files = current->files; /* Refcounted so ok */
597 retval = unshare_files();
600 if (files == current->files) {
601 put_files_struct(files);
605 /* exec will make our files private anyway, but for the a.out
606 loader stuff we need to do it earlier */
607 retval = get_unused_fd();
610 get_file(bprm->file);
611 fd_install(elf_exec_fileno = retval, bprm->file);
613 elf_ppnt = elf_phdata;
622 for (i = 0; i < loc->elf_ex.e_phnum; i++) {
623 if (elf_ppnt->p_type == PT_INTERP) {
624 /* This is the program interpreter used for
625 * shared libraries - for now assume that this
626 * is an a.out format binary
629 if (elf_ppnt->p_filesz > PATH_MAX ||
630 elf_ppnt->p_filesz < 2)
634 elf_interpreter = kmalloc(elf_ppnt->p_filesz,
636 if (!elf_interpreter)
639 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
642 if (retval != elf_ppnt->p_filesz) {
645 goto out_free_interp;
647 /* make sure path is NULL terminated */
649 if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
650 goto out_free_interp;
653 * The early SET_PERSONALITY here is so that the lookup
654 * for the interpreter happens in the namespace of the
655 * to-be-execed image. SET_PERSONALITY can select an
658 * However, SET_PERSONALITY is NOT allowed to switch
659 * this task into the new images's memory mapping
660 * policy - that is, TASK_SIZE must still evaluate to
661 * that which is appropriate to the execing application.
662 * This is because exit_mmap() needs to have TASK_SIZE
663 * evaluate to the size of the old image.
665 * So if (say) a 64-bit application is execing a 32-bit
666 * application it is the architecture's responsibility
667 * to defer changing the value of TASK_SIZE until the
668 * switch really is going to happen - do this in
669 * flush_thread(). - akpm
671 SET_PERSONALITY(loc->elf_ex, 0);
673 interpreter = open_exec(elf_interpreter);
674 retval = PTR_ERR(interpreter);
675 if (IS_ERR(interpreter))
676 goto out_free_interp;
679 * If the binary is not readable then enforce
680 * mm->dumpable = 0 regardless of the interpreter's
683 if (file_permission(interpreter, MAY_READ) < 0)
684 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
686 retval = kernel_read(interpreter, 0, bprm->buf,
688 if (retval != BINPRM_BUF_SIZE) {
691 goto out_free_dentry;
694 /* Get the exec headers */
695 loc->interp_ex = *((struct exec *)bprm->buf);
696 loc->interp_elf_ex = *((struct elfhdr *)bprm->buf);
702 elf_ppnt = elf_phdata;
703 for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
704 if (elf_ppnt->p_type == PT_GNU_STACK) {
705 if (elf_ppnt->p_flags & PF_X)
706 executable_stack = EXSTACK_ENABLE_X;
708 executable_stack = EXSTACK_DISABLE_X;
712 /* Some simple consistency checks for the interpreter */
713 if (elf_interpreter) {
715 /* Not an ELF interpreter */
716 if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
717 goto out_free_dentry;
718 /* Verify the interpreter has a valid arch */
719 if (!elf_check_arch(&loc->interp_elf_ex))
720 goto out_free_dentry;
722 /* Executables without an interpreter also need a personality */
723 SET_PERSONALITY(loc->elf_ex, 0);
726 /* Flush all traces of the currently running executable */
727 retval = flush_old_exec(bprm);
729 goto out_free_dentry;
731 /* Discard our unneeded old files struct */
733 put_files_struct(files);
737 /* OK, This is the point of no return */
738 current->flags &= ~PF_FORKNOEXEC;
739 current->mm->def_flags = def_flags;
741 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
742 may depend on the personality. */
743 SET_PERSONALITY(loc->elf_ex, 0);
744 if (elf_read_implies_exec(loc->elf_ex, executable_stack))
745 current->personality |= READ_IMPLIES_EXEC;
747 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
748 current->flags |= PF_RANDOMIZE;
749 arch_pick_mmap_layout(current->mm);
751 /* Do this so that we can load the interpreter, if need be. We will
752 change some of these later */
753 current->mm->free_area_cache = current->mm->mmap_base;
754 current->mm->cached_hole_size = 0;
755 retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
758 send_sig(SIGKILL, current, 0);
759 goto out_free_dentry;
762 current->mm->start_stack = bprm->p;
764 /* Now we do a little grungy work by mmaping the ELF image into
765 the correct location in memory. */
766 for(i = 0, elf_ppnt = elf_phdata;
767 i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
768 int elf_prot = 0, elf_flags;
769 unsigned long k, vaddr;
771 if (elf_ppnt->p_type != PT_LOAD)
774 if (unlikely (elf_brk > elf_bss)) {
777 /* There was a PT_LOAD segment with p_memsz > p_filesz
778 before this one. Map anonymous pages, if needed,
779 and clear the area. */
780 retval = set_brk (elf_bss + load_bias,
781 elf_brk + load_bias);
783 send_sig(SIGKILL, current, 0);
784 goto out_free_dentry;
786 nbyte = ELF_PAGEOFFSET(elf_bss);
788 nbyte = ELF_MIN_ALIGN - nbyte;
789 if (nbyte > elf_brk - elf_bss)
790 nbyte = elf_brk - elf_bss;
791 if (clear_user((void __user *)elf_bss +
794 * This bss-zeroing can fail if the ELF
795 * file specifies odd protections. So
796 * we don't check the return value
802 if (elf_ppnt->p_flags & PF_R)
803 elf_prot |= PROT_READ;
804 if (elf_ppnt->p_flags & PF_W)
805 elf_prot |= PROT_WRITE;
806 if (elf_ppnt->p_flags & PF_X)
807 elf_prot |= PROT_EXEC;
809 elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
811 vaddr = elf_ppnt->p_vaddr;
812 if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
813 elf_flags |= MAP_FIXED;
814 } else if (loc->elf_ex.e_type == ET_DYN) {
815 /* Try and get dynamic programs out of the way of the
816 * default mmap base, as well as whatever program they
817 * might try to exec. This is because the brk will
818 * follow the loader, and is not movable. */
822 load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
826 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
827 elf_prot, elf_flags, 0);
828 if (BAD_ADDR(error)) {
829 send_sig(SIGKILL, current, 0);
830 retval = IS_ERR((void *)error) ?
831 PTR_ERR((void*)error) : -EINVAL;
832 goto out_free_dentry;
835 if (!load_addr_set) {
837 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
838 if (loc->elf_ex.e_type == ET_DYN) {
840 ELF_PAGESTART(load_bias + vaddr);
841 load_addr += load_bias;
842 reloc_func_desc = load_bias;
845 k = elf_ppnt->p_vaddr;
852 * Check to see if the section's size will overflow the
853 * allowed task size. Note that p_filesz must always be
854 * <= p_memsz so it is only necessary to check p_memsz.
856 if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
857 elf_ppnt->p_memsz > TASK_SIZE ||
858 TASK_SIZE - elf_ppnt->p_memsz < k) {
859 /* set_brk can never work. Avoid overflows. */
860 send_sig(SIGKILL, current, 0);
862 goto out_free_dentry;
865 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
869 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
873 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
878 loc->elf_ex.e_entry += load_bias;
879 elf_bss += load_bias;
880 elf_brk += load_bias;
881 start_code += load_bias;
882 end_code += load_bias;
883 start_data += load_bias;
884 end_data += load_bias;
886 /* Calling set_brk effectively mmaps the pages that we need
887 * for the bss and break sections. We must do this before
888 * mapping in the interpreter, to make sure it doesn't wind
889 * up getting placed where the bss needs to go.
891 retval = set_brk(elf_bss, elf_brk);
893 send_sig(SIGKILL, current, 0);
894 goto out_free_dentry;
896 if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
897 send_sig(SIGSEGV, current, 0);
898 retval = -EFAULT; /* Nobody gets to see this, but.. */
899 goto out_free_dentry;
902 if (elf_interpreter) {
903 unsigned long uninitialized_var(interp_map_addr);
905 elf_entry = load_elf_interp(&loc->interp_elf_ex,
909 if (!IS_ERR((void *)elf_entry)) {
911 * load_elf_interp() returns relocation
914 interp_load_addr = elf_entry;
915 elf_entry += loc->interp_elf_ex.e_entry;
917 if (BAD_ADDR(elf_entry)) {
918 force_sig(SIGSEGV, current);
919 retval = IS_ERR((void *)elf_entry) ?
920 (int)elf_entry : -EINVAL;
921 goto out_free_dentry;
923 reloc_func_desc = interp_load_addr;
925 allow_write_access(interpreter);
927 kfree(elf_interpreter);
929 elf_entry = loc->elf_ex.e_entry;
930 if (BAD_ADDR(elf_entry)) {
931 force_sig(SIGSEGV, current);
933 goto out_free_dentry;
939 sys_close(elf_exec_fileno);
941 set_binfmt(&elf_format);
943 #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
944 retval = arch_setup_additional_pages(bprm, executable_stack);
946 send_sig(SIGKILL, current, 0);
949 #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
952 current->flags &= ~PF_FORKNOEXEC;
953 retval = create_elf_tables(bprm, &loc->elf_ex,
954 load_addr, interp_load_addr);
956 send_sig(SIGKILL, current, 0);
959 /* N.B. passed_fileno might not be initialized? */
960 current->mm->end_code = end_code;
961 current->mm->start_code = start_code;
962 current->mm->start_data = start_data;
963 current->mm->end_data = end_data;
964 current->mm->start_stack = bprm->p;
966 #ifdef arch_randomize_brk
967 if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1))
968 current->mm->brk = current->mm->start_brk =
969 arch_randomize_brk(current->mm);
972 if (current->personality & MMAP_PAGE_ZERO) {
973 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
974 and some applications "depend" upon this behavior.
975 Since we do not have the power to recompile these, we
976 emulate the SVr4 behavior. Sigh. */
977 down_write(¤t->mm->mmap_sem);
978 error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
979 MAP_FIXED | MAP_PRIVATE, 0);
980 up_write(¤t->mm->mmap_sem);
985 * The ABI may specify that certain registers be set up in special
986 * ways (on i386 %edx is the address of a DT_FINI function, for
987 * example. In addition, it may also specify (eg, PowerPC64 ELF)
988 * that the e_entry field is the address of the function descriptor
989 * for the startup routine, rather than the address of the startup
990 * routine itself. This macro performs whatever initialization to
991 * the regs structure is required as well as any relocations to the
992 * function descriptor entries when executing dynamically links apps.
994 ELF_PLAT_INIT(regs, reloc_func_desc);
997 start_thread(regs, elf_entry, bprm->p);
998 if (unlikely(current->ptrace & PT_PTRACED)) {
999 if (current->ptrace & PT_TRACE_EXEC)
1000 ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
1002 send_sig(SIGTRAP, current, 0);
1012 allow_write_access(interpreter);
1016 kfree(elf_interpreter);
1018 sys_close(elf_exec_fileno);
1021 reset_files_struct(current, files);
1027 /* This is really simpleminded and specialized - we are loading an
1028 a.out library that is given an ELF header. */
1029 static int load_elf_library(struct file *file)
1031 struct elf_phdr *elf_phdata;
1032 struct elf_phdr *eppnt;
1033 unsigned long elf_bss, bss, len;
1034 int retval, error, i, j;
1035 struct elfhdr elf_ex;
1038 retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
1039 if (retval != sizeof(elf_ex))
1042 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
1045 /* First of all, some simple consistency checks */
1046 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
1047 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
1050 /* Now read in all of the header information */
1052 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
1053 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1056 elf_phdata = kmalloc(j, GFP_KERNEL);
1062 retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
1066 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
1067 if ((eppnt + i)->p_type == PT_LOAD)
1072 while (eppnt->p_type != PT_LOAD)
1075 /* Now use mmap to map the library into memory. */
1076 down_write(¤t->mm->mmap_sem);
1077 error = do_mmap(file,
1078 ELF_PAGESTART(eppnt->p_vaddr),
1080 ELF_PAGEOFFSET(eppnt->p_vaddr)),
1081 PROT_READ | PROT_WRITE | PROT_EXEC,
1082 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
1084 ELF_PAGEOFFSET(eppnt->p_vaddr)));
1085 up_write(¤t->mm->mmap_sem);
1086 if (error != ELF_PAGESTART(eppnt->p_vaddr))
1089 elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
1090 if (padzero(elf_bss)) {
1095 len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr +
1097 bss = eppnt->p_memsz + eppnt->p_vaddr;
1099 down_write(¤t->mm->mmap_sem);
1100 do_brk(len, bss - len);
1101 up_write(¤t->mm->mmap_sem);
1112 * Note that some platforms still use traditional core dumps and not
1113 * the ELF core dump. Each platform can select it as appropriate.
1115 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1120 * Modelled on fs/exec.c:aout_core_dump()
1121 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1124 * These are the only things you should do on a core-file: use only these
1125 * functions to write out all the necessary info.
1127 static int dump_write(struct file *file, const void *addr, int nr)
1129 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1132 static int dump_seek(struct file *file, loff_t off)
1134 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
1135 if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
1138 char *buf = (char *)get_zeroed_page(GFP_KERNEL);
1142 unsigned long n = off;
1145 if (!dump_write(file, buf, n))
1149 free_page((unsigned long)buf);
1155 * Decide what to dump of a segment, part, all or none.
1157 static unsigned long vma_dump_size(struct vm_area_struct *vma,
1158 unsigned long mm_flags)
1160 /* The vma can be set up to tell us the answer directly. */
1161 if (vma->vm_flags & VM_ALWAYSDUMP)
1164 /* Do not dump I/O mapped devices or special mappings */
1165 if (vma->vm_flags & (VM_IO | VM_RESERVED))
1168 #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1170 /* By default, dump shared memory if mapped from an anonymous file. */
1171 if (vma->vm_flags & VM_SHARED) {
1172 if (vma->vm_file->f_path.dentry->d_inode->i_nlink == 0 ?
1173 FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
1178 /* Dump segments that have been written to. */
1179 if (vma->anon_vma && FILTER(ANON_PRIVATE))
1181 if (vma->vm_file == NULL)
1184 if (FILTER(MAPPED_PRIVATE))
1188 * If this looks like the beginning of a DSO or executable mapping,
1189 * check for an ELF header. If we find one, dump the first page to
1190 * aid in determining what was mapped here.
1192 if (FILTER(ELF_HEADERS) && vma->vm_file != NULL && vma->vm_pgoff == 0) {
1193 u32 __user *header = (u32 __user *) vma->vm_start;
1196 * Doing it this way gets the constant folded by GCC.
1200 char elfmag[SELFMAG];
1202 BUILD_BUG_ON(SELFMAG != sizeof word);
1203 magic.elfmag[EI_MAG0] = ELFMAG0;
1204 magic.elfmag[EI_MAG1] = ELFMAG1;
1205 magic.elfmag[EI_MAG2] = ELFMAG2;
1206 magic.elfmag[EI_MAG3] = ELFMAG3;
1207 if (get_user(word, header) == 0 && word == magic.cmp)
1216 return vma->vm_end - vma->vm_start;
1219 /* An ELF note in memory */
1224 unsigned int datasz;
1228 static int notesize(struct memelfnote *en)
1232 sz = sizeof(struct elf_note);
1233 sz += roundup(strlen(en->name) + 1, 4);
1234 sz += roundup(en->datasz, 4);
1239 #define DUMP_WRITE(addr, nr, foffset) \
1240 do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
1242 static int alignfile(struct file *file, loff_t *foffset)
1244 static const char buf[4] = { 0, };
1245 DUMP_WRITE(buf, roundup(*foffset, 4) - *foffset, foffset);
1249 static int writenote(struct memelfnote *men, struct file *file,
1253 en.n_namesz = strlen(men->name) + 1;
1254 en.n_descsz = men->datasz;
1255 en.n_type = men->type;
1257 DUMP_WRITE(&en, sizeof(en), foffset);
1258 DUMP_WRITE(men->name, en.n_namesz, foffset);
1259 if (!alignfile(file, foffset))
1261 DUMP_WRITE(men->data, men->datasz, foffset);
1262 if (!alignfile(file, foffset))
1269 #define DUMP_WRITE(addr, nr) \
1270 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1272 #define DUMP_SEEK(off) \
1273 if (!dump_seek(file, (off))) \
1276 static void fill_elf_header(struct elfhdr *elf, int segs,
1277 u16 machine, u32 flags, u8 osabi)
1279 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1280 elf->e_ident[EI_CLASS] = ELF_CLASS;
1281 elf->e_ident[EI_DATA] = ELF_DATA;
1282 elf->e_ident[EI_VERSION] = EV_CURRENT;
1283 elf->e_ident[EI_OSABI] = ELF_OSABI;
1284 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1286 elf->e_type = ET_CORE;
1287 elf->e_machine = machine;
1288 elf->e_version = EV_CURRENT;
1290 elf->e_phoff = sizeof(struct elfhdr);
1292 elf->e_flags = flags;
1293 elf->e_ehsize = sizeof(struct elfhdr);
1294 elf->e_phentsize = sizeof(struct elf_phdr);
1295 elf->e_phnum = segs;
1296 elf->e_shentsize = 0;
1298 elf->e_shstrndx = 0;
1302 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
1304 phdr->p_type = PT_NOTE;
1305 phdr->p_offset = offset;
1308 phdr->p_filesz = sz;
1315 static void fill_note(struct memelfnote *note, const char *name, int type,
1316 unsigned int sz, void *data)
1326 * fill up all the fields in prstatus from the given task struct, except
1327 * registers which need to be filled up separately.
1329 static void fill_prstatus(struct elf_prstatus *prstatus,
1330 struct task_struct *p, long signr)
1332 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1333 prstatus->pr_sigpend = p->pending.signal.sig[0];
1334 prstatus->pr_sighold = p->blocked.sig[0];
1335 prstatus->pr_pid = task_pid_vnr(p);
1336 prstatus->pr_ppid = task_pid_vnr(p->real_parent);
1337 prstatus->pr_pgrp = task_pgrp_vnr(p);
1338 prstatus->pr_sid = task_session_vnr(p);
1339 if (thread_group_leader(p)) {
1341 * This is the record for the group leader. Add in the
1342 * cumulative times of previous dead threads. This total
1343 * won't include the time of each live thread whose state
1344 * is included in the core dump. The final total reported
1345 * to our parent process when it calls wait4 will include
1346 * those sums as well as the little bit more time it takes
1347 * this and each other thread to finish dying after the
1348 * core dump synchronization phase.
1350 cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
1351 &prstatus->pr_utime);
1352 cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
1353 &prstatus->pr_stime);
1355 cputime_to_timeval(p->utime, &prstatus->pr_utime);
1356 cputime_to_timeval(p->stime, &prstatus->pr_stime);
1358 cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
1359 cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
1362 static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1363 struct mm_struct *mm)
1365 unsigned int i, len;
1367 /* first copy the parameters from user space */
1368 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1370 len = mm->arg_end - mm->arg_start;
1371 if (len >= ELF_PRARGSZ)
1372 len = ELF_PRARGSZ-1;
1373 if (copy_from_user(&psinfo->pr_psargs,
1374 (const char __user *)mm->arg_start, len))
1376 for(i = 0; i < len; i++)
1377 if (psinfo->pr_psargs[i] == 0)
1378 psinfo->pr_psargs[i] = ' ';
1379 psinfo->pr_psargs[len] = 0;
1381 psinfo->pr_pid = task_pid_vnr(p);
1382 psinfo->pr_ppid = task_pid_vnr(p->real_parent);
1383 psinfo->pr_pgrp = task_pgrp_vnr(p);
1384 psinfo->pr_sid = task_session_vnr(p);
1386 i = p->state ? ffz(~p->state) + 1 : 0;
1387 psinfo->pr_state = i;
1388 psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
1389 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1390 psinfo->pr_nice = task_nice(p);
1391 psinfo->pr_flag = p->flags;
1392 SET_UID(psinfo->pr_uid, p->uid);
1393 SET_GID(psinfo->pr_gid, p->gid);
1394 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1399 static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)
1401 elf_addr_t *auxv = (elf_addr_t *) mm->saved_auxv;
1405 while (auxv[i - 2] != AT_NULL);
1406 fill_note(note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv);
1409 #ifdef CORE_DUMP_USE_REGSET
1410 #include <linux/regset.h>
1412 struct elf_thread_core_info {
1413 struct elf_thread_core_info *next;
1414 struct task_struct *task;
1415 struct elf_prstatus prstatus;
1416 struct memelfnote notes[0];
1419 struct elf_note_info {
1420 struct elf_thread_core_info *thread;
1421 struct memelfnote psinfo;
1422 struct memelfnote auxv;
1427 static int fill_thread_core_info(struct elf_thread_core_info *t,
1428 const struct user_regset_view *view,
1429 long signr, size_t *total)
1434 * NT_PRSTATUS is the one special case, because the regset data
1435 * goes into the pr_reg field inside the note contents, rather
1436 * than being the whole note contents. We fill the reset in here.
1437 * We assume that regset 0 is NT_PRSTATUS.
1439 fill_prstatus(&t->prstatus, t->task, signr);
1440 (void) view->regsets[0].get(t->task, &view->regsets[0],
1441 0, sizeof(t->prstatus.pr_reg),
1442 &t->prstatus.pr_reg, NULL);
1444 fill_note(&t->notes[0], "CORE", NT_PRSTATUS,
1445 sizeof(t->prstatus), &t->prstatus);
1446 *total += notesize(&t->notes[0]);
1449 * Each other regset might generate a note too. For each regset
1450 * that has no core_note_type or is inactive, we leave t->notes[i]
1451 * all zero and we'll know to skip writing it later.
1453 for (i = 1; i < view->n; ++i) {
1454 const struct user_regset *regset = &view->regsets[i];
1455 if (regset->core_note_type &&
1456 (!regset->active || regset->active(t->task, regset))) {
1458 size_t size = regset->n * regset->size;
1459 void *data = kmalloc(size, GFP_KERNEL);
1460 if (unlikely(!data))
1462 ret = regset->get(t->task, regset,
1463 0, size, data, NULL);
1467 if (regset->core_note_type != NT_PRFPREG)
1468 fill_note(&t->notes[i], "LINUX",
1469 regset->core_note_type,
1472 t->prstatus.pr_fpvalid = 1;
1473 fill_note(&t->notes[i], "CORE",
1474 NT_PRFPREG, size, data);
1476 *total += notesize(&t->notes[i]);
1484 static int fill_note_info(struct elfhdr *elf, int phdrs,
1485 struct elf_note_info *info,
1486 long signr, struct pt_regs *regs)
1488 struct task_struct *dump_task = current;
1489 const struct user_regset_view *view = task_user_regset_view(dump_task);
1490 struct elf_thread_core_info *t;
1491 struct elf_prpsinfo *psinfo;
1492 struct task_struct *g, *p;
1496 info->thread = NULL;
1498 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1499 fill_note(&info->psinfo, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1505 * Figure out how many notes we're going to need for each thread.
1507 info->thread_notes = 0;
1508 for (i = 0; i < view->n; ++i)
1509 if (view->regsets[i].core_note_type != 0)
1510 ++info->thread_notes;
1513 * Sanity check. We rely on regset 0 being in NT_PRSTATUS,
1514 * since it is our one special case.
1516 if (unlikely(info->thread_notes == 0) ||
1517 unlikely(view->regsets[0].core_note_type != NT_PRSTATUS)) {
1523 * Initialize the ELF file header.
1525 fill_elf_header(elf, phdrs,
1526 view->e_machine, view->e_flags, view->ei_osabi);
1529 * Allocate a structure for each thread.
1532 do_each_thread(g, p)
1533 if (p->mm == dump_task->mm) {
1534 t = kzalloc(offsetof(struct elf_thread_core_info,
1535 notes[info->thread_notes]),
1542 if (p == dump_task || !info->thread) {
1543 t->next = info->thread;
1547 * Make sure to keep the original task at
1548 * the head of the list.
1550 t->next = info->thread->next;
1551 info->thread->next = t;
1554 while_each_thread(g, p);
1558 * Now fill in each thread's information.
1560 for (t = info->thread; t != NULL; t = t->next)
1561 if (!fill_thread_core_info(t, view, signr, &info->size))
1565 * Fill in the two process-wide notes.
1567 fill_psinfo(psinfo, dump_task->group_leader, dump_task->mm);
1568 info->size += notesize(&info->psinfo);
1570 fill_auxv_note(&info->auxv, current->mm);
1571 info->size += notesize(&info->auxv);
1576 static size_t get_note_info_size(struct elf_note_info *info)
1582 * Write all the notes for each thread. When writing the first thread, the
1583 * process-wide notes are interleaved after the first thread-specific note.
1585 static int write_note_info(struct elf_note_info *info,
1586 struct file *file, loff_t *foffset)
1589 struct elf_thread_core_info *t = info->thread;
1594 if (!writenote(&t->notes[0], file, foffset))
1597 if (first && !writenote(&info->psinfo, file, foffset))
1599 if (first && !writenote(&info->auxv, file, foffset))
1602 for (i = 1; i < info->thread_notes; ++i)
1603 if (t->notes[i].data &&
1604 !writenote(&t->notes[i], file, foffset))
1614 static void free_note_info(struct elf_note_info *info)
1616 struct elf_thread_core_info *threads = info->thread;
1619 struct elf_thread_core_info *t = threads;
1621 WARN_ON(t->notes[0].data && t->notes[0].data != &t->prstatus);
1622 for (i = 1; i < info->thread_notes; ++i)
1623 kfree(t->notes[i].data);
1626 kfree(info->psinfo.data);
1631 /* Here is the structure in which status of each thread is captured. */
1632 struct elf_thread_status
1634 struct list_head list;
1635 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1636 elf_fpregset_t fpu; /* NT_PRFPREG */
1637 struct task_struct *thread;
1638 #ifdef ELF_CORE_COPY_XFPREGS
1639 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
1641 struct memelfnote notes[3];
1646 * In order to add the specific thread information for the elf file format,
1647 * we need to keep a linked list of every threads pr_status and then create
1648 * a single section for them in the final core file.
1650 static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1653 struct task_struct *p = t->thread;
1656 fill_prstatus(&t->prstatus, p, signr);
1657 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1659 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
1662 sz += notesize(&t->notes[0]);
1664 if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
1666 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
1669 sz += notesize(&t->notes[1]);
1672 #ifdef ELF_CORE_COPY_XFPREGS
1673 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
1674 fill_note(&t->notes[2], "LINUX", ELF_CORE_XFPREG_TYPE,
1675 sizeof(t->xfpu), &t->xfpu);
1677 sz += notesize(&t->notes[2]);
1683 struct elf_note_info {
1684 struct memelfnote *notes;
1685 struct elf_prstatus *prstatus; /* NT_PRSTATUS */
1686 struct elf_prpsinfo *psinfo; /* NT_PRPSINFO */
1687 struct list_head thread_list;
1688 elf_fpregset_t *fpu;
1689 #ifdef ELF_CORE_COPY_XFPREGS
1690 elf_fpxregset_t *xfpu;
1692 int thread_status_size;
1696 static int fill_note_info(struct elfhdr *elf, int phdrs,
1697 struct elf_note_info *info,
1698 long signr, struct pt_regs *regs)
1701 struct list_head *t;
1702 struct task_struct *g, *p;
1705 info->prstatus = NULL;
1706 info->psinfo = NULL;
1708 #ifdef ELF_CORE_COPY_XFPREGS
1711 INIT_LIST_HEAD(&info->thread_list);
1713 info->notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote),
1717 info->psinfo = kmalloc(sizeof(*info->psinfo), GFP_KERNEL);
1720 info->prstatus = kmalloc(sizeof(*info->prstatus), GFP_KERNEL);
1721 if (!info->prstatus)
1723 info->fpu = kmalloc(sizeof(*info->fpu), GFP_KERNEL);
1726 #ifdef ELF_CORE_COPY_XFPREGS
1727 info->xfpu = kmalloc(sizeof(*info->xfpu), GFP_KERNEL);
1732 info->thread_status_size = 0;
1734 struct elf_thread_status *tmp;
1736 do_each_thread(g, p)
1737 if (current->mm == p->mm && current != p) {
1738 tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
1744 list_add(&tmp->list, &info->thread_list);
1746 while_each_thread(g, p);
1748 list_for_each(t, &info->thread_list) {
1749 struct elf_thread_status *tmp;
1752 tmp = list_entry(t, struct elf_thread_status, list);
1753 sz = elf_dump_thread_status(signr, tmp);
1754 info->thread_status_size += sz;
1757 /* now collect the dump for the current */
1758 memset(info->prstatus, 0, sizeof(*info->prstatus));
1759 fill_prstatus(info->prstatus, current, signr);
1760 elf_core_copy_regs(&info->prstatus->pr_reg, regs);
1763 fill_elf_header(elf, phdrs, ELF_ARCH, ELF_CORE_EFLAGS, ELF_OSABI);
1766 * Set up the notes in similar form to SVR4 core dumps made
1767 * with info from their /proc.
1770 fill_note(info->notes + 0, "CORE", NT_PRSTATUS,
1771 sizeof(*info->prstatus), info->prstatus);
1772 fill_psinfo(info->psinfo, current->group_leader, current->mm);
1773 fill_note(info->notes + 1, "CORE", NT_PRPSINFO,
1774 sizeof(*info->psinfo), info->psinfo);
1778 fill_auxv_note(&info->notes[info->numnote++], current->mm);
1780 /* Try to dump the FPU. */
1781 info->prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, regs,
1783 if (info->prstatus->pr_fpvalid)
1784 fill_note(info->notes + info->numnote++,
1785 "CORE", NT_PRFPREG, sizeof(*info->fpu), info->fpu);
1786 #ifdef ELF_CORE_COPY_XFPREGS
1787 if (elf_core_copy_task_xfpregs(current, info->xfpu))
1788 fill_note(info->notes + info->numnote++,
1789 "LINUX", ELF_CORE_XFPREG_TYPE,
1790 sizeof(*info->xfpu), info->xfpu);
1798 static size_t get_note_info_size(struct elf_note_info *info)
1803 for (i = 0; i < info->numnote; i++)
1804 sz += notesize(info->notes + i);
1806 sz += info->thread_status_size;
1811 static int write_note_info(struct elf_note_info *info,
1812 struct file *file, loff_t *foffset)
1815 struct list_head *t;
1817 for (i = 0; i < info->numnote; i++)
1818 if (!writenote(info->notes + i, file, foffset))
1821 /* write out the thread status notes section */
1822 list_for_each(t, &info->thread_list) {
1823 struct elf_thread_status *tmp =
1824 list_entry(t, struct elf_thread_status, list);
1826 for (i = 0; i < tmp->num_notes; i++)
1827 if (!writenote(&tmp->notes[i], file, foffset))
1834 static void free_note_info(struct elf_note_info *info)
1836 while (!list_empty(&info->thread_list)) {
1837 struct list_head *tmp = info->thread_list.next;
1839 kfree(list_entry(tmp, struct elf_thread_status, list));
1842 kfree(info->prstatus);
1843 kfree(info->psinfo);
1846 #ifdef ELF_CORE_COPY_XFPREGS
1853 static struct vm_area_struct *first_vma(struct task_struct *tsk,
1854 struct vm_area_struct *gate_vma)
1856 struct vm_area_struct *ret = tsk->mm->mmap;
1863 * Helper function for iterating across a vma list. It ensures that the caller
1864 * will visit `gate_vma' prior to terminating the search.
1866 static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
1867 struct vm_area_struct *gate_vma)
1869 struct vm_area_struct *ret;
1871 ret = this_vma->vm_next;
1874 if (this_vma == gate_vma)
1882 * This is a two-pass process; first we find the offsets of the bits,
1883 * and then they are actually written out. If we run out of core limit
1886 static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit)
1892 struct vm_area_struct *vma, *gate_vma;
1893 struct elfhdr *elf = NULL;
1894 loff_t offset = 0, dataoff, foffset;
1895 unsigned long mm_flags;
1896 struct elf_note_info info;
1899 * We no longer stop all VM operations.
1901 * This is because those proceses that could possibly change map_count
1902 * or the mmap / vma pages are now blocked in do_exit on current
1903 * finishing this core dump.
1905 * Only ptrace can touch these memory addresses, but it doesn't change
1906 * the map_count or the pages allocated. So no possibility of crashing
1907 * exists while dumping the mm->vm_next areas to the core file.
1910 /* alloc memory for large data structures: too large to be on stack */
1911 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1915 segs = current->mm->map_count;
1916 #ifdef ELF_CORE_EXTRA_PHDRS
1917 segs += ELF_CORE_EXTRA_PHDRS;
1920 gate_vma = get_gate_vma(current);
1921 if (gate_vma != NULL)
1925 * Collect all the non-memory information about the process for the
1926 * notes. This also sets up the file header.
1928 if (!fill_note_info(elf, segs + 1, /* including notes section */
1929 &info, signr, regs))
1933 current->flags |= PF_DUMPCORE;
1938 DUMP_WRITE(elf, sizeof(*elf));
1939 offset += sizeof(*elf); /* Elf header */
1940 offset += (segs + 1) * sizeof(struct elf_phdr); /* Program headers */
1943 /* Write notes phdr entry */
1945 struct elf_phdr phdr;
1946 size_t sz = get_note_info_size(&info);
1948 sz += elf_coredump_extra_notes_size();
1950 fill_elf_note_phdr(&phdr, sz, offset);
1952 DUMP_WRITE(&phdr, sizeof(phdr));
1955 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1958 * We must use the same mm->flags while dumping core to avoid
1959 * inconsistency between the program headers and bodies, otherwise an
1960 * unusable core file can be generated.
1962 mm_flags = current->mm->flags;
1964 /* Write program headers for segments dump */
1965 for (vma = first_vma(current, gate_vma); vma != NULL;
1966 vma = next_vma(vma, gate_vma)) {
1967 struct elf_phdr phdr;
1969 phdr.p_type = PT_LOAD;
1970 phdr.p_offset = offset;
1971 phdr.p_vaddr = vma->vm_start;
1973 phdr.p_filesz = vma_dump_size(vma, mm_flags);
1974 phdr.p_memsz = vma->vm_end - vma->vm_start;
1975 offset += phdr.p_filesz;
1976 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1977 if (vma->vm_flags & VM_WRITE)
1978 phdr.p_flags |= PF_W;
1979 if (vma->vm_flags & VM_EXEC)
1980 phdr.p_flags |= PF_X;
1981 phdr.p_align = ELF_EXEC_PAGESIZE;
1983 DUMP_WRITE(&phdr, sizeof(phdr));
1986 #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1987 ELF_CORE_WRITE_EXTRA_PHDRS;
1990 /* write out the notes section */
1991 if (!write_note_info(&info, file, &foffset))
1994 if (elf_coredump_extra_notes_write(file, &foffset))
1998 DUMP_SEEK(dataoff - foffset);
2000 for (vma = first_vma(current, gate_vma); vma != NULL;
2001 vma = next_vma(vma, gate_vma)) {
2005 end = vma->vm_start + vma_dump_size(vma, mm_flags);
2007 for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) {
2009 struct vm_area_struct *vma;
2011 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
2012 &page, &vma) <= 0) {
2013 DUMP_SEEK(PAGE_SIZE);
2015 if (page == ZERO_PAGE(0)) {
2016 if (!dump_seek(file, PAGE_SIZE)) {
2017 page_cache_release(page);
2022 flush_cache_page(vma, addr,
2025 if ((size += PAGE_SIZE) > limit ||
2026 !dump_write(file, kaddr,
2029 page_cache_release(page);
2034 page_cache_release(page);
2039 #ifdef ELF_CORE_WRITE_EXTRA_DATA
2040 ELF_CORE_WRITE_EXTRA_DATA;
2048 free_note_info(&info);
2052 #endif /* USE_ELF_CORE_DUMP */
2054 static int __init init_elf_binfmt(void)
2056 return register_binfmt(&elf_format);
2059 static void __exit exit_elf_binfmt(void)
2061 /* Remove the COFF and ELF loaders. */
2062 unregister_binfmt(&elf_format);
2065 core_initcall(init_elf_binfmt);
2066 module_exit(exit_elf_binfmt);
2067 MODULE_LICENSE("GPL");