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

Nick Clifton nickc@redhat.com
Wed Sep 8 14:15:00 GMT 2004


Hi Valery,

> Sorry if I bother you, but how this situation differs from the static link
> case ? Would the linker remove such function with static linking ?


> mov eax,myfxptr
> ...
> call [eax]    // in user's program
> 
> Does GCC compiler tells the linker in this case that myfx is called ?

Effectively yes.  GCC creates the "mov eax,myfxptr" instruction.  GAS 
assembles it, generating a reference to the "myfxptr" symbol in the 
process.  [GAS has to generate a reference to "myfxptr" because it does 
not know its location - it is being defined in another source file].

When LD performs a static link it sees the reference to myfxptr and so 
it knows that the section containing that symbol has to be included in 
the output.

Now of course you could argue that this logic applies to dynamically 
linked code as well.  If the  linker sees that there are no references 
to the symbol "myfxptr" and that this is the only symbol in a particular 
input section then that input section cannot be used by any code.  So it 
could throw the section away.  This is actually true in theory, but in 
practice there is a problem.  All sections have a name.  This name is 
globally visible, and therefore it can be seen by things outside of the 
dynamically linked code.  So in theory something could want to use the 
contents of that section, accessing it via its section-name, even though 
there are no other references to any symbols inside that section.  So 
the linker has to keep the section present in the dynamic 
executable/library.

Cheers
   Nick



More information about the Binutils mailing list