Skip to content

Commit bfa4235

Browse files
committed
[lld][ELF] Support for zero flag section groups
This change introduces support for zero flag ELF section groups to lld. lld already supports COMDAT sections, which in ELF are a special type of ELF section groups. These are generally useful to enable linker GC where you want a group of sections to always travel together, that is to be either retained or discarded as a whole, but without the COMDAT semantics. Other ELF linkers already support zero flag ELF section groups and this change helps us reach feature parity. Differential Revision: https://reviews.llvm.org/D96636
1 parent fbee4a0 commit bfa4235

File tree

2 files changed

+88
-44
lines changed

2 files changed

+88
-44
lines changed

lld/ELF/InputFiles.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -609,27 +609,20 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats) {
609609
StringRef signature = getShtGroupSignature(objSections, sec);
610610
this->sections[i] = &InputSection::discarded;
611611

612-
613612
ArrayRef<Elf_Word> entries =
614613
CHECK(obj.template getSectionContentsAsArray<Elf_Word>(sec), this);
615614
if (entries.empty())
616615
fatal(toString(this) + ": empty SHT_GROUP");
617616

618-
// The first word of a SHT_GROUP section contains flags. Currently,
619-
// the standard defines only "GRP_COMDAT" flag for the COMDAT group.
620-
// An group with the empty flag doesn't define anything; such sections
621-
// are just skipped.
622-
if (entries[0] == 0)
623-
continue;
624-
625-
if (entries[0] != GRP_COMDAT)
617+
Elf_Word flag = entries[0];
618+
if (flag && flag != GRP_COMDAT)
626619
fatal(toString(this) + ": unsupported SHT_GROUP format");
627620

628-
bool isNew =
629-
ignoreComdats ||
621+
bool keepGroup =
622+
(flag & GRP_COMDAT) == 0 || ignoreComdats ||
630623
symtab->comdatGroups.try_emplace(CachedHashStringRef(signature), this)
631624
.second;
632-
if (isNew) {
625+
if (keepGroup) {
633626
if (config->relocatable)
634627
this->sections[i] = createInputSection(sec);
635628
selectedGroups.push_back(entries);

lld/test/ELF/gc-sections-group.s

Lines changed: 83 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,99 @@
77
# RUN: ld.lld --gc-sections %t.o -o %t.dead
88
# RUN: llvm-readobj -S %t.dead | FileCheck %s --check-prefix=CHECK-DEAD
99

10-
## .mynote.bar is retained because it is not in a group.
11-
# CHECK-DEAD-NOT: Name: .myanote.foo
12-
# CHECK-DEAD-NOT: Name: .mytext.foo
13-
# CHECK-DEAD-NOT: Name: .mybss.foo
14-
# CHECK-DEAD-NOT: Name: .mynote.foo
15-
# CHECK-DEAD: Name: .mynote.bar
16-
17-
# RUN: ld.lld --gc-sections %t.o -o %t -e anote_foo
18-
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE
19-
# RUN: ld.lld --gc-sections %t.o -o %t -e foo
20-
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE
21-
# RUN: ld.lld --gc-sections %t.o -o %t -e bss_foo
22-
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE
23-
24-
## note_foo as the entry point does not make much sense because it is defined
10+
## .mynote.ccc is retained because it is not in a group.
11+
# CHECK-DEAD-NOT: Name: .myanote.aaa
12+
# CHECK-DEAD-NOT: Name: .mytext.aaa
13+
# CHECK-DEAD-NOT: Name: .mybss.aaa
14+
# CHECK-DEAD-NOT: Name: .mynote.aaa
15+
# CHECK-DEAD-NOT: Name: .myanote.bbb
16+
# CHECK-DEAD-NOT: Name: .mytext.bbb
17+
# CHECK-DEAD-NOT: Name: .mybss.bbb
18+
# CHECK-DEAD-NOT: Name: .mynote.bbb
19+
# CHECK-DEAD: Name: .mynote.ccc
20+
21+
# RUN: ld.lld --gc-sections %t.o -o %t -e anote_aaa
22+
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE-GROUP
23+
# RUN: ld.lld --gc-sections %t.o -o %t -e aaa
24+
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE-GROUP
25+
# RUN: ld.lld --gc-sections %t.o -o %t -e bss_aaa
26+
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE-GROUP
27+
28+
## note_zero as the entry point does not make much sense because it is defined
29+
## in a non-SHF_ALLOC section. This is just to demonstrate the behavior.
30+
# RUN: ld.lld --gc-sections %t.o -o %t -e note_aaa
31+
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE-GROUP
32+
33+
# CHECK-LIVE-GROUP: Name: .myanote.aaa
34+
# CHECK-LIVE-GROUP: Name: .mytext.aaa
35+
# CHECK-LIVE-GROUP: Name: .mybss.aaa
36+
# CHECK-LIVE-GROUP: Name: .mynote.aaa
37+
# CHECK-LIVE-GROUP-NOT: Name: .myanote.bbb
38+
# CHECK-LIVE-GROUP-NOT: Name: .mytext.bbb
39+
# CHECK-LIVE-GROUP-NOT: Name: .mybss.bbb
40+
# CHECK-LIVE-GROUP-NOT: Name: .mynote.bbb
41+
# CHECK-LIVE-GROUP: Name: .mynote.ccc
42+
43+
# RUN: ld.lld --gc-sections %t.o -o %t -e anote_bbb
44+
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE-COMDAT
45+
# RUN: ld.lld --gc-sections %t.o -o %t -e bbb
46+
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE-COMDAT
47+
# RUN: ld.lld --gc-sections %t.o -o %t -e bss_bbb
48+
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE-COMDAT
49+
50+
## note_bbb as the entry point does not make much sense because it is defined
2551
## in a non-SHF_ALLOC section. This is just to demonstrate the behavior.
26-
# RUN: ld.lld --gc-sections %t.o -o %t -e note_foo
27-
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE
52+
# RUN: ld.lld --gc-sections %t.o -o %t -e note_bbb
53+
# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=CHECK-LIVE-COMDAT
54+
55+
# CHECK-LIVE-COMDAT-NOT: Name: .myanote.aaa
56+
# CHECK-LIVE-COMDAT-NOT: Name: .mytext.aaa
57+
# CHECK-LIVE-COMDAT-NOT: Name: .mybss.aaa
58+
# CHECK-LIVE-COMDAT-NOT: Name: .mynote.aaa
59+
# CHECK-LIVE-COMDAT: Name: .myanote.bbb
60+
# CHECK-LIVE-COMDAT: Name: .mytext.bbb
61+
# CHECK-LIVE-COMDAT: Name: .mybss.bbb
62+
# CHECK-LIVE-COMDAT: Name: .mynote.bbb
63+
# CHECK-LIVE-COMDAT: Name: .mynote.ccc
64+
65+
## These sections are in a zero flag group `aaa`.
66+
.globl anote_aaa, aaa, bss_aaa, note_aaa
2867

29-
# CHECK-LIVE: Name: .myanote.foo
30-
# CHECK-LIVE: Name: .mytext.foo
31-
# CHECK-LIVE: Name: .mybss.foo
32-
# CHECK-LIVE: Name: .mynote.foo
33-
# CHECK-LIVE: Name: .mynote.bar
68+
.section .myanote.aaa,"aG",@note,aaa
69+
anote_aaa:
70+
.byte 0
71+
72+
.section .mytext.aaa,"axG",@progbits,aaa
73+
aaa:
74+
.byte 0
75+
76+
.section .mybss.aaa,"awG",@nobits,aaa
77+
bss_aaa:
78+
.byte 0
79+
80+
.section .mynote.aaa,"G",@note,aaa
81+
note_aaa:
82+
.byte 0
3483

35-
.globl anote_foo, foo, bss_foo, note_foo
84+
## These sections are in a COMDAT group `bbb`.
85+
.globl anote_bbb, bbb, bss_bbb, note_bbb
3686

37-
.section .myanote.foo,"aG",@note,foo,comdat
38-
anote_foo:
87+
.section .myanote.bbb,"aG",@note,bbb,comdat
88+
anote_bbb:
3989
.byte 0
4090

41-
.section .mytext.foo,"axG",@progbits,foo,comdat
42-
foo:
91+
.section .mytext.bbb,"axG",@progbits,bbb,comdat
92+
bbb:
4393
.byte 0
4494

45-
.section .mybss.foo,"awG",@nobits,foo,comdat
46-
bss_foo:
95+
.section .mybss.bbb,"awG",@nobits,bbb,comdat
96+
bss_bbb:
4797
.byte 0
4898

49-
.section .mynote.foo,"G",@note,foo,comdat
50-
note_foo:
99+
.section .mynote.bbb,"G",@note,bbb,comdat
100+
note_bbb:
51101
.byte 0
52102

53-
.section .mynote.bar,"",@note
103+
## This section isn't in any group.
104+
.section .mynote.ccc,"",@note
54105
.byte 0

0 commit comments

Comments
 (0)