Here's the "readelf -l test" for a simple executable:
Code: Select all
Elf file type is EXEC (Executable file)
Entry point 0x4000b0
There are 2 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
0x00000000000000b5 0x00000000000000b5 R E 200000
LOAD 0x00000000000000b8 0x00000000006000b8 0x00000000006000b8
0x0000000000000004 0x0000000000000130 RW 200000
Section to Segment mapping:
Segment Sections...
00 .text
01 .data .bss
But the entire section is on a single page!
According to http://pubs.opengroup.org/onlinepubs/00 ... /mmap.html the mapping is to be performed in page units (which is obvious even without looking at the standard). But does that mean if I just map the 4 bytes of the file into memory, the entire page from the file at that position gets mapped? That would not make sense, since that portion of the file does not contain all zeroes and hence violates the rule that the BSS sections must be zeroed.
If, on the other hand, I'm only supposed to read the 4 bytes in, and zero out the rest of the page, that of course means I don't map entire pages from the file. In this case, what is the best way to store file contents in memory and share them (using copy-on-write) between processes? If one process maps those 4 bytes, and another maps 8 bytes from that region, do I need to allocate a separate frame even if that area is never written to, since it is a partial page?
I hope I explained it clearly.