This is an archive of the discontinued LLVM Phabricator instance.

Make section attribute and -ffunction-sections play nicely
AbandonedPublic

Authored by probinson on Mar 2 2023, 11:00 AM.

Details

Reviewers
MaskRay
Summary

People use -ffunction-sections to put each function into its own
object-file section; this makes linker garbage-collection simpler.
However, if there's an explicit attribute((section("name")))
on the function, the programmer is indicating the function has
some special purpose, and is not expected to be garbage collected.

In that case, add the function to the "llvm.used" list, which for ELF
adds SHF_GNU_RETAIN to the section flags, preventing GC.

Diff Detail

Event Timeline

probinson created this revision.Mar 2 2023, 11:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 2 2023, 11:00 AM
probinson requested review of this revision.Mar 2 2023, 11:00 AM

the programmer is indicating the function has some special purpose, and is not expected to be garbage collected.

There are use cases that GC semantics are desired. They may place the address function in a variable.
I don't think changing the behavior by default is fine. Using an option may be fine, but it'd be nicer that GCC commits to do this as well.

probinson abandoned this revision.Mar 7 2023, 6:49 AM

I think the GC behavior with explicit section names is currently a little peculiar. For functions without a section name, -ffunction-sections allows GC to happen at the individual function level. With a section name, GC would happen at the level of all-or-nothing per input file, regardless of -ffunction-sections. That just seems unexpected and inconsistent to me.
But it is the current behavior, and given the objections it's not worth pursuing this.

MaskRay added a comment.EditedMar 7 2023, 5:17 PM

The inconsistency stems from the fact that with the default .text, the compiler may generate .text.$suffix if -ffunction-sections is specified or if a COMDAT is needed.
While with an explicit section, there is no such suffix appending mechanism used together with -ffunction-sections. (By default the assembler will combine sections of the same name.)

Therefore, there is a missing feature and perhaps GCC wants to do something with it as well, but just changing the semantics silently may upset some users.