Skip to content

Commit 0794298

Browse files
committed
[llvm-objcopy] Add --compress-sections
--compress-sections is similar to --compress-debug-sections but applies to arbitrary sections. * `--compress-sections <section>=none`: decompress sections * `--compress-sections <section>=[zlib|zstd]`: compress sections with zlib/zstd Like `--remove-section`, the pattern is by default a glob, but a regex when --regex is specified. For `--remove-section` like options, `!` prevents matches and is not dependent on ordering (see `ELF/wildcard-syntax.test`). Since `--compress-sections a=zlib --compress-sections a=none` naturally allows overriding, having an order-independent `!` would be confusing. Therefore, `!` is disallowed. Sections within a segment are effectively immutable. Report an error for an attempt to (de)compress them. `SHF_ALLOC` sections in a relocatable file can be compressed, but linkers usually reject them. Note: Before this patch, a compressed relocation section is recognized as a `RelocationSectionBase` as well and `removeSections` `!ToRemove(*ToRelSec)` may incorrectly interpret a `CompressedSections` as `RelocationSectionBase`, leading to ubsan failure for the new test. Fix this by setting `OriginalFlags` in CompressedSection::CompressedSection. Link: https://discourse.llvm.org/t/rfc-compress-arbitrary-sections-with-ld-lld-compress-sections/71674 Pull Request: #85036
1 parent 302d0f3 commit 0794298

File tree

10 files changed

+283
-8
lines changed

10 files changed

+283
-8
lines changed

llvm/docs/CommandGuide/llvm-objcopy.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ them.
309309
Compress DWARF debug sections in the output, using the specified format.
310310
Supported formats are ``zlib`` and ``zstd``. Use ``zlib`` if ``<format>`` is omitted.
311311

312+
.. option:: --compress-sections <section>=<format>
313+
314+
Compress or decompress sections matched by ``<section>`` using the specified
315+
format. Supported formats are ``zlib`` and ``zstd``. Specify ``none`` for
316+
decompression. When a section is matched by multiple options, the last one
317+
wins. A wildcard ``<section>`` starting with '!' is disallowed.
318+
Sections within a segment cannot be (de)compressed.
319+
312320
.. option:: --decompress-debug-sections
313321

314322
Decompress any compressed DWARF debug sections in the output.

llvm/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ Changes to the LLVM tools
186186
for ELF input to skip the specified symbols when executing other options
187187
that can change a symbol's name, binding or visibility.
188188

189+
* llvm-objcopy now supports ``--compress-sections`` to compress or decompress
190+
arbitrary sections not within a segment.
191+
(`#85036 <https://github.com/llvm/llvm-project/pull/85036>`_.)
192+
189193
* llvm-profgen now supports COFF+DWARF binaries. This enables Sample-based PGO
190194
on Windows using Intel VTune's SEP. For details on usage, see the `end-user
191195
documentation for SPGO

llvm/include/llvm/ObjCopy/CommonConfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ struct CommonConfig {
262262
bool DecompressDebugSections = false;
263263

264264
DebugCompressionType CompressionType = DebugCompressionType::None;
265+
266+
SmallVector<std::pair<NameMatcher, llvm::DebugCompressionType>, 0>
267+
compressSections;
265268
};
266269

267270
} // namespace objcopy

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,23 +215,41 @@ static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
215215
}
216216

