How to remove unused functions coming from static library

Asked by Vinay Kumar Kotegowder


I am using static library to build my custom application using arm-gnu tool.

My custom application does call function coming from static library, when doing so I do see all other function from one or more objects files from static library are reflecting in final application ELF which is increasing my ELF size.

Is it possible to use only the required function and dependent functions within my application from static library and get rid off unused functions?

If yes can you tell me how to do that please!

Best Regards,

Question information

English Edit question
GNU Arm Embedded Toolchain Edit question
No assignee Edit question
Solved by:
Thomas Preud'homme
Last query:
Last reply:
Revision history for this message
Best Thomas Preud'homme (thomas-preudhomme) said :

It's possible to get rid of unused section (ie section containing no function needed transitivtely). To do so, pass -Wl,--gc-sections to the GCC *link* command-line.

Best regards.

Revision history for this message
Vinay Kumar Kotegowder (kotegowder) said :

Hi Thomas,

I have used -fdata-sections and -ffunction-sections during compile stage and -Wl,--gc-sections at link stage which does what I expected.

Thank you for your quick reply.

Best Regards,

Revision history for this message
Vinay Kumar Kotegowder (kotegowder) said :

Thanks Thomas Preud'homme, that solved my question.

Revision history for this message
David Brown (davidbrown) said :

Just be careful with -fdata-sections - it can make code less efficient. If you have a function that refers to several file-level variables, with -fno-common but not -fdata-sections, the compiler knows the relationship between the variables, and can use one anchor register to refer to them all. But if you have -fdata-sections enabled, each goes in a different section that can be relocated by the linker - and that means the compiler needs to have an anchor register for each of them.

So -ffunction-sections is fine, but don't use -fdata-sections unless you expect to save a lot of data space.