[PATCH] D19044: ELF: Do not create relative relocations for undefined symbols.

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


pcc created this revision.
pcc added reviewers: ruiu, rafael.
pcc added a subscriber: llvm-commits.

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.

http://reviews.llvm.org/D19044

Files:
  ELF/Writer.cpp
  test/ELF/pie-weak.s

Index: test/ELF/pie-weak.s
===================================================================
--- /dev/null
+++ test/ELF/pie-weak.s
@@ -0,0 +1,12 @@
+# 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 %s
+
+# CHECK:      Relocations [
+# CHECK-NEXT: ]
+
+.weak foo
+
+.globl _start
+_start:
+mov foo at gotpcrel(%rip), %rax
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -441,6 +441,10 @@
           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;
         Out<ELFT>::RelaDyn->addReloc({DynType, Out<ELFT>::Got,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19044.53506.patch
Type: text/x-patch
Size: 953 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160413/bc4e403f/attachment.bin>


More information about the llvm-commits mailing list