OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [fs/] [binfmt_elf.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.