Import 2.3.7pre9
[davej-history.git] / fs / binfmt_elf.c
blobbd3c8f490fa51943c682d7cd0bbd7b4ed45db94e
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>
33 #include <asm/uaccess.h>
34 #include <asm/pgtable.h>
36 #include <linux/config.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(int fd);
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);
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 #ifndef MODULE
68 NULL, NULL, load_elf_binary, load_elf_library, elf_core_dump
69 #else
70 NULL, &__this_module, load_elf_binary, load_elf_library, elf_core_dump
71 #endif
74 static void set_brk(unsigned long start, unsigned long end)
76 start = ELF_PAGEALIGN(start);
77 end = ELF_PAGEALIGN(end);
78 if (end <= start)
79 return;
80 do_brk(start, end - start);
84 /* We need to explicitly zero any fractional pages
85 after the data section (i.e. bss). This would
86 contain the junk from the file that should not
87 be in memory */
90 static void padzero(unsigned long elf_bss)
92 unsigned long nbyte;
94 nbyte = ELF_PAGEOFFSET(elf_bss);
95 if (nbyte) {
96 nbyte = ELF_EXEC_PAGESIZE - nbyte;
97 clear_user((void *) elf_bss, nbyte);
101 static elf_addr_t *
102 create_elf_tables(char *p, int argc, int envc,
103 struct elfhdr * exec,
104 unsigned long load_addr,
105 unsigned long load_bias,
106 unsigned long interp_load_addr, int ibcs)
108 elf_caddr_t *argv;
109 elf_caddr_t *envp;
110 elf_addr_t *sp, *csp;
111 char *k_platform, *u_platform;
112 long hwcap;
113 size_t platform_len = 0;
116 * Get hold of platform and hardware capabilities masks for
117 * the machine we are running on. In some cases (Sparc),
118 * this info is impossible to get, in others (i386) it is
119 * merely difficult.
122 hwcap = ELF_HWCAP;
123 k_platform = ELF_PLATFORM;
125 if (k_platform) {
126 platform_len = strlen(k_platform) + 1;
127 u_platform = p - platform_len;
128 __copy_to_user(u_platform, k_platform, platform_len);
129 } else
130 u_platform = p;
133 * Force 16 byte _final_ alignment here for generality.
134 * Leave an extra 16 bytes free so that on the PowerPC we
135 * can move the aux table up to start on a 16-byte boundary.
137 sp = (elf_addr_t *)((~15UL & (unsigned long)(u_platform)) - 16UL);
138 csp = sp;
139 csp -= ((exec ? DLINFO_ITEMS*2 : 4) + (k_platform ? 2 : 0));
140 csp -= envc+1;
141 csp -= argc+1;
142 csp -= (!ibcs ? 3 : 1); /* argc itself */
143 if ((unsigned long)csp & 15UL)
144 sp -= ((unsigned long)csp & 15UL) / sizeof(*sp);
147 * Put the ELF interpreter info on the stack
149 #define NEW_AUX_ENT(nr, id, val) \
150 __put_user ((id), sp+(nr*2)); \
151 __put_user ((val), sp+(nr*2+1)); \
153 sp -= 2;
154 NEW_AUX_ENT(0, AT_NULL, 0);
155 if (k_platform) {
156 sp -= 2;
157 NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
159 sp -= 2;
160 NEW_AUX_ENT(0, AT_HWCAP, hwcap);
162 if (exec) {
163 sp -= 11*2;
165 NEW_AUX_ENT(0, AT_PHDR, load_addr + exec->e_phoff);
166 NEW_AUX_ENT(1, AT_PHENT, sizeof (struct elf_phdr));
167 NEW_AUX_ENT(2, AT_PHNUM, exec->e_phnum);
168 NEW_AUX_ENT(3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
169 NEW_AUX_ENT(4, AT_BASE, interp_load_addr);
170 NEW_AUX_ENT(5, AT_FLAGS, 0);
171 NEW_AUX_ENT(6, AT_ENTRY, load_bias + exec->e_entry);
172 NEW_AUX_ENT(7, AT_UID, (elf_addr_t) current->uid);
173 NEW_AUX_ENT(8, AT_EUID, (elf_addr_t) current->euid);
174 NEW_AUX_ENT(9, AT_GID, (elf_addr_t) current->gid);
175 NEW_AUX_ENT(10, AT_EGID, (elf_addr_t) current->egid);
177 #undef NEW_AUX_ENT
179 sp -= envc+1;
180 envp = (elf_caddr_t *) sp;
181 sp -= argc+1;
182 argv = (elf_caddr_t *) sp;
183 if (!ibcs) {
184 __put_user((elf_addr_t)(unsigned long) envp,--sp);
185 __put_user((elf_addr_t)(unsigned long) argv,--sp);
188 __put_user((elf_addr_t)argc,--sp);
189 current->mm->arg_start = (unsigned long) p;
190 while (argc-->0) {
191 __put_user((elf_caddr_t)(unsigned long)p,argv++);
192 p += strlen_user(p);
194 __put_user(NULL, argv);
195 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
196 while (envc-->0) {
197 __put_user((elf_caddr_t)(unsigned long)p,envp++);
198 p += strlen_user(p);
200 __put_user(NULL, envp);
201 current->mm->env_end = (unsigned long) p;
202 return sp;
206 /* This is much more generalized than the library routine read function,
207 so we keep this separate. Technically the library read function
208 is only provided so that we can read a.out libraries that have
209 an ELF header */
211 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
212 struct dentry * interpreter_dentry,
213 unsigned long *interp_load_addr)
215 struct file * file;
216 struct elf_phdr *elf_phdata;
217 struct elf_phdr *eppnt;
218 unsigned long load_addr = 0;
219 int load_addr_set = 0;
220 unsigned long last_bss = 0, elf_bss = 0;
221 unsigned long error = ~0UL;
222 int elf_exec_fileno;
223 int retval, i, size;
225 /* First of all, some simple consistency checks */
226 if (interp_elf_ex->e_type != ET_EXEC &&
227 interp_elf_ex->e_type != ET_DYN)
228 goto out;
229 if (!elf_check_arch(interp_elf_ex->e_machine))
230 goto out;
231 if (!interpreter_dentry->d_inode->i_op ||
232 !interpreter_dentry->d_inode->i_op->default_file_ops->mmap)
233 goto out;
236 * If the size of this structure has changed, then punt, since
237 * we will be doing the wrong thing.
239 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
240 goto out;
242 /* Now read in all of the header information */
244 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
245 if (size > ELF_EXEC_PAGESIZE)
246 goto out;
247 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
248 if (!elf_phdata)
249 goto out;
251 retval = read_exec(interpreter_dentry, interp_elf_ex->e_phoff,
252 (char *) elf_phdata, size, 1);
253 error = retval;
254 if (retval < 0)
255 goto out_free;
257 error = ~0UL;
258 elf_exec_fileno = open_dentry(interpreter_dentry, O_RDONLY);
259 if (elf_exec_fileno < 0)
260 goto out_free;
261 file = fget(elf_exec_fileno);
263 eppnt = elf_phdata;
264 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
265 if (eppnt->p_type == PT_LOAD) {
266 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
267 int elf_prot = 0;
268 unsigned long vaddr = 0;
269 unsigned long k, map_addr;
271 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
272 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
273 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
274 vaddr = eppnt->p_vaddr;
275 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
276 elf_type |= MAP_FIXED;
277 #ifdef __sparc__
278 } else {
279 load_addr = get_unmapped_area(0, eppnt->p_filesz +
280 ELF_PAGEOFFSET(vaddr));
281 #endif
284 map_addr = do_mmap(file,
285 load_addr + ELF_PAGESTART(vaddr),
286 eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr),
287 elf_prot,
288 elf_type,
289 eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
290 if (map_addr > -1024UL) /* Real error */
291 goto out_close;
293 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
294 load_addr = map_addr - ELF_PAGESTART(vaddr);
295 load_addr_set = 1;
299 * Find the end of the file mapping for this phdr, and keep
300 * track of the largest address we see for this.
302 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
303 if (k > elf_bss)
304 elf_bss = k;
307 * Do the same thing for the memory mapping - between
308 * elf_bss and last_bss is the bss section.
310 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
311 if (k > last_bss)
312 last_bss = k;
316 /* Now use mmap to map the library into memory. */
319 * Now fill out the bss section. First pad the last page up
320 * to the page boundary, and then perform a mmap to make sure
321 * that there are zero-mapped pages up to and including the
322 * last bss page.
324 padzero(elf_bss);
325 elf_bss = ELF_PAGESTART(elf_bss + ELF_EXEC_PAGESIZE - 1); /* What we have mapped so far */
327 /* Map the last of the bss segment */
328 if (last_bss > elf_bss)
329 do_brk(elf_bss, last_bss - elf_bss);
331 *interp_load_addr = load_addr;
332 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
334 out_close:
335 fput(file);
336 sys_close(elf_exec_fileno);
337 out_free:
338 kfree(elf_phdata);
339 out:
340 return error;
343 static unsigned long load_aout_interp(struct exec * interp_ex,
344 struct dentry * interpreter_dentry)
346 unsigned long text_data, offset, elf_entry = ~0UL;
347 char * addr;
348 int retval;
350 current->mm->end_code = interp_ex->a_text;
351 text_data = interp_ex->a_text + interp_ex->a_data;
352 current->mm->end_data = text_data;
353 current->mm->brk = interp_ex->a_bss + text_data;
355 switch (N_MAGIC(*interp_ex)) {
356 case OMAGIC:
357 offset = 32;
358 addr = (char *) 0;
359 break;
360 case ZMAGIC:
361 case QMAGIC:
362 offset = N_TXTOFF(*interp_ex);
363 addr = (char *) N_TXTADDR(*interp_ex);
364 break;
365 default:
366 goto out;
369 do_brk(0, text_data);
370 retval = read_exec(interpreter_dentry, offset, addr, text_data, 0);
371 if (retval < 0)
372 goto out;
373 flush_icache_range((unsigned long)addr,
374 (unsigned long)addr + text_data);
376 do_brk(ELF_PAGESTART(text_data + ELF_EXEC_PAGESIZE - 1),
377 interp_ex->a_bss);
378 elf_entry = interp_ex->a_entry;
380 out:
381 return elf_entry;
385 * These are the functions used to load ELF style executables and shared
386 * libraries. There is no binary dependent code anywhere else.
389 #define INTERPRETER_NONE 0
390 #define INTERPRETER_AOUT 1
391 #define INTERPRETER_ELF 2
394 static inline int
395 do_load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
397 struct file * file;
398 struct dentry *interpreter_dentry = NULL; /* to shut gcc up */
399 unsigned long load_addr = 0, load_bias;
400 int load_addr_set = 0;
401 char * elf_interpreter = NULL;
402 unsigned int interpreter_type = INTERPRETER_NONE;
403 unsigned char ibcs2_interpreter = 0;
404 mm_segment_t old_fs;
405 unsigned long error;
406 struct elf_phdr * elf_ppnt, *elf_phdata;
407 unsigned long elf_bss, k, elf_brk;
408 int elf_exec_fileno;
409 int retval, size, i;
410 unsigned long elf_entry, interp_load_addr = 0;
411 unsigned long start_code, end_code, end_data;
412 struct elfhdr elf_ex;
413 struct elfhdr interp_elf_ex;
414 struct exec interp_ex;
415 char passed_fileno[6];
417 /* Get the exec-header */
418 elf_ex = *((struct elfhdr *) bprm->buf);
420 retval = -ENOEXEC;
421 /* First of all, some simple consistency checks */
422 if (elf_ex.e_ident[0] != 0x7f ||
423 strncmp(&elf_ex.e_ident[1], "ELF", 3) != 0)
424 goto out;
426 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
427 goto out;
428 if (!elf_check_arch(elf_ex.e_machine))
429 goto out;
430 #ifdef __mips__
431 /* IRIX binaries handled elsewhere. */
432 if (elf_ex.e_flags & EF_MIPS_ARCH) {
433 retval = -ENOEXEC;
434 goto out;
436 #endif
437 if (!bprm->dentry->d_inode->i_op ||
438 !bprm->dentry->d_inode->i_op->default_file_ops ||
439 !bprm->dentry->d_inode->i_op->default_file_ops->mmap)
440 goto out;
442 /* Now read in all of the header information */
444 retval = -ENOMEM;
445 size = elf_ex.e_phentsize * elf_ex.e_phnum;
446 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
447 if (!elf_phdata)
448 goto out;
450 retval = read_exec(bprm->dentry, elf_ex.e_phoff,
451 (char *) elf_phdata, size, 1);
452 if (retval < 0)
453 goto out_free_ph;
455 retval = open_dentry(bprm->dentry, O_RDONLY);
456 if (retval < 0)
457 goto out_free_ph;
458 elf_exec_fileno = retval;
459 file = fget(elf_exec_fileno);
461 elf_ppnt = elf_phdata;
462 elf_bss = 0;
463 elf_brk = 0;
465 start_code = ~0UL;
466 end_code = 0;
467 end_data = 0;
469 for (i = 0; i < elf_ex.e_phnum; i++) {
470 if (elf_ppnt->p_type == PT_INTERP) {
471 retval = -EINVAL;
472 if (elf_interpreter)
473 goto out_free_interp;
475 /* This is the program interpreter used for
476 * shared libraries - for now assume that this
477 * is an a.out format binary
480 retval = -ENOMEM;
481 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
482 GFP_KERNEL);
483 if (!elf_interpreter)
484 goto out_free_file;
486 retval = read_exec(bprm->dentry, elf_ppnt->p_offset,
487 elf_interpreter,
488 elf_ppnt->p_filesz, 1);
489 if (retval < 0)
490 goto out_free_interp;
491 /* If the program interpreter is one of these two,
492 * then assume an iBCS2 image. Otherwise assume
493 * a native linux image.
495 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
496 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
497 ibcs2_interpreter = 1;
498 #if 0
499 printk("Using ELF interpreter %s\n", elf_interpreter);
500 #endif
501 old_fs = get_fs(); /* This could probably be optimized */
502 set_fs(get_ds());
503 #ifdef __sparc__
504 if (ibcs2_interpreter) {
505 unsigned long old_pers = current->personality;
507 current->personality = PER_SVR4;
508 interpreter_dentry = open_namei(elf_interpreter,
509 0, 0);
510 current->personality = old_pers;
511 } else
512 #endif
513 interpreter_dentry = open_namei(elf_interpreter,
514 0, 0);
515 set_fs(old_fs);
516 retval = PTR_ERR(interpreter_dentry);
517 if (IS_ERR(interpreter_dentry))
518 goto out_free_interp;
519 retval = permission(interpreter_dentry->d_inode, MAY_EXEC);
520 if (retval < 0)
521 goto out_free_dentry;
522 retval = read_exec(interpreter_dentry, 0, bprm->buf, 128, 1);
523 if (retval < 0)
524 goto out_free_dentry;
526 /* Get the exec headers */
527 interp_ex = *((struct exec *) bprm->buf);
528 interp_elf_ex = *((struct elfhdr *) bprm->buf);
530 elf_ppnt++;
533 /* Some simple consistency checks for the interpreter */
534 if (elf_interpreter) {
535 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
537 /* Now figure out which format our binary is */
538 if ((N_MAGIC(interp_ex) != OMAGIC) &&
539 (N_MAGIC(interp_ex) != ZMAGIC) &&
540 (N_MAGIC(interp_ex) != QMAGIC))
541 interpreter_type = INTERPRETER_ELF;
543 if (interp_elf_ex.e_ident[0] != 0x7f ||
544 strncmp(&interp_elf_ex.e_ident[1], "ELF", 3) != 0)
545 interpreter_type &= ~INTERPRETER_ELF;
547 retval = -ELIBBAD;
548 if (!interpreter_type)
549 goto out_free_dentry;
551 /* Make sure only one type was selected */
552 if ((interpreter_type & INTERPRETER_ELF) &&
553 interpreter_type != INTERPRETER_ELF) {
554 printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
555 interpreter_type = INTERPRETER_ELF;
559 /* OK, we are done with that, now set up the arg stuff,
560 and then start this sucker up */
562 if (!bprm->sh_bang) {
563 char * passed_p;
565 if (interpreter_type == INTERPRETER_AOUT) {
566 sprintf(passed_fileno, "%d", elf_exec_fileno);
567 passed_p = passed_fileno;
569 if (elf_interpreter) {
570 retval = copy_strings_kernel(1,&passed_p,bprm);
571 if (retval)
572 goto out_free_dentry;
573 bprm->argc++;
578 /* Flush all traces of the currently running executable */
579 retval = flush_old_exec(bprm);
580 if (retval)
581 goto out_free_dentry;
583 /* OK, This is the point of no return */
584 current->mm->end_data = 0;
585 current->mm->end_code = 0;
586 current->mm->mmap = NULL;
587 current->flags &= ~PF_FORKNOEXEC;
588 elf_entry = (unsigned long) elf_ex.e_entry;
590 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
591 may depend on the personality. */
592 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
594 /* Do this so that we can load the interpreter, if need be. We will
595 change some of these later */
596 current->mm->rss = 0;
597 setup_arg_pages(bprm); /* XXX: check error */
598 current->mm->start_stack = bprm->p;
600 /* Try and get dynamic programs out of the way of the default mmap
601 base, as well as whatever program they might try to exec. This
602 is because the brk will follow the loader, and is not movable. */
604 load_bias = ELF_PAGESTART(elf_ex.e_type==ET_DYN ? ELF_ET_DYN_BASE : 0);
606 /* Now we do a little grungy work by mmaping the ELF image into
607 the correct location in memory. At this point, we assume that
608 the image should be loaded at fixed address, not at a variable
609 address. */
611 old_fs = get_fs();
612 set_fs(get_ds());
613 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
614 int elf_prot = 0, elf_flags;
615 unsigned long vaddr;
617 if (elf_ppnt->p_type != PT_LOAD)
618 continue;
620 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
621 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
622 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
624 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
626 vaddr = elf_ppnt->p_vaddr;
627 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
628 elf_flags |= MAP_FIXED;
631 error = do_mmap(file, ELF_PAGESTART(load_bias + vaddr),
632 (elf_ppnt->p_filesz +
633 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
634 elf_prot, elf_flags, (elf_ppnt->p_offset -
635 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
637 if (!load_addr_set) {
638 load_addr_set = 1;
639 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
640 if (elf_ex.e_type == ET_DYN) {
641 load_bias += error -
642 ELF_PAGESTART(load_bias + vaddr);
643 load_addr += error;
646 k = elf_ppnt->p_vaddr;
647 if (k < start_code) start_code = k;
648 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
649 if (k > elf_bss)
650 elf_bss = k;
651 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
652 end_code = k;
653 if (end_data < k)
654 end_data = k;
655 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
656 if (k > elf_brk)
657 elf_brk = k;
659 set_fs(old_fs);
660 fput(file); /* all done with the file */
662 elf_entry += load_bias;
663 elf_bss += load_bias;
664 elf_brk += load_bias;
665 start_code += load_bias;
666 end_code += load_bias;
667 end_data += load_bias;
669 if (elf_interpreter) {
670 if (interpreter_type == INTERPRETER_AOUT)
671 elf_entry = load_aout_interp(&interp_ex,
672 interpreter_dentry);
673 else
674 elf_entry = load_elf_interp(&interp_elf_ex,
675 interpreter_dentry,
676 &interp_load_addr);
678 dput(interpreter_dentry);
679 kfree(elf_interpreter);
681 if (elf_entry == ~0UL) {
682 printk(KERN_ERR "Unable to load interpreter\n");
683 kfree(elf_phdata);
684 send_sig(SIGSEGV, current, 0);
685 return 0;
689 kfree(elf_phdata);
691 if (interpreter_type != INTERPRETER_AOUT)
692 sys_close(elf_exec_fileno);
694 if (current->exec_domain && current->exec_domain->module)
695 __MOD_DEC_USE_COUNT(current->exec_domain->module);
696 if (current->binfmt && current->binfmt->module)
697 __MOD_DEC_USE_COUNT(current->binfmt->module);
698 current->exec_domain = lookup_exec_domain(current->personality);
699 current->binfmt = &elf_format;
700 if (current->exec_domain && current->exec_domain->module)
701 __MOD_INC_USE_COUNT(current->exec_domain->module);
702 if (current->binfmt && current->binfmt->module)
703 __MOD_INC_USE_COUNT(current->binfmt->module);
705 #ifndef VM_STACK_FLAGS
706 current->executable = dget(bprm->dentry);
707 #endif
708 compute_creds(bprm);
709 current->flags &= ~PF_FORKNOEXEC;
710 bprm->p = (unsigned long)
711 create_elf_tables((char *)bprm->p,
712 bprm->argc,
713 bprm->envc,
714 (interpreter_type == INTERPRETER_ELF ? &elf_ex : NULL),
715 load_addr, load_bias,
716 interp_load_addr,
717 (interpreter_type == INTERPRETER_AOUT ? 0 : 1));
718 /* N.B. passed_fileno might not be initialized? */
719 if (interpreter_type == INTERPRETER_AOUT)
720 current->mm->arg_start += strlen(passed_fileno) + 1;
721 current->mm->start_brk = current->mm->brk = elf_brk;
722 current->mm->end_code = end_code;
723 current->mm->start_code = start_code;
724 current->mm->end_data = end_data;
725 current->mm->start_stack = bprm->p;
727 /* Calling set_brk effectively mmaps the pages that we need
728 * for the bss and break sections
730 set_brk(elf_bss, elf_brk);
732 padzero(elf_bss);
734 #if 0
735 printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
736 printk("(end_code) %lx\n" , (long) current->mm->end_code);
737 printk("(start_code) %lx\n" , (long) current->mm->start_code);
738 printk("(end_data) %lx\n" , (long) current->mm->end_data);
739 printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
740 printk("(brk) %lx\n" , (long) current->mm->brk);
741 #endif
743 if ( current->personality == PER_SVR4 )
745 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
746 and some applications "depend" upon this behavior.
747 Since we do not have the power to recompile these, we
748 emulate the SVr4 behavior. Sigh. */
749 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
750 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
751 MAP_FIXED | MAP_PRIVATE, 0);
754 #ifdef ELF_PLAT_INIT
756 * The ABI may specify that certain registers be set up in special
757 * ways (on i386 %edx is the address of a DT_FINI function, for
758 * example. This macro performs whatever initialization to
759 * the regs structure is required.
761 ELF_PLAT_INIT(regs);
762 #endif
764 start_thread(regs, elf_entry, bprm->p);
765 if (current->flags & PF_PTRACED)
766 send_sig(SIGTRAP, current, 0);
767 retval = 0;
768 out:
769 return retval;
771 /* error cleanup */
772 out_free_dentry:
773 dput(interpreter_dentry);
774 out_free_interp:
775 if (elf_interpreter)
776 kfree(elf_interpreter);
777 out_free_file:
778 fput(file);
779 sys_close(elf_exec_fileno);
780 out_free_ph:
781 kfree(elf_phdata);
782 goto out;
785 static int
786 load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
788 int retval;
790 MOD_INC_USE_COUNT;
791 retval = do_load_elf_binary(bprm, regs);
792 MOD_DEC_USE_COUNT;
793 return retval;
796 /* This is really simpleminded and specialized - we are loading an
797 a.out library that is given an ELF header. */
799 static inline int
800 do_load_elf_library(int fd)
802 struct file * file;
803 struct dentry * dentry;
804 struct inode * inode;
805 struct elf_phdr *elf_phdata;
806 unsigned long elf_bss = 0, bss, len, k;
807 int retval, error, i, j;
808 struct elfhdr elf_ex;
809 loff_t offset = 0;
811 error = -EACCES;
812 file = fget(fd);
813 if (!file || !file->f_op)
814 goto out;
815 dentry = file->f_dentry;
816 inode = dentry->d_inode;
818 /* seek to the beginning of the file */
819 error = -ENOEXEC;
821 /* N.B. save current DS?? */
822 set_fs(KERNEL_DS);
823 retval = file->f_op->read(file, (char *) &elf_ex, sizeof(elf_ex), &offset);
824 set_fs(USER_DS);
825 if (retval != sizeof(elf_ex))
826 goto out_putf;
828 if (elf_ex.e_ident[0] != 0x7f ||
829 strncmp(&elf_ex.e_ident[1], "ELF", 3) != 0)
830 goto out_putf;
832 /* First of all, some simple consistency checks */
833 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
834 !elf_check_arch(elf_ex.e_machine) ||
835 (!inode->i_op || !inode->i_op->default_file_ops->mmap))
836 goto out_putf;
838 /* Now read in all of the header information */
840 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
841 if (j > ELF_EXEC_PAGESIZE)
842 goto out_putf;
844 error = -ENOMEM;
845 elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
846 if (!elf_phdata)
847 goto out_putf;
849 /* N.B. check for error return?? */
850 retval = read_exec(dentry, elf_ex.e_phoff, (char *) elf_phdata,
851 sizeof(struct elf_phdr) * elf_ex.e_phnum, 1);
853 error = -ENOEXEC;
854 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
855 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
856 if (j != 1)
857 goto out_free_ph;
859 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
861 /* Now use mmap to map the library into memory. */
862 error = do_mmap(file,
863 ELF_PAGESTART(elf_phdata->p_vaddr),
864 (elf_phdata->p_filesz +
865 ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
866 PROT_READ | PROT_WRITE | PROT_EXEC,
867 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
868 (elf_phdata->p_offset -
869 ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
870 if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
871 goto out_free_ph;
873 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
874 if (k > elf_bss)
875 elf_bss = k;
876 padzero(elf_bss);
878 len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr +
879 ELF_EXEC_PAGESIZE - 1);
880 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
881 if (bss > len)
882 do_brk(len, bss - len);
883 error = 0;
885 out_free_ph:
886 kfree(elf_phdata);
887 out_putf:
888 fput(file);
889 out:
890 return error;
893 static int load_elf_library(int fd)
895 int retval;
897 MOD_INC_USE_COUNT;
898 retval = do_load_elf_library(fd);
899 MOD_DEC_USE_COUNT;
900 return retval;
904 * Note that some platforms still use traditional core dumps and not
905 * the ELF core dump. Each platform can select it as appropriate.
907 #ifdef USE_ELF_CORE_DUMP
910 * ELF core dumper
912 * Modelled on fs/exec.c:aout_core_dump()
913 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
916 * These are the only things you should do on a core-file: use only these
917 * functions to write out all the necessary info.
919 static int dump_write(struct file *file, const void *addr, int nr)
921 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
924 static int dump_seek(struct file *file, off_t off)
926 if (file->f_op->llseek) {
927 if (file->f_op->llseek(file, off, 0) != off)
928 return 0;
929 } else
930 file->f_pos = off;
931 return 1;
935 * Decide whether a segment is worth dumping; default is yes to be
936 * sure (missing info is worse than too much; etc).
937 * Personally I'd include everything, and use the coredump limit...
939 * I think we should skip something. But I am not sure how. H.J.
941 static inline int maydump(struct vm_area_struct *vma)
943 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
944 return 0;
946 /* Do not dump I/O mapped devices! -DaveM */
947 if(vma->vm_flags & VM_IO)
948 return 0;
949 #if 1
950 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
951 return 1;
952 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
953 return 0;
954 #endif
955 return 1;
958 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
960 /* An ELF note in memory */
961 struct memelfnote
963 const char *name;
964 int type;
965 unsigned int datasz;
966 void *data;
969 static int notesize(struct memelfnote *en)
971 int sz;
973 sz = sizeof(struct elf_note);
974 sz += roundup(strlen(en->name), 4);
975 sz += roundup(en->datasz, 4);
977 return sz;
980 /* #define DEBUG */
982 #ifdef DEBUG
983 static void dump_regs(const char *str, elf_greg_t *r)
985 int i;
986 static const char *regs[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
987 "eax", "ds", "es", "fs", "gs",
988 "orig_eax", "eip", "cs",
989 "efl", "uesp", "ss"};
990 printk("Registers: %s\n", str);
992 for(i = 0; i < ELF_NGREG; i++)
994 unsigned long val = r[i];
995 printk(" %-2d %-5s=%08lx %lu\n", i, regs[i], val, val);
998 #endif
1000 #define DUMP_WRITE(addr, nr) \
1001 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1002 #define DUMP_SEEK(off) \
1003 do { if (!dump_seek(file, (off))) return 0; } while(0)
1005 static int writenote(struct memelfnote *men, struct file *file)
1007 struct elf_note en;
1009 en.n_namesz = strlen(men->name);
1010 en.n_descsz = men->datasz;
1011 en.n_type = men->type;
1013 DUMP_WRITE(&en, sizeof(en));
1014 DUMP_WRITE(men->name, en.n_namesz);
1015 /* XXX - cast from long long to long to avoid need for libgcc.a */
1016 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1017 DUMP_WRITE(men->data, men->datasz);
1018 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1020 return 1;
1022 #undef DUMP_WRITE
1023 #undef DUMP_SEEK
1025 #define DUMP_WRITE(addr, nr) \
1026 if (!dump_write(file, (addr), (nr))) \
1027 goto close_coredump;
1028 #define DUMP_SEEK(off) \
1029 if (!dump_seek(file, (off))) \
1030 goto close_coredump;
1032 * Actual dumper
1034 * This is a two-pass process; first we find the offsets of the bits,
1035 * and then they are actually written out. If we run out of core limit
1036 * we just truncate.
1038 static int elf_core_dump(long signr, struct pt_regs * regs)
1040 int has_dumped = 0;
1041 struct file *file;
1042 struct dentry *dentry;
1043 struct inode *inode;
1044 mm_segment_t fs;
1045 char corefile[6+sizeof(current->comm)];
1046 int segs;
1047 int i;
1048 size_t size;
1049 struct vm_area_struct *vma;
1050 struct elfhdr elf;
1051 off_t offset = 0, dataoff;
1052 unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1053 int numnote = 4;
1054 struct memelfnote notes[4];
1055 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1056 elf_fpregset_t fpu; /* NT_PRFPREG */
1057 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1059 if (!current->dumpable ||
1060 limit < ELF_EXEC_PAGESIZE ||
1061 atomic_read(&current->mm->count) != 1)
1062 return 0;
1063 current->dumpable = 0;
1065 #ifndef CONFIG_BINFMT_ELF
1066 MOD_INC_USE_COUNT;
1067 #endif
1069 /* Count what's needed to dump, up to the limit of coredump size */
1070 segs = 0;
1071 size = 0;
1072 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1073 if (maydump(vma))
1075 unsigned long sz = vma->vm_end-vma->vm_start;
1077 if (size+sz >= limit)
1078 break;
1079 else
1080 size += sz;
1083 segs++;
1085 #ifdef DEBUG
1086 printk("elf_core_dump: %d segs taking %d bytes\n", segs, size);
1087 #endif
1089 /* Set up header */
1090 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1091 elf.e_ident[EI_CLASS] = ELF_CLASS;
1092 elf.e_ident[EI_DATA] = ELF_DATA;
1093 elf.e_ident[EI_VERSION] = EV_CURRENT;
1094 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1096 elf.e_type = ET_CORE;
1097 elf.e_machine = ELF_ARCH;
1098 elf.e_version = EV_CURRENT;
1099 elf.e_entry = 0;
1100 elf.e_phoff = sizeof(elf);
1101 elf.e_shoff = 0;
1102 elf.e_flags = 0;
1103 elf.e_ehsize = sizeof(elf);
1104 elf.e_phentsize = sizeof(struct elf_phdr);
1105 elf.e_phnum = segs+1; /* Include notes */
1106 elf.e_shentsize = 0;
1107 elf.e_shnum = 0;
1108 elf.e_shstrndx = 0;
1110 fs = get_fs();
1111 set_fs(KERNEL_DS);
1113 memcpy(corefile,"core.",5);
1114 #if 0
1115 memcpy(corefile+5,current->comm,sizeof(current->comm));
1116 #else
1117 corefile[4] = '\0';
1118 #endif
1119 file = filp_open(corefile, O_CREAT | 2 | O_TRUNC | O_NOFOLLOW, 0600);
1120 if (IS_ERR(file))
1121 goto end_coredump;
1122 dentry = file->f_dentry;
1123 inode = dentry->d_inode;
1124 if (inode->i_nlink > 1)
1125 goto close_coredump; /* multiple links - don't dump */
1127 if (!S_ISREG(inode->i_mode))
1128 goto close_coredump;
1129 if (!inode->i_op || !inode->i_op->default_file_ops)
1130 goto close_coredump;
1131 if (!file->f_op->write)
1132 goto close_coredump;
1134 has_dumped = 1;
1135 current->flags |= PF_DUMPCORE;
1137 DUMP_WRITE(&elf, sizeof(elf));
1138 offset += sizeof(elf); /* Elf header */
1139 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1142 * Set up the notes in similar form to SVR4 core dumps made
1143 * with info from their /proc.
1145 memset(&psinfo, 0, sizeof(psinfo));
1146 memset(&prstatus, 0, sizeof(prstatus));
1148 notes[0].name = "CORE";
1149 notes[0].type = NT_PRSTATUS;
1150 notes[0].datasz = sizeof(prstatus);
1151 notes[0].data = &prstatus;
1152 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1153 prstatus.pr_sigpend = current->signal.sig[0];
1154 prstatus.pr_sighold = current->blocked.sig[0];
1155 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1156 psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1157 psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1158 psinfo.pr_sid = prstatus.pr_sid = current->session;
1159 prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
1160 prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
1161 prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
1162 prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
1163 prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
1164 prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
1165 prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
1166 prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
1169 * This transfers the registers from regs into the standard
1170 * coredump arrangement, whatever that is.
1172 #ifdef ELF_CORE_COPY_REGS
1173 ELF_CORE_COPY_REGS(prstatus.pr_reg, regs)
1174 #else
1175 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs))
1177 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1178 (long)sizeof(elf_gregset_t), (long)sizeof(struct pt_regs));
1180 else
1181 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1182 #endif
1184 #ifdef DEBUG
1185 dump_regs("Passed in regs", (elf_greg_t *)regs);
1186 dump_regs("prstatus regs", (elf_greg_t *)&prstatus.pr_reg);
1187 #endif
1189 notes[1].name = "CORE";
1190 notes[1].type = NT_PRPSINFO;
1191 notes[1].datasz = sizeof(psinfo);
1192 notes[1].data = &psinfo;
1193 i = current->state ? ffz(~current->state) + 1 : 0;
1194 psinfo.pr_state = i;
1195 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1196 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1197 psinfo.pr_nice = current->priority-15;
1198 psinfo.pr_flag = current->flags;
1199 psinfo.pr_uid = current->uid;
1200 psinfo.pr_gid = current->gid;
1202 int i, len;
1204 set_fs(fs);
1206 len = current->mm->arg_end - current->mm->arg_start;
1207 if (len >= ELF_PRARGSZ)
1208 len = ELF_PRARGSZ-1;
1209 copy_from_user(&psinfo.pr_psargs,
1210 (const char *)current->mm->arg_start, len);
1211 for(i = 0; i < len; i++)
1212 if (psinfo.pr_psargs[i] == 0)
1213 psinfo.pr_psargs[i] = ' ';
1214 psinfo.pr_psargs[len] = 0;
1216 set_fs(KERNEL_DS);
1218 strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1220 notes[2].name = "CORE";
1221 notes[2].type = NT_TASKSTRUCT;
1222 notes[2].datasz = sizeof(*current);
1223 notes[2].data = current;
1225 /* Try to dump the FPU. */
1226 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1227 if (!prstatus.pr_fpvalid)
1229 numnote--;
1231 else
1233 notes[3].name = "CORE";
1234 notes[3].type = NT_PRFPREG;
1235 notes[3].datasz = sizeof(fpu);
1236 notes[3].data = &fpu;
1239 /* Write notes phdr entry */
1241 struct elf_phdr phdr;
1242 int sz = 0;
1244 for(i = 0; i < numnote; i++)
1245 sz += notesize(&notes[i]);
1247 phdr.p_type = PT_NOTE;
1248 phdr.p_offset = offset;
1249 phdr.p_vaddr = 0;
1250 phdr.p_paddr = 0;
1251 phdr.p_filesz = sz;
1252 phdr.p_memsz = 0;
1253 phdr.p_flags = 0;
1254 phdr.p_align = 0;
1256 offset += phdr.p_filesz;
1257 DUMP_WRITE(&phdr, sizeof(phdr));
1260 /* Page-align dumped data */
1261 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1263 /* Write program headers for segments dump */
1264 for(vma = current->mm->mmap, i = 0;
1265 i < segs && vma != NULL; vma = vma->vm_next) {
1266 struct elf_phdr phdr;
1267 size_t sz;
1269 i++;
1271 sz = vma->vm_end - vma->vm_start;
1273 phdr.p_type = PT_LOAD;
1274 phdr.p_offset = offset;
1275 phdr.p_vaddr = vma->vm_start;
1276 phdr.p_paddr = 0;
1277 phdr.p_filesz = maydump(vma) ? sz : 0;
1278 phdr.p_memsz = sz;
1279 offset += phdr.p_filesz;
1280 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1281 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1282 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1283 phdr.p_align = ELF_EXEC_PAGESIZE;
1285 DUMP_WRITE(&phdr, sizeof(phdr));
1288 for(i = 0; i < numnote; i++)
1289 if (!writenote(&notes[i], file))
1290 goto close_coredump;
1292 set_fs(fs);
1294 DUMP_SEEK(dataoff);
1296 for(i = 0, vma = current->mm->mmap;
1297 i < segs && vma != NULL;
1298 vma = vma->vm_next) {
1299 unsigned long addr = vma->vm_start;
1300 unsigned long len = vma->vm_end - vma->vm_start;
1302 i++;
1303 if (!maydump(vma))
1304 continue;
1305 #ifdef DEBUG
1306 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1307 #endif
1308 DUMP_WRITE((void *)addr, len);
1311 if ((off_t) file->f_pos != offset) {
1312 /* Sanity check */
1313 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1314 (off_t) file->f_pos, offset);
1317 close_coredump:
1318 filp_close(file, NULL);
1320 end_coredump:
1321 set_fs(fs);
1322 #ifndef CONFIG_BINFMT_ELF
1323 MOD_DEC_USE_COUNT;
1324 #endif
1325 return has_dumped;
1327 #endif /* USE_ELF_CORE_DUMP */
1329 int __init init_elf_binfmt(void)
1331 return register_binfmt(&elf_format);
1334 #ifdef MODULE
1336 int init_module(void)
1338 /* Install the COFF, ELF and XOUT loaders.
1339 * N.B. We *rely* on the table being the right size with the
1340 * right number of free slots...
1342 return init_elf_binfmt();
1346 void cleanup_module( void)
1348 /* Remove the COFF and ELF loaders. */
1349 unregister_binfmt(&elf_format);
1351 #endif