[LLD] Emit dynamic relocations for references to script symbols in -pie links
authorBen Dunbobbin <bd1976llvm@gmail.com>
Wed, 1 May 2019 14:07:31 +0000 (14:07 +0000)
committerBen Dunbobbin <bd1976llvm@gmail.com>
Wed, 1 May 2019 14:07:31 +0000 (14:07 +0000)
https://reviews.llvm.org/D55423 caused LLD to stop emitting dynamic relocations for references to script symbols in -pie links.

This patch fixes that regression.

https://reviews.llvm.org/D61298

llvm-svn: 359683

lld/ELF/Relocations.cpp
lld/test/ELF/linkerscript/symbol-pie.s [new file with mode: 0644]

index 7408506..fc35250 100644 (file)
@@ -421,11 +421,6 @@ static bool isStaticLinkTimeConstant(RelExpr E, RelType Type, const Symbol &Sym,
   if (E == R_SIZE)
     return true;
 
-  // We set the final symbols values for linker script defined symbols later.
-  // They always can be computed as a link time constant.
-  if (Sym.ScriptDefined)
-    return true;
-
   // For the target and the relocation, we want to know if they are
   // absolute or relative.
   bool AbsVal = isAbsoluteValue(Sym);
@@ -449,6 +444,11 @@ static bool isStaticLinkTimeConstant(RelExpr E, RelType Type, const Symbol &Sym,
   if (Sym.isUndefWeak())
     return true;
 
+  // We set the final symbols values for linker script defined symbols later.
+  // They always can be computed as a link time constant.
+  if (Sym.ScriptDefined)
+      return true;
+
   error("relocation " + toString(Type) + " cannot refer to absolute symbol: " +
         toString(Sym) + getLocation(S, Sym, RelOff));
   return true;
diff --git a/lld/test/ELF/linkerscript/symbol-pie.s b/lld/test/ELF/linkerscript/symbol-pie.s
new file mode 100644 (file)
index 0000000..1e4bc54
--- /dev/null
@@ -0,0 +1,19 @@
+# REQUIRES: x86\r
+\r
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o\r
+# RUN: echo "SECTIONS { .data 0x2000 : { foo = .; *(.data) } }" > %t.script\r
+# RUN: ld.lld -pie -o %t --script %t.script %t.o\r
+# RUN: llvm-readobj -r %t | FileCheck %s\r
+\r
+## Position independent executables require dynamic\r
+## relocations for references to non-absolute script\r
+## symbols.\r
+\r
+# CHECK:      Relocations [\r
+# CHECK-NEXT:  Section ({{.*}}) .rela.dyn {\r
+# CHECK-NEXT:    0x2000 R_X86_64_RELATIVE - 0x2000\r
+# CHECK-NEXT:  }\r
+# CHECK-NEXT: ]\r
+\r
+.data\r
+.quad foo\r