linux gcc strip命令简介


strip简介

strip经常用来去除目标文件中的一些符号表、调试符号表信息,以减小静态库、动态库和程序的大小。

strip支持的选项可通过如下命令查看:

strip --help

strip示例

有如下test.c文件

//test.c
#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;
}

下面操作编译、并对比strip前后程序的差异

mayue:~/mayueadd/learn/gcc/strip$ gcc test.c 
mayue:~/mayueadd/learn/gcc/strip$ ll a.out 
-rwxrwxr-x 1 mayue mayue 8457  1月 16 11:06 a.out*
mayue:~/mayueadd/learn/gcc/strip$ file a.out 
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x7413e8ec7c63fa4f78aca5d0c7584206d47709f6, not stripped
mayue:~/mayueadd/learn/gcc/strip$ nm a.out 
0000000000600e50 d _DYNAMIC
0000000000600fe8 d _GLOBAL_OFFSET_TABLE_
00000000004005c8 R _IO_stdin_used
                 w _Jv_RegisterClasses
0000000000600e30 d __CTOR_END__
0000000000600e28 d __CTOR_LIST__
0000000000600e40 D __DTOR_END__
0000000000600e38 d __DTOR_LIST__
00000000004006c0 r __FRAME_END__
0000000000600e48 d __JCR_END__
0000000000600e48 d __JCR_LIST__
0000000000601024 A __bss_start
0000000000601008 D __data_start
0000000000400580 t __do_global_ctors_aux
0000000000400420 t __do_global_dtors_aux
0000000000601010 D __dso_handle
                 w __gmon_start__
0000000000600e24 d __init_array_end
0000000000600e24 d __init_array_start
0000000000400570 T __libc_csu_fini
00000000004004e0 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.2.5
0000000000601024 A _edata
0000000000601040 A _end
00000000004005b8 T _fini
0000000000400390 T _init
00000000004003d0 T _start
0000000000601038 B aaa
00000000004004b4 T add
0000000000601018 D bbb
00000000004003fc t call_gmon_start
0000000000601028 b completed.6531
0000000000601008 W data_start
0000000000601030 b dtor_idx.6533
0000000000400490 t frame_dummy
00000000004004c8 T main
000000000060101c D szTest

通过ls -l 命令可知, a.out的大小是8457个字节;
通过file命令可知, a.out是可执行文件, 且是not stripped, 也就是带符号表和调试信息的;
通过nm命令, 可以读出a.out中的符号信息;

现在将a.out程序经过strip处理后得到如下结果

mayue:~/mayueadd/learn/gcc/strip$ 
mayue:~/mayueadd/learn/gcc/strip$ strip a.out 
mayue:~/mayueadd/learn/gcc/strip$ ls -l a.out 
-rwxrwxr-x 1 mayue mayue 6208  1月 16 11:08 a.out
mayue:~/mayueadd/learn/gcc/strip$ file a.out 
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x7413e8ec7c63fa4f78aca5d0c7584206d47709f6, stripped
mayue:~/mayueadd/learn/gcc/strip$ nm a.out 
nm: a.out: no symbols

通过ls -l 命令可知, a.out的大小是6208个字节, 程序大小减小;
通过file命令可知, a.out是可执行文件, 且是stripped, 也就是strip处理过的;
通过nm命令, 发现a.out中的符号没有了;

strip命令用法

https://www.linuxidc.com/Linux/2011-05/35773.htm

程序开发是否要strip

strip可以压缩目标文件、静态库、动态库、可执行程序的大小,但是会失去符号表、调试符号表信息。为了方便定位问题(比如定位 core dump问题), 建议, 尽量不要strip, 除非存储紧张。

扫描二维码关注公众号,回复: 8643601 查看本文章

在实际的开发中, 若需要对动态库.so进行strip操作, 减少占地空间。 通常的做法是: strip前的库用来调试, strip后的库用来实际发布, 他们两者有对应关系。 一旦发布的strip后的库出了问题, 就可以找对应的未strip的库来定位。

参考资料

https://blog.csdn.net/stpeace/article/details/47090255
https://blog.csdn.net/stpeace/article/details/52202420
https://blog.csdn.net/stpeace/article/details/52099242

发布了60 篇原创文章 · 获赞 43 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/mayue_web/article/details/104001392