diff options
author | Paul Semel <semelpaul@gmail.com> | 2018-05-25 11:01:25 +0000 |
---|---|---|
committer | Paul Semel <semelpaul@gmail.com> | 2018-05-25 11:01:25 +0000 |
commit | fe9deeb4321f6389288178b77bd7e945c5398c1d (patch) | |
tree | aef5810161edde93de5d377ad0f6008b8c4a6058 /tools/llvm-objcopy | |
parent | 60231abbcc610c96814149827eecaee7c792cf9e (diff) |
[llvm-objcopy] Add --strip-unneeded option
Differential Revision: https://reviews.llvm.org/D46896
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333267 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/llvm-objcopy')
-rw-r--r-- | tools/llvm-objcopy/ObjcopyOpts.td | 2 | ||||
-rw-r--r-- | tools/llvm-objcopy/Object.cpp | 16 | ||||
-rw-r--r-- | tools/llvm-objcopy/Object.h | 11 | ||||
-rw-r--r-- | tools/llvm-objcopy/llvm-objcopy.cpp | 17 |
4 files changed, 43 insertions, 3 deletions
diff --git a/tools/llvm-objcopy/ObjcopyOpts.td b/tools/llvm-objcopy/ObjcopyOpts.td index 9d1fb19c40c..e7c7541a32c 100644 --- a/tools/llvm-objcopy/ObjcopyOpts.td +++ b/tools/llvm-objcopy/ObjcopyOpts.td @@ -90,3 +90,5 @@ def K : JoinedOrSeparate<["-"], "K">, Alias<keep_symbol>; 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">; diff --git a/tools/llvm-objcopy/Object.cpp b/tools/llvm-objcopy/Object.cpp index efb3207aa8d..c00477a1980 100644 --- a/tools/llvm-objcopy/Object.cpp +++ b/tools/llvm-objcopy/Object.cpp @@ -50,6 +50,7 @@ void SectionBase::removeSectionReferences(const SectionBase *Sec) {} void SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {} void SectionBase::initialize(SectionTableRef SecTable) {} void SectionBase::finalize() {} +void SectionBase::markSymbols() {} template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) { uint8_t *Buf = BufPtr->getBufferStart(); @@ -255,6 +256,11 @@ const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const { return Symbols[Index].get(); } +Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) { + return const_cast<Symbol *>( + static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index)); +} + template <class ELFT> void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) { uint8_t *Buf = Out.getBufferStart(); @@ -352,6 +358,11 @@ void RelocationSection::removeSymbols( "' 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::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) { } } +void GroupSection::markSymbols() { + if (Sym) + Sym->Referenced = true; +} + void Section::initialize(SectionTableRef SecTable) { if (Link != ELF::SHN_UNDEF) LinkSection = diff --git a/tools/llvm-objcopy/Object.h b/tools/llvm-objcopy/Object.h index e4307794456..d005f0d4902 100644 --- a/tools/llvm-objcopy/Object.h +++ b/tools/llvm-objcopy/Object.h @@ -212,6 +212,7 @@ public: virtual void removeSectionReferences(const SectionBase *Sec); virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove); virtual void accept(SectionVisitor &Visitor) const = 0; + virtual void markSymbols(); }; class Segment { @@ -344,6 +345,7 @@ struct Symbol { uint8_t Type; uint64_t Value; uint8_t Visibility; + bool Referenced = false; uint16_t getShndx() const; }; @@ -368,6 +370,7 @@ public: bool empty() const { return Symbols.empty(); } const SectionBase *getStrTab() const { return SymbolNames; } const Symbol *getSymbolByIndex(uint32_t Index) const; + Symbol *getSymbolByIndex(uint32_t Index); void updateSymbols(function_ref<void(Symbol &)> Callable); void removeSectionReferences(const SectionBase *Sec) override; @@ -382,7 +385,7 @@ public: }; struct Relocation { - const Symbol *RelocSymbol = nullptr; + Symbol *RelocSymbol = nullptr; uint64_t Offset; uint64_t Addend; uint32_t Type; @@ -436,6 +439,7 @@ public: void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } void accept(SectionVisitor &Visitor) const override; void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override; + void markSymbols() override; static bool classof(const SectionBase *S) { if (S->Flags & ELF::SHF_ALLOC) @@ -450,7 +454,7 @@ public: class GroupSection : public SectionBase { MAKE_SEC_WRITER_FRIEND const SymbolTableSection *SymTab = nullptr; - const Symbol *Sym = nullptr; + Symbol *Sym = nullptr; ELF::Elf32_Word FlagWord; SmallVector<SectionBase *, 3> GroupMembers; @@ -462,7 +466,7 @@ public: explicit GroupSection(ArrayRef<uint8_t> 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); } @@ -470,6 +474,7 @@ public: void accept(SectionVisitor &) const override; void finalize() override; void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override; + void markSymbols() override; static bool classof(const SectionBase *S) { return S->Type == ELF::SHT_GROUP; diff --git a/tools/llvm-objcopy/llvm-objcopy.cpp b/tools/llvm-objcopy/llvm-objcopy.cpp index 2c2c9926447..0a5b5b0d066 100644 --- a/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/tools/llvm-objcopy/llvm-objcopy.cpp @@ -168,6 +168,7 @@ struct CopyConfig { bool StripSections = false; bool StripNonAlloc = false; bool StripDWO = false; + bool StripUnneeded = false; bool ExtractDWO = false; bool LocalizeHidden = false; bool Weaken = false; @@ -263,6 +264,14 @@ void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader, Sym.Name = I->getValue(); }); + // The purpose of this loop is to mark symbols referenced by sections + // (like GroupSection or RelocationSection). This way, we know which + // symbols are still 'needed' and wich are not. + if (Config.StripUnneeded) { + for (auto &Section : Obj.sections()) + Section.markSymbols(); + } + Obj.removeSymbols([&](const Symbol &Sym) { if (!Config.SymbolsToKeep.empty() && is_contained(Config.SymbolsToKeep, Sym.Name)) @@ -281,6 +290,13 @@ void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader, 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; }); } @@ -509,6 +525,7 @@ CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) { 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); |