<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 handling of relocations to unresolved weak references with -pie not consistent"
   href="https://bugs.llvm.org/show_bug.cgi?id=50759">50759</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>LLD handling of relocations to unresolved weak references with -pie not consistent
          </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>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </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>smithp352@googlemail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, smithp352@googlemail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When we do a -pie link with no shared libraries present LLD can be inconsistent
with its resolution of unresolved weak-references.

Consider the example below (I'm using aarch64 but it should reproduce on other
targets):
// weak.c
extern __attribute__((weak)) int weak_reference;
__attribute__((visibility("hidden"))) int* address_of_weak_reference =
&weak_reference;

void _start() {
  if (&weak_reference)
    weak_reference = 1;
  if (address_of_weak_reference)
    *address_of_weak_reference = 1;
}

clang -c weak.c -fpie --target=aarch64-linux-gnu -ffreestanding -nostdlib
ld.lld -pie weak.o -o weak-dyn
readelf --relocs weak-dyn
Relocation section '.rela.dyn' at offset 0x278 contains 1 entries:
    Offset             Info             Type               Symbol's Value 
Symbol's Name + Addend
0000000000020390  0000000100000401 R_AARCH64_GLOB_DAT     0000000000000000
weak_reference + 0

In the object file there are GOT generating relocations for the references to
weak_reference and a R_AARCH64_ABS64 relocation from the .data section. LLD is
relocating the GOT reference to weak_reference but is statically resolving the
R_AARCH64_ABS64 to the undefined weak_reference to 0

ld.bfd does what I'd expect for this case as -pie is expected to be used with a
dynamic linker:
Relocation section '.rela.dyn' at offset 0x278 contains 2 entries:
    Offset             Info             Type               Symbol's Value 
Symbol's Name + Addend
0000000000010fe0  0000000300000401 R_AARCH64_GLOB_DAT     0000000000000000
weak_reference + 0
0000000000011000  0000000300000101 R_AARCH64_ABS64        0000000000000000
weak_reference + 0

A second part to this is motivated by the linux kernel KASLR configuration that
uses a combination of -fpie and linking -pie with only static libraries and
with a linker script including something like:
# weak.lds
/DISCARD/ : {
                *(.interp .dynamic)
                *(.dynsym .dynstr .hash .gnu.hash)
        }

If we use LLD with this linker script extract the dynamic symbol table will be
removed, leaving the R_AARCH64_GLOB_DAT with a symbol index of 0. This used to
give a warning, but no longer does so.

ld.lld -pie weak.o -o weak --script=weak.lds

Relocation section '.rela.dyn' at offset 0x10000 contains 1 entries:
    Offset             Info             Type               Symbol's Value 
Symbol's Name + Addend
0000000000000058  0000000000000401 R_AARCH64_GLOB_DAT                0

In this case I think there is a strong argument to statically resolve all
dynamic relocations to unresolved weak references to 0 as a R_AARCH64_GLOB_DAT
to symbol idx 0 is arguably ill-formed.

Perhaps we can even error if there are any dynamic relocations expecting a
symbol when the .dynsym is discarded.</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>