2 * linux/fs/binfmt_elf.c
4 * These are the functions used to load ELF format executables as used
5 * on SVr4 machines. Information on the format may be found in the book
6 * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
9 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
12 #include <linux/module.h>
15 #include <linux/stat.h>
16 #include <linux/sched.h>
18 #include <linux/mman.h>
19 #include <linux/a.out.h>
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/binfmts.h>
23 #include <linux/string.h>
24 #include <linux/file.h>
25 #include <linux/fcntl.h>
26 #include <linux/ptrace.h>
27 #include <linux/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_lock.h>
34 #include <linux/compiler.h>
35 #include <linux/highmem.h>
37 #include <asm/uaccess.h>
38 #include <asm/param.h>
39 #include <asm/pgalloc.h>
41 #define DLINFO_ITEMS 13
43 #include <linux/elf.h>
45 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs);
46 static int load_elf_library(struct file*);
47 static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int);
48 extern int dump_fpu (struct pt_regs *, elf_fpregset_t *);
49 extern void dump_thread(struct pt_regs *, struct user *);
52 #define elf_addr_t unsigned long
53 #define elf_caddr_t char *
57 * If we don't support core dumping, then supply a NULL so we
60 #ifdef USE_ELF_CORE_DUMP
61 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file);
63 #define elf_core_dump NULL
66 #if ELF_EXEC_PAGESIZE > PAGE_SIZE
67 # define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
69 # define ELF_MIN_ALIGN PAGE_SIZE
72 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
73 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
74 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
76 static struct linux_binfmt elf_format = {
77 NULL, THIS_MODULE, load_elf_binary, load_elf_library, elf_core_dump, ELF_EXEC_PAGESIZE
80 #define BAD_ADDR(x) ((unsigned long)(x) > TASK_SIZE)
82 static void set_brk(unsigned long start, unsigned long end)
84 start = ELF_PAGEALIGN(start);
85 end = ELF_PAGEALIGN(end);
88 do_brk(start, end - start);
92 /* We need to explicitly zero any fractional pages
93 after the data section (i.e. bss). This would
94 contain the junk from the file that should not
98 static void padzero(unsigned long elf_bss)
102 nbyte = ELF_PAGEOFFSET(elf_bss);
104 nbyte = ELF_MIN_ALIGN - nbyte;
105 clear_user((void *) elf_bss, nbyte);
110 create_elf_tables(char *p, int argc, int envc,
111 struct elfhdr * exec,
112 unsigned long load_addr,
113 unsigned long load_bias,
114 unsigned long interp_load_addr, int ibcs)
118 elf_addr_t *sp, *csp;
119 char *k_platform, *u_platform;
121 size_t platform_len = 0;
125 * Get hold of platform and hardware capabilities masks for
126 * the machine we are running on. In some cases (Sparc),
127 * this info is impossible to get, in others (i386) it is
132 k_platform = ELF_PLATFORM;
135 platform_len = strlen(k_platform) + 1;
136 u_platform = p - platform_len;
137 __copy_to_user(u_platform, k_platform, platform_len);
141 #if defined(__i386__) && defined(CONFIG_SMP)
143 * In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
144 * by the processes running on the same package. One thing we can do
145 * is to shuffle the initial stack for them.
147 * The conditionals here are unneeded, but kept in to make the
148 * code behaviour the same as pre change unless we have hyperthreaded
149 * processors. This keeps Mr Marcelo Person happier but should be
153 if(smp_num_siblings > 1)
154 u_platform = u_platform - ((current->pid % 64) << 7);
158 * Force 16 byte _final_ alignment here for generality.
160 sp = (elf_addr_t *)(~15UL & (unsigned long)(u_platform));
162 csp -= (1+DLINFO_ITEMS)*2 + (k_platform ? 2 : 0);
163 #ifdef DLINFO_ARCH_ITEMS
164 csp -= DLINFO_ARCH_ITEMS*2;
168 csp -= (!ibcs ? 3 : 1); /* argc itself */
169 if ((unsigned long)csp & 15UL)
170 sp -= ((unsigned long)csp & 15UL) / sizeof(*sp);
173 * Put the ELF interpreter info on the stack
175 #define NEW_AUX_ENT(nr, id, val) \
176 __put_user ((id), sp+(nr*2)); \
177 __put_user ((val), sp+(nr*2+1)); \
180 NEW_AUX_ENT(0, AT_NULL, 0);
183 NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
185 sp -= DLINFO_ITEMS*2;
186 NEW_AUX_ENT( 0, AT_HWCAP, hwcap);
187 NEW_AUX_ENT( 1, AT_PAGESZ, ELF_EXEC_PAGESIZE);
188 NEW_AUX_ENT( 2, AT_CLKTCK, CLOCKS_PER_SEC);
189 NEW_AUX_ENT( 3, AT_PHDR, load_addr + exec->e_phoff);
190 NEW_AUX_ENT( 4, AT_PHENT, sizeof (struct elf_phdr));
191 NEW_AUX_ENT( 5, AT_PHNUM, exec->e_phnum);
192 NEW_AUX_ENT( 6, AT_BASE, interp_load_addr);
193 NEW_AUX_ENT( 7, AT_FLAGS, 0);
194 NEW_AUX_ENT( 8, AT_ENTRY, load_bias + exec->e_entry);
195 NEW_AUX_ENT( 9, AT_UID, (elf_addr_t) current->uid);
196 NEW_AUX_ENT(10, AT_EUID, (elf_addr_t) current->euid);
197 NEW_AUX_ENT(11, AT_GID, (elf_addr_t) current->gid);
198 NEW_AUX_ENT(12, AT_EGID, (elf_addr_t) current->egid);
201 * ARCH_DLINFO must come last so platform specific code can enforce
202 * special alignment requirements on the AUXV if necessary (eg. PPC).
209 envp = (elf_caddr_t *) sp;
211 argv = (elf_caddr_t *) sp;
213 __put_user((elf_addr_t)(unsigned long) envp,--sp);
214 __put_user((elf_addr_t)(unsigned long) argv,--sp);
217 __put_user((elf_addr_t)argc,--sp);
218 current->mm->arg_start = (unsigned long) p;
220 __put_user((elf_caddr_t)(unsigned long)p,argv++);
221 len = strnlen_user(p, PAGE_SIZE*MAX_ARG_PAGES);
222 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
226 __put_user(NULL, argv);
227 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
229 __put_user((elf_caddr_t)(unsigned long)p,envp++);
230 len = strnlen_user(p, PAGE_SIZE*MAX_ARG_PAGES);
231 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
235 __put_user(NULL, envp);
236 current->mm->env_end = (unsigned long) p;
242 static inline unsigned long
243 elf_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
245 unsigned long map_addr;
247 down_write(¤t->mm->mmap_sem);
248 map_addr = do_mmap(filep, ELF_PAGESTART(addr),
249 eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr), prot, type,
250 eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
251 up_write(¤t->mm->mmap_sem);
255 #endif /* !elf_map */
257 /* This is much more generalized than the library routine read function,
258 so we keep this separate. Technically the library read function
259 is only provided so that we can read a.out libraries that have
262 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
263 struct file * interpreter,
264 unsigned long *interp_load_addr)
266 struct elf_phdr *elf_phdata;
267 struct elf_phdr *eppnt;
268 unsigned long load_addr = 0;
269 int load_addr_set = 0;
270 unsigned long last_bss = 0, elf_bss = 0;
271 unsigned long error = ~0UL;
274 /* First of all, some simple consistency checks */
275 if (interp_elf_ex->e_type != ET_EXEC &&
276 interp_elf_ex->e_type != ET_DYN)
278 if (!elf_check_arch(interp_elf_ex))
280 if (!interpreter->f_op || !interpreter->f_op->mmap)
284 * If the size of this structure has changed, then punt, since
285 * we will be doing the wrong thing.
287 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
289 if (interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
292 /* Now read in all of the header information */
294 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
295 if (size > ELF_MIN_ALIGN)
297 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
301 retval = kernel_read(interpreter,interp_elf_ex->e_phoff,(char *)elf_phdata,size);
307 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
308 if (eppnt->p_type == PT_LOAD) {
309 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
311 unsigned long vaddr = 0;
312 unsigned long k, map_addr;
314 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
315 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
316 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
317 vaddr = eppnt->p_vaddr;
318 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
319 elf_type |= MAP_FIXED;
321 map_addr = elf_map(interpreter, load_addr + vaddr, eppnt, elf_prot, elf_type);
322 if (BAD_ADDR(map_addr))
325 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
326 load_addr = map_addr - ELF_PAGESTART(vaddr);
331 * Find the end of the file mapping for this phdr, and keep
332 * track of the largest address we see for this.
334 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
339 * Do the same thing for the memory mapping - between
340 * elf_bss and last_bss is the bss section.
342 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
348 /* Now use mmap to map the library into memory. */
351 * Now fill out the bss section. First pad the last page up
352 * to the page boundary, and then perform a mmap to make sure
353 * that there are zero-mapped pages up to and including the
357 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1); /* What we have mapped so far */
359 /* Map the last of the bss segment */
360 if (last_bss > elf_bss)
361 do_brk(elf_bss, last_bss - elf_bss);
363 *interp_load_addr = load_addr;
364 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
372 static unsigned long load_aout_interp(struct exec * interp_ex,
373 struct file * interpreter)
375 unsigned long text_data, elf_entry = ~0UL;
380 current->mm->end_code = interp_ex->a_text;
381 text_data = interp_ex->a_text + interp_ex->a_data;
382 current->mm->end_data = text_data;
383 current->mm->brk = interp_ex->a_bss + text_data;
385 switch (N_MAGIC(*interp_ex)) {
392 offset = N_TXTOFF(*interp_ex);
393 addr = (char *) N_TXTADDR(*interp_ex);
399 do_brk(0, text_data);
401 if (!interpreter->f_op || !interpreter->f_op->read)
403 retval = interpreter->f_op->read(interpreter, addr, text_data, &offset);
406 flush_icache_range((unsigned long)addr,
407 (unsigned long)addr + text_data);
409 do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
411 elf_entry = interp_ex->a_entry;
418 * These are the functions used to load ELF style executables and shared
419 * libraries. There is no binary dependent code anywhere else.
422 #define INTERPRETER_NONE 0
423 #define INTERPRETER_AOUT 1
424 #define INTERPRETER_ELF 2
427 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
429 struct file *interpreter = NULL; /* to shut gcc up */
430 unsigned long load_addr = 0, load_bias = 0;
431 int load_addr_set = 0;
432 char * elf_interpreter = NULL;
433 unsigned int interpreter_type = INTERPRETER_NONE;
434 unsigned char ibcs2_interpreter = 0;
436 struct elf_phdr * elf_ppnt, *elf_phdata;
437 unsigned long elf_bss, k, elf_brk;
441 unsigned long elf_entry, interp_load_addr = 0;
442 unsigned long start_code, end_code, start_data, end_data;
443 struct elfhdr elf_ex;
444 struct elfhdr interp_elf_ex;
445 struct exec interp_ex;
446 char passed_fileno[6];
448 /* Get the exec-header */
449 elf_ex = *((struct elfhdr *) bprm->buf);
452 /* First of all, some simple consistency checks */
453 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
456 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
458 if (!elf_check_arch(&elf_ex))
460 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
463 /* Now read in all of the header information */
466 if (elf_ex.e_phentsize != sizeof(struct elf_phdr))
468 if (elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
470 size = elf_ex.e_phnum * sizeof(struct elf_phdr);
471 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
475 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *) elf_phdata, size);
479 retval = get_unused_fd();
482 get_file(bprm->file);
483 fd_install(elf_exec_fileno = retval, bprm->file);
485 elf_ppnt = elf_phdata;
494 for (i = 0; i < elf_ex.e_phnum; i++) {
495 if (elf_ppnt->p_type == PT_INTERP) {
496 /* This is the program interpreter used for
497 * shared libraries - for now assume that this
498 * is an a.out format binary
502 if (elf_ppnt->p_filesz > PATH_MAX)
504 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
506 if (!elf_interpreter)
509 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
513 goto out_free_interp;
514 /* If the program interpreter is one of these two,
515 * then assume an iBCS2 image. Otherwise assume
516 * a native linux image.
518 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
519 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
520 ibcs2_interpreter = 1;
522 printk("Using ELF interpreter %s\n", elf_interpreter);
525 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
527 interpreter = open_exec(elf_interpreter);
528 retval = PTR_ERR(interpreter);
529 if (IS_ERR(interpreter))
530 goto out_free_interp;
531 retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
533 goto out_free_dentry;
535 /* Get the exec headers */
536 interp_ex = *((struct exec *) bprm->buf);
537 interp_elf_ex = *((struct elfhdr *) bprm->buf);
543 /* Some simple consistency checks for the interpreter */
544 if (elf_interpreter) {
545 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
547 /* Now figure out which format our binary is */
548 if ((N_MAGIC(interp_ex) != OMAGIC) &&
549 (N_MAGIC(interp_ex) != ZMAGIC) &&
550 (N_MAGIC(interp_ex) != QMAGIC))
551 interpreter_type = INTERPRETER_ELF;
553 if (memcmp(interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
554 interpreter_type &= ~INTERPRETER_ELF;
557 if (!interpreter_type)
558 goto out_free_dentry;
560 /* Make sure only one type was selected */
561 if ((interpreter_type & INTERPRETER_ELF) &&
562 interpreter_type != INTERPRETER_ELF) {
563 // FIXME - ratelimit this before re-enabling
564 // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
565 interpreter_type = INTERPRETER_ELF;
568 /* Executables without an interpreter also need a personality */
569 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
572 /* OK, we are done with that, now set up the arg stuff,
573 and then start this sucker up */
575 if (!bprm->sh_bang) {
578 if (interpreter_type == INTERPRETER_AOUT) {
579 sprintf(passed_fileno, "%d", elf_exec_fileno);
580 passed_p = passed_fileno;
582 if (elf_interpreter) {
583 retval = copy_strings_kernel(1,&passed_p,bprm);
585 goto out_free_dentry;
591 /* Flush all traces of the currently running executable */
592 retval = flush_old_exec(bprm);
594 goto out_free_dentry;
596 /* OK, This is the point of no return */
597 current->mm->start_data = 0;
598 current->mm->end_data = 0;
599 current->mm->end_code = 0;
600 current->mm->mmap = NULL;
601 current->flags &= ~PF_FORKNOEXEC;
602 elf_entry = (unsigned long) elf_ex.e_entry;
604 /* Do this so that we can load the interpreter, if need be. We will
605 change some of these later */
606 current->mm->rss = 0;
607 retval = setup_arg_pages(bprm);
609 send_sig(SIGKILL, current, 0);
613 current->mm->start_stack = bprm->p;
615 /* Now we do a little grungy work by mmaping the ELF image into
616 the correct location in memory. At this point, we assume that
617 the image should be loaded at fixed address, not at a variable
620 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
621 int elf_prot = 0, elf_flags;
624 if (elf_ppnt->p_type != PT_LOAD)
627 if (unlikely (elf_brk > elf_bss)) {
630 /* There was a PT_LOAD segment with p_memsz > p_filesz
631 before this one. Map anonymous pages, if needed,
632 and clear the area. */
633 set_brk (elf_bss + load_bias, elf_brk + load_bias);
634 nbyte = ELF_PAGEOFFSET(elf_bss);
636 nbyte = ELF_MIN_ALIGN - nbyte;
637 if (nbyte > elf_brk - elf_bss)
638 nbyte = elf_brk - elf_bss;
639 clear_user((void *) elf_bss + load_bias, nbyte);
643 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
644 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
645 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
647 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
649 vaddr = elf_ppnt->p_vaddr;
650 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
651 elf_flags |= MAP_FIXED;
652 } else if (elf_ex.e_type == ET_DYN) {
653 /* Try and get dynamic programs out of the way of the default mmap
654 base, as well as whatever program they might try to exec. This
655 is because the brk will follow the loader, and is not movable. */
656 load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
659 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags);
663 if (!load_addr_set) {
665 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
666 if (elf_ex.e_type == ET_DYN) {
668 ELF_PAGESTART(load_bias + vaddr);
669 load_addr += load_bias;
672 k = elf_ppnt->p_vaddr;
673 if (k < start_code) start_code = k;
674 if (start_data < k) start_data = k;
676 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
680 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
684 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
689 elf_entry += load_bias;
690 elf_bss += load_bias;
691 elf_brk += load_bias;
692 start_code += load_bias;
693 end_code += load_bias;
694 start_data += load_bias;
695 end_data += load_bias;
697 if (elf_interpreter) {
698 if (interpreter_type == INTERPRETER_AOUT)
699 elf_entry = load_aout_interp(&interp_ex,
702 elf_entry = load_elf_interp(&interp_elf_ex,
706 allow_write_access(interpreter);
708 kfree(elf_interpreter);
710 if (BAD_ADDR(elf_entry)) {
711 printk(KERN_ERR "Unable to load interpreter\n");
713 send_sig(SIGSEGV, current, 0);
720 if (interpreter_type != INTERPRETER_AOUT)
721 sys_close(elf_exec_fileno);
723 set_binfmt(&elf_format);
726 current->flags &= ~PF_FORKNOEXEC;
727 bprm->p = (unsigned long)
728 create_elf_tables((char *)bprm->p,
732 load_addr, load_bias,
734 (interpreter_type == INTERPRETER_AOUT ? 0 : 1));
735 /* N.B. passed_fileno might not be initialized? */
736 if (interpreter_type == INTERPRETER_AOUT)
737 current->mm->arg_start += strlen(passed_fileno) + 1;
738 current->mm->start_brk = current->mm->brk = elf_brk;
739 current->mm->end_code = end_code;
740 current->mm->start_code = start_code;
741 current->mm->start_data = start_data;
742 current->mm->end_data = end_data;
743 current->mm->start_stack = bprm->p;
745 /* Calling set_brk effectively mmaps the pages that we need
746 * for the bss and break sections
748 set_brk(elf_bss, elf_brk);
753 printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
754 printk("(end_code) %lx\n" , (long) current->mm->end_code);
755 printk("(start_code) %lx\n" , (long) current->mm->start_code);
756 printk("(start_data) %lx\n" , (long) current->mm->start_data);
757 printk("(end_data) %lx\n" , (long) current->mm->end_data);
758 printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
759 printk("(brk) %lx\n" , (long) current->mm->brk);
762 if (current->personality & MMAP_PAGE_ZERO) {
763 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
764 and some applications "depend" upon this behavior.
765 Since we do not have the power to recompile these, we
766 emulate the SVr4 behavior. Sigh. */
767 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
768 down_write(¤t->mm->mmap_sem);
769 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
770 MAP_FIXED | MAP_PRIVATE, 0);
771 up_write(¤t->mm->mmap_sem);
776 * The ABI may specify that certain registers be set up in special
777 * ways (on i386 %edx is the address of a DT_FINI function, for
778 * example. This macro performs whatever initialization to
779 * the regs structure is required.
784 start_thread(regs, elf_entry, bprm->p);
785 if (current->ptrace & PT_PTRACED)
786 send_sig(SIGTRAP, current, 0);
793 allow_write_access(interpreter);
797 kfree(elf_interpreter);
799 sys_close(elf_exec_fileno);
805 /* This is really simpleminded and specialized - we are loading an
806 a.out library that is given an ELF header. */
808 static int load_elf_library(struct file *file)
810 struct elf_phdr *elf_phdata;
811 unsigned long elf_bss, bss, len;
812 int retval, error, i, j;
813 struct elfhdr elf_ex;
816 retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
817 if (retval != sizeof(elf_ex))
820 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
823 /* First of all, some simple consistency checks */
824 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
825 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
828 /* Now read in all of the header information */
830 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
831 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
834 elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
839 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata, j);
843 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
844 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
848 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
850 /* Now use mmap to map the library into memory. */
851 down_write(¤t->mm->mmap_sem);
852 error = do_mmap(file,
853 ELF_PAGESTART(elf_phdata->p_vaddr),
854 (elf_phdata->p_filesz +
855 ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
856 PROT_READ | PROT_WRITE | PROT_EXEC,
857 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
858 (elf_phdata->p_offset -
859 ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
860 up_write(¤t->mm->mmap_sem);
861 if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
864 elf_bss = elf_phdata->p_vaddr + elf_phdata->p_filesz;
867 len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr + ELF_MIN_ALIGN - 1);
868 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
870 do_brk(len, bss - len);
880 * Note that some platforms still use traditional core dumps and not
881 * the ELF core dump. Each platform can select it as appropriate.
883 #ifdef USE_ELF_CORE_DUMP
888 * Modelled on fs/exec.c:aout_core_dump()
889 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
892 * These are the only things you should do on a core-file: use only these
893 * functions to write out all the necessary info.
895 static int dump_write(struct file *file, const void *addr, int nr)
897 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
900 static int dump_seek(struct file *file, off_t off)
902 if (file->f_op->llseek) {
903 if (file->f_op->llseek(file, off, 0) != off)
911 * Decide whether a segment is worth dumping; default is yes to be
912 * sure (missing info is worse than too much; etc).
913 * Personally I'd include everything, and use the coredump limit...
915 * I think we should skip something. But I am not sure how. H.J.
917 static inline int maydump(struct vm_area_struct *vma)
920 * If we may not read the contents, don't allow us to dump
921 * them either. "dump_write()" can't handle it anyway.
923 if (!(vma->vm_flags & VM_READ))
926 /* Do not dump I/O mapped devices! -DaveM */
927 if (vma->vm_flags & VM_IO)
930 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
932 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
938 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
940 /* An ELF note in memory */
949 static int notesize(struct memelfnote *en)
953 sz = sizeof(struct elf_note);
954 sz += roundup(strlen(en->name), 4);
955 sz += roundup(en->datasz, 4);
963 static void dump_regs(const char *str, elf_greg_t *r)
966 static const char *regs[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
967 "eax", "ds", "es", "fs", "gs",
968 "orig_eax", "eip", "cs",
969 "efl", "uesp", "ss"};
970 printk("Registers: %s\n", str);
972 for(i = 0; i < ELF_NGREG; i++)
974 unsigned long val = r[i];
975 printk(" %-2d %-5s=%08lx %lu\n", i, regs[i], val, val);
980 #define DUMP_WRITE(addr, nr) \
981 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
982 #define DUMP_SEEK(off) \
983 do { if (!dump_seek(file, (off))) return 0; } while(0)
985 static int writenote(struct memelfnote *men, struct file *file)
989 en.n_namesz = strlen(men->name);
990 en.n_descsz = men->datasz;
991 en.n_type = men->type;
993 DUMP_WRITE(&en, sizeof(en));
994 DUMP_WRITE(men->name, en.n_namesz);
995 /* XXX - cast from long long to long to avoid need for libgcc.a */
996 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
997 DUMP_WRITE(men->data, men->datasz);
998 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1005 #define DUMP_WRITE(addr, nr) \
1006 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1008 #define DUMP_SEEK(off) \
1009 if (!dump_seek(file, (off))) \
1014 * This is a two-pass process; first we find the offsets of the bits,
1015 * and then they are actually written out. If we run out of core limit
1018 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
1025 struct vm_area_struct *vma;
1027 off_t offset = 0, dataoff;
1028 unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1030 struct memelfnote notes[4];
1031 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1032 elf_fpregset_t fpu; /* NT_PRFPREG */
1033 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1035 /* first copy the parameters from user space */
1036 memset(&psinfo, 0, sizeof(psinfo));
1040 len = current->mm->arg_end - current->mm->arg_start;
1041 if (len >= ELF_PRARGSZ)
1042 len = ELF_PRARGSZ-1;
1043 copy_from_user(&psinfo.pr_psargs,
1044 (const char *)current->mm->arg_start, len);
1045 for(i = 0; i < len; i++)
1046 if (psinfo.pr_psargs[i] == 0)
1047 psinfo.pr_psargs[i] = ' ';
1048 psinfo.pr_psargs[len] = 0;
1052 memset(&prstatus, 0, sizeof(prstatus));
1054 * This transfers the registers from regs into the standard
1055 * coredump arrangement, whatever that is.
1057 #ifdef ELF_CORE_COPY_REGS
1058 ELF_CORE_COPY_REGS(prstatus.pr_reg, regs)
1060 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs))
1062 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1063 (long)sizeof(elf_gregset_t), (long)sizeof(struct pt_regs));
1066 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1069 /* now stop all vm operations */
1070 down_write(¤t->mm->mmap_sem);
1071 segs = current->mm->map_count;
1074 printk("elf_core_dump: %d segs %lu limit\n", segs, limit);
1078 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1079 elf.e_ident[EI_CLASS] = ELF_CLASS;
1080 elf.e_ident[EI_DATA] = ELF_DATA;
1081 elf.e_ident[EI_VERSION] = EV_CURRENT;
1082 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1084 elf.e_type = ET_CORE;
1085 elf.e_machine = ELF_ARCH;
1086 elf.e_version = EV_CURRENT;
1088 elf.e_phoff = sizeof(elf);
1091 elf.e_ehsize = sizeof(elf);
1092 elf.e_phentsize = sizeof(struct elf_phdr);
1093 elf.e_phnum = segs+1; /* Include notes */
1094 elf.e_shentsize = 0;
1102 current->flags |= PF_DUMPCORE;
1104 DUMP_WRITE(&elf, sizeof(elf));
1105 offset += sizeof(elf); /* Elf header */
1106 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1109 * Set up the notes in similar form to SVR4 core dumps made
1110 * with info from their /proc.
1113 notes[0].name = "CORE";
1114 notes[0].type = NT_PRSTATUS;
1115 notes[0].datasz = sizeof(prstatus);
1116 notes[0].data = &prstatus;
1117 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1118 prstatus.pr_sigpend = current->pending.signal.sig[0];
1119 prstatus.pr_sighold = current->blocked.sig[0];
1120 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1121 psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1122 psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1123 psinfo.pr_sid = prstatus.pr_sid = current->session;
1124 prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
1125 prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
1126 prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
1127 prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
1128 prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
1129 prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
1130 prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
1131 prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
1134 dump_regs("Passed in regs", (elf_greg_t *)regs);
1135 dump_regs("prstatus regs", (elf_greg_t *)&prstatus.pr_reg);
1138 notes[1].name = "CORE";
1139 notes[1].type = NT_PRPSINFO;
1140 notes[1].datasz = sizeof(psinfo);
1141 notes[1].data = &psinfo;
1142 i = current->state ? ffz(~current->state) + 1 : 0;
1143 psinfo.pr_state = i;
1144 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1145 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1146 psinfo.pr_nice = current->nice;
1147 psinfo.pr_flag = current->flags;
1148 psinfo.pr_uid = NEW_TO_OLD_UID(current->uid);
1149 psinfo.pr_gid = NEW_TO_OLD_GID(current->gid);
1150 strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1152 notes[2].name = "CORE";
1153 notes[2].type = NT_TASKSTRUCT;
1154 notes[2].datasz = sizeof(*current);
1155 notes[2].data = current;
1157 /* Try to dump the FPU. */
1158 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1159 if (!prstatus.pr_fpvalid)
1165 notes[3].name = "CORE";
1166 notes[3].type = NT_PRFPREG;
1167 notes[3].datasz = sizeof(fpu);
1168 notes[3].data = &fpu;
1171 /* Write notes phdr entry */
1173 struct elf_phdr phdr;
1176 for(i = 0; i < numnote; i++)
1177 sz += notesize(¬es[i]);
1179 phdr.p_type = PT_NOTE;
1180 phdr.p_offset = offset;
1188 offset += phdr.p_filesz;
1189 DUMP_WRITE(&phdr, sizeof(phdr));
1192 /* Page-align dumped data */
1193 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1195 /* Write program headers for segments dump */
1196 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1197 struct elf_phdr phdr;
1200 sz = vma->vm_end - vma->vm_start;
1202 phdr.p_type = PT_LOAD;
1203 phdr.p_offset = offset;
1204 phdr.p_vaddr = vma->vm_start;
1206 phdr.p_filesz = maydump(vma) ? sz : 0;
1208 offset += phdr.p_filesz;
1209 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1210 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1211 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1212 phdr.p_align = ELF_EXEC_PAGESIZE;
1214 DUMP_WRITE(&phdr, sizeof(phdr));
1217 for(i = 0; i < numnote; i++)
1218 if (!writenote(¬es[i], file))
1223 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1230 printk("elf_core_dump: writing %08lx-%08lx\n", vma->vm_start, vma->vm_end);
1233 for (addr = vma->vm_start;
1235 addr += PAGE_SIZE) {
1237 struct vm_area_struct *vma;
1239 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1240 &page, &vma) <= 0) {
1241 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1243 if (page == ZERO_PAGE(addr)) {
1244 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1247 flush_cache_page(vma, addr);
1249 DUMP_WRITE(kaddr, PAGE_SIZE);
1250 flush_page_to_ram(page);
1258 if ((off_t) file->f_pos != offset) {
1260 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1261 (off_t) file->f_pos, offset);
1266 up_write(¤t->mm->mmap_sem);
1269 #endif /* USE_ELF_CORE_DUMP */
1271 static int __init init_elf_binfmt(void)
1273 return register_binfmt(&elf_format);
1276 static void __exit exit_elf_binfmt(void)
1278 /* Remove the COFF and ELF loaders. */
1279 unregister_binfmt(&elf_format);
1282 module_init(init_elf_binfmt)
1283 module_exit(exit_elf_binfmt)
1284 MODULE_LICENSE("GPL");