[llvm-objcopy] Add --strip-unneeded option

This option removes symbols that are not needed by relocations.

Differential Revision: https://reviews.llvm.org/D46896

llvm-svn: 332915
This commit is contained in:
Paul Semel 2018-05-21 22:50:32 +00:00
parent 99f7011829
commit 040df77ed6
5 changed files with 175 additions and 4 deletions

View File

@ -0,0 +1,136 @@
# RUN: yaml2obj %s > %t
# RUN: llvm-objcopy --strip-unneeded %t %t2
# RUN: llvm-readobj -symbols %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
- Name: .group
Type: SHT_GROUP
Link: .symtab
AddressAlign: 0x0000000000000004
Info: barfoo
Members:
- SectionOrType: GRP_COMDAT
- SectionOrType: .text
- Name: .rel.text
Type: SHT_REL
Link: .symtab
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
- Name: bar
Type: STT_FUNC
Size: 8
Section: .text
Value: 0x1008
- Name: barfoo
Type: STT_FUNC
Size: 8
Section: .text
Value: 0x1010
- Name: fileSymbol
Type: STT_FILE
- Name: sectionSymbol
Type: STT_SECTION
Weak:
- Name: baz
Type: STT_FUNC
Size: 8
Section: .text
Value: 0x1018
- Name: foobaz
Type: STT_FUNC
Global:
- Name: foobar
Type: STT_FUNC
- Name: barbaz
Type: STT_FUNC
Size: 8
Section: .text
Value: 0x1020
#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: Symbol {
#CHECK-NEXT: Name: barfoo
#CHECK-NEXT: Value: 0x1010
#CHECK-NEXT: Size: 8
#CHECK-NEXT: Binding: Local
#CHECK-NEXT: Type: Function
#CHECK-NEXT: Other: 0
#CHECK-NEXT: Section: .text
#CHECK-NEXT: }
#CHECK-NEXT: Symbol {
#CHECK-NEXT: Name: fileSymbol
#CHECK-NEXT: Value: 0x0
#CHECK-NEXT: Size: 0
#CHECK-NEXT: Binding: Local
#CHECK-NEXT: Type: File
#CHECK-NEXT: Other: 0
#CHECK-NEXT: Section: Undefined
#CHECK-NEXT: }
#CHECK-NEXT: Symbol {
#CHECK-NEXT: Name: sectionSymbol
#CHECK-NEXT: Value: 0x0
#CHECK-NEXT: Size: 0
#CHECK-NEXT: Binding: Local
#CHECK-NEXT: Type: Section
#CHECK-NEXT: Other: 0
#CHECK-NEXT: Section: Undefined
#CHECK-NEXT: }
#CHECK-NEXT: Symbol {
#CHECK-NEXT: Name: barbaz
#CHECK-NEXT: Value: 0x1020
#CHECK-NEXT: Size: 8
#CHECK-NEXT: Binding: Global
#CHECK-NEXT: Type: Function
#CHECK-NEXT: Other: 0
#CHECK-NEXT: Section: .text
#CHECK-NEXT: }
#CHECK-NEXT: Symbol {
#CHECK-NEXT: Name: baz
#CHECK-NEXT: Value: 0x1018
#CHECK-NEXT: Size: 8
#CHECK-NEXT: Binding: Weak
#CHECK-NEXT: Type: Function
#CHECK-NEXT: Other: 0
#CHECK-NEXT: Section: .text
#CHECK-NEXT: }
#CHECK-NEXT:]

View File

@ -90,3 +90,5 @@ def K : JoinedOrSeparate<["-"], "K">,
Alias<keep_symbol>;
def only_keep_debug : Flag<["-", "--"], "only-keep-debug">,
HelpText<"Currently ignored. Only for compaitability with GNU objcopy.">;
def strip_unneeded : Flag<["-", "--"], "strip-unneeded">,
HelpText<"Remove all symbols not needed by relocations">;

View File

@ -185,6 +185,7 @@ void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Sym.Visibility = Visibility;
Sym.Size = Sz;
Sym.Index = Symbols.size();
Sym.ReferenceCount = 0;
Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
Size += this->EntrySize;
}
@ -255,6 +256,11 @@ const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
return Symbols[Index].get();
}
Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) {
return const_cast<Symbol *>(
static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index));
}
template <class ELFT>
void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
uint8_t *Buf = Out.getBufferStart();
@ -344,6 +350,12 @@ void RelocationSection::accept(SectionVisitor &Visitor) const {
Visitor.visit(*this);
}
RelocationSection::~RelocationSection() {
for (auto &Rel : Relocations) {
--Rel.RelocSymbol->ReferenceCount;
}
}
void RelocationSection::removeSymbols(
function_ref<bool(const Symbol &)> ToRemove) {
for (const Relocation &Reloc : Relocations)
@ -647,7 +659,9 @@ void initRelocations(RelocationSection *Relocs, SymbolTableSection *SymbolTable,
ToAdd.Offset = Rel.r_offset;
getAddend(ToAdd.Addend, Rel);
ToAdd.Type = Rel.getType(false);
ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
Symbol *Sym = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
++Sym->ReferenceCount;
ToAdd.RelocSymbol = Sym;
Relocs->addRelocation(ToAdd);
}
}

View File

@ -344,6 +344,7 @@ struct Symbol {
uint8_t Type;
uint64_t Value;
uint8_t Visibility;
uint32_t ReferenceCount;
uint16_t getShndx() const;
};
@ -367,6 +368,7 @@ public:
void addSymbolNames();
const SectionBase *getStrTab() const { return SymbolNames; }
const Symbol *getSymbolByIndex(uint32_t Index) const;
Symbol *getSymbolByIndex(uint32_t Index);
void updateSymbols(function_ref<void(Symbol &)> Callable);
void removeSectionReferences(const SectionBase *Sec) override;
@ -381,7 +383,7 @@ public:
};
struct Relocation {
const Symbol *RelocSymbol = nullptr;
Symbol *RelocSymbol = nullptr;
uint64_t Offset;
uint64_t Addend;
uint32_t Type;
@ -432,6 +434,7 @@ class RelocationSection
std::vector<Relocation> Relocations;
public:
~RelocationSection();
void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
void accept(SectionVisitor &Visitor) const override;
void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
@ -449,7 +452,7 @@ public:
class GroupSection : public SectionBase {
MAKE_SEC_WRITER_FRIEND
const SymbolTableSection *SymTab = nullptr;
const Symbol *Sym = nullptr;
Symbol *Sym = nullptr;
ELF::Elf32_Word FlagWord;
SmallVector<SectionBase *, 3> GroupMembers;
@ -459,9 +462,16 @@ public:
ArrayRef<uint8_t> Contents;
explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
~GroupSection() {
if (Sym)
--Sym->ReferenceCount;
}
void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
void setSymbol(const Symbol *S) { Sym = S; }
void setSymbol(Symbol *S) {
Sym = S;
++Sym->ReferenceCount;
}
void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }

View File

@ -156,6 +156,7 @@ struct CopyConfig {
bool StripSections = false;
bool StripNonAlloc = false;
bool StripDWO = false;
bool StripUnneeded = false;
bool ExtractDWO = false;
bool LocalizeHidden = false;
bool Weaken = false;
@ -389,6 +390,13 @@ void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
return true;
}
// TODO: We might handle the 'null symbol' in a different way
// by probably handling it the same way as we handle 'null section' ?
if (Config.StripUnneeded && !Sym.ReferenceCount && Sym.Index != 0 &&
(Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
return true;
return false;
});
}
@ -479,6 +487,7 @@ CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded);
Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);