binfmt_elf_fdpic.c 48 KB

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