1 /* binfmt_elf_fdpic.c: FDPIC ELF binary format
3 * Copyright (C) 2003, 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * Derived from binfmt_elf.c
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/module.h>
16 #include <linux/stat.h>
17 #include <linux/sched.h>
19 #include <linux/mman.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/slab.h>
27 #include <linux/highmem.h>
28 #include <linux/personality.h>
29 #include <linux/ptrace.h>
30 #include <linux/init.h>
31 #include <linux/smp_lock.h>
32 #include <linux/elf.h>
33 #include <linux/elf-fdpic.h>
34 #include <linux/elfcore.h>
36 #include <asm/uaccess.h>
37 #include <asm/param.h>
38 #include <asm/pgalloc.h>
40 typedef char *elf_caddr_t;
42 #define elf_addr_t unsigned long
46 #define kdebug(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
48 #define kdebug(fmt, ...) do {} while(0)
51 MODULE_LICENSE("GPL");
53 static int load_elf_fdpic_binary(struct linux_binprm *bprm, struct pt_regs *regs);
54 //static int load_elf_fdpic_library(struct file *);
55 static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params, struct file *file);
56 static int elf_fdpic_map_file(struct elf_fdpic_params *params,
61 static int create_elf_fdpic_tables(struct linux_binprm *bprm,
63 struct elf_fdpic_params *exec_params,
64 struct elf_fdpic_params *interp_params);
67 static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm, unsigned long *_sp);
68 static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *params,
70 struct mm_struct *mm);
73 static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
75 struct mm_struct *mm);
77 static struct linux_binfmt elf_fdpic_format = {
78 .module = THIS_MODULE,
79 .load_binary = load_elf_fdpic_binary,
80 // .load_shlib = load_elf_fdpic_library,
81 // .core_dump = elf_fdpic_core_dump,
82 .min_coredump = ELF_EXEC_PAGESIZE,
85 static int __init init_elf_fdpic_binfmt(void) { return register_binfmt(&elf_fdpic_format); }
86 static void __exit exit_elf_fdpic_binfmt(void) { unregister_binfmt(&elf_fdpic_format); }
88 module_init(init_elf_fdpic_binfmt)
89 module_exit(exit_elf_fdpic_binfmt)
91 static int is_elf_fdpic(struct elfhdr *hdr, struct file *file)
93 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
95 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
97 if (!elf_check_arch(hdr) || !elf_check_fdpic(hdr))
99 if (!file->f_op || !file->f_op->mmap)
104 /*****************************************************************************/
106 * read the program headers table into memory
108 static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params, struct file *file)
110 struct elf32_phdr *phdr;
114 if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
116 if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr))
119 size = params->hdr.e_phnum * sizeof(struct elf_phdr);
120 params->phdrs = kmalloc(size, GFP_KERNEL);
124 retval = kernel_read(file, params->hdr.e_phoff, (char *) params->phdrs, size);
128 /* determine stack size for this binary */
129 phdr = params->phdrs;
130 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
131 if (phdr->p_type != PT_GNU_STACK)
134 if (phdr->p_flags & PF_X)
135 params->flags |= ELF_FDPIC_FLAG_EXEC_STACK;
137 params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK;
139 params->stack_size = phdr->p_memsz;
144 } /* end elf_fdpic_fetch_phdrs() */
146 /*****************************************************************************/
148 * load an fdpic binary into various bits of memory
150 static int load_elf_fdpic_binary(struct linux_binprm *bprm, struct pt_regs *regs)
152 struct elf_fdpic_params exec_params, interp_params;
153 struct elf_phdr *phdr;
154 unsigned long stack_size;
155 struct file *interpreter = NULL; /* to shut gcc up */
156 char *interpreter_name = NULL;
157 int executable_stack;
160 memset(&exec_params, 0, sizeof(exec_params));
161 memset(&interp_params, 0, sizeof(interp_params));
163 exec_params.hdr = *(struct elfhdr *) bprm->buf;
164 exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE;
166 /* check that this is a binary we know how to deal with */
168 if (!is_elf_fdpic(&exec_params.hdr, bprm->file))
171 /* read the program header table */
172 retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
176 /* scan for a program header that specifies an interpreter */
177 phdr = exec_params.phdrs;
179 for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) {
180 switch (phdr->p_type) {
183 if (phdr->p_filesz > PATH_MAX)
186 if (phdr->p_filesz < 2)
189 /* read the name of the interpreter into memory */
190 interpreter_name = (char *) kmalloc(phdr->p_filesz, GFP_KERNEL);
191 if (!interpreter_name)
194 retval = kernel_read(bprm->file,
202 if (interpreter_name[phdr->p_filesz - 1] != '\0')
205 kdebug("Using ELF interpreter %s", interpreter_name);
207 /* replace the program with the interpreter */
208 interpreter = open_exec(interpreter_name);
209 retval = PTR_ERR(interpreter);
210 if (IS_ERR(interpreter)) {
215 retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
219 interp_params.hdr = *((struct elfhdr *) bprm->buf);
224 if (exec_params.load_addr == 0)
225 exec_params.load_addr = phdr->p_vaddr;
232 if (elf_check_const_displacement(&exec_params.hdr))
233 exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
235 /* perform insanity checks on the interpreter */
236 if (interpreter_name) {
238 if (!is_elf_fdpic(&interp_params.hdr, interpreter))
241 interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
243 /* read the interpreter's program header table */
244 retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
249 stack_size = exec_params.stack_size;
250 if (stack_size < interp_params.stack_size)
251 stack_size = interp_params.stack_size;
253 if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
254 executable_stack = EXSTACK_ENABLE_X;
255 else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
256 executable_stack = EXSTACK_DISABLE_X;
257 else if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
258 executable_stack = EXSTACK_ENABLE_X;
259 else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
260 executable_stack = EXSTACK_DISABLE_X;
262 executable_stack = EXSTACK_DEFAULT;
268 if (elf_check_const_displacement(&interp_params.hdr))
269 interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
271 /* flush all traces of the currently running executable */
272 retval = flush_old_exec(bprm);
276 /* there's now no turning back... the old userspace image is dead,
277 * defunct, deceased, etc. after this point we have to exit via
279 set_personality(PER_LINUX_FDPIC);
280 set_binfmt(&elf_fdpic_format);
282 current->mm->start_code = 0;
283 current->mm->end_code = 0;
284 current->mm->start_stack = 0;
285 current->mm->start_data = 0;
286 current->mm->end_data = 0;
287 current->mm->context.exec_fdpic_loadmap = 0;
288 current->mm->context.interp_fdpic_loadmap = 0;
290 current->flags &= ~PF_FORKNOEXEC;
293 elf_fdpic_arch_lay_out_mm(&exec_params,
295 ¤t->mm->start_stack,
296 ¤t->mm->start_brk);
299 /* do this so that we can load the interpreter, if need be
300 * - we will change some of these later
302 // current->mm->rss = 0;
303 vx_rsspages_sub(current->mm, current->mm->rss);
306 retval = setup_arg_pages(bprm, current->mm->start_stack, executable_stack);
308 send_sig(SIGKILL, current, 0);
313 /* load the executable and interpreter into memory */
314 retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm, "executable");
318 if (interpreter_name) {
319 retval = elf_fdpic_map_file(&interp_params, interpreter,
320 current->mm, "interpreter");
322 printk(KERN_ERR "Unable to load interpreter\n");
326 allow_write_access(interpreter);
332 if (!current->mm->start_brk)
333 current->mm->start_brk = current->mm->end_data;
335 current->mm->brk = current->mm->start_brk = PAGE_ALIGN(current->mm->start_brk);
338 /* create a stack and brk area big enough for everyone
339 * - the brk heap starts at the bottom and works up
340 * - the stack starts at the top and works down
342 stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
343 if (stack_size < PAGE_SIZE * 2)
344 stack_size = PAGE_SIZE * 2;
346 down_write(¤t->mm->mmap_sem);
347 current->mm->start_brk = do_mmap(NULL,
350 PROT_READ | PROT_WRITE | PROT_EXEC,
351 MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN,
354 if (IS_ERR((void *) current->mm->start_brk)) {
355 up_write(¤t->mm->mmap_sem);
356 retval = current->mm->start_brk;
357 current->mm->start_brk = 0;
361 if (do_mremap(current->mm->start_brk,
363 ksize((char *) current->mm->start_brk),
365 ) == current->mm->start_brk
367 stack_size = ksize((char *) current->mm->start_brk);
368 up_write(¤t->mm->mmap_sem);
370 current->mm->brk = current->mm->start_brk;
371 current->mm->context.end_brk = current->mm->start_brk;
372 current->mm->context.end_brk += (stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0;
373 current->mm->start_stack = current->mm->start_brk + stack_size;
377 current->flags &= ~PF_FORKNOEXEC;
378 if (create_elf_fdpic_tables(bprm, current->mm, &exec_params, &interp_params) < 0)
381 kdebug("- start_code %lx", (long) current->mm->start_code);
382 kdebug("- end_code %lx", (long) current->mm->end_code);
383 kdebug("- start_data %lx", (long) current->mm->start_data);
384 kdebug("- end_data %lx", (long) current->mm->end_data);
385 kdebug("- start_brk %lx", (long) current->mm->start_brk);
386 kdebug("- brk %lx", (long) current->mm->brk);
387 kdebug("- start_stack %lx", (long) current->mm->start_stack);
389 #ifdef ELF_FDPIC_PLAT_INIT
391 * The ABI may specify that certain registers be set up in special
392 * ways (on i386 %edx is the address of a DT_FINI function, for
393 * example. This macro performs whatever initialization to
394 * the regs structure is required.
396 ELF_FDPIC_PLAT_INIT(regs,
397 exec_params.map_addr,
398 interp_params.map_addr,
399 interp_params.dynamic_addr ?: exec_params.dynamic_addr
403 /* everything is now ready... get the userspace context ready to roll */
405 interp_params.entry_addr ?: exec_params.entry_addr,
406 current->mm->start_stack);
408 if (unlikely(current->ptrace & PT_PTRACED)) {
409 if (current->ptrace & PT_TRACE_EXEC)
410 ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
412 send_sig(SIGTRAP, current, 0);
419 allow_write_access(interpreter);
422 if (interpreter_name)
423 kfree(interpreter_name);
424 if (exec_params.phdrs)
425 kfree(exec_params.phdrs);
426 if (exec_params.loadmap)
427 kfree(exec_params.loadmap);
428 if (interp_params.phdrs)
429 kfree(interp_params.phdrs);
430 if (interp_params.loadmap)
431 kfree(interp_params.loadmap);
434 /* unrecoverable error - kill the process */
436 send_sig(SIGSEGV, current, 0);
439 } /* end load_elf_fdpic_binary() */
441 /*****************************************************************************/
443 * present useful information to the program
445 static int create_elf_fdpic_tables(struct linux_binprm *bprm,
446 struct mm_struct *mm,
447 struct elf_fdpic_params *exec_params,
448 struct elf_fdpic_params *interp_params)
450 unsigned long sp, csp, nitems;
451 elf_caddr_t *argv, *envp;
452 size_t platform_len = 0, len;
453 char *k_platform, *u_platform, *p;
457 /* we're going to shovel a whole load of stuff onto the stack */
461 sp = mm->start_stack;
463 /* stack the program arguments and environment */
464 if (elf_fdpic_transfer_args_to_stack(bprm, &sp) < 0)
468 /* get hold of platform and hardware capabilities masks for the machine
469 * we are running on. In some cases (Sparc), this info is impossible
470 * to get, in others (i386) it is merely difficult.
473 k_platform = ELF_PLATFORM;
476 platform_len = strlen(k_platform) + 1;
478 if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
482 u_platform = (char *) sp;
484 #if defined(__i386__) && defined(CONFIG_SMP)
485 /* in some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
486 * by the processes running on the same package. One thing we can do
487 * is to shuffle the initial stack for them.
489 * the conditionals here are unneeded, but kept in to make the
490 * code behaviour the same as pre change unless we have hyperthreaded
491 * processors. This keeps Mr Marcelo Person happier but should be
494 if (smp_num_siblings > 1)
495 sp = sp - ((current->pid % 64) << 7);
500 /* stack the load map(s) */
501 len = sizeof(struct elf32_fdpic_loadmap);
502 len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
503 sp = (sp - len) & ~7UL;
504 exec_params->map_addr = sp;
506 if (copy_to_user((void *) sp, exec_params->loadmap, len) != 0)
509 current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
511 if (interp_params->loadmap) {
512 len = sizeof(struct elf32_fdpic_loadmap);
513 len += sizeof(struct elf32_fdpic_loadseg) * interp_params->loadmap->nsegs;
514 sp = (sp - len) & ~7UL;
515 interp_params->map_addr = sp;
517 if (copy_to_user((void *) sp, interp_params->loadmap, len) != 0)
520 current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
523 /* force 16 byte _final_ alignment here for generality */
524 #define DLINFO_ITEMS 13
526 nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0);
527 #ifdef DLINFO_ARCH_ITEMS
528 nitems += DLINFO_ARCH_ITEMS;
532 sp -= nitems * 2 * sizeof(unsigned long);
533 sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
534 sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
535 sp -= 1 * sizeof(unsigned long); /* argc */
540 /* put the ELF interpreter info on the stack */
541 #define NEW_AUX_ENT(nr, id, val) \
543 struct { unsigned long _id, _val; } *ent = (void *) csp; \
544 __put_user((id), &ent[nr]._id); \
545 __put_user((val), &ent[nr]._val); \
548 csp -= 2 * sizeof(unsigned long);
549 NEW_AUX_ENT(0, AT_NULL, 0);
551 csp -= 2 * sizeof(unsigned long);
552 NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
555 csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
556 NEW_AUX_ENT( 0, AT_HWCAP, hwcap);
557 NEW_AUX_ENT( 1, AT_PAGESZ, PAGE_SIZE);
558 NEW_AUX_ENT( 2, AT_CLKTCK, CLOCKS_PER_SEC);
559 NEW_AUX_ENT( 3, AT_PHDR, exec_params->ph_addr);
560 NEW_AUX_ENT( 4, AT_PHENT, sizeof(struct elf_phdr));
561 NEW_AUX_ENT( 5, AT_PHNUM, exec_params->hdr.e_phnum);
562 NEW_AUX_ENT( 6, AT_BASE, interp_params->elfhdr_addr);
563 NEW_AUX_ENT( 7, AT_FLAGS, 0);
564 NEW_AUX_ENT( 8, AT_ENTRY, exec_params->entry_addr);
565 NEW_AUX_ENT( 9, AT_UID, (elf_addr_t) current->uid);
566 NEW_AUX_ENT(10, AT_EUID, (elf_addr_t) current->euid);
567 NEW_AUX_ENT(11, AT_GID, (elf_addr_t) current->gid);
568 NEW_AUX_ENT(12, AT_EGID, (elf_addr_t) current->egid);
571 /* ARCH_DLINFO must come last so platform specific code can enforce
572 * special alignment requirements on the AUXV if necessary (eg. PPC).
578 /* allocate room for argv[] and envv[] */
579 csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
580 envp = (elf_caddr_t *) csp;
581 csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
582 argv = (elf_caddr_t *) csp;
585 csp -= sizeof(unsigned long);
586 __put_user(bprm->argc, (unsigned long *) csp);
591 /* fill in the argv[] array */
593 current->mm->arg_start = bprm->p;
595 current->mm->arg_start = current->mm->start_stack - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
598 p = (char *) current->mm->arg_start;
599 for (loop = bprm->argc; loop > 0; loop--) {
600 __put_user((elf_caddr_t) p, argv++);
601 len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
602 if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
606 __put_user(NULL, argv);
607 current->mm->arg_end = (unsigned long) p;
609 /* fill in the envv[] array */
610 current->mm->env_start = (unsigned long) p;
611 for (loop = bprm->envc; loop > 0; loop--) {
612 __put_user((elf_caddr_t)(unsigned long) p, envp++);
613 len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
614 if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
618 __put_user(NULL, envp);
619 current->mm->env_end = (unsigned long) p;
621 mm->start_stack = (unsigned long) sp;
623 } /* end create_elf_fdpic_tables() */
625 /*****************************************************************************/
627 * transfer the program arguments and environment from the holding pages onto
631 static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm, unsigned long *_sp)
633 unsigned long index, stop, sp;
637 stop = bprm->p >> PAGE_SHIFT;
640 for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
641 src = kmap(bprm->page[index]);
643 if (copy_to_user((void *) sp, src, PAGE_SIZE) != 0)
645 kunmap(bprm->page[index]);
650 *_sp = (*_sp - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p)) & ~15;
654 } /* end elf_fdpic_transfer_args_to_stack() */
657 /*****************************************************************************/
659 * load the appropriate binary image (executable or interpreter) into memory
660 * - we assume no MMU is available
661 * - if no other PIC bits are set in params->hdr->e_flags
662 * - we assume that the LOADable segments in the binary are independently relocatable
663 * - we assume R/O executable segments are shareable
665 * - we assume the loadable parts of the image to require fixed displacement
666 * - the image is not shareable
668 static int elf_fdpic_map_file(struct elf_fdpic_params *params,
670 struct mm_struct *mm,
673 struct elf32_fdpic_loadmap *loadmap;
675 struct elf32_fdpic_loadseg *mseg;
677 struct elf32_fdpic_loadseg *seg;
678 struct elf32_phdr *phdr;
679 unsigned long load_addr, stop;
680 unsigned nloads, tmp;
684 /* allocate a load map table */
686 for (loop = 0; loop < params->hdr.e_phnum; loop++)
687 if (params->phdrs[loop].p_type == PT_LOAD)
693 size = sizeof(*loadmap) + nloads * sizeof(*seg);
694 loadmap = kmalloc(size, GFP_KERNEL);
698 params->loadmap = loadmap;
699 memset(loadmap, 0, size);
701 loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
702 loadmap->nsegs = nloads;
704 load_addr = params->load_addr;
707 /* map the requested LOADs into the memory space */
708 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
709 case ELF_FDPIC_FLAG_CONSTDISP:
710 case ELF_FDPIC_FLAG_CONTIGUOUS:
712 ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
718 ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
724 /* map the entry point */
725 if (params->hdr.e_entry) {
727 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
728 if (params->hdr.e_entry >= seg->p_vaddr &&
729 params->hdr.e_entry < seg->p_vaddr + seg->p_memsz
732 (params->hdr.e_entry - seg->p_vaddr) + seg->addr;
738 /* determine where the program header table has wound up if mapped */
739 stop = params->hdr.e_phoff + params->hdr.e_phnum * sizeof (struct elf_phdr);
740 phdr = params->phdrs;
742 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
743 if (phdr->p_type != PT_LOAD)
746 if (phdr->p_offset > params->hdr.e_phoff ||
747 phdr->p_offset + phdr->p_filesz < stop)
751 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
752 if (phdr->p_vaddr >= seg->p_vaddr &&
753 phdr->p_vaddr + phdr->p_filesz <= seg->p_vaddr + seg->p_memsz
755 params->ph_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr +
756 params->hdr.e_phoff - phdr->p_offset;
763 /* determine where the dynamic section has wound up if there is one */
764 phdr = params->phdrs;
765 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
766 if (phdr->p_type != PT_DYNAMIC)
770 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
771 if (phdr->p_vaddr >= seg->p_vaddr &&
772 phdr->p_vaddr + phdr->p_memsz <= seg->p_vaddr + seg->p_memsz
774 params->dynamic_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr;
776 /* check the dynamic section contains at least one item, and that
777 * the last item is a NULL entry */
778 if (phdr->p_memsz == 0 ||
779 phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
782 tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
783 if (((Elf32_Dyn *) params->dynamic_addr)[tmp - 1].d_tag != 0)
791 /* now elide adjacent segments in the load map on MMU linux
792 * - on uClinux the holes between may actually be filled with system stuff or stuff from
796 nloads = loadmap->nsegs;
797 mseg = loadmap->segs;
799 for (loop = 1; loop < nloads; loop++) {
800 /* see if we have a candidate for merging */
801 if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
802 load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
803 if (load_addr == (seg->addr & PAGE_MASK)) {
804 mseg->p_memsz += load_addr - (mseg->addr + mseg->p_memsz);
805 mseg->p_memsz += seg->addr & ~PAGE_MASK;
806 mseg->p_memsz += seg->p_memsz;
818 kdebug("Mapped Object [%s]:", what);
819 kdebug("- elfhdr : %lx", params->elfhdr_addr);
820 kdebug("- entry : %lx", params->entry_addr);
821 kdebug("- PHDR[] : %lx", params->ph_addr);
822 kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
824 for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
825 kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
827 seg->addr, seg->addr + seg->p_memsz - 1,
828 seg->p_vaddr, seg->p_memsz);
833 printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
834 what, file->f_dentry->d_inode->i_ino);
836 } /* end elf_fdpic_map_file() */
838 /*****************************************************************************/
840 * map a file with constant displacement under uClinux
843 static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *params,
845 struct mm_struct *mm)
847 struct elf32_fdpic_loadseg *seg;
848 struct elf32_phdr *phdr;
849 unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
853 load_addr = params->load_addr;
854 seg = params->loadmap->segs;
856 /* determine the bounds of the contiguous overall allocation we must make */
857 phdr = params->phdrs;
858 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
859 if (params->phdrs[loop].p_type != PT_LOAD)
862 if (base > phdr->p_vaddr)
863 base = phdr->p_vaddr;
864 if (top < phdr->p_vaddr + phdr->p_memsz)
865 top = phdr->p_vaddr + phdr->p_memsz;
868 /* allocate one big anon block for everything */
869 mflags = MAP_PRIVATE;
870 if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
871 mflags |= MAP_EXECUTABLE;
873 down_write(&mm->mmap_sem);
874 maddr = do_mmap(NULL, load_addr, top - base,
875 PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
876 up_write(&mm->mmap_sem);
877 if (IS_ERR((void *) maddr))
881 load_addr += PAGE_ALIGN(top - base);
883 /* and then load the file segments into it */
884 phdr = params->phdrs;
885 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
886 if (params->phdrs[loop].p_type != PT_LOAD)
889 fpos = phdr->p_offset;
891 seg->addr = maddr + (phdr->p_vaddr - base);
892 seg->p_vaddr = phdr->p_vaddr;
893 seg->p_memsz = phdr->p_memsz;
895 ret = file->f_op->read(file, (void *) seg->addr, phdr->p_filesz, &fpos);
899 /* map the ELF header address if in this segment */
900 if (phdr->p_offset == 0)
901 params->elfhdr_addr = seg->addr;
903 /* clear any space allocated but not loaded */
904 if (phdr->p_filesz < phdr->p_memsz)
905 clear_user((void *) (seg->addr + phdr->p_filesz),
906 phdr->p_memsz - phdr->p_filesz);
909 if (phdr->p_flags & PF_X) {
910 mm->start_code = seg->addr;
911 mm->end_code = seg->addr + phdr->p_memsz;
913 else if (!mm->start_data) {
914 mm->start_data = seg->addr;
916 mm->end_data = seg->addr + phdr->p_memsz;
921 if (seg->addr + phdr->p_memsz > mm->end_data)
922 mm->end_data = seg->addr + phdr->p_memsz;
930 } /* end elf_fdpic_map_file_constdisp_on_uclinux() */
933 /*****************************************************************************/
935 * map a binary by direct mmap() of the individual PT_LOAD segments
937 static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
939 struct mm_struct *mm)
941 struct elf32_fdpic_loadseg *seg;
942 struct elf32_phdr *phdr;
943 unsigned long load_addr, delta_vaddr;
946 load_addr = params->load_addr;
950 seg = params->loadmap->segs;
952 /* deal with each load segment separately */
953 phdr = params->phdrs;
954 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
955 unsigned long maddr, disp, excess, excess1;
958 if (phdr->p_type != PT_LOAD)
961 kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
962 (unsigned long) phdr->p_vaddr,
963 (unsigned long) phdr->p_offset,
964 (unsigned long) phdr->p_filesz,
965 (unsigned long) phdr->p_memsz);
967 /* determine the mapping parameters */
968 if (phdr->p_flags & PF_R) prot |= PROT_READ;
969 if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
970 if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
972 flags = MAP_PRIVATE | MAP_DENYWRITE;
973 if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
974 flags |= MAP_EXECUTABLE;
978 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
979 case ELF_FDPIC_FLAG_INDEPENDENT:
980 /* PT_LOADs are independently locatable */
983 case ELF_FDPIC_FLAG_HONOURVADDR:
984 /* the specified virtual address must be honoured */
985 maddr = phdr->p_vaddr;
989 case ELF_FDPIC_FLAG_CONSTDISP:
990 /* constant displacement
991 * - can be mapped anywhere, but must be mapped as a unit
995 delta_vaddr = phdr->p_vaddr;
999 maddr = load_addr + phdr->p_vaddr - delta_vaddr;
1004 case ELF_FDPIC_FLAG_CONTIGUOUS:
1005 /* contiguity handled later */
1014 /* create the mapping */
1015 disp = phdr->p_vaddr & ~PAGE_MASK;
1016 down_write(&mm->mmap_sem);
1017 maddr = do_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
1018 phdr->p_offset - disp);
1019 up_write(&mm->mmap_sem);
1021 kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
1022 loop, phdr->p_memsz + disp, prot, flags, phdr->p_offset - disp,
1025 if (IS_ERR((void *) maddr))
1028 if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) == ELF_FDPIC_FLAG_CONTIGUOUS)
1029 load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
1031 seg->addr = maddr + disp;
1032 seg->p_vaddr = phdr->p_vaddr;
1033 seg->p_memsz = phdr->p_memsz;
1035 /* map the ELF header address if in this segment */
1036 if (phdr->p_offset == 0)
1037 params->elfhdr_addr = seg->addr;
1039 /* clear the bit between beginning of mapping and beginning of PT_LOAD */
1040 if (prot & PROT_WRITE && disp > 0) {
1041 kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
1042 clear_user((void *) maddr, disp);
1046 /* clear any space allocated but not loaded
1047 * - on uClinux we can just clear the lot
1048 * - on MMU linux we'll get a SIGBUS beyond the last page
1049 * extant in the file
1051 excess = phdr->p_memsz - phdr->p_filesz;
1052 excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
1056 if (excess > excess1) {
1057 unsigned long xaddr = maddr + phdr->p_filesz + excess1;
1058 unsigned long xmaddr;
1060 flags |= MAP_FIXED | MAP_ANONYMOUS;
1061 down_write(&mm->mmap_sem);
1062 xmaddr = do_mmap(NULL, xaddr, excess - excess1, prot, flags, 0);
1063 up_write(&mm->mmap_sem);
1065 kdebug("mmap[%d] <anon>"
1066 " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
1067 loop, xaddr, excess - excess1, prot, flags, xmaddr);
1069 if (xmaddr != xaddr)
1073 if (prot & PROT_WRITE && excess1 > 0) {
1074 kdebug("clear[%d] ad=%lx sz=%lx",
1075 loop, maddr + phdr->p_filesz, excess1);
1076 clear_user((void *) maddr + phdr->p_filesz, excess1);
1081 kdebug("clear[%d] ad=%lx sz=%lx",
1082 loop, maddr + phdr->p_filesz, excess);
1083 clear_user((void *) maddr + phdr->p_filesz, excess);
1088 if (phdr->p_flags & PF_X) {
1089 mm->start_code = maddr;
1090 mm->end_code = maddr + phdr->p_memsz;
1092 else if (!mm->start_data) {
1093 mm->start_data = maddr;
1094 mm->end_data = maddr + phdr->p_memsz;
1102 } /* end elf_fdpic_map_file_by_direct_mmap() */