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 int set_brk(unsigned long start, unsigned long end)
84 start = ELF_PAGEALIGN(start);
85 end = ELF_PAGEALIGN(end);
88 down_write(¤t->mm->mmap_sem);
89 addr = do_brk(start, end - start);
90 up_write(¤t->mm->mmap_sem);
94 current->mm->start_brk = current->mm->brk = end;
99 /* We need to explicitly zero any fractional pages
100 after the data section (i.e. bss). This would
101 contain the junk from the file that should not
105 static void padzero(unsigned long elf_bss)
109 nbyte = ELF_PAGEOFFSET(elf_bss);
111 nbyte = ELF_MIN_ALIGN - nbyte;
112 clear_user((void *) elf_bss, nbyte);
117 create_elf_tables(char *p, int argc, int envc,
118 struct elfhdr * exec,
119 unsigned long load_addr,
120 unsigned long load_bias,
121 unsigned long interp_load_addr, int ibcs)
125 elf_addr_t *sp, *csp;
126 char *k_platform, *u_platform;
128 size_t platform_len = 0;
132 * Get hold of platform and hardware capabilities masks for
133 * the machine we are running on. In some cases (Sparc),
134 * this info is impossible to get, in others (i386) it is
139 k_platform = ELF_PLATFORM;
142 platform_len = strlen(k_platform) + 1;
143 u_platform = p - platform_len;
144 __copy_to_user(u_platform, k_platform, platform_len);
148 #if defined(__i386__) && defined(CONFIG_SMP)
150 * In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
151 * by the processes running on the same package. One thing we can do
152 * is to shuffle the initial stack for them.
154 * The conditionals here are unneeded, but kept in to make the
155 * code behaviour the same as pre change unless we have hyperthreaded
156 * processors. This keeps Mr Marcelo Person happier but should be
160 if(smp_num_siblings > 1)
161 u_platform = u_platform - ((current->pid % 64) << 7);
165 * Force 16 byte _final_ alignment here for generality.
167 sp = (elf_addr_t *)(~15UL & (unsigned long)(u_platform));
169 csp -= (1+DLINFO_ITEMS)*2 + (k_platform ? 2 : 0);
170 #ifdef DLINFO_ARCH_ITEMS
171 csp -= DLINFO_ARCH_ITEMS*2;
175 csp -= (!ibcs ? 3 : 1); /* argc itself */
176 if ((unsigned long)csp & 15UL)
177 sp -= ((unsigned long)csp & 15UL) / sizeof(*sp);
180 * Put the ELF interpreter info on the stack
182 #define NEW_AUX_ENT(nr, id, val) \
183 __put_user ((id), sp+(nr*2)); \
184 __put_user ((val), sp+(nr*2+1)); \
187 NEW_AUX_ENT(0, AT_NULL, 0);
190 NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
192 sp -= DLINFO_ITEMS*2;
193 NEW_AUX_ENT( 0, AT_HWCAP, hwcap);
194 NEW_AUX_ENT( 1, AT_PAGESZ, ELF_EXEC_PAGESIZE);
195 NEW_AUX_ENT( 2, AT_CLKTCK, CLOCKS_PER_SEC);
196 NEW_AUX_ENT( 3, AT_PHDR, load_addr + exec->e_phoff);
197 NEW_AUX_ENT( 4, AT_PHENT, sizeof (struct elf_phdr));
198 NEW_AUX_ENT( 5, AT_PHNUM, exec->e_phnum);
199 NEW_AUX_ENT( 6, AT_BASE, interp_load_addr);
200 NEW_AUX_ENT( 7, AT_FLAGS, 0);
201 NEW_AUX_ENT( 8, AT_ENTRY, load_bias + exec->e_entry);
202 NEW_AUX_ENT( 9, AT_UID, (elf_addr_t) current->uid);
203 NEW_AUX_ENT(10, AT_EUID, (elf_addr_t) current->euid);
204 NEW_AUX_ENT(11, AT_GID, (elf_addr_t) current->gid);
205 NEW_AUX_ENT(12, AT_EGID, (elf_addr_t) current->egid);
208 * ARCH_DLINFO must come last so platform specific code can enforce
209 * special alignment requirements on the AUXV if necessary (eg. PPC).
216 envp = (elf_caddr_t *) sp;
218 argv = (elf_caddr_t *) sp;
220 __put_user((elf_addr_t)(unsigned long) envp,--sp);
221 __put_user((elf_addr_t)(unsigned long) argv,--sp);
224 __put_user((elf_addr_t)argc,--sp);
225 current->mm->arg_start = current->mm->arg_end = (unsigned long) p;
227 __put_user((elf_caddr_t)(unsigned long)p,argv++);
228 len = strnlen_user(p, PAGE_SIZE*MAX_ARG_PAGES);
229 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
233 __put_user(NULL, argv);
234 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
236 __put_user((elf_caddr_t)(unsigned long)p,envp++);
237 len = strnlen_user(p, PAGE_SIZE*MAX_ARG_PAGES);
238 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
242 __put_user(NULL, envp);
243 current->mm->env_end = (unsigned long) p;
249 static inline unsigned long
250 elf_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
252 unsigned long map_addr;
254 down_write(¤t->mm->mmap_sem);
255 map_addr = do_mmap(filep, ELF_PAGESTART(addr),
256 eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr), prot, type,
257 eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
258 up_write(¤t->mm->mmap_sem);
262 #endif /* !elf_map */
264 /* This is much more generalized than the library routine read function,
265 so we keep this separate. Technically the library read function
266 is only provided so that we can read a.out libraries that have
269 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
270 struct file * interpreter,
271 unsigned long *interp_load_addr)
273 struct elf_phdr *elf_phdata;
274 struct elf_phdr *eppnt;
275 unsigned long load_addr = 0;
276 int load_addr_set = 0;
277 unsigned long last_bss = 0, elf_bss = 0;
278 unsigned long error = ~0UL;
281 /* First of all, some simple consistency checks */
282 if (interp_elf_ex->e_type != ET_EXEC &&
283 interp_elf_ex->e_type != ET_DYN)
285 if (!elf_check_arch(interp_elf_ex))
287 if (!interpreter->f_op || !interpreter->f_op->mmap)
291 * If the size of this structure has changed, then punt, since
292 * we will be doing the wrong thing.
294 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
297 if (interp_elf_ex->e_phnum < 1 ||
298 interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
301 /* Now read in all of the header information */
303 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
304 if (size > ELF_MIN_ALIGN)
306 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
310 retval = kernel_read(interpreter,interp_elf_ex->e_phoff,(char *)elf_phdata,size);
312 if (retval != size) {
319 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
320 if (eppnt->p_type == PT_LOAD) {
321 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
323 unsigned long vaddr = 0;
324 unsigned long k, map_addr;
326 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
327 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
328 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
329 vaddr = eppnt->p_vaddr;
330 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
331 elf_type |= MAP_FIXED;
333 map_addr = elf_map(interpreter, load_addr + vaddr, eppnt, elf_prot, elf_type);
334 if (BAD_ADDR(map_addr))
337 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
338 load_addr = map_addr - ELF_PAGESTART(vaddr);
343 * Check to see if the section's size will overflow the
344 * allowed task size. Note that p_filesz must always be
345 * <= p_memsize so it is only necessary to check p_memsz.
347 k = load_addr + eppnt->p_vaddr;
348 if (k > TASK_SIZE || eppnt->p_filesz > eppnt->p_memsz ||
349 eppnt->p_memsz > TASK_SIZE || TASK_SIZE - eppnt->p_memsz < k) {
355 * Find the end of the file mapping for this phdr, and keep
356 * track of the largest address we see for this.
358 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
363 * Do the same thing for the memory mapping - between
364 * elf_bss and last_bss is the bss section.
366 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
372 /* Now use mmap to map the library into memory. */
375 * Now fill out the bss section. First pad the last page up
376 * to the page boundary, and then perform a mmap to make sure
377 * that there are zero-mapped pages up to and including the
381 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1); /* What we have mapped so far */
383 /* Map the last of the bss segment */
384 if (last_bss > elf_bss) {
385 down_write(¤t->mm->mmap_sem);
386 error = do_brk(elf_bss, last_bss - elf_bss);
387 up_write(¤t->mm->mmap_sem);
392 *interp_load_addr = load_addr;
394 * XXX: is everything deallocated properly if this happens
395 * to be ~0UL (that is, we succeeded, but the header is broken
396 * and thus the caller will think that we failed)? We'd better
397 * switch to out-of-band error reporting.
399 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
407 static unsigned long load_aout_interp(struct exec * interp_ex,
408 struct file * interpreter)
410 unsigned long text_data, elf_entry = ~0UL;
414 current->mm->end_code = interp_ex->a_text;
415 text_data = interp_ex->a_text + interp_ex->a_data;
416 current->mm->end_data = text_data;
417 current->mm->brk = interp_ex->a_bss + text_data;
419 switch (N_MAGIC(*interp_ex)) {
426 offset = N_TXTOFF(*interp_ex);
427 addr = (char *) N_TXTADDR(*interp_ex);
433 down_write(¤t->mm->mmap_sem);
434 do_brk(0, text_data);
435 up_write(¤t->mm->mmap_sem);
437 if (!interpreter->f_op || !interpreter->f_op->read)
439 if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
441 flush_icache_range((unsigned long)addr,
442 (unsigned long)addr + text_data);
444 down_write(¤t->mm->mmap_sem);
445 do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
447 up_write(¤t->mm->mmap_sem);
449 elf_entry = interp_ex->a_entry;
456 * These are the functions used to load ELF style executables and shared
457 * libraries. There is no binary dependent code anywhere else.
460 #define INTERPRETER_NONE 0
461 #define INTERPRETER_AOUT 1
462 #define INTERPRETER_ELF 2
465 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
467 struct file *interpreter = NULL; /* to shut gcc up */
468 unsigned long load_addr = 0, load_bias = 0;
469 int load_addr_set = 0;
470 char * elf_interpreter = NULL;
471 unsigned int interpreter_type = INTERPRETER_NONE;
472 unsigned char ibcs2_interpreter = 0;
474 struct elf_phdr * elf_ppnt, *elf_phdata;
475 unsigned long elf_bss, k, elf_brk;
479 unsigned long elf_entry, interp_load_addr = 0;
480 unsigned long start_code, end_code, start_data, end_data;
481 unsigned long reloc_func_desc = 0;
482 struct elfhdr elf_ex;
483 struct elfhdr interp_elf_ex;
484 struct exec interp_ex;
485 char passed_fileno[6];
486 struct files_struct *files;
488 /* Get the exec-header */
489 elf_ex = *((struct elfhdr *) bprm->buf);
492 /* First of all, some simple consistency checks */
493 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
496 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
498 if (!elf_check_arch(&elf_ex))
500 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
503 /* Now read in all of the header information */
505 if (elf_ex.e_phentsize != sizeof(struct elf_phdr))
507 if (elf_ex.e_phnum < 1 ||
508 elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
510 size = elf_ex.e_phnum * sizeof(struct elf_phdr);
512 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
516 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *) elf_phdata, size);
517 if (retval != size) {
523 files = current->files; /* Refcounted so ok */
524 retval = unshare_files();
527 if (files == current->files) {
528 put_files_struct(files);
532 /* exec will make our files private anyway, but for the a.out
533 loader stuff we need to do it earlier */
535 retval = get_unused_fd();
538 get_file(bprm->file);
539 fd_install(elf_exec_fileno = retval, bprm->file);
541 elf_ppnt = elf_phdata;
550 for (i = 0; i < elf_ex.e_phnum; i++) {
551 if (elf_ppnt->p_type == PT_INTERP) {
552 /* This is the program interpreter used for
553 * shared libraries - for now assume that this
554 * is an a.out format binary
558 if (elf_ppnt->p_filesz > PATH_MAX ||
559 elf_ppnt->p_filesz < 2)
563 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
565 if (!elf_interpreter)
568 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
571 if (retval != elf_ppnt->p_filesz) {
574 goto out_free_interp;
576 /* make sure path is NULL terminated */
578 if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
579 goto out_free_interp;
581 /* If the program interpreter is one of these two,
582 * then assume an iBCS2 image. Otherwise assume
583 * a native linux image.
585 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
586 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
587 ibcs2_interpreter = 1;
589 printk("Using ELF interpreter %s\n", elf_interpreter);
592 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
594 interpreter = open_exec(elf_interpreter);
595 retval = PTR_ERR(interpreter);
596 if (IS_ERR(interpreter))
597 goto out_free_interp;
598 retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
599 if (retval != BINPRM_BUF_SIZE) {
602 goto out_free_dentry;
605 /* Get the exec headers */
606 interp_ex = *((struct exec *) bprm->buf);
607 interp_elf_ex = *((struct elfhdr *) bprm->buf);
613 /* Some simple consistency checks for the interpreter */
614 if (elf_interpreter) {
615 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
617 /* Now figure out which format our binary is */
618 if ((N_MAGIC(interp_ex) != OMAGIC) &&
619 (N_MAGIC(interp_ex) != ZMAGIC) &&
620 (N_MAGIC(interp_ex) != QMAGIC))
621 interpreter_type = INTERPRETER_ELF;
623 if (memcmp(interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
624 interpreter_type &= ~INTERPRETER_ELF;
627 if (!interpreter_type)
628 goto out_free_dentry;
630 /* Make sure only one type was selected */
631 if ((interpreter_type & INTERPRETER_ELF) &&
632 interpreter_type != INTERPRETER_ELF) {
633 // FIXME - ratelimit this before re-enabling
634 // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
635 interpreter_type = INTERPRETER_ELF;
637 /* Verify the interpreter has a valid arch */
638 if ((interpreter_type == INTERPRETER_ELF) &&
639 !elf_check_arch(&interp_elf_ex))
640 goto out_free_dentry;
642 /* Executables without an interpreter also need a personality */
643 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
646 if (BAD_ADDR(elf_ex.e_entry)) {
648 goto out_free_dentry;
651 /* OK, we are done with that, now set up the arg stuff,
652 and then start this sucker up */
654 if (!bprm->sh_bang) {
657 if (interpreter_type == INTERPRETER_AOUT) {
658 sprintf(passed_fileno, "%d", elf_exec_fileno);
659 passed_p = passed_fileno;
661 if (elf_interpreter) {
662 retval = copy_strings_kernel(1,&passed_p,bprm);
664 goto out_free_dentry;
670 /* Flush all traces of the currently running executable */
671 retval = flush_old_exec(bprm);
673 goto out_free_dentry;
675 /* Discard our unneeded old files struct */
678 put_files_struct(files);
682 /* OK, This is the point of no return */
683 current->mm->start_data = 0;
684 current->mm->end_data = 0;
685 current->mm->end_code = 0;
686 current->mm->mmap = NULL;
687 current->flags &= ~PF_FORKNOEXEC;
688 elf_entry = (unsigned long) elf_ex.e_entry;
690 /* Do this so that we can load the interpreter, if need be. We will
691 change some of these later */
692 current->mm->rss = 0;
693 retval = setup_arg_pages(bprm);
695 send_sig(SIGKILL, current, 0);
699 current->mm->start_stack = bprm->p;
701 /* Now we do a little grungy work by mmaping the ELF image into
702 the correct location in memory. At this point, we assume that
703 the image should be loaded at fixed address, not at a variable
706 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
707 int elf_prot = 0, elf_flags;
710 if (elf_ppnt->p_type != PT_LOAD)
713 if (unlikely (elf_brk > elf_bss)) {
716 /* There was a PT_LOAD segment with p_memsz > p_filesz
717 before this one. Map anonymous pages, if needed,
718 and clear the area. */
719 retval = set_brk (elf_bss + load_bias,
720 elf_brk + load_bias);
722 send_sig(SIGKILL, current, 0);
723 goto out_free_dentry;
725 nbyte = ELF_PAGEOFFSET(elf_bss);
727 nbyte = ELF_MIN_ALIGN - nbyte;
728 if (nbyte > elf_brk - elf_bss)
729 nbyte = elf_brk - elf_bss;
730 clear_user((void *) elf_bss + load_bias, nbyte);
734 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
735 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
736 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
738 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
740 vaddr = elf_ppnt->p_vaddr;
741 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
742 elf_flags |= MAP_FIXED;
743 } else if (elf_ex.e_type == ET_DYN) {
744 /* Try and get dynamic programs out of the way of the default mmap
745 base, as well as whatever program they might try to exec. This
746 is because the brk will follow the loader, and is not movable. */
747 load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
750 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags);
751 if (BAD_ADDR(error)) {
752 send_sig(SIGKILL, current, 0);
753 goto out_free_dentry;
756 if (!load_addr_set) {
758 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
759 if (elf_ex.e_type == ET_DYN) {
761 ELF_PAGESTART(load_bias + vaddr);
762 load_addr += load_bias;
763 reloc_func_desc = load_addr;
766 k = elf_ppnt->p_vaddr;
767 if (k < start_code) start_code = k;
768 if (start_data < k) start_data = k;
771 * Check to see if the section's size will overflow the
772 * allowed task size. Note that p_filesz must always be
773 * <= p_memsz so it is only necessary to check p_memsz.
775 if (k > TASK_SIZE || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
776 elf_ppnt->p_memsz > TASK_SIZE ||
777 TASK_SIZE - elf_ppnt->p_memsz < k) {
778 /* set_brk can never work. Avoid overflows. */
779 send_sig(SIGKILL, current, 0);
780 goto out_free_dentry;
783 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
787 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
791 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
796 elf_entry += load_bias;
797 elf_bss += load_bias;
798 elf_brk += load_bias;
799 start_code += load_bias;
800 end_code += load_bias;
801 start_data += load_bias;
802 end_data += load_bias;
804 /* Calling set_brk effectively mmaps the pages that we need
805 * for the bss and break sections. We must do this before
806 * mapping in the interpreter, to make sure it doesn't wind
807 * up getting placed where the bss needs to go.
809 retval = set_brk(elf_bss, elf_brk);
811 send_sig(SIGKILL, current, 0);
812 goto out_free_dentry;
816 if (elf_interpreter) {
817 if (interpreter_type == INTERPRETER_AOUT)
818 elf_entry = load_aout_interp(&interp_ex,
821 elf_entry = load_elf_interp(&interp_elf_ex,
824 if (BAD_ADDR(elf_entry)) {
825 printk(KERN_ERR "Unable to load interpreter %.128s\n",
827 force_sig(SIGSEGV, current);
828 retval = IS_ERR((void *)elf_entry) ? PTR_ERR((void *)elf_entry) : -ENOEXEC;
829 goto out_free_dentry;
831 reloc_func_desc = interp_load_addr;
833 allow_write_access(interpreter);
835 kfree(elf_interpreter);
840 if (interpreter_type != INTERPRETER_AOUT)
841 sys_close(elf_exec_fileno);
843 set_binfmt(&elf_format);
846 current->flags &= ~PF_FORKNOEXEC;
847 bprm->p = (unsigned long)
848 create_elf_tables((char *)bprm->p,
852 load_addr, load_bias,
854 (interpreter_type == INTERPRETER_AOUT ? 0 : 1));
855 /* N.B. passed_fileno might not be initialized? */
856 if (interpreter_type == INTERPRETER_AOUT)
857 current->mm->arg_start += strlen(passed_fileno) + 1;
858 current->mm->start_brk = current->mm->brk = elf_brk;
859 current->mm->end_code = end_code;
860 current->mm->start_code = start_code;
861 current->mm->start_data = start_data;
862 current->mm->end_data = end_data;
863 current->mm->start_stack = bprm->p;
866 printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
867 printk("(end_code) %lx\n" , (long) current->mm->end_code);
868 printk("(start_code) %lx\n" , (long) current->mm->start_code);
869 printk("(start_data) %lx\n" , (long) current->mm->start_data);
870 printk("(end_data) %lx\n" , (long) current->mm->end_data);
871 printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
872 printk("(brk) %lx\n" , (long) current->mm->brk);
875 if (current->personality & MMAP_PAGE_ZERO) {
876 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
877 and some applications "depend" upon this behavior.
878 Since we do not have the power to recompile these, we
879 emulate the SVr4 behavior. Sigh. */
880 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
881 down_write(¤t->mm->mmap_sem);
882 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
883 MAP_FIXED | MAP_PRIVATE, 0);
884 up_write(¤t->mm->mmap_sem);
889 * The ABI may specify that certain registers be set up in special
890 * ways (on i386 %edx is the address of a DT_FINI function, for
891 * example. In addition, it may also specify (eg, PowerPC64 ELF)
892 * that the e_entry field is the address of the function descriptor
893 * for the startup routine, rather than the address of the startup
894 * routine itself. This macro performs whatever initialization to
895 * the regs structure is required as well as any relocations to the
896 * function descriptor entries when executing dynamically linked apps.
898 ELF_PLAT_INIT(regs, reloc_func_desc);
901 start_thread(regs, elf_entry, bprm->p);
902 if (current->ptrace & PT_PTRACED)
903 send_sig(SIGTRAP, current, 0);
910 allow_write_access(interpreter);
915 kfree(elf_interpreter);
917 sys_close(elf_exec_fileno);
920 put_files_struct(current->files);
921 current->files = files;
928 /* This is really simpleminded and specialized - we are loading an
929 a.out library that is given an ELF header. */
931 static int load_elf_library(struct file *file)
933 struct elf_phdr *elf_phdata;
934 struct elf_phdr *eppnt;
935 unsigned long elf_bss, bss, len;
936 int retval, error, i, j;
937 struct elfhdr elf_ex;
940 retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
941 if (retval != sizeof(elf_ex))
944 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
947 /* First of all, some simple consistency checks */
948 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
949 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
952 /* Now read in all of the header information */
954 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
955 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
958 elf_phdata = kmalloc(j, GFP_KERNEL);
964 retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
968 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
969 if ((eppnt + i)->p_type == PT_LOAD) j++;
973 while (eppnt->p_type != PT_LOAD)
976 /* Now use mmap to map the library into memory. */
977 down_write(¤t->mm->mmap_sem);
978 error = do_mmap(file,
979 ELF_PAGESTART(eppnt->p_vaddr),
981 ELF_PAGEOFFSET(eppnt->p_vaddr)),
982 PROT_READ | PROT_WRITE | PROT_EXEC,
983 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
985 ELF_PAGEOFFSET(eppnt->p_vaddr)));
986 up_write(¤t->mm->mmap_sem);
987 if (error != ELF_PAGESTART(eppnt->p_vaddr))
990 elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
993 len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr + ELF_MIN_ALIGN - 1);
994 bss = eppnt->p_memsz + eppnt->p_vaddr;
996 down_write(¤t->mm->mmap_sem);
997 do_brk(len, bss - len);
998 up_write(¤t->mm->mmap_sem);
1009 * Note that some platforms still use traditional core dumps and not
1010 * the ELF core dump. Each platform can select it as appropriate.
1012 #ifdef USE_ELF_CORE_DUMP
1017 * Modelled on fs/exec.c:aout_core_dump()
1018 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1021 * These are the only things you should do on a core-file: use only these
1022 * functions to write out all the necessary info.
1024 static int dump_write(struct file *file, const void *addr, int nr)
1026 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1029 static int dump_seek(struct file *file, off_t off)
1031 if (file->f_op->llseek) {
1032 if (file->f_op->llseek(file, off, 0) != off)
1040 * Decide whether a segment is worth dumping; default is yes to be
1041 * sure (missing info is worse than too much; etc).
1042 * Personally I'd include everything, and use the coredump limit...
1044 * I think we should skip something. But I am not sure how. H.J.
1046 static inline int maydump(struct vm_area_struct *vma)
1049 * If we may not read the contents, don't allow us to dump
1050 * them either. "dump_write()" can't handle it anyway.
1052 if (!(vma->vm_flags & VM_READ))
1055 /* Do not dump I/O mapped devices! -DaveM */
1056 if (vma->vm_flags & VM_IO)
1059 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
1061 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
1067 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
1069 /* An ELF note in memory */
1074 unsigned int datasz;
1078 static int notesize(struct memelfnote *en)
1082 sz = sizeof(struct elf_note);
1083 sz += roundup(strlen(en->name), 4);
1084 sz += roundup(en->datasz, 4);
1092 static void dump_regs(const char *str, elf_greg_t *r)
1095 static const char *regs[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
1096 "eax", "ds", "es", "fs", "gs",
1097 "orig_eax", "eip", "cs",
1098 "efl", "uesp", "ss"};
1099 printk("Registers: %s\n", str);
1101 for(i = 0; i < ELF_NGREG; i++)
1103 unsigned long val = r[i];
1104 printk(" %-2d %-5s=%08lx %lu\n", i, regs[i], val, val);
1109 #define DUMP_WRITE(addr, nr) \
1110 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1111 #define DUMP_SEEK(off) \
1112 do { if (!dump_seek(file, (off))) return 0; } while(0)
1114 static int writenote(struct memelfnote *men, struct file *file)
1118 en.n_namesz = strlen(men->name);
1119 en.n_descsz = men->datasz;
1120 en.n_type = men->type;
1122 DUMP_WRITE(&en, sizeof(en));
1123 DUMP_WRITE(men->name, en.n_namesz);
1124 /* XXX - cast from long long to long to avoid need for libgcc.a */
1125 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1126 DUMP_WRITE(men->data, men->datasz);
1127 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1134 #define DUMP_WRITE(addr, nr) \
1135 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1137 #define DUMP_SEEK(off) \
1138 if (!dump_seek(file, (off))) \
1143 * This is a two-pass process; first we find the offsets of the bits,
1144 * and then they are actually written out. If we run out of core limit
1147 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
1154 struct vm_area_struct *vma;
1156 off_t offset = 0, dataoff;
1157 unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1159 struct memelfnote notes[4];
1160 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1161 elf_fpregset_t fpu; /* NT_PRFPREG */
1162 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1164 /* first copy the parameters from user space */
1165 memset(&psinfo, 0, sizeof(psinfo));
1167 unsigned int i, len;
1169 len = current->mm->arg_end - current->mm->arg_start;
1170 if (len >= ELF_PRARGSZ)
1171 len = ELF_PRARGSZ-1;
1172 copy_from_user(&psinfo.pr_psargs,
1173 (const char *)current->mm->arg_start, len);
1174 for(i = 0; i < len; i++)
1175 if (psinfo.pr_psargs[i] == 0)
1176 psinfo.pr_psargs[i] = ' ';
1177 psinfo.pr_psargs[len] = 0;
1181 memset(&prstatus, 0, sizeof(prstatus));
1183 * This transfers the registers from regs into the standard
1184 * coredump arrangement, whatever that is.
1186 #ifdef ELF_CORE_COPY_REGS
1187 ELF_CORE_COPY_REGS(prstatus.pr_reg, regs)
1189 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs))
1191 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1192 (long)sizeof(elf_gregset_t), (long)sizeof(struct pt_regs));
1195 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1198 /* now stop all vm operations */
1199 down_write(¤t->mm->mmap_sem);
1200 segs = current->mm->map_count;
1203 printk("elf_core_dump: %d segs %lu limit\n", segs, limit);
1207 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1208 elf.e_ident[EI_CLASS] = ELF_CLASS;
1209 elf.e_ident[EI_DATA] = ELF_DATA;
1210 elf.e_ident[EI_VERSION] = EV_CURRENT;
1211 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1213 elf.e_type = ET_CORE;
1214 elf.e_machine = ELF_ARCH;
1215 elf.e_version = EV_CURRENT;
1217 elf.e_phoff = sizeof(elf);
1220 elf.e_ehsize = sizeof(elf);
1221 elf.e_phentsize = sizeof(struct elf_phdr);
1222 elf.e_phnum = segs+1; /* Include notes */
1223 elf.e_shentsize = 0;
1231 current->flags |= PF_DUMPCORE;
1233 DUMP_WRITE(&elf, sizeof(elf));
1234 offset += sizeof(elf); /* Elf header */
1235 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1238 * Set up the notes in similar form to SVR4 core dumps made
1239 * with info from their /proc.
1242 notes[0].name = "CORE";
1243 notes[0].type = NT_PRSTATUS;
1244 notes[0].datasz = sizeof(prstatus);
1245 notes[0].data = &prstatus;
1246 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1247 prstatus.pr_sigpend = current->pending.signal.sig[0];
1248 prstatus.pr_sighold = current->blocked.sig[0];
1249 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1250 psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1251 psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1252 psinfo.pr_sid = prstatus.pr_sid = current->session;
1253 prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
1254 prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
1255 prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
1256 prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
1257 prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
1258 prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
1259 prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
1260 prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
1263 dump_regs("Passed in regs", (elf_greg_t *)regs);
1264 dump_regs("prstatus regs", (elf_greg_t *)&prstatus.pr_reg);
1267 notes[1].name = "CORE";
1268 notes[1].type = NT_PRPSINFO;
1269 notes[1].datasz = sizeof(psinfo);
1270 notes[1].data = &psinfo;
1271 i = current->state ? ffz(~current->state) + 1 : 0;
1272 psinfo.pr_state = i;
1273 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1274 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1275 psinfo.pr_nice = current->nice;
1276 psinfo.pr_flag = current->flags;
1277 psinfo.pr_uid = NEW_TO_OLD_UID(current->uid);
1278 psinfo.pr_gid = NEW_TO_OLD_GID(current->gid);
1279 strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1281 notes[2].name = "CORE";
1282 notes[2].type = NT_TASKSTRUCT;
1283 notes[2].datasz = sizeof(*current);
1284 notes[2].data = current;
1286 /* Try to dump the FPU. */
1287 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1288 if (!prstatus.pr_fpvalid)
1294 notes[3].name = "CORE";
1295 notes[3].type = NT_PRFPREG;
1296 notes[3].datasz = sizeof(fpu);
1297 notes[3].data = &fpu;
1300 /* Write notes phdr entry */
1302 struct elf_phdr phdr;
1305 for(i = 0; i < numnote; i++)
1306 sz += notesize(¬es[i]);
1308 phdr.p_type = PT_NOTE;
1309 phdr.p_offset = offset;
1317 offset += phdr.p_filesz;
1318 DUMP_WRITE(&phdr, sizeof(phdr));
1321 /* Page-align dumped data */
1322 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1324 /* Write program headers for segments dump */
1325 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1326 struct elf_phdr phdr;
1329 sz = vma->vm_end - vma->vm_start;
1331 phdr.p_type = PT_LOAD;
1332 phdr.p_offset = offset;
1333 phdr.p_vaddr = vma->vm_start;
1335 phdr.p_filesz = maydump(vma) ? sz : 0;
1337 offset += phdr.p_filesz;
1338 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1339 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1340 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1341 phdr.p_align = ELF_EXEC_PAGESIZE;
1343 DUMP_WRITE(&phdr, sizeof(phdr));
1346 for(i = 0; i < numnote; i++)
1347 if (!writenote(¬es[i], file))
1352 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1359 printk("elf_core_dump: writing %08lx-%08lx\n", vma->vm_start, vma->vm_end);
1362 for (addr = vma->vm_start;
1364 addr += PAGE_SIZE) {
1366 struct vm_area_struct *vma;
1368 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1369 &page, &vma) <= 0) {
1370 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1372 if (page == ZERO_PAGE(addr)) {
1373 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1376 flush_cache_page(vma, addr);
1378 DUMP_WRITE(kaddr, PAGE_SIZE);
1379 flush_page_to_ram(page);
1387 if ((off_t) file->f_pos != offset) {
1389 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1390 (off_t) file->f_pos, offset);
1395 up_write(¤t->mm->mmap_sem);
1398 #endif /* USE_ELF_CORE_DUMP */
1400 static int __init init_elf_binfmt(void)
1402 return register_binfmt(&elf_format);
1405 static void __exit exit_elf_binfmt(void)
1407 /* Remove the COFF and ELF loaders. */
1408 unregister_binfmt(&elf_format);
1411 module_init(init_elf_binfmt)
1412 module_exit(exit_elf_binfmt)
1413 MODULE_LICENSE("GPL");