[PATCH] Support SHF_GNU_RETAIN ELF section flag

Jozef Lawrynowicz jozef.l@mittosystems.com
Wed Sep 23 16:52:11 GMT 2020


On Wed, Sep 23, 2020 at 01:51:56PM +0000, Michael Matz wrote:
> Hello,
> 
> On Wed, 23 Sep 2020, H.J. Lu via Binutils wrote:
> 
> > > I think that:
> > >
> > > >  .section .text,"ax"
> > > >    ...
> > > >  foo:
> > > >    ...
> > > >  .retain
> > > >  retained_fn:
> > > >    ...
> > >
> > > is some nice syntactic sugar compared to:
> > >
> > > >  .section .text,"ax"
> > > >    ...
> > > >  foo:
> > > >    ...
> > > >  .section .text,"axR"
> > > >  retained_fn:
> > > >    ...
> > >
> > > It's also partly for convenience; we have other directives which are
> > > synonyms or short-hand for each other.
> > >
> > 
> > You don't need to keep the whole section when only one symbol should
> > be kept.  Please drop the .retain directive.  GCC, as and ld should do the
> > right thing with
> > 
> > .section .text,"ax"
> >    ...
> > foo:
> >   ...
> >  .section .text,"axR"
> > 
> >  retained_fn:
> > 
> > where foo can be dropped and retained_fn will be kept.
> 
> This is not what we discussed at the ABI list, the flag is per section, so 
> either the whole section is retained or not.  What you describe is 
> something else that would work on a per symbol basis, which would have to 
> be specified in a different way and might or might not be a good idea.  
> But let's not conflate these two.

Also, the linker cannot currently dissect a section and remove a
particular unused symbol anyway. Since garbage collection only operates
on the section level, marking the section itself as "retained" seems
most appropriate.

As an aside.. we would run into limitations in the ELF format if trying
to mark the symbol itself as "retained". You cannot define "retain" as a
new symbol type or binding, because there is not a one-to-one mapping of
"retain" to the existing symbol types and bindings i.e. a retained
symbol might be STT_OBJECT or STT_FUNC, or STB_LOCAL or STB_GLOBAL.
st_other could be another place to store the requirement to "retain" a
symbol, but all those available bits are used up (albeit some
unofficially).

I'm not saying there's no way it could be done, but these are the
hurdles I discovered when considering that path to implementing the
"retain" behavior.

> About the .retain syntactic sugar: I also think it's not necessary, the 
> .section directive with R flag merging is good enough IMHO.

I'm OK with removing the .retain directive, since there is some
consensus it is not necessary. Also, I thought it made life easier for
GCC to mark sections as retained by omitting the section name from the
directive, but I've discovered a way around that now.

Thanks,
Jozef


More information about the Binutils mailing list