Local symbols
It is important to realize that local linker symbols are not the same as local
time on the stack and are not of interest to the linker.
符号表.symtab中不包含对应于本地非静态程序变量的符号。本地非静态变量运行时在栈上管理,
链接对这些变量不感兴趣。(不属于linker的管理范畴)
本地定义成static的变量不在栈上管理
Interestingly, local procedure variables that are defined with the C static
attribute are not managed on the stack. Instead, the compiler allocates space in
.data or .bss for each definition and creates a local linker symbol in the symbol
table with a unique name.
1 int f()
2 {
3 static int x = 0;
4 return x;
5 }
6
7 int g()
8 {
9 static int x = 1;
10 return x;
11 }
In this case, the compiler allocates space for two integers in .data and exports a
pair of unique local linker symbols to the assembler. For example, it might use x.1
for the definition in function f and x.2 for the definition in function g.
C programmers use the static attribute to hide variable and function declarations inside modules
Any global variable or function declared with the static attribute is private to that module
任何声明为static的全局变量或函数是那个模块私有的。
对
gcc -o2 -g -o p main.c swap.c
生成的目标文件
readelf指令
readelf - Displays information about ELF files.
readelf -a p
得到部分消息如下
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x400370
Start of program headers: 64 (bytes into file)
Start of section headers: 4064 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 8
Size of section headers: 64 (bytes)
Number of section headers: 36
Section header string table index: 33
Symbol table '.symtab' contains 78 entries:
Num: Value Size Type Bind Vis Ndx Name
66: 0000000000600870 8 OBJECT GLOBAL DEFAULT 23 bufp0
67: 0000000000400590 0 OBJECT GLOBAL HIDDEN 14 __dso_handle
68: 0000000000600698 0 OBJECT GLOBAL HIDDEN 18 __DTOR_END__
69: 00000000004004b0 139 FUNC GLOBAL DEFAULT 12 __libc_csu_init
70: 0000000000400460 60 FUNC GLOBAL DEFAULT 12 swap
71: 0000000000600878 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
72: 0000000000600888 8 OBJECT GLOBAL DEFAULT 24 bufp1
73: 0000000000600890 0 NOTYPE GLOBAL DEFAULT ABS _end
74: 0000000000600864 8 OBJECT GLOBAL DEFAULT 23 buf
75: 0000000000600878 0 NOTYPE GLOBAL DEFAULT ABS _edata
76: 0000000000400448 21 FUNC GLOBAL DEFAULT 12 main
77: 0000000000400338 0 FUNC GLOBAL DEFAULT 10 _init
Ndx = The section number the symbol is in.
ABS is for symbols that should not be relocated. UNDEF is for undefined symbols,
that is, symbols that are referenced in this object module but defined elsewhere.
COMMON is for uninitialized data objects that are not yet allocated
UNDEF即为readelf显示的UND
重载函数在符号表中的项
比如这两个重载函数在符号表中的信息
int func(int a,int b){
return a+b;
}
double func(double a){
return a;
}
71: 0000000000400648 18 FUNC GLOBAL DEFAULT 12 _Z4funcii
69: 000000000040065a 24 FUNC GLOBAL DEFAULT 12 _Z4funcd