This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: Useless `DT_NEEDED` tags after Garbage Collection
- From: Christoph Conrads <christoph dot conrads at fluent dot ai>
- To: Alan Modra <amodra at gmail dot com>
- Cc: binutils at sourceware dot org
- Date: Fri, 19 Oct 2018 12:22:26 +0200
- Subject: Re: Useless `DT_NEEDED` tags after Garbage Collection
- References: <CAPNgxRyWhjYmLV3Y4j50F=JiChyWzLntYO53mR7uJaoyX-Ljig@mail.gmail.com> <20181019055638.GA29220@bubble.grove.modra.org>
Hi,
>> 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:
https://gitlab.com/christoph-conrads/shared-libraries-and-gnu-ld-garbage-collection
> 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