3

I'm compiling a shared object (.so) that is supposed to be LD_PRELOADed into other application. I'm linking with libstdc++ and libgcc statically using -static-libgcc -static-libstdc++ to avoid shard objects conflics. Doing this however makes my so expose everything in libstdc++ as public symbols. When I do

nm -D mylib.so

I get a lot of stuff like

00000000000714e0 W _ZNKSt15basic_streambufIcSt11char_traitsIcEE4gptrEv
0000000000071530 W _ZNKSt15basic_streambufIcSt11char_traitsIcEE4pptrEv
00000000000714d0 W _ZNKSt15basic_streambufIcSt11char_traitsIcEE5ebackEv
00000000000714f0 W _ZNKSt15basic_streambufIcSt11char_traitsIcEE5egptrEv
0000000000071540 W _ZNKSt15basic_streambufIcSt11char_traitsIcEE5epptrEv
0000000000071520 W _ZNKSt15basic_streambufIcSt11char_traitsIcEE5pbaseEv
00000000000712a0 W _ZNKSt15basic_streambufIcSt11char_traitsIcEE6getlocEv

This is bad since when this so will be LD_PRELOADed into a c++ application, these symbols are going to override the applications symbols, causing unexpected behaviour.

So how do I make gcc link statically to libstdc++ without exposing all of these symbols publically?

shoosh
  • 76,898
  • 55
  • 205
  • 325

2 Answers2

0

You could use visibility pragmas and visibility function attributes.

You probably want to give hidden or protected visibility.

I am not sure you are right to statically link the libstdc++: your resulting mylib.so might then contain non position independent code. Read also this answer which explains why it could be a bad idea, and gives other useful references.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

What I ended up doing is use a version script:

http://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_25.html

to declare what symbols are exported

shoosh
  • 76,898
  • 55
  • 205
  • 325