Import 2.3.12pre9
[davej-history.git] / fs / binfmt_elf.c
blob5b2988201bcbdb2c2916c13f5d299f0c1dd5ff40
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, 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,
68 #ifndef MODULE
69 NULL,
70 #else
71 &__this_module,
72 #endif
73 load_elf_binary, load_elf_library, elf_core_dump, ELF_EXEC_PAGESIZE
76 static void set_brk(unsigned long start, unsigned long end)
78 start = ELF_PAGEALIGN(start);
79 end = ELF_PAGEALIGN(end);
80 if (end <= start)
81 return;
82 do_brk(start, end - start);
86 /* We need to explicitly zero any fractional pages
87 after the data section (i.e. bss). This would
88 contain the junk from the file that should not
89 be in memory */
92 static void padzero(unsigned long elf_bss)
94 unsigned long nbyte;
96 nbyte = ELF_PAGEOFFSET(elf_bss);
97 if (nbyte) {
98 nbyte = ELF_EXEC_PAGESIZE - nbyte;
99 clear_user((void *) elf_bss, nbyte);
103 static elf_addr_t *
104 create_elf_tables(char *p, int argc, int envc,
105 struct elfhdr * exec,
106 unsigned long load_addr,
107 unsigned long load_bias,
108 unsigned long interp_load_addr, int ibcs)
110 elf_caddr_t *argv;
111 elf_caddr_t *envp;
112 elf_addr_t *sp, *csp;
113 char *k_platform, *u_platform;
114 long hwcap;
115 size_t platform_len = 0;
118 * Get hold of platform and hardware capabilities masks for
119 * the machine we are running on. In some cases (Sparc),
120 * this info is impossible to get, in others (i386) it is
121 * merely difficult.
124 hwcap = ELF_HWCAP;
125 k_platform = ELF_PLATFORM;
127 if (k_platform) {
128 platform_len = strlen(k_platform) + 1;
129 u_platform = p - platform_len;
130 __copy_to_user(u_platform, k_platform, platform_len);
131 } else
132 u_platform = p;
135 * Force 16 byte _final_ alignment here for generality.
136 * Leave an extra 16 bytes free so that on the PowerPC we
137 * can move the aux table up to start on a 16-byte boundary.
139 sp = (elf_addr_t *)((~15UL & (unsigned long)(u_platform)) - 16UL);
140 csp = sp;
141 csp -= ((exec ? DLINFO_ITEMS*2 : 4) + (k_platform ? 2 : 0));
142 csp -= envc+1;
143 csp -= argc+1;
144 csp -= (!ibcs ? 3 : 1); /* argc itself */
145 if ((unsigned long)csp & 15UL)
146 sp -= ((unsigned long)csp & 15UL) / sizeof(*sp);
149 * Put the ELF interpreter info on the stack
151 #define NEW_AUX_ENT(nr, id, val) \
152 __put_user ((id), sp+(nr*2)); \
153 __put_user ((val), sp+(nr*2+1)); \
155 sp -= 2;
156 NEW_AUX_ENT(0, AT_NULL, 0);
157 if (k_platform) {
158 sp -= 2;
159 NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
161 sp -= 2;
162 NEW_AUX_ENT(0, AT_HWCAP, hwcap);
164 if (exec) {
165 sp -= 11*2;
167 NEW_AUX_ENT(0, AT_PHDR, load_addr + exec->e_phoff);
168 NEW_AUX_ENT(1, AT_PHENT, sizeof (struct elf_phdr));
169 NEW_AUX_ENT(2, AT_PHNUM, exec->e_phnum);
170 NEW_AUX_ENT(3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
171 NEW_AUX_ENT(4, AT_BASE, interp_load_addr);
172 NEW_AUX_ENT(5, AT_FLAGS, 0);
173 NEW_AUX_ENT(6, AT_ENTRY, load_bias + exec->e_entry);
174 NEW_AUX_ENT(7, AT_UID, (elf_addr_t) current->uid);
175 NEW_AUX_ENT(8, AT_EUID, (elf_addr_t) current->euid);
176 NEW_AUX_ENT(9, AT_GID, (elf_addr_t) current->gid);
177 NEW_AUX_ENT(10, AT_EGID, (elf_addr_t) current->egid);
179 #undef NEW_AUX_ENT
181 sp -= envc+1;
182 envp = (elf_caddr_t *) sp;
183 sp -= argc+1;
184 argv = (elf_caddr_t *) sp;
185 if (!ibcs) {
186 __put_user((elf_addr_t)(unsigned long) envp,--sp);
187 __put_user((elf_addr_t)(unsigned long) argv,--sp);
190 __put_user((elf_addr_t)argc,--sp);
191 current->mm->arg_start = (unsigned long) p;
192 while (argc-->0) {
193 __put_user((elf_caddr_t)(unsigned long)p,argv++);
194 p += strlen_user(p);
196 __put_user(NULL, argv);
197 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
198 while (envc-->0) {
199 __put_user((elf_caddr_t)(unsigned long)p,envp++);
200 p += strlen_user(p);
202 __put_user(NULL, envp);
203 current->mm->env_end = (unsigned long) p;
204 return sp;
208 /* This is much more generalized than the library routine read function,
209 so we keep this separate. Technically the library read function
210 is only provided so that we can read a.out libraries that have
211 an ELF header */
213 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
214 struct dentry * interpreter_dentry,
215 unsigned long *interp_load_addr)
217 struct file * file;
218 struct elf_phdr *elf_phdata;
219 struct elf_phdr *eppnt;
220 unsigned long load_addr = 0;
221 int load_addr_set = 0;
222 unsigned long last_bss = 0, elf_bss = 0;
223 unsigned long error = ~0UL;
224 int elf_exec_fileno;
225 int retval, i, size;
227 /* First of all, some simple consistency checks */
228 if (interp_elf_ex->e_type != ET_EXEC &&
229 interp_elf_ex->e_type != ET_DYN)
230 goto out;
231 if (!elf_check_arch(interp_elf_ex->e_machine))
232 goto out;
233 if (!interpreter_dentry->d_inode->i_op ||
234 !interpreter_dentry->d_inode->i_op->default_file_ops->mmap)
235 goto out;
238 * If the size of this structure has changed, then punt, since
239 * we will be doing the wrong thing.
241 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
242 goto out;
244 /* Now read in all of the header information */
246 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
247 if (size > ELF_EXEC_PAGESIZE)
248 goto out;
249 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
250 if (!elf_phdata)
251 goto out;
253 retval = read_exec(interpreter_dentry, interp_elf_ex->e_phoff,
254 (char *) elf_phdata, size, 1);
255 error = retval;
256 if (retval < 0)
257 goto out_free;
259 error = ~0UL;
260 elf_exec_fileno = open_dentry(interpreter_dentry, O_RDONLY);
261 if (elf_exec_fileno < 0)
262 goto out_free;
263 file = fget(elf_exec_fileno);
265 eppnt = elf_phdata;
266 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
267 if (eppnt->p_type == PT_LOAD) {
268 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
269 int elf_prot = 0;
270 unsigned long vaddr = 0;
271 unsigned long k, map_addr;
273 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
274 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
275 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
276 vaddr = eppnt->p_vaddr;
277 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
278 elf_type |= MAP_FIXED;
279 #ifdef __sparc__
280 } else {
281 load_addr = get_unmapped_area(0, eppnt->p_filesz +
282 ELF_PAGEOFFSET(vaddr));
283 #endif
286 map_addr = do_mmap(file,
287 load_addr + ELF_PAGESTART(vaddr),
288 eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr),
289 elf_prot,
290 elf_type,
291 eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
292 if (map_addr > -1024UL) /* Real error */
293 goto out_close;
295 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
296 load_addr = map_addr - ELF_PAGESTART(vaddr);
297 load_addr_set = 1;
301 * Find the end of the file mapping for this phdr, and keep
302 * track of the largest address we see for this.
304 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
305 if (k > elf_bss)
306 elf_bss = k;
309 * Do the same thing for the memory mapping - between
310 * elf_bss and last_bss is the bss section.
312 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
313 if (k > last_bss)
314 last_bss = k;
318 /* Now use mmap to map the library into memory. */
321 * Now fill out the bss section. First pad the last page up
322 * to the page boundary, and then perform a mmap to make sure
323 * that there are zero-mapped pages up to and including the
324 * last bss page.
326 padzero(elf_bss);
327 elf_bss = ELF_PAGESTART(elf_bss + ELF_EXEC_PAGESIZE - 1); /* What we have mapped so far */
329 /* Map the last of the bss segment */
330 if (last_bss > elf_bss)
331 do_brk(elf_bss, last_bss - elf_bss);
333 *interp_load_addr = load_addr;
334 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
336 out_close:
337 fput(file);
338 sys_close(elf_exec_fileno);
339 out_free:
340 kfree(elf_phdata);
341 out:
342 return error;
345 static unsigned long load_aout_interp(struct exec * interp_ex,
346 struct dentry * interpreter_dentry)
348 unsigned long text_data, offset, elf_entry = ~0UL;
349 char * addr;
350 int retval;
352 current->mm->end_code = interp_ex->a_text;
353 text_data = interp_ex->a_text + interp_ex->a_data;
354 current->mm->end_data = text_data;
355 current->mm->brk = interp_ex->a_bss + text_data;
357 switch (N_MAGIC(*interp_ex)) {
358 case OMAGIC:
359 offset = 32;
360 addr = (char *) 0;
361 break;
362 case ZMAGIC:
363 case QMAGIC:
364 offset = N_TXTOFF(*interp_ex);
365 addr = (char *) N_TXTADDR(*interp_ex);
366 break;
367 default:
368 goto out;
371 do_brk(0, text_data);
372 retval = read_exec(interpreter_dentry, offset, addr, text_data, 0);
373 if (retval < 0)
374 goto out;
375 flush_icache_range((unsigned long)addr,
376 (unsigned long)addr + text_data);
378 do_brk(ELF_PAGESTART(text_data + ELF_EXEC_PAGESIZE - 1),
379 interp_ex->a_bss);
380 elf_entry = interp_ex->a_entry;
382 out:
383 return elf_entry;
387 * These are the functions used to load ELF style executables and shared
388 * libraries. There is no binary dependent code anywhere else.
391 #define INTERPRETER_NONE 0
392 #define INTERPRETER_AOUT 1
393 #define INTERPRETER_ELF 2
396 static inline int
397 do_load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
399 struct file * file;
400 struct dentry *interpreter_dentry = NULL; /* to shut gcc up */
401 unsigned long load_addr = 0, load_bias;
402 int load_addr_set = 0;
403 char * elf_interpreter = NULL;
404 unsigned int interpreter_type = INTERPRETER_NONE;
405 unsigned char ibcs2_interpreter = 0;
406 mm_segment_t old_fs;
407 unsigned long error;
408 struct elf_phdr * elf_ppnt, *elf_phdata;
409 unsigned long elf_bss, k, elf_brk;
410 int elf_exec_fileno;
411 int retval, size, i;
412 unsigned long elf_entry, interp_load_addr = 0;
413 unsigned long start_code, end_code, end_data;
414 struct elfhdr elf_ex;
415 struct elfhdr interp_elf_ex;
416 struct exec interp_ex;
417 char passed_fileno[6];
419 /* Get the exec-header */
420 elf_ex = *((struct elfhdr *) bprm->buf);
422 retval = -ENOEXEC;
423 /* First of all, some simple consistency checks */
424 if (elf_ex.e_ident[0] != 0x7f ||
425 strncmp(&elf_ex.e_ident[1], "ELF", 3) != 0)
426 goto out;
428 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
429 goto out;
430 if (!elf_check_arch(elf_ex.e_machine))
431 goto out;
432 #ifdef __mips__
433 /* IRIX binaries handled elsewhere. */
434 if (elf_ex.e_flags & EF_MIPS_ARCH) {
435 retval = -ENOEXEC;
436 goto out;
438 #endif
439 if (!bprm->dentry->d_inode->i_op ||
440 !bprm->dentry->d_inode->i_op->default_file_ops ||
441 !bprm->dentry->d_inode->i_op->default_file_ops->mmap)
442 goto out;
444 /* Now read in all of the header information */
446 retval = -ENOMEM;
447 size = elf_ex.e_phentsize * elf_ex.e_phnum;
448 if (size > 65536)
449 goto out;
450 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
451 if (!elf_phdata)
452 goto out;
454 retval = read_exec(bprm->dentry, elf_ex.e_phoff,
455 (char *) elf_phdata, size, 1);
456 if (retval < 0)
457 goto out_free_ph;
459 retval = open_dentry(bprm->dentry, O_RDONLY);
460 if (retval < 0)
461 goto out_free_ph;
462 elf_exec_fileno = retval;
463 file = fget(elf_exec_fileno);
465 elf_ppnt = elf_phdata;
466 elf_bss = 0;
467 elf_brk = 0;
469 start_code = ~0UL;
470 end_code = 0;
471 end_data = 0;
473 for (i = 0; i < elf_ex.e_phnum; i++) {
474 if (elf_ppnt->p_type == PT_INTERP) {
475 retval = -EINVAL;
476 if (elf_interpreter)
477 goto out_free_interp;
479 /* This is the program interpreter used for
480 * shared libraries - for now assume that this
481 * is an a.out format binary
484 retval = -ENOMEM;
485 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
486 GFP_KERNEL);
487 if (!elf_interpreter)
488 goto out_free_file;
490 retval = read_exec(bprm->dentry, elf_ppnt->p_offset,
491 elf_interpreter,
492 elf_ppnt->p_filesz, 1);
493 if (retval < 0)
494 goto out_free_interp;
495 /* If the program interpreter is one of these two,
496 * then assume an iBCS2 image. Otherwise assume
497 * a native linux image.
499 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
500 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
501 ibcs2_interpreter = 1;
502 #if 0
503 printk("Using ELF interpreter %s\n", elf_interpreter);
504 #endif
505 old_fs = get_fs(); /* This could probably be optimized */
506 set_fs(get_ds());
507 #ifdef __sparc__
508 if (ibcs2_interpreter) {
509 unsigned long old_pers = current->personality;
511 current->personality = PER_SVR4;
512 interpreter_dentry = open_namei(elf_interpreter,
513 0, 0);
514 current->personality = old_pers;
515 } else
516 #endif
517 interpreter_dentry = open_namei(elf_interpreter,
518 0, 0);
519 set_fs(old_fs);
520 retval = PTR_ERR(interpreter_dentry);
521 if (IS_ERR(interpreter_dentry))
522 goto out_free_interp;
523 retval = permission(interpreter_dentry->d_inode, MAY_EXEC);
524 if (retval < 0)
525 goto out_free_dentry;
526 retval = read_exec(interpreter_dentry, 0, bprm->buf, 128, 1);
527 if (retval < 0)
528 goto out_free_dentry;
530 /* Get the exec headers */
531 interp_ex = *((struct exec *) bprm->buf);
532 interp_elf_ex = *((struct elfhdr *) bprm->buf);
534 elf_ppnt++;
537 /* Some simple consistency checks for the interpreter */
538 if (elf_interpreter) {
539 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
541 /* Now figure out which format our binary is */
542 if ((N_MAGIC(interp_ex) != OMAGIC) &&
543 (N_MAGIC(interp_ex) != ZMAGIC) &&
544 (N_MAGIC(interp_ex) != QMAGIC))
545 interpreter_type = INTERPRETER_ELF;
547 if (interp_elf_ex.e_ident[0] != 0x7f ||
548 strncmp(&interp_elf_ex.e_ident[1], "ELF", 3) != 0)
549 interpreter_type &= ~INTERPRETER_ELF;
551 retval = -ELIBBAD;
552 if (!interpreter_type)
553 goto out_free_dentry;
555 /* Make sure only one type was selected */
556 if ((interpreter_type & INTERPRETER_ELF) &&
557 interpreter_type != INTERPRETER_ELF) {
558 printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
559 interpreter_type = INTERPRETER_ELF;
563 /* OK, we are done with that, now set up the arg stuff,
564 and then start this sucker up */
566 if (!bprm->sh_bang) {
567 char * passed_p;
569 if (interpreter_type == INTERPRETER_AOUT) {
570 sprintf(passed_fileno, "%d", elf_exec_fileno);
571 passed_p = passed_fileno;
573 if (elf_interpreter) {
574 retval = copy_strings_kernel(1,&passed_p,bprm);
575 if (retval)
576 goto out_free_dentry;
577 bprm->argc++;
582 /* Flush all traces of the currently running executable */
583 retval = flush_old_exec(bprm);
584 if (retval)
585 goto out_free_dentry;
587 /* OK, This is the point of no return */
588 current->mm->end_data = 0;
589 current->mm->end_code = 0;
590 current->mm->mmap = NULL;
591 current->flags &= ~PF_FORKNOEXEC;
592 elf_entry = (unsigned long) elf_ex.e_entry;
594 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
595 may depend on the personality. */
596 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
598 /* Do this so that we can load the interpreter, if need be. We will
599 change some of these later */
600 current->mm->rss = 0;
601 setup_arg_pages(bprm); /* XXX: check error */
602 current->mm->start_stack = bprm->p;
604 /* Try and get dynamic programs out of the way of the default mmap
605 base, as well as whatever program they might try to exec. This
606 is because the brk will follow the loader, and is not movable. */
608 load_bias = ELF_PAGESTART(elf_ex.e_type==ET_DYN ? ELF_ET_DYN_BASE : 0);
610 /* Now we do a little grungy work by mmaping the ELF image into
611 the correct location in memory. At this point, we assume that
612 the image should be loaded at fixed address, not at a variable
613 address. */
615 old_fs = get_fs();
616 set_fs(get_ds());
617 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
618 int elf_prot = 0, elf_flags;
619 unsigned long vaddr;
621 if (elf_ppnt->p_type != PT_LOAD)
622 continue;
624 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
625 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
626 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
628 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
630 vaddr = elf_ppnt->p_vaddr;
631 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
632 elf_flags |= MAP_FIXED;
635 error = do_mmap(file, ELF_PAGESTART(load_bias + vaddr),
636 (elf_ppnt->p_filesz +
637 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
638 elf_prot, elf_flags, (elf_ppnt->p_offset -
639 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
641 if (!load_addr_set) {
642 load_addr_set = 1;
643 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
644 if (elf_ex.e_type == ET_DYN) {
645 load_bias += error -
646 ELF_PAGESTART(load_bias + vaddr);
647 load_addr += error;
650 k = elf_ppnt->p_vaddr;
651 if (k < start_code) start_code = k;
652 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
653 if (k > elf_bss)
654 elf_bss = k;
655 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
656 end_code = k;
657 if (end_data < k)
658 end_data = k;
659 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
660 if (k > elf_brk)
661 elf_brk = k;
663 set_fs(old_fs);
664 fput(file); /* all done with the file */
666 elf_entry += load_bias;
667 elf_bss += load_bias;
668 elf_brk += load_bias;
669 start_code += load_bias;
670 end_code += load_bias;
671 end_data += load_bias;
673 if (elf_interpreter) {
674 if (interpreter_type == INTERPRETER_AOUT)
675 elf_entry = load_aout_interp(&interp_ex,
676 interpreter_dentry);
677 else
678 elf_entry = load_elf_interp(&interp_elf_ex,
679 interpreter_dentry,
680 &interp_load_addr);
682 dput(interpreter_dentry);
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->end_data = end_data;
729 current->mm->start_stack = bprm->p;
731 /* Calling set_brk effectively mmaps the pages that we need
732 * for the bss and break sections
734 set_brk(elf_bss, elf_brk);
736 padzero(elf_bss);
738 #if 0
739 printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
740 printk("(end_code) %lx\n" , (long) current->mm->end_code);
741 printk("(start_code) %lx\n" , (long) current->mm->start_code);
742 printk("(end_data) %lx\n" , (long) current->mm->end_data);
743 printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
744 printk("(brk) %lx\n" , (long) current->mm->brk);
745 #endif
747 if ( current->personality == PER_SVR4 )
749 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
750 and some applications "depend" upon this behavior.
751 Since we do not have the power to recompile these, we
752 emulate the SVr4 behavior. Sigh. */
753 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
754 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
755 MAP_FIXED | MAP_PRIVATE, 0);
758 #ifdef ELF_PLAT_INIT
760 * The ABI may specify that certain registers be set up in special
761 * ways (on i386 %edx is the address of a DT_FINI function, for
762 * example. This macro performs whatever initialization to
763 * the regs structure is required.
765 ELF_PLAT_INIT(regs);
766 #endif
768 start_thread(regs, elf_entry, bprm->p);
769 if (current->flags & PF_PTRACED)
770 send_sig(SIGTRAP, current, 0);
771 retval = 0;
772 out:
773 return retval;
775 /* error cleanup */
776 out_free_dentry:
777 dput(interpreter_dentry);
778 out_free_interp:
779 if (elf_interpreter)
780 kfree(elf_interpreter);
781 out_free_file:
782 fput(file);
783 sys_close(elf_exec_fileno);
784 out_free_ph:
785 kfree(elf_phdata);
786 goto out;
789 static int
790 load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
792 int retval;
794 MOD_INC_USE_COUNT;
795 retval = do_load_elf_binary(bprm, regs);
796 MOD_DEC_USE_COUNT;
797 return retval;
800 /* This is really simpleminded and specialized - we are loading an
801 a.out library that is given an ELF header. */
803 static inline int
804 do_load_elf_library(int fd)
806 struct file * file;
807 struct dentry * dentry;
808 struct inode * inode;
809 struct elf_phdr *elf_phdata;
810 unsigned long elf_bss = 0, bss, len, k;
811 int retval, error, i, j;
812 struct elfhdr elf_ex;
813 loff_t offset = 0;
815 error = -EACCES;
816 file = fget(fd);
817 if (!file || !file->f_op)
818 goto out;
819 dentry = file->f_dentry;
820 inode = dentry->d_inode;
822 /* seek to the beginning of the file */
823 error = -ENOEXEC;
825 /* N.B. save current DS?? */
826 set_fs(KERNEL_DS);
827 retval = file->f_op->read(file, (char *) &elf_ex, sizeof(elf_ex), &offset);
828 set_fs(USER_DS);
829 if (retval != sizeof(elf_ex))
830 goto out_putf;
832 if (elf_ex.e_ident[0] != 0x7f ||
833 strncmp(&elf_ex.e_ident[1], "ELF", 3) != 0)
834 goto out_putf;
836 /* First of all, some simple consistency checks */
837 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
838 !elf_check_arch(elf_ex.e_machine) ||
839 (!inode->i_op || !inode->i_op->default_file_ops->mmap))
840 goto out_putf;
842 /* Now read in all of the header information */
844 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
845 if (j > ELF_EXEC_PAGESIZE)
846 goto out_putf;
848 error = -ENOMEM;
849 elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
850 if (!elf_phdata)
851 goto out_putf;
853 /* N.B. check for error return?? */
854 retval = read_exec(dentry, elf_ex.e_phoff, (char *) elf_phdata,
855 sizeof(struct elf_phdr) * elf_ex.e_phnum, 1);
857 error = -ENOEXEC;
858 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
859 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
860 if (j != 1)
861 goto out_free_ph;
863 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
865 /* Now use mmap to map the library into memory. */
866 error = do_mmap(file,
867 ELF_PAGESTART(elf_phdata->p_vaddr),
868 (elf_phdata->p_filesz +
869 ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
870 PROT_READ | PROT_WRITE | PROT_EXEC,
871 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
872 (elf_phdata->p_offset -
873 ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
874 if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
875 goto out_free_ph;
877 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
878 if (k > elf_bss)
879 elf_bss = k;
880 padzero(elf_bss);
882 len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr +
883 ELF_EXEC_PAGESIZE - 1);
884 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
885 if (bss > len)
886 do_brk(len, bss - len);
887 error = 0;
889 out_free_ph:
890 kfree(elf_phdata);
891 out_putf:
892 fput(file);
893 out:
894 return error;
897 static int load_elf_library(int fd)
899 int retval;
901 MOD_INC_USE_COUNT;
902 retval = do_load_elf_library(fd);
903 MOD_DEC_USE_COUNT;
904 return retval;
908 * Note that some platforms still use traditional core dumps and not
909 * the ELF core dump. Each platform can select it as appropriate.
911 #ifdef USE_ELF_CORE_DUMP
914 * ELF core dumper
916 * Modelled on fs/exec.c:aout_core_dump()
917 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
920 * These are the only things you should do on a core-file: use only these
921 * functions to write out all the necessary info.
923 static int dump_write(struct file *file, const void *addr, int nr)
925 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
928 static int dump_seek(struct file *file, off_t off)
930 if (file->f_op->llseek) {
931 if (file->f_op->llseek(file, off, 0) != off)
932 return 0;
933 } else
934 file->f_pos = off;
935 return 1;
939 * Decide whether a segment is worth dumping; default is yes to be
940 * sure (missing info is worse than too much; etc).
941 * Personally I'd include everything, and use the coredump limit...
943 * I think we should skip something. But I am not sure how. H.J.
945 static inline int maydump(struct vm_area_struct *vma)
947 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
948 return 0;
950 /* Do not dump I/O mapped devices! -DaveM */
951 if(vma->vm_flags & VM_IO)
952 return 0;
953 #if 1
954 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
955 return 1;
956 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
957 return 0;
958 #endif
959 return 1;
962 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
964 /* An ELF note in memory */
965 struct memelfnote
967 const char *name;
968 int type;
969 unsigned int datasz;
970 void *data;
973 static int notesize(struct memelfnote *en)
975 int sz;
977 sz = sizeof(struct elf_note);
978 sz += roundup(strlen(en->name), 4);
979 sz += roundup(en->datasz, 4);
981 return sz;
984 /* #define DEBUG */
986 #ifdef DEBUG
987 static void dump_regs(const char *str, elf_greg_t *r)
989 int i;
990 static const char *regs[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
991 "eax", "ds", "es", "fs", "gs",
992 "orig_eax", "eip", "cs",
993 "efl", "uesp", "ss"};
994 printk("Registers: %s\n", str);
996 for(i = 0; i < ELF_NGREG; i++)
998 unsigned long val = r[i];
999 printk(" %-2d %-5s=%08lx %lu\n", i, regs[i], val, val);
1002 #endif
1004 #define DUMP_WRITE(addr, nr) \
1005 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1006 #define DUMP_SEEK(off) \
1007 do { if (!dump_seek(file, (off))) return 0; } while(0)
1009 static int writenote(struct memelfnote *men, struct file *file)
1011 struct elf_note en;
1013 en.n_namesz = strlen(men->name);
1014 en.n_descsz = men->datasz;
1015 en.n_type = men->type;
1017 DUMP_WRITE(&en, sizeof(en));
1018 DUMP_WRITE(men->name, en.n_namesz);
1019 /* XXX - cast from long long to long to avoid need for libgcc.a */
1020 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1021 DUMP_WRITE(men->data, men->datasz);
1022 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1024 return 1;
1026 #undef DUMP_WRITE
1027 #undef DUMP_SEEK
1029 #define DUMP_WRITE(addr, nr) \
1030 if (!dump_write(file, (addr), (nr))) \
1031 goto end_coredump;
1032 #define DUMP_SEEK(off) \
1033 if (!dump_seek(file, (off))) \
1034 goto end_coredump;
1036 * Actual dumper
1038 * This is a two-pass process; first we find the offsets of the bits,
1039 * and then they are actually written out. If we run out of core limit
1040 * we just truncate.
1042 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
1044 int has_dumped = 0;
1045 mm_segment_t fs;
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 #ifndef CONFIG_BINFMT_ELF
1060 MOD_INC_USE_COUNT;
1061 #endif
1063 /* Count what's needed to dump, up to the limit of coredump size */
1064 segs = 0;
1065 size = 0;
1066 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1067 if (maydump(vma))
1069 unsigned long sz = vma->vm_end-vma->vm_start;
1071 if (size+sz >= limit)
1072 break;
1073 else
1074 size += sz;
1077 segs++;
1079 #ifdef DEBUG
1080 printk("elf_core_dump: %d segs taking %d bytes\n", segs, size);
1081 #endif
1083 /* Set up header */
1084 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1085 elf.e_ident[EI_CLASS] = ELF_CLASS;
1086 elf.e_ident[EI_DATA] = ELF_DATA;
1087 elf.e_ident[EI_VERSION] = EV_CURRENT;
1088 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1090 elf.e_type = ET_CORE;
1091 elf.e_machine = ELF_ARCH;
1092 elf.e_version = EV_CURRENT;
1093 elf.e_entry = 0;
1094 elf.e_phoff = sizeof(elf);
1095 elf.e_shoff = 0;
1096 elf.e_flags = 0;
1097 elf.e_ehsize = sizeof(elf);
1098 elf.e_phentsize = sizeof(struct elf_phdr);
1099 elf.e_phnum = segs+1; /* Include notes */
1100 elf.e_shentsize = 0;
1101 elf.e_shnum = 0;
1102 elf.e_shstrndx = 0;
1104 fs = get_fs();
1105 set_fs(KERNEL_DS);
1107 has_dumped = 1;
1108 current->flags |= PF_DUMPCORE;
1110 DUMP_WRITE(&elf, sizeof(elf));
1111 offset += sizeof(elf); /* Elf header */
1112 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1115 * Set up the notes in similar form to SVR4 core dumps made
1116 * with info from their /proc.
1118 memset(&psinfo, 0, sizeof(psinfo));
1119 memset(&prstatus, 0, sizeof(prstatus));
1121 notes[0].name = "CORE";
1122 notes[0].type = NT_PRSTATUS;
1123 notes[0].datasz = sizeof(prstatus);
1124 notes[0].data = &prstatus;
1125 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1126 prstatus.pr_sigpend = current->signal.sig[0];
1127 prstatus.pr_sighold = current->blocked.sig[0];
1128 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1129 psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1130 psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1131 psinfo.pr_sid = prstatus.pr_sid = current->session;
1132 prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
1133 prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
1134 prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
1135 prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
1136 prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
1137 prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
1138 prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
1139 prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
1142 * This transfers the registers from regs into the standard
1143 * coredump arrangement, whatever that is.
1145 #ifdef ELF_CORE_COPY_REGS
1146 ELF_CORE_COPY_REGS(prstatus.pr_reg, regs)
1147 #else
1148 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs))
1150 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1151 (long)sizeof(elf_gregset_t), (long)sizeof(struct pt_regs));
1153 else
1154 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1155 #endif
1157 #ifdef DEBUG
1158 dump_regs("Passed in regs", (elf_greg_t *)regs);
1159 dump_regs("prstatus regs", (elf_greg_t *)&prstatus.pr_reg);
1160 #endif
1162 notes[1].name = "CORE";
1163 notes[1].type = NT_PRPSINFO;
1164 notes[1].datasz = sizeof(psinfo);
1165 notes[1].data = &psinfo;
1166 i = current->state ? ffz(~current->state) + 1 : 0;
1167 psinfo.pr_state = i;
1168 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1169 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1170 psinfo.pr_nice = current->priority-15;
1171 psinfo.pr_flag = current->flags;
1172 psinfo.pr_uid = current->uid;
1173 psinfo.pr_gid = current->gid;
1175 int i, len;
1177 set_fs(fs);
1179 len = current->mm->arg_end - current->mm->arg_start;
1180 if (len >= ELF_PRARGSZ)
1181 len = ELF_PRARGSZ-1;
1182 copy_from_user(&psinfo.pr_psargs,
1183 (const char *)current->mm->arg_start, len);
1184 for(i = 0; i < len; i++)
1185 if (psinfo.pr_psargs[i] == 0)
1186 psinfo.pr_psargs[i] = ' ';
1187 psinfo.pr_psargs[len] = 0;
1189 set_fs(KERNEL_DS);
1191 strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1193 notes[2].name = "CORE";
1194 notes[2].type = NT_TASKSTRUCT;
1195 notes[2].datasz = sizeof(*current);
1196 notes[2].data = current;
1198 /* Try to dump the FPU. */
1199 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1200 if (!prstatus.pr_fpvalid)
1202 numnote--;
1204 else
1206 notes[3].name = "CORE";
1207 notes[3].type = NT_PRFPREG;
1208 notes[3].datasz = sizeof(fpu);
1209 notes[3].data = &fpu;
1212 /* Write notes phdr entry */
1214 struct elf_phdr phdr;
1215 int sz = 0;
1217 for(i = 0; i < numnote; i++)
1218 sz += notesize(&notes[i]);
1220 phdr.p_type = PT_NOTE;
1221 phdr.p_offset = offset;
1222 phdr.p_vaddr = 0;
1223 phdr.p_paddr = 0;
1224 phdr.p_filesz = sz;
1225 phdr.p_memsz = 0;
1226 phdr.p_flags = 0;
1227 phdr.p_align = 0;
1229 offset += phdr.p_filesz;
1230 DUMP_WRITE(&phdr, sizeof(phdr));
1233 /* Page-align dumped data */
1234 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1236 /* Write program headers for segments dump */
1237 for(vma = current->mm->mmap, i = 0;
1238 i < segs && vma != NULL; vma = vma->vm_next) {
1239 struct elf_phdr phdr;
1240 size_t sz;
1242 i++;
1244 sz = vma->vm_end - vma->vm_start;
1246 phdr.p_type = PT_LOAD;
1247 phdr.p_offset = offset;
1248 phdr.p_vaddr = vma->vm_start;
1249 phdr.p_paddr = 0;
1250 phdr.p_filesz = maydump(vma) ? sz : 0;
1251 phdr.p_memsz = sz;
1252 offset += phdr.p_filesz;
1253 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1254 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1255 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1256 phdr.p_align = ELF_EXEC_PAGESIZE;
1258 DUMP_WRITE(&phdr, sizeof(phdr));
1261 for(i = 0; i < numnote; i++)
1262 if (!writenote(&notes[i], file))
1263 goto end_coredump;
1265 set_fs(fs);
1267 DUMP_SEEK(dataoff);
1269 for(i = 0, vma = current->mm->mmap;
1270 i < segs && vma != NULL;
1271 vma = vma->vm_next) {
1272 unsigned long addr = vma->vm_start;
1273 unsigned long len = vma->vm_end - vma->vm_start;
1275 i++;
1276 if (!maydump(vma))
1277 continue;
1278 #ifdef DEBUG
1279 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1280 #endif
1281 DUMP_WRITE((void *)addr, len);
1284 if ((off_t) file->f_pos != offset) {
1285 /* Sanity check */
1286 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1287 (off_t) file->f_pos, offset);
1290 end_coredump:
1291 set_fs(fs);
1292 #ifndef CONFIG_BINFMT_ELF
1293 MOD_DEC_USE_COUNT;
1294 #endif
1295 return has_dumped;
1297 #endif /* USE_ELF_CORE_DUMP */
1299 int __init init_elf_binfmt(void)
1301 return register_binfmt(&elf_format);
1304 #ifdef MODULE
1306 int init_module(void)
1308 /* Install the COFF, ELF and XOUT loaders.
1309 * N.B. We *rely* on the table being the right size with the
1310 * right number of free slots...
1312 return init_elf_binfmt();
1316 void cleanup_module( void)
1318 /* Remove the COFF and ELF loaders. */
1319 unregister_binfmt(&elf_format);
1321 #endif