https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113976
Bug ID: 113976 Summary: explicit instantiation of const variable template following implicit instantiation is assembled in .rodata instead of .bss Product: gcc Version: 13.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: tamiko at gentoo dot org Target Milestone: --- The following snippet of code: inline int mpi_type_id(const unsigned long int *) { return 42; } /* same for inline, static, extern */ template <typename T> const int mpi_type_id_for_type = mpi_type_id(static_cast<T *>(nullptr)); /* implicit instantiation */ void broadcast() { int ierr = mpi_type_id_for_type<unsigned long>; } /* explicit instantiation */ template const int mpi_type_id_for_type<unsigned long int>; int main() {} leads to a segfault: % g++ segfault.cc -o segfault % ./segfault segmentation fault (core dumped) ./segfault The segfault happens because the int mpi_type_id_for_type<unsigned long> gets assembled into the .rodata section: % objdump -xd segfault | grep mpi_type | c++filt 0000000000002004 u O .rodata 0000000000000004 mpi_type_id_for_type<unsigned long> 0000000000004018 u O .bss 0000000000000008 guard variable for mpi_type_id_for_type<unsigned long> and consequently the static initialization segfaults when trying to write into the read only data section: 000000000000114a <_Z41__static_initialization_and_destruction_0v>: <snip> 1167: c6 00 01 movb $0x1,(%rax) 116a: bf 00 00 00 00 mov $0x0,%edi 116f: e8 18 00 00 00 call 118c <_Z11mpi_type_idPKm> 1174: 89 05 8a 0e 00 00 mov %eax,0xe8a(%rip) # 2004 <_Z20mpi_type_id_for_typeImE> <snip> Note that *any* other combination of - only broadcast() function definition - only explicit template instantiation - explicit template instantiation *before* broadcast() function definition assembles mpi_type_id_for_type<unsigned long> correctly into the .bss section Furthermore, linkage (inline, static, extern) of mpi_type_id_for_type also doesn't seem to matter. Reproduced with gcc-13.2.1, gcc-12.3.1, gcc-11.4.1 on gentoo and gcc-13.2.0 on debian. (gentoo) % gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/13/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /var/tmp/portage/sys-devel/gcc-13.2.1_p20240210/work/gcc-13-20240210/configure --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/13 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/13/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/13 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/13/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/13/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13 --disable-silent-rules --disable-dependency-tracking --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/13/python --enable-objc-gc --enable-languages=c,c++,go,objc,obj-c++,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --enable-nls --without-included-gettext --disable-libunwind-exceptions --enable-checking=release --with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo Hardened 13.2.1_p20240210 p13' --with-gcc-major-version-only --enable-libstdcxx-time --enable-lto --disable-libstdcxx-pch --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64 --disable-fixed-point --enable-targets=all --enable-libgomp --disable-libssp --disable-libada --enable-cet --disable-systemtap --disable-valgrind-annotations --enable-vtable-verify --with-zstd --with-isl --disable-isl-version-check --enable-default-pie --enable-default-ssp --with-build-config=bootstrap-cet Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 13.2.1 20240210 (Gentoo Hardened 13.2.1_p20240210 p13) (debian) % gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-linux-gnu/13/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 13.2.0-13' --with-bugurl=file:///usr/share/doc/gcc-13/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-13 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/reproducible-path/gcc-13-13.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/reproducible-path/gcc-13-13.2.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 13.2.0 (Debian 13.2.0-13)