<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 - lld-link x86-64: undefined weak symbol does not resolve to 0"
   href="https://bugs.llvm.org/show_bug.cgi?id=37331">37331</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>lld-link x86-64: undefined weak symbol does not resolve to 0
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>lld
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>COFF
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>sheu+bugs.llvm.org@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Instead of resolving to 0, an undefined weak symbol resolves to (in this case)
0x100000000 (that's right, 33 bits, not a typo).

This only occurs when linking for x86-64; the symbol resolves correctly to 0x0
for x86.

Test case source:

void foo() __attribute__((weak));

int main(int argc, char** argv) {
  if (foo) {
    return 2;
  }
  return 1;
}


Build invocation, 64-bit:

./clang-cl -m64 -fuse-ld=lld-link /Zl noop.c -o noop_64.exe /link /entry:main


Disassembly of binary (with objdump -d):

noop_64.exe:     file format pei-x86-64


Disassembly of section .text:

0000000140001000 <.text>:
   140001000:   48 83 ec 18             sub    $0x18,%rsp
   140001004:   c7 44 24 14 00 00 00    movl   $0x0,0x14(%rsp)
   14000100b:   00 
   14000100c:   48 89 54 24 08          mov    %rdx,0x8(%rsp)
   140001011:   89 4c 24 04             mov    %ecx,0x4(%rsp)
   140001015:   48 8d 15 e4 ef ff bf    lea    -0x4000101c(%rip),%rdx        #
0x100000000
   14000101c:   48 85 d2                test   %rdx,%rdx
   14000101f:   0f 84 12 00 00 00       je     0x140001037
   140001025:   e9 00 00 00 00          jmpq   0x14000102a
   14000102a:   c7 44 24 14 02 00 00    movl   $0x2,0x14(%rsp)
   140001031:   00 
   140001032:   e9 08 00 00 00          jmpq   0x14000103f
   140001037:   c7 44 24 14 01 00 00    movl   $0x1,0x14(%rsp)
   14000103e:   00 
   14000103f:   8b 44 24 14             mov    0x14(%rsp),%eax
   140001043:   48 83 c4 18             add    $0x18,%rsp
   140001047:   c3                      retq


Execution result on Windows:

C:\Users\sheu>noop_64.exe

C:\Users\sheu>echo %ERRORLEVEL%
2


The executable builds and runs correctly (returns '1') when building for
32-bit.</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>