Index: test/tools/llvm-objcopy/ELF/strip-section-err.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/ELF/strip-section-err.test @@ -0,0 +1,42 @@ +## Check we cannot remove the section .data because it +## is used by relocations contained in the object. + +# RUN: yaml2obj %s > %t1 +# RUN: not llvm-objcopy -R .data %t1 2>&1 | FileCheck %s +# CHECK: error: Section .data cannot be removed because of symbol 'foo' used by the relocation R_X86_64_GOTPCREL[1] from 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 @@ -275,7 +275,8 @@ virtual void finalize(); // Remove references to these sections. The list of sections must be sorted. virtual Error - removeSectionReferences(function_ref ToRemove); + removeSectionReferences(function_ref ToRemove, + const Object &Obj); virtual Error removeSymbols(function_ref ToRemove); virtual void accept(SectionVisitor &Visitor) const = 0; virtual void accept(MutableSectionVisitor &Visitor) = 0; @@ -336,8 +337,9 @@ void accept(SectionVisitor &Visitor) const override; void accept(MutableSectionVisitor &Visitor) override; - Error removeSectionReferences( - function_ref ToRemove) override; + Error + removeSectionReferences(function_ref ToRemove, + const Object &Obj) override; void initialize(SectionTableRef SecTable) override; void finalize() override; }; @@ -524,8 +526,9 @@ Symbol *getSymbolByIndex(uint32_t Index); void updateSymbols(function_ref Callable); - Error removeSectionReferences( - function_ref ToRemove) override; + Error + removeSectionReferences(function_ref ToRemove, + const Object &Obj) override; void initialize(SectionTableRef SecTable) override; void finalize() override; void accept(SectionVisitor &Visitor) const override; @@ -570,15 +573,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 +595,9 @@ void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } void accept(SectionVisitor &Visitor) const override; void accept(MutableSectionVisitor &Visitor) override; + Error + removeSectionReferences(function_ref ToRemove, + const Object &Obj) 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 @@ -51,7 +51,7 @@ } Error SectionBase::removeSectionReferences( - function_ref ToRemove) { + function_ref ToRemove, const Object &Obj) { return Error::success(); } @@ -435,7 +435,7 @@ } Error SymbolTableSection::removeSectionReferences( - function_ref ToRemove) { + function_ref ToRemove, const Object &Obj) { if (ToRemove(SectionIndexTable)) SectionIndexTable = nullptr; if (ToRemove(SymbolNames)) @@ -546,14 +546,30 @@ Visitor.visit(*this); } -template -Error RelocSectionWithSymtabBase::removeSectionReferences( - function_ref ToRemove) { +Error RelocationSection::removeSectionReferences( + function_ref ToRemove, const Object &Obj) { 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 (size_t I = 0; I < Relocations.size(); ++I) { + const Relocation &R = Relocations[I]; + if (!R.RelocSymbol->DefinedIn || !ToRemove(R.RelocSymbol->DefinedIn)) + continue; + + StringRef RelName = getELFRelocationTypeName(Obj.Machine, R.Type); + const std::string &SymSecName = R.RelocSymbol->DefinedIn->Name; + + return createStringError( + llvm::errc::invalid_argument, + "Section %s cannot be removed because of symbol '%s' " + "used by the relocation %s[%zu] from section %s.", + SymSecName.data(), R.RelocSymbol->Name.c_str(), RelName.str().c_str(), + I + 1, this->Name.data()); + } + return Error::success(); } @@ -649,7 +665,7 @@ } Error Section::removeSectionReferences( - function_ref ToRemove) { + function_ref ToRemove, const Object &Obj) { if (ToRemove(LinkSection)) return createStringError(llvm::errc::invalid_argument, "Section %s cannot be removed because it is " @@ -1361,12 +1377,21 @@ 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 + // live 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(); - })) + }, + *this)) return E; + } + // Now finally get rid of them all togethor. Sections.erase(Iter, std::end(Sections)); return Error::success();