public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104433] New: [modules] Importing <memory> and using std::make_shared causes linker errors
@ 2022-02-07 20:17 lhlaurini at hotmail dot com
  2022-10-06 14:54 ` [Bug c++/104433] " ppalka at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: lhlaurini at hotmail dot com @ 2022-02-07 20:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104433

            Bug ID: 104433
           Summary: [modules] Importing <memory> and using
                    std::make_shared causes linker errors
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lhlaurini at hotmail dot com
  Target Milestone: ---

Greetings.

I found that by importing <memory> and trying to use std::make_shared, some
symbols seem to not be defined. Example:

$ cat main.cpp 
import <memory>;

int main()
{
        std::make_shared<int>();
}

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl
--with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit
--enable-cet=auto --enable-checking=release --enable-clocale=gnu
--enable-default-pie --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id
--enable-lto --enable-multilib --enable-plugin --enable-shared
--enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-libunwind-exceptions --disable-werror
gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.1.0 (GCC) 

$ g++ -fmodules-ts -x c++-system-header memory

$ g++ -fmodules-ts main.cpp 
/usr/bin/ld: /tmp/ccezjsm1.o: in function `std::_Sp_make_shared_tag::_S_ti()':
main.cpp:(.text._ZNSt19_Sp_make_shared_tag5_S_tiEv[_ZNSt19_Sp_make_shared_tag5_S_tiEv]+0x7):
undefined reference to `std::_Sp_make_shared_tag::_S_ti()::__tag'
collect2: error: ld returned 1 exit status

$ g++-12 -v
Using built-in specs.
COLLECT_GCC=g++-12
COLLECT_LTO_WRAPPER=/home/username/.local/stow/gcc/bin/../libexec/gcc/x86_64-pc-linux-gnu/12.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc/configure --prefix=/home/username/.local
--enable-languages=c,c++ --disable-bootstrap --disable-multilib
--with-system-zlib --program-suffix=-12
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.0.1 20220207 (experimental) (GCC)

$ g++-12 -fmodules-ts -x c++-system-header memory

$ g++-12 -fmodules-ts main.cpp 
/usr/bin/ld: /tmp/cca9Gc2j.o: in function `std::_Sp_make_shared_tag::_S_ti()':
main.cpp:(.text._ZNSt19_Sp_make_shared_tag5_S_tiEv[_ZNSt19_Sp_make_shared_tag5_S_tiEv]+0x5):
undefined reference to `std::_Sp_make_shared_tag::_S_ti()::__tag'
/usr/bin/ld: /tmp/cca9Gc2j.o: in function
`std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count()':
main.cpp:(.text._ZNSt14__shared_countILN9__gnu_cxx12_Lock_policyE2EED2Ev[_ZNSt14__shared_countILN9__gnu_cxx12_Lock_policyE2EED5Ev]+0x23):
undefined reference to
`std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release()'
collect2: error: ld returned 1 exit status

As you can see the code fails on both gcc 11.1 (as packaged by Arch Linux) and
the latest gcc. There are more errors with the newer version, but these are
caused by a change in libstdc++.

>From some quick investigation, I found that the errors seem to be occur when
importing a header which either:
1. has a function that returns a static constexpr variable;
2. defines a member function of a class specialization outside the class
template declaration (using inline or not).

Additionally, I also found that defining a member function outside the
(non-template) class declaration also causes the error, unless it's marked
inline, but this problem doesn't seem to be directly triggered by <memory>.

This is my attempt to create some simpler test cases for showing the bug(s):

$ cat header.hpp
struct A
{
    static const int& f()
    {
        static constexpr int x = 0;
        return x;
    }
};

struct B
{
        static void f1();
        static void f2();
};

void B::f1()
{
}

// This is ok
inline void B::f2()
{
}

template <typename T>
struct C
{
        static void f1();
        static void f2();
};

template <>
void C<int>::f1()
{
}

template <>
inline void C<int>::f2()
{
}

$ cat main.cpp
import "header.hpp";

int main()
{
        A::f();
        B::f1();
        B::f2();
        C<int>::f1();
        C<int>::f2();
}

$ g++ -fmodules-ts -x c++-header header.hpp 
$ g++ -fmodules-ts main.cpp
/usr/bin/ld: /tmp/ccMu0ee3.o: in function `main':
main.cpp:(.text+0xa): undefined reference to `B::f1()'
/usr/bin/ld: main.cpp:(.text+0x14): undefined reference to `C<int>::f1()'
/usr/bin/ld: main.cpp:(.text+0x19): undefined reference to `C<int>::f2()'
/usr/bin/ld: /tmp/ccMu0ee3.o: in function `A::f()':
main.cpp:(.text._ZN1A1fEv[_ZN1A1fEv]+0x7): undefined reference to `A::f()::x'
collect2: error: ld returned 1 exit status

$ g++-12 -fmodules-ts -x c++-header header.hpp 
$ g++-12 -fmodules-ts main.cpp
/usr/bin/ld: /tmp/ccpnyIWr.o: in function `main':
main.cpp:(.text+0xa): undefined reference to `B::f1()'
/usr/bin/ld: main.cpp:(.text+0x14): undefined reference to `C<int>::f1()'
/usr/bin/ld: main.cpp:(.text+0x19): undefined reference to `C<int>::f2()'
/usr/bin/ld: /tmp/ccpnyIWr.o: in function `A::f()':
main.cpp:(.text._ZN1A1fEv[_ZN1A1fEv]+0x5): undefined reference to `A::f()::x'
collect2: error: ld returned 1 exit status

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

* [Bug c++/104433] [modules] Importing <memory> and using std::make_shared causes linker errors
  2022-02-07 20:17 [Bug c++/104433] New: [modules] Importing <memory> and using std::make_shared causes linker errors lhlaurini at hotmail dot com
@ 2022-10-06 14:54 ` ppalka at gcc dot gnu.org
  2022-10-07 13:33 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-10-06 14:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104433

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
                 CC|                            |ppalka at gcc dot gnu.org
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |ppalka at gcc dot gnu.org
   Last reconfirmed|                            |2022-10-06

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

