<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 --- - lld incorrectly resolves function pointer relocation in dso"
   href="https://llvm.org/bugs/show_bug.cgi?id=26818">26818</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>lld incorrectly resolves function pointer relocation in dso
          </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>FreeBSD
          </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>All Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>emaste@freebsd.org
          </td>
        </tr>

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

        <tr>
          <th>Blocks</th>
          <td>23214
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider this test program:

% cat lib.c
#include <stdio.h>

int(*fp)(const char *, ...) = printf;

void
fn(void)
{
        fp("hello %s\n", "world");
}

% cat main.c
void fn(void);

int
main(int argc, char *argv[])
{
        fn();
}

Linking with ld.bfd:
% cc -fPIC -shared -o lib.so lib.c
% cc -fPIC -Wl,-rpath=. lib.so main.c
% ./a.out
hello world

And there's a relocation for 'fp' for printf:

% readelf -s lib.so | grep fp
     7: 0000000000200808     8 OBJECT  GLOBAL DEFAULT   22 fp
    47: 0000000000200808     8 OBJECT  GLOBAL DEFAULT   22 fp

% readelf -r lib.so | grep 200808                 
0000002007c8  000700000006 R_X86_64_GLOB_DAT 0000000000200808 fp + 0
000000200808  000400000001 R_X86_64_64       0000000000000000 printf + 0

Linking with ld.lld:
% cc -fuse-ld=lld -fPIC -shared -o lib.so lib.c
% cc -fuse-ld=lld -fPIC -Wl,-rpath=. lib.so main.c
% ./a.out
zsh: segmentation fault (core dumped)  ./a.out

And no relocation for the function pointer:

% readelf -s lib.so | grep fp
     6: 0000000000003018     8 OBJECT  GLOBAL DEFAULT   19 fp
    21: 0000000000003018     8 OBJECT  GLOBAL DEFAULT   19 fp

% readelf -r lib.so            

Relocation section '.rela.dyn' at offset 0x438 contains 5 entries:
  Offset          Info           Type           Sym. Value    Sym. Name +
Addend
000000002138  000300000006 R_X86_64_GLOB_DAT 0000000000000000 __cxa_finalize +
0
000000002140  000200000006 R_X86_64_GLOB_DAT 0000000000000000
_Jv_RegisterClasses + 0
000000003008  000000000008 R_X86_64_RELATIVE                   
0000000000003008
000000003010  000000000008 R_X86_64_RELATIVE                   
0000000000002018
000000002148  000600000006 R_X86_64_GLOB_DAT 0000000000003018 fp + 0

Relocation section '.rela.plt' at offset 0x4b0 contains 2 entries:
  Offset          Info           Type           Sym. Value    Sym. Name +
Addend
000000003038  000300000007 R_X86_64_JUMP_SLO 0000000000000000 __cxa_finalize +
0
000000003040  000700000007 R_X86_64_JUMP_SLO 0000000000001150 printf + 0</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>