Import 2.1.116pre2
[davej-history.git] / fs / binfmt_elf.c
blob1c8811848751b64a7869e2748c05e69c0b8c1096
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 #ifdef __sparc__
48 extern unsigned long get_unmapped_area(unsigned long addr, unsigned long len);
49 #endif
51 #ifndef elf_addr_t
52 #define elf_addr_t unsigned long
53 #define elf_caddr_t char *
54 #endif
57 * If we don't support core dumping, then supply a NULL so we
58 * don't even try.
60 #ifdef USE_ELF_CORE_DUMP
61 static int elf_core_dump(long signr, struct pt_regs * regs);
62 #else
63 #define elf_core_dump NULL
64 #endif
66 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_EXEC_PAGESIZE-1))
67 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_EXEC_PAGESIZE-1))
68 #define ELF_PAGEALIGN(_v) (((_v) + ELF_EXEC_PAGESIZE - 1) & ~(ELF_EXEC_PAGESIZE - 1))
70 static struct linux_binfmt elf_format = {
71 #ifndef MODULE
72 NULL, NULL, load_elf_binary, load_elf_library, elf_core_dump
73 #else
74 NULL, &__this_module, load_elf_binary, load_elf_library, elf_core_dump
75 #endif
78 static void set_brk(unsigned long start, unsigned long end)
80 start = ELF_PAGEALIGN(start);
81 end = ELF_PAGEALIGN(end);
82 if (end <= start)
83 return;
84 do_mmap(NULL, start, end - start,
85 PROT_READ | PROT_WRITE | PROT_EXEC,
86 MAP_FIXED | MAP_PRIVATE, 0);
90 /* We need to explicitly zero any fractional pages
91 after the data section (i.e. bss). This would
92 contain the junk from the file that should not
93 be in memory */
96 static void padzero(unsigned long elf_bss)
98 unsigned long nbyte;
100 nbyte = ELF_PAGEOFFSET(elf_bss);
101 if (nbyte) {
102 nbyte = ELF_EXEC_PAGESIZE - nbyte;
103 clear_user((void *) elf_bss, nbyte);
107 static elf_addr_t *
108 create_elf_tables(char *p, int argc, int envc,
109 struct elfhdr * exec,
110 unsigned long load_addr,
111 unsigned long load_bias,
112 unsigned long interp_load_addr, int ibcs)
114 elf_caddr_t *argv;
115 elf_caddr_t *envp;
116 elf_addr_t *sp, *csp;
117 char *k_platform, *u_platform;
118 long hwcap;
119 size_t platform_len = 0;
122 * Get hold of platform and hardware capabilities masks for
123 * the machine we are running on. In some cases (Sparc),
124 * this info is impossible to get, in others (i386) it is
125 * merely difficult.
128 hwcap = ELF_HWCAP;
129 k_platform = ELF_PLATFORM;
131 if (k_platform) {
132 platform_len = strlen(k_platform) + 1;
133 u_platform = p - platform_len;
134 __copy_to_user(u_platform, k_platform, platform_len);
135 } else
136 u_platform = p;
139 * Force 16 byte _final_ alignment here for generality.
140 * Leave an extra 16 bytes free so that on the PowerPC we
141 * can move the aux table up to start on a 16-byte boundary.
143 sp = (elf_addr_t *)((~15UL & (unsigned long)(u_platform)) - 16UL);
144 csp = sp;
145 csp -= ((exec ? DLINFO_ITEMS*2 : 4) + (k_platform ? 2 : 0));
146 csp -= envc+1;
147 csp -= argc+1;
148 csp -= (!ibcs ? 3 : 1); /* argc itself */
149 if ((unsigned long)csp & 15UL)
150 sp -= ((unsigned long)csp & 15UL) / sizeof(*sp);
153 * Put the ELF interpreter info on the stack
155 #define NEW_AUX_ENT(nr, id, val) \
156 __put_user ((id), sp+(nr*2)); \
157 __put_user ((val), sp+(nr*2+1)); \
159 sp -= 2;
160 NEW_AUX_ENT(0, AT_NULL, 0);
161 if (k_platform) {
162 sp -= 2;
163 NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
165 sp -= 2;
166 NEW_AUX_ENT(0, AT_HWCAP, hwcap);
168 if (exec) {
169 sp -= 11*2;
171 NEW_AUX_ENT(0, AT_PHDR, load_addr + exec->e_phoff);
172 NEW_AUX_ENT(1, AT_PHENT, sizeof (struct elf_phdr));
173 NEW_AUX_ENT(2, AT_PHNUM, exec->e_phnum);
174 NEW_AUX_ENT(3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
175 NEW_AUX_ENT(4, AT_BASE, interp_load_addr);
176 NEW_AUX_ENT(5, AT_FLAGS, 0);
177 NEW_AUX_ENT(6, AT_ENTRY, load_bias + exec->e_entry);
178 NEW_AUX_ENT(7, AT_UID, (elf_addr_t) current->uid);
179 NEW_AUX_ENT(8, AT_EUID, (elf_addr_t) current->euid);
180 NEW_AUX_ENT(9, AT_GID, (elf_addr_t) current->gid);
181 NEW_AUX_ENT(10, AT_EGID, (elf_addr_t) current->egid);
183 #undef NEW_AUX_ENT
185 sp -= envc+1;
186 envp = (elf_caddr_t *) sp;
187 sp -= argc+1;
188 argv = (elf_caddr_t *) sp;
189 if (!ibcs) {
190 __put_user((elf_addr_t)(unsigned long) envp,--sp);
191 __put_user((elf_addr_t)(unsigned long) argv,--sp);
194 __put_user((elf_addr_t)argc,--sp);
195 current->mm->arg_start = (unsigned long) p;
196 while (argc-->0) {
197 __put_user((elf_caddr_t)(unsigned long)p,argv++);
198 p += strlen_user(p);
200 __put_user(NULL, argv);
201 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
202 while (envc-->0) {
203 __put_user((elf_caddr_t)(unsigned long)p,envp++);
204 p += strlen_user(p);
206 __put_user(NULL, envp);
207 current->mm->env_end = (unsigned long) p;
208 return sp;
212 /* This is much more generalized than the library routine read function,
213 so we keep this separate. Technically the library read function
214 is only provided so that we can read a.out libraries that have
215 an ELF header */
217 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
218 struct dentry * interpreter_dentry,
219 unsigned long *interp_load_addr)
221 struct file * file;
222 struct elf_phdr *elf_phdata;
223 struct elf_phdr *eppnt;
224 unsigned long load_addr = 0;
225 int load_addr_set = 0;
226 unsigned long last_bss = 0, elf_bss = 0;
227 unsigned long error = ~0UL;
228 int elf_exec_fileno;
229 int retval, i, size;
231 /* First of all, some simple consistency checks */
232 if (interp_elf_ex->e_type != ET_EXEC &&
233 interp_elf_ex->e_type != ET_DYN)
234 goto out;
235 if (!elf_check_arch(interp_elf_ex->e_machine))
236 goto out;
237 if (!interpreter_dentry->d_inode->i_op ||
238 !interpreter_dentry->d_inode->i_op->default_file_ops->mmap)
239 goto out;
242 * If the size of this structure has changed, then punt, since
243 * we will be doing the wrong thing.
245 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
246 goto out;
248 /* Now read in all of the header information */
250 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
251 if (size > ELF_EXEC_PAGESIZE)
252 goto out;
253 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
254 if (!elf_phdata)
255 goto out;
257 retval = read_exec(interpreter_dentry, interp_elf_ex->e_phoff,
258 (char *) elf_phdata, size, 1);
259 error = retval;
260 if (retval < 0)
261 goto out_free;
263 error = ~0UL;
264 elf_exec_fileno = open_dentry(interpreter_dentry, O_RDONLY);
265 if (elf_exec_fileno < 0)
266 goto out_free;
267 file = fget(elf_exec_fileno);
269 eppnt = elf_phdata;
270 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
271 if (eppnt->p_type == PT_LOAD) {
272 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
273 int elf_prot = 0;
274 unsigned long vaddr = 0;
275 unsigned long k, map_addr;
277 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
278 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
279 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
280 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
281 elf_type |= MAP_FIXED;
282 vaddr = eppnt->p_vaddr;
283 #ifdef __sparc__
284 } else {
285 load_addr = get_unmapped_area(0, eppnt->p_filesz +
286 ELF_PAGEOFFSET(eppnt->p_vaddr));
287 #endif
290 map_addr = do_mmap(file,
291 load_addr + ELF_PAGESTART(vaddr),
292 eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr),
293 elf_prot,
294 elf_type,
295 eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
296 if (map_addr > -1024UL) /* Real error */
297 goto out_close;
299 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
300 load_addr = map_addr;
301 load_addr_set = 1;
305 * Find the end of the file mapping for this phdr, and keep
306 * track of the largest address we see for this.
308 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
309 if (k > elf_bss)
310 elf_bss = k;
313 * Do the same thing for the memory mapping - between
314 * elf_bss and last_bss is the bss section.
316 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
317 if (k > last_bss)
318 last_bss = k;
322 /* Now use mmap to map the library into memory. */
325 * Now fill out the bss section. First pad the last page up
326 * to the page boundary, and then perform a mmap to make sure
327 * that there are zero-mapped pages up to and including the
328 * last bss page.
330 padzero(elf_bss);
331 elf_bss = ELF_PAGESTART(elf_bss + ELF_EXEC_PAGESIZE - 1); /* What we have mapped so far */
333 /* Map the last of the bss segment */
334 if (last_bss > elf_bss)
335 do_mmap(NULL, elf_bss, last_bss - elf_bss,
336 PROT_READ|PROT_WRITE|PROT_EXEC,
337 MAP_FIXED|MAP_PRIVATE, 0);
339 *interp_load_addr = load_addr;
340 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
342 out_close:
343 fput(file);
344 sys_close(elf_exec_fileno);
345 out_free:
346 kfree(elf_phdata);
347 out:
348 return error;
351 static unsigned long load_aout_interp(struct exec * interp_ex,
352 struct dentry * interpreter_dentry)
354 unsigned long text_data, offset, elf_entry = ~0UL;
355 char * addr;
356 int retval;
358 current->mm->end_code = interp_ex->a_text;
359 text_data = interp_ex->a_text + interp_ex->a_data;
360 current->mm->end_data = text_data;
361 current->mm->brk = interp_ex->a_bss + text_data;
363 switch (N_MAGIC(*interp_ex)) {
364 case OMAGIC:
365 offset = 32;
366 addr = (char *) 0;
367 break;
368 case ZMAGIC:
369 case QMAGIC:
370 offset = N_TXTOFF(*interp_ex);
371 addr = (char *) N_TXTADDR(*interp_ex);
372 break;
373 default:
374 goto out;
377 do_mmap(NULL, 0, text_data,
378 PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED|MAP_PRIVATE, 0);
379 retval = read_exec(interpreter_dentry, offset, addr, text_data, 0);
380 if (retval < 0)
381 goto out;
383 do_mmap(NULL, ELF_PAGESTART(text_data + ELF_EXEC_PAGESIZE - 1),
384 interp_ex->a_bss,
385 PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED|MAP_PRIVATE, 0);
386 elf_entry = interp_ex->a_entry;
388 out:
389 return elf_entry;
393 * These are the functions used to load ELF style executables and shared
394 * libraries. There is no binary dependent code anywhere else.
397 #define INTERPRETER_NONE 0
398 #define INTERPRETER_AOUT 1
399 #define INTERPRETER_ELF 2
402 static inline int
403 do_load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
405 struct file * file;
406 struct dentry *interpreter_dentry = NULL; /* to shut gcc up */
407 unsigned long load_addr = 0, load_bias;
408 int load_addr_set = 0;
409 char * elf_interpreter = NULL;
410 unsigned int interpreter_type = INTERPRETER_NONE;
411 unsigned char ibcs2_interpreter = 0;
412 mm_segment_t old_fs;
413 unsigned long error;
414 struct elf_phdr * elf_ppnt, *elf_phdata;
415 unsigned long elf_bss, k, elf_brk;
416 int elf_exec_fileno;
417 int retval, size, i;
418 unsigned long elf_entry, interp_load_addr = 0;
419 unsigned long start_code, end_code, end_data;
420 struct elfhdr elf_ex;
421 struct elfhdr interp_elf_ex;
422 struct exec interp_ex;
423 char passed_fileno[6];
425 /* Get the exec-header */
426 elf_ex = *((struct elfhdr *) bprm->buf);
428 retval = -ENOEXEC;
429 /* First of all, some simple consistency checks */
430 if (elf_ex.e_ident[0] != 0x7f ||
431 strncmp(&elf_ex.e_ident[1], "ELF", 3) != 0)
432 goto out;
434 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
435 goto out;
436 if (!elf_check_arch(elf_ex.e_machine))
437 goto out;
438 if (!bprm->dentry->d_inode->i_op ||
439 !bprm->dentry->d_inode->i_op->default_file_ops ||
440 !bprm->dentry->d_inode->i_op->default_file_ops->mmap)
441 goto out;
443 /* Now read in all of the header information */
445 retval = -ENOMEM;
446 size = elf_ex.e_phentsize * elf_ex.e_phnum;
447 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
448 if (!elf_phdata)
449 goto out;
451 retval = read_exec(bprm->dentry, elf_ex.e_phoff,
452 (char *) elf_phdata, size, 1);
453 if (retval < 0)
454 goto out_free_ph;
456 retval = open_dentry(bprm->dentry, O_RDONLY);
457 if (retval < 0)
458 goto out_free_ph;
459 elf_exec_fileno = retval;
460 file = fget(elf_exec_fileno);
462 elf_ppnt = elf_phdata;
463 elf_bss = 0;
464 elf_brk = 0;
466 start_code = ~0UL;
467 end_code = 0;
468 end_data = 0;
470 for (i = 0; i < elf_ex.e_phnum; i++) {
471 if (elf_ppnt->p_type == PT_INTERP) {
472 retval = -EINVAL;
473 if (elf_interpreter)
474 goto out_free_interp;
476 /* This is the program interpreter used for
477 * shared libraries - for now assume that this
478 * is an a.out format binary
481 retval = -ENOMEM;
482 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
483 GFP_KERNEL);
484 if (!elf_interpreter)
485 goto out_free_file;
487 retval = read_exec(bprm->dentry, elf_ppnt->p_offset,
488 elf_interpreter,
489 elf_ppnt->p_filesz, 1);
490 if (retval < 0)
491 goto out_free_interp;
492 /* If the program interpreter is one of these two,
493 * then assume an iBCS2 image. Otherwise assume
494 * a native linux image.
496 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
497 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
498 ibcs2_interpreter = 1;
499 #if 0
500 printk("Using ELF interpreter %s\n", elf_interpreter);
501 #endif
502 old_fs = get_fs(); /* This could probably be optimized */
503 set_fs(get_ds());
504 #ifdef __sparc__
505 if (ibcs2_interpreter) {
506 unsigned long old_pers = current->personality;
508 current->personality = PER_SVR4;
509 interpreter_dentry = open_namei(elf_interpreter,
510 0, 0);
511 current->personality = old_pers;
512 } else
513 #endif
514 interpreter_dentry = open_namei(elf_interpreter,
515 0, 0);
516 set_fs(old_fs);
517 retval = PTR_ERR(interpreter_dentry);
518 if (IS_ERR(interpreter_dentry))
519 goto out_free_interp;
520 retval = permission(interpreter_dentry->d_inode, MAY_EXEC);
521 if (retval < 0)
522 goto out_free_dentry;
523 retval = read_exec(interpreter_dentry, 0, bprm->buf, 128, 1);
524 if (retval < 0)
525 goto out_free_dentry;
527 /* Get the exec headers */
528 interp_ex = *((struct exec *) bprm->buf);
529 interp_elf_ex = *((struct elfhdr *) bprm->buf);
531 elf_ppnt++;
534 /* Some simple consistency checks for the interpreter */
535 if (elf_interpreter) {
536 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
538 /* Now figure out which format our binary is */
539 if ((N_MAGIC(interp_ex) != OMAGIC) &&
540 (N_MAGIC(interp_ex) != ZMAGIC) &&
541 (N_MAGIC(interp_ex) != QMAGIC))
542 interpreter_type = INTERPRETER_ELF;
544 if (interp_elf_ex.e_ident[0] != 0x7f ||
545 strncmp(&interp_elf_ex.e_ident[1], "ELF", 3) != 0)
546 interpreter_type &= ~INTERPRETER_ELF;
548 retval = -ELIBBAD;
549 if (!interpreter_type)
550 goto out_free_dentry;
552 /* Make sure only one type was selected */
553 if ((interpreter_type & INTERPRETER_ELF) &&
554 interpreter_type != INTERPRETER_ELF) {
555 printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
556 interpreter_type = INTERPRETER_ELF;
560 /* OK, we are done with that, now set up the arg stuff,
561 and then start this sucker up */
563 if (!bprm->sh_bang) {
564 char * passed_p;
566 if (interpreter_type == INTERPRETER_AOUT) {
567 sprintf(passed_fileno, "%d", elf_exec_fileno);
568 passed_p = passed_fileno;
570 if (elf_interpreter) {
571 bprm->p = copy_strings(1,&passed_p,bprm->page,bprm->p,2);
572 bprm->argc++;
575 retval = -E2BIG;
576 if (!bprm->p)
577 goto out_free_dentry;
580 /* Flush all traces of the currently running executable */
581 retval = flush_old_exec(bprm);
582 if (retval)
583 goto out_free_dentry;
585 /* OK, This is the point of no return */
586 current->mm->end_data = 0;
587 current->mm->end_code = 0;
588 current->mm->mmap = NULL;
589 current->flags &= ~PF_FORKNOEXEC;
590 elf_entry = (unsigned long) elf_ex.e_entry;
592 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
593 may depend on the personality. */
594 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
596 /* Do this so that we can load the interpreter, if need be. We will
597 change some of these later */
598 current->mm->rss = 0;
599 bprm->p = setup_arg_pages(bprm->p, bprm);
600 current->mm->start_stack = bprm->p;
602 /* Try and get dynamic programs out of the way of the default mmap
603 base, as well as whatever program they might try to exec. This
604 is because the brk will follow the loader, and is not movable. */
606 load_bias = (elf_ex.e_type == ET_DYN ? ELF_ET_DYN_BASE : 0);
608 /* Now we do a little grungy work by mmaping the ELF image into
609 the correct location in memory. At this point, we assume that
610 the image should be loaded at fixed address, not at a variable
611 address. */
613 old_fs = get_fs();
614 set_fs(get_ds());
615 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
616 if (elf_ppnt->p_type == PT_LOAD) {
617 int elf_prot = 0, elf_flags;
618 unsigned long vaddr = 0;
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 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
627 vaddr = elf_ppnt->p_vaddr;
628 elf_flags |= MAP_FIXED;
631 error = do_mmap(file,
632 ELF_PAGESTART(load_bias + vaddr),
633 (elf_ppnt->p_filesz +
634 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
635 elf_prot, elf_flags,
636 (elf_ppnt->p_offset -
637 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
639 if (!load_addr_set) {
640 load_addr_set = 1;
641 load_addr = (elf_ppnt->p_vaddr -
642 elf_ppnt->p_offset);
643 if (elf_ex.e_type == ET_DYN) {
644 load_bias = error;
645 load_addr += error;
648 k = elf_ppnt->p_vaddr;
649 if (k < start_code) start_code = k;
650 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
651 if (k > elf_bss)
652 elf_bss = k;
653 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
654 end_code = k;
655 if (end_data < k)
656 end_data = k;
657 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
658 if (k > elf_brk)
659 elf_brk = k;
662 set_fs(old_fs);
663 fput(file); /* all done with the file */
665 elf_entry += load_bias;
666 elf_bss += load_bias;
667 elf_brk += load_bias;
668 start_code += load_bias;
669 end_code += load_bias;
670 end_data += load_bias;
672 if (elf_interpreter) {
673 if (interpreter_type == INTERPRETER_AOUT)
674 elf_entry = load_aout_interp(&interp_ex,
675 interpreter_dentry);
676 else
677 elf_entry = load_elf_interp(&interp_elf_ex,
678 interpreter_dentry,
679 &interp_load_addr);
681 dput(interpreter_dentry);
682 kfree(elf_interpreter);
684 if (elf_entry == ~0UL) {
685 printk(KERN_ERR "Unable to load interpreter\n");
686 kfree(elf_phdata);
687 send_sig(SIGSEGV, current, 0);
688 return 0;
692 kfree(elf_phdata);
694 if (interpreter_type != INTERPRETER_AOUT)
695 sys_close(elf_exec_fileno);
697 if (current->exec_domain && current->exec_domain->module)
698 __MOD_DEC_USE_COUNT(current->exec_domain->module);
699 if (current->binfmt && current->binfmt->module)
700 __MOD_DEC_USE_COUNT(current->binfmt->module);
701 current->exec_domain = lookup_exec_domain(current->personality);
702 current->binfmt = &elf_format;
703 if (current->exec_domain && current->exec_domain->module)
704 __MOD_INC_USE_COUNT(current->exec_domain->module);
705 if (current->binfmt && current->binfmt->module)
706 __MOD_INC_USE_COUNT(current->binfmt->module);
708 #ifndef VM_STACK_FLAGS
709 current->executable = dget(bprm->dentry);
710 #endif
711 compute_creds(bprm);
712 current->flags &= ~PF_FORKNOEXEC;
713 bprm->p = (unsigned long)
714 create_elf_tables((char *)bprm->p,
715 bprm->argc,
716 bprm->envc,
717 (interpreter_type == INTERPRETER_ELF ? &elf_ex : NULL),
718 load_addr, load_bias,
719 interp_load_addr,
720 (interpreter_type == INTERPRETER_AOUT ? 0 : 1));
721 /* N.B. passed_fileno might not be initialized? */
722 if (interpreter_type == INTERPRETER_AOUT)
723 current->mm->arg_start += strlen(passed_fileno) + 1;
724 current->mm->start_brk = current->mm->brk = elf_brk;
725 current->mm->end_code = end_code;
726 current->mm->start_code = start_code;
727 current->mm->end_data = end_data;
728 current->mm->start_stack = bprm->p;
730 /* Calling set_brk effectively mmaps the pages that we need
731 * for the bss and break sections
733 set_brk(elf_bss, elf_brk);
735 padzero(elf_bss);
737 #if 0
738 printk("(start_brk) %x\n" , current->mm->start_brk);
739 printk("(end_code) %x\n" , current->mm->end_code);
740 printk("(start_code) %x\n" , current->mm->start_code);
741 printk("(end_data) %x\n" , current->mm->end_data);
742 printk("(start_stack) %x\n" , current->mm->start_stack);
743 printk("(brk) %x\n" , current->mm->brk);
744 #endif
746 if ( current->personality == PER_SVR4 )
748 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
749 and some applications "depend" upon this behavior.
750 Since we do not have the power to recompile these, we
751 emulate the SVr4 behavior. Sigh. */
752 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
753 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
754 MAP_FIXED | MAP_PRIVATE, 0);
757 #ifdef ELF_PLAT_INIT
759 * The ABI may specify that certain registers be set up in special
760 * ways (on i386 %edx is the address of a DT_FINI function, for
761 * example. This macro performs whatever initialization to
762 * the regs structure is required.
764 ELF_PLAT_INIT(regs);
765 #endif
767 start_thread(regs, elf_entry, bprm->p);
768 if (current->flags & PF_PTRACED)
769 send_sig(SIGTRAP, current, 0);
770 retval = 0;
771 out:
772 return retval;
774 /* error cleanup */
775 out_free_dentry:
776 dput(interpreter_dentry);
777 out_free_interp:
778 if (elf_interpreter)
779 kfree(elf_interpreter);
780 out_free_file:
781 fput(file);
782 sys_close(elf_exec_fileno);
783 out_free_ph:
784 kfree(elf_phdata);
785 goto out;
788 static int
789 load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
791 int retval;
793 MOD_INC_USE_COUNT;
794 retval = do_load_elf_binary(bprm, regs);
795 MOD_DEC_USE_COUNT;
796 return retval;
799 /* This is really simpleminded and specialized - we are loading an
800 a.out library that is given an ELF header. */
802 static inline int
803 do_load_elf_library(int fd)
805 struct file * file;
806 struct dentry * dentry;
807 struct inode * inode;
808 struct elf_phdr *elf_phdata;
809 unsigned long elf_bss = 0, bss, len, k;
810 int retval, error, i, j;
811 struct elfhdr elf_ex;
812 loff_t offset = 0;
814 error = -EACCES;
815 file = fget(fd);
816 if (!file || !file->f_op)
817 goto out;
818 dentry = file->f_dentry;
819 inode = dentry->d_inode;
821 /* seek to the beginning of the file */
822 error = -ENOEXEC;
824 /* N.B. save current DS?? */
825 set_fs(KERNEL_DS);
826 retval = file->f_op->read(file, (char *) &elf_ex, sizeof(elf_ex), &offset);
827 set_fs(USER_DS);
828 if (retval != sizeof(elf_ex))
829 goto out_putf;
831 if (elf_ex.e_ident[0] != 0x7f ||
832 strncmp(&elf_ex.e_ident[1], "ELF", 3) != 0)
833 goto out_putf;
835 /* First of all, some simple consistency checks */
836 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
837 !elf_check_arch(elf_ex.e_machine) ||
838 (!inode->i_op || !inode->i_op->default_file_ops->mmap))
839 goto out_putf;
841 /* Now read in all of the header information */
843 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
844 if (j > ELF_EXEC_PAGESIZE)
845 goto out_putf;
847 error = -ENOMEM;
848 elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
849 if (!elf_phdata)
850 goto out_putf;
852 /* N.B. check for error return?? */
853 retval = read_exec(dentry, elf_ex.e_phoff, (char *) elf_phdata,
854 sizeof(struct elf_phdr) * elf_ex.e_phnum, 1);
856 error = -ENOEXEC;
857 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
858 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
859 if (j != 1)
860 goto out_free_ph;
862 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
864 /* Now use mmap to map the library into memory. */
865 error = do_mmap(file,
866 ELF_PAGESTART(elf_phdata->p_vaddr),
867 (elf_phdata->p_filesz +
868 ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
869 PROT_READ | PROT_WRITE | PROT_EXEC,
870 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
871 (elf_phdata->p_offset -
872 ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
873 if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
874 goto out_free_ph;
876 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
877 if (k > elf_bss)
878 elf_bss = k;
879 padzero(elf_bss);
881 len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr +
882 ELF_EXEC_PAGESIZE - 1);
883 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
884 if (bss > len)
885 do_mmap(NULL, len, bss - len,
886 PROT_READ|PROT_WRITE|PROT_EXEC,
887 MAP_FIXED|MAP_PRIVATE, 0);
888 error = 0;
890 out_free_ph:
891 kfree(elf_phdata);
892 out_putf:
893 fput(file);
894 out:
895 return error;
898 static int load_elf_library(int fd)
900 int retval;
902 MOD_INC_USE_COUNT;
903 retval = do_load_elf_library(fd);
904 MOD_DEC_USE_COUNT;
905 return retval;
909 * Note that some platforms still use traditional core dumps and not
910 * the ELF core dump. Each platform can select it as appropriate.
912 #ifdef USE_ELF_CORE_DUMP
915 * ELF core dumper
917 * Modelled on fs/exec.c:aout_core_dump()
918 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
921 * These are the only things you should do on a core-file: use only these
922 * functions to write out all the necessary info.
924 static int dump_write(struct file *file, const void *addr, int nr)
926 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
929 static int dump_seek(struct file *file, off_t off)
931 if (file->f_op->llseek) {
932 if (file->f_op->llseek(file, off, 0) != off)
933 return 0;
934 } else
935 file->f_pos = off;
936 return 1;
940 * Decide whether a segment is worth dumping; default is yes to be
941 * sure (missing info is worse than too much; etc).
942 * Personally I'd include everything, and use the coredump limit...
944 * I think we should skip something. But I am not sure how. H.J.
946 static inline int maydump(struct vm_area_struct *vma)
948 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
949 return 0;
951 /* Do not dump I/O mapped devices! -DaveM */
952 if(vma->vm_flags & VM_IO)
953 return 0;
954 #if 1
955 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
956 return 1;
957 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
958 return 0;
959 #endif
960 return 1;
963 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
965 /* An ELF note in memory */
966 struct memelfnote
968 const char *name;
969 int type;
970 unsigned int datasz;
971 void *data;
974 static int notesize(struct memelfnote *en)
976 int sz;
978 sz = sizeof(struct elf_note);
979 sz += roundup(strlen(en->name), 4);
980 sz += roundup(en->datasz, 4);
982 return sz;
985 /* #define DEBUG */
987 #ifdef DEBUG
988 static void dump_regs(const char *str, elf_greg_t *r)
990 int i;
991 static const char *regs[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
992 "eax", "ds", "es", "fs", "gs",
993 "orig_eax", "eip", "cs",
994 "efl", "uesp", "ss"};
995 printk("Registers: %s\n", str);
997 for(i = 0; i < ELF_NGREG; i++)
999 unsigned long val = r[i];
1000 printk(" %-2d %-5s=%08lx %lu\n", i, regs[i], val, val);
1003 #endif
1005 #define DUMP_WRITE(addr, nr) \
1006 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1007 #define DUMP_SEEK(off) \
1008 do { if (!dump_seek(file, (off))) return 0; } while(0)
1010 static int writenote(struct memelfnote *men, struct file *file)
1012 struct elf_note en;
1014 en.n_namesz = strlen(men->name);
1015 en.n_descsz = men->datasz;
1016 en.n_type = men->type;
1018 DUMP_WRITE(&en, sizeof(en));
1019 DUMP_WRITE(men->name, en.n_namesz);
1020 /* XXX - cast from long long to long to avoid need for libgcc.a */
1021 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1022 DUMP_WRITE(men->data, men->datasz);
1023 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1025 return 1;
1027 #undef DUMP_WRITE
1028 #undef DUMP_SEEK
1030 #define DUMP_WRITE(addr, nr) \
1031 if (!dump_write(&file, (addr), (nr))) \
1032 goto close_coredump;
1033 #define DUMP_SEEK(off) \
1034 if (!dump_seek(&file, (off))) \
1035 goto close_coredump;
1037 * Actual dumper
1039 * This is a two-pass process; first we find the offsets of the bits,
1040 * and then they are actually written out. If we run out of core limit
1041 * we just truncate.
1043 static int elf_core_dump(long signr, struct pt_regs * regs)
1045 int has_dumped = 0;
1046 struct file file;
1047 struct dentry *dentry;
1048 struct inode *inode;
1049 mm_segment_t fs;
1050 char corefile[6+sizeof(current->comm)];
1051 int segs;
1052 int i;
1053 size_t size;
1054 struct vm_area_struct *vma;
1055 struct elfhdr elf;
1056 off_t offset = 0, dataoff;
1057 long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1058 int numnote = 4;
1059 struct memelfnote notes[4];
1060 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1061 elf_fpregset_t fpu; /* NT_PRFPREG */
1062 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1064 if (!current->dumpable ||
1065 limit < ELF_EXEC_PAGESIZE ||
1066 atomic_read(&current->mm->count) != 1)
1067 return 0;
1068 current->dumpable = 0;
1070 #ifndef CONFIG_BINFMT_ELF
1071 MOD_INC_USE_COUNT;
1072 #endif
1074 /* Count what's needed to dump, up to the limit of coredump size */
1075 segs = 0;
1076 size = 0;
1077 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1078 if (maydump(vma))
1080 int sz = vma->vm_end-vma->vm_start;
1082 if (size+sz >= limit)
1083 break;
1084 else
1085 size += sz;
1088 segs++;
1090 #ifdef DEBUG
1091 printk("elf_core_dump: %d segs taking %d bytes\n", segs, size);
1092 #endif
1094 /* Set up header */
1095 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1096 elf.e_ident[EI_CLASS] = ELF_CLASS;
1097 elf.e_ident[EI_DATA] = ELF_DATA;
1098 elf.e_ident[EI_VERSION] = EV_CURRENT;
1099 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1101 elf.e_type = ET_CORE;
1102 elf.e_machine = ELF_ARCH;
1103 elf.e_version = EV_CURRENT;
1104 elf.e_entry = 0;
1105 elf.e_phoff = sizeof(elf);
1106 elf.e_shoff = 0;
1107 elf.e_flags = 0;
1108 elf.e_ehsize = sizeof(elf);
1109 elf.e_phentsize = sizeof(struct elf_phdr);
1110 elf.e_phnum = segs+1; /* Include notes */
1111 elf.e_shentsize = 0;
1112 elf.e_shnum = 0;
1113 elf.e_shstrndx = 0;
1115 fs = get_fs();
1116 set_fs(KERNEL_DS);
1117 memcpy(corefile,"core.",5);
1118 #if 0
1119 memcpy(corefile+5,current->comm,sizeof(current->comm));
1120 #else
1121 corefile[4] = '\0';
1122 #endif
1123 dentry = open_namei(corefile, O_CREAT | 2 | O_TRUNC, 0600);
1124 if (IS_ERR(dentry)) {
1125 dentry = NULL;
1126 goto end_coredump;
1128 inode = dentry->d_inode;
1129 if (!S_ISREG(inode->i_mode))
1130 goto end_coredump;
1131 if (!inode->i_op || !inode->i_op->default_file_ops)
1132 goto end_coredump;
1133 if (init_private_file(&file, dentry, 3))
1134 goto end_coredump;
1135 if (!file.f_op->write)
1136 goto close_coredump;
1137 has_dumped = 1;
1138 current->flags |= PF_DUMPCORE;
1140 DUMP_WRITE(&elf, sizeof(elf));
1141 offset += sizeof(elf); /* Elf header */
1142 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1145 * Set up the notes in similar form to SVR4 core dumps made
1146 * with info from their /proc.
1148 memset(&psinfo, 0, sizeof(psinfo));
1149 memset(&prstatus, 0, sizeof(prstatus));
1151 notes[0].name = "CORE";
1152 notes[0].type = NT_PRSTATUS;
1153 notes[0].datasz = sizeof(prstatus);
1154 notes[0].data = &prstatus;
1155 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1156 prstatus.pr_sigpend = current->signal.sig[0];
1157 prstatus.pr_sighold = current->blocked.sig[0];
1158 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1159 psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1160 psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1161 psinfo.pr_sid = prstatus.pr_sid = current->session;
1162 prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
1163 prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
1164 prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
1165 prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
1166 prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
1167 prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
1168 prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
1169 prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
1172 * This transfers the registers from regs into the standard
1173 * coredump arrangement, whatever that is.
1175 #ifdef ELF_CORE_COPY_REGS
1176 ELF_CORE_COPY_REGS(prstatus.pr_reg, regs)
1177 #else
1178 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs))
1180 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1181 (long)sizeof(elf_gregset_t), (long)sizeof(struct pt_regs));
1183 else
1184 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1185 #endif
1187 #ifdef DEBUG
1188 dump_regs("Passed in regs", (elf_greg_t *)regs);
1189 dump_regs("prstatus regs", (elf_greg_t *)&prstatus.pr_reg);
1190 #endif
1192 notes[1].name = "CORE";
1193 notes[1].type = NT_PRPSINFO;
1194 notes[1].datasz = sizeof(psinfo);
1195 notes[1].data = &psinfo;
1196 psinfo.pr_state = current->state;
1197 psinfo.pr_sname = (current->state < 0 || current->state > 5) ? '.' : "RSDZTD"[current->state];
1198 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1199 psinfo.pr_nice = current->priority-15;
1200 psinfo.pr_flag = current->flags;
1201 psinfo.pr_uid = current->uid;
1202 psinfo.pr_gid = current->gid;
1204 int i, len;
1206 set_fs(fs);
1208 len = current->mm->arg_end - current->mm->arg_start;
1209 if (len >= ELF_PRARGSZ)
1210 len = ELF_PRARGSZ-1;
1211 copy_from_user(&psinfo.pr_psargs,
1212 (const char *)current->mm->arg_start, len);
1213 for(i = 0; i < len; i++)
1214 if (psinfo.pr_psargs[i] == 0)
1215 psinfo.pr_psargs[i] = ' ';
1216 psinfo.pr_psargs[len] = 0;
1218 set_fs(KERNEL_DS);
1220 strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1222 notes[2].name = "CORE";
1223 notes[2].type = NT_TASKSTRUCT;
1224 notes[2].datasz = sizeof(*current);
1225 notes[2].data = current;
1227 /* Try to dump the FPU. */
1228 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1229 if (!prstatus.pr_fpvalid)
1231 numnote--;
1233 else
1235 notes[3].name = "CORE";
1236 notes[3].type = NT_PRFPREG;
1237 notes[3].datasz = sizeof(fpu);
1238 notes[3].data = &fpu;
1241 /* Write notes phdr entry */
1243 struct elf_phdr phdr;
1244 int sz = 0;
1246 for(i = 0; i < numnote; i++)
1247 sz += notesize(&notes[i]);
1249 phdr.p_type = PT_NOTE;
1250 phdr.p_offset = offset;
1251 phdr.p_vaddr = 0;
1252 phdr.p_paddr = 0;
1253 phdr.p_filesz = sz;
1254 phdr.p_memsz = 0;
1255 phdr.p_flags = 0;
1256 phdr.p_align = 0;
1258 offset += phdr.p_filesz;
1259 DUMP_WRITE(&phdr, sizeof(phdr));
1262 /* Page-align dumped data */
1263 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1265 /* Write program headers for segments dump */
1266 for(vma = current->mm->mmap, i = 0;
1267 i < segs && vma != NULL; vma = vma->vm_next) {
1268 struct elf_phdr phdr;
1269 size_t sz;
1271 i++;
1273 sz = vma->vm_end - vma->vm_start;
1275 phdr.p_type = PT_LOAD;
1276 phdr.p_offset = offset;
1277 phdr.p_vaddr = vma->vm_start;
1278 phdr.p_paddr = 0;
1279 phdr.p_filesz = maydump(vma) ? sz : 0;
1280 phdr.p_memsz = sz;
1281 offset += phdr.p_filesz;
1282 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1283 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1284 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1285 phdr.p_align = ELF_EXEC_PAGESIZE;
1287 DUMP_WRITE(&phdr, sizeof(phdr));
1290 for(i = 0; i < numnote; i++)
1291 if (!writenote(&notes[i], &file))
1292 goto close_coredump;
1294 set_fs(fs);
1296 DUMP_SEEK(dataoff);
1298 for(i = 0, vma = current->mm->mmap;
1299 i < segs && vma != NULL;
1300 vma = vma->vm_next) {
1301 unsigned long addr = vma->vm_start;
1302 unsigned long len = vma->vm_end - vma->vm_start;
1304 i++;
1305 if (!maydump(vma))
1306 continue;
1307 #ifdef DEBUG
1308 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1309 #endif
1310 DUMP_WRITE((void *)addr, len);
1313 if ((off_t) file.f_pos != offset) {
1314 /* Sanity check */
1315 printk("elf_core_dump: file.f_pos (%ld) != offset (%ld)\n",
1316 (off_t) file.f_pos, offset);
1319 close_coredump:
1320 if (file.f_op->release)
1321 file.f_op->release(inode,&file);
1323 end_coredump:
1324 set_fs(fs);
1325 dput(dentry);
1326 #ifndef CONFIG_BINFMT_ELF
1327 MOD_DEC_USE_COUNT;
1328 #endif
1329 return has_dumped;
1331 #endif /* USE_ELF_CORE_DUMP */
1333 __initfunc(int init_elf_binfmt(void))
1335 return register_binfmt(&elf_format);
1338 #ifdef MODULE
1340 int init_module(void)
1342 /* Install the COFF, ELF and XOUT loaders.
1343 * N.B. We *rely* on the table being the right size with the
1344 * right number of free slots...
1346 return init_elf_binfmt();
1350 void cleanup_module( void)
1352 /* Remove the COFF and ELF loaders. */
1353 unregister_binfmt(&elf_format);
1355 #endif