[PATCH] D38790: [ELF] Do not keep symbols if they referenced only from discarded sections.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 20 10:34:00 PST 2017


Taking a look

Igor Kudrin via Phabricator <reviews at reviews.llvm.org> writes:

> ikudrin updated this revision to Diff 123596.
> ikudrin edited the summary of this revision.
> ikudrin added a comment.
>
> - Extract corrections in the existing tests into https://reviews.llvm.org/D40253.
> - Use handling of DT_NEEDED from https://reviews.llvm.org/D40240.
> - Don't try to maintain actual `Live` flag for all symbols. Just mark symbols used in relocations in live sections and make the final decision later in `includeInSymtab`.
>
>
> https://reviews.llvm.org/D38790
>
> Files:
>   ELF/MarkLive.cpp
>   ELF/Symbols.h
>   ELF/Writer.cpp
>   test/ELF/gc-collect-undefined.s
>   test/ELF/gc-sections-shared.s
>
> 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,40 +22,42 @@
>  # 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
>  # CHECK-NEXT:     Type:
>  # CHECK-NEXT:     Other:
>  # 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
> 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: ELF/Writer.cpp
> ===================================================================
> --- ELF/Writer.cpp
> +++ ELF/Writer.cpp
> @@ -441,8 +441,9 @@
>      if (auto *S = dyn_cast<MergeInputSection>(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: 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/MarkLive.cpp
> ===================================================================
> --- ELF/MarkLive.cpp
> +++ ELF/MarkLive.cpp
> @@ -64,6 +64,9 @@
>                           std::function<void(InputSectionBase *, uint64_t)> Fn) {
>    Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
>  
> +  // If a symbol is referenced in a live section, it is alive.
> +  B.ForceLive = true;
> +
>    if (auto *Sym = dyn_cast<SharedSymbol>(&B))
>      if (!Sym->isWeak())
>        cast<SharedFile<ELFT>>(Sym->File)->IsNeeded = true;
> @@ -216,10 +219,6 @@
>    };
>  
>    auto MarkSymbol = [&](Symbol *Sym) {
> -    if (auto *S = dyn_cast_or_null<SharedSymbol>(Sym))
> -      if (!S->isWeak())
> -        cast<SharedFile<ELFT>>(S->File)->IsNeeded = true;
> -
>      if (auto *D = dyn_cast_or_null<Defined>(Sym))
>        if (auto *IS = cast_or_null<InputSectionBase>(D->Section))
>          Enqueue(IS, D->Value);


More information about the llvm-commits mailing list