PDA

View Full Version : How to strip unused functions from a shared lib




kumarkapil
May 30, 2013, 02:59 AM
I am trying to build a shared library on mac (a .bundle file) so that I can use it via js-ctypes in my firefox addon. Because jsctypes has a restriction on using only c (and a small subset of c++) I have created a static library and linking it to create a bundle. The issue is, the bundle that I create, happens to be too big (~8MB) - I am not sure how to strip it down so that all unused functions/symbols are removed.

I tried adding a -s option - but that didnt do much - it brought down the size from 8.3 to 8 mb.

I also tried -Wl,--gc-sections - but I get some error - saying that is not a recognized option.

Here is how I am building the .bundle file:

$ gcc -O -fno-common -c -ansi -fdata-sections -ffunction-sections thumb.c -arch i386 -arch x86_64

$ gcc -flat_namespace -bundle -s -Os -o libThumbnailCreator.bundle thumb.o -L. -lThumbnailCreator_static -lstdc++ -arch i386 -arch x86_64

I get the libThumbnailCreator_static.a file (the static library) to the directory from where I compile this. Also, please note the static library is targetting both i386 arch and x86_64 arch. The size of the static lib is ~16MB

Can someone tell me - if I am doing something wrong?

I have compiled the same code on windows - and my resultant dll is much less in size ~1.2MB

Thanks Kapil



Madd the Sane
Jun 23, 2013, 02:36 PM
There is a linking argument on OS X (-dead_strip) that will strip unused code. I would also recommend using a symbol export file so exported functions from the static library aren't thought by the linker as needing to be not stripped if they're not used. You can ignore this if the static lib was built with the symbols hidden.

gnasher729
Jun 23, 2013, 02:51 PM
There is a linking argument on OS X (-dead_strip) that will strip unused code. I would also recommend using a symbol export file so exported functions from the static library aren't thought by the linker as needing to be not stripped if they're not used. You can ignore this if the static lib was built with the symbols hidden.

That's actually very important. Since it's a shared library, anything that is exported must be included in the library. The linker cannot know about which bits that you later dont use.