-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ELF] Add --compress-section to compress matched non-SHF_ALLOC sections #84855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MaskRay
merged 4 commits into
main
from
users/MaskRay/spr/elf-add-compress-nonalloc-section
Mar 12, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# REQUIRES: x86, zlib | ||
|
||
# RUN: rm -rf %t && mkdir %t && cd %t | ||
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o a.o | ||
# RUN: ld.lld -pie a.o --compress-sections .strtab=zlib --compress-sections .symtab=zlib -o out | ||
# RUN: llvm-readelf -Ss -x .strtab out 2>&1 | FileCheck %s | ||
|
||
# CHECK: nonalloc0 PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 0 0 1 | ||
# CHECK: .symtab SYMTAB 0000000000000000 [[#%x,]] [[#%x,]] 18 C 12 3 1 | ||
# CHECK-NEXT: .shstrtab STRTAB 0000000000000000 [[#%x,]] [[#%x,]] 00 0 0 1 | ||
# CHECK-NEXT: .strtab STRTAB 0000000000000000 [[#%x,]] [[#%x,]] 00 C 0 0 1 | ||
|
||
## TODO Add compressed SHT_STRTAB/SHT_SYMTAB support to llvm-readelf | ||
# CHECK: warning: {{.*}}: unable to get the string table for the SHT_SYMTAB section: SHT_STRTAB string table section | ||
|
||
# CHECK: Hex dump of section '.strtab': | ||
# CHECK-NEXT: 01000000 00000000 1a000000 00000000 | ||
# CHECK-NEXT: 01000000 00000000 {{.*}} | ||
|
||
# RUN: not ld.lld -shared a.o --compress-sections .dynstr=zlib 2>&1 | FileCheck %s --check-prefix=ERR-ALLOC | ||
# ERR-ALLOC: error: --compress-sections: section '.dynstr' with the SHF_ALLOC flag cannot be compressed | ||
|
||
.globl _start, g0, g1 | ||
_start: | ||
l0: | ||
g0: | ||
g1: | ||
|
||
.section nonalloc0,"" | ||
.quad .text+1 | ||
.quad .text+2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# REQUIRES: x86, zlib, zstd | ||
|
||
# RUN: rm -rf %t && mkdir %t && cd %t | ||
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o a.o | ||
# RUN: ld.lld -pie a.o -o out --compress-sections '*0=zlib' --compress-sections '*0=none' --compress-sections 'nomatch=none' | ||
# RUN: llvm-readelf -SrsX out | FileCheck %s --check-prefix=CHECK1 | ||
|
||
# CHECK1: Name Type Address Off Size ES Flg Lk Inf Al | ||
# CHECK1: foo0 PROGBITS [[#%x,FOO0:]] [[#%x,]] [[#%x,]] 00 A 0 0 8 | ||
# CHECK1-NEXT: foo1 PROGBITS [[#%x,FOO1:]] [[#%x,]] [[#%x,]] 00 A 0 0 8 | ||
# CHECK1-NEXT: .text PROGBITS [[#%x,TEXT:]] [[#%x,]] [[#%x,]] 00 AX 0 0 4 | ||
# CHECK1: nonalloc0 PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 0 0 8 | ||
# CHECK1-NEXT: nonalloc1 PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 0 0 8 | ||
# CHECK1-NEXT: .debug_str PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 01 MS 0 0 1 | ||
|
||
# CHECK1: 0000000000000010 0 NOTYPE LOCAL DEFAULT [[#]] (nonalloc0) sym0 | ||
# CHECK1: 0000000000000008 0 NOTYPE LOCAL DEFAULT [[#]] (nonalloc1) sym1 | ||
|
||
# RUN: ld.lld -pie a.o --compress-sections '*c0=zlib' --compress-sections .debug_str=zstd -o out2 | ||
# RUN: llvm-readelf -SrsX -x nonalloc0 -x .debug_str out2 | FileCheck %s --check-prefix=CHECK2 | ||
|
||
# CHECK2: Name Type Address Off Size ES Flg Lk Inf Al | ||
# CHECK2: foo0 PROGBITS [[#%x,FOO0:]] [[#%x,]] [[#%x,]] 00 A 0 0 8 | ||
# CHECK2-NEXT: foo1 PROGBITS [[#%x,FOO1:]] [[#%x,]] [[#%x,]] 00 A 0 0 8 | ||
# CHECK2-NEXT: .text PROGBITS [[#%x,TEXT:]] [[#%x,]] [[#%x,]] 00 AX 0 0 4 | ||
# CHECK2: nonalloc0 PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 C 0 0 1 | ||
# CHECK2-NEXT: nonalloc1 PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 0 0 8 | ||
# CHECK2-NEXT: .debug_str PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 01 MSC 0 0 1 | ||
|
||
# CHECK2: 0000000000000010 0 NOTYPE LOCAL DEFAULT [[#]] (nonalloc0) sym0 | ||
# CHECK2: 0000000000000008 0 NOTYPE LOCAL DEFAULT [[#]] (nonalloc1) sym1 | ||
|
||
# CHECK2: Hex dump of section 'nonalloc0': | ||
## zlib with ch_size=0x10 | ||
# CHECK2-NEXT: 01000000 00000000 10000000 00000000 | ||
# CHECK2-NEXT: 01000000 00000000 {{.*}} | ||
# CHECK2: Hex dump of section '.debug_str': | ||
## zstd with ch_size=0x38 | ||
# CHECK2-NEXT: 02000000 00000000 38000000 00000000 | ||
# CHECK2-NEXT: 01000000 00000000 {{.*}} | ||
|
||
## --compress-debug-sections=none takes precedence. | ||
# RUN: ld.lld a.o --compress-debug-sections=none --compress-sections .debug_str=zstd -o out3 | ||
# RUN: llvm-readelf -S out3 | FileCheck %s --check-prefix=CHECK3 | ||
|
||
# CHECK3: .debug_str PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 01 MS 0 0 1 | ||
|
||
# RUN: not ld.lld a.o --compress-sections '*0=zlib' 2>&1 | \ | ||
# RUN: FileCheck %s --check-prefix=ERR-ALLOC --implicit-check-not=error: | ||
# ERR-ALLOC: error: --compress-sections: section 'foo0' with the SHF_ALLOC flag cannot be compressed | ||
|
||
# RUN: not ld.lld --compress-sections=foo a.o 2>&1 | \ | ||
# RUN: FileCheck %s --check-prefix=ERR1 --implicit-check-not=error: | ||
# ERR1: error: --compress-sections: parse error, not 'section-glob=[none|zlib|zstd]' | ||
|
||
# RUN: not ld.lld --compress-sections 'a[=zlib' a.o 2>&1 | \ | ||
# RUN: FileCheck %s --check-prefix=ERR2 --implicit-check-not=error: | ||
# ERR2: error: --compress-sections: invalid glob pattern, unmatched '[' | ||
|
||
# RUN: not ld.lld a.o --compress-sections='.debug*=zlib-gabi' --compress-sections='.debug*=' 2>&1 | \ | ||
# RUN: FileCheck -check-prefix=ERR3 %s | ||
# ERR3: unknown --compress-sections value: zlib-gabi | ||
# ERR3-NEXT: --compress-sections: parse error, not 'section-glob=[none|zlib|zstd]' | ||
|
||
.globl _start | ||
_start: | ||
ret | ||
|
||
.section foo0,"a" | ||
.balign 8 | ||
.quad .text-. | ||
.quad .text-. | ||
.section foo1,"a" | ||
.balign 8 | ||
.quad .text-. | ||
.quad .text-. | ||
.section nonalloc0,"" | ||
.balign 8 | ||
.quad .text+1 | ||
.quad .text+2 | ||
sym0: | ||
.section nonalloc1,"" | ||
.balign 8 | ||
.quad 42 | ||
sym1: | ||
|
||
.section .debug_str,"MS",@progbits,1 | ||
.Linfo_string0: | ||
.asciz "AAAAAAAAAAAAAAAAAAAAAAAAAAA" | ||
.Linfo_string1: | ||
.asciz "BBBBBBBBBBBBBBBBBBBBBBBBBBB" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# REQUIRES: x86, zlib | ||
|
||
# RUN: rm -rf %t && split-file %s %t && cd %t | ||
# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o | ||
# RUN: ld.lld -T a.lds a.o --compress-sections nonalloc=zlib --compress-sections str=zlib -o out | ||
# RUN: llvm-readelf -SsXz -p str out | FileCheck %s | ||
|
||
# CHECK: Name Type Address Off Size ES Flg Lk Inf Al | ||
# CHECK: nonalloc PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 C 0 0 1 | ||
# CHECK-NEXT: str PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 01 MSC 0 0 1 | ||
|
||
# CHECK: 0000000000000000 0 NOTYPE GLOBAL DEFAULT [[#]] (nonalloc) nonalloc_start | ||
# CHECK: 0000000000000023 0 NOTYPE GLOBAL DEFAULT [[#]] (nonalloc) nonalloc_end | ||
# CHECK: String dump of section 'str': | ||
# CHECK-NEXT: [ 0] AAA | ||
# CHECK-NEXT: [ 4] BBB | ||
|
||
## TODO The uncompressed size of 'nonalloc' is dependent on linker script | ||
## commands, which is not handled. We should report an error. | ||
# RUN: ld.lld -T b.lds a.o --compress-sections nonalloc=zlib | ||
|
||
#--- a.s | ||
.globl _start | ||
_start: | ||
ret | ||
|
||
.section nonalloc0,"" | ||
.balign 8 | ||
.quad .text | ||
.quad .text | ||
.section nonalloc1,"" | ||
.balign 8 | ||
.quad 42 | ||
|
||
.section str,"MS",@progbits,1 | ||
.asciz "AAA" | ||
.asciz "BBB" | ||
|
||
#--- a.lds | ||
SECTIONS { | ||
.text : { *(.text) } | ||
c = SIZEOF(.text); | ||
b = c+1; | ||
a = b+1; | ||
nonalloc : { | ||
nonalloc_start = .; | ||
## In general, using data commands is error-prone. This case is correct, though. | ||
*(nonalloc*) QUAD(SIZEOF(.text)) | ||
. += a; | ||
nonalloc_end = .; | ||
} | ||
str : { *(str) } | ||
} | ||
|
||
#--- b.lds | ||
SECTIONS { | ||
nonalloc : { *(nonalloc*) . += a; } | ||
.text : { *(.text) } | ||
a = b+1; | ||
b = c+1; | ||
c = SIZEOF(.text); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.