Peeter Joot Peeter Joot - 11 months ago 38
Linux Question

Is there a way to have a.out loaded in linux x86_64 "high memory"?

If I look at the memory mapping for a 64-bit process on Linux (x86_64) I see that the a.out is mapped in fairly low memory:

$ cat /proc/1160/maps
00400000-004dd000 r-xp 00000000 103:03 536876177 /usr/bin/bash
006dc000-006dd000 r--p 000dc000 103:03 536876177 /usr/bin/bash
006dd000-006e6000 rw-p 000dd000 103:03 536876177 /usr/bin/bash
006e6000-006ec000 rw-p 00000000 00:00 0
00e07000-00e6a000 rw-p 00000000 00:00 0 [heap]
7fbeac11c000-7fbeac128000 r-xp 00000000 103:03 1074688839 /usr/lib64/
7fbeac128000-7fbeac327000 ---p 0000c000 103:03 1074688839 /usr/lib64/

I'd like to map a 2G memory region in the very lowest portion of memory, but have to put this in the region after these a.out mappings, crossing into the second 2G region.

Is the a.out being mapped here part of the x86_64 ABI, or can this load address be moved to a different region, using one of:

  • runtime loader flags

  • linker flags when the executable is created


Answer Source

Yes. Building a Linux x86-64 application as a position-independent executable will cause both it and its heap to be mapped into high memory, right along with libc and other libraries. This should leave the space under 2GB free for your use. (However, note that the kernel will probably protect the first 64KB or so of memory from being mapped to protect it from certain exploits; look up vm.mmap_min_addr for information.)

To build your application as a position-independent executable, pass -pie -fPIE to the compiler.