Skip to content

Commit

Permalink
Reland D76675 [llvm-objcopy] Match GNU behaviour regarding file symbols
Browse files Browse the repository at this point in the history
Don't error on Config.KeepFileSymbols for COFF and Mach-O.

Original description:

GNU objcopy removes STT_FILE symbols for strip-debug operations, and
keeps them for --discard-all operation. Match their behaviour for
llvm-objcopy.

Bug: android/ndk#1212

Differential Revision: https://reviews.llvm.org/D76675
  • Loading branch information
MaskRay committed Apr 21, 2020
1 parent 37a1c2e commit b14e9e3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 11 deletions.
55 changes: 55 additions & 0 deletions llvm/test/tools/llvm-objcopy/ELF/keep-file-symbols.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@
# RUN: llvm-strip --keep-file-symbols --strip-symbol foo %t -o %t4
# RUN: llvm-readobj --symbols %t4 | FileCheck %s --check-prefix=STRIP

# RUN: llvm-objcopy --keep-file-symbols --strip-debug %t %t5
# RUN: llvm-readobj --symbols %t5 | FileCheck %s --check-prefix=STRIPDEBUG
# RUN: llvm-objcopy --keep-file-symbols --strip-unneeded %t %t6
# RUN: llvm-readobj --symbols %t6 | FileCheck %s --check-prefix=STRIPDEBUG

# RUN: llvm-strip --keep-file-symbols --strip-debug %t -o %t7
# RUN: llvm-readobj --symbols %t7 | FileCheck %s --check-prefix=STRIPDEBUG
# RUN: llvm-strip --keep-file-symbols --strip-unneeded %t -o %t8
# RUN: llvm-readobj --symbols %t8 | FileCheck %s --check-prefix=STRIPDEBUG

!ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_X86_64
Sections:
- Name: .debugfoo
Type: SHT_PROGBITS
Content: "00000000"
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Expand All @@ -30,6 +43,9 @@ Symbols:
Type: STT_FUNC
Section: .text
Binding: STB_GLOBAL
- Name: debugfoo
Section: .debugfoo
Binding: STB_GLOBAL

#STRIPALL: Symbols [
#STRIPALL-NEXT: Symbol {
Expand Down Expand Up @@ -80,4 +96,43 @@ Symbols:
#STRIP-NEXT: Other: 0
#STRIP-NEXT: Section: .text
#STRIP-NEXT: }
#STRIP-NEXT: Symbol {
#STRIP-NEXT: Name: debugfoo
#STRIP-NEXT: Value: 0x0
#STRIP-NEXT: Size: 0
#STRIP-NEXT: Binding: Global
#STRIP-NEXT: Type: None
#STRIP-NEXT: Other: 0
#STRIP-NEXT: Section: .debugfoo
#STRIP-NEXT: }
#STRIP-NEXT:]

#STRIPDEBUG: Symbols [
#STRIPDEBUG-NEXT: Symbol {
#STRIPDEBUG-NEXT: Name:
#STRIPDEBUG-NEXT: Value: 0x0
#STRIPDEBUG-NEXT: Size: 0
#STRIPDEBUG-NEXT: Binding: Local
#STRIPDEBUG-NEXT: Type: None
#STRIPDEBUG-NEXT: Other: 0
#STRIPDEBUG-NEXT: Section: Undefined
#STRIPDEBUG-NEXT: }
#STRIPDEBUG-NEXT: Symbol {
#STRIPDEBUG-NEXT: Name: foo
#STRIPDEBUG-NEXT: Value: 0x0
#STRIPDEBUG-NEXT: Size: 0
#STRIPDEBUG-NEXT: Binding: Local
#STRIPDEBUG-NEXT: Type: File
#STRIPDEBUG-NEXT: Other: 0
#STRIPDEBUG-NEXT: Section: .text
#STRIPDEBUG-NEXT: }
#STRIPDEBUG-NEXT: Symbol {
#STRIPDEBUG-NEXT: Name: bar
#STRIPDEBUG-NEXT: Value: 0x0
#STRIPDEBUG-NEXT: Size: 0
#STRIPDEBUG-NEXT: Binding: Global
#STRIPDEBUG-NEXT: Type: Function
#STRIPDEBUG-NEXT: Other: 0
#STRIPDEBUG-NEXT: Section: .text
#STRIPDEBUG-NEXT: }
#STRIPDEBUG-NEXT:]
4 changes: 3 additions & 1 deletion llvm/test/tools/llvm-objcopy/ELF/strip-debug.test
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ Symbols:
- Name: debugfoo
Section: .debugfoo
Binding: STB_GLOBAL
- Name: filesymbol
Type: STT_FILE

