Skip to contentSkip to conversation

elf_map: why offset of vma need to subtract eppnt->p_vaddr ?

DISCUSSION Jacky 2 participants 4 messages Mar 29, 2013

lore
Share & tools
lore
#1 lore raw
Quoted 5 lines · Jacky

Hi All,

When mmaping elf image into memory, why offset vma need to subtract eppnt->p_vaddr as the following code ?

Quoted 7 lines

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.

Quoted 3 lines

Jacky

Signature · 7 lines
Regards,
Prabhunath G
Linux Trainer
Bangalore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130329/630ae2ff/attachment.html 
author
#2 lore raw

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" <[email protected]> ???

On Fri, Mar 29, 2013 at 12:45 PM, Jacky <[email protected]> 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

#3 lore raw
Quoted 8 lines · Jacky

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 ?

Small change : Substracting ELF_PAGEOFFSET(eppnt->p_vaddr) from variable eppnt->p_offset makes vma map the page-aligned file contents

Quoted 57 lines

? 2013-03-29 16:56:29?"Prabhu nath" <[email protected]> ???

Quoted 5 lines · Jacky

Hi All,

When mmaping elf image into memory, why offset vma need to subtract eppnt->p_vaddr as the following code ?

Quoted 7 lines

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.

Quoted 3 lines

Jacky

--
Regards,
Prabhunath G
Linux Trainer
Bangalore

Signature · 7 lines
Regards,
Prabhunath G
Linux Trainer
Bangalore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130329/1af758a5/attachment.html