[lld] [lld][ICF] Prevent merging two sections when they point to non-globals (PR #136641)

Pranav Kant via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 21 18:42:37 PDT 2025


https://github.com/pranavk created https://github.com/llvm/llvm-project/pull/136641

None

>From 9a3d581261f82ce8ad53d5a001f8cdbbea6e2a89 Mon Sep 17 00:00:00 2001
From: Pranav Kant <prka at google.com>
Date: Tue, 22 Apr 2025 01:41:36 +0000
Subject: [PATCH] [lld][ICF] Prevent merging two sections when they point to
 non-globals

---
 lld/ELF/ICF.cpp | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/lld/ELF/ICF.cpp b/lld/ELF/ICF.cpp
index 849f6bdd445f9..cc2e768cd18cb 100644
--- a/lld/ELF/ICF.cpp
+++ b/lld/ELF/ICF.cpp
@@ -375,6 +375,18 @@ bool ICF<ELFT>::variableEq(const InputSection *secA, Relocs<RelTy> ra,
     auto *da = cast<Defined>(&sa);
     auto *db = cast<Defined>(&sb);
 
+    // Merging sections here also means that we would mark corresponding
+    // relocation target symbols as equivalent, done later in ICF during section
+    // folding. To preserve correctness for such symbol equivalence (see
+    // GH#129122 for details), we also have to disable section merging here:
+    // 1. We don't merge local symbols into global symbols, or vice-versa. There
+    // are post-icf passes that assert on this behavior.
+    // 2. We also don't merge two local symbols together. There are post-icf
+    // passes that expect to see no duplicates when iterating over local
+    // symbols.
+    if (!da->isGlobal() || !db->isGlobal())
+      return false;
+
     // We already dealt with absolute and non-InputSection symbols in
     // constantEq, and for InputSections we have already checked everything
     // except the equivalence class.



More information about the llvm-commits mailing list