[lld] 75fbd79 - [lld-macho] Relax safe ICF's keepUnique for ld64-coalesced data sections (#193125)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 21 19:12:40 PDT 2026
Author: Zhaoxuan Jiang
Date: 2026-06-22T10:12:35+08:00
New Revision: 75fbd791638e583eb5bf483906fb5a75b7ab18b0
URL: https://github.com/llvm/llvm-project/commit/75fbd791638e583eb5bf483906fb5a75b7ab18b0
DIFF: https://github.com/llvm/llvm-project/commit/75fbd791638e583eb5bf483906fb5a75b7ab18b0.diff
LOG: [lld-macho] Relax safe ICF's keepUnique for ld64-coalesced data sections (#193125)
#188400 regressed data-section folding under --icf=safe{,_thunks}:
no-addrsig fallback, and over-broad compiler-emitted addrsig entries
covering data symbols, both caused markSymAsAddrSig to set keepUnique on
data sections, after which foldIdenticalSections refused to fold them.
ld64 coalesces __cfstring, __objc_classrefs and __objc_selrefs
unconditionally regardless of addrsig, so ignore keepUnique for them as
a workaround for the imprecise addrsig payload.
Added:
lld/test/MachO/icf-safe-data-addrsig.s
Modified:
lld/MachO/ICF.cpp
Removed:
################################################################################
diff --git a/lld/MachO/ICF.cpp b/lld/MachO/ICF.cpp
index cd9760dedf608..c7b408ac6df18 100644
--- a/lld/MachO/ICF.cpp
+++ b/lld/MachO/ICF.cpp
@@ -455,12 +455,12 @@ void ICF::run() {
forEachClass([&](size_t begin, size_t end) {
if (end - begin < 2)
return;
- bool useSafeThunks = config->icfLevel == ICFLevel::safe_thunks;
-
// For ICF level safe_thunks, replace keepUnique function bodies with
- // thunks. For all other ICF levles, directly merge the functions.
+ // thunks. For all other ICF levels, directly merge the functions.
ConcatInputSection *beginIsec = icfInputs[begin];
+ bool useSafeThunks =
+ config->icfLevel == ICFLevel::safe_thunks && isCodeSection(beginIsec);
for (size_t i = begin + 1; i < end; ++i) {
// Skip keepUnique inputs when using safe_thunks (already handled above)
if (useSafeThunks && icfInputs[i]->keepUnique) {
@@ -588,19 +588,30 @@ void macho::foldIdenticalSections(bool onlyCfStrings) {
bool isCodeSec = isCodeSection(isec);
- // When keepUnique is true, the section is not foldable. Unless we are at
- // icf level safe_thunks, in which case we still want to fold code sections.
- // When using safe_thunks we'll apply the safe_thunks logic at merge time
- // based on the 'keepUnique' flag.
- bool noUniqueRequirement =
- !isec->keepUnique ||
- ((config->icfLevel == ICFLevel::safe_thunks) && isCodeSec);
+ // Determine whether keepUnique forbids folding this section.
+ // - __cfstring / __objc_classrefs / __objc_selrefs always fold
+ // regardless of keepUnique. Compilers currently emit over-broad
+ // __llvm_addrsig entries that can cover non-address-significant data
+ // symbols in these sections; ld64 coalesces them unconditionally, and
+ // we match that behavior.
+ // - Under safe_thunks, keepUnique code sections still fold; the
+ // safe_thunks logic is applied later at merge time based on the
+ // keepUnique flag.
+ // - Otherwise, keepUnique sections are not foldable.
+ // Happens to match isFoldableWithAddendsRemoved today, but expresses a
+ //
diff erent intent (ld64's coalescing semantics, not addend stripping),
+ // so the two may diverge as either list grows.
+ bool isUnconditionallyCoalescedData = isFoldableWithAddendsRemoved;
+ bool isSafeThunksCode =
+ config->icfLevel == ICFLevel::safe_thunks && isCodeSec;
+ bool keepUniqueAllowsFolding =
+ !isec->keepUnique || isUnconditionallyCoalescedData || isSafeThunksCode;
// FIXME: consider non-code __text sections as foldable?
bool isFoldable = (!onlyCfStrings || isCfStringSection(isec)) &&
(isCodeSec || isFoldableWithAddendsRemoved ||
isGccExceptTabSection(isec)) &&
- noUniqueRequirement && !isec->hasAltEntry &&
+ keepUniqueAllowsFolding && !isec->hasAltEntry &&
!isec->shouldOmitFromOutput() && hasFoldableFlags;
if (isFoldable) {
foldable.push_back(isec);
diff --git a/lld/test/MachO/icf-safe-data-addrsig.s b/lld/test/MachO/icf-safe-data-addrsig.s
new file mode 100644
index 0000000000000..ebabdd42b6acc
--- /dev/null
+++ b/lld/test/MachO/icf-safe-data-addrsig.s
@@ -0,0 +1,105 @@
+# REQUIRES: aarch64
+
+## Compilers currently emit over-broad __llvm_addrsig entries that also cover
+## data symbols which aren't actually address-significant. To match ld64 --
+## which coalesces __cfstring / __objc_classrefs / __objc_selrefs
+## unconditionally -- LLD deliberately disregards addrsig markings on those
+## data sections, so the duplicate _cfs2 / _cr2 / _sr2 entries must fold into
+## _cfs1 / _cr1 / _sr1 whether the compiler lists data symbols in
+## __llvm_addrsig (with-addrsig.o) or emits no __llvm_addrsig at all
+## (without-addrsig.o) when ICF is enabled. At --icf=none, only __cfstring
+## still folds (via --deduplicate-strings, which is on by default);
+## __objc_classrefs and __objc_selrefs stay unique.
+
+# RUN: rm -rf %t && split-file %s %t
+# RUN: llvm-mc -I %t -filetype=obj -triple=arm64-apple-darwin %t/with-addrsig.s -o %t/with-addrsig.o
+# RUN: llvm-mc -I %t -filetype=obj -triple=arm64-apple-darwin %t/without-addrsig.s -o %t/without-addrsig.o
+# RUN: %lld -arch arm64 -lSystem --icf=safe -dylib -map - -o %t/with-addrsig-safe.dylib %t/with-addrsig.o | FileCheck %s --check-prefixes=CHECK,FOLD
+# RUN: %lld -arch arm64 -lSystem --icf=safe -dylib -map - -o %t/without-addrsig-safe.dylib %t/without-addrsig.o | FileCheck %s --check-prefixes=CHECK,FOLD
+# RUN: %lld -arch arm64 -lSystem --icf=safe_thunks -dylib -map - -o %t/with-addrsig-thunks.dylib %t/with-addrsig.o | FileCheck %s --check-prefixes=CHECK,FOLD
+# RUN: %lld -arch arm64 -lSystem --icf=safe_thunks -dylib -map - -o %t/without-addrsig-thunks.dylib %t/without-addrsig.o | FileCheck %s --check-prefixes=CHECK,FOLD
+# RUN: %lld -arch arm64 -lSystem --icf=all -dylib -map - -o %t/with-addrsig-all.dylib %t/with-addrsig.o | FileCheck %s --check-prefixes=CHECK,FOLD
+# RUN: %lld -arch arm64 -lSystem --icf=all -dylib -map - -o %t/without-addrsig-all.dylib %t/without-addrsig.o | FileCheck %s --check-prefixes=CHECK,FOLD
+# RUN: %lld -arch arm64 -lSystem --icf=none -dylib -map - -o %t/with-addrsig-none.dylib %t/with-addrsig.o | FileCheck %s --check-prefixes=CHECK,NOFOLD
+# RUN: %lld -arch arm64 -lSystem --icf=none -dylib -map - -o %t/without-addrsig-none.dylib %t/without-addrsig.o | FileCheck %s --check-prefixes=CHECK,NOFOLD
+
+## __cfstring folds whenever --deduplicate-strings is on, i.e. at every ICF
+## level including --icf=none.
+# CHECK: 0x00000020 [ 2] _cfs1
+# CHECK-NEXT: 0x00000000 [ 2] _cfs2
+
+## __objc_classrefs / __objc_selrefs fold only when ICF runs.
+# FOLD: 0x00000008 [ 2] _cr1
+# FOLD-NEXT: 0x00000000 [ 2] _cr2
+# FOLD: 0x00000008 [ 2] _sr1
+# FOLD-NEXT: 0x00000000 [ 2] _sr2
+
+# NOFOLD: 0x00000008 [ 2] _cr1
+# NOFOLD-NEXT: 0x00000008 [ 2] _cr2
+# NOFOLD: 0x00000008 [ 2] _sr1
+# NOFOLD-NEXT: 0x00000008 [ 2] _sr2
+
+#--- common.s
+.subsections_via_symbols
+
+.section __DATA,__cfstring
+.p2align 3
+.globl _cfs1
+_cfs1:
+ .quad _class
+ .long 1992
+ .space 4
+ .quad Lstr
+ .quad 5
+.globl _cfs2
+_cfs2:
+ .quad _class
+ .long 1992
+ .space 4
+ .quad Lstr
+ .quad 5
+
+.section __TEXT,__cstring,cstring_literals
+Lstr:
+ .asciz "hi"
+
+.section __DATA,__objc_data
+.globl _class
+_class:
+ .quad 42
+
+.section __DATA,__objc_classrefs,regular,no_dead_strip
+.p2align 3
+.globl _cr1
+_cr1:
+ .quad _class
+.globl _cr2
+_cr2:
+ .quad _class
+
+.section __TEXT,__objc_methname,cstring_literals
+Lsel:
+ .asciz "msg"
+
+.section __DATA,__objc_selrefs,literal_pointers,no_dead_strip
+.p2align 3
+.globl _sr1
+_sr1:
+ .quad Lsel
+.globl _sr2
+_sr2:
+ .quad Lsel
+
+#--- with-addrsig.s
+.include "common.s"
+
+.addrsig
+.addrsig_sym _cfs1
+.addrsig_sym _cfs2
+.addrsig_sym _cr1
+.addrsig_sym _cr2
+.addrsig_sym _sr1
+.addrsig_sym _sr2
+
+#--- without-addrsig.s
+.include "common.s"
More information about the llvm-commits
mailing list