1 /* binfmt_elf_fdpic.c: FDPIC ELF binary format
3 * Copyright (C) 2003, 2004, 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * Derived from binfmt_elf.c
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/module.h>
16 #include <linux/stat.h>
17 #include <linux/sched.h>
19 #include <linux/mman.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/slab.h>
27 #include <linux/pagemap.h>
28 #include <linux/highmem.h>
29 #include <linux/highuid.h>
30 #include <linux/personality.h>
31 #include <linux/ptrace.h>
32 #include <linux/init.h>
33 #include <linux/smp_lock.h>
34 #include <linux/elf.h>
35 #include <linux/elf-fdpic.h>
36 #include <linux/elfcore.h>
37 #include <linux/vs_base.h>
38 #include <linux/vs_cvirt.h>
40 #include <asm/uaccess.h>
41 #include <asm/param.h>
42 #include <asm/pgalloc.h>
44 typedef char *elf_caddr_t;
46 #define elf_addr_t unsigned long
50 #define kdebug(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
52 #define kdebug(fmt, ...) do {} while(0)
56 #define kdcore(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
58 #define kdcore(fmt, ...) do {} while(0)
61 MODULE_LICENSE("GPL");
63 static int load_elf_fdpic_binary(struct linux_binprm *, struct pt_regs *);
64 static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *, struct file *);
65 static int elf_fdpic_map_file(struct elf_fdpic_params *, struct file *,
66 struct mm_struct *, const char *);
68 static int create_elf_fdpic_tables(struct linux_binprm *, struct mm_struct *,
69 struct elf_fdpic_params *,
70 struct elf_fdpic_params *);
73 static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *,
75 static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *,
80 static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *,
81 struct file *, struct mm_struct *);
83 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
84 static int elf_fdpic_core_dump(long, struct pt_regs *, struct file *);
87 static struct linux_binfmt elf_fdpic_format = {
88 .module = THIS_MODULE,
89 .load_binary = load_elf_fdpic_binary,
90 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
91 .core_dump = elf_fdpic_core_dump,
93 .min_coredump = ELF_EXEC_PAGESIZE,
96 static int __init init_elf_fdpic_binfmt(void)
98 return register_binfmt(&elf_fdpic_format);
101 static void __exit exit_elf_fdpic_binfmt(void)
103 unregister_binfmt(&elf_fdpic_format);
106 core_initcall(init_elf_fdpic_binfmt);
107 module_exit(exit_elf_fdpic_binfmt);
109 static int is_elf_fdpic(struct elfhdr *hdr, struct file *file)
111 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
113 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
115 if (!elf_check_arch(hdr) || !elf_check_fdpic(hdr))
117 if (!file->f_op || !file->f_op->mmap)
122 /*****************************************************************************/
124 * read the program headers table into memory
126 static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,
129 struct elf32_phdr *phdr;
133 if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
135 if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr))
138 size = params->hdr.e_phnum * sizeof(struct elf_phdr);
139 params->phdrs = kmalloc(size, GFP_KERNEL);
143 retval = kernel_read(file, params->hdr.e_phoff,
144 (char *) params->phdrs, size);
148 /* determine stack size for this binary */
149 phdr = params->phdrs;
150 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
151 if (phdr->p_type != PT_GNU_STACK)
154 if (phdr->p_flags & PF_X)
155 params->flags |= ELF_FDPIC_FLAG_EXEC_STACK;
157 params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK;
159 params->stack_size = phdr->p_memsz;
166 /*****************************************************************************/
168 * load an fdpic binary into various bits of memory
170 static int load_elf_fdpic_binary(struct linux_binprm *bprm,
171 struct pt_regs *regs)
173 struct elf_fdpic_params exec_params, interp_params;
174 struct elf_phdr *phdr;
175 unsigned long stack_size, entryaddr;
177 unsigned long fullsize;
179 #ifdef ELF_FDPIC_PLAT_INIT
180 unsigned long dynaddr;
182 struct file *interpreter = NULL; /* to shut gcc up */
183 char *interpreter_name = NULL;
184 int executable_stack;
187 memset(&exec_params, 0, sizeof(exec_params));
188 memset(&interp_params, 0, sizeof(interp_params));
190 exec_params.hdr = *(struct elfhdr *) bprm->buf;
191 exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE;
193 /* check that this is a binary we know how to deal with */
195 if (!is_elf_fdpic(&exec_params.hdr, bprm->file))
198 /* read the program header table */
199 retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
203 /* scan for a program header that specifies an interpreter */
204 phdr = exec_params.phdrs;
206 for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) {
207 switch (phdr->p_type) {
210 if (phdr->p_filesz > PATH_MAX)
213 if (phdr->p_filesz < 2)
216 /* read the name of the interpreter into memory */
217 interpreter_name = kmalloc(phdr->p_filesz, GFP_KERNEL);
218 if (!interpreter_name)
221 retval = kernel_read(bprm->file,
229 if (interpreter_name[phdr->p_filesz - 1] != '\0')
232 kdebug("Using ELF interpreter %s", interpreter_name);
234 /* replace the program with the interpreter */
235 interpreter = open_exec(interpreter_name);
236 retval = PTR_ERR(interpreter);
237 if (IS_ERR(interpreter)) {
242 retval = kernel_read(interpreter, 0, bprm->buf,
247 interp_params.hdr = *((struct elfhdr *) bprm->buf);
252 if (exec_params.load_addr == 0)
253 exec_params.load_addr = phdr->p_vaddr;
260 if (elf_check_const_displacement(&exec_params.hdr))
261 exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
263 /* perform insanity checks on the interpreter */
264 if (interpreter_name) {
266 if (!is_elf_fdpic(&interp_params.hdr, interpreter))
269 interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
271 /* read the interpreter's program header table */
272 retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
277 stack_size = exec_params.stack_size;
278 if (stack_size < interp_params.stack_size)
279 stack_size = interp_params.stack_size;
281 if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
282 executable_stack = EXSTACK_ENABLE_X;
283 else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
284 executable_stack = EXSTACK_DISABLE_X;
285 else if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
286 executable_stack = EXSTACK_ENABLE_X;
287 else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
288 executable_stack = EXSTACK_DISABLE_X;
290 executable_stack = EXSTACK_DEFAULT;
296 if (elf_check_const_displacement(&interp_params.hdr))
297 interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
299 /* flush all traces of the currently running executable */
300 retval = flush_old_exec(bprm);
304 /* there's now no turning back... the old userspace image is dead,
305 * defunct, deceased, etc. after this point we have to exit via
307 set_personality(PER_LINUX_FDPIC);
308 set_binfmt(&elf_fdpic_format);
310 current->mm->start_code = 0;
311 current->mm->end_code = 0;
312 current->mm->start_stack = 0;
313 current->mm->start_data = 0;
314 current->mm->end_data = 0;
315 current->mm->context.exec_fdpic_loadmap = 0;
316 current->mm->context.interp_fdpic_loadmap = 0;
318 current->flags &= ~PF_FORKNOEXEC;
321 elf_fdpic_arch_lay_out_mm(&exec_params,
323 ¤t->mm->start_stack,
324 ¤t->mm->start_brk);
326 retval = setup_arg_pages(bprm, current->mm->start_stack,
329 send_sig(SIGKILL, current, 0);
334 /* load the executable and interpreter into memory */
335 retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm,
340 if (interpreter_name) {
341 retval = elf_fdpic_map_file(&interp_params, interpreter,
342 current->mm, "interpreter");
344 printk(KERN_ERR "Unable to load interpreter\n");
348 allow_write_access(interpreter);
354 if (!current->mm->start_brk)
355 current->mm->start_brk = current->mm->end_data;
357 current->mm->brk = current->mm->start_brk =
358 PAGE_ALIGN(current->mm->start_brk);
361 /* create a stack and brk area big enough for everyone
362 * - the brk heap starts at the bottom and works up
363 * - the stack starts at the top and works down
365 stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
366 if (stack_size < PAGE_SIZE * 2)
367 stack_size = PAGE_SIZE * 2;
369 down_write(¤t->mm->mmap_sem);
370 current->mm->start_brk = do_mmap(NULL, 0, stack_size,
371 PROT_READ | PROT_WRITE | PROT_EXEC,
372 MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN,
375 if (IS_ERR_VALUE(current->mm->start_brk)) {
376 up_write(¤t->mm->mmap_sem);
377 retval = current->mm->start_brk;
378 current->mm->start_brk = 0;
382 /* expand the stack mapping to use up the entire allocation granule */
383 fullsize = ksize((char *) current->mm->start_brk);
384 if (!IS_ERR_VALUE(do_mremap(current->mm->start_brk, stack_size,
386 stack_size = fullsize;
387 up_write(¤t->mm->mmap_sem);
389 current->mm->brk = current->mm->start_brk;
390 current->mm->context.end_brk = current->mm->start_brk;
391 current->mm->context.end_brk +=
392 (stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0;
393 current->mm->start_stack = current->mm->start_brk + stack_size;
397 current->flags &= ~PF_FORKNOEXEC;
398 if (create_elf_fdpic_tables(bprm, current->mm,
399 &exec_params, &interp_params) < 0)
402 kdebug("- start_code %lx", current->mm->start_code);
403 kdebug("- end_code %lx", current->mm->end_code);
404 kdebug("- start_data %lx", current->mm->start_data);
405 kdebug("- end_data %lx", current->mm->end_data);
406 kdebug("- start_brk %lx", current->mm->start_brk);
407 kdebug("- brk %lx", current->mm->brk);
408 kdebug("- start_stack %lx", current->mm->start_stack);
410 #ifdef ELF_FDPIC_PLAT_INIT
412 * The ABI may specify that certain registers be set up in special
413 * ways (on i386 %edx is the address of a DT_FINI function, for
414 * example. This macro performs whatever initialization to
415 * the regs structure is required.
417 dynaddr = interp_params.dynamic_addr ?: exec_params.dynamic_addr;
418 ELF_FDPIC_PLAT_INIT(regs, exec_params.map_addr, interp_params.map_addr,
422 /* everything is now ready... get the userspace context ready to roll */
423 entryaddr = interp_params.entry_addr ?: exec_params.entry_addr;
424 start_thread(regs, entryaddr, current->mm->start_stack);
426 if (unlikely(current->ptrace & PT_PTRACED)) {
427 if (current->ptrace & PT_TRACE_EXEC)
428 ptrace_notify((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
430 send_sig(SIGTRAP, current, 0);
437 allow_write_access(interpreter);
440 kfree(interpreter_name);
441 kfree(exec_params.phdrs);
442 kfree(exec_params.loadmap);
443 kfree(interp_params.phdrs);
444 kfree(interp_params.loadmap);
447 /* unrecoverable error - kill the process */
449 send_sig(SIGSEGV, current, 0);
454 /*****************************************************************************/
456 * present useful information to the program
458 static int create_elf_fdpic_tables(struct linux_binprm *bprm,
459 struct mm_struct *mm,
460 struct elf_fdpic_params *exec_params,
461 struct elf_fdpic_params *interp_params)
463 unsigned long sp, csp, nitems;
464 elf_caddr_t __user *argv, *envp;
465 size_t platform_len = 0, len;
467 char __user *u_platform, *p;
471 /* we're going to shovel a whole load of stuff onto the stack */
475 sp = mm->start_stack;
477 /* stack the program arguments and environment */
478 if (elf_fdpic_transfer_args_to_stack(bprm, &sp) < 0)
482 /* get hold of platform and hardware capabilities masks for the machine
483 * we are running on. In some cases (Sparc), this info is impossible
484 * to get, in others (i386) it is merely difficult.
487 k_platform = ELF_PLATFORM;
491 platform_len = strlen(k_platform) + 1;
493 u_platform = (char __user *) sp;
494 if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
498 #if defined(__i386__) && defined(CONFIG_SMP)
499 /* in some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
500 * by the processes running on the same package. One thing we can do is
501 * to shuffle the initial stack for them.
503 * the conditionals here are unneeded, but kept in to make the code
504 * behaviour the same as pre change unless we have hyperthreaded
505 * processors. This keeps Mr Marcelo Person happier but should be
508 if (smp_num_siblings > 1)
509 sp = sp - ((current->pid % 64) << 7);
514 /* stack the load map(s) */
515 len = sizeof(struct elf32_fdpic_loadmap);
516 len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
517 sp = (sp - len) & ~7UL;
518 exec_params->map_addr = sp;
520 if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
523 current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
525 if (interp_params->loadmap) {
526 len = sizeof(struct elf32_fdpic_loadmap);
527 len += sizeof(struct elf32_fdpic_loadseg) *
528 interp_params->loadmap->nsegs;
529 sp = (sp - len) & ~7UL;
530 interp_params->map_addr = sp;
532 if (copy_to_user((void __user *) sp, interp_params->loadmap,
536 current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
539 /* force 16 byte _final_ alignment here for generality */
540 #define DLINFO_ITEMS 13
542 nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0);
543 #ifdef DLINFO_ARCH_ITEMS
544 nitems += DLINFO_ARCH_ITEMS;
548 sp -= nitems * 2 * sizeof(unsigned long);
549 sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
550 sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
551 sp -= 1 * sizeof(unsigned long); /* argc */
556 /* put the ELF interpreter info on the stack */
557 #define NEW_AUX_ENT(nr, id, val) \
559 struct { unsigned long _id, _val; } __user *ent; \
561 ent = (void __user *) csp; \
562 __put_user((id), &ent[nr]._id); \
563 __put_user((val), &ent[nr]._val); \
566 csp -= 2 * sizeof(unsigned long);
567 NEW_AUX_ENT(0, AT_NULL, 0);
569 csp -= 2 * sizeof(unsigned long);
570 NEW_AUX_ENT(0, AT_PLATFORM,
571 (elf_addr_t) (unsigned long) u_platform);
574 csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
575 NEW_AUX_ENT( 0, AT_HWCAP, hwcap);
576 NEW_AUX_ENT( 1, AT_PAGESZ, PAGE_SIZE);
577 NEW_AUX_ENT( 2, AT_CLKTCK, CLOCKS_PER_SEC);
578 NEW_AUX_ENT( 3, AT_PHDR, exec_params->ph_addr);
579 NEW_AUX_ENT( 4, AT_PHENT, sizeof(struct elf_phdr));
580 NEW_AUX_ENT( 5, AT_PHNUM, exec_params->hdr.e_phnum);
581 NEW_AUX_ENT( 6, AT_BASE, interp_params->elfhdr_addr);
582 NEW_AUX_ENT( 7, AT_FLAGS, 0);
583 NEW_AUX_ENT( 8, AT_ENTRY, exec_params->entry_addr);
584 NEW_AUX_ENT( 9, AT_UID, (elf_addr_t) current->uid);
585 NEW_AUX_ENT(10, AT_EUID, (elf_addr_t) current->euid);
586 NEW_AUX_ENT(11, AT_GID, (elf_addr_t) current->gid);
587 NEW_AUX_ENT(12, AT_EGID, (elf_addr_t) current->egid);
590 /* ARCH_DLINFO must come last so platform specific code can enforce
591 * special alignment requirements on the AUXV if necessary (eg. PPC).
597 /* allocate room for argv[] and envv[] */
598 csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
599 envp = (elf_caddr_t __user *) csp;
600 csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
601 argv = (elf_caddr_t __user *) csp;
604 csp -= sizeof(unsigned long);
605 __put_user(bprm->argc, (unsigned long __user *) csp);
609 /* fill in the argv[] array */
611 current->mm->arg_start = bprm->p;
613 current->mm->arg_start = current->mm->start_stack -
614 (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
617 p = (char __user *) current->mm->arg_start;
618 for (loop = bprm->argc; loop > 0; loop--) {
619 __put_user((elf_caddr_t) p, argv++);
620 len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
621 if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
625 __put_user(NULL, argv);
626 current->mm->arg_end = (unsigned long) p;
628 /* fill in the envv[] array */
629 current->mm->env_start = (unsigned long) p;
630 for (loop = bprm->envc; loop > 0; loop--) {
631 __put_user((elf_caddr_t)(unsigned long) p, envp++);
632 len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
633 if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
637 __put_user(NULL, envp);
638 current->mm->env_end = (unsigned long) p;
640 mm->start_stack = (unsigned long) sp;
644 /*****************************************************************************/
646 * transfer the program arguments and environment from the holding pages onto
650 static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm,
653 unsigned long index, stop, sp;
657 stop = bprm->p >> PAGE_SHIFT;
660 for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
661 src = kmap(bprm->page[index]);
663 if (copy_to_user((void *) sp, src, PAGE_SIZE) != 0)
665 kunmap(bprm->page[index]);
670 *_sp = (*_sp - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p)) & ~15;
677 /*****************************************************************************/
679 * load the appropriate binary image (executable or interpreter) into memory
680 * - we assume no MMU is available
681 * - if no other PIC bits are set in params->hdr->e_flags
682 * - we assume that the LOADable segments in the binary are independently relocatable
683 * - we assume R/O executable segments are shareable
685 * - we assume the loadable parts of the image to require fixed displacement
686 * - the image is not shareable
688 static int elf_fdpic_map_file(struct elf_fdpic_params *params,
690 struct mm_struct *mm,
693 struct elf32_fdpic_loadmap *loadmap;
695 struct elf32_fdpic_loadseg *mseg;
697 struct elf32_fdpic_loadseg *seg;
698 struct elf32_phdr *phdr;
699 unsigned long load_addr, stop;
700 unsigned nloads, tmp;
704 /* allocate a load map table */
706 for (loop = 0; loop < params->hdr.e_phnum; loop++)
707 if (params->phdrs[loop].p_type == PT_LOAD)
713 size = sizeof(*loadmap) + nloads * sizeof(*seg);
714 loadmap = kmalloc(size, GFP_KERNEL);
718 params->loadmap = loadmap;
719 memset(loadmap, 0, size);
721 loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
722 loadmap->nsegs = nloads;
724 load_addr = params->load_addr;
727 /* map the requested LOADs into the memory space */
728 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
729 case ELF_FDPIC_FLAG_CONSTDISP:
730 case ELF_FDPIC_FLAG_CONTIGUOUS:
732 ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
738 ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
744 /* map the entry point */
745 if (params->hdr.e_entry) {
747 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
748 if (params->hdr.e_entry >= seg->p_vaddr &&
749 params->hdr.e_entry < seg->p_vaddr + seg->p_memsz) {
751 (params->hdr.e_entry - seg->p_vaddr) +
758 /* determine where the program header table has wound up if mapped */
759 stop = params->hdr.e_phoff;
760 stop += params->hdr.e_phnum * sizeof (struct elf_phdr);
761 phdr = params->phdrs;
763 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
764 if (phdr->p_type != PT_LOAD)
767 if (phdr->p_offset > params->hdr.e_phoff ||
768 phdr->p_offset + phdr->p_filesz < stop)
772 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
773 if (phdr->p_vaddr >= seg->p_vaddr &&
774 phdr->p_vaddr + phdr->p_filesz <=
775 seg->p_vaddr + seg->p_memsz) {
777 (phdr->p_vaddr - seg->p_vaddr) +
779 params->hdr.e_phoff - phdr->p_offset;
786 /* determine where the dynamic section has wound up if there is one */
787 phdr = params->phdrs;
788 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
789 if (phdr->p_type != PT_DYNAMIC)
793 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
794 if (phdr->p_vaddr >= seg->p_vaddr &&
795 phdr->p_vaddr + phdr->p_memsz <=
796 seg->p_vaddr + seg->p_memsz) {
797 params->dynamic_addr =
798 (phdr->p_vaddr - seg->p_vaddr) +
801 /* check the dynamic section contains at least
802 * one item, and that the last item is a NULL
804 if (phdr->p_memsz == 0 ||
805 phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
808 tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
810 params->dynamic_addr)[tmp - 1].d_tag != 0)
818 /* now elide adjacent segments in the load map on MMU linux
819 * - on uClinux the holes between may actually be filled with system
820 * stuff or stuff from other processes
823 nloads = loadmap->nsegs;
824 mseg = loadmap->segs;
826 for (loop = 1; loop < nloads; loop++) {
827 /* see if we have a candidate for merging */
828 if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
829 load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
830 if (load_addr == (seg->addr & PAGE_MASK)) {
833 (mseg->addr + mseg->p_memsz);
834 mseg->p_memsz += seg->addr & ~PAGE_MASK;
835 mseg->p_memsz += seg->p_memsz;
847 kdebug("Mapped Object [%s]:", what);
848 kdebug("- elfhdr : %lx", params->elfhdr_addr);
849 kdebug("- entry : %lx", params->entry_addr);
850 kdebug("- PHDR[] : %lx", params->ph_addr);
851 kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
853 for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
854 kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
856 seg->addr, seg->addr + seg->p_memsz - 1,
857 seg->p_vaddr, seg->p_memsz);
862 printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
863 what, file->f_dentry->d_inode->i_ino);
867 /*****************************************************************************/
869 * map a file with constant displacement under uClinux
872 static int elf_fdpic_map_file_constdisp_on_uclinux(
873 struct elf_fdpic_params *params,
875 struct mm_struct *mm)
877 struct elf32_fdpic_loadseg *seg;
878 struct elf32_phdr *phdr;
879 unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
883 load_addr = params->load_addr;
884 seg = params->loadmap->segs;
886 /* determine the bounds of the contiguous overall allocation we must
888 phdr = params->phdrs;
889 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
890 if (params->phdrs[loop].p_type != PT_LOAD)
893 if (base > phdr->p_vaddr)
894 base = phdr->p_vaddr;
895 if (top < phdr->p_vaddr + phdr->p_memsz)
896 top = phdr->p_vaddr + phdr->p_memsz;
899 /* allocate one big anon block for everything */
900 mflags = MAP_PRIVATE;
901 if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
902 mflags |= MAP_EXECUTABLE;
904 down_write(&mm->mmap_sem);
905 maddr = do_mmap(NULL, load_addr, top - base,
906 PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
907 up_write(&mm->mmap_sem);
908 if (IS_ERR_VALUE(maddr))
912 load_addr += PAGE_ALIGN(top - base);
914 /* and then load the file segments into it */
915 phdr = params->phdrs;
916 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
917 if (params->phdrs[loop].p_type != PT_LOAD)
920 fpos = phdr->p_offset;
922 seg->addr = maddr + (phdr->p_vaddr - base);
923 seg->p_vaddr = phdr->p_vaddr;
924 seg->p_memsz = phdr->p_memsz;
926 ret = file->f_op->read(file, (void *) seg->addr,
927 phdr->p_filesz, &fpos);
931 /* map the ELF header address if in this segment */
932 if (phdr->p_offset == 0)
933 params->elfhdr_addr = seg->addr;
935 /* clear any space allocated but not loaded */
936 if (phdr->p_filesz < phdr->p_memsz)
937 clear_user((void *) (seg->addr + phdr->p_filesz),
938 phdr->p_memsz - phdr->p_filesz);
941 if (phdr->p_flags & PF_X) {
942 mm->start_code = seg->addr;
943 mm->end_code = seg->addr + phdr->p_memsz;
944 } else if (!mm->start_data) {
945 mm->start_data = seg->addr;
947 mm->end_data = seg->addr + phdr->p_memsz;
952 if (seg->addr + phdr->p_memsz > mm->end_data)
953 mm->end_data = seg->addr + phdr->p_memsz;
964 /*****************************************************************************/
966 * map a binary by direct mmap() of the individual PT_LOAD segments
968 static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
970 struct mm_struct *mm)
972 struct elf32_fdpic_loadseg *seg;
973 struct elf32_phdr *phdr;
974 unsigned long load_addr, delta_vaddr;
977 load_addr = params->load_addr;
981 seg = params->loadmap->segs;
983 /* deal with each load segment separately */
984 phdr = params->phdrs;
985 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
986 unsigned long maddr, disp, excess, excess1;
989 if (phdr->p_type != PT_LOAD)
992 kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
993 (unsigned long) phdr->p_vaddr,
994 (unsigned long) phdr->p_offset,
995 (unsigned long) phdr->p_filesz,
996 (unsigned long) phdr->p_memsz);
998 /* determine the mapping parameters */
999 if (phdr->p_flags & PF_R) prot |= PROT_READ;
1000 if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
1001 if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
1003 flags = MAP_PRIVATE | MAP_DENYWRITE;
1004 if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
1005 flags |= MAP_EXECUTABLE;
1009 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
1010 case ELF_FDPIC_FLAG_INDEPENDENT:
1011 /* PT_LOADs are independently locatable */
1014 case ELF_FDPIC_FLAG_HONOURVADDR:
1015 /* the specified virtual address must be honoured */
1016 maddr = phdr->p_vaddr;
1020 case ELF_FDPIC_FLAG_CONSTDISP:
1021 /* constant displacement
1022 * - can be mapped anywhere, but must be mapped as a
1027 delta_vaddr = phdr->p_vaddr;
1030 maddr = load_addr + phdr->p_vaddr - delta_vaddr;
1035 case ELF_FDPIC_FLAG_CONTIGUOUS:
1036 /* contiguity handled later */
1045 /* create the mapping */
1046 disp = phdr->p_vaddr & ~PAGE_MASK;
1047 down_write(&mm->mmap_sem);
1048 maddr = do_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
1049 phdr->p_offset - disp);
1050 up_write(&mm->mmap_sem);
1052 kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
1053 loop, phdr->p_memsz + disp, prot, flags,
1054 phdr->p_offset - disp, maddr);
1056 if (IS_ERR_VALUE(maddr))
1059 if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
1060 ELF_FDPIC_FLAG_CONTIGUOUS)
1061 load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
1063 seg->addr = maddr + disp;
1064 seg->p_vaddr = phdr->p_vaddr;
1065 seg->p_memsz = phdr->p_memsz;
1067 /* map the ELF header address if in this segment */
1068 if (phdr->p_offset == 0)
1069 params->elfhdr_addr = seg->addr;
1071 /* clear the bit between beginning of mapping and beginning of
1073 if (prot & PROT_WRITE && disp > 0) {
1074 kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
1075 clear_user((void __user *) maddr, disp);
1079 /* clear any space allocated but not loaded
1080 * - on uClinux we can just clear the lot
1081 * - on MMU linux we'll get a SIGBUS beyond the last page
1082 * extant in the file
1084 excess = phdr->p_memsz - phdr->p_filesz;
1085 excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
1088 if (excess > excess1) {
1089 unsigned long xaddr = maddr + phdr->p_filesz + excess1;
1090 unsigned long xmaddr;
1092 flags |= MAP_FIXED | MAP_ANONYMOUS;
1093 down_write(&mm->mmap_sem);
1094 xmaddr = do_mmap(NULL, xaddr, excess - excess1,
1096 up_write(&mm->mmap_sem);
1098 kdebug("mmap[%d] <anon>"
1099 " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
1100 loop, xaddr, excess - excess1, prot, flags,
1103 if (xmaddr != xaddr)
1107 if (prot & PROT_WRITE && excess1 > 0) {
1108 kdebug("clear[%d] ad=%lx sz=%lx",
1109 loop, maddr + phdr->p_filesz, excess1);
1110 clear_user((void __user *) maddr + phdr->p_filesz,
1116 kdebug("clear[%d] ad=%lx sz=%lx",
1117 loop, maddr + phdr->p_filesz, excess);
1118 clear_user((void *) maddr + phdr->p_filesz, excess);
1123 if (phdr->p_flags & PF_X) {
1124 mm->start_code = maddr;
1125 mm->end_code = maddr + phdr->p_memsz;
1126 } else if (!mm->start_data) {
1127 mm->start_data = maddr;
1128 mm->end_data = maddr + phdr->p_memsz;
1138 /*****************************************************************************/
1140 * ELF-FDPIC core dumper
1142 * Modelled on fs/exec.c:aout_core_dump()
1143 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1145 * Modelled on fs/binfmt_elf.c core dumper
1147 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1150 * These are the only things you should do on a core-file: use only these
1151 * functions to write out all the necessary info.
1153 static int dump_write(struct file *file, const void *addr, int nr)
1155 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1158 static int dump_seek(struct file *file, loff_t off)
1160 if (file->f_op->llseek) {
1161 if (file->f_op->llseek(file, off, SEEK_SET) != off)
1170 * Decide whether a segment is worth dumping; default is yes to be
1171 * sure (missing info is worse than too much; etc).
1172 * Personally I'd include everything, and use the coredump limit...
1174 * I think we should skip something. But I am not sure how. H.J.
1176 static int maydump(struct vm_area_struct *vma)
1178 /* Do not dump I/O mapped devices or special mappings */
1179 if (vma->vm_flags & (VM_IO | VM_RESERVED)) {
1180 kdcore("%08lx: %08lx: no (IO)", vma->vm_start, vma->vm_flags);
1184 /* If we may not read the contents, don't allow us to dump
1185 * them either. "dump_write()" can't handle it anyway.
1187 if (!(vma->vm_flags & VM_READ)) {
1188 kdcore("%08lx: %08lx: no (!read)", vma->vm_start, vma->vm_flags);
1192 /* Dump shared memory only if mapped from an anonymous file. */
1193 if (vma->vm_flags & VM_SHARED) {
1194 if (vma->vm_file->f_dentry->d_inode->i_nlink == 0) {
1195 kdcore("%08lx: %08lx: no (share)", vma->vm_start, vma->vm_flags);
1199 kdcore("%08lx: %08lx: no (share)", vma->vm_start, vma->vm_flags);
1204 /* If it hasn't been written to, don't write it out */
1205 if (!vma->anon_vma) {
1206 kdcore("%08lx: %08lx: no (!anon)", vma->vm_start, vma->vm_flags);
1211 kdcore("%08lx: %08lx: yes", vma->vm_start, vma->vm_flags);
1215 /* An ELF note in memory */
1220 unsigned int datasz;
1224 static int notesize(struct memelfnote *en)
1228 sz = sizeof(struct elf_note);
1229 sz += roundup(strlen(en->name) + 1, 4);
1230 sz += roundup(en->datasz, 4);
1237 #define DUMP_WRITE(addr, nr) \
1238 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1239 #define DUMP_SEEK(off) \
1240 do { if (!dump_seek(file, (off))) return 0; } while(0)
1242 static int writenote(struct memelfnote *men, struct file *file)
1246 en.n_namesz = strlen(men->name) + 1;
1247 en.n_descsz = men->datasz;
1248 en.n_type = men->type;
1250 DUMP_WRITE(&en, sizeof(en));
1251 DUMP_WRITE(men->name, en.n_namesz);
1252 /* XXX - cast from long long to long to avoid need for libgcc.a */
1253 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1254 DUMP_WRITE(men->data, men->datasz);
1255 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1262 #define DUMP_WRITE(addr, nr) \
1263 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1265 #define DUMP_SEEK(off) \
1266 if (!dump_seek(file, (off))) \
1269 static inline void fill_elf_fdpic_header(struct elfhdr *elf, int segs)
1271 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1272 elf->e_ident[EI_CLASS] = ELF_CLASS;
1273 elf->e_ident[EI_DATA] = ELF_DATA;
1274 elf->e_ident[EI_VERSION] = EV_CURRENT;
1275 elf->e_ident[EI_OSABI] = ELF_OSABI;
1276 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1278 elf->e_type = ET_CORE;
1279 elf->e_machine = ELF_ARCH;
1280 elf->e_version = EV_CURRENT;
1282 elf->e_phoff = sizeof(struct elfhdr);
1284 elf->e_flags = ELF_FDPIC_CORE_EFLAGS;
1285 elf->e_ehsize = sizeof(struct elfhdr);
1286 elf->e_phentsize = sizeof(struct elf_phdr);
1287 elf->e_phnum = segs;
1288 elf->e_shentsize = 0;
1290 elf->e_shstrndx = 0;
1294 static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
1296 phdr->p_type = PT_NOTE;
1297 phdr->p_offset = offset;
1300 phdr->p_filesz = sz;
1307 static inline void fill_note(struct memelfnote *note, const char *name, int type,
1308 unsigned int sz, void *data)
1318 * fill up all the fields in prstatus from the given task struct, except
1319 * registers which need to be filled up seperately.
1321 static void fill_prstatus(struct elf_prstatus *prstatus,
1322 struct task_struct *p, long signr)
1324 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1325 prstatus->pr_sigpend = p->pending.signal.sig[0];
1326 prstatus->pr_sighold = p->blocked.sig[0];
1327 prstatus->pr_pid = p->pid;
1328 prstatus->pr_ppid = p->parent->pid;
1329 prstatus->pr_pgrp = process_group(p);
1330 prstatus->pr_sid = p->signal->session;
1331 if (thread_group_leader(p)) {
1333 * This is the record for the group leader. Add in the
1334 * cumulative times of previous dead threads. This total
1335 * won't include the time of each live thread whose state
1336 * is included in the core dump. The final total reported
1337 * to our parent process when it calls wait4 will include
1338 * those sums as well as the little bit more time it takes
1339 * this and each other thread to finish dying after the
1340 * core dump synchronization phase.
1342 cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
1343 &prstatus->pr_utime);
1344 cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
1345 &prstatus->pr_stime);
1347 cputime_to_timeval(p->utime, &prstatus->pr_utime);
1348 cputime_to_timeval(p->stime, &prstatus->pr_stime);
1350 cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
1351 cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
1353 prstatus->pr_exec_fdpic_loadmap = p->mm->context.exec_fdpic_loadmap;
1354 prstatus->pr_interp_fdpic_loadmap = p->mm->context.interp_fdpic_loadmap;
1357 static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1358 struct mm_struct *mm)
1360 unsigned int i, len;
1362 /* first copy the parameters from user space */
1363 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1365 len = mm->arg_end - mm->arg_start;
1366 if (len >= ELF_PRARGSZ)
1367 len = ELF_PRARGSZ - 1;
1368 if (copy_from_user(&psinfo->pr_psargs,
1369 (const char __user *) mm->arg_start, len))
1371 for (i = 0; i < len; i++)
1372 if (psinfo->pr_psargs[i] == 0)
1373 psinfo->pr_psargs[i] = ' ';
1374 psinfo->pr_psargs[len] = 0;
1376 psinfo->pr_pid = p->pid;
1377 psinfo->pr_ppid = p->parent->pid;
1378 psinfo->pr_pgrp = process_group(p);
1379 psinfo->pr_sid = p->signal->session;
1381 i = p->state ? ffz(~p->state) + 1 : 0;
1382 psinfo->pr_state = i;
1383 psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
1384 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1385 psinfo->pr_nice = task_nice(p);
1386 psinfo->pr_flag = p->flags;
1387 SET_UID(psinfo->pr_uid, p->uid);
1388 SET_GID(psinfo->pr_gid, p->gid);
1389 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1394 /* Here is the structure in which status of each thread is captured. */
1395 struct elf_thread_status
1397 struct list_head list;
1398 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1399 elf_fpregset_t fpu; /* NT_PRFPREG */
1400 struct task_struct *thread;
1401 #ifdef ELF_CORE_COPY_XFPREGS
1402 elf_fpxregset_t xfpu; /* NT_PRXFPREG */
1404 struct memelfnote notes[3];
1409 * In order to add the specific thread information for the elf file format,
1410 * we need to keep a linked list of every thread's pr_status and then create
1411 * a single section for them in the final core file.
1413 static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1415 struct task_struct *p = t->thread;
1420 fill_prstatus(&t->prstatus, p, signr);
1421 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1423 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
1426 sz += notesize(&t->notes[0]);
1428 t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, &t->fpu);
1429 if (t->prstatus.pr_fpvalid) {
1430 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
1433 sz += notesize(&t->notes[1]);
1436 #ifdef ELF_CORE_COPY_XFPREGS
1437 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
1438 fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu),
1441 sz += notesize(&t->notes[2]);
1448 * dump the segments for an MMU process
1451 static int elf_fdpic_dump_segments(struct file *file, struct mm_struct *mm,
1452 size_t *size, unsigned long *limit)
1454 struct vm_area_struct *vma;
1456 for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
1462 for (addr = vma->vm_start;
1466 struct vm_area_struct *vma;
1469 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1470 &page, &vma) <= 0) {
1471 DUMP_SEEK(file->f_pos + PAGE_SIZE);
1473 else if (page == ZERO_PAGE(addr)) {
1474 DUMP_SEEK(file->f_pos + PAGE_SIZE);
1475 page_cache_release(page);
1480 flush_cache_page(vma, addr, page_to_pfn(page));
1482 if ((*size += PAGE_SIZE) > *limit ||
1483 !dump_write(file, kaddr, PAGE_SIZE)
1486 page_cache_release(page);
1490 page_cache_release(page);
1503 * dump the segments for a NOMMU process
1506 static int elf_fdpic_dump_segments(struct file *file, struct mm_struct *mm,
1507 size_t *size, unsigned long *limit)
1509 struct vm_list_struct *vml;
1511 for (vml = current->mm->context.vmlist; vml; vml = vml->next) {
1512 struct vm_area_struct *vma = vml->vma;
1517 if ((*size += PAGE_SIZE) > *limit)
1520 if (!dump_write(file, (void *) vma->vm_start,
1521 vma->vm_end - vma->vm_start))
1532 * This is a two-pass process; first we find the offsets of the bits,
1533 * and then they are actually written out. If we run out of core limit
1536 static int elf_fdpic_core_dump(long signr, struct pt_regs *regs,
1545 struct vm_area_struct *vma;
1546 struct elfhdr *elf = NULL;
1547 loff_t offset = 0, dataoff;
1548 unsigned long limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1550 struct memelfnote *notes = NULL;
1551 struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
1552 struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
1553 struct task_struct *g, *p;
1554 LIST_HEAD(thread_list);
1555 struct list_head *t;
1556 elf_fpregset_t *fpu = NULL;
1557 #ifdef ELF_CORE_COPY_XFPREGS
1558 elf_fpxregset_t *xfpu = NULL;
1560 int thread_status_size = 0;
1562 struct vm_list_struct *vml;
1567 * We no longer stop all VM operations.
1569 * This is because those proceses that could possibly change map_count
1570 * or the mmap / vma pages are now blocked in do_exit on current
1571 * finishing this core dump.
1573 * Only ptrace can touch these memory addresses, but it doesn't change
1574 * the map_count or the pages allocated. So no possibility of crashing
1575 * exists while dumping the mm->vm_next areas to the core file.
1578 /* alloc memory for large data structures: too large to be on stack */
1579 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1582 prstatus = kzalloc(sizeof(*prstatus), GFP_KERNEL);
1585 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1588 notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1591 fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1594 #ifdef ELF_CORE_COPY_XFPREGS
1595 xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1601 struct elf_thread_status *tmp;
1602 read_lock(&tasklist_lock);
1604 if (current->mm == p->mm && current != p) {
1605 tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
1607 read_unlock(&tasklist_lock);
1610 INIT_LIST_HEAD(&tmp->list);
1612 list_add(&tmp->list, &thread_list);
1614 while_each_thread(g,p);
1615 read_unlock(&tasklist_lock);
1616 list_for_each(t, &thread_list) {
1617 struct elf_thread_status *tmp;
1620 tmp = list_entry(t, struct elf_thread_status, list);
1621 sz = elf_dump_thread_status(signr, tmp);
1622 thread_status_size += sz;
1626 /* now collect the dump for the current */
1627 fill_prstatus(prstatus, current, signr);
1628 elf_core_copy_regs(&prstatus->pr_reg, regs);
1631 segs = current->mm->map_count;
1634 for (vml = current->mm->context.vmlist; vml; vml = vml->next)
1637 #ifdef ELF_CORE_EXTRA_PHDRS
1638 segs += ELF_CORE_EXTRA_PHDRS;
1642 fill_elf_fdpic_header(elf, segs + 1); /* including notes section */
1645 current->flags |= PF_DUMPCORE;
1648 * Set up the notes in similar form to SVR4 core dumps made
1649 * with info from their /proc.
1652 fill_note(notes + 0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
1653 fill_psinfo(psinfo, current->group_leader, current->mm);
1654 fill_note(notes + 1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1658 auxv = (elf_addr_t *) current->mm->saved_auxv;
1663 while (auxv[i - 2] != AT_NULL);
1664 fill_note(¬es[numnote++], "CORE", NT_AUXV,
1665 i * sizeof(elf_addr_t), auxv);
1667 /* Try to dump the FPU. */
1668 if ((prstatus->pr_fpvalid =
1669 elf_core_copy_task_fpregs(current, regs, fpu)))
1670 fill_note(notes + numnote++,
1671 "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1672 #ifdef ELF_CORE_COPY_XFPREGS
1673 if (elf_core_copy_task_xfpregs(current, xfpu))
1674 fill_note(notes + numnote++,
1675 "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
1681 DUMP_WRITE(elf, sizeof(*elf));
1682 offset += sizeof(*elf); /* Elf header */
1683 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1685 /* Write notes phdr entry */
1687 struct elf_phdr phdr;
1690 for (i = 0; i < numnote; i++)
1691 sz += notesize(notes + i);
1693 sz += thread_status_size;
1695 fill_elf_note_phdr(&phdr, sz, offset);
1697 DUMP_WRITE(&phdr, sizeof(phdr));
1700 /* Page-align dumped data */
1701 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1703 /* write program headers for segments dump */
1706 vma = current->mm->mmap; vma; vma = vma->vm_next
1708 vml = current->mm->context.vmlist; vml; vml = vml->next
1711 struct elf_phdr phdr;
1718 sz = vma->vm_end - vma->vm_start;
1720 phdr.p_type = PT_LOAD;
1721 phdr.p_offset = offset;
1722 phdr.p_vaddr = vma->vm_start;
1724 phdr.p_filesz = maydump(vma) ? sz : 0;
1726 offset += phdr.p_filesz;
1727 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1728 if (vma->vm_flags & VM_WRITE)
1729 phdr.p_flags |= PF_W;
1730 if (vma->vm_flags & VM_EXEC)
1731 phdr.p_flags |= PF_X;
1732 phdr.p_align = ELF_EXEC_PAGESIZE;
1734 DUMP_WRITE(&phdr, sizeof(phdr));
1737 #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1738 ELF_CORE_WRITE_EXTRA_PHDRS;
1741 /* write out the notes section */
1742 for (i = 0; i < numnote; i++)
1743 if (!writenote(notes + i, file))
1746 /* write out the thread status notes section */
1747 list_for_each(t, &thread_list) {
1748 struct elf_thread_status *tmp =
1749 list_entry(t, struct elf_thread_status, list);
1751 for (i = 0; i < tmp->num_notes; i++)
1752 if (!writenote(&tmp->notes[i], file))
1758 if (elf_fdpic_dump_segments(file, current->mm, &size, &limit) < 0)
1761 #ifdef ELF_CORE_WRITE_EXTRA_DATA
1762 ELF_CORE_WRITE_EXTRA_DATA;
1765 if (file->f_pos != offset) {
1768 "elf_core_dump: file->f_pos (%lld) != offset (%lld)\n",
1769 file->f_pos, offset);
1776 while (!list_empty(&thread_list)) {
1777 struct list_head *tmp = thread_list.next;
1779 kfree(list_entry(tmp, struct elf_thread_status, list));
1787 #ifdef ELF_CORE_COPY_XFPREGS
1794 #endif /* USE_ELF_CORE_DUMP */