[lld] r327542 - Error instead of producing broken binary.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 14 11:08:33 PDT 2018


Author: rafael
Date: Wed Mar 14 11:08:33 2018
New Revision: 327542

URL: http://llvm.org/viewvc/llvm-project?rev=327542&view=rev
Log:
Error instead of producing broken binary.

This "fixes" PR36678 by just producing an error when we find a case
where we would produce an plt entry that used ebx but ebx would not be
set.

Added:
    lld/trunk/test/ELF/Inputs/i386-pic-plt.s
    lld/trunk/test/ELF/i386-pic-plt.s
Modified:
    lld/trunk/ELF/Relocations.cpp

Modified: lld/trunk/ELF/Relocations.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Relocations.cpp?rev=327542&r1=327541&r2=327542&view=diff
==============================================================================
--- lld/trunk/ELF/Relocations.cpp (original)
+++ lld/trunk/ELF/Relocations.cpp Wed Mar 14 11:08:33 2018
@@ -875,6 +875,17 @@ static RelExpr processRelocAux(InputSect
     // that points to the real function is a dedicated got entry used by the
     // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
     // R_386_JMP_SLOT, etc).
+
+    // For position independent executable on i386, the plt entry requires ebx
+    // to be set. This causes two problems:
+    // * If some code has a direct reference to a function, it was probably
+    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
+    // * If a library definition gets preempted to the executable, it will have
+    //   the wrong ebx value.
+    if (Config->Pie && Config->EMachine == EM_386)
+      errorOrWarn("symbol '" + toString(Sym) +
+                  "' cannot be preempted; recompile with -fPIE" +
+                  getLocation(Sec, Sym, Offset));
     Sym.NeedsPltAddr = true;
     Expr = toPlt(Expr);
     Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});

Added: lld/trunk/test/ELF/Inputs/i386-pic-plt.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/i386-pic-plt.s?rev=327542&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/i386-pic-plt.s (added)
+++ lld/trunk/test/ELF/Inputs/i386-pic-plt.s Wed Mar 14 11:08:33 2018
@@ -0,0 +1,4 @@
+        .global foo
+        .type foo, @function
+foo:
+        nop

Added: lld/trunk/test/ELF/i386-pic-plt.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/i386-pic-plt.s?rev=327542&view=auto
==============================================================================
--- lld/trunk/test/ELF/i386-pic-plt.s (added)
+++ lld/trunk/test/ELF/i386-pic-plt.s Wed Mar 14 11:08:33 2018
@@ -0,0 +1,12 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=i386-pc-linux %s -o %t.o
+// RUN: llvm-mc -filetype=obj -triple=i386-pc-linux %p/Inputs/i386-pic-plt.s -o %t2.o
+// RUN: ld.lld -shared %t2.o -o %t2.so
+// RUN: ld.lld %t.o %t2.so -o %t
+// RUN: not ld.lld %t.o %t2.so -o %t -pie 2>&1 | FileCheck %s
+
+// CHECK: error: symbol 'foo' cannot be preempted; recompile with -fPIE
+
+.global _start
+_start:
+  call foo




More information about the llvm-commits mailing list