Repository navigation
Conversation
|
@llvm/pr-subscribers-lld @llvm/pr-subscribers-lld-elf Author: Orest Halenza (OgShadoww) ChangesFix 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 Full diff: https://github.com/llvm/llvm-project/pull/240069.diff 2 Files Affected:
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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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