From 00b0ddb3aff9dde6ad9857e2eae55783c33c0b2a Mon Sep 17 00:00:00 2001 From: mingmingl Date: Mon, 14 Jul 2025 23:55:11 -0700 Subject: [PATCH 1/5] 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 From 9cdf86dca74a7341693bb2ad58ba818335765add Mon Sep 17 00:00:00 2001 From: mingmingl Date: Tue, 15 Jul 2025 16:48:03 -0700 Subject: [PATCH 2/5] regard both .hot and .unlikely as section suffix if lld option keep-data-section-prefix is true --- lld/ELF/Config.h | 1 + lld/ELF/Driver.cpp | 2 ++ lld/ELF/Writer.cpp | 22 +++++++++++++++++++--- lld/test/ELF/relro-data-unlikely.s | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 4c771e47a0e72..ae7de6417c3b9 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -405,6 +405,7 @@ struct Config { bool zInitfirst; bool zInterpose; bool zKeepTextSectionPrefix; + bool zKeepDataSectionPrefix; bool zLrodataAfterBss; bool zNoBtCfi; bool zNodefaultlib; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index a194ae71d559a..16ef4e7b0aeea 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1627,6 +1627,8 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) { ctx.arg.zInterpose = hasZOption(args, "interpose"); ctx.arg.zKeepTextSectionPrefix = getZFlag( args, "keep-text-section-prefix", "nokeep-text-section-prefix", false); + ctx.arg.zKeepDataSectionPrefix = getZFlag( + args, "keep-data-section-prefix", "nokeep-data-section-prefix", false); ctx.arg.zLrodataAfterBss = getZFlag(args, "lrodata-after-bss", "nolrodata-after-bss", false); ctx.arg.zNoBtCfi = hasZOption(args, "nobtcfi"); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index dc7f620b9170e..dad072a8d2df5 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -553,6 +553,22 @@ template void Writer::addSectionSymbols() { } } +// Returns true if the section is a data section that's read only and +// relocatable. +static bool isRelRoDataSection(Ctx &ctx, StringRef SectionName) { + // If -z keep-data-section-prefix is given, '
.hot' and + // '
.unlikely' is considered a split of '
' based on hotness. + // They should share the same section attributes. + if (ctx.arg.zKeepDataSectionPrefix) { + if (SectionName.ends_with(".hot")) + SectionName = SectionName.drop_back(4); + else if (SectionName.ends_with(".unlikely")) + SectionName = SectionName.drop_back(9); + } + + return SectionName == ".data.rel.ro"; +} + // Today's loaders have a feature to make segments read-only after // processing dynamic relocations to enhance security. PT_GNU_RELRO // is defined for that. @@ -629,9 +645,9 @@ static bool isRelroSection(Ctx &ctx, const OutputSection *sec) { // magic section names. StringRef s = sec->name; - 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" || + bool abiAgnostic = isRelRoDataSection(ctx, s) || 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 index 951ed59fb0ef2..981b30a42bc91 100644 --- a/lld/test/ELF/relro-data-unlikely.s +++ b/lld/test/ELF/relro-data-unlikely.s @@ -6,7 +6,7 @@ # 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: ld.lld -z keep-data-section-prefix --script=%t.script %t.o -o %t # RUN: llvm-readelf -l %t | FileCheck --check-prefix=SEG %s # RUN: llvm-readelf -S %t | FileCheck %s From 27778e560ac7752f36e8fee279620b362a83e7cf Mon Sep 17 00:00:00 2001 From: mingmingl Date: Wed, 16 Jul 2025 10:48:57 -0700 Subject: [PATCH 3/5] resolve comments --- lld/ELF/Config.h | 2 +- lld/ELF/Driver.cpp | 4 +- lld/ELF/Writer.cpp | 13 ++-- lld/test/ELF/keep-text-section-prefix.s | 89 +++++++++++++++++++++++++ lld/test/ELF/relro-data-unlikely.s | 50 -------------- 5 files changed, 99 insertions(+), 59 deletions(-) create mode 100644 lld/test/ELF/keep-text-section-prefix.s delete mode 100644 lld/test/ELF/relro-data-unlikely.s diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index ae7de6417c3b9..92fce3b81c352 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -404,8 +404,8 @@ struct Config { bool zIfuncNoplt; bool zInitfirst; bool zInterpose; - bool zKeepTextSectionPrefix; bool zKeepDataSectionPrefix; + bool zKeepTextSectionPrefix; bool zLrodataAfterBss; bool zNoBtCfi; bool zNodefaultlib; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 16ef4e7b0aeea..de1d78bf999d4 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1625,10 +1625,10 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) { ctx.arg.zIfuncNoplt = hasZOption(args, "ifunc-noplt"); ctx.arg.zInitfirst = hasZOption(args, "initfirst"); ctx.arg.zInterpose = hasZOption(args, "interpose"); - ctx.arg.zKeepTextSectionPrefix = getZFlag( - args, "keep-text-section-prefix", "nokeep-text-section-prefix", false); ctx.arg.zKeepDataSectionPrefix = getZFlag( args, "keep-data-section-prefix", "nokeep-data-section-prefix", false); + ctx.arg.zKeepTextSectionPrefix = getZFlag( + args, "keep-text-section-prefix", "nokeep-text-section-prefix", false); ctx.arg.zLrodataAfterBss = getZFlag(args, "lrodata-after-bss", "nolrodata-after-bss", false); ctx.arg.zNoBtCfi = hasZOption(args, "nobtcfi"); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index dad072a8d2df5..380f62347acb1 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -554,11 +554,12 @@ template void Writer::addSectionSymbols() { } // Returns true if the section is a data section that's read only and -// relocatable. -static bool isRelRoDataSection(Ctx &ctx, StringRef SectionName) { - // If -z keep-data-section-prefix is given, '
.hot' and - // '
.unlikely' is considered a split of '
' based on hotness. - // They should share the same section attributes. +// relocatable per its section name. Note this function doesn't look at section +// sh_type or sh_flags. +static bool isRelRoDataSectionByName(Ctx &ctx, StringRef SectionName) { + // If -z keep-data-section-prefix is given, '
.hot' and + // '
.unlikely' is considered a split of '
' based on + // hotness. They should share the same section attributes. if (ctx.arg.zKeepDataSectionPrefix) { if (SectionName.ends_with(".hot")) SectionName = SectionName.drop_back(4); @@ -645,7 +646,7 @@ static bool isRelroSection(Ctx &ctx, const OutputSection *sec) { // magic section names. StringRef s = sec->name; - bool abiAgnostic = isRelRoDataSection(ctx, s) || s == ".bss.rel.ro" || + bool abiAgnostic = isRelRoDataSectionByName(ctx, s) || s == ".bss.rel.ro" || s == ".ctors" || s == ".dtors" || s == ".jcr" || s == ".eh_frame" || s == ".fini_array" || s == ".init_array" || s == ".preinit_array"; diff --git a/lld/test/ELF/keep-text-section-prefix.s b/lld/test/ELF/keep-text-section-prefix.s new file mode 100644 index 0000000000000..f598545f0f7eb --- /dev/null +++ b/lld/test/ELF/keep-text-section-prefix.s @@ -0,0 +1,89 @@ +# REQUIRES: x86 + +# RUN: rm -rf %t && split-file %s %t && cd %t + +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux input.s -o a.o + +# RUN: ld.lld -z keep-data-section-prefix -T linker-script.lds a.o -o a.out +# RUN: llvm-readelf -l a.out | FileCheck --check-prefixes=SEG,LS %s +# RUN: llvm-readelf -S a.out | FileCheck %s --check-prefix=CHECK-LS + +# RUN: ld.lld -z keep-data-section-prefix a.o -o b.out +# RUN: llvm-readelf -l b.out | FileCheck --check-prefixes=SEG,PRE %s +# RUN: llvm-readelf -S b.out | FileCheck %s --check-prefix=CHECK-PRE + +# RUN: ld.lld a.o -o c.out +# RUN: llvm-readelf -l c.out | FileCheck --check-prefixes=SEG,PRE %s +# RUN: llvm-readelf -S c.out | FileCheck %s --check-prefix=CHECK-PRE + +# RUN: not ld.lld -T linker-script.lds a.o -o d.out 2>&1 | FileCheck %s +# CHECK: error: section: .relro_padding is not contiguous with other relro sections + +## The first PW PT_LOAD segment has FileSiz 0x126f (0x1000 + 0x200 + 0x60 + 0xf), +## and its p_offset p_vaddr p_paddr p_filesz should match PT_GNU_RELRO. +# Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align +# SEG: LOAD 0x0001c8 0x00000000002011c8 0x00000000002011c8 0x000001 0x000001 R E 0x1000 +# SEG-NEXT: LOAD 0x0001c9 0x00000000002021c9 0x00000000002021c9 0x00126f 0x001e37 RW 0x1000 +# SEG-NEXT: LOAD 0x001438 0x0000000000204438 0x0000000000204438 0x000001 0x000002 RW 0x1000 +# SEG-NEXT: GNU_RELRO 0x0001c9 0x00000000002021c9 0x00000000002021c9 0x00126f 0x001e37 R 0x1 +# SEG-NEXT: GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x0 + +## Input to output mapping per linker script +## .data.rel.ro.split -> .data.rel.ro +## .data.rel.ro -> .data.rel.ro +## .data.rel.ro.hot -> .data.rel.ro.hot +## .data.rel.ro.unlikely -> .data.rel.ro.unlikely +# LS: .text +# LS-NEXT: .data.rel.ro.hot .data.rel.ro .data.rel.ro.unlikely .relro_padding +# LS-NEXT: .data .bss + +# [Nr] Name Type Address Off Size +# CHECK-LS: .data.rel.ro.hot PROGBITS 00000000002021c9 0001c9 00000f +# CHECK-LS-NEXT: .data.rel.ro PROGBITS 00000000002021d8 0001d8 000260 +# CHECK-LS-NEXT: .data.rel.ro.unlikely PROGBITS 0000000000202438 000438 001000 +# CHECK-LS-NEXT: .relro_padding NOBITS 0000000000203438 001438 000bc8 +# CHECK-LS-NEXT: .data PROGBITS 0000000000204438 001438 000001 +# CHECK-LS-NEXT: .bss NOBITS 0000000000204439 001439 000001 + +## Linker script is not provided to map data sections. +## So all input sections with prefix .data.rel.ro will map to .data.rel.ro in the output. +# PRE: .text +# PRE-NEXT: .data.rel.ro .relro_padding +# PRE-NEXT: .data .bss + +# [Nr] Name Type Address Off Size +# CHECK-PRE: .data.rel.ro PROGBITS 00000000002021c9 0001c9 00126f +# CHECK-PRE-NEXT: .relro_padding NOBITS 0000000000203438 001438 000bc8 +# CHECK-PRE-NEXT: .data PROGBITS 0000000000204438 001438 000001 +# CHECK-PRE-NEXT: .bss NOBITS 0000000000204439 001439 000001 + +#--- linker-script.lds +SECTIONS { + .data.rel.ro.hot : { *(.data.rel.ro.hot) } + .data.rel.ro : { .data.rel.ro } + .data.rel.ro.unlikely : { *(.data.rel.ro.unlikely) } +} INSERT AFTER .text + + +#--- input.s +.globl _start +_start: + ret + +.section .data.rel.ro.hot, "aw" +.space 15 + +.section .data.rel.ro, "aw" +.space 96 + +.section .data.rel.ro.split,"aw" +.space 512 + +.section .data.rel.ro.unlikely, "aw" +.space 4096 + +.section .data, "aw" +.space 1 + +.section .bss, "aw" +.space 1 diff --git a/lld/test/ELF/relro-data-unlikely.s b/lld/test/ELF/relro-data-unlikely.s deleted file mode 100644 index 981b30a42bc91..0000000000000 --- a/lld/test/ELF/relro-data-unlikely.s +++ /dev/null @@ -1,50 +0,0 @@ -# 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 -z keep-data-section-prefix --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 From eb439b12c28e05607a92c45cb1f010d9921ca064 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Wed, 16 Jul 2025 20:31:55 -0700 Subject: [PATCH 4/5] omit ByName as suggested --- lld/ELF/Writer.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 380f62347acb1..ce739eba0d60d 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -554,9 +554,8 @@ template void Writer::addSectionSymbols() { } // Returns true if the section is a data section that's read only and -// relocatable per its section name. Note this function doesn't look at section -// sh_type or sh_flags. -static bool isRelRoDataSectionByName(Ctx &ctx, StringRef SectionName) { +// relocatable per its section name. +static bool isRelRoDataSection(Ctx &ctx, StringRef SectionName) { // If -z keep-data-section-prefix is given, '
.hot' and // '
.unlikely' is considered a split of '
' based on // hotness. They should share the same section attributes. @@ -646,7 +645,7 @@ static bool isRelroSection(Ctx &ctx, const OutputSection *sec) { // magic section names. StringRef s = sec->name; - bool abiAgnostic = isRelRoDataSectionByName(ctx, s) || s == ".bss.rel.ro" || + bool abiAgnostic = isRelRoDataSection(ctx, s) || s == ".bss.rel.ro" || s == ".ctors" || s == ".dtors" || s == ".jcr" || s == ".eh_frame" || s == ".fini_array" || s == ".init_array" || s == ".preinit_array"; From 3b23fde82ecb698c3f8c280b18ee61a6a99c88b2 Mon Sep 17 00:00:00 2001 From: Mingming Liu Date: Fri, 18 Jul 2025 15:54:19 -0700 Subject: [PATCH 5/5] apply suggestions Co-authored-by: Sam Elliott --- lld/test/ELF/keep-text-section-prefix.s | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lld/test/ELF/keep-text-section-prefix.s b/lld/test/ELF/keep-text-section-prefix.s index f598545f0f7eb..0fea26355c679 100644 --- a/lld/test/ELF/keep-text-section-prefix.s +++ b/lld/test/ELF/keep-text-section-prefix.s @@ -19,7 +19,7 @@ # RUN: not ld.lld -T linker-script.lds a.o -o d.out 2>&1 | FileCheck %s # CHECK: error: section: .relro_padding is not contiguous with other relro sections -## The first PW PT_LOAD segment has FileSiz 0x126f (0x1000 + 0x200 + 0x60 + 0xf), +## The first RW PT_LOAD segment has FileSiz 0x126f (0x1000 + 0x200 + 0x60 + 0xf), ## and its p_offset p_vaddr p_paddr p_filesz should match PT_GNU_RELRO. # Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align # SEG: LOAD 0x0001c8 0x00000000002011c8 0x00000000002011c8 0x000001 0x000001 R E 0x1000