Skip to content

[lld][ELF] Avoid merging SHT_GROUP sections - #240069

Closed
OgShadoww wants to merge 1 commit into
llvm:mainfrom
OgShadoww:fix/lld-elf-group-merge
Closed

OgShadoww wants to merge 1 commit into
llvm:mainfrom
OgShadoww:fix/lld-elf-group-merge

Conversation

@OgShadoww

Copy link
Copy Markdown
Contributor

Fix a crash during relocatable linking when an SHT_GROUP section has SHF_MERGE set. Before, shouldMerge() could classify the group as a MergeInputSection, losing information needed to update the group section and symbol indices. Fix excludes SHT_GROUP sections from merge classification

AI assisted with understanding and implementing the issue, writing regression tests, and reviewing the changes
Fixes #235240

@llvmorg-github-actions

llvmorg-github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-elf

Author: Orest Halenza (OgShadoww)

Changes

Fix a crash during relocatable linking when an SHT_GROUP section has SHF_MERGE set. Before, shouldMerge() could classify the group as a MergeInputSection, losing information needed to update the group section and symbol indices. Fix excludes SHT_GROUP sections from merge classification

AI assisted with understanding and implementing the issue, writing regression tests, and reviewing the changes
Fixes #235240


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

2 Files Affected:

  • (modified) lld/ELF/InputFiles.cpp (+4)
  • (added) lld/test/ELF/sht-group-merge.test (+48)
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index b412fd566f93e..678beab20f4b7 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -766,6 +766,10 @@ StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
 
 template <class ELFT>
 bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) {
+  // Keep the original group section so copyShtGroup can remap its members.
+  if (sec.sh_type == SHT_GROUP)
+    return false;
+
   // On a regular link we don't merge sections if -O0 (default is -O1). This
   // sometimes makes the linker significantly faster, although the output will
   // be bigger.
diff --git a/lld/test/ELF/sht-group-merge.test b/lld/test/ELF/sht-group-merge.test
new file mode 100644
index 0000000000000..7322ff2188082
--- /dev/null
+++ b/lld/test/ELF/sht-group-merge.test
@@ -0,0 +1,48 @@
+# REQUIRES: x86
+## Remap the group signature and member indices even when the group has merge
+## flags. The two identical constants in the member section should coalesce.
+
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld -r %t.o -o %t.ro
+# RUN: llvm-readelf -Sgs %t.ro | FileCheck %s --check-prefixes=CHECK,PLAIN
+
+# RUN: yaml2obj %s -DFLAGS="[SHF_MERGE, SHF_STRINGS]" -o %t.o
+# RUN: ld.lld -r %t.o -o %t.ro
+# RUN: llvm-readelf -Sgs %t.ro | FileCheck %s --check-prefixes=CHECK,STRINGS
+
+# PLAIN:      .group GROUP {{.*}} 000008 04    [[#SYMTAB:]] [[#SIG:]]
+# STRINGS:    .group GROUP {{.*}} 000008 04 MS [[#SYMTAB:]] [[#SIG:]]
+# CHECK:      [ [[#MEMBER:]]] .rodata.cst4 PROGBITS {{.*}} 000004 04 AMG
+# CHECK:      [ [[#SYMTAB]]] .symtab SYMTAB
+# CHECK:      [[#SIG]]: {{.*}} GLOBAL DEFAULT [[#MEMBER]] sig
+# CHECK:      COMDAT group section {{.*}} `.group' [sig] contains 1 sections:
+# CHECK-NEXT: [Index] Name
+# CHECK-NEXT: [ [[#MEMBER]]] .rodata.cst4
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+## Put .symtab first so both sh_link and the member section index change.
+  - Name: .symtab
+    Type: SHT_SYMTAB
+  - Name:    .group
+    Type:    SHT_GROUP
+    Flags:   [[FLAGS=<none>]]
+    Link:    .symtab
+    Info:    sig
+    Members:
+      - SectionOrType: GRP_COMDAT
+      - SectionOrType: .rodata.cst4
+  - Name:    .rodata.cst4
+    Type:    SHT_PROGBITS
+    Flags:   [ SHF_ALLOC, SHF_MERGE, SHF_GROUP ]
+    EntSize: 4
+    Content: "1122334411223344"
+Symbols:
+  - Name:    sig
+    Section: .rodata.cst4
+    Binding: STB_GLOBAL

@MaskRay MaskRay closed this Nov 7, 2026
@MaskRay MaskRay added the extractive Used for PRs which are extractive according to our developer policy label Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extractive Used for PRs which are extractive according to our developer policy lld:ELF lld

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[lld][ELF] ld.lld -r crashes on an SHT_GROUP section that has SHF_MERGE set (group becomes a MergeInputSection)

2 participants