Index: test/tools/llvm-objcopy/strip-unneeded.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/strip-unneeded.test @@ -0,0 +1,136 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --strip-unneeded %t %t2 +# RUN: llvm-readobj -symbols %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 + - Name: .group + Type: SHT_GROUP + Link: .symtab + AddressAlign: 0x0000000000000004 + Info: barfoo + Members: + - SectionOrType: GRP_COMDAT + - SectionOrType: .text + - Name: .rel.text + Type: SHT_REL + Link: .symtab + Info: .text + Relocations: + - Offset: 0x1000 + Symbol: foo + Type: R_X86_64_PC32 +Symbols: + Local: + - Name: foo + Type: STT_FUNC + Section: .text + Value: 0x1000 + Size: 8 + - Name: bar + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1008 + - Name: barfoo + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1010 + - Name: fileSymbol + Type: STT_FILE + - Name: sectionSymbol + Type: STT_SECTION + Weak: + - Name: baz + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1018 + - Name: foobaz + Type: STT_FUNC + Global: + - Name: foobar + Type: STT_FUNC + - Name: barbaz + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1020 + +#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: foo +#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: Symbol { +#CHECK-NEXT: Name: barfoo +#CHECK-NEXT: Value: 0x1010 +#CHECK-NEXT: Size: 8 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: Function +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .text +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: fileSymbol +#CHECK-NEXT: Value: 0x0 +#CHECK-NEXT: Size: 0 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: File +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: Undefined +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: sectionSymbol +#CHECK-NEXT: Value: 0x0 +#CHECK-NEXT: Size: 0 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: Section +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: Undefined +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: barbaz +#CHECK-NEXT: Value: 0x1020 +#CHECK-NEXT: Size: 8 +#CHECK-NEXT: Binding: Global +#CHECK-NEXT: Type: Function +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .text +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: baz +#CHECK-NEXT: Value: 0x1018 +#CHECK-NEXT: Size: 8 +#CHECK-NEXT: Binding: Weak +#CHECK-NEXT: Type: Function +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .text +#CHECK-NEXT: } +#CHECK-NEXT:] Index: tools/llvm-objcopy/ObjcopyOpts.td =================================================================== --- tools/llvm-objcopy/ObjcopyOpts.td +++ tools/llvm-objcopy/ObjcopyOpts.td @@ -90,3 +90,5 @@ Alias; def only_keep_debug : Flag<["-", "--"], "only-keep-debug">, HelpText<"Currently ignored. Only for compaitability with GNU objcopy.">; +def strip_unneeded : Flag<["-", "--"], "strip-unneeded">, + HelpText<"Remove all symbols not needed by relocations">; Index: tools/llvm-objcopy/Object.h =================================================================== --- tools/llvm-objcopy/Object.h +++ tools/llvm-objcopy/Object.h @@ -212,6 +212,7 @@ virtual void removeSectionReferences(const SectionBase *Sec); virtual void removeSymbols(function_ref ToRemove); virtual void accept(SectionVisitor &Visitor) const = 0; + virtual void markSymbols(); }; class Segment { @@ -344,6 +345,7 @@ uint8_t Type; uint64_t Value; uint8_t Visibility; + bool Referenced = false; uint16_t getShndx() const; }; @@ -367,6 +369,7 @@ void addSymbolNames(); const SectionBase *getStrTab() const { return SymbolNames; } const Symbol *getSymbolByIndex(uint32_t Index) const; + Symbol *getSymbolByIndex(uint32_t Index); void updateSymbols(function_ref Callable); void removeSectionReferences(const SectionBase *Sec) override; @@ -381,7 +384,7 @@ }; struct Relocation { - const Symbol *RelocSymbol = nullptr; + Symbol *RelocSymbol = nullptr; uint64_t Offset; uint64_t Addend; uint32_t Type; @@ -435,6 +438,7 @@ void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } void accept(SectionVisitor &Visitor) const override; void removeSymbols(function_ref ToRemove) override; + void markSymbols() override; static bool classof(const SectionBase *S) { if (S->Flags & ELF::SHF_ALLOC) @@ -449,7 +453,7 @@ class GroupSection : public SectionBase { MAKE_SEC_WRITER_FRIEND const SymbolTableSection *SymTab = nullptr; - const Symbol *Sym = nullptr; + Symbol *Sym = nullptr; ELF::Elf32_Word FlagWord; SmallVector GroupMembers; @@ -461,7 +465,7 @@ explicit GroupSection(ArrayRef Data) : Contents(Data) {} void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; } - void setSymbol(const Symbol *S) { Sym = S; } + void setSymbol(Symbol *S) { Sym = S; } void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; } void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); } @@ -469,6 +473,7 @@ void accept(SectionVisitor &) const override; void finalize() override; void removeSymbols(function_ref ToRemove) override; + void markSymbols() override; static bool classof(const SectionBase *S) { return S->Type == ELF::SHT_GROUP; @@ -624,6 +629,7 @@ void removeSections(std::function ToRemove); void removeSymbols(function_ref ToRemove); + void markSymbols(); 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 @@ -50,6 +50,7 @@ void SectionBase::removeSymbols(function_ref ToRemove) {} void SectionBase::initialize(SectionTableRef SecTable) {} void SectionBase::finalize() {} +void SectionBase::markSymbols() {} template void ELFWriter::writeShdr(const SectionBase &Sec) { uint8_t *Buf = BufPtr->getBufferStart(); @@ -255,6 +256,11 @@ return Symbols[Index].get(); } +Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) { + return const_cast( + static_cast(this)->getSymbolByIndex(Index)); +} + template void ELFSectionWriter::visit(const SymbolTableSection &Sec) { uint8_t *Buf = Out.getBufferStart(); @@ -352,6 +358,11 @@ "' because it is named in a relocation"); } +void RelocationSection::markSymbols() { + for (const Relocation &Reloc : Relocations) + Reloc.RelocSymbol->Referenced = true; +} + void SectionWriter::visit(const DynamicRelocationSection &Sec) { std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), Out.getBufferStart() + Sec.Offset); @@ -384,6 +395,11 @@ } } +void GroupSection::markSymbols() { + if (Sym) + Sym->Referenced = true; +} + void Section::initialize(SectionTableRef SecTable) { if (Link != ELF::SHN_UNDEF) LinkSection = @@ -931,6 +947,11 @@ Sec->removeSymbols(ToRemove); } +void Object::markSymbols() { + for (const SecPtr &Sec : Sections) + Sec->markSymbols(); +} + 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/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -156,6 +156,7 @@ bool StripSections = false; bool StripNonAlloc = false; bool StripDWO = false; + bool StripUnneeded = false; bool ExtractDWO = false; bool LocalizeHidden = false; bool Weaken = false; @@ -374,6 +375,9 @@ Sym.Name = I->getValue(); }); + if (Config.StripUnneeded) + Obj.markSymbols(); + Obj.removeSymbols([&](const Symbol &Sym) { if (!Config.SymbolsToKeep.empty() && is_contained(Config.SymbolsToKeep, Sym.Name)) @@ -389,6 +393,13 @@ return true; } + // TODO: We might handle the 'null symbol' in a different way + // by probably handling it the same way as we handle 'null section' ? + if (Config.StripUnneeded && !Sym.Referenced && Sym.Index != 0 && + (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) && + Sym.Type != STT_FILE && Sym.Type != STT_SECTION) + return true; + return false; }); } @@ -479,6 +490,7 @@ Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo); Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections); Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc); + Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded); Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo); Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);