[PATCH] taskstats: use nla_reserve() for reply assembling
[linux-2.6/zen-sources.git] / fs / binfmt_elf.c
blob68e20d5bfe1b63b415f2e48c315c1636f45bb9ca
1 /*
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
7 * Tools".
9 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/fs.h>
15 #include <linux/stat.h>
16 #include <linux/time.h>
17 #include <linux/mm.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>
39 #include <linux/syscalls.h>
40 #include <linux/random.h>
41 #include <linux/elf.h>
42 #include <asm/uaccess.h>
43 #include <asm/param.h>
44 #include <asm/page.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);
50 #ifndef elf_addr_t
51 #define elf_addr_t unsigned long
52 #endif
55 * If we don't support core dumping, then supply a NULL so we
56 * don't even try.
58 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
59 static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file);
60 #else
61 #define elf_core_dump NULL
62 #endif
64 #if ELF_EXEC_PAGESIZE > PAGE_SIZE
65 #define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
66 #else
67 #define ELF_MIN_ALIGN PAGE_SIZE
68 #endif
70 #ifndef ELF_CORE_EFLAGS
71 #define ELF_CORE_EFLAGS 0
72 #endif
74 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
75 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
76 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
78 static struct linux_binfmt elf_format = {
79 .module = THIS_MODULE,
80 .load_binary = load_elf_binary,
81 .load_shlib = load_elf_library,
82 .core_dump = elf_core_dump,
83 .min_coredump = ELF_EXEC_PAGESIZE
86 #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
88 static int set_brk(unsigned long start, unsigned long end)
90 start = ELF_PAGEALIGN(start);
91 end = ELF_PAGEALIGN(end);
92 if (end > start) {
93 unsigned long addr;
94 down_write(&current->mm->mmap_sem);
95 addr = do_brk(start, end - start);
96 up_write(&current->mm->mmap_sem);
97 if (BAD_ADDR(addr))
98 return addr;
100 current->mm->start_brk = current->mm->brk = end;
101 return 0;
104 /* We need to explicitly zero any fractional pages
105 after the data section (i.e. bss). This would
106 contain the junk from the file that should not
107 be in memory
109 static int padzero(unsigned long elf_bss)
111 unsigned long nbyte;
113 nbyte = ELF_PAGEOFFSET(elf_bss);
114 if (nbyte) {
115 nbyte = ELF_MIN_ALIGN - nbyte;
116 if (clear_user((void __user *) elf_bss, nbyte))
117 return -EFAULT;
119 return 0;
122 /* Let's use some macros to make this stack manipulation a litle clearer */
123 #ifdef CONFIG_STACK_GROWSUP
124 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
125 #define STACK_ROUND(sp, items) \
126 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
127 #define STACK_ALLOC(sp, len) ({ \
128 elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
129 old_sp; })
130 #else
131 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
132 #define STACK_ROUND(sp, items) \
133 (((unsigned long) (sp - items)) &~ 15UL)
134 #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
135 #endif
137 static int
138 create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
139 int interp_aout, unsigned long load_addr,
140 unsigned long interp_load_addr)
142 unsigned long p = bprm->p;
143 int argc = bprm->argc;
144 int envc = bprm->envc;
145 elf_addr_t __user *argv;
146 elf_addr_t __user *envp;
147 elf_addr_t __user *sp;
148 elf_addr_t __user *u_platform;
149 const char *k_platform = ELF_PLATFORM;
150 int items;
151 elf_addr_t *elf_info;
152 int ei_index = 0;
153 struct task_struct *tsk = current;
156 * If this architecture has a platform capability string, copy it
157 * to userspace. In some cases (Sparc), this info is impossible
158 * for userspace to get any other way, in others (i386) it is
159 * merely difficult.
161 u_platform = NULL;
162 if (k_platform) {
163 size_t len = strlen(k_platform) + 1;
166 * In some cases (e.g. Hyper-Threading), we want to avoid L1
167 * evictions by the processes running on the same package. One
168 * thing we can do is to shuffle the initial stack for them.
171 p = arch_align_stack(p);
173 u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
174 if (__copy_to_user(u_platform, k_platform, len))
175 return -EFAULT;
178 /* Create the ELF interpreter info */
179 elf_info = (elf_addr_t *)current->mm->saved_auxv;
180 #define NEW_AUX_ENT(id, val) \
181 do { \
182 elf_info[ei_index++] = id; \
183 elf_info[ei_index++] = val; \
184 } while (0)
186 #ifdef ARCH_DLINFO
188 * ARCH_DLINFO must come first so PPC can do its special alignment of
189 * AUXV.
191 ARCH_DLINFO;
192 #endif
193 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
194 NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
195 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
196 NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
197 NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
198 NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
199 NEW_AUX_ENT(AT_BASE, interp_load_addr);
200 NEW_AUX_ENT(AT_FLAGS, 0);
201 NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
202 NEW_AUX_ENT(AT_UID, tsk->uid);
203 NEW_AUX_ENT(AT_EUID, tsk->euid);
204 NEW_AUX_ENT(AT_GID, tsk->gid);
205 NEW_AUX_ENT(AT_EGID, tsk->egid);
206 NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
207 if (k_platform) {
208 NEW_AUX_ENT(AT_PLATFORM,
209 (elf_addr_t)(unsigned long)u_platform);
211 if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
212 NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
214 #undef NEW_AUX_ENT
215 /* AT_NULL is zero; clear the rest too */
216 memset(&elf_info[ei_index], 0,
217 sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
219 /* And advance past the AT_NULL entry. */
220 ei_index += 2;
222 sp = STACK_ADD(p, ei_index);
224 items = (argc + 1) + (envc + 1);
225 if (interp_aout) {
226 items += 3; /* a.out interpreters require argv & envp too */
227 } else {
228 items += 1; /* ELF interpreters only put argc on the stack */
230 bprm->p = STACK_ROUND(sp, items);
232 /* Point sp at the lowest address on the stack */
233 #ifdef CONFIG_STACK_GROWSUP
234 sp = (elf_addr_t __user *)bprm->p - items - ei_index;
235 bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */
236 #else
237 sp = (elf_addr_t __user *)bprm->p;
238 #endif
240 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
241 if (__put_user(argc, sp++))
242 return -EFAULT;
243 if (interp_aout) {
244 argv = sp + 2;
245 envp = argv + argc + 1;
246 if (__put_user((elf_addr_t)(unsigned long)argv, sp++) ||
247 __put_user((elf_addr_t)(unsigned long)envp, sp++))
248 return -EFAULT;
249 } else {
250 argv = sp;
251 envp = argv + argc + 1;
254 /* Populate argv and envp */
255 p = current->mm->arg_end = current->mm->arg_start;
256 while (argc-- > 0) {
257 size_t len;
258 if (__put_user((elf_addr_t)p, argv++))
259 return -EFAULT;
260 len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
261 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
262 return 0;
263 p += len;
265 if (__put_user(0, argv))
266 return -EFAULT;
267 current->mm->arg_end = current->mm->env_start = p;
268 while (envc-- > 0) {
269 size_t len;
270 if (__put_user((elf_addr_t)p, envp++))
271 return -EFAULT;
272 len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
273 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
274 return 0;
275 p += len;
277 if (__put_user(0, envp))
278 return -EFAULT;
279 current->mm->env_end = p;
281 /* Put the elf_info on the stack in the right place. */
282 sp = (elf_addr_t __user *)envp + 1;
283 if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t)))
284 return -EFAULT;
285 return 0;
288 #ifndef elf_map
290 static unsigned long elf_map(struct file *filep, unsigned long addr,
291 struct elf_phdr *eppnt, int prot, int type)
293 unsigned long map_addr;
294 unsigned long pageoffset = ELF_PAGEOFFSET(eppnt->p_vaddr);
296 down_write(&current->mm->mmap_sem);
297 /* mmap() will return -EINVAL if given a zero size, but a
298 * segment with zero filesize is perfectly valid */
299 if (eppnt->p_filesz + pageoffset)
300 map_addr = do_mmap(filep, ELF_PAGESTART(addr),
301 eppnt->p_filesz + pageoffset, prot, type,
302 eppnt->p_offset - pageoffset);
303 else
304 map_addr = ELF_PAGESTART(addr);
305 up_write(&current->mm->mmap_sem);
306 return(map_addr);
309 #endif /* !elf_map */
311 /* This is much more generalized than the library routine read function,
312 so we keep this separate. Technically the library read function
313 is only provided so that we can read a.out libraries that have
314 an ELF header */
316 static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
317 struct file *interpreter, unsigned long *interp_load_addr)
319 struct elf_phdr *elf_phdata;
320 struct elf_phdr *eppnt;
321 unsigned long load_addr = 0;
322 int load_addr_set = 0;
323 unsigned long last_bss = 0, elf_bss = 0;
324 unsigned long error = ~0UL;
325 int retval, i, size;
327 /* First of all, some simple consistency checks */
328 if (interp_elf_ex->e_type != ET_EXEC &&
329 interp_elf_ex->e_type != ET_DYN)
330 goto out;
331 if (!elf_check_arch(interp_elf_ex))
332 goto out;
333 if (!interpreter->f_op || !interpreter->f_op->mmap)
334 goto out;
337 * If the size of this structure has changed, then punt, since
338 * we will be doing the wrong thing.
340 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
341 goto out;
342 if (interp_elf_ex->e_phnum < 1 ||
343 interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
344 goto out;
346 /* Now read in all of the header information */
347 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
348 if (size > ELF_MIN_ALIGN)
349 goto out;
350 elf_phdata = kmalloc(size, GFP_KERNEL);
351 if (!elf_phdata)
352 goto out;
354 retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
355 (char *)elf_phdata,size);
356 error = -EIO;
357 if (retval != size) {
358 if (retval < 0)
359 error = retval;
360 goto out_close;
363 eppnt = elf_phdata;
364 for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
365 if (eppnt->p_type == PT_LOAD) {
366 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
367 int elf_prot = 0;
368 unsigned long vaddr = 0;
369 unsigned long k, map_addr;
371 if (eppnt->p_flags & PF_R)
372 elf_prot = PROT_READ;
373 if (eppnt->p_flags & PF_W)
374 elf_prot |= PROT_WRITE;
375 if (eppnt->p_flags & PF_X)
376 elf_prot |= PROT_EXEC;
377 vaddr = eppnt->p_vaddr;
378 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
379 elf_type |= MAP_FIXED;
381 map_addr = elf_map(interpreter, load_addr + vaddr,
382 eppnt, elf_prot, elf_type);
383 error = map_addr;
384 if (BAD_ADDR(map_addr))
385 goto out_close;
387 if (!load_addr_set &&
388 interp_elf_ex->e_type == ET_DYN) {
389 load_addr = map_addr - ELF_PAGESTART(vaddr);
390 load_addr_set = 1;
394 * Check to see if the section's size will overflow the
395 * allowed task size. Note that p_filesz must always be
396 * <= p_memsize so it's only necessary to check p_memsz.
398 k = load_addr + eppnt->p_vaddr;
399 if (BAD_ADDR(k) ||
400 eppnt->p_filesz > eppnt->p_memsz ||
401 eppnt->p_memsz > TASK_SIZE ||
402 TASK_SIZE - eppnt->p_memsz < k) {
403 error = -ENOMEM;
404 goto out_close;
408 * Find the end of the file mapping for this phdr, and
409 * keep track of the largest address we see for this.
411 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
412 if (k > elf_bss)
413 elf_bss = k;
416 * Do the same thing for the memory mapping - between
417 * elf_bss and last_bss is the bss section.
419 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
420 if (k > last_bss)
421 last_bss = k;
426 * Now fill out the bss section. First pad the last page up
427 * to the page boundary, and then perform a mmap to make sure
428 * that there are zero-mapped pages up to and including the
429 * last bss page.
431 if (padzero(elf_bss)) {
432 error = -EFAULT;
433 goto out_close;
436 /* What we have mapped so far */
437 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
439 /* Map the last of the bss segment */
440 if (last_bss > elf_bss) {
441 down_write(&current->mm->mmap_sem);
442 error = do_brk(elf_bss, last_bss - elf_bss);
443 up_write(&current->mm->mmap_sem);
444 if (BAD_ADDR(error))
445 goto out_close;
448 *interp_load_addr = load_addr;
449 error = ((unsigned long)interp_elf_ex->e_entry) + load_addr;
451 out_close:
452 kfree(elf_phdata);
453 out:
454 return error;
457 static unsigned long load_aout_interp(struct exec *interp_ex,
458 struct file *interpreter)
460 unsigned long text_data, elf_entry = ~0UL;
461 char __user * addr;
462 loff_t offset;
464 current->mm->end_code = interp_ex->a_text;
465 text_data = interp_ex->a_text + interp_ex->a_data;
466 current->mm->end_data = text_data;
467 current->mm->brk = interp_ex->a_bss + text_data;
469 switch (N_MAGIC(*interp_ex)) {
470 case OMAGIC:
471 offset = 32;
472 addr = (char __user *)0;
473 break;
474 case ZMAGIC:
475 case QMAGIC:
476 offset = N_TXTOFF(*interp_ex);
477 addr = (char __user *)N_TXTADDR(*interp_ex);
478 break;
479 default:
480 goto out;
483 down_write(&current->mm->mmap_sem);
484 do_brk(0, text_data);
485 up_write(&current->mm->mmap_sem);
486 if (!interpreter->f_op || !interpreter->f_op->read)
487 goto out;
488 if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
489 goto out;
490 flush_icache_range((unsigned long)addr,
491 (unsigned long)addr + text_data);
493 down_write(&current->mm->mmap_sem);
494 do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
495 interp_ex->a_bss);
496 up_write(&current->mm->mmap_sem);
497 elf_entry = interp_ex->a_entry;
499 out:
500 return elf_entry;
504 * These are the functions used to load ELF style executables and shared
505 * libraries. There is no binary dependent code anywhere else.
508 #define INTERPRETER_NONE 0
509 #define INTERPRETER_AOUT 1
510 #define INTERPRETER_ELF 2
512 #ifndef STACK_RND_MASK
513 #define STACK_RND_MASK 0x7ff /* with 4K pages 8MB of VA */
514 #endif
516 static unsigned long randomize_stack_top(unsigned long stack_top)
518 unsigned int random_variable = 0;
520 if ((current->flags & PF_RANDOMIZE) &&
521 !(current->personality & ADDR_NO_RANDOMIZE)) {
522 random_variable = get_random_int() & STACK_RND_MASK;
523 random_variable <<= PAGE_SHIFT;
525 #ifdef CONFIG_STACK_GROWSUP
526 return PAGE_ALIGN(stack_top) + random_variable;
527 #else
528 return PAGE_ALIGN(stack_top) - random_variable;
529 #endif
532 static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
534 struct file *interpreter = NULL; /* to shut gcc up */
535 unsigned long load_addr = 0, load_bias = 0;
536 int load_addr_set = 0;
537 char * elf_interpreter = NULL;
538 unsigned int interpreter_type = INTERPRETER_NONE;
539 unsigned char ibcs2_interpreter = 0;
540 unsigned long error;
541 struct elf_phdr *elf_ppnt, *elf_phdata;
542 unsigned long elf_bss, elf_brk;
543 int elf_exec_fileno;
544 int retval, i;
545 unsigned int size;
546 unsigned long elf_entry, interp_load_addr = 0;
547 unsigned long start_code, end_code, start_data, end_data;
548 unsigned long reloc_func_desc = 0;
549 char passed_fileno[6];
550 struct files_struct *files;
551 int have_pt_gnu_stack, executable_stack = EXSTACK_DEFAULT;
552 unsigned long def_flags = 0;
553 struct {
554 struct elfhdr elf_ex;
555 struct elfhdr interp_elf_ex;
556 struct exec interp_ex;
557 } *loc;
559 loc = kmalloc(sizeof(*loc), GFP_KERNEL);
560 if (!loc) {
561 retval = -ENOMEM;
562 goto out_ret;
565 /* Get the exec-header */
566 loc->elf_ex = *((struct elfhdr *)bprm->buf);
568 retval = -ENOEXEC;
569 /* First of all, some simple consistency checks */
570 if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
571 goto out;
573 if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
574 goto out;
575 if (!elf_check_arch(&loc->elf_ex))
576 goto out;
577 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
578 goto out;
580 /* Now read in all of the header information */
581 if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
582 goto out;
583 if (loc->elf_ex.e_phnum < 1 ||
584 loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
585 goto out;
586 size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
587 retval = -ENOMEM;
588 elf_phdata = kmalloc(size, GFP_KERNEL);
589 if (!elf_phdata)
590 goto out;
592 retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
593 (char *)elf_phdata, size);
594 if (retval != size) {
595 if (retval >= 0)
596 retval = -EIO;
597 goto out_free_ph;
600 files = current->files; /* Refcounted so ok */
601 retval = unshare_files();
602 if (retval < 0)
603 goto out_free_ph;
604 if (files == current->files) {
605 put_files_struct(files);
606 files = NULL;
609 /* exec will make our files private anyway, but for the a.out
610 loader stuff we need to do it earlier */
611 retval = get_unused_fd();
612 if (retval < 0)
613 goto out_free_fh;
614 get_file(bprm->file);
615 fd_install(elf_exec_fileno = retval, bprm->file);
617 elf_ppnt = elf_phdata;
618 elf_bss = 0;
619 elf_brk = 0;
621 start_code = ~0UL;
622 end_code = 0;
623 start_data = 0;
624 end_data = 0;
626 for (i = 0; i < loc->elf_ex.e_phnum; i++) {
627 if (elf_ppnt->p_type == PT_INTERP) {
628 /* This is the program interpreter used for
629 * shared libraries - for now assume that this
630 * is an a.out format binary
632 retval = -ENOEXEC;
633 if (elf_ppnt->p_filesz > PATH_MAX ||
634 elf_ppnt->p_filesz < 2)
635 goto out_free_file;
637 retval = -ENOMEM;
638 elf_interpreter = kmalloc(elf_ppnt->p_filesz,
639 GFP_KERNEL);
640 if (!elf_interpreter)
641 goto out_free_file;
643 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
644 elf_interpreter,
645 elf_ppnt->p_filesz);
646 if (retval != elf_ppnt->p_filesz) {
647 if (retval >= 0)
648 retval = -EIO;
649 goto out_free_interp;
651 /* make sure path is NULL terminated */
652 retval = -ENOEXEC;
653 if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
654 goto out_free_interp;
656 /* If the program interpreter is one of these two,
657 * then assume an iBCS2 image. Otherwise assume
658 * a native linux image.
660 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
661 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
662 ibcs2_interpreter = 1;
665 * The early SET_PERSONALITY here is so that the lookup
666 * for the interpreter happens in the namespace of the
667 * to-be-execed image. SET_PERSONALITY can select an
668 * alternate root.
670 * However, SET_PERSONALITY is NOT allowed to switch
671 * this task into the new images's memory mapping
672 * policy - that is, TASK_SIZE must still evaluate to
673 * that which is appropriate to the execing application.
674 * This is because exit_mmap() needs to have TASK_SIZE
675 * evaluate to the size of the old image.
677 * So if (say) a 64-bit application is execing a 32-bit
678 * application it is the architecture's responsibility
679 * to defer changing the value of TASK_SIZE until the
680 * switch really is going to happen - do this in
681 * flush_thread(). - akpm
683 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
685 interpreter = open_exec(elf_interpreter);
686 retval = PTR_ERR(interpreter);
687 if (IS_ERR(interpreter))
688 goto out_free_interp;
689 retval = kernel_read(interpreter, 0, bprm->buf,
690 BINPRM_BUF_SIZE);
691 if (retval != BINPRM_BUF_SIZE) {
692 if (retval >= 0)
693 retval = -EIO;
694 goto out_free_dentry;
697 /* Get the exec headers */
698 loc->interp_ex = *((struct exec *)bprm->buf);
699 loc->interp_elf_ex = *((struct elfhdr *)bprm->buf);
700 break;
702 elf_ppnt++;
705 elf_ppnt = elf_phdata;
706 for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
707 if (elf_ppnt->p_type == PT_GNU_STACK) {
708 if (elf_ppnt->p_flags & PF_X)
709 executable_stack = EXSTACK_ENABLE_X;
710 else
711 executable_stack = EXSTACK_DISABLE_X;
712 break;
714 have_pt_gnu_stack = (i < loc->elf_ex.e_phnum);
716 /* Some simple consistency checks for the interpreter */
717 if (elf_interpreter) {
718 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
720 /* Now figure out which format our binary is */
721 if ((N_MAGIC(loc->interp_ex) != OMAGIC) &&
722 (N_MAGIC(loc->interp_ex) != ZMAGIC) &&
723 (N_MAGIC(loc->interp_ex) != QMAGIC))
724 interpreter_type = INTERPRETER_ELF;
726 if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
727 interpreter_type &= ~INTERPRETER_ELF;
729 retval = -ELIBBAD;
730 if (!interpreter_type)
731 goto out_free_dentry;
733 /* Make sure only one type was selected */
734 if ((interpreter_type & INTERPRETER_ELF) &&
735 interpreter_type != INTERPRETER_ELF) {
736 // FIXME - ratelimit this before re-enabling
737 // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
738 interpreter_type = INTERPRETER_ELF;
740 /* Verify the interpreter has a valid arch */
741 if ((interpreter_type == INTERPRETER_ELF) &&
742 !elf_check_arch(&loc->interp_elf_ex))
743 goto out_free_dentry;
744 } else {
745 /* Executables without an interpreter also need a personality */
746 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
749 /* OK, we are done with that, now set up the arg stuff,
750 and then start this sucker up */
751 if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
752 char *passed_p = passed_fileno;
753 sprintf(passed_fileno, "%d", elf_exec_fileno);
755 if (elf_interpreter) {
756 retval = copy_strings_kernel(1, &passed_p, bprm);
757 if (retval)
758 goto out_free_dentry;
759 bprm->argc++;
763 /* Flush all traces of the currently running executable */
764 retval = flush_old_exec(bprm);
765 if (retval)
766 goto out_free_dentry;
768 /* Discard our unneeded old files struct */
769 if (files) {
770 put_files_struct(files);
771 files = NULL;
774 /* OK, This is the point of no return */
775 current->mm->start_data = 0;
776 current->mm->end_data = 0;
777 current->mm->end_code = 0;
778 current->mm->mmap = NULL;
779 current->flags &= ~PF_FORKNOEXEC;
780 current->mm->def_flags = def_flags;
782 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
783 may depend on the personality. */
784 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
785 if (elf_read_implies_exec(loc->elf_ex, executable_stack))
786 current->personality |= READ_IMPLIES_EXEC;
788 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
789 current->flags |= PF_RANDOMIZE;
790 arch_pick_mmap_layout(current->mm);
792 /* Do this so that we can load the interpreter, if need be. We will
793 change some of these later */
794 current->mm->free_area_cache = current->mm->mmap_base;
795 current->mm->cached_hole_size = 0;
796 retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
797 executable_stack);
798 if (retval < 0) {
799 send_sig(SIGKILL, current, 0);
800 goto out_free_dentry;
803 current->mm->start_stack = bprm->p;
805 /* Now we do a little grungy work by mmaping the ELF image into
806 the correct location in memory. At this point, we assume that
807 the image should be loaded at fixed address, not at a variable
808 address. */
809 for(i = 0, elf_ppnt = elf_phdata;
810 i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
811 int elf_prot = 0, elf_flags;
812 unsigned long k, vaddr;
814 if (elf_ppnt->p_type != PT_LOAD)
815 continue;
817 if (unlikely (elf_brk > elf_bss)) {
818 unsigned long nbyte;
820 /* There was a PT_LOAD segment with p_memsz > p_filesz
821 before this one. Map anonymous pages, if needed,
822 and clear the area. */
823 retval = set_brk (elf_bss + load_bias,
824 elf_brk + load_bias);
825 if (retval) {
826 send_sig(SIGKILL, current, 0);
827 goto out_free_dentry;
829 nbyte = ELF_PAGEOFFSET(elf_bss);
830 if (nbyte) {
831 nbyte = ELF_MIN_ALIGN - nbyte;
832 if (nbyte > elf_brk - elf_bss)
833 nbyte = elf_brk - elf_bss;
834 if (clear_user((void __user *)elf_bss +
835 load_bias, nbyte)) {
837 * This bss-zeroing can fail if the ELF
838 * file specifies odd protections. So
839 * we don't check the return value
845 if (elf_ppnt->p_flags & PF_R)
846 elf_prot |= PROT_READ;
847 if (elf_ppnt->p_flags & PF_W)
848 elf_prot |= PROT_WRITE;
849 if (elf_ppnt->p_flags & PF_X)
850 elf_prot |= PROT_EXEC;
852 elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
854 vaddr = elf_ppnt->p_vaddr;
855 if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
856 elf_flags |= MAP_FIXED;
857 } else if (loc->elf_ex.e_type == ET_DYN) {
858 /* Try and get dynamic programs out of the way of the
859 * default mmap base, as well as whatever program they
860 * might try to exec. This is because the brk will
861 * follow the loader, and is not movable. */
862 if (current->flags & PF_RANDOMIZE)
863 load_bias = randomize_range(0x10000,
864 ELF_ET_DYN_BASE,
866 else
867 load_bias = ELF_ET_DYN_BASE;
868 load_bias = ELF_PAGESTART(load_bias - vaddr);
871 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
872 elf_prot, elf_flags);
873 if (BAD_ADDR(error)) {
874 send_sig(SIGKILL, current, 0);
875 goto out_free_dentry;
878 if (!load_addr_set) {
879 load_addr_set = 1;
880 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
881 if (loc->elf_ex.e_type == ET_DYN) {
882 load_bias += error -
883 ELF_PAGESTART(load_bias + vaddr);
884 load_addr += load_bias;
885 reloc_func_desc = load_bias;
888 k = elf_ppnt->p_vaddr;
889 if (k < start_code)
890 start_code = k;
891 if (start_data < k)
892 start_data = k;
895 * Check to see if the section's size will overflow the
896 * allowed task size. Note that p_filesz must always be
897 * <= p_memsz so it is only necessary to check p_memsz.
899 if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
900 elf_ppnt->p_memsz > TASK_SIZE ||
901 TASK_SIZE - elf_ppnt->p_memsz < k) {
902 /* set_brk can never work. Avoid overflows. */
903 send_sig(SIGKILL, current, 0);
904 goto out_free_dentry;
907 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
909 if (k > elf_bss)
910 elf_bss = k;
911 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
912 end_code = k;
913 if (end_data < k)
914 end_data = k;
915 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
916 if (k > elf_brk)
917 elf_brk = k;
920 loc->elf_ex.e_entry += load_bias;
921 elf_bss += load_bias;
922 elf_brk += load_bias;
923 start_code += load_bias;
924 end_code += load_bias;
925 start_data += load_bias;
926 end_data += load_bias;
928 /* Calling set_brk effectively mmaps the pages that we need
929 * for the bss and break sections. We must do this before
930 * mapping in the interpreter, to make sure it doesn't wind
931 * up getting placed where the bss needs to go.
933 retval = set_brk(elf_bss, elf_brk);
934 if (retval) {
935 send_sig(SIGKILL, current, 0);
936 goto out_free_dentry;
938 if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
939 send_sig(SIGSEGV, current, 0);
940 retval = -EFAULT; /* Nobody gets to see this, but.. */
941 goto out_free_dentry;
944 if (elf_interpreter) {
945 if (interpreter_type == INTERPRETER_AOUT)
946 elf_entry = load_aout_interp(&loc->interp_ex,
947 interpreter);
948 else
949 elf_entry = load_elf_interp(&loc->interp_elf_ex,
950 interpreter,
951 &interp_load_addr);
952 if (BAD_ADDR(elf_entry)) {
953 force_sig(SIGSEGV, current);
954 retval = IS_ERR((void *)elf_entry) ?
955 (int)elf_entry : -EINVAL;
956 goto out_free_dentry;
958 reloc_func_desc = interp_load_addr;
960 allow_write_access(interpreter);
961 fput(interpreter);
962 kfree(elf_interpreter);
963 } else {
964 elf_entry = loc->elf_ex.e_entry;
965 if (BAD_ADDR(elf_entry)) {
966 force_sig(SIGSEGV, current);
967 retval = -EINVAL;
968 goto out_free_dentry;
972 kfree(elf_phdata);
974 if (interpreter_type != INTERPRETER_AOUT)
975 sys_close(elf_exec_fileno);
977 set_binfmt(&elf_format);
979 #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
980 retval = arch_setup_additional_pages(bprm, executable_stack);
981 if (retval < 0) {
982 send_sig(SIGKILL, current, 0);
983 goto out;
985 #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
987 compute_creds(bprm);
988 current->flags &= ~PF_FORKNOEXEC;
989 create_elf_tables(bprm, &loc->elf_ex,
990 (interpreter_type == INTERPRETER_AOUT),
991 load_addr, interp_load_addr);
992 /* N.B. passed_fileno might not be initialized? */
993 if (interpreter_type == INTERPRETER_AOUT)
994 current->mm->arg_start += strlen(passed_fileno) + 1;
995 current->mm->end_code = end_code;
996 current->mm->start_code = start_code;
997 current->mm->start_data = start_data;
998 current->mm->end_data = end_data;
999 current->mm->start_stack = bprm->p;
1001 if (current->personality & MMAP_PAGE_ZERO) {
1002 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1003 and some applications "depend" upon this behavior.
1004 Since we do not have the power to recompile these, we
1005 emulate the SVr4 behavior. Sigh. */
1006 down_write(&current->mm->mmap_sem);
1007 error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
1008 MAP_FIXED | MAP_PRIVATE, 0);
1009 up_write(&current->mm->mmap_sem);
1012 #ifdef ELF_PLAT_INIT
1014 * The ABI may specify that certain registers be set up in special
1015 * ways (on i386 %edx is the address of a DT_FINI function, for
1016 * example. In addition, it may also specify (eg, PowerPC64 ELF)
1017 * that the e_entry field is the address of the function descriptor
1018 * for the startup routine, rather than the address of the startup
1019 * routine itself. This macro performs whatever initialization to
1020 * the regs structure is required as well as any relocations to the
1021 * function descriptor entries when executing dynamically links apps.
1023 ELF_PLAT_INIT(regs, reloc_func_desc);
1024 #endif
1026 start_thread(regs, elf_entry, bprm->p);
1027 if (unlikely(current->ptrace & PT_PTRACED)) {
1028 if (current->ptrace & PT_TRACE_EXEC)
1029 ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
1030 else
1031 send_sig(SIGTRAP, current, 0);
1033 retval = 0;
1034 out:
1035 kfree(loc);
1036 out_ret:
1037 return retval;
1039 /* error cleanup */
1040 out_free_dentry:
1041 allow_write_access(interpreter);
1042 if (interpreter)
1043 fput(interpreter);
1044 out_free_interp:
1045 kfree(elf_interpreter);
1046 out_free_file:
1047 sys_close(elf_exec_fileno);
1048 out_free_fh:
1049 if (files)
1050 reset_files_struct(current, files);
1051 out_free_ph:
1052 kfree(elf_phdata);
1053 goto out;
1056 /* This is really simpleminded and specialized - we are loading an
1057 a.out library that is given an ELF header. */
1058 static int load_elf_library(struct file *file)
1060 struct elf_phdr *elf_phdata;
1061 struct elf_phdr *eppnt;
1062 unsigned long elf_bss, bss, len;
1063 int retval, error, i, j;
1064 struct elfhdr elf_ex;
1066 error = -ENOEXEC;
1067 retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
1068 if (retval != sizeof(elf_ex))
1069 goto out;
1071 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
1072 goto out;
1074 /* First of all, some simple consistency checks */
1075 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
1076 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
1077 goto out;
1079 /* Now read in all of the header information */
1081 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
1082 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1084 error = -ENOMEM;
1085 elf_phdata = kmalloc(j, GFP_KERNEL);
1086 if (!elf_phdata)
1087 goto out;
1089 eppnt = elf_phdata;
1090 error = -ENOEXEC;
1091 retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
1092 if (retval != j)
1093 goto out_free_ph;
1095 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
1096 if ((eppnt + i)->p_type == PT_LOAD)
1097 j++;
1098 if (j != 1)
1099 goto out_free_ph;
1101 while (eppnt->p_type != PT_LOAD)
1102 eppnt++;
1104 /* Now use mmap to map the library into memory. */
1105 down_write(&current->mm->mmap_sem);
1106 error = do_mmap(file,
1107 ELF_PAGESTART(eppnt->p_vaddr),
1108 (eppnt->p_filesz +
1109 ELF_PAGEOFFSET(eppnt->p_vaddr)),
1110 PROT_READ | PROT_WRITE | PROT_EXEC,
1111 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
1112 (eppnt->p_offset -
1113 ELF_PAGEOFFSET(eppnt->p_vaddr)));
1114 up_write(&current->mm->mmap_sem);
1115 if (error != ELF_PAGESTART(eppnt->p_vaddr))
1116 goto out_free_ph;
1118 elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
1119 if (padzero(elf_bss)) {
1120 error = -EFAULT;
1121 goto out_free_ph;
1124 len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr +
1125 ELF_MIN_ALIGN - 1);
1126 bss = eppnt->p_memsz + eppnt->p_vaddr;
1127 if (bss > len) {
1128 down_write(&current->mm->mmap_sem);
1129 do_brk(len, bss - len);
1130 up_write(&current->mm->mmap_sem);
1132 error = 0;
1134 out_free_ph:
1135 kfree(elf_phdata);
1136 out:
1137 return error;
1141 * Note that some platforms still use traditional core dumps and not
1142 * the ELF core dump. Each platform can select it as appropriate.
1144 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1147 * ELF core dumper
1149 * Modelled on fs/exec.c:aout_core_dump()
1150 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1153 * These are the only things you should do on a core-file: use only these
1154 * functions to write out all the necessary info.
1156 static int dump_write(struct file *file, const void *addr, int nr)
1158 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1161 static int dump_seek(struct file *file, loff_t off)
1163 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
1164 if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
1165 return 0;
1166 } else {
1167 char *buf = (char *)get_zeroed_page(GFP_KERNEL);
1168 if (!buf)
1169 return 0;
1170 while (off > 0) {
1171 unsigned long n = off;
1172 if (n > PAGE_SIZE)
1173 n = PAGE_SIZE;
1174 if (!dump_write(file, buf, n))
1175 return 0;
1176 off -= n;
1178 free_page((unsigned long)buf);
1180 return 1;
1184 * Decide whether a segment is worth dumping; default is yes to be
1185 * sure (missing info is worse than too much; etc).
1186 * Personally I'd include everything, and use the coredump limit...
1188 * I think we should skip something. But I am not sure how. H.J.
1190 static int maydump(struct vm_area_struct *vma)
1192 /* Do not dump I/O mapped devices or special mappings */
1193 if (vma->vm_flags & (VM_IO | VM_RESERVED))
1194 return 0;
1196 /* Dump shared memory only if mapped from an anonymous file. */
1197 if (vma->vm_flags & VM_SHARED)
1198 return vma->vm_file->f_dentry->d_inode->i_nlink == 0;
1200 /* If it hasn't been written to, don't write it out */
1201 if (!vma->anon_vma)
1202 return 0;
1204 return 1;
1207 /* An ELF note in memory */
1208 struct memelfnote
1210 const char *name;
1211 int type;
1212 unsigned int datasz;
1213 void *data;
1216 static int notesize(struct memelfnote *en)
1218 int sz;
1220 sz = sizeof(struct elf_note);
1221 sz += roundup(strlen(en->name) + 1, 4);
1222 sz += roundup(en->datasz, 4);
1224 return sz;
1227 #define DUMP_WRITE(addr, nr, foffset) \
1228 do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
1230 static int alignfile(struct file *file, loff_t *foffset)
1232 static const char buf[4] = { 0, };
1233 DUMP_WRITE(buf, roundup(*foffset, 4) - *foffset, foffset);
1234 return 1;
1237 static int writenote(struct memelfnote *men, struct file *file,
1238 loff_t *foffset)
1240 struct elf_note en;
1241 en.n_namesz = strlen(men->name) + 1;
1242 en.n_descsz = men->datasz;
1243 en.n_type = men->type;
1245 DUMP_WRITE(&en, sizeof(en), foffset);
1246 DUMP_WRITE(men->name, en.n_namesz, foffset);
1247 if (!alignfile(file, foffset))
1248 return 0;
1249 DUMP_WRITE(men->data, men->datasz, foffset);
1250 if (!alignfile(file, foffset))
1251 return 0;
1253 return 1;
1255 #undef DUMP_WRITE
1257 #define DUMP_WRITE(addr, nr) \
1258 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1259 goto end_coredump;
1260 #define DUMP_SEEK(off) \
1261 if (!dump_seek(file, (off))) \
1262 goto end_coredump;
1264 static void fill_elf_header(struct elfhdr *elf, int segs)
1266 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1267 elf->e_ident[EI_CLASS] = ELF_CLASS;
1268 elf->e_ident[EI_DATA] = ELF_DATA;
1269 elf->e_ident[EI_VERSION] = EV_CURRENT;
1270 elf->e_ident[EI_OSABI] = ELF_OSABI;
1271 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1273 elf->e_type = ET_CORE;
1274 elf->e_machine = ELF_ARCH;
1275 elf->e_version = EV_CURRENT;
1276 elf->e_entry = 0;
1277 elf->e_phoff = sizeof(struct elfhdr);
1278 elf->e_shoff = 0;
1279 elf->e_flags = ELF_CORE_EFLAGS;
1280 elf->e_ehsize = sizeof(struct elfhdr);
1281 elf->e_phentsize = sizeof(struct elf_phdr);
1282 elf->e_phnum = segs;
1283 elf->e_shentsize = 0;
1284 elf->e_shnum = 0;
1285 elf->e_shstrndx = 0;
1286 return;
1289 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
1291 phdr->p_type = PT_NOTE;
1292 phdr->p_offset = offset;
1293 phdr->p_vaddr = 0;
1294 phdr->p_paddr = 0;
1295 phdr->p_filesz = sz;
1296 phdr->p_memsz = 0;
1297 phdr->p_flags = 0;
1298 phdr->p_align = 0;
1299 return;
1302 static void fill_note(struct memelfnote *note, const char *name, int type,
1303 unsigned int sz, void *data)
1305 note->name = name;
1306 note->type = type;
1307 note->datasz = sz;
1308 note->data = data;
1309 return;
1313 * fill up all the fields in prstatus from the given task struct, except
1314 * registers which need to be filled up separately.
1316 static void fill_prstatus(struct elf_prstatus *prstatus,
1317 struct task_struct *p, long signr)
1319 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1320 prstatus->pr_sigpend = p->pending.signal.sig[0];
1321 prstatus->pr_sighold = p->blocked.sig[0];
1322 prstatus->pr_pid = p->pid;
1323 prstatus->pr_ppid = p->parent->pid;
1324 prstatus->pr_pgrp = process_group(p);
1325 prstatus->pr_sid = p->signal->session;
1326 if (thread_group_leader(p)) {
1328 * This is the record for the group leader. Add in the
1329 * cumulative times of previous dead threads. This total
1330 * won't include the time of each live thread whose state
1331 * is included in the core dump. The final total reported
1332 * to our parent process when it calls wait4 will include
1333 * those sums as well as the little bit more time it takes
1334 * this and each other thread to finish dying after the
1335 * core dump synchronization phase.
1337 cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
1338 &prstatus->pr_utime);
1339 cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
1340 &prstatus->pr_stime);
1341 } else {
1342 cputime_to_timeval(p->utime, &prstatus->pr_utime);
1343 cputime_to_timeval(p->stime, &prstatus->pr_stime);
1345 cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
1346 cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
1349 static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1350 struct mm_struct *mm)
1352 unsigned int i, len;
1354 /* first copy the parameters from user space */
1355 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1357 len = mm->arg_end - mm->arg_start;
1358 if (len >= ELF_PRARGSZ)
1359 len = ELF_PRARGSZ-1;
1360 if (copy_from_user(&psinfo->pr_psargs,
1361 (const char __user *)mm->arg_start, len))
1362 return -EFAULT;
1363 for(i = 0; i < len; i++)
1364 if (psinfo->pr_psargs[i] == 0)
1365 psinfo->pr_psargs[i] = ' ';
1366 psinfo->pr_psargs[len] = 0;
1368 psinfo->pr_pid = p->pid;
1369 psinfo->pr_ppid = p->parent->pid;
1370 psinfo->pr_pgrp = process_group(p);
1371 psinfo->pr_sid = p->signal->session;
1373 i = p->state ? ffz(~p->state) + 1 : 0;
1374 psinfo->pr_state = i;
1375 psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
1376 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1377 psinfo->pr_nice = task_nice(p);
1378 psinfo->pr_flag = p->flags;
1379 SET_UID(psinfo->pr_uid, p->uid);
1380 SET_GID(psinfo->pr_gid, p->gid);
1381 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1383 return 0;
1386 /* Here is the structure in which status of each thread is captured. */
1387 struct elf_thread_status
1389 struct list_head list;
1390 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1391 elf_fpregset_t fpu; /* NT_PRFPREG */
1392 struct task_struct *thread;
1393 #ifdef ELF_CORE_COPY_XFPREGS
1394 elf_fpxregset_t xfpu; /* NT_PRXFPREG */
1395 #endif
1396 struct memelfnote notes[3];
1397 int num_notes;
1401 * In order to add the specific thread information for the elf file format,
1402 * we need to keep a linked list of every threads pr_status and then create
1403 * a single section for them in the final core file.
1405 static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1407 int sz = 0;
1408 struct task_struct *p = t->thread;
1409 t->num_notes = 0;
1411 fill_prstatus(&t->prstatus, p, signr);
1412 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1414 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
1415 &(t->prstatus));
1416 t->num_notes++;
1417 sz += notesize(&t->notes[0]);
1419 if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
1420 &t->fpu))) {
1421 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
1422 &(t->fpu));
1423 t->num_notes++;
1424 sz += notesize(&t->notes[1]);
1427 #ifdef ELF_CORE_COPY_XFPREGS
1428 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
1429 fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu),
1430 &t->xfpu);
1431 t->num_notes++;
1432 sz += notesize(&t->notes[2]);
1434 #endif
1435 return sz;
1439 * Actual dumper
1441 * This is a two-pass process; first we find the offsets of the bits,
1442 * and then they are actually written out. If we run out of core limit
1443 * we just truncate.
1445 static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file)
1447 #define NUM_NOTES 6
1448 int has_dumped = 0;
1449 mm_segment_t fs;
1450 int segs;
1451 size_t size = 0;
1452 int i;
1453 struct vm_area_struct *vma;
1454 struct elfhdr *elf = NULL;
1455 loff_t offset = 0, dataoff, foffset;
1456 unsigned long limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1457 int numnote;
1458 struct memelfnote *notes = NULL;
1459 struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
1460 struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
1461 struct task_struct *g, *p;
1462 LIST_HEAD(thread_list);
1463 struct list_head *t;
1464 elf_fpregset_t *fpu = NULL;
1465 #ifdef ELF_CORE_COPY_XFPREGS
1466 elf_fpxregset_t *xfpu = NULL;
1467 #endif
1468 int thread_status_size = 0;
1469 elf_addr_t *auxv;
1472 * We no longer stop all VM operations.
1474 * This is because those proceses that could possibly change map_count
1475 * or the mmap / vma pages are now blocked in do_exit on current
1476 * finishing this core dump.
1478 * Only ptrace can touch these memory addresses, but it doesn't change
1479 * the map_count or the pages allocated. So no possibility of crashing
1480 * exists while dumping the mm->vm_next areas to the core file.
1483 /* alloc memory for large data structures: too large to be on stack */
1484 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1485 if (!elf)
1486 goto cleanup;
1487 prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
1488 if (!prstatus)
1489 goto cleanup;
1490 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1491 if (!psinfo)
1492 goto cleanup;
1493 notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1494 if (!notes)
1495 goto cleanup;
1496 fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1497 if (!fpu)
1498 goto cleanup;
1499 #ifdef ELF_CORE_COPY_XFPREGS
1500 xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1501 if (!xfpu)
1502 goto cleanup;
1503 #endif
1505 if (signr) {
1506 struct elf_thread_status *tmp;
1507 rcu_read_lock();
1508 do_each_thread(g,p)
1509 if (current->mm == p->mm && current != p) {
1510 tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
1511 if (!tmp) {
1512 rcu_read_unlock();
1513 goto cleanup;
1515 tmp->thread = p;
1516 list_add(&tmp->list, &thread_list);
1518 while_each_thread(g,p);
1519 rcu_read_unlock();
1520 list_for_each(t, &thread_list) {
1521 struct elf_thread_status *tmp;
1522 int sz;
1524 tmp = list_entry(t, struct elf_thread_status, list);
1525 sz = elf_dump_thread_status(signr, tmp);
1526 thread_status_size += sz;
1529 /* now collect the dump for the current */
1530 memset(prstatus, 0, sizeof(*prstatus));
1531 fill_prstatus(prstatus, current, signr);
1532 elf_core_copy_regs(&prstatus->pr_reg, regs);
1534 segs = current->mm->map_count;
1535 #ifdef ELF_CORE_EXTRA_PHDRS
1536 segs += ELF_CORE_EXTRA_PHDRS;
1537 #endif
1539 /* Set up header */
1540 fill_elf_header(elf, segs + 1); /* including notes section */
1542 has_dumped = 1;
1543 current->flags |= PF_DUMPCORE;
1546 * Set up the notes in similar form to SVR4 core dumps made
1547 * with info from their /proc.
1550 fill_note(notes + 0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
1551 fill_psinfo(psinfo, current->group_leader, current->mm);
1552 fill_note(notes + 1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1554 numnote = 2;
1556 auxv = (elf_addr_t *)current->mm->saved_auxv;
1558 i = 0;
1560 i += 2;
1561 while (auxv[i - 2] != AT_NULL);
1562 fill_note(&notes[numnote++], "CORE", NT_AUXV,
1563 i * sizeof(elf_addr_t), auxv);
1565 /* Try to dump the FPU. */
1566 if ((prstatus->pr_fpvalid =
1567 elf_core_copy_task_fpregs(current, regs, fpu)))
1568 fill_note(notes + numnote++,
1569 "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1570 #ifdef ELF_CORE_COPY_XFPREGS
1571 if (elf_core_copy_task_xfpregs(current, xfpu))
1572 fill_note(notes + numnote++,
1573 "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
1574 #endif
1576 fs = get_fs();
1577 set_fs(KERNEL_DS);
1579 DUMP_WRITE(elf, sizeof(*elf));
1580 offset += sizeof(*elf); /* Elf header */
1581 offset += (segs + 1) * sizeof(struct elf_phdr); /* Program headers */
1582 foffset = offset;
1584 /* Write notes phdr entry */
1586 struct elf_phdr phdr;
1587 int sz = 0;
1589 for (i = 0; i < numnote; i++)
1590 sz += notesize(notes + i);
1592 sz += thread_status_size;
1594 #ifdef ELF_CORE_WRITE_EXTRA_NOTES
1595 sz += ELF_CORE_EXTRA_NOTES_SIZE;
1596 #endif
1598 fill_elf_note_phdr(&phdr, sz, offset);
1599 offset += sz;
1600 DUMP_WRITE(&phdr, sizeof(phdr));
1603 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1605 /* Write program headers for segments dump */
1606 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1607 struct elf_phdr phdr;
1608 size_t sz;
1610 sz = vma->vm_end - vma->vm_start;
1612 phdr.p_type = PT_LOAD;
1613 phdr.p_offset = offset;
1614 phdr.p_vaddr = vma->vm_start;
1615 phdr.p_paddr = 0;
1616 phdr.p_filesz = maydump(vma) ? sz : 0;
1617 phdr.p_memsz = sz;
1618 offset += phdr.p_filesz;
1619 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1620 if (vma->vm_flags & VM_WRITE)
1621 phdr.p_flags |= PF_W;
1622 if (vma->vm_flags & VM_EXEC)
1623 phdr.p_flags |= PF_X;
1624 phdr.p_align = ELF_EXEC_PAGESIZE;
1626 DUMP_WRITE(&phdr, sizeof(phdr));
1629 #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1630 ELF_CORE_WRITE_EXTRA_PHDRS;
1631 #endif
1633 /* write out the notes section */
1634 for (i = 0; i < numnote; i++)
1635 if (!writenote(notes + i, file, &foffset))
1636 goto end_coredump;
1638 #ifdef ELF_CORE_WRITE_EXTRA_NOTES
1639 ELF_CORE_WRITE_EXTRA_NOTES;
1640 #endif
1642 /* write out the thread status notes section */
1643 list_for_each(t, &thread_list) {
1644 struct elf_thread_status *tmp =
1645 list_entry(t, struct elf_thread_status, list);
1647 for (i = 0; i < tmp->num_notes; i++)
1648 if (!writenote(&tmp->notes[i], file, &foffset))
1649 goto end_coredump;
1652 /* Align to page */
1653 DUMP_SEEK(dataoff - foffset);
1655 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1656 unsigned long addr;
1658 if (!maydump(vma))
1659 continue;
1661 for (addr = vma->vm_start;
1662 addr < vma->vm_end;
1663 addr += PAGE_SIZE) {
1664 struct page *page;
1665 struct vm_area_struct *vma;
1667 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1668 &page, &vma) <= 0) {
1669 DUMP_SEEK(PAGE_SIZE);
1670 } else {
1671 if (page == ZERO_PAGE(addr)) {
1672 DUMP_SEEK(PAGE_SIZE);
1673 } else {
1674 void *kaddr;
1675 flush_cache_page(vma, addr,
1676 page_to_pfn(page));
1677 kaddr = kmap(page);
1678 if ((size += PAGE_SIZE) > limit ||
1679 !dump_write(file, kaddr,
1680 PAGE_SIZE)) {
1681 kunmap(page);
1682 page_cache_release(page);
1683 goto end_coredump;
1685 kunmap(page);
1687 page_cache_release(page);
1692 #ifdef ELF_CORE_WRITE_EXTRA_DATA
1693 ELF_CORE_WRITE_EXTRA_DATA;
1694 #endif
1696 end_coredump:
1697 set_fs(fs);
1699 cleanup:
1700 while (!list_empty(&thread_list)) {
1701 struct list_head *tmp = thread_list.next;
1702 list_del(tmp);
1703 kfree(list_entry(tmp, struct elf_thread_status, list));
1706 kfree(elf);
1707 kfree(prstatus);
1708 kfree(psinfo);
1709 kfree(notes);
1710 kfree(fpu);
1711 #ifdef ELF_CORE_COPY_XFPREGS
1712 kfree(xfpu);
1713 #endif
1714 return has_dumped;
1715 #undef NUM_NOTES
1718 #endif /* USE_ELF_CORE_DUMP */
1720 static int __init init_elf_binfmt(void)
1722 return register_binfmt(&elf_format);
1725 static void __exit exit_elf_binfmt(void)
1727 /* Remove the COFF and ELF loaders. */
1728 unregister_binfmt(&elf_format);
1731 core_initcall(init_elf_binfmt);
1732 module_exit(exit_elf_binfmt);
1733 MODULE_LICENSE("GPL");