summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-objcopy
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/tools/llvm-objcopy')
-rw-r--r--llvm/tools/llvm-objcopy/Object.cpp16
-rw-r--r--llvm/tools/llvm-objcopy/Object.h1
-rw-r--r--llvm/tools/llvm-objcopy/Opts.td4
-rw-r--r--llvm/tools/llvm-objcopy/llvm-objcopy.cpp10
4 files changed, 25 insertions, 6 deletions
diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp
index 07b60367b9e..93487dc5bf6 100644
--- a/llvm/tools/llvm-objcopy/Object.cpp
+++ b/llvm/tools/llvm-objcopy/Object.cpp
@@ -194,12 +194,7 @@ void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
" cannot be removed because it is referenced by the symbol table " +
this->Name);
}
- auto Iter =
- std::remove_if(std::begin(Symbols), std::end(Symbols),
- [=](const SymPtr &Sym) { return Sym->DefinedIn == Sec; });
- Size -= (std::end(Symbols) - Iter) * this->EntrySize;
- Symbols.erase(Iter, std::end(Symbols));
- assignIndices();
+ removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; });
}
void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
@@ -211,6 +206,15 @@ void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
assignIndices();
}
+void SymbolTableSection::removeSymbols(function_ref<bool(Symbol &)> ToRemove) {
+ Symbols.erase(
+ std::remove_if(std::begin(Symbols), std::end(Symbols),
+ [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
+ std::end(Symbols));
+ Size = Symbols.size() * EntrySize;
+ assignIndices();
+}
+
void SymbolTableSection::initialize(SectionTableRef SecTable) {
Size = 0;
setStrTab(SecTable.getSectionOfType<StringTableSection>(
diff --git a/llvm/tools/llvm-objcopy/Object.h b/llvm/tools/llvm-objcopy/Object.h
index 2598b249a7c..845d6819052 100644
--- a/llvm/tools/llvm-objcopy/Object.h
+++ b/llvm/tools/llvm-objcopy/Object.h
@@ -366,6 +366,7 @@ public:
const SectionBase *getStrTab() const { return SymbolNames; }
const Symbol *getSymbolByIndex(uint32_t Index) const;
void updateSymbols(function_ref<void(Symbol &)> Callable);
+ void removeSymbols(function_ref<bool(Symbol &)> ToRemove);
void removeSectionReferences(const SectionBase *Sec) override;
void initialize(SectionTableRef SecTable) override;
diff --git a/llvm/tools/llvm-objcopy/Opts.td b/llvm/tools/llvm-objcopy/Opts.td
index 9698cef0733..487a3125f00 100644
--- a/llvm/tools/llvm-objcopy/Opts.td
+++ b/llvm/tools/llvm-objcopy/Opts.td
@@ -74,3 +74,7 @@ def W : JoinedOrSeparate<["-"], "W">,
Alias<weaken_symbol>;
def weaken : Flag<["-", "--"], "weaken">,
HelpText<"Mark all global symbols as weak">;
+def discard_all : Flag<["-", "--"], "discard-all">,
+ HelpText<"Remove all local symbols except file and section symbols">;
+def x : Flag<["-"], "x">,
+ Alias<discard_all>;
diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
index 8bf7ac77f16..900a11d04a1 100644
--- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
+++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -131,6 +131,7 @@ struct CopyConfig {
bool ExtractDWO;
bool LocalizeHidden;
bool Weaken;
+ bool DiscardAll;
};
using SectionPred = std::function<bool(const SectionBase &Sec)>;
@@ -343,6 +344,14 @@ void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
if (I != Config.SymbolsToRename.end())
Sym.Name = I->getValue();
});
+
+ Obj.SymbolTable->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;
+ return false;
+ });
}
}
@@ -429,6 +438,7 @@ CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
+ Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all);
for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
Config.SymbolsToLocalize.push_back(Arg->getValue());
for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
OpenPOWER on IntegriCloud