Index: llvm/test/tools/llvm-objcopy/ELF/group.test =================================================================== --- llvm/test/tools/llvm-objcopy/ELF/group.test +++ llvm/test/tools/llvm-objcopy/ELF/group.test @@ -1,6 +1,6 @@ # RUN: yaml2obj %s -o %t -# RUN: llvm-objcopy --remove-section=.text.bar %t %t2 -# RUN: llvm-readobj --elf-section-groups %t2 | FileCheck %s +# RUN: llvm-objcopy --remove-section=.text.bar %t %t1 +# RUN: llvm-readobj --elf-section-groups %t1 | FileCheck %s # In this test the section .text.bar is getting removed, as a result, # the indices of the sections which go after .text.bar will change, @@ -54,3 +54,25 @@ Section: .text.foo Size: 0x0000000000000000 Binding: STB_WEAK + +## This checks that tool will emit an error when trying to remove the symbol +## table when we have the a group section linked with symtab. +# RUN: not llvm-objcopy -R .symtab %t %t2 2>&1 | FileCheck %s --check-prefix=ERR -DINPUT=%t + +# ERR: error: '[[INPUT]]': section '.symtab' cannot be removed because it is referenced by the group section '.group' + +## The '.symtab' section could be removed using --allow-broken-links option. +# RUN: llvm-objcopy --allow-broken-links -R .symtab %t %t3 +# RUN: llvm-readelf --sections %t3 | FileCheck %s --check-prefix=SECTIONS + +# SECTIONS: There are 6 section headers +# SECTIONS: Name +# SECTIONS-NEXT: NULL +# SECTIONS-NEXT: .group +# SECTIONS-NEXT: .text.bar +# SECTIONS-NEXT: .text.foo +# SECTIONS-NEXT: .strtab +# SECTIONS-NEXT: .shstrtab + +## Nothing will change when removing symtab again; tool should exit with return code 0. +# RUN: llvm-objcopy --allow-broken-links -R .symtab %t %t3 Index: llvm/tools/llvm-objcopy/ELF/Object.cpp =================================================================== --- llvm/tools/llvm-objcopy/ELF/Object.cpp +++ llvm/tools/llvm-objcopy/ELF/Object.cpp @@ -972,6 +972,13 @@ Error GroupSection::removeSectionReferences( bool AllowBrokenLinks, function_ref ToRemove) { + if (ToRemove(SymTab) && !AllowBrokenLinks) { + return createStringError( + llvm::errc::invalid_argument, + "section '.symtab' cannot be removed because it is " + "referenced by the group section '%s'", + this->Name.data()); + } llvm::erase_if(GroupMembers, ToRemove); return Error::success(); }