$ make clean
rm -rf test test.o main_test.o
$ make
cc -g -c -o test.o test.c
cc -g -c -o main_test.o main_test.c
cc -g test.o main_test.o -o test
$ ./test
Hello Wen Liao
$ ls test -gG
-rwxrwxr-x 1 10217 Nov 16 22:33 test
可以看到產生出來的執行檔有10217 bytes。我們進一步來看執行檔的symbol。
1234567891011121314
$ nm test
0000000000601050 B __bss_start
...
0000000000601040 D g_myStr
00000000004003e0 T _init
...
0000000000601048 d gp_myStr
...
000000000040056d T main
U printf@@GLIBC_2.2.5
...
000000000040052d t s_test
0000000000400553 T test
0000000000601050 D __TMC_END__
$ make clean
rm -rf test test.o main_test.o
$ make
cc -g -c -o test.o test.c
cc -g -c -o main_test.o main_test.c
cc -g test.o main_test.o -o test
$ nm -a test.o
0000000000000000 b .bss
0000000000000000 n .comment
0000000000000000 d .data
0000000000000000 N .debug_abbrev
0000000000000000 N .debug_aranges
0000000000000000 N .debug_info
0000000000000000 N .debug_line
0000000000000000 N .debug_str
0000000000000000 r .eh_frame
0000000000000000 D g_myStr
0000000000000008 d gp_myStr
0000000000000000 n .note.GNU-stack
U printf
0000000000000000 r .rodata
0000000000000000 t s_test
0000000000000026 T test
0000000000000000 a test.c
0000000000000000 t .text
$ ls -gG test.o
-rw-rw-r-- 1 3944 Nov 16 23:02 test.o
$ strip test.o
$ ls -gG test.o
-rw-rw-r-- 1 952 Nov 16 23:03 test.o
$ nm test.o
nm: test.o: no symbols
$ make
cc -g test.o main_test.o -o test
/usr/bin/ld: error in test.o(.eh_frame); no .eh_frame_hdr table will be created.
main_test.o: In function `main':
/home/wen/tmp/sandbox/main_test.c:6: undefined reference to `test'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
這邊我nm下了-a參數,這會顯示出所有的symbol,預設的nm輸出如下提供比較。
123456
$ nm test.o
0000000000000000 D g_myStr
0000000000000008 d gp_myStr
U printf
0000000000000000 t s_test
0000000000000026 T test
$ make clean
rm -rf test test.o main_test.o
$ ls -gG test.o
-rw-rw-r-- 1 3944 Nov 16 23:02 test.o
$ make
cc -g -c -o test.o test.c
cc -g -c -o main_test.o main_test.c
cc -g test.o main_test.o -o test
$ ls -gG test.o
-rw-rw-r-- 1 3944 Nov 16 23:02 test.o
$ strip -d test.o
$ nm -a test.o
0000000000000000 b .bss
0000000000000000 n .comment
0000000000000000 d .data
0000000000000000 r .eh_frame
0000000000000000 D g_myStr
0000000000000008 d gp_myStr
0000000000000000 n .note.GNU-stack
U printf
0000000000000000 r .rodata
0000000000000000 t s_test
0000000000000026 T test
0000000000000000 t .text
$ make
cc -g test.o main_test.o -o test
$ ./test
Hello Wen Liao
$ ls -gG test test.o
-rwxrwxr-x 1 9737 Nov 16 23:01 test
-rw-rw-r-- 1 1896 Nov 16 23:01 test.o
$ nm -a libtest.so
0000000000000000 a
0000000000201048 b .bss
0000000000201048 B __bss_start
0000000000000000 n .comment
0000000000201048 b completed.6973
0000000000000000 a crtstuff.c
0000000000000000 a crtstuff.c
w __cxa_finalize@@GLIBC_2.2.5
0000000000201030 d .data
0000000000000000 N .debug_abbrev
0000000000000000 N .debug_aranges
0000000000000000 N .debug_info
0000000000000000 N .debug_line
0000000000000000 N .debug_str
0000000000000650 t deregister_tm_clones
00000000000006c0 t __do_global_dtors_aux
0000000000200df0 t __do_global_dtors_aux_fini_array_entry
0000000000201030 d __dso_handle
0000000000200e00 d .dynamic
0000000000200e00 d _DYNAMIC
0000000000000398 r .dynstr
0000000000000230 r .dynsym
0000000000201048 D _edata
00000000000007c0 r .eh_frame
000000000000079c r .eh_frame_hdr
0000000000201050 B _end
000000000000077c T _fini
000000000000077c t .fini
0000000000200df0 t .fini_array
0000000000000700 t frame_dummy
0000000000200de8 t __frame_dummy_init_array_entry
0000000000000840 r __FRAME_END__
0000000000201000 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
0000000000201038 D g_myStr
00000000000001f0 r .gnu.hash
000000000000045c r .gnu.version
0000000000000480 r .gnu.version_r
0000000000200fd0 d .got
0000000000201000 d .got.plt
0000000000201040 d gp_myStr
00000000000005f0 T _init
00000000000005f0 t .init
0000000000200de8 t .init_array
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
0000000000200df8 d .jcr
0000000000200df8 d __JCR_END__
0000000000200df8 d __JCR_LIST__
w _Jv_RegisterClasses
00000000000001c8 r .note.gnu.build-id
0000000000000610 t .plt
U printf@@GLIBC_2.2.5
0000000000000680 t register_tm_clones
00000000000004a0 r .rela.dyn
00000000000005a8 r .rela.plt
0000000000000785 r .rodata
0000000000000735 t s_test
0000000000000760 T test
0000000000000000 a test.c
0000000000000650 t .text
0000000000201048 d __TMC_END__
$ ls -gG libtest.so
-rwxrwxr-x 1 9275 Nov 16 23:47 libtest.so
$ strip libtest.so
$ ls -gG libtest.so
-rwxrwxr-x 1 6104 Nov 16 23:47 libtest.so
$ nm -a libtest.so
nm: libtest.so: no symbols
$ LD_LIBRARY_PATH=`pwd` ./test
Hello Wen Liao