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