From df343b1764a398cdd8d96b227bbc7801e383b2f2 Mon Sep 17 00:00:00 2001 From: Paul Semel Date: Fri, 1 Jun 2018 16:19:46 +0000 Subject: [PATCH] [llvm-objcopy] Fix null symbol handling This fixes the bug where strip-all option was leading to a malformed outputted ELF file. Differential Revision: https://reviews.llvm.org/D47414 llvm-svn: 333772 --- .../tools/llvm-objcopy/keep-file-symbols.test | 9 ++++++ test/tools/llvm-objcopy/null-symbol.test | 28 +++++++++++++++++++ .../strip-all-and-keep-symbol.test | 9 ++++++ tools/llvm-objcopy/Object.cpp | 6 ++-- tools/llvm-objcopy/Object.h | 3 +- tools/llvm-objcopy/llvm-objcopy.cpp | 4 +-- 6 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 test/tools/llvm-objcopy/null-symbol.test diff --git a/test/tools/llvm-objcopy/keep-file-symbols.test b/test/tools/llvm-objcopy/keep-file-symbols.test index 04b07b7652b..85546887ae5 100644 --- a/test/tools/llvm-objcopy/keep-file-symbols.test +++ b/test/tools/llvm-objcopy/keep-file-symbols.test @@ -29,6 +29,15 @@ Symbols: #STRIPALL: Symbols [ #STRIPALL-NEXT: Symbol { +#STRIPALL-NEXT: Name: +#STRIPALL-NEXT: Value: 0x0 +#STRIPALL-NEXT: Size: 0 +#STRIPALL-NEXT: Binding: Local +#STRIPALL-NEXT: Type: None +#STRIPALL-NEXT: Other: 0 +#STRIPALL-NEXT: Section: Undefined +#STRIPALL-NEXT: } +#STRIPALL-NEXT: Symbol { #STRIPALL-NEXT: Name: foo #STRIPALL-NEXT: Value: 0x0 #STRIPALL-NEXT: Size: 0 diff --git a/test/tools/llvm-objcopy/null-symbol.test b/test/tools/llvm-objcopy/null-symbol.test new file mode 100644 index 00000000000..61ed4107839 --- /dev/null +++ b/test/tools/llvm-objcopy/null-symbol.test @@ -0,0 +1,28 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy %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: 8 + +#CHECK: Symbols [ +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: (0) +#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: } diff --git a/test/tools/llvm-objcopy/strip-all-and-keep-symbol.test b/test/tools/llvm-objcopy/strip-all-and-keep-symbol.test index b573356f090..72e17a7d224 100644 --- a/test/tools/llvm-objcopy/strip-all-and-keep-symbol.test +++ b/test/tools/llvm-objcopy/strip-all-and-keep-symbol.test @@ -58,6 +58,15 @@ Symbols: #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 diff --git a/tools/llvm-objcopy/Object.cpp b/tools/llvm-objcopy/Object.cpp index 6b656c6b2f7..606e0321288 100644 --- a/tools/llvm-objcopy/Object.cpp +++ b/tools/llvm-objcopy/Object.cpp @@ -200,8 +200,8 @@ void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) { } void SymbolTableSection::updateSymbols(function_ref Callable) { - for (auto &Sym : Symbols) - Callable(*Sym); + std::for_each(std::begin(Symbols) + 1, std::end(Symbols), + [Callable](SymPtr &Sym) { Callable(*Sym); }); std::stable_partition( std::begin(Symbols), std::end(Symbols), [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; }); @@ -211,7 +211,7 @@ void SymbolTableSection::updateSymbols(function_ref Callable) { void SymbolTableSection::removeSymbols( function_ref ToRemove) { Symbols.erase( - std::remove_if(std::begin(Symbols), std::end(Symbols), + std::remove_if(std::begin(Symbols) + 1, std::end(Symbols), [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }), std::end(Symbols)); Size = Symbols.size() * EntrySize; diff --git a/tools/llvm-objcopy/Object.h b/tools/llvm-objcopy/Object.h index d005f0d4902..c7939b4a498 100644 --- a/tools/llvm-objcopy/Object.h +++ b/tools/llvm-objcopy/Object.h @@ -367,7 +367,8 @@ public: SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility, uint16_t Shndx, uint64_t Sz); void addSymbolNames(); - bool empty() const { return Symbols.empty(); } + // An 'empty' symbol table still contains a null symbol. + bool empty() const { return Symbols.size() == 1; } const SectionBase *getStrTab() const { return SymbolNames; } const Symbol *getSymbolByIndex(uint32_t Index) const; Symbol *getSymbolByIndex(uint32_t Index); diff --git a/tools/llvm-objcopy/llvm-objcopy.cpp b/tools/llvm-objcopy/llvm-objcopy.cpp index bbd21d58823..0563af93290 100644 --- a/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/tools/llvm-objcopy/llvm-objcopy.cpp @@ -292,9 +292,7 @@ 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.Referenced && Sym.Index != 0 && + if (Config.StripUnneeded && !Sym.Referenced && (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) && Sym.Type != STT_FILE && Sym.Type != STT_SECTION) return true;