Skip to content

Commit 76a441a

Browse files
committed
[MC] Fix compression header size check in ELF writer
This is #66888 with a test. For MC we only use a zstd test, as zlib has a lot of versions/forks with different speed/size tradeoff, which would make the test more brittle. If clang/test/Misc/cc1as-compress.s turns out to be brittle, we could make the string longer.
1 parent fd2d5ad commit 76a441a

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

clang/test/Misc/cc1as-compress.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
// ZSTD: 0000 02000000 00000000
1313

1414
.section .debug_str,"MS",@progbits,1
15-
.asciz "perfectly compressable data sample *****************************************"
15+
.asciz "perfectly compressable data sample ******************************************"

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ bool ELFWriter::maybeWriteCompression(
843843
uint32_t ChType, uint64_t Size,
844844
SmallVectorImpl<uint8_t> &CompressedContents, Align Alignment) {
845845
uint64_t HdrSize =
846-
is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
846+
is64Bit() ? sizeof(ELF::Elf64_Chdr) : sizeof(ELF::Elf32_Chdr);
847847
if (Size <= HdrSize + CompressedContents.size())
848848
return false;
849849
// Platform specific header is followed by compressed data.

llvm/test/MC/ELF/compress-debug-sections-zstd.s

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
# SEC: .debug_line PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00 C 0 0 8
1111
# SEC: .debug_abbrev PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00 0 0 1
1212
# SEC: .debug_info PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00 0 0 1
13-
# SEC: .debug_str PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 01 MSC 0 0 8
13+
## .debug_str is uncompressed becuase sizeof(Elf64_Chdr)+len(compressed) >= len(uncompressed).
14+
# SEC: .debug_str PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 01 MS 0 0 1
1415
# SEC: .debug_frame PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00 C 0 0 8
1516

1617
# CHECK: Contents of section .debug_line
@@ -23,6 +24,12 @@
2324
# RUN: llvm-objcopy --decompress-debug-sections %t %t.decom
2425
# RUN: cmp %t.uncom %t.decom
2526

27+
## .debug_str is compressed becuase sizeof(Elf32_Chdr)+len(compressed) < len(uncompressed).
28+
# RUN: llvm-mc -filetype=obj -triple=i386 --defsym I386=1 -compress-debug-sections=zstd --defsym LONG=1 %s -o %t1
29+
# RUN: llvm-readelf -S %t1 | FileCheck %s --check-prefix=SEC1
30+
31+
# SEC1: .debug_str PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 01 MSC 0 0 4
32+
2633
## Don't compress a section not named .debug_*.
2734
.section .nonalloc,"",@progbits
2835
.rept 50

0 commit comments

Comments
 (0)