1) 如何生成不含符号表的目标文件 ?
#一个方式是编译的时候使用 选项 s
root@root2768:~/test_c>gcc -s test1.c
root@root2768:~/test_c> objdump -t a.out
a.out: file format elf64-x86-64
symbol table:
no symbols
# 另外一个方式是,生成目标文件以后,使用strip命令从目标文件中去掉符号表
root@root2768:~/test_c> gcc test1.c
root@root2768:~/test_c> objdump -t a.out
a.out: file format elf64-x86-64
symbol table:
0000000000400238 l d .interp 0000000000000000 .interp
0000000000400254 l d .note.abi-tag 0000000000000000 .note.abi-tag
0000000000400274 l d .note.suse 0000000000000000 .note.suse
000000000040028c l d .note.gnu.build-id 0000000000000000 .note.gnu.build-id
00000000004002b0 l d .hash 0000000000000000 .hash
00000000004002c8 l d .gnu.hash 0000000000000000 .gnu.hash
00000000004002e8 l d .dynsym 0000000000000000 .dynsym
0000000000400330 l d .dynstr 0000000000000000 .dynstr
0000000000400368 l d .gnu.version 0000000000000000 .gnu.version
0000000000400370 l d .gnu.version_r 0000000000000000 .gnu.version_r
0000000000400390 l d .rela.dyn 0000000000000000 .rela.dyn
00000000004003a8 l d .rela.plt 0000000000000000 .rela.plt
00000000004003c0 l d .init 0000000000000000 .init
00000000004003e0 l d .plt 0000000000000000 .plt
0000000000400400 l d .text 0000000000000000 .text
00000000004005d8 l d .fini 0000000000000000 .fini
00000000004005f0 l d .rodata 0000000000000000 .rodata
00000000004005f4 l d .eh_frame_hdr 0000000000000000 .eh_frame_hdr
0000000000400628 l d .eh_frame 0000000000000000 .eh_frame
0000000000600e30 l d .ctors 0000000000000000 .ctors
0000000000600e40 l d .dtors 0000000000000000 .dtors
0000000000600e50 l d .jcr 0000000000000000 .jcr
0000000000600e58 l d .dynamic 0000000000000000 .dynamic
0000000000600ff8 l d .got 0000000000000000 .got
0000000000601000 l d .got.plt 0000000000000000 .got.plt
0000000000601020 l d .data 0000000000000000 .data
0000000000601038 l d .bss 0000000000000000 .bss
0000000000000000 l d .comment 0000000000000000 .comment
0000000000000000 l d .comment.suse.opts 0000000000000000 .comment.suse.opts
0000000000000000 l d .debug_aranges 0000000000000000 .debug_aranges
0000000000000000 l d .debug_pubnames 0000000000000000 .debug_pubnames
0000000000000000 l d .debug_info 0000000000000000 .debug_info
0000000000000000 l d .debug_abbrev 0000000000000000 .debug_abbrev
0000000000000000 l d .debug_line 0000000000000000 .debug_line
0000000000000000 l d .debug_frame 0000000000000000 .debug_frame
0000000000000000 l d .debug_str 0000000000000000 .debug_str
0000000000000000 l d .debug_loc 0000000000000000 .debug_loc
0000000000000000 l d .debug_ranges 0000000000000000 .debug_ranges
0000000000000000 l df *abs* 0000000000000000 init.c
0000000000000000 l df *abs* 0000000000000000
0000000000000000 l df *abs* 0000000000000000 initfini.c
000000000040042c l f .text 0000000000000000 call_gmon_start
00000000004005e8 l .fini 0000000000000000 _real_fini
0000000000000000 l df *abs* 0000000000000000 crtstuff.c
0000000000600e30 l o .ctors 0000000000000000 __ctor_list__
0000000000600e40 l o .dtors 0000000000000000 __dtor_list__
0000000000600e50 l o .jcr 0000000000000000 __jcr_list__
0000000000400450 l f .text 0000000000000000 __do_global_dtors_aux
0000000000601038 l o .bss 0000000000000001 completed.6159
0000000000601040 l o .bss 0000000000000008 dtor_idx.6161
00000000004004c0 l f .text 0000000000000000 frame_dummy
0000000000000000 l df *abs* 0000000000000000 crtstuff.c
0000000000600e38 l o .ctors 0000000000000000 __ctor_end__
0000000000400700 l o .eh_frame 0000000000000000 __frame_end__
0000000000600e50 l o .jcr 0000000000000000 __jcr_end__
00000000004005a0 l f .text 0000000000000000 __do_global_ctors_aux
0000000000000000 l df *abs* 0000000000000000 initfini.c
0000000000000000 l df *abs* 0000000000000000 test1.c
0000000000000000 l df *abs* 0000000000000000 elf-init.c
0000000000000000 l df *abs* 0000000000000000
0000000000600e2c l .ctors 0000000000000000 __init_array_end
0000000000600e58 l o .dynamic 0000000000000000 _dynamic
0000000000600e2c l .ctors 0000000000000000 __init_array_start
0000000000601000 l o .got.plt 0000000000000000 _global_offset_table_
0000000000400500 g f .text 0000000000000002 __libc_csu_fini
0000000000601048 g o .bss 0000000000000004 global_x
0000000000601020 w .data 0000000000000000 data_start
0000000000601034 g .data 0000000000000000 _edata
00000000004005d8 g f .fini 0000000000000010 _fini
0000000000600e48 g o .dtors 0000000000000000 .hidden __dtor_end__
0000000000000000 f *und* 0000000000000000 __libc_start_main@@glibc_2.2.5
0000000000601020 g .data 0000000000000000 __data_start
0000000000000000 w *und* 0000000000000000 __gmon_start__
0000000000601028 g o .data 0000000000000000 .hidden __dso_handle
00000000004005f0 g o .rodata 0000000000000004 _io_stdin_used
0000000000601030 g o .data 0000000000000004 global_y
0000000000400510 g f .text 0000000000000089 __libc_csu_init
0000000000601050 g .bss 0000000000000000 _end
0000000000400400 g f .text 0000000000000000 _start
0000000000601034 g .bss 0000000000000000 __bss_start
00000000004004ec g f .text 0000000000000012 main
0000000000000000 w *und* 0000000000000000 _jv_registerclasses
00000000004003c0 g f .init 0000000000000000 _init
root@root2768:~/test_c> strip a.out
root@root2768:~/test_c> objdump -t a.out
a.out: file format elf64-x86-64
symbol table:
no symbols
# 两种方式的区别,拷贝自stackoverflow
gcc being a compiler/linker, its -s option is something done while compiling and linking. it's also not configurable - it has a set of information which it removes, no more no less.
strip is something which can be run on an object file which is already compiled. it also has a variety of command-line options which you can use to configure which information will be removed. for example, -g strips only the debug information which gcc -g adds.
note that strip is not a bash command, though you may be running it from a bash shell. it is a command totally separate from bash, part of the gnu binary utilities suite.
2) 如何查看目标文件的符号表 ?
#一个方式是使用 objdump
root@root2768:~/test_c> objdump -t test1.o
test1.o: file format elf64-x86-64
symbol table:
0000000000000000 l df *abs* 0000000000000000 test1.c
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 l d .data 0000000000000000 .data
0000000000000000 l d .bss 0000000000000000 .bss
0000000000000000 l o .data 0000000000000004 global_s // local, can not be referenced when linking
0000000000000000 l d .eh_frame 0000000000000000 .eh_frame
0000000000000000 l d .comment.suse.opts 0000000000000000 .comment.suse.opts
0000000000000000 l d .note.gnu-stack 0000000000000000 .note.gnu-stack
0000000000000000 l d .comment 0000000000000000 .comment
0000000000000004 g o .data 0000000000000004 global_y // global, can be referenced when linking
0000000000000000 g f .text 000000000000001a main
0000000000000000 *und* 0000000000000000 global_x //undefined symbol, will search from other obj file when linking
0000000000000000 *und* 0000000000000000 foo //undefined symbol, will search from other obj file when linking
#另外一个方式是使用 nm
ejinfeg@esekilxxen2768:~/test_c> nm -s test1.o
u foo //undefined symbol, will search from other obj file when linking
0000000000000000 0000000000000004 d global_s //initialized local variable, can not be referenced by other obj file when linking
u global_x //undefined symbol, will search from other obj file when linking
0000000000000004 0000000000000004 d global_y //initialized global variable, can be referenced by other obj file when linking
0000000000000000 000000000000001a t main
c代码 test1.c
extern int foo ( int x );
extern int global_x;
static global_s = 2;
int global_y = 1;
int main ()
{
foo(global_x);
int local_x;
return 0;
}
# 附上 nm 命令链接