[lld] [lld][ELF] Allow `.data.rel.ro.unlikely` to be RELRO (PR #148920)

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 15 11:36:52 PDT 2025


https://github.com/mingmingl-llvm created https://github.com/llvm/llvm-project/pull/148920

https://discourse.llvm.org/t/rfc-profile-guided-static-data-partitioning/83744 proposes to partition a static data section (like `.data.rel.ro`) into two sections, one grouping the cold ones and the other grouping the rest.

lld requires all relro sections to be contiguous.  To place `.data.rel.ro.unlikely` in the middle of all relro sections, this change proposes to add `.data.rel.ro.unlikely` explicitly as RELRO section. 

>From 00b0ddb3aff9dde6ad9857e2eae55783c33c0b2a Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Mon, 14 Jul 2025 23:55:11 -0700
Subject: [PATCH] allow data.rel.ro.unlikely as an relro section

---
 lld/ELF/Writer.cpp                 |  6 ++--
 lld/test/ELF/relro-data-unlikely.s | 50 ++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 3 deletions(-)
 create mode 100644 lld/test/ELF/relro-data-unlikely.s

diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 15909daf51ab6..dc7f620b9170e 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -629,9 +629,9 @@ static bool isRelroSection(Ctx &ctx, const OutputSection *sec) {
   // magic section names.
   StringRef s = sec->name;
 
-  bool abiAgnostic = s == ".data.rel.ro" || s == ".bss.rel.ro" ||
-                     s == ".ctors" || s == ".dtors" || s == ".jcr" ||
-                     s == ".eh_frame" || s == ".fini_array" ||
+  bool abiAgnostic = s == ".data.rel.ro" || s == ".data.rel.ro.unlikely" ||
+                     s == ".bss.rel.ro" || s == ".ctors" || s == ".dtors" ||
+                     s == ".jcr" || s == ".eh_frame" || s == ".fini_array" ||
                      s == ".init_array" || s == ".preinit_array";
 
   bool abiSpecific =
diff --git a/lld/test/ELF/relro-data-unlikely.s b/lld/test/ELF/relro-data-unlikely.s
new file mode 100644
index 0000000000000..951ed59fb0ef2
--- /dev/null
+++ b/lld/test/ELF/relro-data-unlikely.s
@@ -0,0 +1,50 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+
+# RUN: echo "SECTIONS { \
+# RUN:  .data.rel.ro : { .data.rel.ro } \
+# RUN:  .data.rel.ro.unlikely : { *(.data.rel.ro.unlikely) } \
+# RUN: } INSERT AFTER .text " > %t.script
+
+# RUN: ld.lld --script=%t.script %t.o -o %t
+# RUN: llvm-readelf -l %t | FileCheck --check-prefix=SEG %s
+# RUN: llvm-readelf -S %t | FileCheck %s
+
+# There are 2 RW PT_LOAD segments. p_offset p_vaddr p_paddr p_filesz of the first
+# should match PT_GNU_RELRO.
+# The .data.rel.ro.unlikely section is in PT_GNU_RELRO segment.
+
+#           Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
+# SEG:      LOAD           0x0001c8 0x00000000002011c8 0x00000000002011c8 0x000001 0x000001 R E 0x1000
+# SEG-NEXT: LOAD           0x0001c9 0x00000000002021c9 0x00000000002021c9 0x001001 0x001e37 RW  0x1000
+# SEG-NEXT: LOAD           0x0011ca 0x00000000002041ca 0x00000000002041ca 0x000001 0x000002 RW  0x1000
+# SEG-NEXT: GNU_RELRO      0x0001c9 0x00000000002021c9 0x00000000002021c9 0x001001 0x001e37 R   0x1
+# SEG-NEXT: GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x0
+
+# SEG:      .text
+# SEG-NEXT: .data.rel.ro .data.rel.ro.unlikely .relro_padding
+# SEG-NEXT: .data .bss
+
+#        [Nr] Name                    Type            Address          Off    Size
+# CHECK:      .data.rel.ro            PROGBITS        00000000002021c9 0001c9 000001
+# CHECK-NEXT: .data.rel.ro.unlikely   PROGBITS        00000000002021ca 0001ca 001000
+# CHECK-NEXT: .relro_padding          NOBITS          00000000002031ca 0011ca 000e36
+# CHECK-NEXT: .data                   PROGBITS        00000000002041ca 0011ca 000001
+# CHECK-NEXT: .bss                    NOBITS          00000000002041cb 0011cb 000001
+
+.globl _start
+_start:
+  ret
+
+
+.section .data.rel.ro, "aw"
+.space 1
+
+.section .data.rel.ro.unlikely, "aw"
+.space 4096
+
+.section .data, "aw"
+.space 1
+
+.section .bss, "aw"
+.space 1



More information about the llvm-commits mailing list