From: jackyclivia@163.com (Jacky) Date: Fri, 29 Mar 2013 17:33:38 +0800 (CST) Subject: elf_map: why offset of vma need to subtract eppnt->p_vaddr ? In-Reply-To: References: Message-ID: <1f23d512.d53d.13db57eccdb.Coremail.jackyclivia@163.com> To: kernelnewbies@lists.kernelnewbies.org List-Id: kernelnewbies.lists.kernelnewbies.org Great ! Thanks Prabhunath. Let me make sure I understand you correctly: Substracting ELF_PAGEOFFSET(eppnt->p_vaddr) from variable off makes vma map the page-aligned file contents, right ? ? 2013-03-29 16:56:29?"Prabhu nath" ??? On Fri, Mar 29, 2013 at 12:45 PM, Jacky wrote: Hi All, When mmaping elf image into memory, why offset vma need to subtract eppnt->p_vaddr as the following code ? static unsigned long elf_map() { ... unsigned long off = eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr); ... } Assume you have logically divided the contents of the ELF file into chunks of PAGE_SIZE (Typically 4K on x86). Let us name each chunk as file page-synonym to page frame in the physical address space and page in the virtual address space respectively. Here variable off is the file page base offset - synonym to page base address or page frame base address which are typically multiple of PAGE_SIZE. off >> PAGE_SHIFT is the value stored in vm_pgoff in struct vm_area_struct In a nutshell vm_pgoff is the page number (file page number) in the ELF file where the corresponding PT_LOAD segment starts. Suppose if a file offset (eppnt->p_offset) of a PT_LOAD segment is 9560 (0x2558) bytes into the file and eppnt->p_vaddr is 0x08048558. Then the file page number base address off is calculated as off = 0x2558 - 0x558; // ELF_PAGEOFFSET(eppnt->p_vaddr) will expand to 0x558. Then off is 0x2000 and vm_pgoff is (off >> PAGE_SHIFT) = 2. This means that the PT_LOAD segment starts at 2nd page or 2nd file page in the ELF file. Thanks in advance. Jacky -- Regards, Prabhunath G Linux Trainer Bangalore -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130329/59057415/attachment-0001.html