[llvm-branch-commits] [lld] 31207bb - [ELF] includeInSymtab: Fix crash for SHF_MERGE section symbol with out-of-range addend (#240732)

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 16 04:46:44 PST 2026


Author: Fangrui Song
Date: 2026-11-16T13:45:53+01:00
New Revision: 31207bbd011aa77273f90d57f265e9a4e91cb85b

URL: https://github.com/llvm/llvm-project/commit/31207bbd011aa77273f90d57f265e9a4e91cb85b
DIFF: https://github.com/llvm/llvm-project/commit/31207bbd011aa77273f90d57f265e9a4e91cb85b.diff

LOG: [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)

Added: 
    

Modified: 
    lld/ELF/Writer.cpp
    lld/test/ELF/merge-piece-oob.s

Removed: 
    


################################################################################
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