Index: test/tools/llvm-objcopy/strip-symbol.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/strip-symbol.test @@ -0,0 +1,68 @@ +# 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: .group + Type: SHT_GROUP + Link: .symtab + AddressAlign: 0x0000000000000004 + Info: Weak + 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: 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: Sections [ +#CHECK-NOT: Name: .groups +#CHECK: ] +#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 removeSymbolReferences(const Symbol *Sym); virtual void accept(SectionVisitor &Visitor) const = 0; }; @@ -364,9 +366,10 @@ uint16_t Shndx, uint64_t Sz); void addSymbolNames(); const SectionBase *getStrTab() const { return SymbolNames; } + const std::vector &getSymbols() const { return Symbols; } const Symbol *getSymbolByIndex(uint32_t Index) const; void updateSymbols(function_ref Callable); - void removeSymbols(function_ref ToRemove); + void removeSymbolReferences(function_ref ToRemove); void removeSectionReferences(const SectionBase *Sec) override; void initialize(SectionTableRef SecTable) override; @@ -619,6 +622,7 @@ ConstRange segments() const { return make_pointee_range(Segments); } void removeSections(std::function ToRemove); + void removeSymbols(std::function 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::removeSymbolReferences(const Symbol *Sym) {} void SectionBase::initialize(SectionTableRef SecTable) {} void SectionBase::finalize() {} @@ -194,7 +195,8 @@ " cannot be removed because it is referenced by the symbol table " + this->Name); } - removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; }); + removeSymbolReferences( + [Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; }); } void SymbolTableSection::updateSymbols(function_ref Callable) { @@ -206,7 +208,8 @@ assignIndices(); } -void SymbolTableSection::removeSymbols(function_ref ToRemove) { +void SymbolTableSection::removeSymbolReferences( + function_ref ToRemove) { Symbols.erase( std::remove_if(std::begin(Symbols), std::end(Symbols), [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }), @@ -904,6 +907,36 @@ Sections.erase(Iter, std::end(Sections)); } +void Object::removeSymbols(std::function ToRemove) { + std::vector Groups; + + std::for_each(std::begin(Sections), std::end(Sections), + [&Groups](const SecPtr &Sec) { + if (Sec->Type == SHT_GROUP) + Groups.push_back(Sec.get()); + }); + + if (!SymbolTable) + return; + + SymbolTable->removeSymbolReferences([&Groups, ToRemove, + this](const Symbol &Sym) { + if (ToRemove(Sym)) { + const auto &It = std::find_if(std::begin(Groups), std::end(Groups), + [&Sym, this](const SectionBase *Sec) { + return Sec->Link == SymbolTable->Index && + Sec->Info == Sym.Index; + }); + if (It != std::end(Groups)) + removeSections([&It](const SectionBase &SecBase) { + return (*It)->Index == SecBase.Index; + }); + return true; + } + return false; + }); +} + 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,20 @@ 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)) { + if (Obj.Type == ET_REL && Sym.getShndx() == SHN_UNDEF) + error("not stripping symbol `" + Sym.Name + + "' because it is named in a relocation"); + return true; + } + return false; }); } @@ -445,6 +455,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; }