Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

Probably a very simple question, I just want to make sure I got it right. + 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?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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:

  1. The kernel parses the file and breaks it into section. Some sections are directly loaded into memory, in separate pages. Some sections aren't loaded at all (e.g. debugging symbols).
  2. If the executable is dynamically linked, the kernel calls the dynamic loader, and it loads the required shared libraries and performs link edition as required.
  3. The program starts executing its code, and usually it will request more memory to store data.

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 the proc(5) man page; see also Understanding Linux /proc/id/maps.

share|improve this answer

Yes: you eventually get two 4k pages. Data is loaded on demand, so if nothing ever refers to the second page it will never be loaded into memory.

include/asm-i386/param.h:#define EXEC_PAGESIZE  4096
include/asm-i386/elf.h:#define ELF_EXEC_PAGESIZE        4096

Do not change these values and expect anything to work.

Yes, the file size is stored in the inode in ext2/3.

share|improve this answer

For the definition part, on a 2.6.38~ish kernel for the Intel architecture:

arch/x86/include/asm/page_types.h:

/* PAGE_SHIFT determines the page size */

#define PAGE_SHIFT 12

#define PAGE_SIZE (_AC(1,UL) << PAGE_SHIFT)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.