<html>
    <head>
      <base href="https://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 --- - Wrong relocation emitted using lld -r"
   href="https://llvm.org/bugs/show_bug.cgi?id=28779">28779</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Wrong relocation emitted using lld -r
          </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>rink@rink.nu
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=16842" name="attach_16842" title="Example code to trigger the bug (make LD=clang-lld)">attachment 16842</a> <a href="attachment.cgi?id=16842&action=edit" title="Example code to trigger the bug (make LD=clang-lld)">[details]</a></span>
Example code to trigger the bug (make LD=clang-lld)

When working on crt1 code for my own OS Ananas, I noticed the following (refer
to the attached archive): when you use 'lld -r' to combine object files, the
wrong offset is used. The attached example contains:

start.S: startup code, calls func() and exits
func.c: contains func() which initializes some value on stack and increments it
Makefile: compiles start.S -> start.o, func.c -> func.o, combines them to
'combined.o' and build 't' using 'combined.o'

Using LLD, the resulting program 't' crashes. This is because the offset of
'func' is wrong, as you can see when using objdump -d:

Disassembly of section .text:

0000000000011000 <_start>:
   11000:    e8 11 00 00 00           callq  11016 <func+0x4>  <-- WRONG
   11005:    48 c7 c0 3c 00 00 00     mov    $0x3c,%rax
   1100c:    48 31 ff                 xor    %rdi,%rdi
   1100f:    0f 05                    syscall 
   11011:    c3                       retq   

0000000000011012 <func>:
   11012:    55                       push   %rbp
   11013:    48 89 e5                 mov    %rsp,%rbp
   11016:    c7 45 fc 00 00 00 00     movl   $0x0,-0x4(%rbp)
   1101d:    83 45 fc 01              addl   $0x1,-0x4(%rbp)
   11021:    90                       nop
   11022:    5d                       pop    %rbp
   11023:    c3                       retq   

Where GNU LD yields:

Disassembly of section .text:

00000000004000b0 <_start>:
  4000b0:    e8 0d 00 00 00           callq  4000c2 <func>
  4000b5:    48 c7 c0 3c 00 00 00     mov    $0x3c,%rax
  4000bc:    48 31 ff                 xor    %rdi,%rdi
  4000bf:    0f 05                    syscall 
  4000c1:    c3                       retq   

00000000004000c2 <func>:
  4000c2:    55                       push   %rbp
  4000c3:    48 89 e5                 mov    %rsp,%rbp
  4000c6:    c7 45 fc 00 00 00 00     movl   $0x0,-0x4(%rbp)
  4000cd:    83 45 fc 01              addl   $0x1,-0x4(%rbp)
  4000d1:    90                       nop
  4000d2:    5d                       pop    %rbp
  4000d3:    c3                       retq   

Note: I understand this is a silly example, but in my actual case combined.o
calls 'main' so I need to use -r because I want to build crt1.o.</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>