[llvm] r331924 - [llvm-objcopy] Add --strip-symbol (-N) option
Paul Semel via llvm-commits
llvm-commits at lists.llvm.org
Wed May 9 14:36:55 PDT 2018
Author: paulsemel
Date: Wed May 9 14:36:54 2018
New Revision: 331924
URL: http://llvm.org/viewvc/llvm-project?rev=331924&view=rev
Log:
[llvm-objcopy] Add --strip-symbol (-N) option
Added:
llvm/trunk/test/tools/llvm-objcopy/strip-group-symbol.test
llvm/trunk/test/tools/llvm-objcopy/strip-reloc-symbol.test
llvm/trunk/test/tools/llvm-objcopy/strip-symbol.test
Modified:
llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td
llvm/trunk/tools/llvm-objcopy/Object.cpp
llvm/trunk/tools/llvm-objcopy/Object.h
llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp
Added: llvm/trunk/test/tools/llvm-objcopy/strip-group-symbol.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/strip-group-symbol.test?rev=331924&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/strip-group-symbol.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/strip-group-symbol.test Wed May 9 14:36:54 2018
@@ -0,0 +1,31 @@
+# RUN: yaml2obj %s > %t
+# RUN: not llvm-objcopy -N foo %t %t2 2>&1 | FileCheck %s
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .group
+ Type: SHT_GROUP
+ Link: .symtab
+ AddressAlign: 0x0000000000000004
+ Info: foo
+ Members:
+ - SectionOrType: GRP_COMDAT
+ - SectionOrType: .text
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x1000
+ AddressAlign: 0x0000000000000010
+ Size: 64
+Symbols:
+ Weak:
+ - Name: foo
+ Type: STT_FUNC
+ Section: .text
+
+#CHECK: {{.*}}llvm-objcopy: Symbol foo cannot be removed because it is referenced by the section .group[1].
Added: llvm/trunk/test/tools/llvm-objcopy/strip-reloc-symbol.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/strip-reloc-symbol.test?rev=331924&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/strip-reloc-symbol.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/strip-reloc-symbol.test Wed May 9 14:36:54 2018
@@ -0,0 +1,32 @@
+# RUN: yaml2obj %s > %t
+# RUN: not llvm-objcopy -N foo %t %t2 2>&1 | FileCheck %s
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x1000
+ AddressAlign: 0x0000000000000010
+ Size: 64
+ - Name: .rel.text
+ Type: SHT_REL
+ Info: .text
+ Relocations:
+ - Offset: 0x1000
+ Symbol: foo
+ Type: R_X86_64_PC32
+Symbols:
+ Local:
+ - Name: foo
+ Type: STT_FUNC
+ Section: .text
+ Value: 0x1000
+ Size: 8
+
+#CHECK: {{.*}}llvm-objcopy: not stripping symbol `foo' because it is named in a relocation.
Added: llvm/trunk/test/tools/llvm-objcopy/strip-symbol.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/strip-symbol.test?rev=331924&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/strip-symbol.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/strip-symbol.test Wed May 9 14:36:54 2018
@@ -0,0 +1,57 @@
+# RUN: yaml2obj %s > %t
+# RUN: llvm-objcopy --strip-symbol baz -N bar %t %t2
+# RUN: llvm-readobj -symbols -sections %t2 | FileCheck %s
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x1000
+ AddressAlign: 0x0000000000000010
+ Size: 64
+Symbols:
+ Local:
+ - Name: foo
+ Type: STT_FUNC
+ Section: .text
+ Value: 0x1000
+ Size: 8
+ Weak:
+ - Name: bar
+ Type: STT_FUNC
+ Size: 8
+ Section: .text
+ Value: 0x1008
+ Global:
+ - Name: baz
+ Type: STT_FUNC
+ Size: 8
+ Section: .text
+ Value: 0x1010
+
+#CHECK: Symbols [
+#CHECK-NEXT: Symbol {
+#CHECK-NEXT: Name:
+#CHECK-NEXT: Value: 0x0
+#CHECK-NEXT: Size: 0
+#CHECK-NEXT: Binding: Local
+#CHECK-NEXT: Type: None
+#CHECK-NEXT: Other: 0
+#CHECK-NEXT: Section: Undefined
+#CHECK-NEXT: }
+#CHECK-NEXT: Symbol {
+#CHECK-NEXT: Name: foo
+#CHECK-NEXT: Value: 0x1000
+#CHECK-NEXT: Size: 8
+#CHECK-NEXT: Binding: Local
+#CHECK-NEXT: Type: Function
+#CHECK-NEXT: Other: 0
+#CHECK-NEXT: Section: .text
+#CHECK-NEXT: }
+#CHECK-NEXT:]
Modified: llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td?rev=331924&r1=331923&r2=331924&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td (original)
+++ llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td Wed May 9 14:36:54 2018
@@ -78,3 +78,8 @@ def discard_all : Flag<["-", "--"], "dis
HelpText<"Remove all local symbols except file and section symbols">;
def x : Flag<["-"], "x">,
Alias<discard_all>;
+defm strip_symbol : Eq<"strip-symbol">,
+ MetaVarName<"symbol">,
+ HelpText<"Remove symbol <symbol>">;
+def N : JoinedOrSeparate<["-"], "N">,
+ Alias<strip_symbol>;
Modified: llvm/trunk/tools/llvm-objcopy/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/Object.cpp?rev=331924&r1=331923&r2=331924&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/Object.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/Object.cpp Wed May 9 14:36:54 2018
@@ -47,6 +47,7 @@ template <class ELFT> void ELFWriter<ELF
}
void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
+void SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {}
void SectionBase::initialize(SectionTableRef SecTable) {}
void SectionBase::finalize() {}
@@ -206,7 +207,8 @@ void SymbolTableSection::updateSymbols(f
assignIndices();
}
-void SymbolTableSection::removeSymbols(function_ref<bool(Symbol &)> ToRemove) {
+void SymbolTableSection::removeSymbols(
+ function_ref<bool(const Symbol &)> ToRemove) {
Symbols.erase(
std::remove_if(std::begin(Symbols), std::end(Symbols),
[ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
@@ -342,6 +344,14 @@ void RelocationSection::accept(SectionVi
Visitor.visit(*this);
}
+void RelocationSection::removeSymbols(
+ function_ref<bool(const Symbol &)> ToRemove) {
+ for (const Relocation &Reloc : Relocations)
+ if (ToRemove(*Reloc.RelocSymbol))
+ error("not stripping symbol `" + Reloc.RelocSymbol->Name +
+ "' because it is named in a relocation");
+}
+
void SectionWriter::visit(const DynamicRelocationSection &Sec) {
std::copy(std::begin(Sec.Contents), std::end(Sec.Contents),
Out.getBufferStart() + Sec.Offset);
@@ -365,6 +375,15 @@ void GroupSection::finalize() {
this->Link = SymTab->Index;
}
+void GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
+ if (ToRemove(*Sym)) {
+ error("Symbol " + Sym->Name +
+ " cannot be removed because it is "
+ "referenced by the section " +
+ this->Name + "[" + Twine(this->Index) + "]");
+ }
+}
+
void Section::initialize(SectionTableRef SecTable) {
if (Link != ELF::SHN_UNDEF)
LinkSection =
@@ -904,6 +923,14 @@ void Object::removeSections(std::functio
Sections.erase(Iter, std::end(Sections));
}
+void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
+ if (!SymbolTable)
+ return;
+
+ for (const SecPtr &Sec : Sections)
+ Sec->removeSymbols(ToRemove);
+}
+
void Object::sortSections() {
// Put all sections in offset order. Maintain the ordering as closely as
// possible while meeting that demand however.
Modified: llvm/trunk/tools/llvm-objcopy/Object.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/Object.h?rev=331924&r1=331923&r2=331924&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/Object.h (original)
+++ llvm/trunk/tools/llvm-objcopy/Object.h Wed May 9 14:36:54 2018
@@ -38,6 +38,7 @@ class GnuDebugLinkSection;
class GroupSection;
class Segment;
class Object;
+struct Symbol;
class SectionTableRef {
MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
@@ -209,6 +210,7 @@ public:
virtual void initialize(SectionTableRef SecTable);
virtual void finalize();
virtual void removeSectionReferences(const SectionBase *Sec);
+ virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
virtual void accept(SectionVisitor &Visitor) const = 0;
};
@@ -366,12 +368,12 @@ 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;
void finalize() override;
void accept(SectionVisitor &Visitor) const override;
+ void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
static bool classof(const SectionBase *S) {
return S->Type == ELF::SHT_SYMTAB;
@@ -432,6 +434,7 @@ class RelocationSection
public:
void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
void accept(SectionVisitor &Visitor) const override;
+ void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
static bool classof(const SectionBase *S) {
if (S->Flags & ELF::SHF_ALLOC)
@@ -465,6 +468,7 @@ public:
void initialize(SectionTableRef SecTable) override{};
void accept(SectionVisitor &) const override;
void finalize() override;
+ void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
static bool classof(const SectionBase *S) {
return S->Type == ELF::SHT_GROUP;
@@ -619,6 +623,7 @@ public:
ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
void removeSections(std::function<bool(const SectionBase &)> ToRemove);
+ void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
template <class T, class... Ts> T &addSection(Ts &&... Args) {
auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
auto Ptr = Sec.get();
Modified: llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp?rev=331924&r1=331923&r2=331924&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp Wed May 9 14:36:54 2018
@@ -147,6 +147,7 @@ struct CopyConfig {
std::vector<StringRef> SymbolsToLocalize;
std::vector<StringRef> SymbolsToGlobalize;
std::vector<StringRef> SymbolsToWeaken;
+ std::vector<StringRef> SymbolsToRemove;
StringMap<StringRef> SymbolsToRename;
bool StripAll = false;
bool StripAllGNU = false;
@@ -371,11 +372,17 @@ void HandleArgs(const CopyConfig &Config
Sym.Name = I->getValue();
});
- Obj.SymbolTable->removeSymbols([&](const Symbol &Sym) {
+ Obj.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;
+
+ if (!Config.SymbolsToRemove.empty() &&
+ is_contained(Config.SymbolsToRemove, Sym.Name)) {
+ return true;
+ }
+
return false;
});
}
@@ -476,6 +483,8 @@ CopyConfig ParseObjcopyOptions(ArrayRef<
Config.SymbolsToGlobalize.push_back(Arg->getValue());
for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
Config.SymbolsToWeaken.push_back(Arg->getValue());
+ for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol))
+ Config.SymbolsToRemove.push_back(Arg->getValue());
return Config;
}
More information about the llvm-commits
mailing list