xref: /llvm-project/llvm/test/tools/llvm-objcopy/ELF/compress-sections.s (revision 07942987b58558a8bcb2cfb7d8bc3d46b1cb1d2b)
1# REQUIRES: x86-registered-target, zlib, zstd
2
3# RUN: rm -rf %t && mkdir %t && cd %t
4# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o a.o
5## '*0=none' wins because it is the last. '*0' sections are decompressed (if originally compressed) or kept unchanged (if uncompressed).
6## No section is named 'nomatch'. The third option is a no-op.
7# RUN: llvm-objcopy a.o out --compress-sections='*0=zlib' --compress-sections '*0=none' --compress-sections 'nomatch=none' 2>&1 | count 0
8# RUN: llvm-readelf -S out | FileCheck %s --check-prefix=CHECK1
9
10# CHECK1:      Name           Type          Address     Off      Size     ES Flg Lk Inf Al
11# CHECK1:      .text          PROGBITS [[#%x,TEXT:]]    [[#%x,]] [[#%x,]] 00 AX   0   0  4
12# CHECK1:      foo0           PROGBITS [[#%x,FOO0:]]    [[#%x,]] [[#%x,]] 00 A    0   0  8
13# CHECK1-NEXT: .relafoo0      RELA     [[#%x,]]         [[#%x,]] [[#%x,]] 18  I  11   3  8
14# CHECK1-NEXT: foo1           PROGBITS [[#%x,FOO1:]]    [[#%x,]] [[#%x,]] 00 A    0   0  8
15# CHECK1-NEXT: .relafoo1      RELA     [[#%x,]]         [[#%x,]] [[#%x,]] 18  I  11   5  8
16# CHECK1:      nonalloc0      PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00      0   0  8
17# CHECK1-NEXT: .relanonalloc0 RELA     [[#%x,]]         [[#%x,]] [[#%x,]] 18  I  11   7  8
18# CHECK1-NEXT: nonalloc1      PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00      0   0  8
19# CHECK1-NEXT: .debug_str     PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 01 MS   0   0  1
20
21## Mixing zlib and zstd.
22# RUN: llvm-objcopy a.o out2 --compress-sections '*c0=zlib' --compress-sections .debug_str=zstd
23# RUN: llvm-readelf -Sr -x nonalloc0 -x .debug_str out2 2>&1 | FileCheck %s --check-prefix=CHECK2
24# RUN: llvm-readelf -z -x nonalloc0 -x .debug_str out2 | FileCheck %s --check-prefix=CHECK2DE
25
26# CHECK2:      Name           Type          Address     Off      Size     ES Flg Lk Inf Al
27# CHECK2:      .text          PROGBITS [[#%x,TEXT:]]    [[#%x,]] [[#%x,]] 00 AX   0   0  4
28# CHECK2:      foo0           PROGBITS [[#%x,FOO0:]]    [[#%x,]] [[#%x,]] 00 A    0   0  8
29# CHECK2-NEXT: .relafoo0      RELA     [[#%x,]]         [[#%x,]] [[#%x,]] 18  I  11   3  8
30# CHECK2-NEXT: foo1           PROGBITS [[#%x,FOO1:]]    [[#%x,]] [[#%x,]] 00 A    0   0  8
31# CHECK2-NEXT: .relafoo1      RELA     [[#%x,]]         [[#%x,]] [[#%x,]] 18  I  11   5  8
32# CHECK2:      nonalloc0      PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00   C  0   0  8
33# CHECK2-NEXT: .relanonalloc0 RELA     [[#%x,]]         [[#%x,]] [[#%x,]] 18  IC 11   7  8
34# CHECK2-NEXT: nonalloc1      PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00      0   0  8
35# CHECK2-NEXT: .debug_str     PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 01 MSC  0   0  8
36
37## llvm-readelf -r doesn't support SHF_COMPRESSED SHT_RELA.
38# CHECK2: warning: {{.*}}: unable to read relocations from SHT_RELA section with index 8: section [index 8] has an invalid sh_size ([[#]]) which is not a multiple of its sh_entsize (24)
39
40# CHECK2:      Hex dump of section 'nonalloc0':
41## zlib with ch_size=0x10
42# CHECK2-NEXT: 01000000 00000000 10000000 00000000
43# CHECK2-NEXT: 08000000 00000000 {{.*}}
44# CHECK2:      Hex dump of section '.debug_str':
45## zstd with ch_size=0x38
46# CHECK2-NEXT: 02000000 00000000 38000000 00000000
47# CHECK2-NEXT: 01000000 00000000 {{.*}}
48
49# CHECK2DE:       Hex dump of section 'nonalloc0':
50# CHECK2DE-NEXT:  0x00000000 00000000 00000000 00000000 00000000 ................
51# CHECK2DE-EMPTY:
52# CHECK2DE-NEXT:  Hex dump of section '.debug_str':
53# CHECK2DE-NEXT:  0x00000000 41414141 41414141 41414141 41414141 AAAAAAAAAAAAAAAA
54
55## --decompress-debug-sections takes precedence, even if it is before --compress-sections.
56# RUN: llvm-objcopy a.o out3 --decompress-debug-sections --compress-sections .debug_str=zstd
57# RUN: llvm-readelf -S out3 | FileCheck %s --check-prefix=CHECK3
58
59# CHECK3:      .debug_str PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 01 MS   0   0  1
60
61# RUN: llvm-objcopy a.o out4 --compress-sections '*0=zlib'
62# RUN: llvm-readelf -S out4 | FileCheck %s --check-prefix=CHECK4
63
64# CHECK4:      Name           Type          Address     Off      Size     ES Flg Lk Inf Al
65# CHECK4:      .text          PROGBITS [[#%x,TEXT:]]    [[#%x,]] [[#%x,]] 00  AX  0   0  4
66# CHECK4:      foo0           PROGBITS [[#%x,FOO0:]]    [[#%x,]] [[#%x,]] 00  AC  0   0  8
67# CHECK4-NEXT: .relafoo0      RELA     [[#%x,]]         [[#%x,]] [[#%x,]] 18  IC 11   3  8
68# CHECK4-NEXT: foo1           PROGBITS [[#%x,FOO1:]]    [[#%x,]] [[#%x,]] 00   A  0   0  8
69# CHECK4-NEXT: .relafoo1      RELA     [[#%x,]]         [[#%x,]] [[#%x,]] 18   I 11   5  8
70# CHECK4:      nonalloc0      PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00   C  0   0  8
71# CHECK4-NEXT: .relanonalloc0 RELA     [[#%x,]]         [[#%x,]] [[#%x,]] 18  IC 11   7  8
72# CHECK4-NEXT: nonalloc1      PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00      0   0  8
73# CHECK4-NEXT: .debug_str     PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 01  MS  0   0  1
74
75## If a section is already compressed, compression request for another format is ignored.
76# RUN: llvm-objcopy a.o out5 --compress-sections 'nonalloc0=zlib'
77# RUN: llvm-readelf -x nonalloc0 out5 | FileCheck %s --check-prefix=CHECK5
78# RUN: llvm-objcopy out5 out5a --compress-sections 'nonalloc0=zstd'
79# RUN: cmp out5 out5a
80
81# CHECK5:      Hex dump of section 'nonalloc0':
82## zlib with ch_size=0x10
83# CHECK5-NEXT: 01000000 00000000 10000000 00000000
84# CHECK5-NEXT: 08000000 00000000 {{.*}}
85
86# RUN: not llvm-objcopy --compress-sections=foo a.o out 2>&1 | \
87# RUN:   FileCheck %s --check-prefix=ERR1 --implicit-check-not=error:
88# ERR1:      error: --compress-sections: parse error, not 'section-glob=[none|zlib|zstd]'
89
90# RUN: llvm-objcopy --compress-sections 'a[=zlib' a.o out 2>&1 | \
91# RUN:   FileCheck %s --check-prefix=ERR2 --implicit-check-not=error:
92# ERR2:      warning: invalid glob pattern, unmatched '['
93
94# RUN: not llvm-objcopy a.o out --compress-sections='.debug*=zlib-gabi' --compress-sections='.debug*=' 2>&1 | \
95# RUN:   FileCheck -check-prefix=ERR3 %s
96# ERR3:      error: invalid or unsupported --compress-sections format: .debug*=zlib-gabi
97
98# RUN: not llvm-objcopy a.o out --compress-sections='!.debug*=zlib' 2>&1 | \
99# RUN:   FileCheck -check-prefix=ERR4 %s
100# ERR4:      error: --compress-sections: negative pattern is unsupported
101
102.globl _start
103_start:
104  ret
105
106.section foo0,"a"
107.balign 8
108.quad .text-.
109.quad .text-.
110.section foo1,"a"
111.balign 8
112.quad .text-.
113.quad .text-.
114.section nonalloc0,""
115.balign 8
116.quad .text+1
117.quad .text+2
118sym0:
119.section nonalloc1,""
120.balign 8
121.quad 42
122sym1:
123
124.section .debug_str,"MS",@progbits,1
125.Linfo_string0:
126  .asciz "AAAAAAAAAAAAAAAAAAAAAAAAAAA"
127.Linfo_string1:
128  .asciz "BBBBBBBBBBBBBBBBBBBBBBBBBBB"
129