【Linux】应用程序内存段布局
一、各段内存布局
内存布局图
1.BSS段(Block Started by Symbol)未初始化段
2.linux ELF base address is 0x8048000
应用程序虚拟地址起始地址均为0x8048000
3.堆向高内存地址生长
4.栈向低内存地址生长
P.S.
// 查看进程ID 图片
ps aux
cat /proc/3794/maps
代码段——只读r 可执行x
起始地址必为0x8048000
二、数据存放位置
- 分析数据存放位置
对比即可
结论
1.代码段:代码,全局常量(const)、字符串常量
2.数据段(广义数据段,含BSS段):全局变量(初始化以及未初始化的)、静态变量(全局的和局部的、初始化的以及未初始化的)
3.堆:动态分配的区域
4.栈:局部变量(初始化以及未初始化的,但不包含静态变量)、局部只读变量(const)
动手尝试即可
#include <stdio.h>
#include <stdlib.h>
int global_init_a = 1; //全局,初始化 :数据段
int global_uninit_a; //全局,未初始化:数据段
static int static_global_init_a =1;//全局,静态,初始化 :数据段
static int static_global_uninit_a;//数据段
const int const_global_a =1; //全局常量 :代码段
int global_init_b = 1;
int global_uninit_b; //全局,未初始化
static int static_global_init_b =1;//全局,静态,初始化 :数据段
static int static_global_uninit_b;
const int const_global_b =1;
int main()
{
int local_init_a = 1; //Stack 栈
int local_uninit_a;
static int static_local_init_a =1;//局部静态:数据段
static int static_local_uninit_a;
const int const_local_a =1; //局部常量:栈
int local_init_b = 1;
int local_uninit_b;
static int static_local_init_b =1;
static int static_local_uninit_b;
const int const_local_b =1;
int* malloc_p_a;
malloc_p_a = malloc(sizeof(int));//通过malloc分配得到的,局部 :堆
//打印地址
printf("&global_init_a = %p, global_init_a =%d\n",&global_init_a,global_init_a);
printf("global_uninit_a =%p, global_uninit_a = %d\n",&global_uninit_a,global_uninit_a);
printf("&static_global_init_a =%p,static_global_init_a = %d\n",&static_global_init_a,static_global_init_a);
printf("static_global_uninit_a = %p,static_global_uninit_a = %d\n",&static_global_uninit_a,static_global_uninit_a);
printf("const_global_a = %p,const_global_a =%d\n",&const_global_a,const_global_a );
printf("&global_init_b = %p, global_init_b =%d\n",&global_init_b,global_init_b);
printf("global_uninit_b =%p, global_uninit_b = %d\n",&global_uninit_b,global_uninit_b);
printf("&static_global_init_b =%p,static_global_init_b = %d\n",&static_global_init_b,static_global_init_b);
printf("static_global_uninit_b = %p,static_global_uninit_b = %d\n",&static_global_uninit_b,static_global_uninit_b);
printf("const_global_b = %p,const_global_b =%d\n",&const_global_b,const_global_b);
printf("&local_init_a = %p,local_init_a =%d\n",&local_init_a,local_init_a);
printf("&local_uninit_a =%p,local_uninit_a\n",&local_uninit_a,local_uninit_a);
printf("&static_local_init_a = %p,static_local_init_a=%d\n",&static_local_init_a,static_local_init_a);
printf("&static_local_uninit_a= %p,static_local_uninit_a= %d\n",&static_local_uninit_a,static_local_uninit_a);
printf("&const_local_a =%p,const_local_a=%d\n", &const_local_a,const_local_a);
printf("&local_init_b = %p,local_init_b =%d\n",&local_init_b,local_init_b);
printf("&local_uninit_b =%p,local_uninit_b\n",&local_uninit_b,local_uninit_b);
printf("&static_local_init_b = %p,static_local_init_b=%d\n",&static_local_init_b,static_local_init_b);
printf("&static_local_uninit_b= %p,static_local_uninit_b= %d\n",&static_local_uninit_b,static_local_uninit_b);
printf("&const_local_b =%p,const_local_b=%d\n", &const_local_b,const_local_b);
printf("malloc_p_a = %p,malloc_p_a",&malloc_p_a,malloc_p_a);
while(1);
return 0;
}
三、BSS段
- (Block Started by Symbol)未初始化段
- 利用readelf –S 程序名 分析BSS段
- elf 为可执行文件类型