blob: 68e20d5bfe1b63b415f2e48c315c1636f45bb9ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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#include <linux/kernel.h>
14#include <linux/fs.h>
15#include <linux/stat.h>
16#include <linux/time.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.h>
34#include <linux/smp_lock.h>
35#include <linux/compiler.h>
36#include <linux/highmem.h>
37#include <linux/pagemap.h>
38#include <linux/security.h>
39#include <linux/syscalls.h>
40#include <linux/random.h>
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070041#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
43#include <asm/param.h>
44#include <asm/page.h>
45
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070046static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs);
47static int load_elf_library(struct file *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#ifndef elf_addr_t
51#define elf_addr_t unsigned long
52#endif
53
54/*
55 * If we don't support core dumping, then supply a NULL so we
56 * don't even try.
57 */
Matt Mackall708e9a72006-01-08 01:05:25 -080058#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070059static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#else
61#define elf_core_dump NULL
62#endif
63
64#if ELF_EXEC_PAGESIZE > PAGE_SIZE
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070065#define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#else
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070067#define ELF_MIN_ALIGN PAGE_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#endif
69
70#ifndef ELF_CORE_EFLAGS
71#define ELF_CORE_EFLAGS 0
72#endif
73
74#define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
75#define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
76#define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
77
78static struct linux_binfmt elf_format = {
79 .module = THIS_MODULE,
80 .load_binary = load_elf_binary,
81 .load_shlib = load_elf_library,
82 .core_dump = elf_core_dump,
83 .min_coredump = ELF_EXEC_PAGESIZE
84};
85
Chuck Ebbertce510592006-07-03 00:24:14 -070086#define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static int set_brk(unsigned long start, unsigned long end)
89{
90 start = ELF_PAGEALIGN(start);
91 end = ELF_PAGEALIGN(end);
92 if (end > start) {
93 unsigned long addr;
94 down_write(&current->mm->mmap_sem);
95 addr = do_brk(start, end - start);
96 up_write(&current->mm->mmap_sem);
97 if (BAD_ADDR(addr))
98 return addr;
99 }
100 current->mm->start_brk = current->mm->brk = end;
101 return 0;
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* We need to explicitly zero any fractional pages
105 after the data section (i.e. bss). This would
106 contain the junk from the file that should not
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700107 be in memory
108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int padzero(unsigned long elf_bss)
110{
111 unsigned long nbyte;
112
113 nbyte = ELF_PAGEOFFSET(elf_bss);
114 if (nbyte) {
115 nbyte = ELF_MIN_ALIGN - nbyte;
116 if (clear_user((void __user *) elf_bss, nbyte))
117 return -EFAULT;
118 }
119 return 0;
120}
121
122/* Let's use some macros to make this stack manipulation a litle clearer */
123#ifdef CONFIG_STACK_GROWSUP
124#define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
125#define STACK_ROUND(sp, items) \
126 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700127#define STACK_ALLOC(sp, len) ({ \
128 elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
129 old_sp; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#else
131#define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
132#define STACK_ROUND(sp, items) \
133 (((unsigned long) (sp - items)) &~ 15UL)
134#define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
135#endif
136
137static int
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700138create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int interp_aout, unsigned long load_addr,
140 unsigned long interp_load_addr)
141{
142 unsigned long p = bprm->p;
143 int argc = bprm->argc;
144 int envc = bprm->envc;
145 elf_addr_t __user *argv;
146 elf_addr_t __user *envp;
147 elf_addr_t __user *sp;
148 elf_addr_t __user *u_platform;
149 const char *k_platform = ELF_PLATFORM;
150 int items;
151 elf_addr_t *elf_info;
152 int ei_index = 0;
153 struct task_struct *tsk = current;
154
155 /*
156 * If this architecture has a platform capability string, copy it
157 * to userspace. In some cases (Sparc), this info is impossible
158 * for userspace to get any other way, in others (i386) it is
159 * merely difficult.
160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 u_platform = NULL;
162 if (k_platform) {
163 size_t len = strlen(k_platform) + 1;
164
165 /*
166 * In some cases (e.g. Hyper-Threading), we want to avoid L1
167 * evictions by the processes running on the same package. One
168 * thing we can do is to shuffle the initial stack for them.
169 */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 p = arch_align_stack(p);
172
173 u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
174 if (__copy_to_user(u_platform, k_platform, len))
175 return -EFAULT;
176 }
177
178 /* Create the ELF interpreter info */
Jesper Juhl785d5572006-06-23 02:05:35 -0700179 elf_info = (elf_addr_t *)current->mm->saved_auxv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#define NEW_AUX_ENT(id, val) \
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700181 do { \
Jesper Juhl785d5572006-06-23 02:05:35 -0700182 elf_info[ei_index++] = id; \
183 elf_info[ei_index++] = val; \
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700184 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186#ifdef ARCH_DLINFO
187 /*
188 * ARCH_DLINFO must come first so PPC can do its special alignment of
189 * AUXV.
190 */
191 ARCH_DLINFO;
192#endif
193 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
194 NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
195 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
196 NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700197 NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
199 NEW_AUX_ENT(AT_BASE, interp_load_addr);
200 NEW_AUX_ENT(AT_FLAGS, 0);
201 NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
Jesper Juhl785d5572006-06-23 02:05:35 -0700202 NEW_AUX_ENT(AT_UID, tsk->uid);
203 NEW_AUX_ENT(AT_EUID, tsk->euid);
204 NEW_AUX_ENT(AT_GID, tsk->gid);
205 NEW_AUX_ENT(AT_EGID, tsk->egid);
206 NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (k_platform) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700208 NEW_AUX_ENT(AT_PLATFORM,
Jesper Juhl785d5572006-06-23 02:05:35 -0700209 (elf_addr_t)(unsigned long)u_platform);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
Jesper Juhl785d5572006-06-23 02:05:35 -0700212 NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214#undef NEW_AUX_ENT
215 /* AT_NULL is zero; clear the rest too */
216 memset(&elf_info[ei_index], 0,
217 sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
218
219 /* And advance past the AT_NULL entry. */
220 ei_index += 2;
221
222 sp = STACK_ADD(p, ei_index);
223
224 items = (argc + 1) + (envc + 1);
225 if (interp_aout) {
226 items += 3; /* a.out interpreters require argv & envp too */
227 } else {
228 items += 1; /* ELF interpreters only put argc on the stack */
229 }
230 bprm->p = STACK_ROUND(sp, items);
231
232 /* Point sp at the lowest address on the stack */
233#ifdef CONFIG_STACK_GROWSUP
234 sp = (elf_addr_t __user *)bprm->p - items - ei_index;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700235 bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#else
237 sp = (elf_addr_t __user *)bprm->p;
238#endif
239
240 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
241 if (__put_user(argc, sp++))
242 return -EFAULT;
243 if (interp_aout) {
244 argv = sp + 2;
245 envp = argv + argc + 1;
Heiko Carstens841d5fb2006-12-06 20:36:35 -0800246 if (__put_user((elf_addr_t)(unsigned long)argv, sp++) ||
247 __put_user((elf_addr_t)(unsigned long)envp, sp++))
248 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 } else {
250 argv = sp;
251 envp = argv + argc + 1;
252 }
253
254 /* Populate argv and envp */
Greg Kroah-Hartmana84a5052005-05-11 00:10:44 -0700255 p = current->mm->arg_end = current->mm->arg_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 while (argc-- > 0) {
257 size_t len;
Heiko Carstens841d5fb2006-12-06 20:36:35 -0800258 if (__put_user((elf_addr_t)p, argv++))
259 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
261 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
262 return 0;
263 p += len;
264 }
265 if (__put_user(0, argv))
266 return -EFAULT;
267 current->mm->arg_end = current->mm->env_start = p;
268 while (envc-- > 0) {
269 size_t len;
Heiko Carstens841d5fb2006-12-06 20:36:35 -0800270 if (__put_user((elf_addr_t)p, envp++))
271 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
273 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
274 return 0;
275 p += len;
276 }
277 if (__put_user(0, envp))
278 return -EFAULT;
279 current->mm->env_end = p;
280
281 /* Put the elf_info on the stack in the right place. */
282 sp = (elf_addr_t __user *)envp + 1;
283 if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t)))
284 return -EFAULT;
285 return 0;
286}
287
288#ifndef elf_map
289
290static unsigned long elf_map(struct file *filep, unsigned long addr,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700291 struct elf_phdr *eppnt, int prot, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 unsigned long map_addr;
David Gibsondda6ebd2006-01-08 01:03:35 -0800294 unsigned long pageoffset = ELF_PAGEOFFSET(eppnt->p_vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 down_write(&current->mm->mmap_sem);
David Gibsondda6ebd2006-01-08 01:03:35 -0800297 /* mmap() will return -EINVAL if given a zero size, but a
298 * segment with zero filesize is perfectly valid */
299 if (eppnt->p_filesz + pageoffset)
300 map_addr = do_mmap(filep, ELF_PAGESTART(addr),
301 eppnt->p_filesz + pageoffset, prot, type,
302 eppnt->p_offset - pageoffset);
303 else
304 map_addr = ELF_PAGESTART(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 up_write(&current->mm->mmap_sem);
306 return(map_addr);
307}
308
309#endif /* !elf_map */
310
311/* This is much more generalized than the library routine read function,
312 so we keep this separate. Technically the library read function
313 is only provided so that we can read a.out libraries that have
314 an ELF header */
315
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700316static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
317 struct file *interpreter, unsigned long *interp_load_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct elf_phdr *elf_phdata;
320 struct elf_phdr *eppnt;
321 unsigned long load_addr = 0;
322 int load_addr_set = 0;
323 unsigned long last_bss = 0, elf_bss = 0;
324 unsigned long error = ~0UL;
325 int retval, i, size;
326
327 /* First of all, some simple consistency checks */
328 if (interp_elf_ex->e_type != ET_EXEC &&
329 interp_elf_ex->e_type != ET_DYN)
330 goto out;
331 if (!elf_check_arch(interp_elf_ex))
332 goto out;
333 if (!interpreter->f_op || !interpreter->f_op->mmap)
334 goto out;
335
336 /*
337 * If the size of this structure has changed, then punt, since
338 * we will be doing the wrong thing.
339 */
340 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
341 goto out;
342 if (interp_elf_ex->e_phnum < 1 ||
343 interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
344 goto out;
345
346 /* Now read in all of the header information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
348 if (size > ELF_MIN_ALIGN)
349 goto out;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700350 elf_phdata = kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!elf_phdata)
352 goto out;
353
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700354 retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
355 (char *)elf_phdata,size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 error = -EIO;
357 if (retval != size) {
358 if (retval < 0)
359 error = retval;
360 goto out_close;
361 }
362
363 eppnt = elf_phdata;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700364 for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
365 if (eppnt->p_type == PT_LOAD) {
366 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
367 int elf_prot = 0;
368 unsigned long vaddr = 0;
369 unsigned long k, map_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700371 if (eppnt->p_flags & PF_R)
372 elf_prot = PROT_READ;
373 if (eppnt->p_flags & PF_W)
374 elf_prot |= PROT_WRITE;
375 if (eppnt->p_flags & PF_X)
376 elf_prot |= PROT_EXEC;
377 vaddr = eppnt->p_vaddr;
378 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
379 elf_type |= MAP_FIXED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700381 map_addr = elf_map(interpreter, load_addr + vaddr,
382 eppnt, elf_prot, elf_type);
383 error = map_addr;
384 if (BAD_ADDR(map_addr))
385 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700387 if (!load_addr_set &&
388 interp_elf_ex->e_type == ET_DYN) {
389 load_addr = map_addr - ELF_PAGESTART(vaddr);
390 load_addr_set = 1;
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700393 /*
394 * Check to see if the section's size will overflow the
395 * allowed task size. Note that p_filesz must always be
396 * <= p_memsize so it's only necessary to check p_memsz.
397 */
398 k = load_addr + eppnt->p_vaddr;
Chuck Ebbertce510592006-07-03 00:24:14 -0700399 if (BAD_ADDR(k) ||
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700400 eppnt->p_filesz > eppnt->p_memsz ||
401 eppnt->p_memsz > TASK_SIZE ||
402 TASK_SIZE - eppnt->p_memsz < k) {
403 error = -ENOMEM;
404 goto out_close;
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700407 /*
408 * Find the end of the file mapping for this phdr, and
409 * keep track of the largest address we see for this.
410 */
411 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
412 if (k > elf_bss)
413 elf_bss = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700415 /*
416 * Do the same thing for the memory mapping - between
417 * elf_bss and last_bss is the bss section.
418 */
419 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
420 if (k > last_bss)
421 last_bss = k;
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
425 /*
426 * Now fill out the bss section. First pad the last page up
427 * to the page boundary, and then perform a mmap to make sure
428 * that there are zero-mapped pages up to and including the
429 * last bss page.
430 */
431 if (padzero(elf_bss)) {
432 error = -EFAULT;
433 goto out_close;
434 }
435
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700436 /* What we have mapped so far */
437 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* Map the last of the bss segment */
440 if (last_bss > elf_bss) {
441 down_write(&current->mm->mmap_sem);
442 error = do_brk(elf_bss, last_bss - elf_bss);
443 up_write(&current->mm->mmap_sem);
444 if (BAD_ADDR(error))
445 goto out_close;
446 }
447
448 *interp_load_addr = load_addr;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700449 error = ((unsigned long)interp_elf_ex->e_entry) + load_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451out_close:
452 kfree(elf_phdata);
453out:
454 return error;
455}
456
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700457static unsigned long load_aout_interp(struct exec *interp_ex,
458 struct file *interpreter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 unsigned long text_data, elf_entry = ~0UL;
461 char __user * addr;
462 loff_t offset;
463
464 current->mm->end_code = interp_ex->a_text;
465 text_data = interp_ex->a_text + interp_ex->a_data;
466 current->mm->end_data = text_data;
467 current->mm->brk = interp_ex->a_bss + text_data;
468
469 switch (N_MAGIC(*interp_ex)) {
470 case OMAGIC:
471 offset = 32;
472 addr = (char __user *)0;
473 break;
474 case ZMAGIC:
475 case QMAGIC:
476 offset = N_TXTOFF(*interp_ex);
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700477 addr = (char __user *)N_TXTADDR(*interp_ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
479 default:
480 goto out;
481 }
482
483 down_write(&current->mm->mmap_sem);
484 do_brk(0, text_data);
485 up_write(&current->mm->mmap_sem);
486 if (!interpreter->f_op || !interpreter->f_op->read)
487 goto out;
488 if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
489 goto out;
490 flush_icache_range((unsigned long)addr,
491 (unsigned long)addr + text_data);
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 down_write(&current->mm->mmap_sem);
494 do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
495 interp_ex->a_bss);
496 up_write(&current->mm->mmap_sem);
497 elf_entry = interp_ex->a_entry;
498
499out:
500 return elf_entry;
501}
502
503/*
504 * These are the functions used to load ELF style executables and shared
505 * libraries. There is no binary dependent code anywhere else.
506 */
507
508#define INTERPRETER_NONE 0
509#define INTERPRETER_AOUT 1
510#define INTERPRETER_ELF 2
511
Andi Kleen913bd902006-03-25 16:29:09 +0100512#ifndef STACK_RND_MASK
513#define STACK_RND_MASK 0x7ff /* with 4K pages 8MB of VA */
514#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516static unsigned long randomize_stack_top(unsigned long stack_top)
517{
518 unsigned int random_variable = 0;
519
Andi Kleenc16b63e02006-09-26 10:52:28 +0200520 if ((current->flags & PF_RANDOMIZE) &&
521 !(current->personality & ADDR_NO_RANDOMIZE)) {
Andi Kleen913bd902006-03-25 16:29:09 +0100522 random_variable = get_random_int() & STACK_RND_MASK;
523 random_variable <<= PAGE_SHIFT;
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#ifdef CONFIG_STACK_GROWSUP
Andi Kleen913bd902006-03-25 16:29:09 +0100526 return PAGE_ALIGN(stack_top) + random_variable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#else
Andi Kleen913bd902006-03-25 16:29:09 +0100528 return PAGE_ALIGN(stack_top) - random_variable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529#endif
530}
531
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700532static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct file *interpreter = NULL; /* to shut gcc up */
535 unsigned long load_addr = 0, load_bias = 0;
536 int load_addr_set = 0;
537 char * elf_interpreter = NULL;
538 unsigned int interpreter_type = INTERPRETER_NONE;
539 unsigned char ibcs2_interpreter = 0;
540 unsigned long error;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700541 struct elf_phdr *elf_ppnt, *elf_phdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 unsigned long elf_bss, elf_brk;
543 int elf_exec_fileno;
544 int retval, i;
545 unsigned int size;
546 unsigned long elf_entry, interp_load_addr = 0;
547 unsigned long start_code, end_code, start_data, end_data;
548 unsigned long reloc_func_desc = 0;
549 char passed_fileno[6];
550 struct files_struct *files;
551 int have_pt_gnu_stack, executable_stack = EXSTACK_DEFAULT;
552 unsigned long def_flags = 0;
553 struct {
554 struct elfhdr elf_ex;
555 struct elfhdr interp_elf_ex;
556 struct exec interp_ex;
557 } *loc;
558
559 loc = kmalloc(sizeof(*loc), GFP_KERNEL);
560 if (!loc) {
561 retval = -ENOMEM;
562 goto out_ret;
563 }
564
565 /* Get the exec-header */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700566 loc->elf_ex = *((struct elfhdr *)bprm->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 retval = -ENOEXEC;
569 /* First of all, some simple consistency checks */
570 if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
571 goto out;
572
573 if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
574 goto out;
575 if (!elf_check_arch(&loc->elf_ex))
576 goto out;
577 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
578 goto out;
579
580 /* Now read in all of the header information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
582 goto out;
583 if (loc->elf_ex.e_phnum < 1 ||
584 loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
585 goto out;
586 size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
587 retval = -ENOMEM;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700588 elf_phdata = kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!elf_phdata)
590 goto out;
591
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700592 retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
593 (char *)elf_phdata, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (retval != size) {
595 if (retval >= 0)
596 retval = -EIO;
597 goto out_free_ph;
598 }
599
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700600 files = current->files; /* Refcounted so ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 retval = unshare_files();
602 if (retval < 0)
603 goto out_free_ph;
604 if (files == current->files) {
605 put_files_struct(files);
606 files = NULL;
607 }
608
609 /* exec will make our files private anyway, but for the a.out
610 loader stuff we need to do it earlier */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 retval = get_unused_fd();
612 if (retval < 0)
613 goto out_free_fh;
614 get_file(bprm->file);
615 fd_install(elf_exec_fileno = retval, bprm->file);
616
617 elf_ppnt = elf_phdata;
618 elf_bss = 0;
619 elf_brk = 0;
620
621 start_code = ~0UL;
622 end_code = 0;
623 start_data = 0;
624 end_data = 0;
625
626 for (i = 0; i < loc->elf_ex.e_phnum; i++) {
627 if (elf_ppnt->p_type == PT_INTERP) {
628 /* This is the program interpreter used for
629 * shared libraries - for now assume that this
630 * is an a.out format binary
631 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 retval = -ENOEXEC;
633 if (elf_ppnt->p_filesz > PATH_MAX ||
634 elf_ppnt->p_filesz < 2)
635 goto out_free_file;
636
637 retval = -ENOMEM;
Jesper Juhl792db3a2006-01-09 20:54:45 -0800638 elf_interpreter = kmalloc(elf_ppnt->p_filesz,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700639 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!elf_interpreter)
641 goto out_free_file;
642
643 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700644 elf_interpreter,
645 elf_ppnt->p_filesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (retval != elf_ppnt->p_filesz) {
647 if (retval >= 0)
648 retval = -EIO;
649 goto out_free_interp;
650 }
651 /* make sure path is NULL terminated */
652 retval = -ENOEXEC;
653 if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
654 goto out_free_interp;
655
656 /* If the program interpreter is one of these two,
657 * then assume an iBCS2 image. Otherwise assume
658 * a native linux image.
659 */
660 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
661 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
662 ibcs2_interpreter = 1;
663
664 /*
665 * The early SET_PERSONALITY here is so that the lookup
666 * for the interpreter happens in the namespace of the
667 * to-be-execed image. SET_PERSONALITY can select an
668 * alternate root.
669 *
670 * However, SET_PERSONALITY is NOT allowed to switch
671 * this task into the new images's memory mapping
672 * policy - that is, TASK_SIZE must still evaluate to
673 * that which is appropriate to the execing application.
674 * This is because exit_mmap() needs to have TASK_SIZE
675 * evaluate to the size of the old image.
676 *
677 * So if (say) a 64-bit application is execing a 32-bit
678 * application it is the architecture's responsibility
679 * to defer changing the value of TASK_SIZE until the
680 * switch really is going to happen - do this in
681 * flush_thread(). - akpm
682 */
683 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
684
685 interpreter = open_exec(elf_interpreter);
686 retval = PTR_ERR(interpreter);
687 if (IS_ERR(interpreter))
688 goto out_free_interp;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700689 retval = kernel_read(interpreter, 0, bprm->buf,
690 BINPRM_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (retval != BINPRM_BUF_SIZE) {
692 if (retval >= 0)
693 retval = -EIO;
694 goto out_free_dentry;
695 }
696
697 /* Get the exec headers */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700698 loc->interp_ex = *((struct exec *)bprm->buf);
699 loc->interp_elf_ex = *((struct elfhdr *)bprm->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 break;
701 }
702 elf_ppnt++;
703 }
704
705 elf_ppnt = elf_phdata;
706 for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
707 if (elf_ppnt->p_type == PT_GNU_STACK) {
708 if (elf_ppnt->p_flags & PF_X)
709 executable_stack = EXSTACK_ENABLE_X;
710 else
711 executable_stack = EXSTACK_DISABLE_X;
712 break;
713 }
714 have_pt_gnu_stack = (i < loc->elf_ex.e_phnum);
715
716 /* Some simple consistency checks for the interpreter */
717 if (elf_interpreter) {
718 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
719
720 /* Now figure out which format our binary is */
721 if ((N_MAGIC(loc->interp_ex) != OMAGIC) &&
722 (N_MAGIC(loc->interp_ex) != ZMAGIC) &&
723 (N_MAGIC(loc->interp_ex) != QMAGIC))
724 interpreter_type = INTERPRETER_ELF;
725
726 if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
727 interpreter_type &= ~INTERPRETER_ELF;
728
729 retval = -ELIBBAD;
730 if (!interpreter_type)
731 goto out_free_dentry;
732
733 /* Make sure only one type was selected */
734 if ((interpreter_type & INTERPRETER_ELF) &&
735 interpreter_type != INTERPRETER_ELF) {
736 // FIXME - ratelimit this before re-enabling
737 // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
738 interpreter_type = INTERPRETER_ELF;
739 }
740 /* Verify the interpreter has a valid arch */
741 if ((interpreter_type == INTERPRETER_ELF) &&
742 !elf_check_arch(&loc->interp_elf_ex))
743 goto out_free_dentry;
744 } else {
745 /* Executables without an interpreter also need a personality */
746 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
747 }
748
749 /* OK, we are done with that, now set up the arg stuff,
750 and then start this sucker up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
752 char *passed_p = passed_fileno;
753 sprintf(passed_fileno, "%d", elf_exec_fileno);
754
755 if (elf_interpreter) {
756 retval = copy_strings_kernel(1, &passed_p, bprm);
757 if (retval)
758 goto out_free_dentry;
759 bprm->argc++;
760 }
761 }
762
763 /* Flush all traces of the currently running executable */
764 retval = flush_old_exec(bprm);
765 if (retval)
766 goto out_free_dentry;
767
768 /* Discard our unneeded old files struct */
769 if (files) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 put_files_struct(files);
771 files = NULL;
772 }
773
774 /* OK, This is the point of no return */
775 current->mm->start_data = 0;
776 current->mm->end_data = 0;
777 current->mm->end_code = 0;
778 current->mm->mmap = NULL;
779 current->flags &= ~PF_FORKNOEXEC;
780 current->mm->def_flags = def_flags;
781
782 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
783 may depend on the personality. */
784 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
785 if (elf_read_implies_exec(loc->elf_ex, executable_stack))
786 current->personality |= READ_IMPLIES_EXEC;
787
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700788 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 current->flags |= PF_RANDOMIZE;
790 arch_pick_mmap_layout(current->mm);
791
792 /* Do this so that we can load the interpreter, if need be. We will
793 change some of these later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 current->mm->free_area_cache = current->mm->mmap_base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700795 current->mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
797 executable_stack);
798 if (retval < 0) {
799 send_sig(SIGKILL, current, 0);
800 goto out_free_dentry;
801 }
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 current->mm->start_stack = bprm->p;
804
805 /* Now we do a little grungy work by mmaping the ELF image into
806 the correct location in memory. At this point, we assume that
807 the image should be loaded at fixed address, not at a variable
808 address. */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700809 for(i = 0, elf_ppnt = elf_phdata;
810 i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int elf_prot = 0, elf_flags;
812 unsigned long k, vaddr;
813
814 if (elf_ppnt->p_type != PT_LOAD)
815 continue;
816
817 if (unlikely (elf_brk > elf_bss)) {
818 unsigned long nbyte;
819
820 /* There was a PT_LOAD segment with p_memsz > p_filesz
821 before this one. Map anonymous pages, if needed,
822 and clear the area. */
823 retval = set_brk (elf_bss + load_bias,
824 elf_brk + load_bias);
825 if (retval) {
826 send_sig(SIGKILL, current, 0);
827 goto out_free_dentry;
828 }
829 nbyte = ELF_PAGEOFFSET(elf_bss);
830 if (nbyte) {
831 nbyte = ELF_MIN_ALIGN - nbyte;
832 if (nbyte > elf_brk - elf_bss)
833 nbyte = elf_brk - elf_bss;
834 if (clear_user((void __user *)elf_bss +
835 load_bias, nbyte)) {
836 /*
837 * This bss-zeroing can fail if the ELF
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700838 * file specifies odd protections. So
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 * we don't check the return value
840 */
841 }
842 }
843 }
844
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700845 if (elf_ppnt->p_flags & PF_R)
846 elf_prot |= PROT_READ;
847 if (elf_ppnt->p_flags & PF_W)
848 elf_prot |= PROT_WRITE;
849 if (elf_ppnt->p_flags & PF_X)
850 elf_prot |= PROT_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700852 elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 vaddr = elf_ppnt->p_vaddr;
855 if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
856 elf_flags |= MAP_FIXED;
857 } else if (loc->elf_ex.e_type == ET_DYN) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700858 /* Try and get dynamic programs out of the way of the
859 * default mmap base, as well as whatever program they
860 * might try to exec. This is because the brk will
861 * follow the loader, and is not movable. */
Marcus Meissner59287c02006-12-06 20:36:24 -0800862 if (current->flags & PF_RANDOMIZE)
863 load_bias = randomize_range(0x10000,
864 ELF_ET_DYN_BASE,
865 0);
866 else
867 load_bias = ELF_ET_DYN_BASE;
868 load_bias = ELF_PAGESTART(load_bias - vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700871 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
872 elf_prot, elf_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (BAD_ADDR(error)) {
874 send_sig(SIGKILL, current, 0);
875 goto out_free_dentry;
876 }
877
878 if (!load_addr_set) {
879 load_addr_set = 1;
880 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
881 if (loc->elf_ex.e_type == ET_DYN) {
882 load_bias += error -
883 ELF_PAGESTART(load_bias + vaddr);
884 load_addr += load_bias;
885 reloc_func_desc = load_bias;
886 }
887 }
888 k = elf_ppnt->p_vaddr;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700889 if (k < start_code)
890 start_code = k;
891 if (start_data < k)
892 start_data = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /*
895 * Check to see if the section's size will overflow the
896 * allowed task size. Note that p_filesz must always be
897 * <= p_memsz so it is only necessary to check p_memsz.
898 */
Chuck Ebbertce510592006-07-03 00:24:14 -0700899 if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 elf_ppnt->p_memsz > TASK_SIZE ||
901 TASK_SIZE - elf_ppnt->p_memsz < k) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700902 /* set_brk can never work. Avoid overflows. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 send_sig(SIGKILL, current, 0);
904 goto out_free_dentry;
905 }
906
907 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
908
909 if (k > elf_bss)
910 elf_bss = k;
911 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
912 end_code = k;
913 if (end_data < k)
914 end_data = k;
915 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
916 if (k > elf_brk)
917 elf_brk = k;
918 }
919
920 loc->elf_ex.e_entry += load_bias;
921 elf_bss += load_bias;
922 elf_brk += load_bias;
923 start_code += load_bias;
924 end_code += load_bias;
925 start_data += load_bias;
926 end_data += load_bias;
927
928 /* Calling set_brk effectively mmaps the pages that we need
929 * for the bss and break sections. We must do this before
930 * mapping in the interpreter, to make sure it doesn't wind
931 * up getting placed where the bss needs to go.
932 */
933 retval = set_brk(elf_bss, elf_brk);
934 if (retval) {
935 send_sig(SIGKILL, current, 0);
936 goto out_free_dentry;
937 }
akpm@osdl.org6de50512005-10-11 08:29:08 -0700938 if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 send_sig(SIGSEGV, current, 0);
940 retval = -EFAULT; /* Nobody gets to see this, but.. */
941 goto out_free_dentry;
942 }
943
944 if (elf_interpreter) {
945 if (interpreter_type == INTERPRETER_AOUT)
946 elf_entry = load_aout_interp(&loc->interp_ex,
947 interpreter);
948 else
949 elf_entry = load_elf_interp(&loc->interp_elf_ex,
950 interpreter,
951 &interp_load_addr);
952 if (BAD_ADDR(elf_entry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 force_sig(SIGSEGV, current);
Chuck Ebbertce510592006-07-03 00:24:14 -0700954 retval = IS_ERR((void *)elf_entry) ?
955 (int)elf_entry : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 goto out_free_dentry;
957 }
958 reloc_func_desc = interp_load_addr;
959
960 allow_write_access(interpreter);
961 fput(interpreter);
962 kfree(elf_interpreter);
963 } else {
964 elf_entry = loc->elf_ex.e_entry;
Suresh Siddha5342fba2006-02-26 04:18:28 +0100965 if (BAD_ADDR(elf_entry)) {
Chuck Ebbertce510592006-07-03 00:24:14 -0700966 force_sig(SIGSEGV, current);
967 retval = -EINVAL;
Suresh Siddha5342fba2006-02-26 04:18:28 +0100968 goto out_free_dentry;
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
972 kfree(elf_phdata);
973
974 if (interpreter_type != INTERPRETER_AOUT)
975 sys_close(elf_exec_fileno);
976
977 set_binfmt(&elf_format);
978
Benjamin Herrenschmidt547ee842005-04-16 15:24:35 -0700979#ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
980 retval = arch_setup_additional_pages(bprm, executable_stack);
981 if (retval < 0) {
982 send_sig(SIGKILL, current, 0);
Roland McGrath18c8baff2005-04-28 15:17:19 -0700983 goto out;
Benjamin Herrenschmidt547ee842005-04-16 15:24:35 -0700984 }
985#endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 compute_creds(bprm);
988 current->flags &= ~PF_FORKNOEXEC;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700989 create_elf_tables(bprm, &loc->elf_ex,
990 (interpreter_type == INTERPRETER_AOUT),
991 load_addr, interp_load_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* N.B. passed_fileno might not be initialized? */
993 if (interpreter_type == INTERPRETER_AOUT)
994 current->mm->arg_start += strlen(passed_fileno) + 1;
995 current->mm->end_code = end_code;
996 current->mm->start_code = start_code;
997 current->mm->start_data = start_data;
998 current->mm->end_data = end_data;
999 current->mm->start_stack = bprm->p;
1000
1001 if (current->personality & MMAP_PAGE_ZERO) {
1002 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1003 and some applications "depend" upon this behavior.
1004 Since we do not have the power to recompile these, we
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001005 emulate the SVr4 behavior. Sigh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 down_write(&current->mm->mmap_sem);
1007 error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
1008 MAP_FIXED | MAP_PRIVATE, 0);
1009 up_write(&current->mm->mmap_sem);
1010 }
1011
1012#ifdef ELF_PLAT_INIT
1013 /*
1014 * The ABI may specify that certain registers be set up in special
1015 * ways (on i386 %edx is the address of a DT_FINI function, for
1016 * example. In addition, it may also specify (eg, PowerPC64 ELF)
1017 * that the e_entry field is the address of the function descriptor
1018 * for the startup routine, rather than the address of the startup
1019 * routine itself. This macro performs whatever initialization to
1020 * the regs structure is required as well as any relocations to the
1021 * function descriptor entries when executing dynamically links apps.
1022 */
1023 ELF_PLAT_INIT(regs, reloc_func_desc);
1024#endif
1025
1026 start_thread(regs, elf_entry, bprm->p);
1027 if (unlikely(current->ptrace & PT_PTRACED)) {
1028 if (current->ptrace & PT_TRACE_EXEC)
1029 ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
1030 else
1031 send_sig(SIGTRAP, current, 0);
1032 }
1033 retval = 0;
1034out:
1035 kfree(loc);
1036out_ret:
1037 return retval;
1038
1039 /* error cleanup */
1040out_free_dentry:
1041 allow_write_access(interpreter);
1042 if (interpreter)
1043 fput(interpreter);
1044out_free_interp:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001045 kfree(elf_interpreter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046out_free_file:
1047 sys_close(elf_exec_fileno);
1048out_free_fh:
Kirill Korotaev3b9b8ab2006-09-29 02:00:05 -07001049 if (files)
1050 reset_files_struct(current, files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051out_free_ph:
1052 kfree(elf_phdata);
1053 goto out;
1054}
1055
1056/* This is really simpleminded and specialized - we are loading an
1057 a.out library that is given an ELF header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058static int load_elf_library(struct file *file)
1059{
1060 struct elf_phdr *elf_phdata;
1061 struct elf_phdr *eppnt;
1062 unsigned long elf_bss, bss, len;
1063 int retval, error, i, j;
1064 struct elfhdr elf_ex;
1065
1066 error = -ENOEXEC;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001067 retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (retval != sizeof(elf_ex))
1069 goto out;
1070
1071 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
1072 goto out;
1073
1074 /* First of all, some simple consistency checks */
1075 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001076 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto out;
1078
1079 /* Now read in all of the header information */
1080
1081 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
1082 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1083
1084 error = -ENOMEM;
1085 elf_phdata = kmalloc(j, GFP_KERNEL);
1086 if (!elf_phdata)
1087 goto out;
1088
1089 eppnt = elf_phdata;
1090 error = -ENOEXEC;
1091 retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
1092 if (retval != j)
1093 goto out_free_ph;
1094
1095 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
1096 if ((eppnt + i)->p_type == PT_LOAD)
1097 j++;
1098 if (j != 1)
1099 goto out_free_ph;
1100
1101 while (eppnt->p_type != PT_LOAD)
1102 eppnt++;
1103
1104 /* Now use mmap to map the library into memory. */
1105 down_write(&current->mm->mmap_sem);
1106 error = do_mmap(file,
1107 ELF_PAGESTART(eppnt->p_vaddr),
1108 (eppnt->p_filesz +
1109 ELF_PAGEOFFSET(eppnt->p_vaddr)),
1110 PROT_READ | PROT_WRITE | PROT_EXEC,
1111 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
1112 (eppnt->p_offset -
1113 ELF_PAGEOFFSET(eppnt->p_vaddr)));
1114 up_write(&current->mm->mmap_sem);
1115 if (error != ELF_PAGESTART(eppnt->p_vaddr))
1116 goto out_free_ph;
1117
1118 elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
1119 if (padzero(elf_bss)) {
1120 error = -EFAULT;
1121 goto out_free_ph;
1122 }
1123
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001124 len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr +
1125 ELF_MIN_ALIGN - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 bss = eppnt->p_memsz + eppnt->p_vaddr;
1127 if (bss > len) {
1128 down_write(&current->mm->mmap_sem);
1129 do_brk(len, bss - len);
1130 up_write(&current->mm->mmap_sem);
1131 }
1132 error = 0;
1133
1134out_free_ph:
1135 kfree(elf_phdata);
1136out:
1137 return error;
1138}
1139
1140/*
1141 * Note that some platforms still use traditional core dumps and not
1142 * the ELF core dump. Each platform can select it as appropriate.
1143 */
Matt Mackall708e9a72006-01-08 01:05:25 -08001144#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146/*
1147 * ELF core dumper
1148 *
1149 * Modelled on fs/exec.c:aout_core_dump()
1150 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1151 */
1152/*
1153 * These are the only things you should do on a core-file: use only these
1154 * functions to write out all the necessary info.
1155 */
1156static int dump_write(struct file *file, const void *addr, int nr)
1157{
1158 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1159}
1160
Daniel Jacobowitz5db92852005-06-15 22:26:34 -07001161static int dump_seek(struct file *file, loff_t off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Andi Kleend025c9d2006-09-30 23:29:28 -07001163 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
Petr Vandrovec7f14daa2006-10-13 04:13:16 +02001164 if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return 0;
Andi Kleend025c9d2006-09-30 23:29:28 -07001166 } else {
1167 char *buf = (char *)get_zeroed_page(GFP_KERNEL);
1168 if (!buf)
1169 return 0;
1170 while (off > 0) {
1171 unsigned long n = off;
1172 if (n > PAGE_SIZE)
1173 n = PAGE_SIZE;
1174 if (!dump_write(file, buf, n))
1175 return 0;
1176 off -= n;
1177 }
1178 free_page((unsigned long)buf);
1179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return 1;
1181}
1182
1183/*
1184 * Decide whether a segment is worth dumping; default is yes to be
1185 * sure (missing info is worse than too much; etc).
1186 * Personally I'd include everything, and use the coredump limit...
1187 *
1188 * I think we should skip something. But I am not sure how. H.J.
1189 */
1190static int maydump(struct vm_area_struct *vma)
1191{
1192 /* Do not dump I/O mapped devices or special mappings */
1193 if (vma->vm_flags & (VM_IO | VM_RESERVED))
1194 return 0;
1195
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001196 /* Dump shared memory only if mapped from an anonymous file. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (vma->vm_flags & VM_SHARED)
1198 return vma->vm_file->f_dentry->d_inode->i_nlink == 0;
1199
1200 /* If it hasn't been written to, don't write it out */
1201 if (!vma->anon_vma)
1202 return 0;
1203
1204 return 1;
1205}
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207/* An ELF note in memory */
1208struct memelfnote
1209{
1210 const char *name;
1211 int type;
1212 unsigned int datasz;
1213 void *data;
1214};
1215
1216static int notesize(struct memelfnote *en)
1217{
1218 int sz;
1219
1220 sz = sizeof(struct elf_note);
1221 sz += roundup(strlen(en->name) + 1, 4);
1222 sz += roundup(en->datasz, 4);
1223
1224 return sz;
1225}
1226
Andi Kleend025c9d2006-09-30 23:29:28 -07001227#define DUMP_WRITE(addr, nr, foffset) \
1228 do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Andi Kleend025c9d2006-09-30 23:29:28 -07001230static int alignfile(struct file *file, loff_t *foffset)
1231{
Petr Vandroveca7a0d862006-10-13 18:42:07 +02001232 static const char buf[4] = { 0, };
Andi Kleend025c9d2006-09-30 23:29:28 -07001233 DUMP_WRITE(buf, roundup(*foffset, 4) - *foffset, foffset);
1234 return 1;
1235}
1236
1237static int writenote(struct memelfnote *men, struct file *file,
1238 loff_t *foffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct elf_note en;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 en.n_namesz = strlen(men->name) + 1;
1242 en.n_descsz = men->datasz;
1243 en.n_type = men->type;
1244
Andi Kleend025c9d2006-09-30 23:29:28 -07001245 DUMP_WRITE(&en, sizeof(en), foffset);
1246 DUMP_WRITE(men->name, en.n_namesz, foffset);
1247 if (!alignfile(file, foffset))
1248 return 0;
1249 DUMP_WRITE(men->data, men->datasz, foffset);
1250 if (!alignfile(file, foffset))
1251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 return 1;
1254}
1255#undef DUMP_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257#define DUMP_WRITE(addr, nr) \
1258 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1259 goto end_coredump;
1260#define DUMP_SEEK(off) \
1261 if (!dump_seek(file, (off))) \
1262 goto end_coredump;
1263
Arjan van de Ven858119e2006-01-14 13:20:43 -08001264static void fill_elf_header(struct elfhdr *elf, int segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1267 elf->e_ident[EI_CLASS] = ELF_CLASS;
1268 elf->e_ident[EI_DATA] = ELF_DATA;
1269 elf->e_ident[EI_VERSION] = EV_CURRENT;
1270 elf->e_ident[EI_OSABI] = ELF_OSABI;
1271 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1272
1273 elf->e_type = ET_CORE;
1274 elf->e_machine = ELF_ARCH;
1275 elf->e_version = EV_CURRENT;
1276 elf->e_entry = 0;
1277 elf->e_phoff = sizeof(struct elfhdr);
1278 elf->e_shoff = 0;
1279 elf->e_flags = ELF_CORE_EFLAGS;
1280 elf->e_ehsize = sizeof(struct elfhdr);
1281 elf->e_phentsize = sizeof(struct elf_phdr);
1282 elf->e_phnum = segs;
1283 elf->e_shentsize = 0;
1284 elf->e_shnum = 0;
1285 elf->e_shstrndx = 0;
1286 return;
1287}
1288
Andrew Morton8d6b5eee2006-09-25 23:32:04 -07001289static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 phdr->p_type = PT_NOTE;
1292 phdr->p_offset = offset;
1293 phdr->p_vaddr = 0;
1294 phdr->p_paddr = 0;
1295 phdr->p_filesz = sz;
1296 phdr->p_memsz = 0;
1297 phdr->p_flags = 0;
1298 phdr->p_align = 0;
1299 return;
1300}
1301
1302static void fill_note(struct memelfnote *note, const char *name, int type,
1303 unsigned int sz, void *data)
1304{
1305 note->name = name;
1306 note->type = type;
1307 note->datasz = sz;
1308 note->data = data;
1309 return;
1310}
1311
1312/*
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001313 * fill up all the fields in prstatus from the given task struct, except
1314 * registers which need to be filled up separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
1316static void fill_prstatus(struct elf_prstatus *prstatus,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001317 struct task_struct *p, long signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1320 prstatus->pr_sigpend = p->pending.signal.sig[0];
1321 prstatus->pr_sighold = p->blocked.sig[0];
1322 prstatus->pr_pid = p->pid;
1323 prstatus->pr_ppid = p->parent->pid;
1324 prstatus->pr_pgrp = process_group(p);
1325 prstatus->pr_sid = p->signal->session;
1326 if (thread_group_leader(p)) {
1327 /*
1328 * This is the record for the group leader. Add in the
1329 * cumulative times of previous dead threads. This total
1330 * won't include the time of each live thread whose state
1331 * is included in the core dump. The final total reported
1332 * to our parent process when it calls wait4 will include
1333 * those sums as well as the little bit more time it takes
1334 * this and each other thread to finish dying after the
1335 * core dump synchronization phase.
1336 */
1337 cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
1338 &prstatus->pr_utime);
1339 cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
1340 &prstatus->pr_stime);
1341 } else {
1342 cputime_to_timeval(p->utime, &prstatus->pr_utime);
1343 cputime_to_timeval(p->stime, &prstatus->pr_stime);
1344 }
1345 cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
1346 cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
1347}
1348
1349static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1350 struct mm_struct *mm)
1351{
Greg Kroah-Hartmana84a5052005-05-11 00:10:44 -07001352 unsigned int i, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 /* first copy the parameters from user space */
1355 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1356
1357 len = mm->arg_end - mm->arg_start;
1358 if (len >= ELF_PRARGSZ)
1359 len = ELF_PRARGSZ-1;
1360 if (copy_from_user(&psinfo->pr_psargs,
1361 (const char __user *)mm->arg_start, len))
1362 return -EFAULT;
1363 for(i = 0; i < len; i++)
1364 if (psinfo->pr_psargs[i] == 0)
1365 psinfo->pr_psargs[i] = ' ';
1366 psinfo->pr_psargs[len] = 0;
1367
1368 psinfo->pr_pid = p->pid;
1369 psinfo->pr_ppid = p->parent->pid;
1370 psinfo->pr_pgrp = process_group(p);
1371 psinfo->pr_sid = p->signal->session;
1372
1373 i = p->state ? ffz(~p->state) + 1 : 0;
1374 psinfo->pr_state = i;
Carsten Otte55148542006-03-25 03:08:22 -08001375 psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1377 psinfo->pr_nice = task_nice(p);
1378 psinfo->pr_flag = p->flags;
1379 SET_UID(psinfo->pr_uid, p->uid);
1380 SET_GID(psinfo->pr_gid, p->gid);
1381 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1382
1383 return 0;
1384}
1385
1386/* Here is the structure in which status of each thread is captured. */
1387struct elf_thread_status
1388{
1389 struct list_head list;
1390 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1391 elf_fpregset_t fpu; /* NT_PRFPREG */
1392 struct task_struct *thread;
1393#ifdef ELF_CORE_COPY_XFPREGS
1394 elf_fpxregset_t xfpu; /* NT_PRXFPREG */
1395#endif
1396 struct memelfnote notes[3];
1397 int num_notes;
1398};
1399
1400/*
1401 * In order to add the specific thread information for the elf file format,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001402 * we need to keep a linked list of every threads pr_status and then create
1403 * a single section for them in the final core file.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
1405static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1406{
1407 int sz = 0;
1408 struct task_struct *p = t->thread;
1409 t->num_notes = 0;
1410
1411 fill_prstatus(&t->prstatus, p, signr);
1412 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1413
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001414 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
1415 &(t->prstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 t->num_notes++;
1417 sz += notesize(&t->notes[0]);
1418
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001419 if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
1420 &t->fpu))) {
1421 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
1422 &(t->fpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 t->num_notes++;
1424 sz += notesize(&t->notes[1]);
1425 }
1426
1427#ifdef ELF_CORE_COPY_XFPREGS
1428 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001429 fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu),
1430 &t->xfpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 t->num_notes++;
1432 sz += notesize(&t->notes[2]);
1433 }
1434#endif
1435 return sz;
1436}
1437
1438/*
1439 * Actual dumper
1440 *
1441 * This is a two-pass process; first we find the offsets of the bits,
1442 * and then they are actually written out. If we run out of core limit
1443 * we just truncate.
1444 */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001445static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447#define NUM_NOTES 6
1448 int has_dumped = 0;
1449 mm_segment_t fs;
1450 int segs;
1451 size_t size = 0;
1452 int i;
1453 struct vm_area_struct *vma;
1454 struct elfhdr *elf = NULL;
Andi Kleend025c9d2006-09-30 23:29:28 -07001455 loff_t offset = 0, dataoff, foffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 unsigned long limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1457 int numnote;
1458 struct memelfnote *notes = NULL;
1459 struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
1460 struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
1461 struct task_struct *g, *p;
1462 LIST_HEAD(thread_list);
1463 struct list_head *t;
1464 elf_fpregset_t *fpu = NULL;
1465#ifdef ELF_CORE_COPY_XFPREGS
1466 elf_fpxregset_t *xfpu = NULL;
1467#endif
1468 int thread_status_size = 0;
1469 elf_addr_t *auxv;
1470
1471 /*
1472 * We no longer stop all VM operations.
1473 *
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001474 * This is because those proceses that could possibly change map_count
1475 * or the mmap / vma pages are now blocked in do_exit on current
1476 * finishing this core dump.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 *
1478 * Only ptrace can touch these memory addresses, but it doesn't change
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001479 * the map_count or the pages allocated. So no possibility of crashing
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 * exists while dumping the mm->vm_next areas to the core file.
1481 */
1482
1483 /* alloc memory for large data structures: too large to be on stack */
1484 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1485 if (!elf)
1486 goto cleanup;
1487 prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
1488 if (!prstatus)
1489 goto cleanup;
1490 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1491 if (!psinfo)
1492 goto cleanup;
1493 notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1494 if (!notes)
1495 goto cleanup;
1496 fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1497 if (!fpu)
1498 goto cleanup;
1499#ifdef ELF_CORE_COPY_XFPREGS
1500 xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1501 if (!xfpu)
1502 goto cleanup;
1503#endif
1504
1505 if (signr) {
1506 struct elf_thread_status *tmp;
Oleg Nesterov486ccb02006-09-29 02:00:24 -07001507 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 do_each_thread(g,p)
1509 if (current->mm == p->mm && current != p) {
Oliver Neukum11b0b5a2006-03-25 03:08:13 -08001510 tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (!tmp) {
Oleg Nesterov486ccb02006-09-29 02:00:24 -07001512 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 goto cleanup;
1514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 tmp->thread = p;
1516 list_add(&tmp->list, &thread_list);
1517 }
1518 while_each_thread(g,p);
Oleg Nesterov486ccb02006-09-29 02:00:24 -07001519 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 list_for_each(t, &thread_list) {
1521 struct elf_thread_status *tmp;
1522 int sz;
1523
1524 tmp = list_entry(t, struct elf_thread_status, list);
1525 sz = elf_dump_thread_status(signr, tmp);
1526 thread_status_size += sz;
1527 }
1528 }
1529 /* now collect the dump for the current */
1530 memset(prstatus, 0, sizeof(*prstatus));
1531 fill_prstatus(prstatus, current, signr);
1532 elf_core_copy_regs(&prstatus->pr_reg, regs);
1533
1534 segs = current->mm->map_count;
1535#ifdef ELF_CORE_EXTRA_PHDRS
1536 segs += ELF_CORE_EXTRA_PHDRS;
1537#endif
1538
1539 /* Set up header */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001540 fill_elf_header(elf, segs + 1); /* including notes section */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 has_dumped = 1;
1543 current->flags |= PF_DUMPCORE;
1544
1545 /*
1546 * Set up the notes in similar form to SVR4 core dumps made
1547 * with info from their /proc.
1548 */
1549
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001550 fill_note(notes + 0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 fill_psinfo(psinfo, current->group_leader, current->mm);
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001552 fill_note(notes + 1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Eric W. Biedermana9289722005-10-30 15:02:08 -08001554 numnote = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001556 auxv = (elf_addr_t *)current->mm->saved_auxv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 i = 0;
1559 do
1560 i += 2;
1561 while (auxv[i - 2] != AT_NULL);
1562 fill_note(&notes[numnote++], "CORE", NT_AUXV,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001563 i * sizeof(elf_addr_t), auxv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 /* Try to dump the FPU. */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001566 if ((prstatus->pr_fpvalid =
1567 elf_core_copy_task_fpregs(current, regs, fpu)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 fill_note(notes + numnote++,
1569 "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1570#ifdef ELF_CORE_COPY_XFPREGS
1571 if (elf_core_copy_task_xfpregs(current, xfpu))
1572 fill_note(notes + numnote++,
1573 "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
1574#endif
1575
1576 fs = get_fs();
1577 set_fs(KERNEL_DS);
1578
1579 DUMP_WRITE(elf, sizeof(*elf));
1580 offset += sizeof(*elf); /* Elf header */
Petr Vandroveca7a0d862006-10-13 18:42:07 +02001581 offset += (segs + 1) * sizeof(struct elf_phdr); /* Program headers */
1582 foffset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 /* Write notes phdr entry */
1585 {
1586 struct elf_phdr phdr;
1587 int sz = 0;
1588
1589 for (i = 0; i < numnote; i++)
1590 sz += notesize(notes + i);
1591
1592 sz += thread_status_size;
1593
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001594#ifdef ELF_CORE_WRITE_EXTRA_NOTES
1595 sz += ELF_CORE_EXTRA_NOTES_SIZE;
1596#endif
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 fill_elf_note_phdr(&phdr, sz, offset);
1599 offset += sz;
1600 DUMP_WRITE(&phdr, sizeof(phdr));
1601 }
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1604
1605 /* Write program headers for segments dump */
1606 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1607 struct elf_phdr phdr;
1608 size_t sz;
1609
1610 sz = vma->vm_end - vma->vm_start;
1611
1612 phdr.p_type = PT_LOAD;
1613 phdr.p_offset = offset;
1614 phdr.p_vaddr = vma->vm_start;
1615 phdr.p_paddr = 0;
1616 phdr.p_filesz = maydump(vma) ? sz : 0;
1617 phdr.p_memsz = sz;
1618 offset += phdr.p_filesz;
1619 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001620 if (vma->vm_flags & VM_WRITE)
1621 phdr.p_flags |= PF_W;
1622 if (vma->vm_flags & VM_EXEC)
1623 phdr.p_flags |= PF_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 phdr.p_align = ELF_EXEC_PAGESIZE;
1625
1626 DUMP_WRITE(&phdr, sizeof(phdr));
1627 }
1628
1629#ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1630 ELF_CORE_WRITE_EXTRA_PHDRS;
1631#endif
1632
1633 /* write out the notes section */
1634 for (i = 0; i < numnote; i++)
Andi Kleend025c9d2006-09-30 23:29:28 -07001635 if (!writenote(notes + i, file, &foffset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 goto end_coredump;
1637
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001638#ifdef ELF_CORE_WRITE_EXTRA_NOTES
1639 ELF_CORE_WRITE_EXTRA_NOTES;
1640#endif
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 /* write out the thread status notes section */
1643 list_for_each(t, &thread_list) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001644 struct elf_thread_status *tmp =
1645 list_entry(t, struct elf_thread_status, list);
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 for (i = 0; i < tmp->num_notes; i++)
Andi Kleend025c9d2006-09-30 23:29:28 -07001648 if (!writenote(&tmp->notes[i], file, &foffset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 goto end_coredump;
1650 }
Andi Kleend025c9d2006-09-30 23:29:28 -07001651
1652 /* Align to page */
1653 DUMP_SEEK(dataoff - foffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1656 unsigned long addr;
1657
1658 if (!maydump(vma))
1659 continue;
1660
1661 for (addr = vma->vm_start;
1662 addr < vma->vm_end;
1663 addr += PAGE_SIZE) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001664 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 struct vm_area_struct *vma;
1666
1667 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1668 &page, &vma) <= 0) {
Andi Kleend025c9d2006-09-30 23:29:28 -07001669 DUMP_SEEK(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 } else {
1671 if (page == ZERO_PAGE(addr)) {
Andi Kleend025c9d2006-09-30 23:29:28 -07001672 DUMP_SEEK(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 } else {
1674 void *kaddr;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001675 flush_cache_page(vma, addr,
1676 page_to_pfn(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 kaddr = kmap(page);
1678 if ((size += PAGE_SIZE) > limit ||
1679 !dump_write(file, kaddr,
1680 PAGE_SIZE)) {
1681 kunmap(page);
1682 page_cache_release(page);
1683 goto end_coredump;
1684 }
1685 kunmap(page);
1686 }
1687 page_cache_release(page);
1688 }
1689 }
1690 }
1691
1692#ifdef ELF_CORE_WRITE_EXTRA_DATA
1693 ELF_CORE_WRITE_EXTRA_DATA;
1694#endif
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696end_coredump:
1697 set_fs(fs);
1698
1699cleanup:
Jesper Juhl74da6cd2006-01-11 01:51:26 +01001700 while (!list_empty(&thread_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 struct list_head *tmp = thread_list.next;
1702 list_del(tmp);
1703 kfree(list_entry(tmp, struct elf_thread_status, list));
1704 }
1705
1706 kfree(elf);
1707 kfree(prstatus);
1708 kfree(psinfo);
1709 kfree(notes);
1710 kfree(fpu);
1711#ifdef ELF_CORE_COPY_XFPREGS
1712 kfree(xfpu);
1713#endif
1714 return has_dumped;
1715#undef NUM_NOTES
1716}
1717
1718#endif /* USE_ELF_CORE_DUMP */
1719
1720static int __init init_elf_binfmt(void)
1721{
1722 return register_binfmt(&elf_format);
1723}
1724
1725static void __exit exit_elf_binfmt(void)
1726{
1727 /* Remove the COFF and ELF loaders. */
1728 unregister_binfmt(&elf_format);
1729}
1730
1731core_initcall(init_elf_binfmt);
1732module_exit(exit_elf_binfmt);
1733MODULE_LICENSE("GPL");