public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113976] New: explicit instantiation of const variable template following implicit instantiation is assembled in .rodata instead of .bss
@ 2024-02-17 21:17 tamiko at gentoo dot org
  2024-02-17 21:29 ` [Bug c++/113976] [11/12/13/14 Regression] " pinskia at gcc dot gnu.org
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: tamiko at gentoo dot org @ 2024-02-17 21:17 UTC (permalink / raw)
  To: gcc-bugs

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)

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2024-03-01 14:38 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-17 21:17 [Bug c++/113976] New: explicit instantiation of const variable template following implicit instantiation is assembled in .rodata instead of .bss tamiko at gentoo dot org
2024-02-17 21:29 ` [Bug c++/113976] [11/12/13/14 Regression] " pinskia at gcc dot gnu.org
2024-02-18  6:49 ` sjames at gcc dot gnu.org
2024-02-18  8:03 ` tamiko at gentoo dot org
2024-02-18 18:06 ` [Bug c++/113976] [11/12/13/14 Regression] explicit instantiation of const variable template following implicit instantiation is assembled in .rodata instead of .bss since r8-2857-g2ec399d8a6c9c2 law at gcc dot gnu.org
2024-02-18 18:09 ` pinskia at gcc dot gnu.org
2024-02-18 18:39 ` jakub at gcc dot gnu.org
2024-02-19  8:10 ` rguenth at gcc dot gnu.org
2024-02-19 16:00 ` jakub at gcc dot gnu.org
2024-02-19 16:58 ` jakub at gcc dot gnu.org
2024-02-19 17:30 ` jakub at gcc dot gnu.org
2024-02-19 17:49 ` jakub at gcc dot gnu.org
2024-02-19 18:15 ` ppalka at gcc dot gnu.org
2024-02-28 22:22 ` cvs-commit at gcc dot gnu.org
2024-03-01 14:38 ` [Bug c++/113976] [11/12/13 " jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).