gcc代码反汇编查看内存分布[2]: arm-linux-gcc

arm-none-linux-gnueabi-gcc -v

  gcc version 4.4.1 (Sourcery G++ Lite 2010q1-202)

重点:

  代码中的内存分配, 地址从低到高: 代码段(RO, 保存函数代码) --> 只读数据段(RO, 保存常量) --> 数据段(RW, 已初始化并且初始值不为0的全局变量和静态变量) -->bss段(RW,  未初始化或者初始化为0的全局变量和静态变量).

  程序运行起来之后, 堆区和栈区的变量地址是动态分配的.

可以看出arm-none-linux-gnueabi-gcc编译和gcc编译结果是类似的.

#include<stdio.h>
#include<stdlib.h> int global_ni; // .bss
int global_iz = ; // .bss int global_inz = ; // .data const int global_const0 = ;
const int global_const1 = ; void function(void)
{
int local_fni;
int local_finz = ;
int local_fiz = ; static int static_fni;
static int static_finz = ;
static int static_fiz = ; int *p2 = (int*)malloc(sizeof(int)); printf("子函数 局部变量 : \n");
printf(" local_fni: %p \n", &local_fni);
printf(" local_finz: %p \n", &local_finz);
printf(" local_fiz: %p \n", &local_fiz); printf("子函数 静态变量 : \n");
printf(" static_fni: %p \n", &static_fni);
printf(" static_finz: %p \n", &static_finz);
printf(" static_fiz: %p \n", &static_fiz); printf("子函数 指针变量 : \n");
printf(" p2 : %p \n", p2); printf("子函数地址 : \n");
printf(" function : %p \n", function);
} int main(int argc, char **argv)
{
int local_mni;
int local_minz = ;
int local_miz = ; static int static_mni;
static int static_minz = ;
static int static_miz = ; int *p1 = (int*)malloc(sizeof(int));
const int local_const0 = ;
const int local_const1 = ;
char* str_ptr = "char"; printf("主函数 局部变量 : \n");
printf(" local_mni : %p \n", &local_mni);
printf(" local_minz : %p \n", &local_minz);
printf(" local_miz : %p \n", &local_miz); printf("const 变量: \n");
printf(" local_const0 : %p \n", &local_const0);
printf(" local_const1 : %p \n", &local_const1);
printf(" global_const0 : %p \n", &global_const0);
printf(" global_const1 : %p \n", &global_const1); printf("主函数 malloc指针变量 : \n");
printf(" p1 : %p \n", p1); printf("全局变量 : \n");
printf(" global_ni : %p \n", &global_ni);
printf(" global_inz : %p \n", &global_inz);
printf(" global_iz : %p \n", &global_iz); printf("主函数 静态变量 : \n");
printf(" static_mni: %p \n", &static_mni);
printf(" static_minz: %p \n", &static_minz);
printf(" static_miz: %p \n", &static_miz); printf("字符串常量 : \n");
printf(" str_ptr : %p \n", str_ptr); printf("主函数地址 : \n");
printf(" main : %p \n", main); printf("= = = = = = = = = = = = = = = \n"); function(); return ;
} /*
= = = = = = = = = = = = = = =
测试:
arm-none-linux-gnueabi-gcc -o test main.c
arm-none-linux-gnueabi-objdump -Dhs test > test.dis 分析:
Disassembly of section .text: 000083e0
{
子函数地址 :
function :
主函数地址 :
main : } Disassembly of section .rodata: 000088c0
{
const 变量: 全局
global_const0
global_const1
字符串常量 : str_ptr
}
//.data中是初始化为非0的全局变量和静态变量
Disassembly of section .data: 00010d5c
{
global_inz
static_minz
static_finz
} //.bss中是初始化为0以及没有初始化的全局变量和静态变量
Disassembly of section .bss: 00010d70
{
global_iz
static_mni
static_miz
static_fni
static_fiz
global_ni } 堆空间: //动态的
{
主函数 malloc指针变量 :
p1 : 子函数 malloc指针变量 :
p2 :
} 栈空间: //动态的
{
子函数 局部变量 :
local_fiz :
local_finz:
local_fni : const 变量: 局部
local_const1 :
local_const0 : 主函数 局部变量 :
local_miz :
local_minz:
local_mni :
}
*/

反汇编文件test.dis节选

test:     file format elf32-littlearm

Sections:
Idx Name Size VMA LMA File off Algn
.interp **
CONTENTS, ALLOC, LOAD, READONLY, DATA
.note.ABI-tag **
CONTENTS, ALLOC, LOAD, READONLY, DATA, LINK_ONCE_SAME_CONTENTS
.hash 0000003c **
CONTENTS, ALLOC, LOAD, READONLY, DATA
.dynsym 000000a0 000081a4 000081a4 000001a4 **
CONTENTS, ALLOC, LOAD, READONLY, DATA
.dynstr 000000a7 **
CONTENTS, ALLOC, LOAD, READONLY, DATA
.gnu.version 000082ec 000082ec 000002ec **
CONTENTS, ALLOC, LOAD, READONLY, DATA
.gnu.version_r **
CONTENTS, ALLOC, LOAD, READONLY, DATA
.rel.dyn **
CONTENTS, ALLOC, LOAD, READONLY, DATA
.rel.plt **
CONTENTS, ALLOC, LOAD, READONLY, DATA
.init 0000000c **
CONTENTS, ALLOC, LOAD, READONLY, CODE
.plt 0000005c **
CONTENTS, ALLOC, LOAD, READONLY, CODE
.text 000004d8 000083e0 000083e0 000003e0 **
CONTENTS, ALLOC, LOAD, READONLY, CODE
.fini 000088b8 000088b8 000008b8 **
CONTENTS, ALLOC, LOAD, READONLY, CODE
.rodata 000088c0 000088c0 000008c0 **
CONTENTS, ALLOC, LOAD, READONLY, DATA
.ARM.extab 00008bd4 00008bd4 00000bd4 **
CONTENTS, ALLOC, LOAD, READONLY, DATA
.ARM.exidx 00008bf8 00008bf8 00000bf8 **
CONTENTS, ALLOC, LOAD, READONLY, DATA
.init_array 00010c38 00010c38 00000c38 **
CONTENTS, ALLOC, LOAD, DATA
.fini_array 00010c3c 00010c3c 00000c3c **
CONTENTS, ALLOC, LOAD, DATA
.jcr 00010c40 00010c40 00000c40 **
CONTENTS, ALLOC, LOAD, DATA
.dynamic 000000f0 00010c44 00010c44 00000c44 **
CONTENTS, ALLOC, LOAD, DATA
.got 00010d34 00010d34 00000d34 **
CONTENTS, ALLOC, LOAD, DATA
.data 00010d5c 00010d5c 00000d5c **
CONTENTS, ALLOC, LOAD, DATA
.bss 0000001c 00010d70 00010d70 00000d70 **
ALLOC
.ARM.attributes 0000002b 00000d70 **
CONTENTS, READONLY
.comment 0000002b 00000d9b **
CONTENTS, READONLY
.debug_frame 00000dc8 **
CONTENTS, READONLY, DEBUGGING
上一篇:ASP.NET页面之间传值


下一篇:axios简单了解