[lld] r266161 - ELF: Do not create relative relocations for undefined symbols.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 12 19:24:49 PDT 2016


Author: pcc
Date: Tue Apr 12 21:24:48 2016
New Revision: 266161

URL: http://llvm.org/viewvc/llvm-project?rev=266161&view=rev
Log:
ELF: Do not create relative relocations for undefined symbols.

We need to ensure that the address of an undefined weak symbol evaluates to
zero. We were getting this right for non-PIC executables (where the symbol
can be evaluated directly) and for DSOs (where we emit a symbolic relocation
for these symbols, as they are preemptible). But we weren't getting it right
for PIEs. Probably the simplest way to ensure that these symbols evaluate
to zero is by not creating a relocation in .got for them.

Differential Revision: http://reviews.llvm.org/D19044

Added:
    lld/trunk/test/ELF/pie-weak.s
Modified:
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=266161&r1=266160&r2=266161&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Apr 12 21:24:48 2016
@@ -573,6 +573,10 @@ void Writer<ELFT>::scanRelocs(InputSecti
           DynType = Target->TlsGotRel;
         else if (Preemptible)
           DynType = Target->GotRel;
+        else if (Body.isUndefined())
+          // Weak undefined symbols evaluate to zero, so don't create
+          // relocations for them.
+          continue;
         else
           DynType = Target->RelativeRel;
         AddDyn({DynType, Out<ELFT>::Got, Body.getGotOffset<ELFT>(),

Added: lld/trunk/test/ELF/pie-weak.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/pie-weak.s?rev=266161&view=auto
==============================================================================
--- lld/trunk/test/ELF/pie-weak.s (added)
+++ lld/trunk/test/ELF/pie-weak.s Tue Apr 12 21:24:48 2016
@@ -0,0 +1,16 @@
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: ld.lld -pie %t.o -o %t
+# RUN: llvm-readobj -r %t | FileCheck --check-prefix=RELOCS %s
+# RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASM %s
+
+# RELOCS:      Relocations [
+# RELOCS-NEXT: ]
+
+.weak foo
+
+.globl _start
+_start:
+# DISASM: _start:
+# DISASM-NEXT: 1000: 48 8b 05 69 10 00 00 movq 4201(%rip), %rax
+#                                              ^ .got - (.text + 7)
+mov foo at gotpcrel(%rip), %rax




More information about the llvm-commits mailing list