irixelf.c 35 KB

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