[PATCH] DVB: Update documentation and credits
[linux-2.6/history.git] / fs / binfmt_elf.c
blobf24e0d0e347869c86a3d42af3ca707c0f4cd4ee3
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 int set_brk(unsigned long start, unsigned long end)
87 start = ELF_PAGEALIGN(start);
88 end = ELF_PAGEALIGN(end);
89 if (end > start) {
90 unsigned long addr = do_brk(start, end - start);
91 if (BAD_ADDR(addr))
92 return addr;
94 current->mm->start_brk = current->mm->brk = end;
95 return 0;
99 /* We need to explicitly zero any fractional pages
100 after the data section (i.e. bss). This would
101 contain the junk from the file that should not
102 be in memory */
105 static void padzero(unsigned long elf_bss)
107 unsigned long nbyte;
109 nbyte = ELF_PAGEOFFSET(elf_bss);
110 if (nbyte) {
111 nbyte = ELF_MIN_ALIGN - nbyte;
112 clear_user((void *) elf_bss, nbyte);
116 /* Let's use some macros to make this stack manipulation a litle clearer */
117 #ifdef CONFIG_STACK_GROWSUP
118 #define STACK_ADD(sp, items) ((elf_addr_t *)(sp) + (items))
119 #define STACK_ROUND(sp, items) \
120 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
121 #define STACK_ALLOC(sp, len) ({ elf_addr_t *old_sp = (elf_addr_t *)sp; sp += len; old_sp; })
122 #else
123 #define STACK_ADD(sp, items) ((elf_addr_t *)(sp) - (items))
124 #define STACK_ROUND(sp, items) \
125 (((unsigned long) (sp - items)) &~ 15UL)
126 #define STACK_ALLOC(sp, len) sp -= len
127 #endif
129 static void
130 create_elf_tables(struct linux_binprm *bprm, struct elfhdr * exec,
131 int interp_aout, unsigned long load_addr,
132 unsigned long interp_load_addr)
134 unsigned long p = bprm->p;
135 int argc = bprm->argc;
136 int envc = bprm->envc;
137 elf_addr_t *argv, *envp;
138 elf_addr_t *sp, *u_platform;
139 const char *k_platform = ELF_PLATFORM;
140 int items;
141 elf_addr_t *elf_info;
142 int ei_index = 0;
143 struct task_struct *tsk = current;
146 * If this architecture has a platform capability string, copy it
147 * to userspace. In some cases (Sparc), this info is impossible
148 * for userspace to get any other way, in others (i386) it is
149 * merely difficult.
152 u_platform = NULL;
153 if (k_platform) {
154 size_t len = strlen(k_platform) + 1;
156 #ifdef CONFIG_X86_HT
158 * In some cases (e.g. Hyper-Threading), we want to avoid L1
159 * evictions by the processes running on the same package. One
160 * thing we can do is to shuffle the initial stack for them.
162 * The conditionals here are unneeded, but kept in to make the
163 * code behaviour the same as pre change unless we have
164 * hyperthreaded processors. This should be cleaned up
165 * before 2.6
168 if (smp_num_siblings > 1)
169 STACK_ALLOC(p, ((current->pid % 64) << 7));
170 #endif
171 u_platform = (elf_addr_t *) STACK_ALLOC(p, len);
172 __copy_to_user(u_platform, k_platform, len);
175 /* Create the ELF interpreter info */
176 elf_info = (elf_addr_t *) current->mm->saved_auxv;
177 #define NEW_AUX_ENT(id, val) \
178 do { elf_info[ei_index++] = id; elf_info[ei_index++] = val; } while (0)
180 #ifdef ARCH_DLINFO
182 * ARCH_DLINFO must come first so PPC can do its special alignment of
183 * AUXV.
185 ARCH_DLINFO;
186 #endif
187 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
188 NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
189 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
190 NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
191 NEW_AUX_ENT(AT_PHENT, sizeof (struct elf_phdr));
192 NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
193 NEW_AUX_ENT(AT_BASE, interp_load_addr);
194 NEW_AUX_ENT(AT_FLAGS, 0);
195 NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
196 NEW_AUX_ENT(AT_UID, (elf_addr_t) tsk->uid);
197 NEW_AUX_ENT(AT_EUID, (elf_addr_t) tsk->euid);
198 NEW_AUX_ENT(AT_GID, (elf_addr_t) tsk->gid);
199 NEW_AUX_ENT(AT_EGID, (elf_addr_t) tsk->egid);
200 NEW_AUX_ENT(AT_SECURE, (elf_addr_t) security_bprm_secureexec(bprm));
201 if (k_platform) {
202 NEW_AUX_ENT(AT_PLATFORM, (elf_addr_t)(long)u_platform);
204 #undef NEW_AUX_ENT
205 /* AT_NULL is zero; clear the rest too */
206 memset(&elf_info[ei_index], 0,
207 sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
209 /* And advance past the AT_NULL entry. */
210 ei_index += 2;
212 sp = STACK_ADD(p, ei_index);
214 items = (argc + 1) + (envc + 1);
215 if (interp_aout) {
216 items += 3; /* a.out interpreters require argv & envp too */
217 } else {
218 items += 1; /* ELF interpreters only put argc on the stack */
220 bprm->p = STACK_ROUND(sp, items);
222 /* Point sp at the lowest address on the stack */
223 #ifdef CONFIG_STACK_GROWSUP
224 sp = (elf_addr_t *)bprm->p - items - ei_index;
225 bprm->exec = (unsigned long) sp; /* XXX: PARISC HACK */
226 #else
227 sp = (elf_addr_t *)bprm->p;
228 #endif
230 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
231 __put_user(argc, sp++);
232 if (interp_aout) {
233 argv = sp + 2;
234 envp = argv + argc + 1;
235 __put_user((elf_addr_t)(long)argv, sp++);
236 __put_user((elf_addr_t)(long)envp, sp++);
237 } else {
238 argv = sp;
239 envp = argv + argc + 1;
242 /* Populate argv and envp */
243 p = current->mm->arg_start;
244 while (argc-- > 0) {
245 size_t len;
246 __put_user((elf_addr_t)p, argv++);
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, argv);
253 current->mm->arg_end = current->mm->env_start = p;
254 while (envc-- > 0) {
255 size_t len;
256 __put_user((elf_addr_t)p, envp++);
257 len = strnlen_user((void *)p, PAGE_SIZE*MAX_ARG_PAGES);
258 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
259 return;
260 p += len;
262 __put_user(0, envp);
263 current->mm->env_end = p;
265 /* Put the elf_info on the stack in the right place. */
266 sp = (elf_addr_t *)envp + 1;
267 copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t));
270 #ifndef elf_map
272 static unsigned long elf_map(struct file *filep, unsigned long addr,
273 struct elf_phdr *eppnt, int prot, int type)
275 unsigned long map_addr;
277 down_write(&current->mm->mmap_sem);
278 map_addr = do_mmap(filep, ELF_PAGESTART(addr),
279 eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr), prot, type,
280 eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
281 up_write(&current->mm->mmap_sem);
282 return(map_addr);
285 #endif /* !elf_map */
287 /* This is much more generalized than the library routine read function,
288 so we keep this separate. Technically the library read function
289 is only provided so that we can read a.out libraries that have
290 an ELF header */
292 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
293 struct file * interpreter,
294 unsigned long *interp_load_addr)
296 struct elf_phdr *elf_phdata;
297 struct elf_phdr *eppnt;
298 unsigned long load_addr = 0;
299 int load_addr_set = 0;
300 unsigned long last_bss = 0, elf_bss = 0;
301 unsigned long error = ~0UL;
302 int retval, i, size;
304 /* First of all, some simple consistency checks */
305 if (interp_elf_ex->e_type != ET_EXEC &&
306 interp_elf_ex->e_type != ET_DYN)
307 goto out;
308 if (!elf_check_arch(interp_elf_ex))
309 goto out;
310 if (!interpreter->f_op || !interpreter->f_op->mmap)
311 goto out;
314 * If the size of this structure has changed, then punt, since
315 * we will be doing the wrong thing.
317 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
318 goto out;
319 if (interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
320 goto out;
322 /* Now read in all of the header information */
324 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
325 if (size > ELF_MIN_ALIGN)
326 goto out;
327 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
328 if (!elf_phdata)
329 goto out;
331 retval = kernel_read(interpreter,interp_elf_ex->e_phoff,(char *)elf_phdata,size);
332 error = retval;
333 if (retval < 0)
334 goto out_close;
336 eppnt = elf_phdata;
337 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
338 if (eppnt->p_type == PT_LOAD) {
339 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
340 int elf_prot = 0;
341 unsigned long vaddr = 0;
342 unsigned long k, map_addr;
344 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
345 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
346 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
347 vaddr = eppnt->p_vaddr;
348 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
349 elf_type |= MAP_FIXED;
351 map_addr = elf_map(interpreter, load_addr + vaddr, eppnt, elf_prot, elf_type);
352 if (BAD_ADDR(map_addr))
353 goto out_close;
355 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
356 load_addr = map_addr - ELF_PAGESTART(vaddr);
357 load_addr_set = 1;
361 * Find the end of the file mapping for this phdr, and keep
362 * track of the largest address we see for this.
364 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
365 if (k > elf_bss)
366 elf_bss = k;
369 * Do the same thing for the memory mapping - between
370 * elf_bss and last_bss is the bss section.
372 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
373 if (k > last_bss)
374 last_bss = k;
379 * Now fill out the bss section. First pad the last page up
380 * to the page boundary, and then perform a mmap to make sure
381 * that there are zero-mapped pages up to and including the
382 * last bss page.
384 padzero(elf_bss);
385 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1); /* What we have mapped so far */
387 /* Map the last of the bss segment */
388 if (last_bss > elf_bss) {
389 error = do_brk(elf_bss, last_bss - elf_bss);
390 if (BAD_ADDR(error))
391 goto out_close;
394 *interp_load_addr = load_addr;
395 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
397 out_close:
398 kfree(elf_phdata);
399 out:
400 return error;
403 static unsigned long load_aout_interp(struct exec * interp_ex,
404 struct file * interpreter)
406 unsigned long text_data, elf_entry = ~0UL;
407 char * addr;
408 loff_t offset;
410 current->mm->end_code = interp_ex->a_text;
411 text_data = interp_ex->a_text + interp_ex->a_data;
412 current->mm->end_data = text_data;
413 current->mm->brk = interp_ex->a_bss + text_data;
415 switch (N_MAGIC(*interp_ex)) {
416 case OMAGIC:
417 offset = 32;
418 addr = (char *) 0;
419 break;
420 case ZMAGIC:
421 case QMAGIC:
422 offset = N_TXTOFF(*interp_ex);
423 addr = (char *) N_TXTADDR(*interp_ex);
424 break;
425 default:
426 goto out;
429 do_brk(0, text_data);
430 if (!interpreter->f_op || !interpreter->f_op->read)
431 goto out;
432 if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
433 goto out;
434 flush_icache_range((unsigned long)addr,
435 (unsigned long)addr + text_data);
437 do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
438 interp_ex->a_bss);
439 elf_entry = interp_ex->a_entry;
441 out:
442 return elf_entry;
446 * These are the functions used to load ELF style executables and shared
447 * libraries. There is no binary dependent code anywhere else.
450 #define INTERPRETER_NONE 0
451 #define INTERPRETER_AOUT 1
452 #define INTERPRETER_ELF 2
455 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
457 struct file *interpreter = NULL; /* to shut gcc up */
458 unsigned long load_addr = 0, load_bias = 0;
459 int load_addr_set = 0;
460 char * elf_interpreter = NULL;
461 unsigned int interpreter_type = INTERPRETER_NONE;
462 unsigned char ibcs2_interpreter = 0;
463 unsigned long error;
464 struct elf_phdr * elf_ppnt, *elf_phdata;
465 unsigned long elf_bss, elf_brk;
466 int elf_exec_fileno;
467 int retval, i;
468 unsigned int size;
469 unsigned long elf_entry, interp_load_addr = 0;
470 unsigned long start_code, end_code, start_data, end_data;
471 unsigned long reloc_func_desc = 0;
472 struct elfhdr elf_ex;
473 struct elfhdr interp_elf_ex;
474 struct exec interp_ex;
475 char passed_fileno[6];
476 struct files_struct *files;
478 /* Get the exec-header */
479 elf_ex = *((struct elfhdr *) bprm->buf);
481 retval = -ENOEXEC;
482 /* First of all, some simple consistency checks */
483 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
484 goto out;
486 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
487 goto out;
488 if (!elf_check_arch(&elf_ex))
489 goto out;
490 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
491 goto out;
493 /* Now read in all of the header information */
495 retval = -ENOMEM;
496 if (elf_ex.e_phentsize != sizeof(struct elf_phdr))
497 goto out;
498 if (elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
499 goto out;
500 size = elf_ex.e_phnum * sizeof(struct elf_phdr);
501 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
502 if (!elf_phdata)
503 goto out;
505 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *) elf_phdata, size);
506 if (retval < 0)
507 goto out_free_ph;
509 files = current->files; /* Refcounted so ok */
510 if(unshare_files() < 0)
511 goto out_free_ph;
512 if (files == current->files) {
513 put_files_struct(files);
514 files = NULL;
517 /* exec will make our files private anyway, but for the a.out
518 loader stuff we need to do it earlier */
520 retval = get_unused_fd();
521 if (retval < 0)
522 goto out_free_fh;
523 get_file(bprm->file);
524 fd_install(elf_exec_fileno = retval, bprm->file);
526 elf_ppnt = elf_phdata;
527 elf_bss = 0;
528 elf_brk = 0;
530 start_code = ~0UL;
531 end_code = 0;
532 start_data = 0;
533 end_data = 0;
535 for (i = 0; i < elf_ex.e_phnum; i++) {
536 if (elf_ppnt->p_type == PT_INTERP) {
537 /* This is the program interpreter used for
538 * shared libraries - for now assume that this
539 * is an a.out format binary
542 retval = -ENOMEM;
543 if (elf_ppnt->p_filesz > PATH_MAX)
544 goto out_free_file;
545 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
546 GFP_KERNEL);
547 if (!elf_interpreter)
548 goto out_free_file;
550 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
551 elf_interpreter,
552 elf_ppnt->p_filesz);
553 if (retval < 0)
554 goto out_free_interp;
555 /* If the program interpreter is one of these two,
556 * then assume an iBCS2 image. Otherwise assume
557 * a native linux image.
559 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
560 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
561 ibcs2_interpreter = 1;
564 * The early SET_PERSONALITY here is so that the lookup
565 * for the interpreter happens in the namespace of the
566 * to-be-execed image. SET_PERSONALITY can select an
567 * alternate root.
569 * However, SET_PERSONALITY is NOT allowed to switch
570 * this task into the new images's memory mapping
571 * policy - that is, TASK_SIZE must still evaluate to
572 * that which is appropriate to the execing application.
573 * This is because exit_mmap() needs to have TASK_SIZE
574 * evaluate to the size of the old image.
576 * So if (say) a 64-bit application is execing a 32-bit
577 * application it is the architecture's responsibility
578 * to defer changing the value of TASK_SIZE until the
579 * switch really is going to happen - do this in
580 * flush_thread(). - akpm
582 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
584 interpreter = open_exec(elf_interpreter);
585 retval = PTR_ERR(interpreter);
586 if (IS_ERR(interpreter))
587 goto out_free_interp;
588 retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
589 if (retval < 0)
590 goto out_free_dentry;
592 /* Get the exec headers */
593 interp_ex = *((struct exec *) bprm->buf);
594 interp_elf_ex = *((struct elfhdr *) bprm->buf);
595 break;
597 elf_ppnt++;
600 /* Some simple consistency checks for the interpreter */
601 if (elf_interpreter) {
602 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
604 /* Now figure out which format our binary is */
605 if ((N_MAGIC(interp_ex) != OMAGIC) &&
606 (N_MAGIC(interp_ex) != ZMAGIC) &&
607 (N_MAGIC(interp_ex) != QMAGIC))
608 interpreter_type = INTERPRETER_ELF;
610 if (memcmp(interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
611 interpreter_type &= ~INTERPRETER_ELF;
613 retval = -ELIBBAD;
614 if (!interpreter_type)
615 goto out_free_dentry;
617 /* Make sure only one type was selected */
618 if ((interpreter_type & INTERPRETER_ELF) &&
619 interpreter_type != INTERPRETER_ELF) {
620 // FIXME - ratelimit this before re-enabling
621 // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
622 interpreter_type = INTERPRETER_ELF;
624 /* Verify the interpreter has a valid arch */
625 if ((interpreter_type == INTERPRETER_ELF) &&
626 !elf_check_arch(&interp_elf_ex))
627 goto out_free_dentry;
628 } else {
629 /* Executables without an interpreter also need a personality */
630 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
633 /* OK, we are done with that, now set up the arg stuff,
634 and then start this sucker up */
636 if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
637 char *passed_p = passed_fileno;
638 sprintf(passed_fileno, "%d", elf_exec_fileno);
640 if (elf_interpreter) {
641 retval = copy_strings_kernel(1, &passed_p, bprm);
642 if (retval)
643 goto out_free_dentry;
644 bprm->argc++;
648 /* Flush all traces of the currently running executable */
649 retval = flush_old_exec(bprm);
650 if (retval)
651 goto out_free_dentry;
653 /* Discard our unneeded old files struct */
654 if (files) {
655 steal_locks(files);
656 put_files_struct(files);
657 files = NULL;
660 /* OK, This is the point of no return */
661 current->mm->start_data = 0;
662 current->mm->end_data = 0;
663 current->mm->end_code = 0;
664 current->mm->mmap = NULL;
665 current->flags &= ~PF_FORKNOEXEC;
667 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
668 may depend on the personality. */
669 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
671 /* Do this so that we can load the interpreter, if need be. We will
672 change some of these later */
673 current->mm->rss = 0;
674 current->mm->free_area_cache = TASK_UNMAPPED_BASE;
675 retval = setup_arg_pages(bprm);
676 if (retval < 0) {
677 send_sig(SIGKILL, current, 0);
678 goto out_free_dentry;
681 current->mm->start_stack = bprm->p;
683 /* Now we do a little grungy work by mmaping the ELF image into
684 the correct location in memory. At this point, we assume that
685 the image should be loaded at fixed address, not at a variable
686 address. */
688 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
689 int elf_prot = 0, elf_flags;
690 unsigned long k, vaddr;
692 if (elf_ppnt->p_type != PT_LOAD)
693 continue;
695 if (unlikely (elf_brk > elf_bss)) {
696 unsigned long nbyte;
698 /* There was a PT_LOAD segment with p_memsz > p_filesz
699 before this one. Map anonymous pages, if needed,
700 and clear the area. */
701 retval = set_brk (elf_bss + load_bias,
702 elf_brk + load_bias);
703 if (retval) {
704 send_sig(SIGKILL, current, 0);
705 goto out_free_dentry;
707 nbyte = ELF_PAGEOFFSET(elf_bss);
708 if (nbyte) {
709 nbyte = ELF_MIN_ALIGN - nbyte;
710 if (nbyte > elf_brk - elf_bss)
711 nbyte = elf_brk - elf_bss;
712 clear_user((void *) elf_bss + load_bias, nbyte);
716 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
717 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
718 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
720 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
722 vaddr = elf_ppnt->p_vaddr;
723 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
724 elf_flags |= MAP_FIXED;
725 } else if (elf_ex.e_type == ET_DYN) {
726 /* Try and get dynamic programs out of the way of the default mmap
727 base, as well as whatever program they might try to exec. This
728 is because the brk will follow the loader, and is not movable. */
729 load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
732 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags);
733 if (BAD_ADDR(error))
734 continue;
736 if (!load_addr_set) {
737 load_addr_set = 1;
738 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
739 if (elf_ex.e_type == ET_DYN) {
740 load_bias += error -
741 ELF_PAGESTART(load_bias + vaddr);
742 load_addr += load_bias;
743 reloc_func_desc = load_bias;
746 k = elf_ppnt->p_vaddr;
747 if (k < start_code) start_code = k;
748 if (start_data < k) start_data = k;
750 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
752 if (k > elf_bss)
753 elf_bss = k;
754 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
755 end_code = k;
756 if (end_data < k)
757 end_data = k;
758 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
759 if (k > elf_brk)
760 elf_brk = k;
763 elf_ex.e_entry += load_bias;
764 elf_bss += load_bias;
765 elf_brk += load_bias;
766 start_code += load_bias;
767 end_code += load_bias;
768 start_data += load_bias;
769 end_data += load_bias;
771 /* Calling set_brk effectively mmaps the pages that we need
772 * for the bss and break sections. We must do this before
773 * mapping in the interpreter, to make sure it doesn't wind
774 * up getting placed where the bss needs to go.
776 retval = set_brk(elf_bss, elf_brk);
777 if (retval) {
778 send_sig(SIGKILL, current, 0);
779 goto out_free_dentry;
781 padzero(elf_bss);
783 if (elf_interpreter) {
784 if (interpreter_type == INTERPRETER_AOUT)
785 elf_entry = load_aout_interp(&interp_ex,
786 interpreter);
787 else
788 elf_entry = load_elf_interp(&interp_elf_ex,
789 interpreter,
790 &interp_load_addr);
791 if (BAD_ADDR(elf_entry)) {
792 printk(KERN_ERR "Unable to load interpreter\n");
793 send_sig(SIGSEGV, current, 0);
794 retval = -ENOEXEC; /* Nobody gets to see this, but.. */
795 goto out_free_dentry;
797 reloc_func_desc = interp_load_addr;
799 allow_write_access(interpreter);
800 fput(interpreter);
801 kfree(elf_interpreter);
802 } else {
803 elf_entry = elf_ex.e_entry;
806 kfree(elf_phdata);
808 if (interpreter_type != INTERPRETER_AOUT)
809 sys_close(elf_exec_fileno);
811 set_binfmt(&elf_format);
813 compute_creds(bprm);
814 current->flags &= ~PF_FORKNOEXEC;
815 create_elf_tables(bprm, &elf_ex, (interpreter_type == INTERPRETER_AOUT),
816 load_addr, interp_load_addr);
817 /* N.B. passed_fileno might not be initialized? */
818 if (interpreter_type == INTERPRETER_AOUT)
819 current->mm->arg_start += strlen(passed_fileno) + 1;
820 current->mm->end_code = end_code;
821 current->mm->start_code = start_code;
822 current->mm->start_data = start_data;
823 current->mm->end_data = end_data;
824 current->mm->start_stack = bprm->p;
826 if (current->personality & MMAP_PAGE_ZERO) {
827 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
828 and some applications "depend" upon this behavior.
829 Since we do not have the power to recompile these, we
830 emulate the SVr4 behavior. Sigh. */
831 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
832 down_write(&current->mm->mmap_sem);
833 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
834 MAP_FIXED | MAP_PRIVATE, 0);
835 up_write(&current->mm->mmap_sem);
838 #ifdef ELF_PLAT_INIT
840 * The ABI may specify that certain registers be set up in special
841 * ways (on i386 %edx is the address of a DT_FINI function, for
842 * example. In addition, it may also specify (eg, PowerPC64 ELF)
843 * that the e_entry field is the address of the function descriptor
844 * for the startup routine, rather than the address of the startup
845 * routine itself. This macro performs whatever initialization to
846 * the regs structure is required as well as any relocations to the
847 * function descriptor entries when executing dynamically links apps.
849 ELF_PLAT_INIT(regs, reloc_func_desc);
850 #endif
852 start_thread(regs, elf_entry, bprm->p);
853 if (unlikely(current->ptrace & PT_PTRACED)) {
854 if (current->ptrace & PT_TRACE_EXEC)
855 ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
856 else
857 send_sig(SIGTRAP, current, 0);
859 retval = 0;
860 out:
861 return retval;
863 /* error cleanup */
864 out_free_dentry:
865 allow_write_access(interpreter);
866 fput(interpreter);
867 out_free_interp:
868 if (elf_interpreter)
869 kfree(elf_interpreter);
870 out_free_file:
871 sys_close(elf_exec_fileno);
872 out_free_fh:
873 if (files) {
874 put_files_struct(current->files);
875 current->files = files;
877 out_free_ph:
878 kfree(elf_phdata);
879 goto out;
882 /* This is really simpleminded and specialized - we are loading an
883 a.out library that is given an ELF header. */
885 static int load_elf_library(struct file *file)
887 struct elf_phdr *elf_phdata;
888 unsigned long elf_bss, bss, len;
889 int retval, error, i, j;
890 struct elfhdr elf_ex;
892 error = -ENOEXEC;
893 retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
894 if (retval != sizeof(elf_ex))
895 goto out;
897 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
898 goto out;
900 /* First of all, some simple consistency checks */
901 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
902 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
903 goto out;
905 /* Now read in all of the header information */
907 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
908 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
910 error = -ENOMEM;
911 elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
912 if (!elf_phdata)
913 goto out;
915 error = -ENOEXEC;
916 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata, j);
917 if (retval != j)
918 goto out_free_ph;
920 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
921 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
922 if (j != 1)
923 goto out_free_ph;
925 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
927 /* Now use mmap to map the library into memory. */
928 down_write(&current->mm->mmap_sem);
929 error = do_mmap(file,
930 ELF_PAGESTART(elf_phdata->p_vaddr),
931 (elf_phdata->p_filesz +
932 ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
933 PROT_READ | PROT_WRITE | PROT_EXEC,
934 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
935 (elf_phdata->p_offset -
936 ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
937 up_write(&current->mm->mmap_sem);
938 if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
939 goto out_free_ph;
941 elf_bss = elf_phdata->p_vaddr + elf_phdata->p_filesz;
942 padzero(elf_bss);
944 len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr + ELF_MIN_ALIGN - 1);
945 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
946 if (bss > len)
947 do_brk(len, bss - len);
948 error = 0;
950 out_free_ph:
951 kfree(elf_phdata);
952 out:
953 return error;
957 * Note that some platforms still use traditional core dumps and not
958 * the ELF core dump. Each platform can select it as appropriate.
960 #ifdef USE_ELF_CORE_DUMP
963 * ELF core dumper
965 * Modelled on fs/exec.c:aout_core_dump()
966 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
969 * These are the only things you should do on a core-file: use only these
970 * functions to write out all the necessary info.
972 static int dump_write(struct file *file, const void *addr, int nr)
974 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
977 static int dump_seek(struct file *file, off_t off)
979 if (file->f_op->llseek) {
980 if (file->f_op->llseek(file, off, 0) != off)
981 return 0;
982 } else
983 file->f_pos = off;
984 return 1;
988 * Decide whether a segment is worth dumping; default is yes to be
989 * sure (missing info is worse than too much; etc).
990 * Personally I'd include everything, and use the coredump limit...
992 * I think we should skip something. But I am not sure how. H.J.
994 static int maydump(struct vm_area_struct *vma)
997 * If we may not read the contents, don't allow us to dump
998 * them either. "dump_write()" can't handle it anyway.
1000 if (!(vma->vm_flags & VM_READ))
1001 return 0;
1003 /* Do not dump I/O mapped devices! -DaveM */
1004 if (vma->vm_flags & VM_IO)
1005 return 0;
1006 #if 1
1007 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
1008 return 1;
1009 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
1010 return 0;
1011 #endif
1012 return 1;
1015 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
1017 /* An ELF note in memory */
1018 struct memelfnote
1020 const char *name;
1021 int type;
1022 unsigned int datasz;
1023 void *data;
1026 static int notesize(struct memelfnote *en)
1028 int sz;
1030 sz = sizeof(struct elf_note);
1031 sz += roundup(strlen(en->name) + 1, 4);
1032 sz += roundup(en->datasz, 4);
1034 return sz;
1037 #define DUMP_WRITE(addr, nr) \
1038 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1039 #define DUMP_SEEK(off) \
1040 do { if (!dump_seek(file, (off))) return 0; } while(0)
1042 static int writenote(struct memelfnote *men, struct file *file)
1044 struct elf_note en;
1046 en.n_namesz = strlen(men->name) + 1;
1047 en.n_descsz = men->datasz;
1048 en.n_type = men->type;
1050 DUMP_WRITE(&en, sizeof(en));
1051 DUMP_WRITE(men->name, en.n_namesz);
1052 /* XXX - cast from long long to long to avoid need for libgcc.a */
1053 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1054 DUMP_WRITE(men->data, men->datasz);
1055 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1057 return 1;
1059 #undef DUMP_WRITE
1060 #undef DUMP_SEEK
1062 #define DUMP_WRITE(addr, nr) \
1063 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1064 goto end_coredump;
1065 #define DUMP_SEEK(off) \
1066 if (!dump_seek(file, (off))) \
1067 goto end_coredump;
1069 static inline void fill_elf_header(struct elfhdr *elf, int segs)
1071 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1072 elf->e_ident[EI_CLASS] = ELF_CLASS;
1073 elf->e_ident[EI_DATA] = ELF_DATA;
1074 elf->e_ident[EI_VERSION] = EV_CURRENT;
1075 elf->e_ident[EI_OSABI] = ELF_OSABI;
1076 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1078 elf->e_type = ET_CORE;
1079 elf->e_machine = ELF_ARCH;
1080 elf->e_version = EV_CURRENT;
1081 elf->e_entry = 0;
1082 elf->e_phoff = sizeof(struct elfhdr);
1083 elf->e_shoff = 0;
1084 elf->e_flags = 0;
1085 elf->e_ehsize = sizeof(struct elfhdr);
1086 elf->e_phentsize = sizeof(struct elf_phdr);
1087 elf->e_phnum = segs;
1088 elf->e_shentsize = 0;
1089 elf->e_shnum = 0;
1090 elf->e_shstrndx = 0;
1091 return;
1094 static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
1096 phdr->p_type = PT_NOTE;
1097 phdr->p_offset = offset;
1098 phdr->p_vaddr = 0;
1099 phdr->p_paddr = 0;
1100 phdr->p_filesz = sz;
1101 phdr->p_memsz = 0;
1102 phdr->p_flags = 0;
1103 phdr->p_align = 0;
1104 return;
1107 static void fill_note(struct memelfnote *note, const char *name, int type,
1108 unsigned int sz, void *data)
1110 note->name = name;
1111 note->type = type;
1112 note->datasz = sz;
1113 note->data = data;
1114 return;
1118 * fill up all the fields in prstatus from the given task struct, except registers
1119 * which need to be filled up separately.
1121 static void fill_prstatus(struct elf_prstatus *prstatus,
1122 struct task_struct *p, long signr)
1124 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1125 prstatus->pr_sigpend = p->pending.signal.sig[0];
1126 prstatus->pr_sighold = p->blocked.sig[0];
1127 prstatus->pr_pid = p->pid;
1128 prstatus->pr_ppid = p->parent->pid;
1129 prstatus->pr_pgrp = process_group(p);
1130 prstatus->pr_sid = p->session;
1131 jiffies_to_timeval(p->utime, &prstatus->pr_utime);
1132 jiffies_to_timeval(p->stime, &prstatus->pr_stime);
1133 jiffies_to_timeval(p->cutime, &prstatus->pr_cutime);
1134 jiffies_to_timeval(p->cstime, &prstatus->pr_cstime);
1137 static void fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1138 struct mm_struct *mm)
1140 int i, len;
1142 /* first copy the parameters from user space */
1143 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1145 len = mm->arg_end - mm->arg_start;
1146 if (len >= ELF_PRARGSZ)
1147 len = ELF_PRARGSZ-1;
1148 copy_from_user(&psinfo->pr_psargs,
1149 (const char *)mm->arg_start, len);
1150 for(i = 0; i < len; i++)
1151 if (psinfo->pr_psargs[i] == 0)
1152 psinfo->pr_psargs[i] = ' ';
1153 psinfo->pr_psargs[len] = 0;
1155 psinfo->pr_pid = p->pid;
1156 psinfo->pr_ppid = p->parent->pid;
1157 psinfo->pr_pgrp = process_group(p);
1158 psinfo->pr_sid = p->session;
1160 i = p->state ? ffz(~p->state) + 1 : 0;
1161 psinfo->pr_state = i;
1162 psinfo->pr_sname = (i < 0 || i > 5) ? '.' : "RSDTZW"[i];
1163 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1164 psinfo->pr_nice = task_nice(p);
1165 psinfo->pr_flag = p->flags;
1166 SET_UID(psinfo->pr_uid, p->uid);
1167 SET_GID(psinfo->pr_gid, p->gid);
1168 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1170 return;
1173 /* Here is the structure in which status of each thread is captured. */
1174 struct elf_thread_status
1176 struct list_head list;
1177 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1178 elf_fpregset_t fpu; /* NT_PRFPREG */
1179 #ifdef ELF_CORE_COPY_XFPREGS
1180 elf_fpxregset_t xfpu; /* NT_PRXFPREG */
1181 #endif
1182 struct memelfnote notes[3];
1183 int num_notes;
1187 * In order to add the specific thread information for the elf file format,
1188 * we need to keep a linked list of every threads pr_status and then
1189 * create a single section for them in the final core file.
1191 static int elf_dump_thread_status(long signr, struct task_struct * p, struct list_head * thread_list)
1194 struct elf_thread_status *t;
1195 int sz = 0;
1197 t = kmalloc(sizeof(*t), GFP_ATOMIC);
1198 if (!t)
1199 return 0;
1200 memset(t, 0, sizeof(*t));
1202 INIT_LIST_HEAD(&t->list);
1203 t->num_notes = 0;
1205 fill_prstatus(&t->prstatus, p, signr);
1206 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1208 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus), &(t->prstatus));
1209 t->num_notes++;
1210 sz += notesize(&t->notes[0]);
1212 if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, &t->fpu))) {
1213 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu), &(t->fpu));
1214 t->num_notes++;
1215 sz += notesize(&t->notes[1]);
1218 #ifdef ELF_CORE_COPY_XFPREGS
1219 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
1220 fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu), &t->xfpu);
1221 t->num_notes++;
1222 sz += notesize(&t->notes[2]);
1224 #endif
1225 list_add(&t->list, thread_list);
1226 return sz;
1230 * Actual dumper
1232 * This is a two-pass process; first we find the offsets of the bits,
1233 * and then they are actually written out. If we run out of core limit
1234 * we just truncate.
1236 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
1238 #define NUM_NOTES 6
1239 int has_dumped = 0;
1240 mm_segment_t fs;
1241 int segs;
1242 size_t size = 0;
1243 int i;
1244 struct vm_area_struct *vma;
1245 struct elfhdr *elf = NULL;
1246 off_t offset = 0, dataoff;
1247 unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1248 int numnote;
1249 struct memelfnote *notes = NULL;
1250 struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
1251 struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
1252 struct task_struct *g, *p;
1253 LIST_HEAD(thread_list);
1254 struct list_head *t;
1255 elf_fpregset_t *fpu = NULL;
1256 #ifdef ELF_CORE_COPY_XFPREGS
1257 elf_fpxregset_t *xfpu = NULL;
1258 #endif
1259 int thread_status_size = 0;
1260 elf_addr_t *auxv;
1263 * We no longer stop all VM operations.
1265 * This is because those proceses that could possibly change map_count or
1266 * the mmap / vma pages are now blocked in do_exit on current finishing
1267 * this core dump.
1269 * Only ptrace can touch these memory addresses, but it doesn't change
1270 * the map_count or the pages allocated. So no possibility of crashing
1271 * exists while dumping the mm->vm_next areas to the core file.
1274 /* alloc memory for large data structures: too large to be on stack */
1275 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1276 if (!elf)
1277 goto cleanup;
1278 prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
1279 if (!prstatus)
1280 goto cleanup;
1281 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1282 if (!psinfo)
1283 goto cleanup;
1284 notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1285 if (!notes)
1286 goto cleanup;
1287 fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1288 if (!fpu)
1289 goto cleanup;
1290 #ifdef ELF_CORE_COPY_XFPREGS
1291 xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1292 if (!xfpu)
1293 goto cleanup;
1294 #endif
1296 /* capture the status of all other threads */
1297 if (signr) {
1298 read_lock(&tasklist_lock);
1299 do_each_thread(g,p)
1300 if (current->mm == p->mm && current != p) {
1301 int sz = elf_dump_thread_status(signr, p, &thread_list);
1302 if (!sz) {
1303 read_unlock(&tasklist_lock);
1304 goto cleanup;
1305 } else
1306 thread_status_size += sz;
1308 while_each_thread(g,p);
1309 read_unlock(&tasklist_lock);
1312 /* now collect the dump for the current */
1313 memset(prstatus, 0, sizeof(*prstatus));
1314 fill_prstatus(prstatus, current, signr);
1315 elf_core_copy_regs(&prstatus->pr_reg, regs);
1317 segs = current->mm->map_count;
1318 #ifdef ELF_CORE_EXTRA_PHDRS
1319 segs += ELF_CORE_EXTRA_PHDRS;
1320 #endif
1322 /* Set up header */
1323 fill_elf_header(elf, segs+1); /* including notes section */
1325 has_dumped = 1;
1326 current->flags |= PF_DUMPCORE;
1329 * Set up the notes in similar form to SVR4 core dumps made
1330 * with info from their /proc.
1333 fill_note(notes +0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
1335 fill_psinfo(psinfo, current->group_leader, current->mm);
1336 fill_note(notes +1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1338 fill_note(notes +2, "CORE", NT_TASKSTRUCT, sizeof(*current), current);
1340 numnote = 3;
1342 auxv = (elf_addr_t *) current->mm->saved_auxv;
1344 i = 0;
1346 i += 2;
1347 while (auxv[i - 2] != AT_NULL);
1348 fill_note(&notes[numnote++], "CORE", NT_AUXV,
1349 i * sizeof (elf_addr_t), auxv);
1351 /* Try to dump the FPU. */
1352 if ((prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, regs, fpu)))
1353 fill_note(notes + numnote++,
1354 "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1355 #ifdef ELF_CORE_COPY_XFPREGS
1356 if (elf_core_copy_task_xfpregs(current, xfpu))
1357 fill_note(notes + numnote++,
1358 "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
1359 #endif
1361 fs = get_fs();
1362 set_fs(KERNEL_DS);
1364 DUMP_WRITE(elf, sizeof(*elf));
1365 offset += sizeof(*elf); /* Elf header */
1366 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1368 /* Write notes phdr entry */
1370 struct elf_phdr phdr;
1371 int sz = 0;
1373 for (i = 0; i < numnote; i++)
1374 sz += notesize(notes + i);
1376 sz += thread_status_size;
1378 fill_elf_note_phdr(&phdr, sz, offset);
1379 offset += sz;
1380 DUMP_WRITE(&phdr, sizeof(phdr));
1383 /* Page-align dumped data */
1384 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1386 /* Write program headers for segments dump */
1387 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1388 struct elf_phdr phdr;
1389 size_t sz;
1391 sz = vma->vm_end - vma->vm_start;
1393 phdr.p_type = PT_LOAD;
1394 phdr.p_offset = offset;
1395 phdr.p_vaddr = vma->vm_start;
1396 phdr.p_paddr = 0;
1397 phdr.p_filesz = maydump(vma) ? sz : 0;
1398 phdr.p_memsz = sz;
1399 offset += phdr.p_filesz;
1400 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1401 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1402 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1403 phdr.p_align = ELF_EXEC_PAGESIZE;
1405 DUMP_WRITE(&phdr, sizeof(phdr));
1408 #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1409 ELF_CORE_WRITE_EXTRA_PHDRS;
1410 #endif
1412 /* write out the notes section */
1413 for (i = 0; i < numnote; i++)
1414 if (!writenote(notes + i, file))
1415 goto end_coredump;
1417 /* write out the thread status notes section */
1418 list_for_each(t, &thread_list) {
1419 struct elf_thread_status *tmp = list_entry(t, struct elf_thread_status, list);
1420 for (i = 0; i < tmp->num_notes; i++)
1421 if (!writenote(&tmp->notes[i], file))
1422 goto end_coredump;
1425 DUMP_SEEK(dataoff);
1427 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1428 unsigned long addr;
1430 if (!maydump(vma))
1431 continue;
1433 for (addr = vma->vm_start;
1434 addr < vma->vm_end;
1435 addr += PAGE_SIZE) {
1436 struct page* page;
1437 struct vm_area_struct *vma;
1439 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1440 &page, &vma) <= 0) {
1441 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1442 } else {
1443 if (page == ZERO_PAGE(addr)) {
1444 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1445 } else {
1446 void *kaddr;
1447 flush_cache_page(vma, addr);
1448 kaddr = kmap(page);
1449 DUMP_WRITE(kaddr, PAGE_SIZE);
1450 kunmap(page);
1452 page_cache_release(page);
1457 #ifdef ELF_CORE_WRITE_EXTRA_DATA
1458 ELF_CORE_WRITE_EXTRA_DATA;
1459 #endif
1461 if ((off_t) file->f_pos != offset) {
1462 /* Sanity check */
1463 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1464 (off_t) file->f_pos, offset);
1467 end_coredump:
1468 set_fs(fs);
1470 cleanup:
1471 while(!list_empty(&thread_list)) {
1472 struct list_head *tmp = thread_list.next;
1473 list_del(tmp);
1474 kfree(list_entry(tmp, struct elf_thread_status, list));
1477 kfree(elf);
1478 kfree(prstatus);
1479 kfree(psinfo);
1480 kfree(notes);
1481 kfree(fpu);
1482 #ifdef ELF_CORE_COPY_XFPREGS
1483 kfree(xfpu);
1484 #endif
1485 return has_dumped;
1486 #undef NUM_NOTES
1489 #endif /* USE_ELF_CORE_DUMP */
1491 static int __init init_elf_binfmt(void)
1493 return register_binfmt(&elf_format);
1496 static void __exit exit_elf_binfmt(void)
1498 /* Remove the COFF and ELF loaders. */
1499 unregister_binfmt(&elf_format);
1502 module_init(init_elf_binfmt)
1503 module_exit(exit_elf_binfmt)
1504 MODULE_LICENSE("GPL");