[lld] [lld][ELF] Coalesce uniqued .sbss and .sdata sections (PR #104485)

Sam Elliott via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 16 05:22:24 PDT 2024


https://github.com/lenary updated https://github.com/llvm/llvm-project/pull/104485

>From 5851747b9abcc4d54041f456c5e6856b38be1b8d Mon Sep 17 00:00:00 2001
From: Sam Elliott <quic_aelliott at quicinc.com>
Date: Thu, 15 Aug 2024 12:11:20 -0700
Subject: [PATCH] [lld][ELF] Combine uniqued small data sections

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.
---
 lld/ELF/LinkerScript.cpp            |  3 ++-
 lld/test/ELF/riscv-section-layout.s | 12 ++++++++++--
 lld/test/ELF/section-name.s         | 19 +++++++++++++++++--
 3 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 0c4ba1abb4778c..ad80b9000a5e16 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -109,7 +109,8 @@ static StringRef getOutputSectionName(const InputSectionBase *s) {
   for (StringRef v :
        {".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss", ".ldata",
         ".lrodata", ".lbss", ".gcc_except_table", ".init_array", ".fini_array",
-        ".tbss", ".tdata", ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors"})
+        ".tbss", ".tdata", ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors",
+        ".sbss", ".sdata", ".srodata"})
     if (isSectionPrefix(v, s->name))
       return v;
 
diff --git a/lld/test/ELF/riscv-section-layout.s b/lld/test/ELF/riscv-section-layout.s
index 10e0febf7c4260..87b85052f9a05d 100644
--- a/lld/test/ELF/riscv-section-layout.s
+++ b/lld/test/ELF/riscv-section-layout.s
@@ -15,7 +15,8 @@
 # RUN: ld.lld -pie %t.64s.o -o %t.64s
 # RUN: llvm-readelf -S -sX %t.64s | FileCheck %s
 
-# NOSDATA:      .text
+# NOSDATA:      .rodata
+# NOSDATA-NEXT: .text
 # NOSDATA-NEXT: .tdata   PROGBITS [[#%x,TDATA:]]
 # NOSDATA-NEXT: .tbss
 # NOSDATA-NEXT: .dynamic
@@ -33,7 +34,9 @@
 # NOSDATA-DAG:  [[#]]: {{0*}}[[#BSS]]         0 NOTYPE  GLOBAL DEFAULT [[#]] (.bss) __bss_start
 # NOSDATA-DAG:  [[#]]: {{0*}}800              0 NOTYPE  GLOBAL DEFAULT  1 (.dynsym) __global_pointer$
 
-# CHECK:      .text
+# CHECK:      .rodata
+# CHECK-NEXT: .srodata
+# CHECK-NEXT: .text
 # CHECK-NEXT: .tdata
 # CHECK-NEXT: .tbss
 # CHECK-NEXT: .dynamic
@@ -51,11 +54,16 @@
 .globl _etext, _edata, __bss_start
   lla gp, __global_pointer$
 
+.section .rodata,"a", at progbits; .space 1
 .section .data,"aw", at progbits; .long _GLOBAL_OFFSET_TABLE_ - .
 .section .bss,"aw", at nobits; .space 1
 .section .tdata,"awT", at progbits; .space 1
 .section .tbss,"awT", at nobits; .space 1
 .ifdef SDATA
 .section .sdata,"aw", at progbits; .space 1
+.section .sdata.suffix,"aw", at progbits; .space 1
 .section .sbss,"aw", at nobits; .space 1
+.section .sbss.suffix,"aw", at nobits; .space 1
+.section .srodata,"a", at progbits; .space 1
+.section .srodata.suffix,"a", at progbits; .space 1
 .endif
diff --git a/lld/test/ELF/section-name.s b/lld/test/ELF/section-name.s
index 819cd9c14d9500..7793cc54729599 100644
--- a/lld/test/ELF/section-name.s
+++ b/lld/test/ELF/section-name.s
@@ -41,19 +41,34 @@ _start:
 .byte 0
 .section .tdata.foo,"aGwT", at progbits,foo,comdat
 .byte 0
+.section .sdata,"aw"
+.byte 0
+.section .sdata.foo,"aw"
+.byte 0
+.section .sbss,"aw", at nobits
+.byte 0
+.section .sbss.foo,"aw", at nobits
+.byte 0
+.section .srodata,"a"
+.byte 0
+.section .srodata.foo,"a"
+.byte 0
 
 // CHECK:      .rodata           00000002
 // CHECK-NEXT: .gcc_except_table 00000001
+// CHECK-NEXT: .srodata          00000002
 // CHECK-NEXT: .text             00000002
 // CHECK-NEXT: .tdata            00000001
 // CHECK-NEXT: .tbss             00000001
 // CHECK-NEXT: .data.rel.ro      00000004
-// CHECK-NEXT: .relro_padding    00000df5
+// CHECK-NEXT: .relro_padding    00000df1
 // CHECK-NEXT: .data             00000002
 // CHECK-NEXT: .foo.a            00000001
 // CHECK-NEXT: .foo              00000001
+// CHECK-NEXT: .sdata            00000002
 // CHECK-NEXT: .bss              00000002
+// CHECK-NEXT: .sbss             00000002
 // CHECK-NEXT: .comment          00000008
 // CHECK-NEXT: .symtab           00000030
-// CHECK-NEXT: .shstrtab         00000084
+// CHECK-NEXT: .shstrtab         0000009a
 // CHECK-NEXT: .strtab           00000008



More information about the llvm-commits mailing list