C source code:
#include <stdio.h> void sub(int x) { } void xiao() { static int x = 10; sub(x); } void yu() { static int x = 20; sub(x); } int main(int argc, const char *argv[]) { return 0; }
Run 'gcc' to compile it. Run 'nm -n' to give the following result:
... 0804a010 d x.1708 0804a014 d x.1704 ...
Run 'objdump -S':
... 080483b9 <xiao>: 80483b9: 55 push %ebp 80483ba: 89 e5 mov %esp,%ebp 80483bc: 83 ec 04 sub $0x4,%esp 80483bf: a1 14 a0 04 08 mov 0x804a014,%eax 80483c4: 89 04 24 mov %eax,(%esp) 80483c7: e8 e8 ff ff ff call 80483b4 <sub> 80483cc: c9 leave 80483cd: c3 ret 080483ce <yu>: 80483ce: 55 push %ebp 80483cf: 89 e5 mov %esp,%ebp 80483d1: 83 ec 04 sub $0x4,%esp 80483d4: a1 10 a0 04 08 mov 0x804a010,%eax 80483d9: 89 04 24 mov %eax,(%esp) 80483dc: e8 d3 ff ff ff call 80483b4 <sub> 80483e1: c9 leave 80483e2: c3 ret ...
We can see that xiao uses 0x804a014 and yu uses 0x804a010. Now let us run 'objdump -s':
... Contents of section .data: 804a008 00000000 00000000 14000000 0a000000 ................ ...We can that xiao's x has value 10 and yu's x has value 20.