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

            Bug ID: 84497
           Summary: link errors with trivial external thread_local
                    variables
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: P at draigBrady dot com
  Target Milestone: ---

In some cases extern thread_local vars will generate a reference
to a tls_init function, when there is none generated for the translation unit
defining the variable, essentially due to it being std::is_trivial.

This used to work with gcc-5.3.1 and gcc-6.3.1,
the reason being that they were less efficient and
always exported the tls_init function, even for these std::is_trivial vars.

With gcc-7 the tls_init function is not emitted for these types,
but there is a mismatch in the logic determining if the extern thread_local
should call the tls_init function.

Specifically, in the module local to the var
if DECL_NONTRIVIALLY_INITIALIZED_P is false
then the tls_init function is not generated.
However in a translation unit that references such
an extern tls variable and TYPE_NEEDS_CONSTRUCTING is true
then a non weak call to the tls_init function is generated,
resulting in a link error.

$ cat a.cpp
#include <atomic>
struct Test { std::atomic<int> x; };

thread_local Test t;

$ cap main.cpp
#include <atomic>
struct Test { std::atomic<int> x; };

extern thread_local Test t;
int main() {
  return t.x.load();
}

$ g++ -std=c++11 a.cpp main.cpp
/tmp/ccvPDpE2.o: In function `TLS wrapper function for t':
main.cpp:(.text._ZTW1t[_ZTW1t]+0x5): undefined reference to `TLS init function
for t'
collect2: error: ld returned 1 exit status

Reply via email to