如何将64位进程地址空间限制为小于4G? [英] How to limit a 64-bit process address space to less than 4G?
问题描述
当我执行64位进程并查看/proc/[pid]/maps
时,布局显示共享库和堆栈节位于更大的地址中;例如以下内容:
When I execute a 64-bit process and look at /proc/[pid]/maps
, the layout show that the shared libraries and stack section are in a larger address; such as the following:
7ffff7ffc000-7ffff7ffd000 r--p 0001d000 08:03 16643 /lib/ld-2.11.2.so
7ffff7ffd000-7ffff7ffe000 rw-p 0001e000 08:03 16643 /lib/ld-2.11.2.so
7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0
7ffffffea000-7ffffffff000 rw-p 00000000 00:00 0 [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
我可以将这些部分限制为更少的4G空间,并且它仍然必须是64位进程,而不是将其编译为32位进程吗?
Can I limit these sections to less 4G space and it must still be a 64-bit process rather than compile it into a 32-bit process?
推荐答案
prelink(8)
工具和概念遭到了广泛的憎恶(并且可能未在您的发行版中提供),您可以使用它来将库链接成二进制文件并存储到低内存中:
While the prelink(8)
tool and concept are widely detested (and probably not shipped on your distribution), you may be able to use it to link libraries into a binary into low memory:
-r --reloc-only=ADDRESS
Instead of prelinking, just relink given shared libraries
to the specified base address.
由于库将映射到进程的地址由ld(1)
确定,因此您可以修改Makefile
以调用具有不同--section-start
值的ld
:
Since the address that libraries will be mapped into the process is determined by ld(1)
, you might be able to modify your Makefile
to invoke ld
with different --section-start
values:
--section-start SECTION=ADDRESS
Set address of named section
-Tbss ADDRESS Set address of .bss section
-Tdata ADDRESS Set address of .data section
-Ttext ADDRESS Set address of .text section
-Ttext-segment ADDRESS Set address of text segment
我将text和bss片段下移到较低的地址:
I moved the text and bss segments down to lower addresses:
$ gcc -Wl,-Ttext-segment=0x200000 -Wl,-Tbss=0x400000 -o broken broken.c
$ readelf -a broken
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x200450
...
如果可以使用--section-start
移动可执行文件中提供的所有部分,并使用prelink(8)
将库向下移动,则可以将所有代码加载到4 GB以下.
If you can move all the sections provided in the executable with --section-start
and move the libraries down with prelink(8)
, you might be able to get all the code loaded below 4 gigabytes.
这篇关于如何将64位进程地址空间限制为小于4G?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!