Useless `DT_NEEDED` tags after Garbage Collection

Christoph Conrads
Fri Oct 19 10:22:00 GMT 2018


>> A relocatable object file seemed to solve my problem:
>> * Compile an Alpha static library, pass `-fPIC -ffunction-sections` to
>>   the compiler, ensure all API symbols have default visibility, hidden
>>   otherwise.
>> * Compile a Beta static library, pass `-fPIC -ffunction-sections` to the
>>   compiler, ensure all symbols have hidden visibility.
>> * Create a relocatable object from the Alpha static library and the Beta
>>   static library with `--gc-sections --gc-keep-exported`. All dead code
>>   is removed in this step.
>> * Create a shared library from the relocatable object, pass
>>   `--as-needed` to the linker.
>> Unfortunately, the creation of the relocatable object does not work as I
>> had planned because one has to pass all of Alpha's symbols with default
>> visibility with the linker option `--entry`.

I made a simple demo before posting. It is available here:

> Really?  And multiple --entry options made a difference?

No, a single `--entry` or `--undefined` option sufficed.

> Did you mean --undefined (or -u) rather than --entry?

After re-reading the man page, I see that I should have used `--undefined` here.

> Even so, I'm
> surprised that you would need to use the -u trick on a linker
> supporting --gc-keep-exported.  What binutils version and target are
> you using?

> $ gcc -dumpmachine
> x86_64-pc-linux-gnu
> $ ld --version
> GNU ld (Gentoo 2.30 p5) 2.30.0

If both `--entry` and `--undefined` are missing, I get the following error:

> ld \
>   --print-gc-sections \
>   --gc-sections --gc-keep-exported \
>   --relocatable alpha.o libbeta.a -o reloc.o
> ld: Removing unused section '.text.beta_fn_unused' in file 'libbeta.a(beta.o)'
> ld: gc-sections requires either an entry or an undefined symbol

More information about the Binutils mailing list