Index: ELF/SymbolTable.cpp =================================================================== --- ELF/SymbolTable.cpp +++ ELF/SymbolTable.cpp @@ -509,7 +509,8 @@ Alignment, VerdefIndex); S->Binding = Binding; - if (!S->isWeak() && !Config->GcSections && WasUndefined) + if (!S->isWeak() && S->IsUsedInRegularObj && !Config->GcSections && + WasUndefined) File.IsNeeded = true; } } Index: test/ELF/as-needed-not-in-regular.s =================================================================== --- /dev/null +++ test/ELF/as-needed-not-in-regular.s @@ -0,0 +1,24 @@ +# REQUIRES: x86 + +# RUN: echo '.globl a; .type a, @function; a: ret' | \ +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %ta.o +# RUN: ld.lld %ta.o --shared --soname=a.so -o %ta.so + +# RUN: echo '.globl b; .type b, @function; b: jmp a@PLT' | \ +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %tb.o +# RUN: ld.lld %tb.o %ta.so --shared --soname=b.so -o %tb.so + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: ld.lld %t.o %tb.so --as-needed %ta.so -o %t +# RUN: llvm-readelf -d %t | FileCheck %s + +# RUN: ld.lld %t.o %tb.so --as-needed %ta.so --gc-sections -o %t +# RUN: llvm-readelf -d %t | FileCheck %s + +# The symbol a (defined in a.so) is not referenced by a regular object, +# don't add a DT_NEEDED entry for it. +# CHECK-NOT: a.so + +.global _start +_start: + jmp b@PLT