diff options
| author | Jordan Rupprecht <rupprecht@google.com> | 2019-10-17 20:51:00 +0000 |
|---|---|---|
| committer | Jordan Rupprecht <rupprecht@google.com> | 2019-10-17 20:51:00 +0000 |
| commit | edeebad7715774b8481103733dc5d52dac43bdf3 (patch) | |
| tree | c84c0c304f7142e3ecef79ccd5120179a6dc1e60 /llvm/test/tools/llvm-objcopy/ELF | |
| parent | 9c5d76ff4d15e2cabf976911bd150302ae5fdeea (diff) | |
| download | bcm5719-llvm-edeebad7715774b8481103733dc5d52dac43bdf3.tar.gz bcm5719-llvm-edeebad7715774b8481103733dc5d52dac43bdf3.zip | |
[llvm-objcopy] Add support for shell wildcards
Summary: GNU objcopy accepts the --wildcard flag to allow wildcard matching on symbol-related flags. (Note: it's implicitly true for section flags).
The basic syntax is to allow *, ?, \, and [] which work similarly to how they work in a shell. Additionally, starting a wildcard with ! causes that wildcard to prevent it from matching a flag.
Use an updated GlobPattern in libSupport to handle these patterns. It does not fully match the `fnmatch` used by GNU objcopy since named character classes (e.g. `[[:digit:]]`) are not supported, but this should support most existing use cases (mostly just `*` is what's used anyway).
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: nickdesaulniers, emaste, arichardson, hiraditya, jakehehrlich, abrachet, seiya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66613
llvm-svn: 375169
Diffstat (limited to 'llvm/test/tools/llvm-objcopy/ELF')
| -rw-r--r-- | llvm/test/tools/llvm-objcopy/ELF/wildcard-flags.test | 162 | ||||
| -rw-r--r-- | llvm/test/tools/llvm-objcopy/ELF/wildcard-syntax.test | 149 |
2 files changed, 311 insertions, 0 deletions
diff --git a/llvm/test/tools/llvm-objcopy/ELF/wildcard-flags.test b/llvm/test/tools/llvm-objcopy/ELF/wildcard-flags.test new file mode 100644 index 00000000000..29a91cd1dc6 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/ELF/wildcard-flags.test @@ -0,0 +1,162 @@ +## This test checks basic functionality of glob (or "shell wildcard") matching, +## as well as verifying all the relevant flags in llvm-objcopy and llvm-strip +## are configured correctly. +## For more detailed syntax tests, see wildcard-syntax.test. + +# RUN: yaml2obj %s > %t.o + +## Check that --regex and --wildcard cannot be used together. +# RUN: not llvm-objcopy --regex --wildcard %t.o %t.err.o 2>&1 \ +# RUN: | FileCheck %s --check-prefix=ERR +# RUN: not llvm-strip --regex --wildcard %t.o -o %t.err.o 2>&1 \ +# RUN: | FileCheck %s --check-prefix=ERR + +# ERR: error: --regex and --wildcard are incompatible + +## Check that section removal flags default to glob matches. + +## --keep-section: +# RUN: llvm-objcopy --strip-all --keep-section='.f*' %t.o %t.ksec.1.o +# RUN: llvm-readobj --sections %t.ksec.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,SEC,FOO-SEC +# RUN: llvm-strip --strip-all --keep-section='.f*' %t.o -o %t.ksec.2.o +# RUN: cmp %t.ksec.1.o %t.ksec.2.o + +## --only-section: +# RUN: llvm-objcopy --strip-all --only-section='.f*' %t.o %t.osec.1.o +# RUN: llvm-readobj --sections %t.osec.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,SEC,FOO-SEC + +## --remove-section: +# RUN: llvm-objcopy --strip-debug --remove-section='.s??tab' %t.o %t.rsec.1.o +# RUN: llvm-readobj --sections %t.rsec.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,SEC,FOO-SEC,BAR-SEC +# RUN: llvm-strip --strip-debug --remove-section='.s??tab' %t.o -o %t.rsec.2.o +# RUN: cmp %t.rsec.1.o %t.rsec.2.o + +## Check that symbol removal options default to literal matches. Adding -w +## enables glob support for these options. + +## --globalize-symbol: +# RUN: llvm-objcopy --globalize-symbol='*' %t.o %t.globsym.1.o +# RUN: llvm-readobj --symbols %t.globsym.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,LOCAL,FOO-SYM,BAR-SYM + +# RUN: llvm-objcopy -w --globalize-symbol='*' %t.o %t.globsym.2.o +# RUN: llvm-readobj --symbols %t.globsym.2.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,GLOBAL,FOO-SYM,BAR-SYM + +## --keep-symbol: +# RUN: llvm-objcopy --discard-all --keep-symbol='f*' %t.o %t.ksym.1.o +# RUN: llvm-readobj --symbols %t.ksym.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK +# RUN: llvm-strip --discard-all --keep-symbol='f*' %t.o -o %t.ksym.2.o +# RUN: cmp %t.ksym.1.o %t.ksym.2.o + +# RUN: llvm-objcopy --discard-all -w --keep-symbol='f*' %t.o %t.ksym.3.o +# RUN: llvm-readobj --symbols %t.ksym.3.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO-SYM +# RUN: llvm-strip --discard-all -w --keep-symbol='f*' %t.o -o %t.ksym.4.o +# RUN: cmp %t.ksym.3.o %t.ksym.4.o + +## --localize-symbol: +## Note: Use %t.globsym.2.o instead of %t.o since those symbols are global. +# RUN: llvm-objcopy --localize-symbol='*' %t.globsym.2.o %t.localsym.1.o +# RUN: llvm-readobj --symbols %t.localsym.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,GLOBAL,FOO-SYM,BAR-SYM + +# RUN: llvm-objcopy -w --localize-symbol='*' %t.globsym.2.o %t.localsym.2.o +# RUN: llvm-readobj --symbols %t.localsym.2.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,LOCAL,FOO-SYM,BAR-SYM + +## --strip-symbol: +# RUN: llvm-objcopy --strip-symbol='f*' %t.o %t.stripsym.1.o +# RUN: llvm-readobj --symbols %t.stripsym.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO-SYM,BAR-SYM +# RUN: llvm-strip --strip-symbol='f*' %t.o -o %t.stripsym.2.o +# RUN: cmp %t.stripsym.1.o %t.stripsym.2.o + +# RUN: llvm-objcopy -w --strip-symbol='f*' %t.o %t.stripsym.3.o +# RUN: llvm-readobj --symbols %t.stripsym.3.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,BAR-SYM +# RUN: llvm-strip -w --strip-symbol='f*' %t.o -o %t.stripsym.4.o +# RUN: cmp %t.stripsym.3.o %t.stripsym.4.o + +## --strip-unneeded-symbol: +# RUN: llvm-objcopy --strip-unneeded-symbol='f*' %t.o %t.stripunsym.1.o +# RUN: llvm-readobj --symbols %t.stripunsym.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO-SYM,BAR-SYM + +# RUN: llvm-objcopy -w --strip-unneeded-symbol='f*' %t.o %t.stripunsym.2.o +# RUN: llvm-readobj --symbols %t.stripunsym.2.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,BAR-SYM + +## --weaken-symbol: +## Note: Use %t.globsym.2.o instead of %t.o since those symbols are global. +# RUN: llvm-objcopy --weaken-symbol='*' %t.globsym.2.o %t.weaksym.1.o +# RUN: llvm-readobj --symbols %t.weaksym.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,GLOBAL,FOO-SYM,BAR-SYM + +# RUN: llvm-objcopy -w --weaken-symbol='*' %t.globsym.2.o %t.weaksym.2.o +# RUN: llvm-readobj --symbols %t.weaksym.2.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,WEAK,FOO-SYM,BAR-SYM + +## --keep-global-symbol: +## Note: Use %t.globsym.2.o instead of %t.o since those symbols are global. +# RUN: llvm-objcopy --keep-global-symbol='*' %t.globsym.2.o %t.keepgsym.1.o +# RUN: llvm-readobj --symbols %t.keepgsym.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,LOCAL,FOO-SYM,BAR-SYM + +# RUN: llvm-objcopy -w --keep-global-symbol='*' %t.globsym.2.o %t.keepgsym.2.o +# RUN: llvm-readobj --symbols %t.keepgsym.2.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,GLOBAL,FOO-SYM,BAR-SYM + +## Check that -w is accepted as an alias for --wildcard. +# RUN: llvm-objcopy --wildcard --keep-global-symbol='*' %t.globsym.2.o %t.keepgsym.3.o +# RUN: cmp %t.keepgsym.2.o %t.keepgsym.3.o + +# CHECK: LoadName: +# CHECK: Name: (0) + +# FOO-SEC: Name: .foo + +# FOO-SYM: Name: foo +# GLOBAL: Binding: Global +# WEAK: Binding: Weak +# LOCAL: Binding: Local + +# BAR-SEC: Name: .bar +# BAR-SYM: Name: bar +# GLOBAL: Binding: Global +# WEAK: Binding: Weak +# LOCAL: Binding: Local + +# SEC: Name: .shstrtab + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_PROGBITS + - Name: .bar + Type: SHT_PROGBITS +Symbols: + - Name: foo + Type: STT_FUNC + Section: .foo + - Name: bar + Type: STT_FUNC + Section: .foo diff --git a/llvm/test/tools/llvm-objcopy/ELF/wildcard-syntax.test b/llvm/test/tools/llvm-objcopy/ELF/wildcard-syntax.test new file mode 100644 index 00000000000..05642896727 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/ELF/wildcard-syntax.test @@ -0,0 +1,149 @@ +## This test checks that llvm-objcopy accepts glob (or "shell wildcard") syntax +## for the --wildcard (-w) flag correctly. + +# RUN: yaml2obj --docnum=1 %s > %t.o + +## * matches all characters. +# RUN: llvm-objcopy --remove-section='.f*' %t.o %t.glob.o +# RUN: llvm-readobj --sections %t.glob.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,BAR + +## Glob matches are full matches. ("*a" does not match ".bar"). +# RUN: llvm-objcopy --remove-section='*a' %t.o %t.full.o +# RUN: llvm-readobj --sections %t.full.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO,BAR + +## ? matches one character. +# RUN: llvm-objcopy --remove-section='.b?r' %t.o %t.question.o +# RUN: llvm-readobj --sections %t.question.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO + +## ! (as a leading character) prevents matches, and is not dependent on +## ordering. +# RUN: llvm-objcopy --remove-section='.???' --remove-section='!.f*' \ +# RUN: %t.o %t.negmatch1.o +# RUN: llvm-readobj --sections %t.negmatch1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO +# RUN: llvm-objcopy --remove-section='!.f*' --remove-section='.???' \ +# RUN: %t.o %t.negmatch2.o +# RUN: llvm-readobj --sections %t.negmatch2.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO +# RUN: llvm-objcopy --remove-section='.???' --remove-section='!.f*' \ +# RUN: --remove-section='.???' %t.o %t.negmatch3.o +# RUN: llvm-readobj --sections %t.negmatch3.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO + +## [a-z] matches a range of characters. +# RUN: llvm-objcopy --remove-section='.[a-c][a-a][q-s]' %t.o %t.range.o +# RUN: llvm-readobj --sections %t.range.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO + +## [^a-z] or [!a-z] match a negated range of characters. +# RUN: llvm-objcopy --remove-section='.[^x]oo' %t.o %t.negrange.1.o +# RUN: llvm-readobj --sections %t.negrange.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,BAR +# RUN: llvm-objcopy --remove-section='.[!x]oo' %t.o %t.negrange.2.o +# RUN: llvm-readobj --sections %t.negrange.2.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,BAR + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_PROGBITS + - Name: .bar + Type: SHT_PROGBITS + +## Use a separate test file with special characters for the following tests. + +# RUN: yaml2obj --docnum=2 %s > %t.special.o + +## \ escapes wildcard characters. +# RUN: llvm-objcopy --remove-section='\*' %t.special.o %t.escape.1.o +# RUN: llvm-readobj --sections %t.escape.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,DOT,QUESTION,LEFT-BRACKET,RIGHT-BRACKET,INVALID-GLOB,Z,XYZ,FOO +# RUN: llvm-objcopy --remove-section='\?' %t.special.o %t.escape.2.o +# RUN: llvm-readobj --sections %t.escape.2.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,DOT,ASTERISK,LEFT-BRACKET,RIGHT-BRACKET,INVALID-GLOB,Z,XYZ,FOO + +## Special characters are not treated like regular expression characters. +# RUN: llvm-objcopy --remove-section='.' %t.special.o %t.dot.o +# RUN: llvm-readobj --sections %t.dot.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,ASTERISK,QUESTION,LEFT-BRACKET,RIGHT-BRACKET,INVALID-GLOB,Z,XYZ,FOO + +## Special characters in character classes are treated literally. +## [*] should not get expanded to [.*], which would match both '.' and '*' +# RUN: llvm-objcopy --remove-section='[*]' %t.special.o %t.class.1.o +# RUN: llvm-readobj --sections %t.class.1.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,DOT,QUESTION,LEFT-BRACKET,RIGHT-BRACKET,INVALID-GLOB,Z,XYZ,FOO + +## ] doesn't close the character class as a first character. This glob matches +## a single character which is one of ']xyz'. ']' and 'z' are removed, and more explicitly, +## section 'xyz]' is not removed, i.e. the glob is not interpreted as "an empty +## character class followed by 'xyz]'" +# RUN: llvm-objcopy --remove-section='[]xyz]' %t.special.o %t.class.2.o +# RUN: llvm-readobj --sections %t.class.2.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,DOT,ASTERISK,QUESTION,LEFT-BRACKET,INVALID-GLOB,XYZ,FOO + +## An invalid glob expression is interpreted as a literal instead. +# RUN: llvm-objcopy --remove-section='][]' %t.special.o %t.class.3.o 2>&1 \ +# RUN: | FileCheck %s --check-prefix=WARN +# RUN: llvm-readobj --sections %t.class.3.o \ +# RUN: | FileCheck %s --implicit-check-not=Name: \ +# RUN: --check-prefixes=CHECK,DOT,ASTERISK,QUESTION,LEFT-BRACKET,RIGHT-BRACKET,Z,XYZ,FOO + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: . + Type: SHT_PROGBITS + - Name: '*' + Type: SHT_PROGBITS + - Name: '?' + Type: SHT_PROGBITS + - Name: '[' + Type: SHT_PROGBITS + - Name: ']' + Type: SHT_PROGBITS + - Name: '][]' + Type: SHT_PROGBITS + - Name: z + Type: SHT_PROGBITS + - Name: 'xyz]' + Type: SHT_PROGBITS + - Name: '[]xyz]' + Type: SHT_PROGBITS + - Name: .foo + Type: SHT_PROGBITS + +# WARN: warning: invalid glob pattern: ][] + +# CHECK: LoadName: +# CHECK: Name: (0) +# DOT: Name: . +# ASTERISK: Name: * +# QUESTION: Name: ? +# LEFT-BRACKET: Name: [ +# RIGHT-BRACKET: Name: ] +# INVALID-GLOB: Name: ][] +# Z: Name: z +# XYZ: Name: xyz] +# XYZ: Name: []xyz] +# FOO: Name: .foo +# BAR: Name: .bar +# CHECK: Name: .symtab +# CHECK: Name: .strtab +# CHECK: Name: .shstrtab |

