title: Stripping linux shared libraries
body: <p>We've recently been asked to ship a Linux version of one of our libraries, previously we've developed under Linux and shipped for Windows where deploying libraries is generally a lot easier. The problem we've hit upon is in stripping the exported symbols down to only those in the exposed interface. There are three good reasons for wanting to do this</p>
  
  <ul>
  <li>To protect the proprietary aspects of our technology from exposure through the exported symbols.</li>
  <li>To prevent users having problems with conflicting symbol names.</li>
  <li>To speed up the loading of the library (at least so I'm told).</li>
  </ul>
  
  <p>Taking a simple example then:</p>
  
  <p>test.cpp</p>
  
  <pre><code>#include &lt;cmath&gt;
  
  float private_function(float f)
  {
      return std::abs(f);
  }
  
  extern "C" float public_function(float f)
  {
      return private_function(f);
  }
  </code></pre>
  
  <p>compiled with (g++ 4.3.2, ld 2.18.93.20081009)</p>
  
  <pre><code>g++ -shared -o libtest.so test.cpp -s
  </code></pre>
  
  <p>and inspecting the symbols with</p>
  
  <pre><code>nm -DC libtest.so
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>         w _Jv_RegisterClasses
  0000047c T private_function(float)
  000004ba W std::abs(float)
  0000200c A __bss_start
           w __cxa_finalize
           w __gmon_start__
  0000200c A _edata
  00002014 A _end
  00000508 T _fini
  00000358 T _init
  0000049b T public_function
  </code></pre>
  
  <p>obviously inadequate. So next we redeclare the public function as  </p>
  
  <pre><code>extern "C" float __attribute__ ((visibility ("default"))) 
      public_function(float f)
  </code></pre>
  
  <p>and compile with</p>
  
  <pre><code>g++ -shared -o libtest.so test.cpp -s -fvisibility=hidden
  </code></pre>
  
  <p>which gives</p>
  
  <pre><code>         w _Jv_RegisterClasses
  0000047a W std::abs(float)
  0000200c A __bss_start
           w __cxa_finalize
           w __gmon_start__
  0000200c A _edata
  00002014 A _end
  000004c8 T _fini
  00000320 T _init
  0000045b T public_function
  </code></pre>
  
  <p>which is good, except that std::abs is exposed. More problematic is when we start linking in other (static) libraries outside of our control, <strong>all of the symbols we use from those libraries get exported</strong>. In addition, when we start using STL containers:</p>
  
  <pre><code>#include &lt;vector&gt;
  struct private_struct
  {
      float f;
  };
  
  void other_private_function()
  {
      std::vector&lt;private_struct&gt; v;
  }
  </code></pre>
  
  <p>we end up with many additional exports from the C++ library</p>
  
  <pre><code>00000b30 W __gnu_cxx::new_allocator&lt;private_struct&gt;::deallocate(private_struct*, unsigned int)
  00000abe W __gnu_cxx::new_allocator&lt;private_struct&gt;::new_allocator()
  00000a90 W __gnu_cxx::new_allocator&lt;private_struct&gt;::~new_allocator()
  00000ac4 W std::allocator&lt;private_struct&gt;::allocator()
  00000a96 W std::allocator&lt;private_struct&gt;::~allocator()
  00000ad8 W std::_Vector_base&lt;private_struct, std::allocator&lt;private_struct&gt; &gt;::_Vector_impl::_Vector_impl()
  00000aaa W std::_Vector_base&lt;private_struct, std::allocator&lt;private_struct&gt; &gt;::_Vector_impl::~_Vector_impl()
  00000b44 W std::_Vector_base&lt;private_struct, std::allocator&lt;private_struct&gt; &gt;::_M_deallocate(private_struct*, unsigned int)
  00000a68 W std::_Vector_base&lt;private_struct, std::allocator&lt;private_struct&gt; &gt;::_M_get_Tp_allocator()
  00000b08 W std::_Vector_base&lt;private_struct, std::allocator&lt;private_struct&gt; &gt;::_Vector_base()
  00000b6e W std::_Vector_base&lt;private_struct, std::allocator&lt;private_struct&gt; &gt;::~_Vector_base()
  00000b1c W std::vector&lt;private_struct, std::allocator&lt;private_struct&gt; &gt;::vector()
  00000bb2 W std::vector&lt;private_struct, std::allocator&lt;private_struct&gt; &gt;::~vector()
  </code></pre>
  
  <p>NB: With optimisations on you'll need to make sure the vector is actually used so the compiler doesn't optimise the unused symbols out.</p>
  
  <p>I believe my colleague has managed to construct an ad-hoc solution involving version files and modifying the STL headers (!) that appears to work, but I would like to ask:</p>
  
  <p><strong>Is there a clean way to strip all unnecessary symbols (IE ones that are not part of the exposed library functionality) from a linux shared library?</strong> I've tried quite a lot of options to both g++ and ld with little success so I'd prefer answers that are known to work rather than believed to.</p>
  
  <p>In particular:</p>
  
  <ul>
  <li>Symbols from (closed-source) static libraries are not exported.</li>
  <li>Symbols from the standard library are not exported.</li>
  <li>Non-public symbols from the object files are not exported.</li>
  </ul>
  
  <p>Our exported interface is C.</p>
  
  <p>I'm aware of the other similar questions on SO:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1638073/not-sharing-all-classes-with-shared-library">NOT sharing all classes with shared library</a></li>
  <li><a href="https://stackoverflow.com/questions/1931626/how-to-really-strip-a-binary-in-macos">How to REALLY strip a binary in MacOs</a></li>
  <li><a href="https://stackoverflow.com/questions/792195/gnu-linker-alternative-to-version-script-to-list-exported-symbols-at-the-comma">GNU linker: alternative to --version-script to list exported symbols at the command line?</a></li>
  </ul>
  
  <p>but have had little success with the answers.</p>