[llvm] bc6e10c - [ELF][llvm-objcopy] Reject duplicate SHT_SYMTAB sections
James Henderson via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 17 09:59:39 PST 2023
Author: Moshe Berman
Date: 2023-02-17T17:59:20Z
New Revision: bc6e10c9efca78cda350b6705de27728d142c0b0
URL: https://github.com/llvm/llvm-project/commit/bc6e10c9efca78cda350b6705de27728d142c0b0
DIFF: https://github.com/llvm/llvm-project/commit/bc6e10c9efca78cda350b6705de27728d142c0b0.diff
LOG: [ELF][llvm-objcopy] Reject duplicate SHT_SYMTAB sections
The gABI prohibits multiple SH_SYMTAB sections. As a result,
llvm-objcopy was crashing in SymbolTableSection::removeSymbols(). This
patch fixes the issue by emitting an error if multiple SH_SYMTAB
sections are encountered when building an ELF object.
Fixes: https://github.com/llvm/llvm-project/issues/60448
Differential Revision: https://reviews.llvm.org/D143508
Added:
llvm/test/tools/llvm-objcopy/ELF/multiple-symtab.test
Modified:
llvm/lib/ObjCopy/ELF/ELFObject.cpp
Removed:
################################################################################
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index ea6dadabace6d..25bfa1a1dfd7a 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -1704,6 +1704,10 @@ Expected<SectionBase &> ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
else
return Data.takeError();
case SHT_SYMTAB: {
+ // Multiple SHT_SYMTAB sections are forbidden by the ELF gABI.
+ if (Obj.SymbolTable != nullptr)
+ return createStringError(llvm::errc::invalid_argument,
+ "found multiple SHT_SYMTAB sections");
auto &SymTab = Obj.addSection<SymbolTableSection>();
Obj.SymbolTable = &SymTab;
return SymTab;
diff --git a/llvm/test/tools/llvm-objcopy/ELF/multiple-symtab.test b/llvm/test/tools/llvm-objcopy/ELF/multiple-symtab.test
new file mode 100644
index 0000000000000..964775edc9eb5
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/multiple-symtab.test
@@ -0,0 +1,20 @@
+## According to the ELF gABI, "Currently, an object file may have only one
+## section of each type [SHT_SYMTAB and SHT_DYNSYM], but this restriction may be
+## relaxed in the future."
+## This test shows that we emit an error if we encounter multiple SHT_SYMTAB
+## sections.
+# RUN: yaml2obj %s -o %t
+# RUN: not llvm-objcopy %t /dev/null 2>&1 | FileCheck %s
+
+# CHECK: error: found multiple SHT_SYMTAB sections
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+Sections:
+ - Name: .symtab
+ Type: SHT_SYMTAB
+ - Name: .symtab2
+ Type: SHT_SYMTAB
More information about the llvm-commits
mailing list