From be01af4a0f4ae4633929985cc689ad98a449db05 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 7 Apr 2022 14:34:31 -0700 Subject: [PATCH] [ELF] Fix non-relocatable-non-emit-relocs --gc-sections to discard .L symbols This reverts commit 764cd491b126318add093c7c8ba3884967d64b21, which I incorrectly assumed NFC partly because there were no test coverage for the non-relocatable non-emit-relocs case before 9d6d936243fe343abe89323a27c7241b395af541. The interaction of {,-r,--emit-relocs} {,--discard-locals} {,--gc-sections} is complex but without -r/--emit-relocs, --gc-sections does need to discard .L symbols like --no-gc-sections. The behavior matches GNU ld. --- lld/ELF/Writer.cpp | 2 +- lld/test/ELF/discard-locals.s | 17 +++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 45b049c..125d718 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -643,7 +643,7 @@ static bool shouldKeepInSymtab(const Defined &sym) { // If --emit-reloc or -r is given, preserve symbols referenced by relocations // from live sections. - if (sym.used) + if (sym.used && config->copyRelocs) return true; // Exclude local symbols pointing to .ARM.exidx sections. diff --git a/lld/test/ELF/discard-locals.s b/lld/test/ELF/discard-locals.s index 88c9825..63a7030 100644 --- a/lld/test/ELF/discard-locals.s +++ b/lld/test/ELF/discard-locals.s @@ -6,27 +6,24 @@ ## --gc-sections can discard symbols relative to GCed sections (including STT_SECTION). # RUN: ld.lld --discard-locals --gc-sections %t.o -o %tlocal.gc -# RUN: llvm-readelf -s %tlocal.gc | FileCheck --check-prefixes=DISCARD-LOCALS,DISCARD-LOCALS-GC %s +# RUN: llvm-readelf -s %tlocal.gc | FileCheck --check-prefix=DISCARD-LOCALS %s # RUN: ld.lld --discard-all %t.o -o %tall # RUN: llvm-readelf -s %tall | FileCheck --check-prefix=DISCARD-ALL %s # RUN: ld.lld --discard-all --gc-sections %t.o -o %tall.gc -# RUN: llvm-readelf -s %tall.gc | FileCheck --check-prefixes=DISCARD-ALL,DISCARD-ALL-GC %s +# RUN: llvm-readelf -s %tall.gc | FileCheck --check-prefix=DISCARD-ALL %s ## --discard-locals removes local symbols which start with ".L" # DISCARD-LOCALS: 0: {{0+}} 0 NOTYPE LOCAL DEFAULT UND -# DISCARD-LOCALS-GC-NEXT: NOTYPE LOCAL DEFAULT [[#]] .Lused -# DISCARD-LOCALS-NEXT: NOTYPE LOCAL DEFAULT [[#]] used -# DISCARD-LOCALS-NEXT: NOTYPE LOCAL DEFAULT [[#]] unused -# DISCARD-LOCALS-NOGC-NEXT: NOTYPE LOCAL DEFAULT [[#]] unused_gc -# DISCARD-LOCALS-NEXT: NOTYPE GLOBAL DEFAULT [[#]] _start +# DISCARD-LOCALS-NEXT: NOTYPE LOCAL DEFAULT {{.*}} used +# DISCARD-LOCALS-NEXT: NOTYPE LOCAL DEFAULT {{.*}} unused +# DISCARD-LOCALS-NOGC-NEXT: NOTYPE LOCAL DEFAULT {{.*}} unused_gc +# DISCARD-LOCALS-NEXT: NOTYPE GLOBAL DEFAULT {{.*}} _start ## --discard-all removes all regular local symbols. # DISCARD-ALL: 0: {{0+}} 0 NOTYPE LOCAL DEFAULT UND -# DISCARD-ALL-GC-NEXT: NOTYPE LOCAL DEFAULT [[#]] .Lused -# DISCARD-ALL-GC-NEXT: NOTYPE LOCAL DEFAULT [[#]] used -# DISCARD-ALL-NEXT: NOTYPE GLOBAL DEFAULT [[#]] _start +# DISCARD-ALL-NEXT: NOTYPE GLOBAL DEFAULT {{.*}} _start .globl _start _start: -- 2.7.4