[ELF] Fix non-relocatable-non-emit-relocs --gc-sections to discard .L symbols
authorFangrui Song <i@maskray.me>
Thu, 7 Apr 2022 21:34:31 +0000 (14:34 -0700)
committerFangrui Song <i@maskray.me>
Thu, 7 Apr 2022 21:34:32 +0000 (14:34 -0700)
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
lld/test/ELF/discard-locals.s

index 45b049c..125d718 100644 (file)
@@ -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.
index 88c9825..63a7030 100644 (file)
@@ -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: