[llvm-objcopy] Add --strip-symbol (-N) option

llvm-svn: 331924
This commit is contained in:
Paul Semel 2018-05-09 21:36:54 +00:00
parent 2e538089fa
commit 4246a462a3
7 changed files with 169 additions and 3 deletions

View File

@ -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].

View File

@ -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.

View File

@ -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:]

View File

@ -78,3 +78,8 @@ def discard_all : Flag<["-", "--"], "discard-all">,
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>;

View File

@ -47,6 +47,7 @@ template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
}
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(function_ref<void(Symbol &)> Callable) {
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(SectionVisitor &Visitor) const {
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::function<bool(const SectionBase &)> ToRemove) {
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.

View File

@ -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();

View File

@ -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, Object &Obj, const Reader &Reader,
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<const char *> ArgsArr) {
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;
}