What is the difference between “gcc -s” and a “strip” command?

坚强是说给别人听的谎言 提交于 2019-11-27 05:06:11

问题


I wonder what is the difference between these two:

  • gcc -s: Remove all symbol table and relocation information from the executable.

  • strip: Discard symbols from object files.

Do they have the same meaning?

Which one do you use to:

  • reduce the size of executable?
  • speed up its running?

回答1:


gcc being a compiler/linker, its -s option is something done while linking. It's also not configurable - it has a set of information which it removes, no more no less.

strip is something which can be run on an object file which is already compiled. It also has a variety of command-line options which you can use to configure which information will be removed. For example, -g strips only the debug information which gcc -g adds.

Note that strip is not a bash command, though you may be running it from a bash shell. It is a command totally separate from bash, part of the GNU binary utilities suite.




回答2:


The accepted answer is very good but just to complement your further questions (and also as reference for anyone that end up here).

What's the equivalent to gcc -s in terms of strip with some of its options?

They both do the same thing, removing the symbols table completely. However, as @JimLewis pointed out strip allows finer control. For example, in a relocatable object, strip --strip-unneeded won't remove its global symbols. However, strip or strip --strip-all would remove the complete symbols table.

Which one do you use to reduce the size of executable and speed up its running

The symbols table is a non-allocable section of the binary. This means that it never gets loaded in RAM memory. It stores information that can be useful for debugging purporses, for instance, to print out a stacktrace when a crash happens. A case where it could make sense to remove the symbols table would be a scenario where you have serious constraints of storage capacity (in that regard, gcc -Os -s or make CXXFLAGS="-Os -s" ... is useful as it will result in a smaller slower binary that is also stripped to reduce size further). I don't think removing the symbols table would result into a speed gain for the reasons commented.

Lastly, I recommend this link about stripping shared objects: http://www.technovelty.org/linux/stripping-shared-libraries.html




回答3:


"gcc -s" removes the relocation information along with the symbol table which is not done by "strip". Note that, removing relocation information would have some effect on Address space layout randomization. See this link.




回答4:


They do similar things, but strip allows finer grained control over what gets removed from the file.



来源:https://stackoverflow.com/questions/1349166/what-is-the-difference-between-gcc-s-and-a-strip-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!