Skip to content

Commit e6d8b6d

Browse files
committed
[ELF] Eliminate symbols demoted due to /DISCARD/ discarded sections
llvm#69295 demoted Defined symbols relative to discarded sections. If such a symbol is unreferenced, the desired behavior is to eliminate it from .symtab just like --gc-sections discarded definitions. Linux kernel's CONFIG_DEBUG_FORCE_WEAK_PER_CPU=y configuration expects that the unreferenced `unused` is not emitted to .symtab (ClangBuiltLinux/linux#2006). For relocations referencing demoted symbols, the symbol index restores to 0 like older lld (`R_X86_64_64 0` in `discard-section.s`). Fix llvm#85048
1 parent 7009c98 commit e6d8b6d

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

lld/ELF/Writer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {
261261
Undefined(sym.file, sym.getName(), binding, sym.stOther, sym.type,
262262
/*discardedSecIdx=*/map.lookup(sym.section))
263263
.overwrite(sym);
264+
// Eliminate from the symbol table, otherwise we would leave an undefined
265+
// symbol if the symbol is unreferenced in the absence of GC.
266+
sym.isUsedInRegularObj = false;
264267
}
265268

266269
// If all references to a DSO happen to be weak, the DSO is not added to

lld/test/ELF/linkerscript/discard-section.s

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
# RUN: ld.lld -r -T a.lds a.o b.o -o a.ro 2>&1 | FileCheck %s --check-prefix=WARNING --implicit-check-not=warning:
1010
# RUN: llvm-readelf -r -s a.ro | FileCheck %s --check-prefix=RELOC
1111

12+
# RUN: ld.lld -r --gc-sections -T a.lds a.o b.o -o a.gc.ro --no-fatal-warnings
13+
# RUN: llvm-readelf -r -s a.gc.ro | FileCheck %s --check-prefix=RELOC-GC
14+
1215
# LOCAL: error: relocation refers to a discarded section: .aaa
1316
# LOCAL-NEXT: >>> defined in a.o
1417
# LOCAL-NEXT: >>> referenced by a.o:(.bbb+0x0)
@@ -32,40 +35,44 @@
3235
# WARNING: warning: relocation refers to a discarded section: .aaa
3336
# WARNING-NEXT: >>> referenced by a.o:(.rela.bbb+0x0)
3437

38+
## GNU ld reports "defined in discarded secion" errors even in -r mode.
39+
## We set the symbol index to 0.
3540
# RELOC: Relocation section '.rela.bbb' at offset {{.*}} contains 1 entries:
3641
# RELOC-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend
3742
# RELOC-NEXT: 0000000000000000 0000000000000000 R_X86_64_NONE 0
3843
# RELOC-EMPTY:
3944
# RELOC-NEXT: Relocation section '.rela.data' at offset {{.*}} contains 4 entries:
4045
# RELOC-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend
41-
# RELOC-NEXT: 0000000000000000 0000000500000001 R_X86_64_64 0000000000000000 global + 0
42-
# RELOC-NEXT: 0000000000000008 0000000700000001 R_X86_64_64 0000000000000000 weak + 0
43-
# RELOC-NEXT: 0000000000000010 0000000600000001 R_X86_64_64 0000000000000000 weakref1 + 0
44-
# RELOC-NEXT: 0000000000000018 0000000800000001 R_X86_64_64 0000000000000000 weakref2 + 0
46+
# RELOC-NEXT: 0000000000000000 0000000000000001 R_X86_64_64 0
47+
# RELOC-NEXT: 0000000000000008 0000000000000001 R_X86_64_64 0
48+
# RELOC-NEXT: 0000000000000010 0000000000000001 R_X86_64_64 0
49+
# RELOC-NEXT: 0000000000000018 0000000000000001 R_X86_64_64 0
4550

4651
# RELOC: Num: Value Size Type Bind Vis Ndx Name
4752
# RELOC-NEXT: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
4853
# RELOC-NEXT: 1: 0000000000000000 0 SECTION LOCAL DEFAULT 1 .text
4954
# RELOC-NEXT: 2: 0000000000000000 0 SECTION LOCAL DEFAULT 2 .bbb
5055
# RELOC-NEXT: 3: 0000000000000000 0 SECTION LOCAL DEFAULT 4 .data
5156
# RELOC-NEXT: 4: 0000000000000000 0 NOTYPE GLOBAL DEFAULT 1 _start
52-
# RELOC-NEXT: 5: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND global
53-
# RELOC-NEXT: 6: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND weakref1
54-
# RELOC-NEXT: 7: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND weak
55-
# RELOC-NEXT: 8: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND weakref2
5657
# RELOC-EMPTY:
5758

59+
# RELOC-GC: There are no relocations in this file.
60+
5861
#--- a.s
5962
.globl _start
6063
_start:
6164

6265
.section .aaa,"a"
63-
.globl global, weakref1
66+
.globl global, weakref1, unused
6467
.weak weak, weakref2
6568
global:
6669
weak:
6770
weakref1:
6871
weakref2:
72+
## Eliminate `unused` just like GC discarded definitions.
73+
## Linux kernel's CONFIG_DEBUG_FORCE_WEAK_PER_CPU=y configuration expects
74+
## that the unreferenced `unused` is not emitted to .symtab.
75+
unused:
6976
.quad 0
7077

7178
.section .bbb,"aw"

0 commit comments

Comments
 (0)