Index: test/tools/llvm-objcopy/strip-symbol-group.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/strip-symbol-group.test @@ -0,0 +1,31 @@ +# RUN: yaml2obj %s > %t +# RUN: not llvm-objcopy -N foo %t %t2 2>&1 | FileCheck %s + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .group + Type: SHT_GROUP + Link: .symtab + AddressAlign: 0x0000000000000004 + Info: foo + Members: + - SectionOrType: GRP_COMDAT + - SectionOrType: .text + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x1000 + AddressAlign: 0x0000000000000010 + Size: 64 +Symbols: + Local: + - Name: foo + Type: STT_FUNC + Section: .text + +#CHECK: {{.*}}llvm-objcopy: Symbol foo cannot be removed because it is referenced by the section .group[1]. Index: test/tools/llvm-objcopy/strip-symbol-reloc.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/strip-symbol-reloc.test @@ -0,0 +1,44 @@ +# RUN: yaml2obj %s > %t +# RUN: not llvm-objcopy -N Local %t %t2 2>&1 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x1000 + AddressAlign: 0x0000000000000010 + Size: 64 + - Name: .rel.text + Type: SHT_REL + Info: .text + Relocations: + - Offset: 0x1000 + Symbol: Local + Type: R_X86_64_PC32 +Symbols: + Local: + - Name: Local + Type: STT_FUNC + Section: .text + Value: 0x1000 + Size: 8 + Weak: + - Name: Weak + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1008 + Global: + - Name: Global + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1010 + +#CHECK: {{.*}}llvm-objcopy: not stripping symbol `Local' because it is named in a relocation. Index: test/tools/llvm-objcopy/strip-symbol.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/strip-symbol.test @@ -0,0 +1,57 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --strip-symbol Global -N Weak %t %t2 +# RUN: llvm-readobj -symbols -sections %t2 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x1000 + AddressAlign: 0x0000000000000010 + Size: 64 +Symbols: + Local: + - Name: Local + Type: STT_FUNC + Section: .text + Value: 0x1000 + Size: 8 + Weak: + - Name: Weak + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1008 + Global: + - Name: Global + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1010 + +#CHECK: Symbols [ +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: +#CHECK-NEXT: Value: 0x0 +#CHECK-NEXT: Size: 0 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: None +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: Undefined +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: Local +#CHECK-NEXT: Value: 0x1000 +#CHECK-NEXT: Size: 8 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: Function +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .text +#CHECK-NEXT: } +#CHECK-NEXT:] Index: tools/llvm-objcopy/Object.h =================================================================== --- tools/llvm-objcopy/Object.h +++ tools/llvm-objcopy/Object.h @@ -38,6 +38,7 @@ class GroupSection; class Segment; class Object; +struct Symbol; class SectionTableRef { MutableArrayRef> Sections; @@ -209,6 +210,7 @@ virtual void initialize(SectionTableRef SecTable); virtual void finalize(); virtual void removeSectionReferences(const SectionBase *Sec); + virtual void removeSymbols(function_ref ToRemove); virtual void accept(SectionVisitor &Visitor) const = 0; }; @@ -366,12 +368,12 @@ const SectionBase *getStrTab() const { return SymbolNames; } const Symbol *getSymbolByIndex(uint32_t Index) const; void updateSymbols(function_ref Callable); - void removeSymbols(function_ref ToRemove); void removeSectionReferences(const SectionBase *Sec) override; void initialize(SectionTableRef SecTable) override; void finalize() override; void accept(SectionVisitor &Visitor) const override; + void removeSymbols(function_ref ToRemove) override; static bool classof(const SectionBase *S) { return S->Type == ELF::SHT_SYMTAB; @@ -432,6 +434,7 @@ public: void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } void accept(SectionVisitor &Visitor) const override; + void removeSymbols(function_ref ToRemove) override; static bool classof(const SectionBase *S) { if (S->Flags & ELF::SHF_ALLOC) @@ -465,6 +468,7 @@ void initialize(SectionTableRef SecTable) override{}; void accept(SectionVisitor &) const override; void finalize() override; + void removeSymbols(function_ref ToRemove) override; static bool classof(const SectionBase *S) { return S->Type == ELF::SHT_GROUP; @@ -619,6 +623,7 @@ ConstRange segments() const { return make_pointee_range(Segments); } void removeSections(std::function ToRemove); + void removeSymbols(function_ref ToRemove); template T &addSection(Ts &&... Args) { auto Sec = llvm::make_unique(std::forward(Args)...); auto Ptr = Sec.get(); Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -47,6 +47,7 @@ } void SectionBase::removeSectionReferences(const SectionBase *Sec) {} +void SectionBase::removeSymbols(function_ref ToRemove) {} void SectionBase::initialize(SectionTableRef SecTable) {} void SectionBase::finalize() {} @@ -206,7 +207,8 @@ assignIndices(); } -void SymbolTableSection::removeSymbols(function_ref ToRemove) { +void SymbolTableSection::removeSymbols( + function_ref ToRemove) { Symbols.erase( std::remove_if(std::begin(Symbols), std::end(Symbols), [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }), @@ -342,6 +344,14 @@ Visitor.visit(*this); } +void RelocationSection::removeSymbols( + function_ref ToRemove) { + for (const Relocation &Reloc : Relocations) + if (ToRemove(*Reloc.RelocSymbol)) + error("not stripping symbol `" + Reloc.RelocSymbol->Name + + "' because it is named in a relocation"); +} + void SectionWriter::visit(const DynamicRelocationSection &Sec) { std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), Out.getBufferStart() + Sec.Offset); @@ -365,6 +375,15 @@ this->Link = SymTab->Index; } +void GroupSection::removeSymbols(function_ref ToRemove) { + if (ToRemove(*Sym)) { + error("Symbol " + Sym->Name + + " cannot be removed because it is " + "referenced by the section " + + this->Name + "[" + std::to_string(this->Index) + "]"); + } +} + void Section::initialize(SectionTableRef SecTable) { if (Link != ELF::SHN_UNDEF) LinkSection = @@ -904,6 +923,14 @@ Sections.erase(Iter, std::end(Sections)); } +void Object::removeSymbols(function_ref ToRemove) { + if (!SymbolTable) + return; + + for (const SecPtr &Sec : Sections) + Sec->removeSymbols(ToRemove); +} + void Object::sortSections() { // Put all sections in offset order. Maintain the ordering as closely as // possible while meeting that demand however. Index: tools/llvm-objcopy/Opts.td =================================================================== --- tools/llvm-objcopy/Opts.td +++ tools/llvm-objcopy/Opts.td @@ -78,3 +78,8 @@ HelpText<"Remove all local symbols except file and section symbols">; def x : Flag<["-"], "x">, Alias; +defm strip_symbol : Eq<"strip-symbol">, + MetaVarName<"symbol">, + HelpText<"Remove symbol ">; +def N : JoinedOrSeparate<["-"], "N">, + Alias; Index: tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -121,6 +121,7 @@ std::vector SymbolsToLocalize; std::vector SymbolsToGlobalize; std::vector SymbolsToWeaken; + std::vector SymbolsToRemove; StringMap SymbolsToRename; bool StripAll; bool StripAllGNU; @@ -345,11 +346,17 @@ Sym.Name = I->getValue(); }); - Obj.SymbolTable->removeSymbols([&](const Symbol &Sym) { + Obj.removeSymbols([&](const Symbol &Sym) { if (Config.DiscardAll && Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE && Sym.Type != STT_SECTION) return true; + + if (!Config.SymbolsToRemove.empty() && + is_contained(Config.SymbolsToRemove, Sym.Name)) { + return true; + } + return false; }); } @@ -445,6 +452,8 @@ Config.SymbolsToGlobalize.push_back(Arg->getValue()); for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol)) Config.SymbolsToWeaken.push_back(Arg->getValue()); + for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol)) + Config.SymbolsToRemove.push_back(Arg->getValue()); return Config; }