Re: V3 [PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions

2020-12-02 Thread Joseph Myers
On Wed, 2 Dec 2020, H.J. Lu via Gcc-patches wrote:

> > Not sure if this is a GCC bug or indicates that glibc's libc_freeres_fn or
> > related macros need to change to work with both old and new GCC.
> 
> We may need to update glibc for GCC 11.  Can you open a glibc bug and
> CC me?

https://sourceware.org/bugzilla/show_bug.cgi?id=27002

-- 
Joseph S. Myers
jos...@codesourcery.com


Re: V3 [PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions

2020-12-02 Thread H.J. Lu via Gcc-patches
On Wed, Dec 2, 2020 at 1:31 PM Joseph Myers  wrote:
>
> This patch (GCC commit 6fbec038f7a7ddf29f074943611b53210d17c40c) has
> broken the glibc build (at least with current binutils master).  The
> errors are of the form:
>
> In file included from :
> gconv_dl.c: In function 'free_mem':
> gconv_dl.c:202:18: error: 'free_mem' causes a section type conflict with 
> 'do_release_all'
>   202 | libc_freeres_fn (free_mem)
>   |  ^~~~
> ./../include/libc-symbols.h:316:15: note: in definition of macro 
> 'libc_freeres_fn'
>   316 |   static void name (void)
>   |   ^~~~
> gconv_dl.c:191:1: note: 'do_release_all' was declared here
>   191 | do_release_all (void *nodep)
>   | ^~
>
> Not sure if this is a GCC bug or indicates that glibc's libc_freeres_fn or
> related macros need to change to work with both old and new GCC.

We may need to update glibc for GCC 11.  Can you open a glibc bug and
CC me?

Thanks.

-- 
H.J.


Re: V3 [PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions

2020-12-02 Thread Joseph Myers
This patch (GCC commit 6fbec038f7a7ddf29f074943611b53210d17c40c) has 
broken the glibc build (at least with current binutils master).  The 
errors are of the form:

In file included from :
gconv_dl.c: In function 'free_mem':
gconv_dl.c:202:18: error: 'free_mem' causes a section type conflict with 
'do_release_all'
  202 | libc_freeres_fn (free_mem)
  |  ^~~~
./../include/libc-symbols.h:316:15: note: in definition of macro 
'libc_freeres_fn'
  316 |   static void name (void)
  |   ^~~~
gconv_dl.c:191:1: note: 'do_release_all' was declared here
  191 | do_release_all (void *nodep)
  | ^~

Not sure if this is a GCC bug or indicates that glibc's libc_freeres_fn or 
related macros need to change to work with both old and new GCC.

-- 
Joseph S. Myers
jos...@codesourcery.com


Re: V3 [PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions

2020-12-01 Thread Jeff Law via Gcc-patches



On 12/1/20 5:36 PM, H.J. Lu wrote:
> On Tue, Dec 1, 2020 at 4:04 PM Jeff Law  wrote:
>>
>>
>> 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  
>>> 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" 
>>> 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  
>>>
>>>   * 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 0x400.
>>>   (SECTION_MACH_DEP): Changed from 0x400 to 0x800.
>>>   (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  
>>>   Jozef Lawrynowicz  
>>>
>>>   * 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 c

V3 [PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions

2020-12-01 Thread H.J. Lu via Gcc-patches
On Tue, Dec 1, 2020 at 4:04 PM Jeff Law  wrote:
>
>
>
> 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  
> > 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" 
> > 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  
> >
> >   * 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 0x400.
> >   (SECTION_MACH_DEP): Changed from 0x400 to 0x800.
> >   (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  
> >   Jozef Lawrynowicz  
> >
> >   * 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 ...

Fixed.

> 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.  

Re: V2 [PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions

2020-12-01 Thread Jeff Law via Gcc-patches



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  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" 
> 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  
>
>   * 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 0x400.
>   (SECTION_MACH_DEP): Changed from 0x400 to 0x800.
>   (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  
>   Jozef Lawrynowicz  
>
>   * 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



Re: V2 [PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions

2020-11-17 Thread H.J. Lu via Gcc-patches
On Mon, Nov 16, 2020 at 7:59 PM Hans-Peter Nilsson  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.

-- 
H.J.
From d19f2e2ec7f0f47121a2a4c05ffe20af8972c1bb Mon Sep 17 00:00:00 2001
From: "H.J. Lu" 
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  

	* 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 0x400.
	(SECTION_MACH_DEP): Changed from 0x400 to 0x800.
	(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  
	Jozef Lawrynowicz  

	* 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.
---
 gcc/config.in |  7 +++
 gcc/configure | 51 +++
 gcc/configure.ac  | 20 
 gcc/output.h  |  6 ++-
 gcc/testsuite/c-c++-common/attr-used-2.c  |  1 +
 gcc/testsuite/c-c++-common/attr-used-3.c  |  7 +++
 gcc/testsuite/c-c++-common/attr-used-4.c  |  7 +++
 gcc/testsuite/c-c++-common/attr-used.c|  1 +
 .../compile/attr-used-retain-1.c  | 37 ++
 .../compile/attr-used-retain-2.c  | 17 +++
 .../compile/attr-used-retain-3.c  | 17 +++
 .../compile/attr-used-retain-4.c  | 17 +++
 gcc/testsuite/lib/target-supports.exp | 40 +++
 gcc/varasm.c  | 17 +--
 14 files changed, 241 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/c-c++-common/attr-used-3.c
 create mode 100644 gcc/testsuite/c-c++-common/attr-used-4.c
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/attr-used-retain-1.c
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/attr-used-retain-2.c
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/attr-used-retain-3.c
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/attr-used-retain-4.c

diff --git a/gcc/config.in b/gcc/config.in
index b7c3107bfe3..23ae2f9bc1b 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -1352,6 +1352,13 @@
 #endif
 
 
+/* Define 0/1 if your assembler supports marking sections with SHF_GNU_RETAIN
+   flag. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_GAS_SHF_GNU_RETAIN
+#endif
+
+
 /* Define 0/1 if your assembler supports marking sections with SHF_MERGE flag.
*/
 #ifndef USED_FOR_TARGET
diff --git a/gcc/configure b/gcc/configure
index dbda4415a17..a925a6e5efb 100755
--- a/gcc/configure
+

Re: V2 [PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions

2020-11-16 Thread Hans-Peter Nilsson
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")));

Thanks in advance.

brgds, H-P


V2 [PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions

2020-11-13 Thread H.J. Lu via Gcc-patches
On Fri, Nov 13, 2020 at 3:36 AM Jozef Lawrynowicz
 wrote:
>
> On Thu, Nov 12, 2020 at 02:41:52PM -0800, H.J. Lu wrote:
> > diff --git a/gcc/varasm.c b/gcc/varasm.c
> > index 435c7b348a5..c48ef9692ee 100644
> > --- a/gcc/varasm.c
> > +++ b/gcc/varasm.c
> > @@ -289,6 +289,10 @@ get_section (const char *name, unsigned int flags, 
> > tree decl,
> >slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
> >   INSERT);
> >flags |= SECTION_NAMED;
> > +#if HAVE_GAS_SHF_GNU_RETAIN
> > +  if (decl != nullptr && DECL_PRESERVE_P (decl))
>
> Minor nit, but I think this should be "decl != NULL_TREE".

We are using C++ now.  Should we start using nullptr instead of
NULL_TREE or NULL_RTX?

> We should also test that "used" with the "section" attribute applies the
> "R" flag. Please apply the attached patch if this gets approved. These
> new tests pass with arm-none-eabi and x86_64-pc-linux-gnu.
>

Done.  Here is the updated patch.

-- 
H.J.
From 07c4c78c43d3b94e56d6ace97b660c69998011e4 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" 
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  

	* 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 0x400.
	(SECTION_MACH_DEP): Changed from 0x400 to 0x800.
	(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  
	Jozef Lawrynowicz  

	* 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.
	* lib/target-supports.exp
	(check_effective_target_R_flag_in_section): New proc.
---
 gcc/config.in |  7 +++
 gcc/configure | 51 +++
 gcc/configure.ac  | 20 
 gcc/output.h  |  6 ++-
 gcc/testsuite/c-c++-common/attr-used-2.c  |  1 +
 gcc/testsuite/c-c++-common/attr-used-3.c  |  7 +++
 gcc/testsuite/c-c++-common/attr-used-4.c  |  7 +++
 gcc/testsuite/c-c++-common/attr-used.c|  1 +
 .../compile/attr-used-retain-1.c  | 35 +
 .../compile/attr-used-retain-2.c  | 16 ++
 gcc/testsuite/lib/target-supports.exp | 40 +++
 gcc/varasm.c  | 17 +--
 12 files changed, 204 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/c-c++-common/attr-used-3.c
 create mode 100644 gcc/testsuite/c-c++-common/attr-used-4.c
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/attr-used-retain-1.c
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/attr-used-retain-2.c

diff --git a/gcc/config.in b/gcc/config.in
index b7c3107bfe3..23ae2f9bc1b 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -1352,6 +1352,13 @@
 #endif
 
 
+/* Define 0/1 if your assembler supports marking sections with SHF_GNU_RETAIN
+   flag. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_GAS_SHF_GNU_RETAIN
+#endif
+
+
 /* Define 0/1 if your assembler supports marking sections with SHF_MERGE flag.
*/
 #ifndef USED_FOR_TARGET
diff --git a/gcc/configure b/gcc/configure
index dbda4415a17..a925a6e5efb 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -24272,6 +24272,57 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
+# Test if the assembler supports the section flag 'R' f

Re: [PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions

2020-11-13 Thread Jozef Lawrynowicz
On Thu, Nov 12, 2020 at 02:41:52PM -0800, H.J. Lu wrote:
> diff --git a/gcc/varasm.c b/gcc/varasm.c
> index 435c7b348a5..c48ef9692ee 100644
> --- a/gcc/varasm.c
> +++ b/gcc/varasm.c
> @@ -289,6 +289,10 @@ get_section (const char *name, unsigned int flags, tree 
> decl,
>slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
>   INSERT);
>flags |= SECTION_NAMED;
> +#if HAVE_GAS_SHF_GNU_RETAIN
> +  if (decl != nullptr && DECL_PRESERVE_P (decl))

Minor nit, but I think this should be "decl != NULL_TREE".

We should also test that "used" with the "section" attribute applies the
"R" flag. Please apply the attached patch if this gets approved. These
new tests pass with arm-none-eabi and x86_64-pc-linux-gnu.

Thanks,
Jozef
commit cf8e26deb43d13268ab6ee231995aecbf41ba3a3
Author: Jozef Lawrynowicz 
Date:   Fri Nov 13 11:07:14 2020 +

Test "used" attribute in conjunction "section" attribute

diff --git a/gcc/testsuite/gcc.c-torture/compile/attr-used-retain-1.c 
b/gcc/testsuite/gcc.c-torture/compile/attr-used-retain-1.c
index b7763af11e4..5f6cbca6e33 100644
--- a/gcc/testsuite/gcc.c-torture/compile/attr-used-retain-1.c
+++ b/gcc/testsuite/gcc.c-torture/compile/attr-used-retain-1.c
@@ -4,6 +4,7 @@
 /* { dg-final { scan-assembler ".bss.*,\"awR\"" } } */
 /* { dg-final { scan-assembler ".data.*,\"awR\"" } } */
 /* { dg-final { scan-assembler ".rodata.*,\"aR\"" } } */
+/* { dg-final { scan-assembler ".data.used_foo_sec,\"awR\"" } } */
 
 void __attribute__((used)) used_fn (void) { }
 void unused_fn (void) { }
@@ -30,3 +31,5 @@ int __attribute__((used)) used_data2 = 1;
 const int __attribute__((used)) used_rodata2 = 2;
 int __attribute__((used)) used_comm2;
 static int __attribute__((used)) used_lcomm2;
+
+int __attribute__((used,section(".data.used_foo_sec"))) used_foo = 2;
diff --git a/gcc/testsuite/gcc.c-torture/compile/attr-used-retain-2.c 
b/gcc/testsuite/gcc.c-torture/compile/attr-used-retain-2.c
index e3b3cf184f8..be5f3917ac8 100644
--- a/gcc/testsuite/gcc.c-torture/compile/attr-used-retain-2.c
+++ b/gcc/testsuite/gcc.c-torture/compile/attr-used-retain-2.c
@@ -10,6 +10,7 @@
 /* { dg-final { scan-assembler ".rodata.used_rodata2,\"aR\"" } } */
 /* { dg-final { scan-assembler ".bss.used_lcomm,\"awR\"" { target arm-*-* } } 
} */
 /* { dg-final { scan-assembler ".bss.used_lcomm2,\"awR\"" { target arm-*-* } } 
} */
+/* { dg-final { scan-assembler ".data.used_foo_sec,\"awR\"" } } */
 /* { dg-options "-ffunction-sections -fdata-sections" } */
 
 #include "attr-used-retain-1.c"


[PATCH] Use SHF_GNU_RETAIN to preserve symbol definitions

2020-11-12 Thread H.J. Lu via Gcc-patches
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  

* 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 0x400.
(SECTION_MACH_DEP): Changed from 0x400 to 0x800.
(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  
Jozef Lawrynowicz  

* 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.
* lib/target-supports.exp
(check_effective_target_R_flag_in_section): New proc.
---
 gcc/config.in |  7 +++
 gcc/configure | 51 +++
 gcc/configure.ac  | 20 
 gcc/output.h  |  6 ++-
 gcc/testsuite/c-c++-common/attr-used-2.c  |  1 +
 gcc/testsuite/c-c++-common/attr-used-3.c  |  7 +++
 gcc/testsuite/c-c++-common/attr-used-4.c  |  7 +++
 gcc/testsuite/c-c++-common/attr-used.c|  1 +
 .../compile/attr-used-retain-1.c  | 32 
 .../compile/attr-used-retain-2.c  | 15 ++
 gcc/testsuite/lib/target-supports.exp | 40 +++
 gcc/varasm.c  | 17 +--
 12 files changed, 200 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/c-c++-common/attr-used-3.c
 create mode 100644 gcc/testsuite/c-c++-common/attr-used-4.c
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/attr-used-retain-1.c
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/attr-used-retain-2.c

diff --git a/gcc/config.in b/gcc/config.in
index b7c3107bfe3..23ae2f9bc1b 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -1352,6 +1352,13 @@
 #endif
 
 
+/* Define 0/1 if your assembler supports marking sections with SHF_GNU_RETAIN
+   flag. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_GAS_SHF_GNU_RETAIN
+#endif
+
+
 /* Define 0/1 if your assembler supports marking sections with SHF_MERGE flag.
*/
 #ifndef USED_FOR_TARGET
diff --git a/gcc/configure b/gcc/configure
index dbda4415a17..a925a6e5efb 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -24272,6 +24272,57 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
+# Test if the assembler supports the section flag 'R' for specifying
+# section with SHF_GNU_RETAIN.
+case "${target}" in
+  # Solaris may use GNU assembler with Solairs ld.  Even if GNU
+  # assembler supports the section flag 'R', it doesn't mean that
+  # Solairs ld supports it.
+  *-*-solaris2*)
+gcc_cv_as_shf_gnu_retain=no
+;;
+  *)
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for section 
'R' flag" >&5
+$as_echo_n "checking assembler for section 'R' flag... " >&6; }
+if ${gcc_cv_as_shf_gnu_retain+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  gcc_cv_as_shf_gnu_retain=no
+if test $in_tree_gas = yes; then
+if test $in_tree_gas_is_elf = yes \
+  && test $gcc_cv_gas_vers -ge `expr \( \( 2 \* 1000 \) + 36 \) \* 1000 + 0`
+  then gcc_cv_as_shf_gnu_retain=yes
+fi
+  elif test x$gcc_cv_as != x; then
+$as_echo '.section .foo,"awR",%progbits
+.byte 0' > conftest.s
+if { ac_try='$gcc_cv_as $gcc_cv_as_flags --fatal-warnings -o conftest.o 
conftest.s >&5'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+then
+   gcc_cv_as_shf_gnu_retain=yes
+