[Bug c++/94342] GCC ignores attribute((section(...))) for static variables inside templates

2020-03-27 Thread bikineev at google dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94342

--- Comment #10 from Anton  ---
I think there are two ways to handle the problem for targets that don't support
COMDAT:
1) issue a diagnostic that the section attribute will be ignored (because it'll
go to .gnu.linkone.*);
2) fallback to weak symbols (which IIUC would be not space efficient).

[Bug c++/94342] GCC ignores attribute((section(...))) for static variables inside templates

2020-03-27 Thread bikineev at google dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94342

--- Comment #8 from Anton  ---
Yeah, the generated sections are different yet have the same name. The user
would rely on the static linker to merge the sections into a single one.

[Bug c++/94342] GCC ignores attribute((section(...))) for static variables inside templates

2020-03-26 Thread bikineev at google dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94342

--- Comment #6 from Anton  ---
I also don't understand why all the parts of a template instantiation need to
be kept in the same COMDAT group. Neither clang nor gcc does it:

template
void Index(int i) { 
  static int VAR1 = i;
  static int VAR2 = i; 
   
 }  

template void Index(int);
template void Index(int);

This gives:

COMDAT group section [1] `.group' [void Index(int)] contains 2
sections:
   [Index]Name
   [   14]   .text._Z5IndexIiEvi
   [   15]   .rela.text._Z5IndexIiEvi

COMDAT group section [2] `.group' [void Index(int)] contains 2
sections:
   [Index]Name
   [   16]   .text._Z5IndexIfEvi
   [   17]   .rela.text._Z5IndexIfEvi

COMDAT group section [3] `.group' [Index(int)::VAR] contains 1
sections:
   [Index]Name
   [   18]   .bss._ZZ5IndexIiEviE3VAR

COMDAT group section [4] `.group' [guard variable for Index(int)::VAR]
contains 1 sections:
   [Index]Name
   [   19]   .bss._ZGVZ5IndexIiEviE3VAR

COMDAT group section [5] `.group' [Index(int)::VAR2] contains 1
sections:
   [Index]Name
   [   20]   .bss._ZZ5IndexIiEviE4VAR2

COMDAT group section [6] `.group' [guard variable for
Index(int)::VAR2] contains 1 sections:
   [Index]Name
   [   21]   .bss._ZGVZ5IndexIiEviE4VAR2

COMDAT group section [7] `.group' [Index(int)::VAR] contains 1
sections:
   [Index]Name
   [   22]   .bss._ZZ5IndexIfEviE3VAR

COMDAT group section [8] `.group' [guard variable for
Index(int)::VAR] contains 1 sections:
   [Index]Name
   [   23]   .bss._ZGVZ5IndexIfEviE3VAR

COMDAT group section [9] `.group' [Index(int)::VAR2] contains 1
sections:
   [Index]Name
   [   24]   .bss._ZZ5IndexIfEviE4VAR2

COMDAT group section [   10] `.group' [guard variable for
Index(int)::VAR2] contains 1 sections:
   [Index]Name
   [   25]   .bss._ZGVZ5IndexIfEviE4VAR2


I might have misunderstood, but I think that COMDAT groups are orthogonal to
section naming.

[Bug c++/94342] GCC ignores attribute((section(...))) for static variables inside templates

2020-03-26 Thread bikineev at google dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94342

--- Comment #5 from Anton  ---
Looking at the COMDAT groups for the example with 2 instantiations (Index
and Index), I think this is what is actually expected: section for
Index must not be grouped with section for Index. In general,
different instantiations must be kept in different COMDAT groups. Otherwise, it
would not work e.g. if one TU instantiates Index, the other TU
instantiates Index and Index.

[Bug c++/94342] New: GCC ignores attribute((section(...))) for static variables inside templates

2020-03-26 Thread bikineev at google dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94342

Bug ID: 94342
   Summary: GCC ignores attribute((section(...))) for static
variables inside templates
   Product: gcc
   Version: 9.2.1
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: bikineev at google dot com
  Target Milestone: ---

GCC ignores the section attribute for static variables declared inside
templates:

template
void Index(int i) { 
  static int VAR __attribute__((used,section("NEW_SECTION"))) = i; 
   
 }  

template void Index(int);

Objdump shows that the VAR symbol is still located in .bss (not NEW_SECTION):

objdump -tC result.o
...
 u O .bss._ZZ5IndexIiEviE3VAR   0004
Index(int)::VAR
...

Tested on gcc-9.2.1. Clang's behaviour is as expected.