ThinLTO and removal of hidden visibility code


I noticed that only when I use -flto=full functions which have hidden visibility are removed
from code when I link it into .so file.

Is this expected behaviour? Is there a way to achieve same with —flto=thin?


Hi Damjan,

Try building with function/data sections and linker gc enabled. I.e. “-ffunction-sections -fdata-sections -Wl,–gc-sections”. If that doesn’t help, then please post a test case.

If those hidden symbols were only referenced within the same translation unit aka module, then there shouldn’t be a difference between full and thin LTO (the linker will tell the post-link compilation in both cases that there are no references outside the module and it could then be internalized and removed as long as all references in the module were inlined.

Assuming there are cross-module references initially:

With full LTO all objects are compiled post link in a monolithic blob, which means that if all references to a hidden symbol are inlined, it knows that there can be no additional references and the symbol can be removed.

With ThinLTO, each object is still compiled post link independently, while ThinLTO enables cross-module importing and inlining the defining module does not know whether those inlines were successful and therefore whether any cross-module references remain, so it needs to leave the outlined definition in place. If you enable function sections and linker GC, then the linker, when it links together the final native objects produced by the ThinLTO post-link compilations into the final native .so, can remove the symbols that were fully inlined across modules and no longer have any references.


Hi Teresa,

Thank you for detailed explanation.

I come to gc-sections solution before, but I was not sure what are the implications of using it.