irixelf.c 35 KB

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