关于Linux进程内存布局的问题

4

我在谈论的是英特尔32位平台。Linux内核版本为2.6.31-14。

#include <stdio.h>
#include <stdlib.h>

int init_global_var = 10;        /* Initialized global variable */
int global_var;                  /* Uninitialized global variable */
static int init_static_var = 20; /* Initialized static variable in global scope */
static int static_var;           /* Uninitialized static variable in global scope */

int main(int argc, char **argv, char **envp)
{
        static int init_static_local_var = 30;   /* Initialized static local variable */
    static int static_local_var;             /* Uninitialized static local variable */
    int init_local_var = 40;                 /* Initialized local variable */
    int local_var;                           /* Uninitialized local variable */
    char *dynamic_var = (char*)malloc(100);  /* Dynamic variable */

    printf("Address of initialized global variable: %p\n", &init_global_var);
    printf("Address of uninitialized global variable: %p\n", &global_var);
    printf("Address of initialized static variable in global scope: %p\n", &init_static_var);
    printf("Address of uninitialized static variable in global scope: %p\n", &static_var);
    printf("Address of initialized static variable in local scope: %p\n", &init_static_local_var);
    printf("Address of uninitialized static variable in local scope: %p\n", &static_local_var);
    printf("Address of initialized local variable: %p\n", &init_local_var);
    printf("Address of uninitialized local variable: %p\n", &local_var);
    printf("Address of function (code): %p\n", &main);
    printf("Address of dynamic variable: %p\n", dynamic_var);
    printf("Address of environment variable: %p\n", &envp[0]);
    char* p=0x0;
    printf("%s\n",p);

    exit(0);
}

输出:

naman@naman-laptop ~> ./a.out
Address of initialized global variable: 0x804a020
Address of uninitialized global variable: 0x804a03c
Address of initialized static variable in global scope: 0x804a024
Address of uninitialized static variable in global scope: 0x804a034
Address of initialized static variable in local scope: 0x804a028
Address of uninitialized static variable in local scope: 0x804a038
Address of initialized local variable: 0xbfc11cbc
Address of uninitialized local variable: 0xbfc11cb8
Address of function (code): 0x8048484
Address of dynamic variable: 0x8223008
Address of environment variable: 0xbfc11d7c
fish: Job 1, “./a.out” terminated by signal SIGSEGV (Address boundary error)

在上述代码中,我有以下疑惑。为什么代码位于0x8048484而不是靠近虚拟内存的起始位置,例如0x00000400?据我所知,布局应该如下:

低内存........................................高内存

Text Data BSS Heap.....................Stack Env

所以,文本不应该存储在内存的下方。它应该靠近较低的内存,不是吗?
2个回答

5
由于默认加载地址(ELF文件的开头将在此地址处加载)为0x8000000(或0x8048000),因此代码位于0x8048484。这个默认值在默认链接器(ld)脚本中是固定的,并且可以通过链接器选项进行更改。
请注意,这是0x08000000或0x08048000(128兆字节),而不是0x80000000(2千兆字节)。
关于这个限制,在论坛上有一些讨论:http://cboard.cprogramming.com/tech-board/101129-why-address-space-0-0x08000000-process-unused.htmlhttp://books.google.com/books?id=Id9cYsIdjIwC&pg=PA111&lpg=PA111&d​​q=linker+0x08000000。此外,在lkml上有一个很好的描述:http://lkml.org/lkml/2002/2/20/194
“根据System V Intel 386 ABI规范(http://stage.caldera.com/developer/devspecs/abi386-4.pdf),0x8048000是文本段的典型起始点。”

这只适用于ELF或任何可执行格式吗?就像这里我的文件是.out(我认为与ELF不同,我可能错了)。此外,这是否取决于可执行文件格式还是Linux进程加载器的属性? - pflz
1
要获取文件格式,请使用命令 file FILENAME,例如 file a.out。您将看到您有一个 ELF 文件。 "a.out" 名称是默认可执行文件名称的历史名称。a.out 文件格式现在已不再使用。链接器脚本特定于文件格式,但 COFF/A.OUT 脚本可能具有类似的加载地址。这是 ABI、静态链接器 (ld) 和(部分)动态链接器 (ld-linux.so) 的属性。 - osgx

0

当一个 a.out 不是一个 a.out 的时候,那就是它实际上是一个 ELF 文件。尝试使用 elfinfo --all a.out 获取详细信息。


但为什么它从0x08000000开始而不是从0x00100000开始? - osgx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接