[lld] r268688 - Don't produce a relocation to read only memory.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu May 5 14:19:38 PDT 2016


Author: rafael
Date: Thu May  5 16:19:38 2016
New Revision: 268688

URL: http://llvm.org/viewvc/llvm-project?rev=268688&view=rev
Log:
Don't produce a relocation to read only memory.

This is hopefully last case where we would produce a relocation to a
read only section.

Added:
    lld/trunk/test/ELF/copy-rel-pie-error.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=268688&r1=268687&r2=268688&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu May  5 16:19:38 2016
@@ -439,6 +439,12 @@ static bool needsPlt(RelExpr Expr) {
   return Expr == R_PLT_PC || Expr == R_PPC_PLT_OPD || Expr == R_PLT;
 }
 
+// True if this expression is of the form Sym - X, where X is a position in the
+// file (PC, or GOT for example).
+static bool isRelExpr(RelExpr Expr) {
+  return Expr == R_PC || Expr == R_GOTREL || Expr == R_PAGE_PC;
+}
+
 template <class ELFT>
 static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type,
                                      const SymbolBody &Body) {
@@ -460,7 +466,7 @@ static bool isStaticLinkTimeConstant(Rel
     return true;
 
   bool AbsVal = isAbsolute<ELFT>(Body) || Body.isTls();
-  bool RelE = E == R_PC || E == R_GOTREL || E == R_PAGE_PC;
+  bool RelE = isRelExpr(E);
   if (AbsVal && !RelE)
     return true;
   if (!AbsVal && RelE)
@@ -520,7 +526,7 @@ RelExpr Writer<ELFT>::adjustExpr(SymbolB
   // This relocation would require the dynamic linker to write a value to read
   // only memory. We can hack around it if we are producing an executable and
   // the refered symbol can be preemepted to refer to the executable.
-  if (Config->Shared) {
+  if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) {
     StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
     error("relocation " + S + " cannot be used when making a shared "
                               "object; recompile with -fPIC.");

Added: lld/trunk/test/ELF/copy-rel-pie-error.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/copy-rel-pie-error.s?rev=268688&view=auto
==============================================================================
--- lld/trunk/test/ELF/copy-rel-pie-error.s (added)
+++ lld/trunk/test/ELF/copy-rel-pie-error.s Thu May  5 16:19:38 2016
@@ -0,0 +1,12 @@
+// RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
+// RUN: llvm-mc %p/Inputs/copy-rel-pie.s -o %t2.o -filetype=obj -triple=x86_64-pc-linux
+// RUN: ld.lld %t2.o -o %t2.so -shared
+// RUN: not ld.lld %t.o %t2.so -o %t.exe -pie 2>&1 | FileCheck %s
+
+// CHECK: relocation R_X86_64_64 cannot be used when making a shared object; recompile with -fPIC.
+// CHECK: relocation R_X86_64_64 cannot be used when making a shared object; recompile with -fPIC.
+
+.global _start
+_start:
+        .quad bar
+        .quad foo




More information about the llvm-commits mailing list