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

            Bug ID: 102496
           Summary: [11 regression] extern __thread declaration in
                    function scope produces a non-TLS reference
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jlegg at feralinteractive dot com
  Target Milestone: ---

A variable declaration inside a function body qualified with extern and
__thread appears to ignore the __thread qualification when compiling c++.

For example:
Contents of test1.cc:
        extern __thread int thing;
        __thread int thing = 0;

Contents of test2.cc:
        int main()
        {
                extern __thread int thing;
                return thing;
        }

When invoking g++ 11.2.0 with arguments test1.cc test2.cc, I get the following
output:
        /usr/local/bin/ld: thing: TLS definition in /tmp/ccXxZiSY.o section
.tbss mismatches non-TLS reference in /tmp/ccYXyoSR.o
        /usr/local/bin/ld: /tmp/ccYXyoSR.o: error adding symbols: bad value
        collect2: error: ld returned 1 exit status

If the example code was instead in files with a .c extension and compiled with
gcc (as opposed to g++) 11.2.0, it does not produce an error. I also could not
reproduce the error with g++ 10.2.0.

The options I used when configuring GCC are: --prefix=/usr
--program-suffix=-11.2 --libexecdir=/usr/lib
--with-gxx-include-dir=/usr/include/c++/11.2 --libdir=/usr/lib
--with-tune=generic --enable-checking=release --disable-multilib
--enable-languages=c,c++ --build=x86_64-linux-gnu --target=x86_64-linux-gnu
--enable-linker-build-id --enable-version-specific-runtime-libs

It is possible to work around the issue by moving or copying the variable
declaration to namespace scope.

Reply via email to