Weird object file link issue

Nick Clifton nickc@redhat.com
Wed Nov 23 12:43:57 GMT 2022


Hi Tom,

> So, on a lark, I added foo.o to an archive "ar cru libfoo.a foo.o" and
> linked against libfoo.a.  libfoo.a was discarded as no symbols from it 
> were used.

Correct - that is the normal way make the inclusion of an object file
optional.


> I guess I was mistaken in thinking that the symbols from a lone object
> file, if not used, would lead to the object file being discarded from 
> the final link.

Not quite.  There is a magic linker option which can achieve this effect:

   --gc-sections

This will tell the linker to run its garbage collector, discarding any
input sections which it can prove are not needed in the output.  Since
none of the sections in foo.o are needed, it should be entirely discarded.
(Well maybe a few note or debug sections might make it through, but no
code from foo.o should be present).

Cheers
   Nick




More information about the Binutils mailing list