I'm interested in the way Linux mmaps files into the main memory (in my context its for executing, but I guess the mmap process is the same for writing and reading as well) and which size it uses.
So I know Linux uses paging with usually 4kB pagesize (where in the kernel can I find this size?). But what exactly does this mean for the memory allocated: Assume you have a binary of size of a few thousned bytes, lets just say 5812B and you execute it.
What happens in the kernel: Does it allocate 2*4kB and then copy the 5812B into this space, wasting >3KB of main memory in the 2nd page?
It would be great if anyone knew the file in the kernel source where the pagesize is defined.
My 2nd question is also very simple I guess: I assumed 5812B as a filesize. Is it right, that this size is simply taken from the inode?
Best Answer
There is no direct relationship between the size of the executable and the size in memory. Here's a very quick overview of what happens when a binary is executed:
For more information about executable formats, linking, and executable loading, you can read Linkers and Loaders by John R. Levine.
In a 5kB executable, it's likely that everything is code or data that needs to be loaded into memory except for the header. The executable code will be at least one page, perhaps two, and then there will be at least one page for the stack, probably one page or for the heap (other data), plus memory used by shared libraries.
Under Linux, you can inspect the memory mappings for an executable with
cat /proc/$pid/maps
. The format is documented in theproc(5)
man page; see also Understanding Linux /proc/id/maps.