[PATCH] Add -fgnu-retain/-fno-gnu-retain

Alan Modra amodra@gmail.com
Thu Feb 18 12:52:42 GMT 2021


On Wed, Feb 17, 2021 at 11:23:12AM +0000, Jozef Lawrynowicz wrote:
> In previous discussions, it seemed to me that there was general support
> for the "used" attribute implying SHF_GNU_RETAIN and saving symbols from
> linker garbage collection[1]. Of course, the current implementation
> results in undesirable behavior - the thought that all linker scripts
> not supporting uniquely named sections would need to be updated is quite
> alarming.
> 
> It's a shame that all this extra complication is required, just because
> we cannot have a ".retain <symbol_name>", directive.

Is that true?  Isn't the problem really that retained sections are
named as if -ffunction-sections/-fdata-sections were in force?  And
that is due to wanting separate sections so that garbage collection
works, since SHF_GNU_RETAIN is all about linker garbage collection.

I don't see how having a ".retain <symbol>" would help much.

> My preferred vision for this functionality was:
>   - SHF_GNU_RETAIN section flag indicates a section should be saved
>     from linker garbage collection.
>   - ".retain <symbol_name>" assembler directive applies SHF_GNU_RETAIN
>     to the section containing <symbol_name>.
>   - GCC "used" attribute emits a ".retain <symbol_name>" directive for
>     the symbol declaration is is applied to.  Applying the "used"
>     attribute to a symbol declaration does not change the structure of
>     the object file, beyond applying SHF_GNU_RETAIN to the section
>     containing that symbol.

That description seems to say that a ".retain foo" would mean
everything in foo's section is kept.  If foo's section was the usual
.data, you've kept virtually everything from garbage collection.
Surely you don't expect ".retain foo" to create a separate .data
section for foo?  If you do, I'm strongly against that idea.

Note that gas indeed supports multiple sections named .data that can
serve the same purpose as -fdata-sections.  See the gas doc for the
optional .section field "unique".  That might be the best way to avoid
an under-the-hood -ffunction-sections/-fdata-sections.

-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list