Wednesday, 7 August 2013

How to remove *ALL* unused symbols from a shared library?

How to remove *ALL* unused symbols from a shared library?

I have a code that I need to compile to shared library and remove ALL
unused code from, but I can't find a proper solution. Here is a simple
example:
// test.cpp, compiled with GCC -fPIC -shared -fvisibility=hidden
#include <stdio.h>
class Foo {
void bar();
};
void Foo::bar() { printf("hello"); } // unused and should be removed
// I'm using printf("hello") so I can detect the symbols with `strings`
__attribute__((visibility("default"))) void test() {} // this function is
"used"
-fvisibility=hidden makes it so that all functions are hidden by default,
and I manually mark public functions with
__attribute__((visibility("default"))). However, hidden functions are not
removed unless marked as static (which I can't do to C++ methods,
obviously).
No matter what I do, GCC will always keep void Foo::bar() and hello
around. Is there a way to remove these symbols without hacking the
compiler? (yes, I am considering it at this point!)
Thanks!

No comments:

Post a Comment