<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 generates strange and incorrect thunk for aarch64 weak function call"
   href="https://bugs.llvm.org/show_bug.cgi?id=44451">44451</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>lld generates strange and incorrect thunk for aarch64 weak function call
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>ELF
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>john.brawn@arm.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, peter.smith@linaro.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>If you have:

example.c
  extern void weakfn() __attribute__((weak));
  int call_weak() {
    weakfn();
    return 0;
  }

script.ld
  SECTIONS
  {
    .text 0x10000000 : { *(.text) }
  }

and do:
  clang --target=aarch64-none-eabi example.c -c -O1
  ld.lld -T script.ld example.o
  llvm-objdump --disassemble a.out

the llvm-objdump disassembly is:

0000000010000000 call_weak:
10000000: fd 7b bf a9                   stp     x29, x30, [sp, #-16]!
10000004: fd 03 00 91                   mov     x29, sp
10000008: 04 00 00 94                   bl      #16
<__AArch64AbsLongThunk_weakfn>
1000000c: e0 03 1f 2a                   mov     w0, wzr
10000010: fd 7b c1 a8                   ldp     x29, x30, [sp], #16
10000014: c0 03 5f d6                   ret

0000000010000018 __AArch64AbsLongThunk_weakfn:
10000018: 50 00 00 58                   ldr     x16, #8
1000001c: 00 02 1f d6                   br      x16

0000000010000020 $d:
10000020:       00 00 00 00     .word   0x00000000
10000024:       00 00 00 00     .word   0x00000000

The weak function call has been converted into a branch to a thunk which
branches to address zero, which is both strange and wrong.

Reducing the address of .text in the linker script to 0x1000000 gives something
more like what I'd expect, i.e. a branch to the next instruction (though a nop
would be better):

0000000001000000 call_weak:
 1000000: fd 7b bf a9                   stp     x29, x30, [sp, #-16]!
 1000004: fd 03 00 91                   mov     x29, sp
 1000008: 01 00 00 94                   bl      #4 <call_weak+0xc>
 100000c: e0 03 1f 2a                   mov     w0, wzr
 1000010: fd 7b c1 a8                   ldp     x29, x30, [sp], #16
 1000014: c0 03 5f d6                   ret</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>