public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96853] New: Explicit template instantiation & thread_local interaction
@ 2020-08-30 18:45 tobias.bruell at gmail dot com
  2024-06-23 20:06 ` [Bug c++/96853] " nsoft.sagaonline at gmail dot com
  0 siblings, 1 reply; 2+ messages in thread
From: tobias.bruell at gmail dot com @ 2020-08-30 18:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96853
           Summary: Explicit template instantiation & thread_local
                    interaction
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tobias.bruell at gmail dot com
  Target Milestone: ---

Compiling the below with

> g++-10 main.cpp library.cpp -o main

in gcc 10.1 leads to an executable "main" that segfaults. The problem seems to
be that the out-commented "extern" declarations in library.hpp are missing.

So, the program might be UB, but it would be nice to get an error message from
the compiler (as Clang does).

Otherwise, the linker could give an error: the final executable "main" contains
a "callq 0x0" instruction (on x86_64) if you also add "-static" to the above
command-line. I think the problem is that "_ZTHN7library3FooIiE1fE" (TLS init
function for library::Foo<int>::f) is missing from any of the intermediate
object files.

--------------------------------
--------- library.hpp ----------

#ifndef INCLUDE_GUARD_LIBRARY_H_
#define INCLUDE_GUARD_LIBRARY_H_

namespace library
{
  template<typename T>
  struct Foo
  {
    using Func = T (*) (T);
    static thread_local Func f;
  };

  //extern template struct Foo<char>;
  //extern template struct Foo<int>;
  //extern template struct Foo<long>;
}

#endif

-------------------------------
--------- library.cpp ---------

#include "library.hpp"

namespace library
{
  template<typename T>
  T core_function (T val)
  {
    return val * 2;
  }

  template<typename T>
  thread_local
  typename Foo<T>::Func
  Foo<T>::f = &core_function<T>;

  template struct Foo<char>;
  template struct Foo<int>;
  template struct Foo<long>;
}

--------------------------------
----------- main.cpp -----------

#include "library.hpp"

int main () {
  return library::Foo<int>::f(2);
}

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

* [Bug c++/96853] Explicit template instantiation & thread_local interaction
  2020-08-30 18:45 [Bug c++/96853] New: Explicit template instantiation & thread_local interaction tobias.bruell at gmail dot com
@ 2024-06-23 20:06 ` nsoft.sagaonline at gmail dot com
  0 siblings, 0 replies; 2+ messages in thread
From: nsoft.sagaonline at gmail dot com @ 2024-06-23 20:06 UTC (permalink / raw)
  To: gcc-bugs

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

NIKEA-SOFT <nsoft.sagaonline at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nsoft.sagaonline at gmail dot com

--- Comment #1 from NIKEA-SOFT <nsoft.sagaonline at gmail dot com> ---
I confirm the presence of this problem since version GCC 5.3.
I came across it just recently when I was working with my library.
The situation is as follows: starting from version 5.3, TLS init is not called
in template global variables marked as thread_local.
Here is the MRE, pay attention to lines 31 and 33, we see the absence of the
TLS Init call since version 5.3 of GCC.
https://godbolt.org/z/a5orhhTjT

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

end of thread, other threads:[~2024-06-23 20:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-30 18:45 [Bug c++/96853] New: Explicit template instantiation & thread_local interaction tobias.bruell at gmail dot com
2024-06-23 20:06 ` [Bug c++/96853] " nsoft.sagaonline at gmail dot com

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).