[MIPS] checkfiles: Fix "need space after that ','" errors.
[linux-2.6/zen-sources.git] / arch / mips / kernel / irixelf.c
blob8ef5cf4cc42355e846eddfd65382e3e83c820137
1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
6 * irixelf.c: Code to load IRIX ELF executables conforming to the MIPS ABI.
7 * Based off of work by Eric Youngdale.
9 * Copyright (C) 1993 - 1994 Eric Youngdale <ericy@cais.com>
10 * Copyright (C) 1996 - 2004 David S. Miller <dm@engr.sgi.com>
11 * Copyright (C) 2004 - 2005 Steven J. Hill <sjhill@realitydiluted.com>
13 #undef DEBUG
15 #include <linux/module.h>
16 #include <linux/fs.h>
17 #include <linux/stat.h>
18 #include <linux/sched.h>
19 #include <linux/mm.h>
20 #include <linux/mman.h>
21 #include <linux/a.out.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/signal.h>
25 #include <linux/binfmts.h>
26 #include <linux/string.h>
27 #include <linux/file.h>
28 #include <linux/fcntl.h>
29 #include <linux/ptrace.h>
30 #include <linux/slab.h>
31 #include <linux/shm.h>
32 #include <linux/personality.h>
33 #include <linux/elfcore.h>
35 #include <asm/mipsregs.h>
36 #include <asm/namei.h>
37 #include <asm/prctl.h>
38 #include <asm/uaccess.h>
40 #define DLINFO_ITEMS 12
42 #include <linux/elf.h>
44 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs);
45 static int load_irix_library(struct file *);
46 static int irix_core_dump(long signr, struct pt_regs * regs,
47 struct file *file);
49 static struct linux_binfmt irix_format = {
50 NULL, THIS_MODULE, load_irix_binary, load_irix_library,
51 irix_core_dump, PAGE_SIZE
54 /* Debugging routines. */
55 static char *get_elf_p_type(Elf32_Word p_type)
57 #ifdef DEBUG
58 switch (p_type) {
59 case PT_NULL:
60 return "PT_NULL";
61 break;
63 case PT_LOAD:
64 return "PT_LOAD";
65 break;
67 case PT_DYNAMIC:
68 return "PT_DYNAMIC";
69 break;
71 case PT_INTERP:
72 return "PT_INTERP";
73 break;
75 case PT_NOTE:
76 return "PT_NOTE";
77 break;
79 case PT_SHLIB:
80 return "PT_SHLIB";
81 break;
83 case PT_PHDR:
84 return "PT_PHDR";
85 break;
87 case PT_LOPROC:
88 return "PT_LOPROC/REGINFO";
89 break;
91 case PT_HIPROC:
92 return "PT_HIPROC";
93 break;
95 default:
96 return "PT_BOGUS";
97 break;
99 #endif
102 static void print_elfhdr(struct elfhdr *ehp)
104 int i;
106 pr_debug("ELFHDR: e_ident<");
107 for (i = 0; i < (EI_NIDENT - 1); i++)
108 pr_debug("%x ", ehp->e_ident[i]);
109 pr_debug("%x>\n", ehp->e_ident[i]);
110 pr_debug(" e_type[%04x] e_machine[%04x] e_version[%08lx]\n",
111 (unsigned short) ehp->e_type, (unsigned short) ehp->e_machine,
112 (unsigned long) ehp->e_version);
113 pr_debug(" e_entry[%08lx] e_phoff[%08lx] e_shoff[%08lx] "
114 "e_flags[%08lx]\n",
115 (unsigned long) ehp->e_entry, (unsigned long) ehp->e_phoff,
116 (unsigned long) ehp->e_shoff, (unsigned long) ehp->e_flags);
117 pr_debug(" e_ehsize[%04x] e_phentsize[%04x] e_phnum[%04x]\n",
118 (unsigned short) ehp->e_ehsize,
119 (unsigned short) ehp->e_phentsize,
120 (unsigned short) ehp->e_phnum);
121 pr_debug(" e_shentsize[%04x] e_shnum[%04x] e_shstrndx[%04x]\n",
122 (unsigned short) ehp->e_shentsize,
123 (unsigned short) ehp->e_shnum,
124 (unsigned short) ehp->e_shstrndx);
127 static void print_phdr(int i, struct elf_phdr *ep)
129 pr_debug("PHDR[%d]: p_type[%s] p_offset[%08lx] p_vaddr[%08lx] "
130 "p_paddr[%08lx]\n", i, get_elf_p_type(ep->p_type),
131 (unsigned long) ep->p_offset, (unsigned long) ep->p_vaddr,
132 (unsigned long) ep->p_paddr);
133 pr_debug(" p_filesz[%08lx] p_memsz[%08lx] p_flags[%08lx] "
134 "p_align[%08lx]\n", (unsigned long) ep->p_filesz,
135 (unsigned long) ep->p_memsz, (unsigned long) ep->p_flags,
136 (unsigned long) ep->p_align);
139 static void dump_phdrs(struct elf_phdr *ep, int pnum)
141 int i;
143 for (i = 0; i < pnum; i++, ep++) {
144 if ((ep->p_type == PT_LOAD) ||
145 (ep->p_type == PT_INTERP) ||
146 (ep->p_type == PT_PHDR))
147 print_phdr(i, ep);
151 static void set_brk(unsigned long start, unsigned long end)
153 start = PAGE_ALIGN(start);
154 end = PAGE_ALIGN(end);
155 if (end <= start)
156 return;
157 down_write(&current->mm->mmap_sem);
158 do_brk(start, end - start);
159 up_write(&current->mm->mmap_sem);
163 /* We need to explicitly zero any fractional pages
164 * after the data section (i.e. bss). This would
165 * contain the junk from the file that should not
166 * be in memory.
168 static void padzero(unsigned long elf_bss)
170 unsigned long nbyte;
172 nbyte = elf_bss & (PAGE_SIZE-1);
173 if (nbyte) {
174 nbyte = PAGE_SIZE - nbyte;
175 clear_user((void __user *) elf_bss, nbyte);
179 static unsigned long * create_irix_tables(char * p, int argc, int envc,
180 struct elfhdr * exec, unsigned int load_addr,
181 unsigned int interp_load_addr, struct pt_regs *regs,
182 struct elf_phdr *ephdr)
184 elf_addr_t *argv;
185 elf_addr_t *envp;
186 elf_addr_t *sp, *csp;
188 pr_debug("create_irix_tables: p[%p] argc[%d] envc[%d] "
189 "load_addr[%08x] interp_load_addr[%08x]\n",
190 p, argc, envc, load_addr, interp_load_addr);
192 sp = (elf_addr_t *) (~15UL & (unsigned long) p);
193 csp = sp;
194 csp -= exec ? DLINFO_ITEMS*2 : 2;
195 csp -= envc+1;
196 csp -= argc+1;
197 csp -= 1; /* argc itself */
198 if ((unsigned long)csp & 15UL) {
199 sp -= (16UL - ((unsigned long)csp & 15UL)) / sizeof(*sp);
203 * Put the ELF interpreter info on the stack
205 #define NEW_AUX_ENT(nr, id, val) \
206 __put_user((id), sp+(nr*2)); \
207 __put_user((val), sp+(nr*2+1)); \
209 sp -= 2;
210 NEW_AUX_ENT(0, AT_NULL, 0);
212 if (exec) {
213 sp -= 11*2;
215 NEW_AUX_ENT(0, AT_PHDR, load_addr + exec->e_phoff);
216 NEW_AUX_ENT(1, AT_PHENT, sizeof(struct elf_phdr));
217 NEW_AUX_ENT(2, AT_PHNUM, exec->e_phnum);
218 NEW_AUX_ENT(3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
219 NEW_AUX_ENT(4, AT_BASE, interp_load_addr);
220 NEW_AUX_ENT(5, AT_FLAGS, 0);
221 NEW_AUX_ENT(6, AT_ENTRY, (elf_addr_t) exec->e_entry);
222 NEW_AUX_ENT(7, AT_UID, (elf_addr_t) current->uid);
223 NEW_AUX_ENT(8, AT_EUID, (elf_addr_t) current->euid);
224 NEW_AUX_ENT(9, AT_GID, (elf_addr_t) current->gid);
225 NEW_AUX_ENT(10, AT_EGID, (elf_addr_t) current->egid);
227 #undef NEW_AUX_ENT
229 sp -= envc+1;
230 envp = sp;
231 sp -= argc+1;
232 argv = sp;
234 __put_user((elf_addr_t)argc, --sp);
235 current->mm->arg_start = (unsigned long) p;
236 while (argc-->0) {
237 __put_user((unsigned long)p, argv++);
238 p += strlen_user(p);
240 __put_user((unsigned long) NULL, argv);
241 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
242 while (envc-->0) {
243 __put_user((unsigned long)p, envp++);
244 p += strlen_user(p);
246 __put_user((unsigned long) NULL, envp);
247 current->mm->env_end = (unsigned long) p;
248 return sp;
252 /* This is much more generalized than the library routine read function,
253 * so we keep this separate. Technically the library read function
254 * is only provided so that we can read a.out libraries that have
255 * an ELF header.
257 static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
258 struct file * interpreter,
259 unsigned int *interp_load_addr)
261 struct elf_phdr *elf_phdata = NULL;
262 struct elf_phdr *eppnt;
263 unsigned int len;
264 unsigned int load_addr;
265 int elf_bss;
266 int retval;
267 unsigned int last_bss;
268 int error;
269 int i;
270 unsigned int k;
272 elf_bss = 0;
273 last_bss = 0;
274 error = load_addr = 0;
276 print_elfhdr(interp_elf_ex);
278 /* First of all, some simple consistency checks */
279 if ((interp_elf_ex->e_type != ET_EXEC &&
280 interp_elf_ex->e_type != ET_DYN) ||
281 !interpreter->f_op->mmap) {
282 printk("IRIX interp has bad e_type %d\n", interp_elf_ex->e_type);
283 return 0xffffffff;
286 /* Now read in all of the header information */
287 if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > PAGE_SIZE) {
288 printk("IRIX interp header bigger than a page (%d)\n",
289 (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum));
290 return 0xffffffff;
293 elf_phdata = kmalloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum,
294 GFP_KERNEL);
296 if (!elf_phdata) {
297 printk("Cannot kmalloc phdata for IRIX interp.\n");
298 return 0xffffffff;
301 /* If the size of this structure has changed, then punt, since
302 * we will be doing the wrong thing.
304 if (interp_elf_ex->e_phentsize != 32) {
305 printk("IRIX interp e_phentsize == %d != 32 ",
306 interp_elf_ex->e_phentsize);
307 kfree(elf_phdata);
308 return 0xffffffff;
311 retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
312 (char *) elf_phdata,
313 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
315 dump_phdrs(elf_phdata, interp_elf_ex->e_phnum);
317 eppnt = elf_phdata;
318 for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
319 if (eppnt->p_type == PT_LOAD) {
320 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
321 int elf_prot = 0;
322 unsigned long vaddr = 0;
323 if (eppnt->p_flags & PF_R)
324 elf_prot = PROT_READ;
325 if (eppnt->p_flags & PF_W)
326 elf_prot |= PROT_WRITE;
327 if (eppnt->p_flags & PF_X)
328 elf_prot |= PROT_EXEC;
329 elf_type |= MAP_FIXED;
330 vaddr = eppnt->p_vaddr;
332 pr_debug("INTERP do_mmap"
333 "(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
334 interpreter, vaddr,
335 (unsigned long)
336 (eppnt->p_filesz + (eppnt->p_vaddr & 0xfff)),
337 (unsigned long)
338 elf_prot, (unsigned long) elf_type,
339 (unsigned long)
340 (eppnt->p_offset & 0xfffff000));
342 down_write(&current->mm->mmap_sem);
343 error = do_mmap(interpreter, vaddr,
344 eppnt->p_filesz + (eppnt->p_vaddr & 0xfff),
345 elf_prot, elf_type,
346 eppnt->p_offset & 0xfffff000);
347 up_write(&current->mm->mmap_sem);
349 if (error < 0 && error > -1024) {
350 printk("Aieee IRIX interp mmap error=%d\n",
351 error);
352 break; /* Real error */
354 pr_debug("error=%08lx ", (unsigned long) error);
355 if (!load_addr && interp_elf_ex->e_type == ET_DYN) {
356 load_addr = error;
357 pr_debug("load_addr = error ");
361 * Find the end of the file mapping for this phdr, and
362 * keep track of the largest address we see for this.
364 k = eppnt->p_vaddr + eppnt->p_filesz;
365 if (k > elf_bss)
366 elf_bss = k;
368 /* Do the same thing for the memory mapping - between
369 * elf_bss and last_bss is the bss section.
371 k = eppnt->p_memsz + eppnt->p_vaddr;
372 if (k > last_bss)
373 last_bss = k;
374 pr_debug("\n");
378 /* Now use mmap to map the library into memory. */
379 if (error < 0 && error > -1024) {
380 pr_debug("got error %d\n", error);
381 kfree(elf_phdata);
382 return 0xffffffff;
385 /* Now fill out the bss section. First pad the last page up
386 * to the page boundary, and then perform a mmap to make sure
387 * that there are zero-mapped pages up to and including the
388 * last bss page.
390 pr_debug("padzero(%08lx) ", (unsigned long) (elf_bss));
391 padzero(elf_bss);
392 len = (elf_bss + 0xfff) & 0xfffff000; /* What we have mapped so far */
394 pr_debug("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss,
395 (unsigned long) len);
397 /* Map the last of the bss segment */
398 if (last_bss > len) {
399 down_write(&current->mm->mmap_sem);
400 do_brk(len, (last_bss - len));
401 up_write(&current->mm->mmap_sem);
403 kfree(elf_phdata);
405 *interp_load_addr = load_addr;
406 return ((unsigned int) interp_elf_ex->e_entry);
409 /* Check sanity of IRIX elf executable header. */
410 static int verify_binary(struct elfhdr *ehp, struct linux_binprm *bprm)
412 if (memcmp(ehp->e_ident, ELFMAG, SELFMAG) != 0)
413 return -ENOEXEC;
415 /* First of all, some simple consistency checks */
416 if ((ehp->e_type != ET_EXEC && ehp->e_type != ET_DYN) ||
417 !bprm->file->f_op->mmap) {
418 return -ENOEXEC;
421 /* XXX Don't support N32 or 64bit binaries yet because they can
422 * XXX and do execute 64 bit instructions and expect all registers
423 * XXX to be 64 bit as well. We need to make the kernel save
424 * XXX all registers as 64bits on cpu's capable of this at
425 * XXX exception time plus frob the XTLB exception vector.
427 if ((ehp->e_flags & EF_MIPS_ABI2))
428 return -ENOEXEC;
430 return 0;
434 * This is where the detailed check is performed. Irix binaries
435 * use interpreters with 'libc.so' in the name, so this function
436 * can differentiate between Linux and Irix binaries.
438 static inline int look_for_irix_interpreter(char **name,
439 struct file **interpreter,
440 struct elfhdr *interp_elf_ex,
441 struct elf_phdr *epp,
442 struct linux_binprm *bprm, int pnum)
444 int i;
445 int retval = -EINVAL;
446 struct file *file = NULL;
448 *name = NULL;
449 for (i = 0; i < pnum; i++, epp++) {
450 if (epp->p_type != PT_INTERP)
451 continue;
453 /* It is illegal to have two interpreters for one executable. */
454 if (*name != NULL)
455 goto out;
457 *name = kmalloc(epp->p_filesz + strlen(IRIX_EMUL), GFP_KERNEL);
458 if (!*name)
459 return -ENOMEM;
461 strcpy(*name, IRIX_EMUL);
462 retval = kernel_read(bprm->file, epp->p_offset, (*name + 16),
463 epp->p_filesz);
464 if (retval < 0)
465 goto out;
467 file = open_exec(*name);
468 if (IS_ERR(file)) {
469 retval = PTR_ERR(file);
470 goto out;
472 retval = kernel_read(file, 0, bprm->buf, 128);
473 if (retval < 0)
474 goto dput_and_out;
476 *interp_elf_ex = *(struct elfhdr *) bprm->buf;
478 *interpreter = file;
479 return 0;
481 dput_and_out:
482 fput(file);
483 out:
484 kfree(*name);
485 return retval;
488 static inline int verify_irix_interpreter(struct elfhdr *ihp)
490 if (memcmp(ihp->e_ident, ELFMAG, SELFMAG) != 0)
491 return -ELIBBAD;
492 return 0;
495 #define EXEC_MAP_FLAGS (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE)
497 static inline void map_executable(struct file *fp, struct elf_phdr *epp, int pnum,
498 unsigned int *estack, unsigned int *laddr,
499 unsigned int *scode, unsigned int *ebss,
500 unsigned int *ecode, unsigned int *edata,
501 unsigned int *ebrk)
503 unsigned int tmp;
504 int i, prot;
506 for (i = 0; i < pnum; i++, epp++) {
507 if (epp->p_type != PT_LOAD)
508 continue;
510 /* Map it. */
511 prot = (epp->p_flags & PF_R) ? PROT_READ : 0;
512 prot |= (epp->p_flags & PF_W) ? PROT_WRITE : 0;
513 prot |= (epp->p_flags & PF_X) ? PROT_EXEC : 0;
514 down_write(&current->mm->mmap_sem);
515 (void) do_mmap(fp, (epp->p_vaddr & 0xfffff000),
516 (epp->p_filesz + (epp->p_vaddr & 0xfff)),
517 prot, EXEC_MAP_FLAGS,
518 (epp->p_offset & 0xfffff000));
519 up_write(&current->mm->mmap_sem);
521 /* Fixup location tracking vars. */
522 if ((epp->p_vaddr & 0xfffff000) < *estack)
523 *estack = (epp->p_vaddr & 0xfffff000);
524 if (!*laddr)
525 *laddr = epp->p_vaddr - epp->p_offset;
526 if (epp->p_vaddr < *scode)
527 *scode = epp->p_vaddr;
529 tmp = epp->p_vaddr + epp->p_filesz;
530 if (tmp > *ebss)
531 *ebss = tmp;
532 if ((epp->p_flags & PF_X) && *ecode < tmp)
533 *ecode = tmp;
534 if (*edata < tmp)
535 *edata = tmp;
537 tmp = epp->p_vaddr + epp->p_memsz;
538 if (tmp > *ebrk)
539 *ebrk = tmp;
544 static inline int map_interpreter(struct elf_phdr *epp, struct elfhdr *ihp,
545 struct file *interp, unsigned int *iladdr,
546 int pnum, mm_segment_t old_fs,
547 unsigned int *eentry)
549 int i;
551 *eentry = 0xffffffff;
552 for (i = 0; i < pnum; i++, epp++) {
553 if (epp->p_type != PT_INTERP)
554 continue;
556 /* We should have fielded this error elsewhere... */
557 if (*eentry != 0xffffffff)
558 return -1;
560 set_fs(old_fs);
561 *eentry = load_irix_interp(ihp, interp, iladdr);
562 old_fs = get_fs();
563 set_fs(get_ds());
565 fput(interp);
567 if (*eentry == 0xffffffff)
568 return -1;
570 return 0;
574 * IRIX maps a page at 0x200000 that holds information about the
575 * process and the system, here we map the page and fill the
576 * structure
578 static void irix_map_prda_page(void)
580 unsigned long v;
581 struct prda *pp;
583 down_write(&current->mm->mmap_sem);
584 v = do_brk(PRDA_ADDRESS, PAGE_SIZE);
585 up_write(&current->mm->mmap_sem);
587 if (v < 0)
588 return;
590 pp = (struct prda *) v;
591 pp->prda_sys.t_pid = current->pid;
592 pp->prda_sys.t_prid = read_c0_prid();
593 pp->prda_sys.t_rpid = current->pid;
595 /* We leave the rest set to zero */
600 /* These are the functions used to load ELF style executables and shared
601 * libraries. There is no binary dependent code anywhere else.
603 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
605 struct elfhdr elf_ex, interp_elf_ex;
606 struct file *interpreter;
607 struct elf_phdr *elf_phdata, *elf_ihdr, *elf_ephdr;
608 unsigned int load_addr, elf_bss, elf_brk;
609 unsigned int elf_entry, interp_load_addr = 0;
610 unsigned int start_code, end_code, end_data, elf_stack;
611 int retval, has_interp, has_ephdr, size, i;
612 char *elf_interpreter;
613 mm_segment_t old_fs;
615 load_addr = 0;
616 has_interp = has_ephdr = 0;
617 elf_ihdr = elf_ephdr = NULL;
618 elf_ex = *((struct elfhdr *) bprm->buf);
619 retval = -ENOEXEC;
621 if (verify_binary(&elf_ex, bprm))
622 goto out;
625 * Telling -o32 static binaries from Linux and Irix apart from each
626 * other is difficult. There are 2 differences to be noted for static
627 * binaries from the 2 operating systems:
629 * 1) Irix binaries have their .text section before their .init
630 * section. Linux binaries are just the opposite.
632 * 2) Irix binaries usually have <= 12 sections and Linux
633 * binaries have > 20.
635 * We will use Method #2 since Method #1 would require us to read in
636 * the section headers which is way too much overhead. This appears
637 * to work for everything we have ran into so far. If anyone has a
638 * better method to tell the binaries apart, I'm listening.
640 if (elf_ex.e_shnum > 20)
641 goto out;
643 print_elfhdr(&elf_ex);
645 /* Now read in all of the header information */
646 size = elf_ex.e_phentsize * elf_ex.e_phnum;
647 if (size > 65536)
648 goto out;
649 elf_phdata = kmalloc(size, GFP_KERNEL);
650 if (elf_phdata == NULL) {
651 retval = -ENOMEM;
652 goto out;
655 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *)elf_phdata, size);
656 if (retval < 0)
657 goto out_free_ph;
659 dump_phdrs(elf_phdata, elf_ex.e_phnum);
661 /* Set some things for later. */
662 for (i = 0; i < elf_ex.e_phnum; i++) {
663 switch (elf_phdata[i].p_type) {
664 case PT_INTERP:
665 has_interp = 1;
666 elf_ihdr = &elf_phdata[i];
667 break;
668 case PT_PHDR:
669 has_ephdr = 1;
670 elf_ephdr = &elf_phdata[i];
671 break;
675 pr_debug("\n");
677 elf_bss = 0;
678 elf_brk = 0;
680 elf_stack = 0xffffffff;
681 elf_interpreter = NULL;
682 start_code = 0xffffffff;
683 end_code = 0;
684 end_data = 0;
687 * If we get a return value, we change the value to be ENOEXEC
688 * so that we can exit gracefully and the main binary format
689 * search loop in 'fs/exec.c' will move onto the next handler
690 * which should be the normal ELF binary handler.
692 retval = look_for_irix_interpreter(&elf_interpreter, &interpreter,
693 &interp_elf_ex, elf_phdata, bprm,
694 elf_ex.e_phnum);
695 if (retval) {
696 retval = -ENOEXEC;
697 goto out_free_file;
700 if (elf_interpreter) {
701 retval = verify_irix_interpreter(&interp_elf_ex);
702 if (retval)
703 goto out_free_interp;
706 /* OK, we are done with that, now set up the arg stuff,
707 * and then start this sucker up.
709 retval = -E2BIG;
710 if (!bprm->sh_bang && !bprm->p)
711 goto out_free_interp;
713 /* Flush all traces of the currently running executable */
714 retval = flush_old_exec(bprm);
715 if (retval)
716 goto out_free_dentry;
718 /* OK, This is the point of no return */
719 current->mm->end_data = 0;
720 current->mm->end_code = 0;
721 current->mm->mmap = NULL;
722 current->flags &= ~PF_FORKNOEXEC;
723 elf_entry = (unsigned int) elf_ex.e_entry;
725 /* Do this so that we can load the interpreter, if need be. We will
726 * change some of these later.
728 setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
729 current->mm->start_stack = bprm->p;
731 /* At this point, we assume that the image should be loaded at
732 * fixed address, not at a variable address.
734 old_fs = get_fs();
735 set_fs(get_ds());
737 map_executable(bprm->file, elf_phdata, elf_ex.e_phnum, &elf_stack,
738 &load_addr, &start_code, &elf_bss, &end_code,
739 &end_data, &elf_brk);
741 if (elf_interpreter) {
742 retval = map_interpreter(elf_phdata, &interp_elf_ex,
743 interpreter, &interp_load_addr,
744 elf_ex.e_phnum, old_fs, &elf_entry);
745 kfree(elf_interpreter);
746 if (retval) {
747 set_fs(old_fs);
748 printk("Unable to load IRIX ELF interpreter\n");
749 send_sig(SIGSEGV, current, 0);
750 retval = 0;
751 goto out_free_file;
755 set_fs(old_fs);
757 kfree(elf_phdata);
758 set_personality(PER_IRIX32);
759 set_binfmt(&irix_format);
760 compute_creds(bprm);
761 current->flags &= ~PF_FORKNOEXEC;
762 bprm->p = (unsigned long)
763 create_irix_tables((char *)bprm->p, bprm->argc, bprm->envc,
764 (elf_interpreter ? &elf_ex : NULL),
765 load_addr, interp_load_addr, regs, elf_ephdr);
766 current->mm->start_brk = current->mm->brk = elf_brk;
767 current->mm->end_code = end_code;
768 current->mm->start_code = start_code;
769 current->mm->end_data = end_data;
770 current->mm->start_stack = bprm->p;
772 /* Calling set_brk effectively mmaps the pages that we need for the
773 * bss and break sections.
775 set_brk(elf_bss, elf_brk);
778 * IRIX maps a page at 0x200000 which holds some system
779 * information. Programs depend on this.
781 irix_map_prda_page();
783 padzero(elf_bss);
785 pr_debug("(start_brk) %lx\n" , (long) current->mm->start_brk);
786 pr_debug("(end_code) %lx\n" , (long) current->mm->end_code);
787 pr_debug("(start_code) %lx\n" , (long) current->mm->start_code);
788 pr_debug("(end_data) %lx\n" , (long) current->mm->end_data);
789 pr_debug("(start_stack) %lx\n" , (long) current->mm->start_stack);
790 pr_debug("(brk) %lx\n" , (long) current->mm->brk);
792 #if 0 /* XXX No fucking way dude... */
793 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
794 * and some applications "depend" upon this behavior.
795 * Since we do not have the power to recompile these, we
796 * emulate the SVr4 behavior. Sigh.
798 down_write(&current->mm->mmap_sem);
799 (void) do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
800 MAP_FIXED | MAP_PRIVATE, 0);
801 up_write(&current->mm->mmap_sem);
802 #endif
804 start_thread(regs, elf_entry, bprm->p);
805 if (current->ptrace & PT_PTRACED)
806 send_sig(SIGTRAP, current, 0);
807 return 0;
808 out:
809 return retval;
811 out_free_dentry:
812 allow_write_access(interpreter);
813 fput(interpreter);
814 out_free_interp:
815 kfree(elf_interpreter);
816 out_free_file:
817 out_free_ph:
818 kfree(elf_phdata);
819 goto out;
822 /* This is really simpleminded and specialized - we are loading an
823 * a.out library that is given an ELF header.
825 static int load_irix_library(struct file *file)
827 struct elfhdr elf_ex;
828 struct elf_phdr *elf_phdata = NULL;
829 unsigned int len = 0;
830 int elf_bss = 0;
831 int retval;
832 unsigned int bss;
833 int error;
834 int i, j, k;
836 error = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
837 if (error != sizeof(elf_ex))
838 return -ENOEXEC;
840 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
841 return -ENOEXEC;
843 /* First of all, some simple consistency checks. */
844 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
845 !file->f_op->mmap)
846 return -ENOEXEC;
848 /* Now read in all of the header information. */
849 if (sizeof(struct elf_phdr) * elf_ex.e_phnum > PAGE_SIZE)
850 return -ENOEXEC;
852 elf_phdata = kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
853 if (elf_phdata == NULL)
854 return -ENOMEM;
856 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata,
857 sizeof(struct elf_phdr) * elf_ex.e_phnum);
859 j = 0;
860 for (i=0; i<elf_ex.e_phnum; i++)
861 if ((elf_phdata + i)->p_type == PT_LOAD) j++;
863 if (j != 1) {
864 kfree(elf_phdata);
865 return -ENOEXEC;
868 while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
870 /* Now use mmap to map the library into memory. */
871 down_write(&current->mm->mmap_sem);
872 error = do_mmap(file,
873 elf_phdata->p_vaddr & 0xfffff000,
874 elf_phdata->p_filesz + (elf_phdata->p_vaddr & 0xfff),
875 PROT_READ | PROT_WRITE | PROT_EXEC,
876 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
877 elf_phdata->p_offset & 0xfffff000);
878 up_write(&current->mm->mmap_sem);
880 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
881 if (k > elf_bss) elf_bss = k;
883 if (error != (elf_phdata->p_vaddr & 0xfffff000)) {
884 kfree(elf_phdata);
885 return error;
888 padzero(elf_bss);
890 len = (elf_phdata->p_filesz + elf_phdata->p_vaddr+ 0xfff) & 0xfffff000;
891 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
892 if (bss > len) {
893 down_write(&current->mm->mmap_sem);
894 do_brk(len, bss-len);
895 up_write(&current->mm->mmap_sem);
897 kfree(elf_phdata);
898 return 0;
901 /* Called through irix_syssgi() to map an elf image given an FD,
902 * a phdr ptr USER_PHDRP in userspace, and a count CNT telling how many
903 * phdrs there are in the USER_PHDRP array. We return the vaddr the
904 * first phdr was successfully mapped to.
906 unsigned long irix_mapelf(int fd, struct elf_phdr __user *user_phdrp, int cnt)
908 unsigned long type, vaddr, filesz, offset, flags;
909 struct elf_phdr __user *hp;
910 struct file *filp;
911 int i, retval;
913 pr_debug("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
914 fd, user_phdrp, cnt);
916 /* First get the verification out of the way. */
917 hp = user_phdrp;
918 if (!access_ok(VERIFY_READ, hp, (sizeof(struct elf_phdr) * cnt))) {
919 pr_debug("irix_mapelf: bad pointer to ELF PHDR!\n");
921 return -EFAULT;
924 dump_phdrs(user_phdrp, cnt);
926 for (i = 0; i < cnt; i++, hp++) {
927 if (__get_user(type, &hp->p_type))
928 return -EFAULT;
929 if (type != PT_LOAD) {
930 printk("irix_mapelf: One section is not PT_LOAD!\n");
931 return -ENOEXEC;
935 filp = fget(fd);
936 if (!filp)
937 return -EACCES;
938 if (!filp->f_op) {
939 printk("irix_mapelf: Bogon filp!\n");
940 fput(filp);
941 return -EACCES;
944 hp = user_phdrp;
945 for (i = 0; i < cnt; i++, hp++) {
946 int prot;
948 retval = __get_user(vaddr, &hp->p_vaddr);
949 retval |= __get_user(filesz, &hp->p_filesz);
950 retval |= __get_user(offset, &hp->p_offset);
951 retval |= __get_user(flags, &hp->p_flags);
952 if (retval)
953 return retval;
955 prot = (flags & PF_R) ? PROT_READ : 0;
956 prot |= (flags & PF_W) ? PROT_WRITE : 0;
957 prot |= (flags & PF_X) ? PROT_EXEC : 0;
959 down_write(&current->mm->mmap_sem);
960 retval = do_mmap(filp, (vaddr & 0xfffff000),
961 (filesz + (vaddr & 0xfff)),
962 prot, (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
963 (offset & 0xfffff000));
964 up_write(&current->mm->mmap_sem);
966 if (retval != (vaddr & 0xfffff000)) {
967 printk("irix_mapelf: do_mmap fails with %d!\n", retval);
968 fput(filp);
969 return retval;
973 pr_debug("irix_mapelf: Success, returning %08lx\n",
974 (unsigned long) user_phdrp->p_vaddr);
976 fput(filp);
978 if (__get_user(vaddr, &user_phdrp->p_vaddr))
979 return -EFAULT;
981 return vaddr;
985 * ELF core dumper
987 * Modelled on fs/exec.c:aout_core_dump()
988 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
991 /* These are the only things you should do on a core-file: use only these
992 * functions to write out all the necessary info.
994 static int dump_write(struct file *file, const void __user *addr, int nr)
996 return file->f_op->write(file, (const char __user *) addr, nr, &file->f_pos) == nr;
999 static int dump_seek(struct file *file, off_t off)
1001 if (file->f_op->llseek) {
1002 if (file->f_op->llseek(file, off, 0) != off)
1003 return 0;
1004 } else
1005 file->f_pos = off;
1006 return 1;
1009 /* Decide whether a segment is worth dumping; default is yes to be
1010 * sure (missing info is worse than too much; etc).
1011 * Personally I'd include everything, and use the coredump limit...
1013 * I think we should skip something. But I am not sure how. H.J.
1015 static inline int maydump(struct vm_area_struct *vma)
1017 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
1018 return 0;
1019 #if 1
1020 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
1021 return 1;
1022 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
1023 return 0;
1024 #endif
1025 return 1;
1028 /* An ELF note in memory. */
1029 struct memelfnote
1031 const char *name;
1032 int type;
1033 unsigned int datasz;
1034 void *data;
1037 static int notesize(struct memelfnote *en)
1039 int sz;
1041 sz = sizeof(struct elf_note);
1042 sz += roundup(strlen(en->name) + 1, 4);
1043 sz += roundup(en->datasz, 4);
1045 return sz;
1048 #define DUMP_WRITE(addr, nr) \
1049 if (!dump_write(file, (addr), (nr))) \
1050 goto end_coredump;
1051 #define DUMP_SEEK(off) \
1052 if (!dump_seek(file, (off))) \
1053 goto end_coredump;
1055 static int writenote(struct memelfnote *men, struct file *file)
1057 struct elf_note en;
1059 en.n_namesz = strlen(men->name) + 1;
1060 en.n_descsz = men->datasz;
1061 en.n_type = men->type;
1063 DUMP_WRITE(&en, sizeof(en));
1064 DUMP_WRITE(men->name, en.n_namesz);
1065 /* XXX - cast from long long to long to avoid need for libgcc.a */
1066 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1067 DUMP_WRITE(men->data, men->datasz);
1068 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1070 return 1;
1072 end_coredump:
1073 return 0;
1075 #undef DUMP_WRITE
1076 #undef DUMP_SEEK
1078 #define DUMP_WRITE(addr, nr) \
1079 if (!dump_write(file, (addr), (nr))) \
1080 goto end_coredump;
1081 #define DUMP_SEEK(off) \
1082 if (!dump_seek(file, (off))) \
1083 goto end_coredump;
1085 /* Actual dumper.
1087 * This is a two-pass process; first we find the offsets of the bits,
1088 * and then they are actually written out. If we run out of core limit
1089 * we just truncate.
1091 static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
1093 int has_dumped = 0;
1094 mm_segment_t fs;
1095 int segs;
1096 int i;
1097 size_t size;
1098 struct vm_area_struct *vma;
1099 struct elfhdr elf;
1100 off_t offset = 0, dataoff;
1101 int limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1102 int numnote = 3;
1103 struct memelfnote notes[3];
1104 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1105 elf_fpregset_t fpu; /* NT_PRFPREG */
1106 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1108 /* Count what's needed to dump, up to the limit of coredump size. */
1109 segs = 0;
1110 size = 0;
1111 for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1112 if (maydump(vma))
1114 int sz = vma->vm_end-vma->vm_start;
1116 if (size+sz >= limit)
1117 break;
1118 else
1119 size += sz;
1122 segs++;
1124 pr_debug("irix_core_dump: %d segs taking %d bytes\n", segs, size);
1126 /* Set up header. */
1127 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1128 elf.e_ident[EI_CLASS] = ELFCLASS32;
1129 elf.e_ident[EI_DATA] = ELFDATA2LSB;
1130 elf.e_ident[EI_VERSION] = EV_CURRENT;
1131 elf.e_ident[EI_OSABI] = ELF_OSABI;
1132 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1134 elf.e_type = ET_CORE;
1135 elf.e_machine = ELF_ARCH;
1136 elf.e_version = EV_CURRENT;
1137 elf.e_entry = 0;
1138 elf.e_phoff = sizeof(elf);
1139 elf.e_shoff = 0;
1140 elf.e_flags = 0;
1141 elf.e_ehsize = sizeof(elf);
1142 elf.e_phentsize = sizeof(struct elf_phdr);
1143 elf.e_phnum = segs+1; /* Include notes. */
1144 elf.e_shentsize = 0;
1145 elf.e_shnum = 0;
1146 elf.e_shstrndx = 0;
1148 fs = get_fs();
1149 set_fs(KERNEL_DS);
1151 has_dumped = 1;
1152 current->flags |= PF_DUMPCORE;
1154 DUMP_WRITE(&elf, sizeof(elf));
1155 offset += sizeof(elf); /* Elf header. */
1156 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers. */
1158 /* Set up the notes in similar form to SVR4 core dumps made
1159 * with info from their /proc.
1161 memset(&psinfo, 0, sizeof(psinfo));
1162 memset(&prstatus, 0, sizeof(prstatus));
1164 notes[0].name = "CORE";
1165 notes[0].type = NT_PRSTATUS;
1166 notes[0].datasz = sizeof(prstatus);
1167 notes[0].data = &prstatus;
1168 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1169 prstatus.pr_sigpend = current->pending.signal.sig[0];
1170 prstatus.pr_sighold = current->blocked.sig[0];
1171 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1172 psinfo.pr_ppid = prstatus.pr_ppid = current->parent->pid;
1173 psinfo.pr_pgrp = prstatus.pr_pgrp = process_group(current);
1174 psinfo.pr_sid = prstatus.pr_sid = process_session(current);
1175 if (current->pid == current->tgid) {
1177 * This is the record for the group leader. Add in the
1178 * cumulative times of previous dead threads. This total
1179 * won't include the time of each live thread whose state
1180 * is included in the core dump. The final total reported
1181 * to our parent process when it calls wait4 will include
1182 * those sums as well as the little bit more time it takes
1183 * this and each other thread to finish dying after the
1184 * core dump synchronization phase.
1186 jiffies_to_timeval(current->utime + current->signal->utime,
1187 &prstatus.pr_utime);
1188 jiffies_to_timeval(current->stime + current->signal->stime,
1189 &prstatus.pr_stime);
1190 } else {
1191 jiffies_to_timeval(current->utime, &prstatus.pr_utime);
1192 jiffies_to_timeval(current->stime, &prstatus.pr_stime);
1194 jiffies_to_timeval(current->signal->cutime, &prstatus.pr_cutime);
1195 jiffies_to_timeval(current->signal->cstime, &prstatus.pr_cstime);
1197 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs)) {
1198 printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
1199 "(%d)\n", sizeof(elf_gregset_t), sizeof(struct pt_regs));
1200 } else {
1201 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1204 notes[1].name = "CORE";
1205 notes[1].type = NT_PRPSINFO;
1206 notes[1].datasz = sizeof(psinfo);
1207 notes[1].data = &psinfo;
1208 i = current->state ? ffz(~current->state) + 1 : 0;
1209 psinfo.pr_state = i;
1210 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1211 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1212 psinfo.pr_nice = task_nice(current);
1213 psinfo.pr_flag = current->flags;
1214 psinfo.pr_uid = current->uid;
1215 psinfo.pr_gid = current->gid;
1217 int i, len;
1219 set_fs(fs);
1221 len = current->mm->arg_end - current->mm->arg_start;
1222 len = len >= ELF_PRARGSZ ? ELF_PRARGSZ : len;
1223 (void *) copy_from_user(&psinfo.pr_psargs,
1224 (const char __user *)current->mm->arg_start, len);
1225 for (i = 0; i < len; i++)
1226 if (psinfo.pr_psargs[i] == 0)
1227 psinfo.pr_psargs[i] = ' ';
1228 psinfo.pr_psargs[len] = 0;
1230 set_fs(KERNEL_DS);
1232 strlcpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1234 /* Try to dump the FPU. */
1235 prstatus.pr_fpvalid = dump_fpu(regs, &fpu);
1236 if (!prstatus.pr_fpvalid) {
1237 numnote--;
1238 } else {
1239 notes[2].name = "CORE";
1240 notes[2].type = NT_PRFPREG;
1241 notes[2].datasz = sizeof(fpu);
1242 notes[2].data = &fpu;
1245 /* Write notes phdr entry. */
1247 struct elf_phdr phdr;
1248 int sz = 0;
1250 for (i = 0; i < numnote; i++)
1251 sz += notesize(&notes[i]);
1253 phdr.p_type = PT_NOTE;
1254 phdr.p_offset = offset;
1255 phdr.p_vaddr = 0;
1256 phdr.p_paddr = 0;
1257 phdr.p_filesz = sz;
1258 phdr.p_memsz = 0;
1259 phdr.p_flags = 0;
1260 phdr.p_align = 0;
1262 offset += phdr.p_filesz;
1263 DUMP_WRITE(&phdr, sizeof(phdr));
1266 /* Page-align dumped data. */
1267 dataoff = offset = roundup(offset, PAGE_SIZE);
1269 /* Write program headers for segments dump. */
1270 for (vma = current->mm->mmap, i = 0;
1271 i < segs && vma != NULL; vma = vma->vm_next) {
1272 struct elf_phdr phdr;
1273 size_t sz;
1275 i++;
1277 sz = vma->vm_end - vma->vm_start;
1279 phdr.p_type = PT_LOAD;
1280 phdr.p_offset = offset;
1281 phdr.p_vaddr = vma->vm_start;
1282 phdr.p_paddr = 0;
1283 phdr.p_filesz = maydump(vma) ? sz : 0;
1284 phdr.p_memsz = sz;
1285 offset += phdr.p_filesz;
1286 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1287 if (vma->vm_flags & VM_WRITE)
1288 phdr.p_flags |= PF_W;
1289 if (vma->vm_flags & VM_EXEC)
1290 phdr.p_flags |= PF_X;
1291 phdr.p_align = PAGE_SIZE;
1293 DUMP_WRITE(&phdr, sizeof(phdr));
1296 for (i = 0; i < numnote; i++)
1297 if (!writenote(&notes[i], file))
1298 goto end_coredump;
1300 set_fs(fs);
1302 DUMP_SEEK(dataoff);
1304 for (i = 0, vma = current->mm->mmap;
1305 i < segs && vma != NULL;
1306 vma = vma->vm_next) {
1307 unsigned long addr = vma->vm_start;
1308 unsigned long len = vma->vm_end - vma->vm_start;
1310 if (!maydump(vma))
1311 continue;
1312 i++;
1313 pr_debug("elf_core_dump: writing %08lx %lx\n", addr, len);
1314 DUMP_WRITE((void __user *)addr, len);
1317 if ((off_t) file->f_pos != offset) {
1318 /* Sanity check. */
1319 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1320 (off_t) file->f_pos, offset);
1323 end_coredump:
1324 set_fs(fs);
1325 return has_dumped;
1328 static int __init init_irix_binfmt(void)
1330 extern int init_inventory(void);
1331 extern asmlinkage unsigned long sys_call_table;
1332 extern asmlinkage unsigned long sys_call_table_irix5;
1334 init_inventory();
1337 * Copy the IRIX5 syscall table (8000 bytes) into the main syscall
1338 * table. The IRIX5 calls are located by an offset of 8000 bytes
1339 * from the beginning of the main table.
1341 memcpy((void *) ((unsigned long) &sys_call_table + 8000),
1342 &sys_call_table_irix5, 8000);
1344 return register_binfmt(&irix_format);
1347 static void __exit exit_irix_binfmt(void)
1350 * Remove the Irix ELF loader.
1352 unregister_binfmt(&irix_format);
1355 module_init(init_irix_binfmt)
1356 module_exit(exit_irix_binfmt)