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

GccStripSymbols

Gcc Problem Overview


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?

Gcc Solutions


Solution 1 - Gcc

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.

Solution 2 - Gcc

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

Solution 3 - Gcc

"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.

Solution 4 - Gcc

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTimView Question on Stackoverflow
Solution 1 - GccCascabelView Answer on Stackoverflow
Solution 2 - GccDiego PinoView Answer on Stackoverflow
Solution 3 - GccKhaledView Answer on Stackoverflow
Solution 4 - GccJim LewisView Answer on Stackoverflow