From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 774A93858C98; Sat, 17 Feb 2024 21:17:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 774A93858C98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1708204630; bh=/MENk1R70B8rsjvUX9FxEo5IM9JQTTdit5uj2tvh1YE=; h=From:To:Subject:Date:From; b=o6czdZEdSQAxcd65fezcvejBI/5Fg5t5Qs/kdiGXjwm2QtmnoPOciMmE/MyDDB2os 8QsxJGQWLKV4C7aPoMLf+4tpczMV8WGAagSST9uA7VPNXOo0r/ZS5aVApRUUEWU4zo hQ17p8/b9uYxfK7Dy0Qtt8+7gRkAhI9ayORyz6RU= From: "tamiko at gentoo dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/113976] New: explicit instantiation of const variable template following implicit instantiation is assembled in .rodata instead of .bss Date: Sat, 17 Feb 2024 21:17:09 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: tamiko at gentoo dot org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D113976 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 const int mpi_type_id_for_type =3D mpi_type_id(static_cast(nullptr= )); /* implicit instantiation */ void broadcast() { int ierr =3D mpi_type_id_for_type; } /* explicit instantiation */ template const int mpi_type_id_for_type; 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 ge= ts assembled into the .rodata section: % objdump -xd segfault | grep mpi_type | c++filt 0000000000002004 u O .rodata 0000000000000004 mpi_type_id_for_type 0000000000004018 u O .bss 0000000000000008 guard variable for mpi_type_id_for_type and consequently the static initialization segfaults when trying to write i= nto the read only data section: 000000000000114a <_Z41__static_initialization_and_destruction_0v>: 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) # 20= 04 <_Z20mpi_type_id_for_typeImE> 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 correctly into the .bss secti= on 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=3Dgcc COLLECT_LTO_WRAPPER=3D/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/config= ure --host=3Dx86_64-pc-linux-gnu --build=3Dx86_64-pc-linux-gnu --prefix=3D/usr --bindir=3D/usr/x86_64-pc-linux-gnu/gcc-bin/13 --includedir=3D/usr/lib/gcc/x86_64-pc-linux-gnu/13/include --datadir=3D/usr/share/gcc-data/x86_64-pc-linux-gnu/13 --mandir=3D/usr/share/gcc-data/x86_64-pc-linux-gnu/13/man --infodir=3D/usr/share/gcc-data/x86_64-pc-linux-gnu/13/info --with-gxx-include-dir=3D/usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13 --disable-silent-rules --disable-dependency-tracking --with-python-dir=3D/share/gcc-data/x86_64-pc-linux-gnu/13/python --enable-objc-gc --enable-languages=3Dc,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=3Drelease --with-bugurl=3Dhttps://bugs.gentoo.org/ --with-pkgversion=3D'Gentoo Hardened 13.2.1_p20240210 p13' --with-gcc-major-version-only --enable-libstdcxx-time --enable-lto --disable-libstdcxx-pch --enable-shared --enable-threads=3Dposix --enable-__cxa_atexit --enable-clocale=3Dgnu --enable-multilib --with-multilib-list=3Dm32,m64 --disable-fixed-point --enable-targets=3Dall --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=3Dbootstrap-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=3Dgcc COLLECT_LTO_WRAPPER=3D/usr/libexec/gcc/x86_64-linux-gnu/13/lto-wrapper OFFLOAD_TARGET_NAMES=3Dnvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=3D1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion=3D'Debian 13.2.0-13' --with-bugurl=3Dfile:///usr/share/doc/gcc-13/README.Bugs --enable-languages=3Dc,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=3D/usr --with-gcc-major-version-only --program-suffix=3D-13 --program-prefix=3Dx86_64-linux-gnu- --enable-shared --enable-linker-build-= id --libexecdir=3D/usr/libexec --without-included-gettext --enable-threads=3Dp= osix --libdir=3D/usr/lib --enable-nls --enable-clocale=3Dgnu --enable-libstdcxx-= debug --enable-libstdcxx-time=3Dyes --with-default-libstdcxx-abi=3Dnew --enable-libstdcxx-backtrace --enable-gnu-unique-object --disable-vtable-ve= rify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=3Drelease --with-target-system-zlib=3Dauto --enable-objc-gc=3Dauto --enable-multiarch --disable-werror --enable-cet --with-arch-32=3Di686 --with-abi=3Dm64 --with-multilib-list=3Dm32,m64,mx32 --enable-multilib --with-tune=3Dgeneric --enable-offload-targets=3Dnvptx-none=3D/build/reproducible-path/gcc-13-13.= 2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=3D/build/reproducible-path/gcc-13-13= .2.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=3Drelease --build=3Dx86_64-linux-gnu --host=3Dx86_64-linux-gnu --target=3Dx86_64-linu= x-gnu Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 13.2.0 (Debian 13.2.0-13)=