Skip to content

Commit 7cb25f5

Browse files
committed
[llvm-strip][WebAssembly] Support strip flags
Summary: Add support for the basic section stripping (and keeping) flags for wasm: strip with no flags, --strip-all, --strip-debug, --only-section, --keep-section, and --only-keep-debug. Factor section removal into a function and use a predicate chain like the ELF implementation. Reviewers: jhenderson, sbc100 Differential Revision: https://reviews.llvm.org/D73820
1 parent d3816ef commit 7cb25f5

File tree

9 files changed

+380
-9
lines changed

9 files changed

+380
-9
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Test that --keep-section keeps a debug section when stripping.
2+
# RUN: yaml2obj %s -o %t
3+
# RUN: llvm-objcopy --strip-all --keep-section=.debug_info %t %t2
4+
# RUN: obj2yaml %t2 | FileCheck --implicit-check-not linking %s
5+
6+
# CHECK: Sections:
7+
# CHECK: Name: .debug_info
8+
# CHECK-NEXT: Payload: DEADBEEF
9+
10+
## Test that keep overrides an explicit removal.
11+
# RUN: llvm-objcopy --remove-section=.debug_info --keep-section=.debug_info %t %t2
12+
# RUN: obj2yaml %t2 | FileCheck %s --check-prefix=KEEP
13+
14+
# KEEP: Sections:
15+
# KEEP: Name: .debug_info
16+
17+
--- !WASM
18+
FileHeader:
19+
Version: 0x00000001
20+
Sections:
21+
- Type: CUSTOM
22+
Name: linking
23+
Version: 2
24+
- Type: CUSTOM
25+
Name: .debug_info
26+
Payload: DEADBEEF
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## Test --only-section.
2+
# RUN: yaml2obj %s -o %t
3+
# RUN: llvm-objcopy --only-section=producers %t %t2
4+
# RUN: obj2yaml %t2 | FileCheck --implicit-check-not TYPE --implicit-check-not linking %s
5+
6+
## Test that it's the same with only-section + keep-section (for the same section).
7+
# RUN: llvm-objcopy --only-section=producers --keep-section=producers %t %t2
8+
# RUN: obj2yaml %t2 | FileCheck --implicit-check-not TYPE --implicit-check-not linking %s
9+
10+
## Also test that only-section overrides remove-section.
11+
# RUN: llvm-objcopy --only-section=producers --remove-section=producers %t %t2
12+
# RUN: obj2yaml %t2 | FileCheck --implicit-check-not linking %s
13+
14+
## This file has both known and custom sections. Check that only the producers section is left.
15+
# CHECK: Sections:
16+
# CHECK-NEXT: - Type: CUSTOM
17+
# CHECK-NEXT: Name: producers
18+
# CHECK-NEXT: Tools:
19+
20+
## Test that only-section + keep-section keeps both sections.
21+
# RUN: llvm-objcopy --only-section=producers --keep-section=linking %t %t2
22+
# RUN: obj2yaml %t2 | FileCheck --implicit-check-not=TYPE --check-prefix=KEEP %s
23+
# KEEP: Name: linking
24+
# KEEP: Name: producers
25+
26+
--- !WASM
27+
FileHeader:
28+
Version: 0x00000001
29+
Sections:
30+
- Type: TYPE
31+
Signatures:
32+
- Index: 0
33+
ParamTypes:
34+
- I32
35+
ReturnTypes:
36+
- F32
37+
- Type: CUSTOM
38+
Name: linking
39+
Version: 2
40+
- Type: CUSTOM
41+
Name: producers
42+
Tools:
43+
- Name: clang
44+
Version: 9.0.0
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Test that debug, name, and producers sections are stripped with --strip-all.
2+
## Other sections (unknown to LLVM, such as foo) are not stripped by --strip-all.
3+
# RUN: yaml2obj %s -o %t
4+
# RUN: llvm-strip --strip-all %t
5+
# RUN: obj2yaml %t | FileCheck --implicit-check-not producers --implicit-check-not .debug --implicit-check-not name %s
6+
7+
## The default no-arg behavior is the same as --strip-all.
8+
# RUN: llvm-strip %t
9+
# RUN: obj2yaml %t | FileCheck --implicit-check-not producers --implicit-check-not .debug --implicit-check-not name %s
10+
11+
# CHECK: Sections:
12+
# CHECK-NEXT: - Type: TYPE
13+
# CHECK: Name: foo
14+
15+
--- !WASM
16+
FileHeader:
17+
Version: 0x00000001
18+
Sections:
19+
- Type: TYPE
20+
Signatures:
21+
- Index: 0
22+
ParamTypes: []
23+
ReturnTypes: []
24+
- Type: FUNCTION
25+
FunctionTypes: [ 0 ]
26+
- Type: CODE
27+
Functions:
28+
- Index: 0
29+
Locals: []
30+
Body: 0B
31+
- Type: CUSTOM
32+
Name: name
33+
FunctionNames:
34+
- Index: 0
35+
Name: foo
36+
- Type: CUSTOM
37+
Name: producers
38+
Tools:
39+
- Name: clang
40+
Version: 9.0.0
41+
- Type: CUSTOM
42+
Name: .debug_info
43+
Payload: DEADBEEF
44+
- Type: CUSTOM
45+
Name: foo
46+
Payload: CAFE
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Test that only debug sections are kept with --only-keep-debug.
2+
# RUN: yaml2obj %s -o %t
3+
# RUN: llvm-strip --only-keep-debug %t
4+
# RUN: obj2yaml %t | FileCheck %s
5+
6+
## Test that keep-section overrides only-keep-debug.
7+
# RUN: yaml2obj %s -o %t
8+
# RUN: llvm-strip --only-keep-debug --keep-section=foo %t
9+
# RUN: obj2yaml %t | FileCheck --implicit-check-not=Name --check-prefix=CHECK --check-prefix=KEEP %s
10+
11+
# CHECK: Sections:
12+
# CHECK: - Type: CUSTOM
13+
# CHECK-NEXT: Name: .debug_info
14+
# CHECK: - Type: CUSTOM
15+
# CHECK-NEXT: Name: .debug_line
16+
# KEEP: Name: foo
17+
18+
## Test that remove-section overrides only-keep-debug.
19+
# RUN: yaml2obj %s -o %t
20+
# RUN: llvm-strip --only-keep-debug --remove-section=.debug_info %t
21+
# RUN: obj2yaml %t | FileCheck %s --check-prefix=NOINFO --implicit-check-not=.debug_info
22+
23+
# NOINFO: Name: .debug_line
24+
25+
--- !WASM
26+
FileHeader:
27+
Version: 0x00000001
28+
Sections:
29+
- Type: TYPE
30+
Signatures:
31+
- Index: 0
32+
ParamTypes:
33+
- I32
34+
ReturnTypes:
35+
- F32
36+
- Type: CUSTOM
37+
Name: .debug_info
38+
Payload: CAFE1234
39+
- Type: CUSTOM
40+
Name: linking
41+
Version: 2
42+
- Type: CUSTOM
43+
Name: producers
44+
Tools:
45+
- Name: clang
46+
Version: 9.0.0
47+
- Type: CUSTOM
48+
Name: .debug_line
49+
Payload: DEADBEEF
50+
- Type: CUSTOM
51+
Name: foo
52+
Payload: CAFE
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## Test that --strip-all removes debug, linking, and producers sections, but not
2+
## known or unknown-custom sections.
3+
# RUN: yaml2obj %s -o %t
4+
# RUN: llvm-objcopy --strip-all %t %t2
5+
# RUN: obj2yaml %t2 | FileCheck --implicit-check-not=Type: %s
6+
7+
# CHECK: Sections:
8+
# CHECK-NEXT: - Type: TYPE
9+
# CHECK: - Type: CUSTOM
10+
# CHECK-NEXT: Name: foo
11+
12+
--- !WASM
13+
FileHeader:
14+
Version: 0x00000001
15+
Sections:
16+
- Type: TYPE
17+
Signatures:
18+
- Index: 0
19+
ParamTypes: []
20+
ReturnTypes: []
21+
- Type: CUSTOM
22+
Name: linking
23+
Version: 2
24+
- Type: CUSTOM
25+
Name: producers
26+
Tools:
27+
- Name: clang
28+
Version: 9.0.0
29+
- Type: CUSTOM
30+
Name: .debug_info
31+
Payload: DEADBEEF
32+
- Type: CUSTOM
33+
Name: foo
34+
Payload: CAFE
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## Test that debug sections (but not linking or names) are stripped with --strip-debug
2+
# RUN: yaml2obj %s -o %t
3+
# RUN: llvm-strip --strip-debug %t
4+
# RUN: obj2yaml %t | FileCheck --implicit-check-not=.debug %s
5+
6+
# CHECK: Sections:
7+
# CHECK-NEXT: - Type: TYPE
8+
# CHECK: Name: linking
9+
# CHECK: Name: name
10+
# CHECK-NEXT: FunctionNames:
11+
# CHECK: Name: producers
12+
13+
--- !WASM
14+
FileHeader:
15+
Version: 0x00000001
16+
Sections:
17+
- Type: TYPE
18+
Signatures:
19+
- Index: 0
20+
ParamTypes: []
21+
ReturnTypes: []
22+
- Type: FUNCTION
23+
FunctionTypes: [ 0 ]
24+
- Type: CODE
25+
Functions:
26+
- Index: 0
27+
Locals: []
28+
Body: 0B
29+
- Type: CUSTOM
30+
Name: .debug_info
31+
Payload: CAFE1234
32+
- Type: CUSTOM
33+
Name: linking
34+
Version: 2
35+
SymbolTable:
36+
- Index: 0
37+
Kind: FUNCTION
38+
Name: foo
39+
Flags: [ BINDING_LOCAL ]
40+
Function: 0
41+
- Type: CUSTOM
42+
Name: name
43+
FunctionNames:
44+
- Index: 0
45+
Name: foo
46+
- Type: CUSTOM
47+
Name: producers
48+
Tools:
49+
- Name: clang
50+
Version: 9.0.0
51+
- Type: CUSTOM
52+
Name: .debug_line
53+
Payload: DEADBEEF
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## Test that linking, reloc, and name sections are stripped by --strip-all.
2+
3+
## These get a separate test because ObjectYaml understands relocs and names,
4+
## so the test needs to be a valid object with relocs and names.
5+
6+
# RUN: yaml2obj %s -o %t
7+
# RUN: llvm-objcopy --strip-all %t %t2
8+
# RUN: obj2yaml %t2 | FileCheck --implicit-check-not=Type: %s
9+
10+
## Check that the known sections are still present.
11+
# CHECK: Sections:
12+
# CHECK: - Type: TYPE
13+
# CHECK: - Type: FUNCTION
14+
# CHECK: - Type: CODE
15+
## Check that there are still functions in the code section.
16+
# CHECK: Functions:
17+
18+
--- !WASM
19+
FileHeader:
20+
Version: 0x00000001
21+
Sections:
22+
- Type: TYPE
23+
Signatures:
24+
- Index: 0
25+
ParamTypes: []
26+
ReturnTypes: []
27+
- Type: FUNCTION
28+
FunctionTypes: [ 0 ]
29+
- Type: CODE
30+
Relocations:
31+
- Type: R_WASM_FUNCTION_INDEX_LEB
32+
Index: 0
33+
Offset: 0x4
34+
Functions:
35+
- Index: 0
36+
Locals: []
37+
Body: 1080808080000B
38+
- Type: CUSTOM
39+
Name: linking
40+
Version: 2
41+
SymbolTable:
42+
- Index: 0
43+
Kind: FUNCTION
44+
Name: foo
45+
Flags: [ BINDING_LOCAL ]
46+
Function: 0
47+
- Type: CUSTOM
48+
Name: name
49+
FunctionNames:
50+
- Index: 0
51+
Name: foo

