Where are static variables stored in C and C++?

前端 未结 16 2482
自闭症患者
自闭症患者 2020-11-22 02:00

In what segment (.BSS, .DATA, other) of an executable file are static variables stored so that they don\'t have name collision? For example:


foo.c:                  


        
16条回答
  •  情歌与酒
    2020-11-22 02:46

    I tried it with objdump and gdb, here is the result what I get:

    (gdb) disas fooTest
    Dump of assembler code for function fooTest:
       0x000000000040052d <+0>: push   %rbp
       0x000000000040052e <+1>: mov    %rsp,%rbp
       0x0000000000400531 <+4>: mov    0x200b09(%rip),%eax        # 0x601040 
       0x0000000000400537 <+10>:    add    $0x1,%eax
       0x000000000040053a <+13>:    mov    %eax,0x200b00(%rip)        # 0x601040 
       0x0000000000400540 <+19>:    mov    0x200afe(%rip),%eax        # 0x601044 
       0x0000000000400546 <+25>:    add    $0x1,%eax
       0x0000000000400549 <+28>:    mov    %eax,0x200af5(%rip)        # 0x601044 
       0x000000000040054f <+34>:    mov    0x200aef(%rip),%edx        # 0x601044 
       0x0000000000400555 <+40>:    mov    0x200ae5(%rip),%eax        # 0x601040 
       0x000000000040055b <+46>:    mov    %eax,%esi
       0x000000000040055d <+48>:    mov    $0x400654,%edi
       0x0000000000400562 <+53>:    mov    $0x0,%eax
       0x0000000000400567 <+58>:    callq  0x400410 
       0x000000000040056c <+63>:    pop    %rbp
       0x000000000040056d <+64>:    retq   
    End of assembler dump.
    
    (gdb) disas barTest
    Dump of assembler code for function barTest:
       0x000000000040056e <+0>: push   %rbp
       0x000000000040056f <+1>: mov    %rsp,%rbp
       0x0000000000400572 <+4>: mov    0x200ad0(%rip),%eax        # 0x601048 
       0x0000000000400578 <+10>:    add    $0x1,%eax
       0x000000000040057b <+13>:    mov    %eax,0x200ac7(%rip)        # 0x601048 
       0x0000000000400581 <+19>:    mov    0x200ac5(%rip),%eax        # 0x60104c 
       0x0000000000400587 <+25>:    add    $0x1,%eax
       0x000000000040058a <+28>:    mov    %eax,0x200abc(%rip)        # 0x60104c 
       0x0000000000400590 <+34>:    mov    0x200ab6(%rip),%edx        # 0x60104c 
       0x0000000000400596 <+40>:    mov    0x200aac(%rip),%eax        # 0x601048 
       0x000000000040059c <+46>:    mov    %eax,%esi
       0x000000000040059e <+48>:    mov    $0x40065c,%edi
       0x00000000004005a3 <+53>:    mov    $0x0,%eax
       0x00000000004005a8 <+58>:    callq  0x400410 
       0x00000000004005ad <+63>:    pop    %rbp
       0x00000000004005ae <+64>:    retq   
    End of assembler dump.
    

    here is the objdump result

    Disassembly of section .data:
    
    0000000000601030 <__data_start>:
        ...
    
    0000000000601038 <__dso_handle>:
        ...
    
    0000000000601040 :
      601040:   01 00                   add    %eax,(%rax)
        ...
    
    0000000000601044 :
      601044:   02 00                   add    (%rax),%al
        ...
    
    0000000000601048 :
      601048:   0a 00                   or     (%rax),%al
        ...
    
    000000000060104c :
      60104c:   14 00                   adc    $0x0,%al
    

    So, that's to say, your four variables are located in data section event the the same name, but with different offset.

提交回复
热议问题