Although the requested mapping address is a page start, it will use an address shifted with a few pages.
I’m trying to do something like this:
char *mapped = mmap(base, page_size, PROT_NONE, MAP_SHARED,
file_handle, 0);
printf("Base : %p\n", base);
printf("Mapped: %p\n", mapped);
Sample output (page_size = 4096 = 0x1000):
Base : 0x7f22a1047000
Mapped: 0x7f22a1045000
Offset is 2 pages. This also seems to vary with length. For example, if instead of one page I try to map 4 pages, output becomes:
Base : 0x7fd24d994000
Mapped: 0x7fd24d98f000
which is 5 pages offset.
Why is it behaving like this?
Because the OS is, when you don’t specifically ask for a mapping at a fixed address, free to choose an address convenient for itself; from the mmap(2) man page:
If you don’t need that exact address, you’re better off letting the system choose it (which, honestly, should be in the majority of cases).