[PATCH] "used" attribute saves decl from linker garbage collection

Jeff Law law@redhat.com
Fri Nov 6 23:21:28 GMT 2020

On 11/5/20 4:00 AM, Jozef Lawrynowicz wrote:
> On Wed, Nov 04, 2020 at 03:58:56PM -0800, H.J. Lu wrote:
>> On Wed, Nov 4, 2020 at 3:00 PM Hans-Peter Nilsson <hp@bitrange.com> wrote:
>>> On Wed, 4 Nov 2020, H.J. Lu wrote:
>>>> On Wed, Nov 4, 2020 at 1:56 PM Hans-Peter Nilsson <hp@bitrange.com> wrote:
>>>>> On Wed, 4 Nov 2020, H.J. Lu wrote:
>>>>>> On Wed, Nov 4, 2020 at 1:03 PM Hans-Peter Nilsson <hp@bitrange.com> wrote:
>>>>>>> On Wed, 4 Nov 2020, H.J. Lu wrote:
>>>>>>>> On Wed, Nov 4, 2020 at 10:09 AM Hans-Peter Nilsson <hp@bitrange.com> wrote:
>>>>>>>>> I'm not much more than a random voice, but an assembly directive
>>>>>>>>> that specifies the symbol (IIUC your .retain directive) to
>>>>>>>> But .retain directive DOES NOT adjust symbol attribute.
>>>>> I see I missed to point out that I was speaking about the *gcc
>>>>> symbol* attribute "used".
>>>> There is no such corresponding symbol attribute in ELF.
>>> I have not missed that, nor that SHF_GNU_RETAIN is so new that
>>> it's not in binutils master.  I have also not missed that gcc
>>> caters to other object formats too.  A common symbol-specific
>>> directive such as .retain, would be better than messing with
>>> section attributes, for gcc.
>> This is totally irrelevant to SHF_GNU_RETAIN.
>>>>> It's cleaner to the compiler if it can pass on to the assembler
>>>>> the specific symbol that needs to be kept.
>>>> SHF_GNU_RETAIN is for section and GCC should place the symbol,
>>>> which should be kept, in the SHF_GNU_RETAIN section directly, not
>>>> through .retain directive.
>>> This is where opinions differ.  Anyway, this is now repetition;
>>> I'm done.
>> .retain is ill-defined.   For example,
>> [hjl@gnu-cfl-2 gcc]$ cat /tmp/x.c
>> static int xyzzy __attribute__((__used__));
>> [hjl@gnu-cfl-2 gcc]$ ./xgcc -B./ -S /tmp/x.c -fcommon
>> [hjl@gnu-cfl-2 gcc]$ cat x.s
>> .file "x.c"
>> .text
>> .retain xyzzy  <<<<<<<<< What does it do?
>> .local xyzzy
>> .comm xyzzy,4,4
>> .ident "GCC: (GNU) 11.0.0 20201103 (experimental)"
>> .section .note.GNU-stack,"",@progbits
>> [hjl@gnu-cfl-2 gcc]$
>> A symbol directive should operate on the symbol table.
>> With 'R' flag, we got
>> .file "x.c"
>> .text
>> .section .bss.xyzzy,"awR",@nobits
>> .align 4
>> .type xyzzy, @object
>> .size xyzzy, 4
>> xyzzy:
>> .zero 4
>> .ident "GCC: (GNU) 11.0.0 20201104 (experimental)"
>> .section .note.GNU-stack,"",@progbits
> I still think it is very wrong for the "used" attribute to place the
> symbol in a unique section. The structure of the sections in the object
> file should be no different whether the "used" attribute was applied to
> a symbol or not.

I tend to agree here.  Also note that someone could have a section
attribute in addition to the used attribute and that section attribute
might reference any arbitrary section.

> I will therefore have to make changes to GCC so that we can get the name
> of "unnamed" sections, and emit a .section directive with the "R" flag
> set on that section name, in order to avoid using a .retain directive.

ISTM that we could have the .retain <sym> set the R flag in the bfd
section associated with <sym>'s definition.  That has other impacts
(namely that anything else in the same section is retained as well).

THe other alternative is to carry the attribute on the symbol into the
linker and teach the linker about the new symbol flag.

I don't have any fundamental objections to .retain.  I'm not sure why HJ
is so dead set against it.


More information about the Gcc-patches mailing list