binfmt_elf.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:34k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/fs/binfmt_elf.c
  3.  *
  4.  * These are the functions used to load ELF format executables as used
  5.  * on SVr4 machines.  Information on the format may be found in the book
  6.  * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
  7.  * Tools".
  8.  *
  9.  * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
  10.  */
  11. #include <linux/module.h>
  12. #include <linux/fs.h>
  13. #include <linux/stat.h>
  14. #include <linux/sched.h>
  15. #include <linux/mm.h>
  16. #include <linux/mman.h>
  17. #include <linux/a.out.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/ptrace.h>
  25. #include <linux/slab.h>
  26. #include <linux/shm.h>
  27. #include <linux/personality.h>
  28. #include <linux/elfcore.h>
  29. #include <linux/init.h>
  30. #include <linux/highuid.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/compiler.h>
  33. #include <linux/highmem.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/param.h>
  36. #include <asm/pgalloc.h>
  37. #define DLINFO_ITEMS 13
  38. #include <linux/elf.h>
  39. static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs);
  40. static int load_elf_library(struct file*);
  41. static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int);
  42. extern int dump_fpu (struct pt_regs *, elf_fpregset_t *);
  43. extern void dump_thread(struct pt_regs *, struct user *);
  44. #ifndef elf_addr_t
  45. #define elf_addr_t unsigned long
  46. #define elf_caddr_t char *
  47. #endif
  48. /*
  49.  * If we don't support core dumping, then supply a NULL so we
  50.  * don't even try.
  51.  */
  52. #ifdef USE_ELF_CORE_DUMP
  53. static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file);
  54. #else
  55. #define elf_core_dump NULL
  56. #endif
  57. #if ELF_EXEC_PAGESIZE > PAGE_SIZE
  58. # define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
  59. #else
  60. # define ELF_MIN_ALIGN PAGE_SIZE
  61. #endif
  62. #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
  63. #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
  64. #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
  65. static struct linux_binfmt elf_format = {
  66. NULL, THIS_MODULE, load_elf_binary, load_elf_library, elf_core_dump, ELF_EXEC_PAGESIZE
  67. };
  68. #define BAD_ADDR(x) ((unsigned long)(x) > TASK_SIZE)
  69. static void set_brk(unsigned long start, unsigned long end)
  70. {
  71. start = ELF_PAGEALIGN(start);
  72. end = ELF_PAGEALIGN(end);
  73. if (end <= start)
  74. return;
  75. do_brk(start, end - start);
  76. }
  77. /* We need to explicitly zero any fractional pages
  78.    after the data section (i.e. bss).  This would
  79.    contain the junk from the file that should not
  80.    be in memory */
  81. static void padzero(unsigned long elf_bss)
  82. {
  83. unsigned long nbyte;
  84. nbyte = ELF_PAGEOFFSET(elf_bss);
  85. if (nbyte) {
  86. nbyte = ELF_MIN_ALIGN - nbyte;
  87. clear_user((void *) elf_bss, nbyte);
  88. }
  89. }
  90. static elf_addr_t * 
  91. create_elf_tables(char *p, int argc, int envc,
  92.   struct elfhdr * exec,
  93.   unsigned long load_addr,
  94.   unsigned long load_bias,
  95.   unsigned long interp_load_addr, int ibcs)
  96. {
  97. elf_caddr_t *argv;
  98. elf_caddr_t *envp;
  99. elf_addr_t *sp, *csp;
  100. char *k_platform, *u_platform;
  101. long hwcap;
  102. size_t platform_len = 0;
  103. size_t len;
  104. /*
  105.  * Get hold of platform and hardware capabilities masks for
  106.  * the machine we are running on.  In some cases (Sparc), 
  107.  * this info is impossible to get, in others (i386) it is
  108.  * merely difficult.
  109.  */
  110. hwcap = ELF_HWCAP;
  111. k_platform = ELF_PLATFORM;
  112. if (k_platform) {
  113. platform_len = strlen(k_platform) + 1;
  114. u_platform = p - platform_len;
  115. __copy_to_user(u_platform, k_platform, platform_len);
  116. } else
  117. u_platform = p;
  118. #if defined(__i386__) && defined(CONFIG_SMP)
  119. /*
  120.  * In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
  121.  * by the processes running on the same package. One thing we can do
  122.  * is to shuffle the initial stack for them.
  123.  *
  124.  * The conditionals here are unneeded, but kept in to make the
  125.  * code behaviour the same as pre change unless we have hyperthreaded
  126.  * processors. This keeps Mr Marcelo Person happier but should be
  127.  * removed for 2.5
  128.  */
  129.  
  130. if(smp_num_siblings > 1)
  131. u_platform = u_platform - ((current->pid % 64) << 7);
  132. #endif
  133. /*
  134.  * Force 16 byte _final_ alignment here for generality.
  135.  */
  136. sp = (elf_addr_t *)(~15UL & (unsigned long)(u_platform));
  137. csp = sp;
  138. csp -= (1+DLINFO_ITEMS)*2 + (k_platform ? 2 : 0);
  139. #ifdef DLINFO_ARCH_ITEMS
  140. csp -= DLINFO_ARCH_ITEMS*2;
  141. #endif
  142. csp -= envc+1;
  143. csp -= argc+1;
  144. csp -= (!ibcs ? 3 : 1); /* argc itself */
  145. if ((unsigned long)csp & 15UL)
  146. sp -= ((unsigned long)csp & 15UL) / sizeof(*sp);
  147. /*
  148.  * Put the ELF interpreter info on the stack
  149.  */
  150. #define NEW_AUX_ENT(nr, id, val) 
  151.   __put_user ((id), sp+(nr*2)); 
  152.   __put_user ((val), sp+(nr*2+1)); 
  153. sp -= 2;
  154. NEW_AUX_ENT(0, AT_NULL, 0);
  155. if (k_platform) {
  156. sp -= 2;
  157. NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
  158. }
  159. sp -= DLINFO_ITEMS*2;
  160. NEW_AUX_ENT( 0, AT_HWCAP, hwcap);
  161. NEW_AUX_ENT( 1, AT_PAGESZ, ELF_EXEC_PAGESIZE);
  162. NEW_AUX_ENT( 2, AT_CLKTCK, CLOCKS_PER_SEC);
  163. NEW_AUX_ENT( 3, AT_PHDR, load_addr + exec->e_phoff);
  164. NEW_AUX_ENT( 4, AT_PHENT, sizeof (struct elf_phdr));
  165. NEW_AUX_ENT( 5, AT_PHNUM, exec->e_phnum);
  166. NEW_AUX_ENT( 6, AT_BASE, interp_load_addr);
  167. NEW_AUX_ENT( 7, AT_FLAGS, 0);
  168. NEW_AUX_ENT( 8, AT_ENTRY, load_bias + exec->e_entry);
  169. NEW_AUX_ENT( 9, AT_UID, (elf_addr_t) current->uid);
  170. NEW_AUX_ENT(10, AT_EUID, (elf_addr_t) current->euid);
  171. NEW_AUX_ENT(11, AT_GID, (elf_addr_t) current->gid);
  172. NEW_AUX_ENT(12, AT_EGID, (elf_addr_t) current->egid);
  173. #ifdef ARCH_DLINFO
  174. /* 
  175.  * ARCH_DLINFO must come last so platform specific code can enforce
  176.  * special alignment requirements on the AUXV if necessary (eg. PPC).
  177.  */
  178. ARCH_DLINFO;
  179. #endif
  180. #undef NEW_AUX_ENT
  181. sp -= envc+1;
  182. envp = (elf_caddr_t *) sp;
  183. sp -= argc+1;
  184. argv = (elf_caddr_t *) sp;
  185. if (!ibcs) {
  186. __put_user((elf_addr_t)(unsigned long) envp,--sp);
  187. __put_user((elf_addr_t)(unsigned long) argv,--sp);
  188. }
  189. __put_user((elf_addr_t)argc,--sp);
  190. current->mm->arg_start = (unsigned long) p;
  191. while (argc-->0) {
  192. __put_user((elf_caddr_t)(unsigned long)p,argv++);
  193. len = strnlen_user(p, PAGE_SIZE*MAX_ARG_PAGES);
  194. if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
  195. return NULL;
  196. p += len;
  197. }
  198. __put_user(NULL, argv);
  199. current->mm->arg_end = current->mm->env_start = (unsigned long) p;
  200. while (envc-->0) {
  201. __put_user((elf_caddr_t)(unsigned long)p,envp++);
  202. len = strnlen_user(p, PAGE_SIZE*MAX_ARG_PAGES);
  203. if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
  204. return NULL;
  205. p += len;
  206. }
  207. __put_user(NULL, envp);
  208. current->mm->env_end = (unsigned long) p;
  209. return sp;
  210. }
  211. #ifndef elf_map
  212. static inline unsigned long
  213. elf_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
  214. {
  215. unsigned long map_addr;
  216. down_write(&current->mm->mmap_sem);
  217. map_addr = do_mmap(filep, ELF_PAGESTART(addr),
  218.    eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr), prot, type,
  219.    eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
  220. up_write(&current->mm->mmap_sem);
  221. return(map_addr);
  222. }
  223. #endif /* !elf_map */
  224. /* This is much more generalized than the library routine read function,
  225.    so we keep this separate.  Technically the library read function
  226.    is only provided so that we can read a.out libraries that have
  227.    an ELF header */
  228. static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
  229.      struct file * interpreter,
  230.      unsigned long *interp_load_addr)
  231. {
  232. struct elf_phdr *elf_phdata;
  233. struct elf_phdr *eppnt;
  234. unsigned long load_addr = 0;
  235. int load_addr_set = 0;
  236. unsigned long last_bss = 0, elf_bss = 0;
  237. unsigned long error = ~0UL;
  238. int retval, i, size;
  239. /* First of all, some simple consistency checks */
  240. if (interp_elf_ex->e_type != ET_EXEC &&
  241.     interp_elf_ex->e_type != ET_DYN)
  242. goto out;
  243. if (!elf_check_arch(interp_elf_ex))
  244. goto out;
  245. if (!interpreter->f_op || !interpreter->f_op->mmap)
  246. goto out;
  247. /*
  248.  * If the size of this structure has changed, then punt, since
  249.  * we will be doing the wrong thing.
  250.  */
  251. if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
  252. goto out;
  253. if (interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
  254. goto out;
  255. /* Now read in all of the header information */
  256. size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
  257. if (size > ELF_MIN_ALIGN)
  258. goto out;
  259. elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
  260. if (!elf_phdata)
  261. goto out;
  262. retval = kernel_read(interpreter,interp_elf_ex->e_phoff,(char *)elf_phdata,size);
  263. error = retval;
  264. if (retval < 0)
  265. goto out_close;
  266. eppnt = elf_phdata;
  267. for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
  268.   if (eppnt->p_type == PT_LOAD) {
  269.     int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
  270.     int elf_prot = 0;
  271.     unsigned long vaddr = 0;
  272.     unsigned long k, map_addr;
  273.     if (eppnt->p_flags & PF_R) elf_prot =  PROT_READ;
  274.     if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
  275.     if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
  276.     vaddr = eppnt->p_vaddr;
  277.     if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
  278.      elf_type |= MAP_FIXED;
  279.     map_addr = elf_map(interpreter, load_addr + vaddr, eppnt, elf_prot, elf_type);
  280.     if (BAD_ADDR(map_addr))
  281.      goto out_close;
  282.     if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
  283. load_addr = map_addr - ELF_PAGESTART(vaddr);
  284. load_addr_set = 1;
  285.     }
  286.     /*
  287.      * Find the end of the file mapping for this phdr, and keep
  288.      * track of the largest address we see for this.
  289.      */
  290.     k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
  291.     if (k > elf_bss)
  292. elf_bss = k;
  293.     /*
  294.      * Do the same thing for the memory mapping - between
  295.      * elf_bss and last_bss is the bss section.
  296.      */
  297.     k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
  298.     if (k > last_bss)
  299. last_bss = k;
  300.   }
  301. }
  302. /* Now use mmap to map the library into memory. */
  303. /*
  304.  * Now fill out the bss section.  First pad the last page up
  305.  * to the page boundary, and then perform a mmap to make sure
  306.  * that there are zero-mapped pages up to and including the 
  307.  * last bss page.
  308.  */
  309. padzero(elf_bss);
  310. elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1); /* What we have mapped so far */
  311. /* Map the last of the bss segment */
  312. if (last_bss > elf_bss)
  313. do_brk(elf_bss, last_bss - elf_bss);
  314. *interp_load_addr = load_addr;
  315. error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;
  316. out_close:
  317. kfree(elf_phdata);
  318. out:
  319. return error;
  320. }
  321. static unsigned long load_aout_interp(struct exec * interp_ex,
  322.      struct file * interpreter)
  323. {
  324. unsigned long text_data, elf_entry = ~0UL;
  325. char * addr;
  326. loff_t offset;
  327. int retval;
  328. current->mm->end_code = interp_ex->a_text;
  329. text_data = interp_ex->a_text + interp_ex->a_data;
  330. current->mm->end_data = text_data;
  331. current->mm->brk = interp_ex->a_bss + text_data;
  332. switch (N_MAGIC(*interp_ex)) {
  333. case OMAGIC:
  334. offset = 32;
  335. addr = (char *) 0;
  336. break;
  337. case ZMAGIC:
  338. case QMAGIC:
  339. offset = N_TXTOFF(*interp_ex);
  340. addr = (char *) N_TXTADDR(*interp_ex);
  341. break;
  342. default:
  343. goto out;
  344. }
  345. do_brk(0, text_data);
  346. retval = -ENOEXEC;
  347. if (!interpreter->f_op || !interpreter->f_op->read)
  348. goto out;
  349. retval = interpreter->f_op->read(interpreter, addr, text_data, &offset);
  350. if (retval < 0)
  351. goto out;
  352. flush_icache_range((unsigned long)addr,
  353.                    (unsigned long)addr + text_data);
  354. do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
  355. interp_ex->a_bss);
  356. elf_entry = interp_ex->a_entry;
  357. out:
  358. return elf_entry;
  359. }
  360. /*
  361.  * These are the functions used to load ELF style executables and shared
  362.  * libraries.  There is no binary dependent code anywhere else.
  363.  */
  364. #define INTERPRETER_NONE 0
  365. #define INTERPRETER_AOUT 1
  366. #define INTERPRETER_ELF 2
  367. static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
  368. {
  369. struct file *interpreter = NULL; /* to shut gcc up */
  370.   unsigned long load_addr = 0, load_bias = 0;
  371. int load_addr_set = 0;
  372. char * elf_interpreter = NULL;
  373. unsigned int interpreter_type = INTERPRETER_NONE;
  374. unsigned char ibcs2_interpreter = 0;
  375. unsigned long error;
  376. struct elf_phdr * elf_ppnt, *elf_phdata;
  377. unsigned long elf_bss, k, elf_brk;
  378. int elf_exec_fileno;
  379. int retval, i;
  380. unsigned int size;
  381. unsigned long elf_entry, interp_load_addr = 0;
  382. unsigned long start_code, end_code, start_data, end_data;
  383. struct elfhdr elf_ex;
  384. struct elfhdr interp_elf_ex;
  385.    struct exec interp_ex;
  386. char passed_fileno[6];
  387. /* Get the exec-header */
  388. elf_ex = *((struct elfhdr *) bprm->buf);
  389. retval = -ENOEXEC;
  390. /* First of all, some simple consistency checks */
  391. if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  392. goto out;
  393. if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
  394. goto out;
  395. if (!elf_check_arch(&elf_ex))
  396. goto out;
  397. if (!bprm->file->f_op||!bprm->file->f_op->mmap)
  398. goto out;
  399. /* Now read in all of the header information */
  400. retval = -ENOMEM;
  401. if (elf_ex.e_phentsize != sizeof(struct elf_phdr))
  402. goto out;
  403. if (elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
  404. goto out;
  405. size = elf_ex.e_phnum * sizeof(struct elf_phdr);
  406. elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
  407. if (!elf_phdata)
  408. goto out;
  409. retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *) elf_phdata, size);
  410. if (retval < 0)
  411. goto out_free_ph;
  412. retval = get_unused_fd();
  413. if (retval < 0)
  414. goto out_free_ph;
  415. get_file(bprm->file);
  416. fd_install(elf_exec_fileno = retval, bprm->file);
  417. elf_ppnt = elf_phdata;
  418. elf_bss = 0;
  419. elf_brk = 0;
  420. start_code = ~0UL;
  421. end_code = 0;
  422. start_data = 0;
  423. end_data = 0;
  424. for (i = 0; i < elf_ex.e_phnum; i++) {
  425. if (elf_ppnt->p_type == PT_INTERP) {
  426. /* This is the program interpreter used for
  427.  * shared libraries - for now assume that this
  428.  * is an a.out format binary
  429.  */
  430. retval = -ENOMEM;
  431. if (elf_ppnt->p_filesz > PATH_MAX)
  432. goto out_free_file;
  433. elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
  434.    GFP_KERNEL);
  435. if (!elf_interpreter)
  436. goto out_free_file;
  437. retval = kernel_read(bprm->file, elf_ppnt->p_offset,
  438.    elf_interpreter,
  439.    elf_ppnt->p_filesz);
  440. if (retval < 0)
  441. goto out_free_interp;
  442. /* If the program interpreter is one of these two,
  443.  * then assume an iBCS2 image. Otherwise assume
  444.  * a native linux image.
  445.  */
  446. if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
  447.     strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
  448. ibcs2_interpreter = 1;
  449. #if 0
  450. printk("Using ELF interpreter %sn", elf_interpreter);
  451. #endif
  452. SET_PERSONALITY(elf_ex, ibcs2_interpreter);
  453. interpreter = open_exec(elf_interpreter);
  454. retval = PTR_ERR(interpreter);
  455. if (IS_ERR(interpreter))
  456. goto out_free_interp;
  457. retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
  458. if (retval < 0)
  459. goto out_free_dentry;
  460. /* Get the exec headers */
  461. interp_ex = *((struct exec *) bprm->buf);
  462. interp_elf_ex = *((struct elfhdr *) bprm->buf);
  463. break;
  464. }
  465. elf_ppnt++;
  466. }
  467. /* Some simple consistency checks for the interpreter */
  468. if (elf_interpreter) {
  469. interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
  470. /* Now figure out which format our binary is */
  471. if ((N_MAGIC(interp_ex) != OMAGIC) &&
  472.     (N_MAGIC(interp_ex) != ZMAGIC) &&
  473.     (N_MAGIC(interp_ex) != QMAGIC))
  474. interpreter_type = INTERPRETER_ELF;
  475. if (memcmp(interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  476. interpreter_type &= ~INTERPRETER_ELF;
  477. retval = -ELIBBAD;
  478. if (!interpreter_type)
  479. goto out_free_dentry;
  480. /* Make sure only one type was selected */
  481. if ((interpreter_type & INTERPRETER_ELF) &&
  482.      interpreter_type != INTERPRETER_ELF) {
  483.       // FIXME - ratelimit this before re-enabling
  484. // printk(KERN_WARNING "ELF: Ambiguous type, using ELFn");
  485. interpreter_type = INTERPRETER_ELF;
  486. }
  487. } else {
  488. /* Executables without an interpreter also need a personality  */
  489. SET_PERSONALITY(elf_ex, ibcs2_interpreter);
  490. }
  491. /* OK, we are done with that, now set up the arg stuff,
  492.    and then start this sucker up */
  493. if (!bprm->sh_bang) {
  494. char * passed_p;
  495. if (interpreter_type == INTERPRETER_AOUT) {
  496.   sprintf(passed_fileno, "%d", elf_exec_fileno);
  497.   passed_p = passed_fileno;
  498.   if (elf_interpreter) {
  499.     retval = copy_strings_kernel(1,&passed_p,bprm);
  500. if (retval)
  501. goto out_free_dentry; 
  502.     bprm->argc++;
  503.   }
  504. }
  505. }
  506. /* Flush all traces of the currently running executable */
  507. retval = flush_old_exec(bprm);
  508. if (retval)
  509. goto out_free_dentry;
  510. /* OK, This is the point of no return */
  511. current->mm->start_data = 0;
  512. current->mm->end_data = 0;
  513. current->mm->end_code = 0;
  514. current->mm->mmap = NULL;
  515. current->flags &= ~PF_FORKNOEXEC;
  516. elf_entry = (unsigned long) elf_ex.e_entry;
  517. /* Do this so that we can load the interpreter, if need be.  We will
  518.    change some of these later */
  519. current->mm->rss = 0;
  520. retval = setup_arg_pages(bprm);
  521. if (retval < 0) {
  522. send_sig(SIGKILL, current, 0);
  523. return retval;
  524. }
  525. current->mm->start_stack = bprm->p;
  526. /* Now we do a little grungy work by mmaping the ELF image into
  527.    the correct location in memory.  At this point, we assume that
  528.    the image should be loaded at fixed address, not at a variable
  529.    address. */
  530. for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
  531. int elf_prot = 0, elf_flags;
  532. unsigned long vaddr;
  533. if (elf_ppnt->p_type != PT_LOAD)
  534. continue;
  535. if (unlikely (elf_brk > elf_bss)) {
  536. unsigned long nbyte;
  537.             
  538. /* There was a PT_LOAD segment with p_memsz > p_filesz
  539.    before this one. Map anonymous pages, if needed,
  540.    and clear the area.  */
  541. set_brk (elf_bss + load_bias, elf_brk + load_bias);
  542. nbyte = ELF_PAGEOFFSET(elf_bss);
  543. if (nbyte) {
  544. nbyte = ELF_MIN_ALIGN - nbyte;
  545. if (nbyte > elf_brk - elf_bss)
  546. nbyte = elf_brk - elf_bss;
  547. clear_user((void *) elf_bss + load_bias, nbyte);
  548. }
  549. }
  550. if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
  551. if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
  552. if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
  553. elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
  554. vaddr = elf_ppnt->p_vaddr;
  555. if (elf_ex.e_type == ET_EXEC || load_addr_set) {
  556. elf_flags |= MAP_FIXED;
  557. } else if (elf_ex.e_type == ET_DYN) {
  558. /* Try and get dynamic programs out of the way of the default mmap
  559.    base, as well as whatever program they might try to exec.  This
  560.            is because the brk will follow the loader, and is not movable.  */
  561. load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
  562. }
  563. error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags);
  564. if (BAD_ADDR(error))
  565. continue;
  566. if (!load_addr_set) {
  567. load_addr_set = 1;
  568. load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
  569. if (elf_ex.e_type == ET_DYN) {
  570. load_bias += error -
  571.              ELF_PAGESTART(load_bias + vaddr);
  572. load_addr += load_bias;
  573. }
  574. }
  575. k = elf_ppnt->p_vaddr;
  576. if (k < start_code) start_code = k;
  577. if (start_data < k) start_data = k;
  578. k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
  579. if (k > elf_bss)
  580. elf_bss = k;
  581. if ((elf_ppnt->p_flags & PF_X) && end_code <  k)
  582. end_code = k;
  583. if (end_data < k)
  584. end_data = k;
  585. k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
  586. if (k > elf_brk)
  587. elf_brk = k;
  588. }
  589. elf_entry += load_bias;
  590. elf_bss += load_bias;
  591. elf_brk += load_bias;
  592. start_code += load_bias;
  593. end_code += load_bias;
  594. start_data += load_bias;
  595. end_data += load_bias;
  596. if (elf_interpreter) {
  597. if (interpreter_type == INTERPRETER_AOUT)
  598. elf_entry = load_aout_interp(&interp_ex,
  599.      interpreter);
  600. else
  601. elf_entry = load_elf_interp(&interp_elf_ex,
  602.     interpreter,
  603.     &interp_load_addr);
  604. allow_write_access(interpreter);
  605. fput(interpreter);
  606. kfree(elf_interpreter);
  607. if (BAD_ADDR(elf_entry)) {
  608. printk(KERN_ERR "Unable to load interpretern");
  609. kfree(elf_phdata);
  610. send_sig(SIGSEGV, current, 0);
  611. return 0;
  612. }
  613. }
  614. kfree(elf_phdata);
  615. if (interpreter_type != INTERPRETER_AOUT)
  616. sys_close(elf_exec_fileno);
  617. set_binfmt(&elf_format);
  618. compute_creds(bprm);
  619. current->flags &= ~PF_FORKNOEXEC;
  620. bprm->p = (unsigned long)
  621.   create_elf_tables((char *)bprm->p,
  622. bprm->argc,
  623. bprm->envc,
  624. &elf_ex,
  625. load_addr, load_bias,
  626. interp_load_addr,
  627. (interpreter_type == INTERPRETER_AOUT ? 0 : 1));
  628. /* N.B. passed_fileno might not be initialized? */
  629. if (interpreter_type == INTERPRETER_AOUT)
  630. current->mm->arg_start += strlen(passed_fileno) + 1;
  631. current->mm->start_brk = current->mm->brk = elf_brk;
  632. current->mm->end_code = end_code;
  633. current->mm->start_code = start_code;
  634. current->mm->start_data = start_data;
  635. current->mm->end_data = end_data;
  636. current->mm->start_stack = bprm->p;
  637. /* Calling set_brk effectively mmaps the pages that we need
  638.  * for the bss and break sections
  639.  */
  640. set_brk(elf_bss, elf_brk);
  641. padzero(elf_bss);
  642. #if 0
  643. printk("(start_brk) %lxn" , (long) current->mm->start_brk);
  644. printk("(end_code) %lxn" , (long) current->mm->end_code);
  645. printk("(start_code) %lxn" , (long) current->mm->start_code);
  646. printk("(start_data) %lxn" , (long) current->mm->start_data);
  647. printk("(end_data) %lxn" , (long) current->mm->end_data);
  648. printk("(start_stack) %lxn" , (long) current->mm->start_stack);
  649. printk("(brk) %lxn" , (long) current->mm->brk);
  650. #endif
  651. if (current->personality & MMAP_PAGE_ZERO) {
  652. /* Why this, you ask???  Well SVr4 maps page 0 as read-only,
  653.    and some applications "depend" upon this behavior.
  654.    Since we do not have the power to recompile these, we
  655.    emulate the SVr4 behavior.  Sigh.  */
  656. /* N.B. Shouldn't the size here be PAGE_SIZE?? */
  657. down_write(&current->mm->mmap_sem);
  658. error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
  659. MAP_FIXED | MAP_PRIVATE, 0);
  660. up_write(&current->mm->mmap_sem);
  661. }
  662. #ifdef ELF_PLAT_INIT
  663. /*
  664.  * The ABI may specify that certain registers be set up in special
  665.  * ways (on i386 %edx is the address of a DT_FINI function, for
  666.  * example.  This macro performs whatever initialization to
  667.  * the regs structure is required.
  668.  */
  669. ELF_PLAT_INIT(regs);
  670. #endif
  671. start_thread(regs, elf_entry, bprm->p);
  672. if (current->ptrace & PT_PTRACED)
  673. send_sig(SIGTRAP, current, 0);
  674. retval = 0;
  675. out:
  676. return retval;
  677. /* error cleanup */
  678. out_free_dentry:
  679. allow_write_access(interpreter);
  680. fput(interpreter);
  681. out_free_interp:
  682. if (elf_interpreter)
  683. kfree(elf_interpreter);
  684. out_free_file:
  685. sys_close(elf_exec_fileno);
  686. out_free_ph:
  687. kfree(elf_phdata);
  688. goto out;
  689. }
  690. /* This is really simpleminded and specialized - we are loading an
  691.    a.out library that is given an ELF header. */
  692. static int load_elf_library(struct file *file)
  693. {
  694. struct elf_phdr *elf_phdata;
  695. unsigned long elf_bss, bss, len;
  696. int retval, error, i, j;
  697. struct elfhdr elf_ex;
  698. error = -ENOEXEC;
  699. retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
  700. if (retval != sizeof(elf_ex))
  701. goto out;
  702. if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  703. goto out;
  704. /* First of all, some simple consistency checks */
  705. if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
  706.    !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
  707. goto out;
  708. /* Now read in all of the header information */
  709. j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
  710. /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
  711. error = -ENOMEM;
  712. elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
  713. if (!elf_phdata)
  714. goto out;
  715. error = -ENOEXEC;
  716. retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata, j);
  717. if (retval != j)
  718. goto out_free_ph;
  719. for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
  720. if ((elf_phdata + i)->p_type == PT_LOAD) j++;
  721. if (j != 1)
  722. goto out_free_ph;
  723. while (elf_phdata->p_type != PT_LOAD) elf_phdata++;
  724. /* Now use mmap to map the library into memory. */
  725. down_write(&current->mm->mmap_sem);
  726. error = do_mmap(file,
  727. ELF_PAGESTART(elf_phdata->p_vaddr),
  728. (elf_phdata->p_filesz +
  729.  ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
  730. PROT_READ | PROT_WRITE | PROT_EXEC,
  731. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  732. (elf_phdata->p_offset -
  733.  ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
  734. up_write(&current->mm->mmap_sem);
  735. if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
  736. goto out_free_ph;
  737. elf_bss = elf_phdata->p_vaddr + elf_phdata->p_filesz;
  738. padzero(elf_bss);
  739. len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr + ELF_MIN_ALIGN - 1);
  740. bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
  741. if (bss > len)
  742. do_brk(len, bss - len);
  743. error = 0;
  744. out_free_ph:
  745. kfree(elf_phdata);
  746. out:
  747. return error;
  748. }
  749. /*
  750.  * Note that some platforms still use traditional core dumps and not
  751.  * the ELF core dump.  Each platform can select it as appropriate.
  752.  */
  753. #ifdef USE_ELF_CORE_DUMP
  754. /*
  755.  * ELF core dumper
  756.  *
  757.  * Modelled on fs/exec.c:aout_core_dump()
  758.  * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  759.  */
  760. /*
  761.  * These are the only things you should do on a core-file: use only these
  762.  * functions to write out all the necessary info.
  763.  */
  764. static int dump_write(struct file *file, const void *addr, int nr)
  765. {
  766. return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  767. }
  768. static int dump_seek(struct file *file, off_t off)
  769. {
  770. if (file->f_op->llseek) {
  771. if (file->f_op->llseek(file, off, 0) != off)
  772. return 0;
  773. } else
  774. file->f_pos = off;
  775. return 1;
  776. }
  777. /*
  778.  * Decide whether a segment is worth dumping; default is yes to be
  779.  * sure (missing info is worse than too much; etc).
  780.  * Personally I'd include everything, and use the coredump limit...
  781.  *
  782.  * I think we should skip something. But I am not sure how. H.J.
  783.  */
  784. static inline int maydump(struct vm_area_struct *vma)
  785. {
  786. /*
  787.  * If we may not read the contents, don't allow us to dump
  788.  * them either. "dump_write()" can't handle it anyway.
  789.  */
  790. if (!(vma->vm_flags & VM_READ))
  791. return 0;
  792. /* Do not dump I/O mapped devices! -DaveM */
  793. if (vma->vm_flags & VM_IO)
  794. return 0;
  795. #if 1
  796. if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
  797. return 1;
  798. if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
  799. return 0;
  800. #endif
  801. return 1;
  802. }
  803. #define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))
  804. /* An ELF note in memory */
  805. struct memelfnote
  806. {
  807. const char *name;
  808. int type;
  809. unsigned int datasz;
  810. void *data;
  811. };
  812. static int notesize(struct memelfnote *en)
  813. {
  814. int sz;
  815. sz = sizeof(struct elf_note);
  816. sz += roundup(strlen(en->name), 4);
  817. sz += roundup(en->datasz, 4);
  818. return sz;
  819. }
  820. /* #define DEBUG */
  821. #ifdef DEBUG
  822. static void dump_regs(const char *str, elf_greg_t *r)
  823. {
  824. int i;
  825. static const char *regs[] = { "ebx", "ecx", "edx", "esi", "edi", "ebp",
  826.       "eax", "ds", "es", "fs", "gs",
  827.       "orig_eax", "eip", "cs",
  828.       "efl", "uesp", "ss"};
  829. printk("Registers: %sn", str);
  830. for(i = 0; i < ELF_NGREG; i++)
  831. {
  832. unsigned long val = r[i];
  833. printk("   %-2d %-5s=%08lx %lun", i, regs[i], val, val);
  834. }
  835. }
  836. #endif
  837. #define DUMP_WRITE(addr, nr)
  838. do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
  839. #define DUMP_SEEK(off)
  840. do { if (!dump_seek(file, (off))) return 0; } while(0)
  841. static int writenote(struct memelfnote *men, struct file *file)
  842. {
  843. struct elf_note en;
  844. en.n_namesz = strlen(men->name);
  845. en.n_descsz = men->datasz;
  846. en.n_type = men->type;
  847. DUMP_WRITE(&en, sizeof(en));
  848. DUMP_WRITE(men->name, en.n_namesz);
  849. /* XXX - cast from long long to long to avoid need for libgcc.a */
  850. DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
  851. DUMP_WRITE(men->data, men->datasz);
  852. DUMP_SEEK(roundup((unsigned long)file->f_pos, 4)); /* XXX */
  853. return 1;
  854. }
  855. #undef DUMP_WRITE
  856. #undef DUMP_SEEK
  857. #define DUMP_WRITE(addr, nr)
  858. if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) 
  859. goto end_coredump;
  860. #define DUMP_SEEK(off)
  861. if (!dump_seek(file, (off))) 
  862. goto end_coredump;
  863. /*
  864.  * Actual dumper
  865.  *
  866.  * This is a two-pass process; first we find the offsets of the bits,
  867.  * and then they are actually written out.  If we run out of core limit
  868.  * we just truncate.
  869.  */
  870. static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
  871. {
  872. int has_dumped = 0;
  873. mm_segment_t fs;
  874. int segs;
  875. size_t size = 0;
  876. int i;
  877. struct vm_area_struct *vma;
  878. struct elfhdr elf;
  879. off_t offset = 0, dataoff;
  880. unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
  881. int numnote = 4;
  882. struct memelfnote notes[4];
  883. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  884. elf_fpregset_t fpu; /* NT_PRFPREG */
  885. struct elf_prpsinfo psinfo; /* NT_PRPSINFO */
  886. /* first copy the parameters from user space */
  887. memset(&psinfo, 0, sizeof(psinfo));
  888. {
  889. int i, len;
  890. len = current->mm->arg_end - current->mm->arg_start;
  891. if (len >= ELF_PRARGSZ)
  892. len = ELF_PRARGSZ-1;
  893. copy_from_user(&psinfo.pr_psargs,
  894.       (const char *)current->mm->arg_start, len);
  895. for(i = 0; i < len; i++)
  896. if (psinfo.pr_psargs[i] == 0)
  897. psinfo.pr_psargs[i] = ' ';
  898. psinfo.pr_psargs[len] = 0;
  899. }
  900. memset(&prstatus, 0, sizeof(prstatus));
  901. /*
  902.  * This transfers the registers from regs into the standard
  903.  * coredump arrangement, whatever that is.
  904.  */
  905. #ifdef ELF_CORE_COPY_REGS
  906. ELF_CORE_COPY_REGS(prstatus.pr_reg, regs)
  907. #else
  908. if (sizeof(elf_gregset_t) != sizeof(struct pt_regs))
  909. {
  910. printk("sizeof(elf_gregset_t) (%ld) != sizeof(struct pt_regs) (%ld)n",
  911. (long)sizeof(elf_gregset_t), (long)sizeof(struct pt_regs));
  912. }
  913. else
  914. *(struct pt_regs *)&prstatus.pr_reg = *regs;
  915. #endif
  916. /* now stop all vm operations */
  917. down_write(&current->mm->mmap_sem);
  918. segs = current->mm->map_count;
  919. #ifdef DEBUG
  920. printk("elf_core_dump: %d segs %lu limitn", segs, limit);
  921. #endif
  922. /* Set up header */
  923. memcpy(elf.e_ident, ELFMAG, SELFMAG);
  924. elf.e_ident[EI_CLASS] = ELF_CLASS;
  925. elf.e_ident[EI_DATA] = ELF_DATA;
  926. elf.e_ident[EI_VERSION] = EV_CURRENT;
  927. memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  928. elf.e_type = ET_CORE;
  929. elf.e_machine = ELF_ARCH;
  930. elf.e_version = EV_CURRENT;
  931. elf.e_entry = 0;
  932. elf.e_phoff = sizeof(elf);
  933. elf.e_shoff = 0;
  934. elf.e_flags = 0;
  935. elf.e_ehsize = sizeof(elf);
  936. elf.e_phentsize = sizeof(struct elf_phdr);
  937. elf.e_phnum = segs+1; /* Include notes */
  938. elf.e_shentsize = 0;
  939. elf.e_shnum = 0;
  940. elf.e_shstrndx = 0;
  941. fs = get_fs();
  942. set_fs(KERNEL_DS);
  943. has_dumped = 1;
  944. current->flags |= PF_DUMPCORE;
  945. DUMP_WRITE(&elf, sizeof(elf));
  946. offset += sizeof(elf); /* Elf header */
  947. offset += (segs+1) * sizeof(struct elf_phdr); /* Program headers */
  948. /*
  949.  * Set up the notes in similar form to SVR4 core dumps made
  950.  * with info from their /proc.
  951.  */
  952. notes[0].name = "CORE";
  953. notes[0].type = NT_PRSTATUS;
  954. notes[0].datasz = sizeof(prstatus);
  955. notes[0].data = &prstatus;
  956. prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
  957. prstatus.pr_sigpend = current->pending.signal.sig[0];
  958. prstatus.pr_sighold = current->blocked.sig[0];
  959. psinfo.pr_pid = prstatus.pr_pid = current->pid;
  960. psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
  961. psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
  962. psinfo.pr_sid = prstatus.pr_sid = current->session;
  963. prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
  964. prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
  965. prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
  966. prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
  967. prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
  968. prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
  969. prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
  970. prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
  971. #ifdef DEBUG
  972. dump_regs("Passed in regs", (elf_greg_t *)regs);
  973. dump_regs("prstatus regs", (elf_greg_t *)&prstatus.pr_reg);
  974. #endif
  975. notes[1].name = "CORE";
  976. notes[1].type = NT_PRPSINFO;
  977. notes[1].datasz = sizeof(psinfo);
  978. notes[1].data = &psinfo;
  979. i = current->state ? ffz(~current->state) + 1 : 0;
  980. psinfo.pr_state = i;
  981. psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
  982. psinfo.pr_zomb = psinfo.pr_sname == 'Z';
  983. psinfo.pr_nice = current->nice;
  984. psinfo.pr_flag = current->flags;
  985. psinfo.pr_uid = NEW_TO_OLD_UID(current->uid);
  986. psinfo.pr_gid = NEW_TO_OLD_GID(current->gid);
  987. strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
  988. notes[2].name = "CORE";
  989. notes[2].type = NT_TASKSTRUCT;
  990. notes[2].datasz = sizeof(*current);
  991. notes[2].data = current;
  992. /* Try to dump the FPU. */
  993. prstatus.pr_fpvalid = dump_fpu (regs, &fpu);
  994. if (!prstatus.pr_fpvalid)
  995. {
  996. numnote--;
  997. }
  998. else
  999. {
  1000. notes[3].name = "CORE";
  1001. notes[3].type = NT_PRFPREG;
  1002. notes[3].datasz = sizeof(fpu);
  1003. notes[3].data = &fpu;
  1004. }
  1005. /* Write notes phdr entry */
  1006. {
  1007. struct elf_phdr phdr;
  1008. int sz = 0;
  1009. for(i = 0; i < numnote; i++)
  1010. sz += notesize(&notes[i]);
  1011. phdr.p_type = PT_NOTE;
  1012. phdr.p_offset = offset;
  1013. phdr.p_vaddr = 0;
  1014. phdr.p_paddr = 0;
  1015. phdr.p_filesz = sz;
  1016. phdr.p_memsz = 0;
  1017. phdr.p_flags = 0;
  1018. phdr.p_align = 0;
  1019. offset += phdr.p_filesz;
  1020. DUMP_WRITE(&phdr, sizeof(phdr));
  1021. }
  1022. /* Page-align dumped data */
  1023. dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
  1024. /* Write program headers for segments dump */
  1025. for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
  1026. struct elf_phdr phdr;
  1027. size_t sz;
  1028. sz = vma->vm_end - vma->vm_start;
  1029. phdr.p_type = PT_LOAD;
  1030. phdr.p_offset = offset;
  1031. phdr.p_vaddr = vma->vm_start;
  1032. phdr.p_paddr = 0;
  1033. phdr.p_filesz = maydump(vma) ? sz : 0;
  1034. phdr.p_memsz = sz;
  1035. offset += phdr.p_filesz;
  1036. phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
  1037. if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
  1038. if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
  1039. phdr.p_align = ELF_EXEC_PAGESIZE;
  1040. DUMP_WRITE(&phdr, sizeof(phdr));
  1041. }
  1042. for(i = 0; i < numnote; i++)
  1043. if (!writenote(&notes[i], file))
  1044. goto end_coredump;
  1045. DUMP_SEEK(dataoff);
  1046. for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
  1047. unsigned long addr;
  1048. if (!maydump(vma))
  1049. continue;
  1050. #ifdef DEBUG
  1051. printk("elf_core_dump: writing %08lx-%08lxn", vma->vm_start, vma->vm_end);
  1052. #endif
  1053. for (addr = vma->vm_start;
  1054.      addr < vma->vm_end;
  1055.      addr += PAGE_SIZE) {
  1056. struct page* page;
  1057. struct vm_area_struct *vma;
  1058. if (get_user_pages(current, current->mm, addr, 1, 0, 1,
  1059. &page, &vma) <= 0) {
  1060. DUMP_SEEK (file->f_pos + PAGE_SIZE);
  1061. } else {
  1062. if (page == ZERO_PAGE(addr)) {
  1063. DUMP_SEEK (file->f_pos + PAGE_SIZE);
  1064. } else {
  1065. void *kaddr;
  1066. flush_cache_page(vma, addr);
  1067. kaddr = kmap(page);
  1068. DUMP_WRITE(kaddr, PAGE_SIZE);
  1069. flush_page_to_ram(page);
  1070. kunmap(page);
  1071. }
  1072. put_page(page);
  1073. }
  1074. }
  1075. }
  1076. if ((off_t) file->f_pos != offset) {
  1077. /* Sanity check */
  1078. printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)n",
  1079.        (off_t) file->f_pos, offset);
  1080. }
  1081.  end_coredump:
  1082. set_fs(fs);
  1083. up_write(&current->mm->mmap_sem);
  1084. return has_dumped;
  1085. }
  1086. #endif /* USE_ELF_CORE_DUMP */
  1087. static int __init init_elf_binfmt(void)
  1088. {
  1089. return register_binfmt(&elf_format);
  1090. }
  1091. static void __exit exit_elf_binfmt(void)
  1092. {
  1093. /* Remove the COFF and ELF loaders. */
  1094. unregister_binfmt(&elf_format);
  1095. }
  1096. module_init(init_elf_binfmt)
  1097. module_exit(exit_elf_binfmt)
  1098. MODULE_LICENSE("GPL");