Index: ELF/SymbolTable.cpp =================================================================== --- ELF/SymbolTable.cpp +++ ELF/SymbolTable.cpp @@ -503,7 +503,8 @@ Alignment, VerdefIndex); if (!WasInserted) { 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,20 @@ +# 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 + +# The symbol a is not referenced by a regular object. +# CHECK-NOT: a.so + +.global _start +_start: + jmp b@PLT