I see that .text segment of ELF files that linked with -pie argument when loaded into memory locates somewhere in virtual memory addresses like 0x00005xxxxxxxxxxx on linux x86_64. I've checked several running processes and it seems that .text segment's address all of them has prefix 0x00005, I could not find any one with 0x00004 prefix for example.
Is there any reason why dynamic linker chooses this address range (0x00005xxxxxxxxxxx) to locate .text segment? Why it does not select some addresses starting from 4Mb+ like binaries linked without -pie argument?
The dynamic linker has nothing to do with this.
The executable is
mmaped by the kernel before the first user-space instruction executes (that instruction is the_startin the dynamic loader for dynamically linked binaries), as part of executing theexecvesystem call.So you have to look in kernel sources for explanation of why PIE binaries end up in the
0x5...range.Looking in
fs/binfmt_elf.cyou can see that it usesELF_ET_DYN_BASE, which is defined inarch/x86/include/asm/elf.hasThe
DEFAULT_MAP_WINDOWis itself coming fromarch/x86/include/asm/page_64_types.h:Putting this all together:
(1UL << 47) - 4096 == 0x7ffffffff000;0x7ffffffff000 / 3 * 2 == 0x555555554aaa.P.S. Note that
.textis a section, not a segment. See also this answer.