linux中的strip命令簡介------給文件脫衣服


轉載於:http://blog.csdn.net/stpeace/article/details/47090255

作為一名linux開發人員, 如果沒有聽說過strip命令, 那是很不應該的。 strip這個單詞, 大家應該早就學過了, 你就記住是脫衣服就行了, 別的不要多想。 在linux中, strip也有脫衣服的含義, 具體就是從特定文件中剝掉一些符號信息和調試信息。

        我們來看main.c文件:

 

  1. #include <stdio.h>  
  2.   
  3. int add(int x, int y)  
  4. {  
  5.     return x + y;  
  6. }  
  7.   
  8. int aaa;  
  9. int bbb = 1;  
  10. char szTest[] = "good";  
  11.   
  12. int main()  
  13. {  
  14.     int ccc = 2;  
  15.     return 0;  
  16. }  
#include <stdio.h>

int add(int x, int y)
{
	return x + y;
}

int aaa;
int bbb = 1;
char szTest[] = "good";

int main()
{
	int ccc = 2;
	return 0;
}

       然后我們看看結果:

 

 

  1. [taoge@localhost learn_strip]$ ls  
  2. main.c  
  3. [taoge@localhost learn_strip]$ gcc main.c   
  4. [taoge@localhost learn_strip]$ ls -l a.out   
  5. -rwxrwxr-x 1 taoge taoge 4673 Jul 27 05:30 a.out  
  6. [taoge@localhost learn_strip]$ file a.out   
  7. a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped  
  8. [taoge@localhost learn_strip]$ nm a.out   
  9. 08049538 d _DYNAMIC  
  10. 08049604 d _GLOBAL_OFFSET_TABLE_  
  11. 0804847c R _IO_stdin_used  
  12.          w _Jv_RegisterClasses  
  13. 08049528 d __CTOR_END__  
  14. 08049524 d __CTOR_LIST__  
  15. 08049530 D __DTOR_END__  
  16. 0804952c d __DTOR_LIST__  
  17. 08048520 r __FRAME_END__  
  18. 08049534 d __JCR_END__  
  19. 08049534 d __JCR_LIST__  
  20. 08049628 A __bss_start  
  21. 08049618 D __data_start  
  22. 08048430 t __do_global_ctors_aux  
  23. 08048310 t __do_global_dtors_aux  
  24. 08048480 R __dso_handle  
  25.          w __gmon_start__  
  26. 0804842a T __i686.get_pc_thunk.bx  
  27. 08049524 d __init_array_end  
  28. 08049524 d __init_array_start  
  29. 080483c0 T __libc_csu_fini  
  30. 080483d0 T __libc_csu_init  
  31.          U __libc_start_main@@GLIBC_2.0  
  32. 08049628 A _edata  
  33. 08049634 A _end  
  34. 0804845c T _fini  
  35. 08048478 R _fp_hw  
  36. 08048274 T _init  
  37. 080482e0 T _start  
  38. 08049630 B aaa  
  39. 08048394 T add  
  40. 0804961c D bbb  
  41. 08049628 b completed.5963  
  42. 08049618 W data_start  
  43. 0804962c b dtor_idx.5965  
  44. 08048370 t frame_dummy  
  45. 080483a2 T main  
  46. 08049620 D szTest  
  47. [taoge@localhost learn_strip]$   
[taoge@localhost learn_strip]$ ls
main.c
[taoge@localhost learn_strip]$ gcc main.c 
[taoge@localhost learn_strip]$ ls -l a.out 
-rwxrwxr-x 1 taoge taoge 4673 Jul 27 05:30 a.out
[taoge@localhost learn_strip]$ file a.out 
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped
[taoge@localhost learn_strip]$ nm a.out 
08049538 d _DYNAMIC
08049604 d _GLOBAL_OFFSET_TABLE_
0804847c R _IO_stdin_used
         w _Jv_RegisterClasses
08049528 d __CTOR_END__
08049524 d __CTOR_LIST__
08049530 D __DTOR_END__
0804952c d __DTOR_LIST__
08048520 r __FRAME_END__
08049534 d __JCR_END__
08049534 d __JCR_LIST__
08049628 A __bss_start
08049618 D __data_start
08048430 t __do_global_ctors_aux
08048310 t __do_global_dtors_aux
08048480 R __dso_handle
         w __gmon_start__
0804842a T __i686.get_pc_thunk.bx
08049524 d __init_array_end
08049524 d __init_array_start
080483c0 T __libc_csu_fini
080483d0 T __libc_csu_init
         U __libc_start_main@@GLIBC_2.0
08049628 A _edata
08049634 A _end
0804845c T _fini
08048478 R _fp_hw
08048274 T _init
080482e0 T _start
08049630 B aaa
08048394 T add
0804961c D bbb
08049628 b completed.5963
08049618 W data_start
0804962c b dtor_idx.5965
08048370 t frame_dummy
080483a2 T main
08049620 D szTest
[taoge@localhost learn_strip]$ 

       通過ls -l 命令可知, a.out的大小是4673個字節;

 

       通過file命令可知, a.out是可執行文件, 且是not stripped, 也就是說沒有脫衣服。

       通過nm命令, 可以讀出a.out中的符號信息。

 

       現在, 我把a.out的衣服strip掉, 得到的結果為:

 

  1. [taoge@localhost learn_strip]$ ls  
  2. a.out  main.c  
  3. [taoge@localhost learn_strip]$ strip a.out   
  4. [taoge@localhost learn_strip]$ ls -l a.out   
  5. -rwxrwxr-x 1 taoge taoge 2980 Jul 27 05:34 a.out  
  6. [taoge@localhost learn_strip]$ file a.out   
  7. a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped  
  8. [taoge@localhost learn_strip]$ nm a.out   
  9. nm: a.out: no symbols  
  10. [taoge@localhost learn_strip]$   
[taoge@localhost learn_strip]$ ls
a.out  main.c
[taoge@localhost learn_strip]$ strip a.out 
[taoge@localhost learn_strip]$ ls -l a.out 
-rwxrwxr-x 1 taoge taoge 2980 Jul 27 05:34 a.out
[taoge@localhost learn_strip]$ file a.out 
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
[taoge@localhost learn_strip]$ nm a.out 
nm: a.out: no symbols
[taoge@localhost learn_strip]$ 

 

       通過ls -l 命令可知, a.out的大小是2980個字節, 大大減小;

       通過file命令可知, a.out是可執行文件, 且是stripped, 也就是說衣服被脫了;

       通過nm命令, 發現a.out中的符號沒有了。

 

        由此可見, strip用於脫掉文件的衣服, 文件會變小, 其中的符號信息會失去。 那這個strip有什么用呢? 很有用的! 原來的a.out比較大, 可以執行。 在strip之后, 文件變小了, 仍然可以執行, 這就就節省了很多空間。

        其實, strip不僅僅可以針對可執行文件, 還能針對目標文件和動態庫等。

     

        在實際的開發中, 經常需要對動態庫.so進行strip操作, 減少占地空間。 而在調試的時候(比如用addr2line), 就需要符號了。 因此, 通常的做法是: strip前的庫用來調試, strip后的庫用來實際發布, 他們兩者有對應關系。 一旦發布的strip后的庫出了問題, 就可以找對應的未strip的庫來定位。

 

        最后啰嗦一句, 某某動態庫strip前是18M左右, strip后是3M左右, 可見, 脫脫衣服還是有明顯好處的。

 

        補充: 后來發現, 在調試過程中, 經常涉及到傳庫, 庫太大時, 很耗費傳輸時間, 所以還是用strip來搞一下吧。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM