如何将 64 位进程地址空间限制为小于 4G? [英] How to limit a 64-bit process address space to less than 4G?

查看:18
本文介绍了如何将 64 位进程地址空间限制为小于 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 来调用 ld 具有不同的 --section-start 值:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