OSDN Git Service

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