How to remove unused functions coming from static library

Asked by Vinay Kumar Kotegowder on 2017-10-05

Hello,

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,
Vinay

Question information

Language:
English Edit question
Status:
Solved
For:
GNU ARM Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
Thomas Preud'homme
Solved:
2017-10-05
Last query:
2017-10-05
Last reply:
2017-10-05

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.

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,
Vinay

Thanks Thomas Preud'homme, that solved my question.

David Brown (davidbrown) said : #4

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.