[ELF] Emit weak-undef symbols in .dynsym of a PIE binary only if linked against share...
authorSiva Chandra <sivachandra@google.com>
Mon, 18 Mar 2019 15:32:57 +0000 (15:32 +0000)
committerSiva Chandra <sivachandra@google.com>
Mon, 18 Mar 2019 15:32:57 +0000 (15:32 +0000)
Reviewers: espindola

Subscribers: emaste, arichardson, MaskRay, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D59275

llvm-svn: 356374

lld/ELF/Symbols.cpp
lld/test/ELF/Inputs/dummy-shared.s [new file with mode: 0644]
lld/test/ELF/pie-weak.s
lld/test/ELF/relocation-relative-weak.s
lld/test/ELF/weak-undef-no-shared-libs.s [new file with mode: 0644]
lld/test/ELF/weak-undef.s

index a1986d8..f07ee3b 100644 (file)
@@ -267,6 +267,10 @@ bool Symbol::includeInDynsym() const {
     return false;
   if (computeBinding() == STB_LOCAL)
     return false;
+  // If a PIE binary was not linked against any shared libraries, then we can
+  // safely drop weak undef symbols from .dynsym.
+  if (isUndefWeak() && Config->Pie && SharedFiles.empty())
+    return false;
   if (!isDefined())
     return true;
   return ExportDynamic;
diff --git a/lld/test/ELF/Inputs/dummy-shared.s b/lld/test/ELF/Inputs/dummy-shared.s
new file mode 100644 (file)
index 0000000..f500936
--- /dev/null
@@ -0,0 +1,2 @@
+.globl bar
+bar:
index c4d64e3..0bfea1f 100644 (file)
@@ -1,6 +1,8 @@
 # REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/dummy-shared.s -o %t1.o
+# RUN: ld.lld %t1.o -shared -o %t1.so
 # RUN: llvm-mc -filetype=obj -relax-relocations=false -triple=x86_64-unknown-linux %s -o %t.o
-# RUN: ld.lld --hash-style=sysv -pie %t.o -o %t
+# RUN: ld.lld --hash-style=sysv -pie %t.o %t1.so -o %t
 # RUN: llvm-readobj -r %t | FileCheck --check-prefix=RELOCS %s
 # RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASM %s
 
@@ -15,6 +17,6 @@
 .globl _start
 _start:
 # DISASM: _start:
-# DISASM-NEXT: 1000: 48 8b 05 99 10 00 00 movq 4249(%rip), %rax
+# DISASM-NEXT: 1000: 48 8b 05 a9 10 00 00 movq 4265(%rip), %rax
 #                                              ^ .got - (.text + 7)
 mov foo@gotpcrel(%rip), %rax
index f6e65ec..78676e3 100644 (file)
@@ -1,6 +1,8 @@
 # REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/dummy-shared.s -o %t1.o
+# RUN: ld.lld %t1.o -shared -o %t1.so
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: ld.lld %t.o -o %t -pie
+# RUN: ld.lld %t.o %t1.so -o %t -pie
 # RUN: llvm-readobj -dyn-relocations %t | FileCheck %s
 
 # CHECK:      Dynamic Relocations {
diff --git a/lld/test/ELF/weak-undef-no-shared-libs.s b/lld/test/ELF/weak-undef-no-shared-libs.s
new file mode 100644 (file)
index 0000000..533eafd
--- /dev/null
@@ -0,0 +1,28 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+// RUN: ld.lld -pie %t.o -o %t
+// RUN: llvm-readobj -V -dyn-symbols %t | FileCheck %s
+
+        .globl _start
+_start:
+        .type foo,@function
+        .weak foo
+        .long foo@gotpcrel
+
+// Test that an entry for weak undefined symbols is NOT emitted in .dynsym as
+// the PIE was not linked with any shared libraries. There are other tests which
+// ensure that the weak undefined symbols do get emitted in .dynsym for PIEs
+// linked against dynamic libraries.
+
+
+// CHECK:      DynamicSymbols [
+// CHECK-NEXT:   Symbol {
+// CHECK-NEXT:     Name:
+// CHECK-NEXT:     Value: 0x0
+// CHECK-NEXT:     Size: 0
+// CHECK-NEXT:     Binding: Local (0x0)
+// CHECK-NEXT:     Type: None (0x0)
+// CHECK-NEXT:     Other: 0
+// CHECK-NEXT:     Section: Undefined (0x0)
+// CHECK-NEXT:   }
+// CHECK-NEXT: ]
index e7905c3..1d15017 100644 (file)
@@ -1,6 +1,8 @@
 # REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/dummy-shared.s -o %t1.o
+# RUN: ld.lld %t1.o -shared -o %t1.so
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: ld.lld %t.o -o %t -pie
+# RUN: ld.lld %t.o -o %t %t1.so -pie
 # RUN: llvm-readobj -dyn-symbols %t | FileCheck %s
 
 # CHECK:      DynamicSymbols [