[lld] [ELF] Don't assume a SHF_LINK_ORDER section is a plain InputSection (PR #219816)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 30 08:43:26 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld
Author: Matt Turner (mattst88)
<details>
<summary>Changes</summary>
`initializeSections()` records an SHF_LINK_ORDER section in its linked-to
section's `dependentSections`, reaching it through `cast<InputSection>`. Not
every SHF_LINK_ORDER section is a plain `InputSection`, though: an SHT_STRTAB
one has no `InputSectionBase` at all, and a mergeable or `.eh_frame` one has a
`MergeInputSection` or an `EhInputSection`. All three asserted, whether the
linked-to section was discarded or kept.
Skip them; there is no dependency to record either way. A mergeable section
consequently keeps SHF_LINK_ORDER in the output but loses its sh_link, and is
collected by `--gc-sections`, which never treats an SHF_LINK_ORDER section as a
root. Demoting it to a plain `InputSection`, as the relocation target path
above does, would record the dependency but would also give up merging, which
nothing here requires. Happy to switch if reviewers prefer that.
A second, independent fix in the same function — discarding an SHF_LINK_ORDER
section whose linked-to section is already discarded — will follow as a
separate PR stacked on this one.
Assisted-by: Claude Code
---
Full diff: https://github.com/llvm/llvm-project/pull/219816.diff
2 Files Affected:
- (modified) lld/ELF/InputFiles.cpp (+8-1)
- (added) lld/test/ELF/linkorder-nonregular.test (+45)
``````````diff
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 23649d6200719..9f24505ad71a4 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -951,9 +951,16 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
continue;
}
+ // Not every SHF_LINK_ORDER section is a plain InputSection: an SHT_STRTAB
+ // one has no InputSectionBase at all, and a mergeable or .eh_frame one has
+ // a MergeInputSection or an EhInputSection. None can be recorded in
+ // dependentSections.
+ auto *isec = dyn_cast_or_null<InputSection>(this->sections[i]);
+ if (!isec)
+ continue;
+
// A SHF_LINK_ORDER section is discarded if its linked-to section is
// discarded.
- InputSection *isec = cast<InputSection>(this->sections[i]);
linkSec->dependentSections.push_back(isec);
if (!isa<InputSection>(linkSec))
ErrAlways(ctx)
diff --git a/lld/test/ELF/linkorder-nonregular.test b/lld/test/ELF/linkorder-nonregular.test
new file mode 100644
index 0000000000000..1952181f3f56c
--- /dev/null
+++ b/lld/test/ELF/linkorder-nonregular.test
@@ -0,0 +1,45 @@
+# REQUIRES: x86
+## Not every SHF_LINK_ORDER section is a plain InputSection: an SHT_STRTAB one
+## has no InputSectionBase at all, and a mergeable or .eh_frame one has a
+## MergeInputSection or an EhInputSection. None can be recorded as a dependent
+## section of the linked-to section, and all used to assert in
+## cast<InputSection>.
+
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld %t.o -o %t -e 0
+# RUN: llvm-readelf -S %t | FileCheck %s --implicit-check-not='{{ }}lo_strtab '
+
+## The mergeable and .eh_frame sections survive; SHT_STRTAB is not a type lld
+## emits. lo_merge keeps SHF_LINK_ORDER but, having no recorded dependency,
+## loses the sh_link to .text; GNU ld keeps it.
+# CHECK-DAG: {{ }}lo_merge PROGBITS
+# CHECK-DAG: {{ }}.eh_frame PROGBITS
+# CHECK-DAG: {{ }}.text PROGBITS
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Size: 1
+ - Name: lo_strtab
+ Type: SHT_STRTAB
+ Flags: [ SHF_ALLOC, SHF_LINK_ORDER ]
+ Link: .text
+ Size: 8
+ - Name: lo_merge
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_MERGE, SHF_LINK_ORDER ]
+ Link: .text
+ EntSize: 4
+ Size: 8
+ - Name: .eh_frame
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_LINK_ORDER ]
+ Link: .text
+ Size: 0
``````````
</details>
https://github.com/llvm/llvm-project/pull/219816
More information about the llvm-commits
mailing list