[llvm] [llvm-objcopy] Preserve symbols with st_shndx in [SHN_LOOS, SHN_HIOS] (PR #228665)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 2 23:15:13 PDT 2026
https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/228665
llvm-objcopy unnecessarily rejects such symbols. Known OS-specific
values, e.g. SHN_LIVEPATCH (0xff20) used by Linux livepatch modules for
.klp.sym.* symbols, do not refer to a section, so removing or reordering
sections does not affect them, and they can be copied without knowing
their meaning, as GNU objcopy and eu-strip do.
Fixes #196381
LLM-aided
>From edca415862b5290c9f810e7e44cd8b61f3bd3512 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Fri, 2 Oct 2026 22:26:38 -0700
Subject: [PATCH] [llvm-objcopy] Preserve symbols with st_shndx in [SHN_LOOS,
SHN_HIOS]
llvm-objcopy unnecessarily rejects such symbols. Known OS-specific
values, e.g. SHN_LIVEPATCH (0xff20) used by Linux livepatch modules for
.klp.sym.* symbols, do not refer to a section, so removing or reordering
sections does not affect them, and they can be copied without knowing
their meaning, as GNU objcopy and eu-strip do.
Fixes #196381
LLM-aided
---
llvm/lib/ObjCopy/ELF/ELFObject.cpp | 5 +++++
llvm/test/tools/llvm-objcopy/ELF/reserved-shndx.test | 11 ++++++++---
2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index 3fbc7f00e61e8..e9de205dab65f 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -653,6 +653,11 @@ static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
return true;
}
+ // Known OS-specific values, e.g. Linux SHN_LIVEPATCH, do not refer to a
+ // section, so they can be preserved without knowing their meaning.
+ if (Index >= SHN_LOOS && Index <= SHN_HIOS)
+ return true;
+
if (Machine == EM_AMDGPU) {
return Index == SHN_AMDGPU_LDS;
}
diff --git a/llvm/test/tools/llvm-objcopy/ELF/reserved-shndx.test b/llvm/test/tools/llvm-objcopy/ELF/reserved-shndx.test
index ba9f2ae3c756d..953f7a352b837 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/reserved-shndx.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/reserved-shndx.test
@@ -1,3 +1,11 @@
+## OS-specific values are preserved.
+# RUN: yaml2obj -DINDEX=SHN_LOOS %s -o %t.os
+# RUN: llvm-objcopy %t.os %t1
+# RUN: llvm-readelf -s %t1 | FileCheck %s --check-prefix=KEEP -DNDX='OS[0xff20]'
+# RUN: yaml2obj -DTYPE=ET_EXEC -DINDEX=SHN_HIOS %s -o %t.os
+# RUN: llvm-objcopy %t.os %t1
+# RUN: llvm-readelf -s %t1 | FileCheck %s --check-prefix=KEEP -DNDX='OS[0xff3f]'
+
## Processor-specific values supported for e_machine are preserved.
# RUN: yaml2obj -DMACHINE=EM_MIPS -DINDEX=SHN_MIPS_SCOMMON %s -o %t.mips
# RUN: llvm-objcopy %t.mips %t1
@@ -12,9 +20,6 @@
# RUN: not llvm-objcopy %t2 /dev/null 2>&1 | FileCheck %s --check-prefix=ERR -DINDEX=65283
# RUN: yaml2obj -DTYPE=ET_EXEC -DINDEX=SHN_HIPROC %s -o %t2
# RUN: not llvm-objcopy %t2 /dev/null 2>&1 | FileCheck %s --check-prefix=ERR -DINDEX=65311
-## [SHN_LOOS, SHN_HIOS] is OS-specific.
-# RUN: yaml2obj -DINDEX=SHN_LOOS %s -o %t2
-# RUN: not llvm-objcopy %t2 /dev/null 2>&1 | FileCheck %s --check-prefix=ERR -DINDEX=65312
## 0xff40 is in (SHN_HIOS, SHN_ABS), which has no defined meaning.
# RUN: yaml2obj -DINDEX=0xff40 %s -o %t2
# RUN: not llvm-objcopy %t2 /dev/null 2>&1 | FileCheck %s --check-prefix=ERR -DINDEX=65344
More information about the llvm-commits
mailing list