Linux 2.3.0
[davej-history.git] / fs / binfmt_elf.c
blob82f75d1e65f8327accf042663dc260a5a090bbe0
1 /*
2 * linux/fs/binfmt_elf.c
4 * These are the functions used to load ELF format executables as used
5 * on SVr4 machines. Information on the format may be found in the book
6 * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
7 * Tools".
9 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
12 #include <linux/module.h>
14 #include <linux/fs.h>
15 #include <linux/stat.h>
16 #include <linux/sched.h>
17 #include <linux/mm.h>
18 #include <linux/mman.h>
19 #include <linux/a.out.h>
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/binfmts.h>
23 #include <linux/string.h>
24 #include <linux/file.h>
25 #include <linux/fcntl.h>
26 #include <linux/ptrace.h>
27 #include <linux/malloc.h>
28 #include <linux/shm.h>
29 #include <linux/personality.h>
30 #include <linux/elfcore.h>
31 #include <linux/init.h>
33 #include <asm/uaccess.h>
34 #include <asm/pgtable.h>
36 #include <linux/config.h>
38 #define DLINFO_ITEMS 13
40 #include <linux/elf.h>
42 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs);
43 static int load_elf_library(int fd);
44 extern int dump_fpu (struct pt_regs *, elf_fpregset_t *);
45 extern void dump_thread(struct pt_regs *, struct user *);
47 #ifndef elf_addr_t
48 #define elf_addr_t unsigned long
49 #define elf_caddr_t char *
50 #endif
53 * If we don't support core dumping, then supply a NULL so we
54 * don't even try.
56 #ifdef USE_ELF_CORE_DUMP
57 static int elf_core_dump(long signr, struct pt_regs * regs);
58 #else
59 #define elf_core_dump NULL
60 #endif
62 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_EXEC_PAGESIZE-1))
63 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_EXEC_PAGESIZE-1))
64 #define ELF_PAGEALIGN(_v) (((_v) + ELF_EXEC_PAGESIZE - 1) & ~(ELF_EXEC_PAGESIZE - 1))
66 static struct linux_binfmt elf_format = {
67 #ifndef MODULE
68 NULL, NULL, load_elf_binary, load_elf_library, elf_core_dump
69 #else
70 NULL, &__this_module, load_elf_binary, load_elf_library, elf_core_dump
71 #endif
74 static void set_brk(unsigned long start, unsigned long end)
76 start = ELF_PAGEALIGN(start);
77 end = ELF_PAGEALIGN(end);
78 if (end <= start)
79 return;
80 do_mmap(NULL, start, end - start,
81 PROT_READ | PROT_WRITE | PROT_EXEC,
82 MAP_FIXED | MAP_PRIVATE, 0);
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_mmap(NULL, elf_bss, last_bss - elf_bss,
332 PROT_READ|PROT_WRITE|PROT_EXEC,
333 MAP_FIXED|MAP_PRIVATE, 0);
335 *interp_load_addr = load_addr;
336 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
338 out_close:
339 fput(file);
340 sys_close(elf_exec_fileno);
341 out_free:
342 kfree(elf_phdata);
343 out:
344 return error;
347 static unsigned long load_aout_interp(struct exec * interp_ex,
348 struct dentry * interpreter_dentry)
350 unsigned long text_data, offset, elf_entry = ~0UL;
351 char * addr;
352 int retval;
354 current->mm->end_code = interp_ex->a_text;
355 text_data = interp_ex->a_text + interp_ex->a_data;
356 current->mm->end_data = text_data;
357 current->mm->brk = interp_ex->a_bss + text_data;
359 switch (N_MAGIC(*interp_ex)) {
360 case OMAGIC:
361 offset = 32;
362 addr = (char *) 0;
363 break;
364 case ZMAGIC:
365 case QMAGIC:
366 offset = N_TXTOFF(*interp_ex);
367 addr = (char *) N_TXTADDR(*interp_ex);
368 break;
369 default:
370 goto out;
373 do_mmap(NULL, 0, text_data,
374 PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED|MAP_PRIVATE, 0);
375 retval = read_exec(interpreter_dentry, offset, addr, text_data, 0);
376 if (retval < 0)
377 goto out;
378 flush_icache_range((unsigned long)addr,
379 (unsigned long)addr + text_data);
381 do_mmap(NULL, ELF_PAGESTART(text_data + ELF_EXEC_PAGESIZE - 1),
382 interp_ex->a_bss,
383 PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED|MAP_PRIVATE, 0);
384 elf_entry = interp_ex->a_entry;
386 out:
387 return elf_entry;
391 * These are the functions used to load ELF style executables and shared
392 * libraries. There is no binary dependent code anywhere else.
395 #define INTERPRETER_NONE 0
396 #define INTERPRETER_AOUT 1
397 #define INTERPRETER_ELF 2
400 static inline int
401 do_load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
403 struct file * file;
404 struct dentry *interpreter_dentry = NULL; /* to shut gcc up */
405 unsigned long load_addr = 0, load_bias;
406 int load_addr_set = 0;
407 char * elf_interpreter = NULL;
408 unsigned int interpreter_type = INTERPRETER_NONE;
409 unsigned char ibcs2_interpreter = 0;
410 mm_segment_t old_fs;
411 unsigned long error;
412 struct elf_phdr * elf_ppnt, *elf_phdata;
413 unsigned long elf_bss, k, elf_brk;
414 int elf_exec_fileno;
415 int retval, size, i;
416 unsigned long elf_entry, interp_load_addr = 0;
417 unsigned long start_code, end_code, end_data;
418 struct elfhdr elf_ex;
419 struct elfhdr interp_elf_ex;
420 struct exec interp_ex;
421 char passed_fileno[6];
423 /* Get the exec-header */
424 elf_ex = *((struct elfhdr *) bprm->buf);
426 retval = -ENOEXEC;
427 /* First of all, some simple consistency checks */
428 if (elf_ex.e_ident[0] != 0x7f ||
429 strncmp(&elf_ex.e_ident[1], "ELF", 3) != 0)
430 goto out;
432 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
433 goto out;
434 if (!elf_check_arch(elf_ex.e_machine))
435 goto out;
436 #ifdef __mips__
437 /* IRIX binaries handled elsewhere. */
438 if (elf_ex.e_flags & EF_MIPS_ARCH) {
439 retval = -ENOEXEC;
440 goto out;
442 #endif
443 if (!bprm->dentry->d_inode->i_op ||
444 !bprm->dentry->d_inode->i_op->default_file_ops ||
445 !bprm->dentry->d_inode->i_op->default_file_ops->mmap)
446 goto out;
448 /* Now read in all of the header information */
450 retval = -ENOMEM;
451 size = elf_ex.e_phentsize * elf_ex.e_phnum;
452 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
453 if (!elf_phdata)
454 goto out;
456 retval = read_exec(bprm->dentry, elf_ex.e_phoff,
457 (char *) elf_phdata, size, 1);
458 if (retval < 0)
459 goto out_free_ph;
461 retval = open_dentry(bprm->dentry, O_RDONLY);
462 if (retval < 0)
463 goto out_free_ph;
464 elf_exec_fileno = retval;
465 file = fget(elf_exec_fileno);
467 elf_ppnt = elf_phdata;
468 elf_bss = 0;
469 elf_brk = 0;
471 start_code = ~0UL;
472 end_code = 0;
473 end_data = 0;
475 for (i = 0; i < elf_ex.e_phnum; i++) {
476 if (elf_ppnt->p_type == PT_INTERP) {
477 retval = -EINVAL;
478 if (elf_interpreter)
479 goto out_free_interp;
481 /* This is the program interpreter used for
482 * shared libraries - for now assume that this
483 * is an a.out format binary
486 retval = -ENOMEM;
487 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
488 GFP_KERNEL);
489 if (!elf_interpreter)
490 goto out_free_file;
492 retval = read_exec(bprm->dentry, elf_ppnt->p_offset,
493 elf_interpreter,
494 elf_ppnt->p_filesz, 1);
495 if (retval < 0)
496 goto out_free_interp;
497 /* If the program interpreter is one of these two,
498 * then assume an iBCS2 image. Otherwise assume
499 * a native linux image.
501 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
502 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
503 ibcs2_interpreter = 1;
504 #if 0
505 printk("Using ELF interpreter %s\n", elf_interpreter);
506 #endif
507 old_fs = get_fs(); /* This could probably be optimized */
508 set_fs(get_ds());
509 #ifdef __sparc__
510 if (ibcs2_interpreter) {
511 unsigned long old_pers = current->personality;
513 current->personality = PER_SVR4;
514 interpreter_dentry = open_namei(elf_interpreter,
515 0, 0);
516 current->personality = old_pers;
517 } else
518 #endif
519 interpreter_dentry = open_namei(elf_interpreter,
520 0, 0);
521 set_fs(old_fs);
522 retval = PTR_ERR(interpreter_dentry);
523 if (IS_ERR(interpreter_dentry))
524 goto out_free_interp;
525 retval = permission(interpreter_dentry->d_inode, MAY_EXEC);
526 if (retval < 0)
527 goto out_free_dentry;
528 retval = read_exec(interpreter_dentry, 0, bprm->buf, 128, 1);
529 if (retval < 0)
530 goto out_free_dentry;
532 /* Get the exec headers */
533 interp_ex = *((struct exec *) bprm->buf);
534 interp_elf_ex = *((struct elfhdr *) bprm->buf);
536 elf_ppnt++;
539 /* Some simple consistency checks for the interpreter */
540 if (elf_interpreter) {
541 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
543 /* Now figure out which format our binary is */
544 if ((N_MAGIC(interp_ex) != OMAGIC) &&
545 (N_MAGIC(interp_ex) != ZMAGIC) &&
546 (N_MAGIC(interp_ex) != QMAGIC))
547 interpreter_type = INTERPRETER_ELF;
549 if (interp_elf_ex.e_ident[0] != 0x7f ||
550 strncmp(&interp_elf_ex.e_ident[1], "ELF", 3) != 0)
551 interpreter_type &= ~INTERPRETER_ELF;
553 retval = -ELIBBAD;
554 if (!interpreter_type)
555 goto out_free_dentry;
557 /* Make sure only one type was selected */
558 if ((interpreter_type & INTERPRETER_ELF) &&
559 interpreter_type != INTERPRETER_ELF) {
560 printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
561 interpreter_type = INTERPRETER_ELF;
565 /* OK, we are done with that, now set up the arg stuff,
566 and then start this sucker up */
568 if (!bprm->sh_bang) {
569 char * passed_p;
571 if (interpreter_type == INTERPRETER_AOUT) {
572 sprintf(passed_fileno, "%d", elf_exec_fileno);
573 passed_p = passed_fileno;
575 if (elf_interpreter) {
576 bprm->p = copy_strings(1,&passed_p,bprm->page,bprm->p,2);
577 bprm->argc++;
580 retval = -E2BIG;
581 if (!bprm->p)
582 goto out_free_dentry;
585 /* Flush all traces of the currently running executable */
586 retval = flush_old_exec(bprm);
587 if (retval)
588 goto out_free_dentry;
590 /* OK, This is the point of no return */
591 current->mm->end_data = 0;
592 current->mm->end_code = 0;
593 current->mm->mmap = NULL;
594 current->flags &= ~PF_FORKNOEXEC;
595 elf_entry = (unsigned long) elf_ex.e_entry;
597 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
598 may depend on the personality. */
599 SET_PERSONALITY(elf_ex, ibcs2_interpreter);
601 /* Do this so that we can load the interpreter, if need be. We will
602 change some of these later */
603 current->mm->rss = 0;
604 bprm->p = setup_arg_pages(bprm->p, bprm);
605 current->mm->start_stack = bprm->p;
607 /* Try and get dynamic programs out of the way of the default mmap
608 base, as well as whatever program they might try to exec. This
609 is because the brk will follow the loader, and is not movable. */
611 load_bias = ELF_PAGESTART(elf_ex.e_type==ET_DYN ? ELF_ET_DYN_BASE : 0);
613 /* Now we do a little grungy work by mmaping the ELF image into
614 the correct location in memory. At this point, we assume that
615 the image should be loaded at fixed address, not at a variable
616 address. */
618 old_fs = get_fs();
619 set_fs(get_ds());
620 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
621 int elf_prot = 0, elf_flags;
622 unsigned long vaddr;
624 if (elf_ppnt->p_type != PT_LOAD)
625 continue;
627 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
628 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
629 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
631 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
633 vaddr = elf_ppnt->p_vaddr;
634 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
635 elf_flags |= MAP_FIXED;
638 error = do_mmap(file, ELF_PAGESTART(load_bias + vaddr),
639 (elf_ppnt->p_filesz +
640 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
641 elf_prot, elf_flags, (elf_ppnt->p_offset -
642 ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
644 if (!load_addr_set) {
645 load_addr_set = 1;
646 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
647 if (elf_ex.e_type == ET_DYN) {
648 load_bias += error -
649 ELF_PAGESTART(load_bias + vaddr);
650 load_addr += error;
653 k = elf_ppnt->p_vaddr;
654 if (k < start_code) start_code = k;
655 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
656 if (k > elf_bss)
657 elf_bss = k;
658 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
659 end_code = k;
660 if (end_data < k)
661 end_data = k;
662 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
663 if (k > elf_brk)
664 elf_brk = k;
666 set_fs(old_fs);
667 fput(file); /* all done with the file */
669 elf_entry += load_bias;
670 elf_bss += load_bias;
671 elf_brk += load_bias;
672 start_code += load_bias;
673 end_code += load_bias;
674 end_data += load_bias;
676 if (elf_interpreter) {
677 if (interpreter_type == INTERPRETER_AOUT)
678 elf_entry = load_aout_interp(&interp_ex,
679 interpreter_dentry);
680 else
681 elf_entry = load_elf_interp(&interp_elf_ex,
682 interpreter_dentry,
683 &interp_load_addr);
685 dput(interpreter_dentry);
686 kfree(elf_interpreter);
688 if (elf_entry == ~0UL) {
689 printk(KERN_ERR "Unable to load interpreter\n");
690 kfree(elf_phdata);
691 send_sig(SIGSEGV, current, 0);
692 return 0;
696 kfree(elf_phdata);
698 if (interpreter_type != INTERPRETER_AOUT)
699 sys_close(elf_exec_fileno);
701 if (current->exec_domain && current->exec_domain->module)
702 __MOD_DEC_USE_COUNT(current->exec_domain->module);
703 if (current->binfmt && current->binfmt->module)
704 __MOD_DEC_USE_COUNT(current->binfmt->module);
705 current->exec_domain = lookup_exec_domain(current->personality);
706 current->binfmt = &elf_format;
707 if (current->exec_domain && current->exec_domain->module)
708 __MOD_INC_USE_COUNT(current->exec_domain->module);
709 if (current->binfmt && current->binfmt->module)
710 __MOD_INC_USE_COUNT(current->binfmt->module);
712 #ifndef VM_STACK_FLAGS
713 current->executable = dget(bprm->dentry);
714 #endif
715 compute_creds(bprm);
716 current->flags &= ~PF_FORKNOEXEC;
717 bprm->p = (unsigned long)
718 create_elf_tables((char *)bprm->p,
719 bprm->argc,
720 bprm->envc,
721 (interpreter_type == INTERPRETER_ELF ? &elf_ex : NULL),
722 load_addr, load_bias,
723 interp_load_addr,
724 (interpreter_type == INTERPRETER_AOUT ? 0 : 1));
725 /* N.B. passed_fileno might not be initialized? */
726 if (interpreter_type == INTERPRETER_AOUT)
727 current->mm->arg_start += strlen(passed_fileno) + 1;
728 current->mm->start_brk = current->mm->brk = elf_brk;
729 current->mm->end_code = end_code;
730 current->mm->start_code = start_code;
731 current->mm->end_data = end_data;
732 current->mm->start_stack = bprm->p;
734 /* Calling set_brk effectively mmaps the pages that we need
735 * for the bss and break sections
737 set_brk(elf_bss, elf_brk);
739 padzero(elf_bss);
741 #if 0
742 printk("(start_brk) %x\n" , current->mm->start_brk);
743 printk("(end_code) %x\n" , current->mm->end_code);
744 printk("(start_code) %x\n" , current->mm->start_code);
745 printk("(end_data) %x\n" , current->mm->end_data);
746 printk("(start_stack) %x\n" , current->mm->start_stack);
747 printk("(brk) %x\n" , current->mm->brk);
748 #endif
750 if ( current->personality == PER_SVR4 )
752 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
753 and some applications "depend" upon this behavior.
754 Since we do not have the power to recompile these, we
755 emulate the SVr4 behavior. Sigh. */
756 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
757 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
758 MAP_FIXED | MAP_PRIVATE, 0);
761 #ifdef ELF_PLAT_INIT
763 * The ABI may specify that certain registers be set up in special
764 * ways (on i386 %edx is the address of a DT_FINI function, for
765 * example. This macro performs whatever initialization to
766 * the regs structure is required.
768 ELF_PLAT_INIT(regs);
769 #endif
771 start_thread(regs, elf_entry, bprm->p);
772 if (current->flags & PF_PTRACED)
773 send_sig(SIGTRAP, current, 0);
774 retval = 0;
775 out:
776 return retval;
778 /* error cleanup */
779 out_free_dentry:
780 dput(interpreter_dentry);
781 out_free_interp:
782 if (elf_interpreter)
783 kfree(elf_interpreter);
784 out_free_file:
785 fput(file);
786 sys_close(elf_exec_fileno);
787 out_free_ph:
788 kfree(elf_phdata);
789 goto out;
792 static int
793 load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
795 int retval;
797 MOD_INC_USE_COUNT;
798 retval = do_load_elf_binary(bprm, regs);
799 MOD_DEC_USE_COUNT;
800 return retval;
803 /* This is really simpleminded and specialized - we are loading an
804 a.out library that is given an ELF header. */
806 static inline int
807 do_load_elf_library(int fd)
809 struct file * file;
810 struct dentry * dentry;
811 struct inode * inode;
812 struct elf_phdr *elf_phdata;
813 unsigned long elf_bss = 0, bss, len, k;
814 int retval, error, i, j;
815 struct elfhdr elf_ex;
816 loff_t offset = 0;
818 error = -EACCES;
819 file = fget(fd);
820 if (!file || !file->f_op)
821 goto out;
822 dentry = file->f_dentry;
823 inode = dentry->d_inode;
825 /* seek to the beginning of the file */
826 error = -ENOEXEC;
828 /* N.B. save current DS?? */
829 set_fs(KERNEL_DS);
830 retval = file->f_op->read(file, (char *) &elf_ex, sizeof(elf_ex), &offset);
831 set_fs(USER_DS);
832 if (retval != sizeof(elf_ex))
833 goto out_putf;
835 if (elf_ex.e_ident[0] != 0x7f ||
836 strncmp(&elf_ex.e_ident[1], "ELF", 3) != 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_op || !inode->i_op->default_file_ops->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_mmap(NULL, len, bss - len,
890 PROT_READ|PROT_WRITE|PROT_EXEC,
891 MAP_FIXED|MAP_PRIVATE, 0);
892 error = 0;
894 out_free_ph:
895 kfree(elf_phdata);
896 out_putf:
897 fput(file);
898 out:
899 return error;
902 static int load_elf_library(int fd)
904 int retval;
906 MOD_INC_USE_COUNT;
907 retval = do_load_elf_library(fd);
908 MOD_DEC_USE_COUNT;
909 return retval;
913 * Note that some platforms still use traditional core dumps and not
914 * the ELF core dump. Each platform can select it as appropriate.
916 #ifdef USE_ELF_CORE_DUMP
919 * ELF core dumper
921 * Modelled on fs/exec.c:aout_core_dump()
922 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
925 * These are the only things you should do on a core-file: use only these
926 * functions to write out all the necessary info.
928 static int dump_write(struct file *file, const void *addr, int nr)
930 int r;
931 down(&file->f_dentry->d_inode->i_sem);
932 r = file->f_op->write(file, addr, nr, &file->f_pos) == nr;
933 up(&file->f_dentry->d_inode->i_sem);
934 return r;
937 static int dump_seek(struct file *file, off_t off)
939 if (file->f_op->llseek) {
940 if (file->f_op->llseek(file, off, 0) != off)
941 return 0;
942 } else
943 file->f_pos = off;
944 return 1;
948 * Decide whether a segment is worth dumping; default is yes to be
949 * sure (missing info is worse than too much; etc).
950 * Personally I'd include everything, and use the coredump limit...
952 * I think we should skip something. But I am not sure how. H.J.
954 static inline int maydump(struct vm_area_struct *vma)
956 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
957 return 0;
959 /* Do not dump I/O mapped devices! -DaveM */
960 if(vma->vm_flags & VM_IO)
961 return 0;
962 #if 1
963 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
964 return 1;
965 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
966 return 0;
967 #endif
968 return 1;
971 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
973 /* An ELF note in memory */
974 struct memelfnote
976 const char *name;
977 int type;
978 unsigned int datasz;
979 void *data;
982 static int notesize(struct memelfnote *en)
984 int sz;
986 sz = sizeof(struct elf_note);
987 sz += roundup(strlen(en->name), 4);
988 sz += roundup(en->datasz, 4);
990 return sz;
993 /* #define DEBUG */
995 #ifdef DEBUG
996 static void dump_regs(const char *str, elf_greg_t *r)
998 int i;
999 static const char *regs[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
1000 "eax", "ds", "es", "fs", "gs",
1001 "orig_eax", "eip", "cs",
1002 "efl", "uesp", "ss"};
1003 printk("Registers: %s\n", str);
1005 for(i = 0; i < ELF_NGREG; i++)
1007 unsigned long val = r[i];
1008 printk(" %-2d %-5s=%08lx %lu\n", i, regs[i], val, val);
1011 #endif
1013 #define DUMP_WRITE(addr, nr) \
1014 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1015 #define DUMP_SEEK(off) \
1016 do { if (!dump_seek(file, (off))) return 0; } while(0)
1018 static int writenote(struct memelfnote *men, struct file *file)
1020 struct elf_note en;
1022 en.n_namesz = strlen(men->name);
1023 en.n_descsz = men->datasz;
1024 en.n_type = men->type;
1026 DUMP_WRITE(&en, sizeof(en));
1027 DUMP_WRITE(men->name, en.n_namesz);
1028 /* XXX - cast from long long to long to avoid need for libgcc.a */
1029 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1030 DUMP_WRITE(men->data, men->datasz);
1031 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1033 return 1;
1035 #undef DUMP_WRITE
1036 #undef DUMP_SEEK
1038 #define DUMP_WRITE(addr, nr) \
1039 if (!dump_write(file, (addr), (nr))) \
1040 goto close_coredump;
1041 #define DUMP_SEEK(off) \
1042 if (!dump_seek(file, (off))) \
1043 goto close_coredump;
1045 * Actual dumper
1047 * This is a two-pass process; first we find the offsets of the bits,
1048 * and then they are actually written out. If we run out of core limit
1049 * we just truncate.
1051 static int elf_core_dump(long signr, struct pt_regs * regs)
1053 int has_dumped = 0;
1054 struct file *file;
1055 struct dentry *dentry;
1056 struct inode *inode;
1057 mm_segment_t fs;
1058 char corefile[6+sizeof(current->comm)];
1059 int segs;
1060 int i;
1061 size_t size;
1062 struct vm_area_struct *vma;
1063 struct elfhdr elf;
1064 off_t offset = 0, dataoff;
1065 unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1066 int numnote = 4;
1067 struct memelfnote notes[4];
1068 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1069 elf_fpregset_t fpu; /* NT_PRFPREG */
1070 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1072 if (!current->dumpable ||
1073 limit < ELF_EXEC_PAGESIZE ||
1074 atomic_read(&current->mm->count) != 1)
1075 return 0;
1076 current->dumpable = 0;
1078 #ifndef CONFIG_BINFMT_ELF
1079 MOD_INC_USE_COUNT;
1080 #endif
1082 /* Count what's needed to dump, up to the limit of coredump size */
1083 segs = 0;
1084 size = 0;
1085 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1086 if (maydump(vma))
1088 unsigned long sz = vma->vm_end-vma->vm_start;
1090 if (size+sz >= limit)
1091 break;
1092 else
1093 size += sz;
1096 segs++;
1098 #ifdef DEBUG
1099 printk("elf_core_dump: %d segs taking %d bytes\n", segs, size);
1100 #endif
1102 /* Set up header */
1103 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1104 elf.e_ident[EI_CLASS] = ELF_CLASS;
1105 elf.e_ident[EI_DATA] = ELF_DATA;
1106 elf.e_ident[EI_VERSION] = EV_CURRENT;
1107 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1109 elf.e_type = ET_CORE;
1110 elf.e_machine = ELF_ARCH;
1111 elf.e_version = EV_CURRENT;
1112 elf.e_entry = 0;
1113 elf.e_phoff = sizeof(elf);
1114 elf.e_shoff = 0;
1115 elf.e_flags = 0;
1116 elf.e_ehsize = sizeof(elf);
1117 elf.e_phentsize = sizeof(struct elf_phdr);
1118 elf.e_phnum = segs+1; /* Include notes */
1119 elf.e_shentsize = 0;
1120 elf.e_shnum = 0;
1121 elf.e_shstrndx = 0;
1123 fs = get_fs();
1124 set_fs(KERNEL_DS);
1126 memcpy(corefile,"core.",5);
1127 #if 0
1128 memcpy(corefile+5,current->comm,sizeof(current->comm));
1129 #else
1130 corefile[4] = '\0';
1131 #endif
1132 file = filp_open(corefile, O_CREAT | 2 | O_TRUNC | O_NOFOLLOW, 0600);
1133 if (IS_ERR(file))
1134 goto end_coredump;
1135 dentry = file->f_dentry;
1136 inode = dentry->d_inode;
1137 if (inode->i_nlink > 1)
1138 goto close_coredump; /* multiple links - don't dump */
1140 if (!S_ISREG(inode->i_mode))
1141 goto close_coredump;
1142 if (!inode->i_op || !inode->i_op->default_file_ops)
1143 goto close_coredump;
1144 if (!file->f_op->write)
1145 goto close_coredump;
1147 has_dumped = 1;
1148 current->flags |= PF_DUMPCORE;
1150 DUMP_WRITE(&elf, sizeof(elf));
1151 offset += sizeof(elf); /* Elf header */
1152 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1155 * Set up the notes in similar form to SVR4 core dumps made
1156 * with info from their /proc.
1158 memset(&psinfo, 0, sizeof(psinfo));
1159 memset(&prstatus, 0, sizeof(prstatus));
1161 notes[0].name = "CORE";
1162 notes[0].type = NT_PRSTATUS;
1163 notes[0].datasz = sizeof(prstatus);
1164 notes[0].data = &prstatus;
1165 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1166 prstatus.pr_sigpend = current->signal.sig[0];
1167 prstatus.pr_sighold = current->blocked.sig[0];
1168 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1169 psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1170 psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1171 psinfo.pr_sid = prstatus.pr_sid = current->session;
1172 prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
1173 prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
1174 prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
1175 prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
1176 prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
1177 prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
1178 prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
1179 prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
1182 * This transfers the registers from regs into the standard
1183 * coredump arrangement, whatever that is.
1185 #ifdef ELF_CORE_COPY_REGS
1186 ELF_CORE_COPY_REGS(prstatus.pr_reg, regs)
1187 #else
1188 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs))
1190 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1191 (long)sizeof(elf_gregset_t), (long)sizeof(struct pt_regs));
1193 else
1194 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1195 #endif
1197 #ifdef DEBUG
1198 dump_regs("Passed in regs", (elf_greg_t *)regs);
1199 dump_regs("prstatus regs", (elf_greg_t *)&prstatus.pr_reg);
1200 #endif
1202 notes[1].name = "CORE";
1203 notes[1].type = NT_PRPSINFO;
1204 notes[1].datasz = sizeof(psinfo);
1205 notes[1].data = &psinfo;
1206 i = current->state ? ffz(~current->state) + 1 : 0;
1207 psinfo.pr_state = i;
1208 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1209 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1210 psinfo.pr_nice = current->priority-15;
1211 psinfo.pr_flag = current->flags;
1212 psinfo.pr_uid = current->uid;
1213 psinfo.pr_gid = current->gid;
1215 int i, len;
1217 set_fs(fs);
1219 len = current->mm->arg_end - current->mm->arg_start;
1220 if (len >= ELF_PRARGSZ)
1221 len = ELF_PRARGSZ-1;
1222 copy_from_user(&psinfo.pr_psargs,
1223 (const char *)current->mm->arg_start, len);
1224 for(i = 0; i < len; i++)
1225 if (psinfo.pr_psargs[i] == 0)
1226 psinfo.pr_psargs[i] = ' ';
1227 psinfo.pr_psargs[len] = 0;
1229 set_fs(KERNEL_DS);
1231 strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1233 notes[2].name = "CORE";
1234 notes[2].type = NT_TASKSTRUCT;
1235 notes[2].datasz = sizeof(*current);
1236 notes[2].data = current;
1238 /* Try to dump the FPU. */
1239 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1240 if (!prstatus.pr_fpvalid)
1242 numnote--;
1244 else
1246 notes[3].name = "CORE";
1247 notes[3].type = NT_PRFPREG;
1248 notes[3].datasz = sizeof(fpu);
1249 notes[3].data = &fpu;
1252 /* Write notes phdr entry */
1254 struct elf_phdr phdr;
1255 int sz = 0;
1257 for(i = 0; i < numnote; i++)
1258 sz += notesize(&notes[i]);
1260 phdr.p_type = PT_NOTE;
1261 phdr.p_offset = offset;
1262 phdr.p_vaddr = 0;
1263 phdr.p_paddr = 0;
1264 phdr.p_filesz = sz;
1265 phdr.p_memsz = 0;
1266 phdr.p_flags = 0;
1267 phdr.p_align = 0;
1269 offset += phdr.p_filesz;
1270 DUMP_WRITE(&phdr, sizeof(phdr));
1273 /* Page-align dumped data */
1274 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1276 /* Write program headers for segments dump */
1277 for(vma = current->mm->mmap, i = 0;
1278 i < segs && vma != NULL; vma = vma->vm_next) {
1279 struct elf_phdr phdr;
1280 size_t sz;
1282 i++;
1284 sz = vma->vm_end - vma->vm_start;
1286 phdr.p_type = PT_LOAD;
1287 phdr.p_offset = offset;
1288 phdr.p_vaddr = vma->vm_start;
1289 phdr.p_paddr = 0;
1290 phdr.p_filesz = maydump(vma) ? sz : 0;
1291 phdr.p_memsz = sz;
1292 offset += phdr.p_filesz;
1293 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1294 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1295 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1296 phdr.p_align = ELF_EXEC_PAGESIZE;
1298 DUMP_WRITE(&phdr, sizeof(phdr));
1301 for(i = 0; i < numnote; i++)
1302 if (!writenote(&notes[i], file))
1303 goto close_coredump;
1305 set_fs(fs);
1307 DUMP_SEEK(dataoff);
1309 for(i = 0, vma = current->mm->mmap;
1310 i < segs && vma != NULL;
1311 vma = vma->vm_next) {
1312 unsigned long addr = vma->vm_start;
1313 unsigned long len = vma->vm_end - vma->vm_start;
1315 i++;
1316 if (!maydump(vma))
1317 continue;
1318 #ifdef DEBUG
1319 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1320 #endif
1321 DUMP_WRITE((void *)addr, len);
1324 if ((off_t) file->f_pos != offset) {
1325 /* Sanity check */
1326 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1327 (off_t) file->f_pos, offset);
1330 close_coredump:
1331 filp_close(file, NULL);
1333 end_coredump:
1334 set_fs(fs);
1335 #ifndef CONFIG_BINFMT_ELF
1336 MOD_DEC_USE_COUNT;
1337 #endif
1338 return has_dumped;
1340 #endif /* USE_ELF_CORE_DUMP */
1342 int __init init_elf_binfmt(void)
1344 return register_binfmt(&elf_format);
1347 #ifdef MODULE
1349 int init_module(void)
1351 /* Install the COFF, ELF and XOUT loaders.
1352 * N.B. We *rely* on the table being the right size with the
1353 * right number of free slots...
1355 return init_elf_binfmt();
1359 void cleanup_module( void)
1361 /* Remove the COFF and ELF loaders. */
1362 unregister_binfmt(&elf_format);
1364 #endif