<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 --- - Missing relocations when linking i686 with -fpie"
   href="https://llvm.org/bugs/show_bug.cgi?id=28924">28924</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missing relocations when linking i686 with -fpie
          </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>All
          </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>llvm@sjor.sg
          </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>Compiling for i686-unknown-linux-gnu with -fpie, it looks like lld is missing
some relocations, or applying them incorrectly, causing the use of external
global variables to fail. I boiled a bug down to this, but am not sure if I can
go further, as this is starting to go well outside my expertise.

This is using LLVM r278216, Clang r278213, LLD r278205.

Thanks for investigating!

-------8<-------

$ cat header.h
extern int (*global_function)();

static inline int do_global_call() {
    return global_function();
}
$ cat unit1.c
#include "header.h"
#include <stdio.h>

int (*global_function)();

int global_implementation() {
    return 1234;
}

void cu2_call();

int main() {
    global_function = global_implementation;
    printf("Global call value: %d\n", do_global_call());
    cu2_call();
}
$ cat unit2.c
#include "header.h"
#include <stdio.h>

void cu2_call() {
    printf("Compilation unit 2 global call value: %d\n", do_global_call());
}
$ i686-unknown-linux-gnu-clang -Wall -fpie unit1.c -c -o unit1.o
$ i686-unknown-linux-gnu-clang -Wall -fpie unit2.c -c -o unit2.o
$ i686-unknown-linux-gnu-clang -fpie -fuse-ld=lld
-Wl,--allow-multiple-definition unit1.o unit2.o -o testbin.lld
duplicate symbol: __x86.get_pc_thunk.bx in
/usr/lib/gcc/i586-linux-gnu/4.9/../../../i386-linux-gnu/crti.o and
/usr/lib/i386-linux-gnu/libc_nonshared.a(elf-init.oS)
$ i686-unknown-linux-gnu-clang -fpie unit1.o unit2.o -o testbin.gld
$ ./testbin.lld
Global call value: 1234
[1]    21726 segmentation fault  ./testbin.lld
$ ./testbin.gld
Global call value: 1234
Compilation unit 2 global call value: 1234

-------8<------

$ readelf -r testbin.lld

Relocation section '.rel.plt' at offset 0x36c contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00013014  00000307 R_386_JUMP_SLOT   000112c0   __libc_start_main
00013018  00000207 R_386_JUMP_SLOT   00000000   printf

$ readelf -r testbin.gld

Relocation section '.rel.dyn' at offset 0x278 contains 1 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
08049784  00000206 R_386_GLOB_DAT    00000000   __gmon_start__

Relocation section '.rel.plt' at offset 0x280 contains 3 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
08049794  00000107 R_386_JUMP_SLOT   00000000   printf
08049798  00000207 R_386_JUMP_SLOT   00000000   __gmon_start__
0804979c  00000307 R_386_JUMP_SLOT   00000000   __libc_start_main</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>