binfmt_elf_fdpic.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. /* binfmt_elf_fdpic.c: FDPIC ELF binary format
  2. *
  3. * Copyright (C) 2003, 2004, 2006 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * Derived from binfmt_elf.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/stat.h>
  15. #include <linux/sched.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/errno.h>
  19. #include <linux/signal.h>
  20. #include <linux/binfmts.h>
  21. #include <linux/string.h>
  22. #include <linux/file.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/slab.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/highmem.h>
  27. #include <linux/highuid.h>
  28. #include <linux/personality.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/init.h>
  31. #include <linux/elf.h>
  32. #include <linux/elf-fdpic.h>
  33. #include <linux/elfcore.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/param.h>
  36. #include <asm/pgalloc.h>
  37. typedef char *elf_caddr_t;
  38. #if 0
  39. #define kdebug(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
  40. #else
  41. #define kdebug(fmt, ...) do {} while(0)
  42. #endif
  43. #if 0
  44. #define kdcore(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
  45. #else
  46. #define kdcore(fmt, ...) do {} while(0)
  47. #endif
  48. MODULE_LICENSE("GPL");
  49. static int load_elf_fdpic_binary(struct linux_binprm *, struct pt_regs *);
  50. static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *, struct file *);
  51. static int elf_fdpic_map_file(struct elf_fdpic_params *, struct file *,
  52. struct mm_struct *, const char *);
  53. static int create_elf_fdpic_tables(struct linux_binprm *, struct mm_struct *,
  54. struct elf_fdpic_params *,
  55. struct elf_fdpic_params *);
  56. #ifndef CONFIG_MMU
  57. static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *,
  58. unsigned long *);
  59. static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *,
  60. struct file *,
  61. struct mm_struct *);
  62. #endif
  63. static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *,
  64. struct file *, struct mm_struct *);
  65. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  66. static int elf_fdpic_core_dump(long, struct pt_regs *, struct file *);
  67. #endif
  68. static struct linux_binfmt elf_fdpic_format = {
  69. .module = THIS_MODULE,
  70. .load_binary = load_elf_fdpic_binary,
  71. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  72. .core_dump = elf_fdpic_core_dump,
  73. #endif
  74. .min_coredump = ELF_EXEC_PAGESIZE,
  75. };
  76. static int __init init_elf_fdpic_binfmt(void)
  77. {
  78. return register_binfmt(&elf_fdpic_format);
  79. }
  80. static void __exit exit_elf_fdpic_binfmt(void)
  81. {
  82. unregister_binfmt(&elf_fdpic_format);
  83. }
  84. core_initcall(init_elf_fdpic_binfmt);
  85. module_exit(exit_elf_fdpic_binfmt);
  86. static int is_elf_fdpic(struct elfhdr *hdr, struct file *file)
  87. {
  88. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
  89. return 0;
  90. if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
  91. return 0;
  92. if (!elf_check_arch(hdr) || !elf_check_fdpic(hdr))
  93. return 0;
  94. if (!file->f_op || !file->f_op->mmap)
  95. return 0;
  96. return 1;
  97. }
  98. /*****************************************************************************/
  99. /*
  100. * read the program headers table into memory
  101. */
  102. static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,
  103. struct file *file)
  104. {
  105. struct elf32_phdr *phdr;
  106. unsigned long size;
  107. int retval, loop;
  108. if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
  109. return -ENOMEM;
  110. if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr))
  111. return -ENOMEM;
  112. size = params->hdr.e_phnum * sizeof(struct elf_phdr);
  113. params->phdrs = kmalloc(size, GFP_KERNEL);
  114. if (!params->phdrs)
  115. return -ENOMEM;
  116. retval = kernel_read(file, params->hdr.e_phoff,
  117. (char *) params->phdrs, size);
  118. if (retval < 0)
  119. return retval;
  120. /* determine stack size for this binary */
  121. phdr = params->phdrs;
  122. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  123. if (phdr->p_type != PT_GNU_STACK)
  124. continue;
  125. if (phdr->p_flags & PF_X)
  126. params->flags |= ELF_FDPIC_FLAG_EXEC_STACK;
  127. else
  128. params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK;
  129. params->stack_size = phdr->p_memsz;
  130. break;
  131. }
  132. return 0;
  133. }
  134. /*****************************************************************************/
  135. /*
  136. * load an fdpic binary into various bits of memory
  137. */
  138. static int load_elf_fdpic_binary(struct linux_binprm *bprm,
  139. struct pt_regs *regs)
  140. {
  141. struct elf_fdpic_params exec_params, interp_params;
  142. struct elf_phdr *phdr;
  143. unsigned long stack_size, entryaddr;
  144. #ifndef CONFIG_MMU
  145. unsigned long fullsize;
  146. #endif
  147. #ifdef ELF_FDPIC_PLAT_INIT
  148. unsigned long dynaddr;
  149. #endif
  150. struct file *interpreter = NULL; /* to shut gcc up */
  151. char *interpreter_name = NULL;
  152. int executable_stack;
  153. int retval, i;
  154. kdebug("____ LOAD %d ____", current->pid);
  155. memset(&exec_params, 0, sizeof(exec_params));
  156. memset(&interp_params, 0, sizeof(interp_params));
  157. exec_params.hdr = *(struct elfhdr *) bprm->buf;
  158. exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE;
  159. /* check that this is a binary we know how to deal with */
  160. retval = -ENOEXEC;
  161. if (!is_elf_fdpic(&exec_params.hdr, bprm->file))
  162. goto error;
  163. /* read the program header table */
  164. retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
  165. if (retval < 0)
  166. goto error;
  167. /* scan for a program header that specifies an interpreter */
  168. phdr = exec_params.phdrs;
  169. for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) {
  170. switch (phdr->p_type) {
  171. case PT_INTERP:
  172. retval = -ENOMEM;
  173. if (phdr->p_filesz > PATH_MAX)
  174. goto error;
  175. retval = -ENOENT;
  176. if (phdr->p_filesz < 2)
  177. goto error;
  178. /* read the name of the interpreter into memory */
  179. interpreter_name = kmalloc(phdr->p_filesz, GFP_KERNEL);
  180. if (!interpreter_name)
  181. goto error;
  182. retval = kernel_read(bprm->file,
  183. phdr->p_offset,
  184. interpreter_name,
  185. phdr->p_filesz);
  186. if (retval < 0)
  187. goto error;
  188. retval = -ENOENT;
  189. if (interpreter_name[phdr->p_filesz - 1] != '\0')
  190. goto error;
  191. kdebug("Using ELF interpreter %s", interpreter_name);
  192. /* replace the program with the interpreter */
  193. interpreter = open_exec(interpreter_name);
  194. retval = PTR_ERR(interpreter);
  195. if (IS_ERR(interpreter)) {
  196. interpreter = NULL;
  197. goto error;
  198. }
  199. /*
  200. * If the binary is not readable then enforce
  201. * mm->dumpable = 0 regardless of the interpreter's
  202. * permissions.
  203. */
  204. if (file_permission(interpreter, MAY_READ) < 0)
  205. bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
  206. retval = kernel_read(interpreter, 0, bprm->buf,
  207. BINPRM_BUF_SIZE);
  208. if (retval < 0)
  209. goto error;
  210. interp_params.hdr = *((struct elfhdr *) bprm->buf);
  211. break;
  212. case PT_LOAD:
  213. #ifdef CONFIG_MMU
  214. if (exec_params.load_addr == 0)
  215. exec_params.load_addr = phdr->p_vaddr;
  216. #endif
  217. break;
  218. }
  219. }
  220. if (elf_check_const_displacement(&exec_params.hdr))
  221. exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
  222. /* perform insanity checks on the interpreter */
  223. if (interpreter_name) {
  224. retval = -ELIBBAD;
  225. if (!is_elf_fdpic(&interp_params.hdr, interpreter))
  226. goto error;
  227. interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
  228. /* read the interpreter's program header table */
  229. retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
  230. if (retval < 0)
  231. goto error;
  232. }
  233. stack_size = exec_params.stack_size;
  234. if (stack_size < interp_params.stack_size)
  235. stack_size = interp_params.stack_size;
  236. if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
  237. executable_stack = EXSTACK_ENABLE_X;
  238. else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
  239. executable_stack = EXSTACK_DISABLE_X;
  240. else if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
  241. executable_stack = EXSTACK_ENABLE_X;
  242. else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
  243. executable_stack = EXSTACK_DISABLE_X;
  244. else
  245. executable_stack = EXSTACK_DEFAULT;
  246. retval = -ENOEXEC;
  247. if (stack_size == 0)
  248. goto error;
  249. if (elf_check_const_displacement(&interp_params.hdr))
  250. interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
  251. /* flush all traces of the currently running executable */
  252. retval = flush_old_exec(bprm);
  253. if (retval)
  254. goto error;
  255. /* there's now no turning back... the old userspace image is dead,
  256. * defunct, deceased, etc. after this point we have to exit via
  257. * error_kill */
  258. set_personality(PER_LINUX_FDPIC);
  259. set_binfmt(&elf_fdpic_format);
  260. current->mm->start_code = 0;
  261. current->mm->end_code = 0;
  262. current->mm->start_stack = 0;
  263. current->mm->start_data = 0;
  264. current->mm->end_data = 0;
  265. current->mm->context.exec_fdpic_loadmap = 0;
  266. current->mm->context.interp_fdpic_loadmap = 0;
  267. current->flags &= ~PF_FORKNOEXEC;
  268. #ifdef CONFIG_MMU
  269. elf_fdpic_arch_lay_out_mm(&exec_params,
  270. &interp_params,
  271. &current->mm->start_stack,
  272. &current->mm->start_brk);
  273. retval = setup_arg_pages(bprm, current->mm->start_stack,
  274. executable_stack);
  275. if (retval < 0) {
  276. send_sig(SIGKILL, current, 0);
  277. goto error_kill;
  278. }
  279. #endif
  280. /* load the executable and interpreter into memory */
  281. retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm,
  282. "executable");
  283. if (retval < 0)
  284. goto error_kill;
  285. if (interpreter_name) {
  286. retval = elf_fdpic_map_file(&interp_params, interpreter,
  287. current->mm, "interpreter");
  288. if (retval < 0) {
  289. printk(KERN_ERR "Unable to load interpreter\n");
  290. goto error_kill;
  291. }
  292. allow_write_access(interpreter);
  293. fput(interpreter);
  294. interpreter = NULL;
  295. }
  296. #ifdef CONFIG_MMU
  297. if (!current->mm->start_brk)
  298. current->mm->start_brk = current->mm->end_data;
  299. current->mm->brk = current->mm->start_brk =
  300. PAGE_ALIGN(current->mm->start_brk);
  301. #else
  302. /* create a stack and brk area big enough for everyone
  303. * - the brk heap starts at the bottom and works up
  304. * - the stack starts at the top and works down
  305. */
  306. stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
  307. if (stack_size < PAGE_SIZE * 2)
  308. stack_size = PAGE_SIZE * 2;
  309. down_write(&current->mm->mmap_sem);
  310. current->mm->start_brk = do_mmap(NULL, 0, stack_size,
  311. PROT_READ | PROT_WRITE | PROT_EXEC,
  312. MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN,
  313. 0);
  314. if (IS_ERR_VALUE(current->mm->start_brk)) {
  315. up_write(&current->mm->mmap_sem);
  316. retval = current->mm->start_brk;
  317. current->mm->start_brk = 0;
  318. goto error_kill;
  319. }
  320. /* expand the stack mapping to use up the entire allocation granule */
  321. fullsize = ksize((char *) current->mm->start_brk);
  322. if (!IS_ERR_VALUE(do_mremap(current->mm->start_brk, stack_size,
  323. fullsize, 0, 0)))
  324. stack_size = fullsize;
  325. up_write(&current->mm->mmap_sem);
  326. current->mm->brk = current->mm->start_brk;
  327. current->mm->context.end_brk = current->mm->start_brk;
  328. current->mm->context.end_brk +=
  329. (stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0;
  330. current->mm->start_stack = current->mm->start_brk + stack_size;
  331. #endif
  332. compute_creds(bprm);
  333. current->flags &= ~PF_FORKNOEXEC;
  334. if (create_elf_fdpic_tables(bprm, current->mm,
  335. &exec_params, &interp_params) < 0)
  336. goto error_kill;
  337. kdebug("- start_code %lx", current->mm->start_code);
  338. kdebug("- end_code %lx", current->mm->end_code);
  339. kdebug("- start_data %lx", current->mm->start_data);
  340. kdebug("- end_data %lx", current->mm->end_data);
  341. kdebug("- start_brk %lx", current->mm->start_brk);
  342. kdebug("- brk %lx", current->mm->brk);
  343. kdebug("- start_stack %lx", current->mm->start_stack);
  344. #ifdef ELF_FDPIC_PLAT_INIT
  345. /*
  346. * The ABI may specify that certain registers be set up in special
  347. * ways (on i386 %edx is the address of a DT_FINI function, for
  348. * example. This macro performs whatever initialization to
  349. * the regs structure is required.
  350. */
  351. dynaddr = interp_params.dynamic_addr ?: exec_params.dynamic_addr;
  352. ELF_FDPIC_PLAT_INIT(regs, exec_params.map_addr, interp_params.map_addr,
  353. dynaddr);
  354. #endif
  355. /* everything is now ready... get the userspace context ready to roll */
  356. entryaddr = interp_params.entry_addr ?: exec_params.entry_addr;
  357. start_thread(regs, entryaddr, current->mm->start_stack);
  358. if (unlikely(current->ptrace & PT_PTRACED)) {
  359. if (current->ptrace & PT_TRACE_EXEC)
  360. ptrace_notify((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
  361. else
  362. send_sig(SIGTRAP, current, 0);
  363. }
  364. retval = 0;
  365. error:
  366. if (interpreter) {
  367. allow_write_access(interpreter);
  368. fput(interpreter);
  369. }
  370. kfree(interpreter_name);
  371. kfree(exec_params.phdrs);
  372. kfree(exec_params.loadmap);
  373. kfree(interp_params.phdrs);
  374. kfree(interp_params.loadmap);
  375. return retval;
  376. /* unrecoverable error - kill the process */
  377. error_kill:
  378. send_sig(SIGSEGV, current, 0);
  379. goto error;
  380. }
  381. /*****************************************************************************/
  382. /*
  383. * present useful information to the program
  384. */
  385. static int create_elf_fdpic_tables(struct linux_binprm *bprm,
  386. struct mm_struct *mm,
  387. struct elf_fdpic_params *exec_params,
  388. struct elf_fdpic_params *interp_params)
  389. {
  390. unsigned long sp, csp, nitems;
  391. elf_caddr_t __user *argv, *envp;
  392. size_t platform_len = 0, len;
  393. char *k_platform;
  394. char __user *u_platform, *p;
  395. long hwcap;
  396. int loop;
  397. /* we're going to shovel a whole load of stuff onto the stack */
  398. #ifdef CONFIG_MMU
  399. sp = bprm->p;
  400. #else
  401. sp = mm->start_stack;
  402. /* stack the program arguments and environment */
  403. if (elf_fdpic_transfer_args_to_stack(bprm, &sp) < 0)
  404. return -EFAULT;
  405. #endif
  406. /* get hold of platform and hardware capabilities masks for the machine
  407. * we are running on. In some cases (Sparc), this info is impossible
  408. * to get, in others (i386) it is merely difficult.
  409. */
  410. hwcap = ELF_HWCAP;
  411. k_platform = ELF_PLATFORM;
  412. u_platform = NULL;
  413. if (k_platform) {
  414. platform_len = strlen(k_platform) + 1;
  415. sp -= platform_len;
  416. u_platform = (char __user *) sp;
  417. if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
  418. return -EFAULT;
  419. }
  420. #if defined(__i386__) && defined(CONFIG_SMP)
  421. /* in some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
  422. * by the processes running on the same package. One thing we can do is
  423. * to shuffle the initial stack for them.
  424. *
  425. * the conditionals here are unneeded, but kept in to make the code
  426. * behaviour the same as pre change unless we have hyperthreaded
  427. * processors. This keeps Mr Marcelo Person happier but should be
  428. * removed for 2.5
  429. */
  430. if (smp_num_siblings > 1)
  431. sp = sp - ((current->pid % 64) << 7);
  432. #endif
  433. sp &= ~7UL;
  434. /* stack the load map(s) */
  435. len = sizeof(struct elf32_fdpic_loadmap);
  436. len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
  437. sp = (sp - len) & ~7UL;
  438. exec_params->map_addr = sp;
  439. if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
  440. return -EFAULT;
  441. current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
  442. if (interp_params->loadmap) {
  443. len = sizeof(struct elf32_fdpic_loadmap);
  444. len += sizeof(struct elf32_fdpic_loadseg) *
  445. interp_params->loadmap->nsegs;
  446. sp = (sp - len) & ~7UL;
  447. interp_params->map_addr = sp;
  448. if (copy_to_user((void __user *) sp, interp_params->loadmap,
  449. len) != 0)
  450. return -EFAULT;
  451. current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
  452. }
  453. /* force 16 byte _final_ alignment here for generality */
  454. #define DLINFO_ITEMS 13
  455. nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0);
  456. #ifdef DLINFO_ARCH_ITEMS
  457. nitems += DLINFO_ARCH_ITEMS;
  458. #endif
  459. csp = sp;
  460. sp -= nitems * 2 * sizeof(unsigned long);
  461. sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
  462. sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
  463. sp -= 1 * sizeof(unsigned long); /* argc */
  464. csp -= sp & 15UL;
  465. sp -= sp & 15UL;
  466. /* put the ELF interpreter info on the stack */
  467. #define NEW_AUX_ENT(nr, id, val) \
  468. do { \
  469. struct { unsigned long _id, _val; } __user *ent; \
  470. \
  471. ent = (void __user *) csp; \
  472. __put_user((id), &ent[nr]._id); \
  473. __put_user((val), &ent[nr]._val); \
  474. } while (0)
  475. csp -= 2 * sizeof(unsigned long);
  476. NEW_AUX_ENT(0, AT_NULL, 0);
  477. if (k_platform) {
  478. csp -= 2 * sizeof(unsigned long);
  479. NEW_AUX_ENT(0, AT_PLATFORM,
  480. (elf_addr_t) (unsigned long) u_platform);
  481. }
  482. csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
  483. NEW_AUX_ENT( 0, AT_HWCAP, hwcap);
  484. NEW_AUX_ENT( 1, AT_PAGESZ, PAGE_SIZE);
  485. NEW_AUX_ENT( 2, AT_CLKTCK, CLOCKS_PER_SEC);
  486. NEW_AUX_ENT( 3, AT_PHDR, exec_params->ph_addr);
  487. NEW_AUX_ENT( 4, AT_PHENT, sizeof(struct elf_phdr));
  488. NEW_AUX_ENT( 5, AT_PHNUM, exec_params->hdr.e_phnum);
  489. NEW_AUX_ENT( 6, AT_BASE, interp_params->elfhdr_addr);
  490. NEW_AUX_ENT( 7, AT_FLAGS, 0);
  491. NEW_AUX_ENT( 8, AT_ENTRY, exec_params->entry_addr);
  492. NEW_AUX_ENT( 9, AT_UID, (elf_addr_t) current->uid);
  493. NEW_AUX_ENT(10, AT_EUID, (elf_addr_t) current->euid);
  494. NEW_AUX_ENT(11, AT_GID, (elf_addr_t) current->gid);
  495. NEW_AUX_ENT(12, AT_EGID, (elf_addr_t) current->egid);
  496. #ifdef ARCH_DLINFO
  497. /* ARCH_DLINFO must come last so platform specific code can enforce
  498. * special alignment requirements on the AUXV if necessary (eg. PPC).
  499. */
  500. ARCH_DLINFO;
  501. #endif
  502. #undef NEW_AUX_ENT
  503. /* allocate room for argv[] and envv[] */
  504. csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
  505. envp = (elf_caddr_t __user *) csp;
  506. csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
  507. argv = (elf_caddr_t __user *) csp;
  508. /* stack argc */
  509. csp -= sizeof(unsigned long);
  510. __put_user(bprm->argc, (unsigned long __user *) csp);
  511. BUG_ON(csp != sp);
  512. /* fill in the argv[] array */
  513. #ifdef CONFIG_MMU
  514. current->mm->arg_start = bprm->p;
  515. #else
  516. current->mm->arg_start = current->mm->start_stack -
  517. (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
  518. #endif
  519. p = (char __user *) current->mm->arg_start;
  520. for (loop = bprm->argc; loop > 0; loop--) {
  521. __put_user((elf_caddr_t) p, argv++);
  522. len = strnlen_user(p, MAX_ARG_STRLEN);
  523. if (!len || len > MAX_ARG_STRLEN)
  524. return -EINVAL;
  525. p += len;
  526. }
  527. __put_user(NULL, argv);
  528. current->mm->arg_end = (unsigned long) p;
  529. /* fill in the envv[] array */
  530. current->mm->env_start = (unsigned long) p;
  531. for (loop = bprm->envc; loop > 0; loop--) {
  532. __put_user((elf_caddr_t)(unsigned long) p, envp++);
  533. len = strnlen_user(p, MAX_ARG_STRLEN);
  534. if (!len || len > MAX_ARG_STRLEN)
  535. return -EINVAL;
  536. p += len;
  537. }
  538. __put_user(NULL, envp);
  539. current->mm->env_end = (unsigned long) p;
  540. mm->start_stack = (unsigned long) sp;
  541. return 0;
  542. }
  543. /*****************************************************************************/
  544. /*
  545. * transfer the program arguments and environment from the holding pages onto
  546. * the stack
  547. */
  548. #ifndef CONFIG_MMU
  549. static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm,
  550. unsigned long *_sp)
  551. {
  552. unsigned long index, stop, sp;
  553. char *src;
  554. int ret = 0;
  555. stop = bprm->p >> PAGE_SHIFT;
  556. sp = *_sp;
  557. for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
  558. src = kmap(bprm->page[index]);
  559. sp -= PAGE_SIZE;
  560. if (copy_to_user((void *) sp, src, PAGE_SIZE) != 0)
  561. ret = -EFAULT;
  562. kunmap(bprm->page[index]);
  563. if (ret < 0)
  564. goto out;
  565. }
  566. *_sp = (*_sp - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p)) & ~15;
  567. out:
  568. return ret;
  569. }
  570. #endif
  571. /*****************************************************************************/
  572. /*
  573. * load the appropriate binary image (executable or interpreter) into memory
  574. * - we assume no MMU is available
  575. * - if no other PIC bits are set in params->hdr->e_flags
  576. * - we assume that the LOADable segments in the binary are independently relocatable
  577. * - we assume R/O executable segments are shareable
  578. * - else
  579. * - we assume the loadable parts of the image to require fixed displacement
  580. * - the image is not shareable
  581. */
  582. static int elf_fdpic_map_file(struct elf_fdpic_params *params,
  583. struct file *file,
  584. struct mm_struct *mm,
  585. const char *what)
  586. {
  587. struct elf32_fdpic_loadmap *loadmap;
  588. #ifdef CONFIG_MMU
  589. struct elf32_fdpic_loadseg *mseg;
  590. #endif
  591. struct elf32_fdpic_loadseg *seg;
  592. struct elf32_phdr *phdr;
  593. unsigned long load_addr, stop;
  594. unsigned nloads, tmp;
  595. size_t size;
  596. int loop, ret;
  597. /* allocate a load map table */
  598. nloads = 0;
  599. for (loop = 0; loop < params->hdr.e_phnum; loop++)
  600. if (params->phdrs[loop].p_type == PT_LOAD)
  601. nloads++;
  602. if (nloads == 0)
  603. return -ELIBBAD;
  604. size = sizeof(*loadmap) + nloads * sizeof(*seg);
  605. loadmap = kzalloc(size, GFP_KERNEL);
  606. if (!loadmap)
  607. return -ENOMEM;
  608. params->loadmap = loadmap;
  609. loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
  610. loadmap->nsegs = nloads;
  611. load_addr = params->load_addr;
  612. seg = loadmap->segs;
  613. /* map the requested LOADs into the memory space */
  614. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  615. case ELF_FDPIC_FLAG_CONSTDISP:
  616. case ELF_FDPIC_FLAG_CONTIGUOUS:
  617. #ifndef CONFIG_MMU
  618. ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
  619. if (ret < 0)
  620. return ret;
  621. break;
  622. #endif
  623. default:
  624. ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
  625. if (ret < 0)
  626. return ret;
  627. break;
  628. }
  629. /* map the entry point */
  630. if (params->hdr.e_entry) {
  631. seg = loadmap->segs;
  632. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  633. if (params->hdr.e_entry >= seg->p_vaddr &&
  634. params->hdr.e_entry < seg->p_vaddr + seg->p_memsz) {
  635. params->entry_addr =
  636. (params->hdr.e_entry - seg->p_vaddr) +
  637. seg->addr;
  638. break;
  639. }
  640. }
  641. }
  642. /* determine where the program header table has wound up if mapped */
  643. stop = params->hdr.e_phoff;
  644. stop += params->hdr.e_phnum * sizeof (struct elf_phdr);
  645. phdr = params->phdrs;
  646. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  647. if (phdr->p_type != PT_LOAD)
  648. continue;
  649. if (phdr->p_offset > params->hdr.e_phoff ||
  650. phdr->p_offset + phdr->p_filesz < stop)
  651. continue;
  652. seg = loadmap->segs;
  653. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  654. if (phdr->p_vaddr >= seg->p_vaddr &&
  655. phdr->p_vaddr + phdr->p_filesz <=
  656. seg->p_vaddr + seg->p_memsz) {
  657. params->ph_addr =
  658. (phdr->p_vaddr - seg->p_vaddr) +
  659. seg->addr +
  660. params->hdr.e_phoff - phdr->p_offset;
  661. break;
  662. }
  663. }
  664. break;
  665. }
  666. /* determine where the dynamic section has wound up if there is one */
  667. phdr = params->phdrs;
  668. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  669. if (phdr->p_type != PT_DYNAMIC)
  670. continue;
  671. seg = loadmap->segs;
  672. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  673. if (phdr->p_vaddr >= seg->p_vaddr &&
  674. phdr->p_vaddr + phdr->p_memsz <=
  675. seg->p_vaddr + seg->p_memsz) {
  676. params->dynamic_addr =
  677. (phdr->p_vaddr - seg->p_vaddr) +
  678. seg->addr;
  679. /* check the dynamic section contains at least
  680. * one item, and that the last item is a NULL
  681. * entry */
  682. if (phdr->p_memsz == 0 ||
  683. phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
  684. goto dynamic_error;
  685. tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
  686. if (((Elf32_Dyn *)
  687. params->dynamic_addr)[tmp - 1].d_tag != 0)
  688. goto dynamic_error;
  689. break;
  690. }
  691. }
  692. break;
  693. }
  694. /* now elide adjacent segments in the load map on MMU linux
  695. * - on uClinux the holes between may actually be filled with system
  696. * stuff or stuff from other processes
  697. */
  698. #ifdef CONFIG_MMU
  699. nloads = loadmap->nsegs;
  700. mseg = loadmap->segs;
  701. seg = mseg + 1;
  702. for (loop = 1; loop < nloads; loop++) {
  703. /* see if we have a candidate for merging */
  704. if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
  705. load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
  706. if (load_addr == (seg->addr & PAGE_MASK)) {
  707. mseg->p_memsz +=
  708. load_addr -
  709. (mseg->addr + mseg->p_memsz);
  710. mseg->p_memsz += seg->addr & ~PAGE_MASK;
  711. mseg->p_memsz += seg->p_memsz;
  712. loadmap->nsegs--;
  713. continue;
  714. }
  715. }
  716. mseg++;
  717. if (mseg != seg)
  718. *mseg = *seg;
  719. }
  720. #endif
  721. kdebug("Mapped Object [%s]:", what);
  722. kdebug("- elfhdr : %lx", params->elfhdr_addr);
  723. kdebug("- entry : %lx", params->entry_addr);
  724. kdebug("- PHDR[] : %lx", params->ph_addr);
  725. kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
  726. seg = loadmap->segs;
  727. for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
  728. kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
  729. loop,
  730. seg->addr, seg->addr + seg->p_memsz - 1,
  731. seg->p_vaddr, seg->p_memsz);
  732. return 0;
  733. dynamic_error:
  734. printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
  735. what, file->f_path.dentry->d_inode->i_ino);
  736. return -ELIBBAD;
  737. }
  738. /*****************************************************************************/
  739. /*
  740. * map a file with constant displacement under uClinux
  741. */
  742. #ifndef CONFIG_MMU
  743. static int elf_fdpic_map_file_constdisp_on_uclinux(
  744. struct elf_fdpic_params *params,
  745. struct file *file,
  746. struct mm_struct *mm)
  747. {
  748. struct elf32_fdpic_loadseg *seg;
  749. struct elf32_phdr *phdr;
  750. unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
  751. loff_t fpos;
  752. int loop, ret;
  753. load_addr = params->load_addr;
  754. seg = params->loadmap->segs;
  755. /* determine the bounds of the contiguous overall allocation we must
  756. * make */
  757. phdr = params->phdrs;
  758. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  759. if (params->phdrs[loop].p_type != PT_LOAD)
  760. continue;
  761. if (base > phdr->p_vaddr)
  762. base = phdr->p_vaddr;
  763. if (top < phdr->p_vaddr + phdr->p_memsz)
  764. top = phdr->p_vaddr + phdr->p_memsz;
  765. }
  766. /* allocate one big anon block for everything */
  767. mflags = MAP_PRIVATE;
  768. if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
  769. mflags |= MAP_EXECUTABLE;
  770. down_write(&mm->mmap_sem);
  771. maddr = do_mmap(NULL, load_addr, top - base,
  772. PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
  773. up_write(&mm->mmap_sem);
  774. if (IS_ERR_VALUE(maddr))
  775. return (int) maddr;
  776. if (load_addr != 0)
  777. load_addr += PAGE_ALIGN(top - base);
  778. /* and then load the file segments into it */
  779. phdr = params->phdrs;
  780. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  781. if (params->phdrs[loop].p_type != PT_LOAD)
  782. continue;
  783. fpos = phdr->p_offset;
  784. seg->addr = maddr + (phdr->p_vaddr - base);
  785. seg->p_vaddr = phdr->p_vaddr;
  786. seg->p_memsz = phdr->p_memsz;
  787. ret = file->f_op->read(file, (void *) seg->addr,
  788. phdr->p_filesz, &fpos);
  789. if (ret < 0)
  790. return ret;
  791. /* map the ELF header address if in this segment */
  792. if (phdr->p_offset == 0)
  793. params->elfhdr_addr = seg->addr;
  794. /* clear any space allocated but not loaded */
  795. if (phdr->p_filesz < phdr->p_memsz)
  796. clear_user((void *) (seg->addr + phdr->p_filesz),
  797. phdr->p_memsz - phdr->p_filesz);
  798. if (mm) {
  799. if (phdr->p_flags & PF_X) {
  800. if (!mm->start_code) {
  801. mm->start_code = seg->addr;
  802. mm->end_code = seg->addr +
  803. phdr->p_memsz;
  804. }
  805. } else if (!mm->start_data) {
  806. mm->start_data = seg->addr;
  807. #ifndef CONFIG_MMU
  808. mm->end_data = seg->addr + phdr->p_memsz;
  809. #endif
  810. }
  811. #ifdef CONFIG_MMU
  812. if (seg->addr + phdr->p_memsz > mm->end_data)
  813. mm->end_data = seg->addr + phdr->p_memsz;
  814. #endif
  815. }
  816. seg++;
  817. }
  818. return 0;
  819. }
  820. #endif
  821. /*****************************************************************************/
  822. /*
  823. * map a binary by direct mmap() of the individual PT_LOAD segments
  824. */
  825. static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
  826. struct file *file,
  827. struct mm_struct *mm)
  828. {
  829. struct elf32_fdpic_loadseg *seg;
  830. struct elf32_phdr *phdr;
  831. unsigned long load_addr, delta_vaddr;
  832. int loop, dvset;
  833. load_addr = params->load_addr;
  834. delta_vaddr = 0;
  835. dvset = 0;
  836. seg = params->loadmap->segs;
  837. /* deal with each load segment separately */
  838. phdr = params->phdrs;
  839. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  840. unsigned long maddr, disp, excess, excess1;
  841. int prot = 0, flags;
  842. if (phdr->p_type != PT_LOAD)
  843. continue;
  844. kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
  845. (unsigned long) phdr->p_vaddr,
  846. (unsigned long) phdr->p_offset,
  847. (unsigned long) phdr->p_filesz,
  848. (unsigned long) phdr->p_memsz);
  849. /* determine the mapping parameters */
  850. if (phdr->p_flags & PF_R) prot |= PROT_READ;
  851. if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
  852. if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
  853. flags = MAP_PRIVATE | MAP_DENYWRITE;
  854. if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
  855. flags |= MAP_EXECUTABLE;
  856. maddr = 0;
  857. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  858. case ELF_FDPIC_FLAG_INDEPENDENT:
  859. /* PT_LOADs are independently locatable */
  860. break;
  861. case ELF_FDPIC_FLAG_HONOURVADDR:
  862. /* the specified virtual address must be honoured */
  863. maddr = phdr->p_vaddr;
  864. flags |= MAP_FIXED;
  865. break;
  866. case ELF_FDPIC_FLAG_CONSTDISP:
  867. /* constant displacement
  868. * - can be mapped anywhere, but must be mapped as a
  869. * unit
  870. */
  871. if (!dvset) {
  872. maddr = load_addr;
  873. delta_vaddr = phdr->p_vaddr;
  874. dvset = 1;
  875. } else {
  876. maddr = load_addr + phdr->p_vaddr - delta_vaddr;
  877. flags |= MAP_FIXED;
  878. }
  879. break;
  880. case ELF_FDPIC_FLAG_CONTIGUOUS:
  881. /* contiguity handled later */
  882. break;
  883. default:
  884. BUG();
  885. }
  886. maddr &= PAGE_MASK;
  887. /* create the mapping */
  888. disp = phdr->p_vaddr & ~PAGE_MASK;
  889. down_write(&mm->mmap_sem);
  890. maddr = do_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
  891. phdr->p_offset - disp);
  892. up_write(&mm->mmap_sem);
  893. kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
  894. loop, phdr->p_memsz + disp, prot, flags,
  895. phdr->p_offset - disp, maddr);
  896. if (IS_ERR_VALUE(maddr))
  897. return (int) maddr;
  898. if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
  899. ELF_FDPIC_FLAG_CONTIGUOUS)
  900. load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
  901. seg->addr = maddr + disp;
  902. seg->p_vaddr = phdr->p_vaddr;
  903. seg->p_memsz = phdr->p_memsz;
  904. /* map the ELF header address if in this segment */
  905. if (phdr->p_offset == 0)
  906. params->elfhdr_addr = seg->addr;
  907. /* clear the bit between beginning of mapping and beginning of
  908. * PT_LOAD */
  909. if (prot & PROT_WRITE && disp > 0) {
  910. kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
  911. clear_user((void __user *) maddr, disp);
  912. maddr += disp;
  913. }
  914. /* clear any space allocated but not loaded
  915. * - on uClinux we can just clear the lot
  916. * - on MMU linux we'll get a SIGBUS beyond the last page
  917. * extant in the file
  918. */
  919. excess = phdr->p_memsz - phdr->p_filesz;
  920. excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
  921. #ifdef CONFIG_MMU
  922. if (excess > excess1) {
  923. unsigned long xaddr = maddr + phdr->p_filesz + excess1;
  924. unsigned long xmaddr;
  925. flags |= MAP_FIXED | MAP_ANONYMOUS;
  926. down_write(&mm->mmap_sem);
  927. xmaddr = do_mmap(NULL, xaddr, excess - excess1,
  928. prot, flags, 0);
  929. up_write(&mm->mmap_sem);
  930. kdebug("mmap[%d] <anon>"
  931. " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
  932. loop, xaddr, excess - excess1, prot, flags,
  933. xmaddr);
  934. if (xmaddr != xaddr)
  935. return -ENOMEM;
  936. }
  937. if (prot & PROT_WRITE && excess1 > 0) {
  938. kdebug("clear[%d] ad=%lx sz=%lx",
  939. loop, maddr + phdr->p_filesz, excess1);
  940. clear_user((void __user *) maddr + phdr->p_filesz,
  941. excess1);
  942. }
  943. #else
  944. if (excess > 0) {
  945. kdebug("clear[%d] ad=%lx sz=%lx",
  946. loop, maddr + phdr->p_filesz, excess);
  947. clear_user((void *) maddr + phdr->p_filesz, excess);
  948. }
  949. #endif
  950. if (mm) {
  951. if (phdr->p_flags & PF_X) {
  952. if (!mm->start_code) {
  953. mm->start_code = maddr;
  954. mm->end_code = maddr + phdr->p_memsz;
  955. }
  956. } else if (!mm->start_data) {
  957. mm->start_data = maddr;
  958. mm->end_data = maddr + phdr->p_memsz;
  959. }
  960. }
  961. seg++;
  962. }
  963. return 0;
  964. }
  965. /*****************************************************************************/
  966. /*
  967. * ELF-FDPIC core dumper
  968. *
  969. * Modelled on fs/exec.c:aout_core_dump()
  970. * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  971. *
  972. * Modelled on fs/binfmt_elf.c core dumper
  973. */
  974. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  975. /*
  976. * These are the only things you should do on a core-file: use only these
  977. * functions to write out all the necessary info.
  978. */
  979. static int dump_write(struct file *file, const void *addr, int nr)
  980. {
  981. return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  982. }
  983. static int dump_seek(struct file *file, loff_t off)
  984. {
  985. if (file->f_op->llseek) {
  986. if (file->f_op->llseek(file, off, SEEK_SET) != off)
  987. return 0;
  988. } else {
  989. file->f_pos = off;
  990. }
  991. return 1;
  992. }
  993. /*
  994. * Decide whether a segment is worth dumping; default is yes to be
  995. * sure (missing info is worse than too much; etc).
  996. * Personally I'd include everything, and use the coredump limit...
  997. *
  998. * I think we should skip something. But I am not sure how. H.J.
  999. */
  1000. static int maydump(struct vm_area_struct *vma, unsigned long mm_flags)
  1001. {
  1002. int dump_ok;
  1003. /* Do not dump I/O mapped devices or special mappings */
  1004. if (vma->vm_flags & (VM_IO | VM_RESERVED)) {
  1005. kdcore("%08lx: %08lx: no (IO)", vma->vm_start, vma->vm_flags);
  1006. return 0;
  1007. }
  1008. /* If we may not read the contents, don't allow us to dump
  1009. * them either. "dump_write()" can't handle it anyway.
  1010. */
  1011. if (!(vma->vm_flags & VM_READ)) {
  1012. kdcore("%08lx: %08lx: no (!read)", vma->vm_start, vma->vm_flags);
  1013. return 0;
  1014. }
  1015. /* By default, dump shared memory if mapped from an anonymous file. */
  1016. if (vma->vm_flags & VM_SHARED) {
  1017. if (vma->vm_file->f_path.dentry->d_inode->i_nlink == 0) {
  1018. dump_ok = test_bit(MMF_DUMP_ANON_SHARED, &mm_flags);
  1019. kdcore("%08lx: %08lx: %s (share)", vma->vm_start,
  1020. vma->vm_flags, dump_ok ? "yes" : "no");
  1021. return dump_ok;
  1022. }
  1023. dump_ok = test_bit(MMF_DUMP_MAPPED_SHARED, &mm_flags);
  1024. kdcore("%08lx: %08lx: %s (share)", vma->vm_start,
  1025. vma->vm_flags, dump_ok ? "yes" : "no");
  1026. return dump_ok;
  1027. }
  1028. #ifdef CONFIG_MMU
  1029. /* By default, if it hasn't been written to, don't write it out */
  1030. if (!vma->anon_vma) {
  1031. dump_ok = test_bit(MMF_DUMP_MAPPED_PRIVATE, &mm_flags);
  1032. kdcore("%08lx: %08lx: %s (!anon)", vma->vm_start,
  1033. vma->vm_flags, dump_ok ? "yes" : "no");
  1034. return dump_ok;
  1035. }
  1036. #endif
  1037. dump_ok = test_bit(MMF_DUMP_ANON_PRIVATE, &mm_flags);
  1038. kdcore("%08lx: %08lx: %s", vma->vm_start, vma->vm_flags,
  1039. dump_ok ? "yes" : "no");
  1040. return dump_ok;
  1041. }
  1042. /* An ELF note in memory */
  1043. struct memelfnote
  1044. {
  1045. const char *name;
  1046. int type;
  1047. unsigned int datasz;
  1048. void *data;
  1049. };
  1050. static int notesize(struct memelfnote *en)
  1051. {
  1052. int sz;
  1053. sz = sizeof(struct elf_note);
  1054. sz += roundup(strlen(en->name) + 1, 4);
  1055. sz += roundup(en->datasz, 4);
  1056. return sz;
  1057. }
  1058. /* #define DEBUG */
  1059. #define DUMP_WRITE(addr, nr) \
  1060. do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
  1061. #define DUMP_SEEK(off) \
  1062. do { if (!dump_seek(file, (off))) return 0; } while(0)
  1063. static int writenote(struct memelfnote *men, struct file *file)
  1064. {
  1065. struct elf_note en;
  1066. en.n_namesz = strlen(men->name) + 1;
  1067. en.n_descsz = men->datasz;
  1068. en.n_type = men->type;
  1069. DUMP_WRITE(&en, sizeof(en));
  1070. DUMP_WRITE(men->name, en.n_namesz);
  1071. /* XXX - cast from long long to long to avoid need for libgcc.a */
  1072. DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
  1073. DUMP_WRITE(men->data, men->datasz);
  1074. DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
  1075. return 1;
  1076. }
  1077. #undef DUMP_WRITE
  1078. #undef DUMP_SEEK
  1079. #define DUMP_WRITE(addr, nr) \
  1080. if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
  1081. goto end_coredump;
  1082. #define DUMP_SEEK(off) \
  1083. if (!dump_seek(file, (off))) \
  1084. goto end_coredump;
  1085. static inline void fill_elf_fdpic_header(struct elfhdr *elf, int segs)
  1086. {
  1087. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  1088. elf->e_ident[EI_CLASS] = ELF_CLASS;
  1089. elf->e_ident[EI_DATA] = ELF_DATA;
  1090. elf->e_ident[EI_VERSION] = EV_CURRENT;
  1091. elf->e_ident[EI_OSABI] = ELF_OSABI;
  1092. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  1093. elf->e_type = ET_CORE;
  1094. elf->e_machine = ELF_ARCH;
  1095. elf->e_version = EV_CURRENT;
  1096. elf->e_entry = 0;
  1097. elf->e_phoff = sizeof(struct elfhdr);
  1098. elf->e_shoff = 0;
  1099. elf->e_flags = ELF_FDPIC_CORE_EFLAGS;
  1100. elf->e_ehsize = sizeof(struct elfhdr);
  1101. elf->e_phentsize = sizeof(struct elf_phdr);
  1102. elf->e_phnum = segs;
  1103. elf->e_shentsize = 0;
  1104. elf->e_shnum = 0;
  1105. elf->e_shstrndx = 0;
  1106. return;
  1107. }
  1108. static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
  1109. {
  1110. phdr->p_type = PT_NOTE;
  1111. phdr->p_offset = offset;
  1112. phdr->p_vaddr = 0;
  1113. phdr->p_paddr = 0;
  1114. phdr->p_filesz = sz;
  1115. phdr->p_memsz = 0;
  1116. phdr->p_flags = 0;
  1117. phdr->p_align = 0;
  1118. return;
  1119. }
  1120. static inline void fill_note(struct memelfnote *note, const char *name, int type,
  1121. unsigned int sz, void *data)
  1122. {
  1123. note->name = name;
  1124. note->type = type;
  1125. note->datasz = sz;
  1126. note->data = data;
  1127. return;
  1128. }
  1129. /*
  1130. * fill up all the fields in prstatus from the given task struct, except
  1131. * registers which need to be filled up seperately.
  1132. */
  1133. static void fill_prstatus(struct elf_prstatus *prstatus,
  1134. struct task_struct *p, long signr)
  1135. {
  1136. prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
  1137. prstatus->pr_sigpend = p->pending.signal.sig[0];
  1138. prstatus->pr_sighold = p->blocked.sig[0];
  1139. prstatus->pr_pid = p->pid;
  1140. prstatus->pr_ppid = p->parent->pid;
  1141. prstatus->pr_pgrp = process_group(p);
  1142. prstatus->pr_sid = process_session(p);
  1143. if (thread_group_leader(p)) {
  1144. /*
  1145. * This is the record for the group leader. Add in the
  1146. * cumulative times of previous dead threads. This total
  1147. * won't include the time of each live thread whose state
  1148. * is included in the core dump. The final total reported
  1149. * to our parent process when it calls wait4 will include
  1150. * those sums as well as the little bit more time it takes
  1151. * this and each other thread to finish dying after the
  1152. * core dump synchronization phase.
  1153. */
  1154. cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
  1155. &prstatus->pr_utime);
  1156. cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
  1157. &prstatus->pr_stime);
  1158. } else {
  1159. cputime_to_timeval(p->utime, &prstatus->pr_utime);
  1160. cputime_to_timeval(p->stime, &prstatus->pr_stime);
  1161. }
  1162. cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
  1163. cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
  1164. prstatus->pr_exec_fdpic_loadmap = p->mm->context.exec_fdpic_loadmap;
  1165. prstatus->pr_interp_fdpic_loadmap = p->mm->context.interp_fdpic_loadmap;
  1166. }
  1167. static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
  1168. struct mm_struct *mm)
  1169. {
  1170. unsigned int i, len;
  1171. /* first copy the parameters from user space */
  1172. memset(psinfo, 0, sizeof(struct elf_prpsinfo));
  1173. len = mm->arg_end - mm->arg_start;
  1174. if (len >= ELF_PRARGSZ)
  1175. len = ELF_PRARGSZ - 1;
  1176. if (copy_from_user(&psinfo->pr_psargs,
  1177. (const char __user *) mm->arg_start, len))
  1178. return -EFAULT;
  1179. for (i = 0; i < len; i++)
  1180. if (psinfo->pr_psargs[i] == 0)
  1181. psinfo->pr_psargs[i] = ' ';
  1182. psinfo->pr_psargs[len] = 0;
  1183. psinfo->pr_pid = p->pid;
  1184. psinfo->pr_ppid = p->parent->pid;
  1185. psinfo->pr_pgrp = process_group(p);
  1186. psinfo->pr_sid = process_session(p);
  1187. i = p->state ? ffz(~p->state) + 1 : 0;
  1188. psinfo->pr_state = i;
  1189. psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
  1190. psinfo->pr_zomb = psinfo->pr_sname == 'Z';
  1191. psinfo->pr_nice = task_nice(p);
  1192. psinfo->pr_flag = p->flags;
  1193. SET_UID(psinfo->pr_uid, p->uid);
  1194. SET_GID(psinfo->pr_gid, p->gid);
  1195. strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
  1196. return 0;
  1197. }
  1198. /* Here is the structure in which status of each thread is captured. */
  1199. struct elf_thread_status
  1200. {
  1201. struct list_head list;
  1202. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  1203. elf_fpregset_t fpu; /* NT_PRFPREG */
  1204. struct task_struct *thread;
  1205. #ifdef ELF_CORE_COPY_XFPREGS
  1206. elf_fpxregset_t xfpu; /* NT_PRXFPREG */
  1207. #endif
  1208. struct memelfnote notes[3];
  1209. int num_notes;
  1210. };
  1211. /*
  1212. * In order to add the specific thread information for the elf file format,
  1213. * we need to keep a linked list of every thread's pr_status and then create
  1214. * a single section for them in the final core file.
  1215. */
  1216. static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
  1217. {
  1218. struct task_struct *p = t->thread;
  1219. int sz = 0;
  1220. t->num_notes = 0;
  1221. fill_prstatus(&t->prstatus, p, signr);
  1222. elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
  1223. fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
  1224. &t->prstatus);
  1225. t->num_notes++;
  1226. sz += notesize(&t->notes[0]);
  1227. t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, &t->fpu);
  1228. if (t->prstatus.pr_fpvalid) {
  1229. fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
  1230. &t->fpu);
  1231. t->num_notes++;
  1232. sz += notesize(&t->notes[1]);
  1233. }
  1234. #ifdef ELF_CORE_COPY_XFPREGS
  1235. if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
  1236. fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu),
  1237. &t->xfpu);
  1238. t->num_notes++;
  1239. sz += notesize(&t->notes[2]);
  1240. }
  1241. #endif
  1242. return sz;
  1243. }
  1244. /*
  1245. * dump the segments for an MMU process
  1246. */
  1247. #ifdef CONFIG_MMU
  1248. static int elf_fdpic_dump_segments(struct file *file, size_t *size,
  1249. unsigned long *limit, unsigned long mm_flags)
  1250. {
  1251. struct vm_area_struct *vma;
  1252. for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
  1253. unsigned long addr;
  1254. if (!maydump(vma, mm_flags))
  1255. continue;
  1256. for (addr = vma->vm_start;
  1257. addr < vma->vm_end;
  1258. addr += PAGE_SIZE
  1259. ) {
  1260. struct vm_area_struct *vma;
  1261. struct page *page;
  1262. if (get_user_pages(current, current->mm, addr, 1, 0, 1,
  1263. &page, &vma) <= 0) {
  1264. DUMP_SEEK(file->f_pos + PAGE_SIZE);
  1265. }
  1266. else if (page == ZERO_PAGE(addr)) {
  1267. page_cache_release(page);
  1268. DUMP_SEEK(file->f_pos + PAGE_SIZE);
  1269. }
  1270. else {
  1271. void *kaddr;
  1272. flush_cache_page(vma, addr, page_to_pfn(page));
  1273. kaddr = kmap(page);
  1274. if ((*size += PAGE_SIZE) > *limit ||
  1275. !dump_write(file, kaddr, PAGE_SIZE)
  1276. ) {
  1277. kunmap(page);
  1278. page_cache_release(page);
  1279. return -EIO;
  1280. }
  1281. kunmap(page);
  1282. page_cache_release(page);
  1283. }
  1284. }
  1285. }
  1286. return 0;
  1287. end_coredump:
  1288. return -EFBIG;
  1289. }
  1290. #endif
  1291. /*
  1292. * dump the segments for a NOMMU process
  1293. */
  1294. #ifndef CONFIG_MMU
  1295. static int elf_fdpic_dump_segments(struct file *file, size_t *size,
  1296. unsigned long *limit, unsigned long mm_flags)
  1297. {
  1298. struct vm_list_struct *vml;
  1299. for (vml = current->mm->context.vmlist; vml; vml = vml->next) {
  1300. struct vm_area_struct *vma = vml->vma;
  1301. if (!maydump(vma, mm_flags))
  1302. continue;
  1303. if ((*size += PAGE_SIZE) > *limit)
  1304. return -EFBIG;
  1305. if (!dump_write(file, (void *) vma->vm_start,
  1306. vma->vm_end - vma->vm_start))
  1307. return -EIO;
  1308. }
  1309. return 0;
  1310. }
  1311. #endif
  1312. /*
  1313. * Actual dumper
  1314. *
  1315. * This is a two-pass process; first we find the offsets of the bits,
  1316. * and then they are actually written out. If we run out of core limit
  1317. * we just truncate.
  1318. */
  1319. static int elf_fdpic_core_dump(long signr, struct pt_regs *regs,
  1320. struct file *file)
  1321. {
  1322. #define NUM_NOTES 6
  1323. int has_dumped = 0;
  1324. mm_segment_t fs;
  1325. int segs;
  1326. size_t size = 0;
  1327. int i;
  1328. struct vm_area_struct *vma;
  1329. struct elfhdr *elf = NULL;
  1330. loff_t offset = 0, dataoff;
  1331. unsigned long limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
  1332. int numnote;
  1333. struct memelfnote *notes = NULL;
  1334. struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
  1335. struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
  1336. struct task_struct *g, *p;
  1337. LIST_HEAD(thread_list);
  1338. struct list_head *t;
  1339. elf_fpregset_t *fpu = NULL;
  1340. #ifdef ELF_CORE_COPY_XFPREGS
  1341. elf_fpxregset_t *xfpu = NULL;
  1342. #endif
  1343. int thread_status_size = 0;
  1344. #ifndef CONFIG_MMU
  1345. struct vm_list_struct *vml;
  1346. #endif
  1347. elf_addr_t *auxv;
  1348. unsigned long mm_flags;
  1349. /*
  1350. * We no longer stop all VM operations.
  1351. *
  1352. * This is because those proceses that could possibly change map_count
  1353. * or the mmap / vma pages are now blocked in do_exit on current
  1354. * finishing this core dump.
  1355. *
  1356. * Only ptrace can touch these memory addresses, but it doesn't change
  1357. * the map_count or the pages allocated. So no possibility of crashing
  1358. * exists while dumping the mm->vm_next areas to the core file.
  1359. */
  1360. /* alloc memory for large data structures: too large to be on stack */
  1361. elf = kmalloc(sizeof(*elf), GFP_KERNEL);
  1362. if (!elf)
  1363. goto cleanup;
  1364. prstatus = kzalloc(sizeof(*prstatus), GFP_KERNEL);
  1365. if (!prstatus)
  1366. goto cleanup;
  1367. psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
  1368. if (!psinfo)
  1369. goto cleanup;
  1370. notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
  1371. if (!notes)
  1372. goto cleanup;
  1373. fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
  1374. if (!fpu)
  1375. goto cleanup;
  1376. #ifdef ELF_CORE_COPY_XFPREGS
  1377. xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
  1378. if (!xfpu)
  1379. goto cleanup;
  1380. #endif
  1381. if (signr) {
  1382. struct elf_thread_status *tmp;
  1383. rcu_read_lock();
  1384. do_each_thread(g,p)
  1385. if (current->mm == p->mm && current != p) {
  1386. tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
  1387. if (!tmp) {
  1388. rcu_read_unlock();
  1389. goto cleanup;
  1390. }
  1391. tmp->thread = p;
  1392. list_add(&tmp->list, &thread_list);
  1393. }
  1394. while_each_thread(g,p);
  1395. rcu_read_unlock();
  1396. list_for_each(t, &thread_list) {
  1397. struct elf_thread_status *tmp;
  1398. int sz;
  1399. tmp = list_entry(t, struct elf_thread_status, list);
  1400. sz = elf_dump_thread_status(signr, tmp);
  1401. thread_status_size += sz;
  1402. }
  1403. }
  1404. /* now collect the dump for the current */
  1405. fill_prstatus(prstatus, current, signr);
  1406. elf_core_copy_regs(&prstatus->pr_reg, regs);
  1407. #ifdef CONFIG_MMU
  1408. segs = current->mm->map_count;
  1409. #else
  1410. segs = 0;
  1411. for (vml = current->mm->context.vmlist; vml; vml = vml->next)
  1412. segs++;
  1413. #endif
  1414. #ifdef ELF_CORE_EXTRA_PHDRS
  1415. segs += ELF_CORE_EXTRA_PHDRS;
  1416. #endif
  1417. /* Set up header */
  1418. fill_elf_fdpic_header(elf, segs + 1); /* including notes section */
  1419. has_dumped = 1;
  1420. current->flags |= PF_DUMPCORE;
  1421. /*
  1422. * Set up the notes in similar form to SVR4 core dumps made
  1423. * with info from their /proc.
  1424. */
  1425. fill_note(notes + 0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
  1426. fill_psinfo(psinfo, current->group_leader, current->mm);
  1427. fill_note(notes + 1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
  1428. numnote = 2;
  1429. auxv = (elf_addr_t *) current->mm->saved_auxv;
  1430. i = 0;
  1431. do
  1432. i += 2;
  1433. while (auxv[i - 2] != AT_NULL);
  1434. fill_note(&notes[numnote++], "CORE", NT_AUXV,
  1435. i * sizeof(elf_addr_t), auxv);
  1436. /* Try to dump the FPU. */
  1437. if ((prstatus->pr_fpvalid =
  1438. elf_core_copy_task_fpregs(current, regs, fpu)))
  1439. fill_note(notes + numnote++,
  1440. "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
  1441. #ifdef ELF_CORE_COPY_XFPREGS
  1442. if (elf_core_copy_task_xfpregs(current, xfpu))
  1443. fill_note(notes + numnote++,
  1444. "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
  1445. #endif
  1446. fs = get_fs();
  1447. set_fs(KERNEL_DS);
  1448. DUMP_WRITE(elf, sizeof(*elf));
  1449. offset += sizeof(*elf); /* Elf header */
  1450. offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
  1451. /* Write notes phdr entry */
  1452. {
  1453. struct elf_phdr phdr;
  1454. int sz = 0;
  1455. for (i = 0; i < numnote; i++)
  1456. sz += notesize(notes + i);
  1457. sz += thread_status_size;
  1458. fill_elf_note_phdr(&phdr, sz, offset);
  1459. offset += sz;
  1460. DUMP_WRITE(&phdr, sizeof(phdr));
  1461. }
  1462. /* Page-align dumped data */
  1463. dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
  1464. /*
  1465. * We must use the same mm->flags while dumping core to avoid
  1466. * inconsistency between the program headers and bodies, otherwise an
  1467. * unusable core file can be generated.
  1468. */
  1469. mm_flags = current->mm->flags;
  1470. /* write program headers for segments dump */
  1471. for (
  1472. #ifdef CONFIG_MMU
  1473. vma = current->mm->mmap; vma; vma = vma->vm_next
  1474. #else
  1475. vml = current->mm->context.vmlist; vml; vml = vml->next
  1476. #endif
  1477. ) {
  1478. struct elf_phdr phdr;
  1479. size_t sz;
  1480. #ifndef CONFIG_MMU
  1481. vma = vml->vma;
  1482. #endif
  1483. sz = vma->vm_end - vma->vm_start;
  1484. phdr.p_type = PT_LOAD;
  1485. phdr.p_offset = offset;
  1486. phdr.p_vaddr = vma->vm_start;
  1487. phdr.p_paddr = 0;
  1488. phdr.p_filesz = maydump(vma, mm_flags) ? sz : 0;
  1489. phdr.p_memsz = sz;
  1490. offset += phdr.p_filesz;
  1491. phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
  1492. if (vma->vm_flags & VM_WRITE)
  1493. phdr.p_flags |= PF_W;
  1494. if (vma->vm_flags & VM_EXEC)
  1495. phdr.p_flags |= PF_X;
  1496. phdr.p_align = ELF_EXEC_PAGESIZE;
  1497. DUMP_WRITE(&phdr, sizeof(phdr));
  1498. }
  1499. #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
  1500. ELF_CORE_WRITE_EXTRA_PHDRS;
  1501. #endif
  1502. /* write out the notes section */
  1503. for (i = 0; i < numnote; i++)
  1504. if (!writenote(notes + i, file))
  1505. goto end_coredump;
  1506. /* write out the thread status notes section */
  1507. list_for_each(t, &thread_list) {
  1508. struct elf_thread_status *tmp =
  1509. list_entry(t, struct elf_thread_status, list);
  1510. for (i = 0; i < tmp->num_notes; i++)
  1511. if (!writenote(&tmp->notes[i], file))
  1512. goto end_coredump;
  1513. }
  1514. DUMP_SEEK(dataoff);
  1515. if (elf_fdpic_dump_segments(file, &size, &limit, mm_flags) < 0)
  1516. goto end_coredump;
  1517. #ifdef ELF_CORE_WRITE_EXTRA_DATA
  1518. ELF_CORE_WRITE_EXTRA_DATA;
  1519. #endif
  1520. if (file->f_pos != offset) {
  1521. /* Sanity check */
  1522. printk(KERN_WARNING
  1523. "elf_core_dump: file->f_pos (%lld) != offset (%lld)\n",
  1524. file->f_pos, offset);
  1525. }
  1526. end_coredump:
  1527. set_fs(fs);
  1528. cleanup:
  1529. while (!list_empty(&thread_list)) {
  1530. struct list_head *tmp = thread_list.next;
  1531. list_del(tmp);
  1532. kfree(list_entry(tmp, struct elf_thread_status, list));
  1533. }
  1534. kfree(elf);
  1535. kfree(prstatus);
  1536. kfree(psinfo);
  1537. kfree(notes);
  1538. kfree(fpu);
  1539. #ifdef ELF_CORE_COPY_XFPREGS
  1540. kfree(xfpu);
  1541. #endif
  1542. return has_dumped;
  1543. #undef NUM_NOTES
  1544. }
  1545. #endif /* USE_ELF_CORE_DUMP */