diff options
author | Jake Ehrlich <jakehehrlich@google.com> | 2017-11-30 20:14:53 +0000 |
---|---|---|
committer | Jake Ehrlich <jakehehrlich@google.com> | 2017-11-30 20:14:53 +0000 |
commit | fad0b0655f08fbe4568dee2cac1108bd931392c7 (patch) | |
tree | c4e01f4feff7d61cf1ee8de39343eb24a5447009 /tools | |
parent | 0ba92ff15e3fd15b9f97983e6ec7e2da00eebd52 (diff) |
[llvm-objcopy] Add support for --only-keep/-j and --keep
This change adds support for the --only-keep option and the -j alias as well.
A common use case for these being used together is to dump a specific section's
data. Additionally the --keep option is added (GNU objcopy doesn't have this)
to avoid removing a bunch of things. This allows people to err on the side of
stripping aggressively and then to keep the specific bits that they need for
their application.
Differential Revision: https://reviews.llvm.org/D39021
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319467 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r-- | tools/llvm-objcopy/Object.h | 2 | ||||
-rw-r--r-- | tools/llvm-objcopy/llvm-objcopy.cpp | 53 |
2 files changed, 55 insertions, 0 deletions
diff --git a/tools/llvm-objcopy/Object.h b/tools/llvm-objcopy/Object.h index f12e6da7d21..9f98c04ad9b 100644 --- a/tools/llvm-objcopy/Object.h +++ b/tools/llvm-objcopy/Object.h @@ -196,6 +196,7 @@ public: SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx, uint64_t Sz); void addSymbolNames(); + const SectionBase *getStrTab() const { return SymbolNames; } const Symbol *getSymbolByIndex(uint32_t Index) const; void removeSectionReferences(const SectionBase *Sec) override; void initialize(SectionTableRef SecTable) override; @@ -368,6 +369,7 @@ public: Object(const object::ELFObjectFile<ELFT> &Obj); virtual ~Object() = default; + const SymbolTableSection *getSymTab() const { return SymbolTable; } const SectionBase *getSectionHeaderStrTab() const { return SectionNames; } void removeSections(std::function<bool(const SectionBase &)> ToRemove); virtual size_t totalSize() const = 0; diff --git a/tools/llvm-objcopy/llvm-objcopy.cpp b/tools/llvm-objcopy/llvm-objcopy.cpp index 09553e85202..615667bf99a 100644 --- a/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/tools/llvm-objcopy/llvm-objcopy.cpp @@ -89,6 +89,13 @@ static cl::opt<bool> StripAll( static cl::opt<bool> StripAllGNU("strip-all-gnu", cl::desc("Removes symbol, relocation, and debug information")); +static cl::list<std::string> Keep("keep", cl::desc("Keep <section>"), + cl::value_desc("section")); +static cl::list<std::string> OnlyKeep("only-keep", + cl::desc("Remove all but <section>"), + cl::value_desc("section")); +static cl::alias OnlyKeepA("j", cl::desc("Alias for only-keep"), + cl::aliasopt(OnlyKeep)); static cl::opt<bool> StripDebug("strip-debug", cl::desc("Removes all debug information")); static cl::opt<bool> StripSections("strip-sections", @@ -150,6 +157,13 @@ void SplitDWOToFile(const ELFObjectFile<ELFT> &ObjFile, StringRef File) { WriteObjectFile(DWOFile, File); } +// This function handles the high level operations of GNU objcopy including +// handling command line options. It's important to outline certain properties +// we expect to hold of the command line operations. Any operation that "keeps" +// should keep regardless of a remove. Additionally any removal should respect +// any previous removals. Lastly whether or not something is removed shouldn't +// depend a) on the order the options occur in or b) on some opaque priority +// system. The only priority is that keeps/copies overrule removes. template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { std::unique_ptr<Object<ELFT>> Obj; @@ -166,6 +180,8 @@ void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { SectionPred RemovePred = [](const SectionBase &) { return false; }; + // Removes: + if (!ToRemove.empty()) { RemovePred = [&](const SectionBase &Sec) { return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) != @@ -234,6 +250,43 @@ void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { return (Sec.Flags & SHF_ALLOC) == 0; }; + // Explicit copies: + + if (!OnlyKeep.empty()) { + RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { + // Explicitly keep these sections regardless of previous removes. + if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) != + std::end(OnlyKeep)) + return false; + + // Allow all implicit removes. + if (RemovePred(Sec)) { + return true; + } + + // Keep special sections. + if (Obj->getSectionHeaderStrTab() == &Sec) { + return false; + } + if (Obj->getSymTab() == &Sec || Obj->getSymTab()->getStrTab() == &Sec) { + return false; + } + // Remove everything else. + return true; + }; + } + + if (!Keep.empty()) { + RemovePred = [RemovePred](const SectionBase &Sec) { + // Explicitly keep these sections regardless of previous removes. + if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) != + std::end(Keep)) + return false; + // Otherwise defer to RemovePred. + return RemovePred(Sec); + }; + } + Obj->removeSections(RemovePred); Obj->finalize(); WriteObjectFile(*Obj, OutputFilename.getValue()); |