Index: ELF/MarkLive.cpp =================================================================== --- ELF/MarkLive.cpp +++ ELF/MarkLive.cpp @@ -64,6 +64,9 @@ std::function Fn) { Symbol &B = Sec.getFile()->getRelocTargetSym(Rel); + // If a symbol is referenced in a live section, it is alive. + B.ForceLive = true; + if (auto *D = dyn_cast(&B)) { if (!D->Section) return; Index: ELF/Symbols.h =================================================================== --- ELF/Symbols.h +++ ELF/Symbols.h @@ -134,8 +134,8 @@ uint8_t Type) : Binding(Binding), SymbolKind(K), NeedsPltAddr(false), IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false), - IsInIgot(false), IsPreemptible(false), Type(Type), StOther(StOther), - Name(Name) {} + IsInIgot(false), IsPreemptible(false), ForceLive(false), Type(Type), + StOther(StOther), Name(Name) {} const unsigned SymbolKind : 8; @@ -157,6 +157,9 @@ unsigned IsPreemptible : 1; + // True if GC marked this symbol alive because it is used in a live section. + unsigned ForceLive : 1; + // The following fields have the same meaning as the ELF symbol attributes. uint8_t Type; // symbol type uint8_t StOther; // st_other field value @@ -363,6 +366,7 @@ S->CanInline = Sym.CanInline; S->Traced = Sym.Traced; S->InVersionScript = Sym.InVersionScript; + S->ForceLive = Sym.ForceLive; // Print out a log message if --trace-symbol was specified. // This is for debugging. Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -441,8 +441,9 @@ if (auto *S = dyn_cast(Sec)) if (!S->getSectionPiece(D->Value)->Live) return false; + return true; } - return true; + return B.ForceLive || !Config->GcSections; } // Local symbols are not in the linker's symbol table. This function scans Index: test/ELF/gc-collect-undefined.s =================================================================== --- /dev/null +++ test/ELF/gc-collect-undefined.s @@ -0,0 +1,18 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: ld.lld %t -o %tout --gc-sections -shared +# RUN: llvm-nm -D %tout | FileCheck %s + +# CHECK: bar +# CHECK-NOT: qux + + .global foo,bar,qux + .local baz + + .section .data.foo,"aw",%progbits +foo: + .dc.a bar + + .section .bata.baz,"aw",%progbits +baz: + .dc.a qux Index: test/ELF/gc-sections-shared.s =================================================================== --- test/ELF/gc-sections-shared.s +++ test/ELF/gc-sections-shared.s @@ -6,7 +6,8 @@ # RUN: llvm-readobj --dynamic-table --dyn-symbols %t | FileCheck %s # This test the property that we have a needed line for every undefined. -# It would also be OK to drop bar2 and the need for the .so +# The test now is in an intermediate state, it needs to be updated +# when handling DT_NEEDED for dead symbols is fixed. # CHECK: DynamicSymbols [ # CHECK-NEXT: Symbol { @@ -19,15 +20,6 @@ # CHECK-NEXT: Section: Undefined (0x0) # CHECK-NEXT: } # CHECK-NEXT: Symbol { -# CHECK-NEXT: Name: bar2 -# CHECK-NEXT: Value: -# CHECK-NEXT: Size: -# CHECK-NEXT: Binding: Global -# CHECK-NEXT: Type: -# CHECK-NEXT: Other: -# CHECK-NEXT: Section: Undefined -# CHECK-NEXT: } -# CHECK-NEXT: Symbol { # CHECK-NEXT: Name: bar # CHECK-NEXT: Value: # CHECK-NEXT: Size: