elf_map: why offset of vma need to subtract eppnt->p_vaddr ?
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); ... } Thanks in advance. Jacky
On Fri, Mar 29, 2013 at 12:45 PM, Jacky <jackyclivia@163.com> 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
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" <gprabhunath@gmail.com> 写道: On Fri, Mar 29, 2013 at 12:45 PM, Jacky <jackyclivia@163.com> 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
participants (2)
-
Jacky -
Prabhu nath