<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 - link error when overriding common symbols with weak symbols in library"
   href="https://bugs.llvm.org/show_bug.cgi?id=51082">51082</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>link error when overriding common symbols with weak symbols in library
          </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>yabinc@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, smithp352@googlemail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>If I define a common symbol in main object file, and define a weak symbol with
the same name in a library. lld may not be able to override the common symbol
correctly with the weak symbol, and report link error.

Below is an example:

main.c:

#include <stdio.h>

extern int A;
const char* B;

int main() {
  printf("A = %d, B = %s\n", A, B);
}

lib.c:

int A;
const char* B __attribute__((weak));

build.sh:

CLANG_DIR=/ssd/opensource/llvm/llvm-build/install/bin

$CLANG_DIR/clang -c -o main.o main.c -fPIE -fcommon
$CLANG_DIR/clang -c -o lib.o lib.c -fPIC
$CLANG_DIR/llvm-ar crsPD -format=gnu lib.a lib.o
$CLANG_DIR/clang main.o lib.a -pie -fuse-ld=lld -v

link error:

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol B;
recompile with -fPIC
<span class="quote">>>> defined in lib.a
>>> referenced by main.c
>>>               main.o:(main)</span >


The cause of the error seems to be below:

1. lld parses main.o, and adds undefined symbol A and common symbol B.
2. lld parses lib.a, reads LazyArchive symbol A, and decides to read lib.o
   to fetch symbol A. By the way, it reads weak symbol B in lib.o, but
   decide not to override common symbol B.
3. lld parses lib.a, reads lazyArchive symbol B, and decides to override
   common symbol B. So it replace common symbol B with lazyArchive symbol B.
   But not further read weak symbol B, because lib.o has been read before.
4. As a result, we have a LazyArchive symbol B, which can't do relocation.</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>