Version:  2.0.40 2.2.26 2.4.37 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10

Linux/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/fcntl.h>
 25 #include <linux/ptrace.h>
 26 #include <linux/malloc.h>
 27 #include <linux/shm.h>
 28 #include <linux/personality.h>
 29 #include <linux/elfcore.h>
 30 
 31 #include <asm/segment.h>
 32 #include <asm/pgtable.h>
 33 
 34 #include <linux/config.h>
 35 
 36 #define DLINFO_ITEMS 12
 37 
 38 #include <linux/elf.h>
 39 
 40 static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs);
 41 static int load_elf_library(int fd);
 42 extern int dump_fpu(struct pt_regs *, elf_fpregset_t *);
 43 extern void dump_thread(struct pt_regs *, struct user *);
 44 
 45 /*
 46  * If we don't support core dumping, then supply a NULL so we
 47  * don't even try.
 48  */
 49 #ifdef USE_ELF_CORE_DUMP
 50 static int elf_core_dump(long signr, struct pt_regs *regs);
 51 #else
 52 #define elf_core_dump   NULL
 53 #endif
 54 
 55 #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_EXEC_PAGESIZE-1))
 56 #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_EXEC_PAGESIZE-1))
 57 
 58 /* Amount to offset dynamic executables, such as glibc 2.x's ld.so when 
 59  * used from command line.
 60  *   Loading at 0x00000000 is not appropriate since it means 
 61  * null-pointer-dereferences are not caught. (and obstructs vm86 use.)
 62  *   Loading low in general is bad for ld.so, since it or the heap may 
 63  * collide with the second executable it loads.
 64  *   Loading near MMAP_SEARCH_START is even worse, since the heap will 
 65  * start after the executable, and quickly be occluded by shared 
 66  * libraries, etc.
 67  *   So we load in the top third of virtual memory.
 68  */
 69 #define DYNAMIC_BIAS  PAGE_ALIGN((TASK_SIZE/3)*2)
 70 
 71 static struct linux_binfmt elf_format =
 72 {
 73 #ifndef MODULE
 74         NULL, NULL, load_elf_binary, load_elf_library, elf_core_dump
 75 #else
 76   NULL, &mod_use_count_, load_elf_binary, load_elf_library, elf_core_dump
 77 #endif
 78 };
 79 
 80 static void set_brk(unsigned long start, unsigned long end)
 81 {
 82         start = PAGE_ALIGN(start);
 83         end = PAGE_ALIGN(end);
 84         if (end <= start)
 85                 return;
 86         do_mmap(NULL, start, end - start,
 87                 PROT_READ | PROT_WRITE | PROT_EXEC,
 88                 MAP_FIXED | MAP_PRIVATE, 0);
 89 }
 90 
 91 
 92 /* We need to explicitly zero any fractional pages
 93    after the data section (i.e. bss).  This would
 94    contain the junk from the file that should not
 95    be in memory */
 96 
 97 
 98 static void padzero(unsigned long elf_bss)
 99 {
100         unsigned long nbyte;
101         char *fpnt;
102 
103         nbyte = elf_bss & (PAGE_SIZE - 1);
104         if (nbyte) {
105                 nbyte = PAGE_SIZE - nbyte;
106                 /* FIXME: someone should investigate, why a bad binary
107                    is allowed to bring a wrong elf_bss until here,
108                    and how to react. Suffice the plain return?
109                    rossius@hrz.tu-chemnitz.de */
110                 if (verify_area(VERIFY_WRITE, (void *) elf_bss, nbyte)) {
111                         return;
112                 }
113                 fpnt = (char *) elf_bss;
114                 do {
115                         put_user(0, fpnt++);
116                 } while (--nbyte);
117         }
118 }
119 
120 unsigned long *create_elf_tables(char *p, int argc, int envc,
121                                  struct elfhdr *exec,
122                                  unsigned long load_addr,
123                                  unsigned long interp_load_addr, int ibcs)
124 {
125         unsigned long *argv, *envp, *dlinfo;
126         unsigned long *sp;
127 
128         /*
129          * Force 16 byte alignment here for generality.
130          */
131         sp = (unsigned long *) (~15UL & (unsigned long) p);
132         sp -= exec ? DLINFO_ITEMS * 2 : 2;
133         dlinfo = sp;
134         sp -= envc + 1;
135         envp = sp;
136         sp -= argc + 1;
137         argv = sp;
138         if (!ibcs) {
139                 put_user(envp, --sp);
140                 put_user(argv, --sp);
141         }
142 #define NEW_AUX_ENT(id, val) \
143           put_user ((id), dlinfo++); \
144           put_user ((val), dlinfo++)
145 
146         if (exec) {             /* Put this here for an ELF program interpreter */
147                 struct elf_phdr *eppnt;
148                 eppnt = (struct elf_phdr *) exec->e_phoff;
149 
150                 NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
151                 NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
152                 NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
153                 NEW_AUX_ENT(AT_PAGESZ, PAGE_SIZE);
154                 NEW_AUX_ENT(AT_BASE, interp_load_addr);
155                 NEW_AUX_ENT(AT_FLAGS, 0);
156                 NEW_AUX_ENT(AT_ENTRY, (unsigned long) exec->e_entry);
157                 NEW_AUX_ENT(AT_UID, (unsigned long) current->uid);
158                 NEW_AUX_ENT(AT_EUID, (unsigned long) current->euid);
159                 NEW_AUX_ENT(AT_GID, (unsigned long) current->gid);
160                 NEW_AUX_ENT(AT_EGID, (unsigned long) current->egid);
161         }
162         NEW_AUX_ENT(AT_NULL, 0);
163 #undef NEW_AUX_ENT
164         put_user((unsigned long) argc, --sp);
165         current->mm->arg_start = (unsigned long) p;
166         while (argc-- > 0) {
167                 put_user(p, argv++);
168                 while (get_user(p++))   /* nothing */
169                         ;
170         }
171         put_user(0, argv);
172         current->mm->arg_end = current->mm->env_start = (unsigned long) p;
173         while (envc-- > 0) {
174                 put_user(p, envp++);
175                 while (get_user(p++))   /* nothing */
176                         ;
177         }
178         put_user(0, envp);
179         current->mm->env_end = (unsigned long) p;
180         return sp;
181 }
182 
183 
184 /* This is much more generalized than the library routine read function,
185    so we keep this separate.  Technically the library read function
186    is only provided so that we can read a.out libraries that have
187    an ELF header */
188 
189 static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
190                                      struct inode *interpreter_inode,
191                                      unsigned long *interp_load_addr)
192 {
193         struct file *file;
194         struct elf_phdr *elf_phdata = NULL;
195         struct elf_phdr *eppnt;
196         unsigned long load_addr;
197         int load_addr_set = 0;
198         int elf_exec_fileno;
199         int retval;
200         unsigned long last_bss, elf_bss;
201         unsigned long error;
202         int i;
203 
204         elf_bss = 0;
205         last_bss = 0;
206         error = load_addr = 0;
207 
208         /* First of all, some simple consistency checks */
209         if ((interp_elf_ex->e_type != ET_EXEC &&
210              interp_elf_ex->e_type != ET_DYN) ||
211             !elf_check_arch(interp_elf_ex->e_machine) ||
212             (!interpreter_inode->i_op ||
213              !interpreter_inode->i_op->default_file_ops->mmap) ||
214             interp_elf_ex->e_phentsize != sizeof(struct elf_phdr) ||
215             interp_elf_ex->e_phnum < 1 ||
216             interp_elf_ex->e_phnum > PAGE_SIZE / sizeof(struct elf_phdr)) {
217                 return ~0UL;
218         }
219         /* Now read in all of the header information */
220 
221         elf_phdata = (struct elf_phdr *)
222             kmalloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum,
223                     GFP_KERNEL);
224         if (!elf_phdata)
225                 return ~0UL;
226 
227         retval = read_exec(interpreter_inode, interp_elf_ex->e_phoff,
228                            (char *) elf_phdata,
229                     sizeof(struct elf_phdr) * interp_elf_ex->e_phnum, 1);
230 
231         if (retval < 0) {
232                 kfree(elf_phdata);
233                 return retval;
234         }
235         elf_exec_fileno = open_inode(interpreter_inode, O_RDONLY);
236         if (elf_exec_fileno < 0) {
237                 kfree(elf_phdata);
238                 return ~0UL;
239         }
240         file = current->files->fd[elf_exec_fileno];
241 
242         eppnt = elf_phdata;
243         for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++)
244                 if (eppnt->p_type == PT_LOAD) {
245                         int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
246                         int elf_prot = 0;
247                         unsigned long vaddr = 0;
248                         unsigned long k;
249 
250                         if (eppnt->p_flags & PF_R)
251                                 elf_prot = PROT_READ;
252                         if (eppnt->p_flags & PF_W)
253                                 elf_prot |= PROT_WRITE;
254                         if (eppnt->p_flags & PF_X)
255                                 elf_prot |= PROT_EXEC;
256                         if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
257                                 elf_type |= MAP_FIXED;
258                                 vaddr = eppnt->p_vaddr;
259                         }
260                         error = do_mmap(file,
261                                         load_addr + ELF_PAGESTART(vaddr),
262                         eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr),
263                                         elf_prot,
264                                         elf_type,
265                         eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
266 
267                         if (error > -1024UL) {
268                                 /* Real error */
269                                 sys_close(elf_exec_fileno);
270                                 kfree(elf_phdata);
271                                 return ~0UL;
272                         }
273                         if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
274                                 load_addr = error;
275                                 load_addr_set = 1;
276                         }
277                         /*
278                          * Find the end of the file  mapping for this phdr, and keep
279                          * track of the largest address we see for this.
280                          */
281                         k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
282                         if (k > elf_bss)
283                                 elf_bss = k;
284 
285                         /*
286                          * Do the same thing for the memory mapping - between
287                          * elf_bss and last_bss is the bss section.
288                          */
289                         k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
290                         if (k > last_bss)
291                                 last_bss = k;
292                 }
293         /* Now use mmap to map the library into memory. */
294 
295         sys_close(elf_exec_fileno);
296 
297         /*
298          * Now fill out the bss section.  First pad the last page up
299          * to the page boundary, and then perform a mmap to make sure
300          * that there are zeromapped pages up to and including the last
301          * bss page.
302          */
303         padzero(elf_bss);
304         elf_bss = ELF_PAGESTART(elf_bss + ELF_EXEC_PAGESIZE - 1);       /* What we have mapped so far */
305 
306         /* Map the last of the bss segment */
307         if (last_bss > elf_bss)
308                 do_mmap(NULL, elf_bss, last_bss - elf_bss,
309                         PROT_READ | PROT_WRITE | PROT_EXEC,
310                         MAP_FIXED | MAP_PRIVATE, 0);
311         kfree(elf_phdata);
312 
313         *interp_load_addr = load_addr;
314         /*
315          * AUDIT: is everything deallocated properly if this happens
316          * to be ~0UL? We'd better switch to out-of-band error reporting.
317          * Also for a.out.
318          */
319         return ((unsigned long) interp_elf_ex->e_entry) + load_addr;
320 }
321 
322 static unsigned long load_aout_interp(struct exec *interp_ex,
323                                       struct inode *interpreter_inode)
324 {
325         int retval;
326         unsigned long elf_entry;
327 
328         current->mm->brk = interp_ex->a_bss +
329             (current->mm->end_data = interp_ex->a_data +
330              (current->mm->end_code = interp_ex->a_text));
331         elf_entry = interp_ex->a_entry;
332 
333 
334         if (N_MAGIC(*interp_ex) == OMAGIC) {
335                 do_mmap(NULL, 0, interp_ex->a_text + interp_ex->a_data,
336                         PROT_READ | PROT_WRITE | PROT_EXEC,
337                         MAP_FIXED | MAP_PRIVATE, 0);
338                 retval = read_exec(interpreter_inode, 32, (char *) 0,
339                                interp_ex->a_text + interp_ex->a_data, 0);
340         } else if (N_MAGIC(*interp_ex) == ZMAGIC || N_MAGIC(*interp_ex) == QMAGIC) {
341                 do_mmap(NULL, 0, interp_ex->a_text + interp_ex->a_data,
342                         PROT_READ | PROT_WRITE | PROT_EXEC,
343                         MAP_FIXED | MAP_PRIVATE, 0);
344                 retval = read_exec(interpreter_inode,
345                                    N_TXTOFF(*interp_ex),
346                                    (char *) N_TXTADDR(*interp_ex),
347                                interp_ex->a_text + interp_ex->a_data, 0);
348         } else
349                 retval = -1;
350 
351         if (retval >= 0)
352                 do_mmap(NULL, ELF_PAGESTART(interp_ex->a_text + interp_ex->a_data + ELF_EXEC_PAGESIZE - 1),
353                         interp_ex->a_bss,
354                         PROT_READ | PROT_WRITE | PROT_EXEC,
355                         MAP_FIXED | MAP_PRIVATE, 0);
356         if (retval < 0)
357                 return ~0UL;
358         return elf_entry;
359 }
360 
361 /*
362  * These are the functions used to load ELF style executables and shared
363  * libraries.  There is no binary dependent code anywhere else.
364  */
365 
366 #define INTERPRETER_NONE 0
367 #define INTERPRETER_AOUT 1
368 #define INTERPRETER_ELF 2
369 
370 
371 static inline int do_load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
372 {
373         struct elfhdr elf_ex;
374         struct elfhdr interp_elf_ex;
375         struct file *file;
376         struct exec interp_ex;
377         struct inode *interpreter_inode;
378         unsigned long load_addr;
379         int load_addr_set = 0;
380         unsigned int interpreter_type = INTERPRETER_NONE;
381         unsigned char ibcs2_interpreter;
382         int i;
383         int old_fs;
384         int error;
385         struct elf_phdr *elf_ppnt, *elf_phdata;
386         int elf_exec_fileno;
387         unsigned long elf_bss, k, elf_brk;
388         int retval, ret_namei = -1;
389         char *elf_interpreter;
390         unsigned long elf_entry, interp_load_addr = 0;
391         int status;
392         unsigned long start_code, end_code, end_data;
393         unsigned long elf_stack;
394         char passed_fileno[6];
395 
396         ibcs2_interpreter = 0;
397         status = 0;
398         load_addr = 0;
399         elf_ex = *((struct elfhdr *) bprm->buf);        /* exec-header */
400 
401         if (elf_ex.e_ident[0] != 0x7f ||
402             strncmp(&elf_ex.e_ident[1], "ELF", 3) != 0) {
403                 return -ENOEXEC;
404         }
405         /* First of all, some simple consistency checks */
406         if ((elf_ex.e_type != ET_EXEC &&
407              elf_ex.e_type != ET_DYN) ||
408             (!elf_check_arch(elf_ex.e_machine)) ||
409             (!bprm->inode->i_op || !bprm->inode->i_op->default_file_ops ||
410              !bprm->inode->i_op->default_file_ops->mmap) ||
411             elf_ex.e_phentsize != sizeof(struct elf_phdr) ||
412             elf_ex.e_phnum < 1 ||
413             elf_ex.e_phnum > 0xfff0 / sizeof(struct elf_phdr)) {
414                 return -ENOEXEC;
415         }
416         /* Now read in all of the header information */
417 
418         elf_phdata = (struct elf_phdr *) kmalloc(elf_ex.e_phentsize *
419                                              elf_ex.e_phnum, GFP_KERNEL);
420         if (elf_phdata == NULL) {
421                 return -ENOMEM;
422         }
423         retval = read_exec(bprm->inode, elf_ex.e_phoff, (char *) elf_phdata,
424                            elf_ex.e_phentsize * elf_ex.e_phnum, 1);
425         if (retval < 0) {
426                 kfree(elf_phdata);
427                 return retval;
428         }
429         elf_ppnt = elf_phdata;
430 
431         elf_bss = 0;
432         elf_brk = 0;
433 
434         elf_exec_fileno = open_inode(bprm->inode, O_RDONLY);
435 
436         if (elf_exec_fileno < 0) {
437                 kfree(elf_phdata);
438                 return elf_exec_fileno;
439         }
440         file = current->files->fd[elf_exec_fileno];
441 
442         elf_stack = ~0UL;
443         elf_interpreter = NULL;
444         start_code = ~0UL;
445         end_code = 0;
446         end_data = 0;
447 
448         for (i = 0; i < elf_ex.e_phnum; i++) {
449                 if (elf_ppnt->p_type == PT_INTERP) {
450                         if (elf_interpreter != NULL ||
451                             elf_ppnt->p_filesz < 2 ||
452                             elf_ppnt->p_filesz > PAGE_SIZE) {
453                                 iput(interpreter_inode);
454                                 kfree(elf_phdata);
455                                 kfree(elf_interpreter);
456                                 sys_close(elf_exec_fileno);
457                                 return -ENOEXEC;
458                         }
459                         /* This is the program interpreter used for
460                          * shared libraries - for now assume that this
461                          * is an a.out format binary 
462                          */
463 
464                         elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
465                                                            GFP_KERNEL);
466                         if (elf_interpreter == NULL) {
467                                 kfree(elf_phdata);
468                                 sys_close(elf_exec_fileno);
469                                 return -ENOMEM;
470                         }
471                         retval = read_exec(bprm->inode, elf_ppnt->p_offset,
472                                            elf_interpreter,
473                                            elf_ppnt->p_filesz, 1);
474                         elf_interpreter[elf_ppnt->p_filesz - 1] = 0;
475                         /* If the program interpreter is one of these two,
476                            then assume an iBCS2 image. Otherwise assume
477                            a native linux image. */
478                         if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0 ||
479                         strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0)
480                                 ibcs2_interpreter = 1;
481 #if 0
482                         printk("Using ELF interpreter %s\n", elf_interpreter);
483 #endif
484                         if (retval >= 0) {
485                                 old_fs = get_fs();      /* This could probably be optimized */
486                                 set_fs(get_ds());
487                                 retval = ret_namei = open_namei(elf_interpreter, 0, 0,
488                                                &interpreter_inode, NULL);
489                                 set_fs(old_fs);
490                         }
491                         if (retval >= 0)
492                                 retval = permission(interpreter_inode, MAY_EXEC);
493                         if (retval >= 0)
494                                 retval = S_ISREG(interpreter_inode->i_mode) ? 0 : -EACCES;
495 
496                         if (retval >= 0)
497                                 retval = read_exec(interpreter_inode, 0, bprm->buf, 128, 1);
498 
499                         if (retval >= 0) {
500                                 interp_ex = *((struct exec *) bprm->buf);       /* exec-header */
501                                 interp_elf_ex = *((struct elfhdr *) bprm->buf);         /* exec-header */
502 
503                         }
504                         if (retval < 0) {
505                                 if (ret_namei >= 0)
506                                         iput(interpreter_inode);
507                                 kfree(elf_phdata);
508                                 kfree(elf_interpreter);
509                                 sys_close(elf_exec_fileno);
510                                 return retval;
511                         }
512                 }
513                 elf_ppnt++;
514         }
515 
516         /* Some simple consistency checks for the interpreter */
517         if (elf_interpreter) {
518                 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
519 
520                 /* Now figure out which format our binary is */
521                 if ((N_MAGIC(interp_ex) != OMAGIC) &&
522                     (N_MAGIC(interp_ex) != ZMAGIC) &&
523                     (N_MAGIC(interp_ex) != QMAGIC))
524                         interpreter_type = INTERPRETER_ELF;
525 
526                 if (interp_elf_ex.e_ident[0] != 0x7f ||
527                     strncmp(&interp_elf_ex.e_ident[1], "ELF", 3) != 0)
528                         interpreter_type &= ~INTERPRETER_ELF;
529 
530                 if (!interpreter_type) {
531                         iput(interpreter_inode);
532                         kfree(elf_interpreter);
533                         kfree(elf_phdata);
534                         sys_close(elf_exec_fileno);
535                         return -ELIBBAD;
536                 }
537         }
538         /* OK, we are done with that, now set up the arg stuff,
539            and then start this sucker up */
540 
541         if (!bprm->sh_bang) {
542                 char *passed_p;
543 
544                 if (interpreter_type == INTERPRETER_AOUT) {
545                         sprintf(passed_fileno, "%d", elf_exec_fileno);
546                         passed_p = passed_fileno;
547 
548                         if (elf_interpreter) {
549                                 bprm->p = copy_strings(1, &passed_p, bprm->page, bprm->p, 2);
550                                 bprm->argc++;
551                         }
552                 }
553                 if ((long)bprm->p < 0) {
554                         if (elf_interpreter) {
555                                 kfree(elf_interpreter);
556                         }
557                         iput(interpreter_inode);
558                         kfree(elf_phdata);
559                         sys_close(elf_exec_fileno);
560                         return (long)bprm->p;
561                 }
562         }
563         if (flush_old_exec(bprm)) {
564                 iput(interpreter_inode);
565                 return -ENOMEM;
566         }
567         /* OK, This is the point of no return */
568 
569         current->mm->end_data = 0;
570         current->mm->end_code = 0;
571         current->mm->start_mmap = ELF_START_MMAP;
572         current->mm->mmap = NULL;
573 
574         /* Do this so that we can load the interpreter, if need be.  We will
575            change some of these later */
576         current->mm->rss = 0;
577         bprm->p = setup_arg_pages(bprm->p, bprm);
578         current->mm->start_stack = bprm->p;
579 
580         /* Bias dynamic executables so they are not loaded at 
581          * 0x00000000.  See comment at DYNAMIC_BIAS define.
582          */
583         if (elf_ex.e_type == ET_DYN)
584         {
585                 for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
586                         elf_ppnt->p_vaddr += DYNAMIC_BIAS;
587                 }
588                 elf_ex.e_entry += DYNAMIC_BIAS;
589         }
590 
591         elf_entry = (unsigned long) elf_ex.e_entry;
592 
593         /* Now we do a little grungy work by mmaping the ELF image into
594            the correct location in memory.  At this point, we assume that
595            the image should be loaded at fixed address, not at a variable
596            address. */
597 
598         old_fs = get_fs();
599         set_fs(get_ds());
600         for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
601                 if (elf_ppnt->p_type == PT_LOAD) {
602                         int elf_prot = 0;
603                         if (elf_ppnt->p_flags & PF_R)
604                                 elf_prot |= PROT_READ;
605                         if (elf_ppnt->p_flags & PF_W)
606                                 elf_prot |= PROT_WRITE;
607                         if (elf_ppnt->p_flags & PF_X)
608                                 elf_prot |= PROT_EXEC;
609 
610                         error = do_mmap(file,
611                                         ELF_PAGESTART(elf_ppnt->p_vaddr),
612                                         (elf_ppnt->p_filesz +
613                                       ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
614                                         elf_prot,
615                                         (MAP_FIXED | MAP_PRIVATE |
616                                          MAP_DENYWRITE | MAP_EXECUTABLE),
617                                         (elf_ppnt->p_offset -
618                                      ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
619 
620 #ifdef LOW_ELF_STACK
621                         if (ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
622                                 elf_stack = ELF_PAGESTART(elf_ppnt->p_vaddr);
623 #endif
624 
625                         if (!load_addr_set) {
626                                 load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
627                                 load_addr_set = 1;
628                         }
629                         k = elf_ppnt->p_vaddr;
630                         if (k < start_code)
631                                 start_code = k;
632                         k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
633                         if (k > elf_bss)
634                                 elf_bss = k;
635 #if 1
636                         if ((elf_ppnt->p_flags & PF_X) && end_code < k)
637 #else
638                         if (!(elf_ppnt->p_flags & PF_W) && end_code < k)
639 #endif
640                                 end_code = k;
641                         if (end_data < k)
642                                 end_data = k;
643                         k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
644                         if (k > elf_brk)
645                                 elf_brk = k;
646                 }
647         }
648         set_fs(old_fs);
649 
650         if (elf_interpreter) {
651                 if (interpreter_type & 1)
652                         elf_entry = load_aout_interp(&interp_ex,
653                                                      interpreter_inode);
654                 else if (interpreter_type & 2)
655                         elf_entry = load_elf_interp(&interp_elf_ex,
656                                                     interpreter_inode,
657                                                     &interp_load_addr);
658 
659                 iput(interpreter_inode);
660 
661                 if (elf_entry == ~0UL) {
662                         printk("Unable to load interpreter %.128s\n",
663                                 elf_interpreter);
664                         kfree(elf_interpreter);
665                         kfree(elf_phdata);
666                         send_sig(SIGSEGV, current, 0);
667                         return 0;
668                 }
669 
670                 kfree(elf_interpreter);
671         }
672         kfree(elf_phdata);
673 
674         if (interpreter_type != INTERPRETER_AOUT)
675                 sys_close(elf_exec_fileno);
676         current->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX);
677 
678         if (current->exec_domain && current->exec_domain->use_count)
679                 (*current->exec_domain->use_count)--;
680         if (current->binfmt && current->binfmt->use_count)
681                 (*current->binfmt->use_count)--;
682         current->exec_domain = lookup_exec_domain(current->personality);
683         current->binfmt = &elf_format;
684         if (current->exec_domain && current->exec_domain->use_count)
685                 (*current->exec_domain->use_count)++;
686         if (current->binfmt && current->binfmt->use_count)
687                 (*current->binfmt->use_count)++;
688 
689 #ifndef VM_STACK_FLAGS
690         current->executable = bprm->inode;
691         bprm->inode->i_count++;
692 #endif
693 #ifdef LOW_ELF_STACK
694         current->start_stack = bprm->p = elf_stack - 4;
695 #endif
696         current->suid = current->euid = current->fsuid = bprm->e_uid;
697         current->sgid = current->egid = current->fsgid = bprm->e_gid;
698         current->flags &= ~PF_FORKNOEXEC;
699         bprm->p = (unsigned long)
700             create_elf_tables((char *) bprm->p,
701                               bprm->argc,
702                               bprm->envc,
703                   (interpreter_type == INTERPRETER_ELF ? &elf_ex : NULL),
704                               load_addr,
705                               interp_load_addr,
706                          (interpreter_type == INTERPRETER_AOUT ? 0 : 1));
707         if (interpreter_type == INTERPRETER_AOUT)
708                 current->mm->arg_start += strlen(passed_fileno) + 1;
709         current->mm->start_brk = current->mm->brk = elf_brk;
710         current->mm->end_code = end_code;
711         current->mm->start_code = start_code;
712         current->mm->end_data = end_data;
713         current->mm->start_stack = bprm->p;
714 
715         /* Calling set_brk effectively mmaps the pages that we need for the bss and break
716            sections */
717         set_brk(elf_bss, elf_brk);
718 
719         padzero(elf_bss);
720 
721 #if 0
722         printk("(start_brk) %x\n", current->mm->start_brk);
723         printk("(end_code) %x\n", current->mm->end_code);
724         printk("(start_code) %x\n", current->mm->start_code);
725         printk("(end_data) %x\n", current->mm->end_data);
726         printk("(start_stack) %x\n", current->mm->start_stack);
727         printk("(brk) %x\n", current->mm->brk);
728 #endif
729 
730         if (current->personality == PER_SVR4) {
731                 /* Why this, you ask???  Well SVr4 maps page 0 as read-only,
732                    and some applications "depend" upon this behavior.
733                    Since we do not have the power to recompile these, we
734                    emulate the SVr4 behavior.  Sigh.  */
735                 error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
736                                 MAP_FIXED | MAP_PRIVATE, 0);
737         }
738 #ifdef ELF_PLAT_INIT
739         /*
740          * The ABI may specify that certain registers be set up in special
741          * ways (on i386 %edx is the address of a DT_FINI function, for
742          * example.  This macro performs whatever initialization to
743          * the regs structure is required.
744          */
745         ELF_PLAT_INIT(regs);
746 #endif
747 
748 
749         start_thread(regs, elf_entry, bprm->p);
750         if (current->flags & PF_PTRACED)
751                 send_sig(SIGTRAP, current, 0);
752         return 0;
753 }
754 
755 static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
756 {
757         int retval;
758 
759         MOD_INC_USE_COUNT;
760         retval = do_load_elf_binary(bprm, regs);
761         MOD_DEC_USE_COUNT;
762         return retval;
763 }
764 
765 /* This is really simpleminded and specialized - we are loading an
766    a.out library that is given an ELF header. */
767 
768 static inline int do_load_elf_library(int fd)
769 {
770         struct file *file;
771         struct elfhdr elf_ex;
772         struct elf_phdr *elf_phdata = NULL;
773         struct inode *inode;
774         unsigned long len;
775         int elf_bss;
776         int retval;
777         unsigned long bss;
778         int error;
779         int i, j, k;
780 
781         len = 0;
782         file = current->files->fd[fd];
783         inode = file->f_inode;
784         elf_bss = 0;
785 
786         if (!file || !file->f_op)
787                 return -EACCES;
788 
789         /* seek to the beginning of the file */
790         if (file->f_op->lseek) {
791                 if ((error = file->f_op->lseek(inode, file, 0, 0)) != 0)
792                         return -ENOEXEC;
793         } else
794                 file->f_pos = 0;
795 
796         set_fs(KERNEL_DS);
797         error = file->f_op->read(inode, file, (char *) &elf_ex, sizeof(elf_ex));
798         set_fs(USER_DS);
799         if (error != sizeof(elf_ex))
800                 return -ENOEXEC;
801 
802         if (elf_ex.e_ident[0] != 0x7f ||
803             strncmp(&elf_ex.e_ident[1], "ELF", 3) != 0)
804                 return -ENOEXEC;
805 
806         /* First of all, some simple consistency checks */
807         if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
808             !elf_check_arch(elf_ex.e_machine) ||
809             (!inode->i_op || !inode->i_op->default_file_ops->mmap))
810                 return -ENOEXEC;
811 
812         /* Now read in all of the header information */
813 
814         if (sizeof(struct elf_phdr) * elf_ex.e_phnum > PAGE_SIZE)
815                  return -ENOEXEC;
816 
817         elf_phdata = (struct elf_phdr *)
818             kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
819         if (elf_phdata == NULL)
820                 return -ENOMEM;
821 
822         retval = read_exec(inode, elf_ex.e_phoff, (char *) elf_phdata,
823                            sizeof(struct elf_phdr) * elf_ex.e_phnum, 1);
824 
825         j = 0;
826         for (i = 0; i < elf_ex.e_phnum; i++)
827                 if ((elf_phdata + i)->p_type == PT_LOAD)
828                         j++;
829 
830         if (j != 1) {
831                 kfree(elf_phdata);
832                 return -ENOEXEC;
833         }
834         while (elf_phdata->p_type != PT_LOAD)
835                 elf_phdata++;
836 
837         /* Now use mmap to map the library into memory. */
838         error = do_mmap(file,
839                         ELF_PAGESTART(elf_phdata->p_vaddr),
840                         (elf_phdata->p_filesz +
841                          ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
842                         PROT_READ | PROT_WRITE | PROT_EXEC,
843                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
844                         (elf_phdata->p_offset -
845                          ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
846 
847         k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
848         if (k > elf_bss)
849                 elf_bss = k;
850 
851         if (error != ELF_PAGESTART(elf_phdata->p_vaddr)) {
852                 kfree(elf_phdata);
853                 return error;
854         }
855         padzero(elf_bss);
856 
857         len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr + ELF_EXEC_PAGESIZE - 1);
858         bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
859         if (bss > len)
860                 do_mmap(NULL, len, bss - len,
861                         PROT_READ | PROT_WRITE | PROT_EXEC,
862                         MAP_FIXED | MAP_PRIVATE, 0);
863         kfree(elf_phdata);
864         return 0;
865 }
866 
867 static int load_elf_library(int fd)
868 {
869         int retval;
870 
871         MOD_INC_USE_COUNT;
872         retval = do_load_elf_library(fd);
873         MOD_DEC_USE_COUNT;
874         return retval;
875 }
876 
877 /*
878  * Note that some platforms still use traditional core dumps and not
879  * the ELF core dump.  Each platform can select it as appropriate.
880  */
881 #ifdef USE_ELF_CORE_DUMP
882 
883 /*
884  * ELF core dumper
885  *
886  * Modelled on fs/exec.c:aout_core_dump()
887  * Jeremy Fitzhardinge <jeremy@sw.oz.au>
888  */
889 /*
890  * These are the only things you should do on a core-file: use only these
891  * functions to write out all the necessary info.
892  */
893 static int dump_write(struct file *file, const void *addr, int nr)
894 {
895         return file->f_op->write(file->f_inode, file, addr, nr) == nr;
896 }
897 
898 static int dump_seek(struct file *file, off_t off)
899 {
900         if (file->f_op->lseek) {
901                 if (file->f_op->lseek(file->f_inode, file, off, 0) != off)
902                         return 0;
903         } else
904                 file->f_pos = off;
905         return 1;
906 }
907 
908 /*
909  * Decide whether a segment is worth dumping; default is yes to be
910  * sure (missing info is worse than too much; etc).
911  * Personally I'd include everything, and use the coredump limit...
912  *
913  * I think we should skip something. But I am not sure how. H.J.
914  */
915 static inline int maydump(struct vm_area_struct *vma)
916 {
917         if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
918                 return 0;
919 #if 1
920         if (vma->vm_flags & (VM_WRITE | VM_GROWSUP | VM_GROWSDOWN))
921                 return 1;
922         if (vma->vm_flags & (VM_READ | VM_EXEC | VM_EXECUTABLE | VM_SHARED))
923                 return 0;
924 #endif
925         return 1;
926 }
927 
928 #define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))
929 
930 /* An ELF note in memory */
931 struct memelfnote {
932         const char *name;
933         int type;
934         unsigned int datasz;
935         void *data;
936 };
937 
938 static int notesize(struct memelfnote *en)
939 {
940         int sz;
941 
942         sz = sizeof(struct elf_note);
943         sz += roundup(strlen(en->name), 4);
944         sz += roundup(en->datasz, 4);
945 
946         return sz;
947 }
948 
949 /* #define DEBUG */
950 
951 #ifdef DEBUG
952 static void dump_regs(const char *str, elf_greg_t * r)
953 {
954         int i;
955         static const char *regs[] =
956         {"ebx", "ecx", "edx", "esi", "edi", "ebp",
957          "eax", "ds", "es", "fs", "gs",
958          "orig_eax", "eip", "cs",
959          "efl", "uesp", "ss"};
960         printk("Registers: %s\n", str);
961 
962         for (i = 0; i < ELF_NGREG; i++) {
963                 unsigned long val = r[i];
964                 printk("   %-2d %-5s=%08lx %lu\n", i, regs[i], val, val);
965         }
966 }
967 
968 #endif
969 
970 #define DUMP_WRITE(addr, nr)    \
971         do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
972 #define DUMP_SEEK(off)  \
973         do { if (!dump_seek(file, (off))) return 0; } while(0)
974 
975 static int writenote(struct memelfnote *men, struct file *file)
976 {
977         struct elf_note en;
978 
979         en.n_namesz = strlen(men->name);
980         en.n_descsz = men->datasz;
981         en.n_type = men->type;
982 
983         DUMP_WRITE(&en, sizeof(en));
984         DUMP_WRITE(men->name, en.n_namesz);
985         /* XXX - cast from long long to long to avoid need for libgcc.a */
986         DUMP_SEEK(roundup((unsigned long) file->f_pos, 4));     /* XXX */
987         DUMP_WRITE(men->data, men->datasz);
988         DUMP_SEEK(roundup((unsigned long) file->f_pos, 4));     /* XXX */
989 
990         return 1;
991 }
992 #undef DUMP_WRITE
993 #undef DUMP_SEEK
994 
995 #define DUMP_WRITE(addr, nr)    \
996         if (!dump_write(&file, (addr), (nr))) \
997                 goto close_coredump;
998 #define DUMP_SEEK(off)  \
999         if (!dump_seek(&file, (off))) \
1000                 goto close_coredump;
1001 /*
1002  * Actual dumper
1003  *
1004  * This is a two-pass process; first we find the offsets of the bits,
1005  * and then they are actually written out.  If we run out of core limit
1006  * we just truncate.
1007  */
1008 static int elf_core_dump(long signr, struct pt_regs *regs)
1009 {
1010         int has_dumped = 0;
1011         struct file file;
1012         struct inode *inode;
1013         unsigned short fs;
1014         char corefile[6 + sizeof(current->comm)];
1015         int segs;
1016         int i;
1017         size_t size;
1018         struct vm_area_struct *vma;
1019         struct elfhdr elf;
1020         off_t offset = 0, dataoff;
1021         int limit = current->rlim[RLIMIT_CORE].rlim_cur;
1022         int numnote = 4;
1023         struct memelfnote notes[4];
1024         struct elf_prstatus prstatus;   /* NT_PRSTATUS */
1025         elf_fpregset_t fpu;     /* NT_PRFPREG */
1026         struct elf_prpsinfo psinfo;     /* NT_PRPSINFO */
1027 
1028         if (!current->dumpable || limit < PAGE_SIZE || current->mm->count != 1)
1029                 return 0;
1030         current->dumpable = 0;
1031 
1032 #ifndef CONFIG_BINFMT_ELF
1033         MOD_INC_USE_COUNT;
1034 #endif
1035 
1036         /* Count what's needed to dump, up to the limit of coredump size */
1037         segs = 0;
1038         size = 0;
1039         for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1040                 if (maydump(vma)) {
1041                         int sz = vma->vm_end - vma->vm_start;
1042 
1043                         if (size + sz >= limit)
1044                                 break;
1045                         else
1046                                 size += sz;
1047                 }
1048                 segs++;
1049         }
1050 #ifdef DEBUG
1051         printk("elf_core_dump: %d segs taking %d bytes\n", segs, size);
1052 #endif
1053 
1054         /* Set up header */
1055         memcpy(elf.e_ident, ELFMAG, SELFMAG);
1056         elf.e_ident[EI_CLASS] = ELF_CLASS;
1057         elf.e_ident[EI_DATA] = ELF_DATA;
1058         elf.e_ident[EI_VERSION] = EV_CURRENT;
1059         memset(elf.e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
1060 
1061         elf.e_type = ET_CORE;
1062         elf.e_machine = ELF_ARCH;
1063         elf.e_version = EV_CURRENT;
1064         elf.e_entry = 0;
1065         elf.e_phoff = sizeof(elf);
1066         elf.e_shoff = 0;
1067         elf.e_flags = 0;
1068         elf.e_ehsize = sizeof(elf);
1069         elf.e_phentsize = sizeof(struct elf_phdr);
1070         elf.e_phnum = segs + 1; /* Include notes */
1071         elf.e_shentsize = 0;
1072         elf.e_shnum = 0;
1073         elf.e_shstrndx = 0;
1074 
1075         fs = get_fs();
1076         set_fs(KERNEL_DS);
1077         memcpy(corefile, "core.", 5);
1078 #if 0
1079         memcpy(corefile + 5, current->comm, sizeof(current->comm));
1080 #else
1081         corefile[4] = '\0';
1082 #endif
1083         if (open_namei(corefile, O_CREAT | 2 | O_TRUNC, 0600, &inode, NULL)) {
1084                 inode = NULL;
1085                 goto end_coredump;
1086         }
1087         if (!S_ISREG(inode->i_mode))
1088                 goto end_coredump;
1089         if (!inode->i_op || !inode->i_op->default_file_ops)
1090                 goto end_coredump;
1091         file.f_mode = 3;
1092         file.f_flags = 0;
1093         file.f_count = 1;
1094         file.f_inode = inode;
1095         file.f_pos = 0;
1096         file.f_reada = 0;
1097         file.f_op = inode->i_op->default_file_ops;
1098         if (file.f_op->open)
1099                 if (file.f_op->open(inode, &file))
1100                         goto end_coredump;
1101         if (!file.f_op->write)
1102                 goto close_coredump;
1103         has_dumped = 1;
1104         current->flags |= PF_DUMPCORE;
1105 
1106         DUMP_WRITE(&elf, sizeof(elf));
1107         offset += sizeof(elf);  /* Elf header */
1108         offset += (segs + 1) * sizeof(struct elf_phdr);         /* Program headers */
1109 
1110         /*
1111          * Set up the notes in similar form to SVR4 core dumps made
1112          * with info from their /proc.
1113          */
1114         memset(&psinfo, 0, sizeof(psinfo));
1115         memset(&prstatus, 0, sizeof(prstatus));
1116 
1117         notes[0].name = "CORE";
1118         notes[0].type = NT_PRSTATUS;
1119         notes[0].datasz = sizeof(prstatus);
1120         notes[0].data = &prstatus;
1121         prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1122         prstatus.pr_sigpend = current->signal;
1123         prstatus.pr_sighold = current->blocked;
1124         psinfo.pr_pid = prstatus.pr_pid = current->pid;
1125         psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1126         psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1127         psinfo.pr_sid = prstatus.pr_sid = current->session;
1128         prstatus.pr_utime.tv_sec = CT_TO_SECS(current->utime);
1129         prstatus.pr_utime.tv_usec = CT_TO_USECS(current->utime);
1130         prstatus.pr_stime.tv_sec = CT_TO_SECS(current->stime);
1131         prstatus.pr_stime.tv_usec = CT_TO_USECS(current->stime);
1132         prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->cutime);
1133         prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->cutime);
1134         prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->cstime);
1135         prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->cstime);
1136 
1137         /*
1138          * This transfers the registers from regs into the standard
1139          * coredump arrangement, whatever that is.
1140          */
1141 #ifdef ELF_CORE_COPY_REGS
1142         ELF_CORE_COPY_REGS(prstatus.pr_reg, regs)
1143 #else
1144         if (sizeof(elf_gregset_t) != sizeof(struct pt_regs)) {
1145                 printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) (%d)\n",
1146                        sizeof(elf_gregset_t), sizeof(struct pt_regs));
1147         } else
1148                 *(struct pt_regs *) &prstatus.pr_reg = *regs;
1149 #endif
1150 
1151 #ifdef DEBUG
1152         dump_regs("Passed in regs", (elf_greg_t *) regs);
1153         dump_regs("prstatus regs", (elf_greg_t *) & prstatus.pr_reg);
1154 #endif
1155 
1156         notes[1].name = "CORE";
1157         notes[1].type = NT_PRPSINFO;
1158         notes[1].datasz = sizeof(psinfo);
1159         notes[1].data = &psinfo;
1160         psinfo.pr_state = current->state;
1161         psinfo.pr_sname = (current->state < 0 || current->state > 5) ? '.' : "RSDZTD"[current->state];
1162         psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1163         psinfo.pr_nice = current->priority - 15;
1164         psinfo.pr_flag = current->flags;
1165         psinfo.pr_uid = current->uid;
1166         psinfo.pr_gid = current->gid;
1167         {
1168                 int i, len;
1169 
1170                 set_fs(fs);
1171 
1172                 len = current->mm->arg_end - current->mm->arg_start;
1173                 len = (len >= ELF_PRARGSZ - 1) ? ELF_PRARGSZ - 1 : len;
1174                 memcpy_fromfs(&psinfo.pr_psargs,
1175                               (const char *) current->mm->arg_start, len);
1176                 for (i = 0; i < len; i++)
1177                         if (psinfo.pr_psargs[i] == 0)
1178                                 psinfo.pr_psargs[i] = ' ';
1179                 psinfo.pr_psargs[len] = 0;
1180 
1181                 set_fs(KERNEL_DS);
1182         }
1183         strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1184 
1185         notes[2].name = "CORE";
1186         notes[2].type = NT_TASKSTRUCT;
1187         notes[2].datasz = sizeof(*current);
1188         notes[2].data = current;
1189 
1190         /* Try to dump the fpu. */
1191         prstatus.pr_fpvalid = dump_fpu(regs, &fpu);
1192         if (!prstatus.pr_fpvalid) {
1193                 numnote--;
1194         } else {
1195                 notes[3].name = "CORE";
1196                 notes[3].type = NT_PRFPREG;
1197                 notes[3].datasz = sizeof(fpu);
1198                 notes[3].data = &fpu;
1199         }
1200 
1201         /* Write notes phdr entry */
1202         {
1203                 struct elf_phdr phdr;
1204                 int sz = 0;
1205 
1206                 for (i = 0; i < numnote; i++)
1207                         sz += notesize(&notes[i]);
1208 
1209                 phdr.p_type = PT_NOTE;
1210                 phdr.p_offset = offset;
1211                 phdr.p_vaddr = 0;
1212                 phdr.p_paddr = 0;
1213                 phdr.p_filesz = sz;
1214                 phdr.p_memsz = 0;
1215                 phdr.p_flags = 0;
1216                 phdr.p_align = 0;
1217 
1218                 offset += phdr.p_filesz;
1219                 DUMP_WRITE(&phdr, sizeof(phdr));
1220         }
1221 
1222         /* Page-align dumped data */
1223         dataoff = offset = roundup(offset, PAGE_SIZE);
1224 
1225         /* Write program headers for segments dump */
1226         for (vma = current->mm->mmap, i = 0;
1227              i < segs && vma != NULL; vma = vma->vm_next) {
1228                 struct elf_phdr phdr;
1229                 size_t sz;
1230 
1231                 i++;
1232 
1233                 sz = vma->vm_end - vma->vm_start;
1234 
1235                 phdr.p_type = PT_LOAD;
1236                 phdr.p_offset = offset;
1237                 phdr.p_vaddr = vma->vm_start;
1238                 phdr.p_paddr = 0;
1239                 phdr.p_filesz = maydump(vma) ? sz : 0;
1240                 phdr.p_memsz = sz;
1241                 offset += phdr.p_filesz;
1242                 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1243                 if (vma->vm_flags & VM_WRITE)
1244                         phdr.p_flags |= PF_W;
1245                 if (vma->vm_flags & VM_EXEC)
1246                         phdr.p_flags |= PF_X;
1247                 phdr.p_align = PAGE_SIZE;
1248 
1249                 DUMP_WRITE(&phdr, sizeof(phdr));
1250         }
1251 
1252         for (i = 0; i < numnote; i++)
1253                 if (!writenote(&notes[i], &file))
1254                         goto close_coredump;
1255 
1256         set_fs(fs);
1257 
1258         DUMP_SEEK(dataoff);
1259 
1260         for (i = 0, vma = current->mm->mmap;
1261              i < segs && vma != NULL;
1262              vma = vma->vm_next) {
1263                 unsigned long addr = vma->vm_start;
1264                 unsigned long len = vma->vm_end - vma->vm_start;
1265 
1266                 i++;
1267                 if (!maydump(vma))
1268                         continue;
1269 #ifdef DEBUG
1270                 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1271 #endif
1272                 DUMP_WRITE((void *) addr, len);
1273         }
1274 
1275         if ((off_t) file.f_pos != offset) {
1276                 /* Sanity check */
1277                 printk("elf_core_dump: file.f_pos (%ld) != offset (%ld)\n",
1278                        (off_t) file.f_pos, offset);
1279         }
1280       close_coredump:
1281         if (file.f_op->release)
1282                 file.f_op->release(inode, &file);
1283 
1284       end_coredump:
1285         set_fs(fs);
1286         iput(inode);
1287 #ifndef CONFIG_BINFMT_ELF
1288         MOD_DEC_USE_COUNT;
1289 #endif
1290         return has_dumped;
1291 }
1292 #endif                          /* USE_ELF_CORE_DUMP */
1293 
1294 int init_elf_binfmt(void)
1295 {
1296         return register_binfmt(&elf_format);
1297 }
1298 
1299 #ifdef MODULE
1300 
1301 int init_module(void)
1302 {
1303         /* Install the COFF, ELF and XOUT loaders.
1304          * N.B. We *rely* on the table being the right size with the
1305          * right number of free slots...
1306          */
1307         return init_elf_binfmt();
1308 }
1309 
1310 
1311 void cleanup_module(void)
1312 {
1313         /* Remove the COFF and ELF loaders. */
1314         unregister_binfmt(&elf_format);
1315 }
1316 
1317 #endif
1318 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us