Linux-2.4.0-test2
[davej-history.git] / fs / binfmt_elf.c
blob5e49df10bde686f3fc3c80721fa3149d7e17769e
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>
14 #include <linux/fs.h>
15 #include <linux/stat.h>
16 #include <linux/sched.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/malloc.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_lock.h>
35 #include <asm/uaccess.h>
36 #include <asm/pgalloc.h>
38 #define DLINFO_ITEMS 13
40 #include <linux/elf.h>
42 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs);
43 static int load_elf_library(struct file*);
44 extern int dump_fpu (struct pt_regs *, elf_fpregset_t *);
45 extern void dump_thread(struct pt_regs *, struct user *);
47 #ifndef elf_addr_t
48 #define elf_addr_t unsigned long
49 #define elf_caddr_t char *
50 #endif
53 * If we don't support core dumping, then supply a NULL so we
54 * don't even try.
56 #ifdef USE_ELF_CORE_DUMP
57 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file);
58 #else
59 #define elf_core_dump NULL
60 #endif
62 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_EXEC_PAGESIZE-1))
63 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_EXEC_PAGESIZE-1))
64 #define ELF_PAGEALIGN(_v) (((_v) + ELF_EXEC_PAGESIZE - 1) & ~(ELF_EXEC_PAGESIZE - 1))
66 static struct linux_binfmt elf_format = {
67 NULL, THIS_MODULE, load_elf_binary, load_elf_library, elf_core_dump, ELF_EXEC_PAGESIZE
70 static void set_brk(unsigned long start, unsigned long end)
72 start = ELF_PAGEALIGN(start);
73 end = ELF_PAGEALIGN(end);
74 if (end <= start)
75 return;
76 do_brk(start, end - start);
80 /* We need to explicitly zero any fractional pages
81 after the data section (i.e. bss). This would
82 contain the junk from the file that should not
83 be in memory */
86 static void padzero(unsigned long elf_bss)
88 unsigned long nbyte;
90 nbyte = ELF_PAGEOFFSET(elf_bss);
91 if (nbyte) {
92 nbyte = ELF_EXEC_PAGESIZE - nbyte;
93 clear_user((void *) elf_bss, nbyte);
97 static elf_addr_t *
98 create_elf_tables(char *p, int argc, int envc,
99 struct elfhdr * exec,
100 unsigned long load_addr,
101 unsigned long load_bias,
102 unsigned long interp_load_addr, int ibcs)
104 elf_caddr_t *argv;
105 elf_caddr_t *envp;
106 elf_addr_t *sp, *csp;
107 char *k_platform, *u_platform;
108 long hwcap;
109 size_t platform_len = 0;
112 * Get hold of platform and hardware capabilities masks for
113 * the machine we are running on. In some cases (Sparc),
114 * this info is impossible to get, in others (i386) it is
115 * merely difficult.
118 hwcap = ELF_HWCAP;
119 k_platform = ELF_PLATFORM;
121 if (k_platform) {
122 platform_len = strlen(k_platform) + 1;
123 u_platform = p - platform_len;
124 __copy_to_user(u_platform, k_platform, platform_len);
125 } else
126 u_platform = p;
129 * Force 16 byte _final_ alignment here for generality.
130 * Leave an extra 16 bytes free so that on the PowerPC we
131 * can move the aux table up to start on a 16-byte boundary.
133 sp = (elf_addr_t *)((~15UL & (unsigned long)(u_platform)) - 16UL);
134 csp = sp;
135 csp -= ((exec ? DLINFO_ITEMS*2 : 4) + (k_platform ? 2 : 0));
136 csp -= envc+1;
137 csp -= argc+1;
138 csp -= (!ibcs ? 3 : 1); /* argc itself */
139 if ((unsigned long)csp & 15UL)
140 sp -= ((unsigned long)csp & 15UL) / sizeof(*sp);
143 * Put the ELF interpreter info on the stack
145 #define NEW_AUX_ENT(nr, id, val) \
146 __put_user ((id), sp+(nr*2)); \
147 __put_user ((val), sp+(nr*2+1)); \
149 sp -= 2;
150 NEW_AUX_ENT(0, AT_NULL, 0);
151 if (k_platform) {
152 sp -= 2;
153 NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
155 sp -= 2;
156 NEW_AUX_ENT(0, AT_HWCAP, hwcap);
158 if (exec) {
159 sp -= 11*2;
161 NEW_AUX_ENT(0, AT_PHDR, load_addr + exec->e_phoff);
162 NEW_AUX_ENT(1, AT_PHENT, sizeof (struct elf_phdr));
163 NEW_AUX_ENT(2, AT_PHNUM, exec->e_phnum);
164 NEW_AUX_ENT(3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
165 NEW_AUX_ENT(4, AT_BASE, interp_load_addr);
166 NEW_AUX_ENT(5, AT_FLAGS, 0);
167 NEW_AUX_ENT(6, AT_ENTRY, load_bias + exec->e_entry);
168 NEW_AUX_ENT(7, AT_UID, (elf_addr_t) current->uid);
169 NEW_AUX_ENT(8, AT_EUID, (elf_addr_t) current->euid);
170 NEW_AUX_ENT(9, AT_GID, (elf_addr_t) current->gid);
171 NEW_AUX_ENT(10, AT_EGID, (elf_addr_t) current->egid);
173 #undef NEW_AUX_ENT
175 sp -= envc+1;
176 envp = (elf_caddr_t *) sp;
177 sp -= argc+1;
178 argv = (elf_caddr_t *) sp;
179 if (!ibcs) {
180 __put_user((elf_addr_t)(unsigned long) envp,--sp);
181 __put_user((elf_addr_t)(unsigned long) argv,--sp);
184 __put_user((elf_addr_t)argc,--sp);
185 current->mm->arg_start = (unsigned long) p;
186 while (argc-->0) {
187 __put_user((elf_caddr_t)(unsigned long)p,argv++);
188 p += strlen_user(p);
190 __put_user(NULL, argv);
191 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
192 while (envc-->0) {
193 __put_user((elf_caddr_t)(unsigned long)p,envp++);
194 p += strlen_user(p);
196 __put_user(NULL, envp);
197 current->mm->env_end = (unsigned long) p;
198 return sp;
202 /* This is much more generalized than the library routine read function,
203 so we keep this separate. Technically the library read function
204 is only provided so that we can read a.out libraries that have
205 an ELF header */
207 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
208 struct file * interpreter,
209 unsigned long *interp_load_addr)
211 struct elf_phdr *elf_phdata;
212 struct elf_phdr *eppnt;
213 unsigned long load_addr = 0;
214 int load_addr_set = 0;
215 unsigned long last_bss = 0, elf_bss = 0;
216 unsigned long error = ~0UL;
217 int retval, i, size;
219 /* First of all, some simple consistency checks */
220 if (interp_elf_ex->e_type != ET_EXEC &&
221 interp_elf_ex->e_type != ET_DYN)
222 goto out;
223 if (!elf_check_arch(interp_elf_ex->e_machine))
224 goto out;
225 if (!interpreter->f_op->mmap)
226 goto out;
229 * If the size of this structure has changed, then punt, since
230 * we will be doing the wrong thing.
232 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
233 goto out;
235 /* Now read in all of the header information */
237 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
238 if (size > ELF_EXEC_PAGESIZE)
239 goto out;
240 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
241 if (!elf_phdata)
242 goto out;
244 retval = kernel_read(interpreter,interp_elf_ex->e_phoff,(char *)elf_phdata,size);
245 error = retval;
246 if (retval < 0)
247 goto out_close;
249 eppnt = elf_phdata;
250 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
251 if (eppnt->p_type == PT_LOAD) {
252 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
253 int elf_prot = 0;
254 unsigned long vaddr = 0;
255 unsigned long k, map_addr;
257 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
258 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
259 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
260 vaddr = eppnt->p_vaddr;
261 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
262 elf_type |= MAP_FIXED;
264 down(&current->mm->mmap_sem);
265 map_addr = do_mmap(interpreter,
266 load_addr + ELF_PAGESTART(vaddr),
267 eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr),
268 elf_prot,
269 elf_type,
270 eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
271 up(&current->mm->mmap_sem);
272 if (map_addr > -1024UL) /* Real error */
273 goto out_close;
275 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
276 load_addr = map_addr - ELF_PAGESTART(vaddr);
277 load_addr_set = 1;
281 * Find the end of the file mapping for this phdr, and keep
282 * track of the largest address we see for this.
284 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
285 if (k > elf_bss)
286 elf_bss = k;
289 * Do the same thing for the memory mapping - between
290 * elf_bss and last_bss is the bss section.
292 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
293 if (k > last_bss)
294 last_bss = k;
298 /* Now use mmap to map the library into memory. */
301 * Now fill out the bss section. First pad the last page up
302 * to the page boundary, and then perform a mmap to make sure
303 * that there are zero-mapped pages up to and including the
304 * last bss page.
306 padzero(elf_bss);
307 elf_bss = ELF_PAGESTART(elf_bss + ELF_EXEC_PAGESIZE - 1); /* What we have mapped so far */
309 /* Map the last of the bss segment */
310 if (last_bss > elf_bss)
311 do_brk(elf_bss, last_bss - elf_bss);
313 *interp_load_addr = load_addr;
314 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
316 out_close:
317 kfree(elf_phdata);
318 out:
319 return error;
322 static unsigned long load_aout_interp(struct exec * interp_ex,
323 struct file * interpreter)
325 unsigned long text_data, elf_entry = ~0UL;
326 char * addr;
327 loff_t offset;
328 int retval;
330 current->mm->end_code = interp_ex->a_text;
331 text_data = interp_ex->a_text + interp_ex->a_data;
332 current->mm->end_data = text_data;
333 current->mm->brk = interp_ex->a_bss + text_data;
335 switch (N_MAGIC(*interp_ex)) {
336 case OMAGIC:
337 offset = 32;
338 addr = (char *) 0;
339 break;
340 case ZMAGIC:
341 case QMAGIC:
342 offset = N_TXTOFF(*interp_ex);
343 addr = (char *) N_TXTADDR(*interp_ex);
344 break;
345 default:
346 goto out;
349 do_brk(0, text_data);
350 retval = -ENOEXEC;
351 if (!interpreter->f_op->read)
352 goto out;
353 retval = interpreter->f_op->read(interpreter, addr, text_data, &offset);
354 if (retval < 0)
355 goto out;
356 flush_icache_range((unsigned long)addr,
357 (unsigned long)addr + text_data);
359 do_brk(ELF_PAGESTART(text_data + ELF_EXEC_PAGESIZE - 1),
360 interp_ex->a_bss);
361 elf_entry = interp_ex->a_entry;
363 out:
364 return elf_entry;
368 * These are the functions used to load ELF style executables and shared
369 * libraries. There is no binary dependent code anywhere else.
372 #define INTERPRETER_NONE 0
373 #define INTERPRETER_AOUT 1
374 #define INTERPRETER_ELF 2
377 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
379 struct file *interpreter = NULL; /* to shut gcc up */
380 unsigned long load_addr = 0, load_bias;
381 int load_addr_set = 0;
382 char * elf_interpreter = NULL;
383 unsigned int interpreter_type = INTERPRETER_NONE;
384 unsigned char ibcs2_interpreter = 0;
385 mm_segment_t old_fs;
386 unsigned long error;
387 struct elf_phdr * elf_ppnt, *elf_phdata;
388 unsigned long elf_bss, k, elf_brk;
389 int elf_exec_fileno;
390 int retval, size, i;
391 unsigned long elf_entry, interp_load_addr = 0;
392 unsigned long start_code, end_code, start_data, end_data;
393 struct elfhdr elf_ex;
394 struct elfhdr interp_elf_ex;
395 struct exec interp_ex;
396 char passed_fileno[6];
398 /* Get the exec-header */
399 elf_ex = *((struct elfhdr *) bprm->buf);
401 retval = -ENOEXEC;
402 /* First of all, some simple consistency checks */
403 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
404 goto out;
406 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
407 goto out;
408 if (!elf_check_arch(elf_ex.e_machine))
409 goto out;
410 #ifdef __mips__
411 /* IRIX binaries handled elsewhere. */
412 if (elf_ex.e_flags & EF_MIPS_ARCH) {
413 retval = -ENOEXEC;
414 goto out;
416 #endif
417 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
418 goto out;
420 /* Now read in all of the header information */
422 retval = -ENOMEM;
423 size = elf_ex.e_phentsize * elf_ex.e_phnum;
424 if (size > 65536)
425 goto out;
426 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
427 if (!elf_phdata)
428 goto out;
430 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *) elf_phdata, size);
431 if (retval < 0)
432 goto out_free_ph;
434 retval = get_unused_fd();
435 if (retval < 0)
436 goto out_free_ph;
437 get_file(bprm->file);
438 fd_install(elf_exec_fileno = retval, bprm->file);
440 elf_ppnt = elf_phdata;
441 elf_bss = 0;
442 elf_brk = 0;
444 start_code = ~0UL;
445 end_code = 0;
446 start_data = 0;
447 end_data = 0;
449 for (i = 0; i < elf_ex.e_phnum; i++) {
450 if (elf_ppnt->p_type == PT_INTERP) {
451 retval = -EINVAL;
452 if (elf_interpreter)
453 goto out_free_dentry;
455 /* This is the program interpreter used for
456 * shared libraries - for now assume that this
457 * is an a.out format binary
460 retval = -ENOMEM;
461 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
462 GFP_KERNEL);
463 if (!elf_interpreter)
464 goto out_free_file;
466 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
467 elf_interpreter,
468 elf_ppnt->p_filesz);
469 if (retval < 0)
470 goto out_free_interp;
471 /* If the program interpreter is one of these two,
472 * then assume an iBCS2 image. Otherwise assume
473 * a native linux image.
475 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
476 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
477 ibcs2_interpreter = 1;
478 #if 0
479 printk("Using ELF interpreter %s\n", elf_interpreter);
480 #endif
481 #ifdef __sparc__
482 if (ibcs2_interpreter) {
483 unsigned long old_pers = current->personality;
484 struct exec_domain *old_domain = current->exec_domain;
485 struct exec_domain *new_domain;
486 struct fs_struct *old_fs = current->fs, *new_fs;
487 get_exec_domain(old_domain);
488 atomic_inc(&old_fs->count);
490 set_personality(PER_SVR4);
491 interpreter = open_exec(elf_interpreter);
493 new_domain = current->exec_domain;
494 new_fs = current->fs;
495 current->personality = old_pers;
496 current->exec_domain = old_domain;
497 current->fs = old_fs;
498 put_exec_domain(new_domain);
499 put_fs_struct(new_fs);
500 } else
501 #endif
503 interpreter = open_exec(elf_interpreter);
505 retval = PTR_ERR(interpreter);
506 if (IS_ERR(interpreter))
507 goto out_free_interp;
508 retval = kernel_read(interpreter, 0, bprm->buf, 128);
509 if (retval < 0)
510 goto out_free_dentry;
512 /* Get the exec headers */
513 interp_ex = *((struct exec *) bprm->buf);
514 interp_elf_ex = *((struct elfhdr *) bprm->buf);
516 elf_ppnt++;
519 /* Some simple consistency checks for the interpreter */
520 if (elf_interpreter) {
521 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
523 /* Now figure out which format our binary is */
524 if ((N_MAGIC(interp_ex) != OMAGIC) &&
525 (N_MAGIC(interp_ex) != ZMAGIC) &&
526 (N_MAGIC(interp_ex) != QMAGIC))
527 interpreter_type = INTERPRETER_ELF;
529 if (memcmp(interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
530 interpreter_type &= ~INTERPRETER_ELF;
532 retval = -ELIBBAD;
533 if (!interpreter_type)
534 goto out_free_dentry;
536 /* Make sure only one type was selected */
537 if ((interpreter_type & INTERPRETER_ELF) &&
538 interpreter_type != INTERPRETER_ELF) {
539 printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
540 interpreter_type = INTERPRETER_ELF;
544 /* OK, we are done with that, now set up the arg stuff,
545 and then start this sucker up */
547 if (!bprm->sh_bang) {
548 char * passed_p;
550 if (interpreter_type == INTERPRETER_AOUT) {
551 sprintf(passed_fileno, "%d", elf_exec_fileno);
552 passed_p = passed_fileno;
554 if (elf_interpreter) {
555 retval = copy_strings_kernel(1,&passed_p,bprm);
556 if (retval)
557 goto out_free_dentry;
558 bprm->argc++;
563 /* Flush all traces of the currently running executable */
564 retval = flush_old_exec(bprm);
565 if (retval)
566 goto out_free_dentry;
568 /* OK, This is the point of no return */
569 current->mm->start_data = 0;
570 current->mm->end_data = 0;
571 current->mm->end_code = 0;
572 current->mm->mmap = NULL;
573 current->flags &= ~PF_FORKNOEXEC;
574 elf_entry = (unsigned long) elf_ex.e_entry;
576 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
577 may depend on the personality. */
578 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
580 /* Do this so that we can load the interpreter, if need be. We will
581 change some of these later */
582 current->mm->rss = 0;
583 setup_arg_pages(bprm); /* XXX: check error */
584 current->mm->start_stack = bprm->p;
586 /* Try and get dynamic programs out of the way of the default mmap
587 base, as well as whatever program they might try to exec. This
588 is because the brk will follow the loader, and is not movable. */
590 load_bias = ELF_PAGESTART(elf_ex.e_type==ET_DYN ? ELF_ET_DYN_BASE : 0);
592 /* Now we do a little grungy work by mmaping the ELF image into
593 the correct location in memory. At this point, we assume that
594 the image should be loaded at fixed address, not at a variable
595 address. */
597 old_fs = get_fs();
598 set_fs(get_ds());
599 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
600 int elf_prot = 0, elf_flags;
601 unsigned long vaddr;
603 if (elf_ppnt->p_type != PT_LOAD)
604 continue;
606 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
607 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
608 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
610 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
612 vaddr = elf_ppnt->p_vaddr;
613 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
614 elf_flags |= MAP_FIXED;
617 down(&current->mm->mmap_sem);
618 error = do_mmap(bprm->file, ELF_PAGESTART(load_bias + vaddr),
619 (elf_ppnt->p_filesz +
620 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
621 elf_prot, elf_flags, (elf_ppnt->p_offset -
622 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
623 up(&current->mm->mmap_sem);
625 if (!load_addr_set) {
626 load_addr_set = 1;
627 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
628 if (elf_ex.e_type == ET_DYN) {
629 load_bias += error -
630 ELF_PAGESTART(load_bias + vaddr);
631 load_addr += error;
634 k = elf_ppnt->p_vaddr;
635 if (k < start_code) start_code = k;
636 if (start_data < k) start_data = k;
638 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
640 if (k > elf_bss)
641 elf_bss = k;
642 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
643 end_code = k;
644 if (end_data < k)
645 end_data = k;
646 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
647 if (k > elf_brk)
648 elf_brk = k;
650 set_fs(old_fs);
652 elf_entry += load_bias;
653 elf_bss += load_bias;
654 elf_brk += load_bias;
655 start_code += load_bias;
656 end_code += load_bias;
657 start_data += load_bias;
658 end_data += load_bias;
660 if (elf_interpreter) {
661 if (interpreter_type == INTERPRETER_AOUT)
662 elf_entry = load_aout_interp(&interp_ex,
663 interpreter);
664 else
665 elf_entry = load_elf_interp(&interp_elf_ex,
666 interpreter,
667 &interp_load_addr);
669 lock_kernel();
670 fput(interpreter);
671 unlock_kernel();
672 kfree(elf_interpreter);
674 if (elf_entry == ~0UL) {
675 printk(KERN_ERR "Unable to load interpreter\n");
676 kfree(elf_phdata);
677 send_sig(SIGSEGV, current, 0);
678 return 0;
682 kfree(elf_phdata);
684 if (interpreter_type != INTERPRETER_AOUT)
685 sys_close(elf_exec_fileno);
687 set_binfmt(&elf_format);
689 compute_creds(bprm);
690 current->flags &= ~PF_FORKNOEXEC;
691 bprm->p = (unsigned long)
692 create_elf_tables((char *)bprm->p,
693 bprm->argc,
694 bprm->envc,
695 (interpreter_type == INTERPRETER_ELF ? &elf_ex : NULL),
696 load_addr, load_bias,
697 interp_load_addr,
698 (interpreter_type == INTERPRETER_AOUT ? 0 : 1));
699 /* N.B. passed_fileno might not be initialized? */
700 if (interpreter_type == INTERPRETER_AOUT)
701 current->mm->arg_start += strlen(passed_fileno) + 1;
702 current->mm->start_brk = current->mm->brk = elf_brk;
703 current->mm->end_code = end_code;
704 current->mm->start_code = start_code;
705 current->mm->start_data = start_data;
706 current->mm->end_data = end_data;
707 current->mm->start_stack = bprm->p;
709 /* Calling set_brk effectively mmaps the pages that we need
710 * for the bss and break sections
712 set_brk(elf_bss, elf_brk);
714 padzero(elf_bss);
716 #if 0
717 printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
718 printk("(end_code) %lx\n" , (long) current->mm->end_code);
719 printk("(start_code) %lx\n" , (long) current->mm->start_code);
720 printk("(start_data) %lx\n" , (long) current->mm->start_data);
721 printk("(end_data) %lx\n" , (long) current->mm->end_data);
722 printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
723 printk("(brk) %lx\n" , (long) current->mm->brk);
724 #endif
726 if ( current->personality == PER_SVR4 )
728 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
729 and some applications "depend" upon this behavior.
730 Since we do not have the power to recompile these, we
731 emulate the SVr4 behavior. Sigh. */
732 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
733 down(&current->mm->mmap_sem);
734 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
735 MAP_FIXED | MAP_PRIVATE, 0);
736 up(&current->mm->mmap_sem);
739 #ifdef ELF_PLAT_INIT
741 * The ABI may specify that certain registers be set up in special
742 * ways (on i386 %edx is the address of a DT_FINI function, for
743 * example. This macro performs whatever initialization to
744 * the regs structure is required.
746 ELF_PLAT_INIT(regs);
747 #endif
749 start_thread(regs, elf_entry, bprm->p);
750 if (current->ptrace & PT_PTRACED)
751 send_sig(SIGTRAP, current, 0);
752 retval = 0;
753 out:
754 return retval;
756 /* error cleanup */
757 out_free_dentry:
758 lock_kernel();
759 fput(interpreter);
760 unlock_kernel();
761 out_free_interp:
762 if (elf_interpreter)
763 kfree(elf_interpreter);
764 out_free_file:
765 sys_close(elf_exec_fileno);
766 out_free_ph:
767 kfree(elf_phdata);
768 goto out;
771 /* This is really simpleminded and specialized - we are loading an
772 a.out library that is given an ELF header. */
774 static int load_elf_library(struct file *file)
776 struct elf_phdr *elf_phdata;
777 unsigned long elf_bss = 0, bss, len, k;
778 int retval, error, i, j;
779 struct elfhdr elf_ex;
781 error = -ENOEXEC;
782 retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
783 if (retval != sizeof(elf_ex))
784 goto out;
786 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
787 goto out;
789 /* First of all, some simple consistency checks */
790 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
791 !elf_check_arch(elf_ex.e_machine) || !file->f_op->mmap)
792 goto out;
794 /* Now read in all of the header information */
796 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
797 if (j > ELF_EXEC_PAGESIZE)
798 goto out;
800 error = -ENOMEM;
801 elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
802 if (!elf_phdata)
803 goto out;
805 /* N.B. check for error return?? */
806 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata,
807 sizeof(struct elf_phdr) * elf_ex.e_phnum);
809 error = -ENOEXEC;
810 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
811 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
812 if (j != 1)
813 goto out_free_ph;
815 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
817 /* Now use mmap to map the library into memory. */
818 down(&current->mm->mmap_sem);
819 error = do_mmap(file,
820 ELF_PAGESTART(elf_phdata->p_vaddr),
821 (elf_phdata->p_filesz +
822 ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
823 PROT_READ | PROT_WRITE | PROT_EXEC,
824 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
825 (elf_phdata->p_offset -
826 ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
827 up(&current->mm->mmap_sem);
828 if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
829 goto out_free_ph;
831 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
832 if (k > elf_bss)
833 elf_bss = k;
834 padzero(elf_bss);
836 len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr +
837 ELF_EXEC_PAGESIZE - 1);
838 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
839 if (bss > len)
840 do_brk(len, bss - len);
841 error = 0;
843 out_free_ph:
844 kfree(elf_phdata);
845 out:
846 return error;
850 * Note that some platforms still use traditional core dumps and not
851 * the ELF core dump. Each platform can select it as appropriate.
853 #ifdef USE_ELF_CORE_DUMP
856 * ELF core dumper
858 * Modelled on fs/exec.c:aout_core_dump()
859 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
862 * These are the only things you should do on a core-file: use only these
863 * functions to write out all the necessary info.
865 static int dump_write(struct file *file, const void *addr, int nr)
867 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
870 static int dump_seek(struct file *file, off_t off)
872 if (file->f_op->llseek) {
873 if (file->f_op->llseek(file, off, 0) != off)
874 return 0;
875 } else
876 file->f_pos = off;
877 return 1;
881 * Decide whether a segment is worth dumping; default is yes to be
882 * sure (missing info is worse than too much; etc).
883 * Personally I'd include everything, and use the coredump limit...
885 * I think we should skip something. But I am not sure how. H.J.
887 static inline int maydump(struct vm_area_struct *vma)
889 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
890 return 0;
892 /* Do not dump I/O mapped devices! -DaveM */
893 if(vma->vm_flags & VM_IO)
894 return 0;
895 #if 1
896 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
897 return 1;
898 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
899 return 0;
900 #endif
901 return 1;
904 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
906 /* An ELF note in memory */
907 struct memelfnote
909 const char *name;
910 int type;
911 unsigned int datasz;
912 void *data;
915 static int notesize(struct memelfnote *en)
917 int sz;
919 sz = sizeof(struct elf_note);
920 sz += roundup(strlen(en->name), 4);
921 sz += roundup(en->datasz, 4);
923 return sz;
926 /* #define DEBUG */
928 #ifdef DEBUG
929 static void dump_regs(const char *str, elf_greg_t *r)
931 int i;
932 static const char *regs[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
933 "eax", "ds", "es", "fs", "gs",
934 "orig_eax", "eip", "cs",
935 "efl", "uesp", "ss"};
936 printk("Registers: %s\n", str);
938 for(i = 0; i < ELF_NGREG; i++)
940 unsigned long val = r[i];
941 printk(" %-2d %-5s=%08lx %lu\n", i, regs[i], val, val);
944 #endif
946 #define DUMP_WRITE(addr, nr) \
947 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
948 #define DUMP_SEEK(off) \
949 do { if (!dump_seek(file, (off))) return 0; } while(0)
951 static int writenote(struct memelfnote *men, struct file *file)
953 struct elf_note en;
955 en.n_namesz = strlen(men->name);
956 en.n_descsz = men->datasz;
957 en.n_type = men->type;
959 DUMP_WRITE(&en, sizeof(en));
960 DUMP_WRITE(men->name, en.n_namesz);
961 /* XXX - cast from long long to long to avoid need for libgcc.a */
962 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
963 DUMP_WRITE(men->data, men->datasz);
964 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
966 return 1;
968 #undef DUMP_WRITE
969 #undef DUMP_SEEK
971 #define DUMP_WRITE(addr, nr) \
972 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
973 goto end_coredump;
974 #define DUMP_SEEK(off) \
975 if (!dump_seek(file, (off))) \
976 goto end_coredump;
978 * Actual dumper
980 * This is a two-pass process; first we find the offsets of the bits,
981 * and then they are actually written out. If we run out of core limit
982 * we just truncate.
984 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
986 int has_dumped = 0;
987 mm_segment_t fs;
988 int segs;
989 size_t size = 0;
990 int i;
991 struct vm_area_struct *vma;
992 struct elfhdr elf;
993 off_t offset = 0, dataoff;
994 unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
995 int numnote = 4;
996 struct memelfnote notes[4];
997 struct elf_prstatus prstatus; /* NT_PRSTATUS */
998 elf_fpregset_t fpu; /* NT_PRFPREG */
999 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1001 segs = current->mm->map_count;
1003 #ifdef DEBUG
1004 printk("elf_core_dump: %d segs %lu limit\n", segs, limit);
1005 #endif
1007 /* Set up header */
1008 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1009 elf.e_ident[EI_CLASS] = ELF_CLASS;
1010 elf.e_ident[EI_DATA] = ELF_DATA;
1011 elf.e_ident[EI_VERSION] = EV_CURRENT;
1012 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1014 elf.e_type = ET_CORE;
1015 elf.e_machine = ELF_ARCH;
1016 elf.e_version = EV_CURRENT;
1017 elf.e_entry = 0;
1018 elf.e_phoff = sizeof(elf);
1019 elf.e_shoff = 0;
1020 elf.e_flags = 0;
1021 elf.e_ehsize = sizeof(elf);
1022 elf.e_phentsize = sizeof(struct elf_phdr);
1023 elf.e_phnum = segs+1; /* Include notes */
1024 elf.e_shentsize = 0;
1025 elf.e_shnum = 0;
1026 elf.e_shstrndx = 0;
1028 fs = get_fs();
1029 set_fs(KERNEL_DS);
1031 has_dumped = 1;
1032 current->flags |= PF_DUMPCORE;
1034 DUMP_WRITE(&elf, sizeof(elf));
1035 offset += sizeof(elf); /* Elf header */
1036 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1039 * Set up the notes in similar form to SVR4 core dumps made
1040 * with info from their /proc.
1042 memset(&psinfo, 0, sizeof(psinfo));
1043 memset(&prstatus, 0, sizeof(prstatus));
1045 notes[0].name = "CORE";
1046 notes[0].type = NT_PRSTATUS;
1047 notes[0].datasz = sizeof(prstatus);
1048 notes[0].data = &prstatus;
1049 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1050 prstatus.pr_sigpend = current->signal.sig[0];
1051 prstatus.pr_sighold = current->blocked.sig[0];
1052 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1053 psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1054 psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1055 psinfo.pr_sid = prstatus.pr_sid = current->session;
1056 prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
1057 prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
1058 prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
1059 prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
1060 prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
1061 prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
1062 prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
1063 prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
1066 * This transfers the registers from regs into the standard
1067 * coredump arrangement, whatever that is.
1069 #ifdef ELF_CORE_COPY_REGS
1070 ELF_CORE_COPY_REGS(prstatus.pr_reg, regs)
1071 #else
1072 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs))
1074 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1075 (long)sizeof(elf_gregset_t), (long)sizeof(struct pt_regs));
1077 else
1078 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1079 #endif
1081 #ifdef DEBUG
1082 dump_regs("Passed in regs", (elf_greg_t *)regs);
1083 dump_regs("prstatus regs", (elf_greg_t *)&prstatus.pr_reg);
1084 #endif
1086 notes[1].name = "CORE";
1087 notes[1].type = NT_PRPSINFO;
1088 notes[1].datasz = sizeof(psinfo);
1089 notes[1].data = &psinfo;
1090 i = current->state ? ffz(~current->state) + 1 : 0;
1091 psinfo.pr_state = i;
1092 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1093 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1094 psinfo.pr_nice = current->priority-15;
1095 psinfo.pr_flag = current->flags;
1096 psinfo.pr_uid = NEW_TO_OLD_UID(current->uid);
1097 psinfo.pr_gid = NEW_TO_OLD_GID(current->gid);
1099 int i, len;
1101 set_fs(fs);
1103 len = current->mm->arg_end - current->mm->arg_start;
1104 if (len >= ELF_PRARGSZ)
1105 len = ELF_PRARGSZ-1;
1106 copy_from_user(&psinfo.pr_psargs,
1107 (const char *)current->mm->arg_start, len);
1108 for(i = 0; i < len; i++)
1109 if (psinfo.pr_psargs[i] == 0)
1110 psinfo.pr_psargs[i] = ' ';
1111 psinfo.pr_psargs[len] = 0;
1113 set_fs(KERNEL_DS);
1115 strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1117 notes[2].name = "CORE";
1118 notes[2].type = NT_TASKSTRUCT;
1119 notes[2].datasz = sizeof(*current);
1120 notes[2].data = current;
1122 /* Try to dump the FPU. */
1123 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1124 if (!prstatus.pr_fpvalid)
1126 numnote--;
1128 else
1130 notes[3].name = "CORE";
1131 notes[3].type = NT_PRFPREG;
1132 notes[3].datasz = sizeof(fpu);
1133 notes[3].data = &fpu;
1136 /* Write notes phdr entry */
1138 struct elf_phdr phdr;
1139 int sz = 0;
1141 for(i = 0; i < numnote; i++)
1142 sz += notesize(&notes[i]);
1144 phdr.p_type = PT_NOTE;
1145 phdr.p_offset = offset;
1146 phdr.p_vaddr = 0;
1147 phdr.p_paddr = 0;
1148 phdr.p_filesz = sz;
1149 phdr.p_memsz = 0;
1150 phdr.p_flags = 0;
1151 phdr.p_align = 0;
1153 offset += phdr.p_filesz;
1154 DUMP_WRITE(&phdr, sizeof(phdr));
1157 /* Page-align dumped data */
1158 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1160 /* Write program headers for segments dump */
1161 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1162 struct elf_phdr phdr;
1163 size_t sz;
1165 sz = vma->vm_end - vma->vm_start;
1167 phdr.p_type = PT_LOAD;
1168 phdr.p_offset = offset;
1169 phdr.p_vaddr = vma->vm_start;
1170 phdr.p_paddr = 0;
1171 phdr.p_filesz = maydump(vma) ? sz : 0;
1172 phdr.p_memsz = sz;
1173 offset += phdr.p_filesz;
1174 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1175 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1176 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1177 phdr.p_align = ELF_EXEC_PAGESIZE;
1179 DUMP_WRITE(&phdr, sizeof(phdr));
1182 for(i = 0; i < numnote; i++)
1183 if (!writenote(&notes[i], file))
1184 goto end_coredump;
1186 set_fs(fs);
1188 DUMP_SEEK(dataoff);
1190 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1191 unsigned long addr;
1193 if (!maydump(vma))
1194 continue;
1195 #ifdef DEBUG
1196 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1197 #endif
1198 for (addr = vma->vm_start;
1199 addr < vma->vm_end;
1200 addr += PAGE_SIZE) {
1201 pgd_t *pgd;
1202 pmd_t *pmd;
1203 pte_t *pte;
1205 pgd = pgd_offset(vma->vm_mm, addr);
1206 pmd = pmd_alloc(pgd, addr);
1208 if (!pmd)
1209 goto end_coredump;
1210 pte = pte_alloc(pmd, addr);
1211 if (!pte)
1212 goto end_coredump;
1213 if (!pte_present(*pte) &&
1214 pte_none(*pte)) {
1215 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1216 } else {
1217 DUMP_WRITE((void*)addr, PAGE_SIZE);
1222 if ((off_t) file->f_pos != offset) {
1223 /* Sanity check */
1224 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1225 (off_t) file->f_pos, offset);
1228 end_coredump:
1229 set_fs(fs);
1230 return has_dumped;
1232 #endif /* USE_ELF_CORE_DUMP */
1234 static int __init init_elf_binfmt(void)
1236 return register_binfmt(&elf_format);
1239 static void __exit exit_elf_binfmt(void)
1241 /* Remove the COFF and ELF loaders. */
1242 unregister_binfmt(&elf_format);
1245 module_init(init_elf_binfmt)
1246 module_exit(exit_elf_binfmt)