系统相关
首页 > 系统相关> > 关于Linux进程内存布局的问题

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

作者:互联网

我说的是英特尔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?据我所知,布局应该是这样的:

内存不足…………………………………. HighMemory

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

所以,文字不应该落在记忆中.它应该接近较低的记忆,不应该吗?

解决方法:

Why does the code lie at 0x8048484

因为默认加载地址(将在此地址加载ELF文件的开头)是0x8000000(或0x8048000).此缺省值在缺省链接器(ld)脚本中得到修复,可以通过链接器选项进行更改.

注意,这是0x

Why does the code lie at 0x8048484

000或0x08048000(128兆字节)而不是0x80000000(2千兆字节).

以下是关于此论坛的限制

Why does the code lie at 0x8048484

标签:linux,process,memory-layout
来源: https://codeday.me/bug/20190621/1254075.html