Skip to content

Commit

Permalink
[MC] Fix compression header size check in ELF writer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MaskRay committed Nov 17, 2023
1 parent fd2d5ad commit 76a441a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion clang/test/Misc/cc1as-compress.s
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
// ZSTD: 0000 02000000 00000000

.section .debug_str,"MS",@progbits,1
.asciz "perfectly compressable data sample *****************************************"
.asciz "perfectly compressable data sample ******************************************"
2 changes: 1 addition & 1 deletion llvm/lib/MC/ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ bool ELFWriter::maybeWriteCompression(
uint32_t ChType, uint64_t Size,
SmallVectorImpl<uint8_t> &CompressedContents, Align Alignment) {
uint64_t HdrSize =
is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
is64Bit() ? sizeof(ELF::Elf64_Chdr) : sizeof(ELF::Elf32_Chdr);
if (Size <= HdrSize + CompressedContents.size())
return false;
// Platform specific header is followed by compressed data.
Expand Down
9 changes: 8 additions & 1 deletion llvm/test/MC/ELF/compress-debug-sections-zstd.s
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
# SEC: .debug_line PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00 C 0 0 8
# SEC: .debug_abbrev PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00 0 0 1
# SEC: .debug_info PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00 0 0 1
# SEC: .debug_str PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 01 MSC 0 0 8
## .debug_str is uncompressed becuase sizeof(Elf64_Chdr)+len(compressed) >= len(uncompressed).
# SEC: .debug_str PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 01 MS 0 0 1
# SEC: .debug_frame PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00 C 0 0 8

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

## .debug_str is compressed becuase sizeof(Elf32_Chdr)+len(compressed) < len(uncompressed).
# RUN: llvm-mc -filetype=obj -triple=i386 --defsym I386=1 -compress-debug-sections=zstd --defsym LONG=1 %s -o %t1
# RUN: llvm-readelf -S %t1 | FileCheck %s --check-prefix=SEC1

# SEC1: .debug_str PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 01 MSC 0 0 4

## Don't compress a section not named .debug_*.
.section .nonalloc,"",@progbits
.rept 50
Expand Down

0 comments on commit 76a441a

Please sign in to comment.