217217
Error Object::compressOrDecompressSections(const CommonConfig &Config) {
218-
// Build a list of the debug sections we are going to replace.
219-
// We can't call `AddSection` while iterating over sections,
218+
// Build a list of sections we are going to replace.
219+
// We can't call `addSection` while iterating over sections,
220220
// because it would mutate the sections array.
221221
SmallVector<std::pair<SectionBase *, std::function<SectionBase *()>>, 0>
222222
ToReplace;
223223
for (SectionBase &Sec : sections()) {
224-
if ((Sec.Flags & SHF_ALLOC) || !StringRef(Sec.Name).starts_with(".debug"))
224+
std::optional<DebugCompressionType> CType;
225+
for (auto &[Matcher, T] : Config.compressSections)
226+
if (Matcher.matches(Sec.Name))
227+
CType = T;
228+
// Handle --compress-debug-sections and --decompress-debug-sections, which
229+
// apply to non-ALLOC debug sections.
230+
if (!(Sec.Flags & SHF_ALLOC) && StringRef(Sec.Name).starts_with(".debug")) {
231+
if (Config.CompressionType != DebugCompressionType::None)
232+
CType = Config.CompressionType;
233+
else if (Config.DecompressDebugSections)
234+
CType = DebugCompressionType::None;
235+
}
236+
if (!CType)
225237
continue;
238+
239+
if (Sec.ParentSegment)
240+
return createStringError(
241+
errc::invalid_argument,
242+
"section '" + Sec.Name +
243+
"' within a segment cannot be (de)compressed");
244+
226245
if (auto *CS = dyn_cast<CompressedSection>(&Sec)) {
227-
if (Config.DecompressDebugSections) {
246+
if (*CType == DebugCompressionType::None)
228247
ToReplace.emplace_back(
229248
&Sec, [=] { return &addSection<DecompressedSection>(*CS); });
230-
}
231-
} else if (Config.CompressionType != DebugCompressionType::None) {
232-
ToReplace.emplace_back(&Sec, [&, S = &Sec] {
249+
} else if (*CType != DebugCompressionType::None) {
250+
ToReplace.emplace_back(&Sec, [=, S = &Sec] {
233251
return &addSection<CompressedSection>(
234-
CompressedSection(*S, Config.CompressionType, Is64Bits));
252+
CompressedSection(*S, *CType, Is64Bits));
235253
});
236254
}
237255
}

llvm/lib/ObjCopy/ELF/ELFObject.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ CompressedSection::CompressedSection(const SectionBase &Sec,
548548
CompressedData);
549549

550550
Flags |= ELF::SHF_COMPRESSED;
551+
OriginalFlags |= ELF::SHF_COMPRESSED;
551552
size_t ChdrSize = Is64Bits ? sizeof(object::Elf_Chdr_Impl<object::ELF64LE>)
552553
: sizeof(object::Elf_Chdr_Impl<object::ELF32LE>);
553554
Size = ChdrSize + CompressedData.size();
@@ -2161,6 +2162,10 @@ Error Object::removeSections(
21612162
std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
21622163
if (ToRemove(*Sec))
21632164
return false;
2165+
// TODO: A compressed relocation section may be recognized as
2166+
// RelocationSectionBase. We don't want such a section to be removed.
2167+
if (isa<CompressedSection>(Sec))
2168+
return true;
21642169
if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
21652170
if (auto ToRelSec = RelSec->getSection())
21662171
return !ToRemove(*ToRelSec);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Disallow (de)compression for sections within a segment as they are
2+
## effectively immutable.
3+
# RUN: rm -rf %t && mkdir %t && cd %t
4+
# RUN: yaml2obj %s -o a
5+
# RUN: not llvm-objcopy a /dev/null --compress-sections .text=zlib 2>&1 | FileCheck %s --implicit-check-not=error:
6+
7+
# CHECK: error: 'a': section '.text' within a segment cannot be (de)compressed
8+
9+
# RUN: not llvm-objcopy a /dev/null --compress-sections foo=none 2>&1 | FileCheck %s --check-prefix=CHECK2 --implicit-check-not=error:
10+
11+
# CHECK2: error: 'a': section 'foo' within a segment cannot be (de)compressed
12+
13+
## There is an error even if 'foo' is already compressed with zlib.
14+
# RUN: not llvm-objcopy a /dev/null --compress-sections foo=zlib 2>&1 | FileCheck %s --check-prefix=CHECK3 --implicit-check-not=error:
15+
16+
# CHECK3: error: 'a': section 'foo' within a segment cannot be (de)compressed
17+
18+
--- !ELF
19+
FileHeader:
20+
Class: ELFCLASS64
21+
Data: ELFDATA2LSB
22+
Type: ET_EXEC
23+
Machine: EM_X86_64
24+
ProgramHeaders:
25+
- Type: PT_LOAD
26+
FirstSec: .text
27+
LastSec: foo
28+
Align: 0x1000
29+
Offset: 0x1000
30+
Sections:
31+
- Name: .text
32+
Type: SHT_PROGBITS
33+
Offset: 0x1000
34+
Content: C3
35+
- Name: foo
36+
Type: SHT_PROGBITS
37+
Flags: [ SHF_COMPRESSED ]
38+
Content: 010000000000000040000000000000000100000000000000789cd36280002d3269002f800151
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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
118+
sym0:
119+
.section nonalloc1,""
120+
.balign 8
121+
.quad 42
122+
sym1:
123+
124+
.section .debug_str,"MS",@progbits,1
125+
.Linfo_string0:
126+
.asciz "AAAAAAAAAAAAAAAAAAAAAAAAAAA"
127+
.Linfo_string1:
128+
.asciz "BBBBBBBBBBBBBBBBBBBBBBBBBBB"

llvm/test/tools/llvm-objcopy/ELF/decompress-sections.test

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,42 @@
44
# RUN: yaml2obj %s -o %t
55
# RUN: llvm-objcopy --decompress-debug-sections %t %t.de
66
# RUN: llvm-readelf -S %t.de | FileCheck %s
7+
# RUN: llvm-objcopy --compress-sections '*nonalloc=none' --compress-sections .debugx=none %t %t.1.de
8+
# RUN: cmp %t.de %t.1.de
79

810
# CHECK: Name Type Address Off Size ES Flg Lk Inf Al
911
# CHECK: .debug_alloc PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 AC 0 0 0
1012
# CHECK-NEXT: .debug_nonalloc PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 0 0 1
1113
# CHECK-NEXT: .debugx PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 0 0 1
1214
# CHECK-NEXT: nodebug PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 C 0 0 0
1315

16+
# RUN: llvm-objcopy --compress-sections '.debug*=none' %t %t2.de
17+
# RUN: llvm-readelf -S -x .debug_alloc -x .debug_nonalloc -x .debugx %t2.de | FileCheck %s --check-prefix=CHECK2
18+
19+
# CHECK2: Name Type Address Off Size ES Flg Lk Inf Al
20+
# CHECK2: .debug_alloc PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 A 0 0 1
21+
# CHECK2-NEXT: .debug_nonalloc PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 0 0 1
22+
# CHECK2-NEXT: .debugx PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 0 0 1
23+
# CHECK2-NEXT: nodebug PROGBITS 0000000000000000 [[#%x,]] [[#%x,]] 00 C 0 0 0
24+
25+
# CHECK2: Hex dump of section '.debug_alloc':
26+
# CHECK2-NEXT: 0x00000000 2a000000 00000000 2a000000 00000000 *.......*.......
27+
# CHECK2-NEXT: 0x00000010 2a000000 00000000 2a000000 00000000 *.......*.......
28+
# CHECK2-NEXT: 0x00000020 2a000000 00000000 2a000000 00000000 *.......*.......
29+
# CHECK2-NEXT: 0x00000030 2a000000 00000000 2a000000 00000000 *.......*.......
30+
# CHECK2-EMPTY:
31+
# CHECK2: Hex dump of section '.debug_nonalloc':
32+
# CHECK2-NEXT: 0x00000000 2a000000 00000000 2a000000 00000000 *.......*.......
33+
# CHECK2-NEXT: 0x00000010 2a000000 00000000 2a000000 00000000 *.......*.......
34+
# CHECK2-NEXT: 0x00000020 2a000000 00000000 2a000000 00000000 *.......*.......
35+
# CHECK2-NEXT: 0x00000030 2a000000 00000000 2a000000 00000000 *.......*.......
36+
# CHECK2-EMPTY:
37+
# CHECK2-NEXT: Hex dump of section '.debugx':
38+
# CHECK2-NEXT: 0x00000000 2a000000 00000000 2a000000 00000000 *.......*.......
39+
# CHECK2-NEXT: 0x00000010 2a000000 00000000 2a000000 00000000 *.......*.......
40+
# CHECK2-NEXT: 0x00000020 2a000000 00000000 2a000000 00000000 *.......*.......
41+
# CHECK2-NEXT: 0x00000030 2a000000 00000000 2a000000 00000000 *.......*.......
42+
1443
--- !ELF
1544
FileHeader:
1645
Class: ELFCLASS64

llvm/tools/llvm-objcopy/ObjcopyOptions.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,42 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
736736
return createStringError(errc::invalid_argument, Reason);
737737
}
738738

739+
for (const auto *A : InputArgs.filtered(OBJCOPY_compress_sections)) {
740+
SmallVector<StringRef, 0> Fields;
741+
StringRef(A->getValue()).split(Fields, '=');
742+
if (Fields.size() != 2 || Fields[1].empty()) {
743+
return createStringError(
744+
errc::invalid_argument,
745+
A->getSpelling() +
746+
": parse error, not 'section-glob=[none|zlib|zstd]'");
747+
}
748+
749+
auto Type = StringSwitch<DebugCompressionType>(Fields[1])
750+
.Case("zlib", DebugCompressionType::Zlib)
751+
.Case("zstd", DebugCompressionType::Zstd)
752+
.Default(DebugCompressionType::None);
753+
if (Type == DebugCompressionType::None && Fields[1] != "none") {
754+
return createStringError(
755+
errc::invalid_argument,
756+
"invalid or unsupported --compress-sections format: %s",
757+
A->getValue());
758+
}
759+
760+
auto &P = Config.compressSections.emplace_back();
761+
P.second = Type;
762+
auto Matcher =
763+
NameOrPattern::create(Fields[0], SectionMatchStyle, ErrorCallback);
764+
// =none allows overriding a previous =zlib or =zstd. Reject negative
765+
// patterns, which would be confusing.
766+
if (Matcher && !Matcher->isPositiveMatch()) {
767+
return createStringError(
768+
errc::invalid_argument,
769+
"--compress-sections: negative pattern is unsupported");
770+
}
771+
if (Error E = P.first.addMatcher(std::move(Matcher)))
772+
return std::move(E);
773+
}
774+
739775
Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
740776
// The gnu_debuglink's target is expected to not change or else its CRC would
741777
// become invalidated and get rejected. We can avoid recalculating the

llvm/tools/llvm-objcopy/ObjcopyOpts.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ def : Flag<["--"], "compress-debug-sections">, Alias<compress_debug_sections>,
3535
AliasArgs<["zlib"]>;
3636
def decompress_debug_sections : Flag<["--"], "decompress-debug-sections">,
3737
HelpText<"Decompress DWARF debug sections">;
38+
defm compress_sections
39+
: Eq<"compress-sections",
40+
"Compress or decompress sections using specified format. Supported "
41+
"formats: zlib, zstd. Specify 'none' for decompression">,
42+
MetaVarName<"<section-glob>=<format>">;
43+
3844
defm split_dwo
3945
: Eq<"split-dwo", "Equivalent to --extract-dwo and <dwo-file> as the output file and no other options, "
4046
"and then --strip-dwo on the input file">,

0 commit comments

Comments
 (0)