Import 2.3.49pre2
[davej-history.git] / fs / binfmt_elf.c
blobec16d8fa5c38eef958afceade5c3595407c519d6
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 #include <linux/config.h>
40 #define DLINFO_ITEMS 13
42 #include <linux/elf.h>
44 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs);
45 static int load_elf_library(int fd);
46 extern int dump_fpu (struct pt_regs *, elf_fpregset_t *);
47 extern void dump_thread(struct pt_regs *, struct user *);
49 #ifndef elf_addr_t
50 #define elf_addr_t unsigned long
51 #define elf_caddr_t char *
52 #endif
55 * If we don't support core dumping, then supply a NULL so we
56 * don't even try.
58 #ifdef USE_ELF_CORE_DUMP
59 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file);
60 #else
61 #define elf_core_dump NULL
62 #endif
64 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_EXEC_PAGESIZE-1))
65 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_EXEC_PAGESIZE-1))
66 #define ELF_PAGEALIGN(_v) (((_v) + ELF_EXEC_PAGESIZE - 1) & ~(ELF_EXEC_PAGESIZE - 1))
68 static struct linux_binfmt elf_format = {
69 NULL, THIS_MODULE, load_elf_binary, load_elf_library, elf_core_dump, ELF_EXEC_PAGESIZE
72 static void set_brk(unsigned long start, unsigned long end)
74 start = ELF_PAGEALIGN(start);
75 end = ELF_PAGEALIGN(end);
76 if (end <= start)
77 return;
78 do_brk(start, end - start);
82 /* We need to explicitly zero any fractional pages
83 after the data section (i.e. bss). This would
84 contain the junk from the file that should not
85 be in memory */
88 static void padzero(unsigned long elf_bss)
90 unsigned long nbyte;
92 nbyte = ELF_PAGEOFFSET(elf_bss);
93 if (nbyte) {
94 nbyte = ELF_EXEC_PAGESIZE - nbyte;
95 clear_user((void *) elf_bss, nbyte);
99 static elf_addr_t *
100 create_elf_tables(char *p, int argc, int envc,
101 struct elfhdr * exec,
102 unsigned long load_addr,
103 unsigned long load_bias,
104 unsigned long interp_load_addr, int ibcs)
106 elf_caddr_t *argv;
107 elf_caddr_t *envp;
108 elf_addr_t *sp, *csp;
109 char *k_platform, *u_platform;
110 long hwcap;
111 size_t platform_len = 0;
114 * Get hold of platform and hardware capabilities masks for
115 * the machine we are running on. In some cases (Sparc),
116 * this info is impossible to get, in others (i386) it is
117 * merely difficult.
120 hwcap = ELF_HWCAP;
121 k_platform = ELF_PLATFORM;
123 if (k_platform) {
124 platform_len = strlen(k_platform) + 1;
125 u_platform = p - platform_len;
126 __copy_to_user(u_platform, k_platform, platform_len);
127 } else
128 u_platform = p;
131 * Force 16 byte _final_ alignment here for generality.
132 * Leave an extra 16 bytes free so that on the PowerPC we
133 * can move the aux table up to start on a 16-byte boundary.
135 sp = (elf_addr_t *)((~15UL & (unsigned long)(u_platform)) - 16UL);
136 csp = sp;
137 csp -= ((exec ? DLINFO_ITEMS*2 : 4) + (k_platform ? 2 : 0));
138 csp -= envc+1;
139 csp -= argc+1;
140 csp -= (!ibcs ? 3 : 1); /* argc itself */
141 if ((unsigned long)csp & 15UL)
142 sp -= ((unsigned long)csp & 15UL) / sizeof(*sp);
145 * Put the ELF interpreter info on the stack
147 #define NEW_AUX_ENT(nr, id, val) \
148 __put_user ((id), sp+(nr*2)); \
149 __put_user ((val), sp+(nr*2+1)); \
151 sp -= 2;
152 NEW_AUX_ENT(0, AT_NULL, 0);
153 if (k_platform) {
154 sp -= 2;
155 NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
157 sp -= 2;
158 NEW_AUX_ENT(0, AT_HWCAP, hwcap);
160 if (exec) {
161 sp -= 11*2;
163 NEW_AUX_ENT(0, AT_PHDR, load_addr + exec->e_phoff);
164 NEW_AUX_ENT(1, AT_PHENT, sizeof (struct elf_phdr));
165 NEW_AUX_ENT(2, AT_PHNUM, exec->e_phnum);
166 NEW_AUX_ENT(3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
167 NEW_AUX_ENT(4, AT_BASE, interp_load_addr);
168 NEW_AUX_ENT(5, AT_FLAGS, 0);
169 NEW_AUX_ENT(6, AT_ENTRY, load_bias + exec->e_entry);
170 NEW_AUX_ENT(7, AT_UID, (elf_addr_t) current->uid);
171 NEW_AUX_ENT(8, AT_EUID, (elf_addr_t) current->euid);
172 NEW_AUX_ENT(9, AT_GID, (elf_addr_t) current->gid);
173 NEW_AUX_ENT(10, AT_EGID, (elf_addr_t) current->egid);
175 #undef NEW_AUX_ENT
177 sp -= envc+1;
178 envp = (elf_caddr_t *) sp;
179 sp -= argc+1;
180 argv = (elf_caddr_t *) sp;
181 if (!ibcs) {
182 __put_user((elf_addr_t)(unsigned long) envp,--sp);
183 __put_user((elf_addr_t)(unsigned long) argv,--sp);
186 __put_user((elf_addr_t)argc,--sp);
187 current->mm->arg_start = (unsigned long) p;
188 while (argc-->0) {
189 __put_user((elf_caddr_t)(unsigned long)p,argv++);
190 p += strlen_user(p);
192 __put_user(NULL, argv);
193 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
194 while (envc-->0) {
195 __put_user((elf_caddr_t)(unsigned long)p,envp++);
196 p += strlen_user(p);
198 __put_user(NULL, envp);
199 current->mm->env_end = (unsigned long) p;
200 return sp;
204 /* This is much more generalized than the library routine read function,
205 so we keep this separate. Technically the library read function
206 is only provided so that we can read a.out libraries that have
207 an ELF header */
209 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
210 struct dentry * interpreter_dentry,
211 unsigned long *interp_load_addr)
213 struct file * file;
214 struct elf_phdr *elf_phdata;
215 struct elf_phdr *eppnt;
216 unsigned long load_addr = 0;
217 int load_addr_set = 0;
218 unsigned long last_bss = 0, elf_bss = 0;
219 unsigned long error = ~0UL;
220 int elf_exec_fileno;
221 int retval, i, size;
223 /* First of all, some simple consistency checks */
224 if (interp_elf_ex->e_type != ET_EXEC &&
225 interp_elf_ex->e_type != ET_DYN)
226 goto out;
227 if (!elf_check_arch(interp_elf_ex->e_machine))
228 goto out;
229 if (!interpreter_dentry->d_inode->i_fop ||
230 !interpreter_dentry->d_inode->i_fop->mmap)
231 goto out;
234 * If the size of this structure has changed, then punt, since
235 * we will be doing the wrong thing.
237 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
238 goto out;
240 /* Now read in all of the header information */
242 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
243 if (size > ELF_EXEC_PAGESIZE)
244 goto out;
245 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
246 if (!elf_phdata)
247 goto out;
249 retval = read_exec(interpreter_dentry, interp_elf_ex->e_phoff,
250 (char *) elf_phdata, size, 1);
251 error = retval;
252 if (retval < 0)
253 goto out_free;
255 error = ~0UL;
256 elf_exec_fileno = open_dentry(interpreter_dentry, O_RDONLY);
257 if (elf_exec_fileno < 0)
258 goto out_free;
259 file = fget(elf_exec_fileno);
261 eppnt = elf_phdata;
262 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
263 if (eppnt->p_type == PT_LOAD) {
264 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
265 int elf_prot = 0;
266 unsigned long vaddr = 0;
267 unsigned long k, map_addr;
269 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
270 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
271 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
272 vaddr = eppnt->p_vaddr;
273 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
274 elf_type |= MAP_FIXED;
276 map_addr = do_mmap(file,
277 load_addr + ELF_PAGESTART(vaddr),
278 eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr),
279 elf_prot,
280 elf_type,
281 eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
282 if (map_addr > -1024UL) /* Real error */
283 goto out_close;
285 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
286 load_addr = map_addr - ELF_PAGESTART(vaddr);
287 load_addr_set = 1;
291 * Find the end of the file mapping for this phdr, and keep
292 * track of the largest address we see for this.
294 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
295 if (k > elf_bss)
296 elf_bss = k;
299 * Do the same thing for the memory mapping - between
300 * elf_bss and last_bss is the bss section.
302 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
303 if (k > last_bss)
304 last_bss = k;
308 /* Now use mmap to map the library into memory. */
311 * Now fill out the bss section. First pad the last page up
312 * to the page boundary, and then perform a mmap to make sure
313 * that there are zero-mapped pages up to and including the
314 * last bss page.
316 padzero(elf_bss);
317 elf_bss = ELF_PAGESTART(elf_bss + ELF_EXEC_PAGESIZE - 1); /* What we have mapped so far */
319 /* Map the last of the bss segment */
320 if (last_bss > elf_bss)
321 do_brk(elf_bss, last_bss - elf_bss);
323 *interp_load_addr = load_addr;
324 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
326 out_close:
327 fput(file);
328 sys_close(elf_exec_fileno);
329 out_free:
330 kfree(elf_phdata);
331 out:
332 return error;
335 static unsigned long load_aout_interp(struct exec * interp_ex,
336 struct dentry * interpreter_dentry)
338 unsigned long text_data, offset, elf_entry = ~0UL;
339 char * addr;
340 int retval;
342 current->mm->end_code = interp_ex->a_text;
343 text_data = interp_ex->a_text + interp_ex->a_data;
344 current->mm->end_data = text_data;
345 current->mm->brk = interp_ex->a_bss + text_data;
347 switch (N_MAGIC(*interp_ex)) {
348 case OMAGIC:
349 offset = 32;
350 addr = (char *) 0;
351 break;
352 case ZMAGIC:
353 case QMAGIC:
354 offset = N_TXTOFF(*interp_ex);
355 addr = (char *) N_TXTADDR(*interp_ex);
356 break;
357 default:
358 goto out;
361 do_brk(0, text_data);
362 retval = read_exec(interpreter_dentry, offset, addr, text_data, 0);
363 if (retval < 0)
364 goto out;
365 flush_icache_range((unsigned long)addr,
366 (unsigned long)addr + text_data);
368 do_brk(ELF_PAGESTART(text_data + ELF_EXEC_PAGESIZE - 1),
369 interp_ex->a_bss);
370 elf_entry = interp_ex->a_entry;
372 out:
373 return elf_entry;
377 * These are the functions used to load ELF style executables and shared
378 * libraries. There is no binary dependent code anywhere else.
381 #define INTERPRETER_NONE 0
382 #define INTERPRETER_AOUT 1
383 #define INTERPRETER_ELF 2
386 static inline int
387 do_load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
389 struct file * file;
390 struct dentry *interpreter_dentry = NULL; /* to shut gcc up */
391 unsigned long load_addr = 0, load_bias;
392 int load_addr_set = 0;
393 char * elf_interpreter = NULL;
394 unsigned int interpreter_type = INTERPRETER_NONE;
395 unsigned char ibcs2_interpreter = 0;
396 mm_segment_t old_fs;
397 unsigned long error;
398 struct elf_phdr * elf_ppnt, *elf_phdata;
399 unsigned long elf_bss, k, elf_brk;
400 int elf_exec_fileno;
401 int retval, size, i;
402 unsigned long elf_entry, interp_load_addr = 0;
403 unsigned long start_code, end_code, start_data, end_data;
404 struct elfhdr elf_ex;
405 struct elfhdr interp_elf_ex;
406 struct exec interp_ex;
407 char passed_fileno[6];
409 /* Get the exec-header */
410 elf_ex = *((struct elfhdr *) bprm->buf);
412 retval = -ENOEXEC;
413 /* First of all, some simple consistency checks */
414 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
415 goto out;
417 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
418 goto out;
419 if (!elf_check_arch(elf_ex.e_machine))
420 goto out;
421 #ifdef __mips__
422 /* IRIX binaries handled elsewhere. */
423 if (elf_ex.e_flags & EF_MIPS_ARCH) {
424 retval = -ENOEXEC;
425 goto out;
427 #endif
428 if (!bprm->dentry->d_inode->i_fop||!bprm->dentry->d_inode->i_fop->mmap)
429 goto out;
431 /* Now read in all of the header information */
433 retval = -ENOMEM;
434 size = elf_ex.e_phentsize * elf_ex.e_phnum;
435 if (size > 65536)
436 goto out;
437 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
438 if (!elf_phdata)
439 goto out;
441 retval = read_exec(bprm->dentry, elf_ex.e_phoff,
442 (char *) elf_phdata, size, 1);
443 if (retval < 0)
444 goto out_free_ph;
446 retval = open_dentry(bprm->dentry, O_RDONLY);
447 if (retval < 0)
448 goto out_free_ph;
449 elf_exec_fileno = retval;
450 file = fget(elf_exec_fileno);
452 elf_ppnt = elf_phdata;
453 elf_bss = 0;
454 elf_brk = 0;
456 start_code = ~0UL;
457 end_code = 0;
458 start_data = 0;
459 end_data = 0;
461 for (i = 0; i < elf_ex.e_phnum; i++) {
462 if (elf_ppnt->p_type == PT_INTERP) {
463 retval = -EINVAL;
464 if (elf_interpreter)
465 goto out_free_interp;
467 /* This is the program interpreter used for
468 * shared libraries - for now assume that this
469 * is an a.out format binary
472 retval = -ENOMEM;
473 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
474 GFP_KERNEL);
475 if (!elf_interpreter)
476 goto out_free_file;
478 retval = read_exec(bprm->dentry, elf_ppnt->p_offset,
479 elf_interpreter,
480 elf_ppnt->p_filesz, 1);
481 if (retval < 0)
482 goto out_free_interp;
483 /* If the program interpreter is one of these two,
484 * then assume an iBCS2 image. Otherwise assume
485 * a native linux image.
487 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
488 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
489 ibcs2_interpreter = 1;
490 #if 0
491 printk("Using ELF interpreter %s\n", elf_interpreter);
492 #endif
493 old_fs = get_fs(); /* This could probably be optimized */
494 set_fs(get_ds());
495 #ifdef __sparc__
496 if (ibcs2_interpreter) {
497 unsigned long old_pers = current->personality;
499 current->personality = PER_SVR4;
500 lock_kernel();
501 interpreter_dentry = open_namei(elf_interpreter,
502 0, 0);
503 unlock_kernel();
504 current->personality = old_pers;
505 } else
506 #endif
508 lock_kernel();
509 interpreter_dentry = open_namei(elf_interpreter,
510 0, 0);
511 unlock_kernel();
513 set_fs(old_fs);
514 retval = PTR_ERR(interpreter_dentry);
515 if (IS_ERR(interpreter_dentry))
516 goto out_free_interp;
517 retval = permission(interpreter_dentry->d_inode, MAY_EXEC);
518 if (retval < 0)
519 goto out_free_dentry;
520 retval = read_exec(interpreter_dentry, 0, bprm->buf, 128, 1);
521 if (retval < 0)
522 goto out_free_dentry;
524 /* Get the exec headers */
525 interp_ex = *((struct exec *) bprm->buf);
526 interp_elf_ex = *((struct elfhdr *) bprm->buf);
528 elf_ppnt++;
531 /* Some simple consistency checks for the interpreter */
532 if (elf_interpreter) {
533 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
535 /* Now figure out which format our binary is */
536 if ((N_MAGIC(interp_ex) != OMAGIC) &&
537 (N_MAGIC(interp_ex) != ZMAGIC) &&
538 (N_MAGIC(interp_ex) != QMAGIC))
539 interpreter_type = INTERPRETER_ELF;
541 if (memcmp(interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
542 interpreter_type &= ~INTERPRETER_ELF;
544 retval = -ELIBBAD;
545 if (!interpreter_type)
546 goto out_free_dentry;
548 /* Make sure only one type was selected */
549 if ((interpreter_type & INTERPRETER_ELF) &&
550 interpreter_type != INTERPRETER_ELF) {
551 printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
552 interpreter_type = INTERPRETER_ELF;
556 /* OK, we are done with that, now set up the arg stuff,
557 and then start this sucker up */
559 if (!bprm->sh_bang) {
560 char * passed_p;
562 if (interpreter_type == INTERPRETER_AOUT) {
563 sprintf(passed_fileno, "%d", elf_exec_fileno);
564 passed_p = passed_fileno;
566 if (elf_interpreter) {
567 retval = copy_strings_kernel(1,&passed_p,bprm);
568 if (retval)
569 goto out_free_dentry;
570 bprm->argc++;
575 /* Flush all traces of the currently running executable */
576 retval = flush_old_exec(bprm);
577 if (retval)
578 goto out_free_dentry;
580 /* OK, This is the point of no return */
581 current->mm->start_data = 0;
582 current->mm->end_data = 0;
583 current->mm->end_code = 0;
584 current->mm->mmap = NULL;
585 current->flags &= ~PF_FORKNOEXEC;
586 elf_entry = (unsigned long) elf_ex.e_entry;
588 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
589 may depend on the personality. */
590 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
592 /* Do this so that we can load the interpreter, if need be. We will
593 change some of these later */
594 current->mm->rss = 0;
595 setup_arg_pages(bprm); /* XXX: check error */
596 current->mm->start_stack = bprm->p;
598 /* Try and get dynamic programs out of the way of the default mmap
599 base, as well as whatever program they might try to exec. This
600 is because the brk will follow the loader, and is not movable. */
602 load_bias = ELF_PAGESTART(elf_ex.e_type==ET_DYN ? ELF_ET_DYN_BASE : 0);
604 /* Now we do a little grungy work by mmaping the ELF image into
605 the correct location in memory. At this point, we assume that
606 the image should be loaded at fixed address, not at a variable
607 address. */
609 old_fs = get_fs();
610 set_fs(get_ds());
611 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
612 int elf_prot = 0, elf_flags;
613 unsigned long vaddr;
615 if (elf_ppnt->p_type != PT_LOAD)
616 continue;
618 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
619 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
620 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
622 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
624 vaddr = elf_ppnt->p_vaddr;
625 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
626 elf_flags |= MAP_FIXED;
629 error = do_mmap(file, ELF_PAGESTART(load_bias + vaddr),
630 (elf_ppnt->p_filesz +
631 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
632 elf_prot, elf_flags, (elf_ppnt->p_offset -
633 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
635 if (!load_addr_set) {
636 load_addr_set = 1;
637 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
638 if (elf_ex.e_type == ET_DYN) {
639 load_bias += error -
640 ELF_PAGESTART(load_bias + vaddr);
641 load_addr += error;
644 k = elf_ppnt->p_vaddr;
645 if (k < start_code) start_code = k;
646 if (start_data < k) start_data = k;
648 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
650 if (k > elf_bss)
651 elf_bss = k;
652 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
653 end_code = k;
654 if (end_data < k)
655 end_data = k;
656 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
657 if (k > elf_brk)
658 elf_brk = k;
660 set_fs(old_fs);
661 fput(file); /* all done with the file */
663 elf_entry += load_bias;
664 elf_bss += load_bias;
665 elf_brk += load_bias;
666 start_code += load_bias;
667 end_code += load_bias;
668 start_data += load_bias;
669 end_data += load_bias;
671 if (elf_interpreter) {
672 if (interpreter_type == INTERPRETER_AOUT)
673 elf_entry = load_aout_interp(&interp_ex,
674 interpreter_dentry);
675 else
676 elf_entry = load_elf_interp(&interp_elf_ex,
677 interpreter_dentry,
678 &interp_load_addr);
680 lock_kernel();
681 dput(interpreter_dentry);
682 unlock_kernel();
683 kfree(elf_interpreter);
685 if (elf_entry == ~0UL) {
686 printk(KERN_ERR "Unable to load interpreter\n");
687 kfree(elf_phdata);
688 send_sig(SIGSEGV, current, 0);
689 return 0;
693 kfree(elf_phdata);
695 if (interpreter_type != INTERPRETER_AOUT)
696 sys_close(elf_exec_fileno);
698 if (current->exec_domain && current->exec_domain->module)
699 __MOD_DEC_USE_COUNT(current->exec_domain->module);
700 if (current->binfmt && current->binfmt->module)
701 __MOD_DEC_USE_COUNT(current->binfmt->module);
702 current->exec_domain = lookup_exec_domain(current->personality);
703 current->binfmt = &elf_format;
704 if (current->exec_domain && current->exec_domain->module)
705 __MOD_INC_USE_COUNT(current->exec_domain->module);
706 if (current->binfmt && current->binfmt->module)
707 __MOD_INC_USE_COUNT(current->binfmt->module);
709 #ifndef VM_STACK_FLAGS
710 current->executable = dget(bprm->dentry);
711 #endif
712 compute_creds(bprm);
713 current->flags &= ~PF_FORKNOEXEC;
714 bprm->p = (unsigned long)
715 create_elf_tables((char *)bprm->p,
716 bprm->argc,
717 bprm->envc,
718 (interpreter_type == INTERPRETER_ELF ? &elf_ex : NULL),
719 load_addr, load_bias,
720 interp_load_addr,
721 (interpreter_type == INTERPRETER_AOUT ? 0 : 1));
722 /* N.B. passed_fileno might not be initialized? */
723 if (interpreter_type == INTERPRETER_AOUT)
724 current->mm->arg_start += strlen(passed_fileno) + 1;
725 current->mm->start_brk = current->mm->brk = elf_brk;
726 current->mm->end_code = end_code;
727 current->mm->start_code = start_code;
728 current->mm->start_data = start_data;
729 current->mm->end_data = end_data;
730 current->mm->start_stack = bprm->p;
732 /* Calling set_brk effectively mmaps the pages that we need
733 * for the bss and break sections
735 set_brk(elf_bss, elf_brk);
737 padzero(elf_bss);
739 #if 0
740 printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
741 printk("(end_code) %lx\n" , (long) current->mm->end_code);
742 printk("(start_code) %lx\n" , (long) current->mm->start_code);
743 printk("(start_data) %lx\n" , (long) current->mm->start_data);
744 printk("(end_data) %lx\n" , (long) current->mm->end_data);
745 printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
746 printk("(brk) %lx\n" , (long) current->mm->brk);
747 #endif
749 if ( current->personality == PER_SVR4 )
751 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
752 and some applications "depend" upon this behavior.
753 Since we do not have the power to recompile these, we
754 emulate the SVr4 behavior. Sigh. */
755 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
756 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
757 MAP_FIXED | MAP_PRIVATE, 0);
760 #ifdef ELF_PLAT_INIT
762 * The ABI may specify that certain registers be set up in special
763 * ways (on i386 %edx is the address of a DT_FINI function, for
764 * example. This macro performs whatever initialization to
765 * the regs structure is required.
767 ELF_PLAT_INIT(regs);
768 #endif
770 start_thread(regs, elf_entry, bprm->p);
771 if (current->flags & PF_PTRACED)
772 send_sig(SIGTRAP, current, 0);
773 retval = 0;
774 out:
775 return retval;
777 /* error cleanup */
778 out_free_dentry:
779 lock_kernel();
780 dput(interpreter_dentry);
781 unlock_kernel();
782 out_free_interp:
783 if (elf_interpreter)
784 kfree(elf_interpreter);
785 out_free_file:
786 fput(file);
787 sys_close(elf_exec_fileno);
788 out_free_ph:
789 kfree(elf_phdata);
790 goto out;
793 static int
794 load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
796 int retval;
798 MOD_INC_USE_COUNT;
799 retval = do_load_elf_binary(bprm, regs);
800 MOD_DEC_USE_COUNT;
801 return retval;
804 /* This is really simpleminded and specialized - we are loading an
805 a.out library that is given an ELF header. */
807 static inline int
808 do_load_elf_library(int fd)
810 struct file * file;
811 struct dentry * dentry;
812 struct inode * inode;
813 struct elf_phdr *elf_phdata;
814 unsigned long elf_bss = 0, bss, len, k;
815 int retval, error, i, j;
816 struct elfhdr elf_ex;
817 loff_t offset = 0;
819 error = -EACCES;
820 file = fget(fd);
821 if (!file || !file->f_op)
822 goto out;
823 dentry = file->f_dentry;
824 inode = dentry->d_inode;
826 /* seek to the beginning of the file */
827 error = -ENOEXEC;
829 /* N.B. save current DS?? */
830 set_fs(KERNEL_DS);
831 retval = file->f_op->read(file, (char *) &elf_ex, sizeof(elf_ex), &offset);
832 set_fs(USER_DS);
833 if (retval != sizeof(elf_ex))
834 goto out_putf;
836 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
837 goto out_putf;
839 /* First of all, some simple consistency checks */
840 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
841 !elf_check_arch(elf_ex.e_machine) ||
842 (!inode->i_fop || !inode->i_fop->mmap))
843 goto out_putf;
845 /* Now read in all of the header information */
847 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
848 if (j > ELF_EXEC_PAGESIZE)
849 goto out_putf;
851 error = -ENOMEM;
852 elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
853 if (!elf_phdata)
854 goto out_putf;
856 /* N.B. check for error return?? */
857 retval = read_exec(dentry, elf_ex.e_phoff, (char *) elf_phdata,
858 sizeof(struct elf_phdr) * elf_ex.e_phnum, 1);
860 error = -ENOEXEC;
861 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
862 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
863 if (j != 1)
864 goto out_free_ph;
866 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
868 /* Now use mmap to map the library into memory. */
869 error = do_mmap(file,
870 ELF_PAGESTART(elf_phdata->p_vaddr),
871 (elf_phdata->p_filesz +
872 ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
873 PROT_READ | PROT_WRITE | PROT_EXEC,
874 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
875 (elf_phdata->p_offset -
876 ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
877 if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
878 goto out_free_ph;
880 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
881 if (k > elf_bss)
882 elf_bss = k;
883 padzero(elf_bss);
885 len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr +
886 ELF_EXEC_PAGESIZE - 1);
887 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
888 if (bss > len)
889 do_brk(len, bss - len);
890 error = 0;
892 out_free_ph:
893 kfree(elf_phdata);
894 out_putf:
895 fput(file);
896 out:
897 return error;
900 static int load_elf_library(int fd)
902 int retval;
904 MOD_INC_USE_COUNT;
905 retval = do_load_elf_library(fd);
906 MOD_DEC_USE_COUNT;
907 return retval;
911 * Note that some platforms still use traditional core dumps and not
912 * the ELF core dump. Each platform can select it as appropriate.
914 #ifdef USE_ELF_CORE_DUMP
917 * ELF core dumper
919 * Modelled on fs/exec.c:aout_core_dump()
920 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
923 * These are the only things you should do on a core-file: use only these
924 * functions to write out all the necessary info.
926 static int dump_write(struct file *file, const void *addr, int nr)
928 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
931 static int dump_seek(struct file *file, off_t off)
933 if (file->f_op->llseek) {
934 if (file->f_op->llseek(file, off, 0) != off)
935 return 0;
936 } else
937 file->f_pos = off;
938 return 1;
942 * Decide whether a segment is worth dumping; default is yes to be
943 * sure (missing info is worse than too much; etc).
944 * Personally I'd include everything, and use the coredump limit...
946 * I think we should skip something. But I am not sure how. H.J.
948 static inline int maydump(struct vm_area_struct *vma)
950 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
951 return 0;
953 /* Do not dump I/O mapped devices! -DaveM */
954 if(vma->vm_flags & VM_IO)
955 return 0;
956 #if 1
957 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
958 return 1;
959 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
960 return 0;
961 #endif
962 return 1;
965 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
967 /* An ELF note in memory */
968 struct memelfnote
970 const char *name;
971 int type;
972 unsigned int datasz;
973 void *data;
976 static int notesize(struct memelfnote *en)
978 int sz;
980 sz = sizeof(struct elf_note);
981 sz += roundup(strlen(en->name), 4);
982 sz += roundup(en->datasz, 4);
984 return sz;
987 /* #define DEBUG */
989 #ifdef DEBUG
990 static void dump_regs(const char *str, elf_greg_t *r)
992 int i;
993 static const char *regs[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
994 "eax", "ds", "es", "fs", "gs",
995 "orig_eax", "eip", "cs",
996 "efl", "uesp", "ss"};
997 printk("Registers: %s\n", str);
999 for(i = 0; i < ELF_NGREG; i++)
1001 unsigned long val = r[i];
1002 printk(" %-2d %-5s=%08lx %lu\n", i, regs[i], val, val);
1005 #endif
1007 #define DUMP_WRITE(addr, nr) \
1008 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1009 #define DUMP_SEEK(off) \
1010 do { if (!dump_seek(file, (off))) return 0; } while(0)
1012 static int writenote(struct memelfnote *men, struct file *file)
1014 struct elf_note en;
1016 en.n_namesz = strlen(men->name);
1017 en.n_descsz = men->datasz;
1018 en.n_type = men->type;
1020 DUMP_WRITE(&en, sizeof(en));
1021 DUMP_WRITE(men->name, en.n_namesz);
1022 /* XXX - cast from long long to long to avoid need for libgcc.a */
1023 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1024 DUMP_WRITE(men->data, men->datasz);
1025 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1027 return 1;
1029 #undef DUMP_WRITE
1030 #undef DUMP_SEEK
1032 #define DUMP_WRITE(addr, nr) \
1033 if (!dump_write(file, (addr), (nr))) \
1034 goto end_coredump;
1035 #define DUMP_SEEK(off) \
1036 if (!dump_seek(file, (off))) \
1037 goto end_coredump;
1039 * Actual dumper
1041 * This is a two-pass process; first we find the offsets of the bits,
1042 * and then they are actually written out. If we run out of core limit
1043 * we just truncate.
1045 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
1047 int has_dumped = 0;
1048 mm_segment_t fs;
1049 int segs;
1050 int i;
1051 size_t size;
1052 struct vm_area_struct *vma;
1053 struct elfhdr elf;
1054 off_t offset = 0, dataoff;
1055 unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1056 int numnote = 4;
1057 struct memelfnote notes[4];
1058 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1059 elf_fpregset_t fpu; /* NT_PRFPREG */
1060 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1062 #ifndef CONFIG_BINFMT_ELF
1063 MOD_INC_USE_COUNT;
1064 #endif
1066 /* Count what's needed to dump, up to the limit of coredump size */
1067 segs = 0;
1068 size = 0;
1069 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1070 if (maydump(vma))
1072 unsigned long sz = vma->vm_end-vma->vm_start;
1074 if (size+sz >= limit)
1075 break;
1076 else
1077 size += sz;
1080 segs++;
1082 #ifdef DEBUG
1083 printk("elf_core_dump: %d segs taking %d bytes\n", segs, size);
1084 #endif
1086 /* Set up header */
1087 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1088 elf.e_ident[EI_CLASS] = ELF_CLASS;
1089 elf.e_ident[EI_DATA] = ELF_DATA;
1090 elf.e_ident[EI_VERSION] = EV_CURRENT;
1091 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1093 elf.e_type = ET_CORE;
1094 elf.e_machine = ELF_ARCH;
1095 elf.e_version = EV_CURRENT;
1096 elf.e_entry = 0;
1097 elf.e_phoff = sizeof(elf);
1098 elf.e_shoff = 0;
1099 elf.e_flags = 0;
1100 elf.e_ehsize = sizeof(elf);
1101 elf.e_phentsize = sizeof(struct elf_phdr);
1102 elf.e_phnum = segs+1; /* Include notes */
1103 elf.e_shentsize = 0;
1104 elf.e_shnum = 0;
1105 elf.e_shstrndx = 0;
1107 fs = get_fs();
1108 set_fs(KERNEL_DS);
1110 has_dumped = 1;
1111 current->flags |= PF_DUMPCORE;
1113 DUMP_WRITE(&elf, sizeof(elf));
1114 offset += sizeof(elf); /* Elf header */
1115 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1118 * Set up the notes in similar form to SVR4 core dumps made
1119 * with info from their /proc.
1121 memset(&psinfo, 0, sizeof(psinfo));
1122 memset(&prstatus, 0, sizeof(prstatus));
1124 notes[0].name = "CORE";
1125 notes[0].type = NT_PRSTATUS;
1126 notes[0].datasz = sizeof(prstatus);
1127 notes[0].data = &prstatus;
1128 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1129 prstatus.pr_sigpend = current->signal.sig[0];
1130 prstatus.pr_sighold = current->blocked.sig[0];
1131 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1132 psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1133 psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1134 psinfo.pr_sid = prstatus.pr_sid = current->session;
1135 prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
1136 prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
1137 prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
1138 prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
1139 prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
1140 prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
1141 prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
1142 prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
1145 * This transfers the registers from regs into the standard
1146 * coredump arrangement, whatever that is.
1148 #ifdef ELF_CORE_COPY_REGS
1149 ELF_CORE_COPY_REGS(prstatus.pr_reg, regs)
1150 #else
1151 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs))
1153 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1154 (long)sizeof(elf_gregset_t), (long)sizeof(struct pt_regs));
1156 else
1157 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1158 #endif
1160 #ifdef DEBUG
1161 dump_regs("Passed in regs", (elf_greg_t *)regs);
1162 dump_regs("prstatus regs", (elf_greg_t *)&prstatus.pr_reg);
1163 #endif
1165 notes[1].name = "CORE";
1166 notes[1].type = NT_PRPSINFO;
1167 notes[1].datasz = sizeof(psinfo);
1168 notes[1].data = &psinfo;
1169 i = current->state ? ffz(~current->state) + 1 : 0;
1170 psinfo.pr_state = i;
1171 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1172 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1173 psinfo.pr_nice = current->priority-15;
1174 psinfo.pr_flag = current->flags;
1175 psinfo.pr_uid = NEW_TO_OLD_UID(current->uid);
1176 psinfo.pr_gid = NEW_TO_OLD_GID(current->gid);
1178 int i, len;
1180 set_fs(fs);
1182 len = current->mm->arg_end - current->mm->arg_start;
1183 if (len >= ELF_PRARGSZ)
1184 len = ELF_PRARGSZ-1;
1185 copy_from_user(&psinfo.pr_psargs,
1186 (const char *)current->mm->arg_start, len);
1187 for(i = 0; i < len; i++)
1188 if (psinfo.pr_psargs[i] == 0)
1189 psinfo.pr_psargs[i] = ' ';
1190 psinfo.pr_psargs[len] = 0;
1192 set_fs(KERNEL_DS);
1194 strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1196 notes[2].name = "CORE";
1197 notes[2].type = NT_TASKSTRUCT;
1198 notes[2].datasz = sizeof(*current);
1199 notes[2].data = current;
1201 /* Try to dump the FPU. */
1202 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1203 if (!prstatus.pr_fpvalid)
1205 numnote--;
1207 else
1209 notes[3].name = "CORE";
1210 notes[3].type = NT_PRFPREG;
1211 notes[3].datasz = sizeof(fpu);
1212 notes[3].data = &fpu;
1215 /* Write notes phdr entry */
1217 struct elf_phdr phdr;
1218 int sz = 0;
1220 for(i = 0; i < numnote; i++)
1221 sz += notesize(&notes[i]);
1223 phdr.p_type = PT_NOTE;
1224 phdr.p_offset = offset;
1225 phdr.p_vaddr = 0;
1226 phdr.p_paddr = 0;
1227 phdr.p_filesz = sz;
1228 phdr.p_memsz = 0;
1229 phdr.p_flags = 0;
1230 phdr.p_align = 0;
1232 offset += phdr.p_filesz;
1233 DUMP_WRITE(&phdr, sizeof(phdr));
1236 /* Page-align dumped data */
1237 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1239 /* Write program headers for segments dump */
1240 for(vma = current->mm->mmap, i = 0;
1241 i < segs && vma != NULL; vma = vma->vm_next) {
1242 struct elf_phdr phdr;
1243 size_t sz;
1245 i++;
1247 sz = vma->vm_end - vma->vm_start;
1249 phdr.p_type = PT_LOAD;
1250 phdr.p_offset = offset;
1251 phdr.p_vaddr = vma->vm_start;
1252 phdr.p_paddr = 0;
1253 phdr.p_filesz = maydump(vma) ? sz : 0;
1254 phdr.p_memsz = sz;
1255 offset += phdr.p_filesz;
1256 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1257 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1258 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1259 phdr.p_align = ELF_EXEC_PAGESIZE;
1261 DUMP_WRITE(&phdr, sizeof(phdr));
1264 for(i = 0; i < numnote; i++)
1265 if (!writenote(&notes[i], file))
1266 goto end_coredump;
1268 set_fs(fs);
1270 DUMP_SEEK(dataoff);
1272 for(i = 0, vma = current->mm->mmap;
1273 i < segs && vma != NULL;
1274 vma = vma->vm_next) {
1275 unsigned long addr = vma->vm_start;
1276 unsigned long len = vma->vm_end - vma->vm_start;
1278 i++;
1279 if (!maydump(vma))
1280 continue;
1281 #ifdef DEBUG
1282 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1283 #endif
1284 DUMP_WRITE((void *)addr, len);
1287 if ((off_t) file->f_pos != offset) {
1288 /* Sanity check */
1289 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1290 (off_t) file->f_pos, offset);
1293 end_coredump:
1294 set_fs(fs);
1295 #ifndef CONFIG_BINFMT_ELF
1296 MOD_DEC_USE_COUNT;
1297 #endif
1298 return has_dumped;
1300 #endif /* USE_ELF_CORE_DUMP */
1302 static int __init init_elf_binfmt(void)
1304 return register_binfmt(&elf_format);
1307 static void __exit exit_elf_binfmt(void)
1309 /* Remove the COFF and ELF loaders. */
1310 unregister_binfmt(&elf_format);
1313 module_init(init_elf_binfmt)
1314 module_exit(exit_elf_binfmt)