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

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 15 12:29:24 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld-elf

Author: Sam Elliott (lenary)

<details>
<summary>Changes</summary>

RISC-V GCC with `-fdata-sections` will emit `.sbss.<name>` and `.sdata.<name>` sections for small data items. We intend to add the same behaviour to Clang/LLVM in #<!-- -->87040.

This change ensures that any section starting `.sbss` is put in an output section called `.sbss`, and the same for `.sdata`, so that the RISC-V specific code for allocating an output order for `.sbss` and `.sdata` sections can apply.

I did not add a `.sdata.rel.ro` prefix to the list as clang/llvm does not seem to create these at all. Whether it should be or not is a different matter.

---

@<!-- -->topperc I think this is the only change needed, because with this change the code in Writer.cpp will only have to deal with output sections named `.sbss` or `.sdata`, rather than also sections prefixed with those.

---
Full diff: https://github.com/llvm/llvm-project/pull/104485.diff


3 Files Affected:

- (modified) lld/ELF/LinkerScript.cpp (+2-1) 
- (modified) lld/test/ELF/riscv-section-layout.s (+2) 
- (modified) lld/test/ELF/section-name.s (+8-1) 


``````````diff
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 0c4ba1abb4778c..5335a747d19417 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"})
     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..ef71370addd7ec 100644
--- a/lld/test/ELF/riscv-section-layout.s
+++ b/lld/test/ELF/riscv-section-layout.s
@@ -57,5 +57,7 @@
 .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
 .endif
diff --git a/lld/test/ELF/section-name.s b/lld/test/ELF/section-name.s
index 819cd9c14d9500..8df30e609b2b80 100644
--- a/lld/test/ELF/section-name.s
+++ b/lld/test/ELF/section-name.s
@@ -41,6 +41,11 @@ _start:
 .byte 0
 .section .tdata.foo,"aGwT", at progbits,foo,comdat
 .byte 0
+.section .sdata.foo,"aw"
+.byte 0
+.section .sbss.foo,"aw", at nobits
+.byte 0
+
 
 // CHECK:      .rodata           00000002
 // CHECK-NEXT: .gcc_except_table 00000001
@@ -52,8 +57,10 @@ _start:
 // CHECK-NEXT: .data             00000002
 // CHECK-NEXT: .foo.a            00000001
 // CHECK-NEXT: .foo              00000001
+// CHECK-NEXT: .sdata            00000001
 // CHECK-NEXT: .bss              00000002
+// CHECK-NEXT: .sbss             00000001
 // CHECK-NEXT: .comment          00000008
 // CHECK-NEXT: .symtab           00000030
-// CHECK-NEXT: .shstrtab         00000084
+// CHECK-NEXT: .shstrtab         00000091
 // CHECK-NEXT: .strtab           00000008

``````````

</details>


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


More information about the llvm-commits mailing list