Search code examples
c++g++static-linking

Why does my executable's .dynstr contain symbols from statically linked libraries?


I'm building a C++ executable. I'm statically linking several of the C and C++ libraries I'm using, including a custom one. (I am not, however, statically linking every library I'm using.)

Even taking that into account, the executable seemed abnormally large. I used objdump -h, which told me that I was using far more space in .dynstr than I expected. I've compiled with -Os and run strip, but when I run

$ readelf -p .dynstr slamshift

I get many entries like

  [ 13588]  _ZN3yuu6windowC2Ev
  [ 1359b]  _ZTSN3yuu7gfx_ctxE
  [ 135ae]  _ZN4YAML7Scanner11ScanFlowEndEv
  [ 135ce]  __glewVertexFormatNV

for symbols in the libraries I've statically linked (my own library, yaml-cpp, and GLEW).

Why are these symbols appearing in my executable? If I've statically linked the libraries involved, shouldn't the symbol names be unnecessary?

I am using Ubuntu 12.04, GCC 4.6.3, and CMake and its default settings to build, if those are relevant.


Solution

  • Why are these symbols appearing in my executable?

    There are only two reasons for these symbols to be exported from your executable and appear in the dynamic symbol table.

    1. You linked your executable with --export-dynamic (aka -E) linker flag, or
    2. Some other shared library participating in the link references these symbols.

    If you are doing #1, the answer is simple: don't do it.

    If you got the symbols exported because of #2, you really want the symbols to be exported, otherwise the code will likely fail elsewhere.