<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - [ItaniumCXXABI] TLS variable access wrapper optimization causes initializer function alias to be undefined (0)"
   href="https://bugs.llvm.org/show_bug.cgi?id=48030">48030</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[ItaniumCXXABI] TLS variable access wrapper optimization causes initializer function alias to be undefined (0)
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>release blocker
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>Wolfgang_Pieb@playstation.sony.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=24116" name="attach_24116" title="Test case">attachment 24116</a> <a href="attachment.cgi?id=24116&action=edit" title="Test case">[details]</a></span>
Test case

The following commit seems to have caused a serious issue wrt
the interaction of global initializers and the wrapper functions to
access thread-local variables:

commit 00223827a952f66e7426c9881a2a4229e59bb019
Author: Richard Smith <<a href="mailto:richard-llvm@metafoo.co.uk">richard-llvm@metafoo.co.uk</a>>
Date:   Thu Sep 12 20:00:24 2019 +0000

    Improve code generation for thread_local variables:

    Summary:
     * Don't bother using a thread wrapper when the variable is known to
       have constant initialization.
     * Emit the thread wrapper as discardable-if-unused in TUs that don't
       contain a definition of the thread_local variable.
     * Don't emit the thread wrapper at all if the thread_local variable
       is unused and discardable; it will be emitted by all TUs that need
       it.

    Reviewers: rjmccall, jdoerfert

    Subscribers: cfe-commits

    Tags: #clang

    Differential Revision: <a href="https://reviews.llvm.org/D67429">https://reviews.llvm.org/D67429</a>

    llvm-svn: 371767

Given the code attached (in "a.h"), the compiler does not generate
a thread-local access wrapper function for the static member
TLS<S>::mData. It also does not generate a definition
for the variable's initializer function's alias. However, it still emits
the initializer function itself as part of a comdat group that encompasses
all sections needed for its initialization.

Given a second translation unit that does have a definition for the wrapper
function as well as a comdat group to contain the variable's initializer,
it is possible that the linker then picks the comdat group from the TU that
does not have the definition for the (weak) initializer function alias. In 
this case the call to the initializer function in the wrapper function can get
fixed up to call location 0, leading to a crash.

The attached source demonstrates the problem on Linux. The test either 
runs or crashes depending on the order of the files on the link line:

sh -x compile_and_run
+ CLANG=/home/test/build/llvm/git/mono-repo/llvm-RelWithDebInfo/bin/clang
+ /home/test/build/llvm/git/mono-repo/llvm-RelWithDebInfo/bin/clang -I.
main.cpp a.cpp -o test1
+ /home/test/build/llvm/git/mono-repo/llvm-RelWithDebInfo/bin/clang -I. a.cpp
main.cpp -o test2
+ ./test1
entering main
returning
+ ./test2
entering main
Segmentation fault (core dumped)</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>