Bug 88061 - section attributes of variable templates are ignored
Summary: section attributes of variable templates are ignored
Status: WAITING
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-11-16 15:41 UTC by Henrik
Modified: 2019-11-06 23:18 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-11-19 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Henrik 2018-11-16 15:41:40 UTC
When providing a variable template with a section attribute its instantiations do not appear in the specified section:

==========================================================

template<unsigned x>
[[gnu::section(".my_data")]] const unsigned g_my_data {x};

template const unsigned g_my_data<0xf0f0f0f0>;

==========================================================

$ g++-7 -c main.cpp -o main.o -std=c++14
$ objdump main.o --full-content

  main.o:     Format elf64-x86-64

  Section .group:
   0000 01000000 05000000                    ........        
  Section .rodata._Z9g_my_dataILj4042322160EE:
   0000 f0f0f0f0                             ....            
  Section .comment:
   0000 00474343 3a202855 62756e74 7520372e  .GCC: (Ubuntu 7.
   0010 332e302d 32337562 756e7475 327e3136  3.0-23ubuntu2~16
   0020 2e30342e 796f726b 30292037 2e332e30  .04.york0) 7.3.0
   0030 00                                   .
Comment 1 Richard Biener 2018-11-19 09:07:36 UTC
I'm not sure such request can be honored given the section has to be comdat
which essentially means it has to have it's own section for each
different instantiation.  Thus, would we reject

template<unsigned x>
[[gnu::section(".my_data")]] const unsigned g_my_data {x};

template const unsigned g_my_data<0xf0f0f0f0>;
template const unsigned g_my_data<0xf0f0f0f1>;

as invalid with an error?  Or somehow take .my_data as prefix and mangle
it appropriately, assigning it COMDAT?

Some things do not make very much sense for C++...
Comment 2 Felix Jones 2019-09-18 21:12:33 UTC
Related bug 70435

Here's a demonstration of the issue with GCC trunk x86-64 on godbolt: https://godbolt.org/z/9GFWZR

Minimal example for reproducing:
>  template<int x>
>  __attribute__( ( used, section( ".my_section" ) ) ) const int s_value { x };
>  
>  int main( int argc, char * argv[] ) {
>    return s_value<3>;
>  }

Current behaviour: s_value within section .rodata
Desired behaviour: s_value within section .my_section

Clang trunk demonstrates the desired behaviour: https://godbolt.org/z/t8FNe0
Comment 3 Strager Neds 2019-11-06 23:09:01 UTC
I also encountered this bug. I started fixing this bug (and PR 70435): https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00348.html