2 * linux/fs/binfmt_elf.c
4 * These are the functions used to load ELF format executables as used
5 * on SVr4 machines. Information on the format may be found in the book
6 * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
9 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
12 #include <linux/module.h>
13 #include <linux/kernel.h>
15 #include <linux/stat.h>
16 #include <linux/time.h>
18 #include <linux/mman.h>
19 #include <linux/a.out.h>
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/binfmts.h>
23 #include <linux/string.h>
24 #include <linux/file.h>
25 #include <linux/fcntl.h>
26 #include <linux/ptrace.h>
27 #include <linux/slab.h>
28 #include <linux/shm.h>
29 #include <linux/personality.h>
30 #include <linux/elfcore.h>
31 #include <linux/init.h>
32 #include <linux/highuid.h>
33 #include <linux/smp.h>
34 #include <linux/smp_lock.h>
35 #include <linux/compiler.h>
36 #include <linux/highmem.h>
37 #include <linux/pagemap.h>
38 #include <linux/security.h>
40 #include <asm/uaccess.h>
41 #include <asm/param.h>
42 #include <asm/pgalloc.h>
44 #include <linux/elf.h>
46 static int load_elf_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
);
47 static int load_elf_library(struct file
*);
48 static unsigned long elf_map (struct file
*, unsigned long, struct elf_phdr
*, int, int);
49 extern int dump_fpu (struct pt_regs
*, elf_fpregset_t
*);
52 #define elf_addr_t unsigned long
56 * If we don't support core dumping, then supply a NULL so we
59 #ifdef USE_ELF_CORE_DUMP
60 static int elf_core_dump(long signr
, struct pt_regs
* regs
, struct file
* file
);
62 #define elf_core_dump NULL
65 #if ELF_EXEC_PAGESIZE > PAGE_SIZE
66 # define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
68 # define ELF_MIN_ALIGN PAGE_SIZE
71 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
72 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
73 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
75 static struct linux_binfmt elf_format
= {
76 .module
= THIS_MODULE
,
77 .load_binary
= load_elf_binary
,
78 .load_shlib
= load_elf_library
,
79 .core_dump
= elf_core_dump
,
80 .min_coredump
= ELF_EXEC_PAGESIZE
83 #define BAD_ADDR(x) ((unsigned long)(x) > TASK_SIZE)
85 static void set_brk(unsigned long start
, unsigned long end
)
87 start
= ELF_PAGEALIGN(start
);
88 end
= ELF_PAGEALIGN(end
);
90 do_brk(start
, end
- start
);
91 current
->mm
->start_brk
= current
->mm
->brk
= end
;
95 /* We need to explicitly zero any fractional pages
96 after the data section (i.e. bss). This would
97 contain the junk from the file that should not
101 static void padzero(unsigned long elf_bss
)
105 nbyte
= ELF_PAGEOFFSET(elf_bss
);
107 nbyte
= ELF_MIN_ALIGN
- nbyte
;
108 clear_user((void *) elf_bss
, nbyte
);
112 /* Let's use some macros to make this stack manipulation a litle clearer */
113 #ifdef CONFIG_STACK_GROWSUP
114 #define STACK_ADD(sp, items) ((elf_addr_t *)(sp) + (items))
115 #define STACK_ROUND(sp, items) \
116 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
117 #define STACK_ALLOC(sp, len) ({ elf_addr_t *old_sp = (elf_addr_t *)sp; sp += len; old_sp; })
119 #define STACK_ADD(sp, items) ((elf_addr_t *)(sp) - (items))
120 #define STACK_ROUND(sp, items) \
121 (((unsigned long) (sp - items)) &~ 15UL)
122 #define STACK_ALLOC(sp, len) sp -= len
126 create_elf_tables(struct linux_binprm
*bprm
, struct elfhdr
* exec
,
127 int interp_aout
, unsigned long load_addr
,
128 unsigned long interp_load_addr
)
130 unsigned long p
= bprm
->p
;
131 int argc
= bprm
->argc
;
132 int envc
= bprm
->envc
;
133 elf_addr_t
*argv
, *envp
;
134 elf_addr_t
*sp
, *u_platform
;
135 const char *k_platform
= ELF_PLATFORM
;
137 elf_addr_t elf_info
[40];
139 struct task_struct
*tsk
= current
;
142 * If this architecture has a platform capability string, copy it
143 * to userspace. In some cases (Sparc), this info is impossible
144 * for userspace to get any other way, in others (i386) it is
150 size_t len
= strlen(k_platform
) + 1;
154 * In some cases (e.g. Hyper-Threading), we want to avoid L1
155 * evictions by the processes running on the same package. One
156 * thing we can do is to shuffle the initial stack for them.
158 * The conditionals here are unneeded, but kept in to make the
159 * code behaviour the same as pre change unless we have
160 * hyperthreaded processors. This should be cleaned up
164 if (smp_num_siblings
> 1)
165 STACK_ALLOC(p
, ((current
->pid
% 64) << 7));
167 u_platform
= (elf_addr_t
*) STACK_ALLOC(p
, len
);
168 __copy_to_user(u_platform
, k_platform
, len
);
171 /* Create the ELF interpreter info */
172 #define NEW_AUX_ENT(id, val) \
173 do { elf_info[ei_index++] = id; elf_info[ei_index++] = val; } while (0)
177 * ARCH_DLINFO must come first so PPC can do its special alignment of
182 NEW_AUX_ENT(AT_HWCAP
, ELF_HWCAP
);
183 NEW_AUX_ENT(AT_PAGESZ
, ELF_EXEC_PAGESIZE
);
184 NEW_AUX_ENT(AT_CLKTCK
, CLOCKS_PER_SEC
);
185 NEW_AUX_ENT(AT_PHDR
, load_addr
+ exec
->e_phoff
);
186 NEW_AUX_ENT(AT_PHENT
, sizeof (struct elf_phdr
));
187 NEW_AUX_ENT(AT_PHNUM
, exec
->e_phnum
);
188 NEW_AUX_ENT(AT_BASE
, interp_load_addr
);
189 NEW_AUX_ENT(AT_FLAGS
, 0);
190 NEW_AUX_ENT(AT_ENTRY
, exec
->e_entry
);
191 NEW_AUX_ENT(AT_UID
, (elf_addr_t
) tsk
->uid
);
192 NEW_AUX_ENT(AT_EUID
, (elf_addr_t
) tsk
->euid
);
193 NEW_AUX_ENT(AT_GID
, (elf_addr_t
) tsk
->gid
);
194 NEW_AUX_ENT(AT_EGID
, (elf_addr_t
) tsk
->egid
);
195 NEW_AUX_ENT(AT_SECURE
, (elf_addr_t
) security_bprm_secureexec(bprm
));
197 NEW_AUX_ENT(AT_PLATFORM
, (elf_addr_t
)(long)u_platform
);
199 NEW_AUX_ENT(AT_NULL
, 0);
202 sp
= STACK_ADD(p
, ei_index
);
204 items
= (argc
+ 1) + (envc
+ 1);
206 items
+= 3; /* a.out interpreters require argv & envp too */
208 items
+= 1; /* ELF interpreters only put argc on the stack */
210 bprm
->p
= STACK_ROUND(sp
, items
);
212 /* Point sp at the lowest address on the stack */
213 #ifdef CONFIG_STACK_GROWSUP
214 sp
= (elf_addr_t
*)bprm
->p
- items
- ei_index
;
215 bprm
->exec
= (unsigned long) sp
; /* XXX: PARISC HACK */
217 sp
= (elf_addr_t
*)bprm
->p
;
220 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
221 __put_user(argc
, sp
++);
224 envp
= argv
+ argc
+ 1;
225 __put_user((elf_addr_t
)(long)argv
, sp
++);
226 __put_user((elf_addr_t
)(long)envp
, sp
++);
229 envp
= argv
+ argc
+ 1;
232 /* Populate argv and envp */
233 p
= current
->mm
->arg_start
;
236 __put_user((elf_addr_t
)p
, argv
++);
237 len
= strnlen_user((void *)p
, PAGE_SIZE
*MAX_ARG_PAGES
);
238 if (!len
|| len
> PAGE_SIZE
*MAX_ARG_PAGES
)
243 current
->mm
->arg_end
= current
->mm
->env_start
= p
;
246 __put_user((elf_addr_t
)p
, envp
++);
247 len
= strnlen_user((void *)p
, PAGE_SIZE
*MAX_ARG_PAGES
);
248 if (!len
|| len
> PAGE_SIZE
*MAX_ARG_PAGES
)
253 current
->mm
->env_end
= p
;
255 /* Put the elf_info on the stack in the right place. */
256 sp
= (elf_addr_t
*)envp
+ 1;
257 copy_to_user(sp
, elf_info
, ei_index
* sizeof(elf_addr_t
));
262 static unsigned long elf_map(struct file
*filep
, unsigned long addr
,
263 struct elf_phdr
*eppnt
, int prot
, int type
)
265 unsigned long map_addr
;
267 down_write(¤t
->mm
->mmap_sem
);
268 map_addr
= do_mmap(filep
, ELF_PAGESTART(addr
),
269 eppnt
->p_filesz
+ ELF_PAGEOFFSET(eppnt
->p_vaddr
), prot
, type
,
270 eppnt
->p_offset
- ELF_PAGEOFFSET(eppnt
->p_vaddr
));
271 up_write(¤t
->mm
->mmap_sem
);
275 #endif /* !elf_map */
277 /* This is much more generalized than the library routine read function,
278 so we keep this separate. Technically the library read function
279 is only provided so that we can read a.out libraries that have
282 static unsigned long load_elf_interp(struct elfhdr
* interp_elf_ex
,
283 struct file
* interpreter
,
284 unsigned long *interp_load_addr
)
286 struct elf_phdr
*elf_phdata
;
287 struct elf_phdr
*eppnt
;
288 unsigned long load_addr
= 0;
289 int load_addr_set
= 0;
290 unsigned long last_bss
= 0, elf_bss
= 0;
291 unsigned long error
= ~0UL;
294 /* First of all, some simple consistency checks */
295 if (interp_elf_ex
->e_type
!= ET_EXEC
&&
296 interp_elf_ex
->e_type
!= ET_DYN
)
298 if (!elf_check_arch(interp_elf_ex
))
300 if (!interpreter
->f_op
|| !interpreter
->f_op
->mmap
)
304 * If the size of this structure has changed, then punt, since
305 * we will be doing the wrong thing.
307 if (interp_elf_ex
->e_phentsize
!= sizeof(struct elf_phdr
))
309 if (interp_elf_ex
->e_phnum
> 65536U / sizeof(struct elf_phdr
))
312 /* Now read in all of the header information */
314 size
= sizeof(struct elf_phdr
) * interp_elf_ex
->e_phnum
;
315 if (size
> ELF_MIN_ALIGN
)
317 elf_phdata
= (struct elf_phdr
*) kmalloc(size
, GFP_KERNEL
);
321 retval
= kernel_read(interpreter
,interp_elf_ex
->e_phoff
,(char *)elf_phdata
,size
);
327 for (i
=0; i
<interp_elf_ex
->e_phnum
; i
++, eppnt
++) {
328 if (eppnt
->p_type
== PT_LOAD
) {
329 int elf_type
= MAP_PRIVATE
| MAP_DENYWRITE
;
331 unsigned long vaddr
= 0;
332 unsigned long k
, map_addr
;
334 if (eppnt
->p_flags
& PF_R
) elf_prot
= PROT_READ
;
335 if (eppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
336 if (eppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
337 vaddr
= eppnt
->p_vaddr
;
338 if (interp_elf_ex
->e_type
== ET_EXEC
|| load_addr_set
)
339 elf_type
|= MAP_FIXED
;
341 map_addr
= elf_map(interpreter
, load_addr
+ vaddr
, eppnt
, elf_prot
, elf_type
);
342 if (BAD_ADDR(map_addr
))
345 if (!load_addr_set
&& interp_elf_ex
->e_type
== ET_DYN
) {
346 load_addr
= map_addr
- ELF_PAGESTART(vaddr
);
351 * Find the end of the file mapping for this phdr, and keep
352 * track of the largest address we see for this.
354 k
= load_addr
+ eppnt
->p_vaddr
+ eppnt
->p_filesz
;
359 * Do the same thing for the memory mapping - between
360 * elf_bss and last_bss is the bss section.
362 k
= load_addr
+ eppnt
->p_memsz
+ eppnt
->p_vaddr
;
369 * Now fill out the bss section. First pad the last page up
370 * to the page boundary, and then perform a mmap to make sure
371 * that there are zero-mapped pages up to and including the
375 elf_bss
= ELF_PAGESTART(elf_bss
+ ELF_MIN_ALIGN
- 1); /* What we have mapped so far */
377 /* Map the last of the bss segment */
378 if (last_bss
> elf_bss
)
379 do_brk(elf_bss
, last_bss
- elf_bss
);
381 *interp_load_addr
= load_addr
;
382 error
= ((unsigned long) interp_elf_ex
->e_entry
) + load_addr
;
390 static unsigned long load_aout_interp(struct exec
* interp_ex
,
391 struct file
* interpreter
)
393 unsigned long text_data
, elf_entry
= ~0UL;
397 current
->mm
->end_code
= interp_ex
->a_text
;
398 text_data
= interp_ex
->a_text
+ interp_ex
->a_data
;
399 current
->mm
->end_data
= text_data
;
400 current
->mm
->brk
= interp_ex
->a_bss
+ text_data
;
402 switch (N_MAGIC(*interp_ex
)) {
409 offset
= N_TXTOFF(*interp_ex
);
410 addr
= (char *) N_TXTADDR(*interp_ex
);
416 do_brk(0, text_data
);
417 if (!interpreter
->f_op
|| !interpreter
->f_op
->read
)
419 if (interpreter
->f_op
->read(interpreter
, addr
, text_data
, &offset
) < 0)
421 flush_icache_range((unsigned long)addr
,
422 (unsigned long)addr
+ text_data
);
424 do_brk(ELF_PAGESTART(text_data
+ ELF_MIN_ALIGN
- 1),
426 elf_entry
= interp_ex
->a_entry
;
433 * These are the functions used to load ELF style executables and shared
434 * libraries. There is no binary dependent code anywhere else.
437 #define INTERPRETER_NONE 0
438 #define INTERPRETER_AOUT 1
439 #define INTERPRETER_ELF 2
442 static int load_elf_binary(struct linux_binprm
* bprm
, struct pt_regs
* regs
)
444 struct file
*interpreter
= NULL
; /* to shut gcc up */
445 unsigned long load_addr
= 0, load_bias
= 0;
446 int load_addr_set
= 0;
447 char * elf_interpreter
= NULL
;
448 unsigned int interpreter_type
= INTERPRETER_NONE
;
449 unsigned char ibcs2_interpreter
= 0;
451 struct elf_phdr
* elf_ppnt
, *elf_phdata
;
452 unsigned long elf_bss
, elf_brk
;
456 unsigned long elf_entry
, interp_load_addr
= 0;
457 unsigned long start_code
, end_code
, start_data
, end_data
;
458 unsigned long reloc_func_desc
= 0;
459 struct elfhdr elf_ex
;
460 struct elfhdr interp_elf_ex
;
461 struct exec interp_ex
;
462 char passed_fileno
[6];
464 /* Get the exec-header */
465 elf_ex
= *((struct elfhdr
*) bprm
->buf
);
468 /* First of all, some simple consistency checks */
469 if (memcmp(elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
472 if (elf_ex
.e_type
!= ET_EXEC
&& elf_ex
.e_type
!= ET_DYN
)
474 if (!elf_check_arch(&elf_ex
))
476 if (!bprm
->file
->f_op
||!bprm
->file
->f_op
->mmap
)
479 /* Now read in all of the header information */
482 if (elf_ex
.e_phentsize
!= sizeof(struct elf_phdr
))
484 if (elf_ex
.e_phnum
> 65536U / sizeof(struct elf_phdr
))
486 size
= elf_ex
.e_phnum
* sizeof(struct elf_phdr
);
487 elf_phdata
= (struct elf_phdr
*) kmalloc(size
, GFP_KERNEL
);
491 retval
= kernel_read(bprm
->file
, elf_ex
.e_phoff
, (char *) elf_phdata
, size
);
495 retval
= get_unused_fd();
498 get_file(bprm
->file
);
499 fd_install(elf_exec_fileno
= retval
, bprm
->file
);
501 elf_ppnt
= elf_phdata
;
510 for (i
= 0; i
< elf_ex
.e_phnum
; i
++) {
511 if (elf_ppnt
->p_type
== PT_INTERP
) {
512 /* This is the program interpreter used for
513 * shared libraries - for now assume that this
514 * is an a.out format binary
518 if (elf_ppnt
->p_filesz
> PATH_MAX
)
520 elf_interpreter
= (char *) kmalloc(elf_ppnt
->p_filesz
,
522 if (!elf_interpreter
)
525 retval
= kernel_read(bprm
->file
, elf_ppnt
->p_offset
,
529 goto out_free_interp
;
530 /* If the program interpreter is one of these two,
531 * then assume an iBCS2 image. Otherwise assume
532 * a native linux image.
534 if (strcmp(elf_interpreter
,"/usr/lib/libc.so.1") == 0 ||
535 strcmp(elf_interpreter
,"/usr/lib/ld.so.1") == 0)
536 ibcs2_interpreter
= 1;
539 * The early SET_PERSONALITY here is so that the lookup
540 * for the interpreter happens in the namespace of the
541 * to-be-execed image. SET_PERSONALITY can select an
544 * However, SET_PERSONALITY is NOT allowed to switch
545 * this task into the new images's memory mapping
546 * policy - that is, TASK_SIZE must still evaluate to
547 * that which is appropriate to the execing application.
548 * This is because exit_mmap() needs to have TASK_SIZE
549 * evaluate to the size of the old image.
551 * So if (say) a 64-bit application is execing a 32-bit
552 * application it is the architecture's responsibility
553 * to defer changing the value of TASK_SIZE until the
554 * switch really is going to happen - do this in
555 * flush_thread(). - akpm
557 SET_PERSONALITY(elf_ex
, ibcs2_interpreter
);
559 interpreter
= open_exec(elf_interpreter
);
560 retval
= PTR_ERR(interpreter
);
561 if (IS_ERR(interpreter
))
562 goto out_free_interp
;
563 retval
= kernel_read(interpreter
, 0, bprm
->buf
, BINPRM_BUF_SIZE
);
565 goto out_free_dentry
;
567 /* Get the exec headers */
568 interp_ex
= *((struct exec
*) bprm
->buf
);
569 interp_elf_ex
= *((struct elfhdr
*) bprm
->buf
);
575 /* Some simple consistency checks for the interpreter */
576 if (elf_interpreter
) {
577 interpreter_type
= INTERPRETER_ELF
| INTERPRETER_AOUT
;
579 /* Now figure out which format our binary is */
580 if ((N_MAGIC(interp_ex
) != OMAGIC
) &&
581 (N_MAGIC(interp_ex
) != ZMAGIC
) &&
582 (N_MAGIC(interp_ex
) != QMAGIC
))
583 interpreter_type
= INTERPRETER_ELF
;
585 if (memcmp(interp_elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
586 interpreter_type
&= ~INTERPRETER_ELF
;
589 if (!interpreter_type
)
590 goto out_free_dentry
;
592 /* Make sure only one type was selected */
593 if ((interpreter_type
& INTERPRETER_ELF
) &&
594 interpreter_type
!= INTERPRETER_ELF
) {
595 // FIXME - ratelimit this before re-enabling
596 // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
597 interpreter_type
= INTERPRETER_ELF
;
600 /* Executables without an interpreter also need a personality */
601 SET_PERSONALITY(elf_ex
, ibcs2_interpreter
);
604 /* OK, we are done with that, now set up the arg stuff,
605 and then start this sucker up */
607 if ((!bprm
->sh_bang
) && (interpreter_type
== INTERPRETER_AOUT
)) {
608 char *passed_p
= passed_fileno
;
609 sprintf(passed_fileno
, "%d", elf_exec_fileno
);
611 if (elf_interpreter
) {
612 retval
= copy_strings_kernel(1, &passed_p
, bprm
);
614 goto out_free_dentry
;
619 /* Flush all traces of the currently running executable */
620 retval
= flush_old_exec(bprm
);
622 goto out_free_dentry
;
624 /* OK, This is the point of no return */
625 current
->mm
->start_data
= 0;
626 current
->mm
->end_data
= 0;
627 current
->mm
->end_code
= 0;
628 current
->mm
->mmap
= NULL
;
629 current
->flags
&= ~PF_FORKNOEXEC
;
631 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
632 may depend on the personality. */
633 SET_PERSONALITY(elf_ex
, ibcs2_interpreter
);
635 /* Do this so that we can load the interpreter, if need be. We will
636 change some of these later */
637 current
->mm
->rss
= 0;
638 current
->mm
->free_area_cache
= TASK_UNMAPPED_BASE
;
639 retval
= setup_arg_pages(bprm
);
641 send_sig(SIGKILL
, current
, 0);
642 goto out_free_dentry
;
645 current
->mm
->start_stack
= bprm
->p
;
647 /* Now we do a little grungy work by mmaping the ELF image into
648 the correct location in memory. At this point, we assume that
649 the image should be loaded at fixed address, not at a variable
652 for(i
= 0, elf_ppnt
= elf_phdata
; i
< elf_ex
.e_phnum
; i
++, elf_ppnt
++) {
653 int elf_prot
= 0, elf_flags
;
654 unsigned long k
, vaddr
;
656 if (elf_ppnt
->p_type
!= PT_LOAD
)
659 if (unlikely (elf_brk
> elf_bss
)) {
662 /* There was a PT_LOAD segment with p_memsz > p_filesz
663 before this one. Map anonymous pages, if needed,
664 and clear the area. */
665 set_brk (elf_bss
+ load_bias
, elf_brk
+ load_bias
);
666 nbyte
= ELF_PAGEOFFSET(elf_bss
);
668 nbyte
= ELF_MIN_ALIGN
- nbyte
;
669 if (nbyte
> elf_brk
- elf_bss
)
670 nbyte
= elf_brk
- elf_bss
;
671 clear_user((void *) elf_bss
+ load_bias
, nbyte
);
675 if (elf_ppnt
->p_flags
& PF_R
) elf_prot
|= PROT_READ
;
676 if (elf_ppnt
->p_flags
& PF_W
) elf_prot
|= PROT_WRITE
;
677 if (elf_ppnt
->p_flags
& PF_X
) elf_prot
|= PROT_EXEC
;
679 elf_flags
= MAP_PRIVATE
|MAP_DENYWRITE
|MAP_EXECUTABLE
;
681 vaddr
= elf_ppnt
->p_vaddr
;
682 if (elf_ex
.e_type
== ET_EXEC
|| load_addr_set
) {
683 elf_flags
|= MAP_FIXED
;
684 } else if (elf_ex
.e_type
== ET_DYN
) {
685 /* Try and get dynamic programs out of the way of the default mmap
686 base, as well as whatever program they might try to exec. This
687 is because the brk will follow the loader, and is not movable. */
688 load_bias
= ELF_PAGESTART(ELF_ET_DYN_BASE
- vaddr
);
691 error
= elf_map(bprm
->file
, load_bias
+ vaddr
, elf_ppnt
, elf_prot
, elf_flags
);
695 if (!load_addr_set
) {
697 load_addr
= (elf_ppnt
->p_vaddr
- elf_ppnt
->p_offset
);
698 if (elf_ex
.e_type
== ET_DYN
) {
700 ELF_PAGESTART(load_bias
+ vaddr
);
701 load_addr
+= load_bias
;
702 reloc_func_desc
= load_bias
;
705 k
= elf_ppnt
->p_vaddr
;
706 if (k
< start_code
) start_code
= k
;
707 if (start_data
< k
) start_data
= k
;
709 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_filesz
;
713 if ((elf_ppnt
->p_flags
& PF_X
) && end_code
< k
)
717 k
= elf_ppnt
->p_vaddr
+ elf_ppnt
->p_memsz
;
722 elf_ex
.e_entry
+= load_bias
;
723 elf_bss
+= load_bias
;
724 elf_brk
+= load_bias
;
725 start_code
+= load_bias
;
726 end_code
+= load_bias
;
727 start_data
+= load_bias
;
728 end_data
+= load_bias
;
730 if (elf_interpreter
) {
731 if (interpreter_type
== INTERPRETER_AOUT
)
732 elf_entry
= load_aout_interp(&interp_ex
,
735 elf_entry
= load_elf_interp(&interp_elf_ex
,
739 allow_write_access(interpreter
);
741 kfree(elf_interpreter
);
743 if (BAD_ADDR(elf_entry
)) {
744 printk(KERN_ERR
"Unable to load interpreter\n");
746 send_sig(SIGSEGV
, current
, 0);
747 retval
= -ENOEXEC
; /* Nobody gets to see this, but.. */
750 reloc_func_desc
= interp_load_addr
;
752 elf_entry
= elf_ex
.e_entry
;
757 if (interpreter_type
!= INTERPRETER_AOUT
)
758 sys_close(elf_exec_fileno
);
760 set_binfmt(&elf_format
);
763 current
->flags
&= ~PF_FORKNOEXEC
;
764 create_elf_tables(bprm
, &elf_ex
, (interpreter_type
== INTERPRETER_AOUT
),
765 load_addr
, interp_load_addr
);
766 /* N.B. passed_fileno might not be initialized? */
767 if (interpreter_type
== INTERPRETER_AOUT
)
768 current
->mm
->arg_start
+= strlen(passed_fileno
) + 1;
769 current
->mm
->end_code
= end_code
;
770 current
->mm
->start_code
= start_code
;
771 current
->mm
->start_data
= start_data
;
772 current
->mm
->end_data
= end_data
;
773 current
->mm
->start_stack
= bprm
->p
;
775 /* Calling set_brk effectively mmaps the pages that we need
776 * for the bss and break sections
778 set_brk(elf_bss
, elf_brk
);
782 if (current
->personality
& MMAP_PAGE_ZERO
) {
783 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
784 and some applications "depend" upon this behavior.
785 Since we do not have the power to recompile these, we
786 emulate the SVr4 behavior. Sigh. */
787 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
788 down_write(¤t
->mm
->mmap_sem
);
789 error
= do_mmap(NULL
, 0, 4096, PROT_READ
| PROT_EXEC
,
790 MAP_FIXED
| MAP_PRIVATE
, 0);
791 up_write(¤t
->mm
->mmap_sem
);
796 * The ABI may specify that certain registers be set up in special
797 * ways (on i386 %edx is the address of a DT_FINI function, for
798 * example. In addition, it may also specify (eg, PowerPC64 ELF)
799 * that the e_entry field is the address of the function descriptor
800 * for the startup routine, rather than the address of the startup
801 * routine itself. This macro performs whatever initialization to
802 * the regs structure is required as well as any relocations to the
803 * function descriptor entries when executing dynamically links apps.
805 ELF_PLAT_INIT(regs
, reloc_func_desc
);
808 start_thread(regs
, elf_entry
, bprm
->p
);
809 if (unlikely(current
->ptrace
& PT_PTRACED
)) {
810 if (current
->ptrace
& PT_TRACE_EXEC
)
811 ptrace_notify ((PTRACE_EVENT_EXEC
<< 8) | SIGTRAP
);
813 send_sig(SIGTRAP
, current
, 0);
821 allow_write_access(interpreter
);
825 kfree(elf_interpreter
);
827 sys_close(elf_exec_fileno
);
833 /* This is really simpleminded and specialized - we are loading an
834 a.out library that is given an ELF header. */
836 static int load_elf_library(struct file
*file
)
838 struct elf_phdr
*elf_phdata
;
839 unsigned long elf_bss
, bss
, len
;
840 int retval
, error
, i
, j
;
841 struct elfhdr elf_ex
;
844 retval
= kernel_read(file
, 0, (char *) &elf_ex
, sizeof(elf_ex
));
845 if (retval
!= sizeof(elf_ex
))
848 if (memcmp(elf_ex
.e_ident
, ELFMAG
, SELFMAG
) != 0)
851 /* First of all, some simple consistency checks */
852 if (elf_ex
.e_type
!= ET_EXEC
|| elf_ex
.e_phnum
> 2 ||
853 !elf_check_arch(&elf_ex
) || !file
->f_op
|| !file
->f_op
->mmap
)
856 /* Now read in all of the header information */
858 j
= sizeof(struct elf_phdr
) * elf_ex
.e_phnum
;
859 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
862 elf_phdata
= (struct elf_phdr
*) kmalloc(j
, GFP_KERNEL
);
867 retval
= kernel_read(file
, elf_ex
.e_phoff
, (char *) elf_phdata
, j
);
871 for (j
= 0, i
= 0; i
<elf_ex
.e_phnum
; i
++)
872 if ((elf_phdata
+ i
)->p_type
== PT_LOAD
) j
++;
876 while (elf_phdata
->p_type
!= PT_LOAD
) elf_phdata
++;
878 /* Now use mmap to map the library into memory. */
879 down_write(¤t
->mm
->mmap_sem
);
880 error
= do_mmap(file
,
881 ELF_PAGESTART(elf_phdata
->p_vaddr
),
882 (elf_phdata
->p_filesz
+
883 ELF_PAGEOFFSET(elf_phdata
->p_vaddr
)),
884 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
885 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
,
886 (elf_phdata
->p_offset
-
887 ELF_PAGEOFFSET(elf_phdata
->p_vaddr
)));
888 up_write(¤t
->mm
->mmap_sem
);
889 if (error
!= ELF_PAGESTART(elf_phdata
->p_vaddr
))
892 elf_bss
= elf_phdata
->p_vaddr
+ elf_phdata
->p_filesz
;
895 len
= ELF_PAGESTART(elf_phdata
->p_filesz
+ elf_phdata
->p_vaddr
+ ELF_MIN_ALIGN
- 1);
896 bss
= elf_phdata
->p_memsz
+ elf_phdata
->p_vaddr
;
898 do_brk(len
, bss
- len
);
908 * Note that some platforms still use traditional core dumps and not
909 * the ELF core dump. Each platform can select it as appropriate.
911 #ifdef USE_ELF_CORE_DUMP
916 * Modelled on fs/exec.c:aout_core_dump()
917 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
920 * These are the only things you should do on a core-file: use only these
921 * functions to write out all the necessary info.
923 static int dump_write(struct file
*file
, const void *addr
, int nr
)
925 return file
->f_op
->write(file
, addr
, nr
, &file
->f_pos
) == nr
;
928 static int dump_seek(struct file
*file
, off_t off
)
930 if (file
->f_op
->llseek
) {
931 if (file
->f_op
->llseek(file
, off
, 0) != off
)
939 * Decide whether a segment is worth dumping; default is yes to be
940 * sure (missing info is worse than too much; etc).
941 * Personally I'd include everything, and use the coredump limit...
943 * I think we should skip something. But I am not sure how. H.J.
945 static int maydump(struct vm_area_struct
*vma
)
948 * If we may not read the contents, don't allow us to dump
949 * them either. "dump_write()" can't handle it anyway.
951 if (!(vma
->vm_flags
& VM_READ
))
954 /* Do not dump I/O mapped devices! -DaveM */
955 if (vma
->vm_flags
& VM_IO
)
958 if (vma
->vm_flags
& (VM_WRITE
|VM_GROWSUP
|VM_GROWSDOWN
))
960 if (vma
->vm_flags
& (VM_READ
|VM_EXEC
|VM_EXECUTABLE
|VM_SHARED
))
966 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
968 /* An ELF note in memory */
977 static int notesize(struct memelfnote
*en
)
981 sz
= sizeof(struct elf_note
);
982 sz
+= roundup(strlen(en
->name
) + 1, 4);
983 sz
+= roundup(en
->datasz
, 4);
988 #define DUMP_WRITE(addr, nr) \
989 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
990 #define DUMP_SEEK(off) \
991 do { if (!dump_seek(file, (off))) return 0; } while(0)
993 static int writenote(struct memelfnote
*men
, struct file
*file
)
997 en
.n_namesz
= strlen(men
->name
) + 1;
998 en
.n_descsz
= men
->datasz
;
999 en
.n_type
= men
->type
;
1001 DUMP_WRITE(&en
, sizeof(en
));
1002 DUMP_WRITE(men
->name
, en
.n_namesz
);
1003 /* XXX - cast from long long to long to avoid need for libgcc.a */
1004 DUMP_SEEK(roundup((unsigned long)file
->f_pos
, 4)); /* XXX */
1005 DUMP_WRITE(men
->data
, men
->datasz
);
1006 DUMP_SEEK(roundup((unsigned long)file
->f_pos
, 4)); /* XXX */
1013 #define DUMP_WRITE(addr, nr) \
1014 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1016 #define DUMP_SEEK(off) \
1017 if (!dump_seek(file, (off))) \
1020 static inline void fill_elf_header(struct elfhdr
*elf
, int segs
)
1022 memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
1023 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
1024 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
1025 elf
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1026 memset(elf
->e_ident
+EI_PAD
, 0, EI_NIDENT
-EI_PAD
);
1028 elf
->e_type
= ET_CORE
;
1029 elf
->e_machine
= ELF_ARCH
;
1030 elf
->e_version
= EV_CURRENT
;
1032 elf
->e_phoff
= sizeof(struct elfhdr
);
1035 elf
->e_ehsize
= sizeof(struct elfhdr
);
1036 elf
->e_phentsize
= sizeof(struct elf_phdr
);
1037 elf
->e_phnum
= segs
;
1038 elf
->e_shentsize
= 0;
1040 elf
->e_shstrndx
= 0;
1044 static inline void fill_elf_note_phdr(struct elf_phdr
*phdr
, int sz
, off_t offset
)
1046 phdr
->p_type
= PT_NOTE
;
1047 phdr
->p_offset
= offset
;
1050 phdr
->p_filesz
= sz
;
1057 static void fill_note(struct memelfnote
*note
, const char *name
, int type
,
1058 unsigned int sz
, void *data
)
1068 * fill up all the fields in prstatus from the given task struct, except registers
1069 * which need to be filled up separately.
1071 static void fill_prstatus(struct elf_prstatus
*prstatus
,
1072 struct task_struct
*p
, long signr
)
1074 prstatus
->pr_info
.si_signo
= prstatus
->pr_cursig
= signr
;
1075 prstatus
->pr_sigpend
= p
->pending
.signal
.sig
[0];
1076 prstatus
->pr_sighold
= p
->blocked
.sig
[0];
1077 prstatus
->pr_pid
= p
->pid
;
1078 prstatus
->pr_ppid
= p
->parent
->pid
;
1079 prstatus
->pr_pgrp
= p
->pgrp
;
1080 prstatus
->pr_sid
= p
->session
;
1081 jiffies_to_timeval(p
->utime
, &prstatus
->pr_utime
);
1082 jiffies_to_timeval(p
->stime
, &prstatus
->pr_stime
);
1083 jiffies_to_timeval(p
->cutime
, &prstatus
->pr_cutime
);
1084 jiffies_to_timeval(p
->cstime
, &prstatus
->pr_cstime
);
1087 static void fill_psinfo(struct elf_prpsinfo
*psinfo
, struct task_struct
*p
)
1091 /* first copy the parameters from user space */
1092 memset(psinfo
, 0, sizeof(struct elf_prpsinfo
));
1094 len
= p
->mm
->arg_end
- p
->mm
->arg_start
;
1095 if (len
>= ELF_PRARGSZ
)
1096 len
= ELF_PRARGSZ
-1;
1097 copy_from_user(&psinfo
->pr_psargs
,
1098 (const char *)p
->mm
->arg_start
, len
);
1099 for(i
= 0; i
< len
; i
++)
1100 if (psinfo
->pr_psargs
[i
] == 0)
1101 psinfo
->pr_psargs
[i
] = ' ';
1102 psinfo
->pr_psargs
[len
] = 0;
1104 psinfo
->pr_pid
= p
->pid
;
1105 psinfo
->pr_ppid
= p
->parent
->pid
;
1106 psinfo
->pr_pgrp
= p
->pgrp
;
1107 psinfo
->pr_sid
= p
->session
;
1109 i
= p
->state
? ffz(~p
->state
) + 1 : 0;
1110 psinfo
->pr_state
= i
;
1111 psinfo
->pr_sname
= (i
< 0 || i
> 5) ? '.' : "RSDTZW"[i
];
1112 psinfo
->pr_zomb
= psinfo
->pr_sname
== 'Z';
1113 psinfo
->pr_nice
= task_nice(p
);
1114 psinfo
->pr_flag
= p
->flags
;
1115 psinfo
->pr_uid
= NEW_TO_OLD_UID(p
->uid
);
1116 psinfo
->pr_gid
= NEW_TO_OLD_GID(p
->gid
);
1117 strncpy(psinfo
->pr_fname
, p
->comm
, sizeof(psinfo
->pr_fname
));
1122 /* Here is the structure in which status of each thread is captured. */
1123 struct elf_thread_status
1125 struct list_head list
;
1126 struct elf_prstatus prstatus
; /* NT_PRSTATUS */
1127 elf_fpregset_t fpu
; /* NT_PRFPREG */
1128 #ifdef ELF_CORE_COPY_XFPREGS
1129 elf_fpxregset_t xfpu
; /* NT_PRXFPREG */
1131 struct memelfnote notes
[3];
1136 * In order to add the specific thread information for the elf file format,
1137 * we need to keep a linked list of every threads pr_status and then
1138 * create a single section for them in the final core file.
1140 static int elf_dump_thread_status(long signr
, struct task_struct
* p
, struct list_head
* thread_list
)
1143 struct elf_thread_status
*t
;
1146 t
= kmalloc(sizeof(*t
), GFP_ATOMIC
);
1149 memset(t
, 0, sizeof(*t
));
1151 INIT_LIST_HEAD(&t
->list
);
1154 fill_prstatus(&t
->prstatus
, p
, signr
);
1155 elf_core_copy_task_regs(p
, &t
->prstatus
.pr_reg
);
1157 fill_note(&t
->notes
[0], "CORE", NT_PRSTATUS
, sizeof(t
->prstatus
), &(t
->prstatus
));
1159 sz
+= notesize(&t
->notes
[0]);
1161 if ((t
->prstatus
.pr_fpvalid
= elf_core_copy_task_fpregs(p
, &t
->fpu
))) {
1162 fill_note(&t
->notes
[1], "CORE", NT_PRFPREG
, sizeof(t
->fpu
), &(t
->fpu
));
1164 sz
+= notesize(&t
->notes
[1]);
1167 #ifdef ELF_CORE_COPY_XFPREGS
1168 if (elf_core_copy_task_xfpregs(p
, &t
->xfpu
)) {
1169 fill_note(&t
->notes
[2], "LINUX", NT_PRXFPREG
, sizeof(t
->xfpu
), &t
->xfpu
);
1171 sz
+= notesize(&t
->notes
[2]);
1174 list_add(&t
->list
, thread_list
);
1181 * This is a two-pass process; first we find the offsets of the bits,
1182 * and then they are actually written out. If we run out of core limit
1185 static int elf_core_dump(long signr
, struct pt_regs
* regs
, struct file
* file
)
1193 struct vm_area_struct
*vma
;
1194 struct elfhdr
*elf
= NULL
;
1195 off_t offset
= 0, dataoff
;
1196 unsigned long limit
= current
->rlim
[RLIMIT_CORE
].rlim_cur
;
1197 int numnote
= NUM_NOTES
;
1198 struct memelfnote
*notes
= NULL
;
1199 struct elf_prstatus
*prstatus
= NULL
; /* NT_PRSTATUS */
1200 struct elf_prpsinfo
*psinfo
= NULL
; /* NT_PRPSINFO */
1201 struct task_struct
*g
, *p
;
1202 LIST_HEAD(thread_list
);
1203 struct list_head
*t
;
1204 elf_fpregset_t
*fpu
= NULL
;
1205 #ifdef ELF_CORE_COPY_XFPREGS
1206 elf_fpxregset_t
*xfpu
= NULL
;
1208 int thread_status_size
= 0;
1211 * We no longer stop all VM operations.
1213 * This is because those proceses that could possibly change map_count or
1214 * the mmap / vma pages are now blocked in do_exit on current finishing
1217 * Only ptrace can touch these memory addresses, but it doesn't change
1218 * the map_count or the pages allocated. So no possibility of crashing
1219 * exists while dumping the mm->vm_next areas to the core file.
1222 /* alloc memory for large data structures: too large to be on stack */
1223 elf
= kmalloc(sizeof(*elf
), GFP_KERNEL
);
1226 prstatus
= kmalloc(sizeof(*prstatus
), GFP_KERNEL
);
1229 psinfo
= kmalloc(sizeof(*psinfo
), GFP_KERNEL
);
1232 notes
= kmalloc(NUM_NOTES
* sizeof(struct memelfnote
), GFP_KERNEL
);
1235 fpu
= kmalloc(sizeof(*fpu
), GFP_KERNEL
);
1238 #ifdef ELF_CORE_COPY_XFPREGS
1239 xfpu
= kmalloc(sizeof(*xfpu
), GFP_KERNEL
);
1244 /* capture the status of all other threads */
1246 read_lock(&tasklist_lock
);
1248 if (current
->mm
== p
->mm
&& current
!= p
) {
1249 int sz
= elf_dump_thread_status(signr
, p
, &thread_list
);
1251 read_unlock(&tasklist_lock
);
1254 thread_status_size
+= sz
;
1256 while_each_thread(g
,p
);
1257 read_unlock(&tasklist_lock
);
1260 /* now collect the dump for the current */
1261 memset(prstatus
, 0, sizeof(*prstatus
));
1262 fill_prstatus(prstatus
, current
, signr
);
1263 elf_core_copy_regs(&prstatus
->pr_reg
, regs
);
1265 segs
= current
->mm
->map_count
;
1266 #ifdef ELF_CORE_EXTRA_PHDRS
1267 segs
+= ELF_CORE_EXTRA_PHDRS
;
1271 fill_elf_header(elf
, segs
+1); /* including notes section */
1274 current
->flags
|= PF_DUMPCORE
;
1277 * Set up the notes in similar form to SVR4 core dumps made
1278 * with info from their /proc.
1281 fill_note(notes
+0, "CORE", NT_PRSTATUS
, sizeof(*prstatus
), prstatus
);
1283 fill_psinfo(psinfo
, current
->group_leader
);
1284 fill_note(notes
+1, "CORE", NT_PRPSINFO
, sizeof(*psinfo
), psinfo
);
1286 fill_note(notes
+2, "CORE", NT_TASKSTRUCT
, sizeof(*current
), current
);
1288 /* Try to dump the FPU. */
1289 if ((prstatus
->pr_fpvalid
= elf_core_copy_task_fpregs(current
, fpu
)))
1290 fill_note(notes
+3, "CORE", NT_PRFPREG
, sizeof(*fpu
), fpu
);
1293 #ifdef ELF_CORE_COPY_XFPREGS
1294 if (elf_core_copy_task_xfpregs(current
, xfpu
))
1295 fill_note(notes
+4, "LINUX", NT_PRXFPREG
, sizeof(*xfpu
), xfpu
);
1305 DUMP_WRITE(elf
, sizeof(*elf
));
1306 offset
+= sizeof(*elf
); /* Elf header */
1307 offset
+= (segs
+1) * sizeof(struct elf_phdr
); /* Program headers */
1309 /* Write notes phdr entry */
1311 struct elf_phdr phdr
;
1314 for (i
= 0; i
< numnote
; i
++)
1315 sz
+= notesize(notes
+ i
);
1317 sz
+= thread_status_size
;
1319 fill_elf_note_phdr(&phdr
, sz
, offset
);
1321 DUMP_WRITE(&phdr
, sizeof(phdr
));
1324 /* Page-align dumped data */
1325 dataoff
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
1327 /* Write program headers for segments dump */
1328 for (vma
= current
->mm
->mmap
; vma
!= NULL
; vma
= vma
->vm_next
) {
1329 struct elf_phdr phdr
;
1332 sz
= vma
->vm_end
- vma
->vm_start
;
1334 phdr
.p_type
= PT_LOAD
;
1335 phdr
.p_offset
= offset
;
1336 phdr
.p_vaddr
= vma
->vm_start
;
1338 phdr
.p_filesz
= maydump(vma
) ? sz
: 0;
1340 offset
+= phdr
.p_filesz
;
1341 phdr
.p_flags
= vma
->vm_flags
& VM_READ
? PF_R
: 0;
1342 if (vma
->vm_flags
& VM_WRITE
) phdr
.p_flags
|= PF_W
;
1343 if (vma
->vm_flags
& VM_EXEC
) phdr
.p_flags
|= PF_X
;
1344 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
1346 DUMP_WRITE(&phdr
, sizeof(phdr
));
1349 #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1350 ELF_CORE_WRITE_EXTRA_PHDRS
;
1353 /* write out the notes section */
1354 for (i
= 0; i
< numnote
; i
++)
1355 if (!writenote(notes
+ i
, file
))
1358 /* write out the thread status notes section */
1359 list_for_each(t
, &thread_list
) {
1360 struct elf_thread_status
*tmp
= list_entry(t
, struct elf_thread_status
, list
);
1361 for (i
= 0; i
< tmp
->num_notes
; i
++)
1362 if (!writenote(&tmp
->notes
[i
], file
))
1368 for (vma
= current
->mm
->mmap
; vma
!= NULL
; vma
= vma
->vm_next
) {
1374 for (addr
= vma
->vm_start
;
1376 addr
+= PAGE_SIZE
) {
1378 struct vm_area_struct
*vma
;
1380 if (get_user_pages(current
, current
->mm
, addr
, 1, 0, 1,
1381 &page
, &vma
) <= 0) {
1382 DUMP_SEEK (file
->f_pos
+ PAGE_SIZE
);
1384 if (page
== ZERO_PAGE(addr
)) {
1385 DUMP_SEEK (file
->f_pos
+ PAGE_SIZE
);
1388 flush_cache_page(vma
, addr
);
1390 DUMP_WRITE(kaddr
, PAGE_SIZE
);
1393 page_cache_release(page
);
1398 #ifdef ELF_CORE_WRITE_EXTRA_DATA
1399 ELF_CORE_WRITE_EXTRA_DATA
;
1402 if ((off_t
) file
->f_pos
!= offset
) {
1404 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1405 (off_t
) file
->f_pos
, offset
);
1412 while(!list_empty(&thread_list
)) {
1413 struct list_head
*tmp
= thread_list
.next
;
1415 kfree(list_entry(tmp
, struct elf_thread_status
, list
));
1423 #ifdef ELF_CORE_COPY_XFPREGS
1430 #endif /* USE_ELF_CORE_DUMP */
1432 static int __init
init_elf_binfmt(void)
1434 return register_binfmt(&elf_format
);
1437 static void __exit
exit_elf_binfmt(void)
1439 /* Remove the COFF and ELF loaders. */
1440 unregister_binfmt(&elf_format
);
1443 module_init(init_elf_binfmt
)
1444 module_exit(exit_elf_binfmt
)
1445 MODULE_LICENSE("GPL");