[lld] be01af4 - [ELF] Fix non-relocatable-non-emit-relocs --gc-sections to discard .L symbols
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 7 14:34:36 PDT 2022
Author: Fangrui Song
Date: 2022-04-07T14:34:32-07:00
New Revision: be01af4a0f4ae4633929985cc689ad98a449db05
URL: https://github.com/llvm/llvm-project/commit/be01af4a0f4ae4633929985cc689ad98a449db05
DIFF: https://github.com/llvm/llvm-project/commit/be01af4a0f4ae4633929985cc689ad98a449db05.diff
LOG: [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.
Added:
Modified:
lld/ELF/Writer.cpp
lld/test/ELF/discard-locals.s
Removed:
################################################################################
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 45b049c724da7..125d7189a4477 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 88c982553b8b2..63a70300e3931 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:
More information about the llvm-commits
mailing list