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 *Sym = dyn_cast(&B)) if (!Sym->isWeak()) cast>(Sym->File)->IsNeeded = true; @@ -216,10 +219,6 @@ }; auto MarkSymbol = [&](Symbol *Sym) { - if (auto *S = dyn_cast_or_null(Sym)) - if (!S->isWeak()) - cast>(S->File)->IsNeeded = true; - if (auto *D = dyn_cast_or_null(Sym)) if (auto *IS = cast_or_null(D->Section)) Enqueue(IS, D->Value); 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 @@ -1,12 +1,15 @@ # REQUIRES: x86 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t2.o # RUN: ld.lld -shared %t2.o -o %t2.so +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared3.s -o %t3.o +# RUN: ld.lld -shared %t3.o -o %t3.so # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o -# RUN: ld.lld --gc-sections --export-dynamic-symbol foo -o %t %t.o --as-needed %t2.so +# RUN: ld.lld --gc-sections --export-dynamic-symbol foo -o %t %t.o --as-needed %t2.so %t3.so # 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 +# '%t2.so' is dropped because 'bar2' is eliminated, whereas +# '%t3.so' is preserved because 'baz' is used. # CHECK: DynamicSymbols [ # CHECK-NEXT: Symbol { @@ -19,16 +22,16 @@ # CHECK-NEXT: Section: Undefined (0x0) # CHECK-NEXT: } # CHECK-NEXT: Symbol { -# CHECK-NEXT: Name: bar2 +# CHECK-NEXT: Name: bar # CHECK-NEXT: Value: # CHECK-NEXT: Size: # CHECK-NEXT: Binding: Global # CHECK-NEXT: Type: # CHECK-NEXT: Other: -# CHECK-NEXT: Section: Undefined +# CHECK-NEXT: Section: .text # CHECK-NEXT: } # CHECK-NEXT: Symbol { -# CHECK-NEXT: Name: bar +# CHECK-NEXT: Name: foo # CHECK-NEXT: Value: # CHECK-NEXT: Size: # CHECK-NEXT: Binding: Global @@ -37,22 +40,24 @@ # CHECK-NEXT: Section: .text # CHECK-NEXT: } # CHECK-NEXT: Symbol { -# CHECK-NEXT: Name: foo +# CHECK-NEXT: Name: baz # CHECK-NEXT: Value: # CHECK-NEXT: Size: # CHECK-NEXT: Binding: Global # CHECK-NEXT: Type: # CHECK-NEXT: Other: -# CHECK-NEXT: Section: .text +# CHECK-NEXT: Section: Undefined # CHECK-NEXT: } # CHECK-NEXT: ] -# CHECK: NEEDED Shared library: [{{.*}}.so] +# CHECK-NOT: NEEDED Shared library: [{{.*}}2.so] +# CHECK: NEEDED Shared library: [{{.*}}3.so] .section .text.foo, "ax" .globl foo foo: call bar +call baz .section .text.bar, "ax" .globl bar