[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:19:25 PST 2026
https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/240736
Backport 4423d417a23c193b234c2a8c6fa566e2d60c0ab9
Requested by: @MaskRay
>From 23898131cc3f4272f2999533180ed4518d46fa59 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Mon, 9 Nov 2026 23:04:45 -0800
Subject: [PATCH] [ELF] includeInSymtab: Fix crash for SHF_MERGE section symbol
with out-of-range addend (#240732)
includeInSymtab passes a section symbol's st_value, which is not
range-checked, to getSectionPiece. A value at or past the end of the
SHF_MERGE section causes an assertion failure or crash.
A local section symbol is not copied to .symtab, and a non-local one
gets the "offset is outside the section" error from getSymVA.
Fix #235203
(cherry picked from commit 4423d417a23c193b234c2a8c6fa566e2d60c0ab9)
---
lld/ELF/Writer.cpp | 2 +-
lld/test/ELF/merge-piece-oob.s | 9 +++++++++
2 files changed, 10 insertions(+), 1 deletion(-)
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
More information about the llvm-branch-commits
mailing list