[ELF] - Make defsym to work correctly with reserved symbols.
authorGeorge Rimar <grimar@accesssoftek.com>
Wed, 7 Feb 2018 09:00:34 +0000 (09:00 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Wed, 7 Feb 2018 09:00:34 +0000 (09:00 +0000)
Previously --defsym=foo2=etext+2 would produce incorrect value
for foo2 because expressions did not work correctly with
reserved symbols, section offset was calculated wrong for them.

Fixes PR35744.

Differential revision: https://reviews.llvm.org/D42911

llvm-svn: 324461

lld/ELF/LinkerScript.cpp
lld/test/ELF/defsym-reserved-syms.s [new file with mode: 0644]

index 24885e0..309755e 100644 (file)
@@ -74,7 +74,7 @@ uint64_t ExprValue::getSectionOffset() const {
   // If the alignment is trivial, we don't have to compute the full
   // value to know the offset. This allows this function to succeed in
   // cases where the output section is not yet known.
-  if (Alignment == 1)
+  if (Alignment == 1 && (!Sec || !Sec->getOutputSection()))
     return Val;
   return getValue() - getSecAddr();
 }
diff --git a/lld/test/ELF/defsym-reserved-syms.s b/lld/test/ELF/defsym-reserved-syms.s
new file mode 100644 (file)
index 0000000..fdab00f
--- /dev/null
@@ -0,0 +1,30 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld -o %t %t.o --defsym=foo2=etext
+# RUN: llvm-readobj -t -s %t | FileCheck %s
+
+## Check 'foo2' value is equal to value of 'etext'.
+# CHECK:     Symbol {
+# CHECK:      Name: foo2
+# CHECK-NEXT:  Value: 0x[[VAL:.*]]
+# CHECK:     Symbol {
+# CHECK:      Name: etext
+# CHECK-NEXT:  Value: 0x[[VAL]]
+
+## Check 'foo2' value set correctly when using
+## reserved symbol 'etext' in expression.
+# RUN: ld.lld -o %t %t.o --defsym=foo2=etext+2
+# RUN: llvm-readobj -t -s %t | FileCheck %s --check-prefix=EXPR
+# EXPR:     Symbol {
+# EXPR:      Name: foo2
+# EXPR-NEXT:  Value: 0x201007
+# EXPR:     Symbol {
+# EXPR:      Name: etext
+# EXPR-NEXT:  Value: 0x201005
+
+.globl foo1
+ foo1 = 0x123
+
+.global _start
+_start:
+  movl $foo2, %edx