Partial rewrite.
[linux-2.6/linux-mips.git] / fs / binfmt_elf.c
blob910c65de26fba62e8358a2a7ae2efe99a677859a
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>
39 #include <asm/uaccess.h>
40 #include <asm/param.h>
41 #include <asm/pgalloc.h>
43 #include <linux/elf.h>
45 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs);
46 static int load_elf_library(struct file*);
47 static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int);
48 extern int dump_fpu (struct pt_regs *, elf_fpregset_t *);
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 #ifdef USE_ELF_CORE_DUMP
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 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
71 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
72 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
74 static struct linux_binfmt elf_format = {
75 .module = THIS_MODULE,
76 .load_binary = load_elf_binary,
77 .load_shlib = load_elf_library,
78 .core_dump = elf_core_dump,
79 .min_coredump = ELF_EXEC_PAGESIZE
82 #define BAD_ADDR(x) ((unsigned long)(x) > TASK_SIZE)
84 static void set_brk(unsigned long start, unsigned long end)
86 start = ELF_PAGEALIGN(start);
87 end = ELF_PAGEALIGN(end);
88 if (end > start)
89 do_brk(start, end - start);
90 current->mm->start_brk = current->mm->brk = end;
94 /* We need to explicitly zero any fractional pages
95 after the data section (i.e. bss). This would
96 contain the junk from the file that should not
97 be in memory */
100 static void padzero(unsigned long elf_bss)
102 unsigned long nbyte;
104 nbyte = ELF_PAGEOFFSET(elf_bss);
105 if (nbyte) {
106 nbyte = ELF_MIN_ALIGN - nbyte;
107 clear_user((void *) elf_bss, nbyte);
111 /* Let's use some macros to make this stack manipulation a litle clearer */
112 #ifdef CONFIG_STACK_GROWSUP
113 #define STACK_ADD(sp, items) ((elf_addr_t *)(sp) + (items))
114 #define STACK_ROUND(sp, items) \
115 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
116 #define STACK_ALLOC(sp, len) ({ elf_addr_t *old_sp = sp; sp += len; old_sp; })
117 #else
118 #define STACK_ADD(sp, items) ((elf_addr_t *)(sp) - (items))
119 #define STACK_ROUND(sp, items) \
120 (((unsigned long) (sp - items)) &~ 15UL)
121 #define STACK_ALLOC(sp, len) sp -= len
122 #endif
124 static void
125 create_elf_tables(struct linux_binprm *bprm, struct elfhdr * exec,
126 int interp_aout, unsigned long load_addr,
127 unsigned long interp_load_addr)
129 unsigned long p = bprm->p;
130 int argc = bprm->argc;
131 int envc = bprm->envc;
132 elf_addr_t *argv, *envp;
133 elf_addr_t *sp, *u_platform;
134 const char *k_platform = ELF_PLATFORM;
135 int items;
136 elf_addr_t elf_info[40];
137 int ei_index = 0;
138 struct task_struct *tsk = current;
141 * If this architecture has a platform capability string, copy it
142 * to userspace. In some cases (Sparc), this info is impossible
143 * for userspace to get any other way, in others (i386) it is
144 * merely difficult.
147 u_platform = NULL;
148 if (k_platform) {
149 size_t len = strlen(k_platform) + 1;
151 #ifdef CONFIG_X86_HT
153 * In some cases (e.g. Hyper-Threading), we want to avoid L1
154 * evictions by the processes running on the same package. One
155 * thing we can do is to shuffle the initial stack for them.
157 * The conditionals here are unneeded, but kept in to make the
158 * code behaviour the same as pre change unless we have
159 * hyperthreaded processors. This should be cleaned up
160 * before 2.6
163 if (smp_num_siblings > 1)
164 STACK_ALLOC(p, ((current->pid % 64) << 7));
165 #endif
166 u_platform = (elf_addr_t *) STACK_ALLOC(p, len);
167 __copy_to_user(u_platform, k_platform, len);
170 /* Create the ELF interpreter info */
171 #define NEW_AUX_ENT(id, val) \
172 do { elf_info[ei_index++] = id; elf_info[ei_index++] = val; } while (0)
174 #ifdef ARCH_DLINFO
176 * ARCH_DLINFO must come first so PPC can do its special alignment of
177 * AUXV.
179 ARCH_DLINFO;
180 #endif
181 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
182 NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
183 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
184 NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
185 NEW_AUX_ENT(AT_PHENT, sizeof (struct elf_phdr));
186 NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
187 NEW_AUX_ENT(AT_BASE, interp_load_addr);
188 NEW_AUX_ENT(AT_FLAGS, 0);
189 NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
190 NEW_AUX_ENT(AT_UID, (elf_addr_t) tsk->uid);
191 NEW_AUX_ENT(AT_EUID, (elf_addr_t) tsk->euid);
192 NEW_AUX_ENT(AT_GID, (elf_addr_t) tsk->gid);
193 NEW_AUX_ENT(AT_EGID, (elf_addr_t) tsk->egid);
194 if (k_platform) {
195 NEW_AUX_ENT(AT_PLATFORM, (elf_addr_t)(long)u_platform);
197 NEW_AUX_ENT(AT_NULL, 0);
198 #undef NEW_AUX_ENT
200 sp = STACK_ADD(p, ei_index);
202 items = (argc + 1) + (envc + 1);
203 if (interp_aout) {
204 items += 3; /* a.out interpreters require argv & envp too */
205 } else {
206 items += 1; /* ELF interpreters only put argc on the stack */
208 bprm->p = STACK_ROUND(sp, items);
210 /* Point sp at the lowest address on the stack */
211 #ifdef CONFIG_STACK_GROWSUP
212 sp = (elf_addr_t *)bprm->p - items - ei_index;
213 bprm->exec = (unsigned long) sp; /* XXX: PARISC HACK */
214 #else
215 sp = (elf_addr_t *)bprm->p;
216 #endif
218 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
219 __put_user(argc, sp++);
220 if (interp_aout) {
221 argv = sp + 2;
222 envp = argv + argc + 1;
223 __put_user((elf_addr_t)(long)argv, sp++);
224 __put_user((elf_addr_t)(long)envp, sp++);
225 } else {
226 argv = sp;
227 envp = argv + argc + 1;
230 /* Populate argv and envp */
231 p = current->mm->arg_start;
232 while (argc-- > 0) {
233 size_t len;
234 __put_user((elf_addr_t)p, argv++);
235 len = strnlen_user((void *)p, PAGE_SIZE*MAX_ARG_PAGES);
236 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
237 return;
238 p += len;
240 __put_user(0, argv);
241 current->mm->arg_end = current->mm->env_start = p;
242 while (envc-- > 0) {
243 size_t len;
244 __put_user((elf_addr_t)p, envp++);
245 len = strnlen_user((void *)p, PAGE_SIZE*MAX_ARG_PAGES);
246 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
247 return;
248 p += len;
250 __put_user(0, envp);
251 current->mm->env_end = p;
253 /* Put the elf_info on the stack in the right place. */
254 sp = (elf_addr_t *)envp + 1;
255 copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t));
258 #ifndef elf_map
260 static unsigned long elf_map(struct file *filep, unsigned long addr,
261 struct elf_phdr *eppnt, int prot, int type)
263 unsigned long map_addr;
265 down_write(&current->mm->mmap_sem);
266 map_addr = do_mmap(filep, ELF_PAGESTART(addr),
267 eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr), prot, type,
268 eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
269 up_write(&current->mm->mmap_sem);
270 return(map_addr);
273 #endif /* !elf_map */
275 /* This is much more generalized than the library routine read function,
276 so we keep this separate. Technically the library read function
277 is only provided so that we can read a.out libraries that have
278 an ELF header */
280 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
281 struct file * interpreter,
282 unsigned long *interp_load_addr)
284 struct elf_phdr *elf_phdata;
285 struct elf_phdr *eppnt;
286 unsigned long load_addr = 0;
287 int load_addr_set = 0;
288 unsigned long last_bss = 0, elf_bss = 0;
289 unsigned long error = ~0UL;
290 int retval, i, size;
292 /* First of all, some simple consistency checks */
293 if (interp_elf_ex->e_type != ET_EXEC &&
294 interp_elf_ex->e_type != ET_DYN)
295 goto out;
296 if (!elf_check_arch(interp_elf_ex))
297 goto out;
298 if (!interpreter->f_op || !interpreter->f_op->mmap)
299 goto out;
302 * If the size of this structure has changed, then punt, since
303 * we will be doing the wrong thing.
305 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
306 goto out;
307 if (interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
308 goto out;
310 /* Now read in all of the header information */
312 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
313 if (size > ELF_MIN_ALIGN)
314 goto out;
315 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
316 if (!elf_phdata)
317 goto out;
319 retval = kernel_read(interpreter,interp_elf_ex->e_phoff,(char *)elf_phdata,size);
320 error = retval;
321 if (retval < 0)
322 goto out_close;
324 eppnt = elf_phdata;
325 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
326 if (eppnt->p_type == PT_LOAD) {
327 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
328 int elf_prot = 0;
329 unsigned long vaddr = 0;
330 unsigned long k, map_addr;
332 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
333 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
334 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
335 vaddr = eppnt->p_vaddr;
336 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
337 elf_type |= MAP_FIXED;
339 map_addr = elf_map(interpreter, load_addr + vaddr, eppnt, elf_prot, elf_type);
340 if (BAD_ADDR(map_addr))
341 goto out_close;
343 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
344 load_addr = map_addr - ELF_PAGESTART(vaddr);
345 load_addr_set = 1;
349 * Find the end of the file mapping for this phdr, and keep
350 * track of the largest address we see for this.
352 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
353 if (k > elf_bss)
354 elf_bss = k;
357 * Do the same thing for the memory mapping - between
358 * elf_bss and last_bss is the bss section.
360 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
361 if (k > last_bss)
362 last_bss = k;
367 * Now fill out the bss section. First pad the last page up
368 * to the page boundary, and then perform a mmap to make sure
369 * that there are zero-mapped pages up to and including the
370 * last bss page.
372 padzero(elf_bss);
373 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1); /* What we have mapped so far */
375 /* Map the last of the bss segment */
376 if (last_bss > elf_bss)
377 do_brk(elf_bss, last_bss - elf_bss);
379 *interp_load_addr = load_addr;
380 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
382 out_close:
383 kfree(elf_phdata);
384 out:
385 return error;
388 static unsigned long load_aout_interp(struct exec * interp_ex,
389 struct file * interpreter)
391 unsigned long text_data, elf_entry = ~0UL;
392 char * addr;
393 loff_t offset;
395 current->mm->end_code = interp_ex->a_text;
396 text_data = interp_ex->a_text + interp_ex->a_data;
397 current->mm->end_data = text_data;
398 current->mm->brk = interp_ex->a_bss + text_data;
400 switch (N_MAGIC(*interp_ex)) {
401 case OMAGIC:
402 offset = 32;
403 addr = (char *) 0;
404 break;
405 case ZMAGIC:
406 case QMAGIC:
407 offset = N_TXTOFF(*interp_ex);
408 addr = (char *) N_TXTADDR(*interp_ex);
409 break;
410 default:
411 goto out;
414 do_brk(0, text_data);
415 if (!interpreter->f_op || !interpreter->f_op->read)
416 goto out;
417 if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
418 goto out;
419 flush_icache_range((unsigned long)addr,
420 (unsigned long)addr + text_data);
422 do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
423 interp_ex->a_bss);
424 elf_entry = interp_ex->a_entry;
426 out:
427 return elf_entry;
431 * These are the functions used to load ELF style executables and shared
432 * libraries. There is no binary dependent code anywhere else.
435 #define INTERPRETER_NONE 0
436 #define INTERPRETER_AOUT 1
437 #define INTERPRETER_ELF 2
440 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
442 struct file *interpreter = NULL; /* to shut gcc up */
443 unsigned long load_addr = 0, load_bias = 0;
444 int load_addr_set = 0;
445 char * elf_interpreter = NULL;
446 unsigned int interpreter_type = INTERPRETER_NONE;
447 unsigned char ibcs2_interpreter = 0;
448 unsigned long error;
449 struct elf_phdr * elf_ppnt, *elf_phdata;
450 unsigned long elf_bss, elf_brk;
451 int elf_exec_fileno;
452 int retval, i;
453 unsigned int size;
454 unsigned long elf_entry, interp_load_addr = 0;
455 unsigned long start_code, end_code, start_data, end_data;
456 unsigned long reloc_func_desc = 0;
457 struct elfhdr elf_ex;
458 struct elfhdr interp_elf_ex;
459 struct exec interp_ex;
460 char passed_fileno[6];
462 /* Get the exec-header */
463 elf_ex = *((struct elfhdr *) bprm->buf);
465 retval = -ENOEXEC;
466 /* First of all, some simple consistency checks */
467 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
468 goto out;
470 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
471 goto out;
472 if (!elf_check_arch(&elf_ex))
473 goto out;
474 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
475 goto out;
477 /* Now read in all of the header information */
479 retval = -ENOMEM;
480 if (elf_ex.e_phentsize != sizeof(struct elf_phdr))
481 goto out;
482 if (elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
483 goto out;
484 size = elf_ex.e_phnum * sizeof(struct elf_phdr);
485 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
486 if (!elf_phdata)
487 goto out;
489 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *) elf_phdata, size);
490 if (retval < 0)
491 goto out_free_ph;
493 retval = get_unused_fd();
494 if (retval < 0)
495 goto out_free_ph;
496 get_file(bprm->file);
497 fd_install(elf_exec_fileno = retval, bprm->file);
499 elf_ppnt = elf_phdata;
500 elf_bss = 0;
501 elf_brk = 0;
503 start_code = ~0UL;
504 end_code = 0;
505 start_data = 0;
506 end_data = 0;
508 for (i = 0; i < elf_ex.e_phnum; i++) {
509 if (elf_ppnt->p_type == PT_INTERP) {
510 /* This is the program interpreter used for
511 * shared libraries - for now assume that this
512 * is an a.out format binary
515 retval = -ENOMEM;
516 if (elf_ppnt->p_filesz > PATH_MAX)
517 goto out_free_file;
518 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
519 GFP_KERNEL);
520 if (!elf_interpreter)
521 goto out_free_file;
523 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
524 elf_interpreter,
525 elf_ppnt->p_filesz);
526 if (retval < 0)
527 goto out_free_interp;
528 /* If the program interpreter is one of these two,
529 * then assume an iBCS2 image. Otherwise assume
530 * a native linux image.
532 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
533 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
534 ibcs2_interpreter = 1;
537 * The early SET_PERSONALITY here is so that the lookup
538 * for the interpreter happens in the namespace of the
539 * to-be-execed image. SET_PERSONALITY can select an
540 * alternate root.
542 * However, SET_PERSONALITY is NOT allowed to switch
543 * this task into the new images's memory mapping
544 * policy - that is, TASK_SIZE must still evaluate to
545 * that which is appropriate to the execing application.
546 * This is because exit_mmap() needs to have TASK_SIZE
547 * evaluate to the size of the old image.
549 * So if (say) a 64-bit application is execing a 32-bit
550 * application it is the architecture's responsibility
551 * to defer changing the value of TASK_SIZE until the
552 * switch really is going to happen - do this in
553 * flush_thread(). - akpm
555 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
557 interpreter = open_exec(elf_interpreter);
558 retval = PTR_ERR(interpreter);
559 if (IS_ERR(interpreter))
560 goto out_free_interp;
561 retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
562 if (retval < 0)
563 goto out_free_dentry;
565 /* Get the exec headers */
566 interp_ex = *((struct exec *) bprm->buf);
567 interp_elf_ex = *((struct elfhdr *) bprm->buf);
568 break;
570 elf_ppnt++;
573 /* Some simple consistency checks for the interpreter */
574 if (elf_interpreter) {
575 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
577 /* Now figure out which format our binary is */
578 if ((N_MAGIC(interp_ex) != OMAGIC) &&
579 (N_MAGIC(interp_ex) != ZMAGIC) &&
580 (N_MAGIC(interp_ex) != QMAGIC))
581 interpreter_type = INTERPRETER_ELF;
583 if (memcmp(interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
584 interpreter_type &= ~INTERPRETER_ELF;
586 retval = -ELIBBAD;
587 if (!interpreter_type)
588 goto out_free_dentry;
590 /* Make sure only one type was selected */
591 if ((interpreter_type & INTERPRETER_ELF) &&
592 interpreter_type != INTERPRETER_ELF) {
593 // FIXME - ratelimit this before re-enabling
594 // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
595 interpreter_type = INTERPRETER_ELF;
597 } else {
598 /* Executables without an interpreter also need a personality */
599 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
602 /* OK, we are done with that, now set up the arg stuff,
603 and then start this sucker up */
605 if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
606 char *passed_p = passed_fileno;
607 sprintf(passed_fileno, "%d", elf_exec_fileno);
609 if (elf_interpreter) {
610 retval = copy_strings_kernel(1, &passed_p, bprm);
611 if (retval)
612 goto out_free_dentry;
613 bprm->argc++;
617 /* Flush all traces of the currently running executable */
618 retval = flush_old_exec(bprm);
619 if (retval)
620 goto out_free_dentry;
622 /* OK, This is the point of no return */
623 current->mm->start_data = 0;
624 current->mm->end_data = 0;
625 current->mm->end_code = 0;
626 current->mm->mmap = NULL;
627 current->flags &= ~PF_FORKNOEXEC;
629 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
630 may depend on the personality. */
631 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
633 /* Do this so that we can load the interpreter, if need be. We will
634 change some of these later */
635 current->mm->rss = 0;
636 current->mm->free_area_cache = TASK_UNMAPPED_BASE;
637 retval = setup_arg_pages(bprm);
638 if (retval < 0) {
639 send_sig(SIGKILL, current, 0);
640 goto out_free_dentry;
643 current->mm->start_stack = bprm->p;
645 /* Now we do a little grungy work by mmaping the ELF image into
646 the correct location in memory. At this point, we assume that
647 the image should be loaded at fixed address, not at a variable
648 address. */
650 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
651 int elf_prot = 0, elf_flags;
652 unsigned long k, vaddr;
654 if (elf_ppnt->p_type != PT_LOAD)
655 continue;
657 if (unlikely (elf_brk > elf_bss)) {
658 unsigned long nbyte;
660 /* There was a PT_LOAD segment with p_memsz > p_filesz
661 before this one. Map anonymous pages, if needed,
662 and clear the area. */
663 set_brk (elf_bss + load_bias, elf_brk + load_bias);
664 nbyte = ELF_PAGEOFFSET(elf_bss);
665 if (nbyte) {
666 nbyte = ELF_MIN_ALIGN - nbyte;
667 if (nbyte > elf_brk - elf_bss)
668 nbyte = elf_brk - elf_bss;
669 clear_user((void *) elf_bss + load_bias, nbyte);
673 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
674 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
675 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
677 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
679 vaddr = elf_ppnt->p_vaddr;
680 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
681 elf_flags |= MAP_FIXED;
682 } else if (elf_ex.e_type == ET_DYN) {
683 /* Try and get dynamic programs out of the way of the default mmap
684 base, as well as whatever program they might try to exec. This
685 is because the brk will follow the loader, and is not movable. */
686 load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
689 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags);
690 if (BAD_ADDR(error))
691 continue;
693 if (!load_addr_set) {
694 load_addr_set = 1;
695 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
696 if (elf_ex.e_type == ET_DYN) {
697 load_bias += error -
698 ELF_PAGESTART(load_bias + vaddr);
699 load_addr += load_bias;
700 reloc_func_desc = load_bias;
703 k = elf_ppnt->p_vaddr;
704 if (k < start_code) start_code = k;
705 if (start_data < k) start_data = k;
707 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
709 if (k > elf_bss)
710 elf_bss = k;
711 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
712 end_code = k;
713 if (end_data < k)
714 end_data = k;
715 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
716 if (k > elf_brk)
717 elf_brk = k;
720 elf_ex.e_entry += load_bias;
721 elf_bss += load_bias;
722 elf_brk += load_bias;
723 start_code += load_bias;
724 end_code += load_bias;
725 start_data += load_bias;
726 end_data += load_bias;
728 if (elf_interpreter) {
729 if (interpreter_type == INTERPRETER_AOUT)
730 elf_entry = load_aout_interp(&interp_ex,
731 interpreter);
732 else
733 elf_entry = load_elf_interp(&interp_elf_ex,
734 interpreter,
735 &interp_load_addr);
737 allow_write_access(interpreter);
738 fput(interpreter);
739 kfree(elf_interpreter);
741 if (BAD_ADDR(elf_entry)) {
742 printk(KERN_ERR "Unable to load interpreter\n");
743 kfree(elf_phdata);
744 send_sig(SIGSEGV, current, 0);
745 retval = -ENOEXEC; /* Nobody gets to see this, but.. */
746 goto out;
748 reloc_func_desc = interp_load_addr;
749 } else {
750 elf_entry = elf_ex.e_entry;
753 kfree(elf_phdata);
755 if (interpreter_type != INTERPRETER_AOUT)
756 sys_close(elf_exec_fileno);
758 set_binfmt(&elf_format);
760 compute_creds(bprm);
761 current->flags &= ~PF_FORKNOEXEC;
762 create_elf_tables(bprm, &elf_ex, (interpreter_type == INTERPRETER_AOUT),
763 load_addr, interp_load_addr);
764 /* N.B. passed_fileno might not be initialized? */
765 if (interpreter_type == INTERPRETER_AOUT)
766 current->mm->arg_start += strlen(passed_fileno) + 1;
767 current->mm->end_code = end_code;
768 current->mm->start_code = start_code;
769 current->mm->start_data = start_data;
770 current->mm->end_data = end_data;
771 current->mm->start_stack = bprm->p;
773 /* Calling set_brk effectively mmaps the pages that we need
774 * for the bss and break sections
776 set_brk(elf_bss, elf_brk);
778 padzero(elf_bss);
780 if (current->personality & MMAP_PAGE_ZERO) {
781 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
782 and some applications "depend" upon this behavior.
783 Since we do not have the power to recompile these, we
784 emulate the SVr4 behavior. Sigh. */
785 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
786 down_write(&current->mm->mmap_sem);
787 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
788 MAP_FIXED | MAP_PRIVATE, 0);
789 up_write(&current->mm->mmap_sem);
792 #ifdef ELF_PLAT_INIT
794 * The ABI may specify that certain registers be set up in special
795 * ways (on i386 %edx is the address of a DT_FINI function, for
796 * example. In addition, it may also specify (eg, PowerPC64 ELF)
797 * that the e_entry field is the address of the function descriptor
798 * for the startup routine, rather than the address of the startup
799 * routine itself. This macro performs whatever initialization to
800 * the regs structure is required as well as any relocations to the
801 * function descriptor entries when executing dynamically links apps.
803 ELF_PLAT_INIT(regs, reloc_func_desc);
804 #endif
806 start_thread(regs, elf_entry, bprm->p);
807 if (unlikely(current->ptrace & PT_PTRACED)) {
808 if (current->ptrace & PT_TRACE_EXEC)
809 ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
810 else
811 send_sig(SIGTRAP, current, 0);
813 retval = 0;
814 out:
815 return retval;
817 /* error cleanup */
818 out_free_dentry:
819 allow_write_access(interpreter);
820 fput(interpreter);
821 out_free_interp:
822 if (elf_interpreter)
823 kfree(elf_interpreter);
824 out_free_file:
825 sys_close(elf_exec_fileno);
826 out_free_ph:
827 kfree(elf_phdata);
828 goto out;
831 /* This is really simpleminded and specialized - we are loading an
832 a.out library that is given an ELF header. */
834 static int load_elf_library(struct file *file)
836 struct elf_phdr *elf_phdata;
837 unsigned long elf_bss, bss, len;
838 int retval, error, i, j;
839 struct elfhdr elf_ex;
841 error = -ENOEXEC;
842 retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
843 if (retval != sizeof(elf_ex))
844 goto out;
846 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
847 goto out;
849 /* First of all, some simple consistency checks */
850 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
851 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
852 goto out;
854 /* Now read in all of the header information */
856 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
857 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
859 error = -ENOMEM;
860 elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
861 if (!elf_phdata)
862 goto out;
864 error = -ENOEXEC;
865 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata, j);
866 if (retval != j)
867 goto out_free_ph;
869 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
870 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
871 if (j != 1)
872 goto out_free_ph;
874 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
876 /* Now use mmap to map the library into memory. */
877 down_write(&current->mm->mmap_sem);
878 error = do_mmap(file,
879 ELF_PAGESTART(elf_phdata->p_vaddr),
880 (elf_phdata->p_filesz +
881 ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
882 PROT_READ | PROT_WRITE | PROT_EXEC,
883 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
884 (elf_phdata->p_offset -
885 ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
886 up_write(&current->mm->mmap_sem);
887 if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
888 goto out_free_ph;
890 elf_bss = elf_phdata->p_vaddr + elf_phdata->p_filesz;
891 padzero(elf_bss);
893 len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr + ELF_MIN_ALIGN - 1);
894 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
895 if (bss > len)
896 do_brk(len, bss - len);
897 error = 0;
899 out_free_ph:
900 kfree(elf_phdata);
901 out:
902 return error;
906 * Note that some platforms still use traditional core dumps and not
907 * the ELF core dump. Each platform can select it as appropriate.
909 #ifdef USE_ELF_CORE_DUMP
912 * ELF core dumper
914 * Modelled on fs/exec.c:aout_core_dump()
915 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
918 * These are the only things you should do on a core-file: use only these
919 * functions to write out all the necessary info.
921 static int dump_write(struct file *file, const void *addr, int nr)
923 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
926 static int dump_seek(struct file *file, off_t off)
928 if (file->f_op->llseek) {
929 if (file->f_op->llseek(file, off, 0) != off)
930 return 0;
931 } else
932 file->f_pos = off;
933 return 1;
937 * Decide whether a segment is worth dumping; default is yes to be
938 * sure (missing info is worse than too much; etc).
939 * Personally I'd include everything, and use the coredump limit...
941 * I think we should skip something. But I am not sure how. H.J.
943 static int maydump(struct vm_area_struct *vma)
946 * If we may not read the contents, don't allow us to dump
947 * them either. "dump_write()" can't handle it anyway.
949 if (!(vma->vm_flags & VM_READ))
950 return 0;
952 /* Do not dump I/O mapped devices! -DaveM */
953 if (vma->vm_flags & VM_IO)
954 return 0;
955 #if 1
956 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
957 return 1;
958 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
959 return 0;
960 #endif
961 return 1;
964 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
966 /* An ELF note in memory */
967 struct memelfnote
969 const char *name;
970 int type;
971 unsigned int datasz;
972 void *data;
975 static int notesize(struct memelfnote *en)
977 int sz;
979 sz = sizeof(struct elf_note);
980 sz += roundup(strlen(en->name) + 1, 4);
981 sz += roundup(en->datasz, 4);
983 return sz;
986 #define DUMP_WRITE(addr, nr) \
987 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
988 #define DUMP_SEEK(off) \
989 do { if (!dump_seek(file, (off))) return 0; } while(0)
991 static int writenote(struct memelfnote *men, struct file *file)
993 struct elf_note en;
995 en.n_namesz = strlen(men->name) + 1;
996 en.n_descsz = men->datasz;
997 en.n_type = men->type;
999 DUMP_WRITE(&en, sizeof(en));
1000 DUMP_WRITE(men->name, en.n_namesz);
1001 /* XXX - cast from long long to long to avoid need for libgcc.a */
1002 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1003 DUMP_WRITE(men->data, men->datasz);
1004 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1006 return 1;
1008 #undef DUMP_WRITE
1009 #undef DUMP_SEEK
1011 #define DUMP_WRITE(addr, nr) \
1012 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1013 goto end_coredump;
1014 #define DUMP_SEEK(off) \
1015 if (!dump_seek(file, (off))) \
1016 goto end_coredump;
1018 static inline void fill_elf_header(struct elfhdr *elf, int segs)
1020 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1021 elf->e_ident[EI_CLASS] = ELF_CLASS;
1022 elf->e_ident[EI_DATA] = ELF_DATA;
1023 elf->e_ident[EI_VERSION] = EV_CURRENT;
1024 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1026 elf->e_type = ET_CORE;
1027 elf->e_machine = ELF_ARCH;
1028 elf->e_version = EV_CURRENT;
1029 elf->e_entry = 0;
1030 elf->e_phoff = sizeof(struct elfhdr);
1031 elf->e_shoff = 0;
1032 #ifdef ELF_CORE_EFLAGS
1033 elf->e_flags = ELF_CORE_EFLAGS;
1034 #else
1035 elf->e_flags = 0;
1036 #endif
1037 elf->e_ehsize = sizeof(struct elfhdr);
1038 elf->e_phentsize = sizeof(struct elf_phdr);
1039 elf->e_phnum = segs;
1040 elf->e_shentsize = 0;
1041 elf->e_shnum = 0;
1042 elf->e_shstrndx = 0;
1043 return;
1046 static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
1048 phdr->p_type = PT_NOTE;
1049 phdr->p_offset = offset;
1050 phdr->p_vaddr = 0;
1051 phdr->p_paddr = 0;
1052 phdr->p_filesz = sz;
1053 phdr->p_memsz = 0;
1054 phdr->p_flags = 0;
1055 phdr->p_align = 0;
1056 return;
1059 static void fill_note(struct memelfnote *note, const char *name, int type,
1060 unsigned int sz, void *data)
1062 note->name = name;
1063 note->type = type;
1064 note->datasz = sz;
1065 note->data = data;
1066 return;
1070 * fill up all the fields in prstatus from the given task struct, except registers
1071 * which need to be filled up separately.
1073 static void fill_prstatus(struct elf_prstatus *prstatus,
1074 struct task_struct *p, long signr)
1076 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1077 prstatus->pr_sigpend = p->pending.signal.sig[0];
1078 prstatus->pr_sighold = p->blocked.sig[0];
1079 prstatus->pr_pid = p->pid;
1080 prstatus->pr_ppid = p->parent->pid;
1081 prstatus->pr_pgrp = p->pgrp;
1082 prstatus->pr_sid = p->session;
1083 jiffies_to_timeval(p->utime, &prstatus->pr_utime);
1084 jiffies_to_timeval(p->stime, &prstatus->pr_stime);
1085 jiffies_to_timeval(p->cutime, &prstatus->pr_cutime);
1086 jiffies_to_timeval(p->cstime, &prstatus->pr_cstime);
1089 static void fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p)
1091 int i, len;
1093 /* first copy the parameters from user space */
1094 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1096 len = p->mm->arg_end - p->mm->arg_start;
1097 if (len >= ELF_PRARGSZ)
1098 len = ELF_PRARGSZ-1;
1099 copy_from_user(&psinfo->pr_psargs,
1100 (const char *)p->mm->arg_start, len);
1101 for(i = 0; i < len; i++)
1102 if (psinfo->pr_psargs[i] == 0)
1103 psinfo->pr_psargs[i] = ' ';
1104 psinfo->pr_psargs[len] = 0;
1106 psinfo->pr_pid = p->pid;
1107 psinfo->pr_ppid = p->parent->pid;
1108 psinfo->pr_pgrp = p->pgrp;
1109 psinfo->pr_sid = p->session;
1111 i = p->state ? ffz(~p->state) + 1 : 0;
1112 psinfo->pr_state = i;
1113 psinfo->pr_sname = (i < 0 || i > 5) ? '.' : "RSDTZW"[i];
1114 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1115 psinfo->pr_nice = task_nice(p);
1116 psinfo->pr_flag = p->flags;
1117 psinfo->pr_uid = NEW_TO_OLD_UID(p->uid);
1118 psinfo->pr_gid = NEW_TO_OLD_GID(p->gid);
1119 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1121 return;
1124 /* Here is the structure in which status of each thread is captured. */
1125 struct elf_thread_status
1127 struct list_head list;
1128 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1129 elf_fpregset_t fpu; /* NT_PRFPREG */
1130 #ifdef ELF_CORE_COPY_XFPREGS
1131 elf_fpxregset_t xfpu; /* NT_PRXFPREG */
1132 #endif
1133 struct memelfnote notes[3];
1134 int num_notes;
1138 * In order to add the specific thread information for the elf file format,
1139 * we need to keep a linked list of every threads pr_status and then
1140 * create a single section for them in the final core file.
1142 static int elf_dump_thread_status(long signr, struct task_struct * p, struct list_head * thread_list)
1145 struct elf_thread_status *t;
1146 int sz = 0;
1148 t = kmalloc(sizeof(*t), GFP_ATOMIC);
1149 if (!t)
1150 return 0;
1151 memset(t, 0, sizeof(*t));
1153 INIT_LIST_HEAD(&t->list);
1154 t->num_notes = 0;
1156 fill_prstatus(&t->prstatus, p, signr);
1157 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1159 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus), &(t->prstatus));
1160 t->num_notes++;
1161 sz += notesize(&t->notes[0]);
1163 if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, &t->fpu))) {
1164 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu), &(t->fpu));
1165 t->num_notes++;
1166 sz += notesize(&t->notes[1]);
1169 #ifdef ELF_CORE_COPY_XFPREGS
1170 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
1171 fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu), &t->xfpu);
1172 t->num_notes++;
1173 sz += notesize(&t->notes[2]);
1175 #endif
1176 list_add(&t->list, thread_list);
1177 return sz;
1181 * Actual dumper
1183 * This is a two-pass process; first we find the offsets of the bits,
1184 * and then they are actually written out. If we run out of core limit
1185 * we just truncate.
1187 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
1189 #define NUM_NOTES 5
1190 int has_dumped = 0;
1191 mm_segment_t fs;
1192 int segs;
1193 size_t size = 0;
1194 int i;
1195 struct vm_area_struct *vma;
1196 struct elfhdr *elf = NULL;
1197 off_t offset = 0, dataoff;
1198 unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1199 int numnote = NUM_NOTES;
1200 struct memelfnote *notes = NULL;
1201 struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
1202 struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
1203 struct task_struct *g, *p;
1204 LIST_HEAD(thread_list);
1205 struct list_head *t;
1206 elf_fpregset_t *fpu = NULL;
1207 #ifdef ELF_CORE_COPY_XFPREGS
1208 elf_fpxregset_t *xfpu = NULL;
1209 #endif
1210 int thread_status_size = 0;
1213 * We no longer stop all VM operations.
1215 * This is because those proceses that could possibly change map_count or
1216 * the mmap / vma pages are now blocked in do_exit on current finishing
1217 * this core dump.
1219 * Only ptrace can touch these memory addresses, but it doesn't change
1220 * the map_count or the pages allocated. So no possibility of crashing
1221 * exists while dumping the mm->vm_next areas to the core file.
1224 /* alloc memory for large data structures: too large to be on stack */
1225 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1226 if (!elf)
1227 goto cleanup;
1228 prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
1229 if (!prstatus)
1230 goto cleanup;
1231 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1232 if (!psinfo)
1233 goto cleanup;
1234 notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1235 if (!notes)
1236 goto cleanup;
1237 fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1238 if (!fpu)
1239 goto cleanup;
1240 #ifdef ELF_CORE_COPY_XFPREGS
1241 xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1242 if (!xfpu)
1243 goto cleanup;
1244 #endif
1246 /* capture the status of all other threads */
1247 if (signr) {
1248 read_lock(&tasklist_lock);
1249 do_each_thread(g,p)
1250 if (current->mm == p->mm && current != p) {
1251 int sz = elf_dump_thread_status(signr, p, &thread_list);
1252 if (!sz) {
1253 read_unlock(&tasklist_lock);
1254 goto cleanup;
1255 } else
1256 thread_status_size += sz;
1258 while_each_thread(g,p);
1259 read_unlock(&tasklist_lock);
1262 /* now collect the dump for the current */
1263 memset(prstatus, 0, sizeof(*prstatus));
1264 fill_prstatus(prstatus, current, signr);
1265 elf_core_copy_regs(&prstatus->pr_reg, regs);
1267 segs = current->mm->map_count;
1268 #ifdef ELF_CORE_EXTRA_PHDRS
1269 segs += ELF_CORE_EXTRA_PHDRS;
1270 #endif
1272 /* Set up header */
1273 fill_elf_header(elf, segs+1); /* including notes section */
1275 has_dumped = 1;
1276 current->flags |= PF_DUMPCORE;
1279 * Set up the notes in similar form to SVR4 core dumps made
1280 * with info from their /proc.
1283 fill_note(notes +0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
1285 fill_psinfo(psinfo, current->group_leader);
1286 fill_note(notes +1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1288 fill_note(notes +2, "CORE", NT_TASKSTRUCT, sizeof(*current), current);
1290 /* Try to dump the FPU. */
1291 if ((prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, fpu)))
1292 fill_note(notes +3, "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1293 else
1294 --numnote;
1295 #ifdef ELF_CORE_COPY_XFPREGS
1296 if (elf_core_copy_task_xfpregs(current, xfpu))
1297 fill_note(notes +4, "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
1298 else
1299 --numnote;
1300 #else
1301 numnote--;
1302 #endif
1304 fs = get_fs();
1305 set_fs(KERNEL_DS);
1307 DUMP_WRITE(elf, sizeof(*elf));
1308 offset += sizeof(*elf); /* Elf header */
1309 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1311 /* Write notes phdr entry */
1313 struct elf_phdr phdr;
1314 int sz = 0;
1316 for (i = 0; i < numnote; i++)
1317 sz += notesize(notes + i);
1319 sz += thread_status_size;
1321 fill_elf_note_phdr(&phdr, sz, offset);
1322 offset += sz;
1323 DUMP_WRITE(&phdr, sizeof(phdr));
1326 /* Page-align dumped data */
1327 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1329 /* Write program headers for segments dump */
1330 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1331 struct elf_phdr phdr;
1332 size_t sz;
1334 sz = vma->vm_end - vma->vm_start;
1336 phdr.p_type = PT_LOAD;
1337 phdr.p_offset = offset;
1338 phdr.p_vaddr = vma->vm_start;
1339 phdr.p_paddr = 0;
1340 phdr.p_filesz = maydump(vma) ? sz : 0;
1341 phdr.p_memsz = sz;
1342 offset += phdr.p_filesz;
1343 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1344 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1345 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1346 phdr.p_align = ELF_EXEC_PAGESIZE;
1348 DUMP_WRITE(&phdr, sizeof(phdr));
1351 #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1352 ELF_CORE_WRITE_EXTRA_PHDRS;
1353 #endif
1355 /* write out the notes section */
1356 for (i = 0; i < numnote; i++)
1357 if (!writenote(notes + i, file))
1358 goto end_coredump;
1360 /* write out the thread status notes section */
1361 list_for_each(t, &thread_list) {
1362 struct elf_thread_status *tmp = list_entry(t, struct elf_thread_status, list);
1363 for (i = 0; i < tmp->num_notes; i++)
1364 if (!writenote(&tmp->notes[i], file))
1365 goto end_coredump;
1368 DUMP_SEEK(dataoff);
1370 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1371 unsigned long addr;
1373 if (!maydump(vma))
1374 continue;
1376 for (addr = vma->vm_start;
1377 addr < vma->vm_end;
1378 addr += PAGE_SIZE) {
1379 struct page* page;
1380 struct vm_area_struct *vma;
1382 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1383 &page, &vma) <= 0) {
1384 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1385 } else {
1386 if (page == ZERO_PAGE(addr)) {
1387 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1388 } else {
1389 void *kaddr;
1390 flush_cache_page(vma, addr);
1391 kaddr = kmap(page);
1392 DUMP_WRITE(kaddr, PAGE_SIZE);
1393 kunmap(page);
1395 page_cache_release(page);
1400 #ifdef ELF_CORE_WRITE_EXTRA_DATA
1401 ELF_CORE_WRITE_EXTRA_DATA;
1402 #endif
1404 if ((off_t) file->f_pos != offset) {
1405 /* Sanity check */
1406 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1407 (off_t) file->f_pos, offset);
1410 end_coredump:
1411 set_fs(fs);
1413 cleanup:
1414 while(!list_empty(&thread_list)) {
1415 struct list_head *tmp = thread_list.next;
1416 list_del(tmp);
1417 kfree(list_entry(tmp, struct elf_thread_status, list));
1420 kfree(elf);
1421 kfree(prstatus);
1422 kfree(psinfo);
1423 kfree(notes);
1424 kfree(fpu);
1425 #ifdef ELF_CORE_COPY_XFPREGS
1426 kfree(xfpu);
1427 #endif
1428 return has_dumped;
1429 #undef NUM_NOTES
1432 #endif /* USE_ELF_CORE_DUMP */
1434 static int __init init_elf_binfmt(void)
1436 return register_binfmt(&elf_format);
1439 static void __exit exit_elf_binfmt(void)
1441 /* Remove the COFF and ELF loaders. */
1442 unregister_binfmt(&elf_format);
1445 module_init(init_elf_binfmt)
1446 module_exit(exit_elf_binfmt)
1447 MODULE_LICENSE("GPL");