• gcc代码反汇编查看内存分布[1]: gcc


    # gcc -v
    gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)

    重点:

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

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

    #include<stdio.h>
    #include<stdlib.h>
    
    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("子函数 局部变量 : 
    ");
        printf(" local_fni: %p 
    ", &local_fni);
        printf(" local_finz: %p 
    ", &local_finz);
        printf(" local_fiz: %p 
    ", &local_fiz);
    
        printf("子函数 静态变量 : 
    ");
        printf(" static_fni: %p 
    ", &static_fni);
        printf(" static_finz: %p 
    ", &static_finz);
        printf(" static_fiz: %p 
    ", &static_fiz);
        
        printf("子函数 指针变量 : 
    ");
        printf(" p2 : %p 
    ", p2);
        
        printf("子函数地址 : 
    ");
        printf(" function : %p 
    ", 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("主函数 局部变量 : 
    ");
        printf(" local_mni : %p 
    ", &local_mni);
        printf(" local_minz : %p 
    ", &local_minz);
        printf(" local_miz : %p 
    ", &local_miz);
        
        printf("const 变量: 
    ");
        printf(" local_const0 : %p 
    ", &local_const0);
        printf(" local_const1 : %p 
    ", &local_const1);
        printf(" global_const0 : %p 
    ", &global_const0);
        printf(" global_const1 : %p 
    ", &global_const1);
        
        printf("主函数 malloc指针变量 : 
    ");
        printf(" p1 : %p 
    ", p1);
    
        printf("全局变量 : 
    ");
        printf(" global_ni : %p 
    ", &global_ni);
        printf(" global_inz : %p 
    ", &global_inz);
        printf(" global_iz : %p 
    ", &global_iz);
    
        printf("主函数 静态变量 : 
    ");
        printf(" static_mni: %p 
    ", &static_mni);
        printf(" static_minz: %p 
    ", &static_minz);
        printf(" static_miz: %p 
    ", &static_miz);
    
        printf("字符串常量 : 
    ");
        printf(" str_ptr : %p 
    ", str_ptr);
    
        printf("主函数地址 : 
    ");
        printf(" main : %p 
    ", main);
    
        printf("= = = = = = = = = = = = = = = 
    ");
    
        function();
    
        return 0;
    }
    
    /*
    = = = = = = = = = = = = = = = 
    测试:
     gcc -o test main.c 
     objdump -Dhs test > test.dis 
     ./test 
     
    分析:
    Disassembly of section .text: 08048370 { 子函数地址 : function : 0x8048424 主函数地址 : main : 0x804851d } Disassembly of section .rodata: 080487d8 { const 变量: 全局 global_const0 : 0x80487e0 global_const1 : 0x80487e4 字符串常量 : str_ptr : 0x80488d0 }
    //.data中是初始化为非0的全局变量和静态变量 Disassembly of section .data: 0804a014 { 子函数 静态变量 : static_finz: 0x804a024 全局变量 : global_inz : 0x804a01c 主函数 静态变量 : static_minz: 0x804a020 } //.bss中是初始化为0以及没有初始化的全局变量和静态变量 Disassembly of section .bss: 0804a028 { 子函数 静态变量 : static_fni: 0x804a03c static_fiz: 0x804a040 全局变量 : global_iz : 0x804a030 global_ni : 0x804a044 主函数 静态变量 : static_mni: 0x804a034 static_miz: 0x804a038 } 堆空间: //动态的 { 主函数 malloc指针变量 : p1 : 0x967c008 子函数 malloc指针变量 : p2 : 0x967c018 } 栈空间: //动态的 { 子函数 局部变量 : local_fiz : 0xbfb79924 local_finz: 0xbfb79928 local_fni : 0xbfb7992c const 变量: 局部 local_const1 : 0xbfe3cee8 local_const0 : 0xbfe3ceec 主函数 局部变量 : local_miz : 0xbfb79964 local_minz: 0xbfb79968 local_mni : 0xbfb7996c }
    */

    附反汇编文件test.dis节选

    test:     file format elf32-i386
    
    Sections:
    Idx Name          Size      VMA       LMA       File off  Algn
      0 .interp       00000013  08048134  08048134  00000134  2**0
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
      1 .note.ABI-tag 00000020  08048148  08048148  00000148  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
      2 .note.gnu.build-id 00000024  08048168  08048168  00000168  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
      3 .gnu.hash     00000020  0804818c  0804818c  0000018c  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
      4 .dynsym       00000070  080481ac  080481ac  000001ac  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
      5 .dynstr       00000058  0804821c  0804821c  0000021c  2**0
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
      6 .gnu.version  0000000e  08048274  08048274  00000274  2**1
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
      7 .gnu.version_r 00000020  08048284  08048284  00000284  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
      8 .rel.dyn      00000008  080482a4  080482a4  000002a4  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
      9 .rel.plt      00000028  080482ac  080482ac  000002ac  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
     10 .init         00000030  080482d4  080482d4  000002d4  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, CODE
     11 .plt          00000060  08048304  08048304  00000304  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, CODE
     12 .text         0000044c  08048370  08048370  00000370  2**4
                      CONTENTS, ALLOC, LOAD, READONLY, CODE
     13 .fini         0000001c  080487bc  080487bc  000007bc  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, CODE
     14 .rodata       000002db  080487d8  080487d8  000007d8  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
     15 .eh_frame     00000004  08048ab4  08048ab4  00000ab4  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
     16 .ctors        00000008  08049f14  08049f14  00000f14  2**2
                      CONTENTS, ALLOC, LOAD, DATA
     17 .dtors        00000008  08049f1c  08049f1c  00000f1c  2**2
                      CONTENTS, ALLOC, LOAD, DATA
     18 .jcr          00000004  08049f24  08049f24  00000f24  2**2
                      CONTENTS, ALLOC, LOAD, DATA
     19 .dynamic      000000c8  08049f28  08049f28  00000f28  2**2
                      CONTENTS, ALLOC, LOAD, DATA
     20 .got          00000004  08049ff0  08049ff0  00000ff0  2**2
                      CONTENTS, ALLOC, LOAD, DATA
     21 .got.plt      00000020  08049ff4  08049ff4  00000ff4  2**2
                      CONTENTS, ALLOC, LOAD, DATA
     22 .data         00000014  0804a014  0804a014  00001014  2**2
                      CONTENTS, ALLOC, LOAD, DATA
     23 .bss          00000020  0804a028  0804a028  00001028  2**2
                      ALLOC
     24 .comment      0000006c  00000000  00000000  00001028  2**0
                      CONTENTS, READONLY
  • 相关阅读:
    BUAA 软工 | 从计算机技术中探索艺术之路
    好好编程BUAA_SE(组/团队) Scrum Meeting 博客汇总
    beta事后分析
    Beta阶段项目展示
    Beta阶段测试报告
    Beta版本发布计划
    Beta阶段 第十次Scrum Meeting
    Beta阶段 第九次Scrum Meeting
    Beta阶段 第八次Scrum Meeting
    Beta阶段 第七次Scrum Meeting
  • 原文地址:https://www.cnblogs.com/mylinux/p/5611225.html
Copyright © 2020-2023  润新知