irixelf.c 35 KB

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