Re: [PATCH] [RFC] arm64: enable HAVE_LD_DEAD_CODE_DATA_ELIMINATION

From: Fangrui Song
Date: Wed Mar 10 2021 - 17:43:03 EST


On 2021-03-10, Nicolas Pitre wrote:
On Mon, 1 Mar 2021, Nicholas Piggin wrote:

Excerpts from Arnd Bergmann's message of February 27, 2021 7:49 pm:
> Unlike what Nick expected in his submission, I now think the annotations
> will be needed for LTO just like they are for --gc-sections.

Yeah I wasn't sure exactly what LTO looks like or how it would work.
I thought perhaps LTO might be able to find dead code with circular /
back references, we could put references from the code back to these
tables or something so they would be kept without KEEP. I don't know, I
was handwaving!

I managed to get powerpc (and IIRC x86?) working with gc sections with
those KEEP annotations, but effectiveness of course is far worse than
what Nicolas was able to achieve with all his techniques and tricks.

But yes unless there is some other mechanism to handle these tables,
then KEEP probably has to stay. I suggest this wants a very explicit and
systematic way to handle it (maybe with some toolchain support) rather
than trying to just remove things case by case and see what breaks.

I don't know if Nicolas is still been working on his shrinking patches
recenty but he probably knows more than anyone about this stuff.

Looks like not much has changed since last time I played with this stuff.

There is a way to omit the KEEP() on tables, but something must create a
dependency from the code being pointed to by those tables to the table
entries themselves. I did write my findings in the following article
(just skip over the introductory blurb):

https://lwn.net/Articles/741494/

Hey, this article taught me R_*_NONE which motivated me to add various R_*_NONE
support to LLVM 9!

In the weekend I noticed that with binutils>=2.26, one can use
.reloc ., BFD_RELOC_NONE, target
(https://sourceware.org/bugzilla/show_bug.cgi?id=27530 ).
I implemented it for many targets in LLVM, but that will require 13.0.0.

Once that dependency is there, then the KEEP() may go and
garbage-collecting a function will also garbage-collect the table entry
automatically.

OTOH this trickery is not needed with LTO as garbage collection happens
at the source code optimization level. The KEEP() may remain in that
case as unneeded table entries will simply not be created in the first
place.

For Thin LTO, --gc-sections is still very useful.
I have more notes in https://maskray.me/blog/2021-02-28-linker-garbage-collection#link-time-optimization .