File: /usr/src/linux/fs/binfmt_elf.c
1 /*
2 * linux/fs/binfmt_elf.c
3 *
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".
8 *
9 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
10 */
11
12 #include <linux/module.h>
13
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/slab.h>
28 #include <linux/shm.h>
29 #include <linux/personality.h>
30 #include <linux/elfcore.h>
31 #include <linux/init.h>
32 #include <linux/highuid.h>
33 #include <linux/smp_lock.h>
34
35 #include <asm/uaccess.h>
36 #include <asm/param.h>
37 #include <asm/pgalloc.h>
38
39 #define DLINFO_ITEMS 13
40
41 #include <linux/elf.h>
42
43 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs);
44 static int load_elf_library(struct file*);
45 static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int);
46 extern int dump_fpu (struct pt_regs *, elf_fpregset_t *);
47 extern void dump_thread(struct pt_regs *, struct user *);
48
49 #ifndef elf_addr_t
50 #define elf_addr_t unsigned long
51 #define elf_caddr_t char *
52 #endif
53
54 /*
55 * If we don't support core dumping, then supply a NULL so we
56 * don't even try.
57 */
58 #ifdef USE_ELF_CORE_DUMP
59 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file);
60 #else
61 #define elf_core_dump NULL
62 #endif
63
64 #if ELF_EXEC_PAGESIZE > PAGE_SIZE
65 # define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
66 #else
67 # define ELF_MIN_ALIGN PAGE_SIZE
68 #endif
69
70 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
71 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
72 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
73
74 static struct linux_binfmt elf_format = {
75 NULL, THIS_MODULE, load_elf_binary, load_elf_library, elf_core_dump, ELF_EXEC_PAGESIZE
76 };
77
78 static void set_brk(unsigned long start, unsigned long end)
79 {
80 start = ELF_PAGEALIGN(start);
81 end = ELF_PAGEALIGN(end);
82 if (end <= start)
83 return;
84 do_brk(start, end - start);
85 }
86
87
88 /* We need to explicitly zero any fractional pages
89 after the data section (i.e. bss). This would
90 contain the junk from the file that should not
91 be in memory */
92
93
94 static void padzero(unsigned long elf_bss)
95 {
96 unsigned long nbyte;
97
98 nbyte = ELF_PAGEOFFSET(elf_bss);
99 if (nbyte) {
100 nbyte = ELF_MIN_ALIGN - nbyte;
101 clear_user((void *) elf_bss, nbyte);
102 }
103 }
104
105 static elf_addr_t *
106 create_elf_tables(char *p, int argc, int envc,
107 struct elfhdr * exec,
108 unsigned long load_addr,
109 unsigned long load_bias,
110 unsigned long interp_load_addr, int ibcs)
111 {
112 elf_caddr_t *argv;
113 elf_caddr_t *envp;
114 elf_addr_t *sp, *csp;
115 char *k_platform, *u_platform;
116 long hwcap;
117 size_t platform_len = 0;
118 size_t len;
119
120 /*
121 * Get hold of platform and hardware capabilities masks for
122 * the machine we are running on. In some cases (Sparc),
123 * this info is impossible to get, in others (i386) it is
124 * merely difficult.
125 */
126
127 hwcap = ELF_HWCAP;
128 k_platform = ELF_PLATFORM;
129
130 if (k_platform) {
131 platform_len = strlen(k_platform) + 1;
132 u_platform = p - platform_len;
133 __copy_to_user(u_platform, k_platform, platform_len);
134 } else
135 u_platform = p;
136
137 /*
138 * Force 16 byte _final_ alignment here for generality.
139 */
140 sp = (elf_addr_t *)(~15UL & (unsigned long)(u_platform));
141 csp = sp;
142 csp -= (1+DLINFO_ITEMS)*2 + (k_platform ? 2 : 0);
143 #ifdef DLINFO_ARCH_ITEMS
144 csp -= DLINFO_ARCH_ITEMS*2;
145 #endif
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);
151
152 /*
153 * Put the ELF interpreter info on the stack
154 */
155 #define NEW_AUX_ENT(nr, id, val) \
156 __put_user ((id), sp+(nr*2)); \
157 __put_user ((val), sp+(nr*2+1)); \
158
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);
164 }
165 sp -= DLINFO_ITEMS*2;
166 NEW_AUX_ENT( 0, AT_HWCAP, hwcap);
167 NEW_AUX_ENT( 1, AT_PAGESZ, ELF_EXEC_PAGESIZE);
168 NEW_AUX_ENT( 2, AT_CLKTCK, CLOCKS_PER_SEC);
169 NEW_AUX_ENT( 3, AT_PHDR, load_addr + exec->e_phoff);
170 NEW_AUX_ENT( 4, AT_PHENT, sizeof (struct elf_phdr));
171 NEW_AUX_ENT( 5, AT_PHNUM, exec->e_phnum);
172 NEW_AUX_ENT( 6, AT_BASE, interp_load_addr);
173 NEW_AUX_ENT( 7, AT_FLAGS, 0);
174 NEW_AUX_ENT( 8, AT_ENTRY, load_bias + exec->e_entry);
175 NEW_AUX_ENT( 9, AT_UID, (elf_addr_t) current->uid);
176 NEW_AUX_ENT(10, AT_EUID, (elf_addr_t) current->euid);
177 NEW_AUX_ENT(11, AT_GID, (elf_addr_t) current->gid);
178 NEW_AUX_ENT(12, AT_EGID, (elf_addr_t) current->egid);
179 #ifdef ARCH_DLINFO
180 /*
181 * ARCH_DLINFO must come last so platform specific code can enforce
182 * special alignment requirements on the AUXV if necessary (eg. PPC).
183 */
184 ARCH_DLINFO;
185 #endif
186 #undef NEW_AUX_ENT
187
188 sp -= envc+1;
189 envp = (elf_caddr_t *) sp;
190 sp -= argc+1;
191 argv = (elf_caddr_t *) sp;
192 if (!ibcs) {
193 __put_user((elf_addr_t)(unsigned long) envp,--sp);
194 __put_user((elf_addr_t)(unsigned long) argv,--sp);
195 }
196
197 __put_user((elf_addr_t)argc,--sp);
198 current->mm->arg_start = (unsigned long) p;
199 while (argc-->0) {
200 __put_user((elf_caddr_t)(unsigned long)p,argv++);
201 len = strnlen_user(p, PAGE_SIZE*MAX_ARG_PAGES);
202 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
203 return NULL;
204 p += len;
205 }
206 __put_user(NULL, argv);
207 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
208 while (envc-->0) {
209 __put_user((elf_caddr_t)(unsigned long)p,envp++);
210 len = strnlen_user(p, PAGE_SIZE*MAX_ARG_PAGES);
211 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
212 return NULL;
213 p += len;
214 }
215 __put_user(NULL, envp);
216 current->mm->env_end = (unsigned long) p;
217 return sp;
218 }
219
220 #ifndef elf_map
221
222 static inline unsigned long
223 elf_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
224 {
225 unsigned long map_addr;
226
227 down_write(¤t->mm->mmap_sem);
228 map_addr = do_mmap(filep, ELF_PAGESTART(addr),
229 eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr), prot, type,
230 eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
231 up_write(¤t->mm->mmap_sem);
232 return(map_addr);
233 }
234
235 #endif /* !elf_map */
236
237 /* This is much more generalized than the library routine read function,
238 so we keep this separate. Technically the library read function
239 is only provided so that we can read a.out libraries that have
240 an ELF header */
241
242 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
243 struct file * interpreter,
244 unsigned long *interp_load_addr)
245 {
246 struct elf_phdr *elf_phdata;
247 struct elf_phdr *eppnt;
248 unsigned long load_addr = 0;
249 int load_addr_set = 0;
250 unsigned long last_bss = 0, elf_bss = 0;
251 unsigned long error = ~0UL;
252 int retval, i, size;
253
254 /* First of all, some simple consistency checks */
255 if (interp_elf_ex->e_type != ET_EXEC &&
256 interp_elf_ex->e_type != ET_DYN)
257 goto out;
258 if (!elf_check_arch(interp_elf_ex))
259 goto out;
260 if (!interpreter->f_op || !interpreter->f_op->mmap)
261 goto out;
262
263 /*
264 * If the size of this structure has changed, then punt, since
265 * we will be doing the wrong thing.
266 */
267 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
268 goto out;
269
270 /* Now read in all of the header information */
271
272 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
273 if (size > ELF_MIN_ALIGN)
274 goto out;
275 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
276 if (!elf_phdata)
277 goto out;
278
279 retval = kernel_read(interpreter,interp_elf_ex->e_phoff,(char *)elf_phdata,size);
280 error = retval;
281 if (retval < 0)
282 goto out_close;
283
284 eppnt = elf_phdata;
285 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
286 if (eppnt->p_type == PT_LOAD) {
287 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
288 int elf_prot = 0;
289 unsigned long vaddr = 0;
290 unsigned long k, map_addr;
291
292 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
293 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
294 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
295 vaddr = eppnt->p_vaddr;
296 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
297 elf_type |= MAP_FIXED;
298
299 map_addr = elf_map(interpreter, load_addr + vaddr, eppnt, elf_prot, elf_type);
300
301 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
302 load_addr = map_addr - ELF_PAGESTART(vaddr);
303 load_addr_set = 1;
304 }
305
306 /*
307 * Find the end of the file mapping for this phdr, and keep
308 * track of the largest address we see for this.
309 */
310 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
311 if (k > elf_bss)
312 elf_bss = k;
313
314 /*
315 * Do the same thing for the memory mapping - between
316 * elf_bss and last_bss is the bss section.
317 */
318 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
319 if (k > last_bss)
320 last_bss = k;
321 }
322 }
323
324 /* Now use mmap to map the library into memory. */
325
326 /*
327 * Now fill out the bss section. First pad the last page up
328 * to the page boundary, and then perform a mmap to make sure
329 * that there are zero-mapped pages up to and including the
330 * last bss page.
331 */
332 padzero(elf_bss);
333 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1); /* What we have mapped so far */
334
335 /* Map the last of the bss segment */
336 if (last_bss > elf_bss)
337 do_brk(elf_bss, last_bss - elf_bss);
338
339 *interp_load_addr = load_addr;
340 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
341
342 out_close:
343 kfree(elf_phdata);
344 out:
345 return error;
346 }
347
348 static unsigned long load_aout_interp(struct exec * interp_ex,
349 struct file * interpreter)
350 {
351 unsigned long text_data, elf_entry = ~0UL;
352 char * addr;
353 loff_t offset;
354 int retval;
355
356 current->mm->end_code = interp_ex->a_text;
357 text_data = interp_ex->a_text + interp_ex->a_data;
358 current->mm->end_data = text_data;
359 current->mm->brk = interp_ex->a_bss + text_data;
360
361 switch (N_MAGIC(*interp_ex)) {
362 case OMAGIC:
363 offset = 32;
364 addr = (char *) 0;
365 break;
366 case ZMAGIC:
367 case QMAGIC:
368 offset = N_TXTOFF(*interp_ex);
369 addr = (char *) N_TXTADDR(*interp_ex);
370 break;
371 default:
372 goto out;
373 }
374
375 do_brk(0, text_data);
376 retval = -ENOEXEC;
377 if (!interpreter->f_op || !interpreter->f_op->read)
378 goto out;
379 retval = interpreter->f_op->read(interpreter, addr, text_data, &offset);
380 if (retval < 0)
381 goto out;
382 flush_icache_range((unsigned long)addr,
383 (unsigned long)addr + text_data);
384
385 do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
386 interp_ex->a_bss);
387 elf_entry = interp_ex->a_entry;
388
389 out:
390 return elf_entry;
391 }
392
393 /*
394 * These are the functions used to load ELF style executables and shared
395 * libraries. There is no binary dependent code anywhere else.
396 */
397
398 #define INTERPRETER_NONE 0
399 #define INTERPRETER_AOUT 1
400 #define INTERPRETER_ELF 2
401
402
403 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
404 {
405 struct file *interpreter = NULL; /* to shut gcc up */
406 unsigned long load_addr = 0, load_bias = 0;
407 int load_addr_set = 0;
408 char * elf_interpreter = NULL;
409 unsigned int interpreter_type = INTERPRETER_NONE;
410 unsigned char ibcs2_interpreter = 0;
411 mm_segment_t old_fs;
412 unsigned long error;
413 struct elf_phdr * elf_ppnt, *elf_phdata;
414 unsigned long elf_bss, k, elf_brk;
415 int elf_exec_fileno;
416 int retval, i;
417 unsigned int size;
418 unsigned long elf_entry, interp_load_addr = 0;
419 unsigned long start_code, end_code, start_data, end_data;
420 struct elfhdr elf_ex;
421 struct elfhdr interp_elf_ex;
422 struct exec interp_ex;
423 char passed_fileno[6];
424
425 /* Get the exec-header */
426 elf_ex = *((struct elfhdr *) bprm->buf);
427
428 retval = -ENOEXEC;
429 /* First of all, some simple consistency checks */
430 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
431 goto out;
432
433 if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
434 goto out;
435 if (!elf_check_arch(&elf_ex))
436 goto out;
437 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
438 goto out;
439
440 /* Now read in all of the header information */
441
442 retval = -ENOMEM;
443 size = ((unsigned int)elf_ex.e_phentsize) * elf_ex.e_phnum;
444 if (size > 65536)
445 goto out;
446 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
447 if (!elf_phdata)
448 goto out;
449
450 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *) elf_phdata, size);
451 if (retval < 0)
452 goto out_free_ph;
453
454 retval = get_unused_fd();
455 if (retval < 0)
456 goto out_free_ph;
457 get_file(bprm->file);
458 fd_install(elf_exec_fileno = retval, bprm->file);
459
460 elf_ppnt = elf_phdata;
461 elf_bss = 0;
462 elf_brk = 0;
463
464 start_code = ~0UL;
465 end_code = 0;
466 start_data = 0;
467 end_data = 0;
468
469 for (i = 0; i < elf_ex.e_phnum; i++) {
470 if (elf_ppnt->p_type == PT_INTERP) {
471 retval = -EINVAL;
472 if (elf_interpreter)
473 goto out_free_dentry;
474
475 /* This is the program interpreter used for
476 * shared libraries - for now assume that this
477 * is an a.out format binary
478 */
479
480 retval = -ENOMEM;
481 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
482 GFP_KERNEL);
483 if (!elf_interpreter)
484 goto out_free_file;
485
486 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
487 elf_interpreter,
488 elf_ppnt->p_filesz);
489 if (retval < 0)
490 goto out_free_interp;
491 /* If the program interpreter is one of these two,
492 * then assume an iBCS2 image. Otherwise assume
493 * a native linux image.
494 */
495 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
496 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
497 ibcs2_interpreter = 1;
498 #if 0
499 printk("Using ELF interpreter %s\n", elf_interpreter);
500 #endif
501 #ifdef __sparc__
502 if (ibcs2_interpreter) {
503 unsigned long old_pers = current->personality;
504 struct exec_domain *old_domain = current->exec_domain;
505 struct exec_domain *new_domain;
506 struct fs_struct *old_fs = current->fs, *new_fs;
507 get_exec_domain(old_domain);
508 atomic_inc(&old_fs->count);
509
510 set_personality(PER_SVR4);
511 interpreter = open_exec(elf_interpreter);
512
513 new_domain = current->exec_domain;
514 new_fs = current->fs;
515 current->personality = old_pers;
516 current->exec_domain = old_domain;
517 current->fs = old_fs;
518 put_exec_domain(new_domain);
519 put_fs_struct(new_fs);
520 } else
521 #endif
522 {
523 interpreter = open_exec(elf_interpreter);
524 }
525 retval = PTR_ERR(interpreter);
526 if (IS_ERR(interpreter))
527 goto out_free_interp;
528 retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
529 if (retval < 0)
530 goto out_free_dentry;
531
532 /* Get the exec headers */
533 interp_ex = *((struct exec *) bprm->buf);
534 interp_elf_ex = *((struct elfhdr *) bprm->buf);
535 }
536 elf_ppnt++;
537 }
538
539 /* Some simple consistency checks for the interpreter */
540 if (elf_interpreter) {
541 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
542
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;
548
549 if (memcmp(interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
550 interpreter_type &= ~INTERPRETER_ELF;
551
552 retval = -ELIBBAD;
553 if (!interpreter_type)
554 goto out_free_dentry;
555
556 /* Make sure only one type was selected */
557 if ((interpreter_type & INTERPRETER_ELF) &&
558 interpreter_type != INTERPRETER_ELF) {
559 // FIXME - ratelimit this before re-enabling
560 // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
561 interpreter_type = INTERPRETER_ELF;
562 }
563 }
564
565 /* OK, we are done with that, now set up the arg stuff,
566 and then start this sucker up */
567
568 if (!bprm->sh_bang) {
569 char * passed_p;
570
571 if (interpreter_type == INTERPRETER_AOUT) {
572 sprintf(passed_fileno, "%d", elf_exec_fileno);
573 passed_p = passed_fileno;
574
575 if (elf_interpreter) {
576 retval = copy_strings_kernel(1,&passed_p,bprm);
577 if (retval)
578 goto out_free_dentry;
579 bprm->argc++;
580 }
581 }
582 }
583
584 /* Flush all traces of the currently running executable */
585 retval = flush_old_exec(bprm);
586 if (retval)
587 goto out_free_dentry;
588
589 /* OK, This is the point of no return */
590 current->mm->start_data = 0;
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;
596
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);
600
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 setup_arg_pages(bprm); /* XXX: check error */
605 current->mm->start_stack = bprm->p;
606
607 /* Now we do a little grungy work by mmaping the ELF image into
608 the correct location in memory. At this point, we assume that
609 the image should be loaded at fixed address, not at a variable
610 address. */
611
612 old_fs = get_fs();
613 set_fs(get_ds());
614 for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
615 int elf_prot = 0, elf_flags;
616 unsigned long vaddr;
617
618 if (elf_ppnt->p_type != PT_LOAD)
619 continue;
620
621 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
622 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
623 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
624
625 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
626
627 vaddr = elf_ppnt->p_vaddr;
628 if (elf_ex.e_type == ET_EXEC || load_addr_set) {
629 elf_flags |= MAP_FIXED;
630 } else if (elf_ex.e_type == ET_DYN) {
631 /* Try and get dynamic programs out of the way of the default mmap
632 base, as well as whatever program they might try to exec. This
633 is because the brk will follow the loader, and is not movable. */
634 load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
635 }
636
637 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags);
638
639 if (!load_addr_set) {
640 load_addr_set = 1;
641 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
642 if (elf_ex.e_type == ET_DYN) {
643 load_bias += error -
644 ELF_PAGESTART(load_bias + vaddr);
645 load_addr += load_bias;
646 }
647 }
648 k = elf_ppnt->p_vaddr;
649 if (k < start_code) start_code = k;
650 if (start_data < k) start_data = k;
651
652 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
653
654 if (k > elf_bss)
655 elf_bss = k;
656 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
657 end_code = k;
658 if (end_data < k)
659 end_data = k;
660 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
661 if (k > elf_brk)
662 elf_brk = k;
663 }
664 set_fs(old_fs);
665
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 start_data += load_bias;
672 end_data += load_bias;
673
674 if (elf_interpreter) {
675 if (interpreter_type == INTERPRETER_AOUT)
676 elf_entry = load_aout_interp(&interp_ex,
677 interpreter);
678 else
679 elf_entry = load_elf_interp(&interp_elf_ex,
680 interpreter,
681 &interp_load_addr);
682
683 allow_write_access(interpreter);
684 fput(interpreter);
685 kfree(elf_interpreter);
686
687 if (elf_entry == ~0UL) {
688 printk(KERN_ERR "Unable to load interpreter\n");
689 kfree(elf_phdata);
690 send_sig(SIGSEGV, current, 0);
691 return 0;
692 }
693 }
694
695 kfree(elf_phdata);
696
697 if (interpreter_type != INTERPRETER_AOUT)
698 sys_close(elf_exec_fileno);
699
700 set_binfmt(&elf_format);
701
702 compute_creds(bprm);
703 current->flags &= ~PF_FORKNOEXEC;
704 bprm->p = (unsigned long)
705 create_elf_tables((char *)bprm->p,
706 bprm->argc,
707 bprm->envc,
708 &elf_ex,
709 load_addr, load_bias,
710 interp_load_addr,
711 (interpreter_type == INTERPRETER_AOUT ? 0 : 1));
712 /* N.B. passed_fileno might not be initialized? */
713 if (interpreter_type == INTERPRETER_AOUT)
714 current->mm->arg_start += strlen(passed_fileno) + 1;
715 current->mm->start_brk = current->mm->brk = elf_brk;
716 current->mm->end_code = end_code;
717 current->mm->start_code = start_code;
718 current->mm->start_data = start_data;
719 current->mm->end_data = end_data;
720 current->mm->start_stack = bprm->p;
721
722 /* Calling set_brk effectively mmaps the pages that we need
723 * for the bss and break sections
724 */
725 set_brk(elf_bss, elf_brk);
726
727 padzero(elf_bss);
728
729 #if 0
730 printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
731 printk("(end_code) %lx\n" , (long) current->mm->end_code);
732 printk("(start_code) %lx\n" , (long) current->mm->start_code);
733 printk("(start_data) %lx\n" , (long) current->mm->start_data);
734 printk("(end_data) %lx\n" , (long) current->mm->end_data);
735 printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
736 printk("(brk) %lx\n" , (long) current->mm->brk);
737 #endif
738
739 if ( current->personality == PER_SVR4 )
740 {
741 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
742 and some applications "depend" upon this behavior.
743 Since we do not have the power to recompile these, we
744 emulate the SVr4 behavior. Sigh. */
745 /* N.B. Shouldn't the size here be PAGE_SIZE?? */
746 down_write(¤t->mm->mmap_sem);
747 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
748 MAP_FIXED | MAP_PRIVATE, 0);
749 up_write(¤t->mm->mmap_sem);
750 }
751
752 #ifdef ELF_PLAT_INIT
753 /*
754 * The ABI may specify that certain registers be set up in special
755 * ways (on i386 %edx is the address of a DT_FINI function, for
756 * example. This macro performs whatever initialization to
757 * the regs structure is required.
758 */
759 ELF_PLAT_INIT(regs);
760 #endif
761
762 start_thread(regs, elf_entry, bprm->p);
763 if (current->ptrace & PT_PTRACED)
764 send_sig(SIGTRAP, current, 0);
765 retval = 0;
766 out:
767 return retval;
768
769 /* error cleanup */
770 out_free_dentry:
771 allow_write_access(interpreter);
772 fput(interpreter);
773 out_free_interp:
774 if (elf_interpreter)
775 kfree(elf_interpreter);
776 out_free_file:
777 sys_close(elf_exec_fileno);
778 out_free_ph:
779 kfree(elf_phdata);
780 goto out;
781 }
782
783 /* This is really simpleminded and specialized - we are loading an
784 a.out library that is given an ELF header. */
785
786 static int load_elf_library(struct file *file)
787 {
788 struct elf_phdr *elf_phdata;
789 unsigned long elf_bss = 0, bss, len, k;
790 int retval, error, i, j;
791 struct elfhdr elf_ex;
792
793 error = -ENOEXEC;
794 retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
795 if (retval != sizeof(elf_ex))
796 goto out;
797
798 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
799 goto out;
800
801 /* First of all, some simple consistency checks */
802 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
803 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
804 goto out;
805
806 /* Now read in all of the header information */
807
808 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
809 if (j > ELF_MIN_ALIGN)
810 goto out;
811
812 error = -ENOMEM;
813 elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
814 if (!elf_phdata)
815 goto out;
816
817 /* N.B. check for error return?? */
818 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata,
819 sizeof(struct elf_phdr) * elf_ex.e_phnum);
820
821 error = -ENOEXEC;
822 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
823 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
824 if (j != 1)
825 goto out_free_ph;
826
827 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
828
829 /* Now use mmap to map the library into memory. */
830 down_write(¤t->mm->mmap_sem);
831 error = do_mmap(file,
832 ELF_PAGESTART(elf_phdata->p_vaddr),
833 (elf_phdata->p_filesz +
834 ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
835 PROT_READ | PROT_WRITE | PROT_EXEC,
836 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
837 (elf_phdata->p_offset -
838 ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
839 up_write(¤t->mm->mmap_sem);
840 if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
841 goto out_free_ph;
842
843 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
844 if (k > elf_bss)
845 elf_bss = k;
846 padzero(elf_bss);
847
848 len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr + ELF_MIN_ALIGN - 1);
849 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
850 if (bss > len)
851 do_brk(len, bss - len);
852 error = 0;
853
854 out_free_ph:
855 kfree(elf_phdata);
856 out:
857 return error;
858 }
859
860 /*
861 * Note that some platforms still use traditional core dumps and not
862 * the ELF core dump. Each platform can select it as appropriate.
863 */
864 #ifdef USE_ELF_CORE_DUMP
865
866 /*
867 * ELF core dumper
868 *
869 * Modelled on fs/exec.c:aout_core_dump()
870 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
871 */
872 /*
873 * These are the only things you should do on a core-file: use only these
874 * functions to write out all the necessary info.
875 */
876 static int dump_write(struct file *file, const void *addr, int nr)
877 {
878 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
879 }
880
881 static int dump_seek(struct file *file, off_t off)
882 {
883 if (file->f_op->llseek) {
884 if (file->f_op->llseek(file, off, 0) != off)
885 return 0;
886 } else
887 file->f_pos = off;
888 return 1;
889 }
890
891 /*
892 * Decide whether a segment is worth dumping; default is yes to be
893 * sure (missing info is worse than too much; etc).
894 * Personally I'd include everything, and use the coredump limit...
895 *
896 * I think we should skip something. But I am not sure how. H.J.
897 */
898 static inline int maydump(struct vm_area_struct *vma)
899 {
900 /*
901 * If we may not read the contents, don't allow us to dump
902 * them either. "dump_write()" can't handle it anyway.
903 */
904 if (!(vma->vm_flags & VM_READ))
905 return 0;
906
907 /* Do not dump I/O mapped devices! -DaveM */
908 if (vma->vm_flags & VM_IO)
909 return 0;
910 #if 1
911 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
912 return 1;
913 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
914 return 0;
915 #endif
916 return 1;
917 }
918
919 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
920
921 /* An ELF note in memory */
922 struct memelfnote
923 {
924 const char *name;
925 int type;
926 unsigned int datasz;
927 void *data;
928 };
929
930 static int notesize(struct memelfnote *en)
931 {
932 int sz;
933
934 sz = sizeof(struct elf_note);
935 sz += roundup(strlen(en->name), 4);
936 sz += roundup(en->datasz, 4);
937
938 return sz;
939 }
940
941 /* #define DEBUG */
942
943 #ifdef DEBUG
944 static void dump_regs(const char *str, elf_greg_t *r)
945 {
946 int i;
947 static const char *regs[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
948 "eax", "ds", "es", "fs", "gs",
949 "orig_eax", "eip", "cs",
950 "efl", "uesp", "ss"};
951 printk("Registers: %s\n", str);
952
953 for(i = 0; i < ELF_NGREG; i++)
954 {
955 unsigned long val = r[i];
956 printk(" %-2d %-5s=%08lx %lu\n", i, regs[i], val, val);
957 }
958 }
959 #endif
960
961 #define DUMP_WRITE(addr, nr) \
962 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
963 #define DUMP_SEEK(off) \
964 do { if (!dump_seek(file, (off))) return 0; } while(0)
965
966 static int writenote(struct memelfnote *men, struct file *file)
967 {
968 struct elf_note en;
969
970 en.n_namesz = strlen(men->name);
971 en.n_descsz = men->datasz;
972 en.n_type = men->type;
973
974 DUMP_WRITE(&en, sizeof(en));
975 DUMP_WRITE(men->name, en.n_namesz);
976 /* XXX - cast from long long to long to avoid need for libgcc.a */
977 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
978 DUMP_WRITE(men->data, men->datasz);
979 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
980
981 return 1;
982 }
983 #undef DUMP_WRITE
984 #undef DUMP_SEEK
985
986 #define DUMP_WRITE(addr, nr) \
987 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
988 goto end_coredump;
989 #define DUMP_SEEK(off) \
990 if (!dump_seek(file, (off))) \
991 goto end_coredump;
992 /*
993 * Actual dumper
994 *
995 * This is a two-pass process; first we find the offsets of the bits,
996 * and then they are actually written out. If we run out of core limit
997 * we just truncate.
998 */
999 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
1000 {
1001 int has_dumped = 0;
1002 mm_segment_t fs;
1003 int segs;
1004 size_t size = 0;
1005 int i;
1006 struct vm_area_struct *vma;
1007 struct elfhdr elf;
1008 off_t offset = 0, dataoff;
1009 unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1010 int numnote = 4;
1011 struct memelfnote notes[4];
1012 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1013 elf_fpregset_t fpu; /* NT_PRFPREG */
1014 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1015
1016 segs = current->mm->map_count;
1017
1018 #ifdef DEBUG
1019 printk("elf_core_dump: %d segs %lu limit\n", segs, limit);
1020 #endif
1021
1022 /* Set up header */
1023 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1024 elf.e_ident[EI_CLASS] = ELF_CLASS;
1025 elf.e_ident[EI_DATA] = ELF_DATA;
1026 elf.e_ident[EI_VERSION] = EV_CURRENT;
1027 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1028
1029 elf.e_type = ET_CORE;
1030 elf.e_machine = ELF_ARCH;
1031 elf.e_version = EV_CURRENT;
1032 elf.e_entry = 0;
1033 elf.e_phoff = sizeof(elf);
1034 elf.e_shoff = 0;
1035 elf.e_flags = 0;
1036 elf.e_ehsize = sizeof(elf);
1037 elf.e_phentsize = sizeof(struct elf_phdr);
1038 elf.e_phnum = segs+1; /* Include notes */
1039 elf.e_shentsize = 0;
1040 elf.e_shnum = 0;
1041 elf.e_shstrndx = 0;
1042
1043 fs = get_fs();
1044 set_fs(KERNEL_DS);
1045
1046 has_dumped = 1;
1047 current->flags |= PF_DUMPCORE;
1048
1049 DUMP_WRITE(&elf, sizeof(elf));
1050 offset += sizeof(elf); /* Elf header */
1051 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1052
1053 /*
1054 * Set up the notes in similar form to SVR4 core dumps made
1055 * with info from their /proc.
1056 */
1057 memset(&psinfo, 0, sizeof(psinfo));
1058 memset(&prstatus, 0, sizeof(prstatus));
1059
1060 notes[0].name = "CORE";
1061 notes[0].type = NT_PRSTATUS;
1062 notes[0].datasz = sizeof(prstatus);
1063 notes[0].data = &prstatus;
1064 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1065 prstatus.pr_sigpend = current->pending.signal.sig[0];
1066 prstatus.pr_sighold = current->blocked.sig[0];
1067 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1068 psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1069 psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1070 psinfo.pr_sid = prstatus.pr_sid = current->session;
1071 prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
1072 prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
1073 prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
1074 prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
1075 prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
1076 prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
1077 prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
1078 prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
1079
1080 /*
1081 * This transfers the registers from regs into the standard
1082 * coredump arrangement, whatever that is.
1083 */
1084 #ifdef ELF_CORE_COPY_REGS
1085 ELF_CORE_COPY_REGS(prstatus.pr_reg, regs)
1086 #else
1087 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs))
1088 {
1089 printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)\n",
1090 (long)sizeof(elf_gregset_t), (long)sizeof(struct pt_regs));
1091 }
1092 else
1093 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1094 #endif
1095
1096 #ifdef DEBUG
1097 dump_regs("Passed in regs", (elf_greg_t *)regs);
1098 dump_regs("prstatus regs", (elf_greg_t *)&prstatus.pr_reg);
1099 #endif
1100
1101 notes[1].name = "CORE";
1102 notes[1].type = NT_PRPSINFO;
1103 notes[1].datasz = sizeof(psinfo);
1104 notes[1].data = &psinfo;
1105 i = current->state ? ffz(~current->state) + 1 : 0;
1106 psinfo.pr_state = i;
1107 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1108 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1109 psinfo.pr_nice = current->nice;
1110 psinfo.pr_flag = current->flags;
1111 psinfo.pr_uid = NEW_TO_OLD_UID(current->uid);
1112 psinfo.pr_gid = NEW_TO_OLD_GID(current->gid);
1113 {
1114 int i, len;
1115
1116 set_fs(fs);
1117
1118 len = current->mm->arg_end - current->mm->arg_start;
1119 if (len >= ELF_PRARGSZ)
1120 len = ELF_PRARGSZ-1;
1121 copy_from_user(&psinfo.pr_psargs,
1122 (const char *)current->mm->arg_start, len);
1123 for(i = 0; i < len; i++)
1124 if (psinfo.pr_psargs[i] == 0)
1125 psinfo.pr_psargs[i] = ' ';
1126 psinfo.pr_psargs[len] = 0;
1127
1128 set_fs(KERNEL_DS);
1129 }
1130 strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1131
1132 notes[2].name = "CORE";
1133 notes[2].type = NT_TASKSTRUCT;
1134 notes[2].datasz = sizeof(*current);
1135 notes[2].data = current;
1136
1137 /* Try to dump the FPU. */
1138 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1139 if (!prstatus.pr_fpvalid)
1140 {
1141 numnote--;
1142 }
1143 else
1144 {
1145 notes[3].name = "CORE";
1146 notes[3].type = NT_PRFPREG;
1147 notes[3].datasz = sizeof(fpu);
1148 notes[3].data = &fpu;
1149 }
1150
1151 /* Write notes phdr entry */
1152 {
1153 struct elf_phdr phdr;
1154 int sz = 0;
1155
1156 for(i = 0; i < numnote; i++)
1157 sz += notesize(¬es[i]);
1158
1159 phdr.p_type = PT_NOTE;
1160 phdr.p_offset = offset;
1161 phdr.p_vaddr = 0;
1162 phdr.p_paddr = 0;
1163 phdr.p_filesz = sz;
1164 phdr.p_memsz = 0;
1165 phdr.p_flags = 0;
1166 phdr.p_align = 0;
1167
1168 offset += phdr.p_filesz;
1169 DUMP_WRITE(&phdr, sizeof(phdr));
1170 }
1171
1172 /* Page-align dumped data */
1173 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1174
1175 /* Write program headers for segments dump */
1176 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1177 struct elf_phdr phdr;
1178 size_t sz;
1179
1180 sz = vma->vm_end - vma->vm_start;
1181
1182 phdr.p_type = PT_LOAD;
1183 phdr.p_offset = offset;
1184 phdr.p_vaddr = vma->vm_start;
1185 phdr.p_paddr = 0;
1186 phdr.p_filesz = maydump(vma) ? sz : 0;
1187 phdr.p_memsz = sz;
1188 offset += phdr.p_filesz;
1189 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1190 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1191 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1192 phdr.p_align = ELF_EXEC_PAGESIZE;
1193
1194 DUMP_WRITE(&phdr, sizeof(phdr));
1195 }
1196
1197 for(i = 0; i < numnote; i++)
1198 if (!writenote(¬es[i], file))
1199 goto end_coredump;
1200
1201 set_fs(fs);
1202
1203 DUMP_SEEK(dataoff);
1204
1205 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1206 unsigned long addr;
1207
1208 if (!maydump(vma))
1209 continue;
1210 #ifdef DEBUG
1211 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1212 #endif
1213 for (addr = vma->vm_start;
1214 addr < vma->vm_end;
1215 addr += PAGE_SIZE) {
1216 pgd_t *pgd;
1217 pmd_t *pmd;
1218 pte_t *pte;
1219
1220 pgd = pgd_offset(vma->vm_mm, addr);
1221 if (pgd_none(*pgd))
1222 goto nextpage_coredump;
1223 pmd = pmd_offset(pgd, addr);
1224 if (pmd_none(*pmd))
1225 goto nextpage_coredump;
1226 pte = pte_offset(pmd, addr);
1227 if (pte_none(*pte)) {
1228 nextpage_coredump:
1229 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1230 } else {
1231 DUMP_WRITE((void*)addr, PAGE_SIZE);
1232 }
1233 }
1234 }
1235
1236 if ((off_t) file->f_pos != offset) {
1237 /* Sanity check */
1238 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1239 (off_t) file->f_pos, offset);
1240 }
1241
1242 end_coredump:
1243 set_fs(fs);
1244 return has_dumped;
1245 }
1246 #endif /* USE_ELF_CORE_DUMP */
1247
1248 static int __init init_elf_binfmt(void)
1249 {
1250 return register_binfmt(&elf_format);
1251 }
1252
1253 static void __exit exit_elf_binfmt(void)
1254 {
1255 /* Remove the COFF and ELF loaders. */
1256 unregister_binfmt(&elf_format);
1257 }
1258
1259 module_init(init_elf_binfmt)
1260 module_exit(exit_elf_binfmt)
1261