106

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?
Community
  • 1
  • 1
Tim
  • 1
  • 141
  • 372
  • 590

4 Answers4

98

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.

xamid
  • 440
  • 11
  • 25
Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 1
    Thanks! what's the equivalent to gcc -s in terms of strip with some of its options? – Tim Aug 28 '09 at 20:53
  • 3
    Which one do you use to reduce the size of executable and speed up its running. – Tim Aug 28 '09 at 20:54
  • 1
    I think the default behavior of `strip` is similar to that of `gcc -s`, but I'm no expert. Wait for another answer if you really need to know. Personally, though, I'd avoid mucking around with anything you don't fully understand in this department. I'm pretty sure you should expect decreased executable size but not significant changes in execution speed. – Cascabel Aug 28 '09 at 21:08
62

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

Daniel Sokolowski
  • 11,982
  • 4
  • 69
  • 55
Diego Pino
  • 11,278
  • 1
  • 55
  • 57
  • 4
    +1 for "non-allocable section of the binary" and "removing the symbols table would NOT result into a speed gain" – Makesh Jun 09 '15 at 12:10
  • 1
    "it never gets loaded in RAM memory". So why does [man strip](https://sourceware.org/binutils/docs/binutils/strip.html) state _'a stripped binary which will occupy less space in RAM'_? – YvesgereY May 29 '18 at 15:12
  • 1
    @YvesgereY: I think that's a somewhat optimistic claim. It occupies less space on *disk*, and in the pagecache in RAM if the kernel's file readahead algorithm happens to pull in that page instead of a useful page. Or if there's any part of the binary that *does* need to get loaded in the same 4k page as the symbol table, then a page of mostly useless data may get pulled in to memory. (e.g. the part of the symbol table used for dynamic linking.) I'm actually not sure if the symbols needed for dynamics linking are grouped together, or if they're mixed with "useless" debug symbols. – Peter Cordes Oct 31 '20 at 10:40
  • The linked article about stripping shared objects is very good. – drlolly Jan 26 '22 at 14:37
24

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

Community
  • 1
  • 1
Khaled
  • 670
  • 6
  • 18
7

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

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96