# CHECK: SectionHeaderCount: 5

Expand All @@ -124,7 +126,7 @@ Symbols:
# CHECK: Name: .strtab
# CHECK: Name: .shstrtab

# Check that *only* foo is copied and not debugfoo
# Check that *only* foo is copied and not debugfoo or filesymbol.
# CHECK: Symbols [
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name:
Expand Down
6 changes: 3 additions & 3 deletions llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
!Config.SymbolsToLocalize.empty() || !Config.SymbolsToWeaken.empty() ||
!Config.SymbolsToKeepGlobal.empty() || !Config.SectionsToRename.empty() ||
!Config.SetSectionAlignment.empty() || Config.ExtractDWO ||
Config.KeepFileSymbols || Config.LocalizeHidden || Config.PreserveDates ||
Config.StripDWO || Config.StripNonAlloc || Config.StripSections ||
Config.Weaken || Config.DecompressDebugSections ||
Config.LocalizeHidden || Config.PreserveDates || Config.StripDWO ||
Config.StripNonAlloc || Config.StripSections || Config.Weaken ||
Config.DecompressDebugSections ||
Config.DiscardMode == DiscardType::Locals ||
!Config.SymbolsToAdd.empty() || Config.EntryExpr) {
return createStringError(llvm::errc::invalid_argument,
Expand Down
8 changes: 6 additions & 2 deletions llvm/tools/llvm-objcopy/CopyConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,10 @@ parseObjcopyOptions(ArrayRef<const char *> ArgsArr,
Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
Config.DecompressDebugSections =
InputArgs.hasArg(OBJCOPY_decompress_debug_sections);
if (Config.DiscardMode == DiscardType::All)
if (Config.DiscardMode == DiscardType::All) {
Config.StripDebug = true;
Config.KeepFileSymbols = true;
}
for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
if (Error E = Config.SymbolsToLocalize.addMatcher(NameOrPattern::create(
Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
Expand Down Expand Up @@ -938,8 +940,10 @@ parseStripOptions(ArrayRef<const char *> ArgsArr,
!Config.StripAllGNU && Config.SymbolsToRemove.empty())
Config.StripAll = true;

if (Config.DiscardMode == DiscardType::All)
if (Config.DiscardMode == DiscardType::All) {
Config.StripDebug = true;
Config.KeepFileSymbols = true;
}

Config.DeterministicArchives =
InputArgs.hasFlag(STRIP_enable_deterministic_archives,
Expand Down
3 changes: 3 additions & 0 deletions llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ static Error updateAndRemoveSymbols(const CopyConfig &Config, Object &Obj) {
if (Config.StripAll || Config.StripAllGNU)
return true;

if (Config.StripDebug && Sym.Type == STT_FILE)
return true;

if (Config.SymbolsToRemove.matches(Sym.Name))
return true;

Expand Down
9 changes: 4 additions & 5 deletions llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,10 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
!Config.SectionsToRename.empty() ||
!Config.UnneededSymbolsToRemove.empty() ||
!Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() ||
Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden ||
Config.PreserveDates || Config.StripAllGNU || Config.StripDWO ||
Config.StripNonAlloc || Config.StripSections || Config.Weaken ||
Config.DecompressDebugSections || Config.StripNonAlloc ||
Config.StripSections || Config.StripUnneeded ||
Config.ExtractDWO || Config.LocalizeHidden || Config.PreserveDates ||
Config.StripAllGNU || Config.StripDWO || Config.StripNonAlloc ||
Config.StripSections || Config.Weaken || Config.DecompressDebugSections ||
Config.StripNonAlloc || Config.StripSections || Config.StripUnneeded ||
Config.DiscardMode == DiscardType::Locals ||
!Config.SymbolsToAdd.empty() || Config.EntryExpr) {
return createStringError(llvm::errc::invalid_argument,
Expand Down

0 comments on commit b14e9e3

Please sign in to comment.