C Program Memory Layout

来源:互联网 发布:数据库试用 编辑:程序博客网 时间:2024/05/20 07:19
memory_layout.c#include <stdio.h>#include <stdlib.h>int gvar1 = 111;int main(int argc, char** argv){    int lvar1 = 211;    printf("sum is: %d\n", gvar1 + lvar1);    return 0;}代码编译:gcc memory_layout.c -o memory_layout -O0-O0 是 gcc 的编译优化控制选项,-O0 Reduce compilation time and make debugging produce the expected results.  This is the default.
nm 是 GNU 的工具,其作用:GNU nm lists the symbols from object files objfile....  If no object files are listed as arguments, nm assumes the file a.out.nm memory_layout会列出 memory_layout 中的符号:nm memory_layout0000000000601044 B __bss_start0000000000601044 b completed.69730000000000601030 D __data_start0000000000601030 W data_start0000000000400470 t deregister_tm_clones00000000004004e0 t __do_global_dtors_aux0000000000600e18 t __do_global_dtors_aux_fini_array_entry0000000000601038 D __dso_handle0000000000600e28 d _DYNAMIC0000000000601044 D _edata0000000000601048 B _end00000000004005e4 T _fini0000000000400500 t frame_dummy0000000000600e10 t __frame_dummy_init_array_entry0000000000400728 r __FRAME_END__0000000000601000 d _GLOBAL_OFFSET_TABLE_                 w __gmon_start__0000000000601040 D gvar100000000004003e0 T _init0000000000600e18 t __init_array_end0000000000600e10 t __init_array_start00000000004005f0 R _IO_stdin_used                 w _ITM_deregisterTMCloneTable                 w _ITM_registerTMCloneTable0000000000600e20 d __JCR_END__0000000000600e20 d __JCR_LIST__                 w _Jv_RegisterClasses00000000004005e0 T __libc_csu_fini0000000000400570 T __libc_csu_init                 U __libc_start_main@@GLIBC_2.2.5000000000040052d T main                 U printf@@GLIBC_2.2.500000000004004a0 t register_tm_clones0000000000400440 T _start0000000000601048 D __TMC_END__关于 B b D W 等等这些符号的说明,可以参考 man nm 中的说明,其中 0000000000601040 D gvar1“D 或者 d : The symbol is in the initialized data section”表示 gvar1 位于 initialized data section 中但是代码中的 lvar1 变量没了,是被优化没了吗?
gcc memory_layout.c -S -o memory_layout.s只编译,不汇编和链接,但是生成的 memory_layout.s 中就没有了lvar1 这个变量,为什么呢?
局部变量,存在于BSS中,运行时,如何映射到堆栈中呢?malloc分配,又是如何从堆中分配的呢?OS中如何存储每个ProcessProcess Address Space的布局呢?
1 0
原创粉丝点击