diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp --- a/lld/ELF/Symbols.cpp +++ b/lld/ELF/Symbols.cpp @@ -267,6 +267,10 @@ 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 --- /dev/null +++ b/lld/test/ELF/Inputs/dummy-shared.s @@ -0,0 +1,2 @@ +.globl bar +bar: diff --git a/lld/test/ELF/pie-weak.s b/lld/test/ELF/pie-weak.s --- a/lld/test/ELF/pie-weak.s +++ b/lld/test/ELF/pie-weak.s @@ -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 diff --git a/lld/test/ELF/relocation-relative-weak.s b/lld/test/ELF/relocation-relative-weak.s --- a/lld/test/ELF/relocation-relative-weak.s +++ b/lld/test/ELF/relocation-relative-weak.s @@ -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 --- /dev/null +++ b/lld/test/ELF/weak-undef-no-shared-libs.s @@ -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: ] diff --git a/lld/test/ELF/weak-undef.s b/lld/test/ELF/weak-undef.s --- a/lld/test/ELF/weak-undef.s +++ b/lld/test/ELF/weak-undef.s @@ -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 [