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 dynamic linker is not specified, then weak-undef symbols in a PIE + // binary will not get resolved at runtime. Hence, drop them from .dynsym. + if (isUndefWeak() && Config->Pie && Config->DynamicLinker.empty()) + return false; if (!isDefined()) return true; return ExportDynamic; 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,6 @@ # REQUIRES: x86 # 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 -dynamic-linker /path/to/ld.so -pie %t.o -o %t # RUN: llvm-readobj -r %t | FileCheck --check-prefix=RELOCS %s # RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASM %s 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,6 @@ # REQUIRES: x86 # 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 -pie -dynamic-linker /path/to/ld.so # RUN: llvm-readobj -dyn-relocations %t | FileCheck %s # CHECK: Dynamic Relocations { diff --git a/lld/test/ELF/weak-undef-no-dynamic-linker.s b/lld/test/ELF/weak-undef-no-dynamic-linker.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/weak-undef-no-dynamic-linker.s @@ -0,0 +1,27 @@ +// REQUIRES: x86 +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +// RUN: ld.lld -pie --no-dynamic-linker %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. There +// are other tests which ensure that the weak undefined symbols do get emitted in .dynsym +// for non-static PIEs. + + +// 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,6 @@ # REQUIRES: x86 # 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 -pie -dynamic-linker /path/to/ld.so # RUN: llvm-readobj -dyn-symbols %t | FileCheck %s # CHECK: DynamicSymbols [