[llvm-branch-commits] [lld] release/23.x: [ELF] includeInSymtab: Fix crash for SHF_MERGE section symbol with out-of-range addend (#240732) (PR #240736)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 9 23:20:03 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld-elf

Author: llvmbot

<details>
<summary>Changes</summary>

Backport 4423d417a23c193b234c2a8c6fa566e2d60c0ab9

Requested by: @<!-- -->MaskRay

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


2 Files Affected:

- (modified) lld/ELF/Writer.cpp (+1-1) 
- (modified) lld/test/ELF/merge-piece-oob.s (+9) 


``````````diff
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index e88ef20eea65bc..2fb62c0b4ea443 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -436,7 +436,7 @@ bool elf::includeInSymtab(Ctx &ctx, const Symbol &b) {
     assert(sec->isLive());
 
     if (auto *s = dyn_cast<MergeInputSection>(sec))
-      return s->getSectionPiece(d->value).live;
+      return d->isSection() || s->getSectionPiece(d->value).live;
     return true;
   }
   return b.hasFlag(USED) || !ctx.arg.gcSections;
diff --git a/lld/test/ELF/merge-piece-oob.s b/lld/test/ELF/merge-piece-oob.s
index 8ce5cc46d69606..2f4911685ac075 100644
--- a/lld/test/ELF/merge-piece-oob.s
+++ b/lld/test/ELF/merge-piece-oob.s
@@ -24,6 +24,15 @@
 # RUN: not ld.lld %t.err.o -o /dev/null --gc-sections 2>&1 | FileCheck %s --check-prefix=GC
 # GC: error: relocation refers to a discarded section: .foo
 
+## A local section symbol with an out-of-bounds st_value is not copied to
+## .symtab, so its value is unused. A non-local one is checked.
+# RUN: echo '.section .foo,"aM", at progbits,8; .quad 0' | llvm-mc -filetype=obj -triple=x86_64 - -o %t.sec.o
+# RUN: llvm-objcopy --add-symbol=.foo=.foo:9,section,local --add-symbol=.foo=.foo:0x200000000,section,local %t.sec.o %t.sec1.o
+# RUN: ld.lld %t.sec1.o -shared -o /dev/null
+# RUN: llvm-objcopy --add-symbol=.foo=.foo:9,section %t.sec.o %t.sec2.o
+# RUN: not ld.lld %t.sec2.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=SECSYM --implicit-check-not=error:
+# SECSYM: error: {{.*}}:(.foo): offset 0x9 is outside the section
+
 # RUN: ld.lld -r %t.o -o %t.ro
 # RUN: llvm-readelf -s -r %t.ro | FileCheck %s --check-prefix=RELOC
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/240736


More information about the llvm-branch-commits mailing list