Brief introduction of linux gcc strip command

Posted by Deserteye on Thu, 16 Jan 2020 10:10:01 +0100

Reading catalogue

strip introduction

strip is often used to remove some symbol table and debug symbol table information from the target file, so as to reduce the size of static library, dynamic library and program.

The options supported by strip can be viewed by the following command:

strip --help

strip example

There is the following test.c file

//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;
}

The following operations compile and compare the differences between the programs before and after strip

mayue:~/mayueadd/learn/gcc/strip$ gcc test.c 
mayue:~/mayueadd/learn/gcc/strip$ ll a.out 
-rwxrwxr-x 1 mayue mayue 8457  1 Month 1611: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

According to the ls-l command, the size of a.out is 8457 bytes;
It can be seen from the file command that a.out is an executable file, and it is not stripped, that is, with symbol table and debugging information;
The symbol information in a.out can be read out by nm command;

Now the a.out program is processed by strip to get the following results

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 Month 1611: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

According to the ls-l command, the size of a.out is 6208 bytes, and the program size decreases;
It can be seen from the file command that a.out is an executable file, and it is stripped, that is, it is processed by strip;
Through the nm command, we find that the symbol in a.out is missing;

strip command usage

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

Whether to strip in program development

Strip can compress the size of target file, static library, dynamic library and executable program, but it will lose the information of symbol table and debugging symbol table. In order to facilitate locating problems (such as locating core dump problems), it is recommended to try not to strip unless the storage is tight.

In the actual development, if we need to strip the dynamic library. so, we can reduce the floor space. The common practice is: the library before strip is used for debugging, and the library after strip is used for actual publishing. They have a corresponding relationship. Once there is a problem with the library after the release of strip, you can find the corresponding library without strip to locate.

Reference material

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 original articles published, 43 praised, 70000 visitors+
Private letter follow

Topics: Linux SHA1