[lld] [lld][ELF] Fix crash when relocations proceed relocated section (PR #156354)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 16 00:10:49 PDT 2025


https://github.com/mykouHW updated https://github.com/llvm/llvm-project/pull/156354

>From 3cc58a30bbdff74179b45bf0001943c200947186 Mon Sep 17 00:00:00 2001
From: koumeiyuan <koumeiyuan at huawei.com>
Date: Fri, 29 Aug 2025 09:07:10 +0000
Subject: [PATCH] [lld][ELF] Fix crash when relocations proceed relocated
 section

Fix the error generated during the linking process when the relocation section is placed before the relocated section and the relocated section is not defined in the linker script.
---
 lld/ELF/LinkerScript.cpp                      | 12 ++++--
 .../ELF/linkerscript/orphan-sections-init.s   | 37 +++++++++++++++++++
 2 files changed, 46 insertions(+), 3 deletions(-)
 create mode 100644 lld/test/ELF/linkerscript/orphan-sections-init.s

diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 921128dae2bdb..4d66408a6510e 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -1037,10 +1037,16 @@ void LinkerScript::addOrphanSections() {
     if (ctx.arg.relocatable && (isec->flags & SHF_LINK_ORDER))
       continue;
 
-    if (auto *sec = dyn_cast<InputSection>(isec))
-      if (InputSectionBase *rel = sec->getRelocatedSection())
-        if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
+    if (auto *sec = dyn_cast<InputSection>(isec)) {
+      if (InputSectionBase *relocated = sec->getRelocatedSection()) {
+        // Ensure creation of OutputSection for relocated section before
+        // relocation section
+        if (auto *relIS = dyn_cast_or_null<InputSectionBase>(relocated))
           add(relIS);
+        if (auto *relIS = dyn_cast_or_null<InputSectionBase>(relocated->parent))
+          add(relIS);
+      }
+    }
     add(isec);
     if (ctx.arg.relocatable)
       for (InputSectionBase *depSec : isec->dependentSections)
diff --git a/lld/test/ELF/linkerscript/orphan-sections-init.s b/lld/test/ELF/linkerscript/orphan-sections-init.s
new file mode 100644
index 0000000000000..fa70d29d41130
--- /dev/null
+++ b/lld/test/ELF/linkerscript/orphan-sections-init.s
@@ -0,0 +1,37 @@
+# REQUIRES: x86
+# RUN: rm -rf %t && mkdir -p %t
+# RUN: split-file %s %t && cd %t
+
+## Test that lld's orphan section placement can handle a relocatable link where
+## the relocation section is seen before the relocated section. 
+
+## Create a relocatable object with the relocations before the relocated section
+# RUN: llvm-mc -filetype=obj -triple=x86_64 foo.s -o foo.o
+# RUN: ld.lld -r  foo.o -T script.ld -o foo_mc.o
+
+## Rename the sections to make them orphans
+# RUN: llvm-objcopy \
+# RUN: --rename-section .text=.com.text \
+# RUN: --rename-section .rela.text=.rela.com.text \
+# RUN: foo_mc.o foo_mc.o
+
+# RUN: ld.lld -r foo_mc.o  -T script.ld -o foo_mc_after.o
+# RUN:  llvm-readelf -S foo_mc_after.o | FileCheck %s
+# CHECK:       .com.text         PROGBITS        0000000000000000 000040 000007 00  AX  0   0 16
+# CHECK-NEXT:  .rela.com.text    RELA            0000000000000000 000048 000018 18   I  4   1  8
+
+#--- foo.s
+  .text
+  .globl	foo
+  .p2align	4
+  .type	foo, at function
+foo:
+  mov $bar, %rax
+
+#--- script.ld
+SECTIONS
+{
+  .rela.text    0 : { *(.rela.text) }
+  .text         0 : { *(.text) }
+}
+



More information about the llvm-commits mailing list