[lld] r321507 - Allow copy relocation with -z notext.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 27 16:23:49 PST 2017


Author: rafael
Date: Wed Dec 27 16:23:49 2017
New Revision: 321507

URL: http://llvm.org/viewvc/llvm-project?rev=321507&view=rev
Log:
Allow copy relocation with -z notext.

This makes adjustExpr a bit simpler too IMHO.

It seems that some of the complication around relocation processing
is that we are trying to create copy relocations too early. It seems
we could handle a few simple cases first and continue.

Added:
    lld/trunk/test/ELF/Inputs/znotext-copy-relocations.s
    lld/trunk/test/ELF/znotext-copy-relocation.s
Modified:
    lld/trunk/ELF/Relocations.cpp
    lld/trunk/test/ELF/x86-64-dyn-rel-error.s
    lld/trunk/test/ELF/x86-64-dyn-rel-error2.s

Modified: lld/trunk/ELF/Relocations.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Relocations.cpp?rev=321507&r1=321506&r2=321507&view=diff
==============================================================================
--- lld/trunk/ELF/Relocations.cpp (original)
+++ lld/trunk/ELF/Relocations.cpp Wed Dec 27 16:23:49 2017
@@ -581,6 +581,8 @@ static RelExpr getPltExpr(Symbol &Sym, R
   return toPlt(Expr);
 }
 
+// This modifies the expression if we can use a copy relocation or point the
+// symbol to the PLT.
 template <class ELFT>
 static RelExpr adjustExpr(Symbol &Sym, RelExpr Expr, RelType Type,
                           InputSectionBase &S, uint64_t RelOff,
@@ -608,13 +610,9 @@ static RelExpr adjustExpr(Symbol &Sym, R
   // linker to write a value to read only memory or use an unsupported
   // relocation.
 
-  // FIXME: This is a hack to avoid changing error messages for now.
-  if (CanWrite && !Sym.isFunc())
-    return Expr;
-
   // 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 || (Config->Pic && !isRelExpr(Expr))) {
+  if (!CanWrite && (Config->Shared || (Config->Pic && !isRelExpr(Expr)))) {
     error(
         "can't create dynamic relocation " + toString(Type) + " against " +
         (Sym.getName().empty() ? "local symbol" : "symbol: " + toString(Sym)) +
@@ -623,6 +621,11 @@ static RelExpr adjustExpr(Symbol &Sym, R
     return Expr;
   }
 
+  // Copy relocations are only possible if we are creating an executable and the
+  // symbol is shared.
+  if (!Sym.isShared() || Config->Shared)
+    return Expr;
+
   if (Sym.getVisibility() != STV_DEFAULT) {
     error("cannot preempt symbol: " + toString(Sym) +
           getLocation(S, Sym, RelOff));

Added: lld/trunk/test/ELF/Inputs/znotext-copy-relocations.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/znotext-copy-relocations.s?rev=321507&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/znotext-copy-relocations.s (added)
+++ lld/trunk/test/ELF/Inputs/znotext-copy-relocations.s Wed Dec 27 16:23:49 2017
@@ -0,0 +1,5 @@
+.global foo
+.type foo, at object
+.size foo, 8
+foo:
+.quad 42

Modified: lld/trunk/test/ELF/x86-64-dyn-rel-error.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/x86-64-dyn-rel-error.s?rev=321507&r1=321506&r2=321507&view=diff
==============================================================================
--- lld/trunk/test/ELF/x86-64-dyn-rel-error.s (original)
+++ lld/trunk/test/ELF/x86-64-dyn-rel-error.s Wed Dec 27 16:23:49 2017
@@ -2,7 +2,7 @@
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/shared.s -o %t2.o
 // RUN: ld.lld %t2.o -shared -o %t2.so
-// RUN: not ld.lld %t.o %t2.so -o %t 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o %t2.so -o %t 2>&1 | FileCheck %s
 
         .global _start
 _start:

Modified: lld/trunk/test/ELF/x86-64-dyn-rel-error2.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/x86-64-dyn-rel-error2.s?rev=321507&r1=321506&r2=321507&view=diff
==============================================================================
--- lld/trunk/test/ELF/x86-64-dyn-rel-error2.s (original)
+++ lld/trunk/test/ELF/x86-64-dyn-rel-error2.s Wed Dec 27 16:23:49 2017
@@ -2,7 +2,7 @@
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/shared.s -o %t2.o
 // RUN: ld.lld %t2.o -shared -o %t2.so
-// RUN: not ld.lld %t.o %t2.so -o %t 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o %t2.so -o %t 2>&1 | FileCheck %s
 
 // CHECK: relocation R_X86_64_PC32 cannot be used against shared object; recompile with -fPIC
 // CHECK: >>> defined in {{.*}}.so

Added: lld/trunk/test/ELF/znotext-copy-relocation.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/znotext-copy-relocation.s?rev=321507&view=auto
==============================================================================
--- lld/trunk/test/ELF/znotext-copy-relocation.s (added)
+++ lld/trunk/test/ELF/znotext-copy-relocation.s Wed Dec 27 16:23:49 2017
@@ -0,0 +1,16 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/znotext-copy-relocations.s -o %t2.o
+# RUN: ld.lld %t2.o -o %t2.so -shared
+# RUN: ld.lld -z notext %t.o %t2.so -o %t
+# RUN: llvm-readobj -r %t | FileCheck %s
+
+# CHECK:      Relocations [
+# CHECK-NEXT:   Section ({{.*}}) .rela.dyn {
+# CHECK-NEXT:     R_X86_64_COPY foo 0x0
+# CHECK-NEXT:   }
+# CHECK-NEXT: ]
+
+.global _start
+_start:
+.long foo




More information about the llvm-commits mailing list