[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
This commit is contained in:
Moshe Berman 2023-02-17 17:18:14 +00:00 committed by James Henderson
parent 998ad085e8
commit bc6e10c9ef
2 changed files with 24 additions and 0 deletions

View File

@ -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;

View File

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