* [Bug c++/104433] [modules] Importing <memory> and using std::make_shared causes linker errors
  2022-02-07 20:17 [Bug c++/104433] New: [modules] Importing <memory> and using std::make_shared causes linker errors lhlaurini at hotmail dot com
  2022-10-06 14:54 ` [Bug c++/104433] " ppalka at gcc dot gnu.org
@ 2022-10-07 13:33 ` cvs-commit at gcc dot gnu.org
  2022-10-07 13:36 ` ppalka at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-10-07 13:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104433

--- Comment #1 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:edbb2551d156d69a2e337dcd8daa69f2680d57ea

commit r13-3163-gedbb2551d156d69a2e337dcd8daa69f2680d57ea
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Oct 7 09:32:45 2022 -0400

    c++ modules: static var in inline function [PR104433]

    The below testcase fails to link with the error

      undefined reference to `f()::y'

    ultimately because during stream out for the static VAR_DECL y we
    override DECL_EXTERNAL to true, which later during IPA confuses
    symbol_table::remove_unreachable_nodes into thinking it's safe
    to not emit the symbol.

    The streaming code here already avoids overriding DECL_EXTERNAL for
    inline vars and functions, so it seems natural to extend this to
    static vars from an inline function.

            PR c++/104433

    gcc/cp/ChangeLog:

            * module.cc (trees_out::core_bools): Don't override
            DECL_EXTERNAL to true for static variables from an inline
            function.

    gcc/testsuite/ChangeLog:

            * g++.dg/modules/static-2_a.H: New test.
            * g++.dg/modules/static-2_b.C: New test.

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

