irixelf.c 35 KB

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