irixelf.c 34 KB

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