MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / binfmt_elf.c
blobca48694d9e1fc02281a2927447034a2e3c830126
1 /*
2 * linux/fs/binfmt_elf.c
4 * These are the functions used to load ELF format executables as used
5 * on SVr4 machines. Information on the format may be found in the book
6 * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
7 * Tools".
9 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
11 * History:
12 * 02-13-2006 Add the soft lock feature. This has patent. It belongs to Moxa Systems Corp.
13 * If you want to use this feature, you must get the license from Moxa Systems.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/fs.h>
19 #include <linux/stat.h>
20 #include <linux/time.h>
21 #include <linux/mm.h>
22 #include <linux/mman.h>
23 #include <linux/a.out.h>
24 #include <linux/errno.h>
25 #include <linux/signal.h>
26 #include <linux/binfmts.h>
27 #include <linux/string.h>
28 #include <linux/file.h>
29 #include <linux/fcntl.h>
30 #include <linux/ptrace.h>
31 #include <linux/slab.h>
32 #include <linux/shm.h>
33 #include <linux/personality.h>
34 #include <linux/elfcore.h>
35 #include <linux/init.h>
36 #include <linux/highuid.h>
37 #include <linux/smp.h>
38 #include <linux/smp_lock.h>
39 #include <linux/compiler.h>
40 #include <linux/highmem.h>
41 #include <linux/pagemap.h>
42 #include <linux/security.h>
43 #include <linux/syscalls.h>
45 #include <asm/uaccess.h>
46 #include <asm/param.h>
47 #include <asm/page.h>
49 #include <linux/elf.h>
51 #ifdef CONFIG_MOXA_SOFT_LOCK // add by Victor Yu. 02-13-2006
52 #include <linux/crypto.h>
53 #define EXTEND_FILE_NAME ".enc"
54 #define EXTEND_FILE_NAME_LEN 4
55 #endif
57 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs);
58 static int load_elf_library(struct file*);
59 static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int);
60 extern int dump_fpu (struct pt_regs *, elf_fpregset_t *);
62 #ifndef elf_addr_t
63 #define elf_addr_t unsigned long
64 #endif
67 * If we don't support core dumping, then supply a NULL so we
68 * don't even try.
70 #ifdef USE_ELF_CORE_DUMP
71 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file);
72 #else
73 #define elf_core_dump NULL
74 #endif
76 #if ELF_EXEC_PAGESIZE > PAGE_SIZE
77 # define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
78 #else
79 # define ELF_MIN_ALIGN PAGE_SIZE
80 #endif
82 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
83 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
84 #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
86 static struct linux_binfmt elf_format = {
87 .module = THIS_MODULE,
88 .load_binary = load_elf_binary,
89 .load_shlib = load_elf_library,
90 .core_dump = elf_core_dump,
91 .min_coredump = ELF_EXEC_PAGESIZE
94 #define BAD_ADDR(x) ((unsigned long)(x) > TASK_SIZE)
96 static int set_brk(unsigned long start, unsigned long end)
98 start = ELF_PAGEALIGN(start);
99 end = ELF_PAGEALIGN(end);
100 if (end > start) {
101 unsigned long addr = do_brk(start, end - start);
102 if (BAD_ADDR(addr))
103 return addr;
105 current->mm->start_brk = current->mm->brk = end;
106 return 0;
110 /* We need to explicitly zero any fractional pages
111 after the data section (i.e. bss). This would
112 contain the junk from the file that should not
113 be in memory */
116 static void padzero(unsigned long elf_bss)
118 unsigned long nbyte;
120 nbyte = ELF_PAGEOFFSET(elf_bss);
121 if (nbyte) {
122 nbyte = ELF_MIN_ALIGN - nbyte;
123 clear_user((void __user *) elf_bss, nbyte);
127 /* Let's use some macros to make this stack manipulation a litle clearer */
128 #ifdef CONFIG_STACK_GROWSUP
129 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
130 #define STACK_ROUND(sp, items) \
131 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
132 #define STACK_ALLOC(sp, len) ({ elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; old_sp; })
133 #else
134 #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
135 #define STACK_ROUND(sp, items) \
136 (((unsigned long) (sp - items)) &~ 15UL)
137 #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
138 #endif
140 static void
141 create_elf_tables(struct linux_binprm *bprm, struct elfhdr * exec,
142 int interp_aout, unsigned long load_addr,
143 unsigned long interp_load_addr)
145 unsigned long p = bprm->p;
146 int argc = bprm->argc;
147 int envc = bprm->envc;
148 elf_addr_t __user *argv;
149 elf_addr_t __user *envp;
150 elf_addr_t __user *sp;
151 elf_addr_t __user *u_platform;
152 const char *k_platform = ELF_PLATFORM;
153 int items;
154 elf_addr_t *elf_info;
155 int ei_index = 0;
156 struct task_struct *tsk = current;
159 * If this architecture has a platform capability string, copy it
160 * to userspace. In some cases (Sparc), this info is impossible
161 * for userspace to get any other way, in others (i386) it is
162 * merely difficult.
165 u_platform = NULL;
166 if (k_platform) {
167 size_t len = strlen(k_platform) + 1;
169 #ifdef CONFIG_X86_HT
171 * In some cases (e.g. Hyper-Threading), we want to avoid L1
172 * evictions by the processes running on the same package. One
173 * thing we can do is to shuffle the initial stack for them.
175 * The conditionals here are unneeded, but kept in to make the
176 * code behaviour the same as pre change unless we have
177 * hyperthreaded processors. This should be cleaned up
178 * before 2.6
181 if (smp_num_siblings > 1)
182 STACK_ALLOC(p, ((current->pid % 64) << 7));
183 #endif
184 u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
185 __copy_to_user(u_platform, k_platform, len);
188 /* Create the ELF interpreter info */
189 elf_info = (elf_addr_t *) current->mm->saved_auxv;
190 #define NEW_AUX_ENT(id, val) \
191 do { elf_info[ei_index++] = id; elf_info[ei_index++] = val; } while (0)
193 #ifdef ARCH_DLINFO
195 * ARCH_DLINFO must come first so PPC can do its special alignment of
196 * AUXV.
198 ARCH_DLINFO;
199 #endif
200 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
201 NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
202 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
203 NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
204 NEW_AUX_ENT(AT_PHENT, sizeof (struct elf_phdr));
205 NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
206 NEW_AUX_ENT(AT_BASE, interp_load_addr);
207 NEW_AUX_ENT(AT_FLAGS, 0);
208 NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
209 NEW_AUX_ENT(AT_UID, (elf_addr_t) tsk->uid);
210 NEW_AUX_ENT(AT_EUID, (elf_addr_t) tsk->euid);
211 NEW_AUX_ENT(AT_GID, (elf_addr_t) tsk->gid);
212 NEW_AUX_ENT(AT_EGID, (elf_addr_t) tsk->egid);
213 NEW_AUX_ENT(AT_SECURE, (elf_addr_t) security_bprm_secureexec(bprm));
214 if (k_platform) {
215 NEW_AUX_ENT(AT_PLATFORM, (elf_addr_t)(unsigned long)u_platform);
217 if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
218 NEW_AUX_ENT(AT_EXECFD, (elf_addr_t) bprm->interp_data);
220 #undef NEW_AUX_ENT
221 /* AT_NULL is zero; clear the rest too */
222 memset(&elf_info[ei_index], 0,
223 sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
225 /* And advance past the AT_NULL entry. */
226 ei_index += 2;
228 sp = STACK_ADD(p, ei_index);
230 items = (argc + 1) + (envc + 1);
231 if (interp_aout) {
232 items += 3; /* a.out interpreters require argv & envp too */
233 } else {
234 items += 1; /* ELF interpreters only put argc on the stack */
236 bprm->p = STACK_ROUND(sp, items);
238 /* Point sp at the lowest address on the stack */
239 #ifdef CONFIG_STACK_GROWSUP
240 sp = (elf_addr_t __user *)bprm->p - items - ei_index;
241 bprm->exec = (unsigned long) sp; /* XXX: PARISC HACK */
242 #else
243 sp = (elf_addr_t __user *)bprm->p;
244 #endif
246 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
247 __put_user(argc, sp++);
248 if (interp_aout) {
249 argv = sp + 2;
250 envp = argv + argc + 1;
251 __put_user((elf_addr_t)(unsigned long)argv, sp++);
252 __put_user((elf_addr_t)(unsigned long)envp, sp++);
253 } else {
254 argv = sp;
255 envp = argv + argc + 1;
258 /* Populate argv and envp */
259 p = current->mm->arg_start;
260 while (argc-- > 0) {
261 size_t len;
262 __put_user((elf_addr_t)p, argv++);
263 len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
264 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
265 return;
266 p += len;
268 __put_user(0, argv);
269 current->mm->arg_end = current->mm->env_start = p;
270 while (envc-- > 0) {
271 size_t len;
272 __put_user((elf_addr_t)p, envp++);
273 len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
274 if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
275 return;
276 p += len;
278 __put_user(0, envp);
279 current->mm->env_end = p;
281 /* Put the elf_info on the stack in the right place. */
282 sp = (elf_addr_t __user *)envp + 1;
283 copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t));
286 #ifndef elf_map
288 static unsigned long elf_map(struct file *filep, unsigned long addr,
289 struct elf_phdr *eppnt, int prot, int type)
291 unsigned long map_addr;
293 down_write(&current->mm->mmap_sem);
294 map_addr = do_mmap(filep, ELF_PAGESTART(addr),
295 eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr), prot, type,
296 eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
297 up_write(&current->mm->mmap_sem);
298 return(map_addr);
301 #endif /* !elf_map */
303 /* This is much more generalized than the library routine read function,
304 so we keep this separate. Technically the library read function
305 is only provided so that we can read a.out libraries that have
306 an ELF header */
308 static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
309 struct file * interpreter,
310 unsigned long *interp_load_addr)
312 struct elf_phdr *elf_phdata;
313 struct elf_phdr *eppnt;
314 unsigned long load_addr = 0;
315 int load_addr_set = 0;
316 unsigned long last_bss = 0, elf_bss = 0;
317 unsigned long error = ~0UL;
318 int retval, i, size;
320 /* First of all, some simple consistency checks */
321 if (interp_elf_ex->e_type != ET_EXEC &&
322 interp_elf_ex->e_type != ET_DYN)
323 goto out;
324 if (!elf_check_arch(interp_elf_ex))
325 goto out;
326 if (!interpreter->f_op || !interpreter->f_op->mmap)
327 goto out;
330 * If the size of this structure has changed, then punt, since
331 * we will be doing the wrong thing.
333 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
334 goto out;
335 if (interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
336 goto out;
338 /* Now read in all of the header information */
340 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
341 if (size > ELF_MIN_ALIGN)
342 goto out;
343 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
344 if (!elf_phdata)
345 goto out;
347 retval = kernel_read(interpreter,interp_elf_ex->e_phoff,(char *)elf_phdata,size);
348 error = retval;
349 if (retval < 0)
350 goto out_close;
352 eppnt = elf_phdata;
353 for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
354 if (eppnt->p_type == PT_LOAD) {
355 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
356 int elf_prot = 0;
357 unsigned long vaddr = 0;
358 unsigned long k, map_addr;
360 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
361 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
362 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
363 vaddr = eppnt->p_vaddr;
364 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
365 elf_type |= MAP_FIXED;
367 map_addr = elf_map(interpreter, load_addr + vaddr, eppnt, elf_prot, elf_type);
368 error = map_addr;
369 if (BAD_ADDR(map_addr))
370 goto out_close;
372 if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
373 load_addr = map_addr - ELF_PAGESTART(vaddr);
374 load_addr_set = 1;
378 * Check to see if the section's size will overflow the
379 * allowed task size. Note that p_filesz must always be
380 * <= p_memsize so it is only necessary to check p_memsz.
382 k = load_addr + eppnt->p_vaddr;
383 if (k > TASK_SIZE || eppnt->p_filesz > eppnt->p_memsz ||
384 eppnt->p_memsz > TASK_SIZE || TASK_SIZE - eppnt->p_memsz < k) {
385 error = -ENOMEM;
386 goto out_close;
390 * Find the end of the file mapping for this phdr, and keep
391 * track of the largest address we see for this.
393 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
394 if (k > elf_bss)
395 elf_bss = k;
398 * Do the same thing for the memory mapping - between
399 * elf_bss and last_bss is the bss section.
401 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
402 if (k > last_bss)
403 last_bss = k;
408 * Now fill out the bss section. First pad the last page up
409 * to the page boundary, and then perform a mmap to make sure
410 * that there are zero-mapped pages up to and including the
411 * last bss page.
413 padzero(elf_bss);
414 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1); /* What we have mapped so far */
416 /* Map the last of the bss segment */
417 if (last_bss > elf_bss) {
418 error = do_brk(elf_bss, last_bss - elf_bss);
419 if (BAD_ADDR(error))
420 goto out_close;
423 *interp_load_addr = load_addr;
424 error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
426 out_close:
427 kfree(elf_phdata);
428 out:
429 return error;
432 static unsigned long load_aout_interp(struct exec * interp_ex,
433 struct file * interpreter)
435 unsigned long text_data, elf_entry = ~0UL;
436 char __user * addr;
437 loff_t offset;
439 current->mm->end_code = interp_ex->a_text;
440 text_data = interp_ex->a_text + interp_ex->a_data;
441 current->mm->end_data = text_data;
442 current->mm->brk = interp_ex->a_bss + text_data;
444 switch (N_MAGIC(*interp_ex)) {
445 case OMAGIC:
446 offset = 32;
447 addr = (char __user *)0;
448 break;
449 case ZMAGIC:
450 case QMAGIC:
451 offset = N_TXTOFF(*interp_ex);
452 addr = (char __user *) N_TXTADDR(*interp_ex);
453 break;
454 default:
455 goto out;
458 do_brk(0, text_data);
459 if (!interpreter->f_op || !interpreter->f_op->read)
460 goto out;
461 if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
462 goto out;
463 flush_icache_range((unsigned long)addr,
464 (unsigned long)addr + text_data);
466 do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
467 interp_ex->a_bss);
468 elf_entry = interp_ex->a_entry;
470 out:
471 return elf_entry;
474 #ifdef CONFIG_MOXA_SOFT_LOCK // add by Victor Yu. 02-13-2006
475 #define MOXA_SOFT_LOCK_IV_LEN 8
476 #include <linux/mtd/mtd.h>
477 #include <asm/scatterlist.h>
478 int moxa_soft_lock_desc(char *buf, int size)
480 #if 0
481 unsigned short *ptr, us;
482 int i;
484 ptr = (unsigned short *)buf;
485 for ( i=0; i<size/2; i++, ptr++ ) {
486 us = *ptr;
487 us = (us << 8) | (us >> 8);
488 *ptr = us;
490 return 0;
491 #else
493 int retval=0, bsize, len;
494 struct crypto_tfm *tfm;
495 char iv[MOXA_SOFT_LOCK_IV_LEN], *tmpbuf;
496 char key[MOXA_SOFT_LOCK_IV_LEN];
497 struct scatterlist sg;
498 struct mtd_info *mtd;
500 size &= ~7; //prince add 20061215
501 // first allocate the transform table
502 tfm = crypto_alloc_tfm("des", CRYPTO_TFM_MODE_CBC);
503 if ( tfm == NULL )
504 return -ENOMEM;
505 bsize = crypto_tfm_alg_blocksize(tfm);
506 if ( bsize <= 0 ) {
507 retval = -ENOMEM;
508 goto out_moxa_soft_lock;
510 tmpbuf = kmalloc(bsize, GFP_KERNEL);
511 if ( !tmpbuf ) {
512 retval = -ENOMEM;
513 goto out_moxa_soft_lock;
516 // set the key value
517 tfm->crt_flags = 0;
518 mtd = get_mtd_device(NULL, 0);
519 if ( mtd == NULL ) {
520 retval = -ENOMEM;
521 goto out_tmpbuf;
523 mtd->read(mtd, 0x68, MOXA_SOFT_LOCK_IV_LEN, &len, key);
524 if ( len != MOXA_SOFT_LOCK_IV_LEN ) {
525 retval = -ENOMEM;
526 goto out_tmpbuf;
528 retval = crypto_cipher_setkey(tfm, key, MOXA_SOFT_LOCK_IV_LEN);
529 if ( retval )
530 goto out_tmpbuf;
532 // set the initial vector
533 memset(iv, 0, MOXA_SOFT_LOCK_IV_LEN);
534 crypto_cipher_set_iv(tfm, iv, MOXA_SOFT_LOCK_IV_LEN);
536 // dencryptation
537 while ( size ) {
538 if ( size > bsize )
539 len = bsize;
540 else
541 len = size;
542 memcpy(tmpbuf, buf, len);
543 if ( len < bsize )
544 memset(&tmpbuf[len], 0, bsize-len);
545 sg.page = virt_to_page(tmpbuf);
546 sg.offset = offset_in_page(tmpbuf);
547 sg.length = bsize;
548 retval = crypto_cipher_decrypt(tfm, &sg, &sg, bsize);
549 if ( retval ) {
550 break;
552 memcpy(buf, tmpbuf, len);
553 buf += len;
554 size -= len;
557 out_tmpbuf:
558 kfree(tmpbuf);
559 out_moxa_soft_lock:
560 crypto_free_tfm(tfm);
561 return retval;
562 #endif
564 #endif
567 * These are the functions used to load ELF style executables and shared
568 * libraries. There is no binary dependent code anywhere else.
571 #define INTERPRETER_NONE 0
572 #define INTERPRETER_AOUT 1
573 #define INTERPRETER_ELF 2
575 static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
577 struct file *interpreter = NULL; /* to shut gcc up */
578 unsigned long load_addr = 0, load_bias = 0;
579 int load_addr_set = 0;
580 char * elf_interpreter = NULL;
581 unsigned int interpreter_type = INTERPRETER_NONE;
582 unsigned char ibcs2_interpreter = 0;
583 unsigned long error;
584 struct elf_phdr * elf_ppnt, *elf_phdata;
585 unsigned long elf_bss, elf_brk;
586 int elf_exec_fileno;
587 int retval, i;
588 unsigned int size;
589 unsigned long elf_entry, interp_load_addr = 0;
590 unsigned long start_code, end_code, start_data, end_data;
591 unsigned long reloc_func_desc = 0;
592 char passed_fileno[6];
593 struct files_struct *files;
594 int have_pt_gnu_stack, executable_stack = EXSTACK_DEFAULT;
595 unsigned long def_flags = 0;
596 struct {
597 struct elfhdr elf_ex;
598 struct elfhdr interp_elf_ex;
599 struct exec interp_ex;
600 } *loc;
601 #ifdef CONFIG_MOXA_SOFT_LOCK // add by Victor Yu. 02-09-2006
602 int dsize=0, lock_flag=0;
603 #endif
605 loc = kmalloc(sizeof(*loc), GFP_KERNEL);
606 if (!loc) {
607 retval = -ENOMEM;
608 goto out_ret;
611 /* Get the exec-header */
612 loc->elf_ex = *((struct elfhdr *) bprm->buf);
614 retval = -ENOEXEC;
615 /* First of all, some simple consistency checks */
616 if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
617 goto out;
619 if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
620 goto out;
621 if (!elf_check_arch(&loc->elf_ex))
622 goto out;
623 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
624 goto out;
626 #ifdef CONFIG_MOXA_SOFT_LOCK // add by Victor Yu. 02-09-2006
627 i = strlen(bprm->file->f_dentry->d_iname);
628 if ( i > EXTEND_FILE_NAME_LEN && strcmp(&bprm->file->f_dentry->d_iname[i-EXTEND_FILE_NAME_LEN], EXTEND_FILE_NAME) == 0 ) {
629 lock_flag = 1;
631 #endif
632 /* Now read in all of the header information */
633 retval = -ENOMEM;
634 if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
635 goto out;
636 if (loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
637 goto out;
638 size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
639 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
640 if (!elf_phdata)
641 goto out;
643 retval = kernel_read(bprm->file, loc->elf_ex.e_phoff, (char *) elf_phdata, size);
644 if (retval < 0)
645 goto out_free_ph;
647 files = current->files; /* Refcounted so ok */
648 retval = unshare_files();
649 if (retval < 0)
650 goto out_free_ph;
651 if (files == current->files) {
652 put_files_struct(files);
653 files = NULL;
656 /* exec will make our files private anyway, but for the a.out
657 loader stuff we need to do it earlier */
659 retval = get_unused_fd();
660 if (retval < 0)
661 goto out_free_fh;
662 get_file(bprm->file);
663 fd_install(elf_exec_fileno = retval, bprm->file);
665 elf_ppnt = elf_phdata;
666 elf_bss = 0;
667 elf_brk = 0;
669 start_code = ~0UL;
670 end_code = 0;
671 start_data = 0;
672 end_data = 0;
674 for (i = 0; i < loc->elf_ex.e_phnum; i++) {
675 if (elf_ppnt->p_type == PT_INTERP) {
676 /* This is the program interpreter used for
677 * shared libraries - for now assume that this
678 * is an a.out format binary
681 retval = -ENOMEM;
682 if (elf_ppnt->p_filesz > PATH_MAX)
683 goto out_free_file;
684 elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
685 GFP_KERNEL);
686 if (!elf_interpreter)
687 goto out_free_file;
689 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
690 elf_interpreter,
691 elf_ppnt->p_filesz);
692 if (retval < 0)
693 goto out_free_interp;
694 /* If the program interpreter is one of these two,
695 * then assume an iBCS2 image. Otherwise assume
696 * a native linux image.
698 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
699 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
700 ibcs2_interpreter = 1;
703 * The early SET_PERSONALITY here is so that the lookup
704 * for the interpreter happens in the namespace of the
705 * to-be-execed image. SET_PERSONALITY can select an
706 * alternate root.
708 * However, SET_PERSONALITY is NOT allowed to switch
709 * this task into the new images's memory mapping
710 * policy - that is, TASK_SIZE must still evaluate to
711 * that which is appropriate to the execing application.
712 * This is because exit_mmap() needs to have TASK_SIZE
713 * evaluate to the size of the old image.
715 * So if (say) a 64-bit application is execing a 32-bit
716 * application it is the architecture's responsibility
717 * to defer changing the value of TASK_SIZE until the
718 * switch really is going to happen - do this in
719 * flush_thread(). - akpm
721 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
723 interpreter = open_exec(elf_interpreter);
724 retval = PTR_ERR(interpreter);
725 if (IS_ERR(interpreter))
726 goto out_free_interp;
727 retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
728 if (retval < 0)
729 goto out_free_dentry;
731 /* Get the exec headers */
732 loc->interp_ex = *((struct exec *) bprm->buf);
733 loc->interp_elf_ex = *((struct elfhdr *) bprm->buf);
734 break;
736 elf_ppnt++;
739 elf_ppnt = elf_phdata;
740 for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
741 if (elf_ppnt->p_type == PT_GNU_STACK) {
742 if (elf_ppnt->p_flags & PF_X)
743 executable_stack = EXSTACK_ENABLE_X;
744 else
745 executable_stack = EXSTACK_DISABLE_X;
746 break;
748 have_pt_gnu_stack = (i < loc->elf_ex.e_phnum);
750 /* Some simple consistency checks for the interpreter */
751 if (elf_interpreter) {
752 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
754 /* Now figure out which format our binary is */
755 if ((N_MAGIC(loc->interp_ex) != OMAGIC) &&
756 (N_MAGIC(loc->interp_ex) != ZMAGIC) &&
757 (N_MAGIC(loc->interp_ex) != QMAGIC))
758 interpreter_type = INTERPRETER_ELF;
760 if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
761 interpreter_type &= ~INTERPRETER_ELF;
763 retval = -ELIBBAD;
764 if (!interpreter_type)
765 goto out_free_dentry;
767 /* Make sure only one type was selected */
768 if ((interpreter_type & INTERPRETER_ELF) &&
769 interpreter_type != INTERPRETER_ELF) {
770 // FIXME - ratelimit this before re-enabling
771 // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
772 interpreter_type = INTERPRETER_ELF;
774 /* Verify the interpreter has a valid arch */
775 if ((interpreter_type == INTERPRETER_ELF) &&
776 !elf_check_arch(&loc->interp_elf_ex))
777 goto out_free_dentry;
778 } else {
779 /* Executables without an interpreter also need a personality */
780 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
783 /* OK, we are done with that, now set up the arg stuff,
784 and then start this sucker up */
786 if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
787 char *passed_p = passed_fileno;
788 sprintf(passed_fileno, "%d", elf_exec_fileno);
790 if (elf_interpreter) {
791 retval = copy_strings_kernel(1, &passed_p, bprm);
792 if (retval)
793 goto out_free_dentry;
794 bprm->argc++;
798 /* Flush all traces of the currently running executable */
799 retval = flush_old_exec(bprm);
800 if (retval)
801 goto out_free_dentry;
803 /* Discard our unneeded old files struct */
804 if (files) {
805 steal_locks(files);
806 put_files_struct(files);
807 files = NULL;
810 /* OK, This is the point of no return */
811 current->mm->start_data = 0;
812 current->mm->end_data = 0;
813 current->mm->end_code = 0;
814 current->mm->mmap = NULL;
815 current->flags &= ~PF_FORKNOEXEC;
816 current->mm->def_flags = def_flags;
818 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
819 may depend on the personality. */
820 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
821 if (elf_read_implies_exec(loc->elf_ex, have_pt_gnu_stack))
822 current->personality |= READ_IMPLIES_EXEC;
824 arch_pick_mmap_layout(current->mm);
826 /* Do this so that we can load the interpreter, if need be. We will
827 change some of these later */
828 current->mm->rss = 0;
829 current->mm->free_area_cache = current->mm->mmap_base;
830 retval = setup_arg_pages(bprm, executable_stack);
831 if (retval < 0) {
832 send_sig(SIGKILL, current, 0);
833 goto out_free_dentry;
836 current->mm->start_stack = bprm->p;
838 /* Now we do a little grungy work by mmaping the ELF image into
839 the correct location in memory. At this point, we assume that
840 the image should be loaded at fixed address, not at a variable
841 address. */
843 for(i = 0, elf_ppnt = elf_phdata; i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
844 int elf_prot = 0, elf_flags;
845 unsigned long k, vaddr;
847 if (elf_ppnt->p_type != PT_LOAD)
848 continue;
850 if (unlikely (elf_brk > elf_bss)) {
851 unsigned long nbyte;
853 /* There was a PT_LOAD segment with p_memsz > p_filesz
854 before this one. Map anonymous pages, if needed,
855 and clear the area. */
856 retval = set_brk (elf_bss + load_bias,
857 elf_brk + load_bias);
858 if (retval) {
859 send_sig(SIGKILL, current, 0);
860 goto out_free_dentry;
862 nbyte = ELF_PAGEOFFSET(elf_bss);
863 if (nbyte) {
864 nbyte = ELF_MIN_ALIGN - nbyte;
865 if (nbyte > elf_brk - elf_bss)
866 nbyte = elf_brk - elf_bss;
867 clear_user((void __user *) elf_bss + load_bias, nbyte);
871 if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
872 if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
873 if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
875 elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
877 vaddr = elf_ppnt->p_vaddr;
878 if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
879 elf_flags |= MAP_FIXED;
880 } else if (loc->elf_ex.e_type == ET_DYN) {
881 /* Try and get dynamic programs out of the way of the default mmap
882 base, as well as whatever program they might try to exec. This
883 is because the brk will follow the loader, and is not movable. */
884 load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
887 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags);
888 if (BAD_ADDR(error))
889 continue;
890 #ifdef CONFIG_MOXA_SOFT_LOCK // add by Victor Yu. 02-09-2006
891 if ( lock_flag && elf_ppnt->p_offset ) {
892 dsize = elf_ppnt->p_filesz;
893 //kernel_read(bprm->file, elf_ppnt->p_offset, (char *)error, dsize);
895 #endif
897 if (!load_addr_set) {
898 load_addr_set = 1;
899 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
900 if (loc->elf_ex.e_type == ET_DYN) {
901 load_bias += error -
902 ELF_PAGESTART(load_bias + vaddr);
903 load_addr += load_bias;
904 reloc_func_desc = load_bias;
907 k = elf_ppnt->p_vaddr;
908 if (k < start_code) start_code = k;
909 if (start_data < k) start_data = k;
912 * Check to see if the section's size will overflow the
913 * allowed task size. Note that p_filesz must always be
914 * <= p_memsz so it is only necessary to check p_memsz.
916 if (k > TASK_SIZE || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
917 elf_ppnt->p_memsz > TASK_SIZE ||
918 TASK_SIZE - elf_ppnt->p_memsz < k) {
919 /* set_brk can never work. Avoid overflows. */
920 send_sig(SIGKILL, current, 0);
921 goto out_free_dentry;
924 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
926 if (k > elf_bss)
927 elf_bss = k;
928 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
929 end_code = k;
930 if (end_data < k)
931 end_data = k;
932 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
933 if (k > elf_brk)
934 elf_brk = k;
937 loc->elf_ex.e_entry += load_bias;
938 elf_bss += load_bias;
939 elf_brk += load_bias;
940 start_code += load_bias;
941 end_code += load_bias;
942 start_data += load_bias;
943 end_data += load_bias;
945 /* Calling set_brk effectively mmaps the pages that we need
946 * for the bss and break sections. We must do this before
947 * mapping in the interpreter, to make sure it doesn't wind
948 * up getting placed where the bss needs to go.
950 retval = set_brk(elf_bss, elf_brk);
951 if (retval) {
952 send_sig(SIGKILL, current, 0);
953 goto out_free_dentry;
955 padzero(elf_bss);
957 if (elf_interpreter) {
958 if (interpreter_type == INTERPRETER_AOUT)
959 elf_entry = load_aout_interp(&loc->interp_ex,
960 interpreter);
961 else
962 elf_entry = load_elf_interp(&loc->interp_elf_ex,
963 interpreter,
964 &interp_load_addr);
965 if (BAD_ADDR(elf_entry)) {
966 printk(KERN_ERR "Unable to load interpreter\n");
967 send_sig(SIGSEGV, current, 0);
968 retval = -ENOEXEC; /* Nobody gets to see this, but.. */
969 goto out_free_dentry;
971 reloc_func_desc = interp_load_addr;
973 allow_write_access(interpreter);
974 fput(interpreter);
975 kfree(elf_interpreter);
976 } else {
977 elf_entry = loc->elf_ex.e_entry;
980 kfree(elf_phdata);
982 if (interpreter_type != INTERPRETER_AOUT)
983 sys_close(elf_exec_fileno);
985 set_binfmt(&elf_format);
987 compute_creds(bprm);
988 current->flags &= ~PF_FORKNOEXEC;
989 create_elf_tables(bprm, &loc->elf_ex, (interpreter_type == INTERPRETER_AOUT),
990 load_addr, interp_load_addr);
991 /* N.B. passed_fileno might not be initialized? */
992 if (interpreter_type == INTERPRETER_AOUT)
993 current->mm->arg_start += strlen(passed_fileno) + 1;
994 current->mm->end_code = end_code;
995 current->mm->start_code = start_code;
996 current->mm->start_data = start_data;
997 current->mm->end_data = end_data;
998 current->mm->start_stack = bprm->p;
1000 if (current->personality & MMAP_PAGE_ZERO) {
1001 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1002 and some applications "depend" upon this behavior.
1003 Since we do not have the power to recompile these, we
1004 emulate the SVr4 behavior. Sigh. */
1005 down_write(&current->mm->mmap_sem);
1006 error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
1007 MAP_FIXED | MAP_PRIVATE, 0);
1008 up_write(&current->mm->mmap_sem);
1011 #ifdef ELF_PLAT_INIT
1013 * The ABI may specify that certain registers be set up in special
1014 * ways (on i386 %edx is the address of a DT_FINI function, for
1015 * example. In addition, it may also specify (eg, PowerPC64 ELF)
1016 * that the e_entry field is the address of the function descriptor
1017 * for the startup routine, rather than the address of the startup
1018 * routine itself. This macro performs whatever initialization to
1019 * the regs structure is required as well as any relocations to the
1020 * function descriptor entries when executing dynamically links apps.
1022 ELF_PLAT_INIT(regs, reloc_func_desc);
1023 #endif
1025 #ifdef CONFIG_MOXA_SOFT_LOCK // add by Victor Yu. 02-09-2006
1026 if ( lock_flag ) {
1027 retval = moxa_soft_lock_desc((char *)start_data, dsize);
1028 if ( retval ) {
1029 send_sig(SIGKILL, current, 0);
1030 goto out;
1033 #endif
1034 start_thread(regs, elf_entry, bprm->p);
1035 if (unlikely(current->ptrace & PT_PTRACED)) {
1036 if (current->ptrace & PT_TRACE_EXEC)
1037 ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
1038 else
1039 send_sig(SIGTRAP, current, 0);
1041 retval = 0;
1042 out:
1043 kfree(loc);
1044 out_ret:
1045 return retval;
1047 /* error cleanup */
1048 out_free_dentry:
1049 allow_write_access(interpreter);
1050 if (interpreter)
1051 fput(interpreter);
1052 out_free_interp:
1053 if (elf_interpreter)
1054 kfree(elf_interpreter);
1055 out_free_file:
1056 sys_close(elf_exec_fileno);
1057 out_free_fh:
1058 if (files) {
1059 put_files_struct(current->files);
1060 current->files = files;
1062 out_free_ph:
1063 kfree(elf_phdata);
1064 goto out;
1067 /* This is really simpleminded and specialized - we are loading an
1068 a.out library that is given an ELF header. */
1070 static int load_elf_library(struct file *file)
1072 struct elf_phdr *elf_phdata;
1073 unsigned long elf_bss, bss, len;
1074 int retval, error, i, j;
1075 struct elfhdr elf_ex;
1077 error = -ENOEXEC;
1078 retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
1079 if (retval != sizeof(elf_ex))
1080 goto out;
1082 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
1083 goto out;
1085 /* First of all, some simple consistency checks */
1086 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
1087 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
1088 goto out;
1090 /* Now read in all of the header information */
1092 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
1093 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1095 error = -ENOMEM;
1096 elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
1097 if (!elf_phdata)
1098 goto out;
1100 error = -ENOEXEC;
1101 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata, j);
1102 if (retval != j)
1103 goto out_free_ph;
1105 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
1106 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
1107 if (j != 1)
1108 goto out_free_ph;
1110 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
1112 /* Now use mmap to map the library into memory. */
1113 down_write(&current->mm->mmap_sem);
1114 error = do_mmap(file,
1115 ELF_PAGESTART(elf_phdata->p_vaddr),
1116 (elf_phdata->p_filesz +
1117 ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
1118 PROT_READ | PROT_WRITE | PROT_EXEC,
1119 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
1120 (elf_phdata->p_offset -
1121 ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
1122 up_write(&current->mm->mmap_sem);
1123 if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
1124 goto out_free_ph;
1126 elf_bss = elf_phdata->p_vaddr + elf_phdata->p_filesz;
1127 padzero(elf_bss);
1129 len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr + ELF_MIN_ALIGN - 1);
1130 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
1131 if (bss > len)
1132 do_brk(len, bss - len);
1133 error = 0;
1135 out_free_ph:
1136 kfree(elf_phdata);
1137 out:
1138 return error;
1142 * Note that some platforms still use traditional core dumps and not
1143 * the ELF core dump. Each platform can select it as appropriate.
1145 #ifdef USE_ELF_CORE_DUMP
1148 * ELF core dumper
1150 * Modelled on fs/exec.c:aout_core_dump()
1151 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1154 * These are the only things you should do on a core-file: use only these
1155 * functions to write out all the necessary info.
1157 static int dump_write(struct file *file, const void *addr, int nr)
1159 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1162 static int dump_seek(struct file *file, off_t off)
1164 if (file->f_op->llseek) {
1165 if (file->f_op->llseek(file, off, 0) != off)
1166 return 0;
1167 } else
1168 file->f_pos = off;
1169 return 1;
1173 * Decide whether a segment is worth dumping; default is yes to be
1174 * sure (missing info is worse than too much; etc).
1175 * Personally I'd include everything, and use the coredump limit...
1177 * I think we should skip something. But I am not sure how. H.J.
1179 static int maydump(struct vm_area_struct *vma)
1182 * If we may not read the contents, don't allow us to dump
1183 * them either. "dump_write()" can't handle it anyway.
1185 if (!(vma->vm_flags & VM_READ))
1186 return 0;
1188 /* Do not dump I/O mapped devices! -DaveM */
1189 if (vma->vm_flags & VM_IO)
1190 return 0;
1191 #if 1
1192 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
1193 return 1;
1194 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
1195 return 0;
1196 #endif
1197 return 1;
1200 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
1202 /* An ELF note in memory */
1203 struct memelfnote
1205 const char *name;
1206 int type;
1207 unsigned int datasz;
1208 void *data;
1211 static int notesize(struct memelfnote *en)
1213 int sz;
1215 sz = sizeof(struct elf_note);
1216 sz += roundup(strlen(en->name) + 1, 4);
1217 sz += roundup(en->datasz, 4);
1219 return sz;
1222 #define DUMP_WRITE(addr, nr) \
1223 do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
1224 #define DUMP_SEEK(off) \
1225 do { if (!dump_seek(file, (off))) return 0; } while(0)
1227 static int writenote(struct memelfnote *men, struct file *file)
1229 struct elf_note en;
1231 en.n_namesz = strlen(men->name) + 1;
1232 en.n_descsz = men->datasz;
1233 en.n_type = men->type;
1235 DUMP_WRITE(&en, sizeof(en));
1236 DUMP_WRITE(men->name, en.n_namesz);
1237 /* XXX - cast from long long to long to avoid need for libgcc.a */
1238 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1239 DUMP_WRITE(men->data, men->datasz);
1240 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1242 return 1;
1244 #undef DUMP_WRITE
1245 #undef DUMP_SEEK
1247 #define DUMP_WRITE(addr, nr) \
1248 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1249 goto end_coredump;
1250 #define DUMP_SEEK(off) \
1251 if (!dump_seek(file, (off))) \
1252 goto end_coredump;
1254 static inline void fill_elf_header(struct elfhdr *elf, int segs)
1256 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1257 elf->e_ident[EI_CLASS] = ELF_CLASS;
1258 elf->e_ident[EI_DATA] = ELF_DATA;
1259 elf->e_ident[EI_VERSION] = EV_CURRENT;
1260 elf->e_ident[EI_OSABI] = ELF_OSABI;
1261 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1263 elf->e_type = ET_CORE;
1264 elf->e_machine = ELF_ARCH;
1265 elf->e_version = EV_CURRENT;
1266 elf->e_entry = 0;
1267 elf->e_phoff = sizeof(struct elfhdr);
1268 elf->e_shoff = 0;
1269 elf->e_flags = 0;
1270 elf->e_ehsize = sizeof(struct elfhdr);
1271 elf->e_phentsize = sizeof(struct elf_phdr);
1272 elf->e_phnum = segs;
1273 elf->e_shentsize = 0;
1274 elf->e_shnum = 0;
1275 elf->e_shstrndx = 0;
1276 return;
1279 static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
1281 phdr->p_type = PT_NOTE;
1282 phdr->p_offset = offset;
1283 phdr->p_vaddr = 0;
1284 phdr->p_paddr = 0;
1285 phdr->p_filesz = sz;
1286 phdr->p_memsz = 0;
1287 phdr->p_flags = 0;
1288 phdr->p_align = 0;
1289 return;
1292 static void fill_note(struct memelfnote *note, const char *name, int type,
1293 unsigned int sz, void *data)
1295 note->name = name;
1296 note->type = type;
1297 note->datasz = sz;
1298 note->data = data;
1299 return;
1303 * fill up all the fields in prstatus from the given task struct, except registers
1304 * which need to be filled up separately.
1306 static void fill_prstatus(struct elf_prstatus *prstatus,
1307 struct task_struct *p, long signr)
1309 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1310 prstatus->pr_sigpend = p->pending.signal.sig[0];
1311 prstatus->pr_sighold = p->blocked.sig[0];
1312 prstatus->pr_pid = p->pid;
1313 prstatus->pr_ppid = p->parent->pid;
1314 prstatus->pr_pgrp = process_group(p);
1315 prstatus->pr_sid = p->signal->session;
1316 if (p->pid == p->tgid) {
1318 * This is the record for the group leader. Add in the
1319 * cumulative times of previous dead threads. This total
1320 * won't include the time of each live thread whose state
1321 * is included in the core dump. The final total reported
1322 * to our parent process when it calls wait4 will include
1323 * those sums as well as the little bit more time it takes
1324 * this and each other thread to finish dying after the
1325 * core dump synchronization phase.
1327 jiffies_to_timeval(p->utime + p->signal->utime,
1328 &prstatus->pr_utime);
1329 jiffies_to_timeval(p->stime + p->signal->stime,
1330 &prstatus->pr_stime);
1331 } else {
1332 jiffies_to_timeval(p->utime, &prstatus->pr_utime);
1333 jiffies_to_timeval(p->stime, &prstatus->pr_stime);
1335 jiffies_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
1336 jiffies_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
1339 static void fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1340 struct mm_struct *mm)
1342 int i, len;
1344 /* first copy the parameters from user space */
1345 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1347 len = mm->arg_end - mm->arg_start;
1348 if (len >= ELF_PRARGSZ)
1349 len = ELF_PRARGSZ-1;
1350 copy_from_user(&psinfo->pr_psargs,
1351 (const char __user *)mm->arg_start, len);
1352 for(i = 0; i < len; i++)
1353 if (psinfo->pr_psargs[i] == 0)
1354 psinfo->pr_psargs[i] = ' ';
1355 psinfo->pr_psargs[len] = 0;
1357 psinfo->pr_pid = p->pid;
1358 psinfo->pr_ppid = p->parent->pid;
1359 psinfo->pr_pgrp = process_group(p);
1360 psinfo->pr_sid = p->signal->session;
1362 i = p->state ? ffz(~p->state) + 1 : 0;
1363 psinfo->pr_state = i;
1364 psinfo->pr_sname = (i < 0 || i > 5) ? '.' : "RSDTZW"[i];
1365 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1366 psinfo->pr_nice = task_nice(p);
1367 psinfo->pr_flag = p->flags;
1368 SET_UID(psinfo->pr_uid, p->uid);
1369 SET_GID(psinfo->pr_gid, p->gid);
1370 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1372 return;
1375 /* Here is the structure in which status of each thread is captured. */
1376 struct elf_thread_status
1378 struct list_head list;
1379 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1380 elf_fpregset_t fpu; /* NT_PRFPREG */
1381 struct task_struct *thread;
1382 #ifdef ELF_CORE_COPY_XFPREGS
1383 elf_fpxregset_t xfpu; /* NT_PRXFPREG */
1384 #endif
1385 struct memelfnote notes[3];
1386 int num_notes;
1390 * In order to add the specific thread information for the elf file format,
1391 * we need to keep a linked list of every threads pr_status and then
1392 * create a single section for them in the final core file.
1394 static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1396 int sz = 0;
1397 struct task_struct *p = t->thread;
1398 t->num_notes = 0;
1400 fill_prstatus(&t->prstatus, p, signr);
1401 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1403 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus), &(t->prstatus));
1404 t->num_notes++;
1405 sz += notesize(&t->notes[0]);
1407 if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, &t->fpu))) {
1408 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu), &(t->fpu));
1409 t->num_notes++;
1410 sz += notesize(&t->notes[1]);
1413 #ifdef ELF_CORE_COPY_XFPREGS
1414 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
1415 fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu), &t->xfpu);
1416 t->num_notes++;
1417 sz += notesize(&t->notes[2]);
1419 #endif
1420 return sz;
1424 * Actual dumper
1426 * This is a two-pass process; first we find the offsets of the bits,
1427 * and then they are actually written out. If we run out of core limit
1428 * we just truncate.
1430 static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
1432 #define NUM_NOTES 6
1433 int has_dumped = 0;
1434 mm_segment_t fs;
1435 int segs;
1436 size_t size = 0;
1437 int i;
1438 struct vm_area_struct *vma;
1439 struct elfhdr *elf = NULL;
1440 off_t offset = 0, dataoff;
1441 unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1442 int numnote;
1443 struct memelfnote *notes = NULL;
1444 struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
1445 struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
1446 struct task_struct *g, *p;
1447 LIST_HEAD(thread_list);
1448 struct list_head *t;
1449 elf_fpregset_t *fpu = NULL;
1450 #ifdef ELF_CORE_COPY_XFPREGS
1451 elf_fpxregset_t *xfpu = NULL;
1452 #endif
1453 int thread_status_size = 0;
1454 elf_addr_t *auxv;
1457 * We no longer stop all VM operations.
1459 * This is because those proceses that could possibly change map_count or
1460 * the mmap / vma pages are now blocked in do_exit on current finishing
1461 * this core dump.
1463 * Only ptrace can touch these memory addresses, but it doesn't change
1464 * the map_count or the pages allocated. So no possibility of crashing
1465 * exists while dumping the mm->vm_next areas to the core file.
1468 /* alloc memory for large data structures: too large to be on stack */
1469 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1470 if (!elf)
1471 goto cleanup;
1472 prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
1473 if (!prstatus)
1474 goto cleanup;
1475 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1476 if (!psinfo)
1477 goto cleanup;
1478 notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1479 if (!notes)
1480 goto cleanup;
1481 fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1482 if (!fpu)
1483 goto cleanup;
1484 #ifdef ELF_CORE_COPY_XFPREGS
1485 xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1486 if (!xfpu)
1487 goto cleanup;
1488 #endif
1490 if (signr) {
1491 struct elf_thread_status *tmp;
1492 read_lock(&tasklist_lock);
1493 do_each_thread(g,p)
1494 if (current->mm == p->mm && current != p) {
1495 tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC);
1496 if (!tmp) {
1497 read_unlock(&tasklist_lock);
1498 goto cleanup;
1500 memset(tmp, 0, sizeof(*tmp));
1501 INIT_LIST_HEAD(&tmp->list);
1502 tmp->thread = p;
1503 list_add(&tmp->list, &thread_list);
1505 while_each_thread(g,p);
1506 read_unlock(&tasklist_lock);
1507 list_for_each(t, &thread_list) {
1508 struct elf_thread_status *tmp;
1509 int sz;
1511 tmp = list_entry(t, struct elf_thread_status, list);
1512 sz = elf_dump_thread_status(signr, tmp);
1513 thread_status_size += sz;
1516 /* now collect the dump for the current */
1517 memset(prstatus, 0, sizeof(*prstatus));
1518 fill_prstatus(prstatus, current, signr);
1519 elf_core_copy_regs(&prstatus->pr_reg, regs);
1521 segs = current->mm->map_count;
1522 #ifdef ELF_CORE_EXTRA_PHDRS
1523 segs += ELF_CORE_EXTRA_PHDRS;
1524 #endif
1526 /* Set up header */
1527 fill_elf_header(elf, segs+1); /* including notes section */
1529 has_dumped = 1;
1530 current->flags |= PF_DUMPCORE;
1533 * Set up the notes in similar form to SVR4 core dumps made
1534 * with info from their /proc.
1537 fill_note(notes +0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
1539 fill_psinfo(psinfo, current->group_leader, current->mm);
1540 fill_note(notes +1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1542 fill_note(notes +2, "CORE", NT_TASKSTRUCT, sizeof(*current), current);
1544 numnote = 3;
1546 auxv = (elf_addr_t *) current->mm->saved_auxv;
1548 i = 0;
1550 i += 2;
1551 while (auxv[i - 2] != AT_NULL);
1552 fill_note(&notes[numnote++], "CORE", NT_AUXV,
1553 i * sizeof (elf_addr_t), auxv);
1555 /* Try to dump the FPU. */
1556 if ((prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, regs, fpu)))
1557 fill_note(notes + numnote++,
1558 "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1559 #ifdef ELF_CORE_COPY_XFPREGS
1560 if (elf_core_copy_task_xfpregs(current, xfpu))
1561 fill_note(notes + numnote++,
1562 "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
1563 #endif
1565 fs = get_fs();
1566 set_fs(KERNEL_DS);
1568 DUMP_WRITE(elf, sizeof(*elf));
1569 offset += sizeof(*elf); /* Elf header */
1570 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
1572 /* Write notes phdr entry */
1574 struct elf_phdr phdr;
1575 int sz = 0;
1577 for (i = 0; i < numnote; i++)
1578 sz += notesize(notes + i);
1580 sz += thread_status_size;
1582 fill_elf_note_phdr(&phdr, sz, offset);
1583 offset += sz;
1584 DUMP_WRITE(&phdr, sizeof(phdr));
1587 /* Page-align dumped data */
1588 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1590 /* Write program headers for segments dump */
1591 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1592 struct elf_phdr phdr;
1593 size_t sz;
1595 sz = vma->vm_end - vma->vm_start;
1597 phdr.p_type = PT_LOAD;
1598 phdr.p_offset = offset;
1599 phdr.p_vaddr = vma->vm_start;
1600 phdr.p_paddr = 0;
1601 phdr.p_filesz = maydump(vma) ? sz : 0;
1602 phdr.p_memsz = sz;
1603 offset += phdr.p_filesz;
1604 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1605 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1606 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1607 phdr.p_align = ELF_EXEC_PAGESIZE;
1609 DUMP_WRITE(&phdr, sizeof(phdr));
1612 #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1613 ELF_CORE_WRITE_EXTRA_PHDRS;
1614 #endif
1616 /* write out the notes section */
1617 for (i = 0; i < numnote; i++)
1618 if (!writenote(notes + i, file))
1619 goto end_coredump;
1621 /* write out the thread status notes section */
1622 list_for_each(t, &thread_list) {
1623 struct elf_thread_status *tmp = list_entry(t, struct elf_thread_status, list);
1624 for (i = 0; i < tmp->num_notes; i++)
1625 if (!writenote(&tmp->notes[i], file))
1626 goto end_coredump;
1629 DUMP_SEEK(dataoff);
1631 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1632 unsigned long addr;
1634 if (!maydump(vma))
1635 continue;
1637 for (addr = vma->vm_start;
1638 addr < vma->vm_end;
1639 addr += PAGE_SIZE) {
1640 struct page* page;
1641 struct vm_area_struct *vma;
1643 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1644 &page, &vma) <= 0) {
1645 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1646 } else {
1647 if (page == ZERO_PAGE(addr)) {
1648 DUMP_SEEK (file->f_pos + PAGE_SIZE);
1649 } else {
1650 void *kaddr;
1651 flush_cache_page(vma, addr);
1652 kaddr = kmap(page);
1653 if ((size += PAGE_SIZE) > limit ||
1654 !dump_write(file, kaddr,
1655 PAGE_SIZE)) {
1656 kunmap(page);
1657 page_cache_release(page);
1658 goto end_coredump;
1660 kunmap(page);
1662 page_cache_release(page);
1667 #ifdef ELF_CORE_WRITE_EXTRA_DATA
1668 ELF_CORE_WRITE_EXTRA_DATA;
1669 #endif
1671 if ((off_t) file->f_pos != offset) {
1672 /* Sanity check */
1673 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1674 (off_t) file->f_pos, offset);
1677 end_coredump:
1678 set_fs(fs);
1680 cleanup:
1681 while(!list_empty(&thread_list)) {
1682 struct list_head *tmp = thread_list.next;
1683 list_del(tmp);
1684 kfree(list_entry(tmp, struct elf_thread_status, list));
1687 kfree(elf);
1688 kfree(prstatus);
1689 kfree(psinfo);
1690 kfree(notes);
1691 kfree(fpu);
1692 #ifdef ELF_CORE_COPY_XFPREGS
1693 kfree(xfpu);
1694 #endif
1695 return has_dumped;
1696 #undef NUM_NOTES
1699 #endif /* USE_ELF_CORE_DUMP */
1701 static int __init init_elf_binfmt(void)
1703 return register_binfmt(&elf_format);
1706 static void __exit exit_elf_binfmt(void)
1708 /* Remove the COFF and ELF loaders. */
1709 unregister_binfmt(&elf_format);
1712 core_initcall(init_elf_binfmt);
1713 module_exit(exit_elf_binfmt);
1714 MODULE_LICENSE("GPL");