On 11/17/20 6:20 AM, H.J. Lu via Gcc-patches wrote:
> On Mon, Nov 16, 2020 at 7:59 PM Hans-Peter Nilsson <h...@bitrange.com> wrote:
>> On Fri, 13 Nov 2020, H.J. Lu via Gcc-patches wrote:
>>> Done.  Here is the updated patch.
>> Hi.  I see a test-case for this kind of construct:
>>
>>  int foo __attribute__((__used__, __section__ (".bar"))) = 42;
>>
>> and IIUC that it's handled as I'd hope (setting "R" on the named
>> section, not another derived section), good.
>>
>> Could you also add a test-case that the same construct
>> *without* a specific initializer is handled the same way?
>> I.e.:
>>  int foo __attribute__((__used__, __section__ (".bar")));
>>
> Done.  The only changes are
>
> /* { dg-final { scan-assembler ".data.used_bar_sec,\"awR\"" } } */
> ...
> int __attribute__((used,section(".data.used_bar_sec"))) used_bar;
>
> and 2 additional tests for -fcommon.
>
> Thanks.
>
>
> 0001-Use-SHF_GNU_RETAIN-to-preserve-symbol-definitions.patch
>
> From d19f2e2ec7f0f47121a2a4c05ffe20af8972c1bb Mon Sep 17 00:00:00 2001
> From: "H.J. Lu" <hjl.to...@gmail.com>
> Date: Mon, 3 Feb 2020 11:55:43 -0800
> Subject: [PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions
>
> In assemly code, the section flag 'R' sets the SHF_GNU_RETAIN flag to
> indicate that the section must be preserved by the linker.
>
> Add SECTION_RETAIN to indicate a section should be retained by the linker
> and set SECTION_RETAIN on section for the preserved symbol if assembler
> supports SHF_GNU_RETAIN.  All retained symbols are placed in separate
> sections with
>
>       .section .data.rel.local.preserved_symbol,"awR"
> preserved_symbol:
> ...
>       .section .data.rel.local,"aw"
> not_preserved_symbol:
> ...
>
> to avoid
>
>       .section .data.rel.local,"awR"
> preserved_symbol:
> ...
> not_preserved_symbol:
> ...
>
> which places not_preserved_symbol definition in the SHF_GNU_RETAIN
> section.
>
> gcc/
>
> 2020-11-XX  H.J. Lu  <hjl.to...@gmail.com>
>
>       * configure.ac (HAVE_GAS_SHF_GNU_RETAIN): New.  Define 1 if
>       the assembler supports marking sections with SHF_GNU_RETAIN flag.
>       * output.h (SECTION_RETAIN): New.  Defined as 0x4000000.
>       (SECTION_MACH_DEP): Changed from 0x4000000 to 0x8000000.
>       (default_unique_section): Add a bool argument.
>       * varasm.c (get_section): Set SECTION_RETAIN for the preserved
>       symbol with HAVE_GAS_SHF_GNU_RETAIN.
>       (resolve_unique_section): Used named section for the preserved
>       symbol if assembler supports SHF_GNU_RETAIN.
>       (get_variable_section): Handle the preserved common symbol with
>       HAVE_GAS_SHF_GNU_RETAIN.
>       (default_elf_asm_named_section): Require the full declaration and
>       use the 'R' flag for SECTION_RETAIN.
>       * config.in: Regenerated.
>       * configure: Likewise.
>
> gcc/testsuite/
>
> 2020-11-XX  H.J. Lu  <hjl.to...@gmail.com>
>           Jozef Lawrynowicz  <joze...@mittosystems.com>
>
>       * c-c++-common/attr-used.c: Check the 'R' flag.
>       * c-c++-common/attr-used-2.c: Likewise.
>       * c-c++-common/attr-used-3.c: New test.
>       * c-c++-common/attr-used-4.c: Likewise.
>       * gcc.c-torture/compile/attr-used-retain-1.c: Likewise.
>       * gcc.c-torture/compile/attr-used-retain-2.c: Likewise.
>       * gcc.c-torture/compile/attr-used-retain-3.c: Likewise.
>       * gcc.c-torture/compile/attr-used-retain-4.c: Likewise.
>       * lib/target-supports.exp
>       (check_effective_target_R_flag_in_section): New proc.
Can we try to avoid the #if stuff, particulary in varasm.c.  We worked
pretty hard to reduce the amount of conditionally compiled code through
the years and we should try to avoid adding new instances.

HAVE_GAS_SHF_GNU_RETAIN is going to be a compile-time constant, so it
should be just as efficient to do things like

if (HAVE_GAS_SHF_GNU_RETAIN)
   ... whatever ...

This has the advantage that the same code is presented to the
front-ends, so we're at lot less likely to stumble over set-but-not-used
warnings and the like that were so problematical a few years back.  In
fact, you use that style elsewhere in varasm.c :-)


I think you need to document the new effective target check in
doc/sourcebuild.texi

With those changes this should be fine.

jeff

Reply via email to