Skip to content

Commit 9b65558

Browse files
authored
[lld][ELF] Combine uniqued small data sections (#104485)
RISC-V GCC with `-fdata-sections` will emit `.sbss.<name>`, `.srodata.<name>`, and `.sdata.<name>` sections for small data items of different kinds. Clang/LLVM already emits `.srodata.*` sections, and we intend to emit the other two section name patterns in #87040. This change ensures that any input sections starting `.sbss` are combined into one output section called `.sbss`, and the same respectively for `.srodata` and `.sdata`. This also allows the existing RISC-V specific code for determining an output order for `.sbss` and `.sdata` sections to apply to placing the sections.
1 parent e9b2e16 commit 9b65558

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

lld/ELF/LinkerScript.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ static StringRef getOutputSectionName(const InputSectionBase *s) {
106106
return ".text";
107107
}
108108

109-
for (StringRef v :
110-
{".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss", ".ldata",
111-
".lrodata", ".lbss", ".gcc_except_table", ".init_array", ".fini_array",
112-
".tbss", ".tdata", ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors"})
109+
for (StringRef v : {".data.rel.ro", ".data", ".rodata",
110+
".bss.rel.ro", ".bss", ".ldata",
111+
".lrodata", ".lbss", ".gcc_except_table",
112+
".init_array", ".fini_array", ".tbss",
113+
".tdata", ".ARM.exidx", ".ARM.extab",
114+
".ctors", ".dtors", ".sbss",
115+
".sdata", ".srodata"})
113116
if (isSectionPrefix(v, s->name))
114117
return v;
115118

lld/test/ELF/riscv-section-layout.s

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
# RUN: ld.lld -pie %t.64s.o -o %t.64s
1616
# RUN: llvm-readelf -S -sX %t.64s | FileCheck %s
1717

18-
# NOSDATA: .text
18+
# NOSDATA: .rodata PROGBITS
19+
# NOSDATA-NEXT: .text PROGBITS
1920
# NOSDATA-NEXT: .tdata PROGBITS [[#%x,TDATA:]]
20-
# NOSDATA-NEXT: .tbss
21-
# NOSDATA-NEXT: .dynamic
22-
# NOSDATA-NEXT: .got
23-
# NOSDATA-NEXT: .relro_padding
21+
# NOSDATA-NEXT: .tbss NOBITS
22+
# NOSDATA-NEXT: .dynamic DYNAMIC
23+
# NOSDATA-NEXT: .got PROGBITS
24+
# NOSDATA-NEXT: .relro_padding NOBITS
2425
# NOSDATA-NEXT: .data PROGBITS [[#%x,DATA:]]
2526
# NOSDATA-NEXT: .bss NOBITS [[#%x,BSS:]]
2627

@@ -33,16 +34,18 @@
3334
# NOSDATA-DAG: [[#]]: {{0*}}[[#BSS]] 0 NOTYPE GLOBAL DEFAULT [[#]] (.bss) __bss_start
3435
# NOSDATA-DAG: [[#]]: {{0*}}800 0 NOTYPE GLOBAL DEFAULT 1 (.dynsym) __global_pointer$
3536

36-
# CHECK: .text
37-
# CHECK-NEXT: .tdata
38-
# CHECK-NEXT: .tbss
39-
# CHECK-NEXT: .dynamic
40-
# CHECK-NEXT: .got
41-
# CHECK-NEXT: .relro_padding
42-
# CHECK-NEXT: .data
37+
# CHECK: .rodata PROGBITS
38+
# CHECK-NEXT: .srodata PROGBITS
39+
# CHECK-NEXT: .text PROGBITS
40+
# CHECK-NEXT: .tdata PROGBITS
41+
# CHECK-NEXT: .tbss NOBITS
42+
# CHECK-NEXT: .dynamic DYNAMIC
43+
# CHECK-NEXT: .got PROGBITS
44+
# CHECK-NEXT: .relro_padding NOBITS
45+
# CHECK-NEXT: .data PROGBITS
4346
# CHECK-NEXT: .sdata PROGBITS [[#%x,SDATA:]]
4447
# CHECK-NEXT: .sbss NOBITS [[#%x,SBSS:]]
45-
# CHECK-NEXT: .bss
48+
# CHECK-NEXT: .bss NOBITS
4649

4750
# CHECK-DAG: [[#]]: {{0*}}[[#SBSS]] 0 NOTYPE GLOBAL DEFAULT [[#]] (.sdata) _edata
4851
# CHECK-DAG: [[#]]: {{0*}}[[#SBSS]] 0 NOTYPE GLOBAL DEFAULT [[#]] (.sbss) __bss_start
@@ -51,11 +54,16 @@
5154
.globl _etext, _edata, __bss_start
5255
lla gp, __global_pointer$
5356

57+
.section .rodata,"a",@progbits; .space 1
5458
.section .data,"aw",@progbits; .long _GLOBAL_OFFSET_TABLE_ - .
5559
.section .bss,"aw",@nobits; .space 1
5660
.section .tdata,"awT",@progbits; .space 1
5761
.section .tbss,"awT",@nobits; .space 1
5862
.ifdef SDATA
5963
.section .sdata,"aw",@progbits; .space 1
64+
.section .sdata.suffix,"aw",@progbits; .space 1
6065
.section .sbss,"aw",@nobits; .space 1
66+
.section .sbss.suffix,"aw",@nobits; .space 1
67+
.section .srodata,"a",@progbits; .space 1
68+
.section .srodata.suffix,"a",@progbits; .space 1
6169
.endif

lld/test/ELF/section-name.s

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,34 @@ _start:
4141
.byte 0
4242
.section .tdata.foo,"aGwT",@progbits,foo,comdat
4343
.byte 0
44+
.section .sdata,"aw"
45+
.byte 0
46+
.section .sdata.foo,"aw"
47+
.byte 0
48+
.section .sbss,"aw",@nobits
49+
.byte 0
50+
.section .sbss.foo,"aw",@nobits
51+
.byte 0
52+
.section .srodata,"a"
53+
.byte 0
54+
.section .srodata.foo,"a"
55+
.byte 0
4456

4557
// CHECK: .rodata 00000002
4658
// CHECK-NEXT: .gcc_except_table 00000001
59+
// CHECK-NEXT: .srodata 00000002
4760
// CHECK-NEXT: .text 00000002
4861
// CHECK-NEXT: .tdata 00000001
4962
// CHECK-NEXT: .tbss 00000001
5063
// CHECK-NEXT: .data.rel.ro 00000004
51-
// CHECK-NEXT: .relro_padding 00000df5
64+
// CHECK-NEXT: .relro_padding 00000df1
5265
// CHECK-NEXT: .data 00000002
5366
// CHECK-NEXT: .foo.a 00000001
5467
// CHECK-NEXT: .foo 00000001
68+
// CHECK-NEXT: .sdata 00000002
5569
// CHECK-NEXT: .bss 00000002
70+
// CHECK-NEXT: .sbss 00000002
5671
// CHECK-NEXT: .comment 00000008
5772
// CHECK-NEXT: .symtab 00000030
58-
// CHECK-NEXT: .shstrtab 00000084
73+
// CHECK-NEXT: .shstrtab 0000009a
5974
// CHECK-NEXT: .strtab 00000008

0 commit comments

Comments
 (0)