llvm/tools/llvm-objcopy/ConfigManager.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,16 +614,15 @@ Expected<const WasmConfig &> ConfigManager::getWasmConfig() const {
614614
!Common.AllocSectionsPrefix.empty() ||
615615
Common.DiscardMode != DiscardType::None || ELF.NewSymbolVisibility ||
616616
!Common.SymbolsToAdd.empty() || !Common.RPathToAdd.empty() ||
617-
!Common.OnlySection.empty() || !Common.SymbolsToGlobalize.empty() ||
618-
!Common.SymbolsToKeep.empty() || !Common.SymbolsToLocalize.empty() ||
619-
!Common.SymbolsToRemove.empty() ||
617+
!Common.SymbolsToGlobalize.empty() || !Common.SymbolsToLocalize.empty() ||
618+
!Common.SymbolsToKeep.empty() || !Common.SymbolsToRemove.empty() ||
620619
!Common.UnneededSymbolsToRemove.empty() ||
621620
!Common.SymbolsToWeaken.empty() || !Common.SymbolsToKeepGlobal.empty() ||
622621
!Common.SectionsToRename.empty() || !Common.SetSectionAlignment.empty() ||
623622
!Common.SetSectionFlags.empty() || !Common.SymbolsToRename.empty()) {
624623
return createStringError(
625624
llvm::errc::invalid_argument,
626-
"only add-section, dump-section, and remove-section are supported");
625+
"only flags for section dumping, removal, and addition are supported");
627626
}
628627

629628
return Wasm;

0 commit comments

Comments
 (0)