Merge with 2.3.99-pre1.
[linux-2.6/linux-mips.git] / arch / mips / kernel / irixelf.c
blob35055ac68b9755b50afbfd13643f673738201c26
1 /* $Id: irixelf.c,v 1.26 2000/03/07 15:45:28 ralf Exp $
3 * irixelf.c: Code to load IRIX ELF executables which conform to
4 * the MIPS ABI.
6 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
8 * Based upon work which is:
9 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
12 #include <linux/module.h>
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/init.h>
22 #include <linux/signal.h>
23 #include <linux/binfmts.h>
24 #include <linux/string.h>
25 #include <linux/file.h>
26 #include <linux/fcntl.h>
27 #include <linux/ptrace.h>
28 #include <linux/malloc.h>
29 #include <linux/shm.h>
30 #include <linux/personality.h>
31 #include <linux/elfcore.h>
32 #include <linux/smp_lock.h>
34 #include <asm/uaccess.h>
35 #include <asm/pgalloc.h>
36 #include <asm/mipsregs.h>
37 #include <asm/prctl.h>
39 #define DLINFO_ITEMS 12
41 #include <linux/elf.h>
43 #undef DEBUG_ELF
45 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs);
46 static int load_irix_library(int fd);
47 static int irix_core_dump(long signr, struct pt_regs * regs,
48 struct file *file);
49 extern int dump_fpu (elf_fpregset_t *);
51 static struct linux_binfmt irix_format = {
52 NULL, THIS_MODULE, load_irix_binary, load_irix_library,
53 irix_core_dump, PAGE_SIZE
56 #ifndef elf_addr_t
57 #define elf_addr_t unsigned long
58 #define elf_caddr_t char *
59 #endif
61 #ifdef DEBUG_ELF
62 /* Debugging routines. */
63 static char *get_elf_p_type(Elf32_Word p_type)
65 int i = (int) p_type;
67 switch(i) {
68 case PT_NULL: return("PT_NULL"); break;
69 case PT_LOAD: return("PT_LOAD"); break;
70 case PT_DYNAMIC: return("PT_DYNAMIC"); break;
71 case PT_INTERP: return("PT_INTERP"); break;
72 case PT_NOTE: return("PT_NOTE"); break;
73 case PT_SHLIB: return("PT_SHLIB"); break;
74 case PT_PHDR: return("PT_PHDR"); break;
75 case PT_LOPROC: return("PT_LOPROC/REGINFO"); break;
76 case PT_HIPROC: return("PT_HIPROC"); break;
77 default: return("PT_BOGUS"); break;
81 static void print_elfhdr(struct elfhdr *ehp)
83 int i;
85 printk("ELFHDR: e_ident<");
86 for(i = 0; i < (EI_NIDENT - 1); i++) printk("%x ", ehp->e_ident[i]);
87 printk("%x>\n", ehp->e_ident[i]);
88 printk(" e_type[%04x] e_machine[%04x] e_version[%08lx]\n",
89 (unsigned short) ehp->e_type, (unsigned short) ehp->e_machine,
90 (unsigned long) ehp->e_version);
91 printk(" e_entry[%08lx] e_phoff[%08lx] e_shoff[%08lx] "
92 "e_flags[%08lx]\n",
93 (unsigned long) ehp->e_entry, (unsigned long) ehp->e_phoff,
94 (unsigned long) ehp->e_shoff, (unsigned long) ehp->e_flags);
95 printk(" e_ehsize[%04x] e_phentsize[%04x] e_phnum[%04x]\n",
96 (unsigned short) ehp->e_ehsize, (unsigned short) ehp->e_phentsize,
97 (unsigned short) ehp->e_phnum);
98 printk(" e_shentsize[%04x] e_shnum[%04x] e_shstrndx[%04x]\n",
99 (unsigned short) ehp->e_shentsize, (unsigned short) ehp->e_shnum,
100 (unsigned short) ehp->e_shstrndx);
103 static void print_phdr(int i, struct elf_phdr *ep)
105 printk("PHDR[%d]: p_type[%s] p_offset[%08lx] p_vaddr[%08lx] "
106 "p_paddr[%08lx]\n", i, get_elf_p_type(ep->p_type),
107 (unsigned long) ep->p_offset, (unsigned long) ep->p_vaddr,
108 (unsigned long) ep->p_paddr);
109 printk(" p_filesz[%08lx] p_memsz[%08lx] p_flags[%08lx] "
110 "p_align[%08lx]\n", (unsigned long) ep->p_filesz,
111 (unsigned long) ep->p_memsz, (unsigned long) ep->p_flags,
112 (unsigned long) ep->p_align);
115 static void dump_phdrs(struct elf_phdr *ep, int pnum)
117 int i;
119 for(i = 0; i < pnum; i++, ep++) {
120 if((ep->p_type == PT_LOAD) ||
121 (ep->p_type == PT_INTERP) ||
122 (ep->p_type == PT_PHDR))
123 print_phdr(i, ep);
126 #endif /* (DEBUG_ELF) */
128 static void set_brk(unsigned long start, unsigned long end)
130 start = PAGE_ALIGN(start);
131 end = PAGE_ALIGN(end);
132 if (end <= start)
133 return;
134 do_brk(start, end - start);
138 /* We need to explicitly zero any fractional pages
139 * after the data section (i.e. bss). This would
140 * contain the junk from the file that should not
141 * be in memory.
143 static void padzero(unsigned long elf_bss)
145 unsigned long nbyte;
147 nbyte = elf_bss & (PAGE_SIZE-1);
148 if (nbyte) {
149 nbyte = PAGE_SIZE - nbyte;
150 clear_user((void *) elf_bss, nbyte);
154 unsigned long * create_irix_tables(char * p, int argc, int envc,
155 struct elfhdr * exec, unsigned int load_addr,
156 unsigned int interp_load_addr,
157 struct pt_regs *regs, struct elf_phdr *ephdr)
159 elf_caddr_t *argv;
160 elf_caddr_t *envp;
161 elf_addr_t *sp, *csp;
163 #ifdef DEBUG_ELF
164 printk("create_irix_tables: p[%p] argc[%d] envc[%d] "
165 "load_addr[%08x] interp_load_addr[%08x]\n",
166 p, argc, envc, load_addr, interp_load_addr);
167 #endif
168 sp = (elf_addr_t *) (~15UL & (unsigned long) p);
169 csp = sp;
170 csp -= exec ? DLINFO_ITEMS*2 : 2;
171 csp -= envc+1;
172 csp -= argc+1;
173 csp -= 1; /* argc itself */
174 if ((unsigned long)csp & 15UL) {
175 sp -= (16UL - ((unsigned long)csp & 15UL)) / sizeof(*sp);
179 * Put the ELF interpreter info on the stack
181 #define NEW_AUX_ENT(nr, id, val) \
182 __put_user ((id), sp+(nr*2)); \
183 __put_user ((val), sp+(nr*2+1)); \
185 sp -= 2;
186 NEW_AUX_ENT(0, AT_NULL, 0);
188 if(exec) {
189 sp -= 11*2;
191 NEW_AUX_ENT (0, AT_PHDR, load_addr + exec->e_phoff);
192 NEW_AUX_ENT (1, AT_PHENT, sizeof (struct elf_phdr));
193 NEW_AUX_ENT (2, AT_PHNUM, exec->e_phnum);
194 NEW_AUX_ENT (3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
195 NEW_AUX_ENT (4, AT_BASE, interp_load_addr);
196 NEW_AUX_ENT (5, AT_FLAGS, 0);
197 NEW_AUX_ENT (6, AT_ENTRY, (elf_addr_t) exec->e_entry);
198 NEW_AUX_ENT (7, AT_UID, (elf_addr_t) current->uid);
199 NEW_AUX_ENT (8, AT_EUID, (elf_addr_t) current->euid);
200 NEW_AUX_ENT (9, AT_GID, (elf_addr_t) current->gid);
201 NEW_AUX_ENT (10, AT_EGID, (elf_addr_t) current->egid);
203 #undef NEW_AUX_ENT
205 sp -= envc+1;
206 envp = (elf_caddr_t *) sp;
207 sp -= argc+1;
208 argv = (elf_caddr_t *) sp;
210 __put_user((elf_addr_t)argc,--sp);
211 current->mm->arg_start = (unsigned long) p;
212 while (argc-->0) {
213 __put_user((elf_caddr_t)(unsigned long)p,argv++);
214 p += strlen_user(p);
216 __put_user(NULL, argv);
217 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
218 while (envc-->0) {
219 __put_user((elf_caddr_t)(unsigned long)p,envp++);
220 p += strlen_user(p);
222 __put_user(NULL, envp);
223 current->mm->env_end = (unsigned long) p;
224 return sp;
228 /* This is much more generalized than the library routine read function,
229 * so we keep this separate. Technically the library read function
230 * is only provided so that we can read a.out libraries that have
231 * an ELF header.
233 static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
234 struct dentry * interpreter_dentry,
235 unsigned int *interp_load_addr)
237 struct file * file;
238 struct elf_phdr *elf_phdata = NULL;
239 struct elf_phdr *eppnt;
240 unsigned int len;
241 unsigned int load_addr;
242 int elf_exec_fileno;
243 int elf_bss;
244 int retval;
245 unsigned int last_bss;
246 int error;
247 int i;
248 unsigned int k;
250 elf_bss = 0;
251 last_bss = 0;
252 error = load_addr = 0;
254 #ifdef DEBUG_ELF
255 print_elfhdr(interp_elf_ex);
256 #endif
258 /* First of all, some simple consistency checks */
259 if ((interp_elf_ex->e_type != ET_EXEC &&
260 interp_elf_ex->e_type != ET_DYN) ||
261 !elf_check_arch(interp_elf_ex->e_machine) ||
262 (!interpreter_dentry->d_inode->i_fop ||
263 !interpreter_dentry->d_inode->i_fop->mmap)) {
264 printk("IRIX interp has bad e_type %d\n", interp_elf_ex->e_type);
265 return 0xffffffff;
268 /* Now read in all of the header information */
269 if(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > PAGE_SIZE) {
270 printk("IRIX interp header bigger than a page (%d)\n",
271 (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum));
272 return 0xffffffff;
275 elf_phdata = (struct elf_phdr *)
276 kmalloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum,
277 GFP_KERNEL);
279 if(!elf_phdata) {
280 printk("Cannot kmalloc phdata for IRIX interp.\n");
281 return 0xffffffff;
284 /* If the size of this structure has changed, then punt, since
285 * we will be doing the wrong thing.
287 if(interp_elf_ex->e_phentsize != 32) {
288 printk("IRIX interp e_phentsize == %d != 32 ",
289 interp_elf_ex->e_phentsize);
290 kfree(elf_phdata);
291 return 0xffffffff;
294 retval = read_exec(interpreter_dentry, interp_elf_ex->e_phoff,
295 (char *) elf_phdata,
296 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum, 1);
298 #ifdef DEBUG_ELF
299 dump_phdrs(elf_phdata, interp_elf_ex->e_phnum);
300 #endif
302 elf_exec_fileno = open_dentry(interpreter_dentry, O_RDONLY);
303 if (elf_exec_fileno < 0) {
304 printk("Could not open IRIX interp inode.\n");
305 kfree(elf_phdata);
306 return 0xffffffff;
309 file = fget(elf_exec_fileno);
311 eppnt = elf_phdata;
312 for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
313 if(eppnt->p_type == PT_LOAD) {
314 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
315 int elf_prot = 0;
316 unsigned long vaddr = 0;
317 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
318 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
319 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
320 elf_type |= MAP_FIXED;
321 vaddr = eppnt->p_vaddr;
323 #ifdef DEBUG_ELF
324 printk("INTERP do_mmap(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
325 file, vaddr,
326 (unsigned long) (eppnt->p_filesz + (eppnt->p_vaddr & 0xfff)),
327 (unsigned long) elf_prot, (unsigned long) elf_type,
328 (unsigned long) (eppnt->p_offset & 0xfffff000));
329 #endif
330 error = do_mmap(file, vaddr,
331 eppnt->p_filesz + (eppnt->p_vaddr & 0xfff),
332 elf_prot, elf_type,
333 eppnt->p_offset & 0xfffff000);
335 if(error < 0 && error > -1024) {
336 printk("Aieee IRIX interp mmap error=%d\n", error);
337 break; /* Real error */
339 #ifdef DEBUG_ELF
340 printk("error=%08lx ", (unsigned long) error);
341 #endif
342 if(!load_addr && interp_elf_ex->e_type == ET_DYN) {
343 load_addr = error;
344 #ifdef DEBUG_ELF
345 printk("load_addr = error ");
346 #endif
349 /* Find the end of the file mapping for this phdr, and keep
350 * track of the largest address we see for this.
352 k = eppnt->p_vaddr + eppnt->p_filesz;
353 if(k > elf_bss) elf_bss = k;
355 /* Do the same thing for the memory mapping - between
356 * elf_bss and last_bss is the bss section.
358 k = eppnt->p_memsz + eppnt->p_vaddr;
359 if(k > last_bss) last_bss = k;
360 #ifdef DEBUG_ELF
361 printk("\n");
362 #endif
366 /* Now use mmap to map the library into memory. */
367 fput(file);
368 sys_close(elf_exec_fileno);
369 if(error < 0 && error > -1024) {
370 #ifdef DEBUG_ELF
371 printk("got error %d\n", error);
372 #endif
373 kfree(elf_phdata);
374 return 0xffffffff;
377 /* Now fill out the bss section. First pad the last page up
378 * to the page boundary, and then perform a mmap to make sure
379 * that there are zero-mapped pages up to and including the
380 * last bss page.
382 #ifdef DEBUG_ELF
383 printk("padzero(%08lx) ", (unsigned long) (elf_bss));
384 #endif
385 padzero(elf_bss);
386 len = (elf_bss + 0xfff) & 0xfffff000; /* What we have mapped so far */
388 #ifdef DEBUG_ELF
389 printk("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss,
390 (unsigned long) len);
391 #endif
393 /* Map the last of the bss segment */
394 if (last_bss > len) {
395 do_brk(len, (last_bss - len));
397 kfree(elf_phdata);
399 *interp_load_addr = load_addr;
400 return ((unsigned int) interp_elf_ex->e_entry);
403 /* Check sanity of IRIX elf executable header. */
404 static int verify_binary(struct elfhdr *ehp, struct linux_binprm *bprm)
406 if (memcmp(ehp->e_ident, ELFMAG, SELFMAG) != 0)
407 return -ENOEXEC;
409 /* First of all, some simple consistency checks */
410 if((ehp->e_type != ET_EXEC && ehp->e_type != ET_DYN) ||
411 !elf_check_arch(ehp->e_machine) ||
412 (!bprm->dentry->d_inode->i_fop ||
413 !bprm->dentry->d_inode->i_fop->mmap)) {
414 return -ENOEXEC;
417 /* Only support MIPS ARCH2 or greater IRIX binaries for now. */
418 if(!(ehp->e_flags & EF_MIPS_ARCH) && !(ehp->e_flags & 0x04)) {
419 return -ENOEXEC;
422 /* XXX Don't support N32 or 64bit binaries yet because they can
423 * XXX and do execute 64 bit instructions and expect all registers
424 * XXX to be 64 bit as well. We need to make the kernel save
425 * XXX all registers as 64bits on cpu's capable of this at
426 * XXX exception time plus frob the XTLB exception vector.
428 if((ehp->e_flags & 0x20)) {
429 return -ENOEXEC;
432 return 0; /* It's ok. */
435 #define IRIX_INTERP_PREFIX "/usr/gnemul/irix"
437 /* Look for an IRIX ELF interpreter. */
438 static inline int look_for_irix_interpreter(char **name,
439 struct dentry **interpreter_dentry,
440 struct elfhdr *interp_elf_ex,
441 struct elf_phdr *epp,
442 struct linux_binprm *bprm, int pnum)
444 mm_segment_t old_fs;
445 int i;
446 int retval = -EINVAL;
447 struct dentry *dentry = NULL;
449 *name = NULL;
450 for(i = 0; i < pnum; i++, epp++) {
451 if (epp->p_type != PT_INTERP)
452 continue;
454 /* It is illegal to have two interpreters for one executable. */
455 if (*name != NULL)
456 goto out;
458 *name = (char *) kmalloc((epp->p_filesz +
459 strlen(IRIX_INTERP_PREFIX)),
460 GFP_KERNEL);
461 if (!*name)
462 return -ENOMEM;
464 strcpy(*name, IRIX_INTERP_PREFIX);
465 retval = read_exec(bprm->dentry, epp->p_offset, (*name + 16),
466 epp->p_filesz, 1);
467 if (retval < 0)
468 goto out;
470 old_fs = get_fs(); set_fs(get_ds());
471 lock_kernel();
472 dentry = namei(*name);
473 unlock_kernel();
474 set_fs(old_fs);
475 if (IS_ERR(dentry)) {
476 retval = PTR_ERR(dentry);
477 goto out;
479 retval = read_exec(dentry, 0, bprm->buf, 128, 1);
480 if(retval < 0)
481 goto dput_and_out;
483 *interp_elf_ex = *((struct elfhdr *) bprm->buf);
485 *interpreter_dentry = dentry;
486 return 0;
488 dput_and_out:
489 lock_kernel();
490 dput(dentry);
491 unlock_kernel();
492 out:
493 kfree(*name);
494 return retval;
497 static inline int verify_irix_interpreter(struct elfhdr *ihp)
499 if (memcmp(ihp->e_ident, ELFMAG, SELFMAG) != 0)
500 return -ELIBBAD;
501 return 0;
504 #define EXEC_MAP_FLAGS (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE)
506 static inline void map_executable(struct file *fp, struct elf_phdr *epp, int pnum,
507 unsigned int *estack, unsigned int *laddr,
508 unsigned int *scode, unsigned int *ebss,
509 unsigned int *ecode, unsigned int *edata,
510 unsigned int *ebrk)
512 unsigned int tmp;
513 int i, prot;
515 for(i = 0; i < pnum; i++, epp++) {
516 if(epp->p_type != PT_LOAD)
517 continue;
519 /* Map it. */
520 prot = (epp->p_flags & PF_R) ? PROT_READ : 0;
521 prot |= (epp->p_flags & PF_W) ? PROT_WRITE : 0;
522 prot |= (epp->p_flags & PF_X) ? PROT_EXEC : 0;
523 (void) do_mmap(fp, (epp->p_vaddr & 0xfffff000),
524 (epp->p_filesz + (epp->p_vaddr & 0xfff)),
525 prot, EXEC_MAP_FLAGS,
526 (epp->p_offset & 0xfffff000));
528 /* Fixup location tracking vars. */
529 if((epp->p_vaddr & 0xfffff000) < *estack)
530 *estack = (epp->p_vaddr & 0xfffff000);
531 if(!*laddr)
532 *laddr = epp->p_vaddr - epp->p_offset;
533 if(epp->p_vaddr < *scode)
534 *scode = epp->p_vaddr;
536 tmp = epp->p_vaddr + epp->p_filesz;
537 if(tmp > *ebss)
538 *ebss = tmp;
539 if((epp->p_flags & PF_X) && *ecode < tmp)
540 *ecode = tmp;
541 if(*edata < tmp)
542 *edata = tmp;
544 tmp = epp->p_vaddr + epp->p_memsz;
545 if(tmp > *ebrk)
546 *ebrk = tmp;
551 static inline int map_interpreter(struct elf_phdr *epp, struct elfhdr *ihp,
552 struct dentry *identry, unsigned int *iladdr,
553 int pnum, mm_segment_t old_fs,
554 unsigned int *eentry)
556 int i;
558 *eentry = 0xffffffff;
559 for(i = 0; i < pnum; i++, epp++) {
560 if(epp->p_type != PT_INTERP)
561 continue;
563 /* We should have fielded this error elsewhere... */
564 if(*eentry != 0xffffffff)
565 return -1;
567 set_fs(old_fs);
568 *eentry = load_irix_interp(ihp, identry, iladdr);
569 old_fs = get_fs();
570 set_fs(get_ds());
572 lock_kernel();
573 dput(identry);
574 unlock_kernel();
576 if(*eentry == 0xffffffff)
577 return -1;
579 return 0;
583 * IRIX maps a page at 0x200000 that holds information about the
584 * process and the system, here we map the page and fill the
585 * structure
587 void irix_map_prda_page (void)
589 unsigned long v;
590 struct prda *pp;
592 v = do_brk (PRDA_ADDRESS, PAGE_SIZE);
594 if (v < 0)
595 return;
597 pp = (struct prda *) v;
598 pp->prda_sys.t_pid = current->pid;
599 pp->prda_sys.t_prid = read_32bit_cp0_register (CP0_PRID);
600 pp->prda_sys.t_rpid = current->pid;
602 /* We leave the rest set to zero */
607 /* These are the functions used to load ELF style executables and shared
608 * libraries. There is no binary dependent code anywhere else.
610 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
612 struct elfhdr elf_ex, interp_elf_ex;
613 struct dentry *interpreter_dentry;
614 struct elf_phdr *elf_phdata, *elf_ihdr, *elf_ephdr;
615 unsigned int load_addr, elf_bss, elf_brk;
616 unsigned int elf_entry, interp_load_addr = 0;
617 unsigned int start_code, end_code, end_data, elf_stack;
618 int elf_exec_fileno, retval, has_interp, has_ephdr, size, i;
619 char *elf_interpreter;
620 struct file *file;
621 mm_segment_t old_fs;
623 load_addr = 0;
624 has_interp = has_ephdr = 0;
625 elf_ihdr = elf_ephdr = 0;
626 elf_ex = *((struct elfhdr *) bprm->buf);
627 retval = -ENOEXEC;
629 if (verify_binary(&elf_ex, bprm))
630 goto out;
632 #ifdef DEBUG_ELF
633 print_elfhdr(&elf_ex);
634 #endif
636 /* Now read in all of the header information */
637 size = elf_ex.e_phentsize * elf_ex.e_phnum;
638 if (size > 65536)
639 goto out;
640 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
641 if (elf_phdata == NULL) {
642 retval = -ENOMEM;
643 goto out;
646 retval = read_exec(bprm->dentry, elf_ex.e_phoff,
647 (char *) elf_phdata, size, 1);
648 if (retval < 0)
649 goto out_free_ph;
651 #ifdef DEBUG_ELF
652 dump_phdrs(elf_phdata, elf_ex.e_phnum);
653 #endif
655 /* Set some things for later. */
656 for(i = 0; i < elf_ex.e_phnum; i++) {
657 switch(elf_phdata[i].p_type) {
658 case PT_INTERP:
659 has_interp = 1;
660 elf_ihdr = &elf_phdata[i];
661 break;
662 case PT_PHDR:
663 has_ephdr = 1;
664 elf_ephdr = &elf_phdata[i];
665 break;
668 #ifdef DEBUG_ELF
669 printk("\n");
670 #endif
672 elf_bss = 0;
673 elf_brk = 0;
674 retval = open_dentry(bprm->dentry, O_RDONLY);
675 if (retval < 0)
676 goto out_free_ph;
677 file = fget(elf_exec_fileno = retval);
679 elf_stack = 0xffffffff;
680 elf_interpreter = NULL;
681 start_code = 0xffffffff;
682 end_code = 0;
683 end_data = 0;
685 retval = look_for_irix_interpreter(&elf_interpreter,
686 &interpreter_dentry,
687 &interp_elf_ex, elf_phdata, bprm,
688 elf_ex.e_phnum);
689 if (retval)
690 goto out_free_file;
692 if (elf_interpreter) {
693 retval = verify_irix_interpreter(&interp_elf_ex);
694 if(retval)
695 goto out_free_interp;
698 /* OK, we are done with that, now set up the arg stuff,
699 * and then start this sucker up.
701 retval = -E2BIG;
702 if (!bprm->sh_bang && !bprm->p)
703 goto out_free_interp;
705 /* Flush all traces of the currently running executable */
706 retval = flush_old_exec(bprm);
707 if (retval)
708 goto out_free_dentry;
710 /* OK, This is the point of no return */
711 current->mm->end_data = 0;
712 current->mm->end_code = 0;
713 current->mm->mmap = NULL;
714 current->flags &= ~PF_FORKNOEXEC;
715 elf_entry = (unsigned int) elf_ex.e_entry;
717 /* Do this so that we can load the interpreter, if need be. We will
718 * change some of these later.
720 current->mm->rss = 0;
721 setup_arg_pages(bprm);
722 current->mm->start_stack = bprm->p;
724 /* At this point, we assume that the image should be loaded at
725 * fixed address, not at a variable address.
727 old_fs = get_fs();
728 set_fs(get_ds());
730 map_executable(file, elf_phdata, elf_ex.e_phnum, &elf_stack,
731 &load_addr, &start_code, &elf_bss, &end_code,
732 &end_data, &elf_brk);
734 if(elf_interpreter) {
735 retval = map_interpreter(elf_phdata, &interp_elf_ex,
736 interpreter_dentry, &interp_load_addr,
737 elf_ex.e_phnum, old_fs, &elf_entry);
738 kfree(elf_interpreter);
739 if(retval) {
740 set_fs(old_fs);
741 printk("Unable to load IRIX ELF interpreter\n");
742 send_sig(SIGSEGV, current, 0);
743 retval = 0;
744 goto out_free_file;
748 set_fs(old_fs);
750 kfree(elf_phdata);
751 fput(file);
752 sys_close(elf_exec_fileno);
753 current->personality = PER_IRIX32;
755 put_exec_domain(current->exec_domain);
756 if (current->binfmt && current->binfmt->module)
757 __MOD_DEC_USE_COUNT(current->binfmt->module);
758 current->exec_domain = lookup_exec_domain(current->personality);
759 current->binfmt = &irix_format;
760 if (current->binfmt && current->binfmt->module)
761 __MOD_INC_USE_COUNT(current->binfmt->module);
763 compute_creds(bprm);
764 current->flags &= ~PF_FORKNOEXEC;
765 bprm->p = (unsigned long)
766 create_irix_tables((char *)bprm->p, bprm->argc, bprm->envc,
767 (elf_interpreter ? &elf_ex : NULL),
768 load_addr, interp_load_addr, regs, elf_ephdr);
769 current->mm->start_brk = current->mm->brk = elf_brk;
770 current->mm->end_code = end_code;
771 current->mm->start_code = start_code;
772 current->mm->end_data = end_data;
773 current->mm->start_stack = bprm->p;
775 /* Calling set_brk effectively mmaps the pages that we need for the
776 * bss and break sections.
778 set_brk(elf_bss, elf_brk);
781 * IRIX maps a page at 0x200000 which holds some system
782 * information. Programs depend on this.
784 irix_map_prda_page ();
786 padzero(elf_bss);
788 #ifdef DEBUG_ELF
789 printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
790 printk("(end_code) %lx\n" , (long) current->mm->end_code);
791 printk("(start_code) %lx\n" , (long) current->mm->start_code);
792 printk("(end_data) %lx\n" , (long) current->mm->end_data);
793 printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
794 printk("(brk) %lx\n" , (long) current->mm->brk);
795 #endif
797 #if 0 /* XXX No fucking way dude... */
798 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
799 * and some applications "depend" upon this behavior.
800 * Since we do not have the power to recompile these, we
801 * emulate the SVr4 behavior. Sigh.
803 (void) do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
804 MAP_FIXED | MAP_PRIVATE, 0);
805 #endif
807 start_thread(regs, elf_entry, bprm->p);
808 if (current->flags & PF_PTRACED)
809 send_sig(SIGTRAP, current, 0);
810 return 0;
811 out:
812 return retval;
814 out_free_dentry:
815 lock_kernel();
816 dput(interpreter_dentry);
817 unlock_kernel();
818 out_free_interp:
819 if (elf_interpreter)
820 kfree(elf_interpreter);
821 out_free_file:
822 fput(file);
823 sys_close(elf_exec_fileno);
824 out_free_ph:
825 kfree (elf_phdata);
826 goto out;
829 /* This is really simpleminded and specialized - we are loading an
830 * a.out library that is given an ELF header.
832 static inline int do_load_irix_library(struct file *file)
834 struct elfhdr elf_ex;
835 struct elf_phdr *elf_phdata = NULL;
836 struct dentry *dentry;
837 struct inode *inode;
838 unsigned int len;
839 int elf_bss;
840 int retval;
841 unsigned int bss;
842 int error;
843 int i,j, k;
845 len = 0;
846 if (!file->f_op)
847 return -EACCES;
848 dentry = file->f_dentry;
849 inode = dentry->d_inode;
850 elf_bss = 0;
852 /* Seek to the beginning of the file. */
853 if (file->f_op->llseek) {
854 if ((error = file->f_op->llseek(file, 0, 0)) != 0)
855 return -ENOEXEC;
856 } else
857 file->f_pos = 0;
859 set_fs(KERNEL_DS);
860 error = file->f_op->read(file, (char *) &elf_ex, sizeof(elf_ex),
861 &file->f_pos);
862 set_fs(USER_DS);
863 if (error != sizeof(elf_ex))
864 return -ENOEXEC;
866 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
867 return -ENOEXEC;
869 /* First of all, some simple consistency checks. */
870 if(elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
871 !elf_check_arch(elf_ex.e_machine) ||
872 (!dentry->d_inode->i_fop ||
873 !dentry->d_inode->i_fop->mmap))
874 return -ENOEXEC;
876 /* Now read in all of the header information. */
877 if(sizeof(struct elf_phdr) * elf_ex.e_phnum > PAGE_SIZE)
878 return -ENOEXEC;
880 elf_phdata = (struct elf_phdr *)
881 kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
882 if (elf_phdata == NULL)
883 return -ENOMEM;
885 retval = read_exec(dentry, elf_ex.e_phoff, (char *) elf_phdata,
886 sizeof(struct elf_phdr) * elf_ex.e_phnum, 1);
888 j = 0;
889 for(i=0; i<elf_ex.e_phnum; i++)
890 if((elf_phdata + i)->p_type == PT_LOAD) j++;
892 if(j != 1) {
893 kfree(elf_phdata);
894 return -ENOEXEC;
897 while(elf_phdata->p_type != PT_LOAD) elf_phdata++;
899 /* Now use mmap to map the library into memory. */
900 error = do_mmap(file,
901 elf_phdata->p_vaddr & 0xfffff000,
902 elf_phdata->p_filesz + (elf_phdata->p_vaddr & 0xfff),
903 PROT_READ | PROT_WRITE | PROT_EXEC,
904 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
905 elf_phdata->p_offset & 0xfffff000);
907 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
908 if(k > elf_bss) elf_bss = k;
910 if (error != (elf_phdata->p_vaddr & 0xfffff000)) {
911 kfree(elf_phdata);
912 return error;
915 padzero(elf_bss);
917 len = (elf_phdata->p_filesz + elf_phdata->p_vaddr+ 0xfff) & 0xfffff000;
918 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
919 if (bss > len)
920 do_brk(len, bss-len);
921 kfree(elf_phdata);
922 return 0;
925 static int load_irix_library(int fd)
927 int retval = -EACCES;
928 struct file *file;
930 file = fget(fd);
931 if (file) {
932 retval = do_load_irix_library(file);
933 fput(file);
935 return retval;
938 /* Called through irix_syssgi() to map an elf image given an FD,
939 * a phdr ptr USER_PHDRP in userspace, and a count CNT telling how many
940 * phdrs there are in the USER_PHDRP array. We return the vaddr the
941 * first phdr was successfully mapped to.
943 unsigned long irix_mapelf(int fd, struct elf_phdr *user_phdrp, int cnt)
945 struct elf_phdr *hp;
946 struct file *filp;
947 int i, retval;
949 #ifdef DEBUG_ELF
950 printk("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
951 fd, user_phdrp, cnt);
952 #endif
954 /* First get the verification out of the way. */
955 hp = user_phdrp;
956 retval = verify_area(VERIFY_READ, hp, (sizeof(struct elf_phdr) * cnt));
957 if(retval) {
958 #ifdef DEBUG_ELF
959 printk("irix_mapelf: verify_area fails!\n");
960 #endif
961 return retval;
964 #ifdef DEBUG_ELF
965 dump_phdrs(user_phdrp, cnt);
966 #endif
968 for(i = 0; i < cnt; i++, hp++)
969 if(hp->p_type != PT_LOAD) {
970 printk("irix_mapelf: One section is not PT_LOAD!\n");
971 return -ENOEXEC;
974 filp = fget(fd);
975 if (!filp)
976 return -EACCES;
977 if(!filp->f_op) {
978 printk("irix_mapelf: Bogon filp!\n");
979 fput(filp);
980 return -EACCES;
983 hp = user_phdrp;
984 for(i = 0; i < cnt; i++, hp++) {
985 int prot;
987 prot = (hp->p_flags & PF_R) ? PROT_READ : 0;
988 prot |= (hp->p_flags & PF_W) ? PROT_WRITE : 0;
989 prot |= (hp->p_flags & PF_X) ? PROT_EXEC : 0;
990 retval = do_mmap(filp, (hp->p_vaddr & 0xfffff000),
991 (hp->p_filesz + (hp->p_vaddr & 0xfff)),
992 prot, (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
993 (hp->p_offset & 0xfffff000));
995 if(retval != (hp->p_vaddr & 0xfffff000)) {
996 printk("irix_mapelf: do_mmap fails with %d!\n", retval);
997 fput(filp);
998 return retval;
1002 #ifdef DEBUG_ELF
1003 printk("irix_mapelf: Success, returning %08lx\n", user_phdrp->p_vaddr);
1004 #endif
1005 fput(filp);
1006 return user_phdrp->p_vaddr;
1010 * ELF core dumper
1012 * Modelled on fs/exec.c:aout_core_dump()
1013 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1016 /* These are the only things you should do on a core-file: use only these
1017 * functions to write out all the necessary info.
1019 static int dump_write(struct file *file, const void *addr, int nr)
1021 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1024 static int dump_seek(struct file *file, off_t off)
1026 if (file->f_op->llseek) {
1027 if (file->f_op->llseek(file, off, 0) != off)
1028 return 0;
1029 } else
1030 file->f_pos = off;
1031 return 1;
1034 /* Decide whether a segment is worth dumping; default is yes to be
1035 * sure (missing info is worse than too much; etc).
1036 * Personally I'd include everything, and use the coredump limit...
1038 * I think we should skip something. But I am not sure how. H.J.
1040 static inline int maydump(struct vm_area_struct *vma)
1042 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
1043 return 0;
1044 #if 1
1045 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
1046 return 1;
1047 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
1048 return 0;
1049 #endif
1050 return 1;
1053 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
1055 /* An ELF note in memory. */
1056 struct memelfnote
1058 const char *name;
1059 int type;
1060 unsigned int datasz;
1061 void *data;
1064 static int notesize(struct memelfnote *en)
1066 int sz;
1068 sz = sizeof(struct elf_note);
1069 sz += roundup(strlen(en->name), 4);
1070 sz += roundup(en->datasz, 4);
1072 return sz;
1075 /* #define DEBUG */
1077 #define DUMP_WRITE(addr, nr) \
1078 if (!dump_write(file, (addr), (nr))) \
1079 goto end_coredump;
1080 #define DUMP_SEEK(off) \
1081 if (!dump_seek(file, (off))) \
1082 goto end_coredump;
1084 static int writenote(struct memelfnote *men, struct file *file)
1086 struct elf_note en;
1088 en.n_namesz = strlen(men->name);
1089 en.n_descsz = men->datasz;
1090 en.n_type = men->type;
1092 DUMP_WRITE(&en, sizeof(en));
1093 DUMP_WRITE(men->name, en.n_namesz);
1094 /* XXX - cast from long long to long to avoid need for libgcc.a */
1095 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1096 DUMP_WRITE(men->data, men->datasz);
1097 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1099 return 1;
1101 end_coredump:
1102 return 0;
1104 #undef DUMP_WRITE
1105 #undef DUMP_SEEK
1107 #define DUMP_WRITE(addr, nr) \
1108 if (!dump_write(file, (addr), (nr))) \
1109 goto end_coredump;
1110 #define DUMP_SEEK(off) \
1111 if (!dump_seek(file, (off))) \
1112 goto end_coredump;
1114 /* Actual dumper.
1116 * This is a two-pass process; first we find the offsets of the bits,
1117 * and then they are actually written out. If we run out of core limit
1118 * we just truncate.
1120 static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
1122 int has_dumped = 0;
1123 mm_segment_t fs;
1124 int segs;
1125 int i;
1126 size_t size;
1127 struct vm_area_struct *vma;
1128 struct elfhdr elf;
1129 off_t offset = 0, dataoff;
1130 int limit = current->rlim[RLIMIT_CORE].rlim_cur;
1131 int numnote = 4;
1132 struct memelfnote notes[4];
1133 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1134 elf_fpregset_t fpu; /* NT_PRFPREG */
1135 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1137 /* Count what's needed to dump, up to the limit of coredump size. */
1138 segs = 0;
1139 size = 0;
1140 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1141 if (maydump(vma))
1143 int sz = vma->vm_end-vma->vm_start;
1145 if (size+sz >= limit)
1146 break;
1147 else
1148 size += sz;
1151 segs++;
1153 #ifdef DEBUG
1154 printk("irix_core_dump: %d segs taking %d bytes\n", segs, size);
1155 #endif
1157 /* Set up header. */
1158 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1159 elf.e_ident[EI_CLASS] = ELFCLASS32;
1160 elf.e_ident[EI_DATA] = ELFDATA2LSB;
1161 elf.e_ident[EI_VERSION] = EV_CURRENT;
1162 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1164 elf.e_type = ET_CORE;
1165 elf.e_machine = ELF_ARCH;
1166 elf.e_version = EV_CURRENT;
1167 elf.e_entry = 0;
1168 elf.e_phoff = sizeof(elf);
1169 elf.e_shoff = 0;
1170 elf.e_flags = 0;
1171 elf.e_ehsize = sizeof(elf);
1172 elf.e_phentsize = sizeof(struct elf_phdr);
1173 elf.e_phnum = segs+1; /* Include notes. */
1174 elf.e_shentsize = 0;
1175 elf.e_shnum = 0;
1176 elf.e_shstrndx = 0;
1178 fs = get_fs();
1179 set_fs(KERNEL_DS);
1181 has_dumped = 1;
1182 current->flags |= PF_DUMPCORE;
1184 DUMP_WRITE(&elf, sizeof(elf));
1185 offset += sizeof(elf); /* Elf header. */
1186 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers. */
1188 /* Set up the notes in similar form to SVR4 core dumps made
1189 * with info from their /proc.
1191 memset(&psinfo, 0, sizeof(psinfo));
1192 memset(&prstatus, 0, sizeof(prstatus));
1194 notes[0].name = "CORE";
1195 notes[0].type = NT_PRSTATUS;
1196 notes[0].datasz = sizeof(prstatus);
1197 notes[0].data = &prstatus;
1198 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1199 prstatus.pr_sigpend = current->signal.sig[0];
1200 prstatus.pr_sighold = current->blocked.sig[0];
1201 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1202 psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1203 psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1204 psinfo.pr_sid = prstatus.pr_sid = current->session;
1205 prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
1206 prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
1207 prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
1208 prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
1209 prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
1210 prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
1211 prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
1212 prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
1213 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs)) {
1214 printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
1215 "(%d)\n", sizeof(elf_gregset_t), sizeof(struct pt_regs));
1216 } else {
1217 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1220 notes[1].name = "CORE";
1221 notes[1].type = NT_PRPSINFO;
1222 notes[1].datasz = sizeof(psinfo);
1223 notes[1].data = &psinfo;
1224 i = current->state ? ffz(~current->state) + 1 : 0;
1225 psinfo.pr_state = i;
1226 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1227 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1228 psinfo.pr_nice = current->priority-15;
1229 psinfo.pr_flag = current->flags;
1230 psinfo.pr_uid = current->uid;
1231 psinfo.pr_gid = current->gid;
1233 int i, len;
1235 set_fs(fs);
1237 len = current->mm->arg_end - current->mm->arg_start;
1238 len = len >= ELF_PRARGSZ ? ELF_PRARGSZ : len;
1239 copy_from_user(&psinfo.pr_psargs,
1240 (const char *)current->mm->arg_start, len);
1241 for(i = 0; i < len; i++)
1242 if (psinfo.pr_psargs[i] == 0)
1243 psinfo.pr_psargs[i] = ' ';
1244 psinfo.pr_psargs[len] = 0;
1246 set_fs(KERNEL_DS);
1248 strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1250 notes[2].name = "CORE";
1251 notes[2].type = NT_TASKSTRUCT;
1252 notes[2].datasz = sizeof(*current);
1253 notes[2].data = current;
1255 /* Try to dump the FPU. */
1256 prstatus.pr_fpvalid = dump_fpu (&fpu);
1257 if (!prstatus.pr_fpvalid) {
1258 numnote--;
1259 } else {
1260 notes[3].name = "CORE";
1261 notes[3].type = NT_PRFPREG;
1262 notes[3].datasz = sizeof(fpu);
1263 notes[3].data = &fpu;
1266 /* Write notes phdr entry. */
1268 struct elf_phdr phdr;
1269 int sz = 0;
1271 for(i = 0; i < numnote; i++)
1272 sz += notesize(&notes[i]);
1274 phdr.p_type = PT_NOTE;
1275 phdr.p_offset = offset;
1276 phdr.p_vaddr = 0;
1277 phdr.p_paddr = 0;
1278 phdr.p_filesz = sz;
1279 phdr.p_memsz = 0;
1280 phdr.p_flags = 0;
1281 phdr.p_align = 0;
1283 offset += phdr.p_filesz;
1284 DUMP_WRITE(&phdr, sizeof(phdr));
1287 /* Page-align dumped data. */
1288 dataoff = offset = roundup(offset, PAGE_SIZE);
1290 /* Write program headers for segments dump. */
1291 for(vma = current->mm->mmap, i = 0;
1292 i < segs && vma != NULL; vma = vma->vm_next) {
1293 struct elf_phdr phdr;
1294 size_t sz;
1296 i++;
1298 sz = vma->vm_end - vma->vm_start;
1300 phdr.p_type = PT_LOAD;
1301 phdr.p_offset = offset;
1302 phdr.p_vaddr = vma->vm_start;
1303 phdr.p_paddr = 0;
1304 phdr.p_filesz = maydump(vma) ? sz : 0;
1305 phdr.p_memsz = sz;
1306 offset += phdr.p_filesz;
1307 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1308 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1309 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1310 phdr.p_align = PAGE_SIZE;
1312 DUMP_WRITE(&phdr, sizeof(phdr));
1315 for(i = 0; i < numnote; i++)
1316 if (!writenote(&notes[i], file))
1317 goto end_coredump;
1319 set_fs(fs);
1321 DUMP_SEEK(dataoff);
1323 for(i = 0, vma = current->mm->mmap;
1324 i < segs && vma != NULL;
1325 vma = vma->vm_next) {
1326 unsigned long addr = vma->vm_start;
1327 unsigned long len = vma->vm_end - vma->vm_start;
1329 if (!maydump(vma))
1330 continue;
1331 i++;
1332 #ifdef DEBUG
1333 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1334 #endif
1335 DUMP_WRITE((void *)addr, len);
1338 if ((off_t) file->f_pos != offset) {
1339 /* Sanity check. */
1340 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1341 (off_t) file->f_pos, offset);
1344 end_coredump:
1345 set_fs(fs);
1346 return has_dumped;
1349 static int __init init_irix_binfmt(void)
1351 return register_binfmt(&irix_format);
1354 static void __exit cleanup_module(void)
1356 /* Remove the IRIX ELF loaders. */
1357 unregister_binfmt(&irix_format);
1360 module_init(init_irix_binfmt)
1361 module_exit(exit_irix_binfmt)