5

Linux/Gcc/LD - Toolchain.

I would like to remove STL/Boost debug symbols from libraries and executable, for two reasons:

  1. Linking gets very slow for big programs
  2. Debugging jumps into stl/boost code, which is annoying

For 1. incremental linking would be a big improvement, but AFAIK ld does not support incremental linking. There is a workaround "pseudo incremental linking" in an 1999 dr.dobb's journal (not in the web any more, but at archive.org (the idea is to put everything in a dynamic library and all updated object files in an second one that is loaded first) but this is not really a general solution.

For 2. there is a script here, but a) it did not work for me (it did not remove symbols), b) it is very slow as it works at the end of the pipe, while it would be more efficient to remove the symbols earlier.

Obviously, the other debug symbols should stay in place.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Weidenrinde
  • 2,152
  • 1
  • 20
  • 21

6 Answers6

3

GNU strip accepts regex arguments to --strip-symbols= The STL and boost symbols are name-mangled because of the namespaces they're in. I don't have GCC binutils handy at this moment, but just peek at the name mangling used for namespaces and construct the regex for 'symbols from namespace X' and pass this to --strip-symbols=

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • This doesn't help much. Not all functions generated from templates get their own symbols. Try this with a simple program that uses std::vector. – n. m. could be an AI Mar 08 '17 at 15:33
  • @n.m.: Considering that the question is about _removing_ symbols, I suppose there's no reason to even consider functions which didn't become symbols to start with! – MSalters Mar 08 '17 at 16:39
  • sorry I just mean the second part of the question, about debugging. Striipping symbols will not prevent gdb from going into functiona that don't have symbols in the first place! – n. m. could be an AI Mar 08 '17 at 16:52
2

As far as I know there's no real option to do what you want in gcc. The main problem being that all the code you want to strip debug symbols for is defined in headers.

Otherwhise it would be possible to build a library separatly, strip that, and link with the stripped version.

But only getting debug symbols from certain parts of a compilation unit, while building and linking (for your desired link time speedup) is not possible in gcc as far as I know.

Pieter
  • 17,435
  • 8
  • 50
  • 89
1

This answer provides some specifics that I needed to make MSalters' answer work for removing STL symbols.

The STL symbol names are mangled. The trick is to find a regular expression that covers these names. I looked these symbols up with GNU's Binutils:

> nm --debug-syms <objectfile>

I basically searched on STL functions, like resize. If this is difficult, the output becomes readable when using the following command:

> nm --debug-syms --demangle <objectfile>

Look up a line number containing an STL function call, then look up it's mangled name on that same line number using the first provided command. This allowed me to see that all STL symbol names began with _ZNSt[0-9]+ or _ZSt[0-9]+, etc.

To allow GNU Strip to remove these symbols I used:

> strip --wildcard              \
    --strip-symbol='_ZNKSt*'    \
    --strip-symbol='_ZNSt*'     \
    --strip-symbol='_ZSt*'      \
    --strip-symbol='_ZNSa*'     \
    <objectfile>

I used these commands directly on the compiled/linked binary. I verified the removal of these symbols by comparing the output of nm before and after the removal (I wrote the output to files and used vimdiff). The --wildcard option allows the use of regular expressions. Although I would expect [0-9]* to mean 0 to an infinite amount of numbers, here it actually means 1 number followed by an infinite amount of anything (until the end of the line).

If you are looking to not step into STL code this can be achieved by gdb's skip file command, as done here.

Hope it helps

Community
  • 1
  • 1
gospes
  • 3,819
  • 1
  • 28
  • 31
1

You probably don't want to strip the debug symbols from the shared libraries, as you may need that at some point.

If you are using GDB or DDD to debug, you may be able to get away with removing the Boost source files from the Source Path so it can't trace into the functions. (Or just don't trace into them, trace over!)

You can remove the option to compile the program with debug symbols, which will speed the link time.

Like the script you link to, you can consult the strip program ("man strip") to remove all or certain symbols.

Kris Kumler
  • 6,307
  • 3
  • 24
  • 27
1

You may want to use strip. strip --strip-unneeded --strip-debug libfoo.so

Why don't you just build without debugging in the first place though?

Leon Timmermans
  • 30,029
  • 2
  • 61
  • 110
-1

Which compiler are you using? For example, if I understand your question correctly, this is a trivial matter in MS Visual Studio.

hatcat
  • 1,876
  • 1
  • 18
  • 37