Does -gc-section (garbage collection) works only with static linking ?

Nick Clifton nickc@redhat.com
Tue Sep 7 14:07:00 GMT 2004


Hi Valery,

> As I understand unreachable code elimination can be only achieved with
> static linking. Am I right ?

Essentially yes, but first lets be clear about one thing:  We are 
talking about the elimination of unreachable functions, and for 
languages like C++, unused constructors and destructors.  We are not 
talking about the elimination of unreachable code inside a particular 
function.  That is the job of the compiler.

With static linking all of the references to symbols are resolved at 
link-time and so the linker has the opportunity to detect and eliminate 
unused functions.  (It needs assistance from the compiler in order to do 
this, since the linker has no knowledge of flow control or program 
semantics).  With dynamically linked code, some of the symbol references 
are left unresolved until run-time and so the linker cannot know whether 
a particular function is unreachable.  Hence it cannot eliminate anything.

Cheers
   Nick



More information about the Binutils mailing list