From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id B525F3858D28; Mon, 8 Apr 2024 17:13:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B525F3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1712596410; bh=R2cYeT3q4d1TZsnZOdHfp5AsZ7+0RvGluLHNoAVnKj8=; h=From:To:Subject:Date:From; b=t95iB0eWPBgIVE8CvxyVgZMtmgGiK/dvuLezZtddn7RfMrB0kBLDC30iPCfh8JJCn inGaDNtJbZRueuL7D+PqZlanmy33wtKz4/HP53gL8gDvJwRM5KjfnAtMFtnm8+BMIs i/Mi38kqRLWSKof8d48bImKjf3rDrzPNO97M/s6Q= From: "abbeyj+gcc at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/114643] New: Call to a template function emitted but without the code for the template function itself Date: Mon, 08 Apr 2024 17:13:30 +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: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: abbeyj+gcc at gmail dot com 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=3D114643 Bug ID: 114643 Summary: Call to a template function emitted but without the code for the template function itself Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: abbeyj+gcc at gmail dot com Target Milestone: --- When building the following example, a linker error is produced. ``` void foo(); template class DynArray { public: virtual void SetAlloc(unsigned); int *m_pElems; unsigned m_nNumElems; unsigned m_nNumAlloc; long m_nChunkSize; }; void *DoRealloc(void *, long); template void DynArray::SetAlloc(unsigned new_size) { if (m_nChunkSize) new_size =3D m_nChunkSize * m_nChunkSize - 1 / m_nChunkSize; else if (m_nNumAlloc) if (new_size > m_nNumAlloc / 2) new_size =3D new_size > m_nNumAlloc + 1 ? new_size : m_nNumAlloc * 1.= 5 + 1; if (m_nNumElems && m_nNumAlloc && (new_size || m_pElems)) m_pElems =3D (T *)DoRealloc(m_pElems, 0); } struct object { DynArray d; void f(int size); }; template void deser(int size, T &x) { x.SetAlloc(0); if (size) { x.SetAlloc(size); foo(); } } void object::f(int size) { deser(size, d); } ``` Build with: $ g++ -fPIC -O3 -Wall -Werror -fvisibility=3Dhidden reloc.cxx -shared Output: > /usr/bin/ld: /tmp/ccLaKma6.o: relocation R_X86_64_PC32 against undefined = symbol `_ZN8DynArrayIiE8SetAllocEj' can not be used when making a shared ob= ject; recompile with -fPIC > /usr/bin/ld: final link failed: Bad value > collect2: error: ld returned 1 exit status The symbol in the above error message demangles to `DynArray::SetAlloc(unsigned int)`. The error message suggests to compile with `-fPIC` but this is already being compiled with `-fPIC`. The issue seems to be that GCC emits a call to `DynArray::SetAlloc` but doesn't emit code for `DynArray::SetAllo= c` itself. The reproducer was minimized with the help of C-Reduce. Very minor changes= to the code result in the problem not reproducing anymore. This is the smalle= st that I could manage to get it. Godbolt link: https://godbolt.org/z/1Ynz1Khac This appears to have started sometime around GCC 8.1 and still happens on t= he trunk version that's currently available on Godbolt.=