#core dump# strip命令

strip简单的说就是给文件脱掉外衣,具体就是从特定文件中剥掉一些符号信息和调试信息,使文件变小。 

我们可以在shell中输入man strip来获取strip的些相关信息

STRIP(1)                     GNU Development Tools                    STRIP(1)

NAME
       strip - Discard symbols from object files.

SYNOPSIS
       strip [-F bfdname |--target=bfdname]
             [-I bfdname |--input-target=bfdname]
             [-O bfdname |--output-target=bfdname]
             [-s|--strip-all]
             [-S|-g|-d|--strip-debug]
             [--strip-dwo]
             [-K symbolname |--keep-symbol=symbolname]
             [-N symbolname |--strip-symbol=symbolname]
             [-w|--wildcard]
             [-x|--discard-all] [-X |--discard-locals]
             [-R sectionname |--remove-section=sectionname]
             [-o file] [-p|--preserve-dates]
             [-D|--enable-deterministic-archives]
             [-U|--disable-deterministic-archives]
             [--keep-file-symbols]
             [--only-keep-debug]
             [-v |--verbose] [-V|--version]
             [--help] [--info]
             objfile...

DESCRIPTION
       GNU strip discards all symbols from object files objfile.  The list of
       object files may include archives.  At least one object file must be
       given.

       strip modifies the files named in its argument, rather than writing
       modified copies under different names.

strip 命令减少 XCOFF(Common Object File Format) 对象文件的大小。strip 命令从 XCOFF 对象文件中有选择地除去行号信息、重定位信息、调试段、typchk 段、注释段、文件头以及所有或部分符号表。节省很多空间。特别对于嵌入式来说空间还是比较重要的。 

我们可以使用file命令来查看某个文件,如下:

瘦身前大小2856byte,足足节约将近一倍的空间!代价是除去行号信息、重定位信息、调试段、typchk 段、注释段、文件头以及所有或部分符号表。无法进行调试及定位

运行命令进行瘦身,我这是嵌入式 strip 换成 arm-linux-gnueabihf-strip

arm-linux-gnueabihf-strip dump_test

文件大小5984byte

strip不仅仅可以针对可执行文件, 还能针对目标文件和动态库等. 在实际的开发中, 经常需要对动态库.so进行strip操作, 减少占地空间。 而在调试的时候(比如用addr2line), 就需要符号了。 因此, 通常的做法是: strip前的库用来调试, strip后的库用来实际发布, 他们两者有对应关系。 一旦发布的strip后的库出了问题, 就可以找对应的未strip的库来定位。

 

整体的COFF文件结构体:

The COFF Object Format is used both for object files (.o extension) and executable files.

COFF目标格式既用于中间文件,也用于可执行文件

Some of the information is only present in object files,

一些信息只出现在对象文件中

other information is only present in the executable files.

其他的信息只出现在可执行文件中

Table G-1   COFF file components COFF文件组成

Section  区段名

Description  说明

File header  

文件头

Contains general information; always present.  

包含一般性的消息, 永远有效

Optional header  

扩展头

Contains information about an executable file; usually only present in executables.  

包含关于可执行文件的信息, 通常只出现在可执行文件中

Section header  

区段头

Contains information about the different COFF sections; one for each section.  

包含每个不同的COFF区段信息, 每个区段头对应每个区段

Raw data sections

原始数据区

One for each section containing raw data, such as machine instructions and initialized variables.  

每个区段包含的数据, 例如可执行的机器码,和用来初始化变量的数据

Relocation information

重定位信息  

Contains information about unresolved references to symbols in other modules;

包含来自其它文件中没有确定地址的符号的信息.

one for each section having external references.

每个区段都有一个外部符号

Usually only present in object files and not in executable files.  

通常在目标文件出现而不在可执行文件中出现

Line number information  

行号信息

Contains debugging information about source line numbers;

好汉源代码行号的调试信息

one for each section if compiled with the -g option.  

如果编译选项含有-g参数,那么每一个区段都含有

Symbol table  

符号表

Contains information about all the symbols in the object file;

包含目标文件的所有符号信息

present if not stripped from an executable file.  

目标文件都含有, 可执行文件如果没有剔除的话也有

String table  

字符串表

Contains long symbol names.

包含一些长过8字节的符号名  

发布了170 篇原创文章 · 获赞 207 · 访问量 459万+

猜你喜欢

转载自blog.csdn.net/xiaoting451292510/article/details/104978115