Index: test/tools/llvm-objcopy/strip-unneeded-dyn.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/strip-unneeded-dyn.test @@ -0,0 +1,96 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy -remove-section .rel.text.dup --strip-unneeded %t %t2 +# RUN: llvm-readobj -symbols %t2 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + 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 + Link: .symtab + Info: .text + Relocations: + - Offset: 0x1000 + Symbol: foo + Type: R_X86_64_PC32 + - Name: .rel.text.dup + 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 + Weak: + - Name: baz + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1010 + Global: + - Name: foobar + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1018 + +#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: foobar +#CHECK-NEXT: Value: 0x1018 +#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: 0x1010 +#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: test/tools/llvm-objcopy/strip-unneeded-exec.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/strip-unneeded-exec.test @@ -0,0 +1,88 @@ +# 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_EXEC + 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 + 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 + Weak: + - Name: baz + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1010 + Global: + - Name: foobar + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1018 + +#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: foobar +#CHECK-NEXT: Value: 0x1018 +#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: 0x1010 +#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: test/tools/llvm-objcopy/strip-unneeded-rel.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/strip-unneeded-rel.test @@ -0,0 +1,76 @@ +# 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: .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 + Weak: + - Name: baz + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1010 + Global: + - Name: foobar + Type: STT_FUNC + +#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: baz +#CHECK-NEXT: Value: 0x1010 +#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 @@ -88,3 +88,5 @@ HelpText<"Do not remove symbol ">; def K : JoinedOrSeparate<["-"], "K">, Alias; +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 @@ -344,6 +344,7 @@ uint8_t Type; uint64_t Value; uint8_t Visibility; + uint32_t RelocRefs; uint16_t getShndx() const; }; @@ -367,6 +368,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; @@ -429,12 +431,18 @@ : public RelocSectionWithSymtabBase { MAKE_SEC_WRITER_FRIEND + template + using ConstRange = iterator_range::const_iterator>; + std::vector Relocations; public: void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } void accept(SectionVisitor &Visitor) const override; void removeSymbols(function_ref ToRemove) override; + ConstRange relocations() const { + return make_range(Relocations.begin(), Relocations.end()); + } static bool classof(const SectionBase *S) { if (S->Flags & ELF::SHF_ALLOC) Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -185,6 +185,7 @@ Sym.Visibility = Visibility; Sym.Size = Sz; Sym.Index = Symbols.size(); + Sym.RelocRefs = 0; Symbols.emplace_back(llvm::make_unique(Sym)); Size += this->EntrySize; } @@ -195,6 +196,10 @@ " cannot be removed because it is referenced by the symbol table " + this->Name); } + if (auto RelSec = dyn_cast(Sec)) { + for (const auto &Reloc : RelSec->relocations()) + Symbols[Reloc.RelocSymbol->Index]->RelocRefs -= 1; + } removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; }); } @@ -255,6 +260,12 @@ return Symbols[Index].get(); } +Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) { + if (Symbols.size() <= Index) + error("Invalid symbol index: " + Twine(Index)); + return Symbols[Index].get(); +} + template void ELFSectionWriter::visit(const SymbolTableSection &Sec) { uint8_t *Buf = Out.getBufferStart(); @@ -647,7 +658,9 @@ ToAdd.Offset = Rel.r_offset; getAddend(ToAdd.Addend, Rel); ToAdd.Type = Rel.getType(false); - ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false)); + Symbol *Sym = SymbolTable->getSymbolByIndex(Rel.getSymbol(false)); + Sym->RelocRefs += 1; + ToAdd.RelocSymbol = Sym; Relocs->addRelocation(ToAdd); } } 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; @@ -388,6 +389,13 @@ return true; } + // TODO: We might handle the 'zero symbol' in a different way + // by probably handling it the same way as we handle 'zero section' ? + if (Config.StripUnneeded && !Sym.RelocRefs && Sym.Index != 0 && + (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) && + Sym.Type != STT_FILE && Sym.Type != STT_SECTION) + return true; + return false; }); } @@ -478,6 +486,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);