initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / mips / kernel / irixelf.c
blobdc9f7924e024f46d7f008a130f90e826c0224d75
1 /*
2 * irixelf.c: Code to load IRIX ELF executables which conform to
3 * the MIPS ABI.
5 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
7 * Based upon work which is:
8 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
9 */
11 #include <linux/module.h>
13 #include <linux/fs.h>
14 #include <linux/stat.h>
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/mman.h>
18 #include <linux/a.out.h>
19 #include <linux/errno.h>
20 #include <linux/init.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/smp_lock.h>
33 #include <asm/uaccess.h>
34 #include <asm/mipsregs.h>
35 #include <asm/prctl.h>
37 #define DLINFO_ITEMS 12
39 #include <linux/elf.h>
41 #undef DEBUG_ELF
43 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs);
44 static int load_irix_library(struct file *);
45 static int irix_core_dump(long signr, struct pt_regs * regs,
46 struct file *file);
48 static struct linux_binfmt irix_format = {
49 NULL, THIS_MODULE, load_irix_binary, load_irix_library,
50 irix_core_dump, PAGE_SIZE
53 #ifndef elf_addr_t
54 #define elf_addr_t unsigned long
55 #endif
57 #ifdef DEBUG_ELF
58 /* Debugging routines. */
59 static char *get_elf_p_type(Elf32_Word p_type)
61 int i = (int) p_type;
63 switch(i) {
64 case PT_NULL: return("PT_NULL"); break;
65 case PT_LOAD: return("PT_LOAD"); break;
66 case PT_DYNAMIC: return("PT_DYNAMIC"); break;
67 case PT_INTERP: return("PT_INTERP"); break;
68 case PT_NOTE: return("PT_NOTE"); break;
69 case PT_SHLIB: return("PT_SHLIB"); break;
70 case PT_PHDR: return("PT_PHDR"); break;
71 case PT_LOPROC: return("PT_LOPROC/REGINFO"); break;
72 case PT_HIPROC: return("PT_HIPROC"); break;
73 default: return("PT_BOGUS"); break;
77 static void print_elfhdr(struct elfhdr *ehp)
79 int i;
81 printk("ELFHDR: e_ident<");
82 for(i = 0; i < (EI_NIDENT - 1); i++) printk("%x ", ehp->e_ident[i]);
83 printk("%x>\n", ehp->e_ident[i]);
84 printk(" e_type[%04x] e_machine[%04x] e_version[%08lx]\n",
85 (unsigned short) ehp->e_type, (unsigned short) ehp->e_machine,
86 (unsigned long) ehp->e_version);
87 printk(" e_entry[%08lx] e_phoff[%08lx] e_shoff[%08lx] "
88 "e_flags[%08lx]\n",
89 (unsigned long) ehp->e_entry, (unsigned long) ehp->e_phoff,
90 (unsigned long) ehp->e_shoff, (unsigned long) ehp->e_flags);
91 printk(" e_ehsize[%04x] e_phentsize[%04x] e_phnum[%04x]\n",
92 (unsigned short) ehp->e_ehsize, (unsigned short) ehp->e_phentsize,
93 (unsigned short) ehp->e_phnum);
94 printk(" e_shentsize[%04x] e_shnum[%04x] e_shstrndx[%04x]\n",
95 (unsigned short) ehp->e_shentsize, (unsigned short) ehp->e_shnum,
96 (unsigned short) ehp->e_shstrndx);
99 static void print_phdr(int i, struct elf_phdr *ep)
101 printk("PHDR[%d]: p_type[%s] p_offset[%08lx] p_vaddr[%08lx] "
102 "p_paddr[%08lx]\n", i, get_elf_p_type(ep->p_type),
103 (unsigned long) ep->p_offset, (unsigned long) ep->p_vaddr,
104 (unsigned long) ep->p_paddr);
105 printk(" p_filesz[%08lx] p_memsz[%08lx] p_flags[%08lx] "
106 "p_align[%08lx]\n", (unsigned long) ep->p_filesz,
107 (unsigned long) ep->p_memsz, (unsigned long) ep->p_flags,
108 (unsigned long) ep->p_align);
111 static void dump_phdrs(struct elf_phdr *ep, int pnum)
113 int i;
115 for(i = 0; i < pnum; i++, ep++) {
116 if((ep->p_type == PT_LOAD) ||
117 (ep->p_type == PT_INTERP) ||
118 (ep->p_type == PT_PHDR))
119 print_phdr(i, ep);
122 #endif /* (DEBUG_ELF) */
124 static void set_brk(unsigned long start, unsigned long end)
126 start = PAGE_ALIGN(start);
127 end = PAGE_ALIGN(end);
128 if (end <= start)
129 return;
130 do_brk(start, end - start);
134 /* We need to explicitly zero any fractional pages
135 * after the data section (i.e. bss). This would
136 * contain the junk from the file that should not
137 * be in memory.
139 static void padzero(unsigned long elf_bss)
141 unsigned long nbyte;
143 nbyte = elf_bss & (PAGE_SIZE-1);
144 if (nbyte) {
145 nbyte = PAGE_SIZE - nbyte;
146 clear_user((void *) elf_bss, nbyte);
150 unsigned long * create_irix_tables(char * p, int argc, int envc,
151 struct elfhdr * exec, unsigned int load_addr,
152 unsigned int interp_load_addr,
153 struct pt_regs *regs, struct elf_phdr *ephdr)
155 elf_addr_t *argv;
156 elf_addr_t *envp;
157 elf_addr_t *sp, *csp;
159 #ifdef DEBUG_ELF
160 printk("create_irix_tables: p[%p] argc[%d] envc[%d] "
161 "load_addr[%08x] interp_load_addr[%08x]\n",
162 p, argc, envc, load_addr, interp_load_addr);
163 #endif
164 sp = (elf_addr_t *) (~15UL & (unsigned long) p);
165 csp = sp;
166 csp -= exec ? DLINFO_ITEMS*2 : 2;
167 csp -= envc+1;
168 csp -= argc+1;
169 csp -= 1; /* argc itself */
170 if ((unsigned long)csp & 15UL) {
171 sp -= (16UL - ((unsigned long)csp & 15UL)) / sizeof(*sp);
175 * Put the ELF interpreter info on the stack
177 #define NEW_AUX_ENT(nr, id, val) \
178 __put_user ((id), sp+(nr*2)); \
179 __put_user ((val), sp+(nr*2+1)); \
181 sp -= 2;
182 NEW_AUX_ENT(0, AT_NULL, 0);
184 if(exec) {
185 sp -= 11*2;
187 NEW_AUX_ENT (0, AT_PHDR, load_addr + exec->e_phoff);
188 NEW_AUX_ENT (1, AT_PHENT, sizeof (struct elf_phdr));
189 NEW_AUX_ENT (2, AT_PHNUM, exec->e_phnum);
190 NEW_AUX_ENT (3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
191 NEW_AUX_ENT (4, AT_BASE, interp_load_addr);
192 NEW_AUX_ENT (5, AT_FLAGS, 0);
193 NEW_AUX_ENT (6, AT_ENTRY, (elf_addr_t) exec->e_entry);
194 NEW_AUX_ENT (7, AT_UID, (elf_addr_t) current->uid);
195 NEW_AUX_ENT (8, AT_EUID, (elf_addr_t) current->euid);
196 NEW_AUX_ENT (9, AT_GID, (elf_addr_t) current->gid);
197 NEW_AUX_ENT (10, AT_EGID, (elf_addr_t) current->egid);
199 #undef NEW_AUX_ENT
201 sp -= envc+1;
202 envp = sp;
203 sp -= argc+1;
204 argv = sp;
206 __put_user((elf_addr_t)argc,--sp);
207 current->mm->arg_start = (unsigned long) p;
208 while (argc-->0) {
209 __put_user((unsigned long)p,argv++);
210 p += strlen_user(p);
212 __put_user(NULL, argv);
213 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
214 while (envc-->0) {
215 __put_user((unsigned long)p,envp++);
216 p += strlen_user(p);
218 __put_user(NULL, envp);
219 current->mm->env_end = (unsigned long) p;
220 return sp;
224 /* This is much more generalized than the library routine read function,
225 * so we keep this separate. Technically the library read function
226 * is only provided so that we can read a.out libraries that have
227 * an ELF header.
229 static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
230 struct file * interpreter,
231 unsigned int *interp_load_addr)
233 struct elf_phdr *elf_phdata = NULL;
234 struct elf_phdr *eppnt;
235 unsigned int len;
236 unsigned int load_addr;
237 int elf_bss;
238 int retval;
239 unsigned int last_bss;
240 int error;
241 int i;
242 unsigned int k;
244 elf_bss = 0;
245 last_bss = 0;
246 error = load_addr = 0;
248 #ifdef DEBUG_ELF
249 print_elfhdr(interp_elf_ex);
250 #endif
252 /* First of all, some simple consistency checks */
253 if ((interp_elf_ex->e_type != ET_EXEC &&
254 interp_elf_ex->e_type != ET_DYN) ||
255 !irix_elf_check_arch(interp_elf_ex) ||
256 !interpreter->f_op->mmap) {
257 printk("IRIX interp has bad e_type %d\n", interp_elf_ex->e_type);
258 return 0xffffffff;
261 /* Now read in all of the header information */
262 if(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > PAGE_SIZE) {
263 printk("IRIX interp header bigger than a page (%d)\n",
264 (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum));
265 return 0xffffffff;
268 elf_phdata = (struct elf_phdr *)
269 kmalloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum,
270 GFP_KERNEL);
272 if(!elf_phdata) {
273 printk("Cannot kmalloc phdata for IRIX interp.\n");
274 return 0xffffffff;
277 /* If the size of this structure has changed, then punt, since
278 * we will be doing the wrong thing.
280 if(interp_elf_ex->e_phentsize != 32) {
281 printk("IRIX interp e_phentsize == %d != 32 ",
282 interp_elf_ex->e_phentsize);
283 kfree(elf_phdata);
284 return 0xffffffff;
287 retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
288 (char *) elf_phdata,
289 sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
291 #ifdef DEBUG_ELF
292 dump_phdrs(elf_phdata, interp_elf_ex->e_phnum);
293 #endif
295 eppnt = elf_phdata;
296 for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
297 if(eppnt->p_type == PT_LOAD) {
298 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
299 int elf_prot = 0;
300 unsigned long vaddr = 0;
301 if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
302 if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
303 if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
304 elf_type |= MAP_FIXED;
305 vaddr = eppnt->p_vaddr;
307 #ifdef DEBUG_ELF
308 printk("INTERP do_mmap(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
309 interpreter, vaddr,
310 (unsigned long) (eppnt->p_filesz + (eppnt->p_vaddr & 0xfff)),
311 (unsigned long) elf_prot, (unsigned long) elf_type,
312 (unsigned long) (eppnt->p_offset & 0xfffff000));
313 #endif
314 down_write(&current->mm->mmap_sem);
315 error = do_mmap(interpreter, vaddr,
316 eppnt->p_filesz + (eppnt->p_vaddr & 0xfff),
317 elf_prot, elf_type,
318 eppnt->p_offset & 0xfffff000);
319 up_write(&current->mm->mmap_sem);
321 if(error < 0 && error > -1024) {
322 printk("Aieee IRIX interp mmap error=%d\n", error);
323 break; /* Real error */
325 #ifdef DEBUG_ELF
326 printk("error=%08lx ", (unsigned long) error);
327 #endif
328 if(!load_addr && interp_elf_ex->e_type == ET_DYN) {
329 load_addr = error;
330 #ifdef DEBUG_ELF
331 printk("load_addr = error ");
332 #endif
335 /* Find the end of the file mapping for this phdr, and keep
336 * track of the largest address we see for this.
338 k = eppnt->p_vaddr + eppnt->p_filesz;
339 if(k > elf_bss) elf_bss = k;
341 /* Do the same thing for the memory mapping - between
342 * elf_bss and last_bss is the bss section.
344 k = eppnt->p_memsz + eppnt->p_vaddr;
345 if(k > last_bss) last_bss = k;
346 #ifdef DEBUG_ELF
347 printk("\n");
348 #endif
352 /* Now use mmap to map the library into memory. */
353 if(error < 0 && error > -1024) {
354 #ifdef DEBUG_ELF
355 printk("got error %d\n", error);
356 #endif
357 kfree(elf_phdata);
358 return 0xffffffff;
361 /* Now fill out the bss section. First pad the last page up
362 * to the page boundary, and then perform a mmap to make sure
363 * that there are zero-mapped pages up to and including the
364 * last bss page.
366 #ifdef DEBUG_ELF
367 printk("padzero(%08lx) ", (unsigned long) (elf_bss));
368 #endif
369 padzero(elf_bss);
370 len = (elf_bss + 0xfff) & 0xfffff000; /* What we have mapped so far */
372 #ifdef DEBUG_ELF
373 printk("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss,
374 (unsigned long) len);
375 #endif
377 /* Map the last of the bss segment */
378 if (last_bss > len) {
379 do_brk(len, (last_bss - len));
381 kfree(elf_phdata);
383 *interp_load_addr = load_addr;
384 return ((unsigned int) interp_elf_ex->e_entry);
387 /* Check sanity of IRIX elf executable header. */
388 static int verify_binary(struct elfhdr *ehp, struct linux_binprm *bprm)
390 if (memcmp(ehp->e_ident, ELFMAG, SELFMAG) != 0)
391 return -ENOEXEC;
393 /* First of all, some simple consistency checks */
394 if((ehp->e_type != ET_EXEC && ehp->e_type != ET_DYN) ||
395 !irix_elf_check_arch(ehp) || !bprm->file->f_op->mmap) {
396 return -ENOEXEC;
399 /* Only support MIPS ARCH2 or greater IRIX binaries for now. */
400 if(!(ehp->e_flags & EF_MIPS_ARCH) && !(ehp->e_flags & 0x04)) {
401 return -ENOEXEC;
404 /* XXX Don't support N32 or 64bit binaries yet because they can
405 * XXX and do execute 64 bit instructions and expect all registers
406 * XXX to be 64 bit as well. We need to make the kernel save
407 * XXX all registers as 64bits on cpu's capable of this at
408 * XXX exception time plus frob the XTLB exception vector.
410 if((ehp->e_flags & 0x20)) {
411 return -ENOEXEC;
414 return 0; /* It's ok. */
417 #define IRIX_INTERP_PREFIX "/usr/gnemul/irix"
419 /* Look for an IRIX ELF interpreter. */
420 static inline int look_for_irix_interpreter(char **name,
421 struct file **interpreter,
422 struct elfhdr *interp_elf_ex,
423 struct elf_phdr *epp,
424 struct linux_binprm *bprm, int pnum)
426 int i;
427 int retval = -EINVAL;
428 struct file *file = NULL;
430 *name = NULL;
431 for(i = 0; i < pnum; i++, epp++) {
432 if (epp->p_type != PT_INTERP)
433 continue;
435 /* It is illegal to have two interpreters for one executable. */
436 if (*name != NULL)
437 goto out;
439 *name = (char *) kmalloc((epp->p_filesz +
440 strlen(IRIX_INTERP_PREFIX)),
441 GFP_KERNEL);
442 if (!*name)
443 return -ENOMEM;
445 strcpy(*name, IRIX_INTERP_PREFIX);
446 retval = kernel_read(bprm->file, epp->p_offset, (*name + 16),
447 epp->p_filesz);
448 if (retval < 0)
449 goto out;
451 file = open_exec(*name);
452 if (IS_ERR(file)) {
453 retval = PTR_ERR(file);
454 goto out;
456 retval = kernel_read(file, 0, bprm->buf, 128);
457 if (retval < 0)
458 goto dput_and_out;
460 *interp_elf_ex = *(struct elfhdr *) bprm->buf;
462 *interpreter = file;
463 return 0;
465 dput_and_out:
466 fput(file);
467 out:
468 kfree(*name);
469 return retval;
472 static inline int verify_irix_interpreter(struct elfhdr *ihp)
474 if (memcmp(ihp->e_ident, ELFMAG, SELFMAG) != 0)
475 return -ELIBBAD;
476 return 0;
479 #define EXEC_MAP_FLAGS (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE)
481 static inline void map_executable(struct file *fp, struct elf_phdr *epp, int pnum,
482 unsigned int *estack, unsigned int *laddr,
483 unsigned int *scode, unsigned int *ebss,
484 unsigned int *ecode, unsigned int *edata,
485 unsigned int *ebrk)
487 unsigned int tmp;
488 int i, prot;
490 for(i = 0; i < pnum; i++, epp++) {
491 if(epp->p_type != PT_LOAD)
492 continue;
494 /* Map it. */
495 prot = (epp->p_flags & PF_R) ? PROT_READ : 0;
496 prot |= (epp->p_flags & PF_W) ? PROT_WRITE : 0;
497 prot |= (epp->p_flags & PF_X) ? PROT_EXEC : 0;
498 down_write(&current->mm->mmap_sem);
499 (void) do_mmap(fp, (epp->p_vaddr & 0xfffff000),
500 (epp->p_filesz + (epp->p_vaddr & 0xfff)),
501 prot, EXEC_MAP_FLAGS,
502 (epp->p_offset & 0xfffff000));
503 up_write(&current->mm->mmap_sem);
505 /* Fixup location tracking vars. */
506 if((epp->p_vaddr & 0xfffff000) < *estack)
507 *estack = (epp->p_vaddr & 0xfffff000);
508 if(!*laddr)
509 *laddr = epp->p_vaddr - epp->p_offset;
510 if(epp->p_vaddr < *scode)
511 *scode = epp->p_vaddr;
513 tmp = epp->p_vaddr + epp->p_filesz;
514 if(tmp > *ebss)
515 *ebss = tmp;
516 if((epp->p_flags & PF_X) && *ecode < tmp)
517 *ecode = tmp;
518 if(*edata < tmp)
519 *edata = tmp;
521 tmp = epp->p_vaddr + epp->p_memsz;
522 if(tmp > *ebrk)
523 *ebrk = tmp;
528 static inline int map_interpreter(struct elf_phdr *epp, struct elfhdr *ihp,
529 struct file *interp, unsigned int *iladdr,
530 int pnum, mm_segment_t old_fs,
531 unsigned int *eentry)
533 int i;
535 *eentry = 0xffffffff;
536 for(i = 0; i < pnum; i++, epp++) {
537 if(epp->p_type != PT_INTERP)
538 continue;
540 /* We should have fielded this error elsewhere... */
541 if(*eentry != 0xffffffff)
542 return -1;
544 set_fs(old_fs);
545 *eentry = load_irix_interp(ihp, interp, iladdr);
546 old_fs = get_fs();
547 set_fs(get_ds());
549 fput(interp);
551 if (*eentry == 0xffffffff)
552 return -1;
554 return 0;
558 * IRIX maps a page at 0x200000 that holds information about the
559 * process and the system, here we map the page and fill the
560 * structure
562 void irix_map_prda_page (void)
564 unsigned long v;
565 struct prda *pp;
567 v = do_brk (PRDA_ADDRESS, PAGE_SIZE);
569 if (v < 0)
570 return;
572 pp = (struct prda *) v;
573 pp->prda_sys.t_pid = current->pid;
574 pp->prda_sys.t_prid = read_c0_prid();
575 pp->prda_sys.t_rpid = current->pid;
577 /* We leave the rest set to zero */
582 /* These are the functions used to load ELF style executables and shared
583 * libraries. There is no binary dependent code anywhere else.
585 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
587 struct elfhdr elf_ex, interp_elf_ex;
588 struct file *interpreter;
589 struct elf_phdr *elf_phdata, *elf_ihdr, *elf_ephdr;
590 unsigned int load_addr, elf_bss, elf_brk;
591 unsigned int elf_entry, interp_load_addr = 0;
592 unsigned int start_code, end_code, end_data, elf_stack;
593 int retval, has_interp, has_ephdr, size, i;
594 char *elf_interpreter;
595 mm_segment_t old_fs;
597 load_addr = 0;
598 has_interp = has_ephdr = 0;
599 elf_ihdr = elf_ephdr = 0;
600 elf_ex = *((struct elfhdr *) bprm->buf);
601 retval = -ENOEXEC;
603 if (verify_binary(&elf_ex, bprm))
604 goto out;
606 #ifdef DEBUG_ELF
607 print_elfhdr(&elf_ex);
608 #endif
610 /* Now read in all of the header information */
611 size = elf_ex.e_phentsize * elf_ex.e_phnum;
612 if (size > 65536)
613 goto out;
614 elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
615 if (elf_phdata == NULL) {
616 retval = -ENOMEM;
617 goto out;
620 retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *)elf_phdata, size);
621 if (retval < 0)
622 goto out_free_ph;
624 #ifdef DEBUG_ELF
625 dump_phdrs(elf_phdata, elf_ex.e_phnum);
626 #endif
628 /* Set some things for later. */
629 for(i = 0; i < elf_ex.e_phnum; i++) {
630 switch(elf_phdata[i].p_type) {
631 case PT_INTERP:
632 has_interp = 1;
633 elf_ihdr = &elf_phdata[i];
634 break;
635 case PT_PHDR:
636 has_ephdr = 1;
637 elf_ephdr = &elf_phdata[i];
638 break;
641 #ifdef DEBUG_ELF
642 printk("\n");
643 #endif
645 elf_bss = 0;
646 elf_brk = 0;
648 elf_stack = 0xffffffff;
649 elf_interpreter = NULL;
650 start_code = 0xffffffff;
651 end_code = 0;
652 end_data = 0;
654 retval = look_for_irix_interpreter(&elf_interpreter,
655 &interpreter,
656 &interp_elf_ex, elf_phdata, bprm,
657 elf_ex.e_phnum);
658 if (retval)
659 goto out_free_file;
661 if (elf_interpreter) {
662 retval = verify_irix_interpreter(&interp_elf_ex);
663 if(retval)
664 goto out_free_interp;
667 /* OK, we are done with that, now set up the arg stuff,
668 * and then start this sucker up.
670 retval = -E2BIG;
671 if (!bprm->sh_bang && !bprm->p)
672 goto out_free_interp;
674 /* Flush all traces of the currently running executable */
675 retval = flush_old_exec(bprm);
676 if (retval)
677 goto out_free_dentry;
679 /* OK, This is the point of no return */
680 current->mm->end_data = 0;
681 current->mm->end_code = 0;
682 current->mm->mmap = NULL;
683 current->flags &= ~PF_FORKNOEXEC;
684 elf_entry = (unsigned int) elf_ex.e_entry;
686 /* Do this so that we can load the interpreter, if need be. We will
687 * change some of these later.
689 current->mm->rss = 0;
690 setup_arg_pages(bprm, EXSTACK_DEFAULT);
691 current->mm->start_stack = bprm->p;
693 /* At this point, we assume that the image should be loaded at
694 * fixed address, not at a variable address.
696 old_fs = get_fs();
697 set_fs(get_ds());
699 map_executable(bprm->file, elf_phdata, elf_ex.e_phnum, &elf_stack,
700 &load_addr, &start_code, &elf_bss, &end_code,
701 &end_data, &elf_brk);
703 if(elf_interpreter) {
704 retval = map_interpreter(elf_phdata, &interp_elf_ex,
705 interpreter, &interp_load_addr,
706 elf_ex.e_phnum, old_fs, &elf_entry);
707 kfree(elf_interpreter);
708 if(retval) {
709 set_fs(old_fs);
710 printk("Unable to load IRIX ELF interpreter\n");
711 send_sig(SIGSEGV, current, 0);
712 retval = 0;
713 goto out_free_file;
717 set_fs(old_fs);
719 kfree(elf_phdata);
720 set_personality(PER_IRIX32);
721 set_binfmt(&irix_format);
722 compute_creds(bprm);
723 current->flags &= ~PF_FORKNOEXEC;
724 bprm->p = (unsigned long)
725 create_irix_tables((char *)bprm->p, bprm->argc, bprm->envc,
726 (elf_interpreter ? &elf_ex : NULL),
727 load_addr, interp_load_addr, regs, elf_ephdr);
728 current->mm->start_brk = current->mm->brk = elf_brk;
729 current->mm->end_code = end_code;
730 current->mm->start_code = start_code;
731 current->mm->end_data = end_data;
732 current->mm->start_stack = bprm->p;
734 /* Calling set_brk effectively mmaps the pages that we need for the
735 * bss and break sections.
737 set_brk(elf_bss, elf_brk);
740 * IRIX maps a page at 0x200000 which holds some system
741 * information. Programs depend on this.
743 irix_map_prda_page ();
745 padzero(elf_bss);
747 #ifdef DEBUG_ELF
748 printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
749 printk("(end_code) %lx\n" , (long) current->mm->end_code);
750 printk("(start_code) %lx\n" , (long) current->mm->start_code);
751 printk("(end_data) %lx\n" , (long) current->mm->end_data);
752 printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
753 printk("(brk) %lx\n" , (long) current->mm->brk);
754 #endif
756 #if 0 /* XXX No fucking way dude... */
757 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
758 * and some applications "depend" upon this behavior.
759 * Since we do not have the power to recompile these, we
760 * emulate the SVr4 behavior. Sigh.
762 down_write(&current->mm->mmap_sem);
763 (void) do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
764 MAP_FIXED | MAP_PRIVATE, 0);
765 up_write(&current->mm->mmap_sem);
766 #endif
768 start_thread(regs, elf_entry, bprm->p);
769 if (current->ptrace & PT_PTRACED)
770 send_sig(SIGTRAP, current, 0);
771 return 0;
772 out:
773 return retval;
775 out_free_dentry:
776 allow_write_access(interpreter);
777 fput(interpreter);
778 out_free_interp:
779 if (elf_interpreter)
780 kfree(elf_interpreter);
781 out_free_file:
782 out_free_ph:
783 kfree (elf_phdata);
784 goto out;
787 /* This is really simpleminded and specialized - we are loading an
788 * a.out library that is given an ELF header.
790 static int load_irix_library(struct file *file)
792 struct elfhdr elf_ex;
793 struct elf_phdr *elf_phdata = NULL;
794 unsigned int len = 0;
795 int elf_bss = 0;
796 int retval;
797 unsigned int bss;
798 int error;
799 int i,j, k;
801 error = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
802 if (error != sizeof(elf_ex))
803 return -ENOEXEC;
805 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
806 return -ENOEXEC;
808 /* First of all, some simple consistency checks. */
809 if(elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
810 !irix_elf_check_arch(&elf_ex) || !file->f_op->mmap)
811 return -ENOEXEC;
813 /* Now read in all of the header information. */
814 if(sizeof(struct elf_phdr) * elf_ex.e_phnum > PAGE_SIZE)
815 return -ENOEXEC;
817 elf_phdata = (struct elf_phdr *)
818 kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
819 if (elf_phdata == NULL)
820 return -ENOMEM;
822 retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata,
823 sizeof(struct elf_phdr) * elf_ex.e_phnum);
825 j = 0;
826 for(i=0; i<elf_ex.e_phnum; i++)
827 if((elf_phdata + i)->p_type == PT_LOAD) j++;
829 if(j != 1) {
830 kfree(elf_phdata);
831 return -ENOEXEC;
834 while(elf_phdata->p_type != PT_LOAD) elf_phdata++;
836 /* Now use mmap to map the library into memory. */
837 down_write(&current->mm->mmap_sem);
838 error = do_mmap(file,
839 elf_phdata->p_vaddr & 0xfffff000,
840 elf_phdata->p_filesz + (elf_phdata->p_vaddr & 0xfff),
841 PROT_READ | PROT_WRITE | PROT_EXEC,
842 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
843 elf_phdata->p_offset & 0xfffff000);
844 up_write(&current->mm->mmap_sem);
846 k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
847 if (k > elf_bss) elf_bss = k;
849 if (error != (elf_phdata->p_vaddr & 0xfffff000)) {
850 kfree(elf_phdata);
851 return error;
854 padzero(elf_bss);
856 len = (elf_phdata->p_filesz + elf_phdata->p_vaddr+ 0xfff) & 0xfffff000;
857 bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
858 if (bss > len)
859 do_brk(len, bss-len);
860 kfree(elf_phdata);
861 return 0;
864 /* Called through irix_syssgi() to map an elf image given an FD,
865 * a phdr ptr USER_PHDRP in userspace, and a count CNT telling how many
866 * phdrs there are in the USER_PHDRP array. We return the vaddr the
867 * first phdr was successfully mapped to.
869 unsigned long irix_mapelf(int fd, struct elf_phdr *user_phdrp, int cnt)
871 struct elf_phdr *hp;
872 struct file *filp;
873 int i, retval;
875 #ifdef DEBUG_ELF
876 printk("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
877 fd, user_phdrp, cnt);
878 #endif
880 /* First get the verification out of the way. */
881 hp = user_phdrp;
882 retval = verify_area(VERIFY_READ, hp, (sizeof(struct elf_phdr) * cnt));
883 if(retval) {
884 #ifdef DEBUG_ELF
885 printk("irix_mapelf: verify_area fails!\n");
886 #endif
887 return retval;
890 #ifdef DEBUG_ELF
891 dump_phdrs(user_phdrp, cnt);
892 #endif
894 for(i = 0; i < cnt; i++, hp++)
895 if(hp->p_type != PT_LOAD) {
896 printk("irix_mapelf: One section is not PT_LOAD!\n");
897 return -ENOEXEC;
900 filp = fget(fd);
901 if (!filp)
902 return -EACCES;
903 if(!filp->f_op) {
904 printk("irix_mapelf: Bogon filp!\n");
905 fput(filp);
906 return -EACCES;
909 hp = user_phdrp;
910 for(i = 0; i < cnt; i++, hp++) {
911 int prot;
913 prot = (hp->p_flags & PF_R) ? PROT_READ : 0;
914 prot |= (hp->p_flags & PF_W) ? PROT_WRITE : 0;
915 prot |= (hp->p_flags & PF_X) ? PROT_EXEC : 0;
916 down_write(&current->mm->mmap_sem);
917 retval = do_mmap(filp, (hp->p_vaddr & 0xfffff000),
918 (hp->p_filesz + (hp->p_vaddr & 0xfff)),
919 prot, (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
920 (hp->p_offset & 0xfffff000));
921 up_write(&current->mm->mmap_sem);
923 if(retval != (hp->p_vaddr & 0xfffff000)) {
924 printk("irix_mapelf: do_mmap fails with %d!\n", retval);
925 fput(filp);
926 return retval;
930 #ifdef DEBUG_ELF
931 printk("irix_mapelf: Success, returning %08lx\n", user_phdrp->p_vaddr);
932 #endif
933 fput(filp);
934 return user_phdrp->p_vaddr;
938 * ELF core dumper
940 * Modelled on fs/exec.c:aout_core_dump()
941 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
944 /* These are the only things you should do on a core-file: use only these
945 * functions to write out all the necessary info.
947 static int dump_write(struct file *file, const void *addr, int nr)
949 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
952 static int dump_seek(struct file *file, off_t off)
954 if (file->f_op->llseek) {
955 if (file->f_op->llseek(file, off, 0) != off)
956 return 0;
957 } else
958 file->f_pos = off;
959 return 1;
962 /* Decide whether a segment is worth dumping; default is yes to be
963 * sure (missing info is worse than too much; etc).
964 * Personally I'd include everything, and use the coredump limit...
966 * I think we should skip something. But I am not sure how. H.J.
968 static inline int maydump(struct vm_area_struct *vma)
970 if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
971 return 0;
972 #if 1
973 if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
974 return 1;
975 if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
976 return 0;
977 #endif
978 return 1;
981 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
983 /* An ELF note in memory. */
984 struct memelfnote
986 const char *name;
987 int type;
988 unsigned int datasz;
989 void *data;
992 static int notesize(struct memelfnote *en)
994 int sz;
996 sz = sizeof(struct elf_note);
997 sz += roundup(strlen(en->name), 4);
998 sz += roundup(en->datasz, 4);
1000 return sz;
1003 /* #define DEBUG */
1005 #define DUMP_WRITE(addr, nr) \
1006 if (!dump_write(file, (addr), (nr))) \
1007 goto end_coredump;
1008 #define DUMP_SEEK(off) \
1009 if (!dump_seek(file, (off))) \
1010 goto end_coredump;
1012 static int writenote(struct memelfnote *men, struct file *file)
1014 struct elf_note en;
1016 en.n_namesz = strlen(men->name);
1017 en.n_descsz = men->datasz;
1018 en.n_type = men->type;
1020 DUMP_WRITE(&en, sizeof(en));
1021 DUMP_WRITE(men->name, en.n_namesz);
1022 /* XXX - cast from long long to long to avoid need for libgcc.a */
1023 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1024 DUMP_WRITE(men->data, men->datasz);
1025 DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
1027 return 1;
1029 end_coredump:
1030 return 0;
1032 #undef DUMP_WRITE
1033 #undef DUMP_SEEK
1035 #define DUMP_WRITE(addr, nr) \
1036 if (!dump_write(file, (addr), (nr))) \
1037 goto end_coredump;
1038 #define DUMP_SEEK(off) \
1039 if (!dump_seek(file, (off))) \
1040 goto end_coredump;
1042 /* Actual dumper.
1044 * This is a two-pass process; first we find the offsets of the bits,
1045 * and then they are actually written out. If we run out of core limit
1046 * we just truncate.
1048 static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
1050 int has_dumped = 0;
1051 mm_segment_t fs;
1052 int segs;
1053 int i;
1054 size_t size;
1055 struct vm_area_struct *vma;
1056 struct elfhdr elf;
1057 off_t offset = 0, dataoff;
1058 int limit = current->rlim[RLIMIT_CORE].rlim_cur;
1059 int numnote = 4;
1060 struct memelfnote notes[4];
1061 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1062 elf_fpregset_t fpu; /* NT_PRFPREG */
1063 struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
1065 /* Count what's needed to dump, up to the limit of coredump size. */
1066 segs = 0;
1067 size = 0;
1068 for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1069 if (maydump(vma))
1071 int sz = vma->vm_end-vma->vm_start;
1073 if (size+sz >= limit)
1074 break;
1075 else
1076 size += sz;
1079 segs++;
1081 #ifdef DEBUG
1082 printk("irix_core_dump: %d segs taking %d bytes\n", segs, size);
1083 #endif
1085 /* Set up header. */
1086 memcpy(elf.e_ident, ELFMAG, SELFMAG);
1087 elf.e_ident[EI_CLASS] = ELFCLASS32;
1088 elf.e_ident[EI_DATA] = ELFDATA2LSB;
1089 elf.e_ident[EI_VERSION] = EV_CURRENT;
1090 elf.e_ident[EI_OSABI] = ELF_OSABI;
1091 memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1093 elf.e_type = ET_CORE;
1094 elf.e_machine = ELF_ARCH;
1095 elf.e_version = EV_CURRENT;
1096 elf.e_entry = 0;
1097 elf.e_phoff = sizeof(elf);
1098 elf.e_shoff = 0;
1099 elf.e_flags = 0;
1100 elf.e_ehsize = sizeof(elf);
1101 elf.e_phentsize = sizeof(struct elf_phdr);
1102 elf.e_phnum = segs+1; /* Include notes. */
1103 elf.e_shentsize = 0;
1104 elf.e_shnum = 0;
1105 elf.e_shstrndx = 0;
1107 fs = get_fs();
1108 set_fs(KERNEL_DS);
1110 has_dumped = 1;
1111 current->flags |= PF_DUMPCORE;
1113 DUMP_WRITE(&elf, sizeof(elf));
1114 offset += sizeof(elf); /* Elf header. */
1115 offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers. */
1117 /* Set up the notes in similar form to SVR4 core dumps made
1118 * with info from their /proc.
1120 memset(&psinfo, 0, sizeof(psinfo));
1121 memset(&prstatus, 0, sizeof(prstatus));
1123 notes[0].name = "CORE";
1124 notes[0].type = NT_PRSTATUS;
1125 notes[0].datasz = sizeof(prstatus);
1126 notes[0].data = &prstatus;
1127 prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1128 prstatus.pr_sigpend = current->pending.signal.sig[0];
1129 prstatus.pr_sighold = current->blocked.sig[0];
1130 psinfo.pr_pid = prstatus.pr_pid = current->pid;
1131 psinfo.pr_ppid = prstatus.pr_ppid = current->parent->pid;
1132 psinfo.pr_pgrp = prstatus.pr_pgrp = process_group(current);
1133 psinfo.pr_sid = prstatus.pr_sid = current->signal->session;
1134 prstatus.pr_utime.tv_sec = CT_TO_SECS(current->utime);
1135 prstatus.pr_utime.tv_usec = CT_TO_USECS(current->utime);
1136 prstatus.pr_stime.tv_sec = CT_TO_SECS(current->stime);
1137 prstatus.pr_stime.tv_usec = CT_TO_USECS(current->stime);
1138 prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->cutime);
1139 prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->cutime);
1140 prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->cstime);
1141 prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->cstime);
1142 if (sizeof(elf_gregset_t) != sizeof(struct pt_regs)) {
1143 printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
1144 "(%d)\n", sizeof(elf_gregset_t), sizeof(struct pt_regs));
1145 } else {
1146 *(struct pt_regs *)&prstatus.pr_reg = *regs;
1149 notes[1].name = "CORE";
1150 notes[1].type = NT_PRPSINFO;
1151 notes[1].datasz = sizeof(psinfo);
1152 notes[1].data = &psinfo;
1153 i = current->state ? ffz(~current->state) + 1 : 0;
1154 psinfo.pr_state = i;
1155 psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1156 psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1157 psinfo.pr_nice = task_nice(current);
1158 psinfo.pr_flag = current->flags;
1159 psinfo.pr_uid = current->uid;
1160 psinfo.pr_gid = current->gid;
1162 int i, len;
1164 set_fs(fs);
1166 len = current->mm->arg_end - current->mm->arg_start;
1167 len = len >= ELF_PRARGSZ ? ELF_PRARGSZ : len;
1168 copy_from_user(&psinfo.pr_psargs,
1169 (const char *)current->mm->arg_start, len);
1170 for(i = 0; i < len; i++)
1171 if (psinfo.pr_psargs[i] == 0)
1172 psinfo.pr_psargs[i] = ' ';
1173 psinfo.pr_psargs[len] = 0;
1175 set_fs(KERNEL_DS);
1177 strlcpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1179 notes[2].name = "CORE";
1180 notes[2].type = NT_TASKSTRUCT;
1181 notes[2].datasz = sizeof(*current);
1182 notes[2].data = current;
1184 /* Try to dump the FPU. */
1185 prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
1186 if (!prstatus.pr_fpvalid) {
1187 numnote--;
1188 } else {
1189 notes[3].name = "CORE";
1190 notes[3].type = NT_PRFPREG;
1191 notes[3].datasz = sizeof(fpu);
1192 notes[3].data = &fpu;
1195 /* Write notes phdr entry. */
1197 struct elf_phdr phdr;
1198 int sz = 0;
1200 for(i = 0; i < numnote; i++)
1201 sz += notesize(&notes[i]);
1203 phdr.p_type = PT_NOTE;
1204 phdr.p_offset = offset;
1205 phdr.p_vaddr = 0;
1206 phdr.p_paddr = 0;
1207 phdr.p_filesz = sz;
1208 phdr.p_memsz = 0;
1209 phdr.p_flags = 0;
1210 phdr.p_align = 0;
1212 offset += phdr.p_filesz;
1213 DUMP_WRITE(&phdr, sizeof(phdr));
1216 /* Page-align dumped data. */
1217 dataoff = offset = roundup(offset, PAGE_SIZE);
1219 /* Write program headers for segments dump. */
1220 for(vma = current->mm->mmap, i = 0;
1221 i < segs && vma != NULL; vma = vma->vm_next) {
1222 struct elf_phdr phdr;
1223 size_t sz;
1225 i++;
1227 sz = vma->vm_end - vma->vm_start;
1229 phdr.p_type = PT_LOAD;
1230 phdr.p_offset = offset;
1231 phdr.p_vaddr = vma->vm_start;
1232 phdr.p_paddr = 0;
1233 phdr.p_filesz = maydump(vma) ? sz : 0;
1234 phdr.p_memsz = sz;
1235 offset += phdr.p_filesz;
1236 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1237 if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1238 if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1239 phdr.p_align = PAGE_SIZE;
1241 DUMP_WRITE(&phdr, sizeof(phdr));
1244 for(i = 0; i < numnote; i++)
1245 if (!writenote(&notes[i], file))
1246 goto end_coredump;
1248 set_fs(fs);
1250 DUMP_SEEK(dataoff);
1252 for(i = 0, vma = current->mm->mmap;
1253 i < segs && vma != NULL;
1254 vma = vma->vm_next) {
1255 unsigned long addr = vma->vm_start;
1256 unsigned long len = vma->vm_end - vma->vm_start;
1258 if (!maydump(vma))
1259 continue;
1260 i++;
1261 #ifdef DEBUG
1262 printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1263 #endif
1264 DUMP_WRITE((void *)addr, len);
1267 if ((off_t) file->f_pos != offset) {
1268 /* Sanity check. */
1269 printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1270 (off_t) file->f_pos, offset);
1273 end_coredump:
1274 set_fs(fs);
1275 return has_dumped;
1278 static int __init init_irix_binfmt(void)
1280 return register_binfmt(&irix_format);
1283 static void __exit exit_irix_binfmt(void)
1285 /* Remove the IRIX ELF loaders. */
1286 unregister_binfmt(&irix_format);
1289 module_init(init_irix_binfmt)
1290 module_exit(exit_irix_binfmt)