linux gcc strip command Introduction


Introduction strip

strip object files are often used to remove some of the symbol table, debug symbol table information, in order to reduce the size of static library, dynamic library and programs.

strip supported options can be viewed via the following command:

strip --help

Examples strip

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

Compile the following operation, and compare the difference before and after application of the 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 command understood by the size of the a.out is 8457 bytes;
understood by the command file, executable a.out file, and is not stripped, i.e. with the symbol table and debug information;
through nm command, a.out symbol information can be read out of;

The now treated strip a.out after the procedure 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月 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 command understood by the size of the a.out is 6208 bytes, the program size is reduced;
by known command file, an executable file is a.out, and is stripped In, i.e. the treated strip;
by command nm , the symbol is not found in the a.out;

strip Command Usage

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

Do you want to strip application development

strip can compress object files, static library, dynamic library, the size of the executable program, but will lose the symbol table, debug symbol table information. In order to facilitate the positioning problems (such as positioning core dump problem), it is recommended, try not to strip, unless stored tension.

In the actual development, if the need to strip the operation of dynamic libraries .so, reduce floor space. The usual practice is: pre-strip library for debugging, the library is used to strip the actual release, they both have a corresponding relationship. Once the strip published library is a problem, you can not find the corresponding strip of the library 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

Published 60 original articles · won praise 43 · views 70000 +

Guess you like

Origin blog.csdn.net/mayue_web/article/details/104001392