diff options
Diffstat (limited to 'llvm/tools/llvm-objcopy')
| -rw-r--r-- | llvm/tools/llvm-objcopy/Object.cpp | 19 | ||||
| -rw-r--r-- | llvm/tools/llvm-objcopy/Object.h | 3 | ||||
| -rw-r--r-- | llvm/tools/llvm-objcopy/llvm-objcopy.cpp | 12 |
3 files changed, 33 insertions, 1 deletions
diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp index 9e82448187e..a0708ed59b4 100644 --- a/llvm/tools/llvm-objcopy/Object.cpp +++ b/llvm/tools/llvm-objcopy/Object.cpp @@ -175,6 +175,25 @@ void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) { 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>( diff --git a/llvm/tools/llvm-objcopy/Object.h b/llvm/tools/llvm-objcopy/Object.h index 639f0f29ceb..17ea6c9413b 100644 --- a/llvm/tools/llvm-objcopy/Object.h +++ b/llvm/tools/llvm-objcopy/Object.h @@ -214,6 +214,7 @@ public: const SectionBase *getStrTab() const { return SymbolNames; } const Symbol *getSymbolByIndex(uint32_t Index) const; void removeSectionReferences(const SectionBase *Sec) override; + void localize(std::function<bool(const Symbol &)> ToLocalize); void initialize(SectionTableRef SecTable) override; void finalize() override; @@ -384,7 +385,7 @@ public: Object(const object::ELFObjectFile<ELFT> &Obj); virtual ~Object() = default; - const SymbolTableSection *getSymTab() const { return SymbolTable; } + SymbolTableSection *getSymTab() const { return SymbolTable; } const SectionBase *getSectionHeaderStrTab() const { return SectionNames; } void removeSections(std::function<bool(const SectionBase &)> ToRemove); void addSection(StringRef SecName, ArrayRef<uint8_t> Data); diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp index 20ce93bb40e..eb1d0de90d5 100644 --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -117,6 +117,10 @@ static cl::list<std::string> AddSection( "add-section", cl::desc("Make a section named <section> with the contents of <file>."), cl::value_desc("section=file")); +static cl::opt<bool> LocalizeHidden( + "localize-hidden", + cl::desc( + "Mark all symbols that have hidden or internal visibility as local")); using SectionPred = std::function<bool(const SectionBase &Sec)>; @@ -180,6 +184,14 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { if (!SplitDWO.empty()) SplitDWOToFile<ELFT>(ObjFile, SplitDWO.getValue()); + // Localize: + + if (LocalizeHidden) { + Obj->getSymTab()->localize([](const Symbol &Sym) { + return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL; + }); + } + SectionPred RemovePred = [](const SectionBase &) { return false; }; // Removes: |

