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

Valery Pykhtin pykhtin@prosoft.ural.ru
Wed Sep 8 04:03:00 GMT 2004


Hi Nick,

> 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.

Thank you for clarifiing this. This is exactly what I talked about :)
However why only destructors and constructors? This is not perfect world (I
mean not all of the programmers are guru) and not all classes has such high
cohesion to have no "unneeded" function members :)

> 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.

I didn't understand that fully. How the dynamic library code can depend on
functions inside user (my) program if the program doesn't export any symbols
(entry points) ?  I suppose when I do not export any entry points, my code
can only call library functions and the library code cannot call ANY
function within my program... So what is the problem to resolve the flow
control ?

The only thing I can expect is if the library code use base classes
polimorhically and thus need virtual function from the user's program (if
the user's program extends that base class). But even in this case the
virtual table is constructed inside the contructor that must be called
within the program (as we know there is no virtual constructors) and thus
must have the reference from the flow control graph ?

I must confess that I'm the programmer with windows background and new to
GNU world. May be all symbols in a program considered to be exported? Or
what ? :)

Kind regards,
Valery




More information about the Binutils mailing list