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 #include int global_ni; // .bss int global_iz = 0 ; // .bss int global_inz = 1; // .data const int global_const0 = 0; const int global_const1 = 1; void function(void) { int local_fni; int local_finz = 1; int local_fiz = 0 ; static int static_fni; static int static_finz = 1; static int static_fiz = 0 ; 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 = 1; int local_miz = 0 ; static int static_mni; static int static_minz = 1; static int static_miz = 0 ; int *p1 = (int*)malloc(sizeof(int)); const int local_const0 = 0; const int local_const1 = 1; 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 0; } /* = = = = = = = = = = = = = = = 测试: 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 0 .interp 00000013 00008134 00008134 00000134 2**0 CONTENTS, ALLOC, LOAD, READONLY, DATA 1 .note.ABI-tag 00000020 00008148 00008148 00000148 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA, LINK_ONCE_SAME_CONTENTS 2 .hash 0000003c 00008168 00008168 00000168 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 3 .dynsym 000000a0 000081a4 000081a4 000001a4 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 4 .dynstr 000000a7 00008244 00008244 00000244 2**0 CONTENTS, ALLOC, LOAD, READONLY, DATA 5 .gnu.version 00000014 000082ec 000082ec 000002ec 2**1 CONTENTS, ALLOC, LOAD, READONLY, DATA 6 .gnu.version_r 00000040 00008300 00008300 00000300 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 7 .rel.dyn 00000008 00008340 00008340 00000340 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 8 .rel.plt 00000030 00008348 00008348 00000348 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 9 .init 0000000c 00008378 00008378 00000378 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 10 .plt 0000005c 00008384 00008384 00000384 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 11 .text 000004d8 000083e0 000083e0 000003e0 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 12 .fini 00000008 000088b8 000088b8 000008b8 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 13 .rodata 00000314 000088c0 000088c0 000008c0 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 14 .ARM.extab 00000024 00008bd4 00008bd4 00000bd4 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 15 .ARM.exidx 00000040 00008bf8 00008bf8 00000bf8 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 16 .init_array 00000004 00010c38 00010c38 00000c38 2**2 CONTENTS, ALLOC, LOAD, DATA 17 .fini_array 00000004 00010c3c 00010c3c 00000c3c 2**2 CONTENTS, ALLOC, LOAD, DATA 18 .jcr 00000004 00010c40 00010c40 00000c40 2**2 CONTENTS, ALLOC, LOAD, DATA 19 .dynamic 000000f0 00010c44 00010c44 00000c44 2**2 CONTENTS, ALLOC, LOAD, DATA 20 .got 00000028 00010d34 00010d34 00000d34 2**2 CONTENTS, ALLOC, LOAD, DATA 21 .data 00000014 00010d5c 00010d5c 00000d5c 2**2 CONTENTS, ALLOC, LOAD, DATA 22 .bss 0000001c 00010d70 00010d70 00000d70 2**2 ALLOC 23 .ARM.attributes 0000002b 00000000 00000000 00000d70 2**0 CONTENTS, READONLY 24 .comment 0000002b 00000000 00000000 00000d9b 2**0