Index: test/tools/llvm-objcopy/keep-file-symbols.test =================================================================== --- test/tools/llvm-objcopy/keep-file-symbols.test +++ test/tools/llvm-objcopy/keep-file-symbols.test @@ -29,6 +29,15 @@ #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 Index: test/tools/llvm-objcopy/null-symbol.test =================================================================== --- /dev/null +++ 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: } Index: test/tools/llvm-objcopy/strip-all-and-keep-symbol.test =================================================================== --- test/tools/llvm-objcopy/strip-all-and-keep-symbol.test +++ test/tools/llvm-objcopy/strip-all-and-keep-symbol.test @@ -58,6 +58,15 @@ #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 Index: tools/llvm-objcopy/Object.h =================================================================== --- tools/llvm-objcopy/Object.h +++ tools/llvm-objcopy/Object.h @@ -367,7 +367,8 @@ 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); Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -200,8 +200,9 @@ } void SymbolTableSection::updateSymbols(function_ref Callable) { - for (auto &Sym : Symbols) - Callable(*Sym); + for (auto Sym = std::begin(Symbols) + 1, E = std::end(Symbols); Sym != E; + ++Sym) + Callable(**Sym); std::stable_partition( std::begin(Symbols), std::end(Symbols), [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; }); @@ -211,7 +212,7 @@ 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; Index: tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -292,9 +292,7 @@ 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;