<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - thread_local accessor function for 'primitive' variable in anonymous namespace has wrong linkage"
   href="http://llvm.org/bugs/show_bug.cgi?id=19655">19655</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>thread_local accessor function for 'primitive' variable in anonymous namespace has wrong linkage
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.4
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++11
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>michael@ensslin.cc
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>For thread-local variables, clang++ auto-generates a wrapper that will
initialize the memory on first access. In contrast to g++, this also happens
for primitive types, e.g. ints or simple structs without constructors. For
those primitive types, the wrapper is globally linked in clang++, with
-std=c++11

Minimal example:

mic@mic-nb /tmp/repr4 $ cat a.cpp
namespace {

thread_local int i {1};

}

void f() {
    i = 2;
}
mic@mic-nb /tmp/repr4 $ cat b.cpp
namespace {

thread_local int i {0};

}

void f();

int main() {
    f();
    return i;
}
mic@mic-nb /tmp/repr4 $ g++ --version
g++ (GCC) 4.9.0
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

mic@mic-nb /tmp/repr4 $ clang++ --version
clang version 3.4 (tags/RELEASE_34/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
mic@mic-nb /tmp/repr4 $ g++ -std=c++11 a.cpp b.cpp; ./a.out; echo $?
0
mic@mic-nb /tmp/repr4 $ clang++ -std=c++11 a.cpp b.cpp; ./a.out; echo $?
2

As you can see, f() in a.o influences the variable i from b.o.

mic@mic-nb /tmp/repr4 $ nm -C a.o
                 U _GLOBAL_OFFSET_TABLE_
0000000000000000 T f()
0000000000000000 d (anonymous namespace)::i
0000000000000000 W TLS wrapper function for (anonymous namespace)::i
mic@mic-nb /tmp/repr4 $ nm -C b.o
                 U _GLOBAL_OFFSET_TABLE_
0000000000000000 T main
                 U f()
0000000000000000 b (anonymous namespace)::i
0000000000000000 W TLS wrapper function for (anonymous namespace)::i
mic@mic-nb /tmp/repr4 $ nm -C a.out
(...)
00000000004005b0 T f()
0000000000000000 d (anonymous namespace)::i
0000000000000004 b (anonymous namespace)::i
00000000004005d0 t TLS wrapper function for (anonymous namespace)::i

As you can see, both variables are linked into the finaly binary, but one of
the TLS wrappers is scrapped.

If a more complex type is used instead of int, the error does not occur.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>