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