Fix IP22 timer calibration.
[linux-2.6/linux-mips.git] / fs / binfmt_elf.c
blob548d341155bf6e12fbcc1786d505a944741ee524
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>
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 *);
51 #ifndef elf_addr_t
52 #define elf_addr_t unsigned long
53 #endif
56 * If we don't support core dumping, then supply a NULL so we
57 * don't even try.
59 #ifdef USE_ELF_CORE_DUMP
60 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file);
61 #else
62 #define elf_core_dump NULL
63 #endif
65 #if ELF_EXEC_PAGESIZE > PAGE_SIZE
66 # define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
67 #else
68 # define ELF_MIN_ALIGN PAGE_SIZE
69 #endif
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);
89 if (end > start)
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
98 be in memory */
101 static void padzero(unsigned long elf_bss)
103 unsigned long nbyte;
105 nbyte = ELF_PAGEOFFSET(elf_bss);
106 if (nbyte) {
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 = sp; sp += len; old_sp; })
118 #else
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
123 #endif
125 static void
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;
136 int items;
137 elf_addr_t elf_info[40];
138 int ei_index = 0;
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
145 * merely difficult.
148 u_platform = NULL;
149 if (k_platform) {
150 size_t len = strlen(k_platform) + 1;
152 #ifdef CONFIG_X86_HT
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
161 * before 2.6
164 if (smp_num_siblings > 1)
165 STACK_ALLOC(p, ((current->pid % 64) << 7));
166 #endif
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)
175 #ifdef ARCH_DLINFO
177 * ARCH_DLINFO must come first so PPC can do its special alignment of
178 * AUXV.
180 ARCH_DLINFO;
181 #endif
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));
196 if (k_platform) {
197 NEW_AUX_ENT(AT_PLATFORM, (elf_addr_t)(long)u_platform);
199 NEW_AUX_ENT(AT_NULL, 0);
200 #undef NEW_AUX_ENT
202 sp = STACK_ADD(p, ei_index);
204 items = (argc + 1) + (envc + 1);
205 if (interp_aout) {
206 items += 3; /* a.out interpreters require argv & envp too */
207 } else {
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 */
216 #else
217 sp = (elf_addr_t *)bprm->p;
218 #endif
220 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
221 __put_user(argc, sp++);
222 if (interp_aout) {
223 argv = sp + 2;
224 envp = argv + argc + 1;
225 __put_user((elf_addr_t)(long)argv, sp++);
226 __put_user((elf_addr_t)(long)envp, sp++);
227 } else {
228 argv = sp;
229 envp = argv + argc + 1;
232 /* Populate argv and envp */
233 p = current->mm->arg_start;
234 while (argc-- > 0) {
235 size_t len;
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)
239 return;
240 p += len;
242 __put_user(0, argv);
243 current->mm->arg_end = current->mm->env_start = p;
244 while (envc-- > 0) {
245 size_t len;
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)
249 return;
250 p += len;
252 __put_user(0, envp);
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));
260 #ifndef elf_map
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(&current->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(&current->mm->mmap_sem);
272 return(map_addr);
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
280 an ELF header */
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;
292 int retval, i, size;
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)
297 goto out;
298 if (!elf_check_arch(interp_elf_ex))
299 goto out;
300 if (!interpreter->f_op || !interpreter->f_op->mmap)
301 goto out;
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))
308 goto out;
309 if (interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
310 goto out;
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)
316 goto out;
317 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
318 if (!elf_phdata)
319 goto out;
321 retval = kernel_read(interpreter,interp_elf_ex->e_phoff,(char *)elf_phdata,size);
322 error = retval;
323 if (retval < 0)
324 goto out_close;
326 eppnt = elf_phdata;
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;
330 int elf_prot = 0;
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))
343 goto out_close;
345 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
346 load_addr = map_addr - ELF_PAGESTART(vaddr);
347 load_addr_set = 1;
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;
355 if (k > elf_bss)
356 elf_bss = k;
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;
363 if (k > last_bss)
364 last_bss = k;
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
372 * last bss page.
374 padzero(elf_bss);
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;
384 out_close:
385 kfree(elf_phdata);
386 out:
387 return error;
390 static unsigned long load_aout_interp(struct exec * interp_ex,
391 struct file * interpreter)
393 unsigned long text_data, elf_entry = ~0UL;
394 char * addr;
395 loff_t offset;
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)) {
403 case OMAGIC:
404 offset = 32;
405 addr = (char *) 0;
406 break;
407 case ZMAGIC:
408 case QMAGIC:
409 offset = N_TXTOFF(*interp_ex);
410 addr = (char *) N_TXTADDR(*interp_ex);
411 break;
412 default:
413 goto out;
416 do_brk(0, text_data);
417 if (!interpreter->f_op || !interpreter->f_op->read)
418 goto out;
419 if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
420 goto out;
421 flush_icache_range((unsigned long)addr,
422 (unsigned long)addr + text_data);
424 do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
425 interp_ex->a_bss);
426 elf_entry = interp_ex->a_entry;
428 out:
429 return elf_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;
450 unsigned long error;
451 struct elf_phdr * elf_ppnt, *elf_phdata;
452 unsigned long elf_bss, elf_brk;
453 int elf_exec_fileno;
454 int retval, i;
455 unsigned int size;
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);
467 retval = -ENOEXEC;
468 /* First of all, some simple consistency checks */
469 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
470 goto out;
472 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
473 goto out;
474 if (!elf_check_arch(&elf_ex))
475 goto out;
476 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
477 goto out;
479 /* Now read in all of the header information */
481 retval = -ENOMEM;
482 if (elf_ex.e_phentsize != sizeof(struct elf_phdr))
483 goto out;
484 if (elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
485 goto out;
486 size = elf_ex.e_phnum * sizeof(struct elf_phdr);
487 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
488 if (!elf_phdata)
489 goto out;
491 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *) elf_phdata, size);
492 if (retval < 0)
493 goto out_free_ph;
495 retval = get_unused_fd();
496 if (retval < 0)
497 goto out_free_ph;
498 get_file(bprm->file);
499 fd_install(elf_exec_fileno = retval, bprm->file);
501 elf_ppnt = elf_phdata;
502 elf_bss = 0;
503 elf_brk = 0;
505 start_code = ~0UL;
506 end_code = 0;
507 start_data = 0;
508 end_data = 0;
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
517 retval = -ENOMEM;
518 if (elf_ppnt->p_filesz > PATH_MAX)
519 goto out_free_file;
520 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
521 GFP_KERNEL);
522 if (!elf_interpreter)
523 goto out_free_file;
525 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
526 elf_interpreter,
527 elf_ppnt->p_filesz);
528 if (retval < 0)
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
542 * alternate root.
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);
564 if (retval < 0)
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);
570 break;
572 elf_ppnt++;
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;
588 retval = -ELIBBAD;
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;
599 } else {
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);
613 if (retval)
614 goto out_free_dentry;
615 bprm->argc++;
619 /* Flush all traces of the currently running executable */
620 retval = flush_old_exec(bprm);
621 if (retval)
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);
640 if (retval < 0) {
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
650 address. */
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)
657 continue;
659 if (unlikely (elf_brk > elf_bss)) {
660 unsigned long nbyte;
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);
667 if (nbyte) {
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);
692 if (BAD_ADDR(error))
693 continue;
695 if (!load_addr_set) {
696 load_addr_set = 1;
697 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
698 if (elf_ex.e_type == ET_DYN) {
699 load_bias += error -
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;
711 if (k > elf_bss)
712 elf_bss = k;
713 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
714 end_code = k;
715 if (end_data < k)
716 end_data = k;
717 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
718 if (k > elf_brk)
719 elf_brk = k;
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,
733 interpreter);
734 else
735 elf_entry = load_elf_interp(&interp_elf_ex,
736 interpreter,
737 &interp_load_addr);
739 allow_write_access(interpreter);
740 fput(interpreter);
741 kfree(elf_interpreter);
743 if (BAD_ADDR(elf_entry)) {
744 printk(KERN_ERR "Unable to load interpreter\n");
745 kfree(elf_phdata);
746 send_sig(SIGSEGV, current, 0);
747 retval = -ENOEXEC; /* Nobody gets to see this, but.. */
748 goto out;
750 reloc_func_desc = interp_load_addr;
751 } else {
752 elf_entry = elf_ex.e_entry;
755 kfree(elf_phdata);
757 if (interpreter_type != INTERPRETER_AOUT)
758 sys_close(elf_exec_fileno);
760 set_binfmt(&elf_format);
762 compute_creds(bprm);
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);
780 padzero(elf_bss);
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(&current->mm->mmap_sem);
789 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
790 MAP_FIXED | MAP_PRIVATE, 0);
791 up_write(&current->mm->mmap_sem);
794 #ifdef ELF_PLAT_INIT
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);
806 #endif
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);
812 else
813 send_sig(SIGTRAP, current, 0);
815 retval = 0;
816 out:
817 return retval;
819 /* error cleanup */
820 out_free_dentry:
821 allow_write_access(interpreter);
822 fput(interpreter);
823 out_free_interp:
824 if (elf_interpreter)
825 kfree(elf_interpreter);
826 out_free_file:
827 sys_close(elf_exec_fileno);
828 out_free_ph:
829 kfree(elf_phdata);
830 goto out;
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;
843 error = -ENOEXEC;
844 retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
845 if (retval != sizeof(elf_ex))
846 goto out;
848 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
849 goto out;
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)
854 goto out;
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 */
861 error = -ENOMEM;
862 elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
863 if (!elf_phdata)
864 goto out;
866 error = -ENOEXEC;
867 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata, j);
868 if (retval != j)
869 goto out_free_ph;
871 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
872 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
873 if (j != 1)
874 goto out_free_ph;
876 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
878 /* Now use mmap to map the library into memory. */
879 down_write(&current->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(&current->mm->mmap_sem);
889 if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
890 goto out_free_ph;
892 elf_bss = elf_phdata->p_vaddr + elf_phdata->p_filesz;
893 padzero(elf_bss);
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;
897 if (bss > len)
898 do_brk(len, bss - len);
899 error = 0;
901 out_free_ph:
902 kfree(elf_phdata);
903 out:
904 return error;
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
914 * ELF core dumper
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)
932 return 0;
933 } else
934 file->f_pos = off;
935 return 1;
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))
952 return 0;
954 /* Do not dump I/O mapped devices! -DaveM */
955 if (vma->vm_flags & VM_IO)
956 return 0;
957 #if 1
958 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
959 return 1;
960 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
961 return 0;
962 #endif
963 return 1;
966 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
968 /* An ELF note in memory */
969 struct memelfnote
971 const char *name;
972 int type;
973 unsigned int datasz;
974 void *data;
977 static int notesize(struct memelfnote *en)
979 int sz;
981 sz = sizeof(struct elf_note);
982 sz += roundup(strlen(en->name) + 1, 4);
983 sz += roundup(en->datasz, 4);
985 return sz;
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)
995 struct elf_note en;
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 */
1008 return 1;
1010 #undef DUMP_WRITE
1011 #undef DUMP_SEEK
1013 #define DUMP_WRITE(addr, nr) \
1014 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1015 goto end_coredump;
1016 #define DUMP_SEEK(off) \
1017 if (!dump_seek(file, (off))) \
1018 goto end_coredump;
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;
1031 elf->e_entry = 0;
1032 elf->e_phoff = sizeof(struct elfhdr);
1033 elf->e_shoff = 0;
1034 #ifdef ELF_CORE_EFLAGS
1035 elf->e_flags = ELF_CORE_EFLAGS;
1036 #else
1037 elf->e_flags = 0;
1038 #endif
1039 elf->e_ehsize = sizeof(struct elfhdr);
1040 elf->e_phentsize = sizeof(struct elf_phdr);
1041 elf->e_phnum = segs;
1042 elf->e_shentsize = 0;
1043 elf->e_shnum = 0;
1044 elf->e_shstrndx = 0;
1045 return;
1048 static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
1050 phdr->p_type = PT_NOTE;
1051 phdr->p_offset = offset;
1052 phdr->p_vaddr = 0;
1053 phdr->p_paddr = 0;
1054 phdr->p_filesz = sz;
1055 phdr->p_memsz = 0;
1056 phdr->p_flags = 0;
1057 phdr->p_align = 0;
1058 return;
1061 static void fill_note(struct memelfnote *note, const char *name, int type,
1062 unsigned int sz, void *data)
1064 note->name = name;
1065 note->type = type;
1066 note->datasz = sz;
1067 note->data = data;
1068 return;
1072 * fill up all the fields in prstatus from the given task struct, except registers
1073 * which need to be filled up separately.
1075 static void fill_prstatus(struct elf_prstatus *prstatus,
1076 struct task_struct *p, long signr)
1078 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1079 prstatus->pr_sigpend = p->pending.signal.sig[0];
1080 prstatus->pr_sighold = p->blocked.sig[0];
1081 prstatus->pr_pid = p->pid;
1082 prstatus->pr_ppid = p->parent->pid;
1083 prstatus->pr_pgrp = p->pgrp;
1084 prstatus->pr_sid = p->session;
1085 jiffies_to_timeval(p->utime, &prstatus->pr_utime);
1086 jiffies_to_timeval(p->stime, &prstatus->pr_stime);
1087 jiffies_to_timeval(p->cutime, &prstatus->pr_cutime);
1088 jiffies_to_timeval(p->cstime, &prstatus->pr_cstime);
1091 static void fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p)
1093 int i, len;
1095 /* first copy the parameters from user space */
1096 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1098 len = p->mm->arg_end - p->mm->arg_start;
1099 if (len >= ELF_PRARGSZ)
1100 len = ELF_PRARGSZ-1;
1101 copy_from_user(&psinfo->pr_psargs,
1102 (const char *)p->mm->arg_start, len);
1103 for(i = 0; i < len; i++)
1104 if (psinfo->pr_psargs[i] == 0)
1105 psinfo->pr_psargs[i] = ' ';
1106 psinfo->pr_psargs[len] = 0;
1108 psinfo->pr_pid = p->pid;
1109 psinfo->pr_ppid = p->parent->pid;
1110 psinfo->pr_pgrp = p->pgrp;
1111 psinfo->pr_sid = p->session;
1113 i = p->state ? ffz(~p->state) + 1 : 0;
1114 psinfo->pr_state = i;
1115 psinfo->pr_sname = (i < 0 || i > 5) ? '.' : "RSDTZW"[i];
1116 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1117 psinfo->pr_nice = task_nice(p);
1118 psinfo->pr_flag = p->flags;
1119 psinfo->pr_uid = NEW_TO_OLD_UID(p->uid);
1120 psinfo->pr_gid = NEW_TO_OLD_GID(p->gid);
1121 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1123 return;
1126 /* Here is the structure in which status of each thread is captured. */
1127 struct elf_thread_status
1129 struct list_head list;
1130 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1131 elf_fpregset_t fpu; /* NT_PRFPREG */
1132 #ifdef ELF_CORE_COPY_XFPREGS
1133 elf_fpxregset_t xfpu; /* NT_PRXFPREG */
1134 #endif
1135 struct memelfnote notes[3];
1136 int num_notes;
1140 * In order to add the specific thread information for the elf file format,
1141 * we need to keep a linked list of every threads pr_status and then
1142 * create a single section for them in the final core file.
1144 static int elf_dump_thread_status(long signr, struct task_struct * p, struct list_head * thread_list)
1147 struct elf_thread_status *t;
1148 int sz = 0;
1150 t = kmalloc(sizeof(*t), GFP_ATOMIC);
1151 if (!t)
1152 return 0;
1153 memset(t, 0, sizeof(*t));
1155 INIT_LIST_HEAD(&t->list);
1156 t->num_notes = 0;
1158 fill_prstatus(&t->prstatus, p, signr);
1159 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1161 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus), &(t->prstatus));
1162 t->num_notes++;
1163 sz += notesize(&t->notes[0]);
1165 if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, &t->fpu))) {
1166 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu), &(t->fpu));
1167 t->num_notes++;
1168 sz += notesize(&t->notes[1]);
1171 #ifdef ELF_CORE_COPY_XFPREGS
1172 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
1173 fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu), &t->xfpu);
1174 t->num_notes++;
1175 sz += notesize(&t->notes[2]);
1177 #endif
1178 list_add(&t->list, thread_list);
1179 return sz;
1183 * Actual dumper
1185 * This is a two-pass process; first we find the offsets of the bits,
1186 * and then they are actually written out. If we run out of core limit
1187 * we just truncate.
1189 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
1191 #define NUM_NOTES 5
1192 int has_dumped = 0;
1193 mm_segment_t fs;
1194 int segs;
1195 size_t size = 0;
1196 int i;
1197 struct vm_area_struct *vma;
1198 struct elfhdr *elf = NULL;
1199 off_t offset = 0, dataoff;
1200 unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1201 int numnote = NUM_NOTES;
1202 struct memelfnote *notes = NULL;
1203 struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
1204 struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
1205 struct task_struct *g, *p;
1206 LIST_HEAD(thread_list);
1207 struct list_head *t;
1208 elf_fpregset_t *fpu = NULL;
1209 #ifdef ELF_CORE_COPY_XFPREGS
1210 elf_fpxregset_t *xfpu = NULL;
1211 #endif
1212 int thread_status_size = 0;
1215 * We no longer stop all VM operations.
1217 * This is because those proceses that could possibly change map_count or
1218 * the mmap / vma pages are now blocked in do_exit on current finishing
1219 * this core dump.
1221 * Only ptrace can touch these memory addresses, but it doesn't change
1222 * the map_count or the pages allocated. So no possibility of crashing
1223 * exists while dumping the mm->vm_next areas to the core file.
1226 /* alloc memory for large data structures: too large to be on stack */
1227 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1228 if (!elf)
1229 goto cleanup;
1230 prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
1231 if (!prstatus)
1232 goto cleanup;
1233 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1234 if (!psinfo)
1235 goto cleanup;
1236 notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1237 if (!notes)
1238 goto cleanup;
1239 fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1240 if (!fpu)
1241 goto cleanup;
1242 #ifdef ELF_CORE_COPY_XFPREGS
1243 xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1244 if (!xfpu)
1245 goto cleanup;
1246 #endif
1248 /* capture the status of all other threads */
1249 if (signr) {
1250 read_lock(&tasklist_lock);
1251 do_each_thread(g,p)
1252 if (current->mm == p->mm && current != p) {
1253 int sz = elf_dump_thread_status(signr, p, &thread_list);
1254 if (!sz) {
1255 read_unlock(&tasklist_lock);
1256 goto cleanup;
1257 } else
1258 thread_status_size += sz;
1260 while_each_thread(g,p);
1261 read_unlock(&tasklist_lock);
1264 /* now collect the dump for the current */
1265 memset(prstatus, 0, sizeof(*prstatus));
1266 fill_prstatus(prstatus, current, signr);
1267 elf_core_copy_regs(&prstatus->pr_reg, regs);
1269 segs = current->mm->map_count;
1270 #ifdef ELF_CORE_EXTRA_PHDRS
1271 segs += ELF_CORE_EXTRA_PHDRS;
1272 #endif
1274 /* Set up header */
1275 fill_elf_header(elf, segs+1); /* including notes section */
1277 has_dumped = 1;
1278 current->flags |= PF_DUMPCORE;
1281 * Set up the notes in similar form to SVR4 core dumps made
1282 * with info from their /proc.
1285 fill_note(notes +0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
1287 fill_psinfo(psinfo, current->group_leader);
1288 fill_note(notes +1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1290 fill_note(notes +2, "CORE", NT_TASKSTRUCT, sizeof(*current), current);
1292 /* Try to dump the FPU. */
1293 if ((prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, fpu)))
1294 fill_note(notes +3, "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1295 else
1296 --numnote;
1297 #ifdef ELF_CORE_COPY_XFPREGS
1298 if (elf_core_copy_task_xfpregs(current, xfpu))
1299 fill_note(notes +4, "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
1300 else
1301 --numnote;
1302 #else
1303 numnote--;
1304 #endif
1306 fs = get_fs();
1307 set_fs(KERNEL_DS);
1309 DUMP_WRITE(elf, sizeof(*elf));
1310 offset += sizeof(*elf); /* Elf header */
1311 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1313 /* Write notes phdr entry */
1315 struct elf_phdr phdr;
1316 int sz = 0;
1318 for (i = 0; i < numnote; i++)
1319 sz += notesize(notes + i);
1321 sz += thread_status_size;
1323 fill_elf_note_phdr(&phdr, sz, offset);
1324 offset += sz;
1325 DUMP_WRITE(&phdr, sizeof(phdr));
1328 /* Page-align dumped data */
1329 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1331 /* Write program headers for segments dump */
1332 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1333 struct elf_phdr phdr;
1334 size_t sz;
1336 sz = vma->vm_end - vma->vm_start;
1338 phdr.p_type = PT_LOAD;
1339 phdr.p_offset = offset;
1340 phdr.p_vaddr = vma->vm_start;
1341 phdr.p_paddr = 0;
1342 phdr.p_filesz = maydump(vma) ? sz : 0;
1343 phdr.p_memsz = sz;
1344 offset += phdr.p_filesz;
1345 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1346 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1347 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1348 phdr.p_align = ELF_EXEC_PAGESIZE;
1350 DUMP_WRITE(&phdr, sizeof(phdr));
1353 #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1354 ELF_CORE_WRITE_EXTRA_PHDRS;
1355 #endif
1357 /* write out the notes section */
1358 for (i = 0; i < numnote; i++)
1359 if (!writenote(notes + i, file))
1360 goto end_coredump;
1362 /* write out the thread status notes section */
1363 list_for_each(t, &thread_list) {
1364 struct elf_thread_status *tmp = list_entry(t, struct elf_thread_status, list);
1365 for (i = 0; i < tmp->num_notes; i++)
1366 if (!writenote(&tmp->notes[i], file))
1367 goto end_coredump;
1370 DUMP_SEEK(dataoff);
1372 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1373 unsigned long addr;
1375 if (!maydump(vma))
1376 continue;
1378 for (addr = vma->vm_start;
1379 addr < vma->vm_end;
1380 addr += PAGE_SIZE) {
1381 struct page* page;
1382 struct vm_area_struct *vma;
1384 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1385 &page, &vma) <= 0) {
1386 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1387 } else {
1388 if (page == ZERO_PAGE(addr)) {
1389 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1390 } else {
1391 void *kaddr;
1392 flush_cache_page(vma, addr);
1393 kaddr = kmap(page);
1394 DUMP_WRITE(kaddr, PAGE_SIZE);
1395 kunmap(page);
1397 page_cache_release(page);
1402 #ifdef ELF_CORE_WRITE_EXTRA_DATA
1403 ELF_CORE_WRITE_EXTRA_DATA;
1404 #endif
1406 if ((off_t) file->f_pos != offset) {
1407 /* Sanity check */
1408 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1409 (off_t) file->f_pos, offset);
1412 end_coredump:
1413 set_fs(fs);
1415 cleanup:
1416 while(!list_empty(&thread_list)) {
1417 struct list_head *tmp = thread_list.next;
1418 list_del(tmp);
1419 kfree(list_entry(tmp, struct elf_thread_status, list));
1422 kfree(elf);
1423 kfree(prstatus);
1424 kfree(psinfo);
1425 kfree(notes);
1426 kfree(fpu);
1427 #ifdef ELF_CORE_COPY_XFPREGS
1428 kfree(xfpu);
1429 #endif
1430 return has_dumped;
1431 #undef NUM_NOTES
1434 #endif /* USE_ELF_CORE_DUMP */
1436 static int __init init_elf_binfmt(void)
1438 return register_binfmt(&elf_format);
1441 static void __exit exit_elf_binfmt(void)
1443 /* Remove the COFF and ELF loaders. */
1444 unregister_binfmt(&elf_format);
1447 module_init(init_elf_binfmt)
1448 module_exit(exit_elf_binfmt)
1449 MODULE_LICENSE("GPL");