Index: test/tools/llvm-objcopy/ELF/strip-section-err.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/ELF/strip-section-err.test @@ -0,0 +1,43 @@ +# RUN: yaml2obj %s > %t +# RUN: not llvm-objcopy -R .data %t 2>&1 | FileCheck %s + +## Check we cannot remove the section .data because it +## is referenced by relocations contained in the object. + +# CHECK: error: Section .data cannot be removed because it is referenced by the relocation section .rela.text. + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + AddressAlign: 0x0000000000000004 + Content: E800000000 + - Name: .rela.text + Type: SHT_RELA + Link: .symtab + AddressAlign: 0x0000000000000008 + EntSize: 0x0000000000000018 + Info: .text + Relocations: + - Offset: 0x0000000000000001 + Symbol: foo + Type: R_X86_64_GOTPCREL + Addend: -4 + - Name: .data + Type: SHT_PROGBITS + Flags: [ SHF_WRITE, SHF_ALLOC ] + AddressAlign: 0x0000000000000001 + Content: '0102' +Symbols: + Local: + - Name: foo + Section: .data + Value: 0x0000000000000001 +DynamicSymbols: {} +... Index: tools/llvm-objcopy/ELF/Object.h =================================================================== --- tools/llvm-objcopy/ELF/Object.h +++ tools/llvm-objcopy/ELF/Object.h @@ -570,15 +570,14 @@ // that code between the two symbol table types. template class RelocSectionWithSymtabBase : public RelocationSectionBase { - SymTabType *Symbols = nullptr; void setSymTab(SymTabType *SymTab) { Symbols = SymTab; } protected: RelocSectionWithSymtabBase() = default; + SymTabType *Symbols = nullptr; + public: - Error removeSectionReferences( - function_ref ToRemove) override; void initialize(SectionTableRef SecTable) override; void finalize() override; }; @@ -593,6 +592,8 @@ void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } void accept(SectionVisitor &Visitor) const override; void accept(MutableSectionVisitor &Visitor) override; + Error removeSectionReferences( + function_ref ToRemove) override; Error removeSymbols(function_ref ToRemove) override; void markSymbols() override; Index: tools/llvm-objcopy/ELF/Object.cpp =================================================================== --- tools/llvm-objcopy/ELF/Object.cpp +++ tools/llvm-objcopy/ELF/Object.cpp @@ -546,14 +546,23 @@ Visitor.visit(*this); } -template -Error RelocSectionWithSymtabBase::removeSectionReferences( +Error RelocationSection::removeSectionReferences( function_ref ToRemove) { if (ToRemove(Symbols)) return createStringError(llvm::errc::invalid_argument, "Symbol table %s cannot be removed because it is " "referenced by the relocation section %s.", Symbols->Name.data(), this->Name.data()); + + for (const Relocation &R : Relocations) + if (R.RelocSymbol && R.RelocSymbol->DefinedIn && + ToRemove(R.RelocSymbol->DefinedIn)) + return createStringError(llvm::errc::invalid_argument, + "Section %s cannot be removed because it is " + "referenced by the relocation section %s.", + R.RelocSymbol->DefinedIn->Name.data(), + this->Name.data()); + return Error::success(); } @@ -1361,12 +1370,20 @@ Segment->removeSection(RemoveSec.get()); RemoveSections.insert(RemoveSec.get()); } - for (auto &KeepSec : make_range(std::begin(Sections), Iter)) + + // For each section that remains alive, we want to remove the dead references. + // This either might update the content of the section (e.g.remove symbols + // from symbol table that belongs to removed section) or trigger an error if + // alive section critically depends on a section being removed somehow + // (e.g.removed section is referenced by a relocation). + for (auto &KeepSec : make_range(std::begin(Sections), Iter)) { if (Error E = KeepSec->removeSectionReferences( [&RemoveSections](const SectionBase *Sec) { return RemoveSections.find(Sec) != RemoveSections.end(); })) return E; + } + // Now finally get rid of them all togethor. Sections.erase(Iter, std::end(Sections)); return Error::success();