diff --git a/llvm/test/tools/llvm-objcopy/wasm/basic-keep.test b/llvm/test/tools/llvm-objcopy/wasm/basic-keep.test --- a/llvm/test/tools/llvm-objcopy/wasm/basic-keep.test +++ b/llvm/test/tools/llvm-objcopy/wasm/basic-keep.test @@ -1,42 +1,28 @@ -## Test that --keep-section keeps a section when stripping. +## Test that --keep-section keeps a debug section when stripping. # RUN: yaml2obj %s -o %t # RUN: llvm-objcopy --strip-all --keep-section=.debug_info %t %t2 -# RUN: obj2yaml %t2 | FileCheck %s +# RUN: obj2yaml %t2 | FileCheck --implicit-check-not linking %s # CHECK: Sections: -# CHECK-NEXT: - Type: TYPE # CHECK: Name: .debug_info # CHECK-NEXT: Payload: DEADBEEF -## Ensure that there are no more sections after .debug_info. -# CHECK-NOT: Type +# ## Test that keep overrides an explicit removal. # RUN: llvm-objcopy --remove-section=.debug_info --keep-section=.debug_info %t %t2 -# RUN: obj2yaml %t2 | FileCheck %s --check-prefix=REMOVE +# RUN: obj2yaml %t2 | FileCheck %s --check-prefix=KEEP -# REMOVE: Sections: -# REMOVE: - Type: TYPE -# REMOVE: - Type: CUSTOM -# REMOVE: Name: producers -# REMOVE: - Type: CUSTOM -# REMOVE: Name: .debug_info +# KEEP: Sections: +# KEEP: - Type: CUSTOM +# KEEP: Name: .debug_info --- !WASM FileHeader: Version: 0x00000001 Sections: - - Type: TYPE - Signatures: - - Index: 0 - ParamTypes: - - I32 - ReturnTypes: - - F32 - Type: CUSTOM - Name: producers - Tools: - - Name: clang - Version: 9.0.0 + Name: linking + Version: 2 - Type: CUSTOM Name: .debug_info Payload: DEADBEEF diff --git a/llvm/test/tools/llvm-objcopy/wasm/basic-only-section.test b/llvm/test/tools/llvm-objcopy/wasm/basic-only-section.test --- a/llvm/test/tools/llvm-objcopy/wasm/basic-only-section.test +++ b/llvm/test/tools/llvm-objcopy/wasm/basic-only-section.test @@ -1,15 +1,32 @@ ## Test --only-section. # RUN: yaml2obj %s -o %t # RUN: llvm-objcopy --only-section=producers %t %t2 -# RUN: obj2yaml %t2 | FileCheck %s +# RUN: obj2yaml %t2 | FileCheck --implicit-check-not TYPE --implicit-check-not linking %s -# This file has both known and custom sections. Check that only the producers section is left. +## Test that it's the same with only-section + keep-section (for the same section). +# RUN: llvm-objcopy --only-section=producers --keep-section=producers %t %t2 +# RUN: obj2yaml %t2 | FileCheck --implicit-check-not TYPE --implicit-check-not linking %s + +## Also Test that only-section overrides remove-section. +# RUN: llvm-objcopy --only-section=producers --remove-section=producers %t %t2 +# RUN: obj2yaml %t2 +# RUN: obj2yaml %t2 | FileCheck --implicit-check-not linking %s + +## This file has both known and custom sections. Check that only the producers section is left. # CHECK: Sections: # CHECK-NEXT: - Type: CUSTOM # CHECK-NEXT: Name: producers # CHECK-NEXT: Tools: -## Ensure that there are no more sections after producers. -# CHECK-NOT: Type + + +## Test that only-section + keep-section keeps both sections +# RUN: llvm-objcopy --only-section=producers --keep-section=linking %t %t2 +# RUN: obj2yaml %t2 | FileCheck --implicit-check-not=TYPE --check-prefix=KEEP %s + +# KEEP: Name: linking +# KEEP: Name: producers + + --- !WASM FileHeader: @@ -22,6 +39,9 @@ - I32 ReturnTypes: - F32 + - Type: CUSTOM + Name: linking + Version: 2 - Type: CUSTOM Name: producers Tools: diff --git a/llvm/test/tools/llvm-objcopy/wasm/basic-strip.test b/llvm/test/tools/llvm-objcopy/wasm/basic-strip.test --- a/llvm/test/tools/llvm-objcopy/wasm/basic-strip.test +++ b/llvm/test/tools/llvm-objcopy/wasm/basic-strip.test @@ -1,16 +1,15 @@ -## Test that all custom sections are stripped with --strip-all. +## Test that debug, linking, and producers sections are stripped with --strip-all. # RUN: yaml2obj %s -o %t # RUN: llvm-strip --strip-all %t -# RUN: obj2yaml %t | FileCheck %s +# RUN: obj2yaml %t | FileCheck --implicit-check-not producers --implicit-check-not .debug %s ## The default no-arg behavior is the same as --strip-all. # RUN: llvm-strip %t -# RUN: obj2yaml %t | FileCheck %s +# RUN: obj2yaml %t | FileCheck --implicit-check-not producers --implicit-check-not .debug %s # CHECK: Sections: # CHECK-NEXT: - Type: TYPE -# CHECK: Name: producers -# CHECK-NOT: .debug_info +# CHECK: Name: foo --- !WASM FileHeader: @@ -31,3 +30,6 @@ - Type: CUSTOM Name: .debug_info Payload: DEADBEEF + - Type: CUSTOM + Name: foo + Payload: CAFE diff --git a/llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test b/llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test --- a/llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test +++ b/llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test @@ -3,14 +3,28 @@ # RUN: llvm-strip --only-keep-debug %t # RUN: obj2yaml %t | FileCheck %s +## Test that keep-section overrides only-keep-debug. +# RUN: yaml2obj %s -o %t +# RUN: llvm-strip --only-keep-debug --keep-section=foo %t +# RUN: obj2yaml %t | FileCheck --check-prefix=CHECK --check-prefix=KEEP %s + +## Test that remove-section does not override only-keep-debug. +# RUN: yaml2obj %s -o %t +# RUN: llvm-strip --only-keep-debug --remove-section=.debug_info %t +# RUN: obj2yaml %t | FileCheck %s + # CHECK: Sections: # CHECK-NEXT: - Type: CUSTOM # CHECK-NEXT: Name: .debug_info -# CHECK: - Type: CUSTOM +# CHECK: - Type: +# CHECK-SAME: CUSTOM # CHECK-NEXT: Name: .debug_line +# KEEP: Name: foo ## Ensure that there are no more sections after .debug_line. # CHECK-NOT: Type + + --- !WASM FileHeader: Version: 0x00000001 @@ -25,6 +39,9 @@ - Type: CUSTOM Name: .debug_info Payload: CAFE1234 + - Type: CUSTOM + Name: linking + Version: 2 - Type: CUSTOM Name: producers Tools: @@ -33,3 +50,6 @@ - Type: CUSTOM Name: .debug_line Payload: DEADBEEF + - Type: CUSTOM + Name: foo + Payload: CAFE diff --git a/llvm/test/tools/llvm-objcopy/wasm/strip-debug.test b/llvm/test/tools/llvm-objcopy/wasm/strip-all.test copy from llvm/test/tools/llvm-objcopy/wasm/strip-debug.test copy to llvm/test/tools/llvm-objcopy/wasm/strip-all.test --- a/llvm/test/tools/llvm-objcopy/wasm/strip-debug.test +++ b/llvm/test/tools/llvm-objcopy/wasm/strip-all.test @@ -1,14 +1,13 @@ -## Test that debug sections are stripped with --strip-debug +## Test that --strip-all removes debug, linking, and producers sections, but not +## known or unknown-custom sections. # RUN: yaml2obj %s -o %t -# RUN: llvm-strip --strip-debug %t -# RUN: obj2yaml %t | FileCheck %s +# RUN: llvm-objcopy --strip-all %t %t2 +# RUN: obj2yaml %t2 | FileCheck --implicit-check-not=linking --implicit-check-not=.debug --implicit-check-not producers %s # CHECK: Sections: # CHECK-NEXT: - Type: TYPE -# CHECK: - Type: CUSTOM -# CHECK-NEXT: Name: producers -## Ensure that there are no more sections after producers. -# CHECK-NOT: Type +# CHECK: Name: foo + --- !WASM FileHeader: @@ -22,13 +21,16 @@ ReturnTypes: - F32 - Type: CUSTOM - Name: .debug_info - Payload: CAFE1234 + Name: linking + Version: 2 - Type: CUSTOM Name: producers Tools: - Name: clang Version: 9.0.0 - Type: CUSTOM - Name: .debug_line + Name: .debug_info Payload: DEADBEEF + - Type: CUSTOM + Name: foo + Payload: CAFE \ No newline at end of file diff --git a/llvm/test/tools/llvm-objcopy/wasm/strip-debug.test b/llvm/test/tools/llvm-objcopy/wasm/strip-debug.test --- a/llvm/test/tools/llvm-objcopy/wasm/strip-debug.test +++ b/llvm/test/tools/llvm-objcopy/wasm/strip-debug.test @@ -24,6 +24,11 @@ - Type: CUSTOM Name: .debug_info Payload: CAFE1234 +# - Type: CUSTOM +# Name: name +# FunctionNames: +# - Index: 1 +# Name: foo - Type: CUSTOM Name: producers Tools: @@ -31,4 +36,4 @@ Version: 9.0.0 - Type: CUSTOM Name: .debug_line - Payload: DEADBEEF + Payload: DEADBEEF \ No newline at end of file diff --git a/llvm/test/tools/llvm-objcopy/wasm/strip-reloc.test b/llvm/test/tools/llvm-objcopy/wasm/strip-reloc.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/wasm/strip-reloc.test @@ -0,0 +1,89 @@ +## Test that reloc sections are stripped by --strip-all. +## Test that name sections are stripped by --strip-debug. + +## These get a separate test because ObjectYaml understands relocs and names, +## so the test needs to be a valid object with relocs and names. + +# RUN: yaml2obj %s -o %t +# RUN: llvm-objcopy --strip-all %t %t2 +# RUN: obj2yaml %t2 | FileCheck --implicit-check-not=linking %s + +# Check that the known sections are still present +# CHECK: Sections: +# CHECK: - Type: TYPE +# CHECK: - Type: FUNCTION +# CHECK: - Type: CODE +# Check that there are no relocs against the code, +# CHECK-NOT: Relocations +# ... but that the functions are still there. +# CHECK: Functions: +# And check that there are no more sections. +# CHECK-NOT: - Name: + +## Check that strip-debug removes the name section +# RUN: yaml2obj %s -o %t +# RUN: llvm-objcopy --strip-debug %t %t2 +# RUN: obj2yaml %t2 | FileCheck --implicit-check-not=FunctionNames --check-prefix=NAME %s + +# Just a basic sanity check that strip-debug hasn't stripped everything. +# NAME: Sections: +# NAME: Name: linking + +--- !WASM +FileHeader: + Version: 0x00000001 +Sections: + - Type: TYPE + Signatures: + - Index: 0 + ParamTypes: + - I32 + ReturnTypes: + - F32 + - Index: 1 + ParamTypes: + - I32 + - I64 + ReturnTypes: [] + - Type: FUNCTION + FunctionTypes: + - 0 + - 1 + - Type: CODE + Relocations: + - Type: R_WASM_TABLE_INDEX_SLEB + Index: 0 + Offset: 0x00000006 + - Type: R_WASM_FUNCTION_INDEX_LEB + Index: 1 + Offset: 0x00000025 + Functions: + - Index: 0 + Locals: + - Type: I32 + Count: 3 + Body: 418080808000210020002101200111808080800000210220020F0B + - Index: 1 + Locals: + - Type: I32 + Count: 1 + Body: 108180808000210020000F0B + - Type: CUSTOM + Name: linking + Version: 2 + SymbolTable: + - Index: 0 + Kind: FUNCTION + Name: func1 + Flags: [ ] + Function: 0 + - Index: 1 + Kind: FUNCTION + Name: func2 + Flags: [ ] + Function: 1 + - Type: CUSTOM + Name: name + FunctionNames: + - Index: 1 + Name: foo \ No newline at end of file diff --git a/llvm/tools/llvm-objcopy/wasm/WasmObjcopy.cpp b/llvm/tools/llvm-objcopy/wasm/WasmObjcopy.cpp --- a/llvm/tools/llvm-objcopy/wasm/WasmObjcopy.cpp +++ b/llvm/tools/llvm-objcopy/wasm/WasmObjcopy.cpp @@ -30,6 +30,12 @@ return Sec.Name.startswith("reloc.") || Sec.Name == "linking"; } +// Sections which are known to be "comments" or informational and do not affect +// program semantics. +static bool isCommentSection(const Section &Sec) { + return Sec.Name == "producers"; +} + static Error dumpSectionToFile(StringRef SecName, StringRef Filename, Object &Obj) { for (const Section &Sec : Obj.Sections) { @@ -68,7 +74,8 @@ if (Config.StripAll) { RemovePred = [RemovePred](const Section &Sec) { - return RemovePred(Sec) || isDebugSection(Sec) || isLinkerSection(Sec); + return RemovePred(Sec) || isDebugSection(Sec) || isLinkerSection(Sec) || + isCommentSection(Sec); }; }