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