Commit 7cb25f53 authored by Derek Schuff's avatar Derek Schuff
Browse files

[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
parent d3816ef0
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
## 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 --implicit-check-not linking %s

# CHECK:      Sections:
# CHECK:        Name: .debug_info
# CHECK-NEXT:   Payload: DEADBEEF

## 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=KEEP

# KEEP: Sections:
# KEEP:   Name: .debug_info

--- !WASM
FileHeader:
  Version: 0x00000001
Sections:
  - Type: CUSTOM
    Name: linking
    Version: 2
  - Type: CUSTOM
    Name: .debug_info
    Payload: DEADBEEF
+44 −0
Original line number Diff line number Diff line
## Test --only-section.
# RUN: yaml2obj %s -o %t
# RUN: llvm-objcopy --only-section=producers %t %t2
# RUN: obj2yaml %t2 | FileCheck --implicit-check-not TYPE --implicit-check-not linking %s

## 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 | 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:

## 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:
  Version: 0x00000001
Sections:
  - Type: TYPE
    Signatures:
      - Index: 0
        ParamTypes:
          - I32
        ReturnTypes:
          - F32
  - Type: CUSTOM
    Name: linking
    Version: 2
  - Type: CUSTOM
    Name: producers
    Tools:
      - Name:   clang
        Version: 9.0.0
+46 −0
Original line number Diff line number Diff line
## Test that debug, name, and producers sections are stripped with --strip-all.
## Other sections (unknown to LLVM, such as foo) are not stripped by --strip-all.
# RUN: yaml2obj %s -o %t
# RUN: llvm-strip --strip-all %t
# RUN: obj2yaml %t | FileCheck --implicit-check-not producers --implicit-check-not .debug --implicit-check-not name %s

## The default no-arg behavior is the same as --strip-all.
# RUN: llvm-strip %t
# RUN: obj2yaml %t | FileCheck --implicit-check-not producers --implicit-check-not .debug --implicit-check-not name %s

# CHECK:      Sections:
# CHECK-NEXT:   - Type: TYPE
# CHECK:          Name: foo

--- !WASM
FileHeader:
  Version: 0x00000001
Sections:
  - Type: TYPE
    Signatures:
      - Index: 0
        ParamTypes: []
        ReturnTypes: []
  - Type: FUNCTION
    FunctionTypes: [ 0 ]
  - Type: CODE
    Functions:
      - Index: 0
        Locals: []
        Body: 0B
  - Type: CUSTOM
    Name: name
    FunctionNames:
     - Index:          0
       Name:           foo
  - Type: CUSTOM
    Name: producers
    Tools:
      - Name:   clang
        Version: 9.0.0
  - Type: CUSTOM
    Name: .debug_info
    Payload: DEADBEEF
  - Type: CUSTOM
    Name: foo
    Payload: CAFE
+52 −0
Original line number Diff line number Diff line
## Test that only debug sections are kept with --only-keep-debug.
# RUN: yaml2obj %s -o %t
# 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 --implicit-check-not=Name --check-prefix=CHECK --check-prefix=KEEP %s

# CHECK:      Sections:
# CHECK:        - Type: CUSTOM
# CHECK-NEXT:     Name: .debug_info
# CHECK:        - Type: CUSTOM
# CHECK-NEXT:     Name: .debug_line
# KEEP:           Name: foo

## Test that remove-section overrides 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-prefix=NOINFO --implicit-check-not=.debug_info

# NOINFO: Name: .debug_line

--- !WASM
FileHeader:
  Version: 0x00000001
Sections:
  - Type: TYPE
    Signatures:
      - Index: 0
        ParamTypes:
          - I32
        ReturnTypes:
          - F32
  - Type: CUSTOM
    Name: .debug_info
    Payload: CAFE1234
  - Type: CUSTOM
    Name: linking
    Version: 2
  - Type: CUSTOM
    Name: producers
    Tools:
      - Name:   clang
        Version: 9.0.0
  - Type: CUSTOM
    Name: .debug_line
    Payload: DEADBEEF
  - Type: CUSTOM
    Name: foo
    Payload: CAFE
+34 −0
Original line number Diff line number Diff line
## Test that --strip-all removes debug, linking, and producers sections, but not
## known or unknown-custom sections.
# RUN: yaml2obj %s -o %t
# RUN: llvm-objcopy --strip-all %t %t2
# RUN: obj2yaml %t2 | FileCheck --implicit-check-not=Type: %s

# CHECK:      Sections:
# CHECK-NEXT:   - Type: TYPE
# CHECK:        - Type: CUSTOM
# CHECK-NEXT:        Name: foo

--- !WASM
FileHeader:
  Version: 0x00000001
Sections:
  - Type: TYPE
    Signatures:
      - Index: 0
        ParamTypes: []
        ReturnTypes: []
  - Type: CUSTOM
    Name: linking
    Version: 2
  - Type: CUSTOM
    Name: producers
    Tools:
      - Name:   clang
        Version: 9.0.0
  - Type: CUSTOM
    Name: .debug_info
    Payload: DEADBEEF
  - Type: CUSTOM
    Name: foo
    Payload: CAFE
Loading