This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Make SHF_GNU_RETAIN sections GC roots
ClosedPublic

Authored by MaskRay on Jan 30 2021, 2:59 PM.

Details

Summary

binutils 2.36 introduced the new section flag SHF_GNU_RETAIN (for ELFOSABI_GNU &
ELFOSABI_FREEBSD) to mark a sections as a GC root. Several LLVM side toolchain
folks (including me) were involved in the design process of SHF_GNU_RETAIN and
were happy with this proposal.

Currently GNU ld only respects SHF_GNU_RETAIN semantics for ELFOSABI_GNU &
ELFOSABI_FREEBSD object files
(https://sourceware.org/bugzilla/show_bug.cgi?id=27282). GNU ld sets EI_OSABI
to ELFOSABI_GNU for relocatable output
(https://sourceware.org/bugzilla/show_bug.cgi?id=27091). In practice the single
value EI_OSABI is neither a good indicator for object file compatibility, nor a
useful mechanism marking used ELF extensions.

For input, we respect SHF_GNU_RETAIN semantics even for ELFOSABI_NONE object
files. This is compatible with how LLD and GNU ld handle (mildly useful) STT_GNU_IFUNC
/ (emitted by GCC, considered misfeature by some folks) STB_GNU_UNIQUE input.
(As of LLVM 12.0.0, the integrated assembler does not set ELFOSABI_GNU for
STT_GNU_IFUNC/STB_GNU_UNIQUE).
Arguably STT_GNU_IFUNC/STB_GNU_UNIQUE probably need indicators in object files
but SHF_GNU_RETAIN is more likely accepted by more OSABI platforms.

For output, we take a step further than GNU ld: we don't promote ELFOSABI_NONE
to ELFOSABI_GNU for all output.

Diff Detail

Event Timeline

MaskRay requested review of this revision.Jan 30 2021, 2:59 PM
MaskRay created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptJan 30 2021, 2:59 PM
MaskRay edited the summary of this revision. (Show Details)Jan 30 2021, 4:32 PM
MaskRay updated this revision to Diff 320331.Jan 30 2021, 4:45 PM

Test output flag

jhenderson added inline comments.Feb 1 2021, 1:47 AM
lld/ELF/MarkLive.cpp
267

Nit: should we have a blank line after this if statement? It's somewhat unrelated to SHF_LINK_ORDER after all.

lld/test/ELF/gc-sections-retain.s
27

Should we use FileCheck -DFILE=%t1.o to check the files in the error message are correct?

38

Maybe it would be simpler to use split-file to create two input files? The ifdef makes the test somewhat less readable to me.

MaskRay added inline comments.Feb 1 2021, 2:19 PM
lld/ELF/MarkLive.cpp
267

This version looks equally readable to me...

lld/test/ELF/gc-sections-retain.s
27

I think it is unnecessary. There are dedicates tests checking the file name. And for this test it is difficult to make the filename wrong while keeping the .o suffix

38

This is to keep _start so that there is no warning for missing entry point.
(I think this is equally readable.)

jhenderson accepted this revision.Feb 2 2021, 12:23 AM

LGTM. Might be worth giving @peter.smith a chance to chime in too.

This revision is now accepted and ready to land.Feb 2 2021, 12:23 AM
This revision was automatically updated to reflect the committed changes.