linux中的strip命令简介------给文件脱衣服【转】

 

(转自:https://blog.csdn.net/stpeace/article/details/47090255)

我们来看main.c文件:

#include int add(int x, int y)
{return x + y;
}int aaa;
int bbb = 1;
char szTest[] = "good";int main()
{int ccc = 2;return 0;
}


       然后我们看看结果:

[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_usedw _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_handlew __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_initU __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掉, 得到的结果为:

[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不仅仅可以针对可执行文件, 还能针对目标文件和动态库等。

     

        最后啰嗦一句, 某某动态库strip前是18M左右, strip后是3M左右, 可见, 脱脱衣服还是有明显好处的。

        补充: 后来发现, 在调试过程中, 经常涉及到传库, 库太大时, 很耗费传输时间, 所以还是用strip来搞一下吧。

 

 

(转自:https://blog.csdn.net/feihongwang/article/details/6949139)

UNIX下文件压缩命令compress大家都比较熟悉了,它的压缩率比较高, 和tar命令结合使用来做数据备份是最合适不过了。但compress压缩也有缺点,就是被压缩后的文件需要用命令uncompress解压后才能正常使用。而用strip命令就没有这个问题,它能清除执行文件中不必要的标示符及调试信息,可减小文件大小而不影响正常使用。但与compress 不同的是,文件一旦strip后就不能恢复原样了,所以strip是一个减肥工具而不是压缩工具。而且,被strip后的文件不包含调试信息,就不能用dbx来调试程序了。现在让我们来具体效果如何,看下面一个程序:

#include
main()
{
  printf("hello, world\n");
}
  用cc编译以后长度为46176字节,用strip后变为了30648,足足减小了 1/3,而且仍然可以正常执行。除了用strip外,用共享库也是减小执行文件长度的方法。

注:
1.如果文件大小没有减小,那就是已经strip过了.
2.cc 编译时加上"-s"参数,具有同样的作用。
 


 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部