diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /fs/binfmt_elf_fdpic.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'fs/binfmt_elf_fdpic.c')
-rw-r--r-- | fs/binfmt_elf_fdpic.c | 1101 |
1 files changed, 1101 insertions, 0 deletions
diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c new file mode 100644 index 000000000000..134c9c0d1f54 --- /dev/null +++ b/fs/binfmt_elf_fdpic.c | |||
@@ -0,0 +1,1101 @@ | |||
1 | /* binfmt_elf_fdpic.c: FDPIC ELF binary format | ||
2 | * | ||
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 | ||
6 | * | ||
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. | ||
11 | */ | ||
12 | |||
13 | #include <linux/module.h> | ||
14 | |||
15 | #include <linux/fs.h> | ||
16 | #include <linux/stat.h> | ||
17 | #include <linux/sched.h> | ||
18 | #include <linux/mm.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> | ||
35 | |||
36 | #include <asm/uaccess.h> | ||
37 | #include <asm/param.h> | ||
38 | #include <asm/pgalloc.h> | ||
39 | |||
40 | typedef char *elf_caddr_t; | ||
41 | #ifndef elf_addr_t | ||
42 | #define elf_addr_t unsigned long | ||
43 | #endif | ||
44 | |||
45 | #if 0 | ||
46 | #define kdebug(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ ) | ||
47 | #else | ||
48 | #define kdebug(fmt, ...) do {} while(0) | ||
49 | #endif | ||
50 | |||
51 | MODULE_LICENSE("GPL"); | ||
52 | |||
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, | ||
57 | struct file *file, | ||
58 | struct mm_struct *mm, | ||
59 | const char *what); | ||
60 | |||
61 | static int create_elf_fdpic_tables(struct linux_binprm *bprm, | ||
62 | struct mm_struct *mm, | ||
63 | struct elf_fdpic_params *exec_params, | ||
64 | struct elf_fdpic_params *interp_params); | ||
65 | |||
66 | #ifndef CONFIG_MMU | ||
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, | ||
69 | struct file *file, | ||
70 | struct mm_struct *mm); | ||
71 | #endif | ||
72 | |||
73 | static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params, | ||
74 | struct file *file, | ||
75 | struct mm_struct *mm); | ||
76 | |||
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, | ||
83 | }; | ||
84 | |||
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); } | ||
87 | |||
88 | module_init(init_elf_fdpic_binfmt) | ||
89 | module_exit(exit_elf_fdpic_binfmt) | ||
90 | |||
91 | static int is_elf_fdpic(struct elfhdr *hdr, struct file *file) | ||
92 | { | ||
93 | if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0) | ||
94 | return 0; | ||
95 | if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) | ||
96 | return 0; | ||
97 | if (!elf_check_arch(hdr) || !elf_check_fdpic(hdr)) | ||
98 | return 0; | ||
99 | if (!file->f_op || !file->f_op->mmap) | ||
100 | return 0; | ||
101 | return 1; | ||
102 | } | ||
103 | |||
104 | /*****************************************************************************/ | ||
105 | /* | ||
106 | * read the program headers table into memory | ||
107 | */ | ||
108 | static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params, struct file *file) | ||
109 | { | ||
110 | struct elf32_phdr *phdr; | ||
111 | unsigned long size; | ||
112 | int retval, loop; | ||
113 | |||
114 | if (params->hdr.e_phentsize != sizeof(struct elf_phdr)) | ||
115 | return -ENOMEM; | ||
116 | if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr)) | ||
117 | return -ENOMEM; | ||
118 | |||
119 | size = params->hdr.e_phnum * sizeof(struct elf_phdr); | ||
120 | params->phdrs = kmalloc(size, GFP_KERNEL); | ||
121 | if (!params->phdrs) | ||
122 | return -ENOMEM; | ||
123 | |||
124 | retval = kernel_read(file, params->hdr.e_phoff, (char *) params->phdrs, size); | ||
125 | if (retval < 0) | ||
126 | return retval; | ||
127 | |||
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) | ||
132 | continue; | ||
133 | |||
134 | if (phdr->p_flags & PF_X) | ||
135 | params->flags |= ELF_FDPIC_FLAG_EXEC_STACK; | ||
136 | else | ||
137 | params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK; | ||
138 | |||
139 | params->stack_size = phdr->p_memsz; | ||
140 | break; | ||
141 | } | ||
142 | |||
143 | return 0; | ||
144 | } /* end elf_fdpic_fetch_phdrs() */ | ||
145 | |||
146 | /*****************************************************************************/ | ||
147 | /* | ||
148 | * load an fdpic binary into various bits of memory | ||
149 | */ | ||
150 | static int load_elf_fdpic_binary(struct linux_binprm *bprm, struct pt_regs *regs) | ||
151 | { | ||
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; | ||
158 | int retval, i; | ||
159 | |||
160 | memset(&exec_params, 0, sizeof(exec_params)); | ||
161 | memset(&interp_params, 0, sizeof(interp_params)); | ||
162 | |||
163 | exec_params.hdr = *(struct elfhdr *) bprm->buf; | ||
164 | exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE; | ||
165 | |||
166 | /* check that this is a binary we know how to deal with */ | ||
167 | retval = -ENOEXEC; | ||
168 | if (!is_elf_fdpic(&exec_params.hdr, bprm->file)) | ||
169 | goto error; | ||
170 | |||
171 | /* read the program header table */ | ||
172 | retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file); | ||
173 | if (retval < 0) | ||
174 | goto error; | ||
175 | |||
176 | /* scan for a program header that specifies an interpreter */ | ||
177 | phdr = exec_params.phdrs; | ||
178 | |||
179 | for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) { | ||
180 | switch (phdr->p_type) { | ||
181 | case PT_INTERP: | ||
182 | retval = -ENOMEM; | ||
183 | if (phdr->p_filesz > PATH_MAX) | ||
184 | goto error; | ||
185 | retval = -ENOENT; | ||
186 | if (phdr->p_filesz < 2) | ||
187 | goto error; | ||
188 | |||
189 | /* read the name of the interpreter into memory */ | ||
190 | interpreter_name = (char *) kmalloc(phdr->p_filesz, GFP_KERNEL); | ||
191 | if (!interpreter_name) | ||
192 | goto error; | ||
193 | |||
194 | retval = kernel_read(bprm->file, | ||
195 | phdr->p_offset, | ||
196 | interpreter_name, | ||
197 | phdr->p_filesz); | ||
198 | if (retval < 0) | ||
199 | goto error; | ||
200 | |||
201 | retval = -ENOENT; | ||
202 | if (interpreter_name[phdr->p_filesz - 1] != '\0') | ||
203 | goto error; | ||
204 | |||
205 | kdebug("Using ELF interpreter %s", interpreter_name); | ||
206 | |||
207 | /* replace the program with the interpreter */ | ||
208 | interpreter = open_exec(interpreter_name); | ||
209 | retval = PTR_ERR(interpreter); | ||
210 | if (IS_ERR(interpreter)) { | ||
211 | interpreter = NULL; | ||
212 | goto error; | ||
213 | } | ||
214 | |||
215 | retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE); | ||
216 | if (retval < 0) | ||
217 | goto error; | ||
218 | |||
219 | interp_params.hdr = *((struct elfhdr *) bprm->buf); | ||
220 | break; | ||
221 | |||
222 | case PT_LOAD: | ||
223 | #ifdef CONFIG_MMU | ||
224 | if (exec_params.load_addr == 0) | ||
225 | exec_params.load_addr = phdr->p_vaddr; | ||
226 | #endif | ||
227 | break; | ||
228 | } | ||
229 | |||
230 | } | ||
231 | |||
232 | if (elf_check_const_displacement(&exec_params.hdr)) | ||
233 | exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP; | ||
234 | |||
235 | /* perform insanity checks on the interpreter */ | ||
236 | if (interpreter_name) { | ||
237 | retval = -ELIBBAD; | ||
238 | if (!is_elf_fdpic(&interp_params.hdr, interpreter)) | ||
239 | goto error; | ||
240 | |||
241 | interp_params.flags = ELF_FDPIC_FLAG_PRESENT; | ||
242 | |||
243 | /* read the interpreter's program header table */ | ||
244 | retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter); | ||
245 | if (retval < 0) | ||
246 | goto error; | ||
247 | } | ||
248 | |||
249 | stack_size = exec_params.stack_size; | ||
250 | if (stack_size < interp_params.stack_size) | ||
251 | stack_size = interp_params.stack_size; | ||
252 | |||
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; | ||
261 | else | ||
262 | executable_stack = EXSTACK_DEFAULT; | ||
263 | |||
264 | retval = -ENOEXEC; | ||
265 | if (stack_size == 0) | ||
266 | goto error; | ||
267 | |||
268 | if (elf_check_const_displacement(&interp_params.hdr)) | ||
269 | interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP; | ||
270 | |||
271 | /* flush all traces of the currently running executable */ | ||
272 | retval = flush_old_exec(bprm); | ||
273 | if (retval) | ||
274 | goto error; | ||
275 | |||
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 | ||
278 | * error_kill */ | ||
279 | set_personality(PER_LINUX_FDPIC); | ||
280 | set_binfmt(&elf_fdpic_format); | ||
281 | |||
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; | ||
289 | |||
290 | current->flags &= ~PF_FORKNOEXEC; | ||
291 | |||
292 | #ifdef CONFIG_MMU | ||
293 | elf_fdpic_arch_lay_out_mm(&exec_params, | ||
294 | &interp_params, | ||
295 | ¤t->mm->start_stack, | ||
296 | ¤t->mm->start_brk); | ||
297 | #endif | ||
298 | |||
299 | /* do this so that we can load the interpreter, if need be | ||
300 | * - we will change some of these later | ||
301 | */ | ||
302 | set_mm_counter(current->mm, rss, 0); | ||
303 | |||
304 | #ifdef CONFIG_MMU | ||
305 | retval = setup_arg_pages(bprm, current->mm->start_stack, executable_stack); | ||
306 | if (retval < 0) { | ||
307 | send_sig(SIGKILL, current, 0); | ||
308 | goto error_kill; | ||
309 | } | ||
310 | #endif | ||
311 | |||
312 | /* load the executable and interpreter into memory */ | ||
313 | retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm, "executable"); | ||
314 | if (retval < 0) | ||
315 | goto error_kill; | ||
316 | |||
317 | if (interpreter_name) { | ||
318 | retval = elf_fdpic_map_file(&interp_params, interpreter, | ||
319 | current->mm, "interpreter"); | ||
320 | if (retval < 0) { | ||
321 | printk(KERN_ERR "Unable to load interpreter\n"); | ||
322 | goto error_kill; | ||
323 | } | ||
324 | |||
325 | allow_write_access(interpreter); | ||
326 | fput(interpreter); | ||
327 | interpreter = NULL; | ||
328 | } | ||
329 | |||
330 | #ifdef CONFIG_MMU | ||
331 | if (!current->mm->start_brk) | ||
332 | current->mm->start_brk = current->mm->end_data; | ||
333 | |||
334 | current->mm->brk = current->mm->start_brk = PAGE_ALIGN(current->mm->start_brk); | ||
335 | |||
336 | #else | ||
337 | /* create a stack and brk area big enough for everyone | ||
338 | * - the brk heap starts at the bottom and works up | ||
339 | * - the stack starts at the top and works down | ||
340 | */ | ||
341 | stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK; | ||
342 | if (stack_size < PAGE_SIZE * 2) | ||
343 | stack_size = PAGE_SIZE * 2; | ||
344 | |||
345 | down_write(¤t->mm->mmap_sem); | ||
346 | current->mm->start_brk = do_mmap(NULL, | ||
347 | 0, | ||
348 | stack_size, | ||
349 | PROT_READ | PROT_WRITE | PROT_EXEC, | ||
350 | MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN, | ||
351 | 0); | ||
352 | |||
353 | if (IS_ERR((void *) current->mm->start_brk)) { | ||
354 | up_write(¤t->mm->mmap_sem); | ||
355 | retval = current->mm->start_brk; | ||
356 | current->mm->start_brk = 0; | ||
357 | goto error_kill; | ||
358 | } | ||
359 | |||
360 | if (do_mremap(current->mm->start_brk, | ||
361 | stack_size, | ||
362 | ksize((char *) current->mm->start_brk), | ||
363 | 0, 0 | ||
364 | ) == current->mm->start_brk | ||
365 | ) | ||
366 | stack_size = ksize((char *) current->mm->start_brk); | ||
367 | up_write(¤t->mm->mmap_sem); | ||
368 | |||
369 | current->mm->brk = current->mm->start_brk; | ||
370 | current->mm->context.end_brk = current->mm->start_brk; | ||
371 | current->mm->context.end_brk += (stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0; | ||
372 | current->mm->start_stack = current->mm->start_brk + stack_size; | ||
373 | #endif | ||
374 | |||
375 | compute_creds(bprm); | ||
376 | current->flags &= ~PF_FORKNOEXEC; | ||
377 | if (create_elf_fdpic_tables(bprm, current->mm, &exec_params, &interp_params) < 0) | ||
378 | goto error_kill; | ||
379 | |||
380 | kdebug("- start_code %lx", (long) current->mm->start_code); | ||
381 | kdebug("- end_code %lx", (long) current->mm->end_code); | ||
382 | kdebug("- start_data %lx", (long) current->mm->start_data); | ||
383 | kdebug("- end_data %lx", (long) current->mm->end_data); | ||
384 | kdebug("- start_brk %lx", (long) current->mm->start_brk); | ||
385 | kdebug("- brk %lx", (long) current->mm->brk); | ||
386 | kdebug("- start_stack %lx", (long) current->mm->start_stack); | ||
387 | |||
388 | #ifdef ELF_FDPIC_PLAT_INIT | ||
389 | /* | ||
390 | * The ABI may specify that certain registers be set up in special | ||
391 | * ways (on i386 %edx is the address of a DT_FINI function, for | ||
392 | * example. This macro performs whatever initialization to | ||
393 | * the regs structure is required. | ||
394 | */ | ||
395 | ELF_FDPIC_PLAT_INIT(regs, | ||
396 | exec_params.map_addr, | ||
397 | interp_params.map_addr, | ||
398 | interp_params.dynamic_addr ?: exec_params.dynamic_addr | ||
399 | ); | ||
400 | #endif | ||
401 | |||
402 | /* everything is now ready... get the userspace context ready to roll */ | ||
403 | start_thread(regs, | ||
404 | interp_params.entry_addr ?: exec_params.entry_addr, | ||
405 | current->mm->start_stack); | ||
406 | |||
407 | if (unlikely(current->ptrace & PT_PTRACED)) { | ||
408 | if (current->ptrace & PT_TRACE_EXEC) | ||
409 | ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP); | ||
410 | else | ||
411 | send_sig(SIGTRAP, current, 0); | ||
412 | } | ||
413 | |||
414 | retval = 0; | ||
415 | |||
416 | error: | ||
417 | if (interpreter) { | ||
418 | allow_write_access(interpreter); | ||
419 | fput(interpreter); | ||
420 | } | ||
421 | if (interpreter_name) | ||
422 | kfree(interpreter_name); | ||
423 | if (exec_params.phdrs) | ||
424 | kfree(exec_params.phdrs); | ||
425 | if (exec_params.loadmap) | ||
426 | kfree(exec_params.loadmap); | ||
427 | if (interp_params.phdrs) | ||
428 | kfree(interp_params.phdrs); | ||
429 | if (interp_params.loadmap) | ||
430 | kfree(interp_params.loadmap); | ||
431 | return retval; | ||
432 | |||
433 | /* unrecoverable error - kill the process */ | ||
434 | error_kill: | ||
435 | send_sig(SIGSEGV, current, 0); | ||
436 | goto error; | ||
437 | |||
438 | } /* end load_elf_fdpic_binary() */ | ||
439 | |||
440 | /*****************************************************************************/ | ||
441 | /* | ||
442 | * present useful information to the program | ||
443 | */ | ||
444 | static int create_elf_fdpic_tables(struct linux_binprm *bprm, | ||
445 | struct mm_struct *mm, | ||
446 | struct elf_fdpic_params *exec_params, | ||
447 | struct elf_fdpic_params *interp_params) | ||
448 | { | ||
449 | unsigned long sp, csp, nitems; | ||
450 | elf_caddr_t *argv, *envp; | ||
451 | size_t platform_len = 0, len; | ||
452 | char *k_platform, *u_platform, *p; | ||
453 | long hwcap; | ||
454 | int loop; | ||
455 | |||
456 | /* we're going to shovel a whole load of stuff onto the stack */ | ||
457 | #ifdef CONFIG_MMU | ||
458 | sp = bprm->p; | ||
459 | #else | ||
460 | sp = mm->start_stack; | ||
461 | |||
462 | /* stack the program arguments and environment */ | ||
463 | if (elf_fdpic_transfer_args_to_stack(bprm, &sp) < 0) | ||
464 | return -EFAULT; | ||
465 | #endif | ||
466 | |||
467 | /* get hold of platform and hardware capabilities masks for the machine | ||
468 | * we are running on. In some cases (Sparc), this info is impossible | ||
469 | * to get, in others (i386) it is merely difficult. | ||
470 | */ | ||
471 | hwcap = ELF_HWCAP; | ||
472 | k_platform = ELF_PLATFORM; | ||
473 | |||
474 | if (k_platform) { | ||
475 | platform_len = strlen(k_platform) + 1; | ||
476 | sp -= platform_len; | ||
477 | if (__copy_to_user(u_platform, k_platform, platform_len) != 0) | ||
478 | return -EFAULT; | ||
479 | } | ||
480 | |||
481 | u_platform = (char *) sp; | ||
482 | |||
483 | #if defined(__i386__) && defined(CONFIG_SMP) | ||
484 | /* in some cases (e.g. Hyper-Threading), we want to avoid L1 evictions | ||
485 | * by the processes running on the same package. One thing we can do | ||
486 | * is to shuffle the initial stack for them. | ||
487 | * | ||
488 | * the conditionals here are unneeded, but kept in to make the | ||
489 | * code behaviour the same as pre change unless we have hyperthreaded | ||
490 | * processors. This keeps Mr Marcelo Person happier but should be | ||
491 | * removed for 2.5 | ||
492 | */ | ||
493 | if (smp_num_siblings > 1) | ||
494 | sp = sp - ((current->pid % 64) << 7); | ||
495 | #endif | ||
496 | |||
497 | sp &= ~7UL; | ||
498 | |||
499 | /* stack the load map(s) */ | ||
500 | len = sizeof(struct elf32_fdpic_loadmap); | ||
501 | len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs; | ||
502 | sp = (sp - len) & ~7UL; | ||
503 | exec_params->map_addr = sp; | ||
504 | |||
505 | if (copy_to_user((void *) sp, exec_params->loadmap, len) != 0) | ||
506 | return -EFAULT; | ||
507 | |||
508 | current->mm->context.exec_fdpic_loadmap = (unsigned long) sp; | ||
509 | |||
510 | if (interp_params->loadmap) { | ||
511 | len = sizeof(struct elf32_fdpic_loadmap); | ||
512 | len += sizeof(struct elf32_fdpic_loadseg) * interp_params->loadmap->nsegs; | ||
513 | sp = (sp - len) & ~7UL; | ||
514 | interp_params->map_addr = sp; | ||
515 | |||
516 | if (copy_to_user((void *) sp, interp_params->loadmap, len) != 0) | ||
517 | return -EFAULT; | ||
518 | |||
519 | current->mm->context.interp_fdpic_loadmap = (unsigned long) sp; | ||
520 | } | ||
521 | |||
522 | /* force 16 byte _final_ alignment here for generality */ | ||
523 | #define DLINFO_ITEMS 13 | ||
524 | |||
525 | nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0); | ||
526 | #ifdef DLINFO_ARCH_ITEMS | ||
527 | nitems += DLINFO_ARCH_ITEMS; | ||
528 | #endif | ||
529 | |||
530 | csp = sp; | ||
531 | sp -= nitems * 2 * sizeof(unsigned long); | ||
532 | sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */ | ||
533 | sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */ | ||
534 | sp -= 1 * sizeof(unsigned long); /* argc */ | ||
535 | |||
536 | csp -= sp & 15UL; | ||
537 | sp -= sp & 15UL; | ||
538 | |||
539 | /* put the ELF interpreter info on the stack */ | ||
540 | #define NEW_AUX_ENT(nr, id, val) \ | ||
541 | do { \ | ||
542 | struct { unsigned long _id, _val; } *ent = (void *) csp; \ | ||
543 | __put_user((id), &ent[nr]._id); \ | ||
544 | __put_user((val), &ent[nr]._val); \ | ||
545 | } while (0) | ||
546 | |||
547 | csp -= 2 * sizeof(unsigned long); | ||
548 | NEW_AUX_ENT(0, AT_NULL, 0); | ||
549 | if (k_platform) { | ||
550 | csp -= 2 * sizeof(unsigned long); | ||
551 | NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform); | ||
552 | } | ||
553 | |||
554 | csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long); | ||
555 | NEW_AUX_ENT( 0, AT_HWCAP, hwcap); | ||
556 | NEW_AUX_ENT( 1, AT_PAGESZ, PAGE_SIZE); | ||
557 | NEW_AUX_ENT( 2, AT_CLKTCK, CLOCKS_PER_SEC); | ||
558 | NEW_AUX_ENT( 3, AT_PHDR, exec_params->ph_addr); | ||
559 | NEW_AUX_ENT( 4, AT_PHENT, sizeof(struct elf_phdr)); | ||
560 | NEW_AUX_ENT( 5, AT_PHNUM, exec_params->hdr.e_phnum); | ||
561 | NEW_AUX_ENT( 6, AT_BASE, interp_params->elfhdr_addr); | ||
562 | NEW_AUX_ENT( 7, AT_FLAGS, 0); | ||
563 | NEW_AUX_ENT( 8, AT_ENTRY, exec_params->entry_addr); | ||
564 | NEW_AUX_ENT( 9, AT_UID, (elf_addr_t) current->uid); | ||
565 | NEW_AUX_ENT(10, AT_EUID, (elf_addr_t) current->euid); | ||
566 | NEW_AUX_ENT(11, AT_GID, (elf_addr_t) current->gid); | ||
567 | NEW_AUX_ENT(12, AT_EGID, (elf_addr_t) current->egid); | ||
568 | |||
569 | #ifdef ARCH_DLINFO | ||
570 | /* ARCH_DLINFO must come last so platform specific code can enforce | ||
571 | * special alignment requirements on the AUXV if necessary (eg. PPC). | ||
572 | */ | ||
573 | ARCH_DLINFO; | ||
574 | #endif | ||
575 | #undef NEW_AUX_ENT | ||
576 | |||
577 | /* allocate room for argv[] and envv[] */ | ||
578 | csp -= (bprm->envc + 1) * sizeof(elf_caddr_t); | ||
579 | envp = (elf_caddr_t *) csp; | ||
580 | csp -= (bprm->argc + 1) * sizeof(elf_caddr_t); | ||
581 | argv = (elf_caddr_t *) csp; | ||
582 | |||
583 | /* stack argc */ | ||
584 | csp -= sizeof(unsigned long); | ||
585 | __put_user(bprm->argc, (unsigned long *) csp); | ||
586 | |||
587 | if (csp != sp) | ||
588 | BUG(); | ||
589 | |||
590 | /* fill in the argv[] array */ | ||
591 | #ifdef CONFIG_MMU | ||
592 | current->mm->arg_start = bprm->p; | ||
593 | #else | ||
594 | current->mm->arg_start = current->mm->start_stack - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p); | ||
595 | #endif | ||
596 | |||
597 | p = (char *) current->mm->arg_start; | ||
598 | for (loop = bprm->argc; loop > 0; loop--) { | ||
599 | __put_user((elf_caddr_t) p, argv++); | ||
600 | len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES); | ||
601 | if (!len || len > PAGE_SIZE * MAX_ARG_PAGES) | ||
602 | return -EINVAL; | ||
603 | p += len; | ||
604 | } | ||
605 | __put_user(NULL, argv); | ||
606 | current->mm->arg_end = (unsigned long) p; | ||
607 | |||
608 | /* fill in the envv[] array */ | ||
609 | current->mm->env_start = (unsigned long) p; | ||
610 | for (loop = bprm->envc; loop > 0; loop--) { | ||
611 | __put_user((elf_caddr_t)(unsigned long) p, envp++); | ||
612 | len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES); | ||
613 | if (!len || len > PAGE_SIZE * MAX_ARG_PAGES) | ||
614 | return -EINVAL; | ||
615 | p += len; | ||
616 | } | ||
617 | __put_user(NULL, envp); | ||
618 | current->mm->env_end = (unsigned long) p; | ||
619 | |||
620 | mm->start_stack = (unsigned long) sp; | ||
621 | return 0; | ||
622 | } /* end create_elf_fdpic_tables() */ | ||
623 | |||
624 | /*****************************************************************************/ | ||
625 | /* | ||
626 | * transfer the program arguments and environment from the holding pages onto | ||
627 | * the stack | ||
628 | */ | ||
629 | #ifndef CONFIG_MMU | ||
630 | static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm, unsigned long *_sp) | ||
631 | { | ||
632 | unsigned long index, stop, sp; | ||
633 | char *src; | ||
634 | int ret = 0; | ||
635 | |||
636 | stop = bprm->p >> PAGE_SHIFT; | ||
637 | sp = *_sp; | ||
638 | |||
639 | for (index = MAX_ARG_PAGES - 1; index >= stop; index--) { | ||
640 | src = kmap(bprm->page[index]); | ||
641 | sp -= PAGE_SIZE; | ||
642 | if (copy_to_user((void *) sp, src, PAGE_SIZE) != 0) | ||
643 | ret = -EFAULT; | ||
644 | kunmap(bprm->page[index]); | ||
645 | if (ret < 0) | ||
646 | goto out; | ||
647 | } | ||
648 | |||
649 | *_sp = (*_sp - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p)) & ~15; | ||
650 | |||
651 | out: | ||
652 | return ret; | ||
653 | } /* end elf_fdpic_transfer_args_to_stack() */ | ||
654 | #endif | ||
655 | |||
656 | /*****************************************************************************/ | ||
657 | /* | ||
658 | * load the appropriate binary image (executable or interpreter) into memory | ||
659 | * - we assume no MMU is available | ||
660 | * - if no other PIC bits are set in params->hdr->e_flags | ||
661 | * - we assume that the LOADable segments in the binary are independently relocatable | ||
662 | * - we assume R/O executable segments are shareable | ||
663 | * - else | ||
664 | * - we assume the loadable parts of the image to require fixed displacement | ||
665 | * - the image is not shareable | ||
666 | */ | ||
667 | static int elf_fdpic_map_file(struct elf_fdpic_params *params, | ||
668 | struct file *file, | ||
669 | struct mm_struct *mm, | ||
670 | const char *what) | ||
671 | { | ||
672 | struct elf32_fdpic_loadmap *loadmap; | ||
673 | #ifdef CONFIG_MMU | ||
674 | struct elf32_fdpic_loadseg *mseg; | ||
675 | #endif | ||
676 | struct elf32_fdpic_loadseg *seg; | ||
677 | struct elf32_phdr *phdr; | ||
678 | unsigned long load_addr, stop; | ||
679 | unsigned nloads, tmp; | ||
680 | size_t size; | ||
681 | int loop, ret; | ||
682 | |||
683 | /* allocate a load map table */ | ||
684 | nloads = 0; | ||
685 | for (loop = 0; loop < params->hdr.e_phnum; loop++) | ||
686 | if (params->phdrs[loop].p_type == PT_LOAD) | ||
687 | nloads++; | ||
688 | |||
689 | if (nloads == 0) | ||
690 | return -ELIBBAD; | ||
691 | |||
692 | size = sizeof(*loadmap) + nloads * sizeof(*seg); | ||
693 | loadmap = kmalloc(size, GFP_KERNEL); | ||
694 | if (!loadmap) | ||
695 | return -ENOMEM; | ||
696 | |||
697 | params->loadmap = loadmap; | ||
698 | memset(loadmap, 0, size); | ||
699 | |||
700 | loadmap->version = ELF32_FDPIC_LOADMAP_VERSION; | ||
701 | loadmap->nsegs = nloads; | ||
702 | |||
703 | load_addr = params->load_addr; | ||
704 | seg = loadmap->segs; | ||
705 | |||
706 | /* map the requested LOADs into the memory space */ | ||
707 | switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) { | ||
708 | case ELF_FDPIC_FLAG_CONSTDISP: | ||
709 | case ELF_FDPIC_FLAG_CONTIGUOUS: | ||
710 | #ifndef CONFIG_MMU | ||
711 | ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm); | ||
712 | if (ret < 0) | ||
713 | return ret; | ||
714 | break; | ||
715 | #endif | ||
716 | default: | ||
717 | ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm); | ||
718 | if (ret < 0) | ||
719 | return ret; | ||
720 | break; | ||
721 | } | ||
722 | |||
723 | /* map the entry point */ | ||
724 | if (params->hdr.e_entry) { | ||
725 | seg = loadmap->segs; | ||
726 | for (loop = loadmap->nsegs; loop > 0; loop--, seg++) { | ||
727 | if (params->hdr.e_entry >= seg->p_vaddr && | ||
728 | params->hdr.e_entry < seg->p_vaddr + seg->p_memsz | ||
729 | ) { | ||
730 | params->entry_addr = | ||
731 | (params->hdr.e_entry - seg->p_vaddr) + seg->addr; | ||
732 | break; | ||
733 | } | ||
734 | } | ||
735 | } | ||
736 | |||
737 | /* determine where the program header table has wound up if mapped */ | ||
738 | stop = params->hdr.e_phoff + params->hdr.e_phnum * sizeof (struct elf_phdr); | ||
739 | phdr = params->phdrs; | ||
740 | |||
741 | for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) { | ||
742 | if (phdr->p_type != PT_LOAD) | ||
743 | continue; | ||
744 | |||
745 | if (phdr->p_offset > params->hdr.e_phoff || | ||
746 | phdr->p_offset + phdr->p_filesz < stop) | ||
747 | continue; | ||
748 | |||
749 | seg = loadmap->segs; | ||
750 | for (loop = loadmap->nsegs; loop > 0; loop--, seg++) { | ||
751 | if (phdr->p_vaddr >= seg->p_vaddr && | ||
752 | phdr->p_vaddr + phdr->p_filesz <= seg->p_vaddr + seg->p_memsz | ||
753 | ) { | ||
754 | params->ph_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr + | ||
755 | params->hdr.e_phoff - phdr->p_offset; | ||
756 | break; | ||
757 | } | ||
758 | } | ||
759 | break; | ||
760 | } | ||
761 | |||
762 | /* determine where the dynamic section has wound up if there is one */ | ||
763 | phdr = params->phdrs; | ||
764 | for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) { | ||
765 | if (phdr->p_type != PT_DYNAMIC) | ||
766 | continue; | ||
767 | |||
768 | seg = loadmap->segs; | ||
769 | for (loop = loadmap->nsegs; loop > 0; loop--, seg++) { | ||
770 | if (phdr->p_vaddr >= seg->p_vaddr && | ||
771 | phdr->p_vaddr + phdr->p_memsz <= seg->p_vaddr + seg->p_memsz | ||
772 | ) { | ||
773 | params->dynamic_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr; | ||
774 | |||
775 | /* check the dynamic section contains at least one item, and that | ||
776 | * the last item is a NULL entry */ | ||
777 | if (phdr->p_memsz == 0 || | ||
778 | phdr->p_memsz % sizeof(Elf32_Dyn) != 0) | ||
779 | goto dynamic_error; | ||
780 | |||
781 | tmp = phdr->p_memsz / sizeof(Elf32_Dyn); | ||
782 | if (((Elf32_Dyn *) params->dynamic_addr)[tmp - 1].d_tag != 0) | ||
783 | goto dynamic_error; | ||
784 | break; | ||
785 | } | ||
786 | } | ||
787 | break; | ||
788 | } | ||
789 | |||
790 | /* now elide adjacent segments in the load map on MMU linux | ||
791 | * - on uClinux the holes between may actually be filled with system stuff or stuff from | ||
792 | * other processes | ||
793 | */ | ||
794 | #ifdef CONFIG_MMU | ||
795 | nloads = loadmap->nsegs; | ||
796 | mseg = loadmap->segs; | ||
797 | seg = mseg + 1; | ||
798 | for (loop = 1; loop < nloads; loop++) { | ||
799 | /* see if we have a candidate for merging */ | ||
800 | if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) { | ||
801 | load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz); | ||
802 | if (load_addr == (seg->addr & PAGE_MASK)) { | ||
803 | mseg->p_memsz += load_addr - (mseg->addr + mseg->p_memsz); | ||
804 | mseg->p_memsz += seg->addr & ~PAGE_MASK; | ||
805 | mseg->p_memsz += seg->p_memsz; | ||
806 | loadmap->nsegs--; | ||
807 | continue; | ||
808 | } | ||
809 | } | ||
810 | |||
811 | mseg++; | ||
812 | if (mseg != seg) | ||
813 | *mseg = *seg; | ||
814 | } | ||
815 | #endif | ||
816 | |||
817 | kdebug("Mapped Object [%s]:", what); | ||
818 | kdebug("- elfhdr : %lx", params->elfhdr_addr); | ||
819 | kdebug("- entry : %lx", params->entry_addr); | ||
820 | kdebug("- PHDR[] : %lx", params->ph_addr); | ||
821 | kdebug("- DYNAMIC[]: %lx", params->dynamic_addr); | ||
822 | seg = loadmap->segs; | ||
823 | for (loop = 0; loop < loadmap->nsegs; loop++, seg++) | ||
824 | kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]", | ||
825 | loop, | ||
826 | seg->addr, seg->addr + seg->p_memsz - 1, | ||
827 | seg->p_vaddr, seg->p_memsz); | ||
828 | |||
829 | return 0; | ||
830 | |||
831 | dynamic_error: | ||
832 | printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n", | ||
833 | what, file->f_dentry->d_inode->i_ino); | ||
834 | return -ELIBBAD; | ||
835 | } /* end elf_fdpic_map_file() */ | ||
836 | |||
837 | /*****************************************************************************/ | ||
838 | /* | ||
839 | * map a file with constant displacement under uClinux | ||
840 | */ | ||
841 | #ifndef CONFIG_MMU | ||
842 | static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *params, | ||
843 | struct file *file, | ||
844 | struct mm_struct *mm) | ||
845 | { | ||
846 | struct elf32_fdpic_loadseg *seg; | ||
847 | struct elf32_phdr *phdr; | ||
848 | unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags; | ||
849 | loff_t fpos; | ||
850 | int loop, ret; | ||
851 | |||
852 | load_addr = params->load_addr; | ||
853 | seg = params->loadmap->segs; | ||
854 | |||
855 | /* determine the bounds of the contiguous overall allocation we must make */ | ||
856 | phdr = params->phdrs; | ||
857 | for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) { | ||
858 | if (params->phdrs[loop].p_type != PT_LOAD) | ||
859 | continue; | ||
860 | |||
861 | if (base > phdr->p_vaddr) | ||
862 | base = phdr->p_vaddr; | ||
863 | if (top < phdr->p_vaddr + phdr->p_memsz) | ||
864 | top = phdr->p_vaddr + phdr->p_memsz; | ||
865 | } | ||
866 | |||
867 | /* allocate one big anon block for everything */ | ||
868 | mflags = MAP_PRIVATE; | ||
869 | if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE) | ||
870 | mflags |= MAP_EXECUTABLE; | ||
871 | |||
872 | down_write(&mm->mmap_sem); | ||
873 | maddr = do_mmap(NULL, load_addr, top - base, | ||
874 | PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0); | ||
875 | up_write(&mm->mmap_sem); | ||
876 | if (IS_ERR((void *) maddr)) | ||
877 | return (int) maddr; | ||
878 | |||
879 | if (load_addr != 0) | ||
880 | load_addr += PAGE_ALIGN(top - base); | ||
881 | |||
882 | /* and then load the file segments into it */ | ||
883 | phdr = params->phdrs; | ||
884 | for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) { | ||
885 | if (params->phdrs[loop].p_type != PT_LOAD) | ||
886 | continue; | ||
887 | |||
888 | fpos = phdr->p_offset; | ||
889 | |||
890 | seg->addr = maddr + (phdr->p_vaddr - base); | ||
891 | seg->p_vaddr = phdr->p_vaddr; | ||
892 | seg->p_memsz = phdr->p_memsz; | ||
893 | |||
894 | ret = file->f_op->read(file, (void *) seg->addr, phdr->p_filesz, &fpos); | ||
895 | if (ret < 0) | ||
896 | return ret; | ||
897 | |||
898 | /* map the ELF header address if in this segment */ | ||
899 | if (phdr->p_offset == 0) | ||
900 | params->elfhdr_addr = seg->addr; | ||
901 | |||
902 | /* clear any space allocated but not loaded */ | ||
903 | if (phdr->p_filesz < phdr->p_memsz) | ||
904 | clear_user((void *) (seg->addr + phdr->p_filesz), | ||
905 | phdr->p_memsz - phdr->p_filesz); | ||
906 | |||
907 | if (mm) { | ||
908 | if (phdr->p_flags & PF_X) { | ||
909 | mm->start_code = seg->addr; | ||
910 | mm->end_code = seg->addr + phdr->p_memsz; | ||
911 | } | ||
912 | else if (!mm->start_data) { | ||
913 | mm->start_data = seg->addr; | ||
914 | #ifndef CONFIG_MMU | ||
915 | mm->end_data = seg->addr + phdr->p_memsz; | ||
916 | #endif | ||
917 | } | ||
918 | |||
919 | #ifdef CONFIG_MMU | ||
920 | if (seg->addr + phdr->p_memsz > mm->end_data) | ||
921 | mm->end_data = seg->addr + phdr->p_memsz; | ||
922 | #endif | ||
923 | } | ||
924 | |||
925 | seg++; | ||
926 | } | ||
927 | |||
928 | return 0; | ||
929 | } /* end elf_fdpic_map_file_constdisp_on_uclinux() */ | ||
930 | #endif | ||
931 | |||
932 | /*****************************************************************************/ | ||
933 | /* | ||
934 | * map a binary by direct mmap() of the individual PT_LOAD segments | ||
935 | */ | ||
936 | static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params, | ||
937 | struct file *file, | ||
938 | struct mm_struct *mm) | ||
939 | { | ||
940 | struct elf32_fdpic_loadseg *seg; | ||
941 | struct elf32_phdr *phdr; | ||
942 | unsigned long load_addr, delta_vaddr; | ||
943 | int loop, dvset; | ||
944 | |||
945 | load_addr = params->load_addr; | ||
946 | delta_vaddr = 0; | ||
947 | dvset = 0; | ||
948 | |||
949 | seg = params->loadmap->segs; | ||
950 | |||
951 | /* deal with each load segment separately */ | ||
952 | phdr = params->phdrs; | ||
953 | for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) { | ||
954 | unsigned long maddr, disp, excess, excess1; | ||
955 | int prot = 0, flags; | ||
956 | |||
957 | if (phdr->p_type != PT_LOAD) | ||
958 | continue; | ||
959 | |||
960 | kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx", | ||
961 | (unsigned long) phdr->p_vaddr, | ||
962 | (unsigned long) phdr->p_offset, | ||
963 | (unsigned long) phdr->p_filesz, | ||
964 | (unsigned long) phdr->p_memsz); | ||
965 | |||
966 | /* determine the mapping parameters */ | ||
967 | if (phdr->p_flags & PF_R) prot |= PROT_READ; | ||
968 | if (phdr->p_flags & PF_W) prot |= PROT_WRITE; | ||
969 | if (phdr->p_flags & PF_X) prot |= PROT_EXEC; | ||
970 | |||
971 | flags = MAP_PRIVATE | MAP_DENYWRITE; | ||
972 | if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE) | ||
973 | flags |= MAP_EXECUTABLE; | ||
974 | |||
975 | maddr = 0; | ||
976 | |||
977 | switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) { | ||
978 | case ELF_FDPIC_FLAG_INDEPENDENT: | ||
979 | /* PT_LOADs are independently locatable */ | ||
980 | break; | ||
981 | |||
982 | case ELF_FDPIC_FLAG_HONOURVADDR: | ||
983 | /* the specified virtual address must be honoured */ | ||
984 | maddr = phdr->p_vaddr; | ||
985 | flags |= MAP_FIXED; | ||
986 | break; | ||
987 | |||
988 | case ELF_FDPIC_FLAG_CONSTDISP: | ||
989 | /* constant displacement | ||
990 | * - can be mapped anywhere, but must be mapped as a unit | ||
991 | */ | ||
992 | if (!dvset) { | ||
993 | maddr = load_addr; | ||
994 | delta_vaddr = phdr->p_vaddr; | ||
995 | dvset = 1; | ||
996 | } | ||
997 | else { | ||
998 | maddr = load_addr + phdr->p_vaddr - delta_vaddr; | ||
999 | flags |= MAP_FIXED; | ||
1000 | } | ||
1001 | break; | ||
1002 | |||
1003 | case ELF_FDPIC_FLAG_CONTIGUOUS: | ||
1004 | /* contiguity handled later */ | ||
1005 | break; | ||
1006 | |||
1007 | default: | ||
1008 | BUG(); | ||
1009 | } | ||
1010 | |||
1011 | maddr &= PAGE_MASK; | ||
1012 | |||
1013 | /* create the mapping */ | ||
1014 | disp = phdr->p_vaddr & ~PAGE_MASK; | ||
1015 | down_write(&mm->mmap_sem); | ||
1016 | maddr = do_mmap(file, maddr, phdr->p_memsz + disp, prot, flags, | ||
1017 | phdr->p_offset - disp); | ||
1018 | up_write(&mm->mmap_sem); | ||
1019 | |||
1020 | kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx", | ||
1021 | loop, phdr->p_memsz + disp, prot, flags, phdr->p_offset - disp, | ||
1022 | maddr); | ||
1023 | |||
1024 | if (IS_ERR((void *) maddr)) | ||
1025 | return (int) maddr; | ||
1026 | |||
1027 | if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) == ELF_FDPIC_FLAG_CONTIGUOUS) | ||
1028 | load_addr += PAGE_ALIGN(phdr->p_memsz + disp); | ||
1029 | |||
1030 | seg->addr = maddr + disp; | ||
1031 | seg->p_vaddr = phdr->p_vaddr; | ||
1032 | seg->p_memsz = phdr->p_memsz; | ||
1033 | |||
1034 | /* map the ELF header address if in this segment */ | ||
1035 | if (phdr->p_offset == 0) | ||
1036 | params->elfhdr_addr = seg->addr; | ||
1037 | |||
1038 | /* clear the bit between beginning of mapping and beginning of PT_LOAD */ | ||
1039 | if (prot & PROT_WRITE && disp > 0) { | ||
1040 | kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp); | ||
1041 | clear_user((void *) maddr, disp); | ||
1042 | maddr += disp; | ||
1043 | } | ||
1044 | |||
1045 | /* clear any space allocated but not loaded | ||
1046 | * - on uClinux we can just clear the lot | ||
1047 | * - on MMU linux we'll get a SIGBUS beyond the last page | ||
1048 | * extant in the file | ||
1049 | */ | ||
1050 | excess = phdr->p_memsz - phdr->p_filesz; | ||
1051 | excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK); | ||
1052 | |||
1053 | #ifdef CONFIG_MMU | ||
1054 | |||
1055 | if (excess > excess1) { | ||
1056 | unsigned long xaddr = maddr + phdr->p_filesz + excess1; | ||
1057 | unsigned long xmaddr; | ||
1058 | |||
1059 | flags |= MAP_FIXED | MAP_ANONYMOUS; | ||
1060 | down_write(&mm->mmap_sem); | ||
1061 | xmaddr = do_mmap(NULL, xaddr, excess - excess1, prot, flags, 0); | ||
1062 | up_write(&mm->mmap_sem); | ||
1063 | |||
1064 | kdebug("mmap[%d] <anon>" | ||
1065 | " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx", | ||
1066 | loop, xaddr, excess - excess1, prot, flags, xmaddr); | ||
1067 | |||
1068 | if (xmaddr != xaddr) | ||
1069 | return -ENOMEM; | ||
1070 | } | ||
1071 | |||
1072 | if (prot & PROT_WRITE && excess1 > 0) { | ||
1073 | kdebug("clear[%d] ad=%lx sz=%lx", | ||
1074 | loop, maddr + phdr->p_filesz, excess1); | ||
1075 | clear_user((void *) maddr + phdr->p_filesz, excess1); | ||
1076 | } | ||
1077 | |||
1078 | #else | ||
1079 | if (excess > 0) { | ||
1080 | kdebug("clear[%d] ad=%lx sz=%lx", | ||
1081 | loop, maddr + phdr->p_filesz, excess); | ||
1082 | clear_user((void *) maddr + phdr->p_filesz, excess); | ||
1083 | } | ||
1084 | #endif | ||
1085 | |||
1086 | if (mm) { | ||
1087 | if (phdr->p_flags & PF_X) { | ||
1088 | mm->start_code = maddr; | ||
1089 | mm->end_code = maddr + phdr->p_memsz; | ||
1090 | } | ||
1091 | else if (!mm->start_data) { | ||
1092 | mm->start_data = maddr; | ||
1093 | mm->end_data = maddr + phdr->p_memsz; | ||
1094 | } | ||
1095 | } | ||
1096 | |||
1097 | seg++; | ||
1098 | } | ||
1099 | |||
1100 | return 0; | ||
1101 | } /* end elf_fdpic_map_file_by_direct_mmap() */ | ||