Index: lld/trunk/ELF/Symbols.cpp =================================================================== --- lld/trunk/ELF/Symbols.cpp +++ lld/trunk/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; Index: lld/trunk/test/ELF/Inputs/dummy-shared.s =================================================================== --- lld/trunk/test/ELF/Inputs/dummy-shared.s +++ lld/trunk/test/ELF/Inputs/dummy-shared.s @@ -0,0 +1,2 @@ +.globl bar +bar: Index: lld/trunk/test/ELF/pie-weak.s =================================================================== --- lld/trunk/test/ELF/pie-weak.s +++ lld/trunk/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 Index: lld/trunk/test/ELF/relocation-relative-weak.s =================================================================== --- lld/trunk/test/ELF/relocation-relative-weak.s +++ lld/trunk/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 { Index: lld/trunk/test/ELF/weak-undef-no-shared-libs.s =================================================================== --- lld/trunk/test/ELF/weak-undef-no-shared-libs.s +++ lld/trunk/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: ] Index: lld/trunk/test/ELF/weak-undef.s =================================================================== --- lld/trunk/test/ELF/weak-undef.s +++ lld/trunk/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 [