* [Bug c++/104433] [modules] Importing <memory> and using std::make_shared causes linker errors
  2022-02-07 20:17 [Bug c++/104433] New: [modules] Importing <memory> and using std::make_shared causes linker errors lhlaurini at hotmail dot com
  2022-10-06 14:54 ` [Bug c++/104433] " ppalka at gcc dot gnu.org
  2022-10-07 13:33 ` cvs-commit at gcc dot gnu.org
@ 2022-10-07 13:36 ` ppalka at gcc dot gnu.org
  2022-10-11 18:50 ` lhlaurini at hotmail dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-10-07 13:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104433

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
   Target Milestone|---                         |13.0
         Resolution|---                         |FIXED

--- Comment #2 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Should be fixed on trunk after the above commit and r13-3134, thanks for the
bug report and testcases.

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

* [Bug c++/104433] [modules] Importing <memory> and using std::make_shared causes linker errors
  2022-02-07 20:17 [Bug c++/104433] New: [modules] Importing <memory> and using std::make_shared causes linker errors lhlaurini at hotmail dot com
                   ` (2 preceding siblings ...)
  2022-10-07 13:36 ` ppalka at gcc dot gnu.org
@ 2022-10-11 18:50 ` lhlaurini at hotmail dot com
  2022-10-12  9:25 ` redi at gcc dot gnu.org
  2022-10-12 19:13 ` ppalka at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: lhlaurini at hotmail dot com @ 2022-10-11 18:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104433

--- Comment #3 from Luiz Henrique Laurini <lhlaurini at hotmail dot com> ---
Thank you for the fix. The test case seems to be working as expected now.

However, the original code

import <memory>;

int main()
{
        std::make_shared<int>();
}

now causes an internal compiler error:

during IPA pass: visibility
main.cpp:6:1: internal compiler error: in function_and_variable_visibility, at
ipa-visibility.cc:712
    6 | }
      | ^
0x236019e internal_error(char const*, ...)
        ???:0
0xaa1f68 fancy_abort(char const*, int, char const*)
        ???:0
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.

Godbolt link (using gcc trunk): https://godbolt.org/z/bMYYvWd8e

Should I report a new bug?

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

* [Bug c++/104433] [modules] Importing <memory> and using std::make_shared causes linker errors
  2022-02-07 20:17 [Bug c++/104433] New: [modules] Importing <memory> and using std::make_shared causes linker errors lhlaurini at hotmail dot com
                   ` (3 preceding siblings ...)
  2022-10-11 18:50 ` lhlaurini at hotmail dot com
@ 2022-10-12  9:25 ` redi at gcc dot gnu.org
  2022-10-12 19:13 ` ppalka at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-10-12  9:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104433

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
That looks like PR 106820

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

* [Bug c++/104433] [modules] Importing <memory> and using std::make_shared causes linker errors
  2022-02-07 20:17 [Bug c++/104433] New: [modules] Importing <memory> and using std::make_shared causes linker errors lhlaurini at hotmail dot com
                   ` (4 preceding siblings ...)
  2022-10-12  9:25 ` redi at gcc dot gnu.org
@ 2022-10-12 19:13 ` ppalka at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-10-12 19:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104433

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |markmigm at gmail dot com

--- Comment #5 from Patrick Palka <ppalka at gcc dot gnu.org> ---
*** Bug 106825 has been marked as a duplicate of this bug. ***

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

end of thread, other threads:[~2022-10-12 19:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-07 20:17 [Bug c++/104433] New: [modules] Importing <memory> and using std::make_shared causes linker errors lhlaurini at hotmail dot com
2022-10-06 14:54 ` [Bug c++/104433] " ppalka at gcc dot gnu.org
2022-10-07 13:33 ` cvs-commit at gcc dot gnu.org
2022-10-07 13:36 ` ppalka at gcc dot gnu.org
2022-10-11 18:50 ` lhlaurini at hotmail dot com
2022-10-12  9:25 ` redi at gcc dot gnu.org
2022-10-12 19:13 ` ppalka 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).