[RFC] SHF_GNU_RETAIN ELF Section Flag

Florian Weimer fweimer@redhat.com
Fri Sep 18 12:07:42 GMT 2020


* Michael Matz:

> Hello,
>
> On Wed, 16 Sep 2020, Florian Weimer via Gnu-gabi wrote:
>
>> * Jozef Lawrynowicz:
>> 
>> > I suppose the most compelling use cases for SHF_GNU_RETAIN are when the
>> > dependency cannot be expressed with references to ELF sections. You
>> > can't use SHF_GROUP because there is nothing to group the section with.
>> 
>> But if there is nothing to group the section with, why would the link
>> editor load the object?
>> 
>> That's the part I don't understand.
>
> Hmm?  By putting it on the command line:
>
> % ld --gc-sections needed.o
>
> should retain the RETAIN sections from needed.o, even if otherwise not 
> seemingly referenced.

But this use case already works today with an implicit linker script, I
think:

$ cat needed-impl.S
	.section .data.need-to-be-kept
symbol_to_be_retained:
	.long 0
$ cat needed.o
/* GNU ld script */
INPUT (needed-impl.o)
SECTIONS {
  .data : {
    KEEP (*(.data.need-to-be-kept))
  }
}
$ gcc -c needed-impl.S
$ gcc -shared -Wl,--gc-sections needed.o
$ $ readelf -s a.out | grep symbol_to_be_retained
    35: 0000000000004020     0 NOTYPE  LOCAL  DEFAULT   20 symbol_to_be_retained

(Some polishing by a linker expert is certainly needed, but I hope you
get the idea.)

Thanks,
Florian
-- 
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill



More information about the Gnu-gabi mailing list