Great ! Thanks Prabhunath. makes vma map the page-aligned file contents, right ?
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