[llvm-objcopy] Add --localize-hidden option
This change adds support in llvm-objcopy for GNU objcopy's --localize-hidden
option. This option changes every hidden or internal symbol into a local symbol.
llvm-svn: 321884
diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp
index 9e82448..a0708ed 100644
--- a/llvm/tools/llvm-objcopy/Object.cpp
+++ b/llvm/tools/llvm-objcopy/Object.cpp
@@ -175,6 +175,25 @@
Symbols.erase(Iter, std::end(Symbols));
}
+void SymbolTableSection::localize(
+ std::function<bool(const Symbol &)> ToLocalize) {
+ for (const auto &Sym : Symbols) {
+ if (ToLocalize(*Sym))
+ Sym->Binding = STB_LOCAL;
+ }
+
+ // Now that the local symbols aren't grouped at the start we have to reorder
+ // the symbols to respect this property.
+ std::stable_partition(
+ std::begin(Symbols), std::end(Symbols),
+ [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; });
+
+ // Lastly we fix the symbol indexes.
+ uint32_t Index = 0;
+ for (auto &Sym : Symbols)
+ Sym->Index = Index++;
+}
+
void SymbolTableSection::initialize(SectionTableRef SecTable) {
Size = 0;
setStrTab(SecTable.getSectionOfType<StringTableSection>(