irixelf.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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 = (char *) kmalloc((epp->p_filesz +
  371. strlen(IRIX_EMUL)),
  372. GFP_KERNEL);
  373. if (!*name)
  374. return -ENOMEM;
  375. strcpy(*name, IRIX_EMUL);
  376. retval = kernel_read(bprm->file, epp->p_offset, (*name + 16),
  377. epp->p_filesz);
  378. if (retval < 0)
  379. goto out;
  380. file = open_exec(*name);
  381. if (IS_ERR(file)) {
  382. retval = PTR_ERR(file);
  383. goto out;
  384. }
  385. retval = kernel_read(file, 0, bprm->buf, 128);
  386. if (retval < 0)
  387. goto dput_and_out;
  388. *interp_elf_ex = *(struct elfhdr *) bprm->buf;
  389. }
  390. *interpreter = file;
  391. return 0;
  392. dput_and_out:
  393. fput(file);
  394. out:
  395. kfree(*name);
  396. return retval;
  397. }
  398. static inline int verify_irix_interpreter(struct elfhdr *ihp)
  399. {
  400. if (memcmp(ihp->e_ident, ELFMAG, SELFMAG) != 0)
  401. return -ELIBBAD;
  402. return 0;
  403. }
  404. #define EXEC_MAP_FLAGS (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE)
  405. static inline void map_executable(struct file *fp, struct elf_phdr *epp, int pnum,
  406. unsigned int *estack, unsigned int *laddr,
  407. unsigned int *scode, unsigned int *ebss,
  408. unsigned int *ecode, unsigned int *edata,
  409. unsigned int *ebrk)
  410. {
  411. unsigned int tmp;
  412. int i, prot;
  413. for(i = 0; i < pnum; i++, epp++) {
  414. if(epp->p_type != PT_LOAD)
  415. continue;
  416. /* Map it. */
  417. prot = (epp->p_flags & PF_R) ? PROT_READ : 0;
  418. prot |= (epp->p_flags & PF_W) ? PROT_WRITE : 0;
  419. prot |= (epp->p_flags & PF_X) ? PROT_EXEC : 0;
  420. down_write(&current->mm->mmap_sem);
  421. (void) do_mmap(fp, (epp->p_vaddr & 0xfffff000),
  422. (epp->p_filesz + (epp->p_vaddr & 0xfff)),
  423. prot, EXEC_MAP_FLAGS,
  424. (epp->p_offset & 0xfffff000));
  425. up_write(&current->mm->mmap_sem);
  426. /* Fixup location tracking vars. */
  427. if((epp->p_vaddr & 0xfffff000) < *estack)
  428. *estack = (epp->p_vaddr & 0xfffff000);
  429. if(!*laddr)
  430. *laddr = epp->p_vaddr - epp->p_offset;
  431. if(epp->p_vaddr < *scode)
  432. *scode = epp->p_vaddr;
  433. tmp = epp->p_vaddr + epp->p_filesz;
  434. if(tmp > *ebss)
  435. *ebss = tmp;
  436. if((epp->p_flags & PF_X) && *ecode < tmp)
  437. *ecode = tmp;
  438. if(*edata < tmp)
  439. *edata = tmp;
  440. tmp = epp->p_vaddr + epp->p_memsz;
  441. if(tmp > *ebrk)
  442. *ebrk = tmp;
  443. }
  444. }
  445. static inline int map_interpreter(struct elf_phdr *epp, struct elfhdr *ihp,
  446. struct file *interp, unsigned int *iladdr,
  447. int pnum, mm_segment_t old_fs,
  448. unsigned int *eentry)
  449. {
  450. int i;
  451. *eentry = 0xffffffff;
  452. for(i = 0; i < pnum; i++, epp++) {
  453. if(epp->p_type != PT_INTERP)
  454. continue;
  455. /* We should have fielded this error elsewhere... */
  456. if(*eentry != 0xffffffff)
  457. return -1;
  458. set_fs(old_fs);
  459. *eentry = load_irix_interp(ihp, interp, iladdr);
  460. old_fs = get_fs();
  461. set_fs(get_ds());
  462. fput(interp);
  463. if (*eentry == 0xffffffff)
  464. return -1;
  465. }
  466. return 0;
  467. }
  468. /*
  469. * IRIX maps a page at 0x200000 that holds information about the
  470. * process and the system, here we map the page and fill the
  471. * structure
  472. */
  473. static void irix_map_prda_page(void)
  474. {
  475. unsigned long v;
  476. struct prda *pp;
  477. down_write(&current->mm->mmap_sem);
  478. v = do_brk (PRDA_ADDRESS, PAGE_SIZE);
  479. up_write(&current->mm->mmap_sem);
  480. if (v < 0)
  481. return;
  482. pp = (struct prda *) v;
  483. pp->prda_sys.t_pid = current->pid;
  484. pp->prda_sys.t_prid = read_c0_prid();
  485. pp->prda_sys.t_rpid = current->pid;
  486. /* We leave the rest set to zero */
  487. }
  488. /* These are the functions used to load ELF style executables and shared
  489. * libraries. There is no binary dependent code anywhere else.
  490. */
  491. static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
  492. {
  493. struct elfhdr elf_ex, interp_elf_ex;
  494. struct file *interpreter;
  495. struct elf_phdr *elf_phdata, *elf_ihdr, *elf_ephdr;
  496. unsigned int load_addr, elf_bss, elf_brk;
  497. unsigned int elf_entry, interp_load_addr = 0;
  498. unsigned int start_code, end_code, end_data, elf_stack;
  499. int retval, has_interp, has_ephdr, size, i;
  500. char *elf_interpreter;
  501. mm_segment_t old_fs;
  502. load_addr = 0;
  503. has_interp = has_ephdr = 0;
  504. elf_ihdr = elf_ephdr = NULL;
  505. elf_ex = *((struct elfhdr *) bprm->buf);
  506. retval = -ENOEXEC;
  507. if (verify_binary(&elf_ex, bprm))
  508. goto out;
  509. /*
  510. * Telling -o32 static binaries from Linux and Irix apart from each
  511. * other is difficult. There are 2 differences to be noted for static
  512. * binaries from the 2 operating systems:
  513. *
  514. * 1) Irix binaries have their .text section before their .init
  515. * section. Linux binaries are just the opposite.
  516. *
  517. * 2) Irix binaries usually have <= 12 sections and Linux
  518. * binaries have > 20.
  519. *
  520. * We will use Method #2 since Method #1 would require us to read in
  521. * the section headers which is way too much overhead. This appears
  522. * to work for everything we have ran into so far. If anyone has a
  523. * better method to tell the binaries apart, I'm listening.
  524. */
  525. if (elf_ex.e_shnum > 20)
  526. goto out;
  527. #ifdef DEBUG
  528. print_elfhdr(&elf_ex);
  529. #endif
  530. /* Now read in all of the header information */
  531. size = elf_ex.e_phentsize * elf_ex.e_phnum;
  532. if (size > 65536)
  533. goto out;
  534. elf_phdata = kmalloc(size, GFP_KERNEL);
  535. if (elf_phdata == NULL) {
  536. retval = -ENOMEM;
  537. goto out;
  538. }
  539. retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *)elf_phdata, size);
  540. if (retval < 0)
  541. goto out_free_ph;
  542. #ifdef DEBUG
  543. dump_phdrs(elf_phdata, elf_ex.e_phnum);
  544. #endif
  545. /* Set some things for later. */
  546. for(i = 0; i < elf_ex.e_phnum; i++) {
  547. switch(elf_phdata[i].p_type) {
  548. case PT_INTERP:
  549. has_interp = 1;
  550. elf_ihdr = &elf_phdata[i];
  551. break;
  552. case PT_PHDR:
  553. has_ephdr = 1;
  554. elf_ephdr = &elf_phdata[i];
  555. break;
  556. };
  557. }
  558. pr_debug("\n");
  559. elf_bss = 0;
  560. elf_brk = 0;
  561. elf_stack = 0xffffffff;
  562. elf_interpreter = NULL;
  563. start_code = 0xffffffff;
  564. end_code = 0;
  565. end_data = 0;
  566. /*
  567. * If we get a return value, we change the value to be ENOEXEC
  568. * so that we can exit gracefully and the main binary format
  569. * search loop in 'fs/exec.c' will move onto the next handler
  570. * which should be the normal ELF binary handler.
  571. */
  572. retval = look_for_irix_interpreter(&elf_interpreter, &interpreter,
  573. &interp_elf_ex, elf_phdata, bprm,
  574. elf_ex.e_phnum);
  575. if (retval) {
  576. retval = -ENOEXEC;
  577. goto out_free_file;
  578. }
  579. if (elf_interpreter) {
  580. retval = verify_irix_interpreter(&interp_elf_ex);
  581. if(retval)
  582. goto out_free_interp;
  583. }
  584. /* OK, we are done with that, now set up the arg stuff,
  585. * and then start this sucker up.
  586. */
  587. retval = -E2BIG;
  588. if (!bprm->sh_bang && !bprm->p)
  589. goto out_free_interp;
  590. /* Flush all traces of the currently running executable */
  591. retval = flush_old_exec(bprm);
  592. if (retval)
  593. goto out_free_dentry;
  594. /* OK, This is the point of no return */
  595. current->mm->end_data = 0;
  596. current->mm->end_code = 0;
  597. current->mm->mmap = NULL;
  598. current->flags &= ~PF_FORKNOEXEC;
  599. elf_entry = (unsigned int) elf_ex.e_entry;
  600. /* Do this so that we can load the interpreter, if need be. We will
  601. * change some of these later.
  602. */
  603. set_mm_counter(current->mm, rss, 0);
  604. setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
  605. current->mm->start_stack = bprm->p;
  606. /* At this point, we assume that the image should be loaded at
  607. * fixed address, not at a variable address.
  608. */
  609. old_fs = get_fs();
  610. set_fs(get_ds());
  611. map_executable(bprm->file, elf_phdata, elf_ex.e_phnum, &elf_stack,
  612. &load_addr, &start_code, &elf_bss, &end_code,
  613. &end_data, &elf_brk);
  614. if(elf_interpreter) {
  615. retval = map_interpreter(elf_phdata, &interp_elf_ex,
  616. interpreter, &interp_load_addr,
  617. elf_ex.e_phnum, old_fs, &elf_entry);
  618. kfree(elf_interpreter);
  619. if(retval) {
  620. set_fs(old_fs);
  621. printk("Unable to load IRIX ELF interpreter\n");
  622. send_sig(SIGSEGV, current, 0);
  623. retval = 0;
  624. goto out_free_file;
  625. }
  626. }
  627. set_fs(old_fs);
  628. kfree(elf_phdata);
  629. set_personality(PER_IRIX32);
  630. set_binfmt(&irix_format);
  631. compute_creds(bprm);
  632. current->flags &= ~PF_FORKNOEXEC;
  633. bprm->p = (unsigned long)
  634. create_irix_tables((char *)bprm->p, bprm->argc, bprm->envc,
  635. (elf_interpreter ? &elf_ex : NULL),
  636. load_addr, interp_load_addr, regs, elf_ephdr);
  637. current->mm->start_brk = current->mm->brk = elf_brk;
  638. current->mm->end_code = end_code;
  639. current->mm->start_code = start_code;
  640. current->mm->end_data = end_data;
  641. current->mm->start_stack = bprm->p;
  642. /* Calling set_brk effectively mmaps the pages that we need for the
  643. * bss and break sections.
  644. */
  645. set_brk(elf_bss, elf_brk);
  646. /*
  647. * IRIX maps a page at 0x200000 which holds some system
  648. * information. Programs depend on this.
  649. */
  650. irix_map_prda_page();
  651. padzero(elf_bss);
  652. pr_debug("(start_brk) %lx\n" , (long) current->mm->start_brk);
  653. pr_debug("(end_code) %lx\n" , (long) current->mm->end_code);
  654. pr_debug("(start_code) %lx\n" , (long) current->mm->start_code);
  655. pr_debug("(end_data) %lx\n" , (long) current->mm->end_data);
  656. pr_debug("(start_stack) %lx\n" , (long) current->mm->start_stack);
  657. pr_debug("(brk) %lx\n" , (long) current->mm->brk);
  658. #if 0 /* XXX No fucking way dude... */
  659. /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
  660. * and some applications "depend" upon this behavior.
  661. * Since we do not have the power to recompile these, we
  662. * emulate the SVr4 behavior. Sigh.
  663. */
  664. down_write(&current->mm->mmap_sem);
  665. (void) do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
  666. MAP_FIXED | MAP_PRIVATE, 0);
  667. up_write(&current->mm->mmap_sem);
  668. #endif
  669. start_thread(regs, elf_entry, bprm->p);
  670. if (current->ptrace & PT_PTRACED)
  671. send_sig(SIGTRAP, current, 0);
  672. return 0;
  673. out:
  674. return retval;
  675. out_free_dentry:
  676. allow_write_access(interpreter);
  677. fput(interpreter);
  678. out_free_interp:
  679. kfree(elf_interpreter);
  680. out_free_file:
  681. out_free_ph:
  682. kfree (elf_phdata);
  683. goto out;
  684. }
  685. /* This is really simpleminded and specialized - we are loading an
  686. * a.out library that is given an ELF header.
  687. */
  688. static int load_irix_library(struct file *file)
  689. {
  690. struct elfhdr elf_ex;
  691. struct elf_phdr *elf_phdata = NULL;
  692. unsigned int len = 0;
  693. int elf_bss = 0;
  694. int retval;
  695. unsigned int bss;
  696. int error;
  697. int i,j, k;
  698. error = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
  699. if (error != sizeof(elf_ex))
  700. return -ENOEXEC;
  701. if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  702. return -ENOEXEC;
  703. /* First of all, some simple consistency checks. */
  704. if(elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
  705. !file->f_op->mmap)
  706. return -ENOEXEC;
  707. /* Now read in all of the header information. */
  708. if(sizeof(struct elf_phdr) * elf_ex.e_phnum > PAGE_SIZE)
  709. return -ENOEXEC;
  710. elf_phdata = kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
  711. if (elf_phdata == NULL)
  712. return -ENOMEM;
  713. retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata,
  714. sizeof(struct elf_phdr) * elf_ex.e_phnum);
  715. j = 0;
  716. for(i=0; i<elf_ex.e_phnum; i++)
  717. if((elf_phdata + i)->p_type == PT_LOAD) j++;
  718. if(j != 1) {
  719. kfree(elf_phdata);
  720. return -ENOEXEC;
  721. }
  722. while(elf_phdata->p_type != PT_LOAD) elf_phdata++;
  723. /* Now use mmap to map the library into memory. */
  724. down_write(&current->mm->mmap_sem);
  725. error = do_mmap(file,
  726. elf_phdata->p_vaddr & 0xfffff000,
  727. elf_phdata->p_filesz + (elf_phdata->p_vaddr & 0xfff),
  728. PROT_READ | PROT_WRITE | PROT_EXEC,
  729. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  730. elf_phdata->p_offset & 0xfffff000);
  731. up_write(&current->mm->mmap_sem);
  732. k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
  733. if (k > elf_bss) elf_bss = k;
  734. if (error != (elf_phdata->p_vaddr & 0xfffff000)) {
  735. kfree(elf_phdata);
  736. return error;
  737. }
  738. padzero(elf_bss);
  739. len = (elf_phdata->p_filesz + elf_phdata->p_vaddr+ 0xfff) & 0xfffff000;
  740. bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
  741. if (bss > len) {
  742. down_write(&current->mm->mmap_sem);
  743. do_brk(len, bss-len);
  744. up_write(&current->mm->mmap_sem);
  745. }
  746. kfree(elf_phdata);
  747. return 0;
  748. }
  749. /* Called through irix_syssgi() to map an elf image given an FD,
  750. * a phdr ptr USER_PHDRP in userspace, and a count CNT telling how many
  751. * phdrs there are in the USER_PHDRP array. We return the vaddr the
  752. * first phdr was successfully mapped to.
  753. */
  754. unsigned long irix_mapelf(int fd, struct elf_phdr __user *user_phdrp, int cnt)
  755. {
  756. unsigned long type, vaddr, filesz, offset, flags;
  757. struct elf_phdr __user *hp;
  758. struct file *filp;
  759. int i, retval;
  760. pr_debug("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
  761. fd, user_phdrp, cnt);
  762. /* First get the verification out of the way. */
  763. hp = user_phdrp;
  764. if (!access_ok(VERIFY_READ, hp, (sizeof(struct elf_phdr) * cnt))) {
  765. pr_debug("irix_mapelf: bad pointer to ELF PHDR!\n");
  766. return -EFAULT;
  767. }
  768. #ifdef DEBUG
  769. dump_phdrs(user_phdrp, cnt);
  770. #endif
  771. for (i = 0; i < cnt; i++, hp++) {
  772. if (__get_user(type, &hp->p_type))
  773. return -EFAULT;
  774. if (type != PT_LOAD) {
  775. printk("irix_mapelf: One section is not PT_LOAD!\n");
  776. return -ENOEXEC;
  777. }
  778. }
  779. filp = fget(fd);
  780. if (!filp)
  781. return -EACCES;
  782. if(!filp->f_op) {
  783. printk("irix_mapelf: Bogon filp!\n");
  784. fput(filp);
  785. return -EACCES;
  786. }
  787. hp = user_phdrp;
  788. for(i = 0; i < cnt; i++, hp++) {
  789. int prot;
  790. retval = __get_user(vaddr, &hp->p_vaddr);
  791. retval |= __get_user(filesz, &hp->p_filesz);
  792. retval |= __get_user(offset, &hp->p_offset);
  793. retval |= __get_user(flags, &hp->p_flags);
  794. if (retval)
  795. return retval;
  796. prot = (flags & PF_R) ? PROT_READ : 0;
  797. prot |= (flags & PF_W) ? PROT_WRITE : 0;
  798. prot |= (flags & PF_X) ? PROT_EXEC : 0;
  799. down_write(&current->mm->mmap_sem);
  800. retval = do_mmap(filp, (vaddr & 0xfffff000),
  801. (filesz + (vaddr & 0xfff)),
  802. prot, (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
  803. (offset & 0xfffff000));
  804. up_write(&current->mm->mmap_sem);
  805. if (retval != (vaddr & 0xfffff000)) {
  806. printk("irix_mapelf: do_mmap fails with %d!\n", retval);
  807. fput(filp);
  808. return retval;
  809. }
  810. }
  811. pr_debug("irix_mapelf: Success, returning %08lx\n",
  812. (unsigned long) user_phdrp->p_vaddr);
  813. fput(filp);
  814. if (__get_user(vaddr, &user_phdrp->p_vaddr))
  815. return -EFAULT;
  816. return vaddr;
  817. }
  818. /*
  819. * ELF core dumper
  820. *
  821. * Modelled on fs/exec.c:aout_core_dump()
  822. * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  823. */
  824. /* These are the only things you should do on a core-file: use only these
  825. * functions to write out all the necessary info.
  826. */
  827. static int dump_write(struct file *file, const void __user *addr, int nr)
  828. {
  829. return file->f_op->write(file, (const char __user *) addr, nr, &file->f_pos) == nr;
  830. }
  831. static int dump_seek(struct file *file, off_t off)
  832. {
  833. if (file->f_op->llseek) {
  834. if (file->f_op->llseek(file, off, 0) != off)
  835. return 0;
  836. } else
  837. file->f_pos = off;
  838. return 1;
  839. }
  840. /* Decide whether a segment is worth dumping; default is yes to be
  841. * sure (missing info is worse than too much; etc).
  842. * Personally I'd include everything, and use the coredump limit...
  843. *
  844. * I think we should skip something. But I am not sure how. H.J.
  845. */
  846. static inline int maydump(struct vm_area_struct *vma)
  847. {
  848. if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
  849. return 0;
  850. #if 1
  851. if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
  852. return 1;
  853. if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
  854. return 0;
  855. #endif
  856. return 1;
  857. }
  858. #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
  859. /* An ELF note in memory. */
  860. struct memelfnote
  861. {
  862. const char *name;
  863. int type;
  864. unsigned int datasz;
  865. void *data;
  866. };
  867. static int notesize(struct memelfnote *en)
  868. {
  869. int sz;
  870. sz = sizeof(struct elf_note);
  871. sz += roundup(strlen(en->name), 4);
  872. sz += roundup(en->datasz, 4);
  873. return sz;
  874. }
  875. /* #define DEBUG */
  876. #define DUMP_WRITE(addr, nr) \
  877. if (!dump_write(file, (addr), (nr))) \
  878. goto end_coredump;
  879. #define DUMP_SEEK(off) \
  880. if (!dump_seek(file, (off))) \
  881. goto end_coredump;
  882. static int writenote(struct memelfnote *men, struct file *file)
  883. {
  884. struct elf_note en;
  885. en.n_namesz = strlen(men->name);
  886. en.n_descsz = men->datasz;
  887. en.n_type = men->type;
  888. DUMP_WRITE(&en, sizeof(en));
  889. DUMP_WRITE(men->name, en.n_namesz);
  890. /* XXX - cast from long long to long to avoid need for libgcc.a */
  891. DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
  892. DUMP_WRITE(men->data, men->datasz);
  893. DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
  894. return 1;
  895. end_coredump:
  896. return 0;
  897. }
  898. #undef DUMP_WRITE
  899. #undef DUMP_SEEK
  900. #define DUMP_WRITE(addr, nr) \
  901. if (!dump_write(file, (addr), (nr))) \
  902. goto end_coredump;
  903. #define DUMP_SEEK(off) \
  904. if (!dump_seek(file, (off))) \
  905. goto end_coredump;
  906. /* Actual dumper.
  907. *
  908. * This is a two-pass process; first we find the offsets of the bits,
  909. * and then they are actually written out. If we run out of core limit
  910. * we just truncate.
  911. */
  912. static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
  913. {
  914. int has_dumped = 0;
  915. mm_segment_t fs;
  916. int segs;
  917. int i;
  918. size_t size;
  919. struct vm_area_struct *vma;
  920. struct elfhdr elf;
  921. off_t offset = 0, dataoff;
  922. int limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
  923. int numnote = 4;
  924. struct memelfnote notes[4];
  925. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  926. elf_fpregset_t fpu; /* NT_PRFPREG */
  927. struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
  928. /* Count what's needed to dump, up to the limit of coredump size. */
  929. segs = 0;
  930. size = 0;
  931. for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
  932. if (maydump(vma))
  933. {
  934. int sz = vma->vm_end-vma->vm_start;
  935. if (size+sz >= limit)
  936. break;
  937. else
  938. size += sz;
  939. }
  940. segs++;
  941. }
  942. #ifdef DEBUG
  943. printk("irix_core_dump: %d segs taking %d bytes\n", segs, size);
  944. #endif
  945. /* Set up header. */
  946. memcpy(elf.e_ident, ELFMAG, SELFMAG);
  947. elf.e_ident[EI_CLASS] = ELFCLASS32;
  948. elf.e_ident[EI_DATA] = ELFDATA2LSB;
  949. elf.e_ident[EI_VERSION] = EV_CURRENT;
  950. elf.e_ident[EI_OSABI] = ELF_OSABI;
  951. memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  952. elf.e_type = ET_CORE;
  953. elf.e_machine = ELF_ARCH;
  954. elf.e_version = EV_CURRENT;
  955. elf.e_entry = 0;
  956. elf.e_phoff = sizeof(elf);
  957. elf.e_shoff = 0;
  958. elf.e_flags = 0;
  959. elf.e_ehsize = sizeof(elf);
  960. elf.e_phentsize = sizeof(struct elf_phdr);
  961. elf.e_phnum = segs+1; /* Include notes. */
  962. elf.e_shentsize = 0;
  963. elf.e_shnum = 0;
  964. elf.e_shstrndx = 0;
  965. fs = get_fs();
  966. set_fs(KERNEL_DS);
  967. has_dumped = 1;
  968. current->flags |= PF_DUMPCORE;
  969. DUMP_WRITE(&elf, sizeof(elf));
  970. offset += sizeof(elf); /* Elf header. */
  971. offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers. */
  972. /* Set up the notes in similar form to SVR4 core dumps made
  973. * with info from their /proc.
  974. */
  975. memset(&psinfo, 0, sizeof(psinfo));
  976. memset(&prstatus, 0, sizeof(prstatus));
  977. notes[0].name = "CORE";
  978. notes[0].type = NT_PRSTATUS;
  979. notes[0].datasz = sizeof(prstatus);
  980. notes[0].data = &prstatus;
  981. prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
  982. prstatus.pr_sigpend = current->pending.signal.sig[0];
  983. prstatus.pr_sighold = current->blocked.sig[0];
  984. psinfo.pr_pid = prstatus.pr_pid = current->pid;
  985. psinfo.pr_ppid = prstatus.pr_ppid = current->parent->pid;
  986. psinfo.pr_pgrp = prstatus.pr_pgrp = process_group(current);
  987. psinfo.pr_sid = prstatus.pr_sid = current->signal->session;
  988. if (current->pid == current->tgid) {
  989. /*
  990. * This is the record for the group leader. Add in the
  991. * cumulative times of previous dead threads. This total
  992. * won't include the time of each live thread whose state
  993. * is included in the core dump. The final total reported
  994. * to our parent process when it calls wait4 will include
  995. * those sums as well as the little bit more time it takes
  996. * this and each other thread to finish dying after the
  997. * core dump synchronization phase.
  998. */
  999. jiffies_to_timeval(current->utime + current->signal->utime,
  1000. &prstatus.pr_utime);
  1001. jiffies_to_timeval(current->stime + current->signal->stime,
  1002. &prstatus.pr_stime);
  1003. } else {
  1004. jiffies_to_timeval(current->utime, &prstatus.pr_utime);
  1005. jiffies_to_timeval(current->stime, &prstatus.pr_stime);
  1006. }
  1007. jiffies_to_timeval(current->signal->cutime, &prstatus.pr_cutime);
  1008. jiffies_to_timeval(current->signal->cstime, &prstatus.pr_cstime);
  1009. if (sizeof(elf_gregset_t) != sizeof(struct pt_regs)) {
  1010. printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
  1011. "(%d)\n", sizeof(elf_gregset_t), sizeof(struct pt_regs));
  1012. } else {
  1013. *(struct pt_regs *)&prstatus.pr_reg = *regs;
  1014. }
  1015. notes[1].name = "CORE";
  1016. notes[1].type = NT_PRPSINFO;
  1017. notes[1].datasz = sizeof(psinfo);
  1018. notes[1].data = &psinfo;
  1019. i = current->state ? ffz(~current->state) + 1 : 0;
  1020. psinfo.pr_state = i;
  1021. psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
  1022. psinfo.pr_zomb = psinfo.pr_sname == 'Z';
  1023. psinfo.pr_nice = task_nice(current);
  1024. psinfo.pr_flag = current->flags;
  1025. psinfo.pr_uid = current->uid;
  1026. psinfo.pr_gid = current->gid;
  1027. {
  1028. int i, len;
  1029. set_fs(fs);
  1030. len = current->mm->arg_end - current->mm->arg_start;
  1031. len = len >= ELF_PRARGSZ ? ELF_PRARGSZ : len;
  1032. (void *) copy_from_user(&psinfo.pr_psargs,
  1033. (const char __user *)current->mm->arg_start, len);
  1034. for (i = 0; i < len; i++)
  1035. if (psinfo.pr_psargs[i] == 0)
  1036. psinfo.pr_psargs[i] = ' ';
  1037. psinfo.pr_psargs[len] = 0;
  1038. set_fs(KERNEL_DS);
  1039. }
  1040. strlcpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
  1041. notes[2].name = "CORE";
  1042. notes[2].type = NT_TASKSTRUCT;
  1043. notes[2].datasz = sizeof(*current);
  1044. notes[2].data = current;
  1045. /* Try to dump the FPU. */
  1046. prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
  1047. if (!prstatus.pr_fpvalid) {
  1048. numnote--;
  1049. } else {
  1050. notes[3].name = "CORE";
  1051. notes[3].type = NT_PRFPREG;
  1052. notes[3].datasz = sizeof(fpu);
  1053. notes[3].data = &fpu;
  1054. }
  1055. /* Write notes phdr entry. */
  1056. {
  1057. struct elf_phdr phdr;
  1058. int sz = 0;
  1059. for(i = 0; i < numnote; i++)
  1060. sz += notesize(&notes[i]);
  1061. phdr.p_type = PT_NOTE;
  1062. phdr.p_offset = offset;
  1063. phdr.p_vaddr = 0;
  1064. phdr.p_paddr = 0;
  1065. phdr.p_filesz = sz;
  1066. phdr.p_memsz = 0;
  1067. phdr.p_flags = 0;
  1068. phdr.p_align = 0;
  1069. offset += phdr.p_filesz;
  1070. DUMP_WRITE(&phdr, sizeof(phdr));
  1071. }
  1072. /* Page-align dumped data. */
  1073. dataoff = offset = roundup(offset, PAGE_SIZE);
  1074. /* Write program headers for segments dump. */
  1075. for(vma = current->mm->mmap, i = 0;
  1076. i < segs && vma != NULL; vma = vma->vm_next) {
  1077. struct elf_phdr phdr;
  1078. size_t sz;
  1079. i++;
  1080. sz = vma->vm_end - vma->vm_start;
  1081. phdr.p_type = PT_LOAD;
  1082. phdr.p_offset = offset;
  1083. phdr.p_vaddr = vma->vm_start;
  1084. phdr.p_paddr = 0;
  1085. phdr.p_filesz = maydump(vma) ? sz : 0;
  1086. phdr.p_memsz = sz;
  1087. offset += phdr.p_filesz;
  1088. phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
  1089. if (vma->vm_flags & VM_WRITE)
  1090. phdr.p_flags |= PF_W;
  1091. if (vma->vm_flags & VM_EXEC)
  1092. phdr.p_flags |= PF_X;
  1093. phdr.p_align = PAGE_SIZE;
  1094. DUMP_WRITE(&phdr, sizeof(phdr));
  1095. }
  1096. for(i = 0; i < numnote; i++)
  1097. if (!writenote(&notes[i], file))
  1098. goto end_coredump;
  1099. set_fs(fs);
  1100. DUMP_SEEK(dataoff);
  1101. for(i = 0, vma = current->mm->mmap;
  1102. i < segs && vma != NULL;
  1103. vma = vma->vm_next) {
  1104. unsigned long addr = vma->vm_start;
  1105. unsigned long len = vma->vm_end - vma->vm_start;
  1106. if (!maydump(vma))
  1107. continue;
  1108. i++;
  1109. #ifdef DEBUG
  1110. printk("elf_core_dump: writing %08lx %lx\n", addr, len);
  1111. #endif
  1112. DUMP_WRITE((void __user *)addr, len);
  1113. }
  1114. if ((off_t) file->f_pos != offset) {
  1115. /* Sanity check. */
  1116. printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
  1117. (off_t) file->f_pos, offset);
  1118. }
  1119. end_coredump:
  1120. set_fs(fs);
  1121. return has_dumped;
  1122. }
  1123. static int __init init_irix_binfmt(void)
  1124. {
  1125. extern int init_inventory(void);
  1126. extern asmlinkage unsigned long sys_call_table;
  1127. extern asmlinkage unsigned long sys_call_table_irix5;
  1128. init_inventory();
  1129. /*
  1130. * Copy the IRIX5 syscall table (8000 bytes) into the main syscall
  1131. * table. The IRIX5 calls are located by an offset of 8000 bytes
  1132. * from the beginning of the main table.
  1133. */
  1134. memcpy((void *) ((unsigned long) &sys_call_table + 8000),
  1135. &sys_call_table_irix5, 8000);
  1136. return register_binfmt(&irix_format);
  1137. }
  1138. static void __exit exit_irix_binfmt(void)
  1139. {
  1140. /*
  1141. * Remove the Irix ELF loader.
  1142. */
  1143. unregister_binfmt(&irix_format);
  1144. }
  1145. module_init(init_irix_binfmt)
  1146. module_exit(exit_irix_binfmt)