Page MenuHomePhabricator

[ELF] Support -r --gc-sections
ClosedPublic

Authored by MaskRay on Jul 19 2020, 2:59 PM.

Details

Summary

-r --gc-sections is usually not useful because it just makes intermediate output
smaller. https://bugs.llvm.org/show_bug.cgi?id=46700#c7 mentions a use case:
validating the absence of undefined symbols ealier than in the final link.

After D84129 (SHT_GROUP support in -r links), we can support -r
--gc-sections without extra code. So let's allow it.

Diff Detail

Event Timeline

MaskRay created this revision.Jul 19 2020, 2:59 PM
MaskRay marked an inline comment as done.Jul 19 2020, 3:03 PM
MaskRay added inline comments.
lld/test/ELF/relocatable-gc.s
27

Note that GNU ld will error if neither -e nor -u is specified, although --init can serve as GC roots as well.

I don't find the GNU ld error particularly useful, so I just don't bother implementing it.

The pre-merge check seems to be saying there are failing tests?

I guess if the use case for -r is a kernel module then garbage collection can make sense as the "object" is not relinked. What happens when there is no GC root? I think it will be worth detecting that and turning GC off as otherwise everything will get removed which is unlikely to be what was wanted.

I guess if the use case for -r is a kernel module then garbage collection can make sense as the "object" is not relinked. What happens when there is no GC root? I think it will be worth detecting that and turning GC off as otherwise everything will get removed which is unlikely to be what was wanted.

GNU ld has a diagnostic gc-sections requires either an entry or an undefined symbol (the resolution to https://sourceware.org/bugzilla/show_bug.cgi?id=26265 added --init and --fini).
We have other GC roots like SHT_INIT_ARRAY and C identifier sections. I don't know whether we want such a diagnostic. Users of the feature usually come from GNU ld. Failing to set a GC root can get pretty obvious broken output. I don't thing the diagnostic is very necessary.

The pre-merge check seems to be saying there are failing tests?

It depends on D84129. Harbormaster probably does not support dependent revisions.

jhenderson added inline comments.Jul 21 2020, 12:34 AM
lld/ELF/InputFiles.cpp
947–948

Whilst you're moving this comment, some grammar fixes:

apply relocation -> apply relocations
to output -> to the output

951–955

similar like we -> like we
for the case when linker script -> when a linker script
the "/DISCARD/" -> a "/DISCARD/" output section
have to handle such case and not to crash -> have to handle such cases without crashing

lld/test/ELF/relocatable-gc.s
7–8

I'm not sure I follow from this why group signature symbols and STT_SECTION symbols are kept? I think this explanation needs expanding.

21

"Other group members"

MaskRay updated this revision to Diff 279557.Jul 21 2020, 9:27 AM
MaskRay marked 5 inline comments as done.

Simplify InputFiles.cpp SHT_REL[A] handling
Add a test comment

lld/ELF/InputFiles.cpp
951–955

The two if can be merged. I merged them.

jhenderson accepted this revision.Jul 22 2020, 12:59 AM

LGTM, with one remaining nit.

lld/ELF/InputFiles.cpp
946–956

returning -> return

951–955

Makes sense!

This revision is now accepted and ready to land.Jul 22 2020, 12:59 AM
MaskRay updated this revision to Diff 279900.Jul 22 2020, 11:43 AM
MaskRay marked 2 inline comments as done.

returning -> return

grimar accepted this revision.Jul 23 2020, 1:05 AM

LGTM

This revision was automatically updated to reflect the committed changes.